vtcode-llm 0.154.0

LLM provider abstraction, client implementations, and streaming for VT Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Model capability detection for Anthropic Claude models
//!
//! Provides methods to determine what features each Claude model supports:
//! - Reasoning/extended thinking
//! - Vision (image inputs)
//! - Structured outputs
//! - Parallel tool configuration
//! - Context window sizes

use crate::providers::anthropic_types::ThinkingDisplay;
use vtcode_config::constants::{models, reasoning};

const CLAUDE_OPUS_4_8: &str = "claude-opus-4-8";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ClaudeThinkingMode {
    ManualBudget,
    Adaptive,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ClaudeThinkingProfile {
    pub mode: ClaudeThinkingMode,
    pub supports_manual_budget: bool,
    pub adaptive_only: bool,
    pub default_thinking_enabled: bool,
    pub manual_interleaved_beta: bool,
    pub supports_effort: bool,
    pub supports_task_budget: bool,
    pub default_display: ThinkingDisplay,
    pub default_effort: &'static str,
    pub supports_xhigh_effort: bool,
    pub supports_max_effort: bool,
}

const ANTHROPIC_EFFORTS_UP_TO_HIGH: &[&str] = &[reasoning::LOW, reasoning::MEDIUM, reasoning::HIGH];
const ANTHROPIC_EFFORTS_UP_TO_MAX: &[&str] = &[reasoning::LOW, reasoning::MEDIUM, reasoning::HIGH, reasoning::MAX];
const ANTHROPIC_EFFORTS_UP_TO_XHIGH_AND_MAX: &[&str] = &[
    reasoning::LOW,
    reasoning::MEDIUM,
    reasoning::HIGH,
    reasoning::XHIGH,
    reasoning::MAX,
];

pub(crate) fn resolve_model_name<'a>(model: &'a str, default_model: &'a str) -> &'a str {
    if model.trim().is_empty() { default_model } else { model }
}

pub(crate) fn matches_model(model: &str, candidate: &str) -> bool {
    model == candidate || model.contains(candidate)
}

pub(crate) fn claude_thinking_profile(model: &str, default_model: &str) -> Option<ClaudeThinkingProfile> {
    let requested = resolve_model_name(model, default_model);

    // Check most specific models first – `matches_model` uses `contains`,
    // so `claude-fable-5-1` contains `claude-fable-5`. The 5.1 variants
    // must be checked before their 5 counterparts.
    if matches_model(requested, models::anthropic::CLAUDE_FABLE_5_1) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: false,
            adaptive_only: true,
            default_thinking_enabled: true,
            manual_interleaved_beta: false,
            supports_effort: true,
            supports_task_budget: true,
            default_display: ThinkingDisplay::Omitted,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: true,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_MYTHOS_5_1) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: false,
            adaptive_only: true,
            default_thinking_enabled: true,
            manual_interleaved_beta: false,
            supports_effort: true,
            supports_task_budget: true,
            default_display: ThinkingDisplay::Omitted,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: true,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_SONNET_5) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: false,
            adaptive_only: false,
            default_thinking_enabled: true,
            manual_interleaved_beta: false,
            supports_effort: true,
            supports_task_budget: false,
            default_display: ThinkingDisplay::Omitted,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: true,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_FABLE_5) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: false,
            adaptive_only: true,
            default_thinking_enabled: true,
            manual_interleaved_beta: false,
            supports_effort: true,
            supports_task_budget: true,
            default_display: ThinkingDisplay::Omitted,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: true,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_MYTHOS_5) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: false,
            adaptive_only: true,
            default_thinking_enabled: true,
            manual_interleaved_beta: false,
            supports_effort: true,
            supports_task_budget: true,
            default_display: ThinkingDisplay::Omitted,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: true,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_OPUS_5) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: false,
            adaptive_only: false,
            default_thinking_enabled: true,
            manual_interleaved_beta: false,
            supports_effort: true,
            supports_task_budget: true,
            default_display: ThinkingDisplay::Omitted,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: true,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_OPUS_5) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: false,
            adaptive_only: false,
            default_thinking_enabled: false,
            manual_interleaved_beta: false,
            supports_effort: true,
            supports_task_budget: true,
            default_display: ThinkingDisplay::Omitted,
            default_effort: reasoning::XHIGH,
            supports_xhigh_effort: true,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_SONNET_5) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::Adaptive,
            supports_manual_budget: true,
            adaptive_only: false,
            default_thinking_enabled: false,
            manual_interleaved_beta: true,
            supports_effort: true,
            supports_task_budget: false,
            default_display: ThinkingDisplay::Summarized,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: false,
            supports_max_effort: true,
        });
    }

    if matches_model(requested, models::anthropic::CLAUDE_SONNET_5) {
        return Some(ClaudeThinkingProfile {
            mode: ClaudeThinkingMode::ManualBudget,
            supports_manual_budget: true,
            adaptive_only: false,
            default_thinking_enabled: false,
            manual_interleaved_beta: true,
            supports_effort: false,
            supports_task_budget: false,
            default_display: ThinkingDisplay::Summarized,
            default_effort: reasoning::HIGH,
            supports_xhigh_effort: false,
            supports_max_effort: false,
        });
    }

    None
}

