Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

180 KB
Raw Blame
/**
 * Built-in model catalog (single source of truth): official chat models that AgentHub can
 * auto-route, shared by core's default config, server's initial config, and web/cli display.
 * Data verified as of 2026-07-10 (Qwen Token Plan entries: 2026-07-20; MiniMax: 2026-08-03;
 * DeepSeek, Gemini 3.7, GLM-5.3 and the whole OpenAI line-up (direct + OpenRouter):
 * 2026-08-18; the direct Anthropic group: 2026-08-20; the DeepSeek V4 Flash Vision Exp rows:
 * 2026-08-21; the TokenDance group: 2026-08-25, its glm-5.3-flash row: 2026-08-26, its
 * qwen3.8-flash row: 2026-08-27 and its running promotions plus the hy4-preview rows
 * (TokenDance + OpenRouter): 2026-08-28; the GLM-5.3 Flash rows (direct + OpenRouter) and
 * the direct qwen3.8-flash: 2026-08-26; the TokenDance Doubao Seed rows (seed-2.1-pro,
 * seed-2.1-turbo, seed-evolving): 2026-09-02; the vLLM group: 2026-09-03; the GPT-6 Astra
 * rows (direct + OpenRouter): 2026-09-09; the whole Gemini 3.x line-up, direct + OpenRouter —
 * the 3.6 / 3.7 / 3.8 Flash launch discounts declared, every other row re-read and unchanged:
 * 2026-09-09; the OpenRouter and TokenDance V4.1 Flash rows, and TokenDance's running
 * promotions plus its Doubao Seed display names: 2026-09-10; the Penguin Go resale lineup,
 * matched to the relay's current generic-client model export: 2026-09-11, its DeepSeek rows to
 * DeepSeek's own lineup: 2026-09-16; the OpenRouter z-ai/glm-5.3-flash row: 2026-09-16; and
 * the 2026-09-16 refresh — the direct DeepSeek group down to the two names its pricing page
 * lists, TokenDance's deepseek-v4-flash-0731 / deepseek-v4-pro-0813 / kimi-k3 promotions, the
 * OpenRouter qwen/qwen3.8-27b row, the Fireworks AI and SiliconFlow additions, and both Qwen
 * groups' line-ups with their peak/off-peak DeepSeek rows: 2026-09-16; the OpenCode Go group:
 * 2026-09-18 — per each provider's docs; and the ModelScope group, whose preset ids were read
 * from public model pages and endpoint listings — their windows, vision flags and prices are
 * NOT verified and say so on the rows themselves: 2026-09-18 and 2026-09-20; and the direct
 * Z.AI glm-4.7-flash row, the group's free tier, read from Z.AI's price list and
 * 智谱开放平台's free-model page: 2026-10-07; and the four Chinese first-party groups added
 * together — Xiaomi MiMo, Baidu ERNIE, Tencent Hunyuan and iFlytek Spark — read from each
 * vendor's own docs and console pages: 2026-10-15, with the iFlytek rows deliberately unpriced
 * (its HTTP doc publishes no price list, and the product page's prices name model families it
 * has published no API id for); and the Abliteration group, whose four rows were read from the
 * service's own model list on 2026-10-23; and the Volcengine Ark (ByteDance) group, whose rows
 * and prices were read from Ark's own model list and model-pricing pages on 2026-10-25, and the
 * TokenDance group re-read whole — its ids, windows, modalities, protocols and prices, four
 * DeepSeek rows moved onto the vendor's schedule, the K3.1 Pro / V4 Pro / GLM-5.3 FlashX / V4.1
 * Flash 1017 rows added and the K3 promotion dropped — from the seller's own catalog API on
 * 2026-10-27.
 * Docs: packages/docs/content/models.{zh,en}.md (site path /docs/models) documents the
 * provider groups and credential resolution described here.
 *
 * Three-bucket pricing convention (USD per million tokens, matching usageToTokenCounts'
 * token-to-bucket mapping):
 * - cache_read: the vendor's "cache hit" price;
 * - cache_write: the vendor's "cache write" price (e.g. Anthropic uses 1.25 x input); vendors
 *   without a separate cache-write fee use the standard input price;
 * - output: output price (thinking + reply).
 * OpenAI charges extra for >272K input, Gemini 3.1 Pro for >200K input, and MiniMax M3 doubles
 * every rate above 512K input; this catalog records their base tier (the cost center uses a
 * single rate, so long-context usage will be underestimated).
 *
 * Scope: excludes deepseek-chat / deepseek-reasoner legacy aliases that AgentHub cannot
 * auto-route (deprecated 2026-07-24), glm-5v-turbo (AgentHub's GLM client forwards images
 * only for glm-5.3-flash, so a vision model cannot do the one thing it exists for), the
 * OpenRouter z-ai/glm-5.1 and SiliconFlow Pro/zai-org/GLM-5.1 gateway listings
 * (delisted 2026-08-06; the Z.AI direct glm-5.1 remains), the OpenRouter
 * inclusionai/ling-3.0-flash:free listing (delisted from OpenRouter, removed 2026-08-18),
 * non-chat models (embedding / image generation / TTS), and Bedrock. Direct-vendor ids are
 * auto-routed by AgentHub and leave client_type unset; eight gateway groups (OpenRouter,
 * Fireworks AI, SiliconFlow, TokenDance, OpenCode Go, Qwen Pay-As-You-Go, Qwen Token Plan and
 * Abliteration)
 * can't be auto-routed, so every gateway row **always pins an explicit client_type** and
 * inlines its preset base URL. ModelScope's rows pin one protocol too (see below).
 * Two groups pin at GROUP level as well (ModelProviderInfo.clientType, read
 * through providerClientType), so that a model the user adds there speaks the same protocol
 * as the presets: vLLM, whose added models have no preset base URL to inherit either, and
 * OpenRouter, whose do — see each group's own block comment.
 * That pin is load-bearing, not decoration: AgentHub's AutoLLMClient matches raw substrings
 * against `client_type || model_id` and never looks at base_url, so an unpinned gateway id
 * would be routed by its own spelling — `openai/gpt-5.6-sol` would reach the first-party
 * GPT-5.6 client aimed at a gateway, and `anthropic/claude-opus-4.8` would throw outright
 * (dotted "4.8" matches neither "4-8" nor "-5"). Four protocols are pinned:
 * - `openai-responses` for every OpenRouter row: OpenRouter serves the Responses API for
 *   every upstream at the same base URL the rows already carry, and the group pins the same
 *   protocol so a user-added entry inherits it;
 * - `openai-chat` for the other OpenAI-compatible gateway rows (AgentHub 0.4.2's canonical
 *   name for the generic Chat Completions client — the bare "openai" spelling is a
 *   deprecated upstream alias, see canonicalClientType);
 * - `openai-chat-vllm-adapter` for the vLLM group, which is Chat Completions on the wire
 *   but maps the thinking level onto the served model's own chat template
 *   (VLLM_CLIENT_TYPE);
 * - `ant-messages` for the OpenCode Go rows its endpoint table serves on the Anthropic
 *   Messages API. That group pins row by row, since its models sit on three protocols: the
 *   rest of it is `openai-chat` and `openai-responses` — see its block comment.
 * ModelScope's rows all pin `openai-responses`: ModelScope serves every preset through its
 * OpenAI Responses endpoint, and the explicit pin keeps a Qwen or DeepSeek id from auto-routing
 * to a vendor client with a different request shape.
 *
 * Two direct-vendor rows pin anyway, because their own id does not route: the MiniMax M3
 * preset pins AgentHub's first-party `minimax-m3` protocol and direct API endpoint, and
 * `deepseek-flash` pins `deepseek-v4` because AgentHub 0.4.11 routes DeepSeek on that
 * substring alone and the released V4.1 Flash id no longer carries it.
 *
 * App attribution (`attributionHeaders`, bottom of this file) rides alongside the protocol
 * pins: it names Adelie to the gateways that read such a header, keyed on the
 * endpoint host rather than on the provider group.
 *
 * This file imports no Node built-ins (type-only imports only), so it can be bundled directly
 * for the browser.
 */
import type { ModelEntry, ModelPricing } from "./project-config.js";

/** Model provider info (used for web grouping/logo and the "API key blank falls back to env var" hint). */
/**
 * A provider that mints API keys through an authorization page instead of a console visit.
 * Present only on providers that publish such a flow; its absence is what makes the harness
 * refuse to start one.
 */
export interface ModelProviderOAuth {
  /** Authorization page the user is sent to; the flow's parameters ride on its query string. */
  authorizeUrl: string;
  /** Endpoint that trades an authorization code for a newly minted key. */
  exchangeUrl: string;
  /** Name recorded on the minted key, and the app name the authorization page displays. */
  keyName: string;
}

/**
 * A group whose credential the App obtains through its own authorization flow rather than by
 * asking the vendor: the client registers a one-time code and device secret with an external
 * bridge, sends the user to the page that bridge hands back, and polls until the bridge
 * delivers the credential. Distinct from `oauth`, where the App itself speaks the vendor's
 * PKCE page — here the App never reaches the vendor at all, because the bridge holds the
 * client secret that conversation would need.
 *
 * The bridge's own address is deliberately NOT here: this file is bundled for the browser and
 * cannot read configuration, so the App resolves the bridge server-side from its environment
 * (PENGUIN_GO_ORIGIN / MODELSCOPE_BRIDGE_URL), keyed by the group's id.
 */
export interface ModelProviderBridgeAuth {
  /**
   * Which App-side flow serves this group — one route family and one service per value, so
   * the Web App picks from the group's descriptor rather than from its id.
   */
  flow: "penguin-go" | "modelscope";
}

/**
 * An account-balance endpoint the vendor publishes, called with the group's own API key as a
 * Bearer token. The server makes that call (the key never reaches the browser); the endpoint
 * is the vendor's, like the OAuth one above, so it can live in this browser-bundled file.
 */
export interface ModelProviderBalance {
  /** GET endpoint answering the balance of the account the key belongs to. */
  url: string;
  /**
   * How to read the answer, one reader per published response shape:
   * - `tokendance`: `{ balance: { credits, credits_used, balance } }`, integers in micro-yuan
   *   (1 CNY = 1,000,000);
   * - `deepseek`: `{ is_available, balance_infos: [{ currency, total_balance, … }] }`, one entry
   *   per currency the account holds.
   */
  format: "tokendance" | "deepseek";
}

export interface ModelProviderInfo {
  id: string;
  /** Display name (brand name, shared by Chinese and English UI). */
  label: string;
  /**
   * API key env var name: the pair AgentHub's client for this group's rows reads when handed no
   * key. Whether a keyless row may actually lean on it is decided per entry by
   * modelEnvFallback — a gateway group records OPENAI_* because that is what its generic
   * client reads, and precisely for that reason its rows never get the fallback.
   */
  envKey: string;
  /** base URL env var name. */
  envBaseUrlKey: string;
  /** Console URL for obtaining an API key (frontend links it beside the API key field of the group key dialog and the model dialog); none for custom. */
  apiKeyUrl?: string;
  /** Vendor's model list / docs page URL (frontend's "add model" dialog links this as "get model id"); none for custom. */
  modelsUrl?: string;
  /**
   * Gateway's OpenAI-compatible endpoint (openrouter / siliconflow / qwen-token-plan): used by
   * the frontend's "add model" dialog to prefill base URL by group; left blank for direct
   * vendors and custom.
   */
  gatewayBaseUrl?: string;
  /**
   * Authorization flow that mints a key for the user (see ModelProviderOAuth); absent for
   * every provider whose keys are only obtainable from its console.
   */
  oauth?: ModelProviderOAuth;
  /**
   * The App-side authorization flow that obtains this group's credential (see
   * ModelProviderBridgeAuth); absent for every group whose credential comes from a console or
   * from the vendor's own page. Declaring it is what puts the "authorize a key" action on the
   * group in the Web App.
   */
  bridgeAuth?: ModelProviderBridgeAuth;
  /**
   * The vendor's account-balance endpoint (see ModelProviderBalance); absent for every group
   * whose vendor publishes none. Declaring it is what puts the balance in the group's header.
   */
  balance?: ModelProviderBalance;
  /**
   * The group takes models the user adds by hand: `custom` and vLLM, whose rows point at an
   * endpoint the catalog cannot know. Every other built-in group carries its catalog presets
   * and the rows it already stores, and nothing else. Read it through isAddableGroup, which
   * also answers for user-defined groups.
   */
  addable?: boolean;
  /**
   * The AgentHub protocol EVERY entry in this group speaks, models the user adds included.
   *
   * Set it only where the group itself decides the answer and no other property already
   * implies it: the other gateways derive `openai-chat` from carrying a `gatewayBaseUrl`, and
   * `custom` / user-defined groups deliberately declare nothing — their whole point is that
   * the protocol is detected from the endpoint or picked by hand, and a pin here would
   * take that choice away.
   *
   * Where it IS set, it outranks every group-shape guess in the app: the add-model dialog
   * preselects it, moving an entry into the group rewrites the entry to it, the API-key env
   * hint resolves against it, and protocol detection is skipped because the group already
   * knows. Read it through providerClientType rather than reaching for the field, so those
   * call sites keep answering as one.
   */
  clientType?: string;
}

/** A single built-in model's catalog entry (`modelId` is the upstream id; paired with `provider` it forms the catalog's unique key). */
export interface ModelCatalogEntry {
  modelId: string;
  displayName: string;
  /** Provider id (one of MODEL_PROVIDERS). */
  provider: string;
  contextWindow?: number;
  pricing?: ModelPricing;
  /**
   * Fraction off the list price the seller is currently running (0.5 = 50% off), applied to
   * every bucket. `pricing` stays the LIST price whatever promotion is live, so a lapsed
   * promotion is one field to delete rather than three numbers to reconstruct;
   * effectivePricing is the rate actually billed. A Project stores the list price too: the
   * fraction is seeded beside it as a promotion (presetPromotions), which the cost center
   * applies when it prices usage, so it charges what the seller charges.
   */
  discount?: number;
  /**
   * A discount that is only live outside a weekly set of peak windows — a vendor billing
   * cheaper off-hours. `pricing` holds the PEAK price, the one billed inside the windows, and
   * the rate applies everywhere else.
   *
   * Like `discount`, this one is never baked into a Project, and here there is no choice: it
   * changes twice a day. `presetModelEntries` writes the peak price and the rate is applied
   * when a price is read.
   * A number on disk that silently meant something different at 09:00 than at 08:00 would be
   * unreadable, and re-syncing presets would rewrite prices by the clock. Mutually exclusive
   * with `discount`; an entry declaring both is a catalog error.
   */
  offPeakDiscount?: OffPeakDiscount;
  /** Whether image input (vision modality) is supported. */
  supportsVision: boolean;
  /**
   * How many images ONE request may carry, when the seller publishes a cap. Unset = no known
   * cap, and the request goes out with whatever the conversation holds.
   *
   * This is a request-shape fact, not a display one: a conversation accumulates images (a
   * `read_file` of a picture becomes one, and every later request replays the history), and a
   * provider that counts them refuses the request outright once the total passes its limit —
   * `400 … media_count_exceeded` from Abliteration, whose cap is 4. The Session therefore
   * trims the oldest replayed images down to this number before each request (see
   * GenerativeModel's image budget), keeping the newest; the conversation's own history and
   * Trace keep every image, so nothing is lost from the record.
   */
  maxImages?: number;
  /**
   * Whether video input is supported. Absent = no, which is every row but the ones whose
   * vendor documents it: the modality is young enough that a vendor's model list states it
   * per model, and guessing it from `supportsVision` would send a video to a model that
   * rejects the content item outright.
   *
   * Read through catalogSupportsVideo rather than off the row, the way maxImages is read
   * through catalogImageLimit: it is a property of the model being called, so a Project that
   * renamed or repriced the row still gets the same answer, and a user-added row (no catalog
   * entry) gets no video input — it can still work with a video file by path.
   */
  supportsVideo?: boolean;
  /** AgentHub client protocol: required when an id cannot be auto-routed or a shared protocol must be pinned. */
  clientType?: string;
  /** Preset base URL: inlined into gateway and direct MiniMax entries so only an API key is required. */
  baseUrl?: string;
  /**
   * The seller no longer offers this row, but Projects created while it did still carry it: a
   * row a Project has is never deleted by "Sync presets", and several of these were the default
   * model of new Projects. A retired row is not a preset — `presetModelEntries` and
   * `presetPromotions` skip it, so a new Project never gets it or a promotion for it, and a sync
   * never adds it — but it is still in `catalogModelEntries`, so a sync keeps updating the row
   * on a Project that carries it, and lookups by `(provider, modelId)` still find it. Those
   * Projects keep its display name and vision flag, and their usage on it is priced on its
   * off-peak schedule whenever the row stores the catalog's price: the tier only applies to that
   * price, and a sync puts back a price an older release stored. The schedule is the part that
   * matters: cost is priced when it is read, so deleting a scheduled row would reprice every
   * off-peak record already on those Projects at the peak rate.
   *
   * Compatibility, not catalog data: remove a retired row only after its seller has stopped
   * accepting the id AND the release notes have told users that usage recorded on it will be
   * priced without its schedule from then on. Whoever does the catalog refresh that finds the
   * id rejected makes that call.
   */
  retired?: true;
}

/**
 * AgentHub's client for models served by vLLM's OpenAI-compatible Chat Completions API. It
 * is Chat Completions on the wire, but a distinct client: it maps the thinking level onto
 * the `chat_template_kwargs` the SERVED model's chat template reads, which differs per model
 * family, and AgentHub matches this name by exact equality (before its `openai` substring
 * branches) so `openai-chat` would silently lose that mapping.
 */
const VLLM_CLIENT_TYPE = "openai-chat-vllm-adapter";

/** Preset provider endpoints; only OpenAI-compatible gateways expose theirs as gatewayBaseUrl. */
const OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1";
const SILICONFLOW_BASE_URL = "https://api.siliconflow.cn/v1";
const QWEN_TOKEN_PLAN_BASE_URL =
  "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1";
const QWEN_PAYG_BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1";
const FIREWORKS_BASE_URL = "https://api.fireworks.ai/inference/v1";
const TOKENDANCE_BASE_URL = "https://tokendance.space/gateway/v1";
/** Abliteration's OpenAI-compatible base (its docs' base URL for every SDK and agent they wire up). */
const ABLITERATION_BASE_URL = "https://api.abliteration.ai/v1";
const MINIMAX_BASE_URL = "https://api.minimax.io/v1";
const DEEPSEEK_BASE_URL = "https://api.deepseek.com";
const MODELSCOPE_BASE_URL = "https://api-inference.modelscope.cn/v1";
/** Xiaomi MiMo's pay-as-you-go endpoint (its batch and token-plan hosts are separate services with their own keys). */
const XIAOMI_BASE_URL = "https://api.xiaomimimo.com/v1";
/** Baidu Qianfan's OpenAI-compatible base: `/v2` — `/v1` is the retired interface. */
const BAIDU_BASE_URL = "https://qianfan.baidubce.com/v2";
/** Tencent Hunyuan's OpenAI-compatible endpoint. */
const HUNYUAN_BASE_URL = "https://api.hunyuan.cloud.tencent.com/v1";
/** iFlytek Spark's OpenAI-compatible endpoint (the WebSocket API is a different, signed protocol). */
const IFLYTEK_BASE_URL = "https://spark-api-open.xf-yun.com/v1";
/**
 * Volcengine Ark's (火山方舟) OpenAI-compatible endpoint — ByteDance's own model platform, the
 * one the Doubao / Seed line is served from. Its console issues an API key that the Ark SDKs
 * pass as a Bearer token; the base URL is region-scoped, and this is the cn-beijing host its
 * own quick-start and every per-model page use.
 */
const VOLCENGINE_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3";
export const PENGUIN_GO_BASE_URL = "https://token.penguin.ooo/api";
/** OpenCode Go's OpenAI-protocol base: the clients append /chat/completions or /responses. */
const OPENCODE_GO_BASE_URL = "https://opencode.ai/zen/go/v1";
/** OpenCode Go's Anthropic Messages base: the SDK appends /v1/messages itself, so no /v1 here. */
const OPENCODE_GO_MESSAGES_BASE_URL = "https://opencode.ai/zen/go";

/** Provider id for the preconfigured Penguin Go relay group. */
export const PENGUIN_GO_PROVIDER_ID = "penguin-go";

/** Provider id for the ModelScope group. */
export const MODELSCOPE_PROVIDER_ID = "modelscope";

