tool.test.ts 8.3 KB

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