matrixcode-core 0.4.27

MatrixCode Agent Core - Pure logic, no UI
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
//! Runtime Context Injection
//!
//! Provides dynamic context injection for:
//! - User context (CLAUDE.md files, preferences)
//! - System context (git status, date, workspace info)
//! - Environment context (platform, tools available)

use crate::lsp::LspServerInfo;
use chrono::Local;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

/// User context from CLAUDE.md and preferences
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserContext {
    /// Content from CLAUDE.md file
    pub claude_md_content: Option<String>,
    /// User preferences (from accumulated memory)
    pub preferences: Vec<String>,
    /// Current date/time
    pub current_date: String,
    /// User's preferred language
    pub language: Option<String>,
}

impl Default for UserContext {
    fn default() -> Self {
        Self {
            claude_md_content: None,
            preferences: Vec::new(),
            current_date: Local::now().format("%Y-%m-%d").to_string(),
            language: None,
        }
    }
}

/// System context (git, workspace, tools)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemContext {
    /// Git branch name
    pub git_branch: Option<String>,
    /// Git status (clean/dirty)
    pub git_status: Option<String>,
    /// Current working directory
    pub working_directory: Option<String>,
    /// Project root directory
    pub project_root: Option<String>,
    /// Detected project type
    pub project_type: Option<ProjectType>,
    /// Available tools
    pub available_tools: Vec<String>,
    /// Platform info
    pub platform: String,
    /// LSP servers status (dynamic injection)
    #[serde(default)]
    pub lsp_servers: Vec<LspServerInfo>,
}

impl Default for SystemContext {
    fn default() -> Self {
        Self {
            git_branch: None,
            git_status: None,
            working_directory: None,
            project_root: None,
            project_type: None,
            available_tools: Vec::new(),
            platform: std::env::consts::OS.to_string(),
            lsp_servers: Vec::new(),
        }
    }
}

/// Detected project type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ProjectType {
    Rust,
    NodeJs,
    Python,
    Go,
    Java,
    Cpp,
    Mixed,
    Unknown,
}

impl ProjectType {
    /// Detect project type from directory contents
    pub fn detect<P: AsRef<Path>>(dir: P) -> Self {
        let dir = dir.as_ref();

        let has_cargo = dir.join("Cargo.toml").exists();
        let has_package_json = dir.join("package.json").exists();
        let has_pyproject = dir.join("pyproject.toml").exists() || dir.join("setup.py").exists();
        let has_go_mod = dir.join("go.mod").exists();
        let has_pom = dir.join("pom.xml").exists() || dir.join("build.gradle").exists();
        let has_cmake = dir.join("CMakeLists.txt").exists() || dir.join("Makefile").exists();

        let types = [
            has_cargo,
            has_package_json,
            has_pyproject,
            has_go_mod,
            has_pom,
            has_cmake,
        ];
        let count = types.iter().filter(|&&x| x).count();

        if count > 1 {
            return ProjectType::Mixed;
        }

        if has_cargo {
            ProjectType::Rust
        } else if has_package_json {
            ProjectType::NodeJs
        } else if has_pyproject {
            ProjectType::Python
        } else if has_go_mod {
            ProjectType::Go
        } else if has_pom {
            ProjectType::Java
        } else if has_cmake {
            ProjectType::Cpp
        } else {
            ProjectType::Unknown
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            ProjectType::Rust => "rust",
            ProjectType::NodeJs => "nodejs",
            ProjectType::Python => "python",
            ProjectType::Go => "go",
            ProjectType::Java => "java",
            ProjectType::Cpp => "cpp",
            ProjectType::Mixed => "mixed",
            ProjectType::Unknown => "unknown",
        }
    }
}

/// Context injector for runtime context
pub struct ContextInjector {
    /// Working directory
    working_dir: PathBuf,
    /// Cache of user context
    user_context_cache: Option<UserContext>,
    /// Cache of system context
    system_context_cache: Option<SystemContext>,
    /// Whether to refresh cache
    dirty: bool,
}