/**
 * Provider list (web model page groups in this order BY DEFAULT — a user's dragged
 * arrangement is stored per Project and wins over this sequence; see the web's
 * model-group-order.ts). The sequence is a hand-curated display order: TokenDance leads,
 * Penguin Go follows, then OpenCode Go, then DeepSeek as the default
 * model's provider, and custom
 * (custom OpenAI-protocol models) is always last; in between, gateways and first-party
 * vendors are interleaved by expected use rather than sorted by kind. Only this default
 * moves when the curation changes: a Project that has ever reordered its groups has every
 * key stored already, so it keeps the arrangement its user built.
 *
 * The seven gateway groups — OpenRouter, Fireworks AI, SiliconFlow, TokenDance, OpenCode Go,
 * Qwen Pay-As-You-Go and Qwen Token Plan — reach their models through AgentHub's generic
 * protocol clients (`openai-responses` for OpenRouter, `openai-chat` for the rest, and all
 * three generic clients within OpenCode Go). Those clients read the vendor variables
 * (**OPENAI_API_KEY / OPENAI_BASE_URL**, and ANTHROPIC_* for OpenCode Go's `ant-messages`
 * rows) when the credential is blank, not the gateway's own variable names, so every gateway
 * group records the OPENAI_* pair as the fact it is — and that is exactly why a keyless
 * gateway row is refused the fallback (see modelEnvFallback): the variable holds the user's
 * OpenAI (or Anthropic) key, and a gateway is neither. ModelScope also records the OPENAI_*
 * pair because its group credential is an api-inference access token; its rows are refused
 * the fallback on the same terms.
 */
export const MODEL_PROVIDERS: ModelProviderInfo[] = [
  {
    id: "tokendance",
    label: "TokenDance",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://tokendance.space/keys",
    modelsUrl: "https://tokendance.space/models",
    gatewayBaseUrl: TOKENDANCE_BASE_URL,
    // https://tokendance.space/docs/api-key-oauth
    oauth: {
      authorizeUrl: "https://tokendance.space/auth",
      exchangeUrl: "https://tokendance.space/portal/api/v1/auth/keys",
      keyName: "Adelie",
    },
    // https://tokendance.space/docs/open-api (the account's wallet, in micro-yuan)
    balance: {
      url: "https://tokendance.space/portal/api/v1/user/balance",
      format: "tokendance",
    },
  },
  {
    id: PENGUIN_GO_PROVIDER_ID,
    label: "Penguin Go",
    // The authorization flow writes one inlined relay key across the group. Keep the env
    // names private to this relay so the UI never suggests using a vendor credential here.
    envKey: "PENGUIN_GO_API_KEY",
    envBaseUrlKey: "PENGUIN_GO_BASE_URL",
    apiKeyUrl: "https://token.penguin.ooo/",
    modelsUrl: "https://token.penguin.ooo/",
    bridgeAuth: { flow: "penguin-go" },
  },
  {
    // One key serves the whole group, but its models sit on three protocols (see the group's
    // block comment in MODEL_CATALOG), so no group-level pin. The env pair records what its
    // Chat Completions majority's client reads, and a model added here by hand gets the preset
    // base URL below with openai-chat. Like every gateway's, a keyless row is refused that
    // fallback (modelEnvFallback): the key goes on the rows.
    id: "opencode-go",
    label: "OpenCode Go",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://opencode.ai/auth",
    modelsUrl: "https://opencode.ai/docs/go/",
    gatewayBaseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    id: "deepseek",
    label: "DeepSeek",
    envKey: "DEEPSEEK_API_KEY",
    envBaseUrlKey: "DEEPSEEK_BASE_URL",
    apiKeyUrl: "https://platform.deepseek.com/api_keys",
    modelsUrl: "https://api-docs.deepseek.com/quick_start/pricing",
    // https://api-docs.deepseek.com/api/get-user-balance
    balance: { url: `${DEEPSEEK_BASE_URL}/user/balance`, format: "deepseek" },
  },
  {
    id: "openrouter",
    label: "OpenRouter",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://openrouter.ai/workspaces/default/keys",
    modelsUrl: "https://openrouter.ai/models",
    gatewayBaseUrl: OPENROUTER_BASE_URL,
    // The whole group speaks the Responses API, which OpenRouter serves at the preset base
    // URL below for every upstream — presets and user-added entries alike.
    clientType: "openai-responses",
  },
  {
    id: "fireworks",
    label: "Fireworks AI",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://app.fireworks.ai/settings/users/api-keys",
    modelsUrl: "https://app.fireworks.ai/models",
    gatewayBaseUrl: FIREWORKS_BASE_URL,
  },
  {
    id: "google",
    label: "Google Gemini",
    envKey: "GEMINI_API_KEY",
    envBaseUrlKey: "GEMINI_BASE_URL",
    apiKeyUrl: "https://aistudio.google.com/api-keys",
    modelsUrl: "https://ai.google.dev/gemini-api/docs/models",
  },
  {
    id: "openai",
    label: "OpenAI",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://platform.openai.com/api-keys",
    modelsUrl: "https://platform.openai.com/docs/models",
  },
  {
    id: "anthropic",
    label: "Anthropic",
    envKey: "ANTHROPIC_API_KEY",
    envBaseUrlKey: "ANTHROPIC_BASE_URL",
    apiKeyUrl: "https://platform.claude.com/settings/keys",
    modelsUrl: "https://docs.claude.com/en/docs/about-claude/models/overview",
  },
  {
    id: "siliconflow",
    label: "SiliconFlow",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://cloud.siliconflow.cn/me/account/ak",
    modelsUrl: "https://cloud.siliconflow.cn/models",
    gatewayBaseUrl: SILICONFLOW_BASE_URL,
  },
  {
    id: "zhipu",
    label: "Z.AI (GLM)",
    envKey: "ZAI_API_KEY",
    envBaseUrlKey: "ZAI_BASE_URL",
    apiKeyUrl: "https://open.bigmodel.cn/apikey/platform",
    modelsUrl: "https://docs.z.ai/guides/overview/pricing",
  },
  {
    id: "moonshot",
    label: "Moonshot (Kimi)",
    envKey: "MOONSHOT_API_KEY",
    envBaseUrlKey: "MOONSHOT_BASE_URL",
    apiKeyUrl: "https://platform.kimi.com/console/api-keys",
    modelsUrl: "https://platform.kimi.com/docs/pricing",
  },
  {
    id: "minimax",
    label: "MiniMax",
    envKey: "MINIMAX_API_KEY",
    envBaseUrlKey: "MINIMAX_BASE_URL",
    // The pay-as-you-go key page; a Token Plan Subscription Key (Billing > Token Plan) works
    // against the same endpoint, so the group is not tied to either billing mode.
    apiKeyUrl: "https://platform.minimax.io/user-center/basic-information/interface-key",
    modelsUrl: "https://platform.minimax.io/docs/guides/models-intro",
  },
  {
    // Xiaomi's own model platform (MiMo API Open Platform). OpenAI-compatible at the
    // pay-as-you-go endpoint the preset rows inline (the same host's Anthropic-protocol path is
    // not catalogued), keys from its console, so no oauth or bridgeAuth. AgentHub has no Xiaomi
    // client, so every row pins `openai-chat` and names the endpoint; the group pins the same
    // protocol, which is also what keeps it out of the OPENAI_* env fallback
    // (providerEnvFallbackKey) — the pair below records what that generic client reads when a
    // row is handed no endpoint, not a variable of Xiaomi's.
    // https://mimo.mi.com/docs/quick-start/first-api-call, https://mimo.mi.com/docs/price/pay-as-you-go
    id: "xiaomi",
    label: "Xiaomi MiMo",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://platform.xiaomimimo.com/#/console/api-keys",
    modelsUrl: "https://mimo.mi.com/docs/quick-start/summary/model",
    clientType: "openai-chat",
  },
  {
    // Baidu's Qianfan platform, serving the ERNIE line. Its OpenAI-compatible base is `/v2` —
    // `/v1` is the retired interface, and nothing here derives the path from a `v1` habit.
    // https://cloud.baidu.com/doc/qianfan-api/s/3m7of64lb (endpoint and auth),
    // https://cloud.baidu.com/doc/qianfan/s/rmh4stp0j (model list), .../s/wmh4sv6ya (prices)
    id: "baidu",
    label: "Baidu ERNIE",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://console.bce.baidu.com/iam/#/iam/apikey/list",
    modelsUrl: "https://cloud.baidu.com/doc/qianfan/s/rmh4stp0j",
    clientType: "openai-chat",
  },
  {
    // Tencent Hunyuan's own endpoint (the OpenAI-compatible one; Tencent also publishes an
    // Anthropic-compatible path and the TC3-native API, neither catalogued). Tencent is moving
    // this platform to TokenHub, so the console link below is the one its docs name today.
    // https://cloud.tencent.com/document/product/1729/111007 (endpoint and auth),
    // .../1729/104753 (model list), .../1729/97731 (prices)
    id: "tencent",
    label: "Tencent Hunyuan",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://console.cloud.tencent.com/hunyuan/start",
    modelsUrl: "https://cloud.tencent.com/document/product/1729/104753",
    clientType: "openai-chat",
  },
  {
    // iFlytek Spark (讯飞星火). The OpenAI-compatible endpoint takes the console's APIPassword
    // as a Bearer token; only the WebSocket API needs the appid/APIKey/APISecret signature, and
    // nothing here uses it. The documented `model` values are still the 4.0 Ultra / Pro /
    // Pro-128K / Lite names (the doc names no id for the newer X2.5 and X2 line-ups), and the
    // doc's own model table dates from 2025 — see the rows in MODEL_CATALOG for what that means
    // for their prices.
    // https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html
    id: "iflytek",
    label: "iFlytek Spark",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://console.xfyun.cn/services/cbm",
    modelsUrl: "https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html",
    clientType: "openai-chat",
  },
  {
    // ByteDance's own model platform (火山方舟 / Volcengine Ark), serving the Doubao line,
    // whose Seed models are the ones that read video as well as images. OpenAI-compatible at
    // the region-scoped endpoint the rows below inline, one Bearer key from the Ark console,
    // so no oauth or bridgeAuth — the same shape as Xiaomi's group, and pinned the same way:
    // AgentHub has no Ark client, so the group pins `openai-chat` and every row names the
    // endpoint. Ark also resells GLM, Kimi and DeepSeek and serves image, video, 3D and
    // embedding models; only its text-generation chat models are catalogued here (the
    // modalities a Session can hold a conversation on), and the ids are the versioned ones
    // Ark's model list publishes.
    // https://www.volcengine.com/docs/82379/1330310 (model list),
    // https://docs.volcengine.com/docs/ark/model-pricing (prices)
    id: "volcengine",
    label: "Volcengine Ark",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey",
    modelsUrl: "https://www.volcengine.com/docs/82379/1330310",
    clientType: "openai-chat",
  },
  {
    id: "qwen-pay-as-you-go",
    label: "Qwen Pay-As-You-Go",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://platform.qianwenai.com/docs/api-reference/preparation/api-key",
    modelsUrl: "https://www.qianwenai.com/models",
    gatewayBaseUrl: QWEN_PAYG_BASE_URL,
  },
  {
    id: "qwen-token-plan",
    label: "Qwen Token Plan",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://platform.qianwenai.com/pricing/token-plan",
    modelsUrl:
      "https://platform.qianwenai.com/docs/token-plan/personal/token-plan-personal-overview",
    gatewayBaseUrl: QWEN_TOKEN_PLAN_BASE_URL,
  },
  {
    // ModelScope's own inference service: OpenAI-compatible, reselling other vendors' models
    // under `org/Model-Name` ids. Two things set it apart from the gateways above.
    //
    // Its credential is the account's access token rather than a minted API key, and the
    // group obtains one through the authorization bridge instead of sending the user to copy
    // it out of the console (bridgeAuth below; the console link stays for anyone who already
    // has a token). ModelScope is the vendor here as well as the gateway — the bridge exists
    // because its OAuth requires a client secret the desktop App cannot keep locally, not
    // because inference requests are routed through that bridge.
    //
    // The endpoint host is still inlined on every preset row; the row's own `client_type` tells
    // the frontend which AgentHub protocol that upstream model uses, as with Penguin Go.
    id: MODELSCOPE_PROVIDER_ID,
    label: "ModelScope",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://www.modelscope.cn/my/myaccesstoken",
    modelsUrl: "https://www.modelscope.cn/models",
    gatewayBaseUrl: MODELSCOPE_BASE_URL,
    bridgeAuth: { flow: "modelscope" },
  },
  {
    // Abliteration (abliteration.ai): its own line of "abliterated" models — the uncensored
    // fine-tunes the service is named after — served on one OpenAI-compatible base URL that
    // answers /chat/completions, /responses and /models. Keys start with `ak_` and come from
    // its console. Every id is its own spelling (`abliterated-model…`), which matches none of
    // AgentHub's routing rules, so each row pins the generic Chat Completions client and
    // inlines the endpoint; the group carries the endpoint too, as every gateway does.
    // https://docs.abliteration.ai/api/openai-compatibility
    id: "abliteration",
    label: "Abliteration",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    apiKeyUrl: "https://abliteration.ai/console",
    modelsUrl: "https://docs.abliteration.ai/",
    gatewayBaseUrl: ABLITERATION_BASE_URL,
  },
  {
    // Self-hosted: the user runs the server, so there is no console to mint a key at
    // (apiKeyUrl) and no endpoint to preset (gatewayBaseUrl). modelsUrl points at the recipe
    // index, which is where the served ids in this group are documented.
    id: "vllm",
    label: "vLLM",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    modelsUrl: "https://recipes.vllm.ai/",
    clientType: VLLM_CLIENT_TYPE,
    // The served ids are the user's own, so this group keeps taking models added by hand.
    addable: true,
  },
  {
    id: "custom",
    label: "Custom",
    envKey: "OPENAI_API_KEY",
    envBaseUrlKey: "OPENAI_BASE_URL",
    addable: true,
  },
];

/** Three-bucket price literal (unit fixed to usd_per_mtok). */
/**
 * Converts official CNY pricing to USD for storage (prices are always persisted in USD). The
 * conversion rate matches the web display's 7:1 convention, so switching the UI to CNY shows
 * exactly the vendor's official CNY price.
 */
function cny(cacheRead: number, cacheWrite: number, output: number): ModelPricing {
  const r = (v: number): number => Math.round((v / 7) * 1e6) / 1e6;
  return usd(r(cacheRead), r(cacheWrite), r(output));
}

function usd(cacheRead: number, cacheWrite: number, output: number): ModelPricing {
  return { unit: "usd_per_mtok", cache_read: cacheRead, cache_write: cacheWrite, output };
}

/**
 * A weekly peak/off-peak billing schedule, written in a fixed-offset zone.
 *
 * Only fixed offsets are expressible, which is the whole of what the catalog needs: the vendors
 * that bill this way publish their windows in a single national zone that does not observe DST
 * (Beijing is UTC+8 year round). A vendor on a DST zone would need a real tz database, and the
 * honest move then is to add one rather than to approximate.
 */
export interface OffPeakDiscount {
  /** Fraction off the peak price outside the windows below (0.5 = half price). */
  rate: number;
  /** Minutes east of UTC the windows are written in (Beijing = 480). */
  utcOffsetMinutes: number;
  /** Days the windows apply to, ISO numbering: 1 = Monday … 7 = Sunday. Days not listed are off-peak all day. */
  peakDays: readonly number[];
  /** Peak windows as [startHour, endHour) in the zone's local hours — end-exclusive, so 12:00 is already off-peak. */
  peakHours: readonly (readonly [number, number])[];
}

/**
 * Whether `now` falls outside every peak window, i.e. whether the discount is live.
 *
 * Computed in UTC arithmetic rather than through the host's local time: the schedule belongs to
 * the vendor's zone, and a server in Los Angeles must reach the same answer as one in Shanghai.
 */
export function offPeakAt(schedule: OffPeakDiscount, now: Date): boolean {
  const local = new Date(now.getTime() + schedule.utcOffsetMinutes * 60_000);
  // getUTCDay is 0 = Sunday; the ISO numbering the schedule uses puts Sunday at 7.
  const isoDay = local.getUTCDay() === 0 ? 7 : local.getUTCDay();
  if (!schedule.peakDays.includes(isoDay)) return true;
  const hour = local.getUTCHours() + local.getUTCMinutes() / 60;
  return !schedule.peakHours.some(([from, to]) => hour >= from && hour < to);
}

/** DeepSeek's official off-peak tier: half price outside Beijing weekday 09:00–12:00 and 14:00–18:00. */
export const DEEPSEEK_OFF_PEAK: OffPeakDiscount = {
  rate: 0.5,
  utcOffsetMinutes: 480,
  peakDays: [1, 2, 3, 4, 5],
  peakHours: [
    [9, 12],
    [14, 18],
  ],
};

/**
 * Qwen's off-peak tier on the time-banded models it sells (www.qianwenai.com/models/<id>, read
 * 2026-09-16): half price from 22:00 to 08:00 Beijing time, every day of the week, and the peak
 * price at every other hour. Each model page shows the two tiers as separate price tabs, and
 * every bucket's off-peak figure is exactly half its peak one.
 */
export const QWEN_OFF_PEAK: OffPeakDiscount = {
  rate: 0.5,
  utcOffsetMinutes: 480,
  peakDays: [1, 2, 3, 4, 5, 6, 7],
  peakHours: [[8, 22]],
};

/**
 * The catalog's time-based schedules, each with the references that carry it.
 *
 * The cost center needs this to split an aggregation by tier before it prices anything: the
 * rate a request ran at is a fact about when it ran, and only the catalog knows which rows have
 * two rates at all. Grouped by schedule so a second vendor's windows cost one entry, not a
 * second query.
 */
export function offPeakScheduledRefs(): Array<{
  schedule: OffPeakDiscount;
  refs: Array<{ provider: string; modelId: string }>;
}> {
  const bySchedule = new Map<
    OffPeakDiscount,
    { schedule: OffPeakDiscount; refs: Array<{ provider: string; modelId: string }> }
  >();
  for (const entry of MODEL_CATALOG) {
    const schedule = entry.offPeakDiscount;
    if (schedule === undefined) continue;
    const group = bySchedule.get(schedule) ?? { schedule, refs: [] };
    group.refs.push({ provider: entry.provider, modelId: entry.modelId });
    bySchedule.set(schedule, group);
  }
  return [...bySchedule.values()];
}

/**
 * What the seller actually bills for an entry: its list `pricing` less any running `discount`,
 * on every bucket. Rounded to the six decimals cny() already stores at, so a promotional rate
 * reads as a price rather than as a float artifact. An entry with no discount (or no pricing at
 * all) is returned untouched.
 *
 * Nothing writes this rate into a Project: `presetModelEntries` writes the list price (a
 * scheduled row's peak price), a flat promotion is stored beside it as a fraction, and which
 * tier a request ran in is decided from that request's own timestamp when the usage is
 * aggregated.
 */
export function effectivePricing(
  entry: ModelCatalogEntry,
  now: Date = new Date(),
): ModelPricing | undefined {
  const { pricing } = entry;
  if (pricing === undefined) return pricing;
  // A scheduled discount bills the list price during its peak windows and the reduced rate
  // outside them; a flat one always bills the reduced rate.
  const rate =
    entry.offPeakDiscount !== undefined
      ? offPeakAt(entry.offPeakDiscount, now)
        ? entry.offPeakDiscount.rate
        : 0
      : entry.discount;
  if (rate === undefined || rate === 0) return pricing;
  const off = (v: number): number => Math.round(v * (1 - rate) * 1e6) / 1e6;
  return {
    unit: pricing.unit,
    cache_read: off(pricing.cache_read),
    cache_write: off(pricing.cache_write),
    output: off(pricing.output),
  };
}

/**
 * Built-in model catalog, clustered by provider. Within each provider, entries are in
 * dictionary order by model id, except that newer versions of the same series come first
 * (e.g. gpt-5.6-* before gpt-5.5, claude-opus-4.8 before 4.7, glm-5.2 before glm-5). The
 * order is precomputed by hand right here — no runtime sorting anywhere.
 *
 * The clusters run in their own sequence, which is the row order a new Project's
 * `[[models]]` table is written in. It is not MODEL_PROVIDERS' display order and does not
 * have to track it: nothing renders a catalog row outside its provider group, and the page
 * takes the group sequence from MODEL_PROVIDERS.
 */
