1
0

tool.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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", { filePath: "/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([])
  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. structured: {},
  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. structured: {},
  92. }).content,
  93. ).toEqual([
  94. {
  95. type: "content",
  96. content: { type: "text", text: "wrote /tmp/file.ts" },
  97. },
  98. ])
  99. })
  100. test("uses clean structured read content instead of model-facing formatting", () => {
  101. expect(
  102. completedToolUpdate({
  103. toolCallId: "tool-read",
  104. toolName: "read",
  105. input: { path: "/tmp/file.ts" },
  106. content: [{ type: "text", text: "<content>1: first\n2: second</content>" }],
  107. structured: {
  108. type: "text-page",
  109. content: "first\nsecond",
  110. mime: "text/plain",
  111. offset: 1,
  112. truncated: false,
  113. },
  114. }).content,
  115. ).toEqual([{ type: "content", content: { type: "text", text: "first\nsecond" } }])
  116. expect(
  117. completedToolUpdate({
  118. toolCallId: "tool-list",
  119. toolName: "read",
  120. input: { path: "/tmp" },
  121. content: [],
  122. structured: {
  123. entries: [
  124. { path: "a.ts", type: "file" },
  125. { path: "src", type: "directory" },
  126. ],
  127. },
  128. }).content,
  129. ).toEqual([{ type: "content", content: { type: "text", text: "a.ts\nsrc" } }])
  130. })
  131. test("sends completed tool calls as partial updates", () => {
  132. expect(
  133. pendingToolCall({
  134. toolCallId: "tool-1",
  135. toolName: "edit",
  136. state: {
  137. input: {
  138. filePath: "/tmp/file.ts",
  139. oldString: "before",
  140. newString: "after",
  141. },
  142. },
  143. }),
  144. ).toMatchObject({
  145. toolCallId: "tool-1",
  146. status: "pending",
  147. kind: "edit",
  148. locations: [{ path: "/tmp/file.ts" }],
  149. rawInput: {
  150. filePath: "/tmp/file.ts",
  151. oldString: "before",
  152. newString: "after",
  153. },
  154. })
  155. expect(
  156. completedToolUpdate({
  157. toolCallId: "tool-1",
  158. toolName: "edit",
  159. input: {
  160. filePath: "/tmp/file.ts",
  161. oldString: "before",
  162. newString: "after",
  163. },
  164. content: [{ type: "text", text: "Edit applied successfully." }],
  165. structured: { output: "Edit applied successfully." },
  166. }),
  167. ).toEqual({
  168. toolCallId: "tool-1",
  169. status: "completed",
  170. content: [
  171. {
  172. type: "content",
  173. content: { type: "text", text: "Edit applied successfully." },
  174. },
  175. {
  176. type: "diff",
  177. path: "/tmp/file.ts",
  178. oldText: "before",
  179. newText: "after",
  180. },
  181. ],
  182. rawOutput: {
  183. structured: { output: "Edit applied successfully." },
  184. },
  185. })
  186. })
  187. test("builds running tool updates with normalized content", () => {
  188. expect(
  189. runningToolUpdate({
  190. toolCallId: "call",
  191. toolName: "read",
  192. state: { input: { filePath: "/tmp/a" } },
  193. content: [{ type: "text", text: "done" }],
  194. }),
  195. ).toMatchObject({
  196. toolCallId: "call",
  197. status: "in_progress",
  198. content: [{ type: "content", content: { type: "text", text: "done" } }],
  199. })
  200. })
  201. test("builds completed raw output with structured data and optional result", () => {
  202. const attachments = [
  203. {
  204. type: "file",
  205. mime: "image/jpeg",
  206. name: "photo.jpg",
  207. uri: "data:image/jpeg;base64,AAAA",
  208. },
  209. ]
  210. expect(
  211. completedToolUpdate({
  212. toolCallId: "call",
  213. toolName: "read",
  214. input: {},
  215. content: [],
  216. structured: { output: "done", metadata: { exit: 0 }, attachments },
  217. result: "done",
  218. }).rawOutput,
  219. ).toEqual({
  220. structured: { output: "done", metadata: { exit: 0 }, attachments },
  221. result: "done",
  222. })
  223. expect(
  224. completedToolUpdate({
  225. toolCallId: "call",
  226. toolName: "read",
  227. input: {},
  228. content: [],
  229. structured: { output: "done" },
  230. }).rawOutput,
  231. ).toEqual({ structured: { output: "done" } })
  232. })
  233. test("extracts image attachments only from data URLs", () => {
  234. expect(
  235. completedToolUpdate({
  236. toolCallId: "call",
  237. toolName: "read",
  238. input: {},
  239. content: [
  240. { type: "file", mime: "image/webp", uri: "data:image/webp;charset=utf-8;base64,AAAA" },
  241. { type: "file", mime: "image/png", uri: "https://example.com/image.png" },
  242. { type: "file", mime: "text/plain", uri: "data:text/plain;base64,BBBB" },
  243. ],
  244. structured: {},
  245. }).content,
  246. ).toEqual([
  247. {
  248. type: "content",
  249. content: { type: "image", mimeType: "image/webp", data: "AAAA" },
  250. },
  251. ])
  252. })
  253. test("builds failed tool updates", () => {
  254. expect(
  255. errorToolUpdate({
  256. toolCallId: "call",
  257. toolName: "read",
  258. input: { filePath: "/tmp/a" },
  259. content: [{ type: "text", text: "partial output" }],
  260. structured: { path: "/tmp/a" },
  261. error: "failed",
  262. }),
  263. ).toEqual({
  264. toolCallId: "call",
  265. status: "failed",
  266. kind: "read",
  267. title: "read",
  268. locations: [{ path: "/tmp/a" }],
  269. rawInput: { filePath: "/tmp/a" },
  270. content: [
  271. { type: "content", content: { type: "text", text: "partial output" } },
  272. { type: "content", content: { type: "text", text: "failed" } },
  273. ],
  274. rawOutput: { structured: { path: "/tmp/a" }, error: "failed" },
  275. })
  276. })
  277. })