model-io.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { mkdtemp, readFile, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { deepStrictEqual, equal } from "node:assert/strict";
  5. import { describe, it } from "node:test";
  6. import {
  7. deriveTrajectoriesFromModelIoEntries,
  8. writeModelIoAnthropicTrajectory,
  9. type ModelIoJsonlEntry,
  10. } from "../src/model-io.js";
  11. describe("deriveTrajectoriesFromModelIoEntries", () => {
  12. it("expands model-io body message deltas before deriving the main trajectory", () => {
  13. const entries: ModelIoJsonlEntry[] = [
  14. {
  15. querySource: "main_turn",
  16. request: {
  17. body: {
  18. messages: [{ role: "user", content: [{ type: "text", text: "first" }] }],
  19. model: "glm-test",
  20. },
  21. bodyMessageCount: 1,
  22. bodyMessageOffset: 0,
  23. bodyMessagesKind: "full",
  24. },
  25. response: {
  26. finishReason: "stop",
  27. text: "first answer",
  28. toolCalls: [],
  29. },
  30. type: "model_io",
  31. },
  32. {
  33. querySource: "main_turn",
  34. request: {
  35. body: {
  36. messages: [
  37. {
  38. role: "assistant",
  39. content: [{ type: "text", text: "first answer" }],
  40. },
  41. { role: "user", content: [{ type: "text", text: "second" }] },
  42. ],
  43. model: "glm-test",
  44. },
  45. bodyMessageCount: 3,
  46. bodyMessageOffset: 1,
  47. bodyMessagesKind: "delta",
  48. },
  49. response: {
  50. finishReason: "stop",
  51. text: "second answer",
  52. toolCalls: [],
  53. },
  54. type: "model_io",
  55. },
  56. ];
  57. const result = deriveTrajectoriesFromModelIoEntries(entries);
  58. equal(result.trajectories.length, 1);
  59. equal(result.trajectories[0]?.requestCount, 2);
  60. deepStrictEqual(result.trajectories[0]?.requestBody.messages, [
  61. { role: "user", content: [{ type: "text", text: "first" }] },
  62. { role: "assistant", content: [{ type: "text", text: "first answer" }] },
  63. { role: "user", content: [{ type: "text", text: "second" }] },
  64. { role: "assistant", content: [{ type: "text", text: "second answer" }] },
  65. ]);
  66. });
  67. it("keeps append-only main turns together when a session_title sidecar is present", () => {
  68. const entries: ModelIoJsonlEntry[] = [
  69. {
  70. querySource: "main_turn",
  71. request: {
  72. body: {
  73. messages: [{ role: "user", content: [{ type: "text", text: "first" }] }],
  74. model: "glm-test",
  75. },
  76. bodyMessageCount: 1,
  77. bodyMessageOffset: 0,
  78. bodyMessagesKind: "full",
  79. },
  80. response: {
  81. finishReason: "tool-calls",
  82. text: "reading",
  83. toolCalls: [{ id: "call_1", input: { file_path: "/tmp/a.md" }, name: "Read" }],
  84. },
  85. type: "model_io",
  86. },
  87. {
  88. querySource: "session_title",
  89. request: {
  90. body: {
  91. messages: [{ role: "user", content: "Generate a title" }],
  92. model: "glm-test",
  93. },
  94. bodyMessageCount: 1,
  95. bodyMessageOffset: 0,
  96. bodyMessagesKind: "full",
  97. },
  98. response: { finishReason: "stop", text: "{\"title\":\"demo\"}", toolCalls: [] },
  99. type: "model_io",
  100. },
  101. {
  102. querySource: "main_turn",
  103. request: {
  104. body: {
  105. messages: [
  106. { role: "user", content: [{ type: "text", text: "first" }] },
  107. {
  108. role: "assistant",
  109. content: [
  110. { type: "text", text: "reading" },
  111. {
  112. type: "tool_use",
  113. id: "call_1",
  114. input: { file_path: "/tmp/a.md" },
  115. name: "Read",
  116. },
  117. ],
  118. },
  119. {
  120. role: "user",
  121. content: [
  122. { type: "tool_result", tool_use_id: "call_1", content: "# A" },
  123. { type: "text", text: "summarize" },
  124. ],
  125. },
  126. ],
  127. model: "glm-test",
  128. },
  129. bodyMessageCount: 3,
  130. bodyMessageOffset: 0,
  131. bodyMessagesKind: "full",
  132. },
  133. response: { finishReason: "stop", text: "summary", toolCalls: [] },
  134. type: "model_io",
  135. },
  136. ];
  137. const result = deriveTrajectoriesFromModelIoEntries(entries);
  138. equal(result.trajectories.length, 1);
  139. equal(result.trajectories[0]?.requestCount, 2);
  140. equal(result.manifest.trajectories[0]?.reason, "initial");
  141. });
  142. });
  143. describe("writeModelIoAnthropicTrajectory", () => {
  144. it("writes a single Anthropic trajectory JSON for main_turn records by default", async () => {
  145. const root = await mkdtemp(join(tmpdir(), "zcode-model-io-trajectory-"));
  146. const inputPath = join(root, "model-io.jsonl");
  147. const entries: ModelIoJsonlEntry[] = [
  148. {
  149. querySource: "session_title",
  150. request: {
  151. body: {
  152. messages: [{ role: "user", content: "Generate a title" }],
  153. model: "glm-test",
  154. },
  155. bodyMessageCount: 1,
  156. bodyMessageOffset: 0,
  157. bodyMessagesKind: "full",
  158. },
  159. response: { finishReason: "stop", text: "{\"title\":\"demo\"}", toolCalls: [] },
  160. type: "model_io",
  161. },
  162. {
  163. querySource: "main_turn",
  164. request: {
  165. body: {
  166. messages: [{ role: "user", content: [{ type: "text", text: "hello" }] }],
  167. model: "glm-test",
  168. system: [{ type: "text", text: "stable system" }],
  169. },
  170. bodyMessageCount: 1,
  171. bodyMessageOffset: 0,
  172. bodyMessagesKind: "full",
  173. },
  174. response: { finishReason: "stop", text: "hi", toolCalls: [] },
  175. type: "model_io",
  176. },
  177. ];
  178. await writeFile(
  179. inputPath,
  180. entries.map((entry) => JSON.stringify(entry)).join("\n") + "\n",
  181. "utf8",
  182. );
  183. await writeModelIoAnthropicTrajectory({ inputPath, outDir: root });
  184. const anthropic = JSON.parse(await readFile(join(root, "anthropic_trajectory.json"), "utf8"));
  185. deepStrictEqual(anthropic, {
  186. model: "glm-test",
  187. system: [{ type: "text", text: "stable system" }],
  188. messages: [
  189. { role: "user", content: [{ type: "text", text: "hello" }] },
  190. { role: "assistant", content: [{ type: "text", text: "hi" }] },
  191. ],
  192. });
  193. });
  194. it("projects recorded mid-conversation system blocks to the final wire string", async () => {
  195. const root = await mkdtemp(join(tmpdir(), "zcode-model-io-mcs-"));
  196. const inputPath = join(root, "model-io.jsonl");
  197. const entries: ModelIoJsonlEntry[] = [
  198. {
  199. querySource: "main_turn",
  200. request: {
  201. body: {
  202. messages: [
  203. { role: "user", content: [{ type: "text", text: "hello" }] },
  204. { role: "system", content: [{ type: "text", text: "runtime reminder" }] },
  205. { role: "assistant", content: [{ type: "text", text: "hi" }] },
  206. { role: "user", content: [{ type: "text", text: "continue" }] },
  207. ],
  208. model: "glm-test",
  209. system: [{ type: "text", text: "stable system" }],
  210. },
  211. bodyMessageCount: 4,
  212. bodyMessageOffset: 0,
  213. bodyMessagesKind: "full",
  214. },
  215. response: { finishReason: "stop", text: "done", toolCalls: [] },
  216. type: "model_io",
  217. },
  218. ];
  219. await writeFile(
  220. inputPath,
  221. entries.map((entry) => JSON.stringify(entry)).join("\n") + "\n",
  222. "utf8",
  223. );
  224. await writeModelIoAnthropicTrajectory({ inputPath, outDir: root });
  225. const openAi = JSON.parse(
  226. await readFile(join(root, "trajectories", "0001.openai_request_body.json"), "utf8"),
  227. );
  228. const anthropic = JSON.parse(await readFile(join(root, "anthropic_trajectory.json"), "utf8"));
  229. deepStrictEqual(openAi.messages, [
  230. { role: "user", content: [{ type: "text", text: "hello" }] },
  231. { role: "system", content: [{ type: "text", text: "runtime reminder" }] },
  232. { role: "assistant", content: [{ type: "text", text: "hi" }] },
  233. { role: "user", content: [{ type: "text", text: "continue" }] },
  234. { role: "assistant", content: [{ type: "text", text: "done" }] },
  235. ]);
  236. deepStrictEqual(anthropic.system, [{ type: "text", text: "stable system" }]);
  237. deepStrictEqual(anthropic.messages, [
  238. { role: "user", content: [{ type: "text", text: "hello" }] },
  239. { role: "system", content: "runtime reminder" },
  240. { role: "assistant", content: [{ type: "text", text: "hi" }] },
  241. { role: "user", content: [{ type: "text", text: "continue" }] },
  242. { role: "assistant", content: [{ type: "text", text: "done" }] },
  243. ]);
  244. });
  245. });