toolIdentity.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. isFileContentWriteToolCall,
  4. isFileDiffToolCall,
  5. resolveToolCallIdentity,
  6. } from "../src/lib/toolIdentity.js";
  7. describe("toolIdentity", () => {
  8. it("keeps fixed AskUserQuestion name while routing it as user-question family", () => {
  9. expect(
  10. resolveToolCallIdentity({
  11. toolName: "AskUserQuestion",
  12. kind: "AskUserQuestion",
  13. title: "AskUserQuestion",
  14. input: {
  15. questions: [
  16. {
  17. id: "q1",
  18. question: "选哪个方向?",
  19. options: [{ id: "a", label: "A" }],
  20. },
  21. ],
  22. },
  23. }),
  24. ).toMatchObject({
  25. toolName: "AskUserQuestion",
  26. family: "ask-user-question",
  27. isLegacy: false,
  28. });
  29. });
  30. it("does not classify TodoWrite as a file write tool", () => {
  31. const identity = resolveToolCallIdentity({
  32. kind: "TodoWrite",
  33. title: "TodoWrite",
  34. input: {
  35. todos: [{ content: "更新 UI", status: "pending", priority: "high" }],
  36. },
  37. });
  38. expect(identity).toMatchObject({
  39. toolName: "TodoWrite",
  40. family: "todo",
  41. isLegacy: false,
  42. });
  43. expect(isFileContentWriteToolCall({ kind: "TodoWrite" }, identity)).toBe(false);
  44. expect(isFileDiffToolCall({ kind: "TodoWrite" }, identity)).toBe(false);
  45. });
  46. it("classifies current fixed file tools without regex collisions", () => {
  47. const writeTool = { kind: "Write", input: { path: "a.ts" } };
  48. const editTool = { kind: "Edit", input: { path: "a.ts" } };
  49. const patchTool = { kind: "ApplyPatch", input: { patch: "..." } };
  50. expect(isFileContentWriteToolCall(writeTool)).toBe(true);
  51. expect(isFileDiffToolCall(writeTool)).toBe(false);
  52. expect(isFileContentWriteToolCall(editTool)).toBe(false);
  53. expect(isFileDiffToolCall(editTool)).toBe(true);
  54. expect(isFileDiffToolCall(patchTool)).toBe(true);
  55. });
  56. it("keeps legacy ZCode Agent agent shapes behind fallback identity", () => {
  57. expect(
  58. resolveToolCallIdentity({
  59. kind: "think",
  60. title: "Task",
  61. input: { subagent_type: "Explore" },
  62. }),
  63. ).toMatchObject({
  64. family: "agent",
  65. isLegacy: true,
  66. });
  67. });
  68. it("keeps explicit Task tool calls as current agent identity", () => {
  69. expect(
  70. resolveToolCallIdentity({
  71. toolName: "Task",
  72. input: { subagent_type: "Explore" },
  73. }),
  74. ).toMatchObject({
  75. toolName: "Task",
  76. family: "agent",
  77. isLegacy: false,
  78. });
  79. });
  80. it("recognizes current SendMessage but not legacy send_message", () => {
  81. expect(
  82. resolveToolCallIdentity({
  83. toolName: "SendMessage",
  84. }),
  85. ).toMatchObject({
  86. toolName: "SendMessage",
  87. family: "message",
  88. isLegacy: false,
  89. });
  90. expect(
  91. resolveToolCallIdentity({
  92. toolName: "send_message",
  93. }),
  94. ).toMatchObject({
  95. toolName: null,
  96. family: "unknown",
  97. });
  98. });
  99. it("recognizes current TaskStop as task control", () => {
  100. expect(
  101. resolveToolCallIdentity({
  102. toolName: "TaskStop",
  103. }),
  104. ).toMatchObject({
  105. toolName: "TaskStop",
  106. family: "task-control",
  107. isLegacy: false,
  108. });
  109. });
  110. it("recognizes TaskOutput and RespondToCoordinator presentation families", () => {
  111. expect(
  112. resolveToolCallIdentity({
  113. toolName: "TaskOutput",
  114. }),
  115. ).toMatchObject({
  116. toolName: "TaskOutput",
  117. family: "task-control",
  118. isLegacy: false,
  119. });
  120. expect(
  121. resolveToolCallIdentity({
  122. toolName: "RespondToCoordinator",
  123. }),
  124. ).toMatchObject({
  125. toolName: "RespondToCoordinator",
  126. family: "message",
  127. isLegacy: false,
  128. });
  129. });
  130. it("recognizes the snake_case submit_result as a workflow tool", () => {
  131. // 唯一一个 snake_case 现役工具名:下划线必须字面登记,否则动态工作流 actor 的提交
  132. // 会退回 unknown fallback。
  133. expect(
  134. resolveToolCallIdentity({
  135. toolName: "submit_result",
  136. kind: "submit_result",
  137. title: "submit_result",
  138. }),
  139. ).toMatchObject({
  140. toolName: "submit_result",
  141. family: "workflow",
  142. isLegacy: false,
  143. });
  144. });
  145. it("routes node repl tools through their dedicated presentation family", () => {
  146. for (const toolName of [
  147. "js",
  148. "js_reset",
  149. "js_add_node_module_dir",
  150. "mcp__node_repl__js",
  151. "mcp__node_repl__js_reset",
  152. "mcp__node_repl__js_add_node_module_dir",
  153. ]) {
  154. expect(
  155. resolveToolCallIdentity({
  156. toolName,
  157. kind: toolName,
  158. title: toolName,
  159. }),
  160. ).toMatchObject({
  161. toolName,
  162. family: "node-repl",
  163. isLegacy: false,
  164. });
  165. }
  166. });
  167. it("routes current no-separator ExitPlanMode names as switch mode", () => {
  168. expect(
  169. resolveToolCallIdentity({
  170. toolName: "ExitPlanMode",
  171. kind: "ExitPlanMode",
  172. title: "ExitPlanMode",
  173. }),
  174. ).toMatchObject({
  175. toolName: "switch_mode",
  176. family: "switch-mode",
  177. isLegacy: true,
  178. });
  179. });
  180. });