refactor: share conversation target normalization

This commit is contained in:
Peter Steinberger
2026-04-19 05:02:03 +01:00
parent 2a14f76964
commit 45381135df

View File

@@ -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(