vtcode-llm 0.146.8

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
//! Merge Gateway authenticated model catalog discovery.
//!
//! This module fetches the native `/v1/models` catalog, paginates cursor-based
//! results, and normalizes vendor-specific capability data into vtcode-core
//! friendly snapshot structures.

use anyhow::{Context, Result, bail, ensure};
use reqwest::{
    Client, StatusCode,
    header::{ACCEPT, IF_NONE_MATCH},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::time::Duration;
use vtcode_commons::tool_types::CompactStr;
use vtcode_config::TimeoutsConfig;

use super::merge_gateway_contract::{
    MergeAvailabilityStatus, MergeInputModality, MergeModelCatalogResponse, MergeModelRecord, MergeModelsListQuery,
    MergeServiceTier, MergeVendorModelInfo,
};

const CATALOG_PAGE_LIMIT: u32 = 500;
const MAX_CATALOG_PAGES: usize = 1_000;
const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 30;
const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 120;

/// Normalized catalog availability for vtcode-core.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MergeCatalogAvailability {
    Available,
    Deprecated,
    Unknown,
}

impl From<MergeAvailabilityStatus> for MergeCatalogAvailability {
    fn from(value: MergeAvailabilityStatus) -> Self {
        match value {
            MergeAvailabilityStatus::Available => Self::Available,
            MergeAvailabilityStatus::Deprecated => Self::Deprecated,
            MergeAvailabilityStatus::Unknown => Self::Unknown,
        }
    }
}

/// Normalized service tiers for the catalog snapshot.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MergeCatalogServiceTier {
    Standard,
    Flex,
    Priority,
    Unknown,
}

impl From<MergeServiceTier> for MergeCatalogServiceTier {
    fn from(value: MergeServiceTier) -> Self {
        match value {
            MergeServiceTier::Standard => Self::Standard,
            MergeServiceTier::Flex => Self::Flex,
            MergeServiceTier::Priority => Self::Priority,
            MergeServiceTier::Unknown => Self::Unknown,
        }
    }
}

/// Optional Merge catalog filters preserved across pagination.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MergeCatalogFilters {
    pub model: Option<String>,
    pub provider: Option<String>,
    pub vendor: Option<String>,
}

/// Normalized Merge catalog model metadata.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MergeCatalogModel {
    pub model: String,
    pub provider: String,
    pub display_name: Option<String>,
    pub availability: MergeCatalogAvailability,
    pub context_window: Option<u32>,
    pub max_output_tokens: Option<u32>,
    pub supports_tool_use: bool,
    pub supports_streaming: bool,
    pub supports_vision: bool,
    pub supports_structured_output: bool,
    pub service_tiers: Vec<MergeCatalogServiceTier>,
    pub supports_reasoning: bool,
    pub reasoning_disable_supported: bool,
    pub reasoning_controls: Vec<String>,
}

/// Full normalized snapshot of the Merge catalog.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MergeCatalogSnapshot {
    pub models: Vec<MergeCatalogModel>,
    pub etag: Option<String>,
}

/// Authenticated Merge Gateway `/v1/models` discovery client.
#[derive(Clone)]
pub struct MergeGatewayCatalogClient {
    api_key: String,
    catalog_base_url: String,
    http_client: Client,
}

impl MergeGatewayCatalogClient {
    /// Creates a client using an injected HTTP client.
    pub fn try_with_client(
        api_key: impl Into<String>,
        base_url: impl Into<String>,
        http_client: Client,
    ) -> Result<Self> {
        Self::try_from_parts(api_key.into(), base_url.into(), http_client)
    }

    /// Creates a client using a timeout-aware HTTP client derived from `TimeoutsConfig`.
    pub fn try_with_timeouts(
        api_key: impl Into<String>,
        base_url: impl Into<String>,
        timeouts: Option<TimeoutsConfig>,
    ) -> Result<Self> {
        let http_client = build_http_client(timeouts.as_ref())?;
        Self::try_from_parts(api_key.into(), base_url.into(), http_client)
    }

