wishforge c75f429716 🐛 Read the line breaks a model escapes inside its tool call
An answer travels in a submit_answer tool call, and the model writes its
line breaks there as the two characters \n. The console draws exactly what
it is given, so a customer read \n\n where a paragraph break belonged.

The layout escapes are now unfolded once, where model text enters: the
answer, the Wish draft, the quick replies and the excerpts all pass one
door. Answers stored before this change are unfolded as they are drawn, so
the history reads correctly without touching the database.
2026-11-09 20:12:01 -08:00

15 lines
939 B
TypeScript

/**
* A model writes prose with real line breaks, but the same text sent inside a JSON tool call
* arrives with the escape sequence spelled out: `\n` as two characters where a line break belongs.
* Nothing downstream can tell that apart from a customer who typed a backslash, so layout escapes
* are unwrapped once, where the text enters WishForge, and then drawn exactly as stored.
*
* Only the line-break spellings are translated. Every other backslash — a regular expression, a
* quoted path — is left as it is. A Windows path that happens to contain `\n` or `\r` is the one
* thing this cannot tell apart from an escaped line break; no answer in this product carries one.
*/
const LINE_BREAKS: Record<string, string> = { '\\r\\n': '\n', '\\n': '\n', '\\r': '\n' };
export function plainText(value: string) {
return value.includes('\\') ? value.replace(/\\r\\n|\\n|\\r/g, token => LINE_BREAKS[token] ?? token) : value;
}