Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
feat: the catalog decides chat capability and carries comparable prices
Jack Rudenko7fce457ab723
diff --git a/packages/cli/src/model-loader.ts b/packages/cli/src/model-loader.tsindex dda7a619..469c955d 100644--- a/packages/cli/src/model-loader.ts+++ b/packages/cli/src/model-loader.ts@@ -128,6 +128,8 @@ export interface AggregatorEntry {* OpenRouter/OpenCode Zen can charge differently from the model owner.*/pricing?: {+ /** How to read the rest of this object. Absent means the price is unknown. */+ type?: "flat" | "tiered" | "free" | "unavailable";input?: number;output?: number;cachedRead?: number;@@ -135,6 +137,20 @@ export interface AggregatorEntry {imageInput?: number;audioInput?: number;batchDiscountPct?: number;+ /**+ * Ascending price bands by input size. The catalog guarantees the top-level+ * `input`/`output` equal the first tier, verified on all 28 tiered+ * connections of generations g-20260920154425586-418ad3dd and+ * g-20260921062451697-f490edba (the latter being the first to carry the+ * agreed `type` field name).+ */+ tiers?: Array<{+ maxInputTokens?: number;+ input?: number;+ output?: number;+ cachedRead?: number;+ cachedWrite?: number;+ }>;};/*** True per-aggregator context window (max input tokens) for thisdiff --git a/packages/cli/src/providers/all-models-cache.ts b/packages/cli/src/providers/all-models-cache.tsindex 5eb8c14c..fdb80825 100644--- a/packages/cli/src/providers/all-models-cache.ts+++ b/packages/cli/src/providers/all-models-cache.ts@@ -45,6 +45,10 @@ export interface SlimModelEntry {supportsThinking?: boolean;videoInput?: boolean;videoOutput?: boolean;+ /** What the model accepts, from the cloud models catalog. Absent, null or [] means unknown. */+ inputModalities?: string[] | null;+ /** What the model produces. Absent, null or [] means unknown; a list without "text" is not a chat model. */+ outputModalities?: string[] | null;aggregators?: AggregatorEntry[];subscriptionPlanIds?: string[];tokenParam?: string;diff --git a/packages/cli/src/providers/connection-price.ts b/packages/cli/src/providers/connection-price.tsnew file mode 100644index 00000000..ea8999fa--- /dev/null+++ b/packages/cli/src/providers/connection-price.ts@@ -0,0 +1,176 @@+/**+ * One comparable price per CONNECTION, read from the cloud models catalog.+ *+ * A connection is one mapped way to call a model — an `aggregators[]` entry+ * with `routeStatus: "mapped"`. Each carries the serving gateway's OWN rate, so+ * two connections to the same canonical model id routinely cost different+ * amounts. To order them, routing needs one number per connection, and this+ * module is the only place that decides what that number is.+ *+ * **What the number is for.** It exists to ORDER connections BEFORE a request+ * is built, when the input size is not yet known. That constrains it hard:+ *+ * - Tiered prices are read from the FIRST tier and nothing else. The number+ * never averages tiers, and it never selects a tier from a request's size —+ * there is no request yet. A per-request cost estimate is a different+ * calculation that would take the real input size as an argument; this is+ * not it, and it must not be mistaken for it.+ * - It is a rate in dollars per million tokens, summed over input and output+ * (`input + output`), which is a ranking key rather than a bill. It assumes+ * nothing about the input:output mix of the eventual request.+ *+ * **What an unknown price means.** claudish reads the discriminator+ * `pricing.type` and only `pricing.type` — there is no alias and no fallback+ * to any other field name. Unknown therefore means one of two concrete things:+ * the catalog published `type: "unavailable"` for that connection (467 of them+ * on generation `g-20260921062451697-f490edba`), or the connection carries no+ * pricing object at all. Unknown is a sizeable minority of any real list, so+ * every caller must render and sort it, never assume it away.+ *+ * Absent-means-unknown is a rule, never an inference: without the+ * discriminator this module does not know how to read the rest of the object,+ * so it does not treat the presence of `input`/`output` as evidence of a flat+ * price. Numbers read under a guessed type are worse than no numbers.+ */++import type { AggregatorEntry } from "../model-loader.js";++/** A connection's price reduced to one number that can be ordered, plus what to show a user. */+export type ConnectionPrice =+ | { known: true; perMillionTokens: number; label: string }+ | { known: false; label: string };++/**+ * A fresh object per call, so a caller that decorates or mutates the result+ * cannot corrupt a shared one.+ */+function unknownPrice(): ConnectionPrice {+ return { known: false, label: "unknown" };+}++/**+ * A catalog rate claudish is willing to act on, or `null`.+ *+ * Rejecting instead of trusting matters because the catalog aggregates+ * scraped sources: a missing field arrives as `undefined`, a failed parse can+ * arrive as `NaN`, and a sentinel can arrive as `-1`. Any of those summed into+ * a ranking key would sort a connection to a position nothing justifies, and+ * `-1` would sort it AHEAD of a genuinely free one. Zero is kept — free is a+ * real price.+ */+function usableRate(value: number | undefined): number | null {+ if (typeof value !== "number") return null;+ if (!Number.isFinite(value)) return null;+ if (value < 0) return null;+ return value;+}++/** `input + output`, or `null` when either side is not a rate worth acting on. */+function sumRates(input: number | undefined, output: number | undefined): number | null {+ const inputRate = usableRate(input);+ const outputRate = usableRate(output);+ if (inputRate === null || outputRate === null) return null;+ return inputRate + outputRate;+}++/** Strip the trailing zeros a fixed-precision render leaves behind: `"0.0060"` → `"0.006"`. */+function trimTrailingZeros(text: string): string {+ if (!text.includes(".") || text.includes("e")) return text;+ return text.replace(/0+$/, "").replace(/\.$/, "");+}++/**+ * Short, currency-marked, and derived entirely from the number — no price is+ * ever hardcoded here.+ *+ * Two decimals read naturally for the common range (`"$1.50/M"`), but sub-cent+ * rates exist and `toFixed(2)` renders them as `"$0.00/M"`, which a user reads+ * as free. Those fall back to two significant digits (`"$0.006/M"`). A rate of+ * exactly zero is free whatever the `type` said, so it says so.+ */+function perMillionLabel(perMillionTokens: number): string {+ if (perMillionTokens === 0) return "free";+ if (perMillionTokens < 0.01) return `$${trimTrailingZeros(perMillionTokens.toPrecision(2))}/M`;+ return `$${perMillionTokens.toFixed(2)}/M`;+}++/**+ * Reduce one connection's catalog pricing to a single orderable number.+ *+ * The rules, agreed with the backend — these and no others:+ *+ * | `pricing.type` | number | label |+ * |---------------------|--------------------------------------|------------------------|+ * | `"flat"` | `input + output` | `"$1.50/M"` |+ * | `"tiered"` | `tiers[0].input + tiers[0].output` | `"$0.16/M (first tier)"` |+ * | `"free"` | `0` | `"free"` |+ * | `"unavailable"` | — | `"unknown"` |+ * | absent / unrecognised / no pricing object | — | `"unknown"` |+ *+ * No branch here is speculative: on generation `g-20260921062451697-f490edba`+ * the catalog publishes `flat` for 975 connections, `unavailable` for 467,+ * `free` for 30 and `tiered` for 28.+ *+ * A `"tiered"` connection with no usable `tiers[0]` falls back to the+ * top-level `input`/`output`, which the catalog guarantees equal the first+ * tier, and keeps the `(first tier)` label because that is still what the+ * number is. A `"flat"` or `"tiered"` connection whose numbers survive neither+ * route is unknown — a declared type is not itself a price.+ */+export function connectionPrice(pricing: AggregatorEntry["pricing"]): ConnectionPrice {+ if (!pricing) return unknownPrice();++ switch (pricing.type) {+ case "flat": {+ const perMillionTokens = sumRates(pricing.input, pricing.output);+ if (perMillionTokens === null) return unknownPrice();+ return { known: true, perMillionTokens, label: perMillionLabel(perMillionTokens) };+ }++ case "tiered": {+ const firstTier = pricing.tiers?.[0];+ const perMillionTokens =+ (firstTier ? sumRates(firstTier.input, firstTier.output) : null) ??+ sumRates(pricing.input, pricing.output);+ if (perMillionTokens === null) return unknownPrice();+ return {+ known: true,+ perMillionTokens,+ label: `${perMillionLabel(perMillionTokens)} (first tier)`,+ };+ }++ case "free":+ return { known: true, perMillionTokens: 0, label: perMillionLabel(0) };++ case "unavailable":+ // The catalog knows this connection publishes no price. Same outcome as+ // not knowing how to read the object at all: nothing to order it by.+ return unknownPrice();++ default:+ // An absent `type`, or a value this build does not recognise because the+ // contract grew one. Reached even when `input`/`output` are present:+ // without the discriminator, how to read them is not known.+ return unknownPrice();+ }+}++/**+ * Ascending by price; an unknown price sorts after every known one.+ *+ * Ties return 0 so the caller applies its own tie-break — freshness, provider+ * preference, whatever that list orders by. Two unknowns also tie, which keeps+ * the unpriced tail of a list (the `unavailable` connections) in the caller's+ * own order under a stable sort rather than scrambling it.+ */+export function compareByConnectionPrice(a: ConnectionPrice, b: ConnectionPrice): number {+ if (a.known && b.known) {+ if (a.perMillionTokens < b.perMillionTokens) return -1;+ if (a.perMillionTokens > b.perMillionTokens) return 1;+ return 0;+ }+ if (a.known) return -1;+ if (b.known) return 1;+ return 0;+}diff --git a/packages/cli/src/providers/transport/probe-discovery.ts b/packages/cli/src/providers/transport/probe-discovery.tsindex 7e3698a2..53124b62 100644--- a/packages/cli/src/providers/transport/probe-discovery.ts+++ b/packages/cli/src/providers/transport/probe-discovery.ts@@ -43,12 +43,12 @@ const SMALL_MODEL_PATTERNS = [* speech-to-text. They appear in `/v1/models` lists beside chat models and 404* or 400 a probe.*- * A NEGATIVE name rule: it can deny, never confirm. It exists because the slim- * cloud models catalog publishes no image-output or audio-output flag (the full- * projection does), and providers the catalog never covers (Ollama, LiteLLM,- * custom endpoints) publish nothing at all. Once the catalog carries explicit- * input and output modalities on every projection, the catalog decides and this- * list is consulted only for models it does not know.+ * A NEGATIVE name rule: it can deny, never confirm. It is now the FALLBACK, not+ * the rule: the cloud models catalog publishes output modalities, so for a model+ * it describes the published list decides and this list is never consulted. It+ * still decides for what the catalog leaves without an output modality — local+ * servers (Ollama, LM Studio), custom endpoints, LiteLLM deployments, and catalog+ * rows that carry no modality field.*/const NON_CHAT_PATTERNS = [/\bimage\b/i,@@ -92,11 +92,16 @@ function isSmallName(name: string): boolean {/*** What is known about whether a model can answer a chat turn.*- * - `"not-chat"` — positive evidence of another output modality: the catalog's- * `videoOutput: true`, a {@link NON_CHAT_PATTERNS} match, or a LiteLLM wildcard- * route. Never offered as a chat model.- * - `"chat"` — the catalog knows the id and declares a chat-shaped capability- * (tools, thinking or vision).+ * The cloud models catalog's OUTPUT MODALITY is the evidence; the name rules are+ * the fallback for models it does not describe.+ *+ * - `"not-chat"` — the catalog's published output modalities exclude `"text"`+ * (image, audio, video, embeddings, transcription, speech, rerank, decisions);+ * or, for a model with no published output modality, `videoOutput: true`, a+ * {@link NON_CHAT_PATTERNS} match, or a LiteLLM wildcard route. Never offered.+ * - `"chat"` — the catalog's published output modalities include `"text"`; or,+ * with none published, the catalog declares a chat-shaped capability (tools,+ * thinking or vision).* - `"unknown"` — neither. Still offered. Rounding it to `"chat"` would assert* what nothing established; rounding it to `"not-chat"` would hide a newly* shipped chat model with no error and no trace.@@ -112,6 +117,10 @@ const CATALOG_CHAT_INDEX_TTL_MS = 60_000;interface CatalogCapabilityIndex {/** Ids the catalog declares chat-shaped. */chat: Set<string>;+ /** Ids whose published output modalities INCLUDE `"text"`: chat models, by catalog evidence. */+ textOutput: Set<string>;+ /** Ids whose published output modalities EXCLUDE `"text"`: they produce something else only. */+ nonTextOutput: Set<string>;/** Ids the catalog declares video GENERATORS (`videoOutput: true`). */videoOutput: Set<string>;/** Ids with ANY published `videoOutput`, `false` included: a statement, not a silence. */@@ -130,6 +139,27 @@ function catalogKey(name: string): string {return lower.includes("/") ? lower.slice(lower.lastIndexOf("/") + 1) : lower;}+/**+ * File a catalog row's keys under its published OUTPUT modality.+ *+ * Unknown arrives as an absent field; `null` and `[]` read as unknown too, because+ * no model produces nothing — such a row joins neither set and is left to the+ * `videoOutput` boolean and the name rules. The test is "includes text", never+ * "equals text": `["audio", "text"]` speaks AND writes, so it can answer a chat turn.+ *+ * An INPUT modality is never filed here, in either direction: a model that accepts+ * video, audio or images is still a chat model.+ */+function indexOutputModality(+ index: CatalogCapabilityIndex,+ keys: string[],+ modalities: string[] | null | undefined+): void {+ if (!Array.isArray(modalities) || modalities.length === 0) return;+ const target = modalities.includes("text") ? index.textOutput : index.nonTextOutput;+ for (const k of keys) target.add(k);+}+function catalogCapabilityIndex(cachePath?: string): CatalogCapabilityIndex {const key = cachePath ?? "";const hit = _catalogChatIndex.get(key);@@ -137,11 +167,17 @@ function catalogCapabilityIndex(cachePath?: string): CatalogCapabilityIndex {const index: CatalogCapabilityIndex = {chat: new Set(),+ textOutput: new Set(),+ nonTextOutput: new Set(),videoOutput: new Set(),videoOutputKnown: new Set(),};for (const entry of readAllModelsCache(cachePath)?.entries ?? []) {const keys = [catalogKey(entry.modelId), ...(entry.aliases ?? []).map(catalogKey)];+ // Filed BEFORE the `videoOutput: true` `continue` below, so a video generator+ // still contributes its published output modality — and where the two disagree+ // the modality list wins, being the more specific statement.+ indexOutputModality(index, keys, entry.outputModalities);// `videoOutput` is read in both directions; `videoInput` is never read as a// denial, because a model that reads video is still a chat model.if (entry.videoOutput !== undefined) {@@ -165,9 +201,16 @@ function catalogCapabilityIndex(cachePath?: string): CatalogCapabilityIndex {/*** Classify whether a model can answer a chat turn — see {@link ChatCapability}.*- * The catalog's published facts outrank every name rule: a `videoOutput: true`- * denies, and a published `videoOutput` of either value switches the video name- * guess off. Name rules decide only what the catalog does not cover.+ * The cloud models catalog's published OUTPUT MODALITY outranks every name rule,+ * in both directions: a model named like an image generator whose published output+ * is `["text"]` IS a chat model, and a model with a blameless name whose output is+ * `["image"]` is NOT. Only where the catalog publishes no output modality — local+ * servers, custom endpoints, LiteLLM deployments, catalog rows without the field —+ * do the older `videoOutput` boolean and the name rules decide.+ *+ * Order: wildcard route, non-text output, text output, `videoOutput: true`,+ * {@link NON_CHAT_PATTERNS}, the video name guess (only while `videoOutput` is+ * unpublished), the chat-shaped capability flags, then `"unknown"`.** @param cachePath Override the catalog cache path. Tests only.*/@@ -178,6 +221,10 @@ export function classifyChatCapability(name: string, cachePath?: string): ChatCaconst index = catalogCapabilityIndex(cachePath);const key = catalogKey(name);+ // Catalog evidence, ahead of every name rule.+ if (index.nonTextOutput.has(key)) return "not-chat";+ if (index.textOutput.has(key)) return "chat";+if (index.videoOutput.has(key)) return "not-chat";if (NON_CHAT_PATTERNS.some((re) => re.test(name))) return "not-chat";if (!index.videoOutputKnown.has(key) && VIDEO_OUTPUT_NAME_PATTERNS.some((re) => re.test(name))) {