fn supports_native_1m_context(model: &str) -> bool {
    matches_model(model, models::anthropic::CLAUDE_SONNET_5)
        || matches_model(model, models::anthropic::CLAUDE_FABLE_5)
        || matches_model(model, models::anthropic::CLAUDE_FABLE_5_1)
        || matches_model(model, models::anthropic::CLAUDE_MYTHOS_5)
        || matches_model(model, models::anthropic::CLAUDE_MYTHOS_5_1)
        || matches_model(model, models::anthropic::CLAUDE_OPUS_5)
        || matches_model(model, models::anthropic::CLAUDE_SONNET_5)
        || matches_model(model, models::anthropic::CLAUDE_OPUS_5)
}

pub(crate) fn supports_reasoning(model: &str, default_model: &str) -> bool {
    let requested = resolve_model_name(model, default_model);
    if claude_thinking_profile(requested, default_model).is_some() {
        return true;
    }

    models::minimax::SUPPORTED_MODELS.contains(&requested)
}

pub(crate) fn supports_reasoning_effort(model: &str, default_model: &str) -> bool {
    let requested = resolve_model_name(model, default_model);

    if claude_thinking_profile(requested, default_model).is_some() {
        return true;
    }

    if models::minimax::SUPPORTED_MODELS.contains(&requested) {
        return true;
    }

    models::anthropic::REASONING_MODELS.contains(&requested)
}

pub(crate) fn supports_effort(model: &str, default_model: &str) -> bool {
    claude_thinking_profile(model, default_model).is_some_and(|profile| profile.supports_effort)
}

pub(crate) fn supports_task_budget(model: &str, default_model: &str) -> bool {
    claude_thinking_profile(model, default_model).is_some_and(|profile| profile.supports_task_budget)
}

pub(crate) fn supports_manual_thinking_budget(model: &str, default_model: &str) -> bool {
    claude_thinking_profile(model, default_model).is_some_and(|profile| profile.supports_manual_budget)
}

pub(crate) fn supports_manual_interleaved_beta(model: &str, default_model: &str) -> bool {
    claude_thinking_profile(model, default_model).is_some_and(|profile| profile.manual_interleaved_beta)
}

pub(crate) fn supports_assistant_prefill(model: &str, default_model: &str) -> bool {
    let requested = resolve_model_name(model, default_model);

    // Models with thinking profiles are newer and generally do not support prefill,
    // except Haiku 4.5 which explicitly does. For models without a thinking profile
    // (legacy models), prefill is supported.
    match claude_thinking_profile(requested, default_model) {
        Some(profile) => {
            // Haiku 4.5 is the only thinking-profile model that supports prefill.
            // All others (Sonnet 5, Fable 5, Mythos 5, Opus 4.8, Sonnet 4.6) do not.
            !profile.adaptive_only && matches_model(requested, models::anthropic::CLAUDE_SONNET_5)
        }
        None => true,
    }
}

pub(crate) fn supports_mid_conversation_system_messages(model: &str, default_model: &str) -> bool {
    supports_turn_scoped_system_messages(model, default_model)
}

/// Whether the model accepts Anthropic's turn-scoped `clear_at` system-message
/// field. The current beta is available to the model families that accept
/// mid-conversation system messages, but not Sonnet 5.
pub(crate) fn supports_turn_scoped_system_messages(model: &str, default_model: &str) -> bool {
    let requested = resolve_model_name(model, default_model);
    matches_model(requested, models::anthropic::CLAUDE_FABLE_5)
        || matches_model(requested, models::anthropic::CLAUDE_MYTHOS_5)
        || matches_model(requested, CLAUDE_OPUS_4_8)
        || matches_model(requested, models::anthropic::CLAUDE_OPUS_5)
}

pub(crate) fn adaptive_thinking_always_on(model: &str, default_model: &str) -> bool {
    claude_thinking_profile(model, default_model).is_some_and(|profile| profile.adaptive_only)
}

pub(crate) fn default_effort_for_model(model: &str, default_model: &str) -> Option<&'static str> {
    claude_thinking_profile(model, default_model)
        .filter(|profile| profile.supports_effort)
        .map(|profile| profile.default_effort)
}

pub(crate) fn allowed_efforts_for_model(model: &str, default_model: &str) -> Option<&'static [&'static str]> {
    let profile = claude_thinking_profile(model, default_model)?;
    if !profile.supports_effort {
        return None;
    }

    if profile.supports_xhigh_effort {
        Some(ANTHROPIC_EFFORTS_UP_TO_XHIGH_AND_MAX)
    } else if profile.supports_max_effort {
        Some(ANTHROPIC_EFFORTS_UP_TO_MAX)
    } else {
        Some(ANTHROPIC_EFFORTS_UP_TO_HIGH)
    }
}