export const MODEL_CATALOG: ModelCatalogEntry[] = [
  // -- DeepSeek (official CNY pricing: cache hit / cache miss / output). Prices and model
  // names re-read 2026-09-16 from api-docs.deepseek.com/zh-cn/quick_start/pricing, which now
  // lists exactly two names, both with a 1M context window and a 384K output cap:
  // `deepseek-flash` (DeepSeek-V4.1-Flash, image input) on the peak tier CNY 0.04 / 2 / 8, and
  // `deepseek-v4-pro` (DeepSeek-V4-Pro-0813, text only) at 0.30 / 9 / 27. The rows store the
  // PEAK tier and declare DEEPSEEK_OFF_PEAK, which halves every bucket outside Beijing weekday
  // 09:00-12:00 and 14:00-18:00 — so both tiers are billed at the rate actually in force,
  // rather than one of them being approximated by the other.
  // The retired V4 Flash names, `deepseek-v4-flash` and `deepseek-v4-flash-vision-exp`, are
  // still accepted and served by V4.1 Flash at the Flash price and on the same schedule, but
  // the page no longer lists them: they stay below as retired rows, which are not presets. --
  {
    // DeepSeek's pricing page names this model `deepseek-flash` (model version
    // DeepSeek-V4.1-Flash, released as of the 2026-09-10 read): 1M context, image input, and
    // the Flash series peak price and off-peak schedule.
    // The id carries no `deepseek-v4` substring, and that substring is the only thing
    // AgentHub 0.4.11's AutoLLMClient routes DeepSeek on (it matches raw substrings against
    // `client_type || model_id`), so the bare name would be rejected as unsupported. This
    // direct-vendor row therefore pins the `deepseek-v4` client and inlines the vendor
    // endpoint — the same shape the MiniMax row uses, and the one exception to direct rows
    // leaving both unset. Drop the two fields once AgentHub routes the bare id.
    modelId: "deepseek-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: "deepseek",
    contextWindow: 1000000,
    pricing: cny(0.04, 2, 8),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "deepseek-v4",
    baseUrl: DEEPSEEK_BASE_URL,
  },
  {
    // Retired 2026-09-16 (see ModelCatalogEntry.retired): the default model of new Projects
    // from 0.2.0 to 0.2.8. DeepSeek still accepts the id and serves it from V4.1 Flash at the
    // Flash price, off-peak schedule included. Text only: AgentHub's DeepSeek client refuses
    // image parts for this id.
    modelId: "deepseek-v4-flash",
    displayName: "DeepSeek V4 Flash",
    provider: "deepseek",
    contextWindow: 1000000,
    pricing: cny(0.04, 2, 8),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: false,
    retired: true,
  },
  {
    // Retired 2026-09-16 (see ModelCatalogEntry.retired): the default model of new Projects in
    // 0.2.9, served from V4.1 Flash at the Flash price like deepseek-v4-flash above.
    modelId: "deepseek-v4-flash-vision-exp",
    displayName: "DeepSeek V4 Flash Vision Exp",
    provider: "deepseek",
    contextWindow: 1000000,
    pricing: cny(0.04, 2, 8),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    retired: true,
  },
  {
    // DeepSeek's pricing page names the model behind this id DeepSeek-V4-Pro-0813 (read
    // 2026-09-16), which is what the display name says — the same 0813 release the gateway
    // groups sell under dated ids. Text only. DeepSeek states that V4 Pro stays available
    // after 2026-09-14 with its billing unchanged, so the row keeps the V4 Pro peak price on
    // the shared off-peak schedule. AgentHub 0.4.11's DeepSeek client also matches this bare
    // id against its text-only deny-list /^deepseek-v4-(flash|pro)(-\d{4})?$/, so image parts
    // never leave the harness for it.
    modelId: "deepseek-v4-pro",
    displayName: "DeepSeek V4 Pro 0813",
    provider: "deepseek",
    contextWindow: 1000000,
    pricing: cny(0.3, 9, 27),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: false,
  },
  // -- OpenRouter (gateway: OpenAI-compatible protocol, preset base URL). Prices re-read in
  // one pass on 2026-08-07 from the models API (/api/v1/models; the API is authoritative
  // where a model's web page disagrees); the DeepSeek rows, the rows added on 2026-08-18
  // (gemini-3.7-flash, grok-4.6, deepseek-v4-pro-0813, glm-5.3, the openai/* additions) and
  // every pre-existing openai/* and google/* row were re-read on 2026-08-18 from the model
  // pages and the per-model endpoints API.
  //
  // Protocol: every row pins `openai-responses`. OpenRouter serves the Responses API at
  // {base}/responses for every upstream, at the same https://openrouter.ai/api/v1 base URL
  // the rows already carry, and the group pins the same protocol so an entry added to it by
  // hand inherits it. The generic Responses client sends a text-only tool result as a plain
  // string (AgentHub 0.4.11) and replays reasoning items only where the upstream returned
  // them.
  //
  // Price buckets: cache_read stores the published input_cache_read
  // (falling back to the input price for the rows without one — the :free rows and the
  // GPT Pro tiers, which publish no cache discount); cache_write stores
  // input_cache_write only when it is a genuine per-token write premium (the Anthropic, GPT
  // and qwen3.8-max rows, 1.25x input) —
  // Gemini's field is an hourly cache-STORAGE rate, not a per-token price, so those rows
  // keep the input price — and otherwise also carries the input price. The :free tier and
  // the openrouter/free Free Models Router store a genuine $0 price (not "unknown"), so
  // costs correctly compute to 0. GPT models are uniformly vision-capable (OpenAI
  // product-line policy) even where the gateway page omits the modality.
  //
  // Discounts: what these rows record is what OpenRouter actually BILLS, so a gateway
  // promotion is stored at its discounted rate (unlike the direct-vendor rows, which keep the
  // list price). The Gemini 3.x Flash rows are the exception: the promotion they sit on is
  // Google's own dated launch discount rather than the gateway's, so they keep the list price
  // and declare it in `discount`, exactly as their direct-vendor twins do. The endpoints API
  // exposes the running promotion as `pricing.discount` on the default endpoint; rows sitting
  // on one say so and name the rate to restore, because a lapsed promotion silently doubles
  // the real cost — that is exactly how the gpt-5.6-terra and gpt-5.6-luna rows drifted 2x
  // low before the 2026-08-18 re-read. --
  {
    modelId: "anthropic/claude-fable-5",
    displayName: "Claude Fable 5",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(1, 12.5, 50),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "anthropic/claude-opus-5",
    displayName: "Claude Opus 5",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.5, 6.25, 25),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "anthropic/claude-opus-4.8",
    displayName: "Claude Opus 4.8",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.5, 6.25, 25),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "anthropic/claude-opus-4.7",
    displayName: "Claude Opus 4.7",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.5, 6.25, 25),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "anthropic/claude-sonnet-5",
    displayName: "Claude Sonnet 5",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.2, 2.5, 10),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Listed 2026-09-10 from the models API: the base price is the PEAK tier and
    // `pricing.overrides` bill exactly half on weekends and outside weekday UTC 01:00-04:00 /
    // 06:00-10:00 — DeepSeek's own Beijing windows — so the row stores the peak price and
    // declares DEEPSEEK_OFF_PEAK, exactly as the direct row does. Context window and image
    // input from the same listing.
    modelId: "deepseek/deepseek-v4.1-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.006, 0.3, 1.2),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "deepseek/deepseek-v4-flash-0731",
    displayName: "DeepSeek V4 Flash 0731",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.0157192, 0.078596, 0.157192),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "deepseek/deepseek-v4-flash",
    displayName: "DeepSeek V4 Flash",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.0168, 0.0679, 0.168),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // DeepSeek serves this one alone on OpenRouter, so the stored rates are its own published
    // USD list ($0.22 / $0.66 / $0.007 cache read), and the context window is that endpoint's
    // 1,048,576. The row keeps that flat price as the API lists it: as of the 2026-09-10 read
    // the listing carries no `pricing.overrides`, so unlike the deepseek-v4.1-flash row above
    // there is no peak/off-peak split to record here.
    modelId: "deepseek/deepseek-v4-flash-vision-exp",
    displayName: "DeepSeek V4 Flash Vision Exp",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.007, 0.22, 0.66),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // The 0813 general-availability release of DeepSeek V4 Pro (OpenRouter listing dated
    // 2026-08-12); the default routed endpoint is DeepSeek's own API, so the price matches
    // the official USD list, and the context window is the default endpoint's 1,048,576.
    modelId: "deepseek/deepseek-v4-pro-0813",
    displayName: "DeepSeek V4 Pro 0813",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.022, 0.66, 1.98),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "deepseek/deepseek-v4-pro",
    displayName: "DeepSeek V4 Pro",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.022, 0.66, 1.98),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Same Gemini cache conventions as the gemini-3.7-flash row below. OpenRouter passes
    // Google's launch discount through, so it bills $0.075/$0.75/$3.75 — the list price less
    // 50%, declared in `discount` so the $0.15/$1.50/$7.50 list survives the promotion (same
    // treatment as the 3.6 and 3.7 rows below).
    modelId: "google/gemini-3.8-flash",
    displayName: "Gemini 3.8 Flash",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 7.5),
    discount: 0.5,
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Same Gemini cache conventions as the gemini-3.6-flash row below. The default Google
    // endpoint now bills $0.075/$0.75/$3.75 with `discount: 0.5` (endpoints API, read
    // 2026-09-09) — Google's launch discount passed straight through; the deeper
    // `discount: 0.75` promotion this row used to store has ended. Declared in `discount` so
    // the $0.15/$1.50/$7.50 list stays on file.
    modelId: "google/gemini-3.7-flash",
    displayName: "Gemini 3.7 Flash",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 7.5),
    discount: 0.5,
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // cache_read is billed as its own bucket in the cost center, and an input-priced
    // cache_read would overstate cache-heavy Gemini spend 10x; cache_write repeats the input
    // price (see the block comment — Gemini publishes storage-per-hour, not per-token write),
    // matching the direct-vendor Gemini rows below. OpenRouter bills $0.075/$0.75/$3.75
    // today (endpoints API, read 2026-09-09): it reports that halved rate as its plain price
    // with `discount: 0`, but it is Google's launch discount passed through and ends with it
    // on 2026-12-31, so this row records the list price and the promotion the way its
    // siblings do.
    modelId: "google/gemini-3.6-flash",
    displayName: "Gemini 3.6 Flash",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 7.5),
    discount: 0.5,
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // The 3.5 tier's own list price — $1.50 input / $9.00 output / $0.15 cache hit on Google's
    // page, with no launch discount — and OpenRouter's default endpoint bills exactly that
    // (`discount: 0`, read 2026-09-09). The $9 output is the tier, not a promotion to declare.
    modelId: "google/gemini-3.5-flash",
    displayName: "Gemini 3.5 Flash",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 9),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Same published-cache-price convention as gemini-3.6-flash above (2026-07-22: $0.03/mtok
    // cache hit, $0.30 input, $2.50 output; re-read 2026-09-09, unchanged and `discount: 0`).
    modelId: "google/gemini-3.5-flash-lite",
    displayName: "Gemini 3.5 Flash-Lite",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.03, 0.3, 2.5),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // No official separate cache price published: cache_read uses the standard input price (no discount assumed).
    modelId: "minimax/minimax-m3",
    displayName: "MiniMax M3",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.06, 0.3, 1.2),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "moonshotai/kimi-k3",
    displayName: "Kimi K3",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.3, 3, 15),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "moonshotai/kimi-k2.6",
    displayName: "Kimi K2.6",
    provider: "openrouter",
    contextWindow: 262144,
    pricing: usd(0.0992, 0.589, 2.48),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "nvidia/nemotron-3-ultra-550b-a55b:free",
    displayName: "Nemotron 3 Ultra (free)",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0, 0, 0),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  // The openai/* rows below mirror the direct OpenAI group one-for-one. Their context
  // windows are OpenRouter's published 1,050,000 / 400,000, matching the direct rows.
  {
    // Read 2026-09-09 from the models API and the per-model endpoints API: the default
    // endpoint is OpenAI's own, listed at $10 input / $1 cached input / $12.5 cache write /
    // $50 output with `discount: 0`, so the list price is what OpenRouter bills. The
    // endpoints API also publishes `overrides` above 272,000 prompt tokens (2x prompt and
    // cache, 1.5x completion); as on the direct row, only the base tier is recorded.
    modelId: "openai/gpt-6-astra",
    displayName: "GPT-6 Astra",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(1, 12.5, 50),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // The 50% promotion this row used to store has ended: OpenRouter now bills the full
    // $0.20/$1.20 rate (endpoints API `discount: 0`), so the stored rates doubled on the
    // 2026-08-18 re-read.
    modelId: "openai/gpt-5.6-luna",
    displayName: "GPT-5.6 Luna",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(0.02, 0.25, 1.2),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Running a `discount: 0.5` promotion as of 2026-08-18, so OpenRouter bills half the
    // $0.50/$6.25/$30 list price — restore the list rates when the promotion ends.
    modelId: "openai/gpt-5.6-sol",
    displayName: "GPT-5.6 Sol",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(0.25, 3.125, 15),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Same lapsed promotion as the luna row: now billed at the full $2/$12 rate.
    modelId: "openai/gpt-5.6-terra",
    displayName: "GPT-5.6 Terra",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(0.2, 2.5, 12),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "openai/gpt-5.5",
    displayName: "GPT-5.5",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(0.5, 5, 30),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // No published cache discount (the Pro tiers bill cached input at the standard rate), so
    // cache_read carries the input price — same convention as the direct gpt-5.5-pro row.
    modelId: "openai/gpt-5.5-pro",
    displayName: "GPT-5.5 Pro",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(30, 30, 180),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "openai/gpt-5.4",
    displayName: "GPT-5.4",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(0.25, 2.5, 15),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "openai/gpt-5.4-mini",
    displayName: "GPT-5.4 mini",
    provider: "openrouter",
    contextWindow: 400000,
    pricing: usd(0.075, 0.75, 4.5),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "openai/gpt-5.4-nano",
    displayName: "GPT-5.4 nano",
    provider: "openrouter",
    contextWindow: 400000,
    pricing: usd(0.02, 0.2, 1.25),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // No published cache discount, as with gpt-5.5-pro above.
    modelId: "openai/gpt-5.4-pro",
    displayName: "GPT-5.4 Pro",
    provider: "openrouter",
    contextWindow: 1050000,
    pricing: usd(30, 30, 180),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // OpenRouter's unified Free Models Router: each request is routed to a random free model
    // currently on OpenRouter, filtered by the features the request needs (tool calling,
    // structured outputs, ...). Routed targets vary, so the context window is a deliberately
    // conservative figure rather than any single target's real window: it keeps the 75%
    // compaction clamp meaningful (compaction fires at 96000) and reduces hard context-length
    // 400s on small-window targets. supportsVision stays false deliberately: the harness must
    // not send images to a router whose target may be text-only.
    modelId: "openrouter/free",
    displayName: "Free Models Router",
    provider: "openrouter",
    contextWindow: 128000,
    pricing: usd(0, 0, 0),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Read 2026-09-16 from the models API: $0.214 input / $2.55 output with a published $0.15
    // input_cache_read, a 1,000,000-token context window, and text, image and video input.
    modelId: "qwen/qwen3.8-27b",
    displayName: "Qwen 3.8 27B",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.15, 0.214, 2.55),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "qwen/qwen3.8-max",
    displayName: "Qwen 3.8 Max",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.25, 2.5, 6),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "qwen/qwen3.6-35b-a3b",
    displayName: "Qwen 3.6 35B A3B",
    provider: "openrouter",
    contextWindow: 262144,
    pricing: usd(0.05, 0.14, 1),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // No official separate cache price published: cache_read uses the standard input price.
    modelId: "stepfun/step-3.7-flash",
    displayName: "Step 3.7 Flash",
    provider: "openrouter",
    contextWindow: 256000,
    pricing: usd(0.04, 0.2, 1.15),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Tencent's Hy4 preview, read 2026-08-28 from its OpenRouter page and the models API:
    // $0.834 input / $2.501 output with a published $0.042 input_cache_read, a 1,048,576
    // context window, and text-only modalities in and out. The same upstream model is sold
    // in the TokenDance group at that gateway's own CNY rate; the two rows are priced by
    // their sellers and are not copies of one another.
    modelId: "tencent/hy4-preview",
    displayName: "Hy4 preview",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.042, 0.834, 2.501),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "tencent/hy3",
    displayName: "Hy3",
    provider: "openrouter",
    contextWindow: 262144,
    pricing: usd(0.033, 0.132, 0.528),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // Thinking Machines Lab's Inkling (released 2026-07-14): multimodal (image + audio
    // input). Specs from its OpenRouter page; pricing from the models API (2026-08-07),
    // which publishes $1 input (the page shows $0.95) and a $0.17 cached-input price.
    modelId: "thinkingmachines/inkling",
    displayName: "Inkling",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.17, 1, 4.05),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // xAI's Grok 4.6 (OpenRouter listing dated 2026-08-12): same $2/$6 input/output rates as
    // Grok 4.5 with a raised $0.50 cache-hit price.
    modelId: "x-ai/grok-4.6",
    displayName: "Grok 4.6",
    provider: "openrouter",
    contextWindow: 500000,
    pricing: usd(0.5, 2, 6),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "x-ai/grok-4.5",
    displayName: "Grok 4.5",
    provider: "openrouter",
    contextWindow: 500000,
    pricing: usd(0.3, 2, 6),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "xiaomi/mimo-v2.5",
    displayName: "MiMo-V2.5",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.0028, 0.14, 0.28),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // The gateway listing of the direct glm-5.3 row below; OpenRouter's single Z.AI endpoint
    // passes Z.AI's published price straight through (no discount), which is why the two
    // rows agree to the cent. Text-only, per the listing's modalities.
    modelId: "z-ai/glm-5.3",
    displayName: "GLM-5.3",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    // The gateway listing of the direct glm-5.3-flash row below. Stored at the rate OpenRouter's
    // default endpoint bills: DeepInfra (fp4), running `discount: 0.5` on the 0.03 / 0.15 / 0.5
    // list (endpoints API, re-read 2026-09-16) — restore the list rates when that endpoint's
    // promotion ends. Z.AI's own 50% promotion is a different one: it ended at 2026-09-09
    // 16:00 UTC, and the Z.AI endpoint on OpenRouter bills the list price since. The listing
    // takes text, images and video, and the generic Responses client it pins carries image
    // parts through.
    modelId: "z-ai/glm-5.3-flash",
    displayName: "GLM-5.3 Flash",
    provider: "openrouter",
    contextWindow: 1048576,
    pricing: usd(0.015, 0.075, 0.25),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  {
    modelId: "z-ai/glm-5.2",
    displayName: "GLM-5.2",
    provider: "openrouter",
    contextWindow: 1000000,
    pricing: usd(0.1261, 0.679, 2.134),
    supportsVision: false,
    clientType: "openai-responses",
    baseUrl: OPENROUTER_BASE_URL,
  },
  // -- Fireworks AI (gateway, standard serverless USD pricing: cached input / uncached
  // input / output from each model's page; API ids use the accounts/fireworks/models/<slug>
  // form, where Fireworks spells a version's dot as `p`: deepseek-v4p1-flash, glm-5p3). The
  // DeepSeek V4.1 Flash, DeepSeek V4 Pro 0813, GLM-5.3, GLM-5.3 Flash and Qwen 3.8 Max rows
  // were added 2026-09-16 from their fireworks.ai/models pages. Fireworks publishes no context
  // length for qwen3p8-max, so that row records the vendor's 1M window, as the other
  // qwen3.8-max rows do. --
  {
    // Fireworks' listing of the model DeepSeek serves directly as `deepseek-flash`: 1,048,576
    // context, image input, and the single serverless price its page lists, so no schedule.
    modelId: "accounts/fireworks/models/deepseek-v4p1-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: "fireworks",
    contextWindow: 1048576,
    pricing: usd(0.007, 0.22, 0.66),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/deepseek-v4-flash-0731",
    displayName: "DeepSeek V4 Flash 0731",
    provider: "fireworks",
    contextWindow: 1000000,
    pricing: usd(0.028, 0.14, 0.28),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/deepseek-v4-flash",
    displayName: "DeepSeek V4 Flash",
    provider: "fireworks",
    contextWindow: 1000000,
    pricing: usd(0.03, 0.14, 0.28),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/deepseek-v4-pro-0813",
    displayName: "DeepSeek V4 Pro 0813",
    provider: "fireworks",
    contextWindow: 1048576,
    pricing: usd(0.044, 1.32, 3.96),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/deepseek-v4-pro",
    displayName: "DeepSeek V4 Pro",
    provider: "fireworks",
    contextWindow: 1000000,
    pricing: usd(0.15, 1.74, 3.48),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/glm-5p3",
    displayName: "GLM-5.3",
    provider: "fireworks",
    contextWindow: 1048576,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    // Natively multimodal like every other GLM-5.3 Flash row, and this group's generic
    // openai-chat client forwards image_url parts, so image input works on this path.
    modelId: "accounts/fireworks/models/glm-5p3-flash",
    displayName: "GLM-5.3 Flash",
    provider: "fireworks",
    contextWindow: 1048576,
    pricing: usd(0.03, 0.15, 0.5),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/glm-5p2",
    displayName: "GLM-5.2",
    provider: "fireworks",
    contextWindow: 1000000,
    pricing: usd(0.14, 1.4, 4.4),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    // Thinking Machines Lab's Inkling (released 2026-07-14): multimodal (image + audio
    // input); specs and serverless pricing from its Fireworks model page (2026-08-06).
    modelId: "accounts/fireworks/models/inkling",
    displayName: "Inkling",
    provider: "fireworks",
    contextWindow: 1000000,
    pricing: usd(0.17, 1, 4.05),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/kimi-k3",
    displayName: "Kimi K3",
    provider: "fireworks",
    contextWindow: 1000000,
    pricing: usd(0.3, 3, 15),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/kimi-k2p7-code",
    displayName: "Kimi K2.7 Code",
    provider: "fireworks",
    contextWindow: 262144,
    pricing: usd(0.19, 0.95, 4),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/minimax-m3",
    displayName: "MiniMax M3",
    provider: "fireworks",
    contextWindow: 524288,
    pricing: usd(0.06, 0.3, 1.2),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  {
    modelId: "accounts/fireworks/models/qwen3p8-max",
    displayName: "Qwen 3.8 Max",
    provider: "fireworks",
    contextWindow: 1000000,
    pricing: usd(0.25, 2, 6),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: FIREWORKS_BASE_URL,
  },
  // -- SiliconFlow (gateway, official CNY pricing: cache hit / input / output) --
  {
    modelId: "deepseek-ai/DeepSeek-V4-Flash",
    displayName: "DeepSeek V4 Flash",
    provider: "siliconflow",
    contextWindow: 1000000,
    pricing: cny(0.02, 1, 2),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  {
    modelId: "deepseek-ai/DeepSeek-V4-Pro",
    displayName: "DeepSeek V4 Pro",
    provider: "siliconflow",
    contextWindow: 1000000,
    pricing: cny(0.1, 12, 24),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  {
    modelId: "meituan-longcat/LongCat-2.0",
    displayName: "LongCat 2.0",
    provider: "siliconflow",
    contextWindow: 1000000,
    pricing: cny(0.1, 5, 20),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  {
    modelId: "moonshotai/Kimi-K2.7-Code",
    displayName: "Kimi K2.7 Code",
    provider: "siliconflow",
    contextWindow: 262144,
    pricing: cny(1.3, 6.5, 27),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  // The Pro/ and Qwen/ entries below were unpriced until 2026-08-03 (SiliconFlow's price
  // list sits behind an authenticated console); prices below are its official CNY list
  // prices.
  {
    modelId: "Pro/moonshotai/Kimi-K2.6",
    displayName: "Kimi K2.6",
    provider: "siliconflow",
    contextWindow: 262144,
    pricing: cny(1.1, 6.5, 27),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  {
    // No cache-hit price on the list, so cache_read carries the input price.
    modelId: "Qwen/Qwen3.6-35B-A3B",
    displayName: "Qwen 3.6 35B A3B",
    provider: "siliconflow",
    contextWindow: 262144,
    pricing: cny(1.8, 1.8, 10.8),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  {
    // Added 2026-09-16, both ids confirmed in SiliconFlow's /v1/models listing. Hy4 preview is
    // text only at CNY 6 input / 18 output / 0.3 cache hit, with the 1,024,000-token window the
    // TokenDance hy4-preview row records; GLM-5.3 below is text only at 8 / 28 / 2, with the
    // same 1M window as zai-org/GLM-5.2.
    modelId: "tencent/Hy4-preview",
    displayName: "Hy4 preview",
    provider: "siliconflow",
    contextWindow: 1024000,
    pricing: cny(0.3, 6, 18),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  {
    modelId: "zai-org/GLM-5.3",
    displayName: "GLM-5.3",
    provider: "siliconflow",
    contextWindow: 1000000,
    pricing: cny(2, 8, 28),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  {
    modelId: "zai-org/GLM-5.2",
    displayName: "GLM-5.2",
    provider: "siliconflow",
    contextWindow: 1000000,
    pricing: cny(2, 8, 28),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: SILICONFLOW_BASE_URL,
  },
  // -- TokenDance (gateway: OpenAI-compatible protocol, preset base URL). Ids, context windows,
  // vision flags, supported protocols AND prices re-read 2026-10-27 from the seller's own
  // catalog API (GET https://tokendance.space/portal/api/models/all, no credential required;
  // the /gateway/v1/models listing beside it carries the modalities but no rates). That payload
  // states each model's current rate (`pricing.items`), the list price it is discounted FROM
  // (`pricing.reference`), and, for a row on a weekly schedule, both tiers again under
  // `pricing.time_pricing` — `options` holds the windows, `items` the peak rate, `base_items`
  // the rate outside them, `base_reference` the list those come off. TokenDance publishes an
  // input price and a cache-hit price with no separate cache-write fee, so cache_write carries
  // the input price.
  //
  // Discounts: a row stores the price billed INSIDE its peak window — the list price where
  // nothing is running — and declares what reduces it, a flat gateway promotion in `discount`
  // or the vendor's own windows in `offPeakDiscount`. effectivePricing() applies whichever one
  // is declared; a new Project stores the price and, for a flat promotion, the fraction beside
  // it, which the cost center applies, so it charges what the gateway charges. The two fields
  // are mutually exclusive. Where a gateway promotion rides ON TOP of DeepSeek's windows the
  // row keeps the schedule: the windows are the part that changes twice a day, and a flat
  // fraction in their place would over-state every off-peak record instead.
  //
  // Seven rows carry a flat promotion (re-read 2026-10-27): glm-5.3, glm-5.3-flash,
  // qwen3.8-max and deepseek-v4-pro at 10% off, the three Doubao Seed rows (seed-2.1-pro,
  // seed-2.1-turbo, seed-evolving) at 50%. Five follow DeepSeek's schedule instead — the four
  // DeepSeek ids this gateway resells (deepseek-v4-flash-0731, deepseek-v4-pro-0813,
  // deepseek-v4.1-flash, deepseek-v4.1-flash-1017) and the retired
  // deepseek-v4-flash-vision-exp. The rest carry neither, so for them the stored price is both
  // the list price and the billed rate.
  //
  // Two of the scheduled rows approximate the tier the schema cannot hold: the gateway bills
  // deepseek-v4-flash-0731 and deepseek-v4-pro-0813 at half their LIST price off-peak while
  // their peak rate is the promoted one, so a row that stores one rate and halves it lands 20%
  // under the gateway's own off-peak figure; their peak figures are exact. deepseek-v4.1-flash
  // and deepseek-v4.1-flash-1017 promote both tiers, so halving is exact for them. The seller's
  // catalog notes on deepseek-v4.1-flash that its DeepSeek endpoint has been upgraded to the
  // 1017 release; the dated id is a separate row, priced identically today.
  //
  // A running promotion also shows up without a credential: the catalog API opens such a
  // model's `description` with a bracketed 限时 ("limited-time") tag, so the same anonymous
  // request the windows come from is a cheap check on whether one is still live. --
  {
    // The dated V4 Flash the gateway resells. Re-read 2026-10-27: the list is CNY 3 input /
    // 9 output / 0.1 cache hit, the gateway bills 2.4 / 7.2 / 0.08 of that inside DeepSeek's
    // peak windows (20% off) and 1.5 / 4.5 / 0.05 outside them (half the list). One rate fits
    // on a row, so this one stores the promoted peak figure and halves it; see the group note
    // on the 20% the off-peak half then misses.
    modelId: "deepseek-v4-flash-0731",
    displayName: "DeepSeek V4 Flash 0731",
    provider: "tokendance",
    contextWindow: 1048576,
    pricing: cny(0.08, 2.4, 7.2),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Retired from TokenDance's line-up 2026-09-16 (see ModelCatalogEntry.retired). It was sold
    // on DeepSeek's own peak/off-peak schedule at the peak tier CNY 0.04 / 2 / 8, the price a
    // sync keeps on the Projects still carrying it.
    modelId: "deepseek-v4-flash-vision-exp",
    displayName: "DeepSeek V4 Flash Vision Exp",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(0.04, 2, 8),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
    retired: true,
  },
  {
    // The undated id of the same V4 Pro line, listed beside the dated one and read 2026-10-27:
    // a 1,048,576-token window, text only, no peak/off-peak schedule, and a flat 10% off the
    // CNY 9 input / 27 output / 0.3 cache hit list, so the gateway bills 8.1 / 24.3 / 0.27. Its
    // only other protocol is anthropic:messages, so this group's openai-chat pin is a
    // convention here rather than the only shape the id serves.
    modelId: "deepseek-v4-pro",
    displayName: "DeepSeek V4 Pro Preview",
    provider: "tokendance",
    contextWindow: 1048576,
    pricing: cny(0.3, 9, 27),
    discount: 0.1,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Re-read 2026-10-27: list CNY 9 input / 27 output / 0.3 cache hit, billed 7.2 / 21.6 /
    // 0.24 inside DeepSeek's peak windows (20% off) and 4.5 / 13.5 / 0.15 outside (half the
    // list) — the same shape as deepseek-v4-flash-0731 above, and the same 20% the stored
    // off-peak half misses.
    modelId: "deepseek-v4-pro-0813",
    displayName: "DeepSeek V4 Pro 0813",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(0.24, 7.2, 21.6),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // The dotted id is the seller's spelling of the model DeepSeek serves directly as
    // `deepseek-flash`. 1M context and native multimodality from the catalog API. It follows
    // the vendor's own peak/off-peak schedule rather than a flat gateway discount, so the row
    // stores the peak tier and declares DEEPSEEK_OFF_PEAK, which halves every bucket outside
    // Beijing weekday 09:00-12:00 and 14:00-18:00. Re-read 2026-10-27, that peak tier is the
    // gateway's BILLED one — CNY 2 input / 8 output / 0.04 cache hit list less the 20% it is
    // promoting, so 1.6 / 6.4 / 0.032 — and the off-peak half (0.8 / 3.2 / 0.016) is exactly
    // what the gateway bills outside the windows, the promotion applying to both tiers. The
    // seller's own note on this model says its DeepSeek endpoint has been upgraded to the 1017
    // release, which is the dated id below at the same rates. Unlike the direct row this one
    // needs no DeepSeek-specific pin — it is reached through this group's generic openai-chat
    // client, which forwards image_url parts.
    modelId: "deepseek-v4.1-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(0.032, 1.6, 6.4),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Listed 2026-10-27: DeepSeek-V4.1-Flash-1017, the release the undated row above now
    // serves, sold under its own id at the same figures — CNY 2 input / 8 output / 0.04 cache
    // hit list, 20% off inside DeepSeek's peak windows and half the list outside them (the
    // row stores the promoted peak tier, as that one does), 1M context, image input. The
    // seller's description says DeepSeek tuned it for its own harness and for two third-party
    // agent platforms, shortening task runtime and output length.
    modelId: "deepseek-v4.1-flash-1017",
    displayName: "DeepSeek V4.1 Flash 1017",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(0.032, 1.6, 6.4),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Dots Studio's (rednote / Xiaohongshu) Dots3-Note Preview: an open-weight MoE, 280B total
    // and 16B active, the lightest of the Dots 3 family. TokenDance lists it as "Dots3-Note
    // Preview(Free)": the words and the tag are kept as the seller spells them, as the
    // OpenRouter `(free)` rows keep theirs, but the full-width parentheses are the typesetting
    // of the seller's Chinese listing rather than part of the name, and a catalog name shows
    // untranslated in every UI language — so it takes the ASCII " (" of every other
    // parenthesised row, which reads right in both. The price is a
    // genuine CNY 0 on every bucket, the same treatment as the OpenRouter `:free` rows, so
    // costs compute to 0 and the free badge shows — over a 512,000-token context window, with
    // openai:chat-completions and anthropic:messages as its supported_protocols (this group's
    // openai-chat pin is a convention here, not the only shape the id serves). The gateway's
    // own listing describes it as covering multimodal understanding, which is what the
    // vision flag records; no image request was sent to it. Read 2026-09-15 from the
    // gateway's /models listing and tokendance.space/models/dots-3-note-preview.
    modelId: "dots-3-note-preview",
    displayName: "Dots3-Note Preview (Free)",
    provider: "tokendance",
    contextWindow: 512000,
    pricing: cny(0, 0, 0),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    modelId: "glm-5.3",
    displayName: "GLM-5.3",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(2, 8, 28),
    discount: 0.1,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Natively multimodal per TokenDance's catalog entry, and this group's generic
    // openai-chat client forwards image_url parts, so image input works on this path. Its
    // supported_protocols is openai:chat-completions alone, so the openai-chat pin is the
    // only shape this id serves.
    //
    // The 50% promotion this row used to carry ran through 2026-09-09 24:00 (the catalog
    // API's own description said so) and has been replaced by 10% off; the list price is
    // unchanged, only the `discount` fraction moved.
    modelId: "glm-5.3-flash",
    displayName: "GLM-5.3 Flash",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(0.23, 0.8, 2.8),
    discount: 0.1,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Listed 2026-10-27 beside the Flash above: GLM-5.3 FlashX, the same 1M window and the
    // same native multimodality (text, image and video in) with no promotion running, at CNY 2
    // input / 7 output / 0.57 cache hit. Its protocols are openai:chat-completions and
    // anthropic:messages, so the openai-chat pin is this group's convention rather than the
    // only shape it serves — unlike glm-5.3-flash above, where it is forced.
    modelId: "glm-5.3-flashx",
    displayName: "GLM-5.3 FlashX",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(0.57, 2, 7),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // The same upstream model as the OpenRouter `tencent/hy4-preview` row above, reached
    // through a second seller: each row records what its own seller charges, so the two must
    // not be made to agree (the qwen3.8-flash pair below states the same rule). TokenDance
    // sells it at CNY 6 input / 18 output / 0.3 cache hit, undiscounted, over a 1,024,000
    // context window — both figures its own, neither copied from the OpenRouter listing.
    //
    // Text-only: the catalog entry advertises no image modality, matching the OpenRouter
    // listing's text-in/text-out. Its supported_protocols are openai:chat-completions and
    // openai:responses, so the openai-chat pin is this group's convention rather than the
    // only shape the id serves — unlike glm-5.3-flash above, where it is forced.
    modelId: "hy4-preview",
    displayName: "Hy4 preview",
    provider: "tokendance",
    contextWindow: 1024000,
    pricing: cny(0.3, 6, 18),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Re-read 2026-10-27: the 40% promotion this row carried through 2026-09-16 has lapsed and
    // nothing replaced it, so the row stores the list price and declares no discount — CNY 20
    // input / 100 output / 1.6 cache hit, over a 1,048,576-token window.
    modelId: "kimi-k3",
    displayName: "Kimi K3",
    provider: "tokendance",
    contextWindow: 1048576,
    pricing: cny(1.6, 20, 100),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // Listed 2026-10-27: Kimi K3.1 Pro, the first of Moonshot's K3.1 line and the largest
    // model the seller carries — 1M context, text / image / video input, text out. Undiscounted
    // at CNY 13 input / 68 output / 1.3 cache hit. Its protocols are openai:chat-completions,
    // openai:responses and anthropic:messages, so the openai-chat pin is this group's
    // convention, as on kimi-k3 above.
    modelId: "kimi-k3.1-pro",
    displayName: "Kimi K3.1 Pro",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(1.3, 13, 68),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // One model reached two ways, priced by whoever is selling it: this row carries the
    // gateway's own CNY 0.8 input / 2.7 output / 0.1 cache hit, and the qwen-pay-as-you-go row
    // carries Qwen's direct list price for the same id. The two figures agree since Qwen's
    // 2026-09-16 re-pricing (its page had read 1 / 3 / 0.1), but only because the two sellers
    // do: each row still follows its own seller.
    //
    // Its supported_protocols is the widest in this group — openai:chat-completions,
    // openai:responses AND anthropic:messages — so the openai-chat pin here is the group's
    // convention rather than the only shape the id serves, unlike glm-5.3-flash above where
    // it is forced. Natively multimodal per the catalog entry, and this group's openai-chat
    // client forwards image_url parts, so image input works on this path.
    //
    // Undiscounted, so its list price and its billed rate coincide — unlike the
    // qwen3.8-max row below, which is on 10% off.
    modelId: "qwen3.8-flash",
    displayName: "Qwen 3.8 Flash",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(0.1, 0.8, 2.7),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    modelId: "qwen3.8-max",
    displayName: "Qwen 3.8 Max",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(1.5, 12, 36),
    discount: 0.1,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // The three Doubao Seed rows share this note. Priced 2026-09-02 from the seller's quoted
    // 50%-off rates, doubled back to the list price a row stores; context windows and
    // protocols from the catalog API, whose descriptions call the 2.1 models multimodal
    // Coding/Agent models (this group's openai-chat client forwards image_url parts, so
    // image input works on this path). That API tags the two 2.1 rows with a limited-time
    // 20% line rather than the 50% here; as for every promoted row in this group, the rate
    // recorded is the one the seller confirmed. seed-2.1-pro and seed-2.1-turbo also list
    // openai:responses, so their openai-chat pin is the group's convention rather than the
    // only shape they serve; seed-evolving lists chat-completions alone.
    //
    // Display names are the seller's own, as the catalog API spells them (re-read
    // 2026-09-10): Seed-2.1-Pro, Seed-2.1-Turbo, Seed-Evolving.
    modelId: "seed-2.1-pro",
    displayName: "Seed-2.1-Pro",
    provider: "tokendance",
    contextWindow: 256000,
    pricing: cny(1.2, 6, 30),
    discount: 0.5,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    modelId: "seed-2.1-turbo",
    displayName: "Seed-2.1-Turbo",
    provider: "tokendance",
    contextWindow: 256000,
    pricing: cny(0.6, 3, 15),
    discount: 0.5,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  {
    // A rolling id: the catalog API describes it as the newest Seed Coding/Agent model
    // under one stable id — the same model as seed-2.1-pro at the time of reading. Its window
    // was read as 256,000 on 2026-09-02 and is 1,000,000 on the 2026-10-27 re-read; the seller
    // prices it as seed-2.1-pro does.
    modelId: "seed-evolving",
    displayName: "Seed-Evolving",
    provider: "tokendance",
    contextWindow: 1000000,
    pricing: cny(1.2, 6, 30),
    discount: 0.5,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: TOKENDANCE_BASE_URL,
  },
  // -- Penguin Go (mixed-protocol relay). The model ids are the generation rows
  // provisioned by Penguin Go's generic-client authorization contract. Both
  // protocols share the /api base: the Google client appends /v1beta itself, while the
  // DeepSeek Responses client appends /responses. The rows carry Penguin Go's current list
  // prices so a new Project is complete before its first authorization, and no `discount`:
  // the platform delivers any promotion it runs at authorization and Sync, and both stay
  // authoritative when the relay later publishes changed metadata. The DeepSeek rows follow
  // DeepSeek's own lineup (re-read 2026-09-16): V4.1 Flash as `deepseek-flash` and V4 Pro 0813
  // as `deepseek-v4-pro`; the two retired V4 Flash ids, served from V4.1 Flash, are not resold.
  {
    modelId: "gemini-3.8-flash",
    displayName: "Gemini 3.8 Flash",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1048576,
    pricing: usd(0.075, 0.75, 3.75),
    supportsVision: true,
    clientType: "gemini-3.8",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "gemini-3.7-flash",
    displayName: "Gemini 3.7 Flash",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1048576,
    pricing: usd(0.075, 0.75, 3.75),
    supportsVision: true,
    clientType: "gemini-3.8",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "gemini-3.6-flash",
    displayName: "Gemini 3.6 Flash",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1048576,
    pricing: usd(0.075, 0.75, 3.75),
    supportsVision: true,
    clientType: "gemini-3.8",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "gemini-3.5-flash",
    displayName: "Gemini 3.5 Flash",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 9),
    supportsVision: true,
    clientType: "gemini-3.8",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "gemini-3.5-flash-lite",
    displayName: "Gemini 3.5 Flash-Lite",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1048576,
    pricing: usd(0.03, 0.3, 2.5),
    supportsVision: true,
    clientType: "gemini-3.8",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "gemini-3.1-flash-lite",
    displayName: "Gemini 3.1 Flash-Lite",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1048576,
    pricing: usd(0.025, 0.25, 1.5),
    supportsVision: true,
    clientType: "gemini-3.8",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "gemini-3.1-pro-preview",
    displayName: "Gemini 3.1 Pro (Preview)",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1048576,
    pricing: usd(0.2, 2, 12),
    supportsVision: true,
    clientType: "gemini-3.8",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "deepseek-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1000000,
    pricing: cny(0.04, 2, 8),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "deepseek-v4",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  {
    modelId: "deepseek-v4-pro",
    displayName: "DeepSeek V4 Pro 0813",
    provider: PENGUIN_GO_PROVIDER_ID,
    contextWindow: 1000000,
    pricing: cny(0.3, 9, 27),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: false,
    clientType: "deepseek-v4",
    baseUrl: PENGUIN_GO_BASE_URL,
  },
  // -- OpenCode Go (subscription gateway). The lineup, model ids, endpoints and per-token rates
  // are from opencode.ai/docs/go (read 2026-09-18), which lists these 27 models. Context
  // windows and input modalities are from the opencode-go provider on models.dev, the model
  // registry OpenCode maintains (hy3 records its input cap instead; see its row). The gateway's
  // /models listing also answers ten older ids the docs page does not list and models.dev
  // marks deprecated (minimax-m2.5, kimi-k2.5, glm-5, deepseek-flash, qwen3.5-plus,
  // mimo-v2-pro, mimo-v2-omni, hy3-preview, grok-4.5, omen-alpha); they are left out. So is
  // union-alpha, a limited-time free model the page listed on 2026-09-17 and dropped by
  // 2026-09-18.
  //
  // Protocol: the docs page's endpoint table puts each model on one path, and each row pins
  // the generic client for it: `openai-chat` for /chat/completions and `openai-responses` for
  // /responses, both on OPENCODE_GO_BASE_URL, and `ant-messages` for /v1/messages on
  // OPENCODE_GO_MESSAGES_BASE_URL. The paths are not interchangeable: /chat/completions refuses
  // grok-4.6 and fails for gpt-5.6-luna. The pins are load-bearing too: unpinned, gpt-5.6-luna,
  // glm-*, kimi-k3, kimi-k2.6, deepseek-v4* and minimax-m3 would reach their first-party
  // clients, and the other ids would not route at all. The gateway refuses a request that does
  // not name its conversation (400 "Request is missing x-opencode-session"), which
  // attributionHeaders does for this host on every request: a Session's requests carry its id,
  // and a request outside any Session — a connectivity test, a vision probe — a fresh one.
  //
  // Vision: from models.dev, and checked live on 2026-09-17 and 2026-09-18 by sending an image
  // of a number. Every vision row that could be reached read it back, and the text-only rows
  // refused the image or answered without seeing it. The rows below that could not be reached
  // keep models.dev's flag.
  //
  // Pricing: Go is a monthly subscription whose usage limits are dollar amounts. Each model has
  // a monthly allowance, capped at 20% per 5 hours and 50% per week. A request draws the
  // per-token rates the docs page publishes, and those are what the rows store. So the cost
  // center shows allowance spent, not an invoice, as with the Qwen Token Plan rows, which store
  // per-token list prices rather than the plan's fee. Where the page lists no cached-write rate,
  // cache_write carries the input rate. Four rows are tiered and store their base tier: GPT-5.6
  // Luna above 272K input tokens, Grok 4.6 above 200K, Qwen 3.7 Plus and Qwen 3.6 Plus above
  // 256K. The four DeepSeek rows publish a peak and an off-peak rate on DeepSeek's own windows
  // (peak 01:00-04:00 and 06:00-10:00 UTC on weekdays, i.e. DEEPSEEK_OFF_PEAK), off-peak
  // exactly half, so they store the peak rate and declare that schedule. DeepSeek V4.1 Flash's
  // running "4x" offer (ends 2026-09-20) raises its monthly allowance, not its rates, so no
  // row carries a `discount`.
  //
  // Account opt-in and region: five models answer 403 until the key's OpenCode workspace opts
  // in. The two Muse Spark Contributor models require consent to Meta training on prompts and
  // completions. deepseek-v4.1-flash, deepseek-v4-flash and deepseek-v4-pro require consent to
  // China-hosted serving. Some upstreams also refuse a caller's region: from a mainland China
  // address, gpt-5.6-luna answers 403 unsupported_country_region_territory and both Muse Spark
  // rows 403 "not available in your country", while gpt-5.6-luna passed every check from
  // outside it. The rows that could not be reached follow the docs page and the protocol the
  // other rows on their path proved. --
  {
    modelId: "deepseek-v4.1-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.006, 0.3, 1.2),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "deepseek-v4-flash",
    displayName: "DeepSeek V4 Flash",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.006, 0.3, 1.2),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "deepseek-v4-flash-vision-exp",
    displayName: "DeepSeek V4 Flash Vision Exp",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.006, 0.3, 1.2),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "deepseek-v4-pro",
    displayName: "DeepSeek V4 Pro",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.044, 1.32, 3.96),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "glm-5.3",
    displayName: "GLM-5.3",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "glm-5.3-flash",
    displayName: "GLM-5.3 Flash",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.03, 0.15, 0.5),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "glm-5.2",
    displayName: "GLM-5.2",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "glm-5.1",
    displayName: "GLM-5.1",
    provider: "opencode-go",
    contextWindow: 202752,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "gpt-5.6-luna",
    displayName: "GPT-5.6 Luna",
    provider: "opencode-go",
    contextWindow: 1050000,
    pricing: usd(0.02, 0.25, 1.2),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "grok-4.6",
    displayName: "Grok 4.6",
    provider: "opencode-go",
    contextWindow: 500000,
    pricing: usd(0.5, 2, 6),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "hy4-preview",
    displayName: "Hy4 preview",
    provider: "opencode-go",
    contextWindow: 1024000,
    pricing: usd(0.042, 0.834, 2.501),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    // models.dev lists a 256,000-token context for Hy3 but caps its input at 192,000. Compaction
    // derives from this field, so it records the input cap: at 256,000 a Session would keep
    // sending prompts the upstream refuses until it compacted at ~254K.
    modelId: "hy3",
    displayName: "Hy3",
    provider: "opencode-go",
    contextWindow: 192000,
    pricing: usd(0.035, 0.14, 0.58),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "kimi-k3",
    displayName: "Kimi K3",
    provider: "opencode-go",
    contextWindow: 1048576,
    pricing: usd(0.3, 3, 15),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "kimi-k2.7-code",
    displayName: "Kimi K2.7 Code",
    provider: "opencode-go",
    contextWindow: 262144,
    pricing: usd(0.19, 0.95, 4),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "kimi-k2.6",
    displayName: "Kimi K2.6",
    provider: "opencode-go",
    contextWindow: 262144,
    pricing: usd(0.16, 0.95, 4),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "longcat-2.0",
    displayName: "LongCat 2.0",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.006, 0.3, 1.2),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "mimo-v2.5",
    displayName: "MiMo-V2.5",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.0028, 0.14, 0.28),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "mimo-v2.5-pro",
    displayName: "MiMo-V2.5-Pro",
    provider: "opencode-go",
    contextWindow: 1048576,
    pricing: usd(0.003625, 0.435, 0.87),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "minimax-m3",
    displayName: "MiniMax M3",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.06, 0.3, 1.2),
    supportsVision: true,
    clientType: "ant-messages",
    baseUrl: OPENCODE_GO_MESSAGES_BASE_URL,
  },
  {
    modelId: "minimax-m2.7",
    displayName: "MiniMax M2.7",
    provider: "opencode-go",
    contextWindow: 204800,
    pricing: usd(0.06, 0.375, 1.2),
    supportsVision: false,
    clientType: "ant-messages",
    baseUrl: OPENCODE_GO_MESSAGES_BASE_URL,
  },
  {
    modelId: "muse-spark-1.3-contributor",
    displayName: "Muse Spark 1.3 Contributor",
    provider: "opencode-go",
    contextWindow: 1048576,
    pricing: usd(0.002, 0.1, 0.2),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "muse-spark-1.2-contributor",
    displayName: "Muse Spark 1.2 Contributor",
    provider: "opencode-go",
    contextWindow: 1048576,
    pricing: usd(0.002, 0.1, 0.2),
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: OPENCODE_GO_BASE_URL,
  },
  {
    modelId: "qwen3.8-flash",
    displayName: "Qwen 3.8 Flash",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.016, 0.2, 0.47),
    supportsVision: true,
    clientType: "ant-messages",
    baseUrl: OPENCODE_GO_MESSAGES_BASE_URL,
  },
  {
    modelId: "qwen3.8-max",
    displayName: "Qwen 3.8 Max",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.25, 2.5, 6),
    supportsVision: true,
    clientType: "ant-messages",
    baseUrl: OPENCODE_GO_MESSAGES_BASE_URL,
  },
  {
    modelId: "qwen3.7-max",
    displayName: "Qwen 3.7 Max",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.5, 3.125, 7.5),
    supportsVision: false,
    clientType: "ant-messages",
    baseUrl: OPENCODE_GO_MESSAGES_BASE_URL,
  },
  {
    modelId: "qwen3.7-plus",
    displayName: "Qwen 3.7 Plus",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.04, 0.5, 1.6),
    supportsVision: true,
    clientType: "ant-messages",
    baseUrl: OPENCODE_GO_MESSAGES_BASE_URL,
  },
  {
    modelId: "qwen3.6-plus",
    displayName: "Qwen 3.6 Plus",
    provider: "opencode-go",
    contextWindow: 1000000,
    pricing: usd(0.05, 0.625, 3),
    supportsVision: true,
    clientType: "ant-messages",
    baseUrl: OPENCODE_GO_MESSAGES_BASE_URL,
  },
  // -- Qwen Token Plan (subscription gateway; vision flags per the plan's supported-model
  // table, and for the rows added 2026-09-16 per their model pages' input modalities).
  // Pricing and context windows from each model's page at www.qianwenai.com/models/<id>
  // (official CNY list prices; limited-time promotions such as the 20%/50% off discounts are
  // not stored). Lineup updated 2026-09-16: deepseek-v4.1-flash, deepseek-v4-pro-0813,
  // glm-5.3 and qwen3.8-flash join; deepseek-v4-flash-0731, deepseek-v4-pro and glm-5.2
  // leave the plan.
  //
  // The two DeepSeek rows are priced by time of day: their pages list a peak and an off-peak
  // price, the off-peak one exactly half on every bucket and in force from 22:00 to 08:00
  // Beijing time every day. They store the PEAK price and declare QWEN_OFF_PEAK, the same
  // shape the DeepSeek rows take with DeepSeek's own schedule. --
  {
    // Peak CNY 2 input / 8 output / 0.2 cache hit; off-peak 1 / 4 / 0.1. Text and image input.
    modelId: "deepseek-v4.1-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: "qwen-token-plan",
    contextWindow: 1000000,
    pricing: cny(0.2, 2, 8),
    offPeakDiscount: QWEN_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_TOKEN_PLAN_BASE_URL,
  },
  {
    // Peak CNY 9 input / 27 output / 0.9 cache hit; off-peak 4.5 / 13.5 / 0.45. Text only.
    modelId: "deepseek-v4-pro-0813",
    displayName: "DeepSeek V4 Pro 0813",
    provider: "qwen-token-plan",
    contextWindow: 1000000,
    pricing: cny(0.9, 9, 27),
    offPeakDiscount: QWEN_OFF_PEAK,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: QWEN_TOKEN_PLAN_BASE_URL,
  },
  {
    modelId: "glm-5.3",
    displayName: "GLM-5.3",
    provider: "qwen-token-plan",
    contextWindow: 1048576,
    pricing: cny(2, 8, 28),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: QWEN_TOKEN_PLAN_BASE_URL,
  },
  {
    modelId: "qwen3.8-flash",
    displayName: "Qwen 3.8 Flash",
    provider: "qwen-token-plan",
    contextWindow: 1000000,
    pricing: cny(0.1, 0.8, 2.7),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_TOKEN_PLAN_BASE_URL,
  },
  {
    modelId: "qwen3.8-max",
    displayName: "Qwen 3.8 Max",
    provider: "qwen-token-plan",
    contextWindow: 1000000,
    pricing: cny(1.5, 12, 36),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_TOKEN_PLAN_BASE_URL,
  },
  {
    modelId: "qwen3.7-plus",
    displayName: "Qwen 3.7 Plus",
    provider: "qwen-token-plan",
    contextWindow: 1000000,
    pricing: cny(0.4, 2, 8),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_TOKEN_PLAN_BASE_URL,
  },
  // -- Qwen Pay-As-You-Go (DashScope's OpenAI-compatible pay-per-token marketplace; official
  // CNY list prices and specs from each model's page at www.qianwenai.com/models/<id> —
  // resold third-party models keep their upstream ids exactly as the page lists them: kimi/
  // and ZHIPU/ carry vendor prefixes, DeepSeek is listed bare). Lineup updated 2026-09-16:
  // deepseek-v4.1-flash, kimi/kimi-k2.8-preview and ZHIPU/GLM-5.3-Flash join;
  // deepseek-v4-flash-0731 and ZHIPU/GLM-5.2 leave. The DeepSeek row is priced by time of day
  // on QWEN_OFF_PEAK and stores its peak price, as in the Token Plan group above. --
  {
    // Peak CNY 2 input / 8 output / 0.2 cache hit; off-peak (22:00-08:00 Beijing) 1 / 4 / 0.1.
    // Text and image input, which this group's openai-chat client forwards.
    modelId: "deepseek-v4.1-flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: "qwen-pay-as-you-go",
    contextWindow: 1000000,
    pricing: cny(0.2, 2, 8),
    offPeakDiscount: QWEN_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_PAYG_BASE_URL,
  },
  {
    modelId: "kimi/kimi-k3",
    displayName: "Kimi K3",
    provider: "qwen-pay-as-you-go",
    contextWindow: 1048576,
    pricing: cny(2, 20, 100),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_PAYG_BASE_URL,
  },
  {
    // CNY 6.5 input / 27 output / 1.7 cache hit over a 1M window (1,048,576, as on the
    // kimi/kimi-k3 row); text, image and video input.
    modelId: "kimi/kimi-k2.8-preview",
    displayName: "Kimi K2.8 Preview",
    provider: "qwen-pay-as-you-go",
    contextWindow: 1048576,
    pricing: cny(1.7, 6.5, 27),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_PAYG_BASE_URL,
  },
  {
    // Official CNY list price from www.qianwenai.com/models/qwen3.8-flash, re-read 2026-09-16:
    // CNY 0.8 input / CNY 0.1 cache hit / CNY 2.7 output per MTok (it had been 1 / 0.1 / 3),
    // over a 1M-token input window with a 131K output cap. Its input modalities include
    // images and video, and this group's openai-chat client converts image parts.
    modelId: "qwen3.8-flash",
    displayName: "Qwen 3.8 Flash",
    provider: "qwen-pay-as-you-go",
    contextWindow: 1000000,
    pricing: cny(0.1, 0.8, 2.7),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_PAYG_BASE_URL,
  },
  {
    modelId: "qwen3.8-max",
    displayName: "Qwen 3.8 Max",
    provider: "qwen-pay-as-you-go",
    contextWindow: 1000000,
    pricing: cny(1.5, 12, 36),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_PAYG_BASE_URL,
  },
  {
    modelId: "qwen3.7-plus",
    displayName: "Qwen 3.7 Plus",
    provider: "qwen-pay-as-you-go",
    contextWindow: 1000000,
    pricing: cny(0.4, 2, 8),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_PAYG_BASE_URL,
  },
  {
    // CNY 0.8 input / 2.8 output / 0.23 cache hit over a 1M window with a 131K output cap;
    // natively multimodal like every other GLM-5.3 Flash row, and this group's openai-chat
    // client forwards image parts.
    modelId: "ZHIPU/GLM-5.3-Flash",
    displayName: "GLM-5.3 Flash",
    provider: "qwen-pay-as-you-go",
    contextWindow: 1000000,
    pricing: cny(0.23, 0.8, 2.8),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: QWEN_PAYG_BASE_URL,
  },
  // -- ModelScope (api-inference aggregate, preset base URL). Preset ids keep ModelScope's
  // own `org/Model-Name` spelling; DeepSeek was read from the endpoint's public /v1/models
  // listing (2026-09-18), and the Qwen additions from their public model pages (2026-09-20).
  //
  // ModelScope serves all three presets through its OpenAI Responses endpoint, so every row pins
  // AgentHub's generic Responses client. Keeping the protocol explicit also prevents ids from
  // auto-routing to vendor-specific clients with a different request shape.
  //
  // The window and vision flags are NOT read from ModelScope's own docs — its model pages are
  // client-rendered and carry no price. They repeat what other rows of the same models record,
  // because ModelScope serves the same upstream ids.
  //
  // `pricing` is deliberately absent. ModelScope bills for api-inference and does not publish a
  // read-able rate, so an absent pricing — which the catalog reads as "nobody has looked this
  // up", leaving the row unbadged and the group's usage uncosted — is the honest state. A
  // three-zero pricing would be worse than absent here, not better: `isFreeModel` would badge a
  // billed gateway as "Free". Whoever next reads ModelScope's pricing page should fill it in. --
  {
    modelId: "deepseek-ai/DeepSeek-V4.1-Flash",
    displayName: "DeepSeek V4.1 Flash",
    provider: MODELSCOPE_PROVIDER_ID,
    contextWindow: 1000000,
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: MODELSCOPE_BASE_URL,
  },
  {
    modelId: "Qwen/Qwen3.8-27B",
    displayName: "Qwen 3.8 27B",
    provider: MODELSCOPE_PROVIDER_ID,
    contextWindow: 262144,
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: MODELSCOPE_BASE_URL,
  },
  {
    modelId: "Qwen/Qwen3.8-Flash-Next",
    displayName: "Qwen 3.8 Flash Next",
    provider: MODELSCOPE_PROVIDER_ID,
    contextWindow: 262144,
    supportsVision: true,
    clientType: "openai-responses",
    baseUrl: MODELSCOPE_BASE_URL,
  },
  // -- MiniMax (direct M3 Responses client; official USD pay-as-you-go list prices, standard
  // tier at <=512K input — every rate doubles above 512K, and the priority tier is 1.5x). --
  {
    modelId: "MiniMax-M3",
    displayName: "MiniMax M3",
    provider: "minimax",
    contextWindow: 1000000,
    pricing: usd(0.06, 0.3, 1.2),
    supportsVision: true,
    clientType: "minimax-m3",
    baseUrl: MINIMAX_BASE_URL,
  },
  // -- Google Gemini (official USD pricing) --
  {
    // Same list price and same launch discount as the gemini-3.7-flash and gemini-3.6-flash
    // rows below: Google halves all three rates through 2026-12-31. Like those rows this one
    // declares the promotion in `discount`, so the list price stays on file — here and in a
    // Project, which is seeded with the fraction beside it — while the cost center bills the
    // 0.075/0.75/3.75 Google actually charges today. One field to delete when the promotion
    // lapses.
    modelId: "gemini-3.8-flash",
    displayName: "Gemini 3.8 Flash",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 7.5),
    discount: 0.5,
    supportsVision: true,
  },
  {
    // Google's list price, identical to gemini-3.6-flash (per AgentHub 0.4.2's registry and
    // Google's price page), halved through 2026-12-31 by a launch discount on all three
    // rates. That promotion is declared in `discount` — same treatment as gemini-3.8-flash
    // above — so the list price survives it and there is one field to delete when it lapses.
    modelId: "gemini-3.7-flash",
    displayName: "Gemini 3.7 Flash",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 7.5),
    discount: 0.5,
    supportsVision: true,
  },
  {
    // Same list price and same launch discount as the 3.7 and 3.8 rows: Google halves all
    // three rates through 2026-12-31 (Google's pricing page, read 2026-09-09).
    modelId: "gemini-3.6-flash",
    displayName: "Gemini 3.6 Flash",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 7.5),
    discount: 0.5,
    supportsVision: true,
  },
  {
    // Google's 3.5 tier list price ($1.50 / $9.00 / $0.15 cache hit), which carries no launch
    // discount — re-read 2026-09-09. The $9 output is what the tier costs, not a promotion.
    modelId: "gemini-3.5-flash",
    displayName: "Gemini 3.5 Flash",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.15, 1.5, 9),
    supportsVision: true,
  },
  {
    modelId: "gemini-3.5-flash-lite",
    displayName: "Gemini 3.5 Flash-Lite",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.03, 0.3, 2.5),
    supportsVision: true,
  },
  {
    modelId: "gemini-3.1-flash-lite",
    displayName: "Gemini 3.1 Flash-Lite",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.025, 0.25, 1.5),
    supportsVision: true,
  },
  {
    // ≤200K input tier; >200K has official surcharge pricing (see file header comment).
    modelId: "gemini-3.1-pro-preview",
    displayName: "Gemini 3.1 Pro (Preview)",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.2, 2, 12),
    supportsVision: true,
  },
  {
    modelId: "gemini-3-flash-preview",
    displayName: "Gemini 3 Flash (Preview)",
    provider: "google",
    contextWindow: 1048576,
    pricing: usd(0.05, 0.5, 3),
    supportsVision: true,
  },
  // -- Anthropic (official USD pricing; cache write = 1.25 x input). Re-read 2026-08-20 from
  // platform.claude.com/docs/en/about-claude/pricing. Sonnet 5's $2 input / $10 output is its
  // standard rate rather than an introductory one, so it prices below Sonnet 4.6 — that
  // inversion is Anthropic's list, not a transcription slip. Anthropic bills the full 1M
  // window at a single rate, so none of the long-context tiers named in the file header apply
  // here, and the fast-mode premium on Opus 5 / Opus 4.8 ($10 input / $50 output) is a
  // separate tier these rows do not record. --
  {
    modelId: "claude-fable-5",
    displayName: "Claude Fable 5",
    provider: "anthropic",
    contextWindow: 1000000,
    pricing: usd(1, 12.5, 50),
    supportsVision: true,
  },
  {
    modelId: "claude-opus-5",
    displayName: "Claude Opus 5",
    provider: "anthropic",
    contextWindow: 1000000,
    pricing: usd(0.5, 6.25, 25),
    supportsVision: true,
  },
  {
    modelId: "claude-opus-4-8",
    displayName: "Claude Opus 4.8",
    provider: "anthropic",
    contextWindow: 1000000,
    pricing: usd(0.5, 6.25, 25),
    supportsVision: true,
  },
  {
    modelId: "claude-opus-4-7",
    displayName: "Claude Opus 4.7",
    provider: "anthropic",
    contextWindow: 1000000,
    pricing: usd(0.5, 6.25, 25),
    supportsVision: true,
  },
  {
    modelId: "claude-sonnet-5",
    displayName: "Claude Sonnet 5",
    provider: "anthropic",
    contextWindow: 1000000,
    pricing: usd(0.2, 2.5, 10),
    supportsVision: true,
  },
  {
    modelId: "claude-sonnet-4-6",
    displayName: "Claude Sonnet 4.6",
    provider: "anthropic",
    contextWindow: 1000000,
    pricing: usd(0.3, 3.75, 15),
    supportsVision: true,
  },
  // -- OpenAI (official USD pricing) --
  {
    // OpenAI's list price (developers.openai.com/api/docs/models/gpt-6-astra, read
    // 2026-09-09) is $10 input / $1 cached input / $12.5 cache write / $50 output. Per the
    // bucket convention at the top of this file, cache_write carries the published
    // cache-write price of 12.5; the $10 rate applies only to input that is not written to
    // cache, a split the three buckets do not express. Every rate doubles above 272K input
    // tokens (output 1.5x) — the base tier is what this row records, as the header says.
    // Served by AgentHub's gpt6 client from the release that ships it.
    modelId: "gpt-6-astra",
    displayName: "GPT-6 Astra",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(1, 12.5, 50),
    supportsVision: true,
  },
  {
    // The bare gpt-5.6 id routes to gpt-5.6-sol upstream and is priced as that tier, so the
    // row names the Sol codename its siblings and the openai/gpt-5.6-sol row already show —
    // the id stays bare, only the label says which variant this is; served by AgentHub
    // 0.4.2's native gpt-5.6 client. This row and the two gpt-5.6 rows below mirror the
    // openai/gpt-5.6-* OpenRouter rows above, which carry the gateway's (currently
    // discounted) rates instead of this list price.
    modelId: "gpt-5.6",
    displayName: "GPT-5.6 Sol",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(0.5, 5, 30),
    supportsVision: true,
  },
  {
    modelId: "gpt-5.6-luna",
    displayName: "GPT-5.6 Luna",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(0.02, 0.2, 1.2),
    supportsVision: true,
  },
  {
    modelId: "gpt-5.6-terra",
    displayName: "GPT-5.6 Terra",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(0.2, 2, 12),
    supportsVision: true,
  },
  {
    modelId: "gpt-5.5",
    displayName: "GPT-5.5",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(0.5, 5, 30),
    supportsVision: true,
  },
  {
    // No official cache discount: cache_read uses the standard input price.
    modelId: "gpt-5.5-pro",
    displayName: "GPT-5.5 Pro",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(30, 30, 180),
    supportsVision: true,
  },
  {
    modelId: "gpt-5.4",
    displayName: "GPT-5.4",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(0.25, 2.5, 15),
    supportsVision: true,
  },
  {
    modelId: "gpt-5.4-mini",
    displayName: "GPT-5.4 mini",
    provider: "openai",
    contextWindow: 400000,
    pricing: usd(0.075, 0.75, 4.5),
    supportsVision: true,
  },
  {
    modelId: "gpt-5.4-nano",
    displayName: "GPT-5.4 nano",
    provider: "openai",
    contextWindow: 400000,
    pricing: usd(0.02, 0.2, 1.25),
    supportsVision: true,
  },
  {
    // No official cache discount: cache_read uses the standard input price.
    modelId: "gpt-5.4-pro",
    displayName: "GPT-5.4 Pro",
    provider: "openai",
    contextWindow: 1050000,
    pricing: usd(30, 30, 180),
    supportsVision: true,
  },
  // -- Z.AI (GLM) --
  {
    // Announced 2026-08-14 and served by AgentHub 0.4.2's unified GLM client. Z.AI's price
    // list (docs.z.ai/guides/overview/pricing, read 2026-08-18) publishes the same USD rates
    // as glm-5.2 / glm-5.1.
    modelId: "glm-5.3",
    displayName: "GLM-5.3",
    provider: "zhipu",
    contextWindow: 1000000,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
  },
  {
    // Z.AI's price list (docs.z.ai/guides/overview/pricing) publishes $0.15 input / $0.03
    // cached input / $0.50 output; a 50% promotion halved all three through 24:00 on
    // 2026-09-09 (UTC+8). Direct-vendor rows record the vendor's list price, so that is what
    // is stored here — the OpenRouter z-ai/glm-5.3-flash row above carries the rate its
    // default endpoint bills, which runs a promotion of its own.
    //
    // The model is natively multimodal (docs.z.ai/guides/vlm/glm-5.3-flash: images, video
    // and files), and it is the one GLM id whose images AgentHub's GLM client forwards — as
    // image_url parts, in a prompt and in a tool result alike. Every other GLM id refuses
    // one outright ("GLM <id> does not support image inputs."), which is why the rest of
    // this group is vision-off. That forwarding is why core's dependency range floors
    // @prismshadow/agenthub at 0.4.8.
    modelId: "glm-5.3-flash",
    displayName: "GLM-5.3 Flash",
    provider: "zhipu",
    contextWindow: 1000000,
    pricing: usd(0.03, 0.15, 0.5),
    supportsVision: true,
  },
  {
    modelId: "glm-5.2",
    displayName: "GLM-5.2",
    provider: "zhipu",
    contextWindow: 1000000,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
  },
  {
    modelId: "glm-5.1",
    displayName: "GLM-5.1",
    provider: "zhipu",
    contextWindow: 200000,
    pricing: usd(0.26, 1.4, 4.4),
    supportsVision: false,
  },
  {
    modelId: "glm-5",
    displayName: "GLM-5",
    provider: "zhipu",
    contextWindow: 200000,
    pricing: usd(0.2, 1, 3.2),
    supportsVision: false,
  },
  {
    // Zhipu's free tier, and the group's only $0 row: Z.AI's price list
    // (docs.z.ai/guides/overview/pricing) reads `Free` on all four of its buckets, and
    // 智谱开放平台 files the model under its own free-model list
    // (docs.bigmodel.cn/cn/guide/models/free/glm-4.7-flash) — both read 2026-10-07. Text in,
    // text out; 200K context with a 128K output cap; thinking mode, streaming, function
    // calling, context caching and MCP.
    //
    // It sits below the 5.x rows because it is older, not because it is smaller: Z.AI's
    // released-notes page dates GLM-4.7-Flash to 2026-01-19 and GLM-5 to 2026-02-12, and the
    // whole GLM-5 line follows it. The same page calls it the free-tier version of GLM-4.7
    // (2025-12-22).
    //
    // The id cannot be auto-routed. AgentHub 0.4.15 hands a model to its unified GLM client
    // only when the routing token — client_type, or the id itself — contains `glm-5`, and
    // this one carries `glm-4.7`; unrouted, AutoLLMClient rejects it outright. So the row
    // pins the client, exactly as the DeepSeek `deepseek-flash` and MiniMax M3 rows pin
    // theirs, and it pins no endpoint: the GLM client's own default is Z.AI's, and a
    // 智谱开放平台 key reaches the same model on the same id through ZAI_BASE_URL.
    modelId: "glm-4.7-flash",
    displayName: "GLM-4.7 Flash",
    provider: "zhipu",
    contextWindow: 200000,
    pricing: usd(0, 0, 0),
    supportsVision: false,
    clientType: "glm-5.3",
  },
  // -- Moonshot (Kimi) (official CNY pricing) --
  {
    modelId: "kimi-k3",
    displayName: "Kimi K3",
    provider: "moonshot",
    contextWindow: 1048576,
    pricing: cny(2, 20, 100),
    supportsVision: true,
  },
  {
    modelId: "kimi-k2.6",
    displayName: "Kimi K2.6",
    provider: "moonshot",
    contextWindow: 262144,
    pricing: cny(1.1, 6.5, 27),
    supportsVision: true,
  },
  {
    modelId: "kimi-k2.5",
    displayName: "Kimi K2.5",
    provider: "moonshot",
    contextWindow: 262144,
    pricing: cny(0.7, 4, 21),
    supportsVision: true,
  },
  // -- vLLM (self-hosted: the models AgentHub's openai-chat-vllm-adapter client carries a
  // per-model thinking switch for, as published at recipes.vllm.ai — read 2026-09-03).
  //
  // Every row prices at zero, and omits two other things, all because the user runs the server:
  // - **zero pricing**. There is no seller charging per token: what the deployment costs is
  //   the operator's own hardware, which no catalog rate could express. Three zero buckets are
  //   the same genuine $0 tier the `:free` gateway rows carry, so these models show the free
  //   badge and contribute 0 to the cost center rather than the "unpriced" mark — which is the
  //   truthful reading of a self-hosted endpoint that bills nobody.
  // - **no base URL**. Every deployment has its own; the user supplies it, as in `custom`.
  // - **no auto-routing**. Each row pins openai-chat-vllm-adapter explicitly, and the pin is
  //   load-bearing twice over: `Qwen/*` matches none of AutoLLMClient's substring rules and
  //   would be rejected outright, while `deepseek-ai/DeepSeek-V4-*` contains "deepseek-v4"
  //   and would reach DeepSeek's first-party Responses client — pointed at a vLLM server.
  //
  // contextWindow is the recipe's NATIVE length, which is the most a deployment can serve
  // without reconfiguration; an operator may serve less (`--max-model-len` below the native
  // limit) or, for the Qwen rows, far more with YaRN rope scaling. The catalog cannot know
  // which, and it derives the compaction thresholds from this number, so the honest default
  // is the checkpoint's own figure — an entry without one would be assumed to be 128000.
  // The DeepSeek rows additionally document `--max-model-len >= 393216` as the floor for
  // their top reasoning levels, which is well inside the window recorded here.
  {
    modelId: "deepseek-ai/DeepSeek-V4-Flash",
    displayName: "DeepSeek V4 Flash",
    provider: "vllm",
    contextWindow: 1000000,
    pricing: usd(0, 0, 0),
    supportsVision: false,
    clientType: VLLM_CLIENT_TYPE,
  },
  {
    // The experimental vision revision: DeepSeek's first multimodal V4, a ViT tower on the
    // same language backbone. Its recipe verifies a 32K deployment and notes the 1M the
    // checkpoint advertises was not what was measured; the window below is the checkpoint's,
    // matching every other DeepSeek V4 row in this catalog.
    modelId: "deepseek-ai/DeepSeek-V4-Flash-Vision-Exp",
    displayName: "DeepSeek V4 Flash Vision Exp",
    provider: "vllm",
    contextWindow: 1000000,
    pricing: usd(0, 0, 0),
    supportsVision: true,
    clientType: VLLM_CLIENT_TYPE,
  },
  {
    modelId: "deepseek-ai/DeepSeek-V4-Pro",
    displayName: "DeepSeek V4 Pro",
    provider: "vllm",
    contextWindow: 1000000,
    pricing: usd(0, 0, 0),
    supportsVision: false,
    clientType: VLLM_CLIENT_TYPE,
  },
  {
    modelId: "Qwen/Qwen3.5-0.8B",
    displayName: "Qwen 3.5 0.8B",
    provider: "vllm",
    contextWindow: 262144,
    pricing: usd(0, 0, 0),
    supportsVision: true,
    clientType: VLLM_CLIENT_TYPE,
  },
  {
    modelId: "Qwen/Qwen3.5-9B",
    displayName: "Qwen 3.5 9B",
    provider: "vllm",
    contextWindow: 262144,
    pricing: usd(0, 0, 0),
    supportsVision: true,
    clientType: VLLM_CLIENT_TYPE,
  },
  {
    modelId: "Qwen/Qwen3.6-35B-A3B",
    displayName: "Qwen 3.6 35B A3B",
    provider: "vllm",
    contextWindow: 262144,
    pricing: usd(0, 0, 0),
    supportsVision: true,
    clientType: VLLM_CLIENT_TYPE,
  },
  {
    modelId: "Qwen/Qwen3.8-27B",
    displayName: "Qwen 3.8 27B",
    provider: "vllm",
    contextWindow: 262144,
    pricing: usd(0, 0, 0),
    supportsVision: true,
    clientType: VLLM_CLIENT_TYPE,
  },
  {
    modelId: "Qwen/Qwen3.8-Flash-Next",
    displayName: "Qwen 3.8 Flash Next",
    provider: "vllm",
    contextWindow: 262144,
    pricing: usd(0, 0, 0),
    supportsVision: true,
    clientType: VLLM_CLIENT_TYPE,
  },
  // -- Xiaomi MiMo (first-party vendor: OpenAI-compatible, so every row pins the protocol and
  // inlines the endpoint — AgentHub routes a bare `mimo-*` id nowhere). Ids, windows, vision
  // flags and prices read off the MiMo open platform's model and price pages on 2026-10-15;
  // its prices are CNY per million tokens (cache hit / input miss / output), which is the order
  // `cny` takes. Left out: `mimo-v2.6-pro-ultraspeed` (the model page carries no capability
  // column for it, so neither a window nor vision support could be read off it) and the v2.5
  // pair the platform dates for retirement on 2026-10-21.
  {
    modelId: "mimo-v2.6-pro",
    displayName: "MiMo V2.6 Pro",
    provider: "xiaomi",
    contextWindow: 1000000,
    pricing: cny(0.025, 3, 6),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: XIAOMI_BASE_URL,
  },
  {
    modelId: "mimo-v2.6-flash",
    displayName: "MiMo V2.6 Flash",
    provider: "xiaomi",
    contextWindow: 1000000,
    pricing: cny(0.02, 1, 2),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: XIAOMI_BASE_URL,
  },
  // -- Baidu Qianfan (ERNIE). Prices are the vendor's CNY per thousand tokens scaled to the
  // million the catalog stores, read on 2026-10-15; the ≥32K-input tier of the ERNIE 5 rows
  // (6 / 22 CNY) is not recorded, for the same single-rate reason the OpenAI rows record their
  // base tier. `ernie-5.1` sits in the model list's text-generation section rather than the
  // vision one, so its row is text-only. Qianfan also resells other vendors' models
  // (DeepSeek, Qwen, GLM, Kimi); they are left out — each has its own group here.
  {
    modelId: "ernie-5.1",
    displayName: "ERNIE 5.1",
    provider: "baidu",
    contextWindow: 131072,
    pricing: cny(4, 4, 18),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: BAIDU_BASE_URL,
  },
  {
    modelId: "ernie-5.0",
    displayName: "ERNIE 5.0",
    provider: "baidu",
    contextWindow: 131072,
    pricing: cny(6, 6, 24),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: BAIDU_BASE_URL,
  },
  {
    modelId: "ernie-4.5-turbo-vl",
    displayName: "ERNIE 4.5 Turbo VL",
    provider: "baidu",
    contextWindow: 131072,
    pricing: cny(3, 3, 9),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: BAIDU_BASE_URL,
  },
  {
    modelId: "ernie-4.5-turbo-128k",
    displayName: "ERNIE 4.5 Turbo 128K",
    provider: "baidu",
    contextWindow: 131072,
    // The one ERNIE row whose price list publishes a cache-hit rate (0.2 CNY); the others read
    // their input price, as every row without a published cache price does.
    pricing: cny(0.2, 0.8, 3.2),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: BAIDU_BASE_URL,
  },
  {
    modelId: "ernie-x1.1",
    displayName: "ERNIE X1.1",
    provider: "baidu",
    contextWindow: 65536,
    pricing: cny(1, 1, 4),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: BAIDU_BASE_URL,
  },
  // -- Tencent Hunyuan. The windows are the model list's "maximum input" (Hunyuan publishes a
  // separate maximum output beside it, which the catalog's single window cannot carry). Prices
  // are the vendor's CNY per million tokens, read on 2026-10-15. Left out: the role-play and
  // translation rows (single-purpose), the embedding row (not a chat model), and
  // `hunyuan-turbos-latest` / `hunyuan-vision`, which the compatibility example names but the
  // model list does not describe.
  {
    modelId: "hunyuan-a13b",
    displayName: "Hunyuan A13B",
    provider: "tencent",
    contextWindow: 229376,
    pricing: cny(0.5, 0.5, 2),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: HUNYUAN_BASE_URL,
  },
  {
    modelId: "hunyuan-vision-1.5-instruct",
    displayName: "Tencent HY Vision 1.5 Instruct",
    provider: "tencent",
    contextWindow: 24576,
    pricing: cny(3, 3, 9),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: HUNYUAN_BASE_URL,
  },
  {
    modelId: "hunyuan-t1-vision-20250916",
    displayName: "Hunyuan T1 Vision",
    provider: "tencent",
    contextWindow: 28672,
    pricing: cny(3, 3, 9),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: HUNYUAN_BASE_URL,
  },
  {
    modelId: "hunyuan-turbos-vision-video",
    displayName: "Hunyuan TurboS Vision Video",
    provider: "tencent",
    contextWindow: 24576,
    pricing: cny(3, 3, 9),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: HUNYUAN_BASE_URL,
  },
  // -- iFlytek Spark. The `model` values and the windows are the HTTP doc's own table (Ultra
  // 32K in / 32K out, Pro-128K 128K, Lite 8K; the Max pair the table also lists is left out —
  // Spark states its Max plans went offline on 2026-03-10 and their traffic moved to Ultra).
  // That doc publishes no price list, and the product page's prices name X2.5 / X2 model
  // families for which iFlytek has published no API id, so those rows carry no price at all
  // rather than a guessed or borrowed one; Lite is the one the doc calls free, and its zero is
  // the vendor's. Both facts are why this group is the catalog's weakest: re-read it before
  // trusting a row.
  {
    modelId: "4.0Ultra",
    displayName: "Spark 4.0 Ultra",
    provider: "iflytek",
    contextWindow: 32768,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: IFLYTEK_BASE_URL,
  },
  {
    modelId: "pro-128k",
    displayName: "Spark Pro-128K",
    provider: "iflytek",
    contextWindow: 131072,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: IFLYTEK_BASE_URL,
  },
  {
    modelId: "lite",
    displayName: "Spark Lite",
    provider: "iflytek",
    contextWindow: 8192,
    pricing: usd(0, 0, 0),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: IFLYTEK_BASE_URL,
  },
  // -- Volcengine Ark (ByteDance). Ids, windows and input modalities read 2026-10-25 from
  // Ark's own model list (its rows name the ids and the per-model context window; the
  // "多模态理解" capability marker is what makes a row vision), prices from its model-pricing
  // page's 在线推理(常规) table in CNY per million tokens (input non-audio / cache hit
  // non-audio / output) — the same three buckets `cny` takes. Two things the rows say that the
  // pages state only indirectly: Ark's prices are tiered by input length and only the base
  // tier is recorded (the catalog's standing convention, so long-context usage is
  // underestimated); and `supportsVideo` follows models.dev's per-model input modalities for
  // this provider, the only source that states video per model — which is why the 2.1 Lite row
  // records no video while its siblings do. Ark's resold GLM / Kimi / DeepSeek rows are
  // catalogued here (Ark is the seller) but its image, video, 3D and embedding models are not:
  // they are not chat models, and a Session cannot hold a conversation on one.
  {
    modelId: "doubao-seed-evolving",
    displayName: "Doubao Seed Evolving",
    provider: "volcengine",
    contextWindow: 1048576,
    pricing: cny(1.2, 6, 30),
    supportsVision: true,
    supportsVideo: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    modelId: "doubao-seed-2-1-pro-260915",
    displayName: "Doubao Seed 2.1 Pro",
    provider: "volcengine",
    contextWindow: 1048576,
    pricing: cny(1.2, 6, 30),
    supportsVision: true,
    supportsVideo: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    modelId: "doubao-seed-2-1-lite-260915",
    displayName: "Doubao Seed 2.1 Lite",
    provider: "volcengine",
    contextWindow: 1048576,
    pricing: cny(0.16, 0.8, 2.7),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    modelId: "doubao-seed-2-1-turbo-260628",
    displayName: "Doubao Seed 2.1 Turbo",
    provider: "volcengine",
    contextWindow: 262144,
    pricing: cny(0.6, 3, 15),
    supportsVision: true,
    supportsVideo: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    modelId: "doubao-seed-2-0-lite-260428",
    displayName: "Doubao Seed 2.0 Lite",
    provider: "volcengine",
    contextWindow: 262144,
    pricing: cny(0.12, 0.6, 3.6),
    supportsVision: true,
    supportsVideo: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    modelId: "doubao-seed-2-0-mini-260428",
    displayName: "Doubao Seed 2.0 Mini",
    provider: "volcengine",
    contextWindow: 262144,
    pricing: cny(0.04, 0.2, 2),
    supportsVision: true,
    supportsVideo: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    // The role-play model: no GUI capability and a smaller window than the Seed line it
    // belongs to, but the same multimodal input (video included).
    modelId: "doubao-seed-character-260628",
    displayName: "Doubao Seed Character",
    provider: "volcengine",
    contextWindow: 131072,
    pricing: cny(0.16, 0.8, 2),
    supportsVision: true,
    supportsVideo: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    modelId: "glm-5-3-flash-260828",
    displayName: "GLM-5.3-Flash",
    provider: "volcengine",
    contextWindow: 1048576,
    pricing: cny(0.23, 0.8, 2.8),
    supportsVision: true,
    supportsVideo: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    modelId: "kimi-k3-1-pro-261024",
    displayName: "Kimi K3.1 Pro",
    provider: "volcengine",
    contextWindow: 1048576,
    pricing: cny(1.3, 13, 68),
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    // Half price outside Beijing weekday 09:00–12:00 and 14:00–18:00 — the same windows
    // DeepSeek itself bills on, which is why the shared schedule is reused here; the price
    // recorded is the peak one (see offPeakDiscount).
    modelId: "deepseek-v4-1-flash-261017",
    displayName: "DeepSeek V4.1 Flash",
    provider: "volcengine",
    contextWindow: 1048576,
    pricing: cny(0.04, 2, 8),
    offPeakDiscount: DEEPSEEK_OFF_PEAK,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  {
    // Text only, unlike the flash row above: Ark's model list carries no 多模态理解 marker for it.
    modelId: "deepseek-v4-pro-ga-260813",
    displayName: "DeepSeek V4 Pro",
    provider: "volcengine",
    contextWindow: 1048576,
    pricing: cny(0.3, 9, 27),
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: VOLCENGINE_BASE_URL,
  },
  // -- Abliteration. Its own ids, read on 2026-10-23 from the service's own model list
  // (GET https://api.abliteration.ai/v1/models), which publishes everything the rows below
  // record: the window, the input modalities and the per-token rates. Prices are the list
  // rates converted to the catalog's USD-per-million buckets (input per token 0.000001 = 1
  // USD/Mtok, and so on); the service charges no cache-write premium, so cache_write repeats
  // the input rate while cache_read stores the discounted hit rate it does publish. Vision
  // follows input_modalities: only the two rows that accept an image say so. Every id pins
  // openai-chat and inlines the endpoint — none of them matches an AgentHub routing rule.
  // Largest first, newest revision of a size ahead of its predecessor.
  {
    modelId: "abliterated-model-large-v2",
    displayName: "Abliterated Model Large V2",
    provider: "abliteration",
    contextWindow: 1000000,
    pricing: usd(0.3, 3, 5),
    maxImages: 4,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: ABLITERATION_BASE_URL,
  },
  {
    modelId: "abliterated-model-large",
    displayName: "Abliterated Model Large",
    provider: "abliteration",
    contextWindow: 1000000,
    pricing: usd(0.3, 3, 5),
    maxImages: 4,
    supportsVision: false,
    clientType: "openai-chat",
    baseUrl: ABLITERATION_BASE_URL,
  },
  {
    modelId: "abliterated-model-large-flash",
    displayName: "Abliterated Model Large Flash",
    provider: "abliteration",
    contextWindow: 1048576,
    pricing: usd(0.1, 1, 2),
    maxImages: 4,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: ABLITERATION_BASE_URL,
  },
  {
    modelId: "abliterated-model",
    displayName: "Abliterated Model",
    provider: "abliteration",
    contextWindow: 262144,
    pricing: usd(0.1, 1, 3),
    maxImages: 4,
    supportsVision: true,
    clientType: "openai-chat",
    baseUrl: ABLITERATION_BASE_URL,
  },
  // -- Custom (the group that otherwise holds only user-defined models). A preset may live
  // here only as a complete row — its own base URL and a pinned generic protocol — because the
  // group implies neither; it is where a vendor with a single preview model and no console
  // of its own goes rather than opening a group for it. Atria Dawn Preview: the Anthropic
  // Messages API at api.atria-asi.ai (the client appends /v1/messages, so the base URL carries
  // no /v1; the endpoint also serves Chat Completions and Responses, but its Responses side
  // rejects the replayed assistant turn of a multi-turn conversation), a 256K window (262144 —
  // the API caps max_output_tokens at that minus the input), text only (the endpoint rejects
  // image input), and no published price yet, so the row records $0 until the vendor prices
  // it. Read 2026-09-15 from api.atria-asi.ai/docs.
  {
    modelId: "Atria-Dawn-Preview",
    displayName: "Atria Dawn Preview",
    provider: "custom",
    contextWindow: 262144,
    pricing: usd(0, 0, 0),
    supportsVision: false,
    clientType: "ant-messages",
    baseUrl: "https://api.atria-asi.ai",
  },
];

