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:
amosblomqvist
2026-09-22 00:14:56 +03:00
parent f1373d5de5
commit f9e3a4c43d
26 changed files with 3529 additions and 912 deletions
+45 -19
View File
@@ -6,7 +6,7 @@ import {
type Models,
type StreamFunction,
} from "@earendil-works/pi-ai";
import { openrouterProvider } from "@earendil-works/pi-ai/providers/openrouter";
import { builtinProviders } from "@earendil-works/pi-ai/providers/all";
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
import { MODELS, type ModelRole } from "../../config.ts";
import type { ChatSettings } from "../core/contracts/chat.ts";
@@ -42,11 +42,18 @@ export function thinkingLevelFor(role: ModelRole): ThinkingLevel {
let collection: ReturnType<typeof createModels> | undefined;
/** The process-wide pi-ai Models collection (OpenRouter catalog). */
/** The process-wide pi-ai Models collection — EVERY builtin provider's
* catalog (model slots wave, 2026-09-20): engines and the picker resolve
* against all of pi-ai's per-provider catalogs, so a settings slot can pin
* any native model id. Auth is resolved per request by pi-ai itself from
* the environment (the keys seam reflects file-saved keys into env), and
* a provider with no key simply fails its stream with "not configured" —
* honest, late, and unreachable through the picker, which gates on
* keyPresent. */
export function piModels() {
if (!collection) {
collection = createModels();
collection.setProvider(openrouterProvider());
for (const provider of builtinProviders()) collection.setProvider(provider);
}
return collection;
}
@@ -56,23 +63,28 @@ export function openRouterApiKey(): string | undefined {
return process.env.OPENROUTER_API_KEY || undefined;
}
/** The production engine for one agent role. `settings` is the chat's
* model-control override (prober/teacher only — sub-agent callers pass
* nothing and get config.ts's pin): any absent field falls back to the
* config spec, and the thinking level is CLAMPED to the model's supported
* list (pi's thinkingLevelMap — e.g. GLM-5.3-flash accepts low|high|max),
* so a settings change between models can never send an invalid level. */
export function piEngineFor(role: ModelRole, settings?: ChatSettings): PiEngine {
/** A fully-resolved model pin — the shape every engine now comes from
* (model slots wave, 2026-09-20): the settings-resolution chain's output.
* Every field concrete; the CALLER resolved the chain (role override →
* slot → factory pin, or the chat's conversation chain). */
export interface ResolvedModelSpec {
provider: string;
id: string;
thinkingLevel: ThinkingLevel;
}
/** The production engine for a RESOLVED spec (model slots wave,
* 2026-09-20): provider-aware catalog lookup + thinking clamp. The API
* key is resolved by pi-ai itself from the environment per request (the
* keys seam reflects file-saved keys into env), so the engine never
* hardcodes OpenRouter's. */
export function piEngineForSpec(spec: ResolvedModelSpec): PiEngine {
const models: Models = piModels();
const spec = MODELS[role];
const modelId = settings?.model?.id ?? spec.model;
const providerId = settings?.model?.provider ?? "openrouter";
const model = models.getModel(providerId, modelId);
const model = models.getModel(spec.provider, spec.id);
if (!model) {
throw new Error(`${providerId} catalog has no model ${modelId} (role ${role})`);
throw new Error(`${spec.provider} catalog has no model ${spec.id}`);
}
const requested = settings?.thinkingLevel ?? thinkingLevelFor(role);
const level = clampThinkingLevel(model, requested);
const level = clampThinkingLevel(model, spec.thinkingLevel);
return {
model,
level,
@@ -83,7 +95,21 @@ export function piEngineFor(role: ModelRole, settings?: ChatSettings): PiEngine
models.streamSimple(requestModel, context, {
...options,
reasoning: level === "off" ? undefined : level,
apiKey: options?.apiKey ?? openRouterApiKey(),
}),
};
}
}
/** The production engine for one agent role — the LEGACY fallback path:
* the chat's model-control override over config.ts's pin. Production
* callers (chat-handler) now resolve the full chain (chat override →
* strong slot → factory pin) and call piEngineForSpec; this stays for
* direct config-pin fallbacks (sub-agents without an injected engine —
* the job runner injects one — and tests). */
export function piEngineFor(role: ModelRole, settings?: ChatSettings): PiEngine {
const spec = MODELS[role];
return piEngineForSpec({
provider: settings?.model?.provider ?? spec.provider,
id: settings?.model?.id ?? spec.model,
thinkingLevel: settings?.thinkingLevel ?? thinkingLevelFor(role),
});
}
+4 -1
View File
@@ -293,7 +293,10 @@ async function runSubmitAgent<T>(opts: {
onAssistantMessage: (message, step) =>
opts.onStep(
message.usage,
message.model,
// Provider-qualified stamp (model slots wave, 2026-09-20):
// pi reports the bare id; usage stamps carry provider:id to
// match the price table's uniform keys.
`${message.provider}:${message.model}`,
step,
message.stopReason,
message.content.filter((c) => c.type === "toolCall").map((c) => (c as { name: string }).name).join(", ") || "(none)",
+53 -20
View File
@@ -19,7 +19,7 @@ import {
type VisualToolInput,
} from "../../core/contracts.ts";
import { registerJobAbort, unregisterJobAbort } from "./aborts.ts";
import { MODELS } from "../../../config.ts";
import { MODELS, type ModelRole } from "../../../config.ts";
import type { ChatStore, JobStore } from "../../core/ports.ts";
import { runDemoBuilder } from "./demo-builder.ts";
import { runPdfReaderAgent } from "./pdf-reader.ts";
@@ -68,6 +68,14 @@ export interface JobRunnerDeps {
* flipped); rejects when delivery fails unrecoverably.
*/
deliver: (steer: SubagentSteerMessage) => Promise<void>;
/**
* Settings-resolved engine per role (model slots wave, 2026-09-20): the
* role-override → slot → factory-pin chain, read FRESH per job (the
* server wiring passes effectiveSettings-based resolution). Closes the
* old gap where per-role settings overrides never reached job engines.
* Test overrides (below) still win; absent → config pins.
*/
engineFor?: (role: ModelRole) => PiEngine;
}
export interface JobRunner {
@@ -89,18 +97,26 @@ interface LoopContext {
onUsage: (step: UsageStepLike) => void;
}
/** The OpenRouter model id for a job kind — JobUsage.model. The kind→role
* table mirrors MODELS in config.ts (research→researcher, demo→demoBuilder,
* visual→visualMaker); the id is taken from config so a price-table sync
* re-prices jobs for free. Model overrides (tests) still bill under the
* configured model — usage is what is being captured, not which instance.
* Context-injection jobs make NO model call ("" — onUsage never fires for
* them; the kind is deliberately absent from the runners below). */
/** The kind→role table (model slots wave, 2026-09-20): which ModelRole's
* engine each sub-agent kind runs — the engineFor injection key. */
const KIND_ROLE: Record<Exclude<JobKind, "graph-context">, ModelRole> = {
research: "researcher",
demo: "demoBuilder",
visual: "visualMaker",
pdf: "pdfReader",
};
/** The config-pin model stamp for a job kind — JobUsage.model when no
* engine was resolved (engineFor absent AND no test override). Stamps are
* provider-qualified ("provider:id") since the model slots wave, matching
* the price table's uniform keys. Context-injection jobs make NO model
* call ("" — onUsage never fires for them; the kind is deliberately
* absent from the runners below). */
const KIND_MODEL: Record<JobKind, string> = {
research: MODELS.researcher.model,
demo: MODELS.demoBuilder.model,
visual: MODELS.visualMaker.model,
pdf: MODELS.pdfReader.model,
research: `${MODELS.researcher.provider}:${MODELS.researcher.model}`,
demo: `${MODELS.demoBuilder.provider}:${MODELS.demoBuilder.model}`,
visual: `${MODELS.visualMaker.provider}:${MODELS.visualMaker.model}`,
pdf: `${MODELS.pdfReader.provider}:${MODELS.pdfReader.model}`,
"graph-context": "",
};
@@ -111,8 +127,10 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner {
* then writes through the job store so the poll sees progress). */
const usageTotals = new Map<string, JobUsage>();
function makeLoopContext(jobId: string, kind: JobKind): LoopContext {
const model = KIND_MODEL[kind];
function makeLoopContext(jobId: string, kind: JobKind, engine?: PiEngine): LoopContext {
// Provider-qualified stamp (model slots wave): the engine's actual
// model, else the legacy config-pin id (see KIND_MODEL).
const model = engine ? `${engine.model.provider}:${engine.model.id}` : KIND_MODEL[kind];
// Real-kill wiring (2026-09 abort semantics): the controller is what
// the abort endpoint fires (agent/subagents/aborts.ts registry); the
// signal rides into every model call of the loop.
@@ -143,14 +161,28 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner {
* them below rather than guessing wrong. */
// Each kind's runner narrows the union input itself (dispatch-time inputs
// are kind-correct by construction; transcript fallbacks are cast after).
/** The engine one job runs on: test override first (the old seam —
* tests swap scripted engines without touching deps), then the injected
* settings-resolved engineFor (model slots wave, 2026-09-20), else
* undefined → the sub-agent factories fall back to config pins. */
function pickEngine(kind: Exclude<JobKind, "graph-context">): PiEngine | undefined {
const testOverride: Record<string, { current?: PiEngine } | undefined> = {
researcher: researchModelOverride,
demoBuilder: demoModelOverride,
visualMaker: visualModelOverride,
pdfReader: pdfModelOverride,
};
return testOverride[KIND_ROLE[kind]]?.current ?? deps.engineFor?.(KIND_ROLE[kind]);
}
const KIND_RUNNERS: Record<
Exclude<JobKind, "graph-context">,
(jobId: string, input: JobToolInput | undefined, loop: LoopContext) => Promise<void>
(jobId: string, engine: PiEngine | undefined, input: JobToolInput | undefined, loop: LoopContext) => Promise<void>
> = {
research: (jobId, input, loop) => runResearchJob(jobId, researchModelOverride.current, input, loop),
demo: (jobId, input, loop) => runDemoJob(jobId, demoModelOverride.current, input, loop),
visual: (jobId, input, loop) => runVisualJob(jobId, visualModelOverride.current, input, loop),
pdf: (jobId, input, loop) => runPdfJob(jobId, pdfModelOverride.current, input, loop),
research: (jobId, engine, input, loop) => runResearchJob(jobId, engine, input, loop),
demo: (jobId, engine, input, loop) => runDemoJob(jobId, engine, input, loop),
visual: (jobId, engine, input, loop) => runVisualJob(jobId, engine, input, loop),
pdf: (jobId, engine, input, loop) => runPdfJob(jobId, engine, input, loop),
};
async function runJob(jobId: string, input?: JobToolInput): Promise<void> {
@@ -173,7 +205,8 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner {
return;
}
try {
await run(jobId, input, makeLoopContext(jobId, job.kind));
const engine = pickEngine(job.kind);
await run(jobId, engine, input, makeLoopContext(jobId, job.kind, engine));
} finally {
unregisterJobAbort(jobId);
// The last usage write may have been mid-flight when the loop died;