vtcode-core 0.103.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
//! Conversation memory for vibe coding support
//!
//! Tracks entities mentioned across conversation turns to enable pronoun resolution
//! and contextual understanding.

use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::path::PathBuf;
use vtcode_commons::utils::current_timestamp;

/// Maximum number of conversation turns to remember
const MAX_MEMORY_TURNS: usize = 50;

/// Maximum entity mentions to track
const MAX_ENTITY_MENTIONS: usize = 200;

/// Type of entity mention
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MentionType {
    /// Direct mention (explicit naming)
    Direct,
    /// Pronoun reference (it, that, this)
    Pronoun,
    /// Implicit (inferred from context)
    Implicit,
}

/// History of an entity's mentions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MentionHistory {
    pub entity: String,
    pub first_mention: u64,
    pub last_mention: u64,
    pub mention_count: usize,
    pub context_snippets: Vec<String>,
}

impl MentionHistory {
    /// Create new mention history
    pub fn new(entity: String) -> Self {
        let now = current_timestamp();
        Self {
            entity,
            first_mention: now,
            last_mention: now,
            mention_count: 1,
            context_snippets: Vec::new(),
        }
    }

    /// Record a mention
    pub fn record_mention(&mut self, _turn: usize) {
        self.last_mention = current_timestamp();
        self.mention_count += 1;
    }
}

/// A single entity mention in the timeline
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityMention {
    pub turn: usize,
    pub entity: String,
    pub mention_type: MentionType,
    pub file_context: Option<PathBuf>,
}

/// A user message for context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMessage {
    pub turn: usize,
    pub content: String,
    pub entities: Vec<String>,
}

/// Pronoun reference waiting to be resolved
#[derive(Debug, Clone)]
pub struct PronounReference {
    pub pronoun: String,
    pub turn: usize,
    pub context: String,
}

/// Conversation memory system
pub struct ConversationMemory {
    /// Mentioned entities with history
    mentioned_entities: HashMap<String, MentionHistory>,

    /// Timeline of entity mentions
    entity_timeline: VecDeque<EntityMention>,

    /// Recent user messages
    recent_user_messages: VecDeque<UserMessage>,

    /// Recent file contexts (files mentioned or edited)
    recent_file_contexts: VecDeque<PathBuf>,

    /// Unresolved pronouns
    #[allow(dead_code)]
    unresolved_pronouns: Vec<PronounReference>,

    /// Resolved reference mappings
    resolved_references: HashMap<String, String>,

    /// Current turn number
    current_turn: usize,
}

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

impl ConversationMemory {
    /// Create a new conversation memory
    pub fn new() -> Self {
        Self {
            mentioned_entities: HashMap::new(),
            entity_timeline: VecDeque::with_capacity(MAX_ENTITY_MENTIONS),
            recent_user_messages: VecDeque::with_capacity(MAX_MEMORY_TURNS),
            recent_file_contexts: VecDeque::with_capacity(20),
            unresolved_pronouns: Vec::new(),
            resolved_references: HashMap::new(),
            current_turn: 0,
        }
    }

    /// Extract entities from a message
    pub fn extract_entities(&mut self, message: &str, turn: usize) {
        self.current_turn = turn;

        let entities = self.extract_nouns_and_identifiers(message);
        let mut extracted = Vec::new();

        for entity in entities {
            self.record_entity_mention(&entity, turn, MentionType::Direct);
            extracted.push(entity);
        }

        // Store user message
        self.recent_user_messages.push_back(UserMessage {
            turn,
            content: message.to_string(),
            entities: extracted,
        });

        // Keep bounded
        while self.recent_user_messages.len() > MAX_MEMORY_TURNS {
            self.recent_user_messages.pop_front();
        }
    }

