/** * Text normalisation shared by everything a model wrote and everything an admin * typed into a prompt. * * The prompt is text the model imitates, so a dash that survives in the settings * page reaches every draft. Keeping the rule in one pure module means the model's * output and the admin's input are cleaned the same way. */ /** * Shipped copy never carries an em-dash or an en-dash. * * A spaced dash is punctuation, so a comma reads correctly there. An unspaced * em-dash is almost always punctuation too. A remaining en-dash is a range * (1-20), where a hyphen is the honest swap. */ export function normalizeDashes(text: string): string { return text .replace(/\s+[\u2014\u2013]\s+/g, ", ") .replace(/\s*\u2014\s*/g, ", ") .replace(/\u2013/g, "-"); } /** Length ceilings for admin-entered prompt text. Shared by every writer. */ export const MAX_VOICE_CHARS = 4000; export const MAX_EXTRA_RULES_CHARS = 2000; /** * Clean an admin-entered prompt fragment: normalise dashes, trim, collapse an * empty result to null (meaning "use the built-in"), and cap the length. * * A cap rather than a rejection: an over-long paste should still save something * usable instead of failing the whole settings form with no explanation. */ export function cleanPromptText(value: unknown, maxLength: number): string | null { if (typeof value !== "string") return null; const normalised = normalizeDashes(value.trim()); if (!normalised) return null; return normalised.length > maxLength ? normalised.slice(0, maxLength).trimEnd() : normalised; }