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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. import { parseModelConfig } from "@zcode/provider";
  6. describe("发布模型规则的前后缀与覆盖顺序", () => {
  7. it.each(["zcode-builtin.json", "zcode-builtin.test.json"])(
  8. "%s:默认容量由通用规则提供,专用型号与 Personal 保留覆盖权",
  9. async (filename) => {
  10. const source = new NodeZCodeBuiltinProviderConfigSource({
  11. bundledFilePath: fileURLToPath(
  12. new URL(`../../../config/provider/${filename}`, import.meta.url),
  13. ),
  14. watch: false,
  15. });
  16. try {
  17. const snapshot = await source.read();
  18. for (const apiType of [
  19. "anthropic-messages",
  20. "openai-chat-completions",
  21. "openai-responses",
  22. ]) {
  23. const get = (modelId: string) =>
  24. snapshot.models.resolve({
  25. providerId: "custom-baseline",
  26. modelId,
  27. apiType,
  28. baseUrl: "https://example.test/v1",
  29. });
  30. const fallback = get("future-unknown-model");
  31. expect(fallback.properties?.contextWindow).toBe(1_000_000);
  32. expect(fallback.optionSpecs?.maxOutputTokens?.max).toBe(128_000);
  33. expect(get("claude-haiku-4-5").properties?.contextWindow).toBe(200_000);
  34. expect(get("qwen3.8-omni-flash").optionSpecs?.maxOutputTokens?.max).toBe(16_384);
  35. const personal = fallback.overlay(
  36. parseModelConfig({
  37. properties: { contextWindow: 64_000 },
  38. optionSpecs: { maxOutputTokens: { max: 8_000 } },
  39. }),
  40. );
  41. expect(personal.properties?.contextWindow).toBe(64_000);
  42. expect(personal.optionSpecs?.maxOutputTokens?.max).toBe(8_000);
  43. const request = compileModelOptionMaps(fallback.optionSpecs!.toJSON()).apply(
  44. {},
  45. {
  46. reasoningLevel: "enabled",
  47. maxOutputTokens: 1000,
  48. },
  49. );
  50. const field =
  51. apiType === "anthropic-messages"
  52. ? "max_tokens"
  53. : apiType === "openai-responses"
  54. ? "max_output_tokens"
  55. : "max_completion_tokens";
  56. expect(request[field]).toBe(1000);
  57. }
  58. expect(
  59. snapshot.models
  60. .rules()
  61. .filter((r) => r.type === "model")
  62. .map((r) => r.modelMatch),
  63. ).not.toContain(".*\\[1m\\]");
  64. expect(
  65. snapshot.models
  66. .rules()
  67. .filter((r) => r.type === "model")
  68. .map((r) => r.modelMatch),
  69. ).not.toContain(".*(?:ox-alpha|glm-x-preview-f|x-preview-f-free).*");
  70. } finally {
  71. source.dispose();
  72. }
  73. },
  74. );
  75. it("OpenRouter Messages 的 GPT 选择实际生成对应 effort,保留完整请求型号", async () => {
  76. const source = new NodeZCodeBuiltinProviderConfigSource({
  77. bundledFilePath: fileURLToPath(
  78. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  79. ),
  80. watch: false,
  81. });
  82. try {
  83. const snapshot = await source.read();
  84. for (const modelId of ["openai/gpt-5.6-sol", "openai/gpt-5.3-codex"]) {
  85. const config = snapshot.models.resolve({
  86. providerId: "router-instance",
  87. templateId: "openrouter",
  88. modelId,
  89. apiType: "anthropic-messages",
  90. baseUrl: "https://openrouter.ai/api",
  91. });
  92. const maps = compileModelOptionMaps({
  93. reasoningLevel: { map: config.optionSpecs!.reasoningLevel!.map! },
  94. maxOutputTokens: { map: config.optionSpecs!.maxOutputTokens!.map! },
  95. });
  96. for (const reasoningLevel of config.optionSpecs!.reasoningLevel!.values!) {
  97. const request = maps.apply(
  98. { model: modelId, messages: [] },
  99. { reasoningLevel, maxOutputTokens: 1000 },
  100. );
  101. expect(request).toEqual({
  102. model: modelId,
  103. messages: [],
  104. max_tokens: 1000,
  105. output_config: { effort: reasoningLevel },
  106. });
  107. }
  108. }
  109. } finally {
  110. source.dispose();
  111. }
  112. });
  113. it.each(["anthropic-messages", "openai-chat-completions", "openai-responses"] as const)(
  114. "%s:等价前后缀复用能力与 Map,不依赖聚合站点白名单",
  115. async (apiType) => {
  116. const source = new NodeZCodeBuiltinProviderConfigSource({
  117. bundledFilePath: fileURLToPath(
  118. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  119. ),
  120. watch: false,
  121. });
  122. try {
  123. const snapshot = await source.read();
  124. const modelIds = [
  125. "GLM-5.3",
  126. "GLM-5.3-Flash",
  127. "GLM-5.2",
  128. "GLM-4.6v",
  129. "GLM-4.7-FlashX",
  130. "gpt-5.6-sol",
  131. "claude-haiku-4-5-20251001",
  132. "claude-opus-5",
  133. "MiniMax-M2.7-highspeed",
  134. "MiniMax-M3",
  135. "kimi-k3",
  136. "k3-256k",
  137. "mimo-v2.5-pro",
  138. "qwen3.8-flash",
  139. "deepseek-v4-pro",
  140. "grok-4.6",
  141. ];
  142. const get = (modelId: string) =>
  143. snapshot.models
  144. .resolve({
  145. providerId: "custom-alias-check",
  146. modelId,
  147. apiType,
  148. baseUrl: "https://example.test/v1",
  149. })
  150. .toJSON();
  151. for (const modelId of modelIds) {
  152. const expected = get(modelId);
  153. for (const alias of [
  154. `vendor/${modelId}`,
  155. `gateway/vendor/${modelId}`,
  156. `${modelId}-20250901`,
  157. `gateway/${modelId}-20250901`,
  158. `gateway/${modelId}:free`,
  159. `gateway/${modelId}_custom`,
  160. ]) {
  161. expect(get(alias), alias).toEqual(expected);
  162. }
  163. }
  164. // 先族基线、后具体变体:不能把匹配范围扩大后让族规则重新盖掉具体配置。
  165. expect(get("gateway/GLM-5.3-Flash-20250901").properties?.inputFormat?.supportsImage).toBe(
  166. true,
  167. );
  168. expect(get("gateway/GLM-5.3-20250901").properties?.inputFormat?.supportsImage).toBe(false);
  169. expect(get("gateway/k3-256k-20250901").properties?.contextWindow).toBe(262_144);
  170. expect(get("gateway/mimo-v2.5-pro-20250901").properties?.inputFormat?.supportsVideo).toBe(
  171. false,
  172. );
  173. expect(get("gateway/gpt-5.4-mini_custom").properties?.contextWindow).toBe(400_000);
  174. expect(get("gateway/gpt-5.4_custom").properties?.contextWindow).toBe(1_050_000);
  175. if (apiType === "anthropic-messages") {
  176. // GLM-4.6 文本型号的专用映射不能覆盖 V 视觉型号原先继承的协议基线。
  177. const vision = get("GLM-4.6V");
  178. expect(
  179. compileModelOptionMaps(vision.optionSpecs!).apply(
  180. {},
  181. {
  182. reasoningLevel: "enabled",
  183. maxOutputTokens: 1000,
  184. },
  185. ).thinking,
  186. ).toEqual({ type: "adaptive" });
  187. }
  188. for (const modelId of [
  189. "GLM-5-Turbo",
  190. "GLM-4.7-Flash",
  191. "GLM-4.7-FlashX",
  192. "MiniMax-M2.5-highspeed",
  193. "GLM-4.1V-Thinking-Flash",
  194. ]) {
  195. expect(get(`vendor/${modelId}:free`), modelId).toEqual(get(modelId));
  196. }
  197. } finally {
  198. source.dispose();
  199. }
  200. },
  201. );
  202. it("聚合模板的每家维护厂商有默认启用代表,所有成员能形成完整配置和请求 Map", async () => {
  203. const source = new NodeZCodeBuiltinProviderConfigSource({
  204. bundledFilePath: fileURLToPath(
  205. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  206. ),
  207. watch: false,
  208. });
  209. try {
  210. const snapshot = await source.read();
  211. const enabledVendors = new Set<string>();
  212. for (const templateId of [
  213. "openrouter",
  214. "opencode-zen-responses",
  215. "opencode-zen-messages",
  216. "opencode-zen-chat",
  217. ]) {
  218. const template = snapshot.providerTemplates!.get(templateId)!;
  219. const api = template.config.api!;
  220. for (const modelId of template.config.builtinModelIds!) {
  221. const config = snapshot.models.resolve({
  222. providerId: "aggregate-instance",
  223. templateId,
  224. modelId,
  225. apiType: api.type!,
  226. baseUrl: api.baseUrl!,
  227. });
  228. expect(config.validateComplete(), `${templateId}/${modelId}`).toEqual([]);
  229. if (templateId === "openrouter" && config.enabled)
  230. enabledVendors.add(modelId.split("/")[0]!);
  231. const maps = compileModelOptionMaps({
  232. reasoningLevel: { map: config.optionSpecs!.reasoningLevel!.map! },
  233. maxOutputTokens: { map: config.optionSpecs!.maxOutputTokens!.map! },
  234. });
  235. for (const reasoningLevel of config.optionSpecs!.reasoningLevel!.values!) {
  236. const request = maps.apply(
  237. { model: modelId, messages: [] },
  238. { reasoningLevel, maxOutputTokens: 1000 },
  239. );
  240. expect(request.model).toBe(modelId);
  241. expect(
  242. request[
  243. api.type === "openai-responses"
  244. ? "max_output_tokens"
  245. : api.type === "openai-chat-completions"
  246. ? "max_completion_tokens"
  247. : "max_tokens"
  248. ],
  249. ).toBe(1000);
  250. }
  251. }
  252. }
  253. expect([...enabledVendors].sort()).toEqual(
  254. [
  255. "anthropic",
  256. "deepseek",
  257. "minimax",
  258. "moonshotai",
  259. "openai",
  260. "qwen",
  261. "x-ai",
  262. "xiaomi",
  263. "z-ai",
  264. ].sort(),
  265. );
  266. } finally {
  267. source.dispose();
  268. }
  269. });
  270. });