vtcode-config 0.123.2

Config loader components shared across VT Code and downstream adopters
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use indexmap::IndexMap;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;

use crate::{CapabilityEntry, EntryData, PricingSpec, Provider};

// ---------------------------------------------------------------------------
// Helper: dynamic Ident creation
// ---------------------------------------------------------------------------

fn ident(name: &str) -> Ident {
    Ident::new(name, Span::call_site())
}

fn optional_f64_tokens(value: Option<f64>) -> TokenStream {
    match value {
        Some(v) => {
            let lit = proc_macro2::Literal::f64_suffixed(v);
            quote! { Some(#lit) }
        }
        None => quote! { None },
    }
}

// ---------------------------------------------------------------------------
// 1. openrouter_constants.rs
// ---------------------------------------------------------------------------

pub fn generate_openrouter_constants(
    entries: &[EntryData],
    provider: &Provider,
) -> anyhow::Result<String> {
    // Per-model constants
    let const_defs: TokenStream = entries
        .iter()
        .map(|entry| {
            let name = ident(&entry.const_name);
            let id = &entry.id;
            quote! {
                pub const #name: &str = #id;
            }
        })
        .collect();

    // DEFAULT_MODEL
    let default_id = provider.default_model.as_ref().ok_or_else(|| {
        anyhow::anyhow!("openrouter.default_model is missing in docs/models.json")
    })?;
    let default_entry = entries
        .iter()
        .find(|e| &e.id == default_id)
        .ok_or_else(|| {
            anyhow::anyhow!(
                "Default OpenRouter model '{default_id}' is not declared in vtcode metadata"
            )
        })?;
    let default_const = ident(&default_entry.const_name);

    // SUPPORTED_MODELS
    let supported_models: TokenStream = entries
        .iter()
        .map(|entry| {
            let name = ident(&entry.const_name);
            quote! { #name, }
        })
        .collect();

    // REASONING_MODELS
    let reasoning_models: TokenStream = entries
        .iter()
        .filter(|e| e.reasoning)
        .map(|entry| {
            let name = ident(&entry.const_name);
            quote! { #name, }
        })
        .collect();

    // TOOL_UNAVAILABLE_MODELS
    let tool_unavailable_models: TokenStream = entries
        .iter()
        .filter(|e| !e.tool_call)
        .map(|entry| {
            let name = ident(&entry.const_name);
            quote! { #name, }
        })
        .collect();

    // Vendor modules
    let mut vendor_map: IndexMap<String, Vec<&EntryData>> = IndexMap::new();
    for entry in entries {
        vendor_map
            .entry(entry.vendor.clone())
            .or_default()
            .push(entry);
    }

    let vendor_modules: TokenStream = vendor_map
        .iter()
        .map(|(vendor, vendor_entries)| {
            let mod_name = ident(&super::to_module_name(vendor));
            let model_refs: TokenStream = vendor_entries
                .iter()
                .map(|entry| {
                    let name = ident(&entry.const_name);
                    quote! { super::super::#name, }
                })
                .collect();
            quote! {
                pub mod #mod_name {
                    pub const MODELS: &[&str] = &[
                        #model_refs
                    ];
                }
            }
        })
        .collect();

    let output = quote! {
        #const_defs

        pub const DEFAULT_MODEL: &str = #default_const;

        pub const SUPPORTED_MODELS: &[&str] = &[
            #supported_models
        ];

        pub const REASONING_MODELS: &[&str] = &[
            #reasoning_models
        ];

        pub const TOOL_UNAVAILABLE_MODELS: &[&str] = &[
            #tool_unavailable_models
        ];

        pub mod vendor {
            #vendor_modules
        }
    };

    Ok(output.to_string())
}

// ---------------------------------------------------------------------------
// 2. openrouter_metadata.rs
// ---------------------------------------------------------------------------

pub fn generate_openrouter_metadata(entries: &[EntryData]) -> String {
    // ENTRIES array items
    let entry_literals: TokenStream = entries.iter().map(openrouter_entry_literal).collect();

    // VendorModels
    let mut vendor_map: IndexMap<String, Vec<&EntryData>> = IndexMap::new();
    for entry in entries {
        vendor_map
            .entry(entry.vendor.clone())
            .or_default()
            .push(entry);
    }

    let vendor_models: TokenStream = vendor_map
        .iter()
        .map(|(vendor, vendor_entries)| {
            let vendor_str = vendor.as_str();
            let model_variants: TokenStream = vendor_entries
                .iter()
                .map(|entry| {
                    let variant = ident(&entry.variant);
                    quote! { super::ModelId::#variant, }
                })
                .collect();
            quote! {
                VendorModels {
                    vendor: #vendor_str,
                    models: &[
                        #model_variants
                    ],
                },
            }
        })
        .collect();

    // metadata_for() match arms
    let metadata_match_arms: TokenStream = entries
        .iter()
        .map(|entry| {
            let variant = ident(&entry.variant);
            let const_name = ident(&entry.const_name);
            let vendor = &entry.vendor;
            let display = &entry.display;
            let description = &entry.description;
            let efficient = entry.efficient;
            let top_tier = entry.top_tier;
            let generation = &entry.generation;
            let reasoning = entry.reasoning;
            let tool_call = entry.tool_call;
            quote! {
                super::ModelId::#variant => Some(super::OpenRouterMetadata {
                    id: crate::constants::models::openrouter::#const_name,
                    vendor: #vendor,
                    display: #display,
                    description: #description,
                    efficient: #efficient,
                    top_tier: #top_tier,
                    generation: #generation,
                    reasoning: #reasoning,
                    tool_call: #tool_call,
                }),
            }
        })
        .collect();

    // parse_model() match arms
    let parse_match_arms: TokenStream = entries
        .iter()
        .map(|entry| {
            let variant = ident(&entry.variant);
            let const_name = ident(&entry.const_name);
            quote! {
                crate::constants::models::openrouter::#const_name => Some(super::ModelId::#variant),
            }
        })
        .collect();

    let output = quote! {
        #[derive(Clone, Copy)]
        pub struct Entry {
            pub variant: super::ModelId,
            pub id: &'static str,
            pub vendor: &'static str,
            pub display: &'static str,
            pub description: &'static str,
            pub efficient: bool,
            pub top_tier: bool,
            pub generation: &'static str,
            pub reasoning: bool,
            pub tool_call: bool,
        }

        #[allow(dead_code)]
        pub const ENTRIES: &[Entry] = &[
            #entry_literals
        ];

        #[derive(Clone, Copy)]
        pub struct VendorModels {
            pub vendor: &'static str,
            pub models: &'static [super::ModelId],
        }

        pub const VENDOR_MODELS: &[VendorModels] = &[
            #vendor_models
        ];

        pub fn metadata_for(model: super::ModelId) -> Option<super::OpenRouterMetadata> {
            match model {
                #metadata_match_arms
                _ => None,
            }
        }

        pub fn parse_model(value: &str) -> Option<super::ModelId> {
            match value {
                #parse_match_arms
                _ => None,
            }
        }

        pub fn vendor_groups() -> &'static [VendorModels] {
            VENDOR_MODELS
        }
    };

    output.to_string()
}

fn openrouter_entry_literal(entry: &EntryData) -> TokenStream {
    let variant = ident(&entry.variant);
    let const_name = ident(&entry.const_name);
    let vendor = &entry.vendor;
    let display = &entry.display;
    let description = &entry.description;
    let efficient = entry.efficient;
    let top_tier = entry.top_tier;
    let generation = &entry.generation;
    let reasoning = entry.reasoning;
    let tool_call = entry.tool_call;
    quote! {
        Entry {
            variant: super::ModelId::#variant,
            id: crate::constants::models::openrouter::#const_name,
            vendor: #vendor,
            display: #display,
            description: #description,
            efficient: #efficient,
            top_tier: #top_tier,
            generation: #generation,
            reasoning: #reasoning,
            tool_call: #tool_call,
        },
    }
}

// ---------------------------------------------------------------------------
// 3. model_capabilities.rs
// ---------------------------------------------------------------------------

pub fn generate_model_capabilities(entries: &[CapabilityEntry]) -> String {
    // ENTRIES array
    let entry_literals: TokenStream = entries.iter().map(capability_entry_literal).collect();

    // PROVIDERS list
    let mut provider_map: IndexMap<&str, Vec<&CapabilityEntry>> = IndexMap::new();
    for entry in entries {
        provider_map.entry(&entry.provider).or_default().push(entry);
    }

    let provider_names: TokenStream = provider_map
        .keys()
        .map(|provider| {
            quote! { #provider, }
        })
        .collect();

    // metadata_for() and models_for_provider()
    let (metadata_for_fn, models_for_provider_fn) = if provider_map.is_empty() {
        (
            quote! {
                pub fn metadata_for(_provider: &str, _id: &str) -> Option<Entry> {
                    None
                }
            },
            quote! {
                pub fn models_for_provider(_provider: &str) -> Option<&'static [&'static str]> {
                    None
                }
            },
        )
    } else {
        let provider_match_arms: TokenStream = provider_map
            .iter()
            .map(|(provider, provider_entries)| {
                let id_match_arms: TokenStream = provider_entries
                    .iter()
                    .map(|entry| {
                        let id = &entry.id;
                        let literal = capability_entry_literal_without_comma(entry);
                        quote! {
                            #id => Some(#literal),
                        }
                    })
                    .collect();
                quote! {
                    #provider => match id {
                        #id_match_arms
                        _ => None,
                    },
                }
            })
            .collect();

        let models_match_arms: TokenStream = provider_map
            .iter()
            .map(|(provider, provider_entries)| {
                let model_ids: TokenStream = provider_entries
                    .iter()
                    .map(|entry| {
                        let id = &entry.id;
                        quote! { #id, }
                    })
                    .collect();
                quote! {
                    #provider => Some(&[
                        #model_ids
                    ]),
                }
            })
            .collect();

        (
            quote! {
                pub fn metadata_for(provider: &str, id: &str) -> Option<Entry> {
                    match provider {
                        #provider_match_arms
                        _ => None,
                    }
                }
            },
            quote! {
                pub fn models_for_provider(provider: &str) -> Option<&'static [&'static str]> {
                    match provider {
                        #models_match_arms
                        _ => None,
                    }
                }
            },
        )
    };

    let output = quote! {
        #[derive(Clone, Copy)]
        pub struct Pricing {
            pub input: Option<f64>,
            pub output: Option<f64>,
            pub cache_read: Option<f64>,
            pub cache_write: Option<f64>,
        }

        #[derive(Clone, Copy)]
        pub struct Entry {
            pub provider: &'static str,
            pub id: &'static str,
            pub display_name: &'static str,
            pub description: &'static str,
            pub context_window: usize,
            pub max_output_tokens: Option<usize>,
            pub reasoning: bool,
            pub tool_call: bool,
            pub vision: bool,
            pub input_modalities: &'static [&'static str],
            pub caching: bool,
            pub structured_output: bool,
            pub pricing: Pricing,
        }

        #[allow(dead_code)]
        pub const ENTRIES: &[Entry] = &[
            #entry_literals
        ];

        pub const PROVIDERS: &[&str] = &[
            #provider_names
        ];

        #metadata_for_fn

        #models_for_provider_fn
    };

    output.to_string()
}

