返回 CodeWhale
capabilities.rs
根目录 / crates / config / src / route / capabilities.rs
1 //! Route-scoped capability facts.
2 //!
3 //! Capability state is deliberately three-valued: an absent catalog fact is
4 //! unknown, not unsupported, and must never be promoted to supported by a
5 //! transport/protocol heuristic. These values travel with the exact provider
6 //! offering selected by [`super::resolver::RouteResolver`].
7
8 use serde::{Deserialize, Serialize};
9
10 use crate::ProviderKind;
11
12 /// Whether a resolved provider/model offering supports one capability.
13 #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
14 #[serde(rename_all = "snake_case")]
15 pub enum CapabilityState {
16 /// The selected offering explicitly reports support.
17 Supported,
18 /// The selected offering explicitly reports no support.
19 Unsupported,
20 /// The selected offering did not state the fact.
21 #[default]
22 Unknown,
23 }
24
25 impl CapabilityState {
26 /// Preserve a sourced optional boolean as a three-state fact.
27 #[must_use]
28 pub const fn from_optional_bool(value: Option<bool>) -> Self {
29 match value {
30 Some(true) => Self::Supported,
31 Some(false) => Self::Unsupported,
32 None => Self::Unknown,
33 }
34 }
35
36 /// Whether the source explicitly reports support.
37 #[must_use]
38 pub const fn is_supported(self) -> bool {
39 matches!(self, Self::Supported)
40 }
41 }
42
43 /// Return the documented server-side web-search fact for one exact direct
44 /// provider/model offering.
45 ///
46 /// This is intentionally a small sourced table, not a protocol or model-family
47 /// heuristic. Aggregators, custom endpoints, aliases, snapshots, and nearby
48 /// model names remain [`CapabilityState::Unknown`] until a provider-owned fact
49 /// exists for that exact offering.
50 ///
51 /// Sources:
52 /// - OpenAI Responses web search: <https://developers.openai.com/api/docs/guides/tools-web-search>
53 /// - Anthropic web search tool: <https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-search-tool>
54 /// - xAI web search tool: <https://docs.x.ai/developers/tools/web-search>
55 /// - Xiaomi MiMo web search: <https://mimo.mi.com/docs/en-US/usage-guide/tool-calling/web-search>
56 /// - Z.AI Web Search API: <https://docs.z.ai/api-reference/tools/web-search>
57 /// - Zhipu Web Search API: <https://docs.bigmodel.cn/api-reference/工具-api/网络搜索>
58 /// - Alibaba Model Studio Token Plan Harness tools: <https://help.aliyun.com/en/model-studio/token-plan-harness-tool>
59 /// - DeepSeek Responses web search: <https://api-docs.deepseek.com/api/create-response/>
60 /// - Kimi built-in and Formula web search: <https://platform.kimi.ai/docs/guide/use-web-search>
61 #[must_use]
62 pub(crate) fn documented_server_side_web_search(
63 provider_id: &str,
64 wire_model_id: &str,
65 ) -> CapabilityState {
66 let provider_id = provider_id.trim().to_ascii_lowercase();
67 let wire_model_id = wire_model_id.trim().to_ascii_lowercase();
68 if crate::catalog::reviewed::bundled_reviewed()
69 .search_models
70 .get(&provider_id)
71 .is_some_and(|models| models.contains(&wire_model_id))
72 {
73 CapabilityState::Supported
74 } else {
75 CapabilityState::Unknown
76 }
77 }
78
79 /// Return the Z.AI/Zhipu search fact only for the two exact general API
80 /// products that expose the structured `/web_search` endpoint.
81 #[must_use]
82 pub(crate) fn documented_zai_web_search_for_route(
83 provider: ProviderKind,
84 wire_model_id: &str,
85 base_url: &str,
86 ) -> CapabilityState {
87 if provider != ProviderKind::Zai {
88 return CapabilityState::Unknown;
89 }
90 let normalized = base_url.trim().trim_end_matches('/').to_ascii_lowercase();
91 if !matches!(
92 normalized.as_str(),
93 "https://api.z.ai/api/paas/v4" | "https://open.bigmodel.cn/api/paas/v4"
94 ) {
95 return CapabilityState::Unknown;
96 }
97 documented_server_side_web_search("zai", wire_model_id)
98 }
99
100 /// Return the native-search fact for exact Moonshot direct and Kimi Code
101 /// product routes. Adjacent coding paths and cross-product model ids remain
102 /// unknown even though they share one provider identity.
103 #[must_use]
104 pub(crate) fn documented_moonshot_web_search_for_route(
105 provider: ProviderKind,
106 wire_model_id: &str,
107 base_url: &str,
108 ) -> CapabilityState {
109 if provider != ProviderKind::Moonshot {
110 return CapabilityState::Unknown;
111 }
112 let model = wire_model_id.trim().to_ascii_lowercase();
113 if crate::provider::is_exact_kimi_code_route(provider, base_url)
114 && crate::catalog::reviewed::route_model_set_contains("kimi_membership_search", &model)
115 {
116 return CapabilityState::Supported;
117 }
118 if crate::provider::is_exact_moonshot_platform_route(provider, base_url) {
119 return documented_server_side_web_search("moonshot", &model);
120 }
121 CapabilityState::Unknown
122 }
123
124 #[must_use]
125 pub(crate) fn documented_deepseek_web_search_for_route(
126 provider: ProviderKind,
127 wire_model_id: &str,
128 base_url: &str,
129 ) -> CapabilityState {
130 if !is_official_deepseek_route(provider, base_url) {
131 return CapabilityState::Unknown;
132 }
133 documented_server_side_web_search("deepseek", wire_model_id)
134 }
135
136 /// Return the provider Files API fact for exact DeepSeek direct offerings.
137 ///
138 /// DeepSeek stores one uploaded image per account (`purpose=user_data`) and
139 /// both Codewhale DeepSeek wire dialects can reference the returned
140 /// `file-api-…` id, but only on the exact official hosts: a custom
141 /// DeepSeek-compatible base URL, an aggregator row, or a neighboring model id
142 /// stays [`CapabilityState::Unknown`].
143 ///
144 /// Source: <https://api-docs.deepseek.com/guides/files_api> (verified 2026-09-17)
145 #[must_use]
146 pub(crate) fn documented_deepseek_files_api_for_route(
147 provider: ProviderKind,
148 wire_model_id: &str,
149 base_url: &str,
150 ) -> CapabilityState {
151 if !is_official_deepseek_route(provider, base_url) {
152 return CapabilityState::Unknown;
153 }
154 let model = wire_model_id.trim().to_ascii_lowercase();
155 if crate::catalog::reviewed::route_model_set_contains("deepseek_files", &model) {
156 CapabilityState::Supported
157 } else {
158 CapabilityState::Unknown
159 }
160 }
161
162 /// Return the image-input fact for exact DeepSeek direct Flash routes.
163 ///
164 /// DeepSeek's Vision guide documents image input for `deepseek-flash` over
165 /// Chat Completions, Responses *and* Messages; the legacy `deepseek-v4-flash`
166 /// and `deepseek-v4-flash-vision-exp` ids are served by the same model
167 /// (verified 2026-09-23, #6421). The curated offering rows are scoped to the
168 /// canonical `deepseek` provider and its OpenAI-compatible hosts, so the
169 /// Messages route — `deepseek-anthropic`, or canonical DeepSeek with
170 /// `wire = "anthropic"` (the `/anthropic` base URL) — needs this route-aware
171 /// projection. A custom compatible host or any other model stays `Unknown`.
172 #[must_use]
173 pub(crate) fn documented_deepseek_image_input_for_route(
174 provider: ProviderKind,
175 wire_model_id: &str,
176 base_url: &str,
177 ) -> CapabilityState {
178 if !is_official_deepseek_route(provider, base_url) {
179 return CapabilityState::Unknown;
180 }
181 let model = wire_model_id.trim().to_ascii_lowercase();
182 if crate::catalog::reviewed::route_model_set_contains("deepseek_image", &model) {
183 CapabilityState::Supported
184 } else {
185 CapabilityState::Unknown
186 }
187 }
188
189 /// A DeepSeek provider kind on one of DeepSeek's own hosts, in either wire
190 /// dialect.
191 fn is_official_deepseek_route(provider: ProviderKind, base_url: &str) -> bool {
192 if !matches!(
193 provider,
194 ProviderKind::Deepseek | ProviderKind::DeepseekAnthropic
195 ) {
196 return false;
197 }
198 let normalized = base_url.trim().trim_end_matches('/').to_ascii_lowercase();
199 matches!(
200 normalized.as_str(),
201 "https://api.deepseek.com"
202 | "https://api.deepseek.com/v1"
203 | "https://api.deepseek.com/beta"
204 | "https://api.deepseek.com/anthropic"
205 )
206 }
207
208 /// Capability facts owned by one provider/model route offering.
209 ///
210 /// Fields without a current authoritative catalog source remain `Unknown`.
211 /// They are present now so live/provider-native facts can be added without
212 /// changing the candidate contract or guessing from request protocol.
213 #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
214 pub struct RouteCapabilities {
215 #[serde(default)]
216 pub attachments: CapabilityState,
217 /// Whether the exact offering explicitly accepts image input.
218 #[serde(default)]
219 pub image_input: CapabilityState,
220 /// Whether the exact offering supports the provider's Files API (upload
221 /// once, reference the returned id from later turns).
222 #[serde(default)]
223 pub files_api: CapabilityState,
224 #[serde(default)]
225 pub reasoning: CapabilityState,
226 #[serde(default)]
227 pub native_tool_calls: CapabilityState,
228 #[serde(default)]
229 pub structured_output: CapabilityState,
230 #[serde(default)]
231 pub parallel_tool_calls: CapabilityState,
232 #[serde(default)]
233 pub streaming: CapabilityState,
234 #[serde(default)]
235 pub prompt_caching: CapabilityState,
236 #[serde(default)]
237 pub server_side_web_search: CapabilityState,
238 }
239
240 #[cfg(test)]
241 mod tests {
242 use super::*;
243 use crate::DEFAULT_KIMI_CODE_BASE_URL;
244
245 #[test]
246 fn optional_boolean_preserves_unknown_and_false() {
247 assert_eq!(
248 CapabilityState::from_optional_bool(None),
249 CapabilityState::Unknown
250 );
251 assert_eq!(
252 CapabilityState::from_optional_bool(Some(false)),
253 CapabilityState::Unsupported
254 );
255 assert_eq!(
256 CapabilityState::from_optional_bool(Some(true)),
257 CapabilityState::Supported
258 );
259 }
260
261 #[test]
262 fn unsourced_route_capabilities_default_to_unknown() {
263 let capabilities = RouteCapabilities::default();
264 assert_eq!(capabilities.streaming, CapabilityState::Unknown);
265 assert_eq!(
266 capabilities.server_side_web_search,
267 CapabilityState::Unknown
268 );
269 }
270
271 #[test]
272 fn documented_web_search_is_exact_and_provider_owned() {
273 assert_eq!(
274 documented_server_side_web_search("xai", "grok-4.6"),
275 CapabilityState::Supported
276 );
277 assert_eq!(
278 documented_server_side_web_search("xai", "grok-4.5"),
279 CapabilityState::Supported
280 );
281 assert_eq!(
282 documented_server_side_web_search("openai", "gpt-5.6"),
283 CapabilityState::Supported
284 );
285 assert_eq!(
286 documented_server_side_web_search("anthropic", "claude-sonnet-4-6"),
287 CapabilityState::Supported
288 );
289 assert_eq!(
290 documented_server_side_web_search("xiaomi-mimo", "mimo-v2.5-pro"),
291 CapabilityState::Supported
292 );
293 assert_eq!(
294 documented_server_side_web_search("zai", "GLM-5.3"),
295 CapabilityState::Supported
296 );
297 assert_eq!(
298 documented_server_side_web_search("modelstudio-token-plan", "qwen3.8-max"),
299 CapabilityState::Supported
300 );
301 assert_eq!(
302 documented_server_side_web_search("deepseek", "deepseek-v4-flash"),
303 CapabilityState::Supported
304 );
305 assert_eq!(
306 documented_server_side_web_search("moonshot", "kimi-k3"),
307 CapabilityState::Supported
308 );
309
310 for (provider, model) in [
311 ("openrouter", "openai/gpt-5.6"),
312 ("custom", "gpt-5.6"),
313 ("openai", "gpt-5.6-sol"),
314 ("xai", "grok-4.6-fast"),
315 ("xai", "grok-4.6-latest"),
316 ("xai", "grok-4.5-fast"),
317 ("anthropic", "claude-haiku-4-5"),
318 ("xiaomi-mimo", "mimo-v2.5-pro-ultraspeed"),
319 ("zai", "glm-5.3-preview"),
320 ("modelstudio-coding-plan", "qwen3.8-max"),
321 ("modelstudio-token-plan", "qwen3.8-max-preview"),
322 ("deepseek", "deepseek-v4-flash-preview"),
323 ("moonshot", "kimi-k2.7-code"),
324 ] {
325 assert_eq!(
326 documented_server_side_web_search(provider, model),
327 CapabilityState::Unknown,
328 "{provider}/{model} must not inherit a capability by similarity"
329 );
330 }
331 }
332
333 #[test]
334 fn deepseek_files_api_fact_is_exact_to_model_and_host() {
335 for provider in [ProviderKind::Deepseek, ProviderKind::DeepseekAnthropic] {
336 for base_url in [
337 "https://api.deepseek.com",
338 "https://api.deepseek.com/v1",
339 "https://api.deepseek.com/beta",
340 "https://api.deepseek.com/anthropic/",
341 ] {
342 for model in ["deepseek-flash", "deepseek-v4-flash"] {
343 assert_eq!(
344 documented_deepseek_files_api_for_route(provider, model, base_url),
345 CapabilityState::Supported,
346 "{provider:?}/{model}/{base_url}"
347 );
348 }
349 }
350 }
351 for (provider, model, base_url) in [
352 (
353 ProviderKind::Deepseek,
354 "deepseek-v4-pro",
355 "https://api.deepseek.com",
356 ),
357 (
358 ProviderKind::Deepseek,
359 "deepseek-v4-flash-vision-exp",
360 "https://api.deepseek.com",
361 ),
362 (
363 ProviderKind::Deepseek,
364 "deepseek-v5-future",
365 "https://api.deepseek.com",
366 ),
367 (
368 ProviderKind::Deepseek,
369 "deepseek-flash",
370 "https://compatible.example.test/v1",
371 ),
372 (
373 ProviderKind::Deepseek,
374 "deepseek-flash",
375 "https://api.deepseek.com.example.test/v1",
376 ),
377 (
378 ProviderKind::Openrouter,
379 "deepseek/deepseek-v4-flash",
380 "https://openrouter.ai/api/v1",
381 ),
382 ] {
383 assert_eq!(
384 documented_deepseek_files_api_for_route(provider, model, base_url),
385 CapabilityState::Unknown,
386 "{provider:?}/{model}/{base_url} must not inherit the Files API fact"
387 );
388 }
389 }
390
391 #[test]
392 fn zai_route_fact_rejects_coding_and_neighboring_endpoints() {
393 for base_url in [
394 "https://api.z.ai/api/paas/v4",
395 "https://open.bigmodel.cn/api/paas/v4/",
396 ] {
397 assert_eq!(
398 documented_zai_web_search_for_route(ProviderKind::Zai, "GLM-5.3", base_url),
399 CapabilityState::Supported
400 );
401 }
402 for base_url in [
403 "https://api.z.ai/api/coding/paas/v4",
404 "https://open.bigmodel.cn/api/coding/paas/v4",
405 "https://open.bigmodel.cn/api/paas/v4/preview",
406 "https://gateway.example.test/v4",
407 ] {
408 assert_eq!(
409 documented_zai_web_search_for_route(ProviderKind::Zai, "GLM-5.3", base_url),
410 CapabilityState::Unknown
411 );
412 }
413 }
414
415 #[test]
416 fn moonshot_route_fact_is_exact_to_product_and_model() {
417 for (model, base_url) in [
418 ("kimi-k3", "https://api.moonshot.ai/v1"),
419 ("kimi-k3", "https://api.moonshot.cn/v1"),
420 ("kimi-k2.6", "https://api.moonshot.ai/v1"),
421 ("k3", DEFAULT_KIMI_CODE_BASE_URL),
422 ("kimi-for-coding", DEFAULT_KIMI_CODE_BASE_URL),
423 ] {
424 assert_eq!(
425 documented_moonshot_web_search_for_route(ProviderKind::Moonshot, model, base_url,),
426 CapabilityState::Supported
427 );
428 }
429 for (model, base_url) in [
430 ("kimi-k3", "https://api.kimi.com/coding/v2"),
431 ("kimi-k2.6", "https://api.kimi.com/coding/v1/preview"),
432 ("k3", "https://api.moonshot.ai/v1"),
433 ] {
434 assert_eq!(
435 documented_moonshot_web_search_for_route(ProviderKind::Moonshot, model, base_url,),
436 CapabilityState::Unknown
437 );
438 }
439 }
440
441 #[test]
442 fn deepseek_web_search_fact_is_exact_to_official_routes() {
443 for model in ["deepseek-v4-pro", "deepseek-flash", "deepseek-v4-flash"] {
444 assert_eq!(
445 documented_deepseek_web_search_for_route(
446 ProviderKind::Deepseek,
447 model,
448 "https://api.deepseek.com/v1"
449 ),
450 CapabilityState::Supported
451 );
452 assert_eq!(
453 documented_deepseek_web_search_for_route(
454 ProviderKind::DeepseekAnthropic,
455 model,
456 "https://api.deepseek.com/anthropic"
457 ),
458 CapabilityState::Supported
459 );
460 assert_eq!(
461 documented_deepseek_web_search_for_route(
462 ProviderKind::Deepseek,
463 model,
464 "https://openrouter.ai/api/v1"
465 ),
466 CapabilityState::Unknown
467 );
468 }
469 assert_eq!(
470 documented_deepseek_web_search_for_route(
471 ProviderKind::Openai,
472 "gpt-4o",
473 "https://api.openai.com/v1"
474 ),
475 CapabilityState::Unknown
476 );
477 }
478 }
479
479 lines RUST