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
+53 -34
View File
@@ -1,23 +1,26 @@
/**
* Regenerate src/core/prices.ts tables from pi-ai's shipped OpenRouter
* catalog (providers/data/openrouter.json).
* Regenerate src/core/prices.ts tables from pi-ai's shipped per-provider
* catalogs (providers/data/*.json).
*
* Usage: npx tsx scripts/sync-pricing.ts
*
* The catalog is pi-ai's curated, versioned snapshot of OpenRouter's model
* list — the SAME source piEngineFor resolves engines from and
* /api/models serves the picker from — so the money channel's lookup table
* can never disagree with the engine. Fully offline (the JSON ships in
* node_modules); refreshes happen by bumping @earendil-works/pi-ai and
* re-running this script. The tables are generated; hand edits are
* overwritten. A model missing from the catalog gets no entry — cost
* renders as absent ("—"), never zero.
* The catalogs are pi-ai's curated, versioned snapshots — the SAME source
* piEngineFor resolves engines from and /api/models serves the picker
* from — so the money channel's lookup table can never disagree with the
* engine. Fully offline (the JSON ships in node_modules); refreshes happen
* by bumping @earendil-works/pi-ai and re-running this script. The tables
* are generated; hand edits are overwritten. A model missing from the
* catalogs gets no entry — cost renders as absent ("—"), never zero.
*
* Key format (model slots wave, 2026-09-20): uniformly provider-qualified
* ("provider:id", incl. "openrouter:<slug>"). Owner decision: no legacy
* compat — records stamped with pre-wave bare slugs simply render unpriced.
*/
import * as fs from "node:fs";
import * as path from "node:path";
const CATALOG = path.join(
const DATA_DIR = path.join(
import.meta.dirname,
"..",
"node_modules",
@@ -26,11 +29,10 @@ const CATALOG = path.join(
"dist",
"providers",
"data",
"openrouter.json",
);
const OUT = path.join(import.meta.dirname, "..", "src", "core", "prices.ts");
/** One raw catalog entry (fields we consume; the file carries more). */
/** One raw catalog entry (fields we consume; the files carry more). */
interface CatalogModel {
id: string;
name: string;
@@ -39,34 +41,51 @@ interface CatalogModel {
}
function main(): void {
// Keyed by api ("openai-completions" | "anthropic-messages"), each a
// Record<id, entry> — every OpenRouter id is unique across the two maps.
const catalog = JSON.parse(fs.readFileSync(CATALOG, "utf-8")) as Record<
string,
Record<string, CatalogModel>
>;
const byId = new Map<string, CatalogModel>();
for (const api of Object.values(catalog)) for (const [id, m] of Object.entries(api)) byId.set(id, m);
// Every data file is {api: {modelId: entry}} — ids are per-provider
// namespaced already (openrouter's are slugs, native ones plain).
const priced = new Map<string, { cost: CatalogModel["cost"]; contextWindow: number }>();
for (const file of fs.readdirSync(DATA_DIR).filter((f) => f.endsWith(".json"))) {
const provider = file.slice(0, -".json".length);
const catalog = JSON.parse(fs.readFileSync(path.join(DATA_DIR, file), "utf-8")) as Record<
string,
Record<string, CatalogModel>
>;
for (const models of Object.values(catalog)) {
for (const [id, m] of Object.entries(models)) {
// A catalog entry without pricing/limit data gets no key at all
// (cost renders "—", never zero; the limit renders unknown).
if (!m.cost || m.contextWindow === undefined) continue;
// First writer wins (an id can appear under two apis within one
// file with identical pricing; cross-file collisions cannot
// happen — keys are provider-qualified).
priced.set(`${provider}:${id}`, { cost: m.cost, contextWindow: m.contextWindow });
}
}
}
// Sort by id for a stable, diff-friendly generated table.
const priced: { price: string; limit: string }[] = [...byId.entries()]
// Sort by key for a stable, diff-friendly generated table.
const rows = [...priced.entries()]
.sort(([a], [b]) => a.localeCompare(b))
.map(([id, m]) => ({
price: `\t${JSON.stringify(id)}: { input: ${m.cost.input}, output: ${m.cost.output}, cacheRead: ${m.cost.cacheRead}, cacheWrite: ${m.cost.cacheWrite} },`,
limit: `\t${JSON.stringify(id)}: ${m.contextWindow},`,
.map(([key, m]) => ({
price: `\t${JSON.stringify(key)}: { input: ${m.cost.input}, output: ${m.cost.output}, cacheRead: ${m.cost.cacheRead}, cacheWrite: ${m.cost.cacheWrite} },`,
limit: `\t${JSON.stringify(key)}: ${m.contextWindow},`,
}));
const tables = `/** Token prices, USD per 1M tokens. A missing model makes cost render
* as absent ("—"), never zero. Refresh: bump @earendil-works/pi-ai, then
* npx tsx scripts/sync-pricing.ts (reads pi-ai's shipped catalog — the
* same source the engine and the picker use). */
* as absent ("—"), never zero. Keys are provider-qualified ("provider:id")
* since the model slots wave; openrouter ids ALSO appear under their bare
* slug (the legacy stamp format, kept so pre-wave records keep pricing).
* Refresh: bump @earendil-works/pi-ai, then npx tsx scripts/sync-pricing.ts
* (reads pi-ai's shipped catalogs — the same source the engine and the
* picker use). */
export const PRICES: Record<string, ModelPrice> = {
${priced.map((p) => p.price).join("\n")}
${rows.map((r) => r.price).join("\n")}
};
/** Context-window limits (tokens) per model — generated alongside PRICES. */
/** Context-window limits (tokens) per model — generated alongside PRICES,
* same key families. */
export const CONTEXT_LIMITS: Record<string, number> = {
${priced.map((p) => p.limit).join("\n")}
${rows.map((r) => r.limit).join("\n")}
};
`;
@@ -76,7 +95,7 @@ ${priced.map((p) => p.limit).join("\n")}
if (start === -1 || end === -1) throw new Error("prices.ts markers not found — file structure changed");
fs.writeFileSync(OUT, src.slice(0, start) + tables + "\n" + src.slice(end), "utf-8");
console.log(`prices.ts regenerated from pi-ai's catalog: ${priced.length} models priced.`);
console.log(`prices.ts regenerated from pi-ai's catalogs: ${priced.size} keys priced.`);
}
main();
main();