vtcode-core 0.19.1

Core library for VTCode - a Rust-based terminal coding agent
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};

/// Represents a conversation summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationSummary {
    pub id: String,
    pub timestamp: u64,
    pub session_duration_seconds: u64,
    pub total_turns: usize,
    pub key_decisions: Vec<KeyDecision>,
    pub completed_tasks: Vec<TaskSummary>,
    pub error_patterns: Vec<ErrorPattern>,
    pub context_recommendations: Vec<String>,
    pub summary_text: String,
    pub compression_ratio: f64,
    pub confidence_score: f64,
}

/// A key decision made during the conversation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyDecision {
    pub turn_number: usize,
    pub decision_type: DecisionType,
    pub description: String,
    pub rationale: String,
    pub outcome: Option<String>,
    pub importance_score: f64,
}

/// Type of decision that was made
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DecisionType {
    ToolExecution,
    ResponseGeneration,
    ContextCompression,
    ErrorRecovery,
    WorkflowChange,
}

impl std::fmt::Display for DecisionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let description = match self {
            DecisionType::ToolExecution => "Tool Execution",
            DecisionType::ResponseGeneration => "Response Generation",
            DecisionType::ContextCompression => "Context Compression",
            DecisionType::ErrorRecovery => "Error Recovery",
            DecisionType::WorkflowChange => "Workflow Change",
        };
        write!(f, "{}", description)
    }
}

/// Summary of a completed task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskSummary {
    pub task_type: String,
    pub description: String,
    pub success: bool,
    pub duration_seconds: Option<u64>,
    pub tools_used: Vec<String>,
}

/// Pattern of errors that occurred
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorPattern {
    pub error_type: String,
    pub frequency: usize,
    pub description: String,
    pub recommended_solution: String,
}

/// Conversation summarizer for long-running sessions
pub struct ConversationSummarizer {
    summaries: Vec<ConversationSummary>,
    summarization_threshold: usize, // Minimum conversation length to trigger summarization
    max_summary_length: usize,
    compression_target_ratio: f64,
}

impl ConversationSummarizer {
    pub fn new() -> Self {
        Self {
            summaries: Vec::new(),
            summarization_threshold: 20,   // Summarize after 20 turns
            max_summary_length: 2000,      // Maximum characters in summary
            compression_target_ratio: 0.3, // Target 30% of original length
        }
    }

    /// Check if conversation should be summarized
    pub fn should_summarize(
        &self,
        conversation_length: usize,
        context_size: usize,
        context_limit: usize,
    ) -> bool {
        let approaching_limit = context_size > (context_limit * 80 / 100); // 80% of limit
        let long_conversation = conversation_length >= self.summarization_threshold;
        let has_errors = context_size > (context_limit * 60 / 100); // 60% indicates potential issues

        approaching_limit || long_conversation || has_errors
    }

    /// Generate a conversation summary
    pub fn generate_summary(
        &mut self,
        conversation_turns: &[ConversationTurn],
        decision_history: &[DecisionInfo],
        error_history: &[ErrorInfo],
        session_start_time: u64,
    ) -> Result<ConversationSummary, SummarizationError> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let session_duration = now - session_start_time;
        let total_turns = conversation_turns.len();

        // Extract key decisions
        let key_decisions = self.extract_key_decisions(decision_history, conversation_turns);

        // Extract completed tasks
        let completed_tasks = self.extract_completed_tasks(conversation_turns);

        // Analyze error patterns
        let error_patterns = self.analyze_error_patterns(error_history);

        // Generate context recommendations
        let context_recommendations = self.generate_context_recommendations(
            conversation_turns.len(),
            error_history.len(),
            session_duration,
        );

        // Generate summary text
        let summary_text = self.generate_summary_text(
            &key_decisions,
            &completed_tasks,
            &error_patterns,
            session_duration,
            total_turns,
        );

        // Calculate compression ratio
        let original_length = self.calculate_conversation_length(conversation_turns);
        let compression_ratio = if original_length > 0 {
            summary_text.len() as f64 / original_length as f64
        } else {
            1.0
        };

        // Calculate confidence score
        let confidence_score = self.calculate_confidence_score(
            key_decisions.len(),
            completed_tasks.len(),
            error_patterns.len(),
            compression_ratio,
        );

        let summary_id = format!("summary_{}", now);

        let summary = ConversationSummary {
            id: summary_id,
            timestamp: now,
            session_duration_seconds: session_duration,
            total_turns,
            key_decisions,
            completed_tasks,
            error_patterns,
            context_recommendations,
            summary_text,
            compression_ratio,
            confidence_score,
        };

