fix: thread accountId through subagent announce delivery

Co-authored-by: Adam Holt <adam91holt@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-17 02:45:07 +00:00
parent dbf8829283
commit 0291105913
9 changed files with 209 additions and 22 deletions

View File

@@ -0,0 +1,64 @@
import { describe, expect, it, vi } from "vitest";
import type { CliDeps } from "../cli/deps.js";
import type { ClawdbotConfig } from "../config/config.js";
import type { RuntimeEnv } from "../runtime.js";
import type { SessionEntry } from "../config/sessions.js";
const mocks = vi.hoisted(() => ({
deliverOutboundPayloads: vi.fn(async () => []),
getChannelPlugin: vi.fn(() => ({})),
resolveOutboundTarget: vi.fn(() => ({ ok: true as const, to: "+15551234567" })),
}));
vi.mock("../channels/plugins/index.js", () => ({
getChannelPlugin: mocks.getChannelPlugin,
normalizeChannelId: (value: string) => value,
}));
vi.mock("../infra/outbound/deliver.js", () => ({
deliverOutboundPayloads: mocks.deliverOutboundPayloads,
}));
vi.mock("../infra/outbound/targets.js", () => ({
resolveOutboundTarget: mocks.resolveOutboundTarget,
}));
describe("deliverAgentCommandResult", () => {
it("prefers explicit accountId for outbound delivery", async () => {
const cfg = {} as ClawdbotConfig;
const deps = {} as CliDeps;
const runtime = {
log: vi.fn(),
error: vi.fn(),
} as unknown as RuntimeEnv;
const sessionEntry = {
lastAccountId: "default",
} 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",
accountId: "kev",
to: "+15551234567",
},
sessionEntry,
result,
payloads: result.payloads,
});
expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith(
expect.objectContaining({ accountId: "kev" }),
);
});
});

View File

@@ -19,6 +19,7 @@ import {
isInternalMessageChannel,
resolveGatewayMessageChannel,
} from "../../utils/message-channel.js";
import { normalizeAccountId } from "../../utils/account-id.js";
import type { AgentCommandOpts } from "./types.js";
type RunResult = Awaited<
@@ -49,11 +50,8 @@ export async function deliverAgentCommandResult(params: {
const targetMode: ChannelOutboundTargetMode =
opts.deliveryTargetMode ?? (opts.to ? "explicit" : "implicit");
const resolvedAccountId =
typeof opts.accountId === "string" && opts.accountId.trim()
? opts.accountId.trim()
: targetMode === "implicit"
? sessionEntry?.lastAccountId
: undefined;
normalizeAccountId(opts.accountId) ??
(targetMode === "implicit" ? normalizeAccountId(sessionEntry?.lastAccountId) : undefined);
const resolvedTarget =
deliver && isDeliveryChannelKnown && deliveryChannel
? resolveOutboundTarget({