fix: avoid base string coercion in auto-reply formatting

This commit is contained in:
Peter Steinberger
2026-01-15 17:01:28 +00:00
parent fc4aa9a683
commit 01c43b0b0c
3 changed files with 37 additions and 6 deletions

View File

@@ -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);
});
}