Repository navigation
Expand file tree
/
Copy pathv0_bash_agent.cs
More file actions
274 lines (239 loc) · 9.38 KB
/
Copy pathv0_bash_agent.cs
File metadata and controls
274 lines (239 loc) · 9.38 KB
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
#!/usr/bin/dotnet run
#:sdk Microsoft.NET.Sdk
#:package Anthropic@12.2.0
#:package dotenv.net@4.0.0
#:property LangVersion=latest
#:property ImplicitUsings=enable
#:property PublishAot=false
/*
* v0_bash_agent.cs - Mini Claude Code: Bash is All You Need (~60 lines core)
*
* 核心哲学: "Bash 就是一切"
* ========================
* 这是 Agent 的终极简化。构建了 v1-v3 之后,我们追问:Agent 的本质是什么?
*
* 答案: 一个工具 (bash) + 一个循环 = 完整的 Agent 能力
*
* 为什么 Bash 就够了:
* -----------------
* Unix 哲学说一切皆文件,一切皆可管道。Bash 是通往这个世界的大门:
*
* | 你需要 | Bash 命令 |
* |------------|----------------------------------------|
* | 读取文件 | cat, head, tail, grep |
* | 写入文件 | echo '...' > file, cat << 'EOF' > file |
* | 搜索 | find, grep, rg, ls |
* | 执行 | python, npm, dotnet, 任何命令 |
* | 子代理 | dotnet run v0_bash_agent.cs "task" |
*
* 最后一行是关键洞察:通过 bash 调用自身实现子代理!
* 无需 Task 工具,无需 Agent 注册表 - 只需进程生成的递归。
*
* 子代理工作原理:
* -------------
* 主 Agent
* |-- bash: dotnet run v0_bash_agent.cs "分析架构"
* |-- 子 Agent (隔离进程,干净历史)
* |-- bash: find . -name "*.cs"
* |-- bash: cat src/Program.cs
* |-- 通过 stdout 返回摘要
*
* 进程隔离 = 上下文隔离
*/
using System.Diagnostics;
using System.Text.Json;
using Anthropic;
using Anthropic.Models.Messages;
using dotenv.net;
DotEnv.Load();
var apiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")
?? throw new InvalidOperationException("ANTHROPIC_API_KEY not set");
var baseUrl = Environment.GetEnvironmentVariable("ANTHROPIC_BASE_URL");
var modelId = Environment.GetEnvironmentVariable("MODEL_ID") ?? "claude-sonnet-4-5-20250929";
var client = baseUrl is null
? new AnthropicClient() { ApiKey = apiKey }
: new AnthropicClient() { ApiKey = apiKey, BaseUrl = baseUrl };
var workDir = Directory.GetCurrentDirectory();
var isWindows = OperatingSystem.IsWindows();
var shellName = isWindows ? "powershell" : "bash";
var readExamples = isWindows
? "Get-Content, Select-String, Get-ChildItem, rg, gc"
: "cat, head, tail, grep, find, rg, ls";
var writeExamples = isWindows
? "Set-Content, Add-Content, @'...'@ | Set-Content"
: "echo '...' > file, sed -i, cat << 'EOF' > file";
var subagentExample = isWindows
? "dotnet run .\\v0_bash_agent.cs \"探索 src/ 并总结架构\""
: "dotnet run ./v0_bash_agent.cs \"探索 src/ 并总结架构\"";
// 唯一的工具:shell - 在不同系统上选择合适的命令解释器
var shellTool = new Tool
{
Name = "shell",
Description = isWindows
? """
执行 PowerShell 命令。常用模式:
- 读取: Get-Content, Select-String, Get-ChildItem, rg
- 写入: Set-Content, Add-Content, @'...'@ | Set-Content
- 子代理: dotnet run .\v0_bash_agent.cs "任务描述" (生成隔离 agent,返回摘要)
"""
: """
执行 shell 命令。常用模式:
- 读取: cat/head/tail, grep/find/rg/ls, wc -l
- 写入: echo 'content' > file, sed -i 's/old/new/g' file
- 子代理: dotnet run ./v0_bash_agent.cs "任务描述" (生成隔离 agent,返回摘要)
""",
InputSchema = new InputSchema
{
Properties = new Dictionary<string, JsonElement>
{
["command"] = JsonSerializer.SerializeToElement(new { type = "string", description = "要执行的命令" })
},
Required = ["command"]
}
};
var systemPrompt = $"""
你是一个位于 {workDir} 的 CLI agent。使用 {shellName} 命令解决问题。
规则:
- 行动优先,简短解释。
- 操作系统: {(isWindows ? "Windows" : "Linux/macOS")}
- 使用工具 `shell` 执行命令,不要假设 bash 一定存在。
- 读取文件: {readExamples}
- 写入文件: {writeExamples}
{(isWindows
? "Windows 约定: 优先使用 PowerShell 命令,例如 Get-ChildItem、Get-Content、Select-String、Set-Content、Copy-Item、Move-Item。路径可以使用 .\\relative\\path。"
: "Unix 约定: 优先使用 bash 常见命令,例如 ls、cat、grep、find、sed。路径可以使用 ./relative/path。")}
- 子代理: 对于复杂子任务,生成子代理:
{subagentExample}
何时使用子代理:
- 任务需要读取很多文件(隔离探索过程)
- 任务独立且自包含
- 你想避免用中间细节污染当前对话
子代理在隔离环境中运行,只返回最终摘要。
""";
// 核心 Agent 循环
async Task<string> ChatAsync(string prompt, List<MessageParam>? history = null)
{
history ??= [];
history.Add(new MessageParam { Role = Role.User, Content = prompt });
while (true)
{
// 1. 调用模型
var response = await client.Messages.Create(new MessageCreateParams
{
Model = modelId,
Messages = history,
System = systemPrompt,
Tools = [shellTool],
MaxTokens = 8000
});
// 2. 添加助手消息到历史 - 转换 ContentBlock 为 ContentBlockParam
var assistantContent = response.Content.Select<ContentBlock, ContentBlockParam>(c =>
{
if (c.TryPickText(out var text))
return new TextBlockParam { Text = text.Text };
if (c.TryPickToolUse(out var toolUse))
return new ToolUseBlockParam { ID = toolUse.ID, Name = toolUse.Name, Input = toolUse.Input };
throw new InvalidOperationException("Unknown content block type");
}).ToList();
history.Add(new MessageParam { Role = Role.Assistant, Content = assistantContent });
// 3. 如果没有工具调用,完成
if (response.StopReason != StopReason.ToolUse)
{
return string.Join("", response.Content
.Where(c => c.TryPickText(out _))
.Select(c => { c.TryPickText(out var t); return t!.Text; }));
}
// 4. 执行每个工具调用
var toolResults = new List<ContentBlockParam>();
foreach (var block in response.Content)
{
if (!block.TryPickToolUse(out var toolUse)) continue;
var command = toolUse.Input["command"].GetString()!;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"$ {command}");
Console.ResetColor();
var output = await RunShellAsync(command);
Console.WriteLine(string.IsNullOrEmpty(output) ? "(empty)" : output);
toolResults.Add(new ToolResultBlockParam
{
ToolUseID = toolUse.ID,
Content = output[..Math.Min(output.Length, 50000)]
});
}
// 5. 添加工具结果并继续循环
history.Add(new MessageParam { Role = Role.User, Content = toolResults });
}
}
// Shell 执行函数
async Task<string> RunShellAsync(string command)
{
try
{
var startInfo = new ProcessStartInfo
{
FileName = shellName,
WorkingDirectory = workDir,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
if (isWindows)
{
startInfo.ArgumentList.Add("-NoLogo");
startInfo.ArgumentList.Add("-NoProfile");
startInfo.ArgumentList.Add("-NonInteractive");
startInfo.ArgumentList.Add("-Command");
startInfo.ArgumentList.Add(command);
}
else
{
startInfo.ArgumentList.Add("-c");
startInfo.ArgumentList.Add(command);
}
using var process = new Process
{
StartInfo = startInfo
};
process.Start();
var stdout = await process.StandardOutput.ReadToEndAsync();
var stderr = await process.StandardError.ReadToEndAsync();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(300));
await process.WaitForExitAsync(cts.Token);
return (stdout + stderr).Trim();
}
catch (OperationCanceledException)
{
return "(timeout after 300s)";
}
catch (Exception ex)
{
return $"Error: {ex.Message}";
}
}
// 主入口
if (args.Length > 0)
{
// 子代理模式:执行任务并打印结果
Console.WriteLine(await ChatAsync(args[0]));
}
else
{
// 交互式 REPL 模式
var history = new List<MessageParam>();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Mini Claude Code v0 (C#) - {workDir}");
Console.WriteLine("输入 'exit' 退出\n");
Console.ResetColor();
while (true)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(">> ");
Console.ResetColor();
var query = Console.ReadLine();
if (string.IsNullOrWhiteSpace(query) || query is "q" or "exit" or "quit")
break;
Console.WriteLine(await ChatAsync(query, history));
Console.WriteLine();
}
}