refactor: normalize inbound context

This commit is contained in:
Peter Steinberger
2026-01-17 04:04:05 +00:00
parent 9f4b7a1683
commit a2b5b1f0cb
31 changed files with 155 additions and 35 deletions

View File

@@ -1,28 +1,21 @@
import type { MsgContext } from "../templating.js";
import { normalizeChatType } from "../../channels/chat-type.js";
export function formatInboundBodyWithSenderMeta(params: { body: string; ctx: MsgContext }): string {
const body = params.body;
if (!body.trim()) return body;
const chatType = params.ctx.ChatType?.trim().toLowerCase();
const chatType = normalizeChatType(params.ctx.ChatType);
if (!chatType || chatType === "direct") return body;
if (hasSenderMetaLine(body)) return body;
const senderLabel = formatSenderLabel(params.ctx);
if (!senderLabel) return body;
const lineBreak = resolveBodyLineBreak(body);
return `${body}${lineBreak}[from: ${senderLabel}]`;
}
function resolveBodyLineBreak(body: string): string {
const hasEscaped = body.includes("\\n");
const hasNewline = body.includes("\n");
if (hasEscaped && !hasNewline) return "\\n";
return "\n";
return `${body}\n[from: ${senderLabel}]`;
}
function hasSenderMetaLine(body: string): boolean {
return /(^|\n|\\n)\[from:/i.test(body);
return /(^|\n)\[from:/i.test(body);
}
function formatSenderLabel(ctx: MsgContext): string | null {