zcode-builtin-model-aliases.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { fileURLToPath } from "node:url";
  2. import { describe, expect, it } from "vitest";
  3. import { NodeZCodeBuiltinProviderConfigSource } from "../src/index.js";
  4. import { compileModelOptionMaps } from "@zcode/model-option-map";
  5. describe("发布模型规则的前后缀与覆盖顺序", () => {
  6. it("OpenRouter Messages 的 GPT 选择实际生成对应 effort,保留完整请求型号", async () => {
  7. const source = new NodeZCodeBuiltinProviderConfigSource({
  8. bundledFilePath: fileURLToPath(
  9. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  10. ),
  11. watch: false,
  12. });
  13. try {
  14. const snapshot = await source.read();
  15. for (const modelId of ["openai/gpt-5.6-sol", "openai/gpt-5.3-codex"]) {
  16. const config = snapshot.models.resolve({
  17. providerId: "router-instance",
  18. templateId: "openrouter",
  19. modelId,
  20. apiType: "anthropic-messages",
  21. baseUrl: "https://openrouter.ai/api",
  22. });
  23. const maps = compileModelOptionMaps({
  24. reasoningLevel: { map: config.optionSpecs!.reasoningLevel!.map! },
  25. maxOutputTokens: { map: config.optionSpecs!.maxOutputTokens!.map! },
  26. });
  27. for (const reasoningLevel of config.optionSpecs!.reasoningLevel!.values!) {
  28. const request = maps.apply(
  29. { model: modelId, messages: [] },
  30. { reasoningLevel, maxOutputTokens: 1000 },
  31. );
  32. expect(request).toEqual({
  33. model: modelId,
  34. messages: [],
  35. max_tokens: 1000,
  36. output_config: { effort: reasoningLevel },
  37. });
  38. }
  39. }
  40. } finally {
  41. source.dispose();
  42. }
  43. });
  44. it.each(["anthropic-messages", "openai-chat-completions", "openai-responses"] as const)(
  45. "%s:等价前后缀复用能力与 Map,不依赖聚合站点白名单",
  46. async (apiType) => {
  47. const source = new NodeZCodeBuiltinProviderConfigSource({
  48. bundledFilePath: fileURLToPath(
  49. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  50. ),
  51. watch: false,
  52. });
  53. try {
  54. const snapshot = await source.read();
  55. const modelIds = [
  56. "GLM-5.3",
  57. "GLM-5.3-Flash",
  58. "GLM-5.2",
  59. "GLM-4.6v",
  60. "GLM-4.7-FlashX",
  61. "gpt-5.6-sol",
  62. "claude-haiku-4-5-20251001",
  63. "claude-opus-5",
  64. "MiniMax-M2.7-highspeed",
  65. "MiniMax-M3",
  66. "kimi-k3",
  67. "k3-256k",
  68. "mimo-v2.5-pro",
  69. "qwen3.8-flash",
  70. "deepseek-v4-pro",
  71. "grok-4.6",
  72. ];
  73. const get = (modelId: string) =>
  74. snapshot.models
  75. .resolve({
  76. providerId: "custom-alias-check",
  77. modelId,
  78. apiType,
  79. baseUrl: "https://example.test/v1",
  80. })
  81. .toJSON();
  82. for (const modelId of modelIds) {
  83. const expected = get(modelId);
  84. for (const alias of [
  85. `vendor/${modelId}`,
  86. `gateway/vendor/${modelId}`,
  87. `${modelId}-20250901`,
  88. `gateway/${modelId}-20250901`,
  89. `gateway/${modelId}:free`,
  90. ]) {
  91. expect(get(alias), alias).toEqual(expected);
  92. }
  93. }
  94. // 先族基线、后具体变体:不能把匹配范围扩大后让族规则重新盖掉具体配置。
  95. expect(get("gateway/GLM-5.3-Flash-20250901").properties?.inputFormat?.supportsImage).toBe(
  96. true,
  97. );
  98. expect(get("gateway/GLM-5.3-20250901").properties?.inputFormat?.supportsImage).toBe(false);
  99. expect(get("gateway/k3-256k-20250901").properties?.contextWindow).toBe(262_144);
  100. expect(get("gateway/mimo-v2.5-pro-20250901").properties?.inputFormat?.supportsVideo).toBe(
  101. false,
  102. );
  103. for (const modelId of [
  104. "GLM-5-Turbo",
  105. "GLM-4.7-Flash",
  106. "GLM-4.7-FlashX",
  107. "MiniMax-M2.5-highspeed",
  108. "GLM-4.1V-Thinking-Flash",
  109. ]) {
  110. expect(get(`vendor/${modelId}:free`), modelId).toEqual(get(modelId));
  111. }
  112. } finally {
  113. source.dispose();
  114. }
  115. },
  116. );
  117. it("聚合模板的每家维护厂商有默认启用代表,所有成员能形成完整配置和请求 Map", async () => {
  118. const source = new NodeZCodeBuiltinProviderConfigSource({
  119. bundledFilePath: fileURLToPath(
  120. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  121. ),
  122. watch: false,
  123. });
  124. try {
  125. const snapshot = await source.read();
  126. const enabledVendors = new Set<string>();
  127. for (const templateId of [
  128. "openrouter",
  129. "opencode-zen-responses",
  130. "opencode-zen-messages",
  131. "opencode-zen-chat",
  132. ]) {
  133. const template = snapshot.providerTemplates!.get(templateId)!;
  134. const api = template.config.api!;
  135. for (const modelId of template.config.builtinModelIds!) {
  136. const config = snapshot.models.resolve({
  137. providerId: "aggregate-instance",
  138. templateId,
  139. modelId,
  140. apiType: api.type!,
  141. baseUrl: api.baseUrl!,
  142. });
  143. expect(config.validateComplete(), `${templateId}/${modelId}`).toEqual([]);
  144. if (templateId === "openrouter" && config.enabled)
  145. enabledVendors.add(modelId.split("/")[0]!);
  146. const maps = compileModelOptionMaps({
  147. reasoningLevel: { map: config.optionSpecs!.reasoningLevel!.map! },
  148. maxOutputTokens: { map: config.optionSpecs!.maxOutputTokens!.map! },
  149. });
  150. for (const reasoningLevel of config.optionSpecs!.reasoningLevel!.values!) {
  151. const request = maps.apply(
  152. { model: modelId, messages: [] },
  153. { reasoningLevel, maxOutputTokens: 1000 },
  154. );
  155. expect(request.model).toBe(modelId);
  156. expect(
  157. request[
  158. api.type === "openai-responses"
  159. ? "max_output_tokens"
  160. : api.type === "openai-chat-completions"
  161. ? "max_completion_tokens"
  162. : "max_tokens"
  163. ],
  164. ).toBe(1000);
  165. }
  166. }
  167. }
  168. expect([...enabledVendors].sort()).toEqual(
  169. [
  170. "anthropic",
  171. "deepseek",
  172. "minimax",
  173. "moonshotai",
  174. "openai",
  175. "qwen",
  176. "x-ai",
  177. "xiaomi",
  178. "z-ai",
  179. ].sort(),
  180. );
  181. } finally {
  182. source.dispose();
  183. }
  184. });
  185. });