1use serde::Deserialize;
14use std::collections::BTreeMap;
15
16const RAW: &str = cmduse_core::ZEN_JSON;
19
20pub const DEFAULT_PLAN: &str = "go";
22
23#[derive(Debug, Clone, Deserialize)]
24pub struct RateVariant {
25 pub peak: Option<String>,
26 #[serde(rename = "tierMaxTokens")]
27 pub tier_max_tokens: Option<u64>,
28 #[serde(rename = "tierMinTokens")]
29 pub tier_min_tokens: Option<u64>,
30 pub input: f64,
31 pub output: f64,
32 #[serde(rename = "cacheRead")]
33 pub cache_read: f64,
34 #[serde(rename = "cacheWrite")]
35 pub cache_write: f64,
36}
37
38#[derive(Debug, Clone, Deserialize)]
39pub struct Model {
40 pub id: String,
41 pub name: String,
42 pub variants: Vec<RateVariant>,
43}
44
45#[derive(Debug, Clone, Deserialize)]
46pub struct WindowShare {
47 #[serde(rename = "fiveHour")]
48 pub five_hour: f64,
49 pub weekly: f64,
50 pub monthly: f64,
51}
52
53#[derive(Debug, Clone, Deserialize)]
55pub struct GoPlan {
56 pub label: String,
57 #[serde(rename = "priceUsd")]
58 pub price_usd: f64,
59 #[serde(rename = "allowanceByModel")]
61 pub allowance_by_model: BTreeMap<String, f64>,
62}
63
64#[derive(Debug, Clone, Deserialize)]
65pub struct Go {
66 #[serde(rename = "windowShare")]
67 pub window_share: WindowShare,
68 pub models: Vec<Model>,
70 pub plans: BTreeMap<String, GoPlan>,
72}
73
74#[derive(Debug, Clone, Deserialize)]
75pub struct Zen {
76 pub models: Vec<Model>,
77}
78
79#[derive(Debug, Clone, Deserialize)]
80pub struct Catalog {
81 #[serde(rename = "extractedAt")]
82 pub extracted_at: String,
83 pub go: Go,
84 pub zen: Zen,
85}
86
87pub fn catalog() -> Catalog {
89 serde_json::from_str(RAW)
90 .expect("core/zen.json is generated; regenerate with scripts/extract-zen.ts")
91}
92
93impl Catalog {
94 pub fn plan_ids(&self) -> Vec<&str> {
97 self.go.plans.keys().map(String::as_str).collect()
98 }
99
100 pub fn plan(&self, plan_id: &str) -> Option<&GoPlan> {
101 self.go.plans.get(plan_id)
102 }
103
104 pub fn go_limit(&self, plan_id: &str, model_id: &str) -> Option<f64> {
107 self.plan(plan_id)
108 .and_then(|plan| plan.allowance_by_model.get(model_id).copied())
109 }
110}
111
112pub fn window_cap(limit: f64, share: f64) -> f64 {
114 limit * share
115}
116
117pub fn is_peak(at_ms: i64) -> bool {
126 let secs = at_ms.div_euclid(1000);
127 let weekday = (secs.div_euclid(86_400) + 4).rem_euclid(7); if weekday >= 5 {
129 return false;
130 }
131 let hour = secs.rem_euclid(86_400) / 3600;
132 (1..4).contains(&hour) || (6..10).contains(&hour)
133}
134
135impl Catalog {
136 #[allow(clippy::too_many_arguments)]
142 pub fn price(
143 &self,
144 model_id: &str,
145 at_ms: i64,
146 input: u64,
147 output: u64,
148 reasoning: u64,
149 cache_read: u64,
150 cache_write: u64,
151 ) -> Option<f64> {
152 let model = self.go.models.iter().find(|m| m.id == model_id)?;
153 let peak = model
154 .variants
155 .iter()
156 .find(|v| v.peak.as_deref() == Some("peak"));
157 let off_peak = model
158 .variants
159 .iter()
160 .find(|v| v.peak.as_deref() == Some("off-peak"));
161 let rates = match (peak, off_peak) {
162 (Some(peak), Some(_)) if is_peak(at_ms) => peak,
163 (Some(_), Some(off)) => off,
164 _ => model.variants.first()?,
166 };
167 let cost = input as f64 * rates.input
168 + (output + reasoning) as f64 * rates.output
169 + cache_read as f64 * rates.cache_read
170 + cache_write as f64 * rates.cache_write;
171 (cost > 0.0).then_some(cost / 1e6)
172 }
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178
179 #[test]
180 fn catalogue_parses_and_carries_both_plans() {
181 let c = catalog();
182 assert!(c.go.models.len() > 20, "go models: {}", c.go.models.len());
183 assert!(!c.zen.models.is_empty());
184 assert_eq!(c.plan_ids(), vec!["go", "go-plus"]);
185 assert_eq!(c.plan("go").map(|p| p.price_usd), Some(10.0));
186 assert_eq!(c.plan("go-plus").map(|p| p.price_usd), Some(40.0));
187 assert_eq!(c.go_limit("go", "glm-5.3-flash"), Some(60.0));
188 assert_eq!(c.go_limit("go-plus", "glm-5.3-flash"), Some(180.0));
189 assert_eq!(c.go_limit("go", "nope-not-a-model"), None);
190 assert_eq!(c.go_limit("nope-not-a-plan", "glm-5.3-flash"), None);
191 }
192
193 #[test]
194 fn plans_share_the_model_set_and_differ_on_allowance() {
195 let c = catalog();
196 let go = c.plan("go").expect("go");
197 let plus = c.plan("go-plus").expect("go-plus");
198 assert_eq!(go.label, "Go");
199 assert_eq!(plus.label, "Go Plus");
200 assert_eq!(go.allowance_by_model.len(), plus.allowance_by_model.len());
201 assert!(plus
204 .allowance_by_model
205 .iter()
206 .all(|(id, _)| go.allowance_by_model.contains_key(id)));
207 assert!(plus
208 .allowance_by_model
209 .iter()
210 .any(|(id, limit)| go.allowance_by_model.get(id).is_some_and(|g| limit > g)));
211 }
212
213 #[test]
214 fn window_split_matches_the_docs() {
215 let c = catalog();
216 assert_eq!(c.go.window_share.five_hour, 0.2);
217 assert_eq!(c.go.window_share.weekly, 0.5);
218 assert_eq!(c.go.window_share.monthly, 1.0);
219 assert_eq!(window_cap(60.0, c.go.window_share.five_hour), 12.0);
220 }
221
222 #[test]
223 fn tiered_and_peak_variants_are_kept() {
224 let c = catalog();
225 let qwen =
226 c.go.models
227 .iter()
228 .find(|m| m.id == "qwen3.7-plus")
229 .expect("qwen3.7-plus");
230 assert_eq!(qwen.variants.len(), 2);
231 assert!(qwen
232 .variants
233 .iter()
234 .any(|v| v.tier_max_tokens == Some(256_000)));
235 let deepseek =
236 c.go.models
237 .iter()
238 .find(|m| m.id == "deepseek-v4-flash")
239 .expect("deepseek");
240 assert!(deepseek
241 .variants
242 .iter()
243 .any(|v| v.peak.as_deref() == Some("peak")));
244 }
245}