zcode-protocol-message-mapper.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createMessageId,
  4. createPartId,
  5. createSessionId,
  6. type MessageWithParts,
  7. type ModelId,
  8. type ModelProviderId,
  9. } from "@zcode/contracts";
  10. import { mapMessageWithParts } from "../src/zcode-protocol/message-mapper.js";
  11. describe("zcode protocol message mapper", () => {
  12. it("does not expose provider-only tool metadata", () => {
  13. const sessionID = createSessionId("tool-error-mapper-session");
  14. const messageID = createMessageId("tool-error-mapper-message");
  15. const partID = createPartId("tool-error-mapper-part");
  16. const providerOnlyPartID = createPartId("tool-error-mapper-provider-only-part");
  17. const rawEmptyPartID = createPartId("tool-error-mapper-raw-empty-part");
  18. const legitimatePlaceholderPartID = createPartId(
  19. "tool-error-mapper-legitimate-placeholder-part",
  20. );
  21. const modelContent = "<tool_use_error>InputValidationError: missing parameter</tool_use_error>";
  22. const modelContentLayoutText = "provider-only completed media layout";
  23. const mapped = mapMessageWithParts({
  24. info: {
  25. id: messageID,
  26. sessionID,
  27. role: "assistant",
  28. time: {
  29. created: 1_700_000_000_000,
  30. completed: 1_700_000_000_010,
  31. },
  32. parentID: createMessageId("tool-error-mapper-parent"),
  33. modelId: "test-model" as ModelId,
  34. providerId: "test-provider" as ModelProviderId,
  35. mode: "build",
  36. agent: "zcode-agent",
  37. path: {
  38. cwd: "/tmp/zcode",
  39. root: "/tmp/zcode",
  40. },
  41. cost: 0,
  42. tokens: {
  43. input: 0,
  44. output: 0,
  45. reasoning: 0,
  46. cache: {
  47. read: 0,
  48. write: 0,
  49. },
  50. },
  51. },
  52. parts: [
  53. {
  54. id: partID,
  55. sessionID,
  56. messageID,
  57. type: "tool",
  58. callID: "tool_error_mapper",
  59. tool: "AskUserQuestion",
  60. metadata: {
  61. keepAtPartLevel: "visible",
  62. providerToolName: "",
  63. },
  64. state: {
  65. status: "error",
  66. input: {},
  67. error: "Tool input failed inputSchema validation",
  68. metadata: {
  69. keep: "visible",
  70. modelContent,
  71. },
  72. time: {
  73. start: 1_700_000_000_000,
  74. end: 1_700_000_000_010,
  75. },
  76. },
  77. },
  78. {
  79. id: providerOnlyPartID,
  80. sessionID,
  81. messageID,
  82. type: "tool",
  83. callID: "tool_error_mapper_provider_only",
  84. tool: "empty_tool_name",
  85. metadata: {
  86. providerToolName: "",
  87. },
  88. state: {
  89. status: "error",
  90. input: {},
  91. error: "Model returned an invalid tool call: tool name is empty.",
  92. time: {
  93. start: 1_700_000_000_000,
  94. end: 1_700_000_000_010,
  95. },
  96. },
  97. },
  98. {
  99. id: rawEmptyPartID,
  100. sessionID,
  101. messageID,
  102. type: "tool",
  103. callID: "tool_error_mapper_raw_empty",
  104. tool: " \t ",
  105. state: {
  106. status: "error",
  107. input: {},
  108. error: "Model returned an invalid tool call: tool name is empty.",
  109. time: {
  110. start: 1_700_000_000_000,
  111. end: 1_700_000_000_010,
  112. },
  113. },
  114. },
  115. {
  116. id: legitimatePlaceholderPartID,
  117. sessionID,
  118. messageID,
  119. type: "tool",
  120. callID: "tool_error_mapper_legitimate_placeholder",
  121. tool: "empty_tool_name",
  122. state: {
  123. status: "completed",
  124. input: {},
  125. output: "ok",
  126. title: "empty_tool_name",
  127. metadata: {
  128. keep: "visible",
  129. modelContentLayout: [{ type: "text", text: modelContentLayoutText }],
  130. },
  131. time: {
  132. start: 1_700_000_000_000,
  133. end: 1_700_000_000_010,
  134. },
  135. },
  136. },
  137. ],
  138. } satisfies MessageWithParts);
  139. expect(mapped.parts[0]).toEqual(
  140. expect.objectContaining({
  141. metadata: { keepAtPartLevel: "visible" },
  142. state: expect.objectContaining({
  143. metadata: { keep: "visible" },
  144. status: "error",
  145. }),
  146. type: "tool",
  147. }),
  148. );
  149. expect(mapped.parts).toHaveLength(2);
  150. expect(mapped.parts[1]).toMatchObject({
  151. callId: "tool_error_mapper_legitimate_placeholder",
  152. state: {
  153. metadata: { keep: "visible" },
  154. status: "completed",
  155. },
  156. tool: "empty_tool_name",
  157. type: "tool",
  158. });
  159. expect(JSON.stringify(mapped)).not.toContain(modelContent);
  160. expect(JSON.stringify(mapped)).not.toContain(modelContentLayoutText);
  161. expect(JSON.stringify(mapped)).not.toContain("modelContentLayout");
  162. expect(JSON.stringify(mapped)).not.toContain("providerToolName");
  163. expect(JSON.stringify(mapped)).not.toContain("tool_error_mapper_raw_empty");
  164. });
  165. it("preserves timeline parts in session snapshots", () => {
  166. const sessionID = createSessionId("timeline-mapper-session");
  167. const messageID = createMessageId("timeline-mapper-message");
  168. const partID = createPartId("timeline-mapper-part");
  169. const mapped = mapMessageWithParts({
  170. info: {
  171. id: messageID,
  172. sessionID,
  173. role: "assistant",
  174. time: {
  175. created: 1_700_000_000_000,
  176. completed: 1_700_000_000_010,
  177. },
  178. parentID: createMessageId("timeline-mapper-parent"),
  179. modelId: "test-model" as ModelId,
  180. providerId: "test-provider" as ModelProviderId,
  181. mode: "build",
  182. agent: "zcode-agent",
  183. path: {
  184. cwd: "/tmp/zcode",
  185. root: "/tmp/zcode",
  186. },
  187. cost: 0,
  188. tokens: {
  189. input: 0,
  190. output: 0,
  191. reasoning: 0,
  192. cache: {
  193. read: 0,
  194. write: 0,
  195. },
  196. },
  197. semantics: {
  198. origin: "system",
  199. kind: "timeline_event",
  200. uiVisibility: "visible",
  201. providerVisibility: "hidden",
  202. transcriptVisibility: "visible",
  203. },
  204. },
  205. parts: [
  206. {
  207. id: partID,
  208. sessionID,
  209. messageID,
  210. type: "timeline",
  211. timelineType: "model_change",
  212. display: "separator",
  213. status: "completed",
  214. toModel: {
  215. providerId: "test-provider" as ModelProviderId,
  216. modelId: "test-model" as ModelId,
  217. label: "test-provider/test-model",
  218. },
  219. time: {
  220. start: 1_700_000_000_000,
  221. end: 1_700_000_000_010,
  222. },
  223. },
  224. ],
  225. } satisfies MessageWithParts);
  226. expect(mapped.info).toMatchObject({
  227. role: "assistant",
  228. semantics: {
  229. kind: "timeline_event",
  230. providerVisibility: "hidden",
  231. },
  232. });
  233. expect(mapped.parts).toEqual([
  234. expect.objectContaining({
  235. display: "separator",
  236. partId: partID,
  237. timelineType: "model_change",
  238. toModel: {
  239. label: "test-provider/test-model",
  240. modelId: "test-model",
  241. providerId: "test-provider",
  242. },
  243. type: "timeline",
  244. }),
  245. ]);
  246. });
  247. });
  248. // 历史/索引共用的 CLI snapshot 出口必须解码旧尾块,不能依赖 UI 再过滤。
  249. it.each([undefined, []])(
  250. "normalizes legacy snapshots but preserves explicit new-format text (%j)",
  251. (genUiContext) => {
  252. const text =
  253. 'Question\n\n<untrusted_gen_ui_state>\n[{"path":"/project/page.html","state":{"stage":"entry"}}]\n</untrusted_gen_ui_state>';
  254. const message = {
  255. info: {
  256. id: "message",
  257. sessionID: "session",
  258. role: "user",
  259. time: { created: 1 },
  260. metadata: { conversationInputIntent: { genUiContext } },
  261. },
  262. parts: [{ id: "part", messageID: "message", sessionID: "session", type: "text", text }],
  263. } as unknown as MessageWithParts;
  264. expect(mapMessageWithParts(message).parts[0]).toMatchObject({
  265. text: genUiContext ? text : "Question",
  266. });
  267. expect(message.parts[0]).toMatchObject({ text });
  268. },
  269. );