    /// Extract nouns and identifiers from text
    fn extract_nouns_and_identifiers(&self, text: &str) -> Vec<String> {
        let mut entities = Vec::new();

        const ENTITY_STOPWORDS: &[&str] = &[
            "update",
            "fix",
            "test",
            "look",
            "add",
            "remove",
            "create",
            "delete",
            "refactor",
            "implement",
            "check",
            "review",
        ];

        // Simple extraction: capitalize words, camelCase, PascalCase
        for word in text.split_whitespace() {
            let cleaned = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '.');
            let candidate = cleaned.split('.').next().unwrap_or(cleaned);
            let candidate_lower = candidate.to_ascii_lowercase();

            // Skip empty and short words
            if candidate.len() < 3 {
                continue;
            }

            // Skip common action words that are not entities.
            if ENTITY_STOPWORDS.contains(&candidate_lower.as_str()) {
                continue;
            }

            // Capitalized words (likely proper nouns)
            if candidate
                .chars()
                .next()
                .map(|c| c.is_uppercase())
                .unwrap_or(false)
            {
                entities.push(candidate.to_string());
                continue;
            }

            // camelCase or PascalCase (likely identifiers)
            let has_mixed_case = candidate.chars().any(|c| c.is_uppercase())
                && candidate.chars().any(|c| c.is_lowercase());

            if has_mixed_case {
                entities.push(candidate.to_string());
            }
        }

