openrouter-reasoning-contract.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { readFile } from "node:fs/promises";
  2. import { fileURLToPath } from "node:url";
  3. import { afterAll, beforeAll, describe, expect, it } from "vitest";
  4. import { compileModelOptionMaps } from "@zcode/model-option-map";
  5. import { ModelConfig, ModelOptionSpecsConfig } from "@zcode/provider";
  6. import { NodeZCodeBuiltinProviderConfigSource } from "../src/index.js";
  7. const path = fileURLToPath(new URL("../../../config/provider/zcode-builtin.json", import.meta.url));
  8. const source = new NodeZCodeBuiltinProviderConfigSource({ bundledFilePath: path, watch: false });
  9. let snapshot: Awaited<ReturnType<typeof source.read>>;
  10. let modelIds: string[];
  11. beforeAll(async () => {
  12. snapshot = await source.read();
  13. const file = JSON.parse(await readFile(path, "utf8"));
  14. modelIds = file.config.providerConfigRules.templateRules.find(
  15. (rule: { templateId: string }) => rule.templateId === "openrouter",
  16. ).config.builtinModelIds;
  17. });
  18. afterAll(() => source.dispose());
  19. function resolve(
  20. modelId: string,
  21. baseUrl = "https://openrouter.ai/api",
  22. apiType = "anthropic-messages",
  23. ) {
  24. return snapshot.models.resolve({ providerId: "custom-router", modelId, apiType, baseUrl });
  25. }
  26. function body(config: ModelConfig, reasoningLevel: string, maxOutputTokens = 5000) {
  27. return compileModelOptionMaps({
  28. reasoningLevel: { map: config.optionSpecs!.reasoningLevel!.map! },
  29. maxOutputTokens: { map: config.optionSpecs!.maxOutputTokens!.map! },
  30. }).apply({ model: "unchanged", messages: [] }, { reasoningLevel, maxOutputTokens });
  31. }
  32. describe("OpenRouter Messages 推理请求契约", () => {
  33. it.each(["zcode-builtin.json", "zcode-builtin.test.json"])(
  34. "%s 模板仅简化名称,不改变请求协议",
  35. async (filename) => {
  36. const release = JSON.parse(
  37. await readFile(new URL(`../../../config/provider/${filename}`, import.meta.url), "utf8"),
  38. );
  39. const template = release.config.providerConfigRules.templateRules.find(
  40. (item: { templateId: string }) => item.templateId === "openrouter",
  41. );
  42. expect(template.templateNameMap).toEqual({ "zh-CN": "OpenRouter", "en-US": "OpenRouter" });
  43. expect(template.config.api).toEqual({
  44. type: "anthropic-messages",
  45. baseUrl: "https://openrouter.ai/api",
  46. });
  47. },
  48. );
  49. it("所有模板成员的所有档位不再生成缺预算的 enabled", () => {
  50. expect(modelIds.length).toBeGreaterThan(50);
  51. for (const id of modelIds) {
  52. const config = resolve(id);
  53. for (const level of config.optionSpecs!.reasoningLevel!.values!) {
  54. const request = body(config, level);
  55. const thinking = request.thinking as { type?: string; budget_tokens?: number } | undefined;
  56. if (thinking?.type === "enabled") {
  57. expect(Number.isInteger(thinking.budget_tokens), `${id}/${level}`).toBe(true);
  58. expect(thinking.budget_tokens!, `${id}/${level}`).toBeGreaterThan(0);
  59. }
  60. if (level === "disabled" || level === "none") {
  61. expect(thinking?.type, `${id}/${level}`).not.toBe("adaptive");
  62. expect(thinking?.type, `${id}/${level}`).not.toBe("enabled");
  63. }
  64. }
  65. }
  66. });
  67. it.each([
  68. ["z-ai/glm-5.3", "low"],
  69. ["z-ai/glm-5.3-flash", "max"],
  70. ["z-ai/glm-5.2", "high"],
  71. ["z-ai/glm-4.7", "enabled"],
  72. ["deepseek/deepseek-v4-pro", "max"],
  73. ["deepseek/deepseek-v4.1-flash", "low"],
  74. ["moonshotai/kimi-k2.7-code", "enabled"],
  75. ["moonshotai/kimi-k2.5", "enabled"],
  76. ["qwen/qwen3.8-max-0902", "minimal"],
  77. ["qwen/qwen3.7-plus", "enabled"],
  78. ["xiaomi/mimo-v2.5-pro", "enabled"],
  79. ])("%s/%s 使用 adaptive,不引入与小输出上限冲突的固定预算", (id, level) => {
  80. for (const baseUrl of [
  81. "https://openrouter.ai/api",
  82. "https://openrouter.ai/api/",
  83. "https://openrouter.ai/api/v1",
  84. "https://openrouter.ai/api/v1/",
  85. ]) {
  86. expect(body(resolve(id, baseUrl), level, 1)).toEqual({
  87. model: "unchanged",
  88. messages: [],
  89. max_tokens: 1,
  90. thinking: { type: "adaptive" },
  91. output_config: { effort: level === "enabled" ? "high" : level },
  92. });
  93. }
  94. });
  95. it("关闭仍关闭,现有 Claude 和 GPT 映射不变", () => {
  96. expect(body(resolve("z-ai/glm-5.2"), "disabled")).toMatchObject({
  97. thinking: { type: "disabled" },
  98. });
  99. expect(body(resolve("anthropic/claude-fable-5.1"), "low")).toMatchObject({
  100. thinking: { type: "adaptive" },
  101. output_config: { effort: "low" },
  102. });
  103. expect(body(resolve("openai/gpt-6-astra"), "high")).not.toHaveProperty("thinking");
  104. });
  105. it("不改变官方站点、相似域名、其他协议及 Personal Map", () => {
  106. for (const url of [
  107. "https://api.z.ai/api/anthropic",
  108. "https://open.bigmodel.cn/api/anthropic",
  109. "https://example.test/api",
  110. "https://openrouter.ai.evil.test/api",
  111. ]) {
  112. expect(body(resolve("z-ai/glm-5.3", url), "low").thinking).toEqual({ type: "enabled" });
  113. }
  114. for (const api of ["openai-chat-completions", "openai-responses"]) {
  115. expect(resolve("z-ai/glm-5.3", "https://openrouter.ai/api", api).optionSpecs).toEqual(
  116. resolve("z-ai/glm-5.3", "https://example.test/api", api).optionSpecs,
  117. );
  118. }
  119. const personal = resolve("z-ai/glm-5.3").overlay(
  120. new ModelConfig({
  121. optionSpecs: new ModelOptionSpecsConfig({
  122. reasoningLevel: { map: '{"personal_effort": reasoningLevel}' },
  123. }),
  124. }),
  125. );
  126. expect(body(personal, "high")).toEqual({
  127. model: "unchanged",
  128. messages: [],
  129. max_tokens: 5000,
  130. personal_effort: "high",
  131. });
  132. });
  133. });