    /// Fetches the catalog snapshot. Returns `Ok(None)` when Merge replies `304 Not Modified`
    /// and a non-empty `etag` validator was supplied.
    pub async fn fetch_snapshot(
        &self,
        filters: &MergeCatalogFilters,
        etag: Option<&str>,
    ) -> Result<Option<MergeCatalogSnapshot>> {
        let etag = normalize_optional_value(etag);
        let mut cursor: Option<String> = None;
        let mut seen_cursors: HashSet<String> = HashSet::new();
        let mut models = Vec::new();
        let mut snapshot_etag: Option<String> = None;
        let mut page_index: usize = 1;

        loop {
            ensure!(
                page_index <= MAX_CATALOG_PAGES,
                "Merge Gateway catalog exceeded the maximum of {MAX_CATALOG_PAGES} pages"
            );
            let query = build_wire_query(filters, cursor.as_deref());
            let mut request = self
                .http_client
                .get(self.models_endpoint_url())
                .bearer_auth(&self.api_key)
                .header(ACCEPT, "application/json")
                .query(&query);

            if page_index == 1 {
                if let Some(etag) = etag.as_deref() {
                    request = request.header(IF_NONE_MATCH, etag);
                }
            }

            let response = request.send().await.with_context(|| {
                format!(
                    "Merge Gateway catalog request failed on page {page_index} ({})",
                    describe_filters(filters, cursor.as_deref())
                )
            })?;

            if response.status() == StatusCode::NOT_MODIFIED {
                if page_index == 1 && etag.is_some() {
                    return Ok(None);
                }

                bail!(
                    "Merge Gateway catalog returned 304 Not Modified on page {page_index} without a usable If-None-Match validator"
                );
            }

            if !response.status().is_success() {
                bail!(
                    "Merge Gateway catalog request failed on page {page_index} with HTTP {} ({})",
                    response.status(),
                    describe_filters(filters, cursor.as_deref())
                );
            }

            if snapshot_etag.is_none() {
                snapshot_etag = header_value_to_string(response.headers().get(reqwest::header::ETAG));
            }

            let raw: Value = response
                .json()
                .await
                .with_context(|| format!("Merge Gateway catalog response on page {page_index} was not valid JSON"))?;
            validate_catalog_envelope(&raw, page_index)?;
            let page: MergeModelCatalogResponse = serde_json::from_value(raw)
                .with_context(|| format!("Merge Gateway catalog response on page {page_index} was malformed"))?;

            models.extend(page.data.into_iter().map(normalize_catalog_model));

            if page.has_more {
                let next_cursor = page
                    .next_cursor
                    .as_deref()
                    .map(str::trim)
                    .filter(|value| !value.is_empty())
                    .map(ToOwned::to_owned)
                    .ok_or_else(|| {
                        anyhow::anyhow!(
                            "Merge Gateway catalog page {page_index} set has_more=true without a non-empty next_cursor"
                        )
                    })?;

                if !seen_cursors.insert(next_cursor.clone()) {
                    bail!("Merge Gateway catalog repeated pagination cursor `{next_cursor}` on page {page_index}");
                }

                cursor = Some(next_cursor);
                page_index += 1;
                continue;
            }

            return Ok(Some(MergeCatalogSnapshot { models, etag: snapshot_etag }));
        }
    }

    fn try_from_parts(api_key: String, base_url: String, http_client: Client) -> Result<Self> {
        let api_key = api_key.trim().to_owned();
        ensure!(!api_key.is_empty(), "Merge Gateway API key cannot be empty");

        let catalog_base_url = normalize_catalog_base_url(&base_url)?;
        Ok(Self { api_key, catalog_base_url, http_client })
    }

    fn models_endpoint_url(&self) -> String {
        format!("{}/models", self.catalog_base_url)
    }
}

fn build_http_client(timeouts: Option<&TimeoutsConfig>) -> Result<Client> {
    let request_timeout = timeouts
        .and_then(|config| config.ceiling_duration(config.default_ceiling_seconds))
        .unwrap_or_else(|| Duration::from_secs(DEFAULT_REQUEST_TIMEOUT_SECS));

    Client::builder()
        .timeout(request_timeout)
        .connect_timeout(Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS))
        .build()
        .context("failed to build Merge Gateway catalog HTTP client")
}

fn build_wire_query(filters: &MergeCatalogFilters, cursor: Option<&str>) -> MergeModelsListQuery {
    MergeModelsListQuery {
        model: normalize_optional_value(filters.model.as_deref()),
        provider: normalize_optional_value(filters.provider.as_deref()),
        vendor: normalize_optional_value(filters.vendor.as_deref()),
        cursor: normalize_optional_value(cursor),
        limit: Some(CATALOG_PAGE_LIMIT),
    }
}

fn normalize_optional_value(value: Option<&str>) -> Option<CompactStr> {
    value.map(str::trim).filter(|value| !value.is_empty()).map(CompactStr::from)
}

fn normalize_catalog_base_url(base_url: &str) -> Result<String> {
    let mut normalized = base_url.trim().trim_end_matches('/').to_owned();
    ensure!(!normalized.is_empty(), "Merge Gateway base URL cannot be empty");

    loop {
        let mut changed = false;
        for suffix in ["/chat/completions", "/responses", "/openai", "/models"] {
            if let Some(stripped) = normalized.strip_suffix(suffix) {
                normalized = stripped.trim_end_matches('/').to_owned();
                changed = true;
                break;
            }
        }

        if !changed {
            break;
        }
    }

    if !normalized.ends_with("/v1") && !normalized.contains("/v1/") {
        normalized.push_str("/v1");
    }

    Ok(normalized)
}

