1use crate::core::PromptCachingConfig;
4use serde::{Deserialize, Deserializer, Serialize};
5use std::collections::BTreeMap;
6use std::env;
7use std::fmt;
8use std::path::PathBuf;
9use vtcode_commons::ui_protocol::{KeyboardProtocolSettings, SessionSurface};
10
11pub use vtcode_commons::reasoning::ReasoningEffortLevel;
13
14pub use crate::root::{KeyboardProtocolConfig, KeyboardProtocolMode};
18
19#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
23#[serde(rename_all = "lowercase")]
24#[derive(Default)]
25pub enum SystemPromptMode {
26 #[default]
29 Minimal,
30 Lightweight,
33 Default,
36 Specialized,
39}
40
41impl SystemPromptMode {
42 fn as_str(self) -> &'static str {
44 match self {
45 Self::Minimal => "minimal",
46 Self::Lightweight => "lightweight",
47 Self::Default => "default",
48 Self::Specialized => "specialized",
49 }
50 }
51
52 pub fn parse(value: &str) -> Option<Self> {
54 let normalized = value.trim();
55 if normalized.eq_ignore_ascii_case("minimal") {
56 Some(Self::Minimal)
57 } else if normalized.eq_ignore_ascii_case("lightweight") {
58 Some(Self::Lightweight)
59 } else if normalized.eq_ignore_ascii_case("default") {
60 Some(Self::Default)
61 } else if normalized.eq_ignore_ascii_case("specialized") {
62 Some(Self::Specialized)
63 } else {
64 None
65 }
66 }
67
68 pub fn allowed_values() -> &'static [&'static str] {
70 &["minimal", "lightweight", "default", "specialized"]
71 }
72}
73
74impl fmt::Display for SystemPromptMode {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 f.write_str(self.as_str())
77 }
78}
79
80impl<'de> Deserialize<'de> for SystemPromptMode {
81 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
82 where
83 D: Deserializer<'de>,
84 {
85 let raw = String::deserialize(deserializer)?;
86 if let Some(parsed) = Self::parse(&raw) {
87 Ok(parsed)
88 } else {
89 Ok(Self::default())
90 }
91 }
92}
93
94#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
98#[serde(rename_all = "lowercase")]
99#[derive(Default)]
100pub enum ToolDocumentationMode {
101 Minimal,
105 #[default]
111 Progressive,
112 Full,
115}
116
117impl ToolDocumentationMode {
118 fn as_str(self) -> &'static str {
120 match self {
121 Self::Minimal => "minimal",
122 Self::Progressive => "progressive",
123 Self::Full => "full",
124 }
125 }
126
127 fn parse(value: &str) -> Option<Self> {
129 let normalized = value.trim();
130 if normalized.eq_ignore_ascii_case("minimal") {
131 Some(Self::Minimal)
132 } else if normalized.eq_ignore_ascii_case("progressive") {
133 Some(Self::Progressive)
134 } else if normalized.eq_ignore_ascii_case("full") {
135 Some(Self::Full)
136 } else {
137 None
138 }
139 }
140
141 pub fn allowed_values() -> &'static [&'static str] {
143 &["minimal", "progressive", "full"]
144 }
145}
146
147impl fmt::Display for ToolDocumentationMode {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 f.write_str(self.as_str())
150 }
151}
152
153impl<'de> Deserialize<'de> for ToolDocumentationMode {
154 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
155 where
156 D: Deserializer<'de>,
157 {
158 let raw = String::deserialize(deserializer)?;
159 if let Some(parsed) = Self::parse(&raw) {
160 Ok(parsed)
161 } else {
162 Ok(Self::default())
163 }
164 }
165}
166
167#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
172#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
173#[serde(rename_all = "snake_case")]
174#[derive(Default)]
175pub enum ShellPromptProfile {
176 #[default]
179 Auto,
180 UnixLike,
182 #[serde(rename = "powershell")]
188 PowerShell,
189}
190
191impl ShellPromptProfile {
192 fn as_str(self) -> &'static str {
194 match self {
195 Self::Auto => "auto",
196 Self::UnixLike => "unix_like",
197 Self::PowerShell => "powershell",
198 }
199 }
200
201 fn parse(value: &str) -> Option<Self> {
203 let normalized = value.trim();
204 if normalized.eq_ignore_ascii_case("auto") {
205 Some(Self::Auto)
206 } else if normalized.eq_ignore_ascii_case("unix_like")
207 || normalized.eq_ignore_ascii_case("unix-like")
208 || normalized.eq_ignore_ascii_case("unix")
209 || normalized.eq_ignore_ascii_case("posix")
210 {
211 Some(Self::UnixLike)
212 } else if normalized.eq_ignore_ascii_case("powershell")
213 || normalized.eq_ignore_ascii_case("power_shell")
214 || normalized.eq_ignore_ascii_case("windows")
215 {
216 Some(Self::PowerShell)
217 } else {
218 None
219 }
220 }
221
222 pub fn allowed_values() -> &'static [&'static str] {
224 &["auto", "unix_like", "powershell"]
225 }
226
227 pub fn resolve_for_current_platform(self) -> ResolvedShellPromptProfile {
229 self.resolve_for_platform(ShellProfilePlatform::current())
230 }
231
232 fn resolve_for_platform(self, platform: ShellProfilePlatform) -> ResolvedShellPromptProfile {
234 match self {
235 Self::Auto => match platform {
236 ShellProfilePlatform::NativeWindows => ResolvedShellPromptProfile::PowerShell,
237 ShellProfilePlatform::UnixLike | ShellProfilePlatform::Wsl => ResolvedShellPromptProfile::UnixLike,
238 },
239 Self::UnixLike => ResolvedShellPromptProfile::UnixLike,
240 Self::PowerShell => ResolvedShellPromptProfile::PowerShell,
241 }
242 }
243}
244
245impl fmt::Display for ShellPromptProfile {
246 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
247 f.write_str(self.as_str())
248 }
249}
250
251impl<'de> Deserialize<'de> for ShellPromptProfile {
252 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
253 where
254 D: Deserializer<'de>,
255 {
256 let raw = String::deserialize(deserializer)?;
257 if let Some(parsed) = Self::parse(&raw) {
258 Ok(parsed)
259 } else {
260 Ok(Self::default())
261 }
262 }
263}
264
265#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
267#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
268#[serde(rename_all = "snake_case")]
269pub enum ShellProfilePlatform {
270 UnixLike,
271 Wsl,
272 NativeWindows,
273}
274
275impl ShellProfilePlatform {
276 fn current() -> Self {
277 if cfg!(windows) {
278 Self::NativeWindows
279 } else if is_wsl_environment() {
280 Self::Wsl
281 } else {
282 Self::UnixLike
283 }
284 }
285}
286
287#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
289#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
290#[serde(rename_all = "snake_case")]
291pub enum ResolvedShellPromptProfile {
292 UnixLike,
293 #[serde(rename = "powershell")]
296 PowerShell,
297}
298
299impl ResolvedShellPromptProfile {
300 fn as_str(self) -> &'static str {
301 match self {
302 Self::UnixLike => "unix_like",
303 Self::PowerShell => "powershell",
304 }
305 }
306}
307
308impl fmt::Display for ResolvedShellPromptProfile {
309 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
310 f.write_str(self.as_str())
311 }
312}
313
314fn is_wsl_environment() -> bool {
315 if env::var_os("WSL_DISTRO_NAME").is_some() || env::var_os("WSL_INTEROP").is_some() {
316 return true;
317 }
318
319 for path in ["/proc/sys/kernel/osrelease", "/proc/version"] {
320 if let Ok(contents) = std::fs::read_to_string(path)
321 && contents.to_ascii_lowercase().contains("microsoft")
322 {
323 return true;
324 }
325 }
326
327 false
328}
329
330#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
332#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
333#[serde(rename_all = "lowercase")]
334#[derive(Default)]
335pub enum VerbosityLevel {
336 Low,
337 #[default]
338 Medium,
339 High,
340}
341
342impl VerbosityLevel {
343 pub fn as_str(self) -> &'static str {
345 match self {
346 Self::Low => "low",
347 Self::Medium => "medium",
348 Self::High => "high",
349 }
350 }
351
352 pub fn parse(value: &str) -> Option<Self> {
354 let normalized = value.trim();
355 if normalized.eq_ignore_ascii_case("low") {
356 Some(Self::Low)
357 } else if normalized.eq_ignore_ascii_case("medium") {
358 Some(Self::Medium)
359 } else if normalized.eq_ignore_ascii_case("high") {
360 Some(Self::High)
361 } else {
362 None
363 }
364 }
365
366 pub fn allowed_values() -> &'static [&'static str] {
368 &["low", "medium", "high"]
369 }
370}
371
372impl fmt::Display for VerbosityLevel {
373 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
374 f.write_str(self.as_str())
375 }
376}
377
378impl<'de> Deserialize<'de> for VerbosityLevel {
379 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
380 where
381 D: Deserializer<'de>,
382 {
383 let raw = String::deserialize(deserializer)?;
384 if let Some(parsed) = Self::parse(&raw) {
385 Ok(parsed)
386 } else {
387 Ok(Self::default())
388 }
389 }
390}
391
392#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
394#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
395#[serde(rename_all = "lowercase")]
396#[derive(Default)]
397pub enum UiSurfacePreference {
398 Auto,
399 Alternate,
400 #[default]
401 Inline,
402}
403
404impl UiSurfacePreference {
405 fn as_str(self) -> &'static str {
407 match self {
408 Self::Auto => "auto",
409 Self::Alternate => "alternate",
410 Self::Inline => "inline",
411 }
412 }
413
414 fn parse(value: &str) -> Option<Self> {
416 let normalized = value.trim();
417 if normalized.eq_ignore_ascii_case("auto") {
418 Some(Self::Auto)
419 } else if normalized.eq_ignore_ascii_case("alternate") || normalized.eq_ignore_ascii_case("alt") {
420 Some(Self::Alternate)
421 } else if normalized.eq_ignore_ascii_case("inline") {
422 Some(Self::Inline)
423 } else {
424 None
425 }
426 }
427
428 pub fn allowed_values() -> &'static [&'static str] {
430 &["auto", "alternate", "inline"]
431 }
432}
433
434impl fmt::Display for UiSurfacePreference {
435 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
436 f.write_str(self.as_str())
437 }
438}
439
440impl From<SessionSurface> for UiSurfacePreference {
446 fn from(value: SessionSurface) -> Self {
447 match value {
448 SessionSurface::Auto => Self::Auto,
449 SessionSurface::Alternate => Self::Alternate,
450 SessionSurface::Inline => Self::Inline,
451 }
452 }
453}
454
455impl From<UiSurfacePreference> for SessionSurface {
456 fn from(value: UiSurfacePreference) -> Self {
457 match value {
458 UiSurfacePreference::Auto => Self::Auto,
459 UiSurfacePreference::Alternate => Self::Alternate,
460 UiSurfacePreference::Inline => Self::Inline,
461 }
462 }
463}
464
465impl From<KeyboardProtocolSettings> for KeyboardProtocolConfig {
473 fn from(value: KeyboardProtocolSettings) -> Self {
474 Self {
475 enabled: value.enabled,
476 mode: value.mode.parse().unwrap_or_default(),
477 disambiguate_escape_codes: value.disambiguate_escape_codes,
478 report_event_types: value.report_event_types,
479 report_alternate_keys: value.report_alternate_keys,
480 report_all_keys: value.report_all_keys,
481 }
482 }
483}
484
485impl From<KeyboardProtocolConfig> for KeyboardProtocolSettings {
486 fn from(value: KeyboardProtocolConfig) -> Self {
487 Self {
488 enabled: value.enabled,
489 mode: value.mode.as_str().to_owned(),
490 disambiguate_escape_codes: value.disambiguate_escape_codes,
491 report_event_types: value.report_event_types,
492 report_alternate_keys: value.report_alternate_keys,
493 report_all_keys: value.report_all_keys,
494 }
495 }
496}
497
498impl<'de> Deserialize<'de> for UiSurfacePreference {
499 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
500 where
501 D: Deserializer<'de>,
502 {
503 let raw = String::deserialize(deserializer)?;
504 if let Some(parsed) = Self::parse(&raw) {
505 Ok(parsed)
506 } else {
507 Ok(Self::default())
508 }
509 }
510}
511
512#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
514pub enum ModelSelectionSource {
515 #[default]
517 WorkspaceConfig,
518 CliOverride,
520}
521
522#[derive(Debug, Clone)]
524pub struct AgentConfig {
525 pub model: String,
526 pub api_key: String,
527 pub provider: String,
528 pub openai_chatgpt_auth: Option<crate::auth::OpenAIChatGptAuthHandle>,
529 pub api_key_env: String,
530 pub workspace: PathBuf,
531 pub verbose: bool,
532 pub quiet: bool,
533 pub theme: String,
534 pub reasoning_effort: ReasoningEffortLevel,
535 pub ui_surface: UiSurfacePreference,
536 pub prompt_cache: PromptCachingConfig,
537 pub model_source: ModelSelectionSource,
538 pub custom_api_keys: BTreeMap<String, String>,
539 pub checkpointing_enabled: bool,
540 pub checkpointing_storage_dir: Option<PathBuf>,
541 pub checkpointing_max_snapshots: usize,
542 pub checkpointing_max_age_days: Option<u64>,
543 pub max_conversation_turns: usize,
544 pub model_behavior: Option<crate::core::ModelConfig>,
545}
546
547#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
549pub enum CapabilityLevel {
550 Basic,
552 FileReading,
554 FileListing,
556 Bash,
558 Editing,
560 CodeSearch,
562}
563
564#[derive(Debug, Clone, Serialize, Deserialize)]
566pub struct SessionInfo {
567 pub session_id: String,
568 pub start_time: u64,
569 pub total_turns: usize,
570 pub total_decisions: usize,
571 pub error_count: usize,
572}
573
574#[derive(Debug, Clone, Serialize, Deserialize)]
576pub struct ErrorInfo {
577 error_type: String,
578 message: String,
579 turn_number: usize,
580 recoverable: bool,
581 timestamp: u64,
582}
583
584#[derive(Debug, Clone, Serialize, Deserialize)]
586pub struct PerformanceMetrics {
587 pub session_duration_seconds: u64,
588 pub total_api_calls: usize,
589 pub total_tokens_used: Option<usize>,
590 pub average_response_time_ms: f64,
591 pub tool_execution_count: usize,
592 pub error_count: usize,
593 pub recovery_success_rate: f64,
594}
595
596#[derive(Debug, Clone, Serialize, Deserialize)]
598pub enum AnalysisDepth {
599 Basic,
600 Standard,
601 Deep,
602}
603
604#[derive(Debug, Clone, Serialize, Deserialize)]
606pub enum OutputFormat {
607 Text,
608 Json,
609 Html,
610}
611
612#[cfg(test)]
613mod tests {
614 use super::*;
615
616 #[test]
617 fn test_reasoning_effort_parse_and_allowed_values_include_max() {
618 assert_eq!(ReasoningEffortLevel::parse("max"), Some(ReasoningEffortLevel::Max));
619 assert_eq!(ReasoningEffortLevel::Max.as_str(), "max");
620 assert!(ReasoningEffortLevel::allowed_values().contains(&"max"));
621 }
622
623 #[test]
624 fn shell_prompt_profile_parses_aliases() {
625 assert_eq!(ShellPromptProfile::parse("auto"), Some(ShellPromptProfile::Auto));
626 assert_eq!(ShellPromptProfile::parse("unix-like"), Some(ShellPromptProfile::UnixLike));
627 assert_eq!(ShellPromptProfile::parse("posix"), Some(ShellPromptProfile::UnixLike));
628 assert_eq!(ShellPromptProfile::parse("PowerShell"), Some(ShellPromptProfile::PowerShell));
629 assert_eq!(ShellPromptProfile::parse("unknown"), None);
630 }
631
632 #[test]
633 fn ui_surface_defaults_to_inline() {
634 assert_eq!(UiSurfacePreference::default(), UiSurfacePreference::Inline);
635 assert_eq!(SessionSurface::default(), SessionSurface::from(UiSurfacePreference::default()));
636 }
637
638 #[test]
639 fn ui_surface_converts_to_and_from_tui_surface() {
640 for (preference, surface) in [
641 (UiSurfacePreference::Auto, SessionSurface::Auto),
642 (UiSurfacePreference::Alternate, SessionSurface::Alternate),
643 (UiSurfacePreference::Inline, SessionSurface::Inline),
644 ] {
645 assert_eq!(SessionSurface::from(preference), surface);
646 assert_eq!(UiSurfacePreference::from(surface), preference);
647 }
648 }
649
650 #[test]
651 fn keyboard_protocol_conversion_roundtrips_every_mode() {
652 for (mode, text) in [
653 (KeyboardProtocolMode::Default, "default"),
654 (KeyboardProtocolMode::Full, "full"),
655 (KeyboardProtocolMode::Minimal, "minimal"),
656 (KeyboardProtocolMode::Custom, "custom"),
657 ] {
658 let config = KeyboardProtocolConfig {
659 enabled: false,
660 mode,
661 disambiguate_escape_codes: true,
662 report_event_types: false,
663 report_alternate_keys: true,
664 report_all_keys: true,
665 };
666 let settings = KeyboardProtocolSettings::from(config.clone());
667 assert_eq!(settings.mode, text);
668 assert_eq!(settings.enabled, config.enabled);
669 assert_eq!(settings.disambiguate_escape_codes, config.disambiguate_escape_codes);
670 assert_eq!(settings.report_event_types, config.report_event_types);
671 assert_eq!(settings.report_alternate_keys, config.report_alternate_keys);
672 assert_eq!(settings.report_all_keys, config.report_all_keys);
673 let restored = KeyboardProtocolConfig::from(settings);
674 assert_eq!(restored.mode, mode);
675 assert_eq!(restored.enabled, config.enabled);
676 assert_eq!(restored.disambiguate_escape_codes, config.disambiguate_escape_codes);
677 assert_eq!(restored.report_event_types, config.report_event_types);
678 assert_eq!(restored.report_alternate_keys, config.report_alternate_keys);
679 assert_eq!(restored.report_all_keys, config.report_all_keys);
680 }
681 }
682
683 #[test]
684 fn keyboard_protocol_unknown_mode_falls_back_to_default() {
685 let settings = KeyboardProtocolSettings {
686 enabled: false,
687 mode: "bogus-mode".to_owned(),
688 disambiguate_escape_codes: true,
689 report_event_types: false,
690 report_alternate_keys: true,
691 report_all_keys: false,
692 };
693 let config = KeyboardProtocolConfig::from(settings);
694 assert_eq!(config.mode, KeyboardProtocolMode::Default);
695 assert!(!config.enabled);
696 }
697
698 #[test]
699 fn keyboard_protocol_mode_parsing_is_case_insensitive() {
700 assert_eq!("FULL".parse::<KeyboardProtocolMode>(), Ok(KeyboardProtocolMode::Full));
701 assert!("unknown".parse::<KeyboardProtocolMode>().is_err());
702 }
703
704 #[test]
705 fn auto_shell_prompt_profile_selects_platform_defaults() {
706 let profile = ShellPromptProfile::Auto;
707
708 assert_eq!(profile.resolve_for_platform(ShellProfilePlatform::UnixLike), ResolvedShellPromptProfile::UnixLike);
709 assert_eq!(profile.resolve_for_platform(ShellProfilePlatform::Wsl), ResolvedShellPromptProfile::UnixLike);
710 assert_eq!(
711 profile.resolve_for_platform(ShellProfilePlatform::NativeWindows),
712 ResolvedShellPromptProfile::PowerShell
713 );
714 }
715
716 #[test]
717 fn explicit_shell_prompt_profiles_override_platform_defaults() {
718 assert_eq!(
719 ShellPromptProfile::UnixLike.resolve_for_platform(ShellProfilePlatform::NativeWindows),
720 ResolvedShellPromptProfile::UnixLike
721 );
722 assert_eq!(
723 ShellPromptProfile::PowerShell.resolve_for_platform(ShellProfilePlatform::Wsl),
724 ResolvedShellPromptProfile::PowerShell
725 );
726 }
727
728 #[test]
731 fn string_enums_keep_as_str_and_serde_in_lockstep() {
732 use crate::test_support::assert_string_enum_lockstep;
733
734 assert_string_enum_lockstep!(
735 SystemPromptMode,
736 [
737 SystemPromptMode::Minimal,
738 SystemPromptMode::Lightweight,
739 SystemPromptMode::Default,
740 SystemPromptMode::Specialized,
741 ]
742 );
743 assert_string_enum_lockstep!(
744 ToolDocumentationMode,
745 [
746 ToolDocumentationMode::Minimal,
747 ToolDocumentationMode::Progressive,
748 ToolDocumentationMode::Full,
749 ]
750 );
751 assert_string_enum_lockstep!(
752 ShellPromptProfile,
753 [
754 ShellPromptProfile::Auto,
755 ShellPromptProfile::UnixLike,
756 ShellPromptProfile::PowerShell,
757 ]
758 );
759 assert_string_enum_lockstep!(
760 VerbosityLevel,
761 [VerbosityLevel::Low, VerbosityLevel::Medium, VerbosityLevel::High]
762 );
763 assert_string_enum_lockstep!(
764 UiSurfacePreference,
765 [
766 UiSurfacePreference::Auto,
767 UiSurfacePreference::Alternate,
768 UiSurfacePreference::Inline,
769 ]
770 );
771
772 for value in SystemPromptMode::allowed_values() {
776 assert!(SystemPromptMode::parse(value).is_some(), "allowed value {value:?} must parse");
777 }
778 for value in ToolDocumentationMode::allowed_values() {
779 assert!(ToolDocumentationMode::parse(value).is_some(), "allowed value {value:?} must parse");
780 }
781 for value in ShellPromptProfile::allowed_values() {
782 assert!(ShellPromptProfile::parse(value).is_some(), "allowed value {value:?} must parse");
783 }
784 for value in VerbosityLevel::allowed_values() {
785 assert!(VerbosityLevel::parse(value).is_some(), "allowed value {value:?} must parse");
786 }
787 for value in UiSurfacePreference::allowed_values() {
788 assert!(UiSurfacePreference::parse(value).is_some(), "allowed value {value:?} must parse");
789 }
790 }
791
792 #[test]
797 fn resolved_shell_prompt_profile_matches_display_and_serde() {
798 for profile in [
799 ResolvedShellPromptProfile::UnixLike,
800 ResolvedShellPromptProfile::PowerShell,
801 ] {
802 let expected = profile.to_string();
803 assert_eq!(
804 serde_json::to_value(profile).expect("resolved profile serializes"),
805 serde_json::json!(expected),
806 "serde wire form drifted from Display for {profile:?}"
807 );
808 }
809 }
810}