/**
 * Canonical spelling of an AgentHub client-type string. AgentHub 0.4.2 renamed the generic
 * Chat Completions client from `openai` to `openai-chat`; the bare `openai` spelling still
 * routes upstream as a deprecated alias, but the harness converges on the canonical name —
 * config reads/writes and API request handling all normalize through here, so configs saved
 * before the rename keep working while comparisons (legacy-protocol display, catalog sync)
 * see one spelling. Any other value (including `openai-responses` / `openai-embedding`,
 * which merely contain "openai") passes through unchanged.
 */
export function canonicalClientType(clientType: string | undefined): string | undefined {
  if (clientType === undefined) return undefined;
  return clientType.trim().toLowerCase() === "openai" ? "openai-chat" : clientType;
}

/** Looks up a catalog entry by (provider, upstream id) pair (**the sole catalog-matching entry point**); returns undefined if not in the catalog. */
export function catalogEntryFor(
  provider: string,
  upstreamId: string,
): ModelCatalogEntry | undefined {
  return MODEL_CATALOG.find((m) => m.provider === provider && m.modelId === upstreamId);
}

/** Looks up provider info by provider id; returns undefined for an unknown id. */
export function providerInfo(providerId: string): ModelProviderInfo | undefined {
  return MODEL_PROVIDERS.find((p) => p.id === providerId);
}

