1
0
Fork 0
polaris-task-force/src/collections/server/AiSettings.ts
Z8MB1E 7abca7185c feat(ai): add admin-configurable model connections
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.
2026-11-24 13:04:39 -05:00

62 lines
2.1 KiB
TypeScript

import type { GlobalConfig } from "payload";
import { requirePermission } from "@/utils/access-control/hasPermission";
/**
* Which model connection each AI surface uses.
*
* Blank means the env defaults (`UNSLOTH_URL`, `UNSLOTH_MODEL`,
* `UNSLOTH_API_KEY`), which is what ran before connections existed, so an empty
* global changes nothing. Surfaces are separate on purpose: dialogue drafting wants
* a model that follows a JSON schema, while a line rewrite only needs prose, and
* they can reasonably be different price points.
*
* Superuser-gated to match the connections it points at: choosing a connection for
* Compose repoints traffic that includes field content from every allowlisted
* collection, so this is not a dialogue-author permission.
*/
export const AiSettings: GlobalConfig = {
slug: "ai-settings",
label: "AI Settings",
admin: {
group: "Server",
description:
"Which model connection each AI surface uses. Leave a selector blank to use the Unsloth env defaults.",
},
access: {
read: requirePermission("system:admin-access"),
update: requirePermission("system:admin-access"),
},
fields: [
{
name: "composeConnection",
type: "relationship",
relationTo: "ai-connections",
hasMany: false,
admin: {
description:
"The AI Compose field assistant across every allowlisted collection. Blank uses the env defaults.",
},
},
{
name: "dialogueDraftConnection",
type: "relationship",
relationTo: "ai-connections",
hasMany: false,
admin: {
description:
"Whole-conversation NPC dialogue drafting. This one needs a model that reliably returns JSON in the configured structured mode. Blank uses the env defaults.",
},
},
{
name: "dialogueLineConnection",
type: "relationship",
relationTo: "ai-connections",
hasMany: false,
admin: {
description:
"Per-line rewrites (Rewrite, Shorten, Sharpen). Prose only, so a cheaper model is usually fine. Blank uses the env defaults.",
},
},
],
};