impl ContextInjector {
    /// Create a new context injector
    pub fn new<P: Into<PathBuf>>(working_dir: P) -> Self {
        Self {
            working_dir: working_dir.into(),
            user_context_cache: None,
            system_context_cache: None,
            dirty: true,
        }
    }

    /// Mark cache as dirty (need refresh)
    pub fn invalidate(&mut self) {
        self.dirty = true;
    }

    /// Set LSP servers info (for dynamic injection)
    /// This updates the cached system context with LSP server information
    pub fn set_lsp_servers(&mut self, servers: Vec<LspServerInfo>) {
        if let Some(ref mut ctx) = self.system_context_cache {
            ctx.lsp_servers = servers;
        } else {
            let mut ctx = SystemContext::default();
            ctx.lsp_servers = servers;
            self.system_context_cache = Some(ctx);
        }
    }

    /// Get user context
    pub fn get_user_context(&mut self) -> &UserContext {
        if self.dirty || self.user_context_cache.is_none() {
            self.user_context_cache = Some(self.collect_user_context());
            self.dirty = false;
        }
        self.user_context_cache.as_ref().unwrap()
    }

    /// Get system context
    pub fn get_system_context(&mut self) -> &SystemContext {
        if self.dirty || self.system_context_cache.is_none() {
            self.system_context_cache = Some(self.collect_system_context());
            self.dirty = false; // Reset dirty flag after refresh
        }
        self.system_context_cache.as_ref().unwrap()
    }

