mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:20:43 +00:00
refactor: share conversation target normalization
This commit is contained in:
@@ -10,30 +10,40 @@ export {
|
||||
} from "./delivery-context.shared.js";
|
||||
export type { DeliveryContext, DeliveryContextSessionSource } from "./delivery-context.types.js";
|
||||
|
||||
export function formatConversationTarget(params: {
|
||||
type ConversationTargetParams = {
|
||||
channel?: string;
|
||||
conversationId?: string | number;
|
||||
parentConversationId?: string | number;
|
||||
}): string | undefined {
|
||||
};
|
||||
|
||||
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 =
|
||||
typeof params.conversationId === "number" && Number.isFinite(params.conversationId)
|
||||
? String(Math.trunc(params.conversationId))
|
||||
: typeof params.conversationId === "string"
|
||||
? normalizeOptionalString(params.conversationId)
|
||||
: 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 parentConversationId =
|
||||
typeof params.parentConversationId === "number" && Number.isFinite(params.parentConversationId)
|
||||
? String(Math.trunc(params.parentConversationId))
|
||||
: typeof params.parentConversationId === "string"
|
||||
? normalizeOptionalString(params.parentConversationId)
|
||||
: undefined;
|
||||
const pluginTarget = normalizeChannelId(channel)
|
||||
? getChannelPlugin(normalizeChannelId(channel)!)?.messaging?.resolveDeliveryTarget?.({
|
||||
conversationId,
|
||||
@@ -51,22 +61,8 @@ export function resolveConversationDeliveryTarget(params: {
|
||||
conversationId?: string | number;
|
||||
parentConversationId?: string | number;
|
||||
}): { to?: string; threadId?: string } {
|
||||
const channel =
|
||||
typeof params.channel === "string"
|
||||
? (normalizeMessageChannel(params.channel) ?? params.channel.trim())
|
||||
: undefined;
|
||||
const conversationId =
|
||||
typeof params.conversationId === "number" && Number.isFinite(params.conversationId)
|
||||
? String(Math.trunc(params.conversationId))
|
||||
: typeof params.conversationId === "string"
|
||||
? normalizeOptionalString(params.conversationId)
|
||||
: undefined;
|
||||
const parentConversationId =
|
||||
typeof params.parentConversationId === "number" && Number.isFinite(params.parentConversationId)
|
||||
? String(Math.trunc(params.parentConversationId))
|
||||
: typeof params.parentConversationId === "string"
|
||||
? normalizeOptionalString(params.parentConversationId)
|
||||
: undefined;
|
||||
const { channel, conversationId, parentConversationId } =
|
||||
normalizeConversationTargetParams(params);
|
||||
const pluginTarget =
|
||||
channel && conversationId
|
||||
? getChannelPlugin(
|
||||
|
||||
Reference in New Issue
Block a user