pub(crate) fn effort_allowed_for_model(model: &str, default_model: &str, effort: &str) -> bool {
    let normalized = effort.trim().to_ascii_lowercase();
    allowed_efforts_for_model(model, default_model).is_some_and(|allowed| allowed.contains(&normalized.as_str()))
}

pub(crate) fn supports_compaction(model: &str) -> bool {
    matches_model(model, models::anthropic::CLAUDE_SONNET_5)
        || matches_model(model, models::anthropic::CLAUDE_FABLE_5)
        || matches_model(model, models::anthropic::CLAUDE_FABLE_5_1)
        || matches_model(model, models::anthropic::CLAUDE_MYTHOS_5)
        || matches_model(model, models::anthropic::CLAUDE_MYTHOS_5_1)
        || matches_model(model, models::anthropic::CLAUDE_OPUS_5)
        || matches_model(model, models::anthropic::CLAUDE_OPUS_5)
        || matches_model(model, models::anthropic::CLAUDE_SONNET_5)
}

pub(crate) fn supports_parallel_tool_config(_model: &str) -> bool {
    true
}

pub fn effective_context_size(model: &str) -> usize {
    if supports_native_1m_context(model) {
        1_000_000
    } else {
        200_000
    }
}

pub(crate) fn rejects_sampling(model: &str, default_model: &str) -> bool {
    let requested = resolve_model_name(model, default_model);
    matches_model(requested, models::anthropic::CLAUDE_SONNET_5)
        || matches_model(requested, models::anthropic::CLAUDE_FABLE_5)
        || matches_model(requested, models::anthropic::CLAUDE_FABLE_5_1)
        || matches_model(requested, models::anthropic::CLAUDE_MYTHOS_5)
        || matches_model(requested, models::anthropic::CLAUDE_MYTHOS_5_1)
        || matches_model(requested, models::anthropic::CLAUDE_OPUS_5)
        || matches_model(requested, models::anthropic::CLAUDE_OPUS_5)
}

pub(crate) fn supports_structured_output(model: &str, default_model: &str) -> bool {
    let requested = resolve_model_name(model, default_model);

    // All models with a thinking profile support structured outputs.
    if claude_thinking_profile(requested, default_model).is_some() {
        return true;
    }

    // Legacy models without thinking profiles that support structured outputs.
    matches_model(requested, "claude-sonnet-4-5")
        || matches_model(requested, "claude-opus-4-5")
        || matches_model(requested, "claude-sonnet-5")
}

pub(crate) fn supports_vision(model: &str, default_model: &str) -> bool {
    let requested = resolve_model_name(model, default_model);

    // All models with a thinking profile support vision.
    if claude_thinking_profile(requested, default_model).is_some() {
        return true;
    }

    // Legacy Claude 3 and Claude 4 Sonnet families support vision.
    requested.starts_with("claude-3") || requested.starts_with("claude-4-sonnet")
}

pub fn is_claude_model(model: &str, default_model: &str) -> bool {
    claude_thinking_profile(model, default_model).is_some()
}

pub(crate) fn supported_models() -> Vec<String> {
    let mut supported: Vec<String> = models::anthropic::SUPPORTED_MODELS.iter().map(|s| s.to_string()).collect();

    supported.extend(models::minimax::SUPPORTED_MODELS.iter().map(|s| s.to_string()));

    supported.sort();
    supported.dedup();
    supported
}

/// Returns true if the effective effort for this request is "low", "medium", or "high"
/// (i.e., at most high, not xhigh or max).
///
/// This is used by Opus 5 disabled-thinking validation: Opus 5 only allows
/// `thinking: {type: "disabled"}` at effort ≤ high. When the override is `Omit`,
/// the API uses the model default effort, so we check that instead of the config.
pub(crate) fn effort_is_at_most_high(
    request: &crate::provider::LLMRequest,
    anthropic_config: &vtcode_config::core::AnthropicConfig,
) -> bool {
    use crate::provider::{AnthropicOptionalStringOverride, LLMRequest};
    use vtcode_config::types::ReasoningEffortLevel;

    if let Some(overrides) = request.anthropic_request_overrides.as_ref() {
        match &overrides.effort {
            AnthropicOptionalStringOverride::Explicit(effort) => {
                return matches!(effort.to_ascii_lowercase().as_str(), "low" | "medium" | "high");
            }
            AnthropicOptionalStringOverride::Omit => {
                return default_effort_for_model(&request.model, "").is_some_and(|effort| effort <= "high");
            }
            AnthropicOptionalStringOverride::Inherit => {}
        }
    }

    if let Some(effort) = request.effort.as_ref() {
        return matches!(effort.to_ascii_lowercase().as_str(), "low" | "medium" | "high");
    }
    if let Some(effort) = request.reasoning_effort {
        return matches!(effort, ReasoningEffortLevel::Low | ReasoningEffortLevel::Medium | ReasoningEffortLevel::High);
    }
    matches!(anthropic_config.effort.as_str(), "low" | "medium" | "high")
}