taskChangeSummary.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildTaskChangeSummary,
  4. buildTaskChangeSummaryFromToolCalls,
  5. } from "@/lib/taskChangeSummary.js";
  6. import type { TaskChatMessage } from "@/lib/taskChatMessageTypes.js";
  7. describe("buildTaskChangeSummary", () => {
  8. it("按当前 task 会话 fileChanges 聚合同一文件的多轮改动", () => {
  9. const summary = buildTaskChangeSummary([
  10. {
  11. turnIndex: 0,
  12. snapshots: [
  13. {
  14. path: "/repo/src/a.ts",
  15. beforeContent: "line1\nline2\n",
  16. afterContent: "line1\nline2\nline3\n",
  17. writeCount: 1,
  18. },
  19. ],
  20. },
  21. {
  22. turnIndex: 1,
  23. snapshots: [
  24. {
  25. path: "/repo/src/a.ts",
  26. beforeContent: "line1\nline2\nline3\n",
  27. afterContent: "line0\nline1\nline2\nline3\n",
  28. writeCount: 2,
  29. },
  30. {
  31. path: "/repo/src/b.ts",
  32. beforeContent: null,
  33. afterContent: "new file\n",
  34. writeCount: 1,
  35. },
  36. ],
  37. },
  38. ]);
  39. expect(summary).toEqual({
  40. fileCount: 2,
  41. added: 3,
  42. removed: 0,
  43. files: [
  44. {
  45. path: "/repo/src/a.ts",
  46. added: 2,
  47. removed: 0,
  48. writeCount: 3,
  49. lastTurnIndex: 1,
  50. },
  51. {
  52. path: "/repo/src/b.ts",
  53. added: 1,
  54. removed: 0,
  55. writeCount: 1,
  56. lastTurnIndex: 1,
  57. },
  58. ],
  59. });
  60. });
  61. it("文件最终回到原内容时仍保留触达记录", () => {
  62. const summary = buildTaskChangeSummary([
  63. {
  64. turnIndex: 0,
  65. snapshots: [
  66. {
  67. path: "/repo/src/a.ts",
  68. beforeContent: "same\n",
  69. afterContent: "changed\n",
  70. writeCount: 1,
  71. },
  72. ],
  73. },
  74. {
  75. turnIndex: 1,
  76. snapshots: [
  77. {
  78. path: "/repo/src/a.ts",
  79. beforeContent: "changed\n",
  80. afterContent: "same\n",
  81. writeCount: 1,
  82. },
  83. ],
  84. },
  85. ]);
  86. expect(summary).toEqual({
  87. fileCount: 1,
  88. added: 0,
  89. removed: 0,
  90. files: [
  91. {
  92. path: "/repo/src/a.ts",
  93. added: 0,
  94. removed: 0,
  95. writeCount: 2,
  96. lastTurnIndex: 1,
  97. },
  98. ],
  99. });
  100. });
  101. it("没有会话文件变更时返回 null", () => {
  102. expect(buildTaskChangeSummary([])).toBeNull();
  103. expect(buildTaskChangeSummary(undefined)).toBeNull();
  104. });
  105. it("按当前 task 消息里的 tool call changes 聚合 Recent changes", () => {
  106. const messages: TaskChatMessage[] = [
  107. {
  108. id: "user-1",
  109. role: "user",
  110. content: "edit files",
  111. timestamp: 1,
  112. },
  113. {
  114. id: "assistant-1",
  115. role: "assistant",
  116. content: "",
  117. timestamp: 2,
  118. turnIndex: 0,
  119. toolCalls: [
  120. {
  121. toolId: "tool-1",
  122. toolName: "edit",
  123. kind: "edit",
  124. title: "Edit src/a.ts",
  125. input: { path: "src/a.ts" },
  126. status: "completed",
  127. raw: {
  128. display: {
  129. kind: "file_diffs",
  130. files: [
  131. {
  132. filePath: "/repo/src/a.ts",
  133. additions: 2,
  134. deletions: 1,
  135. },
  136. {
  137. filePath: "/repo/src/b.ts",
  138. additions: 3,
  139. deletions: 0,
  140. },
  141. ],
  142. },
  143. },
  144. },
  145. ],
  146. },
  147. {
  148. id: "assistant-2",
  149. role: "assistant",
  150. content: "",
  151. timestamp: 3,
  152. turnIndex: 2,
  153. toolCalls: [
  154. {
  155. toolId: "tool-2",
  156. toolName: "edit",
  157. kind: "edit",
  158. title: "Edit src/a.ts",
  159. input: { path: "src/a.ts" },
  160. status: "completed",
  161. raw: {
  162. display: {
  163. kind: "file_diff",
  164. filePath: "/repo/src/a.ts",
  165. additions: 1,
  166. deletions: 2,
  167. },
  168. },
  169. },
  170. ],
  171. },
  172. ];
  173. const summary = buildTaskChangeSummaryFromToolCalls(messages);
  174. expect(summary).toEqual({
  175. fileCount: 2,
  176. added: 6,
  177. removed: 3,
  178. files: [
  179. {
  180. path: "/repo/src/a.ts",
  181. added: 3,
  182. removed: 3,
  183. writeCount: 2,
  184. lastTurnIndex: 2,
  185. },
  186. {
  187. path: "/repo/src/b.ts",
  188. added: 3,
  189. removed: 0,
  190. writeCount: 1,
  191. lastTurnIndex: 0,
  192. },
  193. ],
  194. });
  195. });
  196. it("Recent changes 不把非写类 tool call 当作文件变更", () => {
  197. const summary = buildTaskChangeSummaryFromToolCalls([
  198. {
  199. id: "assistant-1",
  200. role: "assistant",
  201. content: "",
  202. timestamp: 1,
  203. turnIndex: 0,
  204. toolCalls: [
  205. {
  206. toolId: "tool-1",
  207. toolName: "read",
  208. kind: "explore",
  209. title: "Read src/a.ts",
  210. input: { path: "/repo/src/a.ts" },
  211. status: "completed",
  212. raw: {
  213. display: {
  214. kind: "file_diff",
  215. filePath: "/repo/src/a.ts",
  216. additions: 9,
  217. deletions: 1,
  218. },
  219. },
  220. },
  221. ],
  222. },
  223. ]);
  224. expect(summary).toBeNull();
  225. });
  226. it("Recent changes 不把失败或拒绝的写工具输入当作文件变更", () => {
  227. const summary = buildTaskChangeSummaryFromToolCalls([
  228. {
  229. id: "assistant-1",
  230. role: "assistant",
  231. content: "",
  232. timestamp: 1,
  233. turnIndex: 0,
  234. toolCalls: [
  235. {
  236. toolId: "tool-1",
  237. toolName: "Write",
  238. kind: "write",
  239. title: "Write denied.ts",
  240. input: {
  241. file_path: "/repo/denied.ts",
  242. content: "must not be counted\n",
  243. },
  244. status: "failed",
  245. raw: { status: "failed" },
  246. },
  247. {
  248. toolId: "tool-2",
  249. toolName: "Write",
  250. kind: "write",
  251. title: "Write rejected.ts",
  252. input: {
  253. file_path: "/repo/rejected.ts",
  254. content: "must not be counted either\n",
  255. },
  256. status: "denied",
  257. raw: { status: "denied" },
  258. },
  259. ],
  260. },
  261. ]);
  262. expect(summary).toBeNull();
  263. });
  264. });