fix: normalize delivery routing context

Co-authored-by: adam91holt <adam91holt@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-17 06:38:15 +00:00
parent eb8a0510e0
commit 65a8a93854
14 changed files with 220 additions and 110 deletions

View File

@@ -26,9 +26,11 @@ describe("resolveAnnounceTarget", () => {
sessions: [
{
key: "agent:main:whatsapp:group:123@g.us",
lastChannel: "whatsapp",
lastTo: "123@g.us",
lastAccountId: "work",
deliveryContext: {
channel: "whatsapp",
to: "123@g.us",
accountId: "work",
},
},
],
});

View File

@@ -33,9 +33,19 @@ export async function resolveAnnounceTarget(params: {
sessions.find((entry) => entry?.key === params.sessionKey) ??
sessions.find((entry) => entry?.key === params.displayKey);
const channel = typeof match?.lastChannel === "string" ? match.lastChannel : undefined;
const to = typeof match?.lastTo === "string" ? match.lastTo : undefined;
const accountId = typeof match?.lastAccountId === "string" ? match.lastAccountId : undefined;
const deliveryContext =
match?.deliveryContext && typeof match.deliveryContext === "object"
? (match.deliveryContext as Record<string, unknown>)
: undefined;
const channel =
(typeof deliveryContext?.channel === "string" ? deliveryContext.channel : undefined) ??
(typeof match?.lastChannel === "string" ? match.lastChannel : undefined);
const to =
(typeof deliveryContext?.to === "string" ? deliveryContext.to : undefined) ??
(typeof match?.lastTo === "string" ? match.lastTo : undefined);
const accountId =
(typeof deliveryContext?.accountId === "string" ? deliveryContext.accountId : undefined) ??
(typeof match?.lastAccountId === "string" ? match.lastAccountId : undefined);
if (channel && to) return { channel, to, accountId };
} catch {
// ignore

View File

@@ -167,9 +167,21 @@ export function createSessionsListTool(opts?: {
});
const entryChannel = typeof entry.channel === "string" ? entry.channel : undefined;
const lastChannel = typeof entry.lastChannel === "string" ? entry.lastChannel : undefined;
const deliveryContext =
entry.deliveryContext && typeof entry.deliveryContext === "object"
? (entry.deliveryContext as Record<string, unknown>)
: undefined;
const deliveryChannel =
typeof deliveryContext?.channel === "string" ? deliveryContext.channel : undefined;
const deliveryTo =
typeof deliveryContext?.to === "string" ? deliveryContext.to : undefined;
const deliveryAccountId =
typeof deliveryContext?.accountId === "string" ? deliveryContext.accountId : undefined;
const lastChannel =
deliveryChannel ?? (typeof entry.lastChannel === "string" ? entry.lastChannel : undefined);
const lastAccountId =
typeof entry.lastAccountId === "string" ? entry.lastAccountId : undefined;
deliveryAccountId ??
(typeof entry.lastAccountId === "string" ? entry.lastAccountId : undefined);
const derivedChannel = deriveChannel({
key,
kind,
@@ -201,7 +213,7 @@ export function createSessionsListTool(opts?: {
typeof entry.abortedLastRun === "boolean" ? entry.abortedLastRun : undefined,
sendPolicy: typeof entry.sendPolicy === "string" ? entry.sendPolicy : undefined,
lastChannel,
lastTo: typeof entry.lastTo === "string" ? entry.lastTo : undefined,
lastTo: deliveryTo ?? (typeof entry.lastTo === "string" ? entry.lastTo : undefined),
lastAccountId,
transcriptPath,
};