Every AI surface talked to the Unsloth box through env vars, so swapping
provider or model meant editing .env and restarting. A connection is now
a stored record: base URL, model id, key, structured-output mode, and a
reasoning toggle.
Three surfaces with their own selector (Compose, dialogue draft, line
rewrite), because they want different things from a model: a draft needs
strict schema adherence while a line rewrite only needs prose. Blank
means the env defaults, so an empty global behaves exactly as before.
The API key is write-only: field read access denies everyone, so it never
reaches the admin form, REST, GraphQL, or a populated relationship. Blank
submission means "unchanged" and `clearApiKey` is the only delete, which
avoids the masked-field-round-trips-empty bug. Backups still contain it;
a blank connection key falls back to UNSLOTH_API_KEY for that reason.
Structured output is no longer assumed to be json_schema. Some
OpenAI-compatible providers reject it, so a connection selects
json_schema, json_object or prompt, and the weaker modes serialise the
schema into the prompt and parse the reply. The Test button runs a plain
probe and a structured probe, because a provider that refuses json_schema
passes the first and fails every draft.
Compose keeps a single fixed dropdown entry: plugin config is built at
boot and cannot list database rows, so the handler resolves the selected
connection per request instead.
Draft provenance records the connection label, so a stored conversation
still names the provider that produced it.
The system prompt was a single constant, so tuning how drafted dialogue
sounds meant editing code and redeploying. Split it into the half that is
copy and the half that is a contract.
VOICE and optional HOUSE RULES are now editable from a new
`npc-dialogue-settings` global, and per conversation through an
`voiceOverride` field on `npc-dialogues`. Precedence is dialogue, then
global, then the built-in default, resolved by the pure `resolveVoice`.
STRUCTURE, FIELDS, STATE, OUTPUT and the worked example stay in
`LOCKED_RULES`: the draft validator enforces the same rules, so an edit
there would make every draft fail for reasons an author cannot trace.
Both surfaces render the assembled prompt through the same pure
composer the server calls, so the preview cannot drift from what the
model receives. The global normalises dashes on save, bumps `revision`
only on a real text change, and keeps 20 versions for rollback.
Provenance gains `promptSource` and `promptRevision` so a stored draft
still names the voice that shaped it. A draft that fails twice while a
custom voice is active now says so, instead of looking like a model bug.
`composeDialogueSystemPrompt()` with no arguments reproduces the old
prompt byte for byte, pinned by a golden fixture.
Add a Generate button to the flow editor that drafts a whole conversation
from a form brief, and per-line Rewrite/Shorten/Sharpen controls in the node
dialog. Both land in unsaved field state, so a draft only reaches players when
an author saves the document: neither server action writes to npc-dialogues.
- src/lib/ai/generate.ts: extract the Unsloth client out of payload.config so
Compose and dialogue drafting share one path; add generateStructured and
resolveModel, with an optional UNSLOTH_MODEL_STRUCTURED override
- generationSchema.ts (pure): strict-safe JSON schema, normalizer with
breadth layout, validator that rejects unknown flag keys, unauthorised
effects, dangling hops and missing keys
- generation.ts: context loading, one validate-and-retry, then give up rather
than hand back a broken graph; dash normalization keeps shipped copy clean
- effect vocabulary limited to notify and flag; emit, reward and discord stay
author-only
- provenance group on npc-dialogues (migration 20261112_171955) records model,
prompt version, attempts and the brief the draft was accepted from
- tests: 21 cases covering the normalizer, validator, retry paths and the
rewrite parser, with the model call injected
- docs/npc-dialogue-ai.md and src/lib/npc-dialogue/AGENTS.md