echo_agent 0.1.0

AI Agent framework with ReAct loop, multi-provider LLM, tool execution, and A2A HTTP server
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
//! Git 版本控制工具
//!
//! 对标 Claude Code/Cursor,提供完整 Git 操作能力:
//! - git_status: 工作区状态
//! - git_diff: 差异比较(unstaged + staged)
//! - git_log: 提交历史
//! - git_blame: 逐行归属
//! - git_branch: 分支操作
//! - git_commit: 创建提交

use futures::future::BoxFuture;
use serde_json::Value;
use std::process::Command;

use crate::error::{Result, ToolError};
use crate::tools::{Tool, ToolParameters, ToolResult};

// ── Git status ──────────────────────────────────────────────────────────────

pub struct GitStatusTool;

impl Tool for GitStatusTool {
    fn name(&self) -> &str {
        "git_status"
    }

    fn description(&self) -> &str {
        "查看当前仓库的工作区状态:已修改、已暂存、未跟踪的文件列表"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "repo_path": {
                    "type": "string",
                    "description": "仓库路径(默认当前运行目录)"
                }
            },
            "required": []
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let repo_path = parameters
                .get("repo_path")
                .and_then(|v| v.as_str())
                .unwrap_or(".");

            let output = run_git(repo_path, &["status", "--short"])?;
            if output.is_empty() {
                Ok(ToolResult::success("工作区干净,没有变更".to_string()))
            } else {
                Ok(ToolResult::success(format!("Git 状态:\n{}", output)))
            }
        })
    }
}

// ── Git diff ────────────────────────────────────────────────────────────────

pub struct GitDiffTool;

impl Tool for GitDiffTool {
    fn name(&self) -> &str {
        "git_diff"
    }

    fn description(&self) -> &str {
        "查看代码差异:unstaged 变更、staged 变更、或指定分支/提交之间的差异"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "repo_path": {
                    "type": "string",
                    "description": "仓库路径(默认当前运行目录)"
                },
                "staged": {
                    "type": "boolean",
                    "description": "是否查看已暂存的变更(默认 false)"
                },
                "target": {
                    "type": "string",
                    "description": "比较目标:分支名、commit hash、或 HEAD~1"
                },
                "file_path": {
                    "type": "string",
                    "description": "只显示指定文件的差异"
                }
            },
            "required": []
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let repo_path = parameters
                .get("repo_path")
                .and_then(|v| v.as_str())
                .unwrap_or(".");

            let mut args = vec!["diff"];
            let staged = parameters
                .get("staged")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            if staged {
                args.push("--staged");
            }

            let target = parameters.get("target").and_then(|v| v.as_str());
            let file_path = parameters.get("file_path").and_then(|v| v.as_str());

            if let Some(t) = target {
                args.push(t);
            }
            if let Some(fp) = file_path {
                args.push("--");
                args.push(fp);
            }

            let output = run_git(repo_path, &args)?;
            if output.is_empty() {
                Ok(ToolResult::success("没有差异".to_string()))
            } else {
                Ok(ToolResult::success(format!("```diff\n{}```", output)))
            }
        })
    }
}

// ── Git log ─────────────────────────────────────────────────────────────────

pub struct GitLogTool;

impl Tool for GitLogTool {
    fn name(&self) -> &str {
        "git_log"
    }

    fn description(&self) -> &str {
        "查看 Git 提交历史,支持限制条数和格式选择"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "repo_path": {
                    "type": "string",
                    "description": "仓库路径(默认当前运行目录)"
                },
                "count": {
                    "type": "integer",
                    "description": "显示的提交条数(默认 20)"
                },
                "oneline": {
                    "type": "boolean",
                    "description": "单行模式(默认 true)"
                },
                "author": {
                    "type": "string",
                    "description": "按作者筛选"
                },
                "since": {
                    "type": "string",
                    "description": "起始日期,如 '2024-01-01'"
                }
            },
            "required": []
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let repo_path = parameters
                .get("repo_path")
                .and_then(|v| v.as_str())
                .unwrap_or(".");

            let count = parameters
                .get("count")
                .and_then(|v| v.as_u64())
                .unwrap_or(20);
            let oneline = parameters
                .get("oneline")
                .and_then(|v| v.as_bool())
                .unwrap_or(true);

            let mut args: Vec<&str> = vec!["log"];
            if oneline {
                args.push("--oneline");
            }
            let count_str = count.to_string();
            args.extend_from_slice(&["-n", &count_str]);

            let author = parameters.get("author").and_then(|v| v.as_str());
            let since = parameters.get("since").and_then(|v| v.as_str());
            let mut extra_args: Vec<String> = Vec::new();
            if let Some(a) = author {
                extra_args.push(format!("--author={}", a));
            }
            if let Some(s) = since {
                extra_args.push(format!("--since={}", s));
            }
            let extra_strs: Vec<&str> = extra_args.iter().map(|s| s.as_str()).collect();
            args.extend(&extra_strs);

            let output = run_git(repo_path, &args)?;
            if output.is_empty() {
                Ok(ToolResult::success("仓库没有提交记录".to_string()))
            } else {
                Ok(ToolResult::success(format!("提交历史:\n{}", output)))
            }
        })
    }
}

