session-selection-protocol.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { describe, expect, it, vi } from "vitest";
  2. import { zcodeProtocolMethods, type ZCodeProtocolMessage } from "@zcode/shared";
  3. import { ZCodeProtocolAgentServer } from "../src/zcode-protocol/server.js";
  4. import { createFakeApp } from "./helpers/fake-zcode-app.js";
  5. async function request(server: ZCodeProtocolAgentServer, method: string, params: unknown) {
  6. const response = await server.handleMessage({ id: 1, method, params } as ZCodeProtocolMessage);
  7. if (!response || !("result" in response)) throw new Error(JSON.stringify(response));
  8. return response.result;
  9. }
  10. describe("Session 原选择与输入协议", () => {
  11. it("完整配置命令保留档位,Session read 不用候选名单过滤原选择", async () => {
  12. let app!: ReturnType<typeof createFakeApp>;
  13. const server = new ZCodeProtocolAgentServer({
  14. createZCodeApp: (options) => (app = createFakeApp(options)),
  15. });
  16. await request(server, zcodeProtocolMethods.sessionCreate, {
  17. workspace: { workspacePath: "/fixture", workspaceKey: "/fixture" },
  18. });
  19. const setModel = vi.spyOn(app, "setModel");
  20. const selection = {
  21. providerId: "glm",
  22. modelId: "glm-4-air",
  23. options: { reasoningLevel: "deep" },
  24. };
  25. const result = await request(server, zcodeProtocolMethods.sessionSetModel, {
  26. sessionId: app.sessionId,
  27. model: selection,
  28. });
  29. expect(setModel).toHaveBeenCalledWith(selection);
  30. expect(result).toMatchObject({ settings: { model: { current: selection } } });
  31. vi.spyOn(app.runtime, "getSessionModelSelection").mockReturnValue({
  32. ...selection,
  33. options: { reasoningLevel: "removed-level" },
  34. });
  35. expect(
  36. await request(server, zcodeProtocolMethods.sessionRead, { sessionId: app.sessionId }),
  37. ).toMatchObject({
  38. settings: {
  39. model: { current: { ...selection, options: { reasoningLevel: "removed-level" } } },
  40. },
  41. });
  42. await request(server, zcodeProtocolMethods.sessionClose, { sessionId: app.sessionId });
  43. });
  44. it("附件 Selection 进入同一个 input intent,执行凭据只进入非持久执行依赖", async () => {
  45. let app!: ReturnType<typeof createFakeApp>;
  46. let finish!: (value: never) => void;
  47. const completion = new Promise<never>((resolve) => {
  48. finish = resolve;
  49. });
  50. const sendInput = vi.fn(async () => ({
  51. kind: "started_turn" as const,
  52. turnId: "turn" as never,
  53. completion,
  54. }));
  55. const server = new ZCodeProtocolAgentServer({
  56. createZCodeApp: (options) => (app = createFakeApp(options, { sendInput })),
  57. });
  58. await request(server, zcodeProtocolMethods.sessionCreate, {
  59. workspace: { workspacePath: "/fixture", workspaceKey: "/fixture" },
  60. });
  61. const selection = {
  62. providerId: "account:offpeak",
  63. modelId: "fixture-model",
  64. options: { reasoningLevel: "high" },
  65. };
  66. await request(server, zcodeProtocolMethods.sessionSend, {
  67. sessionId: app.sessionId,
  68. inputId: "attachment-input",
  69. content: "hi",
  70. attachments: [
  71. { kind: "image", filename: "a.png", mimeType: "image/png", dataBase64: "aGk=" },
  72. ],
  73. modelSelection: selection,
  74. modelExecution: {
  75. selectionScope: "execution",
  76. memoryExtraction: "skip",
  77. requestAuth: { headers: { "x-ticket": "fixture" } },
  78. },
  79. });
  80. await vi.waitFor(() => expect(sendInput).toHaveBeenCalled());
  81. const [input, options] = sendInput.mock.calls[0]! as unknown as Parameters<
  82. typeof app.sendInput
  83. >;
  84. expect(input).toMatchObject({
  85. text: "hi",
  86. attachments: [expect.objectContaining({ type: "image" })],
  87. });
  88. expect(options).toMatchObject({
  89. inputId: "attachment-input",
  90. intent: { modelSelection: selection },
  91. modelExecution: { selectionScope: "execution", memoryExtraction: "skip" },
  92. });
  93. expect(JSON.stringify(options?.intent)).not.toContain("x-ticket");
  94. expect(
  95. await options?.modelExecution?.requestDependencies?.requestAuth?.source.resolve({} as never),
  96. ).toEqual({ headers: { "x-ticket": "fixture" } });
  97. expect(app.runtime.getSessionModelSelection()?.providerId).toBe("glm");
  98. try {
  99. const next = await server.handleMessage({
  100. id: 2,
  101. method: zcodeProtocolMethods.sessionSend,
  102. params: { sessionId: app.sessionId, content: "next" },
  103. });
  104. expect(next).toMatchObject({ error: { code: -32010 } });
  105. expect(sendInput).toHaveBeenCalledTimes(1);
  106. } finally {
  107. finish({} as never);
  108. }
  109. await request(server, zcodeProtocolMethods.sessionClose, { sessionId: app.sessionId });
  110. });
  111. });