fix: preserve session origin account metadata in announce routing

This commit is contained in:
Tak Hoffman 2026-03-27 20:06:44 -05:00
parent 59cd79d37f
commit f0d5d7a33a
No known key found for this signature in database
4 changed files with 43 additions and 2 deletions

View File

@ -38,15 +38,21 @@ export async function resolveAnnounceTarget(params: {
match?.deliveryContext && typeof match.deliveryContext === "object"
? (match.deliveryContext as Record<string, unknown>)
: undefined;
const origin =
match?.origin && typeof match.origin === "object"
? (match.origin as Record<string, unknown>)
: undefined;
const channel =
(typeof deliveryContext?.channel === "string" ? deliveryContext.channel : undefined) ??
(typeof match?.lastChannel === "string" ? match.lastChannel : undefined);
(typeof match?.lastChannel === "string" ? match.lastChannel : undefined) ??
(typeof origin?.provider === "string" ? origin.provider : 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);
(typeof match?.lastAccountId === "string" ? match.lastAccountId : undefined) ??
(typeof origin?.accountId === "string" ? origin.accountId : undefined);
if (channel && to) {
return { channel, to, accountId };
}

View File

@ -55,6 +55,7 @@ export type SessionListRow = {
channel: string;
origin?: {
provider?: string;
accountId?: string;
};
spawnedBy?: string;
label?: string;

View File

@ -201,6 +201,15 @@ export function createSessionsListTool(opts?: {
key: displayKey,
kind,
channel: derivedChannel,
origin:
originChannel ||
(typeof entryOrigin?.accountId === "string" ? entryOrigin.accountId : undefined)
? {
provider: originChannel,
accountId:
typeof entryOrigin?.accountId === "string" ? entryOrigin.accountId : undefined,
}
: undefined,
spawnedBy:
typeof entry.spawnedBy === "string"
? resolveDisplaySessionKey({

View File

@ -272,6 +272,31 @@ describe("resolveAnnounceTarget", () => {
expect(first).toBeDefined();
expect(first?.method).toBe("sessions.list");
});
it("falls back to origin provider and accountId from sessions.list when legacy route fields are absent", async () => {
callGatewayMock.mockResolvedValueOnce({
sessions: [
{
key: "agent:main:whatsapp:group:123@g.us",
origin: {
provider: "whatsapp",
accountId: "work",
},
lastTo: "123@g.us",
},
],
});
const target = await resolveAnnounceTarget({
sessionKey: "agent:main:whatsapp:group:123@g.us",
displayKey: "agent:main:whatsapp:group:123@g.us",
});
expect(target).toEqual({
channel: "whatsapp",
to: "123@g.us",
accountId: "work",
});
});
});
describe("sessions_list gating", () => {