fix: sync delivery routing context

Co-authored-by: adam91holt <adam91holt@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-17 06:01:30 +00:00
parent e59d8c5436
commit 285ed8bac3
10 changed files with 208 additions and 51 deletions

View File

@@ -76,6 +76,7 @@ describe("deliverAgentCommandResult", () => {
} as unknown as RuntimeEnv;
const sessionEntry = {
lastAccountId: "legacy",
lastChannel: "whatsapp",
} as SessionEntry;
const result = {
payloads: [{ text: "hi" }],
@@ -141,4 +142,40 @@ describe("deliverAgentCommandResult", () => {
expect.objectContaining({ accountId: undefined }),
);
});
it("skips session accountId when channel differs", async () => {
const cfg = {} as ClawdbotConfig;
const deps = {} as CliDeps;
const runtime = {
log: vi.fn(),
error: vi.fn(),
} as unknown as RuntimeEnv;
const sessionEntry = {
lastAccountId: "legacy",
lastChannel: "telegram",
} as SessionEntry;
const result = {
payloads: [{ text: "hi" }],
meta: {},
};
const { deliverAgentCommandResult } = await import("./agent/delivery.js");
await deliverAgentCommandResult({
cfg,
deps,
runtime,
opts: {
message: "hello",
deliver: true,
channel: "whatsapp",
},
sessionEntry,
result,
payloads: result.payloads,
});
expect(mocks.resolveOutboundTarget).toHaveBeenCalledWith(
expect.objectContaining({ accountId: undefined, channel: "whatsapp" }),
);
});
});

View File

@@ -30,12 +30,14 @@ function resolveDeliveryAccountId(params: {
opts: AgentCommandOpts;
sessionEntry?: SessionEntry;
targetMode: ChannelOutboundTargetMode;
deliveryChannel?: string;
}) {
const sessionOrigin = deliveryContextFromSession(params.sessionEntry);
return (
normalizeAccountId(params.opts.accountId) ??
(params.targetMode === "implicit" ? normalizeAccountId(sessionOrigin?.accountId) : undefined)
);
const explicit = normalizeAccountId(params.opts.accountId);
if (explicit || params.targetMode !== "implicit") return explicit;
if (!params.deliveryChannel || isInternalMessageChannel(params.deliveryChannel)) return undefined;
if (sessionOrigin?.channel !== params.deliveryChannel) return undefined;
return normalizeAccountId(sessionOrigin?.accountId);
}
export async function deliverAgentCommandResult(params: {
@@ -61,7 +63,12 @@ export async function deliverAgentCommandResult(params: {
const targetMode: ChannelOutboundTargetMode =
opts.deliveryTargetMode ?? (opts.to ? "explicit" : "implicit");
const resolvedAccountId = resolveDeliveryAccountId({ opts, sessionEntry, targetMode });
const resolvedAccountId = resolveDeliveryAccountId({
opts,
sessionEntry,
targetMode,
deliveryChannel,
});
const resolvedTarget =
deliver && isDeliveryChannelKnown && deliveryChannel
? resolveOutboundTarget({