tool.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { resolve } from "node:path"
  2. import { describe, expect, test } from "bun:test"
  3. import {
  4. completedToolUpdate,
  5. errorToolUpdate,
  6. pendingToolCall,
  7. runningToolUpdate,
  8. toLocations,
  9. toToolKind,
  10. } from "../../src/acp/tool"
  11. describe("acp tools", () => {
  12. test("maps OpenCode tool ids to ACP tool kinds", () => {
  13. expect(toToolKind("bash")).toBe("execute")
  14. expect(toToolKind("shell")).toBe("execute")
  15. expect(toToolKind("webfetch")).toBe("fetch")
  16. expect(toToolKind("edit")).toBe("edit")
  17. expect(toToolKind("apply_patch")).toBe("edit")
  18. expect(toToolKind("patch")).toBe("edit")
  19. expect(toToolKind("write")).toBe("edit")
  20. expect(toToolKind("grep")).toBe("search")
  21. expect(toToolKind("glob")).toBe("search")
  22. expect(toToolKind("context7_resolve_library_id")).toBe("search")
  23. expect(toToolKind("context7_get_library_docs")).toBe("search")
  24. expect(toToolKind("read")).toBe("read")
  25. expect(toToolKind("task")).toBe("think")
  26. expect(toToolKind("custom_tool")).toBe("other")
  27. })
  28. test("extracts file locations from tool input", () => {
  29. expect(toLocations("read", { path: "/tmp/a.ts" })).toEqual([{ path: "/tmp/a.ts" }])
  30. expect(toLocations("edit", { filePath: "/tmp/b.ts" })).toEqual([{ path: "/tmp/b.ts" }])
  31. expect(toLocations("write", { filePath: "/tmp/c.ts" })).toEqual([{ path: "/tmp/c.ts" }])
  32. expect(toLocations("grep", { path: "/repo/src" })).toEqual([{ path: "/repo/src" }])
  33. expect(toLocations("glob", { path: "/repo/test" })).toEqual([{ path: "/repo/test" }])
  34. expect(toLocations("context7_get_library_docs", { path: "/docs" })).toEqual([{ path: "/docs" }])
  35. expect(toLocations("external_directory", { directories: ["/tmp/outside"], patterns: ["/tmp/outside/*"] })).toEqual([
  36. { path: "/tmp/outside" },
  37. ])
  38. expect(toLocations("bash", { cmd: "pwd" }, "/workspace")).toEqual([{ path: "/workspace" }])
  39. expect(toLocations("bash", { command: "pwd", workdir: "subdir" }, "/workspace")).toEqual([
  40. { path: resolve("/workspace", "subdir") },
  41. ])
  42. expect(toLocations("bash", { command: "pwd", workdir: "/abs/dir" }, "/workspace")).toEqual([{ path: "/abs/dir" }])
  43. expect(toLocations("bash", { command: "printf hello" })).toEqual([])
  44. expect(toLocations("read", { path: "/tmp/missing-file-path.ts" })).toEqual([{ path: "/tmp/missing-file-path.ts" }])
  45. })
  46. test("builds completed content with text and image attachments", () => {
  47. const image = Buffer.from("image-data").toString("base64")
  48. expect(
  49. completedToolUpdate({
  50. toolCallId: "tool-1",
  51. toolName: "edit",
  52. input: {
  53. filePath: "/tmp/file.ts",
  54. oldString: "before",
  55. newString: "after",
  56. },
  57. content: [
  58. { type: "text", text: "edited /tmp/file.ts" },
  59. { type: "file", mime: "image/png", name: "image.png", uri: `data:image/png;base64,${image}` },
  60. { type: "file", mime: "text/plain", name: "note.txt", uri: "data:text/plain;base64,bm90ZQ==" },
  61. ],
  62. metadata: {},
  63. }).content,
  64. ).toEqual([
  65. {
  66. type: "content",
  67. content: { type: "text", text: "edited /tmp/file.ts" },
  68. },
  69. {
  70. type: "diff",
  71. path: "/tmp/file.ts",
  72. oldText: "before",
  73. newText: "after",
  74. },
  75. {
  76. type: "content",
  77. content: { type: "image", mimeType: "image/png", data: image },
  78. },
  79. ])
  80. })
  81. test("omits edit diffs when normalized content does not contain one", () => {
  82. expect(
  83. completedToolUpdate({
  84. toolCallId: "tool-1",
  85. toolName: "write",
  86. input: {
  87. filePath: "/tmp/file.ts",
  88. content: "created",
  89. },
  90. content: [{ type: "text", text: "wrote /tmp/file.ts" }],
  91. metadata: {},
  92. }).content,
  93. ).toEqual([
  94. {
  95. type: "content",
  96. content: { type: "text", text: "wrote /tmp/file.ts" },
  97. },
  98. ])
  99. })
  100. test("unwraps read's JSON page envelope instead of showing model-facing formatting", () => {
  101. expect(
  102. completedToolUpdate({
  103. toolCallId: "tool-read",
  104. toolName: "read",
  105. input: { path: "/tmp/file.ts" },
  106. content: [
  107. {
  108. type: "text",
  109. text: JSON.stringify(
  110. { type: "text-page", content: "first\nsecond", mime: "text/plain", offset: 1, truncated: false },
  111. null,
  112. 2,
  113. ),
  114. },
  115. ],
  116. }).content,
  117. ).toEqual([{ type: "content", content: { type: "text", text: "first\nsecond" } }])
  118. expect(
  119. completedToolUpdate({
  120. toolCallId: "tool-list",
  121. toolName: "read",
  122. input: { path: "/tmp" },
  123. content: [
  124. {
  125. type: "text",
  126. text: JSON.stringify({
  127. entries: [
  128. { path: "a.ts", type: "file" },
  129. { path: "src", type: "directory" },
  130. ],
  131. }),
  132. },
  133. ],
  134. }).content,
  135. ).toEqual([{ type: "content", content: { type: "text", text: "a.ts\nsrc" } }])
  136. })
  137. test("sends completed tool calls as partial updates", () => {
  138. expect(
  139. pendingToolCall({
  140. toolCallId: "tool-1",
  141. toolName: "edit",
  142. state: {
  143. input: {
  144. filePath: "/tmp/file.ts",
  145. oldString: "before",
  146. newString: "after",
  147. },
  148. },
  149. }),
  150. ).toMatchObject({
  151. toolCallId: "tool-1",
  152. status: "pending",
  153. kind: "edit",
  154. locations: [{ path: "/tmp/file.ts" }],
  155. rawInput: {
  156. filePath: "/tmp/file.ts",
  157. oldString: "before",
  158. newString: "after",
  159. },
  160. })
  161. expect(
  162. completedToolUpdate({
  163. toolCallId: "tool-1",
  164. toolName: "edit",
  165. input: {
  166. filePath: "/tmp/file.ts",
  167. oldString: "before",
  168. newString: "after",
  169. },
  170. content: [{ type: "text", text: "Edit applied successfully." }],
  171. metadata: { output: "Edit applied successfully." },
  172. }),
  173. ).toEqual({
  174. toolCallId: "tool-1",
  175. status: "completed",
  176. content: [
  177. {
  178. type: "content",
  179. content: { type: "text", text: "Edit applied successfully." },
  180. },
  181. {
  182. type: "diff",
  183. path: "/tmp/file.ts",
  184. oldText: "before",
  185. newText: "after",
  186. },
  187. ],
  188. rawOutput: {
  189. metadata: { output: "Edit applied successfully." },
  190. },
  191. })
  192. })
  193. test("builds running tool updates with normalized content", () => {
  194. expect(
  195. runningToolUpdate({
  196. toolCallId: "call",
  197. toolName: "read",
  198. state: { input: { path: "/tmp/a" } },
  199. content: [{ type: "text", text: "done" }],
  200. }),
  201. ).toMatchObject({
  202. toolCallId: "call",
  203. status: "in_progress",
  204. locations: [{ path: "/tmp/a" }],
  205. content: [{ type: "content", content: { type: "text", text: "done" } }],
  206. })
  207. })
  208. test("builds completed raw output with optional metadata", () => {
  209. const attachments = [
  210. {
  211. type: "file",
  212. mime: "image/jpeg",
  213. name: "photo.jpg",
  214. uri: "data:image/jpeg;base64,AAAA",
  215. },
  216. ]
  217. expect(
  218. completedToolUpdate({
  219. toolCallId: "call",
  220. toolName: "read",
  221. input: {},
  222. content: [],
  223. metadata: { output: "done", metadata: { exit: 0 }, attachments },
  224. }).rawOutput,
  225. ).toEqual({
  226. metadata: { output: "done", metadata: { exit: 0 }, attachments },
  227. })
  228. expect(
  229. completedToolUpdate({
  230. toolCallId: "call",
  231. toolName: "read",
  232. input: {},
  233. content: [],
  234. }).rawOutput,
  235. ).toEqual({})
  236. })
  237. test("extracts image attachments only from data URLs", () => {
  238. expect(
  239. completedToolUpdate({
  240. toolCallId: "call",
  241. toolName: "read",
  242. input: {},
  243. content: [
  244. { type: "file", mime: "image/webp", uri: "data:image/webp;charset=utf-8;base64,AAAA" },
  245. { type: "file", mime: "image/png", uri: "https://example.com/image.png" },
  246. { type: "file", mime: "text/plain", uri: "data:text/plain;base64,BBBB" },
  247. ],
  248. metadata: {},
  249. }).content,
  250. ).toEqual([
  251. {
  252. type: "content",
  253. content: { type: "image", mimeType: "image/webp", data: "AAAA" },
  254. },
  255. ])
  256. })
  257. test("builds failed tool updates", () => {
  258. expect(
  259. errorToolUpdate({
  260. toolCallId: "call",
  261. toolName: "read",
  262. input: { path: "/tmp/a" },
  263. content: [{ type: "text", text: "partial output" }],
  264. metadata: { path: "/tmp/a" },
  265. error: "failed",
  266. }),
  267. ).toEqual({
  268. toolCallId: "call",
  269. status: "failed",
  270. kind: "read",
  271. title: "read",
  272. locations: [{ path: "/tmp/a" }],
  273. rawInput: { path: "/tmp/a" },
  274. content: [
  275. { type: "content", content: { type: "text", text: "partial output" } },
  276. { type: "content", content: { type: "text", text: "failed" } },
  277. ],
  278. rawOutput: { metadata: { path: "/tmp/a" }, error: "failed" },
  279. })
  280. })
  281. })