返回 CodeWhale
offering.rs
根目录 / crates / config / src / route / offering.rs
1 //! Provider model offerings (#3084).
2 //!
3 //! A [`ProviderModelOffering`] binds a provider to a canonical model, the
4 //! provider-owned wire id that serves it, and the endpoint key. This is the
5 //! seam that proves the #2608 invariant: the SAME canonical model can be served
6 //! by multiple providers under DIFFERENT wire ids (some aggregator-prefixed),
7 //! and a prefix never implies provider ownership.
8 //!
9 //! Catalog-derived offerings from [`crate::catalog::bundled_catalog_offerings`]
10 //! remain the general bundled source of truth. [`bundled_offerings`] contains
11 //! only transport facts that Models.dev cannot express, such as a single
12 //! provider routing different models over different wire protocols.
13
14 use serde::{Deserialize, Serialize};
15
16 use super::candidate::PricingSku;
17 use super::capabilities::RouteCapabilities;
18 use super::ids::{ModelId, ProviderId, WireModelId};
19
20 /// Token limits for one resolved route/offering.
21 ///
22 /// These are optional because hosted catalogs, local runtimes, and custom
23 /// endpoints can legitimately omit some or all limit facts. Callers should
24 /// treat `None` as unknown, not zero.
25 #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
26 pub struct RouteLimits {
27 /// Total context window (input + output), in tokens.
28 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub context_tokens: Option<u64>,
30 /// Input-token limit, when the provider reports it separately.
31 #[serde(default, skip_serializing_if = "Option::is_none")]
32 pub input_tokens: Option<u64>,
33 /// Output-token cap for the route/offering, when known.
34 #[serde(default, skip_serializing_if = "Option::is_none")]
35 pub output_tokens: Option<u64>,
36 #[serde(skip)]
37 pub output_declared: bool,
38 }
39
40 impl RouteLimits {
41 /// Whether at least one limit fact is known.
42 #[must_use]
43 pub const fn has_known_limit(self) -> bool {
44 self.context_tokens.is_some() || self.input_tokens.is_some() || self.output_tokens.is_some()
45 }
46 }
47
48 /// One provider's way of serving a (possibly canonical) model.
49 ///
50 /// `Eq` is intentionally NOT derived: [`PricingSku::Token`] carries `f64` rates,
51 /// so the offering is only `PartialEq`. No caller keys a set/map on offerings.
52 #[derive(Debug, Clone, PartialEq)]
53 pub struct ProviderModelOffering {
54 /// Provider serving this offering.
55 pub provider: ProviderId,
56 /// Canonical model identity, if this offering maps to one.
57 pub canonical_model: Option<ModelId>,
58 /// Provider-owned wire id sent on the request (verbatim).
59 pub wire_model_id: WireModelId,
60 /// Endpoint key the offering is served on.
61 pub endpoint_key: String,
62 /// Whether this is the provider's default offering.
63 pub default_for_provider: bool,
64 /// Provider/offering-scoped token limits, when known.
65 pub limits: RouteLimits,
66 /// Provider/model-scoped capability facts. Unknown is preserved rather
67 /// than inferred from the wire protocol.
68 pub capabilities: RouteCapabilities,
69 /// Coarse route-facing pricing meter for this offering (#3085).
70 ///
71 /// Projected from the offering's sourced cost at the layer that owns it
72 /// (`CatalogOffering::to_offering` → [`crate::pricing::route_pricing_sku`]).
73 /// The resolver carries this verbatim onto the candidate; it is
74 /// [`PricingSku::UnknownOrStale`] whenever no price was sourced — never a
75 /// fabricated zero (the #2608 / #3085 honesty rule).
76 pub pricing: PricingSku,
77 }
78
79 /// Endpoint key for a Zen catalog row Models.dev marks `deprecated`. It names
80 /// no protocol, so the resolver refuses the model locally with this reason
81 /// instead of sending a request Zen no longer serves.
82 pub const OPENCODE_ZEN_DEPRECATED_ENDPOINT_KEY: &str = "deprecated";
83
84 /// Endpoint key OpenCode Zen serves a model on, from the AI SDK package
85 /// OpenCode's own catalog names for it.
86 ///
87 /// Models.dev's `opencode` provider is Zen's published catalog: its provider
88 /// default is `@ai-sdk/openai-compatible`, and a model served over another
89 /// wire overrides that with `provider.npm`. The package is the wire fact, so it
90 /// is mapped exactly and never guessed from a model-id family, which does not
91 /// hold on Zen (qwen3.8-flash is Messages while qwen3.8-max is Chat). Google's
92 /// package maps to `"google"` and any other to `"unproven"`; neither resolves
93 /// to a protocol, so the resolver fails closed and names the endpoint.
94 #[must_use]
95 pub fn opencode_zen_endpoint_key_for_npm(npm: Option<&str>) -> &'static str {
96 match npm.map(str::trim) {
97 Some("@ai-sdk/openai") => "responses",
98 Some("@ai-sdk/anthropic") => "messages",
99 Some("@ai-sdk/openai-compatible") => "chat",
100 Some("@ai-sdk/google") => "google",
101 _ => "unproven",
102 }
103 }
104
105 /// Logical default plus every documented Zen wire id, for picker fallbacks
106 /// when Models.dev is stale or failed. `gpt-5.6` is the user-facing default;
107 /// `gpt-5.6-sol` is the proven Responses wire id.
108 #[must_use]
109 pub fn opencode_zen_picker_models() -> Vec<&'static str> {
110 crate::catalog::reviewed::constants::completion_names("opencode-zen").to_vec()
111 }
112
113 /// Codewhale API bootstrap rows used only when the account's live
114 /// `GET {base}/models` cannot be fetched.
115 ///
116 /// The account catalog is authoritative: it lists exactly the providers the
117 /// customer connected, and each row states its own protocol. These three rows
118 /// exist so a route can still be selected offline; every consumer that shows
119 /// models must say the list is a fallback, not the account's catalog.
120 pub use crate::catalog::reviewed::constants::CODEWHALE_FALLBACK_MODELS;
121
122 /// Endpoint key for one Codewhale API model id.
123 ///
124 /// The live catalog states the protocol per model in `codewhale.protocol`;
125 /// this is the offline inference used for the bootstrap rows and for a model
126 /// id the local catalog has never seen. Only the `anthropic/` namespace routes
127 /// to `{base}/messages`; everything else is OpenAI Chat Completions at
128 /// `{base}/chat/completions`. The id alone carries no signal for the
129 /// Responses surface — a `responses` row only ever comes from the catalog's
130 /// stated `codewhale.protocol`, never from a model name.
131 #[must_use]
132 pub fn codewhale_endpoint_key_for_model(model: &str) -> &'static str {
133 if model.trim().to_ascii_lowercase().starts_with("anthropic/") {
134 "messages"
135 } else {
136 "chat"
137 }
138 }
139
140 /// Return curated provider/model transport facts as owned offering rows.
141 ///
142 /// OpenCode Zen's official catalog serves models over three protocol families.
143 /// These rows intentionally carry no inferred limits, pricing, or canonical
144 /// identity: their sole claim is the documented wire model and endpoint key.
145 #[must_use]
146 pub fn bundled_offerings() -> Vec<ProviderModelOffering> {
147 crate::catalog::reviewed::bundled_reviewed()
148 .transports
149 .iter()
150 .map(crate::catalog::reviewed::ReviewedTransport::to_offering)
151 .collect()
152 }
153
153 lines RUST