Study/server/ai/providers/catalog.ts
Elijah 25bc03ea75
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 29s
Fourth revision: OpenRouter, live tool stages, Explain layout, share sections
- Interactive blocks are encouraged by default; Explain gets a reduced library
  and never writes practice questions.
- Assistant tool steps breathe while running, show live stages from generation
  and the Python check, and read in the past tense when done.
- The assistant asks before searching the web when class materials do not
  cover a question; clarifying questions list every class's exams and never
  ask about format (always NCLEX).
- Explain opens beside the question from 900px wide, moving the question left
  while open and back on the next one.
- Generate dialog drops the practice set/exam switch (presets cover it) and the
  Generate buttons lose their icon.
- Python check notes are green when every check passed, applied fixes neutral.
- Share pages group by exam, then Quizzes, Flashcards, Notes, Class Materials,
  hiding empty groups; the class page uses the same order. "Documents" is now
  "Class Materials".
- OpenRouter provider replaces the Ollama type; migration 6 turns Ollama
  connections into OpenAI-compatible ones at /v1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:54:39 -07:00

276 lines
17 KiB
TypeScript

/**
* Static defaults for well-known models: capabilities, limits and list prices.
*
* Discovery APIs return ids (and sometimes limits) but almost never prices or
* feature flags, so `listModels` merges its results with this table. Unknown
* models simply get no defaults — nothing here is required for a model to work.
*
* Everything below was read from the official pages listed per provider on the
* dates noted. Prices are USD per million tokens, standard (non-batch,
* non-priority) tier, short-context tier where a provider has several. A field
* is omitted when it could not be confirmed from the page — do not guess.
*/
import type { DiscoveredModel, ModelCapabilities, ProviderKind } from '../types.ts';
export const CATALOG_UPDATED = '2026-11-10';
type Entry = Omit<DiscoveredModel, 'id' | 'capabilities'> & { capabilities?: Partial<ModelCapabilities> };
/* ------------------------------------------------------------------ */
/* OpenAI */
/* Prices: https://developers.openai.com/api/docs/pricing (fetched 2026-11-11)
/* Limits: https://developers.openai.com/api/docs/models (fetched 2026-11-11;
/* only the four current flagship models list limits there — the
/* limits on older models below are the long-published values and were
/* not re-fetched)
/* Reasoning models reject a custom `temperature`, hence temperature:false.
/* ------------------------------------------------------------------ */
const OA_REASONING: Partial<ModelCapabilities> = { tools: true, vision: true, pdf: true, reasoning: true, temperature: false };
const OA_CLASSIC: Partial<ModelCapabilities> = { tools: true, vision: true, pdf: true, reasoning: false, temperature: true };
const OA_FLAGSHIP_LIMITS = { contextWindow: 1_050_000, maxOutput: 128_000 };
const OPENAI: Record<string, Entry> = {
'gpt-6-astra': { label: 'GPT-6 Astra', ...OA_FLAGSHIP_LIMITS, priceIn: 10, priceOut: 50, capabilities: OA_REASONING },
'gpt-6.1-sol': { label: 'GPT-6.1 Sol', ...OA_FLAGSHIP_LIMITS, priceIn: 2, priceOut: 10, capabilities: OA_REASONING },
'gpt-6.1-terra': { label: 'GPT-6.1 Terra', ...OA_FLAGSHIP_LIMITS, priceIn: 1, priceOut: 5, capabilities: OA_REASONING },
'gpt-6-luna': { label: 'GPT-6 Luna', ...OA_FLAGSHIP_LIMITS, priceIn: 0.1, priceOut: 0.5, capabilities: OA_REASONING },
'gpt-6-sol': { label: 'GPT-6 Sol', priceIn: 2, priceOut: 10, capabilities: OA_REASONING },
'gpt-5.6-sol': { label: 'GPT-5.6 Sol', priceIn: 4, priceOut: 20, capabilities: OA_REASONING },
'gpt-5.6-terra': { label: 'GPT-5.6 Terra', priceIn: 2, priceOut: 12, capabilities: OA_REASONING },
'gpt-5.6-luna': { label: 'GPT-5.6 Luna', priceIn: 0.2, priceOut: 1.2, capabilities: OA_REASONING },
'gpt-5.5': { label: 'GPT-5.5', priceIn: 5, priceOut: 30, capabilities: OA_REASONING },
'gpt-5.5-pro': { label: 'GPT-5.5 Pro', priceIn: 30, priceOut: 180, capabilities: OA_REASONING },
'gpt-5.4': { label: 'GPT-5.4', priceIn: 2.5, priceOut: 15, capabilities: OA_REASONING },
'gpt-5.4-pro': { label: 'GPT-5.4 Pro', priceIn: 30, priceOut: 180, capabilities: OA_REASONING },
'gpt-5.4-mini': { label: 'GPT-5.4 mini', priceIn: 0.75, priceOut: 4.5, capabilities: OA_REASONING },
'gpt-5.4-nano': { label: 'GPT-5.4 nano', priceIn: 0.2, priceOut: 1.25, capabilities: OA_REASONING },
'gpt-5.3-codex': { label: 'GPT-5.3 Codex', priceIn: 1.75, priceOut: 14, capabilities: OA_REASONING },
'gpt-5.2': { label: 'GPT-5.2', priceIn: 1.75, priceOut: 14, capabilities: OA_REASONING },
'gpt-5.2-pro': { label: 'GPT-5.2 Pro', priceIn: 21, priceOut: 168, capabilities: OA_REASONING },
'gpt-5.1': { label: 'GPT-5.1', priceIn: 1.25, priceOut: 10, capabilities: OA_REASONING },
'gpt-5': { label: 'GPT-5', contextWindow: 400_000, maxOutput: 128_000, priceIn: 1.25, priceOut: 10, capabilities: OA_REASONING },
'gpt-5-mini': { label: 'GPT-5 mini', contextWindow: 400_000, maxOutput: 128_000, priceIn: 0.25, priceOut: 2, capabilities: OA_REASONING },
'gpt-5-nano': { label: 'GPT-5 nano', contextWindow: 400_000, maxOutput: 128_000, priceIn: 0.05, priceOut: 0.4, capabilities: OA_REASONING },
'gpt-5-pro': { label: 'GPT-5 Pro', priceIn: 15, priceOut: 120, capabilities: OA_REASONING },
'chat-latest': { label: 'ChatGPT (latest)', priceIn: 5, priceOut: 30, capabilities: OA_CLASSIC },
'gpt-4.1': { label: 'GPT-4.1', contextWindow: 1_047_576, maxOutput: 32_768, priceIn: 2, priceOut: 8, capabilities: OA_CLASSIC },
'gpt-4.1-mini': { label: 'GPT-4.1 mini', contextWindow: 1_047_576, maxOutput: 32_768, priceIn: 0.4, priceOut: 1.6, capabilities: OA_CLASSIC },
'gpt-4o': { label: 'GPT-4o', contextWindow: 128_000, maxOutput: 16_384, priceIn: 2.5, priceOut: 10, capabilities: OA_CLASSIC },
'gpt-4o-mini': { label: 'GPT-4o mini', contextWindow: 128_000, maxOutput: 16_384, priceIn: 0.15, priceOut: 0.6, capabilities: OA_CLASSIC },
o3: { label: 'o3', contextWindow: 200_000, maxOutput: 100_000, priceIn: 2, priceOut: 8, capabilities: OA_REASONING },
'o3-pro': { label: 'o3-pro', contextWindow: 200_000, maxOutput: 100_000, priceIn: 20, priceOut: 80, capabilities: OA_REASONING },
};
/* ------------------------------------------------------------------ */
/* Anthropic */
/* Prices: https://platform.claude.com/docs/en/about-claude/pricing (fetched 2026-11-11)
/* Ids and limits: https://platform.claude.com/docs/en/about-claude/models/overview
/* (via Anthropic's Claude API reference, cached 2026-11-03)
/* temperature:false for every Claude model: the adapter speaks API version
/* 2026-10-21, which rejects temperature/top_p/top_k on all models
/* (https://platform.claude.com/docs/en/api/migration-2026-10-21).
/* Claude Haiku 5.5 prices are for prompts up to 100K tokens ($0.50/$2.50 above).
/* ------------------------------------------------------------------ */
const CLAUDE: Partial<ModelCapabilities> = { tools: true, vision: true, pdf: true, reasoning: true, temperature: false };
const CLAUDE_1M = { contextWindow: 1_000_000, maxOutput: 128_000 };
const ANTHROPIC: Record<string, Entry> = {
'claude-fable-5-2': { label: 'Claude Fable 5.2', ...CLAUDE_1M, priceIn: 8, priceOut: 40, capabilities: CLAUDE },
'claude-mythos-5-2': { label: 'Claude Mythos 5.2', ...CLAUDE_1M, priceIn: 8, priceOut: 40, capabilities: CLAUDE },
'claude-fable-5-1': { label: 'Claude Fable 5.1', ...CLAUDE_1M, priceIn: 10, priceOut: 50, capabilities: CLAUDE },
'claude-mythos-5-1': { label: 'Claude Mythos 5.1', ...CLAUDE_1M, priceIn: 10, priceOut: 50, capabilities: CLAUDE },
'claude-fable-5': { label: 'Claude Fable 5', ...CLAUDE_1M, priceIn: 10, priceOut: 50, capabilities: CLAUDE },
'claude-mythos-5': { label: 'Claude Mythos 5', ...CLAUDE_1M, priceIn: 10, priceOut: 50, capabilities: CLAUDE },
'claude-opus-5-5': { label: 'Claude Opus 5.5', ...CLAUDE_1M, priceIn: 4, priceOut: 20, capabilities: CLAUDE },
'claude-opus-5': { label: 'Claude Opus 5', ...CLAUDE_1M, priceIn: 5, priceOut: 25, capabilities: CLAUDE },
'claude-opus-4-8': { label: 'Claude Opus 4.8', ...CLAUDE_1M, priceIn: 5, priceOut: 25, capabilities: CLAUDE },
'claude-opus-4-7': { label: 'Claude Opus 4.7', ...CLAUDE_1M, priceIn: 5, priceOut: 25, capabilities: CLAUDE },
'claude-opus-4-6': { label: 'Claude Opus 4.6', ...CLAUDE_1M, priceIn: 5, priceOut: 25, capabilities: CLAUDE },
'claude-opus-4-5': { label: 'Claude Opus 4.5', priceIn: 5, priceOut: 25, capabilities: CLAUDE },
'claude-sonnet-5-5': { label: 'Claude Sonnet 5.5', ...CLAUDE_1M, priceIn: 2, priceOut: 10, capabilities: CLAUDE },
'claude-sonnet-5': { label: 'Claude Sonnet 5', ...CLAUDE_1M, priceIn: 2, priceOut: 10, capabilities: CLAUDE },
'claude-sonnet-4-6': { label: 'Claude Sonnet 4.6', ...CLAUDE_1M, priceIn: 3, priceOut: 15, capabilities: CLAUDE },
'claude-sonnet-4-5': { label: 'Claude Sonnet 4.5', priceIn: 3, priceOut: 15, capabilities: CLAUDE },
'claude-haiku-5-5': { label: 'Claude Haiku 5.5', ...CLAUDE_1M, priceIn: 0.1, priceOut: 0.5, capabilities: CLAUDE },
'claude-haiku-4-5': { label: 'Claude Haiku 4.5', contextWindow: 200_000, maxOutput: 64_000, priceIn: 1, priceOut: 5, capabilities: CLAUDE },
};
/* ------------------------------------------------------------------ */
/* Google Gemini */
/* Prices: https://ai.google.dev/gemini-api/docs/pricing (fetched 2026-11-11,
/* now served from https://aistudio.google.com/docs/pricing)
/* Limits: https://ai.google.dev/gemini-api/docs/models (fetched 2026-11-11;
/* only the Gemini 4 models list limits — `GET /v1beta/models`
/* supplies inputTokenLimit/outputTokenLimit for the rest at runtime)
/* Scheduled changes announced on the pricing page (NOT applied here):
/* - gemini-4-carbon / gemini-4-argon: $4 / $20 from 2026-12-01
/* - gemini-3.8-flash / gemini-3.6-flash: $1.50 / $7.50 from 2027-01-01
/* - gemini-2.5-pro / gemini-2.5-flash are deprecated and are routed to
/* gemini-3.8-flash / gemini-3.5-flash-lite (and billed as such) from 2026-12-01
/* Pro-tier prices are for prompts up to 200K tokens.
/* ------------------------------------------------------------------ */
const GEMINI_CAPS: Partial<ModelCapabilities> = { tools: true, vision: true, pdf: true, reasoning: true, temperature: true };
const GEMINI_4_LIMITS = { contextWindow: 1_048_576, maxOutput: 65_536 };
const GEMINI: Record<string, Entry> = {
'gemini-4-carbon': { label: 'Gemini 4 Carbon', ...GEMINI_4_LIMITS, priceIn: 2, priceOut: 10, capabilities: GEMINI_CAPS },
'gemini-4-argon': { label: 'Gemini 4 Argon', ...GEMINI_4_LIMITS, priceIn: 2, priceOut: 10, capabilities: GEMINI_CAPS },
'gemini-3.8-flash': { label: 'Gemini 3.8 Flash', priceIn: 0.75, priceOut: 3.75, capabilities: GEMINI_CAPS },
'gemini-3.6-flash': { label: 'Gemini 3.6 Flash', priceIn: 0.75, priceOut: 3.75, capabilities: GEMINI_CAPS },
'gemini-3.5-flash-lite': { label: 'Gemini 3.5 Flash-Lite', priceIn: 0.3, priceOut: 2.5, capabilities: GEMINI_CAPS },
'gemini-3.1-pro-preview': { label: 'Gemini 3.1 Pro (preview)', priceIn: 2, priceOut: 12, capabilities: GEMINI_CAPS },
'gemini-3.1-flash-lite': { label: 'Gemini 3.1 Flash-Lite', priceIn: 0.25, priceOut: 1.5, capabilities: GEMINI_CAPS },
'gemini-3-flash-preview': { label: 'Gemini 3 Flash (preview)', priceIn: 0.5, priceOut: 3, capabilities: GEMINI_CAPS },
'gemini-2.5-pro': { label: 'Gemini 2.5 Pro', priceIn: 1.25, priceOut: 10, capabilities: GEMINI_CAPS },
'gemini-2.5-flash': { label: 'Gemini 2.5 Flash', priceIn: 0.3, priceOut: 2.5, capabilities: GEMINI_CAPS },
'gemini-2.5-flash-lite': { label: 'Gemini 2.5 Flash-Lite', priceIn: 0.1, priceOut: 0.4, capabilities: GEMINI_CAPS },
};
/* ------------------------------------------------------------------ */
/* DeepSeek */
/* Prices and limits: https://api-docs.deepseek.com/quick_start/pricing (fetched 2026-11-15)
/* Peak-hour list prices are stored. Off-peak hours (everything outside 01:00-04:00 and
/* 06:00-10:00 UTC, Monday to Friday) cost half; `priceMultiplier` applies that per call.
/* deepseek-v4-flash / deepseek-v4-flash-vision-exp are retired aliases served by
/* deepseek-flash at the Flash price.
/* ------------------------------------------------------------------ */
const DS_LIMITS = { contextWindow: 1_048_576, maxOutput: 393_216 };
const DEEPSEEK: Record<string, Entry> = {
'deepseek-flash': { label: 'DeepSeek V4.1 Flash', ...DS_LIMITS, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2, capabilities: { tools: true, vision: true, pdf: false, reasoning: true, temperature: true } },
'deepseek-v4-flash': { label: 'DeepSeek V4 Flash (alias of Flash)', ...DS_LIMITS, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2, capabilities: { tools: true, vision: true, pdf: false, reasoning: true, temperature: true } },
'deepseek-v4-pro': { label: 'DeepSeek V4 Pro', ...DS_LIMITS, priceIn: 1.32, priceCachedIn: 0.044, priceOut: 3.96, capabilities: { tools: true, vision: false, pdf: false, reasoning: true, temperature: true } },
};
const TABLES: Record<ProviderKind, Record<string, Entry>> = {
openai: OPENAI,
anthropic: ANTHROPIC,
gemini: GEMINI,
deepseek: DEEPSEEK,
// Aggregators and local servers host arbitrary models at their own prices (OpenRouter
// reports them in its model list).
openrouter: {},
openai_compatible: {},
};
/**
* What may follow a known id for it to still be "the same model at the same
* price": a dated snapshot or a moving alias, e.g. `gpt-5-2025-08-07`,
* `claude-haiku-4-5-20251001`, `claude-opus-4-5@20251101`, `gemini-2.5-flash-latest`.
*/
const SNAPSHOT_SUFFIX = /^[-@:](\d{4}-\d{2}-\d{2}|\d{6,8}|\d{3,4}|latest|preview|preview-\d{2}-\d{2,4}|v\d+(:\d+)?)$/;
function clone(e: Entry): Partial<DiscoveredModel> {
return { ...e, ...(e.capabilities ? { capabilities: { ...e.capabilities } } : {}) };
}
function lookupIn(table: Record<string, Entry>, id: string): Partial<DiscoveredModel> {
const exact = table[id];
if (exact) return clone(exact);
// Longest known id that is a prefix of `id` and ends on a separator.
let best = '';
for (const key of Object.keys(table)) {
if (key.length > best.length && id.startsWith(key) && /[-@:]/.test(id.charAt(key.length))) best = key;
}
if (!best) return {};
const entry = table[best];
if (!entry) return {};
const out = clone(entry);
delete out.label; // the label names the base model, not this variant
if (!SNAPSHOT_SUFFIX.test(id.slice(best.length))) {
// A genuinely different variant (`o3-deep-research`, `gpt-4o-audio-...`):
// capabilities are a fair guess, the price is not.
delete out.priceIn;
delete out.priceOut;
delete out.priceCachedIn;
}
return out;
}
/**
* Defaults for a model: exact id first, then the longest known id that prefixes
* it (so dated snapshots match their base model). Returns `{}` when unknown.
*/
export function lookupModelDefaults(kind: ProviderKind, modelId: string): Partial<DiscoveredModel> {
// Gemini's listing names models "models/<id>".
const id = modelId.trim().replace(/^models\//, '');
if (!id) return {};
if (kind === 'openai_compatible') {
// Routers name models "<vendor>/<model>" (`anthropic/claude-sonnet-5-5`).
// Borrow capabilities and limits from the vendor's table, never its prices.
const bare = id.slice(id.lastIndexOf('/') + 1).replace(/:[a-z]+$/, '');
for (const table of [OPENAI, ANTHROPIC, GEMINI, DEEPSEEK]) {
const hit = lookupIn(table, bare);
if (Object.keys(hit).length > 0) {
delete hit.priceIn;
delete hit.priceOut;
delete hit.priceCachedIn;
// Temperature limits are properties of the first-party APIs, not of the router.
if (hit.capabilities) delete hit.capabilities.temperature;
return hit;
}
}
return {};
}
return lookupIn(TABLES[kind], id);
}
/** Every catalogued model id for a provider (used as a fallback list when discovery is unavailable). */
export function catalogModelIds(kind: ProviderKind): string[] {
return Object.keys(TABLES[kind]);
}
/**
* What a cache-hit input token costs when a model has no cached price of its own, as a
* fraction of the normal input price. These are the providers' published discounts for
* their current models: OpenAI GPT-5 and later, Claude cache reads and Gemini 2.5 and later
* all bill cached input at 10%. Older OpenAI models (GPT-4.1 25%, GPT-4o / o-series 50%)
* discount less. OpenRouter reports each model's cache-read price itself; servers behind the
* compatible adapter have no known discount.
*/
export function cachedInputRatio(kind: ProviderKind, modelId: string): number {
const id = modelId.slice(modelId.lastIndexOf('/') + 1);
switch (kind) {
case 'openai':
if (/^gpt-4\.1/.test(id)) return 0.25;
if (/^(gpt-4o|o\d)/.test(id)) return 0.5;
return 0.1;
case 'anthropic':
case 'gemini':
return 0.1;
case 'deepseek':
return 0.02;
default:
return 1;
}
}
/** USD per million cached input tokens: the model's own price when set, otherwise the provider discount. */
export function cachedInputPrice(kind: ProviderKind, modelId: string, priceIn: number | null, priceCachedIn: number | null): number {
if (priceCachedIn !== null && priceCachedIn !== undefined) return priceCachedIn;
return (priceIn ?? 0) * cachedInputRatio(kind, modelId);
}
/** Cache writes: Claude bills writing the (5-minute) prompt cache at 1.25x the input price. */
export function cacheWriteRatio(kind: ProviderKind): number {
return kind === 'anthropic' ? 1.25 : 1;
}
/**
* Time-of-day pricing. DeepSeek charges half its list price outside peak hours
* (01:00-04:00 and 06:00-10:00 UTC, Monday to Friday).
*/
export function priceMultiplier(kind: ProviderKind, at: number = Date.now()): number {
if (kind !== 'deepseek') return 1;
const d = new Date(at);
const day = d.getUTCDay();
const hour = d.getUTCHours();
const peak = day >= 1 && day <= 5 && ((hour >= 1 && hour < 4) || (hour >= 6 && hour < 10));
return peak ? 1 : 0.5;
}