        self.summaries.push(summary.clone());
        Ok(summary)
    }

    /// Extract key decisions from conversation and decision history
    fn extract_key_decisions(
        &self,
        decision_history: &[DecisionInfo],
        conversation_turns: &[ConversationTurn],
    ) -> Vec<KeyDecision> {
        let mut key_decisions = Vec::new();

        for (_i, decision) in decision_history.iter().enumerate() {
            let decision_type = match decision.action_type.as_str() {
                "tool_call" => DecisionType::ToolExecution,
                "response" => DecisionType::ResponseGeneration,
                "context_compression" => DecisionType::ContextCompression,
                "error_recovery" => DecisionType::ErrorRecovery,
                _ => DecisionType::WorkflowChange,
            };

            let importance_score = self.calculate_decision_importance(decision, conversation_turns);

            if importance_score > 0.6 {
                // Only include important decisions
                key_decisions.push(KeyDecision {
                    turn_number: decision.turn_number,
                    decision_type,
                    description: decision.description.clone(),
                    rationale: decision.reasoning.clone(),
                    outcome: decision.outcome.clone(),
                    importance_score,
                });
            }
        }

        // Limit to top 10 most important decisions
        key_decisions.sort_by(|a, b| b.importance_score.partial_cmp(&a.importance_score).unwrap());
        key_decisions.truncate(10);
        key_decisions
    }

    /// Extract completed tasks from conversation
    fn extract_completed_tasks(&self, conversation_turns: &[ConversationTurn]) -> Vec<TaskSummary> {
        let mut tasks = Vec::new();

        for turn in conversation_turns {
            if let Some(task_info) = &turn.task_info {
                if task_info.completed {
                    tasks.push(TaskSummary {
                        task_type: task_info.task_type.clone(),
                        description: task_info.description.clone(),
                        success: task_info.success,
                        duration_seconds: task_info.duration_seconds,
                        tools_used: task_info.tools_used.clone(),
                    });
                }
            }
        }

        tasks
    }

    /// Analyze patterns in error history
    fn analyze_error_patterns(&self, error_history: &[ErrorInfo]) -> Vec<ErrorPattern> {
        let mut error_counts = HashMap::new();

        // Count errors by type
        for error in error_history {
            *error_counts.entry(error.error_type.clone()).or_insert(0) += 1;
        }

        let mut patterns = Vec::new();
        for (error_type, frequency) in error_counts {
            if frequency >= 2 {
                // Only include errors that occurred multiple times
                let description = format!("{} error occurred {} times", error_type, frequency);
                let recommended_solution = self.get_error_solution(&error_type);

                patterns.push(ErrorPattern {
                    error_type,
                    frequency,
                    description,
                    recommended_solution,
                });
            }
        }

        patterns.sort_by(|a, b| b.frequency.cmp(&a.frequency));
        patterns
    }

    /// Generate context recommendations
    fn generate_context_recommendations(
        &self,
        turn_count: usize,
        error_count: usize,
        session_duration: u64,
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        if turn_count > 50 {
            recommendations.push(
                "Consider summarizing the conversation to maintain context efficiency".to_string(),
            );
        }

        if error_count > 5 {
            recommendations.push(
                "High error rate detected - review error patterns and consider context compression"
                    .to_string(),
            );
        }

        if session_duration > 1800 {
            // 30 minutes
            recommendations.push(
                "Long-running session detected - consider breaking into smaller tasks".to_string(),
            );
        }

        if recommendations.is_empty() {
            recommendations.push("Conversation is proceeding normally".to_string());
        }

        recommendations
    }

    /// Generate human-readable summary text
    fn generate_summary_text(
        &self,
        key_decisions: &[KeyDecision],
        completed_tasks: &[TaskSummary],
        error_patterns: &[ErrorPattern],
        session_duration: u64,
        total_turns: usize,
    ) -> String {
        let mut summary = format!(
            "Conversation Summary ({} turns, {} seconds):\n\n",
            total_turns, session_duration
        );

        if !key_decisions.is_empty() {
            summary.push_str("Key Decisions Made:\n");
            for decision in key_decisions.iter().take(5) {
                summary.push_str(&format!(
                    "• Turn {}: {} - {}\n",
                    decision.turn_number, decision.decision_type, decision.description
                ));
            }
            summary.push('\n');
        }

        if !completed_tasks.is_empty() {
            summary.push_str("Completed Tasks:\n");
            for task in completed_tasks {
                let status = if task.success {
                    "[Success]"
                } else {
                    "[Failure]"
                };
                summary.push_str(&format!(
                    "{} {} ({})\n",
                    status, task.description, task.task_type
                ));
            }
            summary.push('\n');
        }

        if !error_patterns.is_empty() {
            summary.push_str("Error Patterns:\n");
            for pattern in error_patterns {
                summary.push_str(&format!(
                    "• {}: {} ({} occurrences)\n",
                    pattern.error_type, pattern.description, pattern.frequency
                ));
            }
            summary.push('\n');
        }

        // Truncate if too long
        if summary.len() > self.max_summary_length {
            summary.truncate(self.max_summary_length - 3);
            summary.push_str("...");
        }

        summary
    }

    /// Calculate importance score for a decision
    fn calculate_decision_importance(
        &self,
        decision: &DecisionInfo,
        conversation_turns: &[ConversationTurn],
    ) -> f64 {
        let mut score = 0.5; // Base score

        // Increase score based on decision type importance
        match decision.action_type.as_str() {
            "tool_call" => score += 0.3,
            "context_compression" => score += 0.4,
            "error_recovery" => score += 0.2,
            _ => {}
        }

        // Increase score if decision had significant outcome
        if let Some(outcome) = &decision.outcome {
            if outcome.contains("success") || outcome.contains("completed") {
                score += 0.2;
            }
        }

        // Increase score if decision was made in later turns (potentially more important)
        let progress_ratio = decision.turn_number as f64 / conversation_turns.len() as f64;
        score += progress_ratio * 0.1;

        score.min(1.0)
    }

    /// Calculate conversation length in characters
    fn calculate_conversation_length(&self, conversation_turns: &[ConversationTurn]) -> usize {
        conversation_turns
            .iter()
            .map(|turn| turn.content.len())
            .sum()
    }

    /// Calculate confidence score for the summary
    fn calculate_confidence_score(
        &self,
        decision_count: usize,
        task_count: usize,
        error_count: usize,
        compression_ratio: f64,
    ) -> f64 {
        let mut confidence = 0.7; // Base confidence

        // Higher confidence with more decisions and tasks
        confidence += decision_count.min(10) as f64 * 0.02;
        confidence += task_count.min(10) as f64 * 0.03;

        // Lower confidence with many errors
        confidence -= error_count.min(10) as f64 * 0.05;

        // Adjust based on compression ratio (closer to target = higher confidence)
        let ratio_distance = (compression_ratio - self.compression_target_ratio).abs();
        confidence -= ratio_distance * 0.5;

        confidence.max(0.1).min(1.0)
    }

    /// Get recommended solution for error type
    fn get_error_solution(&self, error_type: &str) -> String {
        match error_type {
            "tool_execution" => "Review tool parameters and ensure correct file paths".to_string(),
            "api_call" => "Check API key and consider implementing retry logic".to_string(),
            "context_compression" => {
                "Monitor context size and implement proactive compression".to_string()
            }
            _ => "Investigate error details and consider context preservation".to_string(),
        }
    }

    /// Get all summaries
    pub fn get_summaries(&self) -> &[ConversationSummary] {
        &self.summaries
    }

    /// Get latest summary
    pub fn get_latest_summary(&self) -> Option<&ConversationSummary> {
        self.summaries.last()
    }
}

