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,54 @@
import { describe, expect, it, vi } from "vitest";
import { expectInboundContextContract } from "../../../../test/helpers/inbound-contract.js";
let capturedCtx: unknown;
vi.mock("../../../auto-reply/reply/provider-dispatcher.js", () => ({
dispatchReplyWithBufferedBlockDispatcher: vi.fn(async (params: { ctx: unknown }) => {
capturedCtx = params.ctx;
return { queuedFinal: false };
}),
}));
import { processMessage } from "./process-message.js";
describe("web processMessage inbound contract", () => {
it("passes a finalized MsgContext to the dispatcher", async () => {
capturedCtx = undefined;
await processMessage({
cfg: { messages: {} } as any,
msg: {
id: "msg1",
from: "123@g.us",
to: "+15550001111",
chatType: "group",
body: "hi",
senderName: "Alice",
senderJid: "alice@s.whatsapp.net",
senderE164: "+15550002222",
groupSubject: "Test Group",
groupParticipants: [],
} as any,
route: { agentId: "main", accountId: "default", sessionKey: "agent:main:whatsapp:group:123" } as any,
groupHistoryKey: "123@g.us",
groupHistories: new Map(),
groupMemberNames: new Map(),
connectionId: "conn",
verbose: false,
maxMediaBytes: 1,
replyResolver: (async () => undefined) as any,
replyLogger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } as any,
backgroundTasks: new Set(),
rememberSentText: (_text: string | undefined, _opts: unknown) => {},
echoHas: () => false,
echoForget: () => {},
buildCombinedEchoKey: () => "echo",
groupHistory: [],
} as any);
expect(capturedCtx).toBeTruthy();
expectInboundContextContract(capturedCtx as any);
});
});

View File

@@ -16,13 +16,13 @@ import {
import { dispatchReplyWithBufferedBlockDispatcher } from "../../../auto-reply/reply/provider-dispatcher.js";
import type { getReplyFromConfig } from "../../../auto-reply/reply.js";
import type { ReplyPayload } from "../../../auto-reply/types.js";
import { finalizeInboundContext } from "../../../auto-reply/reply/inbound-context.js";
import { toLocationContext } from "../../../channels/location.js";
import type { loadConfig } from "../../../config/config.js";
import { logVerbose, shouldLogVerbose } from "../../../globals.js";
import type { getChildLogger } from "../../../logging.js";
import type { resolveAgentRoute } from "../../../routing/resolve-route.js";
import { jidToE164, normalizeE164 } from "../../../utils.js";
import { normalizeChatType } from "../../../channels/chat-type.js";
import { newConnectionId } from "../../reconnect.js";
import { formatError } from "../../session.js";
import { deliverWebReply } from "../deliver-reply.js";
@@ -196,12 +196,10 @@ export async function processMessage(params: {
};
const { queuedFinal } = await dispatchReplyWithBufferedBlockDispatcher({
ctx: {
ctx: finalizeInboundContext({
Body: combinedBody,
BodyForAgent: combinedBody,
RawBody: params.msg.body,
CommandBody: params.msg.body,
BodyForCommands: params.msg.body,
From: params.msg.from,
To: params.msg.to,
SessionKey: params.route.sessionKey,
@@ -213,7 +211,7 @@ export async function processMessage(params: {
MediaPath: params.msg.mediaPath,
MediaUrl: params.msg.mediaUrl,
MediaType: params.msg.mediaType,
ChatType: normalizeChatType(params.msg.chatType) ?? params.msg.chatType,
ChatType: params.msg.chatType,
ConversationLabel: params.msg.chatType === "group" ? conversationId : params.msg.from,
GroupSubject: params.msg.groupSubject,
GroupMembers: formatGroupMembers({
@@ -230,7 +228,7 @@ export async function processMessage(params: {
Surface: "whatsapp",
OriginatingChannel: "whatsapp",
OriginatingTo: params.msg.from,
},
}),
cfg: params.cfg,
replyResolver: params.replyResolver,
dispatcherOptions: {