v4-native-selection-side-chat.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { CommandEnvelope } from "@zcode/shared/zcode-protocol-v4";
  3. import { startPromptTurn } from "../src/zcode-protocol-v4/commands/prompt-turn.js";
  4. import {
  5. V4CommandExecutor,
  6. V4SelectionSideChatRestrictedCommandError,
  7. } from "../src/zcode-protocol-v4/commands/executor.js";
  8. import type {
  9. V4CommandCoreHost,
  10. V4SessionRecordView,
  11. } from "../src/zcode-protocol-v4/commands/types.js";
  12. vi.mock("../src/zcode-protocol-v4/commands/prompt-turn.js", () => ({
  13. startPromptTurn: vi.fn(),
  14. }));
  15. function record(taskType: V4SessionRecordView["taskType"] = "interactive"): V4SessionRecordView {
  16. return {
  17. app: { sessionId: "parent-1" } as V4SessionRecordView["app"],
  18. persistence: "immediate",
  19. taskType,
  20. workspace: { workspacePath: "/workspace" },
  21. } as V4SessionRecordView;
  22. }
  23. function envelope(type: CommandEnvelope["type"], payload: unknown = {}): CommandEnvelope {
  24. return {
  25. clientId: "client-1",
  26. commandId: `cmd-${type}`,
  27. issuedAt: 1,
  28. payload,
  29. sessionId: "parent-1",
  30. type,
  31. } as CommandEnvelope;
  32. }
  33. describe("v4 native selection side chat", () => {
  34. it("从 envelope 父会话创建 child 并返回结构化 ACK result", async () => {
  35. const parent = record();
  36. const createSelectionSideSession = vi.fn().mockResolvedValue({
  37. sessionId: "child-1",
  38. });
  39. const host: V4CommandCoreHost = {
  40. createSelectionSideSession,
  41. getRecord: (sessionId) => (sessionId === "parent-1" ? parent : undefined),
  42. };
  43. const result = await new V4CommandExecutor(host).execute(
  44. envelope("createSelectionSideSession"),
  45. );
  46. expect(createSelectionSideSession).toHaveBeenCalledWith("parent-1", {
  47. revisionAtDecision: 0,
  48. sourceCommandId: "cmd-createSelectionSideSession",
  49. });
  50. expect(result).toEqual({
  51. type: "createSelectionSideSession",
  52. sessionId: "child-1",
  53. });
  54. });
  55. it("携带 firstInput 时只在 child 上启动首轮普通输入", async () => {
  56. const parent = record();
  57. const child = record();
  58. child.app.sessionId = "child-1";
  59. const createSelectionSideSession = vi.fn().mockResolvedValue({ sessionId: "child-1" });
  60. const admitInputCommand = vi.fn(async () => null);
  61. vi.mocked(startPromptTurn).mockResolvedValue({
  62. turnStarted: Promise.resolve(),
  63. admission: {
  64. completion: Promise.resolve({}),
  65. kind: "started_turn",
  66. turnId: "turn-1",
  67. },
  68. messageId: "message-1",
  69. });
  70. const host: V4CommandCoreHost = {
  71. createSelectionSideSession,
  72. admitInputCommand,
  73. getRecord: (sessionId) =>
  74. sessionId === "parent-1" ? parent : sessionId === "child-1" ? child : undefined,
  75. };
  76. const result = await new V4CommandExecutor(host).execute(
  77. envelope("createSelectionSideSession", { firstInput: { text: "你好" } }),
  78. );
  79. expect(startPromptTurn).toHaveBeenCalledWith(
  80. host,
  81. child,
  82. expect.objectContaining({
  83. content: "你好",
  84. inputId: "cmd-createSelectionSideSession",
  85. intent: expect.objectContaining({
  86. text: "你好",
  87. requestedDelivery: "startNow",
  88. }),
  89. }),
  90. );
  91. expect(admitInputCommand).toHaveBeenCalledWith(
  92. expect.objectContaining({
  93. sessionId: "parent-1",
  94. type: "createSelectionSideSession",
  95. payload: { firstInput: { text: "你好" } },
  96. }),
  97. "child-1",
  98. expect.objectContaining({ queueItemId: "queue_cmd-createSelectionSideSession" }),
  99. );
  100. expect(result).toEqual({
  101. type: "createSelectionSideSession",
  102. sessionId: "child-1",
  103. input: {
  104. delivery: "startNow",
  105. inputId: "cmd-createSelectionSideSession",
  106. messageId: "message-1",
  107. },
  108. });
  109. });
  110. it("推荐模型随 child 创建一次传入,不修改父会话", async () => {
  111. const parent = record();
  112. const child = record();
  113. child.app.sessionId = "child-1";
  114. const createSelectionSideSession = vi.fn().mockResolvedValue({ sessionId: "child-1" });
  115. const admitInputCommand = vi.fn(async () => null);
  116. vi.mocked(startPromptTurn).mockResolvedValue({
  117. turnStarted: Promise.resolve(),
  118. admission: {
  119. completion: Promise.resolve({}),
  120. kind: "started_turn",
  121. turnId: "turn-1",
  122. },
  123. messageId: "message-1",
  124. });
  125. const host: V4CommandCoreHost = {
  126. createSelectionSideSession,
  127. admitInputCommand,
  128. getRecord: (sessionId) =>
  129. sessionId === "parent-1" ? parent : sessionId === "child-1" ? child : undefined,
  130. };
  131. const result = await new V4CommandExecutor(host).execute(
  132. envelope("createSelectionSideSession", {
  133. firstInput: {
  134. text: "你好",
  135. modelSelection: {
  136. providerId: "account:bigmodel-start-plan",
  137. modelId: "glm-5",
  138. options: { reasoningLevel: "high" },
  139. },
  140. },
  141. }),
  142. );
  143. expect(createSelectionSideSession).toHaveBeenCalledWith(
  144. "parent-1",
  145. expect.objectContaining({
  146. modelSelection: {
  147. providerId: "account:bigmodel-start-plan",
  148. modelId: "glm-5",
  149. options: { reasoningLevel: "high" },
  150. },
  151. }),
  152. );
  153. expect(startPromptTurn).toHaveBeenCalledWith(
  154. host,
  155. child,
  156. expect.objectContaining({
  157. content: "你好",
  158. inputId: "cmd-createSelectionSideSession",
  159. intent: expect.objectContaining({
  160. text: "你好",
  161. requestedDelivery: "startNow",
  162. }),
  163. }),
  164. );
  165. expect(admitInputCommand).toHaveBeenCalledWith(
  166. expect.objectContaining({
  167. sessionId: "parent-1",
  168. type: "createSelectionSideSession",
  169. payload: {
  170. firstInput: {
  171. text: "你好",
  172. modelSelection: {
  173. providerId: "account:bigmodel-start-plan",
  174. modelId: "glm-5",
  175. options: { reasoningLevel: "high" },
  176. },
  177. },
  178. },
  179. }),
  180. "child-1",
  181. expect.objectContaining({ queueItemId: "queue_cmd-createSelectionSideSession" }),
  182. );
  183. expect(result).toEqual({
  184. type: "createSelectionSideSession",
  185. sessionId: "child-1",
  186. input: {
  187. delivery: "startNow",
  188. inputId: "cmd-createSelectionSideSession",
  189. messageId: "message-1",
  190. },
  191. });
  192. });
  193. it.each([
  194. "sendGoalCommand",
  195. "resumeGoal",
  196. "editUserQuery",
  197. "retryTurn",
  198. "forkAssistant",
  199. ] as const)("selection_side_chat 拒绝受限命令 %s", async (command) => {
  200. const host: V4CommandCoreHost = {
  201. getRecord: () => record("selection_side_chat"),
  202. };
  203. await expect(
  204. new V4CommandExecutor(host).execute(envelope(command)),
  205. ).rejects.toMatchObject<V4SelectionSideChatRestrictedCommandError>({
  206. reasonCode: "guard.selectionSideChatRestrictedCommand",
  207. });
  208. });
  209. });