refactor: share inbound envelope label helper

This commit is contained in:
Peter Steinberger
2026-01-17 08:51:26 +00:00
parent 61e60f3b84
commit 1002c74d9c
4 changed files with 46 additions and 16 deletions

View File

@@ -59,6 +59,27 @@ export function formatInboundEnvelope(params: {
}); });
} }
export function formatInboundFromLabel(params: {
isGroup: boolean;
groupLabel?: string;
groupId?: string;
directLabel: string;
directId?: string;
groupFallback?: string;
}): string {
// Keep envelope headers compact: group labels include id, DMs only add id when it differs.
if (params.isGroup) {
const label = params.groupLabel?.trim() || params.groupFallback || "Group";
const id = params.groupId?.trim();
return id ? `${label} id:${id}` : label;
}
const directLabel = params.directLabel.trim();
const directId = params.directId?.trim();
if (!directId || directId === directLabel) return directLabel;
return `${directLabel} id:${directId}`;
}
export function formatThreadStarterEnvelope(params: { export function formatThreadStarterEnvelope(params: {
channel: string; channel: string;
author?: string; author?: string;

View File

@@ -46,4 +46,11 @@ describe("formatInboundBodyWithSenderMeta", () => {
const ctx: MsgContext = { ChatType: "group", SenderName: "Alice", SenderId: "A1" }; const ctx: MsgContext = { ChatType: "group", SenderName: "Alice", SenderId: "A1" };
expect(formatInboundBodyWithSenderMeta({ ctx, body: "Alice (A1): hi" })).toBe("Alice (A1): hi"); expect(formatInboundBodyWithSenderMeta({ ctx, body: "Alice (A1): hi" })).toBe("Alice (A1): hi");
}); });
it("does not append when the sender prefix follows an envelope header", () => {
const ctx: MsgContext = { ChatType: "group", SenderName: "Alice", SenderId: "A1" };
expect(formatInboundBodyWithSenderMeta({ ctx, body: "[Signal Group] Alice (A1): hi" })).toBe(
"[Signal Group] Alice (A1): hi",
);
});
}); });

View File

@@ -11,7 +11,7 @@ import {
} from "../../auto-reply/reply/response-prefix-template.js"; } from "../../auto-reply/reply/response-prefix-template.js";
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js"; import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
import { hasControlCommand } from "../../auto-reply/command-detection.js"; import { hasControlCommand } from "../../auto-reply/command-detection.js";
import { formatInboundEnvelope } from "../../auto-reply/envelope.js"; import { formatInboundEnvelope, formatInboundFromLabel } from "../../auto-reply/envelope.js";
import { import {
createInboundDebouncer, createInboundDebouncer,
resolveInboundDebounceMs, resolveInboundDebounceMs,
@@ -383,13 +383,14 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
} }
const chatTarget = formatIMessageChatTarget(chatId); const chatTarget = formatIMessageChatTarget(chatId);
// For groups: use chat name or just "Group" (channel "iMessage" is already shown). const fromLabel = formatInboundFromLabel({
// For DMs: keep headers compact; only add id: suffix if raw differs from normalized. isGroup,
const fromLabel = isGroup groupLabel: message.chat_name ?? undefined,
? `${message.chat_name || "Group"} id:${chatId ?? "unknown"}` groupId: chatId !== undefined ? String(chatId) : "unknown",
: senderNormalized === sender groupFallback: "Group",
? senderNormalized directLabel: senderNormalized,
: `${senderNormalized} id:${sender}`; directId: sender,
});
const body = formatInboundEnvelope({ const body = formatInboundEnvelope({
channel: "iMessage", channel: "iMessage",
from: fromLabel, from: fromLabel,

View File

@@ -8,7 +8,7 @@ import {
type ResponsePrefixContext, type ResponsePrefixContext,
} from "../../auto-reply/reply/response-prefix-template.js"; } from "../../auto-reply/reply/response-prefix-template.js";
import { hasControlCommand } from "../../auto-reply/command-detection.js"; import { hasControlCommand } from "../../auto-reply/command-detection.js";
import { formatInboundEnvelope } from "../../auto-reply/envelope.js"; import { formatInboundEnvelope, formatInboundFromLabel } from "../../auto-reply/envelope.js";
import { import {
createInboundDebouncer, createInboundDebouncer,
resolveInboundDebounceMs, resolveInboundDebounceMs,
@@ -65,13 +65,14 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
}; };
async function handleSignalInboundMessage(entry: SignalInboundEntry) { async function handleSignalInboundMessage(entry: SignalInboundEntry) {
// For groups: use group name or just "Group" (channel "Signal" is already shown). const fromLabel = formatInboundFromLabel({
// For DMs: keep headers compact; only add id: suffix if display differs from name. isGroup: entry.isGroup,
const fromLabel = entry.isGroup groupLabel: entry.groupName ?? undefined,
? `${entry.groupName || "Group"} id:${entry.groupId}` groupId: entry.groupId ?? "unknown",
: entry.senderName === entry.senderDisplay groupFallback: "Group",
? entry.senderName directLabel: entry.senderName,
: `${entry.senderName} id:${entry.senderDisplay}`; directId: entry.senderDisplay,
});
const body = formatInboundEnvelope({ const body = formatInboundEnvelope({
channel: "Signal", channel: "Signal",
from: fromLabel, from: fromLabel,