    /// Collect user context
    fn collect_user_context(&self) -> UserContext {
        let mut ctx = UserContext::default();

        // Read CLAUDE.md
        let claude_md_path = self.working_dir.join("CLAUDE.md");
        if claude_md_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&claude_md_path) {
                ctx.claude_md_content = Some(content);
            }
        }

        // Also check parent directories
        if ctx.claude_md_content.is_none() {
            if let Some(parent) = self.working_dir.parent() {
                let parent_claude_md = parent.join("CLAUDE.md");
                if parent_claude_md.exists() {
                    if let Ok(content) = std::fs::read_to_string(&parent_claude_md) {
                        ctx.claude_md_content = Some(content);
                    }
                }
            }
        }

        ctx
    }

    /// Collect system context
    fn collect_system_context(&self) -> SystemContext {
        let mut ctx = SystemContext::default();

        // Git info
        if let Ok(output) = std::process::Command::new("git")
            .args(["branch", "--show-current"])
            .current_dir(&self.working_dir)
            .output()
        {
            if output.status.success() {
                ctx.git_branch = Some(String::from_utf8_lossy(&output.stdout).trim().to_string());
            }
        }

        if let Ok(output) = std::process::Command::new("git")
            .args(["status", "--porcelain"])
            .current_dir(&self.working_dir)
            .output()
        {
            if output.status.success() {
                let status = String::from_utf8_lossy(&output.stdout);
                ctx.git_status = if status.trim().is_empty() {
                    Some("clean".to_string())
                } else {
                    Some(format!("dirty ({} changes)", status.lines().count()))
                };
            }
        }

        // Working directory
        ctx.working_directory = self.working_dir.to_str().map(|s| s.to_string());
        ctx.project_root = ctx.working_directory.clone();

        // Project type
        ctx.project_type = Some(ProjectType::detect(&self.working_dir));

        // Available tools (check common tools)
        let tools = ["git", "cargo", "npm", "python", "go", "docker"];
        for tool in tools {
            if Self::tool_available(tool) {
                ctx.available_tools.push(tool.to_string());
            }
        }

        ctx
    }

    /// Check if a tool is available
    fn tool_available(tool: &str) -> bool {
        #[cfg(unix)]
        {
            std::process::Command::new("which")
                .arg(tool)
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false)
        }

        #[cfg(windows)]
        {
            std::process::Command::new("where")
                .arg(tool)
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false)
        }
    }

    /// Render user context as prompt section
    pub fn render_user_context(&mut self) -> String {
        let ctx = self.get_user_context();
        let mut parts = Vec::new();

        // Date
        parts.push(format!(
            "<currentDate>\n{}\n</currentDate>",
            ctx.current_date
        ));

        // CLAUDE.md content
        if let Some(ref claude_md) = ctx.claude_md_content {
            parts.push(format!(
                "<userPreferences>\n{}\n</userPreferences>",
                claude_md
            ));
        }

        parts.join("\n\n")
    }

    /// Render system context as prompt section
    pub fn render_system_context(&mut self) -> String {
        let ctx = self.get_system_context();
        let mut parts = Vec::new();

        // Working directory
        if let Some(ref dir) = ctx.working_directory {
            parts.push(format!("<workingDirectory>\n{}\n</workingDirectory>", dir));
        }

        // Git info
        if let Some(ref branch) = ctx.git_branch {
            let git_info = if let Some(ref status) = ctx.git_status {
                format!("Branch: {}\nStatus: {}", branch, status)
            } else {
                format!("Branch: {}", branch)
            };
            parts.push(format!("<gitContext>\n{}\n</gitContext>", git_info));
        }

        // Project type
        if let Some(ref pt) = ctx.project_type {
            parts.push(format!("<projectType>\n{}\n</projectType>", pt.as_str()));
        }

        // Available tools
        if !ctx.available_tools.is_empty() {
            parts.push(format!(
                "<availableTools>\n{}\n</availableTools>",
                ctx.available_tools.join(", ")
            ));
        }

        // LSP servers (dynamic injection)
        if !ctx.lsp_servers.is_empty() {
            let servers_info = ctx
                .lsp_servers
                .iter()
                .map(|s| {
                    let status = s.status.label();
                    format!("{}: {} [{}]", s.language, s.name, status)
                })
                .collect::<Vec<_>>()
                .join("\n");
            parts.push(format!("<lspServers>\n{}\n</lspServers>", servers_info));
        }

        parts.join("\n\n")
    }

    /// Render full context (for injection into prompt)
    pub fn render_full_context(&mut self) -> String {
        let user = self.render_user_context();
        let system = self.render_system_context();

        format!("<context>\n{}\n\n{}\n</context>", user, system)
    }
}

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

    #[test]
    fn test_user_context_default() {
        let ctx = UserContext::default();
        assert!(ctx.claude_md_content.is_none());
        assert!(!ctx.current_date.is_empty());
    }

    #[test]
    fn test_system_context_default() {
        let ctx = SystemContext::default();
        assert!(ctx.git_branch.is_none());
        assert!(!ctx.platform.is_empty());
    }

    #[test]
    fn test_project_type_detect_rust() {
        let temp_dir = tempfile::tempdir().unwrap();
        std::fs::write(temp_dir.path().join("Cargo.toml"), "").unwrap();

        assert_eq!(ProjectType::detect(temp_dir.path()), ProjectType::Rust);
    }

    #[test]
    fn test_project_type_detect_mixed() {
        let temp_dir = tempfile::tempdir().unwrap();
        std::fs::write(temp_dir.path().join("Cargo.toml"), "").unwrap();
        std::fs::write(temp_dir.path().join("package.json"), "").unwrap();

        assert_eq!(ProjectType::detect(temp_dir.path()), ProjectType::Mixed);
    }

    #[test]
    fn test_context_invalidator() {
        let mut injector = ContextInjector::new(std::env::current_dir().unwrap());

        // Get once
        let _ = injector.get_user_context();

        // Invalidate
        injector.invalidate();

        // Should refresh
        let _ = injector.get_user_context();
    }

    #[test]
    fn test_render_user_context() {
        let mut injector = ContextInjector::new(std::env::current_dir().unwrap());
        let rendered = injector.render_user_context();

        assert!(rendered.contains("<currentDate>"));
    }

    #[test]
    fn test_render_system_context() {
        let mut injector = ContextInjector::new(std::env::current_dir().unwrap());
        let rendered = injector.render_system_context();

        assert!(rendered.contains("<workingDirectory>") || rendered.contains("<projectType>"));
    }
}