zcodePersistedMessageMerge.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. coalesceConsecutiveZCodeAssistants,
  4. type ZCodePersistedMessage,
  5. } from "../src/index.js";
  6. function userMessage(
  7. overrides: Partial<ZCodePersistedMessage> = {},
  8. ): ZCodePersistedMessage {
  9. return {
  10. role: "user",
  11. content: "hello",
  12. timestamp: 1_000,
  13. ...overrides,
  14. };
  15. }
  16. function assistantMessage(
  17. overrides: Partial<ZCodePersistedMessage> = {},
  18. ): ZCodePersistedMessage {
  19. return {
  20. role: "assistant",
  21. content: "ok",
  22. timestamp: 2_000,
  23. durationMs: 500,
  24. ...overrides,
  25. };
  26. }
  27. describe("coalesceConsecutiveZCodeAssistants", () => {
  28. it("leaves a single user/assistant pair untouched (apart from turnIndex)", () => {
  29. const input = [userMessage(), assistantMessage()];
  30. const result = coalesceConsecutiveZCodeAssistants(input);
  31. expect(result).toHaveLength(2);
  32. expect(result[0]).toMatchObject({ role: "user", turnIndex: 0 });
  33. expect(result[1]).toMatchObject({
  34. role: "assistant",
  35. durationMs: 500,
  36. turnIndex: 0,
  37. });
  38. });
  39. it("merges two consecutive assistants into one with summed-span duration", () => {
  40. const first = assistantMessage({
  41. timestamp: 2_000,
  42. durationMs: 1_000,
  43. content: "round1 ",
  44. thought: "thinking1 ",
  45. tools: [{ toolName: "Bash", input: { command: "ls" } }],
  46. parts: [
  47. { type: "content", content: "round1 " },
  48. { type: "tool-call", toolIndex: 0 },
  49. ],
  50. });
  51. const next = assistantMessage({
  52. timestamp: 4_000, // 1s gap simulating tool execution between rounds
  53. durationMs: 2_000,
  54. content: "round2",
  55. thought: "thinking2",
  56. tools: [{ toolName: "Read", input: { path: "/x" } }],
  57. parts: [
  58. { type: "content", content: "round2" },
  59. { type: "tool-call", toolIndex: 0 },
  60. ],
  61. });
  62. const result = coalesceConsecutiveZCodeAssistants([
  63. userMessage(),
  64. first,
  65. next,
  66. ]);
  67. expect(result).toHaveLength(2);
  68. const merged = result[1]!;
  69. expect(merged.role).toBe("assistant");
  70. expect(merged.timestamp).toBe(2_000);
  71. // 整轮跨度 = next 结束时刻 (4000 + 2000) − first 起点 (2000)
  72. expect(merged.durationMs).toBe(4_000);
  73. expect(merged.content).toBe("round1 round2");
  74. expect(merged.thought).toBe("thinking1 thinking2");
  75. expect(merged.tools).toEqual([
  76. { toolName: "Bash", input: { command: "ls" } },
  77. { toolName: "Read", input: { path: "/x" } },
  78. ]);
  79. expect(merged.parts).toEqual([
  80. { type: "content", content: "round1 " },
  81. { type: "tool-call", toolIndex: 0 },
  82. { type: "content", content: "round2" },
  83. { type: "tool-call", toolIndex: 1 }, // shifted from 0 by firstTools.length
  84. ]);
  85. expect(merged.turnIndex).toBe(0);
  86. });
  87. it("keeps original assistant message ids when merging consecutive assistants", () => {
  88. const result = coalesceConsecutiveZCodeAssistants([
  89. userMessage(),
  90. assistantMessage({ id: "msg_assistant_1", content: "round1 " }),
  91. assistantMessage({ id: "msg_assistant_2", content: "round2" }),
  92. ]);
  93. expect(result[1]).toMatchObject({
  94. id: "msg_assistant_1",
  95. mergedMessageIds: ["msg_assistant_1", "msg_assistant_2"],
  96. });
  97. });
  98. it("merges three consecutive assistants (covers the 1s/1s/2s screenshot case)", () => {
  99. const result = coalesceConsecutiveZCodeAssistants([
  100. userMessage({ timestamp: 0 }),
  101. assistantMessage({ timestamp: 100, durationMs: 1_000, content: "a" }),
  102. assistantMessage({ timestamp: 1_100, durationMs: 1_000, content: "b" }),
  103. assistantMessage({ timestamp: 2_100, durationMs: 2_000, content: "c" }),
  104. ]);
  105. expect(result).toHaveLength(2);
  106. const merged = result[1]!;
  107. expect(merged.content).toBe("abc");
  108. expect(merged.timestamp).toBe(100);
  109. expect(merged.durationMs).toBe(4_000); // 2100+2000 − 100
  110. });
  111. it("returns undefined duration when any round is still in progress", () => {
  112. const result = coalesceConsecutiveZCodeAssistants([
  113. userMessage(),
  114. assistantMessage({ timestamp: 2_000, durationMs: 500 }),
  115. assistantMessage({ timestamp: 2_500, durationMs: undefined }),
  116. ]);
  117. expect(result[1]!.durationMs).toBeUndefined();
  118. });
  119. it("does not merge synthetic timeline assistants into surrounding assistant content", () => {
  120. const compact = assistantMessage({
  121. id: "msg_compact",
  122. content: "",
  123. timestamp: 4_000,
  124. durationMs: 2_000,
  125. syntheticTimeline: {
  126. version: 1,
  127. kind: "synthetic",
  128. type: "context_compaction",
  129. operationId: "cmp-1",
  130. status: "completed",
  131. trigger: "manual",
  132. display: "separator",
  133. },
  134. });
  135. const result = coalesceConsecutiveZCodeAssistants([
  136. userMessage({ timestamp: 1_000 }),
  137. assistantMessage({
  138. id: "msg_assistant",
  139. timestamp: 2_000,
  140. durationMs: 500,
  141. content: "hello",
  142. }),
  143. compact,
  144. assistantMessage({
  145. id: "msg_skipped",
  146. content: "",
  147. timestamp: 8_000,
  148. durationMs: 0,
  149. syntheticTimeline: {
  150. version: 1,
  151. kind: "synthetic",
  152. type: "context_compaction",
  153. operationId: "cmp-2",
  154. status: "skipped",
  155. trigger: "manual",
  156. display: "separator",
  157. },
  158. }),
  159. ]);
  160. expect(result).toHaveLength(4);
  161. expect(result[1]).toMatchObject({
  162. id: "msg_assistant",
  163. durationMs: 500,
  164. });
  165. expect(result[2]).toMatchObject({
  166. id: "msg_compact",
  167. syntheticTimeline: { operationId: "cmp-1" },
  168. });
  169. expect(result[3]).toMatchObject({
  170. id: "msg_skipped",
  171. syntheticTimeline: { operationId: "cmp-2" },
  172. });
  173. });
  174. it("is a no-op on strictly alternating legacy ZCode Agent data", () => {
  175. const input = [
  176. userMessage({ timestamp: 0, turnIndex: 0 }),
  177. assistantMessage({
  178. timestamp: 100,
  179. durationMs: 1_000,
  180. turnIndex: 0,
  181. content: "a",
  182. }),
  183. userMessage({ timestamp: 2_000, turnIndex: 1, content: "hi again" }),
  184. assistantMessage({
  185. timestamp: 2_100,
  186. durationMs: 500,
  187. turnIndex: 1,
  188. content: "b",
  189. }),
  190. ];
  191. const result = coalesceConsecutiveZCodeAssistants(input);
  192. expect(result).toHaveLength(4);
  193. expect(
  194. result.map((m) => ({ role: m.role, turnIndex: m.turnIndex })),
  195. ).toEqual([
  196. { role: "user", turnIndex: 0 },
  197. { role: "assistant", turnIndex: 0 },
  198. { role: "user", turnIndex: 1 },
  199. { role: "assistant", turnIndex: 1 },
  200. ]);
  201. });
  202. it("assigns turnIndex by counting user messages across merged sequence", () => {
  203. const result = coalesceConsecutiveZCodeAssistants([
  204. userMessage({ timestamp: 0, content: "u0" }),
  205. assistantMessage({ timestamp: 1, durationMs: 1 }),
  206. assistantMessage({ timestamp: 2, durationMs: 1 }),
  207. userMessage({ timestamp: 10, content: "u1" }),
  208. assistantMessage({ timestamp: 11, durationMs: 1 }),
  209. ]);
  210. expect(result.map((m) => m.turnIndex)).toEqual([0, 0, 1, 1]);
  211. });
  212. it("propagates next's interrupted / feedback / model to the merged message", () => {
  213. const result = coalesceConsecutiveZCodeAssistants([
  214. userMessage(),
  215. assistantMessage({ model: "m1", durationMs: 500, content: "a" }),
  216. assistantMessage({
  217. timestamp: 2_500,
  218. durationMs: 500,
  219. model: "m2",
  220. content: "b",
  221. interrupted: true,
  222. feedback: "like",
  223. }),
  224. ]);
  225. const merged = result[1]!;
  226. expect(merged.model).toBe("m2");
  227. expect(merged.interrupted).toBe(true);
  228. expect(merged.feedback).toBe("like");
  229. });
  230. it("recomputes characterCount only when at least one round tracked it", () => {
  231. const withCount = coalesceConsecutiveZCodeAssistants([
  232. userMessage(),
  233. assistantMessage({ content: "hello", characterCount: 5 }),
  234. assistantMessage({ timestamp: 2_500, durationMs: 500, content: "!" }),
  235. ]);
  236. expect(withCount[1]!.characterCount).toBe("hello!".length);
  237. const withoutCount = coalesceConsecutiveZCodeAssistants([
  238. userMessage(),
  239. assistantMessage({ content: "hello" }),
  240. assistantMessage({ timestamp: 2_500, durationMs: 500, content: "!" }),
  241. ]);
  242. expect(withoutCount[1]!.characterCount).toBeUndefined();
  243. });
  244. });