fix: unify inbound sender labels

This commit is contained in:
Peter Steinberger
2026-01-17 05:21:02 +00:00
parent 572e04d5fb
commit f7089cde54
20 changed files with 587 additions and 40 deletions

View File

@@ -0,0 +1,34 @@
import { describe, expect, it } from "vitest";
import { buildInboundLine } from "./message-line.js";
describe("buildInboundLine", () => {
it("prefixes group messages with sender", () => {
const line = buildInboundLine({
cfg: {
agents: { defaults: { workspace: "/tmp/clawd" } },
channels: { whatsapp: { messagePrefix: "" } },
} as never,
agentId: "main",
msg: {
from: "123@g.us",
conversationId: "123@g.us",
to: "+15550009999",
accountId: "default",
body: "ping",
timestamp: 1700000000000,
chatType: "group",
chatId: "123@g.us",
senderJid: "111@s.whatsapp.net",
senderE164: "+15550001111",
senderName: "Bob",
sendComposing: async () => undefined,
reply: async () => undefined,
sendMedia: async () => undefined,
} as never,
});
expect(line).toContain("Bob (+15550001111):");
expect(line).toContain("ping");
});
});

View File

@@ -1,5 +1,5 @@
import { resolveMessagePrefix } from "../../../agents/identity.js";
import { formatAgentEnvelope } from "../../../auto-reply/envelope.js";
import { formatInboundEnvelope } from "../../../auto-reply/envelope.js";
import type { loadConfig } from "../../../config/config.js";
import type { WebInboundMsg } from "../types.js";
@@ -26,10 +26,16 @@ export function buildInboundLine(params: {
const baseLine = `${prefixStr}${msg.body}${replyContext ? `\n\n${replyContext}` : ""}`;
// Wrap with standardized envelope for the agent.
return formatAgentEnvelope({
return formatInboundEnvelope({
channel: "WhatsApp",
from: msg.chatType === "group" ? msg.from : msg.from?.replace(/^whatsapp:/, ""),
timestamp: msg.timestamp,
body: baseLine,
chatType: msg.chatType,
sender: {
name: msg.senderName,
e164: msg.senderE164,
id: msg.senderJid,
},
});
}

View File

@@ -8,7 +8,7 @@ import {
type ResponsePrefixContext,
} from "../../../auto-reply/reply/response-prefix-template.js";
import { resolveTextChunkLimit } from "../../../auto-reply/chunk.js";
import { formatAgentEnvelope } from "../../../auto-reply/envelope.js";
import { formatInboundEnvelope } from "../../../auto-reply/envelope.js";
import {
buildHistoryContextFromEntries,
type HistoryEntry,
@@ -95,11 +95,13 @@ export async function processMessage(params: {
const bodyWithId = entry.messageId
? `${entry.body}\n[message_id: ${entry.messageId}]`
: entry.body;
return formatAgentEnvelope({
return formatInboundEnvelope({
channel: "WhatsApp",
from: conversationId,
timestamp: entry.timestamp,
body: `${entry.sender}: ${bodyWithId}`,
body: bodyWithId,
chatType: "group",
senderLabel: entry.sender,
});
},
});