fn capability_entry_literal(entry: &CapabilityEntry) -> TokenStream {
    let expr = capability_entry_expr(entry);
    quote! { #expr, }
}

fn capability_entry_literal_without_comma(entry: &CapabilityEntry) -> TokenStream {
    capability_entry_expr(entry)
}

fn capability_entry_expr(entry: &CapabilityEntry) -> TokenStream {
    let provider = &entry.provider;
    let id = &entry.id;
    let display_name = &entry.display_name;
    let description = &entry.description;
    let context_window = entry.context_window;
    let max_output_tokens = optional_usize_tokens(entry.max_output_tokens);
    let reasoning = entry.reasoning;
    let tool_call = entry.tool_call;
    let vision = entry.vision;
    let modalities: TokenStream = entry
        .input_modalities
        .iter()
        .map(|m| quote! { #m, })
        .collect();
    let caching = entry.caching;
    let structured_output = entry.structured_output;
    let pricing = pricing_literal(&entry.pricing);

    quote! {
        Entry {
            provider: #provider,
            id: #id,
            display_name: #display_name,
            description: #description,
            context_window: #context_window,
            max_output_tokens: #max_output_tokens,
            reasoning: #reasoning,
            tool_call: #tool_call,
            vision: #vision,
            input_modalities: &[
                #modalities
            ],
            caching: #caching,
            structured_output: #structured_output,
            pricing: #pricing,
        }
    }
}

fn pricing_literal(pricing: &PricingSpec) -> TokenStream {
    let input = optional_f64_tokens(pricing.input);
    let output = optional_f64_tokens(pricing.output);
    let cache_read = optional_f64_tokens(pricing.cache_read);
    let cache_write = optional_f64_tokens(pricing.cache_write);
    quote! {
        Pricing {
            input: #input,
            output: #output,
            cache_read: #cache_read,
            cache_write: #cache_write,
        }
    }
}

fn optional_usize_tokens(value: Option<usize>) -> TokenStream {
    match value {
        Some(v) => quote! { Some(#v) },
        None => quote! { None },
    }
}