2026-09-12 06:02:40 +03:00
|
|
|
/**
|
2026-09-22 16:40:31 +03:00
|
|
|
* The model catalog's server surface (model-control wave, 2026-09-12;
|
|
|
|
|
* federated since the model-slots wave, 2026-09-20): pi-ai's catalog for
|
|
|
|
|
* every KEYED provider, projected to client-safe DTOs for the picker (GET
|
|
|
|
|
* /api/models), plus the validation gate for settings writes (PUT
|
|
|
|
|
* /api/settings/* — the per-chat settings PUT is retired from the UI;
|
|
|
|
|
* legacy records' per-chat overrides still resolve server-side).
|
2026-09-12 06:02:40 +03:00
|
|
|
*
|
|
|
|
|
* pi-ai owns ALL the data: the catalog (id/name/cost/context/modality/
|
|
|
|
|
* thinkingLevelMap) and the per-model thinking truth
|
|
|
|
|
* (getSupportedThinkingLevels / clampThinkingLevel). This module only
|
|
|
|
|
* projects and validates — no second registry to drift out of sync.
|
|
|
|
|
*
|
2026-09-22 16:40:31 +03:00
|
|
|
* Federation: `getModels()` is called per keyed provider (federated, the
|
|
|
|
|
* model-slots wave), so a provider appears in the picker exactly when its
|
|
|
|
|
* key is present — piModels() in src/agent/models.ts owns the provider
|
|
|
|
|
* set; nothing here changes.
|
2026-09-12 06:02:40 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { getSupportedThinkingLevels, type Api, type Model } from "@earendil-works/pi-ai";
|
|
|
|
|
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
|
|
|
import { piModels } from "../agent/models.ts";
|
2026-09-19 18:05:30 +03:00
|
|
|
import { settingsStore } from "../store/settings.ts";
|
2026-09-19 22:28:11 +03:00
|
|
|
import { effectiveSettings } from "./stores.ts";
|
2026-09-22 00:14:56 +03:00
|
|
|
import { keyValue } from "./keys.ts";
|
2026-09-12 06:02:40 +03:00
|
|
|
import type { ChatSettings } from "../core/contracts/chat.ts";
|
|
|
|
|
import type {
|
|
|
|
|
CatalogDefaults,
|
2026-09-22 00:14:56 +03:00
|
|
|
CatalogProvider,
|
2026-09-12 06:02:40 +03:00
|
|
|
ModelCatalogEntry,
|
|
|
|
|
ModelCatalogResponse,
|
2026-09-19 22:28:11 +03:00
|
|
|
RolePins,
|
2026-09-22 00:14:56 +03:00
|
|
|
SlotPins,
|
2026-09-12 06:02:40 +03:00
|
|
|
} from "../core/contracts/models.ts";
|
2026-09-22 00:14:56 +03:00
|
|
|
import type { EffectiveModelSpec, EffectiveSlot } from "../core/settings-resolution.ts";
|
|
|
|
|
import { AUTH_PROVIDER_KEYS } from "../core/auth-keys.ts";
|
|
|
|
|
import { BACKGROUND_MODEL_ROLES, type BackgroundModelRole, type ModelSlotName, type ModelsSettings } from "../core/settings.ts";
|
2026-09-12 06:02:40 +03:00
|
|
|
|
|
|
|
|
/** The picker's client-safe model entry (contract: core/contracts/models.ts). */
|
|
|
|
|
export type ModelDto = ModelCatalogEntry;
|
|
|
|
|
|
2026-09-22 16:58:25 +03:00
|
|
|
/** The conversation agents' resolved default (pure form — the record's chat
|
|
|
|
|
* section + the strong slot in, CatalogDefaults out; `catalogDefaults()`
|
|
|
|
|
* below is the fs wrapper). BYOK (owner, 2026-09-22): there is no factory
|
|
|
|
|
* floor — when neither the chat section nor the strong slot names a
|
|
|
|
|
* model, the default is NULL and the turn gate refuses with the 503
|
|
|
|
|
* models_not_configured envelope. An unset thinkingLevel resolves from
|
|
|
|
|
* DEFAULT_THINKING_ORDER at engine build (reported as absent here). */
|
|
|
|
|
export function catalogDefaultsFor(
|
|
|
|
|
chat: ChatSettings | undefined,
|
|
|
|
|
strong: EffectiveSlot | null,
|
|
|
|
|
): CatalogDefaults | null {
|
|
|
|
|
const model = chat?.model ?? (strong ? { provider: strong.provider, id: strong.id } : undefined);
|
|
|
|
|
if (!model) return null;
|
|
|
|
|
const level = chat?.thinkingLevel ?? strong?.thinkingLevel;
|
2026-09-12 06:02:40 +03:00
|
|
|
return {
|
2026-09-22 16:58:25 +03:00
|
|
|
model,
|
|
|
|
|
...(level !== undefined ? { thinkingLevel: level } : {}),
|
2026-09-12 06:02:40 +03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 16:58:25 +03:00
|
|
|
/** The conversation agents' resolved default — what a settings-less chat
|
|
|
|
|
* runs. (settings-storage wave, 2026-09-19): the user's picked default
|
|
|
|
|
* rides in the settings record's `chat` section; the picker's defaults
|
|
|
|
|
* row shows it once chosen. */
|
|
|
|
|
export function catalogDefaults(): CatalogDefaults | null {
|
2026-09-22 00:14:56 +03:00
|
|
|
return catalogDefaultsFor(settingsStore.readSettings()?.chat, effectiveSettings().models.strong);
|
2026-09-19 18:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-19 22:28:11 +03:00
|
|
|
/** Project the resolved per-role specs to the client DTO (pure — the
|
|
|
|
|
* settings UI's "Default: <actual pinned model>" caption reads this).
|
2026-09-22 16:58:25 +03:00
|
|
|
* BYOK (owner, 2026-09-22): an empty chain projects to NULL — the caption
|
|
|
|
|
* honestly shows "not configured", never a phantom default. */
|
|
|
|
|
export function rolePinsFor(
|
|
|
|
|
models: Record<BackgroundModelRole, EffectiveModelSpec | undefined>,
|
|
|
|
|
): RolePins {
|
2026-09-19 22:28:11 +03:00
|
|
|
const pins = {} as RolePins;
|
|
|
|
|
for (const role of BACKGROUND_MODEL_ROLES) {
|
|
|
|
|
const spec = models[role];
|
2026-09-22 16:58:25 +03:00
|
|
|
pins[role] = spec
|
|
|
|
|
? {
|
|
|
|
|
model: { provider: spec.provider, id: spec.id },
|
|
|
|
|
...(spec.thinkingLevel !== undefined ? { thinkingLevel: spec.thinkingLevel } : {}),
|
|
|
|
|
}
|
|
|
|
|
: null;
|
2026-09-19 22:28:11 +03:00
|
|
|
}
|
|
|
|
|
return pins;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 16:58:25 +03:00
|
|
|
/** The six background roles' RESOLVED pins: the user's agent-models
|
|
|
|
|
* override → tier slot → nothing — the same resolution the agent wiring
|
|
|
|
|
* uses (src/server/stores.ts effectiveSettings), so the caption can
|
|
|
|
|
* never drift from what actually runs. */
|
2026-09-19 22:28:11 +03:00
|
|
|
export function catalogRolePins(): RolePins {
|
|
|
|
|
return rolePinsFor(effectiveSettings().agent.models);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 16:58:25 +03:00
|
|
|
/** The three slot tiers' RESOLVED pins — effectiveSettings().models
|
|
|
|
|
* verbatim: the same resolution the engine wiring uses, so the Models
|
|
|
|
|
* settings page's captions cannot drift from what actually runs. NULL
|
|
|
|
|
* per tier that holds no model. */
|
2026-09-22 00:14:56 +03:00
|
|
|
export function catalogSlots(): SlotPins {
|
|
|
|
|
const slots = effectiveSettings().models;
|
|
|
|
|
const pins = {} as SlotPins;
|
|
|
|
|
for (const name of ["strong", "cheap", "vision"] as const) {
|
2026-09-22 16:58:25 +03:00
|
|
|
const slot: EffectiveSlot | null = slots[name];
|
|
|
|
|
pins[name] = slot
|
|
|
|
|
? {
|
|
|
|
|
model: { provider: slot.provider, id: slot.id },
|
|
|
|
|
...(slot.thinkingLevel !== undefined ? { thinkingLevel: slot.thinkingLevel } : {}),
|
|
|
|
|
}
|
|
|
|
|
: null;
|
2026-09-22 00:14:56 +03:00
|
|
|
}
|
|
|
|
|
return pins;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The providers surface (model slots wave, 2026-09-20): one row per
|
|
|
|
|
* plain-key model provider (core/auth-keys.ts's pi-ai-derived table),
|
|
|
|
|
* with keyPresent read env-wins through the keys seam, fresh per call —
|
|
|
|
|
* saving a key immediately federates the provider into the picker. */
|
|
|
|
|
export function catalogProviders(): CatalogProvider[] {
|
|
|
|
|
return AUTH_PROVIDER_KEYS.map((p) => ({
|
|
|
|
|
id: p.id,
|
|
|
|
|
envVar: p.envVar,
|
|
|
|
|
keyPresent: keyValue(p.envVar) !== undefined,
|
|
|
|
|
})).sort((a, b) => a.id.localeCompare(b.id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The providers whose key is present — the picker federates exactly
|
|
|
|
|
* these (a keyless provider's models are unreachable by design; the
|
|
|
|
|
* Keys page is the discovery surface, same discipline as search). */
|
|
|
|
|
function keyedProviderIds(): Set<string> {
|
|
|
|
|
return new Set(catalogProviders().filter((p) => p.keyPresent).map((p) => p.id));
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 06:02:40 +03:00
|
|
|
/** Project one catalog model to the client DTO. */
|
|
|
|
|
export function toModelDto(model: Model<Api>): ModelDto {
|
|
|
|
|
return {
|
|
|
|
|
id: model.id,
|
|
|
|
|
name: model.name,
|
|
|
|
|
provider: model.provider,
|
|
|
|
|
reasoning: model.reasoning,
|
|
|
|
|
input: [...model.input],
|
|
|
|
|
supportedLevels: getSupportedThinkingLevels(model),
|
|
|
|
|
cost: {
|
|
|
|
|
input: model.cost.input,
|
|
|
|
|
output: model.cost.output,
|
|
|
|
|
cacheRead: model.cost.cacheRead,
|
|
|
|
|
cacheWrite: model.cost.cacheWrite,
|
|
|
|
|
},
|
|
|
|
|
contextWindow: model.contextWindow,
|
|
|
|
|
maxTokens: model.maxTokens,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The full picker payload: every model of every configured provider
|
|
|
|
|
* (sorted by provider then id — stable, diff-friendly) + the config
|
2026-09-19 22:28:11 +03:00
|
|
|
* defaults a settings-less chat runs + the background roles' resolved
|
|
|
|
|
* pins (the settings UI's per-role default captions). */
|
2026-09-12 06:02:40 +03:00
|
|
|
export function modelCatalog(): ModelCatalogResponse {
|
2026-09-22 00:14:56 +03:00
|
|
|
const keyed = keyedProviderIds();
|
2026-09-12 06:02:40 +03:00
|
|
|
const models = piModels()
|
|
|
|
|
.getProviders()
|
2026-09-22 00:14:56 +03:00
|
|
|
.filter((p) => keyed.has(p.id))
|
2026-09-12 06:02:40 +03:00
|
|
|
.flatMap((p) => piModels().getModels(p.id))
|
|
|
|
|
.map(toModelDto)
|
|
|
|
|
.sort((a, b) => (a.provider === b.provider ? a.id.localeCompare(b.id) : a.provider.localeCompare(b.provider)));
|
2026-09-22 00:14:56 +03:00
|
|
|
return {
|
|
|
|
|
models,
|
|
|
|
|
defaults: catalogDefaults(),
|
|
|
|
|
rolePins: catalogRolePins(),
|
|
|
|
|
slots: catalogSlots(),
|
|
|
|
|
providers: catalogProviders(),
|
|
|
|
|
};
|
2026-09-12 06:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** One validated outcome for a settings write. */
|
|
|
|
|
export type SettingsValidation =
|
|
|
|
|
| { ok: true }
|
|
|
|
|
| { ok: false; error: string; supported?: ThinkingLevel[] };
|
|
|
|
|
|
|
|
|
|
/** Validate a picker write against the catalog (the settings route's gate).
|
|
|
|
|
* `settings` is replace-whole: absent fields mean "config default".
|
|
|
|
|
* - model: provider must be configured, id must exist in its catalog.
|
|
|
|
|
* - thinkingLevel: must be in the resolved model's supported list —
|
|
|
|
|
* the model the picker writes against (settings.model, else config's
|
|
|
|
|
* prober/teacher pin; one controller, so the two specs share it). */
|
|
|
|
|
export function validateChatSettings(settings: ChatSettings): SettingsValidation {
|
|
|
|
|
const providers = piModels().getProviders().map((p) => p.id);
|
|
|
|
|
if (settings.model) {
|
|
|
|
|
const { provider, id } = settings.model;
|
|
|
|
|
if (!providers.includes(provider)) {
|
|
|
|
|
return { ok: false, error: `Unknown provider "${provider}" — configured: ${providers.join(", ")}` };
|
|
|
|
|
}
|
|
|
|
|
if (!piModels().getModel(provider, id)) {
|
|
|
|
|
return { ok: false, error: `No model "${id}" in the ${provider} catalog` };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (settings.thinkingLevel !== undefined) {
|
2026-09-22 16:58:25 +03:00
|
|
|
// The conversation chain's fallback is the strong slot — the model
|
|
|
|
|
// a settings-less chat would run. BYOK (owner, 2026-09-22): with no
|
|
|
|
|
// model to check against (no chat.model, no strong slot), the level
|
|
|
|
|
// rides unchecked — it clamps against whatever the chain later holds.
|
2026-09-22 00:14:56 +03:00
|
|
|
const strong = effectiveSettings().models.strong;
|
2026-09-22 16:58:25 +03:00
|
|
|
const provider = settings.model?.provider ?? strong?.provider;
|
|
|
|
|
const id = settings.model?.id ?? strong?.id;
|
|
|
|
|
if (!provider || !id) return { ok: true };
|
2026-09-12 06:02:40 +03:00
|
|
|
const model = piModels().getModel(provider, id);
|
|
|
|
|
if (!model) return { ok: false, error: `No model "${id}" in the ${provider} catalog` };
|
|
|
|
|
const supported = getSupportedThinkingLevels(model);
|
|
|
|
|
if (!supported.includes(settings.thinkingLevel)) {
|
|
|
|
|
return {
|
|
|
|
|
ok: false,
|
|
|
|
|
error: `${id} does not support thinking level "${settings.thinkingLevel}"`,
|
|
|
|
|
supported,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { ok: true };
|
2026-09-22 00:14:56 +03:00
|
|
|
}
|
|
|
|
|
/** Validate a models-section write (PUT /api/settings/models) against the
|
2026-09-22 16:58:25 +03:00
|
|
|
* catalog — the same gate the chat section gets. A slot must NAME a real
|
|
|
|
|
* provider+model (owner, 2026-09-22: no factory floor exists, so a level
|
|
|
|
|
* alone can never run — the parser rejects it, this gate re-checks the
|
|
|
|
|
* model); a thinkingLevel must be supported by the slot's own model.
|
2026-09-22 00:14:56 +03:00
|
|
|
* keyPresent is NOT gated here (a stored pin for a provider whose key
|
|
|
|
|
* lapsed stays valid — it fails honestly at stream time, and the picker
|
|
|
|
|
* stops offering it). */
|
|
|
|
|
export function validateModelsSettings(settings: ModelsSettings): SettingsValidation {
|
|
|
|
|
for (const name of ["strong", "cheap", "vision"] as const) {
|
|
|
|
|
const slot = settings[name];
|
|
|
|
|
if (!slot) continue;
|
|
|
|
|
const verdict = validateSlotSpec(name, slot);
|
|
|
|
|
if (!verdict.ok) return verdict;
|
|
|
|
|
}
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateSlotSpec(name: ModelSlotName, slot: { model?: { provider: string; id: string }; thinkingLevel?: ThinkingLevel }): SettingsValidation {
|
|
|
|
|
const providers = piModels().getProviders().map((p) => p.id);
|
|
|
|
|
if (slot.model) {
|
|
|
|
|
const { provider, id } = slot.model;
|
|
|
|
|
if (!providers.includes(provider)) {
|
|
|
|
|
return { ok: false, error: `Unknown provider "${provider}" — configured: ${providers.join(", ")}` };
|
|
|
|
|
}
|
|
|
|
|
if (!piModels().getModel(provider, id)) {
|
|
|
|
|
return { ok: false, error: `No model "${id}" in the ${provider} catalog` };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (slot.thinkingLevel !== undefined) {
|
2026-09-22 16:58:25 +03:00
|
|
|
// The write-side parser requires slot.model (owner, 2026-09-22 — a
|
|
|
|
|
// level-only slot can never run), so this fallback is tolerance for
|
|
|
|
|
// future callers; with no model to check against, accept (the
|
|
|
|
|
// engine build clamps whatever the chain later holds).
|
|
|
|
|
const resolved = effectiveSettings().models[name];
|
|
|
|
|
const provider = slot.model?.provider ?? resolved?.provider;
|
|
|
|
|
const id = slot.model?.id ?? resolved?.id;
|
|
|
|
|
if (!provider || !id) return { ok: true };
|
2026-09-22 00:14:56 +03:00
|
|
|
const model = piModels().getModel(provider, id);
|
|
|
|
|
if (!model) return { ok: false, error: `No model "${id}" in the ${provider} catalog` };
|
|
|
|
|
const supported = getSupportedThinkingLevels(model);
|
|
|
|
|
if (!supported.includes(slot.thinkingLevel)) {
|
|
|
|
|
return {
|
|
|
|
|
ok: false,
|
|
|
|
|
error: `${id} does not support thinking level "${slot.thinkingLevel}"`,
|
|
|
|
|
supported,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}
|