vtcode-core 0.104.1

Core library for VT Code - a Rust-based terminal coding agent
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
//! Bridge between messages and tool executions
//!
//! Links LLM messages to their tool executions and tracks intent fulfillment.

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt;

#[cfg(test)]
use crate::config::constants::tools;
use crate::tools::result_metadata::EnhancedToolResult;

/// Tracks intent fulfillment
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntentFulfillment {
    /// Message goal completely achieved
    Fulfilled,

    /// Message goal partially achieved
    PartiallyFulfilled,

    /// Tools executed but results inconclusive
    Attempted,

    /// Tools failed or results contradicted intent
    Failed,
}

impl fmt::Display for IntentFulfillment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::Fulfilled => "fulfilled",
            Self::PartiallyFulfilled => "partially_fulfilled",
            Self::Attempted => "attempted",
            Self::Failed => "failed",
        };
        f.write_str(s)
    }
}

/// Tool execution record tied to message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolExecution {
    pub tool_name: String,
    pub args: Value,
    pub result: EnhancedToolResult,
    pub duration_ms: u64,

    /// Did this tool help fulfill the intent?
    pub contributed_to_intent: bool,
}

/// Stated intent extracted from message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ToolIntent {
    Search(String),
    Execute(String),
    Analyze(String),
    Modify(String),
}

impl fmt::Display for ToolIntent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Search(s) => write!(f, "search: {}", s),
            Self::Execute(s) => write!(f, "execute: {}", s),
            Self::Analyze(s) => write!(f, "analyze: {}", s),
            Self::Modify(s) => write!(f, "modify: {}", s),
        }
    }
}

/// Correlation between message intent and tool execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageToolCorrelation {
    /// Unique message identifier
    pub message_id: String,

    /// Extracted intent from message
    pub stated_intent: ToolIntent,

    /// Original message text
    pub message_text: String,

    /// Tools executed to fulfill this message
    pub tool_executions: Vec<ToolExecution>,

    /// Overall success of fulfilling stated intent
    pub intent_fulfillment: IntentFulfillment,

    /// Confidence in fulfillment assessment (0.0-1.0)
    pub confidence: f32,

    /// Any issues encountered
    pub issues: Vec<String>,
}

impl MessageToolCorrelation {
    pub fn new(message_id: String, message_text: String, intent: ToolIntent) -> Self {
        Self {
            message_id,
            stated_intent: intent,
            message_text,
            tool_executions: vec![],
            intent_fulfillment: IntentFulfillment::Attempted,
            confidence: 0.0,
            issues: vec![],
        }
    }

    /// Add a tool execution
    pub fn add_execution(&mut self, execution: ToolExecution) {
        self.tool_executions.push(execution);
        self.reassess_fulfillment();
    }

    /// Add an issue
    pub fn add_issue(&mut self, issue: String) {
        self.issues.push(issue);
        self.reassess_fulfillment();
    }

    /// Reassess whether intent was fulfilled
    fn reassess_fulfillment(&mut self) {
        if self.tool_executions.is_empty() {
            self.intent_fulfillment = IntentFulfillment::Failed;
            self.confidence = 0.0;
            return;
        }

        // Count contributing executions
        let contributing = self
            .tool_executions
            .iter()
            .filter(|e| e.contributed_to_intent)
            .count();

        let avg_quality = self
            .tool_executions
            .iter()
            .map(|e| e.result.metadata.quality_score())
            .sum::<f32>()
            / self.tool_executions.len() as f32;

        self.intent_fulfillment = match (contributing, avg_quality) {
            (n, q) if n == self.tool_executions.len() && q > 0.75 => IntentFulfillment::Fulfilled,
            (n, q) if n > self.tool_executions.len() / 2 && q > 0.6 => {
                IntentFulfillment::PartiallyFulfilled
            }
            (0, _) => IntentFulfillment::Failed,
            _ => IntentFulfillment::Attempted,
        };

        self.confidence = (contributing as f32 / self.tool_executions.len() as f32) * avg_quality;
    }

