1use crate::core::advisor::AdvisorConfig;
2use serde::{Deserialize, Serialize};
3use vtcode_commons::reasoning::ReasoningEffortLevel;
4
5#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
8#[serde(rename_all = "lowercase")]
9pub enum ThinkingDisplayMode {
10 Summarized,
12 Omitted,
14 Updates,
19 #[serde(other)]
21 Unknown,
22}
23
24impl ThinkingDisplayMode {
25 pub fn as_str(self) -> &'static str {
27 match self {
28 Self::Summarized => "summarized",
29 Self::Omitted => "omitted",
30 Self::Updates => "updates",
31 Self::Unknown => "unknown",
32 }
33 }
34}
35
36#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
39#[serde(rename_all = "lowercase")]
40pub enum OpenAIServiceTier {
41 Flex,
42 Priority,
43 Ultrafast,
44}
45
46impl OpenAIServiceTier {
47 pub const fn as_str(self) -> &'static str {
48 match self {
49 Self::Flex => "flex",
50 Self::Priority => "priority",
51 Self::Ultrafast => "ultrafast",
52 }
53 }
54
55 pub fn parse(value: &str) -> Option<Self> {
56 let normalized = value.trim();
57 if normalized.eq_ignore_ascii_case("flex") {
58 Some(Self::Flex)
59 } else if normalized.eq_ignore_ascii_case("priority") {
60 Some(Self::Priority)
61 } else if normalized.eq_ignore_ascii_case("ultrafast") {
62 Some(Self::Ultrafast)
63 } else {
64 None
65 }
66 }
67}
68
69#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Default)]
72#[serde(rename_all = "snake_case")]
73pub enum OpenAIHostedShellEnvironment {
74 #[default]
75 ContainerAuto,
76 ContainerReference,
77}
78
79impl OpenAIHostedShellEnvironment {
80 pub const fn as_str(self) -> &'static str {
81 match self {
82 Self::ContainerAuto => "container_auto",
83 Self::ContainerReference => "container_reference",
84 }
85 }
86}
87
88impl OpenAIHostedShellEnvironment {
89 const fn uses_container_reference(self) -> bool {
90 matches!(self, Self::ContainerReference)
91 }
92}
93
94#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Default)]
97#[serde(rename_all = "snake_case")]
98pub enum OpenAIHostedShellNetworkPolicyType {
99 #[default]
100 Disabled,
101 Allowlist,
102}
103
104impl OpenAIHostedShellNetworkPolicyType {
105 pub const fn as_str(self) -> &'static str {
106 match self {
107 Self::Disabled => "disabled",
108 Self::Allowlist => "allowlist",
109 }
110 }
111}
112
113#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
115#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
116pub struct OpenAIHostedShellDomainSecret {
117 pub domain: String,
118 pub name: String,
119 pub value: String,
120}
121
122impl OpenAIHostedShellDomainSecret {
123 fn validation_error(&self, index: usize) -> Option<String> {
124 let base = format!("provider.openai.hosted_shell.network_policy.domain_secrets[{index}]");
125
126 if self.domain.trim().is_empty() {
127 return Some(format!("`{base}.domain` must not be empty when set."));
128 }
129 if self.name.trim().is_empty() {
130 return Some(format!("`{base}.name` must not be empty when set."));
131 }
132 if self.value.trim().is_empty() {
133 return Some(format!("`{base}.value` must not be empty when set."));
134 }
135
136 None
137 }
138}
139
140#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
142#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Default)]
143pub struct OpenAIHostedShellNetworkPolicy {
144 #[serde(rename = "type", default)]
145 pub policy_type: OpenAIHostedShellNetworkPolicyType,
146
147 #[serde(default, skip_serializing_if = "Vec::is_empty")]
148 pub allowed_domains: Vec<String>,
149
150 #[serde(default, skip_serializing_if = "Vec::is_empty")]
151 pub domain_secrets: Vec<OpenAIHostedShellDomainSecret>,
152}
153
154impl OpenAIHostedShellNetworkPolicy {
155 pub const fn is_allowlist(&self) -> bool {
156 matches!(self.policy_type, OpenAIHostedShellNetworkPolicyType::Allowlist)
157 }
158
159 fn first_invalid_message(&self) -> Option<String> {
160 match self.policy_type {
161 OpenAIHostedShellNetworkPolicyType::Disabled => {
162 if !self.allowed_domains.is_empty() || !self.domain_secrets.is_empty() {
163 return Some(
164 "`provider.openai.hosted_shell.network_policy.allowed_domains` and `provider.openai.hosted_shell.network_policy.domain_secrets` require `provider.openai.hosted_shell.network_policy.type = \"allowlist\"`."
165 .to_string(),
166 );
167 }
168 }
169 OpenAIHostedShellNetworkPolicyType::Allowlist => {
170 if let Some(index) = self.allowed_domains.iter().position(|value| value.trim().is_empty()) {
171 return Some(format!(
172 "`provider.openai.hosted_shell.network_policy.allowed_domains[{index}]` must not be empty when set."
173 ));
174 }
175
176 if self.allowed_domains.is_empty() {
177 return Some(
178 "`provider.openai.hosted_shell.network_policy.allowed_domains` must include at least one domain when `provider.openai.hosted_shell.network_policy.type = \"allowlist\"`."
179 .to_string(),
180 );
181 }
182
183 for (index, secret) in self.domain_secrets.iter().enumerate() {
184 if let Some(message) = secret.validation_error(index) {
185 return Some(message);
186 }
187
188 let secret_domain = secret.domain.trim();
189 if !self
190 .allowed_domains
191 .iter()
192 .any(|domain| domain.trim().eq_ignore_ascii_case(secret_domain))
193 {
194 return Some(format!(
195 "`provider.openai.hosted_shell.network_policy.domain_secrets[{index}].domain` must also appear in `provider.openai.hosted_shell.network_policy.allowed_domains`."
196 ));
197 }
198 }
199 }
200 }
201
202 None
203 }
204}
205
206#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
208#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Default)]
209#[serde(rename_all = "lowercase")]
210pub enum OpenAIHostedSkillVersionKeyword {
211 #[default]
212 Latest,
213}
214
215#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
217#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
218#[serde(untagged)]
219pub enum OpenAIHostedSkillVersion {
220 Latest(OpenAIHostedSkillVersionKeyword),
221 Number(u64),
222 String(String),
223}
224
225impl Default for OpenAIHostedSkillVersion {
226 fn default() -> Self {
227 Self::Latest(OpenAIHostedSkillVersionKeyword::Latest)
228 }
229}
230
231impl OpenAIHostedSkillVersion {
232 fn validation_error(&self, field_path: &str) -> Option<String> {
233 match self {
234 Self::String(value) if value.trim().is_empty() => {
235 Some(format!("`{field_path}` must not be empty when set."))
236 }
237 _ => None,
238 }
239 }
240}
241
242#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
244#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
245#[serde(tag = "type", rename_all = "snake_case")]
246pub enum OpenAIHostedSkill {
247 SkillReference {
249 skill_id: String,
250 #[serde(default)]
251 version: OpenAIHostedSkillVersion,
252 },
253 Inline {
255 bundle_b64: String,
256 #[serde(skip_serializing_if = "Option::is_none")]
257 sha256: Option<String>,
258 },
259}
260
261impl OpenAIHostedSkill {
262 fn validation_error(&self, index: usize) -> Option<String> {
263 match self {
264 Self::SkillReference { skill_id, version } => {
265 let skill_id_path = format!("provider.openai.hosted_shell.skills[{index}].skill_id");
266 if skill_id.trim().is_empty() {
267 return Some(format!("`{skill_id_path}` must not be empty when `type = \"skill_reference\"`."));
268 }
269
270 let version_path = format!("provider.openai.hosted_shell.skills[{index}].version");
271 version.validation_error(&version_path)
272 }
273 Self::Inline { bundle_b64, .. } => {
274 let bundle_path = format!("provider.openai.hosted_shell.skills[{index}].bundle_b64");
275 if bundle_b64.trim().is_empty() {
276 return Some(format!("`{bundle_path}` must not be empty when `type = \"inline\"`."));
277 }
278 None
279 }
280 }
281 }
282}
283
284#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
286#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Default)]
287pub struct OpenAIHostedShellConfig {
288 #[serde(default)]
290 pub enabled: bool,
291
292 #[serde(default)]
294 pub environment: OpenAIHostedShellEnvironment,
295
296 #[serde(default, skip_serializing_if = "Option::is_none")]
298 pub container_id: Option<String>,
299
300 #[serde(default, skip_serializing_if = "Vec::is_empty")]
302 pub file_ids: Vec<String>,
303
304 #[serde(default, skip_serializing_if = "Vec::is_empty")]
306 pub skills: Vec<OpenAIHostedSkill>,
307
308 #[serde(default)]
310 pub network_policy: OpenAIHostedShellNetworkPolicy,
311}
312
313impl OpenAIHostedShellConfig {
314 fn container_id_ref(&self) -> Option<&str> {
315 self.container_id.as_deref().map(str::trim).filter(|value| !value.is_empty())
316 }
317
318 pub const fn uses_container_reference(&self) -> bool {
319 self.environment.uses_container_reference()
320 }
321
322 pub fn first_invalid_skill_message(&self) -> Option<String> {
323 if self.uses_container_reference() {
324 return None;
325 }
326
327 self.skills
328 .iter()
329 .enumerate()
330 .find_map(|(index, skill)| skill.validation_error(index))
331 }
332
333 fn has_valid_skill_mounts(&self) -> bool {
334 self.first_invalid_skill_message().is_none()
335 }
336
337 pub fn first_invalid_network_policy_message(&self) -> Option<String> {
338 if self.uses_container_reference() {
339 return None;
340 }
341
342 self.network_policy.first_invalid_message()
343 }
344
345 fn has_valid_network_policy(&self) -> bool {
346 self.first_invalid_network_policy_message().is_none()
347 }
348
349 pub fn has_valid_reference_target(&self) -> bool {
350 !self.uses_container_reference() || self.container_id_ref().is_some()
351 }
352
353 pub fn is_valid_for_runtime(&self) -> bool {
354 self.has_valid_reference_target() && self.has_valid_skill_mounts() && self.has_valid_network_policy()
355 }
356}
357
358#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
360#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
361pub struct OpenAIToolSearchConfig {
362 #[serde(default = "default_tool_search_enabled")]
364 pub enabled: bool,
365
366 #[serde(default = "default_defer_by_default")]
368 pub defer_by_default: bool,
369
370 #[serde(default)]
372 pub always_available_tools: Vec<String>,
373}
374
375impl Default for OpenAIToolSearchConfig {
376 fn default() -> Self {
377 Self {
378 enabled: default_tool_search_enabled(),
379 defer_by_default: default_defer_by_default(),
380 always_available_tools: Vec::new(),
381 }
382 }
383}
384
385#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
387#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Default)]
388pub struct OpenAIManualCompactionConfig {
389 #[serde(default, skip_serializing_if = "Option::is_none")]
391 pub instructions: Option<String>,
392}
393
394#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
396#[derive(Debug, Clone, Deserialize, Serialize, Default)]
397pub struct OpenAIConfig {
398 #[serde(default)]
407 pub websocket_mode: bool,
408
409 #[serde(default, skip_serializing_if = "Option::is_none")]
412 pub responses_store: Option<bool>,
413
414 #[serde(default, skip_serializing_if = "Vec::is_empty")]
417 pub responses_include: Vec<String>,
418
419 #[serde(default, skip_serializing_if = "Option::is_none")]
429 pub service_tier: Option<OpenAIServiceTier>,
430
431 #[serde(default)]
433 pub manual_compaction: OpenAIManualCompactionConfig,
434
435 #[serde(default)]
437 pub hosted_shell: OpenAIHostedShellConfig,
438
439 #[serde(default)]
441 pub tool_search: OpenAIToolSearchConfig,
442}
443
444#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
446#[derive(Debug, Clone, Deserialize, Serialize)]
447pub struct AnthropicConfig {
448 #[serde(default, skip_serializing_if = "Option::is_none")]
450 pub speed: Option<super::AnthropicSpeed>,
451
452 #[serde(default = "default_extended_thinking_enabled")]
461 pub extended_thinking_enabled: bool,
462
463 #[serde(default = "default_interleaved_thinking_beta")]
465 pub interleaved_thinking_beta: String,
466
467 #[serde(default = "default_interleaved_thinking_budget_tokens")]
473 pub interleaved_thinking_budget_tokens: u32,
474
475 #[serde(default = "default_interleaved_thinking_type")]
477 pub interleaved_thinking_type_enabled: String,
478
479 #[serde(default)]
481 pub tool_search: ToolSearchConfig,
482
483 #[serde(default)]
485 pub memory: AnthropicMemoryConfig,
486
487 #[serde(default, skip_serializing_if = "Option::is_none")]
495 pub effort: Option<ReasoningEffortLevel>,
496
497 #[serde(default)]
503 pub task_budget_tokens: Option<u32>,
504
505 #[serde(default = "default_task_budget_beta")]
507 pub task_budget_beta: String,
508
509 #[serde(default)]
520 pub thinking_display: Option<ThinkingDisplayMode>,
521
522 #[serde(default = "default_count_tokens_enabled")]
526 pub count_tokens_enabled: bool,
527
528 #[serde(default)]
532 pub advisor: AdvisorConfig,
533
534 #[serde(default)]
544 pub fallbacks: AnthropicFallbacks,
545}
546
547#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
549#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
550#[serde(rename_all = "lowercase")]
551pub enum AnthropicFallbackMode {
552 #[default]
554 Default,
555 Off,
557}
558
559#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
561#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
562pub struct AnthropicFallbackTarget {
563 pub model: String,
565 #[serde(default, skip_serializing_if = "Option::is_none")]
567 pub max_tokens: Option<u32>,
568}
569
570#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
573#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
574#[serde(untagged)]
575pub enum AnthropicFallbacks {
576 Mode(AnthropicFallbackMode),
577 Models(Vec<AnthropicFallbackTarget>),
578}
579
580impl Default for AnthropicFallbacks {
581 fn default() -> Self {
582 Self::Mode(AnthropicFallbackMode::Default)
583 }
584}
585
586impl AnthropicFallbacks {
587 pub const MAX_MODELS: usize = 3;
589
590 pub fn validation_error(&self, field_path: &str) -> Option<String> {
593 let Self::Models(models) = self else {
594 return None;
595 };
596 Self::entries_validation_error(
597 field_path,
598 models.iter().map(|target| (target.model.as_str(), target.max_tokens)),
599 )
600 }
601
602 pub fn entries_validation_error<'a>(
607 field_path: &str,
608 entries: impl ExactSizeIterator<Item = (&'a str, Option<u32>)>,
609 ) -> Option<String> {
610 let len = entries.len();
611 if len == 0 || len > Self::MAX_MODELS {
612 return Some(format!(
613 "`{field_path}` must list between 1 and {} fallback models, or be \"default\" or \"off\".",
614 Self::MAX_MODELS
615 ));
616 }
617 let mut seen = std::collections::HashSet::with_capacity(len);
618 for (model, max_tokens) in entries {
619 let model = model.trim();
620 if model.is_empty() {
621 return Some(format!("`{field_path}` entries must set a non-empty `model`."));
622 }
623 if !seen.insert(model) {
624 return Some(format!("`{field_path}` lists `{model}` more than once; entries must be distinct."));
625 }
626 if max_tokens == Some(0) {
627 return Some(format!("`{field_path}` entry `{model}` must use a positive `max_tokens`."));
628 }
629 }
630 None
631 }
632}
633
634impl Default for AnthropicConfig {
635 fn default() -> Self {
636 Self {
637 speed: None,
638 extended_thinking_enabled: default_extended_thinking_enabled(),
639 interleaved_thinking_beta: default_interleaved_thinking_beta(),
640 interleaved_thinking_budget_tokens: default_interleaved_thinking_budget_tokens(),
641 interleaved_thinking_type_enabled: default_interleaved_thinking_type(),
642 tool_search: ToolSearchConfig::default(),
643 memory: AnthropicMemoryConfig::default(),
644 effort: None,
645 task_budget_tokens: None,
646 task_budget_beta: default_task_budget_beta(),
647 thinking_display: None,
648 count_tokens_enabled: default_count_tokens_enabled(),
649 advisor: AdvisorConfig::default(),
650 fallbacks: AnthropicFallbacks::default(),
651 }
652 }
653}
654
655#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
656#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
657pub struct AnthropicMemoryConfig {
658 #[serde(default)]
659 pub enabled: bool,
660}
661
662#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
668#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
669pub struct OpenRouterAttributionConfig {
670 #[serde(default)]
672 pub enabled: bool,
673
674 #[serde(default, skip_serializing_if = "Option::is_none")]
676 pub referer: Option<String>,
677
678 #[serde(default, skip_serializing_if = "Option::is_none")]
680 pub title: Option<String>,
681}
682
683impl OpenRouterAttributionConfig {
684 pub const DEFAULT_REFERER: &'static str = "https://github.com/vinhnx/vtcode";
686 pub const DEFAULT_TITLE: &'static str = "VT Code";
688 pub const CATEGORIES: &'static str = "cli-agent";
690
691 pub fn is_enabled(&self) -> bool {
698 vtcode_commons::utils::parse_env_bool(crate::constants::env_vars::VTCODE_OPENROUTER_ATTRIBUTION, self.enabled)
699 }
700
701 fn header_safe(value: &str) -> bool {
704 !value.is_empty() && value.len() <= 512 && !value.bytes().any(|b| b < 0x20 || b == 0x7F)
705 }
706
707 pub fn resolved_referer(&self) -> &str {
710 self.referer
711 .as_deref()
712 .map(str::trim)
713 .filter(|s| Self::header_safe(s))
714 .unwrap_or(Self::DEFAULT_REFERER)
715 }
716
717 pub fn resolved_title(&self) -> &str {
720 self.title
721 .as_deref()
722 .map(str::trim)
723 .filter(|s| Self::header_safe(s))
724 .unwrap_or(Self::DEFAULT_TITLE)
725 }
726}
727
728#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
730#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, Eq)]
731pub struct OpenRouterConfig {
732 #[serde(default)]
734 pub attribution: OpenRouterAttributionConfig,
735}
736
737#[inline]
738fn default_count_tokens_enabled() -> bool {
739 false
740}
741
742#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
744#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
745#[serde(rename_all = "lowercase")]
746pub enum ToolSearchAlgorithm {
747 #[default]
749 Regex,
750 Bm25,
752 #[serde(other)]
754 Unknown,
755}
756
757impl ToolSearchAlgorithm {
758 fn as_str(&self) -> &str {
760 match self {
761 Self::Regex => "regex",
762 Self::Bm25 => "bm25",
763 Self::Unknown => "unknown",
764 }
765 }
766}
767
768impl std::fmt::Display for ToolSearchAlgorithm {
769 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
770 f.write_str(self.as_str())
771 }
772}
773
774#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
777#[derive(Debug, Clone, Deserialize, Serialize)]
778pub struct ToolSearchConfig {
779 #[serde(default = "default_tool_search_enabled")]
781 pub enabled: bool,
782
783 #[serde(default = "default_tool_search_algorithm")]
785 pub algorithm: ToolSearchAlgorithm,
786
787 #[serde(default = "default_defer_by_default")]
789 pub defer_by_default: bool,
790
791 #[serde(default = "default_max_results")]
793 max_results: u32,
794
795 #[serde(default)]
797 pub always_available_tools: Vec<String>,
798}
799
800impl Default for ToolSearchConfig {
801 fn default() -> Self {
802 Self {
803 enabled: default_tool_search_enabled(),
804 algorithm: default_tool_search_algorithm(),
805 defer_by_default: default_defer_by_default(),
806 max_results: default_max_results(),
807 always_available_tools: vec![],
808 }
809 }
810}
811
812#[inline]
813fn default_tool_search_enabled() -> bool {
814 true
815}
816
817#[inline]
818fn default_tool_search_algorithm() -> ToolSearchAlgorithm {
819 ToolSearchAlgorithm::Regex
820}
821
822#[inline]
823fn default_defer_by_default() -> bool {
824 true
825}
826
827#[inline]
828fn default_max_results() -> u32 {
829 5
830}
831
832#[inline]
833fn default_extended_thinking_enabled() -> bool {
834 true
835}
836
837mod serde_defaults;
838use serde_defaults::{
839 default_interleaved_thinking_beta, default_interleaved_thinking_budget_tokens, default_interleaved_thinking_type,
840 default_task_budget_beta,
841};
842
843#[cfg(test)]
844mod tests;