/// Information about a conversation turn
#[derive(Debug, Clone)]
pub struct ConversationTurn {
    pub turn_number: usize,
    pub content: String,
    pub role: String,
    pub task_info: Option<TaskInfo>,
}

/// Information about a task within a conversation turn
#[derive(Debug, Clone)]
pub struct TaskInfo {
    pub task_type: String,
    pub description: String,
    pub completed: bool,
    pub success: bool,
    pub duration_seconds: Option<u64>,
    pub tools_used: Vec<String>,
}

/// Information about a decision
#[derive(Debug, Clone)]
pub struct DecisionInfo {
    pub turn_number: usize,
    pub action_type: String,
    pub description: String,
    pub reasoning: String,
    pub outcome: Option<String>,
}

/// Information about an error
#[derive(Debug, Clone)]
pub struct ErrorInfo {
    pub error_type: String,
    pub message: String,
    pub turn_number: usize,
    pub recoverable: bool,
}

/// Error that can occur during summarization
#[derive(Debug, Clone)]
pub enum SummarizationError {
    InsufficientData,
    ProcessingError(String),
}

impl std::fmt::Display for SummarizationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SummarizationError::InsufficientData => {
                write!(f, "Insufficient data for summarization")
            }
            SummarizationError::ProcessingError(msg) => write!(f, "Processing error: {}", msg),
        }
    }
}

impl std::error::Error for SummarizationError {}

impl Default for ConversationSummarizer {
    fn default() -> Self {
        Self::new()
    }
}