/**
 * The per-request image cap the catalog records for a model (ModelCatalogEntry.maxImages), or
 * undefined when the vendor publishes none — the case for every row but Abliteration's.
 *
 * Read from the catalog rather than stored on the Project entry: the cap is a property of the
 * seller, not a preference, so a Project that renamed or repriced the row still sends at most
 * what the seller accepts. A user-added row (no catalog entry) gets no cap.
 */
export function catalogImageLimit(
  provider: string | undefined,
  upstreamId: string,
): number | undefined {
  if (provider === undefined) return undefined;
  return catalogEntryFor(provider, upstreamId)?.maxImages;
}

/**
 * Whether the catalog records video input for a model (ModelCatalogEntry.supportsVideo), read
 * for the same reason catalogImageLimit is: it is a fact about the model being called, not a
 * preference, so it survives a Project renaming or repricing the row. A user-added row (no
 * catalog entry) answers false — the safe direction, since a video the upstream does not
 * accept fails the whole request.
 *
 * The Project entry carries the same bit (`video`, seeded from this at preset time) for the
 * call sites that only have the Session's model entry at hand; this one exists for the paths
 * that resolve a (provider, model_id) pair before a Session exists — the server, deciding
 * whether an uploaded video rides the conversation or lands in the scratchpad.
 */
