session-transcript.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { describe, expect, it } from "vitest";
  2. import type { MessageWithParts } from "@zcode/contracts";
  3. import { projectSessionTranscript } from "../src/session-transcript.js";
  4. describe("session transcript projection", () => {
  5. it("filters model-only goal continuation user inputs", () => {
  6. const transcript = projectSessionTranscript([
  7. {
  8. info: { role: "user" },
  9. parts: [
  10. {
  11. id: "part-user",
  12. text: "加一个地址系统,可以加,可以选",
  13. type: "text",
  14. },
  15. ],
  16. },
  17. {
  18. info: {
  19. role: "user",
  20. source: "goal-continuation",
  21. synthetic: true,
  22. visibility: "model-only",
  23. },
  24. parts: [
  25. {
  26. id: "part-goal",
  27. metadata: {
  28. source: "goal-continuation",
  29. visibility: "model-only",
  30. },
  31. synthetic: true,
  32. text: [
  33. '<system-reminder source="goal-continuation">',
  34. "Continue working toward the active session goal.",
  35. "</system-reminder>",
  36. ].join("\n"),
  37. type: "text",
  38. },
  39. ],
  40. },
  41. {
  42. info: { role: "assistant" },
  43. parts: [
  44. {
  45. id: "part-assistant",
  46. text: "继续完成并验证了地址系统。",
  47. type: "text",
  48. },
  49. ],
  50. },
  51. ] as unknown as MessageWithParts[]);
  52. expect(transcript).toEqual([
  53. {
  54. content: "加一个地址系统,可以加,可以选",
  55. role: "user",
  56. },
  57. {
  58. content: "继续完成并验证了地址系统。",
  59. role: "agent",
  60. },
  61. ]);
  62. });
  63. it("preserves assistant thought and tool parts for resume rendering", () => {
  64. const transcript = projectSessionTranscript([
  65. {
  66. info: { role: "user" },
  67. parts: [
  68. {
  69. id: "part-user",
  70. text: "read the file",
  71. type: "text",
  72. },
  73. ],
  74. },
  75. {
  76. info: { role: "assistant" },
  77. parts: [
  78. {
  79. id: "part-thought",
  80. text: "I should inspect package.json first.",
  81. type: "reasoning",
  82. },
  83. {
  84. id: "part-text",
  85. text: "I'll inspect it.",
  86. type: "text",
  87. },
  88. {
  89. callID: "call-read",
  90. id: "part-tool",
  91. state: {
  92. input: { file_path: "/workspace/project/package.json" },
  93. output: '{"name":"demo"}',
  94. status: "completed",
  95. title: "Read",
  96. },
  97. tool: "Read",
  98. type: "tool",
  99. },
  100. ],
  101. },
  102. ] as unknown as MessageWithParts[]);
  103. expect(transcript).toEqual([
  104. {
  105. content: "read the file",
  106. role: "user",
  107. },
  108. {
  109. content: "I'll inspect it.",
  110. parts: [
  111. {
  112. text: "I should inspect package.json first.",
  113. type: "thought",
  114. },
  115. {
  116. text: "I'll inspect it.",
  117. type: "text",
  118. },
  119. {
  120. input: { file_path: "/workspace/project/package.json" },
  121. output: '{"name":"demo"}',
  122. status: "completed",
  123. title: "Read",
  124. toolCallId: "call-read",
  125. toolName: "Read",
  126. type: "tool",
  127. },
  128. ],
  129. role: "agent",
  130. },
  131. ]);
  132. });
  133. it("restores persisted file diff display metadata for completed tools", () => {
  134. const display = {
  135. additions: 1,
  136. deletions: 1,
  137. filePath: "/workspace/project/src/app.ts",
  138. kind: "file_diff" as const,
  139. structuredPatch: [
  140. {
  141. oldStart: 1,
  142. oldLines: 1,
  143. newStart: 1,
  144. newLines: 1,
  145. lines: ["-old", "+new"],
  146. },
  147. ],
  148. truncated: false,
  149. };
  150. const transcript = projectSessionTranscript([
  151. {
  152. info: { role: "assistant" },
  153. parts: [
  154. {
  155. callID: "call-edit",
  156. id: "part-tool",
  157. state: {
  158. input: {
  159. file_path: "/workspace/project/src/app.ts",
  160. old_string: "old",
  161. new_string: "new",
  162. },
  163. metadata: {
  164. schemaVersion: 1,
  165. display,
  166. serialization: {
  167. budgetStrategy: "inline",
  168. originalBytes: 42,
  169. returnedBytes: 42,
  170. truncated: false,
  171. },
  172. },
  173. output: "The file has been updated successfully.",
  174. status: "completed",
  175. title: "Edit",
  176. time: { start: 1, end: 2 },
  177. },
  178. tool: "Edit",
  179. type: "tool",
  180. },
  181. ],
  182. },
  183. ] as unknown as MessageWithParts[]);
  184. expect(transcript[0]?.parts?.[0]).toMatchObject({
  185. input: {
  186. file_path: "/workspace/project/src/app.ts",
  187. old_string: "old",
  188. new_string: "new",
  189. },
  190. output: "The file has been updated successfully.",
  191. resultDisplay: display,
  192. status: "completed",
  193. toolCallId: "call-edit",
  194. toolName: "Edit",
  195. type: "tool",
  196. });
  197. });
  198. it.each([
  199. {
  200. toolName: "TaskOutput",
  201. input: { task_id: "task-1", block: false, timeout: 0 },
  202. display: {
  203. kind: "task_output",
  204. retrievalStatus: "not_ready",
  205. taskStatus: "running",
  206. output: "partial output",
  207. } as const,
  208. },
  209. {
  210. toolName: "RespondToCoordinator",
  211. input: { summary: "Reply", message: "Continue working." },
  212. display: {
  213. kind: "respond_to_coordinator",
  214. status: "success",
  215. } as const,
  216. },
  217. ])("restores persisted $toolName display metadata", ({ toolName, input, display }) => {
  218. const transcript = projectSessionTranscript([
  219. {
  220. info: { role: "assistant" },
  221. parts: [
  222. {
  223. callID: `call-${toolName}`,
  224. id: `part-${toolName}`,
  225. state: {
  226. input,
  227. metadata: {
  228. schemaVersion: 1,
  229. display,
  230. },
  231. output: "provider-visible content",
  232. status: "completed",
  233. title: toolName,
  234. time: { start: 1, end: 2 },
  235. },
  236. tool: toolName,
  237. type: "tool",
  238. },
  239. ],
  240. },
  241. ] as unknown as MessageWithParts[]);
  242. expect(transcript[0]?.parts?.[0]).toMatchObject({
  243. input,
  244. output: "provider-visible content",
  245. resultDisplay: display,
  246. status: "completed",
  247. toolName,
  248. type: "tool",
  249. });
  250. });
  251. });