fn validate_catalog_envelope(raw: &Value, page_index: usize) -> Result<()> {
    let object = raw.as_object().and_then(|map| map.get("object")).and_then(Value::as_str);
    ensure!(
        object == Some("list"),
        "Merge Gateway catalog envelope on page {page_index} must set object=\"list\""
    );
    Ok(())
}

fn header_value_to_string(value: Option<&reqwest::header::HeaderValue>) -> Option<String> {
    value
        .and_then(|header| header.to_str().ok())
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToOwned::to_owned)
}

fn describe_filters(filters: &MergeCatalogFilters, cursor: Option<&str>) -> String {
    format!(
        "cursor={cursor:?}, model={:?}, provider={:?}, vendor={:?}",
        filters.model.as_deref(),
        filters.provider.as_deref(),
        filters.vendor.as_deref(),
    )
}

fn normalize_catalog_model(record: MergeModelRecord) -> MergeCatalogModel {
    let context_window = aggregate_min_context_window(&record.vendors);
    let max_output_tokens = aggregate_min_max_output_tokens(&record.vendors);
    let supports_tool_use = all_vendors_support(&record.vendors, |vendor| vendor.capabilities.supports_tool_calling);
    let supports_streaming = all_vendors_support(&record.vendors, |vendor| vendor.capabilities.streaming);
    let supports_vision = all_vendors_support(&record.vendors, |vendor| {
        vendor
            .capabilities
            .input
            .iter()
            .any(|modality| matches!(modality, MergeInputModality::Image))
    });
    let supports_structured_output =
        all_vendors_support(&record.vendors, |vendor| vendor.capabilities.supports_structured_outputs);
    let service_tiers = aggregate_service_tiers(&record.vendors);
    let supports_reasoning = all_vendors_support(&record.vendors, |vendor| vendor.capabilities.supports_reasoning());
    let reasoning_disable_supported = !record.vendors.is_empty()
        && record
            .vendors
            .values()
            .all(|vendor| vendor.capabilities.reasoning_disable_supported());
    let reasoning_controls = aggregate_reasoning_controls(&record.vendors);

    MergeCatalogModel {
        model: record.model.to_string(),
        provider: record.provider.to_string(),
        display_name: record.display_name.map(|value| value.to_string()),
        availability: record.availability_status.into(),
        context_window,
        max_output_tokens,
        supports_tool_use,
        supports_streaming,
        supports_vision,
        supports_structured_output,
        service_tiers,
        supports_reasoning,
        reasoning_disable_supported,
        reasoning_controls,
    }
}

fn aggregate_min_context_window(vendors: &BTreeMap<CompactStr, MergeVendorModelInfo>) -> Option<u32> {
    vendors.values().filter_map(|vendor| vendor.context_window).min()
}

fn aggregate_min_max_output_tokens(vendors: &BTreeMap<CompactStr, MergeVendorModelInfo>) -> Option<u32> {
    vendors.values().filter_map(|vendor| vendor.max_output_tokens).min()
}

fn all_vendors_support(
    vendors: &BTreeMap<CompactStr, MergeVendorModelInfo>,
    predicate: impl Fn(&MergeVendorModelInfo) -> bool,
) -> bool {
    !vendors.is_empty() && vendors.values().all(predicate)
}

fn aggregate_service_tiers(vendors: &BTreeMap<CompactStr, MergeVendorModelInfo>) -> Vec<MergeCatalogServiceTier> {
    let mut intersection: Option<BTreeSet<MergeCatalogServiceTier>> = None;

    for vendor in vendors.values() {
        let tiers: BTreeSet<MergeCatalogServiceTier> = vendor
            .service_tiers
            .iter()
            .copied()
            .map(MergeCatalogServiceTier::from)
            .collect();

        intersection = Some(match intersection {
            None => tiers,
            Some(current) => current.intersection(&tiers).copied().collect(),
        });
    }

    intersection.map(|tiers| tiers.into_iter().collect()).unwrap_or_default()
}

fn aggregate_reasoning_controls(vendors: &BTreeMap<CompactStr, MergeVendorModelInfo>) -> Vec<String> {
    if !all_vendors_support(vendors, |vendor| vendor.capabilities.supports_reasoning()) {
        return Vec::new();
    }

    let mut controls = BTreeSet::new();
    for vendor in vendors.values() {
        controls.extend(
            vendor
                .capabilities
                .reasoning_controls()
                .into_iter()
                .map(|control| control.to_string()),
        );
    }
    controls.into_iter().collect()
}

#[cfg(test)]
mod tests;