@@ -2,7 +2,9 @@ package acp |
2 | 2 |
|
3 | 3 | import ( |
4 | 4 | "encoding/json" |
| 5 | + "net/url" |
5 | 6 | "strings" |
| 7 | + "unicode" |
6 | 8 | "unicode/utf8" |
7 | 9 |
|
8 | 10 | "github.com/Gitlawb/zero/internal/agent" |
@@ -44,12 +46,125 @@ func toolKindFor(name string) string { |
44 | 46 |
|
45 | 47 | // toolTitle builds a concise human title, e.g. "read_file src/main.go". |
46 | 48 | func toolTitle(name, rawArgs string) string { |
| 49 | + if browser, ok := browserToolDetails(name); ok { |
| 50 | + return browserToolTitle(browser.Command, rawArgs) |
| 51 | + } |
47 | 52 | if hint := primaryArgHint(rawArgs); hint != "" { |
48 | 53 | return name + " " + hint |
49 | 54 | } |
50 | 55 | return name |
51 | 56 | } |
52 | 57 |
|
| 58 | +// browserToolDetails identifies ZERO's local browser helpers without treating |
| 59 | +// similarly named MCP tools as browser automation. The descriptor intentionally |
| 60 | +// contains no request data: ACP tool input is already protocol-visible, but a |
| 61 | +// durable UI must not need to retain text, local CDP targets, or full URLs just |
| 62 | +// to recognise the browser operation. |
| 63 | +func browserToolDetails(name string) (*BrowserToolDetails, bool) { |
| 64 | + const prefix = "browser_" |
| 65 | + command, ok := strings.CutPrefix(name, prefix) |
| 66 | + if !ok { |
| 67 | + return nil, false |
| 68 | + } |
| 69 | + switch command { |
| 70 | + case "install", "launch", "connect", "open", "snapshot", "click", "type", "press", "action": |
| 71 | + return &BrowserToolDetails{Version: 1, Command: command}, true |
| 72 | + default: |
| 73 | + return nil, false |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +const zeroBrowserMetaKey = "github.com/Gitlawb/zero/browser" |
| 78 | + |
| 79 | +// attachBrowserToolDetails stores ZERO's browser descriptor in ACP's reserved |
| 80 | +// extension channel. Keeping this in one helper prevents start, result, and |
| 81 | +// permission payloads from drifting onto different wire shapes. |
| 82 | +func attachBrowserToolDetails(update *ToolCallUpdate, name string) { |
| 83 | + browser, ok := browserToolDetails(name) |
| 84 | + if !ok { |
| 85 | + return |
| 86 | + } |
| 87 | + raw, err := json.Marshal(browser) |
| 88 | + if err != nil { |
| 89 | + return |
| 90 | + } |
| 91 | + update.Meta = map[string]json.RawMessage{zeroBrowserMetaKey: raw} |
| 92 | +} |
| 93 | + |
| 94 | +// browserToolTitle avoids putting browser_type text, an attached DevTools |
| 95 | +// endpoint, or a URL query/fragment in a tool-card title. Those values can |
| 96 | +// carry credentials or session data; the UI only needs the operation and, for |
| 97 | +// navigation, a human-recognisable origin. |
| 98 | +func browserToolTitle(command, rawArgs string) string { |
| 99 | + switch command { |
| 100 | + case "action": |
| 101 | + action, ok := exactJSONStringArg(rawArgs, "command") |
| 102 | + if !ok { |
| 103 | + return "browser action" |
| 104 | + } |
| 105 | + if action, ok := tools.NormalizedBrowserActionCommand(action); ok { |
| 106 | + return "browser action " + action |
| 107 | + } |
| 108 | + return "browser action" |
| 109 | + case "open": |
| 110 | + rawURL, ok := exactJSONStringArg(rawArgs, "url") |
| 111 | + if !ok { |
| 112 | + return "browser open" |
| 113 | + } |
| 114 | + normalized, err := tools.NormalizeBrowserOpenURL(rawURL) |
| 115 | + if err != nil { |
| 116 | + return "browser open" |
| 117 | + } |
| 118 | + u, err := url.Parse(normalized) |
| 119 | + if err != nil || u.Scheme == "" || u.Host == "" { |
| 120 | + return "browser open" |
| 121 | + } |
| 122 | + origin := u.Scheme + "://" + u.Host |
| 123 | + if !browserTitleTextSafe(origin) { |
| 124 | + return "browser open" |
| 125 | + } |
| 126 | + return "browser open " + truncateHint(origin) |
| 127 | + default: |
| 128 | + return "browser " + command |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// browserTitleTextSafe validates text after URL parsing has decoded escaped |
| 133 | +// UTF-8 in the host. Valid UTF-8 alone is not presentation-safe: control, |
| 134 | +// format/bidi, and line/paragraph separator runes can reorder or split the |
| 135 | +// permission label shown to a user. The execution URL remains unchanged. |
| 136 | +func browserTitleTextSafe(text string) bool { |
| 137 | + if !utf8.ValidString(text) { |
| 138 | + return false |
| 139 | + } |
| 140 | + for _, r := range text { |
| 141 | + if unicode.IsControl(r) || unicode.Is(unicode.Cf, r) || unicode.Is(unicode.Zl, r) || unicode.Is(unicode.Zp, r) { |
| 142 | + return false |
| 143 | + } |
| 144 | + } |
| 145 | + return true |
| 146 | +} |
| 147 | + |
| 148 | +// exactJSONStringArg mirrors ZERO's map-based tool argument decoding: only the |
| 149 | +// exact JSON key is considered, and a non-string value is invalid. In |
| 150 | +// particular, an incidental "URL" key must not change a permission title when |
| 151 | +// browser_open will only read "url". |
| 152 | +func exactJSONStringArg(rawArgs, key string) (string, bool) { |
| 153 | + var args map[string]json.RawMessage |
| 154 | + if json.Unmarshal([]byte(rawArgs), &args) != nil { |
| 155 | + return "", false |
| 156 | + } |
| 157 | + raw, ok := args[key] |
| 158 | + if !ok { |
| 159 | + return "", false |
| 160 | + } |
| 161 | + var value string |
| 162 | + if json.Unmarshal(raw, &value) != nil { |
| 163 | + return "", false |
| 164 | + } |
| 165 | + return value, true |
| 166 | +} |
| 167 | + |
53 | 168 | // primaryArgHint extracts the most relevant argument (path/pattern/command) from |
54 | 169 | // raw JSON arguments. Best-effort; returns "" when it can't parse. |
55 | 170 | func primaryArgHint(rawArgs string) string { |
@@ -89,14 +204,16 @@ func rawInput(args string) json.RawMessage { |
89 | 204 | // toolCallStart maps an advertised ZERO tool call to the initial ACP "tool_call" |
90 | 205 | // update (status in_progress — ZERO executes immediately after advertising). |
91 | 206 | func toolCallStart(call agent.ToolCall) ToolCallUpdate { |
92 | | - return ToolCallUpdate{ |
| 207 | + upd := ToolCallUpdate{ |
93 | 208 | SessionUpdate: UpdateToolCall, |
94 | 209 | ToolCallID: call.ID, |
95 | 210 | Title: toolTitle(call.Name, call.Arguments), |
96 | 211 | Kind: toolKindFor(call.Name), |
97 | 212 | Status: ToolStatusInProgress, |
98 | 213 | RawInput: rawInput(call.Arguments), |
99 | 214 | } |
| 215 | + attachBrowserToolDetails(&upd, call.Name) |
| 216 | + return upd |
100 | 217 | } |
101 | 218 |
|
102 | 219 | // toolCallResult maps a finished ZERO tool result to a "tool_call_update". |
@@ -116,6 +233,7 @@ func toolCallResult(result agent.ToolResult) ToolCallUpdate { |
116 | 233 | if locs := toolResultLocations(result); len(locs) > 0 { |
117 | 234 | upd.Locations = locs |
118 | 235 | } |
| 236 | + attachBrowserToolDetails(&upd, result.Name) |
119 | 237 | return upd |
120 | 238 | } |
121 | 239 |
|
|