fix: finalize inbound contexts

This commit is contained in:
Peter Steinberger
2026-01-17 05:04:29 +00:00
parent 4b085f23e0
commit bc49c20434
27 changed files with 645 additions and 83 deletions

View File

@@ -0,0 +1,90 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import type { MsgContext } from "../../auto-reply/templating.js";
import { expectInboundContextContract } from "../../../test/helpers/inbound-contract.js";
let capturedCtx: MsgContext | undefined;
vi.mock("../../auto-reply/reply/dispatch-from-config.js", () => ({
dispatchReplyFromConfig: vi.fn(async (params: { ctx: MsgContext }) => {
capturedCtx = params.ctx;
return { queuedFinal: false, counts: { tool: 0, block: 0 } };
}),
}));
import { processDiscordMessage } from "./message-handler.process.js";
describe("discord processDiscordMessage inbound contract", () => {
it("passes a finalized MsgContext to dispatchReplyFromConfig", async () => {
capturedCtx = undefined;
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-discord-"));
const storePath = path.join(dir, "sessions.json");
await processDiscordMessage({
cfg: { messages: {}, session: { store: storePath } } as any,
discordConfig: {} as any,
accountId: "default",
token: "token",
runtime: { log: () => {}, error: () => {} } as any,
guildHistories: new Map(),
historyLimit: 0,
mediaMaxBytes: 1024,
textLimit: 4000,
replyToMode: "off",
ackReactionScope: "direct",
groupPolicy: "open",
data: { guild: null } as any,
client: { rest: {} } as any,
message: {
id: "m1",
channelId: "c1",
timestamp: new Date().toISOString(),
attachments: [],
} as any,
author: {
id: "U1",
username: "alice",
discriminator: "0",
globalName: "Alice",
} as any,
channelInfo: null,
channelName: undefined,
isGuildMessage: false,
isDirectMessage: true,
isGroupDm: false,
commandAuthorized: true,
baseText: "hi",
messageText: "hi",
wasMentioned: false,
shouldRequireMention: false,
canDetectMention: false,
effectiveWasMentioned: false,
threadChannel: null,
threadParentId: undefined,
threadParentName: undefined,
threadParentType: undefined,
threadName: undefined,
displayChannelSlug: "",
guildInfo: null,
guildSlug: "",
channelConfig: null,
baseSessionKey: "agent:main:discord:dm:U1",
route: {
agentId: "main",
channel: "discord",
accountId: "default",
sessionKey: "agent:main:discord:dm:U1",
mainSessionKey: "agent:main:main",
} as any,
} as any);
expect(capturedCtx).toBeTruthy();
expectInboundContextContract(capturedCtx!);
});
});

View File

@@ -14,6 +14,7 @@ import {
buildPendingHistoryContextFromMap,
clearHistoryEntries,
} from "../../auto-reply/reply/history.js";
import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js";
import { createReplyDispatcherWithTyping } from "../../auto-reply/reply/reply-dispatcher.js";
import type { ReplyPayload } from "../../auto-reply/types.js";
import { resolveStorePath, updateLastRoute } from "../../config/sessions.js";
@@ -219,12 +220,10 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
return;
}
const ctxPayload = {
const ctxPayload = finalizeInboundContext({
Body: combinedBody,
BodyForAgent: combinedBody,
RawBody: baseText,
CommandBody: baseText,
BodyForCommands: baseText,
From: effectiveFrom,
To: effectiveTo,
SessionKey: autoThreadContext?.SessionKey ?? threadKeys.sessionKey,
@@ -253,7 +252,7 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
// Originating channel for reply routing.
OriginatingChannel: "discord" as const,
OriginatingTo: autoThreadContext?.OriginatingTo ?? replyTarget,
};
});
if (isDirectMessage) {
const sessionCfg = cfg.session;

View File

@@ -30,6 +30,7 @@ import type {
NativeCommandSpec,
} from "../../auto-reply/commands-registry.js";
import { dispatchReplyWithDispatcher } from "../../auto-reply/reply/provider-dispatcher.js";
import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js";
import type { ReplyPayload } from "../../auto-reply/types.js";
import type { ClawdbotConfig, loadConfig } from "../../config/config.js";
import { buildPairingReply } from "../../pairing/pairing-messages.js";
@@ -570,11 +571,10 @@ async function dispatchDiscordCommandInteraction(params: {
},
});
const conversationLabel = isDirectMessage ? (user.globalName ?? user.username) : channelId;
const ctxPayload = {
const ctxPayload = finalizeInboundContext({
Body: prompt,
BodyForAgent: prompt,
RawBody: prompt,
CommandBody: prompt,
BodyForCommands: prompt,
CommandArgs: commandArgs,
From: isDirectMessage ? `discord:${user.id}` : `group:${channelId}`,
To: `slash:${user.id}`,
@@ -607,7 +607,7 @@ async function dispatchDiscordCommandInteraction(params: {
Timestamp: Date.now(),
CommandAuthorized: commandAuthorized,
CommandSource: "native" as const,
};
});
let didReply = false;
await dispatchReplyWithDispatcher({