vtcode_core/commands/
stats.rs1use crate::config::types::{AgentConfig, OutputFormat, PerformanceMetrics};
4use crate::core::agent::core::Agent;
5use crate::tools::build_function_declarations;
6use anyhow::Result;
7use console::style;
8
9pub async fn handle_stats_command(
11 agent: &Agent,
12 detailed: bool,
13 format: String,
14) -> Result<PerformanceMetrics> {
15 let output_format = match format.to_lowercase().as_str() {
16 "text" => OutputFormat::Text,
17 "json" => OutputFormat::Json,
18 "html" => OutputFormat::Html,
19 _ => OutputFormat::Text,
20 };
21
22 println!("{}", style("Session Statistics").cyan().bold());
23
24 let metrics = agent.performance_metrics();
25
26 match output_format {
27 OutputFormat::Text => display_text_stats(agent.config(), &metrics, detailed),
28 OutputFormat::Json => display_json_stats(agent.config(), &metrics),
29 OutputFormat::Html => display_html_stats(agent.config(), &metrics),
30 }
31
32 Ok(metrics)
33}
34
35fn display_text_stats(config: &AgentConfig, metrics: &PerformanceMetrics, detailed: bool) {
36 println!("{} Configuration:", style("[CONFIG]").dim());
37 println!(" Model: {}", style(&config.model).cyan());
38 println!(" Workspace: {}", style(config.workspace.display()).cyan());
39 println!(
40 " Verbose Mode: {}",
41 if config.verbose {
42 "Enabled"
43 } else {
44 "Disabled"
45 }
46 );
47
48 println!("\n{} Tool Information:", style("").dim());
49 let tool_count = build_function_declarations().len();
50 println!(" Available Tools: {}", style(tool_count).cyan());
51
52 if detailed {
53 println!(" Tools:");
54 for tool in build_function_declarations() {
55 println!(" • {}", style(&tool.name).yellow());
56 }
57 }
58
59 println!("\n{} Performance Metrics:", style("[METRICS]").dim());
60 println!(
61 " Session Duration: {} seconds",
62 style(metrics.session_duration_seconds).cyan()
63 );
64 println!(" API Calls: {}", style(metrics.total_api_calls).cyan());
65 println!(
66 " Tool Executions: {}",
67 style(metrics.tool_execution_count).cyan()
68 );
69 println!(" Errors: {}", style(metrics.error_count).red());
70 println!(
71 " Recovery Rate: {:.1}%",
72 style(metrics.recovery_success_rate * 100.0).green()
73 );
74
75 if let Some(tokens) = metrics.total_tokens_used {
76 println!(" Total Tokens: {}", style(tokens).cyan());
77 }
78
79 println!(
80 " Avg Response Time: {:.0}ms",
81 style(metrics.average_response_time_ms).cyan()
82 );
83
84 if detailed {
85 println!("\n{} System Information:", style("💻").dim());
86 println!(
87 " Rust Version: {}",
88 style(env!("CARGO_PKG_RUST_VERSION")).cyan()
89 );
90 println!(
91 " vtcode Version: {}",
92 style(env!("CARGO_PKG_VERSION")).cyan()
93 );
94 println!(
95 " Build Profile: {}",
96 if cfg!(debug_assertions) {
97 "Debug"
98 } else {
99 "Release"
100 }
101 );
102 }
103}
104
105fn display_json_stats(config: &AgentConfig, metrics: &PerformanceMetrics) {
106 let stats = serde_json::json!({
107 "configuration": {
108 "model": config.model,
109 "workspace": config.workspace,
110 "verbose": config.verbose
111 },
112 "tools": {
113 "count": build_function_declarations().len(),
114 "available": build_function_declarations().iter().map(|t| &t.name).collect::<Vec<_>>()
115 },
116 "performance": {
117 "session_duration_seconds": metrics.session_duration_seconds,
118 "total_api_calls": metrics.total_api_calls,
119 "total_tokens_used": metrics.total_tokens_used,
120 "average_response_time_ms": metrics.average_response_time_ms,
121 "tool_execution_count": metrics.tool_execution_count,
122 "error_count": metrics.error_count,
123 "recovery_success_rate": metrics.recovery_success_rate
124 },
125 "system": {
126 "rust_version": env!("CARGO_PKG_RUST_VERSION"),
127 "vtcode_version": env!("CARGO_PKG_VERSION"),
128 "build_profile": if cfg!(debug_assertions) { "debug" } else { "release" }
129 }
130 });
131
132 println!("{}", serde_json::to_string_pretty(&stats).unwrap());
133}
134
135fn display_html_stats(config: &AgentConfig, metrics: &PerformanceMetrics) {
136 println!("<!DOCTYPE html>");
137 println!("<html><head><title>vtcode Statistics</title></head><body>");
138 println!("<h1>vtcode Session Statistics</h1>");
139
140 println!("<h2>Configuration</h2>");
141 println!("<ul>");
142 println!("<li><strong>Model:</strong> {}</li>", config.model);
143 println!(
144 "<li><strong>Workspace:</strong> {}</li>",
145 config.workspace.display()
146 );
147 println!(
148 "<li><strong>Verbose Mode:</strong> {}</li>",
149 if config.verbose {
150 "Enabled"
151 } else {
152 "Disabled"
153 }
154 );
155 println!("</ul>");
156
157 println!("<h2>Tool Information</h2>");
158 println!(
159 "<p><strong>Available Tools:</strong> {}</p>",
160 build_function_declarations().len()
161 );
162 println!("<ul>");
163 for tool in build_function_declarations() {
164 println!("<li>{}</li>", tool.name);
165 }
166 println!("</ul>");
167
168 println!("<h2>Performance Metrics</h2>");
169 println!("<ul>");
170 println!(
171 "<li><strong>Session Duration:</strong> {} seconds</li>",
172 metrics.session_duration_seconds
173 );
174 println!(
175 "<li><strong>API Calls:</strong> {}</li>",
176 metrics.total_api_calls
177 );
178 println!(
179 "<li><strong>Tool Executions:</strong> {}</li>",
180 metrics.tool_execution_count
181 );
182 println!("<li><strong>Errors:</strong> {}</li>", metrics.error_count);
183 println!(
184 "<li><strong>Recovery Rate:</strong> {:.1}%</li>",
185 metrics.recovery_success_rate * 100.0
186 );
187 if let Some(tokens) = metrics.total_tokens_used {
188 println!("<li><strong>Total Tokens:</strong> {}</li>", tokens);
189 }
190 println!(
191 "<li><strong>Avg Response Time:</strong> {:.0}ms</li>",
192 metrics.average_response_time_ms
193 );
194 println!("</ul>");
195
196 println!("</body></html>");
197}