跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

activity.go

73 行 · 2.1 KB历史下载
1package cliconversation
2
3import (
4 "encoding/json"
5 "strings"
6)
7
8// ToolActivity is a display-only summary. Command bodies, file contents and
9// tool outputs stay in the CLI transcript. Approval has a separate protocol.
10type ToolActivity struct {
11 ID string `json:"id"`
12 Name string `json:"name"`
13 Detail string `json:"detail,omitempty"`
14 Status string `json:"status"`
15}
16
17func updateTools(tools []ToolActivity, role string, line []byte) ([]ToolActivity, bool) {
18 var msg struct {
19 Message struct {
20 Content []struct {
21 Type string `json:"type"`
22 ID string `json:"id"`
23 Name string `json:"name"`
24 Input map[string]json.RawMessage `json:"input"`
25 ToolUseID string `json:"tool_use_id"`
26 IsError bool `json:"is_error"`
27 } `json:"content"`
28 } `json:"message"`
29 }
30 if json.Unmarshal(line, &msg) != nil {
31 return tools, false
32 }
33 changed := false
34 for _, b := range msg.Message.Content {
35 if role == "user" && b.Type == "tool_result" {
36 for i := range tools {
37 if tools[i].ID == b.ToolUseID && tools[i].Status == "called" {
38 tools[i].Status = "completed"
39 if b.IsError {
40 tools[i].Status = "failed"
41 }
42 changed = true
43 break
44 }
45 }
46 }
47 if role != "assistant" || (b.Type != "tool_use" && b.Type != "server_tool_use") || !toolNamePattern.MatchString(b.Name) || b.ID == "" || len(b.ID) > 128 || len(tools) >= 64 {
48 continue
49 }
50 duplicate := false
51 for _, item := range tools {
52 if item.ID == b.ID {
53 duplicate = true
54 break
55 }
56 }
57 if duplicate {
58 continue
59 }
60 detail := ""
61 for _, key := range []string{"description", "subject", "file_path", "pattern", "query", "skill", "subagent_type"} {
62 var value string
63 if json.Unmarshal(b.Input[key], &value) == nil && strings.TrimSpace(value) != "" {
64 runes := []rune(strings.TrimSpace(value))
65 detail = string(runes[:min(len(runes), 160)])
66 break
67 }
68 }
69 tools = append(tools, ToolActivity{ID: b.ID, Name: b.Name, Detail: detail, Status: "called"})
70 changed = true
71 }
72 return tools, changed
73}