1
0
Fork 0
polaris-task-force/src/lib/npc-dialogue/normalize.ts
Z8MB1E 5ccb25e643 fix(ai): bound the structured path too, and stop the dialog overpromising
The timeout from the previous commit only covered the plain-text fetch, so
the default drafting path was still unbounded: `streamObject` for
json_schema had no abort signal, which is what `generateDialogueDraft` and
the probe's structured half use. A provider that answers the plain probe
and then stalls could still hold the Generate modal and the Test button
open, while the docs claimed every request was bounded. Both
`streamObject` calls (the shared structured path and Compose's fallback)
now carry the same ceiling.

Dialog copy told the author the branch continues into the successor even
after they cleared the return box, which is when it does not. It now says
that step becomes unreachable until wired by hand. When the node cannot
take a return at all the checkbox used to vanish without explanation; the
reason is now stated where the toggle would be.

A choice node marked as the start could receive the return hop, drawing an
edge into the start node that `connectHop` and the drop handler both
refuse. It no longer returns, and its open end stays free like the other
cases.

Also: the two misplaced doc comments now sit above the declarations they
describe, and the whole-conversation caveat moved into the shared preview
component so the global settings tab carries it too.
2026-11-24 15:00:30 -05:00

42 lines
1.6 KiB
TypeScript

/**
* 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;
}