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

@@ -273,8 +273,10 @@ export async function runPreparedReply(
typing.cleanup();
return undefined;
}
const isBareNewOrReset = rawBodyTrimmed === "/new" || rawBodyTrimmed === "/reset";
const isBareSessionReset =
isNewSession && baseBodyTrimmedRaw.length === 0 && rawBodyTrimmed.length > 0;
isNewSession &&
((baseBodyTrimmedRaw.length === 0 && rawBodyTrimmed.length > 0) || isBareNewOrReset);
const baseBodyFinal = isBareSessionReset ? BARE_SESSION_RESET_PROMPT : baseBody;
const baseBodyTrimmed = baseBodyFinal.trim();
if (!baseBodyTrimmed) {

View File

@@ -17,6 +17,7 @@ import { resolveDefaultModel } from "./directive-handling.js";
import { resolveReplyDirectives } from "./get-reply-directives.js";
import { handleInlineActions } from "./get-reply-inline-actions.js";
import { runPreparedReply } from "./get-reply-run.js";
import { finalizeInboundContext } from "./inbound-context.js";
import { initSessionState } from "./session.js";
import { stageSandboxMedia } from "./stage-sandbox-media.js";
import { createTypingController } from "./typing.js";
@@ -74,6 +75,8 @@ export async function getReplyFromConfig(
});
opts?.onTypingController?.(typing);
finalizeInboundContext(ctx);
await applyMediaUnderstanding({
ctx,
cfg,

View File

@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import type { MsgContext } from "../templating.js";
import { finalizeInboundContext } from "./inbound-context.js";
describe("finalizeInboundContext", () => {
it("fills BodyForAgent/BodyForCommands and normalizes newlines", () => {
const ctx: MsgContext = {
Body: "a\\nb\r\nc",
RawBody: "raw\\nline",
ChatType: "room",
From: "group:123@g.us",
GroupSubject: "Test",
};
const out = finalizeInboundContext(ctx);
expect(out.Body).toBe("a\nb\nc");
expect(out.RawBody).toBe("raw\nline");
expect(out.BodyForAgent).toBe("a\nb\nc");
expect(out.BodyForCommands).toBe("raw\nline");
expect(out.ChatType).toBe("channel");
expect(out.ConversationLabel).toContain("Test");
});
it("can force BodyForCommands to follow updated CommandBody", () => {
const ctx: MsgContext = {
Body: "base",
BodyForCommands: "<media:audio>",
CommandBody: "say hi",
From: "signal:+15550001111",
ChatType: "direct",
};
finalizeInboundContext(ctx, { forceBodyForCommands: true });
expect(ctx.BodyForCommands).toBe("say hi");
});
});

View File

@@ -0,0 +1,59 @@
import { normalizeChatType } from "../../channels/chat-type.js";
import { resolveConversationLabel } from "../../channels/conversation-label.js";
import type { MsgContext } from "../templating.js";
import { normalizeInboundTextNewlines } from "./inbound-text.js";
export type FinalizeInboundContextOptions = {
forceBodyForAgent?: boolean;
forceBodyForCommands?: boolean;
forceChatType?: boolean;
forceConversationLabel?: boolean;
};
function normalizeTextField(value: unknown): string | undefined {
if (typeof value !== "string") return undefined;
return normalizeInboundTextNewlines(value);
}
export function finalizeInboundContext<T extends Record<string, unknown>>(
ctx: T,
opts: FinalizeInboundContextOptions = {},
): T & MsgContext {
const normalized = ctx as T & MsgContext;
normalized.Body = normalizeInboundTextNewlines(
typeof normalized.Body === "string" ? normalized.Body : "",
);
normalized.RawBody = normalizeTextField(normalized.RawBody);
normalized.CommandBody = normalizeTextField(normalized.CommandBody);
normalized.Transcript = normalizeTextField(normalized.Transcript);
normalized.ThreadStarterBody = normalizeTextField(normalized.ThreadStarterBody);
const chatType = normalizeChatType(normalized.ChatType);
if (chatType && (opts.forceChatType || normalized.ChatType !== chatType)) {
normalized.ChatType = chatType;
}
const bodyForAgentSource = opts.forceBodyForAgent
? normalized.Body
: (normalized.BodyForAgent ?? normalized.Body);
normalized.BodyForAgent = normalizeInboundTextNewlines(bodyForAgentSource);
const bodyForCommandsSource = opts.forceBodyForCommands
? (normalized.CommandBody ?? normalized.RawBody ?? normalized.Body)
: (normalized.BodyForCommands ??
normalized.CommandBody ??
normalized.RawBody ??
normalized.Body);
normalized.BodyForCommands = normalizeInboundTextNewlines(bodyForCommandsSource);
const explicitLabel = normalized.ConversationLabel?.trim();
if (opts.forceConversationLabel || !explicitLabel) {
const resolved = resolveConversationLabel(normalized)?.trim();
if (resolved) normalized.ConversationLabel = resolved;
} else {
normalized.ConversationLabel = explicitLabel;
}
return normalized;
}

View File

@@ -1,7 +1,3 @@
export function normalizeInboundTextNewlines(input: string): string {
const text = input.replaceAll("\r\n", "\n").replaceAll("\r", "\n");
if (text.includes("\n")) return text;
if (!text.includes("\\n")) return text;
return text.replaceAll("\\n", "\n");
return input.replaceAll("\r\n", "\n").replaceAll("\r", "\n").replaceAll("\\n", "\n");
}