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")]
417 pub responses_store: Option<bool>,
418
419 #[serde(default, skip_serializing_if = "Vec::is_empty")]
422 pub responses_include: Vec<String>,
423
424 #[serde(default, skip_serializing_if = "Option::is_none")]
434 pub service_tier: Option<OpenAIServiceTier>,
435
436 #[serde(default)]
438 pub manual_compaction: OpenAIManualCompactionConfig,
439
440 #[serde(default)]
442 pub hosted_shell: OpenAIHostedShellConfig,
443
444 #[serde(default)]
446 pub tool_search: OpenAIToolSearchConfig,
447}
448
449#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
451#[derive(Debug, Clone, Deserialize, Serialize)]
452pub struct AnthropicConfig {
453 #[serde(default, skip_serializing_if = "Option::is_none")]
455 pub speed: Option<super::AnthropicSpeed>,
456
457 #[serde(default = "default_extended_thinking_enabled")]
466 pub extended_thinking_enabled: bool,
467
468 #[serde(default = "default_interleaved_thinking_beta")]
470 pub interleaved_thinking_beta: String,
471
472 #[serde(default = "default_interleaved_thinking_budget_tokens")]
478 pub interleaved_thinking_budget_tokens: u32,
479
480 #[serde(default = "default_interleaved_thinking_type")]
482 pub interleaved_thinking_type_enabled: String,
483
484 #[serde(default)]
486 pub tool_search: ToolSearchConfig,
487
488 #[serde(default)]
490 pub memory: AnthropicMemoryConfig,
491
492 #[serde(default, skip_serializing_if = "Option::is_none")]
500 pub effort: Option<ReasoningEffortLevel>,
501
502 #[serde(default)]
508 pub task_budget_tokens: Option<u32>,
509
510 #[serde(default = "default_task_budget_beta")]
512 pub task_budget_beta: String,
513
514 #[serde(default)]
525 pub thinking_display: Option<ThinkingDisplayMode>,
526
527 #[serde(default = "default_count_tokens_enabled")]
531 pub count_tokens_enabled: bool,
532
533 #[serde(default)]
537 pub advisor: AdvisorConfig,
538
539 #[serde(default)]
549 pub fallbacks: AnthropicFallbacks,
550}
551
552#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
554#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
555#[serde(rename_all = "lowercase")]
556pub enum AnthropicFallbackMode {
557 #[default]
559 Default,
560 Off,
562}
563
564#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
566#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
567pub struct AnthropicFallbackTarget {
568 pub model: String,
570 #[serde(default, skip_serializing_if = "Option::is_none")]
572 pub max_tokens: Option<u32>,
573}
574
575#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
578#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
579#[serde(untagged)]
580pub enum AnthropicFallbacks {
581 Mode(AnthropicFallbackMode),
582 Models(Vec<AnthropicFallbackTarget>),
583}
584
585impl Default for AnthropicFallbacks {
586 fn default() -> Self {
587 Self::Mode(AnthropicFallbackMode::Default)
588 }
589}
590
591impl AnthropicFallbacks {
592 pub const MAX_MODELS: usize = 3;
594
595 pub fn validation_error(&self, field_path: &str) -> Option<String> {
598 let Self::Models(models) = self else {
599 return None;
600 };
601 Self::entries_validation_error(
602 field_path,
603 models.iter().map(|target| (target.model.as_str(), target.max_tokens)),
604 )
605 }
606
607 pub fn entries_validation_error<'a>(
612 field_path: &str,
613 entries: impl ExactSizeIterator<Item = (&'a str, Option<u32>)>,
614 ) -> Option<String> {
615 let len = entries.len();
616 if len == 0 || len > Self::MAX_MODELS {
617 return Some(format!(
618 "`{field_path}` must list between 1 and {} fallback models, or be \"default\" or \"off\".",
619 Self::MAX_MODELS
620 ));
621 }
622 let mut seen = std::collections::HashSet::with_capacity(len);
623 for (model, max_tokens) in entries {
624 let model = model.trim();
625 if model.is_empty() {
626 return Some(format!("`{field_path}` entries must set a non-empty `model`."));
627 }
628 if !seen.insert(model) {
629 return Some(format!("`{field_path}` lists `{model}` more than once; entries must be distinct."));
630 }
631 if max_tokens == Some(0) {
632 return Some(format!("`{field_path}` entry `{model}` must use a positive `max_tokens`."));
633 }
634 }
635 None
636 }
637}
638
639impl Default for AnthropicConfig {
640 fn default() -> Self {
641 Self {
642 speed: None,
643 extended_thinking_enabled: default_extended_thinking_enabled(),
644 interleaved_thinking_beta: default_interleaved_thinking_beta(),
645 interleaved_thinking_budget_tokens: default_interleaved_thinking_budget_tokens(),
646 interleaved_thinking_type_enabled: default_interleaved_thinking_type(),
647 tool_search: ToolSearchConfig::default(),
648 memory: AnthropicMemoryConfig::default(),
649 effort: None,
650 task_budget_tokens: None,
651 task_budget_beta: default_task_budget_beta(),
652 thinking_display: None,
653 count_tokens_enabled: default_count_tokens_enabled(),
654 advisor: AdvisorConfig::default(),
655 fallbacks: AnthropicFallbacks::default(),
656 }
657 }
658}
659
660#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
661#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
662pub struct AnthropicMemoryConfig {
663 #[serde(default)]
664 pub enabled: bool,
665}
666
667#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
673#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
674pub struct OpenRouterAttributionConfig {
675 #[serde(default)]
677 pub enabled: bool,
678
679 #[serde(default, skip_serializing_if = "Option::is_none")]
681 pub referer: Option<String>,
682
683 #[serde(default, skip_serializing_if = "Option::is_none")]
685 pub title: Option<String>,
686}
687
688impl OpenRouterAttributionConfig {
689 pub const DEFAULT_REFERER: &'static str = "https://github.com/vinhnx/vtcode";
691 pub const DEFAULT_TITLE: &'static str = "VT Code";
693 pub const CATEGORIES: &'static str = "cli-agent";
695
696 pub fn is_enabled(&self) -> bool {
703 vtcode_commons::utils::parse_env_bool(crate::constants::env_vars::VTCODE_OPENROUTER_ATTRIBUTION, self.enabled)
704 }
705
706 fn header_safe(value: &str) -> bool {
709 !value.is_empty() && value.len() <= 512 && !value.bytes().any(|b| b < 0x20 || b == 0x7F)
710 }
711
712 pub fn resolved_referer(&self) -> &str {
715 self.referer
716 .as_deref()
717 .map(str::trim)
718 .filter(|s| Self::header_safe(s))
719 .unwrap_or(Self::DEFAULT_REFERER)
720 }
721
722 pub fn resolved_title(&self) -> &str {
725 self.title
726 .as_deref()
727 .map(str::trim)
728 .filter(|s| Self::header_safe(s))
729 .unwrap_or(Self::DEFAULT_TITLE)
730 }
731}
732
733#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
735#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, Eq)]
736pub struct OpenRouterConfig {
737 #[serde(default)]
739 pub attribution: OpenRouterAttributionConfig,
740}
741
742#[inline]
743fn default_count_tokens_enabled() -> bool {
744 false
745}
746
747#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
749#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
750#[serde(rename_all = "lowercase")]
751pub enum ToolSearchAlgorithm {
752 #[default]
754 Regex,
755 Bm25,
757 #[serde(other)]
759 Unknown,
760}
761
762impl ToolSearchAlgorithm {
763 fn as_str(&self) -> &str {
765 match self {
766 Self::Regex => "regex",
767 Self::Bm25 => "bm25",
768 Self::Unknown => "unknown",
769 }
770 }
771}
772
773impl std::fmt::Display for ToolSearchAlgorithm {
774 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
775 f.write_str(self.as_str())
776 }
777}
778
779#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
782#[derive(Debug, Clone, Deserialize, Serialize)]
783pub struct ToolSearchConfig {
784 #[serde(default = "default_tool_search_enabled")]
786 pub enabled: bool,
787
788 #[serde(default = "default_tool_search_algorithm")]
790 pub algorithm: ToolSearchAlgorithm,
791
792 #[serde(default = "default_defer_by_default")]
794 pub defer_by_default: bool,
795
796 #[serde(default = "default_max_results")]
798 max_results: u32,
799
800 #[serde(default)]
802 pub always_available_tools: Vec<String>,
803}
804
805impl Default for ToolSearchConfig {
806 fn default() -> Self {
807 Self {
808 enabled: default_tool_search_enabled(),
809 algorithm: default_tool_search_algorithm(),
810 defer_by_default: default_defer_by_default(),
811 max_results: default_max_results(),
812 always_available_tools: vec![],
813 }
814 }
815}
816
817#[inline]
818fn default_tool_search_enabled() -> bool {
819 true
820}
821
822#[inline]
823fn default_tool_search_algorithm() -> ToolSearchAlgorithm {
824 ToolSearchAlgorithm::Regex
825}
826
827#[inline]
828fn default_defer_by_default() -> bool {
829 true
830}
831
832#[inline]
833fn default_max_results() -> u32 {
834 5
835}
836
837#[inline]
838fn default_extended_thinking_enabled() -> bool {
839 true
840}
841
842mod serde_defaults;
843use serde_defaults::{
844 default_interleaved_thinking_beta, default_interleaved_thinking_budget_tokens, default_interleaved_thinking_type,
845 default_task_budget_beta,
846};
847
848#[cfg(test)]
849mod tests;