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
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
pub mod ask;
pub mod bash;
pub mod browser;
pub mod codegraph;
pub mod edit;
pub mod glob;
pub mod grep;
pub mod ls;
pub mod monitor;
pub mod multi_edit;
pub mod plan_mode;
pub mod read;
pub mod registry; // 工具注册中心
pub mod search;
pub mod skill;
pub mod task;
pub mod todo_write;
pub mod toolproxy; // 代理工具模块
pub mod webfetch;
pub mod websearch;
pub mod workflow;
pub mod write;

// Re-export proxy types for convenience
pub use toolproxy::{
    ProxyMetadata, ProxyTool, ProxyToolDef, ProxyToolExecutor, ProxyToolRequest, ProxyToolResponse,
};

use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::approval::RiskLevel;
use crate::skills::Skill;
use std::path::PathBuf;

/// Context for tool definition generation
/// Used to customize tool descriptions based on available features
#[derive(Debug, Clone, Default)]
pub struct ToolContext {
    /// Whether CodeGraph tools are available
    pub codegraph_available: bool,
}

/// Type alias for boxed tool
pub type BoxedTool = Box<dyn Tool>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
    pub name: String,
    pub description: String,
    pub parameters: Value,
    /// 是否为优先工具。true 时会在描述前添加 "[优先]" 提示,
    /// 让 LLM 更倾向选择此工具。默认 false。
    #[serde(default)]
    pub is_priority: bool,
}

impl Default for ToolDefinition {
    fn default() -> Self {
        Self {
            name: String::new(),
            description: String::new(),
            parameters: json!({"type": "object"}),
            is_priority: false,
        }
    }
}

impl ToolDefinition {
    /// 获取发送给 LLM 的描述(带优先标记)
    pub fn description_for_llm(&self) -> String {
        if self.is_priority {
            format!("[优先] {}", self.description)
        } else {
            self.description.clone()
        }
    }
}

#[async_trait]
pub trait Tool: Send + Sync {
    /// Get tool definition (must implement)
    fn definition(&self) -> ToolDefinition;

    /// Get tool definition with context (for dynamic descriptions)
    ///
    /// Default implementation calls definition(). Override this method
    /// if you need context-aware descriptions (e.g., different text
    /// when CodeGraph is available).
    fn definition_with_context(&self, _ctx: &ToolContext) -> ToolDefinition {
        self.definition()
    }

    async fn execute(&self, params: Value) -> Result<String>;

    /// Risk level of this tool. Defaults to Safe (read-only).
    /// Override in tools that modify state.
    fn risk_level(&self) -> RiskLevel {
        RiskLevel::Safe
    }
}

/// Default toolset without any skill integration. Kept for callers
/// (and the existing tests) that don't care about skills.
pub fn all_tools() -> Vec<Box<dyn Tool>> {
    all_tools_with_skills(Arc::new(Vec::new()))
}

/// Base toolset without workflow tools (to avoid duplicates).
fn base_tools(skills: Arc<Vec<Skill>>) -> Vec<Box<dyn Tool>> {
    vec![
        Box::new(ask::AskTool),
        Box::new(read::ReadTool),
        Box::new(write::WriteTool),
        Box::new(edit::EditTool),
        Box::new(multi_edit::MultiEditTool),
        Box::new(search::SearchTool),
        Box::new(grep::GrepTool),
        Box::new(glob::GlobTool),
        Box::new(ls::LsTool),
        Box::new(bash::BashTool),
        Box::new(browser::BrowserOpenTool),
        Box::new(todo_write::TodoWriteTool),
        Box::new(websearch::WebSearchTool::new()),
        Box::new(webfetch::WebFetchTool),
        Box::new(skill::SkillTool::new(skills)),
        Box::new(task::TaskTool),
        Box::new(task::TaskCreateTool),
        Box::new(task::TaskGetTool),
        Box::new(task::TaskListTool),
        Box::new(task::TaskStopTool),
        Box::new(plan_mode::EnterPlanModeTool),
        Box::new(plan_mode::ExitPlanModeTool),
        Box::new(monitor::MonitorTool),
    ]
}

/// Build the toolset with skill support but without provider.
pub fn all_tools_with_skills(skills: Arc<Vec<Skill>>) -> Vec<Box<dyn Tool>> {
    let mut tools = base_tools(skills);
    // Add workflow tools without provider
    tools.extend(workflow::workflow_tools());
    tools
}

/// Build toolset with Provider for AI-powered tools.
pub fn all_tools_with_provider(
    skills: Arc<Vec<Skill>>,
    provider: Arc<dyn crate::providers::Provider>,
) -> Vec<Box<dyn Tool>> {
    let mut tools = base_tools(skills);
    // Add AI-powered workflow tools (with provider)
    tools.extend(workflow::workflow_tools_with_provider(provider));
    tools
}

/// Generate tools description for system prompt
pub fn generate_tools_prompt() -> String {
    generate_tools_prompt_with_path(None)
}

