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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { describe, expect, it } 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. // openai 段是 Responses 加密推理回传的选项,store 和 include 由同一处写出;两个档位的推理映射
  94. // 都写了 reasoning,所以都带 include。映射本身的字段仍只进请求体,不复制进 providerOptions。
  95. const responsesOptions = {
  96. apiFormat: "openai-responses",
  97. openai: { store: false, include: ["reasoning.encrypted_content"] },
  98. };
  99. expect(requests.map((request) => request.providerOptions)).toEqual([
  100. responsesOptions,
  101. responsesOptions,
  102. ]);
  103. expect(model.options).toEqual({ maxOutputTokens: 128_000, reasoningLevel: "max" });
  104. expect(
  105. statusEvents
  106. .filter((event) => event.type === "model_request_started")
  107. .map((event) => event.modelCall.reasoning),
  108. ).toEqual([
  109. expect.objectContaining({
  110. requestedControl: "provider_default",
  111. requestedLevel: "max",
  112. requestedState: "provider_default",
  113. }),
  114. expect.objectContaining({
  115. requestedControl: "provider_default",
  116. requestedLevel: "none",
  117. requestedState: "provider_default",
  118. }),
  119. ]);
  120. });
  121. });