fix: use session origin delivery metadata in outbound targets

This commit is contained in:
Tak Hoffman
2026-03-27 20:01:25 -05:00
parent 8222d3a83a
commit a9e9c7cbfd
2 changed files with 37 additions and 3 deletions

View File

@@ -236,6 +236,34 @@ describe("resolveSessionDeliveryTarget", () => {
});
});
it("uses origin provider and accountId when legacy last route fields are absent", () => {
const resolved = resolveSessionDeliveryTarget({
entry: {
sessionId: "sess-origin-route",
updatedAt: 1,
lastTo: " +1555 ",
origin: {
provider: " whatsapp ",
accountId: " acct-origin ",
},
},
requestedChannel: "last",
});
expect(resolved).toEqual({
channel: "whatsapp",
to: "+1555",
accountId: "acct-origin",
threadId: undefined,
threadIdExplicit: false,
mode: "implicit",
lastChannel: "whatsapp",
lastTo: "+1555",
lastAccountId: "acct-origin",
lastThreadId: undefined,
});
});
it("prefers explicit targets without reusing lastTo", () => {
const resolved = resolveSessionDeliveryTarget({
entry: {

View File

@@ -14,6 +14,11 @@ export type DeliveryContextSessionSource = {
lastTo?: string;
lastAccountId?: string;
lastThreadId?: string | number;
origin?: {
provider?: string;
accountId?: string;
threadId?: string | number;
};
deliveryContext?: DeliveryContext;
};
@@ -165,17 +170,18 @@ export function normalizeSessionDeliveryFields(source?: DeliveryContextSessionSo
}
export function deliveryContextFromSession(
entry?: DeliveryContextSessionSource & { origin?: { threadId?: string | number } },
entry?: DeliveryContextSessionSource,
): DeliveryContext | undefined {
if (!entry) {
return undefined;
}
const source: DeliveryContextSessionSource = {
channel: entry.channel,
channel: entry.channel ?? entry.origin?.provider,
lastChannel: entry.lastChannel,
lastTo: entry.lastTo,
lastAccountId: entry.lastAccountId,
lastAccountId: entry.lastAccountId ?? entry.origin?.accountId,
lastThreadId: entry.lastThreadId ?? entry.deliveryContext?.threadId ?? entry.origin?.threadId,
origin: entry.origin,
deliveryContext: entry.deliveryContext,
};
return normalizeSessionDeliveryFields(source).deliveryContext;