GeezerVison is showing github.com — fix(acp): show shell command in ACP tool calls (#32304) · comio/opencode@5146142
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/acp/event.ts +13-7Lines changed: 13 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -80,10 +80,11 @@ export class Subscription { | 80 | 80 | async replayMessage(message: SessionMessageResponse) { | 81 | 81 | if (message.info.role !== "assistant" && message.info.role !== "user") return | 82 | 82 |
| | 83 | + const cwd = message.info.role === "assistant" ? message.info.path?.cwd : undefined | 83 | 84 | for (const part of message.parts) { | 84 | 85 | await this.recordFetchedPart(message.info.sessionID, message, part) | 85 | 86 | if (part.type === "tool") { | 86 | | - await this.handleToolPart(message.info.sessionID, part) | | 87 | + await this.handleToolPart(message.info.sessionID, part, cwd ?? process.cwd()) | 87 | 88 | continue | 88 | 89 | } | 89 | 90 | await this.replayContentPart(message, part) | @@ -146,7 +147,7 @@ export class Subscription { | 146 | 147 | }), | 147 | 148 | ) | 148 | 149 | if (part.type === "tool") { | 149 | | - await this.handleToolPart(session.id, part) | | 150 | + await this.handleToolPart(session.id, part, session.cwd) | 150 | 151 | } | 151 | 152 | } | 152 | 153 |
| @@ -231,16 +232,16 @@ export class Subscription { | 231 | 232 | ) | 232 | 233 | } | 233 | 234 |
| 234 | | - private async handleToolPart(sessionId: string, part: ToolPart) { | 235 | | - await this.toolStart(sessionId, part) | | 235 | + private async handleToolPart(sessionId: string, part: ToolPart, cwd: string) { | | 236 | + await this.toolStart(sessionId, part, cwd) | 236 | 237 |
| 237 | 238 | switch (part.state.status) { | 238 | 239 | case "pending": | 239 | 240 | this.shellSnapshots.delete(part.callID) | 240 | 241 | return | 241 | 242 |
| 242 | 243 | case "running": | 243 | | - await this.runningTool(sessionId, part) | | 244 | + await this.runningTool(sessionId, part, cwd) | 244 | 245 | return | 245 | 246 |
| 246 | 247 | case "completed": | @@ -253,6 +254,7 @@ export class Subscription { | 253 | 254 | toolCallId: part.callID, | 254 | 255 | toolName: part.tool, | 255 | 256 | state: part.state, | | 257 | + cwd, | 256 | 258 | }), | 257 | 259 | }, | 258 | 260 | }) | @@ -268,14 +270,15 @@ export class Subscription { | 268 | 270 | toolCallId: part.callID, | 269 | 271 | toolName: part.tool, | 270 | 272 | state: part.state, | | 273 | + cwd, | 271 | 274 | }), | 272 | 275 | }, | 273 | 276 | }) | 274 | 277 | return | 275 | 278 | } | 276 | 279 | } | 277 | 280 |
| 278 | | - private async runningTool(sessionId: string, part: ToolPart) { | | 281 | + private async runningTool(sessionId: string, part: ToolPart, cwd: string) { | 279 | 282 | if (part.state.status !== "running") return | 280 | 283 |
| 281 | 284 | const output = part.tool === "bash" ? shellOutputSnapshot(part.state) : undefined | @@ -289,6 +292,7 @@ export class Subscription { | 289 | 292 | toolCallId: part.callID, | 290 | 293 | toolName: part.tool, | 291 | 294 | state: part.state, | | 295 | + cwd, | 292 | 296 | }), | 293 | 297 | }, | 294 | 298 | }) | @@ -306,12 +310,13 @@ export class Subscription { | 306 | 310 | toolName: part.tool, | 307 | 311 | state: part.state, | 308 | 312 | output, | | 313 | + cwd, | 309 | 314 | }), | 310 | 315 | }, | 311 | 316 | }) | 312 | 317 | } | 313 | 318 |
| 314 | | - private async toolStart(sessionId: string, part: ToolPart) { | | 319 | + private async toolStart(sessionId: string, part: ToolPart, cwd: string) { | 315 | 320 | if (this.toolStarts.has(part.callID)) return | 316 | 321 | this.toolStarts.add(part.callID) | 317 | 322 | await this.input.connection.sessionUpdate({ | @@ -322,6 +327,7 @@ export class Subscription { | 322 | 327 | toolCallId: part.callID, | 323 | 328 | toolName: part.tool, | 324 | 329 | state: part.state, | | 330 | + cwd, | 325 | 331 | }), | 326 | 332 | }, | 327 | 333 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/acp/tool.ts +65-19Lines changed: 65 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { isAbsolute, resolve } from "path" | 1 | 2 | import type { ToolCall, ToolCallContent, ToolCallLocation, ToolCallUpdate, ToolKind } from "@agentclientprotocol/sdk" | 2 | 3 |
| 3 | 4 | export type ToolInput = Record<string, unknown> | @@ -69,10 +70,16 @@ export function toToolKind(toolName: string): ToolKind { | 69 | 70 | } | 70 | 71 | } | 71 | 72 |
| 72 | | -export function toLocations(toolName: string, input: ToolInput): ToolCallLocation[] { | | 73 | +export function toLocations(toolName: string, input: ToolInput, cwd?: string): ToolCallLocation[] { | 73 | 74 | const tool = toolName.toLocaleLowerCase() | 74 | 75 |
| 75 | 76 | switch (tool) { | | 77 | + case "bash": | | 78 | + case "shell": { | | 79 | + const workdir = shellWorkdir(input, cwd) | | 80 | + return workdir ? [{ path: workdir }] : [] | | 81 | + } | | 82 | + | 76 | 83 | case "read": | 77 | 84 | case "edit": | 78 | 85 | case "write": | @@ -88,10 +95,6 @@ export function toLocations(toolName: string, input: ToolInput): ToolCallLocatio | 88 | 95 | case "context7_get_library_docs": | 89 | 96 | return locationFrom(input.path) | 90 | 97 |
| 91 | | - case "bash": | 92 | | - case "shell": | 93 | | - return [] | 94 | | - | 95 | 98 | default: | 96 | 99 | return [] | 97 | 100 | } | @@ -122,14 +125,15 @@ export function pendingToolCall(input: { | 122 | 125 | readonly toolCallId: string | 123 | 126 | readonly toolName: string | 124 | 127 | readonly state: { readonly input: ToolInput; readonly title?: string } | | 128 | + readonly cwd?: string | 125 | 129 | }): ToolCall { | 126 | 130 | return { | 127 | 131 | toolCallId: input.toolCallId, | 128 | | - title: input.state.title || input.toolName, | | 132 | + title: toolTitle(input.toolName, input.state.input, input.state.title), | 129 | 133 | kind: toToolKind(input.toolName), | 130 | 134 | status: "pending", | 131 | | - locations: toLocations(input.toolName, input.state.input), | 132 | | - rawInput: input.state.input, | | 135 | + locations: toLocations(input.toolName, input.state.input, input.cwd), | | 136 | + rawInput: rawInput(input.toolName, input.state.input, input.cwd), | 133 | 137 | } | 134 | 138 | } | 135 | 139 |
| @@ -138,6 +142,7 @@ export function runningToolUpdate(input: { | 138 | 142 | readonly toolName: string | 139 | 143 | readonly state: RunningToolState | 140 | 144 | readonly output?: string | | 145 | + readonly cwd?: string | 141 | 146 | }): ToolCallUpdate { | 142 | 147 | const content = input.output | 143 | 148 | ? [ | @@ -155,9 +160,9 @@ export function runningToolUpdate(input: { | 155 | 160 | toolCallId: input.toolCallId, | 156 | 161 | status: "in_progress", | 157 | 162 | kind: toToolKind(input.toolName), | 158 | | - title: input.state.title ?? input.toolName, | 159 | | - locations: toLocations(input.toolName, input.state.input), | 160 | | - rawInput: input.state.input, | | 163 | + title: toolTitle(input.toolName, input.state.input, input.state.title), | | 164 | + locations: toLocations(input.toolName, input.state.input, input.cwd), | | 165 | + rawInput: rawInput(input.toolName, input.state.input, input.cwd), | 161 | 166 | ...(content ? { content } : {}), | 162 | 167 | } | 163 | 168 | } | @@ -166,29 +171,32 @@ export function duplicateRunningToolUpdate(input: { | 166 | 171 | readonly toolCallId: string | 167 | 172 | readonly toolName: string | 168 | 173 | readonly state: RunningToolState | | 174 | + readonly cwd?: string | 169 | 175 | }): ToolCallUpdate { | 170 | 176 | return { | 171 | 177 | toolCallId: input.toolCallId, | 172 | 178 | status: "in_progress", | 173 | 179 | kind: toToolKind(input.toolName), | 174 | | - title: input.state.title ?? input.toolName, | 175 | | - locations: toLocations(input.toolName, input.state.input), | 176 | | - rawInput: input.state.input, | | 180 | + title: toolTitle(input.toolName, input.state.input, input.state.title), | | 181 | + locations: toLocations(input.toolName, input.state.input, input.cwd), | | 182 | + rawInput: rawInput(input.toolName, input.state.input, input.cwd), | 177 | 183 | } | 178 | 184 | } | 179 | 185 |
| 180 | 186 | export function completedToolUpdate(input: { | 181 | 187 | readonly toolCallId: string | 182 | 188 | readonly toolName: string | 183 | | - readonly state: CompletedToolState & { readonly title: string } | | 189 | + readonly state: CompletedToolState & { readonly title?: string } | | 190 | + readonly cwd?: string | 184 | 191 | }): ToolCallUpdate { | 185 | 192 | return { | 186 | 193 | toolCallId: input.toolCallId, | 187 | 194 | status: "completed", | 188 | 195 | kind: toToolKind(input.toolName), | 189 | | - title: input.state.title, | | 196 | + title: toolTitle(input.toolName, input.state.input, input.state.title), | | 197 | + locations: toLocations(input.toolName, input.state.input, input.cwd), | 190 | 198 | content: completedToolContent(input.toolName, input.state), | 191 | | - rawInput: input.state.input, | | 199 | + rawInput: rawInput(input.toolName, input.state.input, input.cwd), | 192 | 200 | rawOutput: completedToolRawOutput(input.state), | 193 | 201 | } | 194 | 202 | } | @@ -197,13 +205,15 @@ export function errorToolUpdate(input: { | 197 | 205 | readonly toolCallId: string | 198 | 206 | readonly toolName: string | 199 | 207 | readonly state: ErrorToolState | | 208 | + readonly cwd?: string | 200 | 209 | }): ToolCallUpdate { | 201 | 210 | return { | 202 | 211 | toolCallId: input.toolCallId, | 203 | 212 | status: "failed", | 204 | 213 | kind: toToolKind(input.toolName), | 205 | | - title: input.toolName, | 206 | | - rawInput: input.state.input, | | 214 | + title: toolTitle(input.toolName, input.state.input, undefined), | | 215 | + locations: toLocations(input.toolName, input.state.input, input.cwd), | | 216 | + rawInput: rawInput(input.toolName, input.state.input, input.cwd), | 207 | 217 | content: [ | 208 | 218 | { | 209 | 219 | type: "content", | @@ -253,6 +263,42 @@ export function shellOutputSnapshot(state: { readonly metadata?: unknown }) { | 253 | 263 | return stringValue((state.metadata as Record<string, unknown>).output) | 254 | 264 | } | 255 | 265 |
| | 266 | +// For shell tools, surface the actual command as the title so it stays visible | | 267 | +// before output lands; non-shell tools keep their model-provided title. | | 268 | +function toolTitle(toolName: string, input: ToolInput, fallback: string | undefined) { | | 269 | + if (isShell(toolName)) return shellCommand(input) ?? stringValue(input.description) ?? fallback ?? toolName | | 270 | + return fallback || toolName | | 271 | +} | | 272 | + | | 273 | +// Enrich shell rawInput with the resolved working directory so clients can show | | 274 | +// where the command runs, unless the model already specified one. | | 275 | +function rawInput(toolName: string, input: ToolInput, cwd?: string): ToolInput { | | 276 | + if (!isShell(toolName)) return input | | 277 | + if (input.cwd || input.workdir) return input | | 278 | + const workdir = shellWorkdir(input, cwd) | | 279 | + return workdir ? { ...input, cwd: workdir } : input | | 280 | +} | | 281 | + | | 282 | +function shellWorkdir(input: ToolInput, cwd?: string) { | | 283 | + const explicit = stringValue(input.workdir) ?? stringValue(input.cwd) | | 284 | + return resolvePath(explicit, cwd) ?? cwd | | 285 | +} | | 286 | + | | 287 | +function resolvePath(value: string | undefined, cwd?: string) { | | 288 | + if (!value) return undefined | | 289 | + if (isAbsolute(value)) return value | | 290 | + return resolve(cwd ?? process.cwd(), value) | | 291 | +} | | 292 | + | | 293 | +function shellCommand(input: ToolInput) { | | 294 | + return stringValue(input.command) ?? stringValue(input.cmd) | | 295 | +} | | 296 | + | | 297 | +function isShell(toolName: string) { | | 298 | + const tool = toolName.toLocaleLowerCase() | | 299 | + return tool === "bash" || tool === "shell" | | 300 | +} | | 301 | + | 256 | 302 | export const mapToolKind = toToolKind | 257 | 303 | export const extractLocations = toLocations | 258 | 304 | export const buildCompletedToolContent = completedToolContent | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/acp/event.test.ts +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -517,7 +517,7 @@ describe("acp event routing", () => { | 517 | 517 | expect(harness.updates).toHaveLength(0) | 518 | 518 | }) | 519 | 519 |
| 520 | | - it("emits synthetic pending before the first running tool update", async () => { | | 520 | + it("exposes the shell command on the synthetic pending tool call", async () => { | 521 | 521 | const harness = createHarness() | 522 | 522 | await Effect.runPromise(harness.session.create({ id: "ses_tool", cwd: "/workspace" })) | 523 | 523 |
| @@ -527,7 +527,14 @@ describe("acp event routing", () => { | 527 | 527 | "tool_call", | 528 | 528 | "tool_call_update", | 529 | 529 | ]) | 530 | | - expect(harness.updates[0]?.update).toMatchObject({ status: "pending", toolCallId: "call_1" }) | | 530 | + expect(harness.updates[0]?.update).toMatchObject({ | | 531 | + status: "pending", | | 532 | + toolCallId: "call_1", | | 533 | + title: "printf hello", | | 534 | + kind: "execute", | | 535 | + locations: [{ path: "/workspace" }], | | 536 | + rawInput: { cmd: "printf hello", cwd: "/workspace" }, | | 537 | + }) | 531 | 538 | expect(harness.updates[1]?.update).toMatchObject({ status: "in_progress", toolCallId: "call_1" }) | 532 | 539 | }) | 533 | 540 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/acp/tool.test.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,12 @@ describe("acp tool conversion", () => { | 37 | 37 | expect(toLocations("external_directory", { directories: ["/tmp/outside"], patterns: ["/tmp/outside/*"] })).toEqual([ | 38 | 38 | { path: "/tmp/outside" }, | 39 | 39 | ]) | 40 | | - expect(toLocations("bash", { filePath: "/tmp/nope.ts", path: "/tmp" })).toEqual([]) | | 40 | + expect(toLocations("bash", { cmd: "pwd" }, "/workspace")).toEqual([{ path: "/workspace" }]) | | 41 | + expect(toLocations("bash", { command: "pwd", workdir: "subdir" }, "/workspace")).toEqual([ | | 42 | + { path: "/workspace/subdir" }, | | 43 | + ]) | | 44 | + expect(toLocations("bash", { command: "pwd", workdir: "/abs/dir" }, "/workspace")).toEqual([{ path: "/abs/dir" }]) | | 45 | + expect(toLocations("bash", { command: "printf hello" })).toEqual([]) | 41 | 46 | expect(toLocations("read", { path: "/tmp/missing-file-path.ts" })).toEqual([]) | 42 | 47 | }) | 43 | 48 |
| |
|