1
0
Fork 0
polaris-task-force/docs/ai-settings.md
Z8MB1E 7adfcde72d fix(npc-dialogue): keep the conversation reachable when a branch attaches
Review findings, all in code this range introduced.

Attaching a branch to a node that already had a next step replaced that
hop, and "come back where it attached" pointed the branch back at the
caller, which loops: the caller then leads into the branch it just
returned from. Everything downstream was orphaned. The branch now
continues into the step that hop used to point at, so attaching inserts
in front of it instead of deleting it. Rejoining a condition is refused
outright, because re-entering re-evaluates the same flags and retakes the
same branch. Return candidates are limited to node types that read
`next`: a choice or condition open end would have received a hop no
walker follows.

The dialog says which of the three applies, and only offers the toggle
when a return exists, so an untouched default is not sent and reported
back as a refusal.

Condition true/false dots were drawn on the input handle: the hit-area
class sets left and right on the handle itself, which over-constrains
React Flow's `right: 0`, so both dots drifted to the card's left edge.
They now take the hit area as a child, like the other handles.

Refusals are honest: `connectHop` reports `changed: false` for a write it
did not make (it used to report true and return a new array), the editor
explains instead of rewriting an identical graph, and a refused
tail-drag keeps the original hop rather than deleting it first.

A passing connection test now really is invalidated when the
configuration changes. That ran in `afterChange`, whose return value is
only the operation's response, so the stored row kept showing a stale
pass while the test asserted the response. It moved to `beforeChange`,
counts only fields actually submitted (so the Test button's own result
write does not invalidate itself), and the test reads the row back.

Also: the fragment prompt no longer teaches `isStart: true` through the
worked example or asks for "the whole conversation" on retry; requests
carry a ten minute ceiling so an unreachable-but-accepting host cannot
hang Generate, Compose or the Test button; the API key description no
longer claims at-rest encryption, which does not exist; the per-dialogue
voice override gets the same dash and length cleaning as the global it
outranks; and the unused `describeAttachmentPoint` is gone, with the
attachment output now included in the hint the model receives.
2026-11-24 14:47:08 -05:00

5.7 KiB

AI model connections

Every AI surface in the app talks to an OpenAI-compatible chat-completions endpoint. By default that is the Unsloth Studio box on the LAN, configured by environment variables. A stored connection lets a different provider or model be chosen from the admin panel without editing .env or redeploying.

The three surfaces

Each surface has its own selector, because they want different things from a model:

Surface What it asks for Bean counter note
AI Compose prose, wrapped into the field's JSON shape runs on every allowlisted field, so the cheapest capable model wins
Dialogue draft a whole conversation as one strict JSON object needs the strongest schema adherence
Dialogue rewrite one rewritten line prose only, a small model is fine

Settings → AI Settings (ai-settings, a global) holds the three selectors. Blank means the environment defaults, so an empty global changes nothing.

Creating a connection

Server → AI Connections (ai-connections).

Field Notes
Label shown in the selectors and recorded in draft provenance; say which provider and model
Base URL includes the API prefix, e.g. http://192.168.1.7:8888/v1 or https://api.deepseek.com/v1. /chat/completions is appended
Model id exactly what the provider expects, e.g. deepseek-v4.1-flash
API key write-only (see below). Blank leaves the stored key alone
Structured mode json_schema, json_object, or prompt
Send reasoning off sends reasoning_effort: "none", off for strict providers

The key is write-only

The apiKey field's read access denies everyone, so it never appears in the admin form, REST, GraphQL, or a populated relationship. The server reads it with overrideAccess: true when it makes a request.

That design makes a blank submission ambiguous, so the rule is: blank means unchanged. Replacing the key means typing a new one. Deleting it means ticking Clear API key and saving. hasApiKey shows whether one is stored without revealing it. The clearApiKey checkbox resets itself after each save so a later edit cannot silently delete a key that was just set.

The key is stored as plain text in the database (there is no at-rest encryption here), so database backups contain it. If that is unacceptable for a provider, keep using the UNSLOTH_API_KEY env var and leave the connection's key blank: a blank connection key falls back to the env var.

Structured output modes

json_schema is the default and the best option: the provider enforces the schema, so a malformed draft is rejected before it reaches the app.

Some OpenAI-compatible providers reject a json_schema request. For those:

  • json_object asks for JSON mode and puts the schema in the system prompt.
  • prompt asks for nothing and relies on the prompt, then parses the reply.

Both weaker modes serialise the schema into the prompt and parse the reply, so a model that does not follow instructions still fails, but through the app's existing validate-and-retry loop rather than a provider 400.

Test the connection before trusting it

The Test connection button on the edit view runs two probes against the saved values:

  1. a plain completion, which validates URL, key and model id;
  2. a tiny structured object, which validates the configured mode.

Both must pass. Every request carries a ten minute ceiling, so a host that accepts the connection and never answers fails visibly instead of hanging the button. The second one is the important one: a provider that rejects json_schema looks perfectly healthy on a plain completion and fails every draft. The result is stored on the record (lastTestOk, lastTestedAt, lastTestMessage) and cleared whenever the base URL, model id, structured mode, reasoning flag or key changes, because a passing test only describes the configuration that was tested. The clearing happens in beforeChange, not afterChange: an after-change return value is only the response of the operation, so the stored row would keep showing a stale pass while the API looked correct. Only fields actually submitted count as changed, otherwise the Test button's own result write would invalidate itself.

What changes when a connection is active

  • Requests go to baseUrl with modelId and the stored key instead of the UNSLOTH_* values.
  • reasoning_effort: "none" is sent only when the connection allows it.
  • A draft's provenance records the connection label and model id, so a stored conversation still says which provider produced it.
  • Compose's model dropdown has a single fixed entry ("Active AI connection"). The plugin's config is built once at boot and cannot list database rows, so the label names the routing rule; the handler resolves the actual connection per request.

Failure messages worth recognising

  • … API error: 401 … Invalid API key: the key is wrong or expired. Note that the Unsloth box rejects an unknown key with this, so a 401 against the LAN host usually means UNSLOTH_API_KEY is stale, not that a connection is misconfigured.
  • … API error: 400 … response_format: the provider rejects the structured mode. Switch to json_object or prompt, or turn off the reasoning toggle if the error names reasoning_effort.
  • Drafts fail twice with before blaming the model: the prompt voice is suspect, not the provider. The message names which voice was in use.

Tests

  • tests/int/ai-connections.int.spec.ts: key write-only contract, blank-means-keep, clear, per-surface resolution, half-filled fallback, test-result invalidation.
  • tests/int/ai-generate-modes.int.spec.ts: request shape against a local stub server (URL, model, key header, reasoning toggle), all three structured modes, and provider-error surfacing.