new-model-catalog-round2.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { readFile } from "node:fs/promises";
  2. import { beforeAll, describe, expect, it } from "vitest";
  3. import { compileModelOptionMaps } from "@zcode/model-option-map";
  4. import {
  5. ModelConfig,
  6. ModelOptionSpecsConfig,
  7. parseZCodeBuiltinModelConfigRules,
  8. } from "@zcode/provider";
  9. type Release = {
  10. config: {
  11. providerConfigRules: {
  12. templateRules: { templateId: string; config: { builtinModelIds: string[] } }[];
  13. };
  14. modelConfigRules: {
  15. modelRules: unknown[];
  16. modelApiRules: unknown[];
  17. providerSiteRules: unknown[];
  18. templateModelRules: { templateId: string; modelId: string; config: { enabled: boolean } }[];
  19. builtinProviderModelRules: unknown[];
  20. };
  21. };
  22. };
  23. let release: Release;
  24. let rules: ReturnType<typeof parseZCodeBuiltinModelConfigRules>;
  25. beforeAll(async () => {
  26. release = JSON.parse(
  27. await readFile(new URL("../../../config/provider/zcode-builtin.json", import.meta.url), "utf8"),
  28. );
  29. rules = parseZCodeBuiltinModelConfigRules(release.config.modelConfigRules);
  30. });
  31. const resolve = (
  32. modelId: string,
  33. apiType = "openai-chat-completions",
  34. baseUrl = "https://fixture.invalid/v1",
  35. templateId?: string,
  36. ) => rules.resolve({ providerId: "fixture", modelId, apiType, baseUrl, templateId });
  37. const patch = (modelId: string, apiType: string, baseUrl: string, reasoningLevel: string) => {
  38. const config = resolve(modelId, apiType, baseUrl);
  39. expect(config.optionSpecs?.reasoningLevel?.values).toContain(reasoningLevel);
  40. return compileModelOptionMaps({
  41. reasoningLevel: { map: config.optionSpecs!.reasoningLevel!.map! },
  42. maxOutputTokens: { map: config.optionSpecs!.maxOutputTokens!.map! },
  43. }).apply({}, { reasoningLevel, maxOutputTokens: 4096 });
  44. };
  45. describe("Todo113 第二轮:原厂、聚合、快照必须有独立正确配置", () => {
  46. it.each([
  47. ["gpt-5.4", 1050000, ["none", "low", "medium", "high", "xhigh"]],
  48. ["gpt-5.4-pro", 1050000, ["medium", "high", "xhigh"]],
  49. ["gpt-5.4-mini", 400000, ["none", "low", "medium", "high", "xhigh"]],
  50. ["gpt-5.4-nano", 400000, ["none", "low", "medium", "high", "xhigh"]],
  51. ])("%s 原厂、Zen、OR 前缀与快照一致", (id, context, levels) => {
  52. for (const [api, url, template, name] of [
  53. ["openai-responses", "https://api.openai.com/v1", "openai", id],
  54. ["openai-responses", "https://opencode.ai/zen/v1", "opencode-zen-responses", id],
  55. ["anthropic-messages", "https://openrouter.ai/api", "openrouter", `openai/${id}`],
  56. ]) {
  57. const config = resolve(String(name), String(api), String(url), String(template));
  58. expect(config.validateComplete()).toEqual([]);
  59. expect(config.properties?.contextWindow).toBe(context);
  60. expect(config.optionSpecs?.maxOutputTokens?.max).toBe(128000);
  61. expect(config.optionSpecs?.reasoningLevel?.values).toEqual(levels);
  62. expect(config.properties?.inputFormat.toJSON()).toMatchObject({
  63. supportsImage: true,
  64. supportsPdf: true,
  65. supportsVideo: false,
  66. supportsAudio: false,
  67. });
  68. expect(config.properties?.supportsJsonSchemaOutput).toBe(true);
  69. expect(config.enabled).toBe(false);
  70. }
  71. expect(resolve(`vendor/${id}-2026-03-05`).properties?.contextWindow).toBe(context);
  72. });
  73. it.each([
  74. ["qwen3.7-max", 131072, false, false, true],
  75. ["qwen3.7-max-2026-06-08", 131072, true, true, true],
  76. ["qwen3.7-plus", 131072, true, true, true],
  77. ["qwen3.7-flash", 131072, true, true, false],
  78. ["qwen3.6-plus", 65536, true, true, false],
  79. ["qwen3.6-flash", 65536, true, true, false],
  80. ] as const)("%s 不混淆默认别名、视觉快照、JSON 与 Schema", (id, max, image, video, schema) => {
  81. for (const name of [id, `vendor/${id}`]) {
  82. const config = resolve(name);
  83. expect(config.properties?.contextWindow).toBe(1000000);
  84. expect(config.optionSpecs?.maxOutputTokens?.max).toBe(max);
  85. expect(config.optionSpecs?.reasoningLevel?.values).toEqual(["disabled", "enabled"]);
  86. expect(config.properties?.inputFormat.toJSON()).toMatchObject({
  87. supportsImage: image,
  88. supportsVideo: video,
  89. supportsAudio: false,
  90. supportsPdf: false,
  91. });
  92. expect(config.properties?.supportsJsonSchemaOutput).toBe(schema);
  93. }
  94. });
  95. it("Omni 独立容量、模态与值域,不套普通 Flash", () => {
  96. const c = resolve("qwen3.8-omni-flash");
  97. expect(c.properties?.contextWindow).toBe(65536);
  98. expect(c.optionSpecs?.maxOutputTokens?.max).toBe(16384);
  99. expect(c.optionSpecs?.reasoningLevel?.values).toEqual([
  100. "none",
  101. "minimal",
  102. "low",
  103. "medium",
  104. "high",
  105. "xhigh",
  106. "max",
  107. ]);
  108. expect(c.properties?.inputFormat.toJSON()).toMatchObject({
  109. supportsImage: true,
  110. supportsVideo: true,
  111. supportsAudio: true,
  112. supportsPdf: false,
  113. });
  114. expect(c.properties?.supportsJsonSchemaOutput).toBe(false);
  115. expect(c.properties?.supportsNativeWebSearch).toBe(false);
  116. });
  117. it("高速 Code 不提供官方拒绝的关闭档,K2.6 不受影响", () => {
  118. for (const id of [
  119. "kimi-k2.7-code",
  120. "kimi-k2.7-code-highspeed",
  121. "vendor/kimi-k2.7-code-highspeed",
  122. ]) {
  123. const c = resolve(id);
  124. expect(c.optionSpecs?.reasoningLevel?.values).toEqual(["enabled"]);
  125. expect(c.properties?.contextWindow).toBe(262144);
  126. expect(c.optionSpecs?.maxOutputTokens?.max).toBe(98304);
  127. }
  128. expect(resolve("kimi-k2.6").optionSpecs?.reasoningLevel?.values).toEqual([
  129. "disabled",
  130. "enabled",
  131. ]);
  132. });
  133. it("网关能力与容量仅覆盖真实站点,不能污染原厂及相似恶意域名", () => {
  134. const router = (id: string) => resolve(id, "anthropic-messages", "https://openrouter.ai/api");
  135. expect(router("deepseek/deepseek-v4.1-flash").properties?.contextWindow).toBe(1048576);
  136. expect(router("deepseek/deepseek-v4.1-flash").properties?.supportsJsonSchemaOutput).toBe(true);
  137. expect(resolve("deepseek-v4.1-flash").properties?.contextWindow).toBe(1000000);
  138. expect(router("qwen/qwen3.7-flash").optionSpecs?.maxOutputTokens?.max).toBe(65536);
  139. expect(router("qwen/qwen3.7-plus").properties?.inputFormat.supportsVideo).toBe(false);
  140. expect(router("qwen/qwen3.8-max-0902").optionSpecs?.reasoningLevel?.values).toEqual([
  141. "minimal",
  142. "low",
  143. "medium",
  144. "high",
  145. "xhigh",
  146. ]);
  147. expect(resolve("qwen3.8-max-0902").optionSpecs?.reasoningLevel?.values).toEqual([
  148. "low",
  149. "medium",
  150. "xhigh",
  151. ]);
  152. for (const id of ["qwen3.6-plus", "qwen3.6-flash"]) {
  153. expect(router(`qwen/${id}`).properties?.supportsJsonSchemaOutput).toBe(true);
  154. expect(resolve(id).properties?.supportsJsonSchemaOutput).toBe(false);
  155. }
  156. expect(
  157. resolve(
  158. "qwen/qwen3.7-plus",
  159. "anthropic-messages",
  160. "https://openrouter.ai.attacker.invalid/api",
  161. ).properties?.inputFormat.supportsVideo,
  162. ).toBe(true);
  163. expect(resolve("deepseek-v4-flash").properties?.inputFormat.supportsImage).toBe(false);
  164. expect(resolve("deepseek-v4-pro").properties?.inputFormat.supportsImage).toBe(false);
  165. });
  166. it("专用协议 Map 不向官方接口注入其他厂商字段", () => {
  167. expect(
  168. patch("gpt-5.4", "openai-chat-completions", "https://api.openai.com/v1", "none"),
  169. ).toEqual({ reasoning_effort: "none", max_completion_tokens: 4096 });
  170. expect(patch("gpt-5.4-pro", "openai-responses", "https://api.openai.com/v1", "xhigh")).toEqual({
  171. reasoning: { effort: "xhigh" },
  172. max_output_tokens: 4096,
  173. });
  174. expect(
  175. patch("openai/gpt-5.4-mini", "anthropic-messages", "https://openrouter.ai/api", "none"),
  176. ).toEqual({ thinking: { type: "disabled" }, max_tokens: 4096 });
  177. expect(
  178. patch("openai/gpt-5.4-mini", "anthropic-messages", "https://openrouter.ai/api", "low"),
  179. ).toEqual({ output_config: { effort: "low" }, max_tokens: 4096 });
  180. expect(
  181. patch(
  182. "qwen3.7-plus",
  183. "openai-chat-completions",
  184. "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
  185. "disabled",
  186. ),
  187. ).toEqual({ enable_thinking: false, max_completion_tokens: 4096 });
  188. expect(
  189. patch(
  190. "qwen/qwen3.7-plus",
  191. "openai-chat-completions",
  192. "https://openrouter.ai/api/v1",
  193. "enabled",
  194. ),
  195. ).toEqual({ reasoning: { enabled: true }, max_completion_tokens: 4096 });
  196. expect(
  197. patch(
  198. "deepseek/deepseek-v4.1-flash",
  199. "openai-chat-completions",
  200. "https://openrouter.ai/api/v1",
  201. "disabled",
  202. ),
  203. ).toEqual({ reasoning: { enabled: false }, max_completion_tokens: 4096 });
  204. expect(
  205. patch(
  206. "qwen3.8-omni-flash",
  207. "openai-chat-completions",
  208. "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
  209. "max",
  210. ),
  211. ).toEqual({ reasoning_effort: "max", max_completion_tokens: 4096 });
  212. });
  213. it.each([
  214. "gpt-5.4-image-2",
  215. "gpt-5.40",
  216. "gpt-5.4-future",
  217. "qwen3.7-omni-flash",
  218. "qwen3.6-max-preview",
  219. "deepseek-v4.1-pro",
  220. ])("相邻型号 %s 不被新规则吞掉", (id) => {
  221. expect(resolve(id).properties?.contextWindow).toBe(200000);
  222. });
  223. it("个人参数仍可覆盖新 Built-in,不代写保存的选择", () => {
  224. const c = resolve("gpt-5.4").overlay(
  225. new ModelConfig({
  226. optionSpecs: new ModelOptionSpecsConfig({
  227. reasoningLevel: { values: ["personal"], map: '{"custom": true}' },
  228. }),
  229. }),
  230. );
  231. expect(c.optionSpecs?.reasoningLevel?.values).toEqual(["personal"]);
  232. expect(c.optionSpecs?.reasoningLevel?.map).toBe('{"custom": true}');
  233. });
  234. it("目录与精确默认成对维护,原厂退役不波及第三方和模型规则", () => {
  235. const ids = (id: string) =>
  236. release.config.providerConfigRules.templateRules.find((t) => t.templateId === id)!.config
  237. .builtinModelIds;
  238. for (const t of release.config.providerConfigRules.templateRules) {
  239. for (const id of t.config.builtinModelIds)
  240. expect(
  241. release.config.modelConfigRules.templateModelRules.filter(
  242. (r) => r.templateId === t.templateId && r.modelId === id,
  243. ),
  244. `${t.templateId}/${id}`,
  245. ).toHaveLength(1);
  246. }
  247. for (const t of ["openai", "openrouter"])
  248. for (const id of ["gpt-5.4", "gpt-5.4-pro", "gpt-5.4-mini", "gpt-5.4-nano"])
  249. expect(ids(t)).toContain(t === "openrouter" ? `openai/${id}` : id);
  250. for (const t of ["qwen-alibaba-model-studio-cn", "qwen-alibaba-model-studio-intl"])
  251. for (const id of [
  252. "qwen3.7-max",
  253. "qwen3.7-plus",
  254. "qwen3.7-flash",
  255. "qwen3.6-plus",
  256. "qwen3.6-flash",
  257. ])
  258. expect(ids(t)).toContain(id);
  259. expect(ids("qwen-alibaba-model-studio-intl")).toContain("qwen3.8-omni-flash");
  260. expect(ids("moonshot-kimi")).toContain("kimi-k2.7-code-highspeed");
  261. expect(ids("openrouter")).toContain("deepseek/deepseek-v4.1-flash");
  262. expect(ids("openrouter")).toContain("qwen/qwen3.8-max-0902");
  263. for (const [t, removed] of [
  264. ["deepseek", ["deepseek-v4-flash"]],
  265. ["xiaomi-mimo", ["mimo-v2-pro", "mimo-v2-omni", "mimo-v2-flash"]],
  266. [
  267. "moonshot-kimi",
  268. [
  269. "kimi-k2.5",
  270. "moonshot-v1-8k",
  271. "moonshot-v1-32k",
  272. "moonshot-v1-128k",
  273. "moonshot-v1-8k-vision-preview",
  274. "moonshot-v1-32k-vision-preview",
  275. "moonshot-v1-128k-vision-preview",
  276. ],
  277. ],
  278. ] as const) {
  279. for (const id of removed) {
  280. expect(ids(t)).not.toContain(id);
  281. expect(
  282. release.config.modelConfigRules.templateModelRules.filter(
  283. (r) => r.templateId === t && r.modelId === id,
  284. ),
  285. ).toEqual([]);
  286. }
  287. }
  288. expect(ids("deepseek")).toContain("deepseek-v4-pro");
  289. expect(ids("openrouter")).toContain("moonshotai/kimi-k2.5");
  290. expect(ids("openrouter")).toContain("deepseek/deepseek-v4-flash");
  291. expect(resolve("mimo-v2-flash").properties?.contextWindow).toBe(262144);
  292. });
  293. });