feat(models): strong/cheap/vision model slots + multi-provider engine federation
Model slots wave (2026-09-20), chunk B:
- settings: new `models` section {strong, cheap, vision} slots (each
field independent, same shape as a role override); ROLE_SLOT maps all
nine roles to tiers (strong=prober+teacher, cheap=researcher/
demoBuilder/extractor/auditor/labeler, vision=visualMaker+pdfReader);
resolution chain per role: override → slot → factory pin.
- config: ModelSpec gains provider (all pins openrouter, the BYOK
default); MODEL_PINS in stores.ts now spans all nine roles (the strong
slot's factory rep IS the prober pin).
- engine: piModels() registers EVERY builtin provider catalog;
piEngineForSpec(resolved spec) is the new engine factory (pi-ai
resolves per-provider keys from env itself); piEngineFor stays as the
config-pin fallback. chat-handler resolves the conversation chain
(chat override → strong slot → pin) and injects engineFor into the job
runner, closing the old gap where per-role overrides never reached
job engines.
- /api/models: models federate ONLY keyed providers (providers surface
with keyPresent, env-wins, fresh per call); slots (resolved tier pins)
join rolePins; catalogDefaultsFor inherits the strong slot; the models
settings section gets the same catalog gate as chat.
- pricing: PRICES/CONTEXT_LIMITS uniformly keyed provider:id
(regenerated from ALL pi-ai catalogs, 1354 keys); usage stamps
(TurnUsage, JobUsage, extraction stages) switch to provider:id —
owner decision: no legacy compat, old stamps render unpriced.
This commit is contained in:
+105
-11
@@ -16,19 +16,22 @@
|
||||
|
||||
import { getSupportedThinkingLevels, type Api, type Model } from "@earendil-works/pi-ai";
|
||||
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
||||
import { MODELS } from "../../config.ts";
|
||||
import { piModels } from "../agent/models.ts";
|
||||
import { settingsStore } from "../store/settings.ts";
|
||||
import { effectiveSettings } from "./stores.ts";
|
||||
import { keyValue } from "./keys.ts";
|
||||
import type { ChatSettings } from "../core/contracts/chat.ts";
|
||||
import type {
|
||||
CatalogDefaults,
|
||||
CatalogProvider,
|
||||
ModelCatalogEntry,
|
||||
ModelCatalogResponse,
|
||||
RolePins,
|
||||
SlotPins,
|
||||
} from "../core/contracts/models.ts";
|
||||
import type { EffectiveModelSpec } from "../core/settings-resolution.ts";
|
||||
import { BACKGROUND_MODEL_ROLES, type BackgroundModelRole } from "../core/settings.ts";
|
||||
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";
|
||||
|
||||
/** The picker's client-safe model entry (contract: core/contracts/models.ts). */
|
||||
export type ModelDto = ModelCatalogEntry;
|
||||
@@ -37,10 +40,10 @@ export type ModelDto = ModelCatalogEntry;
|
||||
* chat section in, CatalogDefaults out; `catalogDefaults()` below is the
|
||||
* fs wrapper). Absent fields (or no record — BYOK first boot) fall back
|
||||
* to the config pin, exactly as before. */
|
||||
export function catalogDefaultsFor(chat?: ChatSettings): CatalogDefaults {
|
||||
export function catalogDefaultsFor(chat: ChatSettings | undefined, strong: EffectiveSlot): CatalogDefaults {
|
||||
return {
|
||||
model: chat?.model ?? { provider: "openrouter", id: MODELS.prober.model },
|
||||
thinkingLevel: (chat?.thinkingLevel ?? MODELS.prober.effort) as ThinkingLevel,
|
||||
model: chat?.model ?? { provider: strong.provider, id: strong.id },
|
||||
thinkingLevel: chat?.thinkingLevel ?? strong.thinkingLevel,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,7 +52,7 @@ export function catalogDefaultsFor(chat?: ChatSettings): CatalogDefaults {
|
||||
* the settings record's `chat` section; the picker's defaults row shows
|
||||
* it once chosen. */
|
||||
export function catalogDefaults(): CatalogDefaults {
|
||||
return catalogDefaultsFor(settingsStore.readSettings()?.chat);
|
||||
return catalogDefaultsFor(settingsStore.readSettings()?.chat, effectiveSettings().models.strong);
|
||||
}
|
||||
|
||||
/** Project the resolved per-role specs to the client DTO (pure — the
|
||||
@@ -77,6 +80,42 @@ export function catalogRolePins(): RolePins {
|
||||
return rolePinsFor(effectiveSettings().agent.models);
|
||||
}
|
||||
|
||||
/** The three slot tiers' RESOLVED pins (model slots wave, 2026-09-20) —
|
||||
* effectiveSettings().models verbatim: slot over factory rep pin, the
|
||||
* same resolution the engine wiring uses, so the Models settings page's
|
||||
* captions cannot drift from what actually runs. */
|
||||
export function catalogSlots(): SlotPins {
|
||||
const slots = effectiveSettings().models;
|
||||
const pins = {} as SlotPins;
|
||||
for (const name of ["strong", "cheap", "vision"] as const) {
|
||||
const slot: EffectiveSlot = slots[name];
|
||||
pins[name] = {
|
||||
model: { provider: slot.provider, id: slot.id },
|
||||
thinkingLevel: slot.thinkingLevel,
|
||||
};
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
/** Project one catalog model to the client DTO. */
|
||||
export function toModelDto(model: Model<Api>): ModelDto {
|
||||
return {
|
||||
@@ -102,12 +141,20 @@ export function toModelDto(model: Model<Api>): ModelDto {
|
||||
* defaults a settings-less chat runs + the background roles' resolved
|
||||
* pins (the settings UI's per-role default captions). */
|
||||
export function modelCatalog(): ModelCatalogResponse {
|
||||
const keyed = keyedProviderIds();
|
||||
const models = piModels()
|
||||
.getProviders()
|
||||
.filter((p) => keyed.has(p.id))
|
||||
.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)));
|
||||
return { models, defaults: catalogDefaults(), rolePins: catalogRolePins() };
|
||||
return {
|
||||
models,
|
||||
defaults: catalogDefaults(),
|
||||
rolePins: catalogRolePins(),
|
||||
slots: catalogSlots(),
|
||||
providers: catalogProviders(),
|
||||
};
|
||||
}
|
||||
|
||||
/** One validated outcome for a settings write. */
|
||||
@@ -133,8 +180,11 @@ export function validateChatSettings(settings: ChatSettings): SettingsValidation
|
||||
}
|
||||
}
|
||||
if (settings.thinkingLevel !== undefined) {
|
||||
const provider = settings.model?.provider ?? "openrouter";
|
||||
const id = settings.model?.id ?? MODELS.prober.model;
|
||||
// The conversation chain's fallback is the strong slot (model slots
|
||||
// wave, 2026-09-20) — the model a settings-less chat would run.
|
||||
const strong = effectiveSettings().models.strong;
|
||||
const provider = settings.model?.provider ?? strong.provider;
|
||||
const id = settings.model?.id ?? strong.id;
|
||||
const model = piModels().getModel(provider, id);
|
||||
if (!model) return { ok: false, error: `No model "${id}" in the ${provider} catalog` };
|
||||
const supported = getSupportedThinkingLevels(model);
|
||||
@@ -147,4 +197,48 @@ export function validateChatSettings(settings: ChatSettings): SettingsValidation
|
||||
}
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
}
|
||||
/** Validate a models-section write (PUT /api/settings/models) against the
|
||||
* catalog — the same gate the chat section gets. Model swaps must name a
|
||||
* real provider+model; a thinkingLevel must be supported by the model the
|
||||
* slot would resolve to (the slot's own model, else the factory rep pin).
|
||||
* 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) {
|
||||
const provider = slot.model?.provider ?? effectiveSettings().models[name].provider;
|
||||
const id = slot.model?.id ?? effectiveSettings().models[name].id;
|
||||
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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user