    /// Get summary of tool execution
    pub fn summary(&self) -> String {
        format!(
            "Intent: {} | Tools: {} | Fulfillment: {} (confidence: {:.0}%)",
            self.stated_intent,
            self.tool_executions
                .iter()
                .map(|e| e.tool_name.clone())
                .collect::<Vec<_>>()
                .join(", "),
            self.intent_fulfillment,
            self.confidence * 100.0
        )
    }
}

/// Extractor for tool intents from messages
pub struct ToolIntentExtractor;

impl ToolIntentExtractor {
    /// Extract intent from message text
    pub fn extract(text: &str) -> Option<ToolIntent> {
        let text_lower = text.to_lowercase();

        // Search patterns
        if let Some(intent) = extract_search_intent(&text_lower) {
            return Some(intent);
        }

        // Execute patterns
        if let Some(intent) = extract_execute_intent(&text_lower) {
            return Some(intent);
        }

        // Analyze patterns
        if let Some(intent) = extract_analyze_intent(&text_lower) {
            return Some(intent);
        }

        // Modify patterns
        if let Some(intent) = extract_modify_intent(&text_lower) {
            return Some(intent);
        }

        None
    }
}

/// Extract search intent
fn extract_search_intent(text: &str) -> Option<ToolIntent> {
    let search_keywords = [
        "grep", "search", "find", "look for", "locate", "check if", "does", "exist",
    ];

    for keyword in &search_keywords {
        if text.contains(keyword) {
            // Try to extract what we're searching for
            if let Some(pattern) = extract_quoted_string(text) {
                return Some(ToolIntent::Search(pattern));
            }

            // Fallback: use keyword
            return Some(ToolIntent::Search(keyword.to_string()));
        }
    }

    None
}

/// Extract execute intent
fn extract_execute_intent(text: &str) -> Option<ToolIntent> {
    let execute_keywords = [
        "run", "execute", "command", "cargo", "npm", "python", "bash", "sh",
    ];

    for keyword in &execute_keywords {
        if text.contains(keyword) {
            // Try to extract command
            if let Some(cmd) = extract_quoted_string(text) {
                return Some(ToolIntent::Execute(cmd));
            }

            return Some(ToolIntent::Execute(keyword.to_string()));
        }
    }

    None
}

/// Extract analyze intent
fn extract_analyze_intent(text: &str) -> Option<ToolIntent> {
    let analyze_keywords = ["analyze", "check", "review", "examine", "inspect", "parse"];

    for keyword in &analyze_keywords {
        if text.contains(keyword) {
            if let Some(target) = extract_quoted_string(text) {
                return Some(ToolIntent::Analyze(target));
            }

            return Some(ToolIntent::Analyze(keyword.to_string()));
        }
    }

    None
}

/// Extract modify intent
fn extract_modify_intent(text: &str) -> Option<ToolIntent> {
    let modify_keywords = ["edit", "modify", "change", "fix", "apply", "patch"];

    for keyword in &modify_keywords {
        if text.contains(keyword) {
            if let Some(target) = extract_quoted_string(text) {
                return Some(ToolIntent::Modify(target));
            }

            return Some(ToolIntent::Modify(keyword.to_string()));
        }
    }

    None
}

/// Extract quoted string from text
fn extract_quoted_string(text: &str) -> Option<String> {
    // Look for "quoted" or 'quoted' strings
    let mut in_quote = false;
    let mut quote_char = ' ';
    let mut current = String::new();

    for c in text.chars() {
        match c {
            '"' | '\'' if !in_quote => {
                in_quote = true;
                quote_char = c;
            }
            c if in_quote && c == quote_char => {
                in_quote = false;
                if !current.is_empty() {
                    return Some(current);
                }
            }
            c if in_quote => {
                current.push(c);
            }
            _ => {}
        }
    }

    None
}

/// Track correlations across a session
pub struct MessageCorrelationTracker {
    correlations: Vec<MessageToolCorrelation>,
}