/// Generate tools description with optional CodeGraph support
pub fn generate_tools_prompt_with_path(project_path: Option<&PathBuf>) -> String {
    // Build tool context based on CodeGraph availability
    let ctx = ToolContext {
        codegraph_available: project_path
            .map(|p| codegraph::should_inject_codegraph_tools(p))
            .unwrap_or(false),
    };

    let mut tools = base_tools(Arc::new(Vec::new()));

    // Add CodeGraph tools only if initialized (CLI installed + .codegraph exists)
    if ctx.codegraph_available {
        if let Some(path) = project_path {
            tools.extend(codegraph::codegraph_tools_with_auto_detect(path));
        }
    }

    // Add workflow tools
    tools.extend(workflow::workflow_tools());

    // 🎯 分类显示:优先工具 + 其他工具
    let mut priority_tools = Vec::new();
    let mut normal_tools = Vec::new();

    for tool in tools {
        // Use definition_with_context for dynamic descriptions
        let def = tool.definition_with_context(&ctx);
        if def.is_priority {
            priority_tools.push(def);
        } else {
            normal_tools.push(def);
        }
    }

    let mut lines = vec!["可用工具:".to_string()];

    // 优先工具(完整描述,包含适用场景)
    if !priority_tools.is_empty() {
        lines.push("\n【优先工具 - 必须优先考虑】".to_string());
        for def in priority_tools {
            // 使用 description_for_llm() 自动添加 [优先] 标记
            let full_desc = def.description_for_llm();
            // 优先工具保留完整描述(最多150字符)
            let desc = full_desc.split('\n').next().unwrap_or(&full_desc);
            if desc.len() > 150 {
                lines.push(format!(
                    "  {}: {}...",
                    def.name,
                    desc.chars().take(147).collect::<String>()
                ));
            } else {
                lines.push(format!("  {}: {}", def.name, desc));
            }
        }
    }

    // 其他工具(简要描述)
    if !normal_tools.is_empty() {
        lines.push("\n【其他工具】".to_string());
        for def in normal_tools {
            // 其他工具保持简要描述(前60字符)
            let desc = def
                .description
                .split('.')
                .next()
                .or_else(|| def.description.split('\n').next())
                .unwrap_or(&def.description);
            if desc.len() > 60 {
                lines.push(format!(
                    "  {}: {}...",
                    def.name,
                    desc.chars().take(57).collect::<String>()
                ));
            } else {
                lines.push(format!("  {}: {}", def.name, desc));
            }
        }
    }

    lines.join("\n")
}

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

    #[test]
    fn test_all_tools_includes_workflow_tools() {
        let tools = all_tools();
        let tool_names: Vec<String> = tools.iter().map(|t| t.definition().name).collect();

        // Verify workflow tools are present
        assert!(
            tool_names.contains(&"workflow_discover".to_string()),
            "workflow_discover should be in tools"
        );
        assert!(
            tool_names.contains(&"workflow_run".to_string()),
            "workflow_run should be in tools"
        );
        assert!(
            tool_names.contains(&"workflow_match".to_string()),
            "workflow_match should be in tools"
        );
    }

    #[test]
    fn test_generate_tools_prompt_includes_workflow() {
        let prompt = generate_tools_prompt();

        // Verify workflow tools appear in prompt
        assert!(
            prompt.contains("workflow_discover"),
            "prompt should mention workflow_discover"
        );
        assert!(
            prompt.contains("workflow_run"),
            "prompt should mention workflow_run"
        );
        assert!(
            prompt.contains("workflow_match"),
            "prompt should mention workflow_match"
        );
    }

    #[test]
    fn test_generate_tools_prompt_with_path_includes_codegraph() {
        let path = PathBuf::from(".");
        let prompt = generate_tools_prompt_with_path(Some(&path));

        // CodeGraph tools are only included when:
        // 1. CodeGraph CLI is installed
        // 2. Project has .codegraph directory
        // So we check based on actual conditions
        if codegraph::should_inject_codegraph_tools(&path) {
            assert!(
                prompt.contains("code_search"),
                "prompt should mention code_search when conditions met"
            );
            assert!(
                prompt.contains("code_callers"),
                "prompt should mention code_callers when conditions met"
            );
        } else {
            // When conditions not met, codegraph tools should NOT appear
            assert!(
                !prompt.contains("code_search"),
                "prompt should NOT mention code_search without .codegraph"
            );
        }
    }

    #[test]
    fn test_generate_tools_prompt_without_path_excludes_codegraph() {
        let prompt = generate_tools_prompt();

        // Verify codegraph tools NOT in prompt without path
        assert!(
            !prompt.contains("code_search"),
            "prompt should NOT mention code_search without path"
        );
    }

    #[test]
    fn test_tool_context_affects_grep_description() {
        use crate::tools::grep::GrepTool;

        // Without CodeGraph - should suggest using grep for definitions
        let ctx_no_codegraph = ToolContext {
            codegraph_available: false,
        };
        let def_no_cg = GrepTool.definition_with_context(&ctx_no_codegraph);
        assert!(
            def_no_cg.description.contains("用 grep 搜索"),
            "Without CodeGraph, grep should suggest using grep for definitions"
        );
        assert!(
            !def_no_cg.description.contains("code_search"),
            "Without CodeGraph, grep description should not mention code_search"
        );

        // With CodeGraph - should recommend code_search
        let ctx_with_codegraph = ToolContext {
            codegraph_available: true,
        };
        let def_with_cg = GrepTool.definition_with_context(&ctx_with_codegraph);
        assert!(
            def_with_cg.description.contains("code_search"),
            "With CodeGraph, grep should recommend code_search"
        );
        assert!(
            def_with_cg.description.contains("快10-100倍"),
            "With CodeGraph, grep should mention speed advantage"
        );
    }

    #[test]
    fn test_tool_context_affects_search_description() {
        use crate::tools::search::SearchTool;

        // Without CodeGraph
        let ctx_no_codegraph = ToolContext {
            codegraph_available: false,
        };
        let def_no_cg = SearchTool.definition_with_context(&ctx_no_codegraph);
        assert!(
            def_no_cg.description.contains("search 的适用场景"),
            "Without CodeGraph, search should show its own applicable scenarios"
        );

        // With CodeGraph
        let ctx_with_codegraph = ToolContext {
            codegraph_available: true,
        };
        let def_with_cg = SearchTool.definition_with_context(&ctx_with_codegraph);
        assert!(
            def_with_cg.description.contains("优先使用 code_search"),
            "With CodeGraph, search should mention code_search priority"
        );
    }

    #[test]
    fn test_tool_context_affects_glob_description() {
        use crate::tools::glob::GlobTool;

        // Without CodeGraph
        let ctx_no_codegraph = ToolContext {
            codegraph_available: false,
        };
        let def_no_cg = GlobTool.definition_with_context(&ctx_no_codegraph);
        assert!(
            def_no_cg.description.contains("glob 的适用场景"),
            "Without CodeGraph, glob should show its own applicable scenarios"
        );

        // With CodeGraph
        let ctx_with_codegraph = ToolContext {
            codegraph_available: true,
        };
        let def_with_cg = GlobTool.definition_with_context(&ctx_with_codegraph);
        assert!(
            def_with_cg.description.contains("优先使用 code_files"),
            "With CodeGraph, glob should mention code_files priority"
        );
    }

    #[test]
    fn test_generate_tools_prompt_dynamic_descriptions() {
        let path = PathBuf::from(".");
        let prompt = generate_tools_prompt_with_path(Some(&path));

        // Check based on actual CodeGraph availability
        if codegraph::should_inject_codegraph_tools(&path) {
            // When CodeGraph is available, grep should mention code_search
            assert!(
                prompt.contains("code_search") || prompt.contains("grep"),
                "Prompt should contain grep tool"
            );
        }

        // Both grep and search should always be present
        assert!(prompt.contains("grep"), "Prompt should contain grep tool");
        assert!(
            prompt.contains("search"),
            "Prompt should contain search tool"
        );
        assert!(prompt.contains("glob"), "Prompt should contain glob tool");
    }
}

