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
use async_stream::try_stream;
use async_trait::async_trait;
use compact_str::format_compact;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
use std::sync::RwLock;
use vtcode_commons::llm::BackendKind;
use vtcode_commons::tool_types::CompactStr;
use super::{
LLMNormalizedStream, LLMRequest, LLMResponse, LLMStream, LLMStreamEvent, Message, ResponsesCompactionOptions,
};
pub use vtcode_commons::llm::{LLMError, LLMErrorMetadata};
/// Cached provider capabilities to reduce repeated trait method calls
#[derive(Debug, Clone)]
pub struct ProviderCapabilities {
pub provider_name: String,
pub model: String,
pub streaming: bool,
pub reasoning: bool,
pub reasoning_effort: bool,
pub tools: bool,
pub parallel_tool_config: bool,
pub structured_output: bool,
pub context_caching: bool,
pub responses_compaction: bool,
pub context_edits: bool,
pub vision: bool,
pub context_size: usize,
}
impl ProviderCapabilities {
pub fn detect(provider: &dyn LLMProvider, model: &str) -> Self {
Self {
provider_name: provider.name().to_string(),
model: model.to_string(),
streaming: provider.supports_streaming(),
reasoning: provider.supports_reasoning(model),
reasoning_effort: provider.supports_reasoning_effort(model),
tools: provider.supports_tools(model),
parallel_tool_config: provider.supports_parallel_tool_config(model),
structured_output: provider.supports_structured_output(model),
context_caching: provider.supports_context_caching(model),
responses_compaction: provider.supports_responses_compaction(model),
context_edits: provider.supports_context_edits(model),
vision: provider.supports_vision(model),
context_size: provider.effective_context_size(model),
}
}
pub fn has_advanced_features(&self) -> bool {
self.reasoning || self.structured_output || self.context_caching || self.reasoning_effort
}
pub fn summary(&self) -> String {
let mut features = Vec::new();
if self.streaming {
features.push("streaming");
}
if self.reasoning {
features.push("advanced-reasoning");
}
if self.reasoning_effort {
features.push("reasoning-effort");
}
if self.structured_output {
features.push("structured-output");
}
if self.context_caching {
features.push("context-caching");
}
if self.parallel_tool_config {
features.push("parallel-tools");
}
if self.responses_compaction {
features.push("responses-compaction");
}
if self.context_edits {
features.push("context-edits");
}
let features_str = if features.is_empty() {
"basic".to_string()
} else {
features.join(", ")
};
format!("{} ({} tokens): {}", self.model, self.context_size, features_str)
}
}
/// Global cache for provider capabilities (provider_name::model -> capabilities)
static CAPABILITY_CACHE: Lazy<RwLock<FxHashMap<CompactStr, ProviderCapabilities>>> =
Lazy::new(|| RwLock::new(FxHashMap::default()));
/// Extract and cache provider capabilities for a given provider and model
pub fn get_cached_capabilities(provider: &dyn LLMProvider, model: &str) -> ProviderCapabilities {
let cache_key = format_compact!("{}::{}", provider.name(), model);
// Check if already cached
if let Ok(cache) = CAPABILITY_CACHE.read()
&& let Some(caps) = cache.get(&cache_key)
{
return caps.clone();
}
// Compute capabilities
let caps = ProviderCapabilities::detect(provider, model);
// Cache for future use
if let Ok(mut cache) = CAPABILITY_CACHE.write() {
cache.insert(cache_key, caps.clone());
}
caps
}
/// Universal LLM provider trait
#[async_trait]
pub trait LLMProvider: Send + Sync {
/// Provider name (e.g., "gemini", "openai", "anthropic")
fn name(&self) -> &str;
/// The canonical backend kind for this provider.
///
/// Defaults to matching on [`name()`](LLMProvider::name) against the
/// well-known provider names. Providers should override this when their
/// name does not match the canonical mapping (e.g., dynamic names).
fn backend_kind(&self) -> BackendKind {
match self.name() {
"gemini" => BackendKind::Gemini,
"openai" => BackendKind::OpenAI,
"anthropic" => BackendKind::Anthropic,
"deepseek" => BackendKind::DeepSeek,
"mistral" => BackendKind::Mistral,
"openrouter" => BackendKind::OpenRouter,
"ollama" => BackendKind::Ollama,
"llamacpp" => BackendKind::LlamaCpp,
"zai" => BackendKind::ZAI,
"moonshot" => BackendKind::Moonshot,
"huggingface" => BackendKind::HuggingFace,
"minimax" => BackendKind::Minimax,
"mimo" => BackendKind::MiMo,
"opencode-zen" => BackendKind::OpenCodeZen,
"opencode-go" => BackendKind::OpenCodeGo,
"qwen" => BackendKind::Qwen,
"stepfun" => BackendKind::StepFun,
"evolink" => BackendKind::Evolink,
"poolside" => BackendKind::Poolside,
_ => BackendKind::OpenAI,
}
}
/// Whether the provider has native streaming support
fn supports_streaming(&self) -> bool {
false
}
/// Whether the provider can service non-streaming generation requests for the model.
fn supports_non_streaming(&self, _model: &str) -> bool {
true
}
/// Whether the provider surfaces structured reasoning traces for the given model
fn supports_reasoning(&self, _model: &str) -> bool {
false
}
/// Whether the provider accepts configurable reasoning effort for the model
fn supports_reasoning_effort(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports structured tool calling for the given model
fn supports_tools(&self, _model: &str) -> bool {
true
}
/// Whether the provider understands parallel tool configuration payloads
fn supports_parallel_tool_config(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports structured output (JSON schema guarantees)
fn supports_structured_output(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports prompt/context caching
fn supports_context_caching(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports vision (image analysis) for given model
fn supports_vision(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports Responses API server-side compaction.
fn supports_responses_compaction(&self, _model: &str) -> bool {
false
}
/// Whether the request path can enforce a provider-native `allowed_tools` subset.
fn supports_native_allowed_tools(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports provider-native context editing such as
/// tool-result clearing.
fn supports_context_edits(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports the interactive manual `/compact` command path.
///
/// This is narrower than general Responses compaction support and may exclude
/// compatible endpoints that do not match VT Code's native OpenAI UX contract.
fn supports_manual_openai_compaction(&self, _model: &str) -> bool {
false
}
/// Whether the provider supports threshold-triggered inline compaction via
/// request fields (Anthropic `compact_20260112`).
///
/// This is distinct from [`supports_responses_compaction`](LLMProvider::supports_responses_compaction),
/// which is overloaded: OpenAI-compatible endpoints report it for their
/// standalone `/responses/compact` endpoint while Anthropic reports it for
/// inline compaction. Only the latter can be driven through `generate` with a
/// `compact_20260112` context-management edit, so the unified compaction
/// dispatch uses this method (not the overloaded flag) to pick the
/// `NativeInline` strategy and avoid sending an Anthropic-specific payload to
/// an OpenAI-compatible endpoint (which would only be rejected and fall back
/// to local summarization anyway).
fn supports_native_inline_compaction(&self, _model: &str) -> bool {
false
}
/// Explain why the `--native-only` manual `/compact` path is unavailable.
///
/// This message only surfaces when the user explicitly passes `--native-only`
/// and the provider does not expose a native server-side compaction endpoint.
/// The plain `/compact` command is provider-agnostic and always falls back to
/// local summarization, so it is never refused on capability grounds.
fn manual_openai_compaction_unavailable_message(&self, model: &str) -> String {
format!(
"`--native-only` `/compact` requires a provider that exposes a native server-side compaction endpoint, which this provider does not. Active provider/model: {} / {}. Run `/compact` without `--native-only` to compact via the universal local summarization fallback.",
self.name(),
model,
)
}
/// Get the effective context window size for a model
fn effective_context_size(&self, _model: &str) -> usize {
// Default to 128k context window (common baseline)
128_000
}
/// Compact conversation history using provider-native Responses `/compact`
/// support when available.
async fn compact_history(&self, _model: &str, _history: &[Message]) -> Result<Vec<Message>, LLMError> {
Err(LLMError::Provider {
message: "Conversation compaction is not supported by this provider".to_string(),
metadata: None,
})
}
/// Compact conversation history with standalone Responses compaction options.
async fn compact_history_with_options(
&self,
_model: &str,
_history: &[Message],
_options: &ResponsesCompactionOptions,
) -> Result<Vec<Message>, LLMError> {
Err(LLMError::Provider {
message: "manual OpenAI compaction is not supported by this provider".to_string(),
metadata: None,
})
}
/// Generate completion
async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError>;
/// Stream completion (optional)
async fn stream(&self, request: LLMRequest) -> Result<LLMStream, LLMError> {
// Default implementation falls back to non-streaming
let response = self.generate(request).await?;
let stream = try_stream! {
yield LLMStreamEvent::Completed { response: Box::new(response) };
};
Ok(Box::pin(stream))
}
/// Normalized streaming contract layered on top of the legacy provider stream.
async fn stream_normalized(&self, request: LLMRequest) -> Result<LLMNormalizedStream, LLMError> {
let mut legacy_stream = self.stream(request).await?;
let stream = try_stream! {
while let Some(event) = futures::StreamExt::next(&mut legacy_stream).await {
for normalized in event?.into_normalized() {
yield normalized;
}
}
};
Ok(Box::pin(stream))
}
/// Provider-specific streaming path that can service interactive runtime
/// requests while the stream is active. Copilot uses this to bridge ACP
/// tool calls and permission prompts back into VT Code's turn runtime.
#[cfg(feature = "copilot")]
fn start_copilot_prompt_session<'a>(
&'a self,
_request: LLMRequest,
_tools: &'a [super::ToolDefinition],
) -> Option<crate::copilot::CopilotPromptSessionFuture<'a>> {
None
}
/// Get supported models
fn supported_models(&self) -> Vec<String>;
/// Fetch account balance for this provider, if supported.
async fn get_balance(&self) -> Result<Option<vtcode_commons::llm::BalanceInfo>, LLMError> {
Ok(None)
}
/// Validate request for this provider
fn validate_request(&self, request: &LLMRequest) -> Result<(), LLMError>;
}