mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 01:08:31 +03:00
fix(acp): report only files as tool call locations
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
absolutePath,
|
||||
canonicalName,
|
||||
filePath,
|
||||
locationFrom,
|
||||
patchHunks,
|
||||
pendingToolCall,
|
||||
stringValue,
|
||||
@@ -190,13 +191,9 @@ function permissionTitle(toolName: string, input: ToolInput, previews: ReadonlyA
|
||||
function permissionLocations(toolName: string, input: ToolInput, ask: PermissionEvent["data"], cwd: string) {
|
||||
const locations = toLocations(toolName, input, cwd)
|
||||
if (locations.length > 0 || !PathActions.has(ask.action)) return locations
|
||||
const paths = ask.resources.flatMap((resource) => {
|
||||
const path = resource.endsWith("/*") ? resource.slice(0, -2) : resource
|
||||
return path && !/[*?]/.test(path) ? [absolutePath(path, cwd)] : []
|
||||
})
|
||||
return Array.from(new Set(paths), (path) => ({ path }))
|
||||
return locationFrom(cwd, ...ask.resources.filter((resource) => !/[*?]/.test(resource)))
|
||||
}
|
||||
|
||||
const PathActions = new Set(["read", "edit", "external_directory"])
|
||||
const PathActions = new Set(["read", "edit"])
|
||||
|
||||
export * as ACPPermission from "./permission"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { statSync } from "node:fs"
|
||||
import { isAbsolute, resolve } from "node:path"
|
||||
import type { ToolCall, ToolCallContent, ToolCallLocation, ToolCallUpdate, ToolKind } from "@agentclientprotocol/sdk"
|
||||
import type { Tool } from "@opencode/schema/tool"
|
||||
@@ -38,10 +39,9 @@ function toToolKind(toolName: string): ToolKind {
|
||||
}
|
||||
}
|
||||
|
||||
// Clients such as Zed open a buffer for every location, so only files are reported.
|
||||
export function toLocations(toolName: string, input: ToolInput, cwd: string): ToolCallLocation[] {
|
||||
switch (canonicalName(toolName)) {
|
||||
case "shell":
|
||||
return locationFrom(cwd, stringValue(input.workdir) ?? stringValue(input.cwd) ?? cwd)
|
||||
case "read":
|
||||
case "edit":
|
||||
case "write":
|
||||
@@ -54,7 +54,6 @@ export function toLocations(toolName: string, input: ToolInput, cwd: string): To
|
||||
case "external_directory":
|
||||
return locationFrom(cwd, input.filepath)
|
||||
case "grep":
|
||||
case "glob":
|
||||
case "context":
|
||||
case "context7_resolve_library_id":
|
||||
case "context7_get_library_docs":
|
||||
@@ -171,11 +170,18 @@ function isShell(toolName: string) {
|
||||
return canonicalName(toolName) === "shell"
|
||||
}
|
||||
|
||||
function locationFrom(cwd: string, ...values: unknown[]): ToolCallLocation[] {
|
||||
export function locationFrom(cwd: string, ...values: unknown[]): ToolCallLocation[] {
|
||||
return Array.from(
|
||||
new Set(values.flatMap((value) => (typeof value === "string" && value ? [absolutePath(value, cwd)] : []))),
|
||||
(path) => ({ path }),
|
||||
)
|
||||
.filter((path) => !isDirectory(path))
|
||||
.map((path) => ({ path }))
|
||||
}
|
||||
|
||||
// A path that can't be inspected, such as a file a tool is about to create, is still reported.
|
||||
function isDirectory(path: string) {
|
||||
const stat = Result.try(() => statSync(path))
|
||||
return Result.isSuccess(stat) && stat.success.isDirectory()
|
||||
}
|
||||
|
||||
const V1Aliases = new Map([
|
||||
|
||||
@@ -67,7 +67,7 @@ describe("acp permissions over the wire", () => {
|
||||
status: "pending",
|
||||
title: "printf hello",
|
||||
kind: "execute",
|
||||
locations: [{ path: "/workspace" }],
|
||||
locations: [],
|
||||
rawInput: { command: "printf hello", cwd: "/workspace" },
|
||||
},
|
||||
options: [
|
||||
@@ -281,7 +281,7 @@ describe("acp edit previews over the wire", () => {
|
||||
])
|
||||
expect(acp.permissions.slice(5).map((request) => request.toolCall.locations)).toEqual([
|
||||
[{ path: file("stale.ts") }],
|
||||
[{ path: file("folder") }],
|
||||
[],
|
||||
[{ path: file("unpatched.ts") }],
|
||||
])
|
||||
expect(acp.permissions[0]?.toolCall.name).toBe("edit")
|
||||
|
||||
@@ -143,7 +143,7 @@ describe("acp prompt turns over the wire", () => {
|
||||
expect(turnUpdates(acp.updates)[5]?.update).toMatchObject({
|
||||
title: "printf done",
|
||||
kind: "execute",
|
||||
locations: [{ path: path.resolve("/workspace", "sub") }],
|
||||
locations: [],
|
||||
rawInput: { command: "printf done", workdir: "sub" },
|
||||
})
|
||||
expect(turnUpdates(acp.updates)[7]?.update).toMatchObject({
|
||||
|
||||
@@ -54,7 +54,7 @@ describe("acp session replay over the wire", () => {
|
||||
toolCallId: "call_running",
|
||||
status: "in_progress",
|
||||
title: "pwd",
|
||||
locations: [{ path: "/workspace" }],
|
||||
locations: [],
|
||||
})
|
||||
expect(updates[10]?.update).toMatchObject({
|
||||
toolCallId: "call_failed",
|
||||
|
||||
@@ -3,9 +3,11 @@ import { OpenCodeEvent, type OpenCodeEventEncoded } from "@opencode/protocol/gro
|
||||
import { Session } from "@opencode/schema/session"
|
||||
import { SessionMessage } from "@opencode/schema/session-message"
|
||||
import { Schema } from "effect"
|
||||
import { mkdir } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { ACPReplay } from "../../src/acp/replay"
|
||||
import { ACPTranslate } from "../../src/acp/translate"
|
||||
import { tmpdir } from "../fixture/tmpdir"
|
||||
import {
|
||||
assistantMessage,
|
||||
childCreated,
|
||||
@@ -230,7 +232,7 @@ const rows: Row[] = [
|
||||
sessionUpdate: "tool_call_update",
|
||||
status: "in_progress",
|
||||
title: "printf done",
|
||||
locations: [{ path: path.resolve("/workspace", "sub") }],
|
||||
locations: [],
|
||||
rawInput: { command: "printf done", workdir: "sub" },
|
||||
},
|
||||
{ sessionUpdate: "tool_call_update", status: "in_progress" },
|
||||
@@ -599,6 +601,56 @@ describe("acp turn translation", () => {
|
||||
test.each(rows)("$name", (row) => {
|
||||
expect(translate(row)).toMatchObject(row.expected)
|
||||
})
|
||||
|
||||
test("locates files but not directories", async () => {
|
||||
await using dir = await tmpdir()
|
||||
await mkdir(path.join(dir.path, "src"))
|
||||
await Bun.write(path.join(dir.path, "src", "a.ts"), "a")
|
||||
const calls = [
|
||||
["call_shell", "shell", { command: "ls", workdir: "src" }],
|
||||
["call_glob", "glob", { pattern: "*.ts", path: "src" }],
|
||||
["call_grep_dir", "grep", { pattern: "a", path: "src" }],
|
||||
["call_read_dir", "read", { path: "src" }],
|
||||
["call_grep_file", "grep", { pattern: "a", path: "src/a.ts" }],
|
||||
["call_read_file", "read", { path: "src/a.ts" }],
|
||||
] as const
|
||||
const result = run(
|
||||
live(
|
||||
...calls.flatMap(([id, name, input]) => [
|
||||
toolStarted(root, id, name),
|
||||
toolCalled(root, id, input),
|
||||
toolSucceeded(root, id, {}, "done"),
|
||||
]),
|
||||
),
|
||||
{ ...ctx, cwd: dir.path },
|
||||
)
|
||||
|
||||
expect(
|
||||
Object.fromEntries(
|
||||
calls.map(([id]) => [
|
||||
id,
|
||||
updates(result.outputs)
|
||||
.filter((update) => "toolCallId" in update && update.toolCallId === id)
|
||||
.map((update) => ("locations" in update ? update.locations : undefined)),
|
||||
]),
|
||||
),
|
||||
).toEqual({
|
||||
call_shell: [[], [], []],
|
||||
call_glob: [[], [], []],
|
||||
call_grep_dir: [[], [], []],
|
||||
call_read_dir: [[], [], []],
|
||||
call_grep_file: [
|
||||
[],
|
||||
[{ path: path.join(dir.path, "src", "a.ts") }],
|
||||
[{ path: path.join(dir.path, "src", "a.ts") }],
|
||||
],
|
||||
call_read_file: [
|
||||
[],
|
||||
[{ path: path.join(dir.path, "src", "a.ts") }],
|
||||
[{ path: path.join(dir.path, "src", "a.ts") }],
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function translate(row: Row) {
|
||||
|
||||
Reference in New Issue
Block a user