From 01c43b0b0cc813518cea53989a60df0428561cf1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 15 Jan 2026 17:01:28 +0000 Subject: [PATCH] fix: avoid base string coercion in auto-reply formatting --- src/auto-reply/commands-args.ts | 10 ++++++++-- src/auto-reply/commands-registry.ts | 10 +++++++--- src/auto-reply/templating.ts | 23 ++++++++++++++++++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/auto-reply/commands-args.ts b/src/auto-reply/commands-args.ts index 31d4fdbf9..63f73c468 100644 --- a/src/auto-reply/commands-args.ts +++ b/src/auto-reply/commands-args.ts @@ -4,8 +4,14 @@ export type CommandArgsFormatter = (values: CommandArgValues) => string | undefi function normalizeArgValue(value: unknown): string | undefined { if (value == null) return undefined; - const text = typeof value === "string" ? value.trim() : String(value).trim(); - return text ? text : undefined; + if (typeof value === "string") { + const trimmed = value.trim(); + return trimmed ? trimmed : undefined; + } + if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") { + return String(value); + } + return undefined; } const formatConfigArgs: CommandArgsFormatter = (values) => { diff --git a/src/auto-reply/commands-registry.ts b/src/auto-reply/commands-registry.ts index a17b681d1..5edc677d2 100644 --- a/src/auto-reply/commands-registry.ts +++ b/src/auto-reply/commands-registry.ts @@ -137,9 +137,13 @@ function formatPositionalArgs( for (const definition of definitions) { const value = values[definition.name]; if (value == null) continue; - const rendered = typeof value === "string" ? value.trim() : String(value); - if (!rendered) continue; - parts.push(rendered); + if (typeof value === "string") { + const trimmed = value.trim(); + if (!trimmed) continue; + parts.push(trimmed); + } else if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") { + parts.push(String(value)); + } if (definition.captureRemaining) break; } return parts.length > 0 ? parts.join(" ") : undefined; diff --git a/src/auto-reply/templating.ts b/src/auto-reply/templating.ts index 4ae58d181..e110e5a9b 100644 --- a/src/auto-reply/templating.ts +++ b/src/auto-reply/templating.ts @@ -78,11 +78,32 @@ export type TemplateContext = MsgContext & { IsNewSession?: string; }; +function formatTemplateValue(value: unknown): string { + if (value == null) return ""; + if (typeof value === "string") return value; + if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") { + return String(value); + } + if (Array.isArray(value)) { + return value + .flatMap((entry) => { + if (entry == null) return []; + if (typeof entry === "string") return [entry]; + if (typeof entry === "number" || typeof entry === "boolean" || typeof entry === "bigint") { + return [String(entry)]; + } + return []; + }) + .join(","); + } + return ""; +} + // Simple {{Placeholder}} interpolation using inbound message context. export function applyTemplate(str: string | undefined, ctx: TemplateContext) { if (!str) return ""; return str.replace(/{{\s*(\w+)\s*}}/g, (_, key) => { const value = ctx[key as keyof TemplateContext]; - return value == null ? "" : String(value); + return formatTemplateValue(value); }); }