forked from sheng-jie/learn-cc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv9_team_protocols.cs
More file actions
595 lines (520 loc) · 27.1 KB
/
v9_team_protocols.cs
File metadata and controls
595 lines (520 loc) · 27.1 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
/*
* v9_team_protocols.cs - Mini Claude Code: Team Protocols (~550 lines)
*
* 对应上游: s10_team_protocols.py
*
* 核心哲学: "同一个 request_id 握手模式驱动关闭与计划审批两种协议"
* =====================================================================
* v8 的队友可以沟通,但缺少结构化协议。
* v9 添加了两种 request_id 关联模式:
*
* 关闭协议 FSM:
* Lead Teammate
* shutdown_request --> receives request
* (req_id=abc) decides: approve?
* shutdown_response <-- {req_id=abc, approve=true}
* status -> "shutdown", thread stops
*
* 计划审批 FSM:
* Teammate Lead
* plan_approval --> reviews plan text
* (req_id=xyz) approve/reject?
* plan_approval_response <-- {req_id=xyz, approve=true}
*
* 追踪器: {request_id: {target|from: name, status: pending|approved|rejected}}
*/
using System.Collections.Concurrent;
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";
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" };
// =============================================================================
// Request trackers - correlate by request_id
// =============================================================================
var shutdownRequests = new ConcurrentDictionary<string, Dictionary<string, string>>();
var planRequests = new ConcurrentDictionary<string, Dictionary<string, string>>();
// =============================================================================
// MessageBus
// =============================================================================
var bus = new MessageBus(inboxDir, validMsgTypes);
// =============================================================================
// 系统提示
// =============================================================================
var systemPrompt = $"""
你是一个位于 {workDir} 的团队领导。
管理队友,使用关闭和计划审批协议。
- 用 shutdown_request 请求队友优雅关闭。
- 用 plan_approval 审批/拒绝队友提交的计划。
规则: 行动优先,不要只是解释。
""";
// =============================================================================
// 基础工具实现 (lead 和 teammate 共用)
// =============================================================================
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 和 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}"
};
}
// =============================================================================
// Lead 协议处理器
// =============================================================================
string HandleShutdownRequest(string teammate)
{
var reqId = Guid.NewGuid().ToString()[..8];
shutdownRequests[reqId] = new Dictionary<string, string>
{ ["target"] = teammate, ["status"] = "pending" };
bus.Send("lead", teammate, "Please shut down gracefully.",
"shutdown_request", new Dictionary<string, object> { ["request_id"] = reqId });
return $"Shutdown request {reqId} sent to '{teammate}' (status: pending)";
}
string HandlePlanReview(string requestId, bool approve, string feedback = "")
{
if (!planRequests.TryGetValue(requestId, out var req))
return $"Error: Unknown plan request_id '{requestId}'";
req["status"] = approve ? "approved" : "rejected";
bus.Send("lead", req["from"], feedback, "plan_approval_response",
new Dictionary<string, object> { ["request_id"] = requestId, ["approve"] = approve, ["feedback"] = feedback });
return $"Plan {req["status"]} for '{req["from"]}'";
}
// =============================================================================
// Lead 工具定义 (12 个工具)
// =============================================================================
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"] } },
// v9 新增: 协议工具
new() { Name = "shutdown_request", Description = "请求队友优雅关闭,返回 request_id。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> { ["teammate"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["teammate"] } },
new() { Name = "shutdown_response", Description = "回应关闭请求。approve=true 则关闭,false 继续工作。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["request_id"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["approve"] = JsonSerializer.SerializeToElement(new { type = "boolean" }),
["reason"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = ["request_id", "approve"] } },
new() { Name = "plan_approval", Description = "队友提交计划 / 领导审批计划。",
InputSchema = new InputSchema {
Properties = new Dictionary<string, JsonElement> {
["request_id"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["approve"] = JsonSerializer.SerializeToElement(new { type = "boolean" }),
["feedback"] = JsonSerializer.SerializeToElement(new { type = "string" }),
["plan"] = JsonSerializer.SerializeToElement(new { type = "string" }) },
Required = [] } }
};
// =============================================================================
// TeammateManager with shutdown + plan approval
// =============================================================================
var team = new TeammateManager(teamDir, bus, client, modelId, workDir, validMsgTypes,
shutdownRequests, planRequests, 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()),
"shutdown_request" => HandleShutdownRequest(args["teammate"].GetString()!),
"plan_approval" => HandlePlanReview(args["request_id"].GetString()!, args["approve"].GetBoolean(),
args.TryGetValue("feedback", out var fb) ? fb.GetString() ?? "" : ""),
_ => $"Unknown tool: {name}"
};
}
// =============================================================================
// 主 Agent 循环
// =============================================================================
async Task AgentLoopAsync(List<MessageParam> messages)
{
while (true)
{
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 v9 (Team Protocols) - {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 dir, HashSet<string> validTypes) { _dir = dir; _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) { File.AppendAllText(Path.Combine(_dir, $"{to}.jsonl"), 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 msgs = File.ReadAllLines(path).Where(l => !string.IsNullOrWhiteSpace(l))
.Select(l => JsonSerializer.Deserialize<Dictionary<string, object>>(l)!).ToList();
File.WriteAllText(path, "");
return msgs;
}
}
public string Broadcast(string sender, string content, List<string> teammates)
{
var c = 0; foreach (var n in teammates) if (n != sender) { Send(sender, n, content, "broadcast"); c++; }
return $"Broadcast to {c} teammates";
}
}
class TeammateManager
{
private readonly string _dir, _configPath, _modelId, _workDir;
private readonly MessageBus _bus;
private readonly AnthropicClient _client;
private readonly HashSet<string> _validMsgTypes;
private readonly ConcurrentDictionary<string, Dictionary<string, string>> _shutdownReqs, _planReqs;
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",
"shutdown_response", "plan_approval"];
public TeammateManager(string dir, MessageBus bus, AnthropicClient client, string modelId,
string workDir, HashSet<string> validMsgTypes,
ConcurrentDictionary<string, Dictionary<string, string>> shutdownReqs,
ConcurrentDictionary<string, Dictionary<string, string>> planReqs,
Func<string, IReadOnlyDictionary<string, JsonElement>, Task<string>> execBaseTool,
List<Tool> tools)
{
_dir = dir; _bus = bus; _client = client; _modelId = modelId;
_workDir = workDir; _validMsgTypes = validMsgTypes;
_shutdownReqs = shutdownReqs; _planReqs = planReqs;
_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, Helpers.JsonPretty));
private List<Dictionary<string, object>> GetMembers()
=> _config["members"] is JsonElement je ? je.Deserialize<List<Dictionary<string, object>>>() ?? [] : [];
private void SetMembers(List<Dictionary<string, object>> m) => _config["members"] = JsonSerializer.SerializeToElement(m);
public string Spawn(string name, string role, string prompt)
{
var members = GetMembers();
var existing = members.FirstOrDefault(m => m["name"]?.ToString() == name);
if (existing != null)
{
if (existing["status"]?.ToString() is not ("idle" or "shutdown")) return $"Error: '{name}' is currently {existing["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}. " +
"Submit plans via plan_approval before major work. Respond to shutdown_request with shutdown_response.";
var messages = new List<MessageParam> { new() { Role = Role.User, Content = prompt } };
var tmTools = TeammateTools();
var shouldExit = false;
for (var i = 0; i < 50 && !shouldExit; 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)}");
if (tu.Name == "shutdown_response" && tu.Input.TryGetValue("approve", out var ap) && ap.GetBoolean())
shouldExit = true;
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"] = shouldExit ? "shutdown" : "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),
"shutdown_response" => HandleShutdownResponse(sender, args),
"plan_approval" => HandlePlanSubmit(sender, args),
_ => $"Unknown tool: {toolName}"
};
}
private string HandleShutdownResponse(string sender, IReadOnlyDictionary<string, JsonElement> args)
{
var reqId = args["request_id"].GetString()!;
var approve = args["approve"].GetBoolean();
if (_shutdownReqs.TryGetValue(reqId, out var r))
r["status"] = approve ? "approved" : "rejected";
_bus.Send(sender, "lead", args.TryGetValue("reason", out var rs) ? rs.GetString() ?? "" : "",
"shutdown_response", new Dictionary<string, object> { ["request_id"] = reqId, ["approve"] = approve });
return approve ? "Shutdown approved" : "Shutdown rejected";
}
private string HandlePlanSubmit(string sender, IReadOnlyDictionary<string, JsonElement> args)
{
var planText = args.TryGetValue("plan", out var p) ? p.GetString() ?? "" : "";
var reqId = Guid.NewGuid().ToString()[..8];
_planReqs[reqId] = new Dictionary<string, string> { ["from"] = sender, ["plan"] = planText, ["status"] = "pending" };
_bus.Send(sender, "lead", planText, "plan_approval_response",
new Dictionary<string, object> { ["request_id"] = reqId, ["plan"] = planText });
return $"Plan submitted (request_id={reqId}). Waiting for lead approval.";
}
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();
}