export function catalogSupportsVideo(provider: string | undefined, upstreamId: string): boolean {
  if (provider === undefined) return false;
  return catalogEntryFor(provider, upstreamId)?.supportsVideo === true;
}

/**
 * The protocol a group pins on every one of its entries (ModelProviderInfo.clientType), or
 * undefined when the group pins none — an unknown id (a user-defined group) included.
 *
 * Two groups pin: vLLM, whose models are served by the user's own vLLM adapter, and
 * OpenRouter, whose models all speak the Responses API OpenRouter serves at its preset base
 * URL.
 *
 * The single entry point for the pin, so the places that decide a saved model's client_type
 * cannot drift apart: the web add-model dialog's default, moving an entry between groups,
 * the last-resort protocol on the save paths that do not probe, the env-var hint, and the
 * CLI's `config model add`. A group without a pin keeps whatever those call sites already
 * derive from its shape.
 */
export function providerClientType(providerId: string): string | undefined {
  return providerInfo(providerId)?.clientType;
}

/**
 * A first-party vendor group: one this catalog knows, that is not `custom`, carries no
 * gateway endpoint and pins no protocol of its own — DeepSeek, Google, OpenAI, Anthropic,
 * Z.AI, Moonshot, MiniMax, and the Penguin Go relay.
 *
 * What distinguishes it is that nothing in it decides a protocol: its entries persist no
 * `client_type`, so AgentHub places every one of them by the spelling of the upstream model
 * id alone. That makes the group's contents a closed set — the ids this catalog ships, plus
 * the few whose preset pins a protocol because their own id would not route — and an id
 * outside it cannot be started at all, whatever else is configured on the entry. Every
 * surface that has to answer "is this one routable?" reads the group's shape through this one
 * predicate rather than re-deriving it: the models page's cards and its config dialog, the
 * models PUT, and the CLI's `config model add`. Whether a model may be added to the group at
 * all is a separate, wider rule: isAddableGroup.
 *
 * Every other group answers the protocol question by itself and therefore routes any id the
 * endpoint serves: `custom` and user-defined groups detect or pick it, a gateway inherits its
 * preset's, and a group-level pin (OpenRouter, vLLM) hands it to every entry.
 */
