forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelivery-context.ts
More file actions
85 lines (80 loc) · 2.9 KB
/
Copy pathdelivery-context.ts
File metadata and controls
85 lines (80 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { getChannelPlugin, normalizeChannelId } from "../channels/plugins/index.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import { normalizeMessageChannel } from "./message-channel.js";
export {
channelRouteFromDeliveryContext,
deliveryContextFromChannelRoute,
deliveryContextFromSession,
deliveryContextKey,
mergeDeliveryContext,
normalizeDeliveryContext,
normalizeSessionDeliveryFields,
} from "./delivery-context.shared.js";
export type { DeliveryContext, DeliveryContextSessionSource } from "./delivery-context.types.js";
type ConversationTargetParams = {
channel?: string;
conversationId?: string | number;
parentConversationId?: string | number;
};
function normalizeConversationId(value: string | number | undefined): string | undefined {
return typeof value === "number" && Number.isFinite(value)
? String(Math.trunc(value))
: typeof value === "string"
? normalizeOptionalString(value)
: undefined;
}
function normalizeConversationTargetParams(params: ConversationTargetParams): {
channel?: string;
conversationId?: string;
parentConversationId?: string;
} {
const channel =
typeof params.channel === "string"
? (normalizeMessageChannel(params.channel) ?? params.channel.trim())
: undefined;
const conversationId = normalizeConversationId(params.conversationId);
const parentConversationId = normalizeConversationId(params.parentConversationId);
return { channel, conversationId, parentConversationId };
}
export function formatConversationTarget(params: ConversationTargetParams): string | undefined {
const { channel, conversationId, parentConversationId } =
normalizeConversationTargetParams(params);
if (!channel || !conversationId) {
return undefined;
}
const pluginTarget = normalizeChannelId(channel)
? getChannelPlugin(normalizeChannelId(channel)!)?.messaging?.resolveDeliveryTarget?.({
conversationId,
parentConversationId,
})
: null;
if (pluginTarget?.to?.trim()) {
return pluginTarget.to.trim();
}
return `channel:${conversationId}`;
}
export function resolveConversationDeliveryTarget(params: {
channel?: string;
conversationId?: string | number;
parentConversationId?: string | number;
}): { to?: string; threadId?: string } {
const { channel, conversationId, parentConversationId } =
normalizeConversationTargetParams(params);
const pluginTarget =
channel && conversationId
? getChannelPlugin(
normalizeChannelId(channel) ?? channel,
)?.messaging?.resolveDeliveryTarget?.({
conversationId,
parentConversationId,
})
: null;
if (pluginTarget) {
return {
...(pluginTarget.to?.trim() ? { to: pluginTarget.to.trim() } : {}),
...(pluginTarget.threadId?.trim() ? { threadId: pluginTarget.threadId.trim() } : {}),
};
}
const to = formatConversationTarget(params);
return { to };
}