toolCallTree.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { describe, expect, it } from "vitest";
  2. import { buildToolCallTree, reconcileToolCallTree } from "../src/lib/toolCallTree.js";
  3. describe("buildToolCallTree", () => {
  4. it("groups child tool calls under their parent while preserving order", () => {
  5. const tree = buildToolCallTree([
  6. {
  7. toolId: "tool-root-before",
  8. kind: "other",
  9. title: "EnterPlanMode",
  10. input: null,
  11. status: "completed",
  12. },
  13. {
  14. toolId: "tool-task",
  15. kind: "think",
  16. title: "Task",
  17. input: { prompt: "Explore project" },
  18. status: "completed",
  19. },
  20. {
  21. toolId: "tool-child-read",
  22. parentToolUseId: "tool-task",
  23. kind: "read",
  24. title: "Read package.json",
  25. input: { file_path: "package.json" },
  26. status: "completed",
  27. },
  28. {
  29. toolId: "tool-child-list",
  30. parentToolUseId: "tool-task",
  31. kind: "execute",
  32. title: "ls -la",
  33. input: { command: "ls -la" },
  34. status: "completed",
  35. },
  36. {
  37. toolId: "tool-root-after",
  38. kind: "read",
  39. title: "Read README.md",
  40. input: { file_path: "README.md" },
  41. status: "completed",
  42. },
  43. ]);
  44. expect(tree.map((node) => node.toolCall.toolId)).toEqual([
  45. "tool-root-before",
  46. "tool-task",
  47. "tool-root-after",
  48. ]);
  49. expect(tree[1]?.childToolCalls.map((node) => node.toolCall.toolId)).toEqual([
  50. "tool-child-read",
  51. "tool-child-list",
  52. ]);
  53. });
  54. it("falls back to root when parent tool is missing", () => {
  55. const tree = buildToolCallTree([
  56. {
  57. toolId: "tool-child",
  58. parentToolUseId: "missing-parent",
  59. kind: "read",
  60. title: "Read package.json",
  61. input: { file_path: "package.json" },
  62. status: "completed",
  63. },
  64. ]);
  65. expect(tree).toHaveLength(1);
  66. expect(tree[0]?.toolCall.toolId).toBe("tool-child");
  67. });
  68. it("does not attach a tool call to itself", () => {
  69. const tree = buildToolCallTree([
  70. {
  71. toolId: "tool-self",
  72. parentToolUseId: "tool-self",
  73. kind: "think",
  74. title: "Task",
  75. input: null,
  76. status: "completed",
  77. },
  78. ]);
  79. expect(tree).toHaveLength(1);
  80. expect(tree[0]?.toolCall.toolId).toBe("tool-self");
  81. expect(tree[0]?.childToolCalls).toHaveLength(0);
  82. });
  83. it("reuses unchanged nodes and child arrays across equivalent inputs", () => {
  84. const firstTree = reconcileToolCallTree([
  85. {
  86. toolId: "tool-task",
  87. kind: "think",
  88. title: "Task",
  89. input: { prompt: "Explore project" },
  90. status: "completed",
  91. },
  92. {
  93. toolId: "tool-child-read",
  94. parentToolUseId: "tool-task",
  95. kind: "read",
  96. title: "Read package.json",
  97. input: { file_path: "package.json" },
  98. status: "completed",
  99. },
  100. ]);
  101. const secondTree = reconcileToolCallTree(
  102. [
  103. {
  104. toolId: "tool-task",
  105. kind: "think",
  106. title: "Task",
  107. input: { prompt: "Explore project" },
  108. status: "completed",
  109. },
  110. {
  111. toolId: "tool-child-read",
  112. parentToolUseId: "tool-task",
  113. kind: "read",
  114. title: "Read package.json",
  115. input: { file_path: "package.json" },
  116. status: "completed",
  117. },
  118. ],
  119. firstTree,
  120. );
  121. expect(secondTree).toBe(firstTree);
  122. expect(secondTree[0]).toBe(firstTree[0]);
  123. expect(secondTree[0]?.toolCall).toBe(firstTree[0]?.toolCall);
  124. expect(secondTree[0]?.childToolCalls).toBe(firstTree[0]?.childToolCalls);
  125. expect(secondTree[0]?.childToolCalls[0]).toBe(firstTree[0]?.childToolCalls[0]);
  126. });
  127. it("replaces only the changed branch when a child tool call changes", () => {
  128. const firstTree = reconcileToolCallTree([
  129. {
  130. toolId: "tool-task",
  131. kind: "think",
  132. title: "Task",
  133. input: { prompt: "Explore project" },
  134. status: "completed",
  135. },
  136. {
  137. toolId: "tool-child-read",
  138. parentToolUseId: "tool-task",
  139. kind: "read",
  140. title: "Read package.json",
  141. input: { file_path: "package.json" },
  142. status: "pending",
  143. },
  144. {
  145. toolId: "tool-root-after",
  146. kind: "read",
  147. title: "Read README.md",
  148. input: { file_path: "README.md" },
  149. status: "completed",
  150. },
  151. ]);
  152. const secondTree = reconcileToolCallTree(
  153. [
  154. {
  155. toolId: "tool-task",
  156. kind: "think",
  157. title: "Task",
  158. input: { prompt: "Explore project" },
  159. status: "completed",
  160. },
  161. {
  162. toolId: "tool-child-read",
  163. parentToolUseId: "tool-task",
  164. kind: "read",
  165. title: "Read package.json",
  166. input: { file_path: "package.json" },
  167. status: "completed",
  168. },
  169. {
  170. toolId: "tool-root-after",
  171. kind: "read",
  172. title: "Read README.md",
  173. input: { file_path: "README.md" },
  174. status: "completed",
  175. },
  176. ],
  177. firstTree,
  178. );
  179. expect(secondTree).not.toBe(firstTree);
  180. expect(secondTree[0]).not.toBe(firstTree[0]);
  181. expect(secondTree[0]?.toolCall).toBe(firstTree[0]?.toolCall);
  182. expect(secondTree[0]?.childToolCalls[0]).not.toBe(firstTree[0]?.childToolCalls[0]);
  183. expect(secondTree[1]).toBe(firstTree[1]);
  184. });
  185. });