v4-input-admission.test.ts 7.1 KB

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