// ── Git blame ───────────────────────────────────────────────────────────────

pub struct GitBlameTool;

impl Tool for GitBlameTool {
    fn name(&self) -> &str {
        "git_blame"
    }

    fn description(&self) -> &str {
        "查看文件的逐行注释,显示每行代码的最后修改者和提交"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "要查看的文件路径(相对于仓库根目录)"
                },
                "repo_path": {
                    "type": "string",
                    "description": "仓库路径(默认当前运行目录)"
                },
                "start_line": {
                    "type": "integer",
                    "description": "起始行号"
                },
                "end_line": {
                    "type": "integer",
                    "description": "结束行号"
                }
            },
            "required": ["file_path"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;
            let repo_path = parameters
                .get("repo_path")
                .and_then(|v| v.as_str())
                .unwrap_or(".");

            let mut args: Vec<String> = vec!["blame".to_string()];
            if let (Some(start), Some(end)) = (
                parameters.get("start_line").and_then(|v| v.as_u64()),
                parameters.get("end_line").and_then(|v| v.as_u64()),
            ) {
                args.push("-L".to_string());
                args.push(format!("{},{}", start, end));
            }
            args.push(file_path.to_string());

            let str_args: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
            let output = run_git(repo_path, &str_args)?;
            Ok(ToolResult::success(output))
        })
    }
}

// ── Git branch ──────────────────────────────────────────────────────────────

pub struct GitBranchTool;

impl Tool for GitBranchTool {
    fn name(&self) -> &str {
        "git_branch"
    }

    fn description(&self) -> &str {
        "查看、创建或切换 Git 分支。不带参数列出所有分支,带 name 创建新分支"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "repo_path": {
                    "type": "string",
                    "description": "仓库路径(默认当前运行目录)"
                },
                "name": {
                    "type": "string",
                    "description": "新分支名称(如提供则创建分支)"
                },
                "switch": {
                    "type": "string",
                    "description": "切换到指定分支"
                },
                "delete": {
                    "type": "string",
                    "description": "删除指定分支(需要已合并)"
                }
            },
            "required": []
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let repo_path = parameters
                .get("repo_path")
                .and_then(|v| v.as_str())
                .unwrap_or(".");

            let action;
            let args: Vec<String>;

            if let Some(name) = parameters.get("name").and_then(|v| v.as_str()) {
                args = vec!["branch".to_string(), name.to_string()];
                action = format!("创建分支 '{}'", name);
            } else if let Some(target) = parameters.get("switch").and_then(|v| v.as_str()) {
                args = vec!["checkout".to_string(), target.to_string()];
                action = format!("切换到分支 '{}'", target);
            } else if let Some(target) = parameters.get("delete").and_then(|v| v.as_str()) {
                args = vec!["branch".to_string(), "-d".to_string(), target.to_string()];
                action = format!("删除分支 '{}'", target);
            } else {
                args = vec!["branch".to_string(), "-a".to_string()];
                action = "列出所有分支".to_string();
            }

            let str_args: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
            let output = run_git(repo_path, &str_args)?;
            Ok(ToolResult::success(format!("{}:\n{}", action, output)))
        })
    }
}

// ── Git commit ──────────────────────────────────────────────────────────────

pub struct GitCommitTool;

impl Tool for GitCommitTool {
    fn name(&self) -> &str {
        "git_commit"
    }

    fn description(&self) -> &str {
        "创建 Git 提交。需要先通过 git add 暂存文件。仅当用户明确请求时才调用此工具。"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "repo_path": {
                    "type": "string",
                    "description": "仓库路径(默认当前运行目录)"
                },
                "message": {
                    "type": "string",
                    "description": "提交信息"
                },
                "files": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "要暂存并提交的文件列表(空 = 提交所有已暂存的)"
                }
            },
            "required": ["message"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let repo_path = parameters
                .get("repo_path")
                .and_then(|v| v.as_str())
                .unwrap_or(".");
            let message = parameters
                .get("message")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("message".to_string()))?;

            // 如果指定了文件列表,先 git add
            if let Some(files) = parameters.get("files").and_then(|v| v.as_array()) {
                for f_val in files {
                    if let Some(f) = f_val.as_str() {
                        let add_args = ["add", f];
                        run_git(repo_path, &add_args)?;
                    }
                }
            }

            let commit_args = ["commit", "-m", message];
            let output = run_git(repo_path, &commit_args)?;
            Ok(ToolResult::success(format!("提交成功:\n{}", output)))
        })
    }
}

// ── Helper ──────────────────────────────────────────────────────────────────

fn run_git(repo_path: &str, args: &[&str]) -> Result<String> {
    let output = Command::new("git")
        .current_dir(repo_path)
        .args(args)
        .output()
        .map_err(|e| ToolError::ExecutionFailed {
            tool: "git".to_string(),
            message: format!("无法执行 git 命令(请确认已安装 git): {}", e),
        })?;

    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr);
        Err(ToolError::ExecutionFailed {
            tool: "git".to_string(),
            message: stderr.trim().to_string(),
        }
        .into())
    }
}