/// Build toolset with Arc Provider (preferred method)
pub fn all_tools_with_arc_provider(
    skills: Arc<Vec<Skill>>,
    provider: Arc<dyn crate::providers::Provider>,
) -> Vec<Box<dyn Tool>> {
    all_tools_with_provider(skills, provider)
}

/// Build toolset with Box Provider (for CLI compatibility - safe implementation)
/// Uses clone_arc to safely convert Box to Arc without unsafe code.
pub fn all_tools_with_box_provider(
    skills: Arc<Vec<Skill>>,
    boxed_provider: Box<dyn crate::providers::Provider>,
) -> Vec<Box<dyn Tool>> {
    // Safe conversion: clone_arc creates a new Arc without unsafe pointer manipulation
    let arc_provider = boxed_provider.clone_arc();
    all_tools_with_provider(skills, arc_provider)
}

/// Build toolset with project path for CodeGraph integration.
pub fn all_tools_with_project_path(
    skills: Arc<Vec<Skill>>,
    project_path: PathBuf,
) -> Vec<Box<dyn Tool>> {
    let mut tools = base_tools(skills);
    // Add CodeGraph tools
    tools.extend(codegraph::codegraph_tools(&project_path));
    // Add workflow tools
    tools.extend(workflow::workflow_tools());
    tools
}

/// Build full toolset with provider and project path.
pub fn all_tools_full(
    skills: Arc<Vec<Skill>>,
    provider: Arc<dyn crate::providers::Provider>,
    project_path: PathBuf,
) -> Vec<Box<dyn Tool>> {
    let mut tools = base_tools(skills);
    // Add CodeGraph tools only if initialized (CLI installed + .codegraph exists)
    if codegraph::should_inject_codegraph_tools(&project_path) {
        tools.extend(codegraph::codegraph_tools(&project_path));
    }
    // Add AI-powered workflow tools
    tools.extend(workflow::workflow_tools_with_provider(provider));
    tools
}