v4-input-admission.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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("gateway admission 接线包含 compact/edit/retry,排除非输入命令", () => {
  20. expect(isConversationInputAdmissionCommand("compact")).toBe(true);
  21. expect(isConversationInputAdmissionCommand("editUserQuery")).toBe(true);
  22. expect(isConversationInputAdmissionCommand("retryTurn")).toBe(true);
  23. expect(isConversationInputAdmissionCommand("applyFileRewind")).toBe(false);
  24. });
  25. it("非输入命令不创建 session_input", () => {
  26. expect(resolveInputCommandForAdmission(envelope("stop", {}), "s-parent", vi.fn())).toBeNull();
  27. });
  28. it("compact admission 保留 typed maintenance intent,不伪装成 sendText", () => {
  29. expect(resolveInputCommandForAdmission(envelope("compact", {}), "s-parent", vi.fn())).toEqual({
  30. kind: "compact",
  31. text: "/compact",
  32. attachments: [],
  33. });
  34. });
  35. it("sendText admission 保留 Highspeed message metadata", () => {
  36. const highspeed = {
  37. schemaVersion: 1 as const,
  38. cardId: "hsc-1",
  39. taskId: "s-parent",
  40. provider: "zai",
  41. model: "glm-5",
  42. issuedAt: 1_000,
  43. expiresAt: 10_000,
  44. };
  45. expect(
  46. resolveInputCommandForAdmission(
  47. envelope("sendText", { text: "accelerate", highspeedMeta: highspeed }),
  48. "s-parent",
  49. vi.fn(),
  50. ),
  51. ).toEqual({
  52. kind: "sendText",
  53. text: "accelerate",
  54. attachments: [],
  55. highspeed,
  56. });
  57. expect(
  58. inputIntentMetadata(
  59. {
  60. ...envelope("sendText", { text: "accelerate", highspeedMeta: highspeed }),
  61. __v4Admission: { admissionSeq: 1, admittedAt: 1, queueItemId: "queue-highspeed" },
  62. } as unknown as CommandEnvelope,
  63. { requestedDelivery: "startNow", text: "accelerate" },
  64. ).highspeed,
  65. ).toEqual(highspeed);
  66. });
  67. it("retry 以新 commandId admission,但保留 canonical kind/delivery/attachments/provenance", () => {
  68. const resolve = vi.fn().mockReturnValue({
  69. ok: true,
  70. action: "retryTurn",
  71. row: {},
  72. messageId: "msg-assistant",
  73. editTarget: {
  74. entityId: "msg-user",
  75. productTurnId: "turn-1",
  76. transcriptMessageId: "msg-user",
  77. coveredByStableCompact: false,
  78. intent: {
  79. kind: "sendGoalCommand",
  80. text: "original objective",
  81. inputOrigin: "mobile",
  82. botGroupSource: {
  83. provider: "feishu",
  84. botId: "bot",
  85. chatId: "group",
  86. senderId: "member",
  87. senderName: "Member",
  88. messageId: "message",
  89. },
  90. sourceCommandId: "cmd-original",
  91. clientId: "client-original",
  92. queueItemId: "queue-original",
  93. requestedDelivery: "guide",
  94. admittedDelivery: "queue",
  95. fallbackReasonCode: "guide.goalUnsupported",
  96. attachments: [
  97. {
  98. ref: "attachment://a",
  99. fileName: "a.txt",
  100. mime: "text/plain",
  101. bytes: 1,
  102. sourceKind: "topic-history",
  103. messageCount: 1,
  104. },
  105. ],
  106. provenance: { sourceCommandId: "cmd-root", clientId: "client-root" },
  107. },
  108. },
  109. });
  110. expect(
  111. resolveInputCommandForAdmission(
  112. envelope("retryTurn", {
  113. target: { rowId: 7, entityId: "msg-assistant" },
  114. }),
  115. "s-parent",
  116. resolve,
  117. ),
  118. ).toEqual({
  119. kind: "sendGoalCommand",
  120. text: "original objective",
  121. inputOrigin: "mobile",
  122. botGroupSource: {
  123. provider: "feishu",
  124. botId: "bot",
  125. chatId: "group",
  126. senderId: "member",
  127. senderName: "Member",
  128. messageId: "message",
  129. },
  130. attachments: [
  131. {
  132. ref: "attachment://a",
  133. fileName: "a.txt",
  134. mime: "text/plain",
  135. bytes: 1,
  136. sourceKind: "topic-history",
  137. messageCount: 1,
  138. },
  139. ],
  140. requestedDelivery: "guide",
  141. admittedDelivery: "queue",
  142. fallbackReasonCode: "guide.goalUnsupported",
  143. provenance: { sourceCommandId: "cmd-root", clientId: "client-root" },
  144. });
  145. });
  146. it("compact-covered edit 只在原 session admission", () => {
  147. const resolve = vi.fn().mockReturnValue({
  148. ok: true,
  149. action: "editUserQuery",
  150. row: {},
  151. editTarget: {
  152. entityId: "msg-user",
  153. productTurnId: "turn-1",
  154. transcriptMessageId: "msg-user",
  155. coveredByStableCompact: true,
  156. intent: {
  157. kind: "sendText",
  158. text: "old",
  159. sourceCommandId: "cmd-original",
  160. },
  161. },
  162. });
  163. const command = envelope("editUserQuery", {
  164. target: { rowId: 7, entityId: "msg-user" },
  165. newText: "edited",
  166. });
  167. expect(resolveInputCommandForAdmission(command, "s-parent", resolve)).toMatchObject({
  168. kind: "sendText",
  169. text: "edited",
  170. provenance: { sourceCommandId: "cmd-original" },
  171. });
  172. });
  173. });