1pub const DEFAULT_CONTEXT_WINDOW: u64 = 128_000;
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub struct ModelLimit {
24 pub context: u64,
26 pub output: u64,
28}
29
30pub const DEFAULT_RESERVE_CAP: u64 = 32_768;
34
35impl ModelLimit {
36 pub fn reserve(&self) -> u64 {
38 self.output.min(DEFAULT_RESERVE_CAP)
39 }
40
41 pub fn trigger(&self) -> u64 {
44 self.context.saturating_sub(self.reserve())
45 }
46
47 pub fn is_full(&self, input_tokens: u64) -> bool {
50 input_tokens >= self.trigger()
51 }
52}
53
54pub fn is_opencode_go(base_url: Option<&str>) -> bool {
56 base_url.is_some_and(|u| u.to_ascii_lowercase().contains("opencode.ai/zen/go"))
57}
58
59pub fn model_limit(base_url: Option<&str>, model: &str) -> Option<ModelLimit> {
62 if is_opencode_go(base_url) {
63 return opencode_go(model);
64 }
65 None
66}
67
68fn opencode_go(model: &str) -> Option<ModelLimit> {
69 OPENCODE_GO
70 .iter()
71 .find(|(id, ..)| *id == model)
72 .map(|(_, context, output)| ModelLimit {
73 context: *context,
74 output: *output,
75 })
76}
77
78static OPENCODE_GO: &[(&str, u64, u64)] = &[
81 ("deepseek-v4-flash", 1_000_000, 384_000),
82 ("deepseek-v4-flash-vision-exp", 1_000_000, 384_000),
83 ("deepseek-v4-pro", 1_000_000, 384_000),
84 ("deepseek-v4.1-flash", 1_000_000, 384_000),
85 ("glm-5", 202_752, 32_768),
86 ("glm-5.1", 202_752, 32_768),
87 ("glm-5.2", 1_000_000, 131_072),
88 ("glm-5.3", 1_000_000, 131_072),
89 ("glm-5.3-flash", 1_000_000, 131_072),
90 ("gpt-5.6-luna", 1_050_000, 128_000),
91 ("grok-4.5", 500_000, 500_000),
92 ("grok-4.6", 500_000, 500_000),
93 ("hy3", 256_000, 128_000),
94 ("hy4-preview", 1_024_000, 64_000),
95 ("kimi-k2.5", 262_144, 65_536),
96 ("kimi-k2.6", 262_144, 65_536),
97 ("kimi-k2.7-code", 262_144, 262_144),
98 ("kimi-k3", 1_048_576, 131_072),
99 ("longcat-2.0", 1_000_000, 131_072),
100 ("mimo-v2-omni", 262_144, 128_000),
101 ("mimo-v2-pro", 1_048_576, 128_000),
102 ("mimo-v2.5", 1_000_000, 128_000),
103 ("mimo-v2.5-pro", 1_048_576, 128_000),
104 ("minimax-m2.5", 204_800, 65_536),
105 ("minimax-m2.7", 204_800, 131_072),
106 ("minimax-m3", 1_000_000, 131_072),
107 ("muse-spark-1.2-contributor", 1_048_576, 131_072),
108 ("muse-spark-1.3-contributor", 1_048_576, 131_072),
109 ("omen-alpha", 500_000, 128_000),
110 ("ox-alpha-free", 1_000_000, 131_072),
111 ("qwen3.5-plus", 262_144, 65_536),
112 ("qwen3.6-plus", 1_000_000, 65_536),
113 ("qwen3.7-max", 1_000_000, 65_536),
114 ("qwen3.7-plus", 1_000_000, 65_536),
115 ("qwen3.8-flash", 1_000_000, 131_072),
116 ("qwen3.8-max", 1_000_000, 131_072),
117];
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 const ZEN: Option<&str> = Some("https://opencode.ai/zen/go/v1");
124
125 #[test]
126 fn recognizes_opencode_go_endpoint() {
127 assert!(is_opencode_go(ZEN));
128 assert!(is_opencode_go(Some("https://opencode.ai/zen/go/v1/")));
129 assert!(is_opencode_go(Some("HTTPS://OPENCODE.AI/ZEN/GO/V1")));
130 assert!(!is_opencode_go(Some("https://api.openai.com/v1")));
131 assert!(!is_opencode_go(Some("https://opencode.ai/zen/v1")));
132 assert!(!is_opencode_go(None));
133 }
134
135 #[test]
136 fn looks_up_configured_model() {
137 let l = model_limit(ZEN, "deepseek-v4.1-flash").expect("known opencode model");
138 assert_eq!(l.context, 1_000_000);
139 assert_eq!(l.output, 384_000);
140 }
141
142 #[test]
143 fn unknown_model_or_provider_is_none() {
144 assert_eq!(model_limit(ZEN, "no-such-model"), None);
145 assert_eq!(model_limit(Some("https://api.openai.com/v1"), "kimi-k3"), None);
147 assert_eq!(model_limit(None, "kimi-k3"), None);
148 }
149
150 #[test]
151 fn reserve_is_capped_and_trigger_and_is_full_agree() {
152 let big = ModelLimit { context: 1_000_000, output: 384_000 };
154 assert_eq!(big.reserve(), DEFAULT_RESERVE_CAP);
155 assert_eq!(big.trigger(), 1_000_000 - DEFAULT_RESERVE_CAP);
156 assert!(!big.is_full(big.trigger() - 1));
157 assert!(big.is_full(big.trigger()));
158
159 let small = ModelLimit { context: 8_192, output: 4_096 };
161 assert_eq!(small.reserve(), 4_096);
162 assert_eq!(small.trigger(), 4_096);
163 assert!(small.is_full(4_096));
164 }
165}