export function isVendorGroup(providerId: string): boolean {
  const info = providerInfo(providerId);
  return (
    info !== undefined &&
    info.id !== "custom" &&
    info.gatewayBaseUrl === undefined &&
    info.clientType === undefined
  );
}

/**
 * Whether this entry would be written into a vendor group with an id AgentHub cannot place
 * — the configuration that produces `"<id> is not supported. Supported client types: …"` on
 * the first request, and nothing before it.
 *
 * `resolveModelEnv` is the authority: it mirrors AutoLLMClient's routing rules branch for
 * branch and returns undefined on exactly the ids that client rejects. A blank id is not a
 * routing failure — the entry is still being typed, and the required-field validation is
 * what has something to say about it.
 */
export function unroutableVendorModel(
  provider: string,
  modelId: string,
  clientType?: string,
): boolean {
  const id = modelId.trim();
  if (id === "" || !isVendorGroup(provider)) return false;
  const pinned = clientType?.trim();
  return resolveModelEnv(id, pinned === "" ? undefined : pinned) === undefined;
}

/**
 * Whether models may be added to this group by hand: `custom`, vLLM (ModelProviderInfo.addable)
 * and every user-defined group, i.e. an id this catalog does not know. Every other built-in
 * group — the first-party vendors and the gateways — carries its catalog presets and the rows
 * it already stores: the models page offers no add-model entry point there, and the models PUT
 * and the CLI's `config model add` refuse a new row that is not a preset (unaddableModel).
 */
export function isAddableGroup(providerId: string): boolean {
  const info = providerInfo(providerId);
  return info === undefined || info.addable === true;
}

/**
 * Whether writing this pair as a NEW row of its group would add a model by hand where the group
 * takes none: a group isAddableGroup refuses, and a `(provider, model_id)` pair that is not one
 * of the catalog's rows for it. A preset passes, so "Sync presets" can put a deleted one back;
 * a row the table already stores is not a new one, and the callers never ask about it.
 */
export function unaddableModel(provider: string, modelId: string): boolean {
  return !isAddableGroup(provider) && catalogEntryFor(provider, modelId.trim()) === undefined;
}

/** Env var fallback for a single model (the var names AgentHub's client actually reads when api_key / base_url is blank). */
export interface ModelEnvInfo {
  envKey: string;
  envBaseUrlKey: string;
}

/**
 * Resolves the env var fallback for a model: mirrors AgentHub's
 * AutoLLMClient routing rules - an explicit client_type takes priority; otherwise the lowercase
 * model_id is matched by the same exact or family-specific rules, returning the var pair that
 * client reads. Branch order matches AutoLLMClient.
 * Returns undefined on no match (AgentHub will reject that id: it needs an explicit
 * client_type, or should be added under custom / a self-built group via the OpenAI protocol).
 */
export function resolveModelEnv(modelId: string, clientType?: string): ModelEnvInfo | undefined {
  const explicitClientType = clientType?.toLowerCase();
  const t = explicitClientType || modelId.toLowerCase();
  const env = (prefix: string): ModelEnvInfo => ({
    envKey: `${prefix}_API_KEY`,
    envBaseUrlKey: `${prefix}_BASE_URL`,
  });
  if (t.includes("gemini-3") || t.includes("gemini-embedding")) return env("GEMINI");
  if (
    t.includes("claude") &&
    (t.includes("4-7") || t.includes("4-8") || t.includes("-5") || t.includes("4-6"))
  ) {
    return env("ANTHROPIC");
  }
  if (
    t.includes("gpt-5.4") ||
    t.includes("gpt-5.5") ||
    t.includes("gpt-5.6") ||
    t.includes("gpt-6")
  ) {
    return env("OPENAI");
  }
  // agenthub 0.4.2's unified GLM client serves the whole glm-5 series (5.3 included).
  if (t.includes("glm-5")) return env("ZAI");
  // agenthub 0.4.2's unified Kimi client serves the whole K2.5+ series; every spelling reads
  // the same MOONSHOT_* pair.
  if (t.includes("kimi-k3") || t.includes("kimi-k2.5") || t.includes("kimi-k2.6")) {
    return env("MOONSHOT");
  }
  if (t === "minimax-m3" && modelId.toLowerCase() === "minimax-m3") {
    return env("MINIMAX");
  }
  // AgentHub 0.4.11 routes DeepSeek on this substring alone, so the released V4.1 Flash id
  // `deepseek-flash` matches nothing here and stays unroutable on its own — which is exactly
  // why its catalog row pins client_type "deepseek-v4" and lands on this branch instead.
  // Mirroring AgentHub is the contract; do not widen the test to the bare name until
  // AgentHub itself does.
  if (t.includes("deepseek-v4")) return env("DEEPSEEK");
  // agenthub 0.4.2's generic Anthropic Messages protocol client reads the ANTHROPIC_* pair.
  // Order mirrors AutoLLMClient: ant-messages before the openai substring match.
  if (t.includes("ant-messages")) return env("ANTHROPIC");
  // The generic OpenAI-protocol clients — openai-chat (canonical since agenthub 0.4.2, with
  // bare "openai" as a deprecated alias), openai-responses, openai-embedding, and
  // openai-chat-vllm-adapter (an openai_chat subclass, so it reads the same pair) — all
  // read the OPENAI_* pair. AutoLLMClient matches openai-chat-vllm-adapter by exact
  // equality one branch earlier; the substring lands on the same answer, so the order costs
  // nothing here.
  if (t.includes("openai")) return env("OPENAI");
  return undefined;
}

/**
 * Resolves the credential environment for a configured model entry. Most groups follow the
 * AgentHub client selected by model id / protocol. Aggregate groups are deliberately different:
 * Penguin Go's Google and DeepSeek routes share one relay credential, and ModelScope's rows
 * share one api-inference token, so the provider-scoped variable must win over the selected
 * protocol everywhere the harness resolves a key. Whether a keyless row may use
 * that variable is modelEnvFallback's decision: ModelScope's is OPENAI_*, a vendor variable, so
 * its rows get no fallback.
 */
export function resolveProviderModelEnv(
  provider: string,
  modelId: string,
  clientType?: string,
): ModelEnvInfo | undefined {
  if (provider === PENGUIN_GO_PROVIDER_ID || provider === MODELSCOPE_PROVIDER_ID) {
    const group = providerInfo(provider);
    return group === undefined
      ? undefined
      : { envKey: group.envKey, envBaseUrlKey: group.envBaseUrlKey };
  }
  return resolveModelEnv(modelId, clientType);
}

/**
 * The endpoints AgentHub's vendor clients talk to when handed no base URL, per credential
 * variable: the OpenAI and Anthropic SDK defaults, Google's Generative Language host, the
 * defaults the vendor-specific clients (deepseek_v4, glm5_3, kimi_k3, minimax_m3) carry, plus
 * the second official host where a vendor runs two (Z.AI's mainland bigmodel.cn, Moonshot's
 * international .ai, MiniMax's mainland minimaxi.com). A key taken from the environment is
 * sent only to one of these — see modelEnvFallback.
 */
export const VENDOR_ENDPOINTS: Readonly<Record<string, readonly string[]>> = {
  OPENAI_API_KEY: ["https://api.openai.com/v1"],
  ANTHROPIC_API_KEY: ["https://api.anthropic.com"],
  GEMINI_API_KEY: ["https://generativelanguage.googleapis.com"],
  DEEPSEEK_API_KEY: [DEEPSEEK_BASE_URL],
  ZAI_API_KEY: ["https://api.z.ai/api/paas/v4", "https://open.bigmodel.cn/api/paas/v4"],
  MOONSHOT_API_KEY: ["https://api.moonshot.cn/v1", "https://api.moonshot.ai/v1"],
  MINIMAX_API_KEY: [MINIMAX_BASE_URL, "https://api.minimaxi.com/v1"],
};

/**
 * Endpoint equality for the credential rule: scheme and host compared case-insensitively
 * (with the port), the path without trailing slashes; a value that does not parse as a URL
 * matches nothing.
 */
export function sameEndpoint(a: string, b: string): boolean {
  const norm = (value: string): string | undefined => {
    try {
      const u = new URL(value.trim());
      return `${u.protocol}//${u.host}${u.pathname.replace(/\/+$/, "")}`;
    } catch {
      return undefined;
    }
  };
  const na = norm(a);
  return na !== undefined && na === norm(b);
}

/** What the credential rule reads off a model entry: the paired reference, the pinned protocol and the endpoint. */
export interface ModelCredentialShape {
  provider: string;
  modelId: string;
  /** Pinned AgentHub client type; blank or absent = auto-routed by model id. */
  clientType?: string | undefined;
  /** Inline base URL; blank or absent = the routed client's default endpoint (or its `*_BASE_URL` variable). */
  baseUrl?: string | undefined;
}

/** The environment pair a keyless entry may fall back to, and who reads it. */
export interface ModelEnvFallback extends ModelEnvInfo {
  /**
   * `true`: AgentHub's routed client reads this pair itself, so the harness hands it no key
   * and the client's own environment lookup — and its own error when the variable is unset —
   * apply unchanged. `false`: a provider-scoped pair no AgentHub client knows (the Penguin Go
   * relay's); the harness reads it and passes the value explicitly, refusing when it is unset.
   */
  readByClient: boolean;
}

/**
 * The environment fallback a keyless model entry is allowed, or `undefined` when it gets
 * none and must carry its own key.
 *
 * AgentHub's clients read a vendor variable (`OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, …)
 * whenever they are handed no key, whatever base URL they were pointed at — so a keyless row
 * in a gateway group would send the user's own OpenAI or Anthropic key to the gateway. The
 * rule here is about the **destination**, not the group's label: environment keys are for
 * official endpoints only.
 *
 * - No base URL on the entry: the routed client talks to its own default endpoint, or to the
 *   `*_BASE_URL` the user set beside the key — that pairing is AgentHub's own and is left to
 *   it entirely; the client reads the pair itself.
 * - A base URL that is one of that vendor's own official endpoints (VENDOR_ENDPOINTS; the
 *   catalog pins the DeepSeek and MiniMax rows this way) — allowed.
 * - Anything else — every gateway group's preset endpoint, custom / user-defined / vLLM rows
 *   with their own endpoints, a vendor row re-pointed at a proxy — refused. A row's own base
 *   URL equal to the `*_BASE_URL` variable's value earns no exception either (per the user:
 *   environment keys are for official endpoints only): put the key on the row.
 * - A group with a provider-scoped pair (Penguin Go, whose key no AgentHub client reads) is
 *   allowed that pair for every one of its rows, regardless of base URL; the harness reads it.
 *   A group pair that is itself a vendor variable (ModelScope records OPENAI_*) is not
 *   provider-scoped: its rows follow the destination rule like any other gateway's.
 *
 * Pure, so the server and the models page answer the same question for the preview, the
 * dialog hint and the refusal.
 */
export function modelEnvFallback(entry: ModelCredentialShape): ModelEnvFallback | undefined {
  const clientType = entry.clientType?.trim() || undefined;
  const clientPair = resolveModelEnv(entry.modelId, clientType);
  const groupPair = resolveProviderModelEnv(entry.provider, entry.modelId, clientType);
  // VENDOR_ENDPOINTS keys every variable an AgentHub client reads (each pair resolveModelEnv
  // can name), so a group pair outside it is one only the harness knows.
  if (
    groupPair !== undefined &&
    groupPair.envKey !== clientPair?.envKey &&
    !Object.hasOwn(VENDOR_ENDPOINTS, groupPair.envKey)
  ) {
    return { ...groupPair, readByClient: false };
  }
  if (clientPair === undefined) return undefined;
  const baseUrl = entry.baseUrl?.trim();
  if (!baseUrl) return { ...clientPair, readByClient: true };
  if ((VENDOR_ENDPOINTS[clientPair.envKey] ?? []).some((own) => sameEndpoint(own, baseUrl))) {
    return { ...clientPair, readByClient: true };
  }
  return undefined;
}

