turn-guide-model-selection.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. createModelId,
  4. createModelProviderId,
  5. type Model,
  6. type ModelSelection,
  7. } from "@zcode/contracts";
  8. import { drainInlineGuideForNextRequest } from "../src/runtime/methods/turn-guide-drain.js";
  9. describe("inline Guide model selection", () => {
  10. it("replaces the Loop Model at the next model-step boundary", async () => {
  11. const oldModel = createModel("provider-a", "model-a");
  12. const newModel = createModel("provider-b", "model-b", "high");
  13. const runtimeConfig = {
  14. mode: "build",
  15. modelSelection: {
  16. providerId: "provider-a",
  17. modelId: "model-a",
  18. } satisfies ModelSelection,
  19. };
  20. let sessionModelSelection: ModelSelection = {
  21. providerId: "provider-a",
  22. modelId: "model-a",
  23. };
  24. let canonicalEntries = [
  25. { message: { role: "system" as const, content: "old context" } },
  26. { message: { role: "user" as const, content: "original prompt" } },
  27. ];
  28. const runtime = {
  29. activeTurn: { pendingInputs: [{}] },
  30. config: runtimeConfig,
  31. contextBuilder: { build: vi.fn() },
  32. contextInitialized: true,
  33. contextSourceSnapshot: {
  34. currentDate: "2026-09-03",
  35. envInfo: {
  36. cwd: "/workspace",
  37. nodeVersion: "test",
  38. osVersion: "test",
  39. platform: "test",
  40. shell: "test",
  41. },
  42. workingDirectory: "/workspace",
  43. },
  44. createContextBuilderFromSnapshot: vi.fn(() => ({
  45. build: () => ({
  46. metaUserAttachments: [],
  47. systemMessages: [{ role: "system" as const, content: "provider-b/model-b context" }],
  48. }),
  49. })),
  50. drainPendingInput: vi.fn(async () => ({
  51. injectedMessageIds: [],
  52. latestMessageId: undefined,
  53. pendingInputIds: ["guide-input"],
  54. intent: {
  55. sourceCommandId: "guide-command",
  56. kind: "sendText",
  57. requestedDelivery: "guide",
  58. admittedDelivery: "guide",
  59. modelSelection: {
  60. providerId: "provider-b",
  61. modelId: "model-b",
  62. options: { reasoningLevel: "high" },
  63. },
  64. },
  65. })),
  66. emitModeChanged: vi.fn(),
  67. emitModelSelected: vi.fn(),
  68. getMode: () => runtimeConfig.mode,
  69. getSessionModelSelection: () => structuredClone(sessionModelSelection),
  70. hasInlineGuidePendingInput: () => true,
  71. messageHistory: {
  72. borrowReadOnlyRuntimeEntries: () => canonicalEntries,
  73. replaceMessages: vi.fn((entries) => {
  74. canonicalEntries = entries;
  75. }),
  76. },
  77. modelFactory: vi.fn(() => newModel),
  78. rootTraceContext: { traceId: "guide-trace" },
  79. sessionStore: { saveSessionEntry: vi.fn(async () => undefined) },
  80. sessionId: "guide-session",
  81. setSessionModelSelection: vi.fn((selection: ModelSelection) => {
  82. sessionModelSelection = structuredClone(selection);
  83. }),
  84. updateConfig: vi.fn((patch: typeof runtimeConfig) => {
  85. Object.assign(runtimeConfig, patch);
  86. }),
  87. };
  88. const state = {
  89. activeTurn: runtime.activeTurn,
  90. currentUserMessageId: "message-before-guide",
  91. events: [],
  92. model: oldModel,
  93. repeatedToolCallSignature: "old-signature",
  94. repeatedToolCallStreakCount: 2,
  95. turnTraceContext: { traceId: "guide-trace", queryId: "old-query" },
  96. turnRequestState: {
  97. entries: canonicalEntries,
  98. outputTokenContinuationCount: 0,
  99. },
  100. };
  101. const drained = await drainInlineGuideForNextRequest(runtime as never, state as never);
  102. expect(drained).toBe(true);
  103. expect(runtime.modelFactory).toHaveBeenCalledWith(
  104. expect.objectContaining({
  105. selection: {
  106. providerId: "provider-b",
  107. modelId: "model-b",
  108. options: { reasoningLevel: "high" },
  109. },
  110. }),
  111. );
  112. expect(state.model).toMatchObject({
  113. providerId: "provider-b",
  114. modelId: "model-b",
  115. options: { reasoningLevel: "high" },
  116. });
  117. expect(JSON.stringify(state.turnRequestState.entries)).toContain("provider-b/model-b context");
  118. expect(JSON.stringify(state.turnRequestState.entries)).not.toContain("old context");
  119. expect(runtime.emitModelSelected).toHaveBeenCalledOnce();
  120. expect(runtime.sessionStore.saveSessionEntry).toHaveBeenCalledWith(
  121. expect.objectContaining({
  122. data: {
  123. modelId: "model-b",
  124. providerId: "provider-b",
  125. options: {
  126. reasoningLevel: "high",
  127. },
  128. },
  129. }),
  130. );
  131. });
  132. });
  133. function createModel(providerId: string, modelId: string, reasoningLevel?: string): Model {
  134. return {
  135. providerId: createModelProviderId(providerId),
  136. modelId: createModelId(modelId),
  137. properties: {
  138. contextWindow: 128_000,
  139. supportsMidConversationSystem: false,
  140. supportsNativeWebSearch: false,
  141. supportsJsonSchemaOutput: true,
  142. supportsToolCall: true,
  143. },
  144. optionSpecs: {
  145. reasoningLevel: {
  146. values: ["low", "high"],
  147. },
  148. maxOutputTokens: { max: 8_000 },
  149. },
  150. options: { reasoningLevel, maxOutputTokens: 8_000 },
  151. bind() {
  152. return this;
  153. },
  154. async generateText() {
  155. throw new Error("not used");
  156. },
  157. async *streamText() {
  158. throw new Error("not used");
  159. },
  160. };
  161. }