fix: unify inbound dispatch pipeline

This commit is contained in:
Peter Steinberger
2026-01-23 22:51:37 +00:00
parent da26954dd0
commit 2e0a835e07
29 changed files with 543 additions and 297 deletions

View File

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createReplyDispatcherWithTyping } from "../auto-reply/reply/reply-dispatcher.js";
const dispatchMock = vi.fn();
@@ -20,15 +21,34 @@ vi.mock("@buape/carbon", () => ({
},
}));
vi.mock("../auto-reply/reply/dispatch-from-config.js", () => ({
dispatchReplyFromConfig: (...args: unknown[]) => dispatchMock(...args),
}));
vi.mock("../auto-reply/dispatch.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../auto-reply/dispatch.js")>();
return {
...actual,
dispatchInboundMessage: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithBufferedDispatcher: (...args: unknown[]) => dispatchMock(...args),
};
});
beforeEach(() => {
dispatchMock.mockReset().mockImplementation(async ({ dispatcher }) => {
dispatcher.sendToolResult({ text: "tool update" });
dispatcher.sendFinalReply({ text: "final reply" });
return { queuedFinal: true, counts: { tool: 1, block: 0, final: 1 } };
dispatchMock.mockReset().mockImplementation(async (params) => {
if ("dispatcher" in params && params.dispatcher) {
params.dispatcher.sendToolResult({ text: "tool update" });
params.dispatcher.sendFinalReply({ text: "final reply" });
return { queuedFinal: true, counts: { tool: 1, block: 0, final: 1 } };
}
if ("dispatcherOptions" in params && params.dispatcherOptions) {
const { dispatcher, markDispatchIdle } = createReplyDispatcherWithTyping(
params.dispatcherOptions,
);
dispatcher.sendToolResult({ text: "tool update" });
dispatcher.sendFinalReply({ text: "final reply" });
await dispatcher.waitForIdle();
markDispatchIdle();
return { queuedFinal: true, counts: dispatcher.getQueuedCounts() };
}
return { queuedFinal: false, counts: { tool: 0, block: 0, final: 0 } };
});
});

View File

@@ -18,9 +18,15 @@ vi.mock("./send.js", () => ({
reactMock(...args);
},
}));
vi.mock("../auto-reply/reply/dispatch-from-config.js", () => ({
dispatchReplyFromConfig: (...args: unknown[]) => dispatchMock(...args),
}));
vi.mock("../auto-reply/dispatch.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../auto-reply/dispatch.js")>();
return {
...actual,
dispatchInboundMessage: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithBufferedDispatcher: (...args: unknown[]) => dispatchMock(...args),
};
});
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
@@ -41,7 +47,7 @@ beforeEach(() => {
updateLastRouteMock.mockReset();
dispatchMock.mockReset().mockImplementation(async ({ dispatcher }) => {
dispatcher.sendFinalReply({ text: "hi" });
return { queuedFinal: true, counts: { final: 1 } };
return { queuedFinal: true, counts: { tool: 0, block: 0, final: 1 } };
});
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });

View File

@@ -18,9 +18,15 @@ vi.mock("./send.js", () => ({
reactMock(...args);
},
}));
vi.mock("../auto-reply/reply/dispatch-from-config.js", () => ({
dispatchReplyFromConfig: (...args: unknown[]) => dispatchMock(...args),
}));
vi.mock("../auto-reply/dispatch.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../auto-reply/dispatch.js")>();
return {
...actual,
dispatchInboundMessage: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithBufferedDispatcher: (...args: unknown[]) => dispatchMock(...args),
};
});
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
@@ -40,7 +46,7 @@ beforeEach(() => {
updateLastRouteMock.mockReset();
dispatchMock.mockReset().mockImplementation(async ({ dispatcher }) => {
dispatcher.sendFinalReply({ text: "hi" });
return { queuedFinal: true, counts: { final: 1 } };
return { queuedFinal: true, counts: { tool: 0, block: 0, final: 1 } };
});
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });

View File

@@ -9,17 +9,24 @@ import { expectInboundContextContract } from "../../../test/helpers/inbound-cont
let capturedCtx: MsgContext | undefined;
vi.mock("../../auto-reply/reply/dispatch-from-config.js", () => ({
dispatchReplyFromConfig: vi.fn(async (params: { ctx: MsgContext }) => {
vi.mock("../../auto-reply/dispatch.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../auto-reply/dispatch.js")>();
const dispatchInboundMessage = vi.fn(async (params: { ctx: MsgContext }) => {
capturedCtx = params.ctx;
return { queuedFinal: false, counts: { tool: 0, block: 0 } };
}),
}));
return { queuedFinal: false, counts: { tool: 0, block: 0, final: 0 } };
});
return {
...actual,
dispatchInboundMessage,
dispatchInboundMessageWithDispatcher: dispatchInboundMessage,
dispatchInboundMessageWithBufferedDispatcher: dispatchInboundMessage,
};
});
import { processDiscordMessage } from "./message-handler.process.js";
describe("discord processDiscordMessage inbound contract", () => {
it("passes a finalized MsgContext to dispatchReplyFromConfig", async () => {
it("passes a finalized MsgContext to dispatchInboundMessage", async () => {
capturedCtx = undefined;
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-discord-"));

View File

@@ -14,7 +14,7 @@ import {
formatThreadStarterEnvelope,
resolveEnvelopeFormatOptions,
} from "../../auto-reply/envelope.js";
import { dispatchReplyFromConfig } from "../../auto-reply/reply/dispatch-from-config.js";
import { dispatchInboundMessage } from "../../auto-reply/dispatch.js";
import {
buildPendingHistoryContextFromMap,
clearHistoryEntries,
@@ -358,7 +358,7 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
onReplyStart: () => sendTyping({ client, channelId: typingChannelId }),
});
const { queuedFinal, counts } = await dispatchReplyFromConfig({
const { queuedFinal, counts } = await dispatchInboundMessage({
ctx: ctxPayload,
cfg,
dispatcher,