/**
 * The variable a keyless entry added to this group with the group's defaults falls back to,
 * or `undefined` when such an entry gets none: gateways (a preset base URL), the pinned
 * self-hosted group (vLLM), custom and user-defined groups all point away from the vendors'
 * own endpoints. The group-level key dialog's hint and the group header read this.
 */
export function providerEnvFallbackKey(providerId: string): string | undefined {
  const info = providerInfo(providerId);
  if (
    info === undefined ||
    info.id === "custom" ||
    info.gatewayBaseUrl !== undefined ||
    info.clientType !== undefined
  ) {
    return undefined;
  }
  return info.envKey;
}

/**
 * A model entry that cannot be given to an AgentHub client as configured: no key, and no
 * environment variable it may use; or a relay row with no endpoint for its relay key. The
 * message names the model and what to do, and says "API key" so hosts that classify
 * credential errors by message (the server's `isMissingCredential`) file it with the SDKs'
 * own missing-credential errors.
 */
export class ModelCredentialError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "ModelCredentialError";
  }
}

/** What a client is constructed with: an explicit key, or none when the routed client may read its own variable. */
export interface ResolvedModelCredential {
  apiKey?: string;
  baseUrl?: string;
}

/**
 * The credential an AgentHub client is built with for a **model entry**, applying
 * modelEnvFallback's rule (the one function every path shares): Session creation and resume,
 * the vision describer, the connectivity / speed / vision probes and the utility completion
 * go through here; the endpoint listing and protocol detection, which have a protocol and a
 * URL but no entry, apply the same rule through endpointEnvApiKey.
 *
 * - An inline key (the entry's, or an explicit override) is used as given.
 * - No key, fallback allowed and read by the client: no key is handed over, the client reads
 *   the pair itself — byte-for-byte what happened before this rule existed, including the
 *   SDK's own error when the variable is unset too (an entry with no base URL under a Bedrock
 *   `ANTHROPIC_BASE_URL` and no `ANTHROPIC_API_KEY` stays valid).
 * - No key, fallback allowed but provider-scoped: the variable's value is passed explicitly,
 *   and an unset variable is refused here rather than letting the client read a vendor's.
 * - No key, no fallback: refused with a ModelCredentialError before any client exists.
 * - A provider-scoped group's row (Penguin Go) is also refused without a base URL, whatever
 *   the key's source: the relay key must not travel to the vendor's default endpoint.
 */
export function resolveModelCredential(
  entry: ModelCredentialShape & { apiKey?: string | undefined },
  env: Readonly<Record<string, string | undefined>> = process.env,
): ResolvedModelCredential {
  const ref = `${entry.provider}/${entry.modelId}`;
  const baseUrl = entry.baseUrl || undefined;
  const apiKey = entry.apiKey || undefined;
  const fallback = modelEnvFallback(entry);
  if (fallback !== undefined && !fallback.readByClient && !baseUrl?.trim()) {
    throw new ModelCredentialError(
      `Model ${ref} has no base URL. Its API key belongs to the ${providerInfo(entry.provider)?.label ?? entry.provider} endpoint and cannot be sent to the vendor's default endpoint: set the base URL on the model entry.`,
    );
  }
  if (apiKey !== undefined) return { apiKey, ...(baseUrl !== undefined ? { baseUrl } : {}) };
  if (fallback === undefined) {
    // A Bedrock region on the entry is not "not the vendor's": it is AWS, whose usual
    // credential is the default provider chain rather than a key. Until AgentHub stops
    // letting its Bedrock client attach ANTHROPIC_API_KEY, a keyless row here is refused,
    // and the message says what still works.
    if (baseUrl?.trim().toLowerCase().startsWith("bedrock://")) {
      throw new ModelCredentialError(
        `Model ${ref} has no API key. A Bedrock endpoint set on the model entry cannot fall back to the environment yet: set the entry's AWS key (access,secret), or leave its base URL empty and set ANTHROPIC_BASE_URL=${baseUrl.trim()} in the server environment.`,
      );
    }
    throw new ModelCredentialError(
      `Model ${ref} has no API key. Its endpoint is not the vendor's own, so no environment variable is used for it: set the API key on the model entry.`,
    );
  }
  if (fallback.readByClient) return baseUrl !== undefined ? { baseUrl } : {};
  const value = env[fallback.envKey]?.trim();
  if (!value) {
    throw new ModelCredentialError(
      `Model ${ref} has no API key: set one on the model entry, or set ${fallback.envKey} in the server environment.`,
    );
  }
  return { apiKey: value, ...(baseUrl !== undefined ? { baseUrl } : {}) };
}

/**
 * The key the environment lends a bare endpoint spoken to on a generic protocol client —
 * the add-group listing and the protocol probes, which have a base URL and a protocol but no
 * entry yet. Same rule as modelEnvFallback: only a vendor's own endpoint gets the vendor's
 * key; a gateway or a private server gets none.
 */
export function endpointEnvApiKey(
  clientType: string,
  baseUrl: string | undefined,
  env: Readonly<Record<string, string | undefined>> = process.env,
): string | undefined {
  const fallback = modelEnvFallback({ provider: "custom", modelId: "", clientType, baseUrl });
  if (fallback === undefined) return undefined;
  return env[fallback.envKey]?.trim() || undefined;
}

/**
 * The variable the UI may present as covering a keyless entry — the masked preview on the
 * card, the dialog's "leave empty to use …" hint — or `undefined` when nothing should be
 * promised. Narrower than modelEnvFallback on purpose: a row with no base URL in a group whose
 * defaults point away from the vendor (the vLLM presets, a custom row saved without an
 * endpoint) does fall back to OPENAI_API_KEY under the rule, but presenting that as "key
 * configured" would encourage exactly the misconfiguration that sends the OpenAI key and a
 * self-hosted model id to api.openai.com. The preview therefore needs the row to name a vendor
 * endpoint itself, or to sit in a group whose defaults are the vendor's (providerEnvFallbackKey).
 * The server's `GET /models` preview and the web dialog's hint both read this, so the two
 * cannot disagree.
 */
export function modelEnvPreviewKey(entry: ModelCredentialShape): string | undefined {
  const fallback = modelEnvFallback(entry);
  if (fallback === undefined) return undefined;
  if (entry.baseUrl?.trim() || providerEnvFallbackKey(entry.provider) !== undefined) {
    return fallback.envKey;
  }
  return undefined;
}

/**
 * The wire protocol that would carry AgentHub's `fast_mode` for a model: `"openai"` for the
 * OpenAI-protocol clients (openai_chat / openai_responses / gpt6 / minimax_m3), which send
 * `service_tier: "priority"`, and `"anthropic"` for the Anthropic-protocol ones (ant_messages
 * / claude5), which send `speed: "fast"` plus the `fast-mode-2026-02-01` beta header. The two
 * differ in what the user must be warned about, not just in wire shape (see fastModeProtocol).
 */
export type FastModeProtocol = "openai" | "anthropic";

/**
 * Whether a model can carry fast mode at all, and on which protocol - `undefined` means no.
 *
 * The fast tier is a property of the **client AgentHub routes to**, never of the catalog row:
 * the registry carries no fast-tier capability flag, but the routing is deterministic, so the
 * answer is too. This mirrors AutoLLMClient's branch order exactly (the same discipline as
 * resolveModelEnv above) and reports what the selected client does with the parameter:
 *
 * - maps it -> the protocol, and the toggle may be offered;
 * - raises UnsupportedParameterError (gemini3_7, glm5_3, kimi_k3, deepseek_v4,
 *   openai_embedding, and claude5 on Bedrock or a Claude 4.6 id) -> `undefined`;
 * - routes nowhere (AutoLLMClient throws for an id it cannot place; there is no openai_chat
 *   fallback) -> `undefined` as well, since a model that cannot run has no fast tier either.
 *
 * A rule rather than a per-model list on purpose: catalog rows added later inherit the right
 * answer without anyone remembering to update a table.
 *
 * Routing reads `(clientType || modelId).toLowerCase()`, exactly as AutoLLMClient resolves it,
 * so an entry that pins no client_type self-routes on its model id - which does not always
 * agree with its provider group (`anthropic/claude-fable-5` with a blank client_type reaches
 * the native claude5 client, not openai_chat, and the dotted `anthropic/claude-opus-4.8`
 * matches no branch at all). The two claude5 carve-outs are therefore checked against the raw
 * `modelId` / `baseUrl`, not against the routing token: the client tests its own `_model` for
 * `"4-6"` and its base URL for the `bedrock://` prefix.
 *
 * `"anthropic"` is reported for every Claude the client serves, including ids outside the
 * research preview's Opus allowlist: Anthropic answers those with a 429 at request time, which
 * is something to warn about before enabling, not grounds to hide the setting.
 *
 * Two runtime inputs stay invisible to a pure function of the config and can still flip the
 * answer: the server's `CLIENT_TYPE` env var overrides the entry's client type, and
 * `ANTHROPIC_BASE_URL` supplies the base URL when the entry leaves it blank (so a `bedrock://`
 * there sends Claude to Bedrock, which has no fast tier). Third-party OpenAI-compatible
 * endpoints are a third: they accept `service_tier` and may quietly serve the standard tier.
 * That residue is why llm/generative-model.ts still handles the rejection at runtime.
 */
export function fastModeProtocol(
  modelId: string,
  clientType?: string,
  baseUrl?: string,
): FastModeProtocol | undefined {
  const t = clientType?.toLowerCase() || modelId.toLowerCase();
  // Branch order mirrors AutoLLMClient. Every test is a substring of `t` except minimax-m3,
  // which the router matches by exact equality; no trimming, matching the router.
  if (t === "minimax-m3") return "openai";
  if (t.includes("gemini-3") || t.includes("gemini-embedding")) return undefined;
  if (
    t.includes("claude") &&
    (t.includes("4-6") || t.includes("4-7") || t.includes("4-8") || t.includes("-5"))
  ) {
    // claude5 refuses fast mode on Bedrock and across the Claude 4.6 family; both tests run
    // against what the client was constructed with, not against the routing token.
    if (baseUrl?.startsWith("bedrock://")) return undefined;
    if (modelId.includes("4-6")) return undefined;
    return "anthropic";
  }
  if (
    t.includes("gpt-5.4") ||
    t.includes("gpt-5.5") ||
    t.includes("gpt-5.6") ||
    t.includes("gpt-6")
  ) {
    return "openai";
  }
  if (t.includes("glm-5")) return undefined;
  if (t.includes("kimi-k3") || t.includes("kimi-k2.5") || t.includes("kimi-k2.6")) return undefined;
  if (t.includes("deepseek-v4")) return undefined;
  if (t.includes("ant-messages")) return "anthropic";
  // openai-chat-vllm-adapter is not carved out: it subclasses openai_chat without touching
  // fast mode, so it maps the parameter exactly as the substring branch below reports. What
  // a self-hosted server then does with `service_tier` is the third-party residue named
  // above.
  if (t.includes("openai-responses")) return "openai";
  if (t.includes("openai") && t.includes("embedding")) return undefined;
  if (t.includes("openai")) return "openai";
  return undefined;
}

/**
 * Catalog -> preset ModelEntry list (shared by defaultProjectConfig and the server's initial
 * config, avoiding duplicate hand-written copies). `provider` and `model_id` are persisted as
 * separate fields (`model_id` is the plain upstream id); models whose upstream id can be
 * auto-routed by AgentHub leave client_type unset; gateway models (OpenRouter / SiliconFlow)
 * always pin a client_type — openai-responses for the OpenRouter rows, each OpenCode Go row's
 * own endpoint protocol, openai-chat for the rest — and inline a preset base_url. The direct
 * MiniMax M3 entry also pins its protocol and endpoint. No secrets are included, so only an
 * API key is needed.
 *
 * Pricing is written as the LIST price the catalog records (a scheduled row's PEAK price),
 * never a discounted number. What is on disk then stays true whatever promotion is live and
 * whatever hour the Project is created or re-synced in: a flat promotion is stored beside the
 * file as a per-Project fraction (presetPromotions), and both it and an off-peak tier are
 * applied when the price is read, by the models page and by the cost center alike.
 */
export function presetModelEntries(): ModelEntry[] {
  // A retired row stays in the catalog for the Projects that still carry it, and only for them.
  return MODEL_CATALOG.filter((m) => m.retired !== true).map(catalogModelEntry);
}

/**
 * Every catalog row as a ModelEntry, retired rows included, in the shape presetModelEntries
 * writes. This is the list "Sync presets" compares a Project's table against: the sync never adds
 * a retired row, but it updates one the Project already carries like any preset, so a price an
 * older release stored returns to the catalog's (see ModelCatalogEntry.retired).
 */
export function catalogModelEntries(): ModelEntry[] {
  return MODEL_CATALOG.map(catalogModelEntry);
}

/** One catalog row as the ModelEntry a Project stores (see presetModelEntries). */
function catalogModelEntry(m: ModelCatalogEntry): ModelEntry {
  return {
    provider: m.provider,
    model_id: m.modelId,
    ...(m.contextWindow !== undefined ? { context_window: m.contextWindow } : {}),
    ...(m.clientType !== undefined ? { client_type: m.clientType } : {}),
    ...(m.pricing ? { pricing: { ...m.pricing } } : {}),
    // ModelEntry.vision defaults to supported: only models that don't support images
    // explicitly persist false (drives read_file's hand-off of images to the vision model and input
    // image hand-off, see project-config.ts). Video is the other way round: only a row whose
    // vendor documents video input persists true, because a video sent to a model that does
    // not take one fails the whole request.
    ...(m.supportsVision ? {} : { vision: false }),
    ...(m.supportsVideo ? { video: true } : {}),
    ...(m.baseUrl !== undefined ? { base_url: m.baseUrl } : {}),
  };
}

/**
 * The catalog's flat promotions, as a new Project is seeded with them: every row whose `discount`
 * is a fraction in (0, 1). A retired row is skipped like it is in presetModelEntries: a new
 * Project never carries it, so there is nothing for its promotion to apply to.
 */
export function presetPromotions(): Array<{ provider: string; modelId: string; discount: number }> {
  return MODEL_CATALOG.flatMap((m) =>
    m.retired !== true && m.discount !== undefined && m.discount > 0 && m.discount < 1
      ? [{ provider: m.provider, modelId: m.modelId, discount: m.discount }]
      : [],
  );
}

/**
 * The model's own homepage/detail page for the frontend's model-card link. Gateway groups
 * have a stable per-model URL pattern (works for user-added ids in those groups too);
 * direct-vendor models link to the vendor's model list/docs page; custom and user-defined
 * groups have no page to vouch for.
 */
export function modelHomepageUrl(provider: string, modelId: string): string | undefined {
  if (provider === "openrouter") return `https://openrouter.ai/${modelId}`;
  if (provider === "qwen-token-plan") {
    return `https://www.qianwenai.com/models/${modelId}`;
  }
  if (provider === "fireworks") {
    // API id "accounts/<owner>/models/<slug>" -> page "app.fireworks.ai/models/<owner>/<slug>";
    // nonconforming (user-added) ids fall back to the models listing.
    const m = /^accounts\/([^/]+)\/models\/(.+)$/.exec(modelId);
    return m
      ? `https://app.fireworks.ai/models/${m[1]}/${m[2]}`
      : providerInfo(provider)?.modelsUrl;
  }
  if (provider === "tokendance") return `https://tokendance.space/models/${modelId}`;
  if (provider === "qwen-pay-as-you-go") {
    return `https://www.qianwenai.com/models/${encodeURIComponent(modelId)}`;
  }
  if (provider === "zhipu") {
    // Z.AI's per-model guide pages use the bare model id as the slug.
    return `https://docs.z.ai/guides/llm/${modelId}`;
  }
  if (provider === "moonshot") {
    // Moonshot's pricing pages: kimi-k2.6 -> chat-k26 (dot dropped); other ids fall back.
    const m = /^kimi-k(\d+)\.(\d+)$/.exec(modelId);
    return m
      ? `https://platform.kimi.com/docs/pricing/chat-k${m[1]}${m[2]}`
      : providerInfo(provider)?.modelsUrl;
  }
  if (provider === "vllm") {
    // recipes.vllm.ai has a page per model vLLM published a recipe for, which is exactly what
    // this group presets; an id the user serves themselves has no page, so it gets the index.
    return catalogEntryFor(provider, modelId) !== undefined
      ? `https://recipes.vllm.ai/${modelId}`
      : providerInfo(provider)?.modelsUrl;
  }
  if (provider === "custom") return undefined;
  return providerInfo(provider)?.modelsUrl;
}

/**
 * App attribution: how the harness identifies itself to gateways that rank or report the apps
 * calling them. Both values describe Adelie itself, never a model or an account.
 *
 * `APP_URL` is also the `app_url` a provider OAuth flow stamps onto the key it mints, which is
 * why it is exported: a stable app URL is required there, and a second copy would let the two
 * attributions drift apart.
 */
export const APP_URL = "https://penguin.ooo/";
const APP_TITLE = "Adelie";
/**
 * OpenRouter marketplace categories, comma-separated. OpenRouter accepts at most **two per
 * request** from a fixed slug list and silently drops anything else, so this string is
 * exactly two recognised slugs.
 */
const OPENROUTER_CATEGORIES = "cli-agent,personal-agent";

/**
 * Lowercase host of a base URL; undefined when it is blank or unparseable. A fully-qualified
 * trailing dot is stripped: `URL` keeps it in `hostname`, but `openrouter.ai.` names the same
 * server as `openrouter.ai` and has to match the same way. Stripping cannot widen the match —
 * a suffix-anchored comparison rejects `openrouter.ai.attacker.com` with or without the dot.
 */
function endpointHost(baseUrl: string | undefined): string | undefined {
  if (!baseUrl?.trim()) return undefined;
  try {
    return new URL(baseUrl).hostname.toLowerCase().replace(/\.$/, "");
  } catch {
    return undefined;
  }
}

/** Host equality extended to subdomains; suffix-anchored, so `notopenrouter.ai` never matches. */
function hostMatches(host: string, domain: string): boolean {
  return host === domain || host.endsWith(`.${domain}`);
}

/**
 * Attribution headers for a request's base URL, or undefined when that endpoint runs no
 * attribution scheme (every direct vendor, and every gateway that does not read one).
 *
 * Keyed on the endpoint host rather than on the catalog's provider group, because the group
 * is a display bucket while the headers are a property of the server being called: a `custom`
 * entry pointed at OpenRouter is still Adelie talking to OpenRouter and is attributed
 * identically. The flip side is that an entry carrying no `base_url` of its own gets no
 * headers even when `OPENAI_BASE_URL` sends it to a gateway — that variable is read inside
 * AgentHub and never reaches this side.
 *
 * - OpenRouter (https://openrouter.ai/docs/app-attribution): `HTTP-Referer` is the identity
 *   that creates the app page and drives the rankings, `X-OpenRouter-Title` is its display
 *   name, `X-OpenRouter-Categories` files it under marketplace categories.
 * - TokenDance (https://tokendance.space/docs/app-attribution): `X-App-URL` alone, and it
 *   takes priority over any App URL recorded on the API key — the same key may be in use by
 *   other tools, so the per-request value is the accurate one.
 * - OpenCode (https://opencode.ai): `x-opencode-session` alone, and it names the conversation
 *   rather than the app — the gateway keys its routing and prompt caching on it, so the value
 *   has to hold still across a conversation's requests and differ between conversations. It
 *   is **always** sent: the gateway refuses a request that names none (400 "Request is missing
 *   x-opencode-session"). A Session's requests carry the Session's id; a request with none (a
 *   connectivity test, a vision probe) gets a fresh random id, making it a conversation of its
 *   own — never a shared constant, which would file every such request under one session. A
 *   caller that sends several requests without a Session computes the headers once and
 *   reuses them (GenerativeModel does, per instance), so they share that one id.
 */
export function attributionHeaders(
  baseUrl: string | undefined,
  sessionId?: string,
): Record<string, string> | undefined {
  const host = endpointHost(baseUrl);
  if (!host) return undefined;
  if (hostMatches(host, "openrouter.ai")) {
    return {
      "HTTP-Referer": APP_URL,
      "X-OpenRouter-Title": APP_TITLE,
      "X-OpenRouter-Categories": OPENROUTER_CATEGORIES,
    };
  }
  if (hostMatches(host, "tokendance.space")) return { "X-App-URL": APP_URL };
  if (hostMatches(host, "opencode.ai")) {
    // The global Web Crypto rather than node:crypto: this module is bundled into the web app too.
    return { "x-opencode-session": sessionId || globalThis.crypto.randomUUID() };
  }
  return undefined;
}