new-model-wire.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { createModelId, createModelProviderId } from "@zcode/contracts";
  4. import {
  5. createRegistryModelConfig,
  6. createRegistryProviderConfig,
  7. parseProviderConfig,
  8. parseZCodeBuiltinModelConfigRules,
  9. } from "@zcode/provider";
  10. import { AiSdkModelAdapter } from "../src/model/runner.js";
  11. describe("新目录通过真实 SDK 发出的请求", () => {
  12. for (const [modelId, apiType, baseUrl, levels] of [
  13. ["gpt-6-astra", "openai-responses", "https://api.openai.com/v1", ["low", "max"]],
  14. ["claude-fable-5-1", "anthropic-messages", "https://api.anthropic.com/v1", ["low", "max"]],
  15. // 厂商的无预算 enabled 不能穿透到 OpenRouter;验证最终 SDK wire 而非只编译 Map。
  16. ["z-ai/glm-5.3", "anthropic-messages", "https://openrouter.ai/api", ["low", "max"]],
  17. ["z-ai/glm-5.3-flash", "anthropic-messages", "https://openrouter.ai/api", ["high"]],
  18. [
  19. "deepseek/deepseek-v4.1-flash",
  20. "anthropic-messages",
  21. "https://openrouter.ai/api",
  22. ["disabled", "max"],
  23. ],
  24. ["moonshotai/kimi-k2.5", "anthropic-messages", "https://openrouter.ai/api", ["enabled"]],
  25. ["qwen/qwen3.8-max-0902", "anthropic-messages", "https://openrouter.ai/api", ["minimal"]],
  26. ["xiaomi/mimo-v2.5", "anthropic-messages", "https://openrouter.ai/api", ["enabled"]],
  27. [
  28. "deepseek-flash",
  29. "anthropic-messages",
  30. "https://api.deepseek.com/anthropic",
  31. ["disabled", "low", "high", "max"],
  32. ],
  33. [
  34. "deepseek-flash",
  35. "openai-chat-completions",
  36. "https://api.deepseek.com/v1",
  37. ["disabled", "low", "high", "max"],
  38. ],
  39. [
  40. "deepseek-flash",
  41. "openai-responses",
  42. "https://api.deepseek.com/v1",
  43. ["disabled", "low", "high", "max"],
  44. ],
  45. ] as const)
  46. for (const level of levels)
  47. it(`${modelId} / ${apiType} / ${level}`, async () => {
  48. const release = JSON.parse(
  49. await readFile(
  50. new URL("../../../../../config/provider/zcode-builtin.json", import.meta.url),
  51. "utf8",
  52. ),
  53. );
  54. const config = parseZCodeBuiltinModelConfigRules(release.config.modelConfigRules).resolve({
  55. providerId: "fixture",
  56. modelId,
  57. apiType,
  58. baseUrl,
  59. });
  60. const modelConfig = createRegistryModelConfig(config);
  61. const providerConfig = createRegistryProviderConfig(
  62. parseProviderConfig({
  63. group: "standard-personal",
  64. access: { type: "api-key", apiKey: "fixture" },
  65. api: { type: apiType, baseUrl },
  66. }),
  67. );
  68. if (!modelConfig.ok || !providerConfig.ok) throw new Error("incomplete fixture");
  69. let body: Record<string, unknown> | undefined;
  70. vi.stubGlobal("fetch", async (url: RequestInfo | URL, init?: RequestInit) => {
  71. const request = new Request(url, init);
  72. body = await request.json();
  73. if (apiType === "anthropic-messages")
  74. return Response.json({
  75. id: "fixture",
  76. type: "message",
  77. role: "assistant",
  78. model: modelId,
  79. content: [
  80. { type: "text", text: modelId.startsWith("claude") ? '{"ok":true}' : "ok" },
  81. ],
  82. stop_reason: "end_turn",
  83. stop_sequence: null,
  84. usage: { input_tokens: 1, output_tokens: 1 },
  85. });
  86. if (apiType === "openai-chat-completions")
  87. return Response.json({
  88. id: "fixture",
  89. object: "chat.completion",
  90. created: 1,
  91. model: modelId,
  92. choices: [
  93. { index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" },
  94. ],
  95. usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
  96. });
  97. return Response.json({
  98. id: "resp_fixture",
  99. object: "response",
  100. created_at: 1,
  101. status: "completed",
  102. model: modelId,
  103. output: [
  104. {
  105. id: "msg_fixture",
  106. type: "message",
  107. role: "assistant",
  108. status: "completed",
  109. content: [{ type: "output_text", text: "ok", annotations: [] }],
  110. },
  111. ],
  112. usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 },
  113. });
  114. });
  115. try {
  116. const model = new AiSdkModelAdapter({ env: {}, retry: { maxAttempts: 1 } }).createModel({
  117. providerId: createModelProviderId("fixture"),
  118. modelId: createModelId(modelId),
  119. providerConfig: providerConfig.config,
  120. modelConfig: modelConfig.config,
  121. options: { maxOutputTokens: 4000, reasoningLevel: level },
  122. });
  123. await model.generateText({
  124. messages: [{ role: "user", content: "hi" }],
  125. tools: [
  126. {
  127. name: "Read",
  128. inputSchema: {
  129. type: "object",
  130. properties: { path: { type: "string" } },
  131. required: ["path"],
  132. additionalProperties: false,
  133. },
  134. },
  135. ],
  136. ...(modelId.startsWith("claude")
  137. ? {
  138. responseJsonSchema: {
  139. type: "object",
  140. properties: { ok: { type: "boolean" } },
  141. required: ["ok"],
  142. additionalProperties: false,
  143. },
  144. }
  145. : {}),
  146. });
  147. expect(body?.model).toBe(modelId);
  148. if (apiType === "openai-responses") {
  149. expect(body).toMatchObject({
  150. reasoning: { effort: level === "disabled" ? "none" : level },
  151. });
  152. expect(body?.max_output_tokens).toBe(4000);
  153. } else {
  154. expect(body).toMatchObject({
  155. thinking: {
  156. type:
  157. level === "disabled"
  158. ? "disabled"
  159. : modelId.startsWith("claude") || baseUrl === "https://openrouter.ai/api"
  160. ? "adaptive"
  161. : "enabled",
  162. },
  163. });
  164. if (level !== "disabled")
  165. expect(body).toMatchObject(
  166. apiType === "anthropic-messages"
  167. ? { output_config: { effort: level === "enabled" ? "high" : level } }
  168. : { reasoning_effort: level },
  169. );
  170. if (modelId.startsWith("claude")) {
  171. expect(body).toMatchObject({
  172. tool_choice: { type: "auto" },
  173. output_config: { format: { type: "json_schema" } },
  174. });
  175. }
  176. if (baseUrl === "https://openrouter.ai/api") {
  177. expect(body?.thinking).not.toHaveProperty("budget_tokens");
  178. expect(body?.max_tokens).toBe(4000);
  179. }
  180. }
  181. } finally {
  182. vi.unstubAllGlobals();
  183. }
  184. });
  185. });