provider-config-model-creation.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { ModelNetworkStatusEvent } from "@zcode/contracts";
  3. import {
  4. ApiKeyAccessConfig,
  5. ModelConfig,
  6. ModelInputFormatConfig,
  7. ModelOptionSpecsConfig,
  8. ModelOutputFormatConfig,
  9. ModelPropertiesConfig,
  10. ProviderApiConfig,
  11. ProviderConfig,
  12. type RegistryModelConfig,
  13. type RegistryProviderConfig,
  14. } from "@zcode/provider";
  15. import { AiSdkModelAdapter } from "../src/model/runner.js";
  16. import type { AiSdkModelRuntime } from "../src/model/runner-runtime.js";
  17. describe("AiSdkModelAdapter Provider Config creation", () => {
  18. it("keeps option maps out of duplicate AI SDK provider options", async () => {
  19. const requests: Array<{ providerOptions?: Record<string, unknown> }> = [];
  20. const statusEvents: ModelNetworkStatusEvent[] = [];
  21. const runtime: AiSdkModelRuntime = {
  22. async generateText(options) {
  23. requests.push(options);
  24. return {
  25. finishReason: "stop",
  26. text: "done",
  27. totalUsage: {},
  28. usage: {},
  29. } as never;
  30. },
  31. streamText() {
  32. throw new Error("not used");
  33. },
  34. };
  35. const providerConfig = new ProviderConfig({
  36. access: new ApiKeyAccessConfig({ apiKey: "test-key" }),
  37. api: new ProviderApiConfig({
  38. type: "openai-responses",
  39. baseUrl: "https://api.openai.com/v1",
  40. }),
  41. builtinModelIds: ["gpt-5.6-sol"],
  42. enabled: true,
  43. id: "openai",
  44. personalModelIds: [],
  45. name: "OpenAI",
  46. visibility: "visible",
  47. }) as RegistryProviderConfig;
  48. const modelConfig = new ModelConfig({
  49. enabled: true,
  50. properties: new ModelPropertiesConfig({
  51. requiresMfjsToolSchema: false,
  52. contextWindow: 1_050_000,
  53. inputFormat: new ModelInputFormatConfig({
  54. supportsText: true,
  55. supportsImage: true,
  56. supportsVideo: false,
  57. supportsAudio: false,
  58. supportsPdf: true,
  59. }),
  60. outputFormat: new ModelOutputFormatConfig({ supportsText: true }),
  61. supportsToolCall: true,
  62. supportsJsonSchemaOutput: true,
  63. supportsNativeWebSearch: false,
  64. supportsMidConversationSystem: false,
  65. }),
  66. optionSpecs: new ModelOptionSpecsConfig({
  67. reasoningLevel: {
  68. values: ["none", "low", "medium", "high", "xhigh", "max"],
  69. map: '{"reasoning":{"effort":reasoningLevel}}',
  70. },
  71. maxOutputTokens: {
  72. max: 128_000,
  73. map: '{"max_output_tokens":maxOutputTokens}',
  74. },
  75. }),
  76. }) as RegistryModelConfig;
  77. const adapter = new AiSdkModelAdapter({
  78. runtime,
  79. statusSink: { publish: (event) => statusEvents.push(event) },
  80. });
  81. const model = adapter.createModel({
  82. providerId: "openai",
  83. modelId: "gpt-5.6-sol",
  84. providerConfig,
  85. modelConfig,
  86. options: { maxOutputTokens: 128_000, reasoningLevel: "max" },
  87. });
  88. await model.generateText({ messages: [{ role: "user", content: "hello" }] });
  89. await model.generateText({
  90. messages: [{ role: "user", content: "summarize cheaply" }],
  91. options: { reasoningLevel: "none" },
  92. });
  93. expect(requests.map((request) => request.providerOptions)).toEqual([
  94. { apiFormat: "openai-responses" },
  95. { apiFormat: "openai-responses" },
  96. ]);
  97. expect(model.options).toEqual({ maxOutputTokens: 128_000, reasoningLevel: "max" });
  98. expect(
  99. statusEvents
  100. .filter((event) => event.type === "model_request_started")
  101. .map((event) => event.modelCall.reasoning),
  102. ).toEqual([
  103. expect.objectContaining({
  104. requestedControl: "provider_default",
  105. requestedLevel: "max",
  106. requestedState: "provider_default",
  107. }),
  108. expect.objectContaining({
  109. requestedControl: "provider_default",
  110. requestedLevel: "none",
  111. requestedState: "provider_default",
  112. }),
  113. ]);
  114. });
  115. it("directly consumes complete Registry Config and freezes it for the created Model", async () => {
  116. const receivedModels: unknown[] = [];
  117. const runtime: AiSdkModelRuntime = {
  118. async generateText(options) {
  119. receivedModels.push(options.model);
  120. return {
  121. finishReason: "stop",
  122. text: "done",
  123. totalUsage: {},
  124. usage: {},
  125. } as never;
  126. },
  127. streamText() {
  128. throw new Error("not used");
  129. },
  130. };
  131. const providerConfig = new ProviderConfig({
  132. access: new ApiKeyAccessConfig({ apiKey: "first-key" }),
  133. api: new ProviderApiConfig({
  134. type: "openai-responses",
  135. baseUrl: "https://first.example.com/v1",
  136. }),
  137. builtinModelIds: ["model-a"],
  138. enabled: true,
  139. id: "provider-a",
  140. personalModelIds: [],
  141. name: "Provider A",
  142. visibility: "visible",
  143. }) as RegistryProviderConfig;
  144. const modelConfig = new ModelConfig({
  145. enabled: true,
  146. properties: new ModelPropertiesConfig({
  147. requiresMfjsToolSchema: false,
  148. contextWindow: 128_000,
  149. inputFormat: new ModelInputFormatConfig({
  150. supportsText: true,
  151. supportsImage: false,
  152. supportsVideo: false,
  153. supportsAudio: false,
  154. supportsPdf: false,
  155. }),
  156. outputFormat: new ModelOutputFormatConfig({ supportsText: true }),
  157. supportsToolCall: true,
  158. supportsJsonSchemaOutput: true,
  159. supportsNativeWebSearch: false,
  160. supportsMidConversationSystem: false,
  161. }),
  162. optionSpecs: new ModelOptionSpecsConfig({
  163. reasoningLevel: {
  164. values: ["disabled"],
  165. map: "{}",
  166. },
  167. maxOutputTokens: {
  168. max: 16_000,
  169. map: '{"max_output_tokens":maxOutputTokens}',
  170. },
  171. }),
  172. }) as RegistryModelConfig;
  173. const adapter = new AiSdkModelAdapter({ runtime });
  174. const model = adapter.createModel({
  175. providerId: "provider-a",
  176. modelId: "model-a",
  177. providerConfig,
  178. modelConfig,
  179. options: { maxOutputTokens: 8_000, reasoningLevel: "disabled" },
  180. });
  181. await model.generateText({ messages: [{ role: "user", content: "hello" }] });
  182. expect(receivedModels).toHaveLength(1);
  183. expect(model.properties.contextWindow).toBe(128_000);
  184. expect(model.options.maxOutputTokens).toBe(8_000);
  185. });
  186. it("does not expose a mutable execution Registry API", () => {
  187. const adapter = new AiSdkModelAdapter({});
  188. expect(adapter).not.toHaveProperty("replaceRegistryConfig");
  189. expect(adapter).not.toHaveProperty("resolveConnection");
  190. });
  191. });