        entities
    }

    /// Record an entity mention
    fn record_entity_mention(&mut self, entity: &str, turn: usize, mention_type: MentionType) {
        let entity_lower = entity.to_lowercase();

        // Update or create history
        self.mentioned_entities
            .entry(entity_lower.clone())
            .and_modify(|history| history.record_mention(turn))
            .or_insert_with(|| MentionHistory::new(entity.to_string()));

        // Add to timeline
        self.entity_timeline.push_back(EntityMention {
            turn,
            entity: entity.to_string(),
            mention_type,
            file_context: None,
        });

        // Keep bounded
        while self.entity_timeline.len() > MAX_ENTITY_MENTIONS {
            self.entity_timeline.pop_front();
        }
    }

    /// Get recent entities (N most recent)
    pub fn get_recent_entities(&self, count: usize) -> Vec<String> {
        self.entity_timeline
            .iter()
            .rev()
            .take(count)
            .map(|mention| mention.entity.clone())
            .collect()
    }

    /// Resolve a pronoun to an entity
    pub fn resolve_pronoun(&self, pronoun: &str, turn: usize) -> Option<String> {
        let pronoun_lower = pronoun.to_lowercase();

        match pronoun_lower.as_str() {
            "it" => {
                // Look back at last mentioned entity
                self.entity_timeline
                    .iter()
                    .rev()
                    .find(|m| m.turn < turn)
                    .map(|m| m.entity.clone())
            }
            "that" | "this" => {
                // Look at last direct mention
                self.entity_timeline
                    .iter()
                    .rev()
                    .filter(|m| m.turn < turn)
                    .find(|m| matches!(m.mention_type, MentionType::Direct))
                    .map(|m| m.entity.clone())
            }
            "those" | "these" => {
                // Multiple entities - return most recent direct mention
                self.entity_timeline
                    .iter()
                    .rev()
                    .filter(|m| m.turn < turn && matches!(m.mention_type, MentionType::Direct))
                    .take(2)
                    .map(|m| m.entity.clone())
                    .next()
            }
            _ => None,
        }
    }

    /// Get all mentioned entities
    pub fn mentioned_entities(&self) -> &HashMap<String, MentionHistory> {
        &self.mentioned_entities
    }

    /// Get entity mention count
    pub fn mention_count(&self, entity: &str) -> usize {
        self.mentioned_entities
            .get(&entity.to_lowercase())
            .map(|h| h.mention_count)
            .unwrap_or(0)
    }

    /// Add file context
    pub fn add_file_context(&mut self, file: PathBuf) {
        self.recent_file_contexts.push_back(file);

        // Keep bounded
        while self.recent_file_contexts.len() > 20 {
            self.recent_file_contexts.pop_front();
        }
    }

    /// Get recent file contexts
    pub fn recent_file_contexts(&self, count: usize) -> Vec<&PathBuf> {
        self.recent_file_contexts.iter().rev().take(count).collect()
    }

    /// Check if entity was recently mentioned
    pub fn was_recently_mentioned(&self, entity: &str, within_turns: usize) -> bool {
        let cutoff_turn = self.current_turn.saturating_sub(within_turns);

        self.entity_timeline
            .iter()
            .rev()
            .any(|m| m.entity.eq_ignore_ascii_case(entity) && m.turn >= cutoff_turn)
    }

    /// Get context summary for recent conversation
    pub fn get_context_summary(&self, turns: usize) -> String {
        let messages: Vec<_> = self.recent_user_messages.iter().rev().take(turns).collect();

        if messages.is_empty() {
            return String::from("No recent context available");
        }

        let mut summary = String::from("Recent conversation:\n");
        for msg in messages.iter().rev() {
            summary.push_str(&format!("Turn {}: {}\n", msg.turn, msg.content));
        }

        summary
    }

    /// Clear old data to free memory
    pub fn clear_old_data(&mut self, keep_turns: usize) {
        let cutoff_turn = self.current_turn.saturating_sub(keep_turns);

        // Remove old entity timeline entries
        self.entity_timeline.retain(|m| m.turn >= cutoff_turn);

        // Remove old user messages
        self.recent_user_messages.retain(|m| m.turn >= cutoff_turn);

        // Clear resolved references from old turns
        self.resolved_references.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_entities() {
        let mut memory = ConversationMemory::new();

        memory.extract_entities("Update the Sidebar component in App.tsx", 1);

        assert_eq!(memory.mentioned_entities.len(), 2); // Sidebar, App
        assert!(memory.mentioned_entities.contains_key("sidebar"));
        assert!(memory.mentioned_entities.contains_key("app"));
    }

    #[test]
    fn test_pronoun_resolution_it() {
        let mut memory = ConversationMemory::new();

        // Turn 1: Mention Sidebar
        memory.extract_entities("The Sidebar is too wide", 1);

        // Turn 2: Reference with "it"
        let resolved = memory.resolve_pronoun("it", 2);

        assert!(resolved.is_some());
        assert_eq!(resolved.unwrap(), "Sidebar");
    }

    #[test]
    fn test_pronoun_resolution_that() {
        let mut memory = ConversationMemory::new();

        memory.extract_entities("Look at the Button component", 1);

        let resolved = memory.resolve_pronoun("that", 2);

        assert!(resolved.is_some());
        assert_eq!(resolved.unwrap(), "Button");
    }

    #[test]
    fn test_recent_entities() {
        let mut memory = ConversationMemory::new();

        memory.extract_entities("Update Sidebar", 1);
        memory.extract_entities("Fix Button", 2);
        memory.extract_entities("Test Form", 3);

        let recent = memory.get_recent_entities(2);

        assert_eq!(recent.len(), 2);
        assert_eq!(recent[0], "Form");
        assert_eq!(recent[1], "Button");
    }

    #[test]
    fn test_mention_count() {
        let mut memory = ConversationMemory::new();

        memory.extract_entities("Update Sidebar", 1);
        memory.extract_entities("The Sidebar is nice", 2);
        memory.extract_entities("Sidebar needs work", 3);

        assert_eq!(memory.mention_count("sidebar"), 3);
        assert_eq!(memory.mention_count("Button"), 0);
    }

    #[test]
    fn test_context_summary() {
        let mut memory = ConversationMemory::new();

        memory.extract_entities("First message", 1);
        memory.extract_entities("Second message", 2);

        let summary = memory.get_context_summary(2);

        assert!(summary.contains("First message"));
        assert!(summary.contains("Second message"));
    }
}