impl MessageCorrelationTracker {
    pub fn new() -> Self {
        Self {
            correlations: vec![],
        }
    }

    /// Add a correlation
    pub fn add(&mut self, correlation: MessageToolCorrelation) {
        self.correlations.push(correlation);
    }

    /// Get all correlations
    pub fn all(&self) -> &[MessageToolCorrelation] {
        &self.correlations
    }

    /// Get unfulfilled intents
    pub fn unfulfilled(&self) -> Vec<&MessageToolCorrelation> {
        self.correlations
            .iter()
            .filter(|c| c.intent_fulfillment == IntentFulfillment::Failed)
            .collect()
    }

    /// Get fulfillment statistics
    pub fn stats(&self) -> CorrelationStats {
        let total = self.correlations.len();
        let fulfilled = self
            .correlations
            .iter()
            .filter(|c| c.intent_fulfillment == IntentFulfillment::Fulfilled)
            .count();
        let partially_fulfilled = self
            .correlations
            .iter()
            .filter(|c| c.intent_fulfillment == IntentFulfillment::PartiallyFulfilled)
            .count();
        let failed = self
            .correlations
            .iter()
            .filter(|c| c.intent_fulfillment == IntentFulfillment::Failed)
            .count();

        let avg_confidence = if total > 0 {
            self.correlations.iter().map(|c| c.confidence).sum::<f32>() / total as f32
        } else {
            0.0
        };

        CorrelationStats {
            total,
            fulfilled,
            partially_fulfilled,
            attempted: total - fulfilled - partially_fulfilled - failed,
            failed,
            avg_confidence,
        }
    }
}

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

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorrelationStats {
    pub total: usize,
    pub fulfilled: usize,
    pub partially_fulfilled: usize,
    pub attempted: usize,
    pub failed: usize,
    pub avg_confidence: f32,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::result_metadata::ResultMetadata;

    #[test]
    fn test_intent_extraction_search() {
        let text = "Let me grep for 'error' in the logs";
        let intent = ToolIntentExtractor::extract(text);

        assert!(matches!(intent, Some(ToolIntent::Search(_))));
    }

    #[test]
    fn test_intent_extraction_execute() {
        let text = "Run 'cargo test' to check";
        let intent = ToolIntentExtractor::extract(text);

        assert!(matches!(intent, Some(ToolIntent::Execute(_))));
    }

    #[test]
    fn test_intent_extraction_analyze() {
        let text = "Analyze the config file please";
        let intent = ToolIntentExtractor::extract(text);

        assert!(matches!(intent, Some(ToolIntent::Analyze(_))));
    }

    #[test]
    fn test_message_correlation() {
        let mut corr = MessageToolCorrelation::new(
            "msg-1".to_owned(),
            "Let me grep for errors".to_owned(),
            ToolIntent::Search("errors".to_owned()),
        );

        let exec = ToolExecution {
            tool_name: tools::GREP_FILE.to_owned(),
            args: Value::Null,
            result: EnhancedToolResult::new(
                Value::Null,
                ResultMetadata::success(0.9, 0.9),
                tools::GREP_FILE.to_owned(),
            ),
            duration_ms: 100,
            contributed_to_intent: true,
        };

        corr.add_execution(exec);

        assert!(matches!(
            corr.intent_fulfillment,
            IntentFulfillment::PartiallyFulfilled
        ));
    }

    #[test]
    fn test_correlation_tracker() {
        let mut tracker = MessageCorrelationTracker::new();

        let corr = MessageToolCorrelation::new(
            "msg-1".to_owned(),
            "test".to_owned(),
            ToolIntent::Search("test".to_owned()),
        );

        tracker.add(corr);

        let stats = tracker.stats();
        assert_eq!(stats.total, 1);
    }

    #[test]
    fn test_extract_quoted_string() {
        assert_eq!(
            extract_quoted_string("grep for \"error pattern\""),
            Some("error pattern".to_owned())
        );
        assert_eq!(
            extract_quoted_string("find 'test.rs'"),
            Some("test.rs".to_owned())
        );
    }
}