| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- import { resolve } from "node:path"
- import { describe, expect, test } from "bun:test"
- import {
- completedToolUpdate,
- errorToolUpdate,
- pendingToolCall,
- runningToolUpdate,
- toLocations,
- toToolKind,
- } from "../../src/acp/tool"
- describe("acp tools", () => {
- test("maps OpenCode tool ids to ACP tool kinds", () => {
- expect(toToolKind("bash")).toBe("execute")
- expect(toToolKind("shell")).toBe("execute")
- expect(toToolKind("webfetch")).toBe("fetch")
- expect(toToolKind("edit")).toBe("edit")
- expect(toToolKind("apply_patch")).toBe("edit")
- expect(toToolKind("patch")).toBe("edit")
- expect(toToolKind("write")).toBe("edit")
- expect(toToolKind("grep")).toBe("search")
- expect(toToolKind("glob")).toBe("search")
- expect(toToolKind("context7_resolve_library_id")).toBe("search")
- expect(toToolKind("context7_get_library_docs")).toBe("search")
- expect(toToolKind("read")).toBe("read")
- expect(toToolKind("task")).toBe("think")
- expect(toToolKind("custom_tool")).toBe("other")
- })
- test("extracts file locations from tool input", () => {
- expect(toLocations("read", { path: "/tmp/a.ts" })).toEqual([{ path: "/tmp/a.ts" }])
- expect(toLocations("edit", { filePath: "/tmp/b.ts" })).toEqual([{ path: "/tmp/b.ts" }])
- expect(toLocations("write", { filePath: "/tmp/c.ts" })).toEqual([{ path: "/tmp/c.ts" }])
- expect(toLocations("grep", { path: "/repo/src" })).toEqual([{ path: "/repo/src" }])
- expect(toLocations("glob", { path: "/repo/test" })).toEqual([{ path: "/repo/test" }])
- expect(toLocations("context7_get_library_docs", { path: "/docs" })).toEqual([{ path: "/docs" }])
- expect(toLocations("external_directory", { directories: ["/tmp/outside"], patterns: ["/tmp/outside/*"] })).toEqual([
- { path: "/tmp/outside" },
- ])
- expect(toLocations("bash", { cmd: "pwd" }, "/workspace")).toEqual([{ path: "/workspace" }])
- expect(toLocations("bash", { command: "pwd", workdir: "subdir" }, "/workspace")).toEqual([
- { path: resolve("/workspace", "subdir") },
- ])
- expect(toLocations("bash", { command: "pwd", workdir: "/abs/dir" }, "/workspace")).toEqual([{ path: "/abs/dir" }])
- expect(toLocations("bash", { command: "printf hello" })).toEqual([])
- expect(toLocations("read", { path: "/tmp/missing-file-path.ts" })).toEqual([{ path: "/tmp/missing-file-path.ts" }])
- })
- test("builds completed content with text and image attachments", () => {
- const image = Buffer.from("image-data").toString("base64")
- expect(
- completedToolUpdate({
- toolCallId: "tool-1",
- toolName: "edit",
- input: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- content: [
- { type: "text", text: "edited /tmp/file.ts" },
- { type: "file", mime: "image/png", name: "image.png", uri: `data:image/png;base64,${image}` },
- { type: "file", mime: "text/plain", name: "note.txt", uri: "data:text/plain;base64,bm90ZQ==" },
- ],
- metadata: {},
- }).content,
- ).toEqual([
- {
- type: "content",
- content: { type: "text", text: "edited /tmp/file.ts" },
- },
- {
- type: "diff",
- path: "/tmp/file.ts",
- oldText: "before",
- newText: "after",
- },
- {
- type: "content",
- content: { type: "image", mimeType: "image/png", data: image },
- },
- ])
- })
- test("omits edit diffs when normalized content does not contain one", () => {
- expect(
- completedToolUpdate({
- toolCallId: "tool-1",
- toolName: "write",
- input: {
- filePath: "/tmp/file.ts",
- content: "created",
- },
- content: [{ type: "text", text: "wrote /tmp/file.ts" }],
- metadata: {},
- }).content,
- ).toEqual([
- {
- type: "content",
- content: { type: "text", text: "wrote /tmp/file.ts" },
- },
- ])
- })
- test("unwraps read's JSON page envelope instead of showing model-facing formatting", () => {
- expect(
- completedToolUpdate({
- toolCallId: "tool-read",
- toolName: "read",
- input: { path: "/tmp/file.ts" },
- content: [
- {
- type: "text",
- text: JSON.stringify(
- { type: "text-page", content: "first\nsecond", mime: "text/plain", offset: 1, truncated: false },
- null,
- 2,
- ),
- },
- ],
- }).content,
- ).toEqual([{ type: "content", content: { type: "text", text: "first\nsecond" } }])
- expect(
- completedToolUpdate({
- toolCallId: "tool-list",
- toolName: "read",
- input: { path: "/tmp" },
- content: [
- {
- type: "text",
- text: JSON.stringify({
- entries: [
- { path: "a.ts", type: "file" },
- { path: "src", type: "directory" },
- ],
- }),
- },
- ],
- }).content,
- ).toEqual([{ type: "content", content: { type: "text", text: "a.ts\nsrc" } }])
- })
- test("sends completed tool calls as partial updates", () => {
- expect(
- pendingToolCall({
- toolCallId: "tool-1",
- toolName: "edit",
- state: {
- input: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- },
- }),
- ).toMatchObject({
- toolCallId: "tool-1",
- status: "pending",
- kind: "edit",
- locations: [{ path: "/tmp/file.ts" }],
- rawInput: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- })
- expect(
- completedToolUpdate({
- toolCallId: "tool-1",
- toolName: "edit",
- input: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- content: [{ type: "text", text: "Edit applied successfully." }],
- metadata: { output: "Edit applied successfully." },
- }),
- ).toEqual({
- toolCallId: "tool-1",
- status: "completed",
- content: [
- {
- type: "content",
- content: { type: "text", text: "Edit applied successfully." },
- },
- {
- type: "diff",
- path: "/tmp/file.ts",
- oldText: "before",
- newText: "after",
- },
- ],
- rawOutput: {
- metadata: { output: "Edit applied successfully." },
- },
- })
- })
- test("builds running tool updates with normalized content", () => {
- expect(
- runningToolUpdate({
- toolCallId: "call",
- toolName: "read",
- state: { input: { path: "/tmp/a" } },
- content: [{ type: "text", text: "done" }],
- }),
- ).toMatchObject({
- toolCallId: "call",
- status: "in_progress",
- locations: [{ path: "/tmp/a" }],
- content: [{ type: "content", content: { type: "text", text: "done" } }],
- })
- })
- test("builds completed raw output with optional metadata", () => {
- const attachments = [
- {
- type: "file",
- mime: "image/jpeg",
- name: "photo.jpg",
- uri: "data:image/jpeg;base64,AAAA",
- },
- ]
- expect(
- completedToolUpdate({
- toolCallId: "call",
- toolName: "read",
- input: {},
- content: [],
- metadata: { output: "done", metadata: { exit: 0 }, attachments },
- }).rawOutput,
- ).toEqual({
- metadata: { output: "done", metadata: { exit: 0 }, attachments },
- })
- expect(
- completedToolUpdate({
- toolCallId: "call",
- toolName: "read",
- input: {},
- content: [],
- }).rawOutput,
- ).toEqual({})
- })
- test("extracts image attachments only from data URLs", () => {
- expect(
- completedToolUpdate({
- toolCallId: "call",
- toolName: "read",
- input: {},
- content: [
- { type: "file", mime: "image/webp", uri: "data:image/webp;charset=utf-8;base64,AAAA" },
- { type: "file", mime: "image/png", uri: "https://example.com/image.png" },
- { type: "file", mime: "text/plain", uri: "data:text/plain;base64,BBBB" },
- ],
- metadata: {},
- }).content,
- ).toEqual([
- {
- type: "content",
- content: { type: "image", mimeType: "image/webp", data: "AAAA" },
- },
- ])
- })
- test("builds failed tool updates", () => {
- expect(
- errorToolUpdate({
- toolCallId: "call",
- toolName: "read",
- input: { path: "/tmp/a" },
- content: [{ type: "text", text: "partial output" }],
- metadata: { path: "/tmp/a" },
- error: "failed",
- }),
- ).toEqual({
- toolCallId: "call",
- status: "failed",
- kind: "read",
- title: "read",
- locations: [{ path: "/tmp/a" }],
- rawInput: { path: "/tmp/a" },
- content: [
- { type: "content", content: { type: "text", text: "partial output" } },
- { type: "content", content: { type: "text", text: "failed" } },
- ],
- rawOutput: { metadata: { path: "/tmp/a" }, error: "failed" },
- })
- })
- })
|