vtcode_core/llm/
error_display.rs

1//! LLM error display utilities with enhanced ANSI color support
2//!
3//! This module provides enhanced error display capabilities for LLM providers
4//! using standard console styling for consistent terminal output.
5
6use crate::ui::styled::*;
7
8/// Get a styled error message with enhanced coloring
9pub fn style_llm_error(message: &str) -> String {
10    // Use a rich red color for LLM errors
11    format!(
12        "{}{}{}",
13        Styles::render(&Styles::error()),
14        message,
15        Styles::render_reset()
16    )
17}
18
19/// Get a styled warning message with enhanced coloring
20pub fn style_llm_warning(message: &str) -> String {
21    // Use an amber color for LLM warnings
22    format!(
23        "{}{}{}",
24        Styles::render(&Styles::warning()),
25        message,
26        Styles::render_reset()
27    )
28}
29
30/// Get a styled success message with enhanced coloring
31pub fn style_llm_success(message: &str) -> String {
32    // Use a vibrant green color for LLM success messages
33    format!(
34        "{}{}{}",
35        Styles::render(&Styles::success()),
36        message,
37        Styles::render_reset()
38    )
39}
40
41/// Get a styled provider name with enhanced coloring based on provider type
42pub fn style_provider_name(provider: &str) -> String {
43    let styled_name = match provider.to_lowercase().as_str() {
44        "gemini" => {
45            // Deep blue for Gemini
46            format!(
47                "{}{}{}",
48                Styles::render(&Styles::info()),
49                provider,
50                Styles::render_reset()
51            )
52        }
53        "openai" => {
54            // Bright orange for OpenAI (using yellow as approximation)
55            format!(
56                "{}{}{}",
57                Styles::render(&Styles::warning()),
58                provider,
59                Styles::render_reset()
60            )
61        }
62        "anthropic" => {
63            // Anthropic's brand purple (using magenta as approximation)
64            format!(
65                "{}{}{}",
66                Styles::render(&Styles::code()),
67                provider,
68                Styles::render_reset()
69            )
70        }
71        _ => {
72            // Default styling for other providers
73            format!(
74                "{}{}{}",
75                Styles::render(&Styles::debug()),
76                provider,
77                Styles::render_reset()
78            )
79        }
80    };
81    styled_name
82}
83
84/// Format an LLM error for display with enhanced coloring
85pub fn format_llm_error(provider: &str, error: &str) -> String {
86    let provider_styled = style_provider_name(provider);
87    let error_styled = style_llm_error(error);
88    format!("{} {}", provider_styled, error_styled)
89}
90
91/// Format an LLM warning for display with enhanced coloring
92pub fn format_llm_warning(provider: &str, warning: &str) -> String {
93    let provider_styled = style_provider_name(provider);
94    let warning_styled = style_llm_warning(warning);
95    format!("{} {}", provider_styled, warning_styled)
96}
97
98/// Format an LLM success message for display with enhanced coloring
99pub fn format_llm_success(provider: &str, message: &str) -> String {
100    let provider_styled = style_provider_name(provider);
101    let success_styled = style_llm_success(message);
102    format!("{} {}", provider_styled, success_styled)
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn test_style_llm_error() {
111        let result = style_llm_error("Test error");
112        assert!(!result.is_empty());
113    }
114
115    #[test]
116    fn test_style_llm_warning() {
117        let result = style_llm_warning("Test warning");
118        assert!(!result.is_empty());
119    }
120
121    #[test]
122    fn test_style_llm_success() {
123        let result = style_llm_success("Test success");
124        assert!(!result.is_empty());
125    }
126
127    #[test]
128    fn test_style_provider_name() {
129        let providers = vec!["gemini", "openai", "anthropic", "unknown"];
130        for provider in providers {
131            let result = style_provider_name(provider);
132            assert!(!result.is_empty());
133        }
134    }
135
136    #[test]
137    fn test_format_llm_error() {
138        let result = format_llm_error("gemini", "Connection failed");
139        assert!(result.contains("gemini"));
140        assert!(result.contains("Connection failed"));
141    }
142
143    #[test]
144    fn test_format_llm_warning() {
145        let result = format_llm_warning("openai", "Rate limit approaching");
146        assert!(result.contains("openai"));
147        assert!(result.contains("Rate limit approaching"));
148    }
149
150    #[test]
151    fn test_format_llm_success() {
152        let result = format_llm_success("anthropic", "Request completed");
153        assert!(result.contains("anthropic"));
154        assert!(result.contains("Request completed"));
155    }
156}