v4-input-admission.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { CommandEnvelope } from "@zcode/shared/zcode-protocol-v4";
  3. import {
  4. isConversationInputAdmissionCommand,
  5. resolveInputCommandForAdmission,
  6. } from "../src/zcode-protocol/v4-bridge.js";
  7. import { inputIntentMetadata } from "../src/zcode-protocol-v4/commands/input-intent.js";
  8. function envelope(type: CommandEnvelope["type"], payload: unknown): CommandEnvelope {
  9. return {
  10. commandId: "cmd-new",
  11. clientId: "client-new",
  12. sessionId: "s-parent",
  13. type,
  14. payload,
  15. issuedAt: 1,
  16. };
  17. }
  18. describe("v4 input admission canonical intent", () => {
  19. it("在写接纳账本前分离旧尾块,并保存新快照字段", () => {
  20. const genUiContext = [{ path: "/project/example.html", state: { stage: "entry" } }];
  21. for (const payload of [
  22. { text: "question", genUiContext },
  23. {
  24. text: `question\n\n<untrusted_gen_ui_state>\n${JSON.stringify(genUiContext)}\n</untrusted_gen_ui_state>`,
  25. },
  26. ]) {
  27. expect(
  28. resolveInputCommandForAdmission(envelope("sendText", payload), "s-parent", vi.fn()),
  29. ).toMatchObject({ text: "question", genUiContext });
  30. }
  31. });
  32. it("gateway admission 接线包含 compact/edit/retry,排除非输入命令", () => {
  33. expect(isConversationInputAdmissionCommand("compact")).toBe(true);
  34. expect(isConversationInputAdmissionCommand("editUserQuery")).toBe(true);
  35. expect(isConversationInputAdmissionCommand("retryTurn")).toBe(true);
  36. expect(isConversationInputAdmissionCommand("applyFileRewind")).toBe(false);
  37. });
  38. it("非输入命令不创建 session_input", () => {
  39. expect(resolveInputCommandForAdmission(envelope("stop", {}), "s-parent", vi.fn())).toBeNull();
  40. });
  41. it("compact admission 保留 typed maintenance intent,不伪装成 sendText", () => {
  42. expect(resolveInputCommandForAdmission(envelope("compact", {}), "s-parent", vi.fn())).toEqual({
  43. kind: "compact",
  44. text: "/compact",
  45. attachments: [],
  46. });
  47. });
  48. it("sendText admission 保留 Highspeed message metadata", () => {
  49. const highspeed = {
  50. schemaVersion: 1 as const,
  51. cardId: "hsc-1",
  52. taskId: "s-parent",
  53. provider: "zai",
  54. model: "glm-5",
  55. issuedAt: 1_000,
  56. expiresAt: 10_000,
  57. };
  58. expect(
  59. resolveInputCommandForAdmission(
  60. envelope("sendText", { text: "accelerate", highspeedMeta: highspeed }),
  61. "s-parent",
  62. vi.fn(),
  63. ),
  64. ).toEqual({
  65. kind: "sendText",
  66. text: "accelerate",
  67. attachments: [],
  68. highspeed,
  69. });
  70. expect(
  71. inputIntentMetadata(
  72. {
  73. ...envelope("sendText", { text: "accelerate", highspeedMeta: highspeed }),
  74. __v4Admission: { admissionSeq: 1, admittedAt: 1, queueItemId: "queue-highspeed" },
  75. } as unknown as CommandEnvelope,
  76. { requestedDelivery: "startNow", text: "accelerate" },
  77. ).highspeed,
  78. ).toEqual(highspeed);
  79. });
  80. it("retry 以新 commandId admission,但保留 canonical kind/delivery/attachments/provenance", () => {
  81. const resolve = vi.fn().mockReturnValue({
  82. ok: true,
  83. action: "retryTurn",
  84. row: {},
  85. messageId: "msg-assistant",
  86. editTarget: {
  87. entityId: "msg-user",
  88. productTurnId: "turn-1",
  89. transcriptMessageId: "msg-user",
  90. coveredByStableCompact: false,
  91. intent: {
  92. kind: "sendGoalCommand",
  93. text: "original objective",
  94. inputOrigin: "mobile",
  95. botGroupSource: {
  96. provider: "feishu",
  97. botId: "bot",
  98. chatId: "group",
  99. senderId: "member",
  100. senderName: "Member",
  101. messageId: "message",
  102. },
  103. sourceCommandId: "cmd-original",
  104. clientId: "client-original",
  105. queueItemId: "queue-original",
  106. requestedDelivery: "guide",
  107. admittedDelivery: "queue",
  108. fallbackReasonCode: "guide.goalUnsupported",
  109. attachments: [
  110. {
  111. ref: "attachment://a",
  112. fileName: "a.txt",
  113. mime: "text/plain",
  114. bytes: 1,
  115. sourceKind: "topic-history",
  116. messageCount: 1,
  117. },
  118. ],
  119. provenance: { sourceCommandId: "cmd-root", clientId: "client-root" },
  120. },
  121. },
  122. });
  123. expect(
  124. resolveInputCommandForAdmission(
  125. envelope("retryTurn", {
  126. target: { rowId: 7, entityId: "msg-assistant" },
  127. }),
  128. "s-parent",
  129. resolve,
  130. ),
  131. ).toEqual({
  132. kind: "sendGoalCommand",
  133. text: "original objective",
  134. inputOrigin: "mobile",
  135. botGroupSource: {
  136. provider: "feishu",
  137. botId: "bot",
  138. chatId: "group",
  139. senderId: "member",
  140. senderName: "Member",
  141. messageId: "message",
  142. },
  143. attachments: [
  144. {
  145. ref: "attachment://a",
  146. fileName: "a.txt",
  147. mime: "text/plain",
  148. bytes: 1,
  149. sourceKind: "topic-history",
  150. messageCount: 1,
  151. },
  152. ],
  153. requestedDelivery: "guide",
  154. admittedDelivery: "queue",
  155. fallbackReasonCode: "guide.goalUnsupported",
  156. provenance: { sourceCommandId: "cmd-root", clientId: "client-root" },
  157. });
  158. });
  159. it("compact-covered edit 只在原 session admission", () => {
  160. const resolve = vi.fn().mockReturnValue({
  161. ok: true,
  162. action: "editUserQuery",
  163. row: {},
  164. editTarget: {
  165. entityId: "msg-user",
  166. productTurnId: "turn-1",
  167. transcriptMessageId: "msg-user",
  168. coveredByStableCompact: true,
  169. intent: {
  170. kind: "sendText",
  171. text: "old",
  172. sourceCommandId: "cmd-original",
  173. },
  174. },
  175. });
  176. const command = envelope("editUserQuery", {
  177. target: { rowId: 7, entityId: "msg-user" },
  178. newText: "edited",
  179. });
  180. expect(resolveInputCommandForAdmission(command, "s-parent", resolve)).toMatchObject({
  181. kind: "sendText",
  182. text: "edited",
  183. provenance: { sourceCommandId: "cmd-original" },
  184. });
  185. });
  186. });