forked from sheng-jie/learn-cc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv8_agent_teams.cs
More file actions
595 lines (518 loc) · 23.6 KB
/
v8_agent_teams.cs
File metadata and controls
595 lines (518 loc) · 23.6 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/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
/*
* v8_agent_teams.cs - Mini Claude Code: Agent Teams (~550 lines)
*
* 对应上游: s09_agent_teams.py
*
* 核心哲学: "能互相交谈的队友"
* ============================
* v3 的子代理是一次性的: spawn → execute → return → destroyed。
* 真实团队不是这样的 —— 成员持续存在,可以互相沟通。
*
* 子代理 vs 队友:
* --------------
* 子代理 (v3): spawn -> 执行 -> 返回摘要 -> 销毁
* 队友 (v8): spawn -> 工作 -> 空闲 -> 工作 -> ... -> 关闭
*
* 通信机制:
* --------
* .team/config.json .team/inbox/
* +---------------------------+ +------------------+
* | {"team_name": "default", | | alice.jsonl |
* | "members": [ | | bob.jsonl |
* | {"name":"alice", | | lead.jsonl |
* | "role":"coder", | +------------------+
* | "status":"idle"} |
* | ]} | send_message("alice", "fix bug"):
* +---------------------------+ 写入 alice.jsonl (追加)
*
* read_inbox("alice"):
* 读取并清空 alice.jsonl (drain)
*
* 5 种消息类型:
* -----------
* message 普通文本消息
* broadcast 发送给所有队友
* shutdown_request 请求优雅关闭 (v9)
* shutdown_response 批准/拒绝关闭 (v9)
* plan_approval_response 批准/拒绝计划 (v9)
*/
using System.Diagnostics;
using System.Text;
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";
using var client = baseUrl is null
? new AnthropicClient() { ApiKey = apiKey }
: new AnthropicClient() { ApiKey = apiKey, BaseUrl = baseUrl };
var workDir = Directory.GetCurrentDirectory();
var teamDir = Path.Combine(workDir, ".team");
var inboxDir = Path.Combine(teamDir, "inbox");
var validMsgTypes = new HashSet<string>
{ "message", "broadcast", "shutdown_request", "shutdown_response", "plan_approval_response" };
// =============================================================================
// MessageBus - JSONL 收件箱
// =============================================================================
var bus = new MessageBus(inboxDir, validMsgTypes);
// =============================================================================
// 系统提示
// =============================================================================
var systemPrompt = $"""
你是一个位于 {workDir} 的团队领导。
生成队友并通过收件箱进行沟通。
规则:
- 用 spawn_teammate 创建持久化队友
- 用 send_message / broadcast 与队友沟通
- 用 read_inbox 检查收件箱
- 用 list_teammates 查看状态
- 行动优先,不要只是解释。
""";
// =============================================================================
// 基础工具实现
// =============================================================================
string SafePath(string p)
{
var full = Path.GetFullPath(Path.Combine(workDir, p));
if (!full.StartsWith(workDir)) throw new InvalidOperationException($"Path escapes workspace: {p}");
return full;
}
async Task<string> RunBashAsync(string command)
{
string[] dangerous = ["rm -rf /", "sudo", "shutdown", "reboot"];
if (dangerous.Any(d => command.Contains(d)))
return "Error: Dangerous command blocked";
try
{
using var proc = new Process();
proc.StartInfo = new ProcessStartInfo
{
FileName = "bash",
Arguments = $"-c \"{command.Replace("\"", "\\\"")}\"",
WorkingDirectory = workDir,
RedirectStandardOutput = true, RedirectStandardError = true,
UseShellExecute = false, CreateNoWindow = true
};
proc.Start();
var stdout = await proc.StandardOutput.ReadToEndAsync();
var stderr = await proc.StandardError.ReadToEndAsync();
await proc.WaitForExitAsync();
var output = (stdout + stderr).Trim();
return string.IsNullOrEmpty(output) ? "(no output)" : output[..Math.Min(output.Length, 50000)];
}
catch (Exception ex) { return $"Error: {ex.Message}"; }
}
string RunRead(string path, int? limit = null)
{
try
{
var lines = File.ReadAllLines(SafePath(path));
if (limit.HasValue && limit.Value < lines.Length)
lines = [.. lines.Take(limit.Value), $"... ({lines.Length - limit.Value} more)"];
var text = string.Join("\n", lines);
return text[..Math.Min(text.Length, 50000)];
}
catch (Exception ex) { return $"Error: {ex.Message}"; }
}
string RunWrite(string path, string content)
{
try
{
var fp = SafePath(path);
Directory.CreateDirectory(Path.GetDirectoryName(fp)!);
File.WriteAllText(fp, content);
return $"Wrote {content.Length} bytes";
}
catch (Exception ex) { return $"Error: {ex.Message}"; }
}
string RunEdit(string path, string oldText, string newText)
{
try
{
var fp = SafePath(path);
var content = File.ReadAllText(fp);
if (!content.Contains(oldText)) return $"Error: Text not found in {path}";
var idx = content.IndexOf(oldText, StringComparison.Ordinal);
File.WriteAllText(fp, string.Concat(content.AsSpan(0, idx), newText, content.AsSpan(idx + oldText.Length)));
return $"Edited {path}";
}
catch (Exception ex) { return $"Error: {ex.Message}"; }
}
// =============================================================================
// Lead 工具定义 (9 个工具)
// =============================================================================
var tools = new List<Tool>
{
// 以下 4 个基础工具同时供 lead 和 teammate 使用
new() { Name = "bash", Description = "运行 shell 命令。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["command"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["command"] } },
new() { Name = "read_file", Description = "读取文件内容。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["path"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["limit"] = JsonSerializer.SerializeToElement(new { type = "integer" }) },
Required = ["path"] } },
new() { Name = "write_file", Description = "写入内容到文件。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["path"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["content"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["path", "content"] } },
new() { Name = "edit_file", Description = "替换文件中的文本。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["path"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["old_text"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["new_text"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["path", "old_text", "new_text"] } },
new() { Name = "spawn_teammate", Description = "生成一个在独立线程运行的持久化队友。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["name"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["role"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["prompt"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["name", "role", "prompt"] } },
new() { Name = "list_teammates", Description = "列出所有队友的名称、角色、状态。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement>(),
Required = [] } },
new() { Name = "send_message", Description = "向队友收件箱发送消息。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["to"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["content"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["msg_type"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["to", "content"] } },
new() { Name = "read_inbox", Description = "读取并清空 lead 的收件箱。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement>(),
Required = [] } },
new() { Name = "broadcast", Description = "向所有队友发送消息。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["content"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["content"] } }
};
// =============================================================================
// TeammateManager - 持久化命名代理
// =============================================================================
// 基础工具执行器 (bash/read/write/edit),供 lead 和 teammate 共用
async Task<string> ExecuteBaseToolAsync(string toolName, IReadOnlyDictionary<string, JsonElement> args)
{
return toolName switch
{
"bash" => await RunBashAsync(args["command"].GetString()!),
"read_file" => RunRead(args["path"].GetString()!,
args.TryGetValue("limit", out var l) ? l.GetInt32() : null),
"write_file" => RunWrite(args["path"].GetString()!, args["content"].GetString()!),
"edit_file" => RunEdit(args["path"].GetString()!,
args["old_text"].GetString()!, args["new_text"].GetString()!),
_ => $"Unknown tool: {toolName}"
};
}
var team = new TeammateManager(teamDir, bus, client, modelId, workDir, validMsgTypes, ExecuteBaseToolAsync, tools);
// =============================================================================
// 工具分发
// =============================================================================
async Task<string> ExecuteToolAsync(string name, IReadOnlyDictionary<string, JsonElement> args)
{
return name switch
{
"bash" or "read_file" or "write_file" or "edit_file"
=> await ExecuteBaseToolAsync(name, args),
"spawn_teammate" => team.Spawn(args["name"].GetString()!, args["role"].GetString()!, args["prompt"].GetString()!),
"list_teammates" => team.ListAll(),
"send_message" => bus.Send("lead", args["to"].GetString()!, args["content"].GetString()!,
args.TryGetValue("msg_type", out var mt) ? mt.GetString() ?? "message" : "message"),
"read_inbox" => JsonSerializer.Serialize(bus.ReadInbox("lead"), Helpers.JsonPretty),
"broadcast" => bus.Broadcast("lead", args["content"].GetString()!, team.MemberNames()),
_ => $"Unknown tool: {name}"
};
}
// =============================================================================
// 主 Agent 循环
// =============================================================================
async Task AgentLoopAsync(List<MessageParam> messages)
{
while (true)
{
// 排空 lead 收件箱
var inbox = bus.ReadInbox("lead");
if (inbox.Count > 0)
{
messages.Add(new MessageParam { Role = Role.User,
Content = $"<inbox>{JsonSerializer.Serialize(inbox, Helpers.JsonPretty)}</inbox>" });
messages.Add(new MessageParam { Role = Role.Assistant, Content = "Noted inbox messages." });
}
var response = await client.Messages.Create(new MessageCreateParams
{
Model = modelId, Messages = [.. messages],
System = systemPrompt, Tools = [.. tools], MaxTokens = 8000
});
foreach (var block in response.Content)
if (block.TryPickText(out var text)) Console.WriteLine(text.Text);
messages.Add(new MessageParam { Role = Role.Assistant, Content = Helpers.ToParams(response.Content) });
if (response.StopReason != StopReason.ToolUse)
return;
var results = new List<ToolResultBlockParam>();
foreach (var block in response.Content)
{
if (!block.TryPickToolUse(out var tu)) continue;
var output = await ExecuteToolAsync(tu.Name, tu.Input);
Console.WriteLine($"\n> {tu.Name}: {(output.Length > 200 ? output[..200] + "..." : output)}");
results.Add(new ToolResultBlockParam { ToolUseID = tu.ID, Content = output });
}
messages.Add(new MessageParam { Role = Role.User, Content = results.Select(r => (ContentBlockParam)r).ToList() });
}
}
// =============================================================================
// 主 REPL
// =============================================================================
Console.WriteLine($"Mini Claude Code v8 (Agent Teams) - {workDir}");
Console.WriteLine("命令: /team (列出队友), /inbox (查看收件箱)");
Console.WriteLine("输入 'exit' 退出。\n");
var history = new List<MessageParam>();
while (true)
{
Console.Write("You: ");
var userInput = Console.ReadLine()?.Trim();
if (string.IsNullOrEmpty(userInput) || userInput.ToLower() is "exit" or "quit" or "q")
break;
if (userInput == "/team") { Console.WriteLine(team.ListAll()); continue; }
if (userInput == "/inbox")
{
Console.WriteLine(JsonSerializer.Serialize(bus.ReadInbox("lead"), Helpers.JsonPretty));
continue;
}
history.Add(new MessageParam { Role = Role.User, Content = userInput });
try { await AgentLoopAsync(history); }
catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); }
Console.WriteLine();
}
// =============================================================================
// 类型定义
// =============================================================================
class MessageBus
{
private readonly string _dir;
private readonly HashSet<string> _validTypes;
private readonly object _lock = new();
public MessageBus(string inboxDir, HashSet<string> validTypes)
{
_dir = inboxDir;
_validTypes = validTypes;
Directory.CreateDirectory(_dir);
}
public string Send(string sender, string to, string content,
string msgType = "message", Dictionary<string, object>? extra = null)
{
if (!_validTypes.Contains(msgType))
return $"Error: Invalid type '{msgType}'";
var msg = new Dictionary<string, object>
{
["type"] = msgType, ["from"] = sender,
["content"] = content, ["timestamp"] = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0
};
if (extra != null) foreach (var kv in extra) msg[kv.Key] = kv.Value;
lock (_lock)
{
var path = Path.Combine(_dir, $"{to}.jsonl");
File.AppendAllText(path, JsonSerializer.Serialize(msg) + "\n");
}
return $"Sent {msgType} to {to}";
}
public List<Dictionary<string, object>> ReadInbox(string name)
{
lock (_lock)
{
var path = Path.Combine(_dir, $"{name}.jsonl");
if (!File.Exists(path)) return [];
var messages = new List<Dictionary<string, object>>();
foreach (var line in File.ReadAllLines(path))
{
if (!string.IsNullOrWhiteSpace(line))
messages.Add(JsonSerializer.Deserialize<Dictionary<string, object>>(line)!);
}
File.WriteAllText(path, ""); // drain
return messages;
}
}
public string Broadcast(string sender, string content, List<string> teammates)
{
var count = 0;
foreach (var name in teammates)
{
if (name != sender) { Send(sender, name, content, "broadcast"); count++; }
}
return $"Broadcast to {count} teammates";
}
}
class TeammateManager
{
private readonly string _dir;
private readonly string _configPath;
private readonly MessageBus _bus;
private readonly AnthropicClient _client;
private readonly string _modelId;
private readonly string _workDir;
private readonly HashSet<string> _validMsgTypes;
private readonly Func<string, IReadOnlyDictionary<string, JsonElement>, Task<string>> _execBaseTool;
private readonly List<Tool> _allTools;
private Dictionary<string, object> _config;
// teammate 可用的工具名称子集 (从 lead tools 中筛选)
private static readonly HashSet<string> TeammateToolNames =
["bash", "read_file", "write_file", "edit_file", "send_message", "read_inbox"];
public TeammateManager(string teamDir, MessageBus bus, AnthropicClient client,
string modelId, string workDir, HashSet<string> validMsgTypes,
Func<string, IReadOnlyDictionary<string, JsonElement>, Task<string>> execBaseTool,
List<Tool> tools)
{
_dir = teamDir;
_bus = bus;
_client = client;
_modelId = modelId;
_workDir = workDir;
_validMsgTypes = validMsgTypes;
_execBaseTool = execBaseTool;
_allTools = tools;
Directory.CreateDirectory(_dir);
_configPath = Path.Combine(_dir, "config.json");
_config = LoadConfig();
}
private Dictionary<string, object> LoadConfig()
{
if (File.Exists(_configPath))
return JsonSerializer.Deserialize<Dictionary<string, object>>(File.ReadAllText(_configPath))!;
return new Dictionary<string, object>
{
["team_name"] = "default",
["members"] = JsonSerializer.SerializeToElement(new List<object>())
};
}
private void SaveConfig()
=> File.WriteAllText(_configPath, JsonSerializer.Serialize(_config, new JsonSerializerOptions { WriteIndented = true }));
private List<Dictionary<string, object>> GetMembers()
{
if (_config["members"] is JsonElement je)
return je.Deserialize<List<Dictionary<string, object>>>() ?? [];
return [];
}
private void SetMembers(List<Dictionary<string, object>> members)
=> _config["members"] = JsonSerializer.SerializeToElement(members);
public string Spawn(string name, string role, string prompt)
{
var members = GetMembers();
var existing = members.FirstOrDefault(m => m["name"]?.ToString() == name);
if (existing != null)
{
var status = existing["status"]?.ToString();
if (status is not ("idle" or "shutdown"))
return $"Error: '{name}' is currently {status}";
existing["status"] = "working";
existing["role"] = role;
}
else
{
members.Add(new Dictionary<string, object>
{ ["name"] = name, ["role"] = role, ["status"] = "working" });
}
SetMembers(members);
SaveConfig();
_ = Task.Run(() => TeammateLoopAsync(name, role, prompt));
return $"Spawned '{name}' (role: {role})";
}
private async Task TeammateLoopAsync(string name, string role, string prompt)
{
var sysPrompt = $"You are '{name}', role: {role}, at {_workDir}. " +
"Use send_message to communicate. Complete your task.";
var messages = new List<MessageParam>
{ new() { Role = Role.User, Content = prompt } };
var tmTools = TeammateTools();
for (var i = 0; i < 50; i++)
{
var inbox = _bus.ReadInbox(name);
foreach (var msg in inbox)
messages.Add(new MessageParam { Role = Role.User, Content = JsonSerializer.Serialize(msg) });
Message response;
try
{
response = await _client.Messages.Create(new MessageCreateParams
{
Model = _modelId, System = sysPrompt,
Messages = [.. messages], Tools = [.. tmTools], MaxTokens = 8000
});
}
catch { break; }
messages.Add(new MessageParam { Role = Role.Assistant, Content = Helpers.ToParams(response.Content) });
if (response.StopReason != StopReason.ToolUse) break;
var results = new List<ToolResultBlockParam>();
foreach (var block in response.Content)
{
if (!block.TryPickToolUse(out var tu)) continue;
var output = await ExecTeammateToolAsync(name, tu.Name, tu.Input);
Console.WriteLine($" [{name}] {tu.Name}: {(output.Length > 120 ? output[..120] + "..." : output)}");
results.Add(new ToolResultBlockParam { ToolUseID = tu.ID, Content = output });
}
messages.Add(new MessageParam { Role = Role.User, Content = results.Select(r => (ContentBlockParam)r).ToList() });
}
var ms = GetMembers();
var member = ms.FirstOrDefault(m => m["name"]?.ToString() == name);
if (member != null && member["status"]?.ToString() != "shutdown")
{
member["status"] = "idle";
SetMembers(ms);
SaveConfig();
}
}
private async Task<string> ExecTeammateToolAsync(string sender, string toolName, IReadOnlyDictionary<string, JsonElement> args)
{
return toolName switch
{
// bash/read_file/write_file/edit_file 复用共享的基础工具
"bash" or "read_file" or "write_file" or "edit_file"
=> await _execBaseTool(toolName, args),
"send_message" => _bus.Send(sender, args["to"].GetString()!, args["content"].GetString()!,
args.TryGetValue("msg_type", out var mt) ? mt.GetString() ?? "message" : "message"),
"read_inbox" => JsonSerializer.Serialize(_bus.ReadInbox(sender), Helpers.JsonPretty),
_ => $"Unknown tool: {toolName}"
};
}
private List<Tool> TeammateTools()
=> _allTools.Where(t => TeammateToolNames.Contains(t.Name)).ToList();
public string ListAll()
{
var members = GetMembers();
if (members.Count == 0) return "No teammates.";
var lines = new List<string> { $"Team: {_config.GetValueOrDefault("team_name", "default")}" };
foreach (var m in members)
lines.Add($" {m["name"]} ({m["role"]}): {m["status"]}");
return string.Join("\n", lines);
}
public List<string> MemberNames()
=> GetMembers().Select(m => m["name"]?.ToString() ?? "").ToList();
}
static class Helpers
{
public static readonly JsonSerializerOptions JsonPretty = new() { WriteIndented = true };
public static List<ContentBlockParam> ToParams(IReadOnlyList<ContentBlock> content) =>
content.Select<ContentBlock, ContentBlockParam>(c =>
{
if (c.TryPickText(out var t)) return new TextBlockParam { Text = t.Text };
if (c.TryPickToolUse(out var tu)) return new ToolUseBlockParam { ID = tu.ID, Name = tu.Name, Input = tu.Input };
throw new InvalidOperationException("Unknown content block type");
}).ToList();
}