diff --git a/src/agents/tools/sessions-announce-target.ts b/src/agents/tools/sessions-announce-target.ts index 0edfafdb2e0..6a9ca29dc1e 100644 --- a/src/agents/tools/sessions-announce-target.ts +++ b/src/agents/tools/sessions-announce-target.ts @@ -38,15 +38,21 @@ export async function resolveAnnounceTarget(params: { match?.deliveryContext && typeof match.deliveryContext === "object" ? (match.deliveryContext as Record) : undefined; + const origin = + match?.origin && typeof match.origin === "object" + ? (match.origin as Record) + : 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 }; } diff --git a/src/agents/tools/sessions-helpers.ts b/src/agents/tools/sessions-helpers.ts index 33b3ebc5dd8..bcb45bfd703 100644 --- a/src/agents/tools/sessions-helpers.ts +++ b/src/agents/tools/sessions-helpers.ts @@ -55,6 +55,7 @@ export type SessionListRow = { channel: string; origin?: { provider?: string; + accountId?: string; }; spawnedBy?: string; label?: string; diff --git a/src/agents/tools/sessions-list-tool.ts b/src/agents/tools/sessions-list-tool.ts index 0b494a6d0ce..0df8955a96d 100644 --- a/src/agents/tools/sessions-list-tool.ts +++ b/src/agents/tools/sessions-list-tool.ts @@ -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({ diff --git a/src/agents/tools/sessions.test.ts b/src/agents/tools/sessions.test.ts index f324d43b775..4a96a04d1c6 100644 --- a/src/agents/tools/sessions.test.ts +++ b/src/agents/tools/sessions.test.ts @@ -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", () => {