feat: add slack multi-account routing
parent
00c1403f5c
commit
8930ec32cb
@ -0,0 +1,85 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const handleSlackActionMock = vi.fn();
|
||||
|
||||
vi.mock("./slack-actions.js", () => ({
|
||||
handleSlackAction: (params: unknown, cfg: unknown) =>
|
||||
handleSlackActionMock(params, cfg),
|
||||
}));
|
||||
|
||||
import { createSlackTool } from "./slack-tool.js";
|
||||
|
||||
describe("slack tool", () => {
|
||||
beforeEach(() => {
|
||||
handleSlackActionMock.mockReset();
|
||||
handleSlackActionMock.mockResolvedValue({
|
||||
content: [],
|
||||
details: { ok: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("injects agentAccountId when accountId is missing", async () => {
|
||||
const tool = createSlackTool({
|
||||
agentAccountId: " Kev ",
|
||||
config: { slack: { accounts: { kev: {} } } },
|
||||
});
|
||||
|
||||
await tool.execute("call-1", {
|
||||
action: "sendMessage",
|
||||
to: "channel:C1",
|
||||
content: "hello",
|
||||
});
|
||||
|
||||
expect(handleSlackActionMock).toHaveBeenCalledTimes(1);
|
||||
const [params] = handleSlackActionMock.mock.calls[0] ?? [];
|
||||
expect(params).toMatchObject({ accountId: "kev" });
|
||||
});
|
||||
|
||||
it("keeps explicit accountId when provided", async () => {
|
||||
const tool = createSlackTool({
|
||||
agentAccountId: "kev",
|
||||
config: {},
|
||||
});
|
||||
|
||||
await tool.execute("call-2", {
|
||||
action: "sendMessage",
|
||||
to: "channel:C1",
|
||||
content: "hello",
|
||||
accountId: "rex",
|
||||
});
|
||||
|
||||
expect(handleSlackActionMock).toHaveBeenCalledTimes(1);
|
||||
const [params] = handleSlackActionMock.mock.calls[0] ?? [];
|
||||
expect(params).toMatchObject({ accountId: "rex" });
|
||||
});
|
||||
|
||||
it("does not inject accountId when agentAccountId is missing", async () => {
|
||||
const tool = createSlackTool({ config: {} });
|
||||
|
||||
await tool.execute("call-3", {
|
||||
action: "sendMessage",
|
||||
to: "channel:C1",
|
||||
content: "hello",
|
||||
});
|
||||
|
||||
expect(handleSlackActionMock).toHaveBeenCalledTimes(1);
|
||||
const [params] = handleSlackActionMock.mock.calls[0] ?? [];
|
||||
expect(params).not.toHaveProperty("accountId");
|
||||
});
|
||||
|
||||
it("does not inject unknown agentAccountId when not configured", async () => {
|
||||
const tool = createSlackTool({
|
||||
agentAccountId: "unknown",
|
||||
config: { slack: { accounts: { kev: {} } } },
|
||||
});
|
||||
|
||||
await tool.execute("call-4", {
|
||||
action: "sendMessage",
|
||||
to: "channel:C1",
|
||||
content: "hello",
|
||||
});
|
||||
|
||||
const [params] = handleSlackActionMock.mock.calls[0] ?? [];
|
||||
expect(params).not.toHaveProperty("accountId");
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,149 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { TemplateContext } from "../templating.js";
|
||||
import type { FollowupRun, QueueSettings } from "./queue.js";
|
||||
import { createMockTypingController } from "./test-helpers.js";
|
||||
|
||||
const runEmbeddedPiAgentMock = vi.fn();
|
||||
|
||||
vi.mock("../../agents/model-fallback.js", () => ({
|
||||
runWithModelFallback: async ({
|
||||
provider,
|
||||
model,
|
||||
run,
|
||||
}: {
|
||||
provider: string;
|
||||
model: string;
|
||||
run: (provider: string, model: string) => Promise<unknown>;
|
||||
}) => ({
|
||||
result: await run(provider, model),
|
||||
provider,
|
||||
model,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("../../agents/pi-embedded.js", () => ({
|
||||
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
|
||||
runEmbeddedPiAgent: (params: unknown) => runEmbeddedPiAgentMock(params),
|
||||
}));
|
||||
|
||||
vi.mock("./queue.js", async () => {
|
||||
const actual =
|
||||
await vi.importActual<typeof import("./queue.js")>("./queue.js");
|
||||
return {
|
||||
...actual,
|
||||
enqueueFollowupRun: vi.fn(),
|
||||
scheduleFollowupDrain: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
import { runReplyAgent } from "./agent-runner.js";
|
||||
|
||||
function createRun(messageProvider = "slack") {
|
||||
const typing = createMockTypingController();
|
||||
const sessionCtx = {
|
||||
Provider: messageProvider,
|
||||
OriginatingTo: "channel:C1",
|
||||
AccountId: "primary",
|
||||
MessageSid: "msg",
|
||||
} as unknown as TemplateContext;
|
||||
const resolvedQueue = { mode: "interrupt" } as unknown as QueueSettings;
|
||||
const followupRun = {
|
||||
prompt: "hello",
|
||||
summaryLine: "hello",
|
||||
enqueuedAt: Date.now(),
|
||||
run: {
|
||||
sessionId: "session",
|
||||
sessionKey: "main",
|
||||
messageProvider,
|
||||
sessionFile: "/tmp/session.jsonl",
|
||||
workspaceDir: "/tmp",
|
||||
config: {},
|
||||
skillsSnapshot: {},
|
||||
provider: "anthropic",
|
||||
model: "claude",
|
||||
thinkLevel: "low",
|
||||
verboseLevel: "off",
|
||||
elevatedLevel: "off",
|
||||
bashElevated: {
|
||||
enabled: false,
|
||||
allowed: false,
|
||||
defaultLevel: "off",
|
||||
},
|
||||
timeoutMs: 1_000,
|
||||
blockReplyBreak: "message_end",
|
||||
},
|
||||
} as unknown as FollowupRun;
|
||||
|
||||
return runReplyAgent({
|
||||
commandBody: "hello",
|
||||
followupRun,
|
||||
queueKey: "main",
|
||||
resolvedQueue,
|
||||
shouldSteer: false,
|
||||
shouldFollowup: false,
|
||||
isActive: false,
|
||||
isStreaming: false,
|
||||
typing,
|
||||
sessionCtx,
|
||||
defaultModel: "anthropic/claude-opus-4-5",
|
||||
resolvedVerboseLevel: "off",
|
||||
isNewSession: false,
|
||||
blockStreamingEnabled: false,
|
||||
resolvedBlockStreamingBreak: "message_end",
|
||||
shouldInjectGroupIntro: false,
|
||||
typingMode: "instant",
|
||||
});
|
||||
}
|
||||
|
||||
describe("runReplyAgent messaging tool suppression", () => {
|
||||
it("drops replies when a messaging tool sent via the same provider + target", async () => {
|
||||
runEmbeddedPiAgentMock.mockResolvedValueOnce({
|
||||
payloads: [{ text: "hello world!" }],
|
||||
messagingToolSentTexts: ["different message"],
|
||||
messagingToolSentTargets: [
|
||||
{ tool: "slack", provider: "slack", to: "channel:C1" },
|
||||
],
|
||||
meta: {},
|
||||
});
|
||||
|
||||
const result = await createRun("slack");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("delivers replies when tool provider does not match", async () => {
|
||||
runEmbeddedPiAgentMock.mockResolvedValueOnce({
|
||||
payloads: [{ text: "hello world!" }],
|
||||
messagingToolSentTexts: ["different message"],
|
||||
messagingToolSentTargets: [
|
||||
{ tool: "discord", provider: "discord", to: "channel:C1" },
|
||||
],
|
||||
meta: {},
|
||||
});
|
||||
|
||||
const result = await createRun("slack");
|
||||
|
||||
expect(result).toMatchObject({ text: "hello world!" });
|
||||
});
|
||||
|
||||
it("delivers replies when account ids do not match", async () => {
|
||||
runEmbeddedPiAgentMock.mockResolvedValueOnce({
|
||||
payloads: [{ text: "hello world!" }],
|
||||
messagingToolSentTexts: ["different message"],
|
||||
messagingToolSentTargets: [
|
||||
{
|
||||
tool: "slack",
|
||||
provider: "slack",
|
||||
to: "channel:C1",
|
||||
accountId: "alt",
|
||||
},
|
||||
],
|
||||
meta: {},
|
||||
});
|
||||
|
||||
const result = await createRun("slack");
|
||||
|
||||
expect(result).toMatchObject({ text: "hello world!" });
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,19 @@
|
||||
declare module "osc-progress" {
|
||||
export type OscProgressController = {
|
||||
setIndeterminate: (label: string) => void;
|
||||
setPercent: (label: string, percent: number) => void;
|
||||
clear: () => void;
|
||||
done?: () => void;
|
||||
};
|
||||
|
||||
export function createOscProgressController(params: {
|
||||
env: NodeJS.ProcessEnv;
|
||||
isTty: boolean;
|
||||
write: (chunk: string) => void;
|
||||
}): OscProgressController;
|
||||
|
||||
export function supportsOscProgress(
|
||||
env: NodeJS.ProcessEnv,
|
||||
isTty: boolean,
|
||||
): boolean;
|
||||
}
|
||||
Loading…
Reference in New Issue