zcode-builtin-integrity.test.ts 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. import { readFile } from "node:fs/promises";
  2. import { compileModelOptionMaps } from "@zcode/model-option-map";
  3. import { fileURLToPath } from "node:url";
  4. import {
  5. ApiKeyAccessConfig,
  6. ModelConfigRules,
  7. ProviderApiConfig,
  8. ProviderConfig,
  9. ProviderConfigMap,
  10. ProviderConfigResolver,
  11. ProviderRegistry,
  12. ZhipuAccountAccessConfig,
  13. parseModelConfig,
  14. } from "@zcode/provider";
  15. import { describe, expect, it } from "vitest";
  16. import { NodeZCodeBuiltinProviderConfigSource } from "../src/index.js";
  17. // 发布验证必须指向最终上传的那份文件,而不是默认悄悄检查仓库源文件。
  18. const zcodeBuiltinProviderConfigFilePath =
  19. process.env.ZCODE_BUILTIN_RELEASE_TEST_FILE?.trim() ||
  20. fileURLToPath(new URL("../../../config/provider/zcode-builtin.json", import.meta.url));
  21. function reasoningPatch(map: string, reasoningLevel: string) {
  22. return compileModelOptionMaps({ reasoningLevel: { map }, maxOutputTokens: { map: "{}" } }).apply(
  23. {},
  24. { reasoningLevel, maxOutputTokens: 32000 },
  25. );
  26. }
  27. describe("ZCode Built-in Provider Config 完整性", () => {
  28. it.each(["anthropic-messages", "openai-chat-completions", "openai-responses"] as const)(
  29. "未知模型在 %s 下拥有完整两档兜底并可进入 Registry",
  30. async (apiType) => {
  31. const source = new NodeZCodeBuiltinProviderConfigSource({
  32. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  33. watch: false,
  34. });
  35. try {
  36. const snapshot = await source.read();
  37. const model = snapshot.models.resolve({
  38. providerId: "unknown-provider",
  39. modelId: "future-unknown-model",
  40. apiType,
  41. baseUrl: "https://unknown.example/v1",
  42. });
  43. expect(model.validateComplete()).toEqual([]);
  44. expect(model.optionSpecs?.maxOutputTokens?.max).toBe(128_000);
  45. expect(model.optionSpecs?.reasoningLevel?.values).toEqual(["disabled", "enabled"]);
  46. expect(model.properties?.inputFormat.supportsImage).toBe(false);
  47. const result = new ProviderConfigResolver().resolve({
  48. zcodeBuiltinProviders: snapshot.providers,
  49. zcodeBuiltinProviderTemplates: snapshot.providerTemplates,
  50. accountProviders: ProviderConfigMap.empty(),
  51. zcodeBuiltinModelRules: snapshot.models,
  52. personalModels: ModelConfigRules.empty(),
  53. personalProviders: new ProviderConfigMap([
  54. [
  55. "unknown-provider",
  56. new ProviderConfig({
  57. group: "standard-personal",
  58. api: new ProviderApiConfig({
  59. type: apiType,
  60. baseUrl: "https://unknown.example/v1",
  61. }),
  62. access: new ApiKeyAccessConfig({ apiKey: "test-only" }),
  63. personalModelIds: ["future-unknown-model"],
  64. }),
  65. ],
  66. ]),
  67. });
  68. expect(
  69. result.registryProviders
  70. .find((entry) => entry.providerId === "unknown-provider")
  71. ?.models.map((entry) => entry.modelId),
  72. ).toContain("future-unknown-model");
  73. } finally {
  74. source.dispose();
  75. }
  76. },
  77. );
  78. it("GLM-5.3-Flash 的媒体输入能力及 GLM-5.3 的套餐桥接保持明确", async () => {
  79. const source = new NodeZCodeBuiltinProviderConfigSource({
  80. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  81. watch: false,
  82. });
  83. try {
  84. const snapshot = await source.read();
  85. const rawRelease = JSON.parse(await readFile(zcodeBuiltinProviderConfigFilePath, "utf8")) as {
  86. revision: number;
  87. config: { modelConfigRules: Record<string, unknown> };
  88. };
  89. expect(rawRelease.config.modelConfigRules).toMatchObject({
  90. modelRules: expect.any(Array),
  91. modelApiRules: expect.any(Array),
  92. providerSiteRules: expect.any(Array),
  93. templateModelRules: expect.any(Array),
  94. builtinProviderModelRules: expect.any(Array),
  95. });
  96. expect(rawRelease.config.modelConfigRules).not.toHaveProperty("matchRules");
  97. expect(snapshot.revision).toMatch(
  98. new RegExp(`^zcode-builtin:${rawRelease.revision}:[a-f0-9]{64}$`, "u"),
  99. );
  100. const apiTemplateIds = [
  101. "zai-api",
  102. "bigmodel-api",
  103. "zai-standard-api",
  104. "bigmodel-standard-api",
  105. ] as const;
  106. const accountProviderIds = [
  107. "account:zai-individual-coding-plan",
  108. "account:zai-team-coding-plan",
  109. "account:zai-start-plan",
  110. "account:bigmodel-individual-coding-plan",
  111. "account:bigmodel-team-coding-plan",
  112. "account:bigmodel-start-plan",
  113. "account:zai-offpeak-idle-plan",
  114. "account:bigmodel-offpeak-idle-plan",
  115. ] as const;
  116. for (const templateId of apiTemplateIds) {
  117. const modelIds = snapshot.providerTemplates?.get(templateId)?.config.builtinModelIds ?? [];
  118. expect(modelIds, `${templateId} 必须同时提供 GLM-5.3 系列`).toEqual(
  119. expect.arrayContaining(["GLM-5.3", "GLM-5.3-Flash"]),
  120. );
  121. }
  122. for (const providerId of accountProviderIds) {
  123. const modelIds = snapshot.providers.get(providerId)?.builtinModelIds ?? [];
  124. expect(modelIds, `${providerId} 必须提供 GLM-5.3-Flash`).toContain("GLM-5.3-Flash");
  125. }
  126. for (const providerId of [
  127. "account:zai-individual-coding-plan",
  128. "account:zai-team-coding-plan",
  129. "account:bigmodel-individual-coding-plan",
  130. "account:bigmodel-team-coding-plan",
  131. ]) {
  132. expect(snapshot.providers.get(providerId)?.builtinModelIds).toEqual([
  133. "GLM-5.3",
  134. "GLM-5.3-Flash",
  135. ]);
  136. const planModel = snapshot.models.resolve({
  137. providerId,
  138. modelId: "GLM-5.3",
  139. apiType: "anthropic-messages",
  140. baseUrl: snapshot.providers.get(providerId)?.api?.baseUrl,
  141. });
  142. expect(planModel.properties?.inputFormat.toJSON()).toMatchObject({
  143. supportsImage: true,
  144. supportsVideo: true,
  145. supportsAudio: false,
  146. supportsPdf: false,
  147. });
  148. }
  149. // 加速卡换端点不换模型,能力事实必须与同 Family Coding Plan 站点逐项一致;
  150. // 否则同一会话里普通轮与加速轮的 system 折叠、WebSearch 暴露和附件合法性会互相打脸。
  151. for (const [highspeedProviderId, planProviderId] of [
  152. ["account:zai-highspeed-card", "account:zai-individual-coding-plan"],
  153. ["account:bigmodel-highspeed-card", "account:bigmodel-individual-coding-plan"],
  154. ] as const) {
  155. const resolveAt = (providerId: string, modelId: string) =>
  156. snapshot.models.resolve({
  157. providerId,
  158. modelId,
  159. apiType: "anthropic-messages",
  160. baseUrl: snapshot.providers.get(providerId)?.api?.baseUrl,
  161. });
  162. expect(snapshot.providers.get(highspeedProviderId)?.builtinModelIds).toEqual(
  163. snapshot.providers.get(planProviderId)?.builtinModelIds,
  164. );
  165. for (const modelId of snapshot.providers.get(highspeedProviderId)?.builtinModelIds ?? []) {
  166. const highspeedModel = resolveAt(highspeedProviderId, modelId);
  167. const planModel = resolveAt(planProviderId, modelId);
  168. expect(
  169. highspeedModel.properties?.inputFormat.toJSON(),
  170. `${highspeedProviderId}/${modelId} 输入形态偏离 Coding Plan 站点`,
  171. ).toEqual(planModel.properties?.inputFormat.toJSON());
  172. expect(
  173. highspeedModel.properties?.supportsMidConversationSystem,
  174. `${highspeedProviderId}/${modelId} MCS 偏离 Coding Plan 站点`,
  175. ).toBe(planModel.properties?.supportsMidConversationSystem);
  176. expect(
  177. highspeedModel.properties?.supportsNativeWebSearch,
  178. `${highspeedProviderId}/${modelId} 原生搜索偏离 Coding Plan 站点`,
  179. ).toBe(planModel.properties?.supportsNativeWebSearch);
  180. }
  181. }
  182. const resolve = (modelId: string) =>
  183. snapshot.models.resolve({
  184. providerId: "personal-zai",
  185. templateId: "zai-api",
  186. modelId,
  187. apiType: "anthropic-messages",
  188. // 模型层自身能力与 §6.3.1 已批准的站点覆盖分开验证。
  189. baseUrl: "https://other.example.com/anthropic",
  190. });
  191. const glm53 = resolve("glm-5.3").toJSON();
  192. const glm53Flash = resolve("glm-5.3-flash").toJSON();
  193. expect(glm53.optionSpecs?.reasoningLevel).toMatchObject({
  194. values: ["low", "high", "max"],
  195. });
  196. expect(glm53.optionSpecs?.reasoningLevel?.map).toContain("reasoningLevel");
  197. expect(reasoningPatch(glm53.optionSpecs!.reasoningLevel!.map!, "low")).toMatchObject({
  198. output_config: { effort: "low" },
  199. });
  200. expect(glm53.properties?.inputFormat).toMatchObject({
  201. supportsImage: false,
  202. supportsVideo: false,
  203. });
  204. expect(glm53Flash).toEqual({
  205. ...glm53,
  206. properties: {
  207. ...glm53.properties,
  208. inputFormat: {
  209. ...glm53.properties?.inputFormat,
  210. supportsImage: true,
  211. supportsVideo: true,
  212. supportsPdf: true,
  213. },
  214. },
  215. });
  216. const glm53FlashChat = snapshot.models.resolve({
  217. providerId: "personal-zai",
  218. templateId: "zai-api",
  219. modelId: "glm-5.3-flash",
  220. apiType: "openai-chat-completions",
  221. baseUrl: "https://api.z.ai/api/anthropic",
  222. });
  223. expect(glm53FlashChat.properties?.inputFormat.supportsPdf).toBe(true);
  224. } finally {
  225. source.dispose();
  226. }
  227. });
  228. it("按当前官方证据声明各厂商主推模型和关键静态属性", async () => {
  229. const source = new NodeZCodeBuiltinProviderConfigSource({
  230. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  231. watch: false,
  232. });
  233. try {
  234. const snapshot = await source.read();
  235. expect(
  236. snapshot.providerTemplates?.get("moonshot-kimi")?.config.builtinModelIds.slice(0, 3),
  237. ).toEqual(["kimi-k3", "kimi-k2.7-code", "kimi-k2.6"]);
  238. expect(
  239. snapshot.providerTemplates?.get("minimax")?.config.builtinModelIds.slice(0, 3),
  240. ).toEqual(["MiniMax-M3", "MiniMax-M2.7", "MiniMax-M2.7-highspeed"]);
  241. for (const providerId of ["qwen-alibaba-model-studio-cn", "qwen-alibaba-model-studio-intl"]) {
  242. expect(
  243. snapshot.providerTemplates?.get(providerId)?.config.builtinModelIds.slice(0, 2),
  244. ).toEqual(["qwen3.8-max", "qwen3.8-flash"]);
  245. }
  246. expect(snapshot.providerTemplates?.get("openai")?.config.builtinModelIds.slice(0, 4)).toEqual(
  247. ["gpt-6-astra", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna"],
  248. );
  249. const resolve = (
  250. providerId: string,
  251. modelId: string,
  252. apiType: "anthropic-messages" | "openai-chat-completions" | "openai-responses",
  253. baseUrl: string,
  254. ) => snapshot.models.resolve({ providerId, modelId, apiType, baseUrl });
  255. const qwen38 = resolve(
  256. "qwen-alibaba-model-studio-intl",
  257. "qwen3.8-flash",
  258. "openai-chat-completions",
  259. "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
  260. );
  261. expect(qwen38.properties?.toJSON()).toMatchObject({
  262. contextWindow: 1_000_000,
  263. inputFormat: {
  264. supportsText: true,
  265. supportsImage: true,
  266. supportsVideo: true,
  267. supportsAudio: false,
  268. supportsPdf: false,
  269. },
  270. supportsToolCall: true,
  271. supportsJsonSchemaOutput: true,
  272. });
  273. expect(qwen38.optionSpecs?.maxOutputTokens).toMatchObject({
  274. max: 131_072,
  275. });
  276. const mimo = resolve(
  277. "xiaomi-mimo",
  278. "mimo-v2.5",
  279. "anthropic-messages",
  280. "https://api.xiaomimimo.com/anthropic",
  281. );
  282. expect(mimo.properties?.inputFormat.toJSON()).toMatchObject({
  283. supportsImage: true,
  284. supportsVideo: true,
  285. supportsAudio: true,
  286. });
  287. const kimiCode = resolve(
  288. "moonshot-kimi",
  289. "kimi-k2.7-code",
  290. "anthropic-messages",
  291. "https://api.moonshot.cn/anthropic",
  292. );
  293. expect(kimiCode.properties?.contextWindow).toBe(262_144);
  294. expect(kimiCode.properties?.inputFormat.toJSON()).toMatchObject({
  295. supportsText: true,
  296. supportsImage: true,
  297. supportsVideo: true,
  298. });
  299. const kimiK3 = resolve(
  300. "moonshot-kimi",
  301. "kimi-k3",
  302. "anthropic-messages",
  303. "https://api.moonshot.cn/anthropic",
  304. );
  305. expect(kimiK3.properties?.toJSON()).toMatchObject({
  306. contextWindow: 1_048_576,
  307. inputFormat: {
  308. supportsText: true,
  309. supportsImage: true,
  310. supportsVideo: true,
  311. },
  312. });
  313. const kimiK3Compact = resolve(
  314. "moonshot-kimi",
  315. "k3-256k",
  316. "anthropic-messages",
  317. "https://api.kimi.com/coding/",
  318. );
  319. expect(kimiK3Compact.properties?.toJSON()).toMatchObject({
  320. contextWindow: 262_144,
  321. inputFormat: {
  322. supportsText: true,
  323. supportsImage: true,
  324. supportsVideo: false,
  325. },
  326. });
  327. const minimax = resolve(
  328. "minimax",
  329. "MiniMax-M2.7",
  330. "anthropic-messages",
  331. "https://api.minimaxi.com/anthropic",
  332. );
  333. expect(minimax.properties?.toJSON()).toMatchObject({
  334. contextWindow: 204_800,
  335. inputFormat: {
  336. supportsText: true,
  337. supportsImage: false,
  338. supportsVideo: false,
  339. },
  340. });
  341. const minimaxM3 = resolve(
  342. "minimax",
  343. "MiniMax-M3",
  344. "anthropic-messages",
  345. "https://api.minimaxi.com/anthropic",
  346. );
  347. expect(minimaxM3.properties?.toJSON()).toMatchObject({
  348. contextWindow: 1_000_000,
  349. inputFormat: {
  350. supportsText: true,
  351. supportsImage: true,
  352. supportsVideo: true,
  353. },
  354. });
  355. } finally {
  356. source.dispose();
  357. }
  358. });
  359. it("聚合供应商按维护清单提供独立模板,不复用上游 Provider 配置", async () => {
  360. const source = new NodeZCodeBuiltinProviderConfigSource({
  361. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  362. watch: false,
  363. });
  364. try {
  365. const snapshot = await source.read();
  366. const openrouter = snapshot.providerTemplates?.get("openrouter")?.config;
  367. expect(openrouter?.api?.type).toBe("anthropic-messages");
  368. expect(openrouter?.api?.baseUrl).toBe("https://openrouter.ai/api");
  369. expect(openrouter?.builtinModelIds).toEqual(
  370. expect.arrayContaining(["openai/gpt-5.6-sol", "anthropic/claude-opus-5", "z-ai/glm-5.3"]),
  371. );
  372. for (const templateId of [
  373. "opencode-zen-responses",
  374. "opencode-zen-messages",
  375. "opencode-zen-chat",
  376. ]) {
  377. expect(snapshot.providerTemplates?.has(templateId)).toBe(true);
  378. }
  379. } finally {
  380. source.dispose();
  381. }
  382. });
  383. it("聚合供应商同型号命中模型规则和 API Map,保留完整 ID 且每家至少启用一个代表型号", async () => {
  384. const source = new NodeZCodeBuiltinProviderConfigSource({
  385. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  386. watch: false,
  387. });
  388. try {
  389. const snapshot = await source.read();
  390. const pairs = [
  391. ["openai/gpt-5.6-sol", "gpt-5.6-sol"],
  392. ["anthropic/claude-opus-5", "claude-opus-5"],
  393. ["anthropic/claude-haiku-4.5", "claude-haiku-4-5-20251001"],
  394. ["deepseek/deepseek-v4-pro", "deepseek-v4-pro"],
  395. ["moonshotai/kimi-k3", "kimi-k3"],
  396. ["z-ai/glm-5.3", "glm-5.3"],
  397. ["qwen/qwen3.8-max", "qwen3.8-max"],
  398. ["minimax/minimax-m3", "MiniMax-M3"],
  399. ["xiaomi/mimo-v2.5-pro", "mimo-v2.5-pro"],
  400. ["x-ai/grok-4.6", "grok-4.6"],
  401. ];
  402. for (const [aggregateId, directId] of pairs) {
  403. const resolve = (modelId: string, templateId?: string) =>
  404. snapshot.models.resolve({
  405. providerId: "aggregate-instance",
  406. templateId,
  407. modelId,
  408. apiType: "anthropic-messages",
  409. baseUrl: "https://openrouter.ai/api",
  410. });
  411. const aggregate = resolve(aggregateId!, "openrouter");
  412. const direct = resolve(directId!);
  413. expect(aggregate.properties?.toJSON(), aggregateId).toEqual(direct.properties?.toJSON());
  414. expect(aggregate.optionSpecs?.toJSON(), aggregateId).toEqual(direct.optionSpecs?.toJSON());
  415. }
  416. for (const templateId of ["openrouter", "opencode-zen-messages", "opencode-zen-chat"]) {
  417. const template = snapshot.providerTemplates!.get(templateId)!.config;
  418. const enabledByVendor = new Map<string, boolean>();
  419. for (const modelId of template.builtinModelIds!) {
  420. const vendor =
  421. templateId === "openrouter" ? modelId.split("/")[0]! : modelId.split("-")[0]!;
  422. const model = snapshot.models.resolve({
  423. providerId: "aggregate-instance",
  424. templateId,
  425. modelId,
  426. apiType: template.api!.type,
  427. baseUrl: template.api!.baseUrl,
  428. });
  429. enabledByVendor.set(
  430. vendor,
  431. (enabledByVendor.get(vendor) ?? false) || model.enabled === true,
  432. );
  433. }
  434. for (const [vendor, enabled] of enabledByVendor)
  435. expect(enabled, `${templateId}/${vendor}`).toBe(true);
  436. }
  437. const haiku = snapshot.models.resolve({
  438. providerId: "zen",
  439. templateId: "opencode-zen-messages",
  440. modelId: "claude-haiku-4-5",
  441. apiType: "anthropic-messages",
  442. baseUrl: "https://opencode.ai/zen/v1",
  443. });
  444. expect(haiku.properties?.inputFormat.supportsImage).toBe(true);
  445. expect(haiku.properties?.contextWindow).toBe(200_000);
  446. } finally {
  447. source.dispose();
  448. }
  449. });
  450. it("每个 Provider 的默认 enabled 模型形成成员前缀,历史或 alias 型号默认关闭", async () => {
  451. const source = new NodeZCodeBuiltinProviderConfigSource({
  452. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  453. watch: false,
  454. });
  455. try {
  456. const snapshot = await source.read();
  457. const exactEnabled = new Map(
  458. snapshot.models
  459. .rules()
  460. .flatMap((rule) =>
  461. rule.config.enabled === undefined ||
  462. (rule.type !== "provider-model" && rule.type !== "template-model")
  463. ? []
  464. : [
  465. [
  466. `${rule.type === "provider-model" ? rule.providerId : rule.templateId}\0${rule.modelId}`,
  467. rule.config.enabled,
  468. ] as const,
  469. ],
  470. ),
  471. );
  472. const memberships = [
  473. ...snapshot.providers.entries(),
  474. ...(snapshot.providerTemplates
  475. ?.entries()
  476. .map(([id, template]) => [id, template.config] as const) ?? []),
  477. ];
  478. for (const [ownerId, owner] of memberships) {
  479. const states = (owner.builtinModelIds ?? []).map((modelId) => {
  480. const enabled = exactEnabled.get(`${ownerId}\0${modelId}`);
  481. expect(enabled, `${ownerId}/${modelId} 必须显式声明 Built-in 默认 enabled`).toBeTypeOf(
  482. "boolean",
  483. );
  484. return enabled!;
  485. });
  486. const firstDisabled = states.indexOf(false);
  487. if (firstDisabled >= 0) {
  488. expect(
  489. states.slice(firstDisabled).every((enabled) => !enabled),
  490. `${ownerId} 的 enabled 成员必须在 disabled 成员之前`,
  491. ).toBe(true);
  492. }
  493. }
  494. expect(exactEnabled.get("openai\0gpt-5.6")).toBe(false);
  495. expect(exactEnabled.get("openai\0gpt-5.6-sol")).toBe(true);
  496. expect(exactEnabled.get("minimax\0MiniMax-M3")).toBe(true);
  497. expect(exactEnabled.get("xai\0grok-4.3")).toBe(false);
  498. } finally {
  499. source.dispose();
  500. }
  501. });
  502. it("按产品分组声明 Zhipu 系统 Provider 与外部主流 Provider", async () => {
  503. const source = new NodeZCodeBuiltinProviderConfigSource({
  504. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  505. watch: false,
  506. });
  507. try {
  508. const snapshot = await source.read();
  509. expect(snapshot.providers.keys()).toEqual([
  510. "account:zai-individual-coding-plan",
  511. "account:zai-team-coding-plan",
  512. "account:zai-start-plan",
  513. "account:bigmodel-individual-coding-plan",
  514. "account:bigmodel-team-coding-plan",
  515. "account:bigmodel-start-plan",
  516. "account:zai-offpeak-idle-plan",
  517. "account:bigmodel-offpeak-idle-plan",
  518. "account:zai-highspeed-card",
  519. "account:bigmodel-highspeed-card",
  520. ]);
  521. expect(snapshot.providerTemplates?.keys()).toEqual([
  522. "zai-api",
  523. "zai-standard-api",
  524. "bigmodel-api",
  525. "bigmodel-standard-api",
  526. "moonshot-kimi",
  527. "minimax",
  528. "deepseek",
  529. "qwen-alibaba-model-studio-cn",
  530. "qwen-alibaba-model-studio-intl",
  531. "xiaomi-mimo",
  532. "xiaomi-mimo-token-plan",
  533. "openai",
  534. "anthropic",
  535. "xai",
  536. "openrouter",
  537. "opencode-go-chat",
  538. "opencode-go-messages",
  539. "opencode-go-responses",
  540. "opencode-zen-responses",
  541. "opencode-zen-messages",
  542. "opencode-zen-chat",
  543. ]);
  544. for (const [templateId, template] of snapshot.providerTemplates?.entries() ?? []) {
  545. expect(
  546. template.templateNameMap["zh-CN"]?.trim(),
  547. `${templateId} 缺少中文 Template 名称`,
  548. ).toBeTruthy();
  549. expect(
  550. template.templateNameMap["en-US"]?.trim(),
  551. `${templateId} 缺少英文 Template 名称`,
  552. ).toBeTruthy();
  553. expect(template.config.logo, `${templateId} 缺少 Built-in Logo 引用`).toMatchObject({
  554. type: "builtin",
  555. });
  556. }
  557. for (const [providerId, provider] of snapshot.providers.entries()) {
  558. expect(provider.logo, `${providerId} 缺少 Built-in Logo 引用`).toMatchObject({
  559. type: "builtin",
  560. });
  561. }
  562. expect(snapshot.providerTemplates?.get("moonshot-kimi")?.templateNameMap).toEqual({
  563. "zh-CN": "Kimi",
  564. "en-US": "Kimi",
  565. });
  566. expect(
  567. snapshot.providerTemplates?.get("qwen-alibaba-model-studio-cn")?.templateNameMap,
  568. ).toEqual({
  569. "zh-CN": "阿里云百炼(中国)",
  570. "en-US": "Alibaba Cloud (China)",
  571. });
  572. expect(
  573. snapshot.providerTemplates?.get("qwen-alibaba-model-studio-intl")?.templateNameMap,
  574. ).toEqual({
  575. "zh-CN": "阿里云百炼(国际)",
  576. "en-US": "Alibaba Cloud (Global)",
  577. });
  578. expect(snapshot.providerTemplates?.get("openai")?.config.toJSON()).toMatchObject({
  579. builtinModelIds: [
  580. "gpt-6-astra",
  581. "gpt-5.6-sol",
  582. "gpt-5.6-terra",
  583. "gpt-5.6-luna",
  584. "gpt-5.6",
  585. "gpt-5.4",
  586. "gpt-5.4-pro",
  587. "gpt-5.4-mini",
  588. "gpt-5.4-nano",
  589. "gpt-5.3-codex",
  590. ],
  591. access: {
  592. type: "api-key",
  593. apiKeyManagementUrl: "https://platform.openai.com/api-keys",
  594. },
  595. api: { type: "openai-responses", baseUrl: "https://api.openai.com/v1" },
  596. });
  597. expect(snapshot.providerTemplates?.get("anthropic")?.config.toJSON()).toMatchObject({
  598. builtinModelIds: [
  599. "claude-fable-5-1",
  600. "claude-fable-5",
  601. "claude-opus-5",
  602. "claude-sonnet-5",
  603. "claude-haiku-4-5-20251001",
  604. ],
  605. access: {
  606. type: "api-key",
  607. apiKeyManagementUrl: "https://console.anthropic.com/settings/keys",
  608. },
  609. api: { type: "anthropic-messages", baseUrl: "https://api.anthropic.com/v1" },
  610. });
  611. expect(snapshot.providerTemplates?.get("xai")?.config.toJSON()).toMatchObject({
  612. builtinModelIds: ["grok-4.6", "grok-build-0.1", "grok-4.3"],
  613. access: {
  614. type: "api-key",
  615. apiKeyManagementUrl: "https://console.x.ai",
  616. },
  617. api: { type: "openai-responses", baseUrl: "https://api.x.ai/v1" },
  618. });
  619. expect(snapshot.providerTemplates?.get("zai-api")?.config.toJSON()).toMatchObject({
  620. access: { type: "zhipu-coding-plan-api-key" },
  621. });
  622. expect(snapshot.providerTemplates?.get("bigmodel-api")?.config.toJSON()).toMatchObject({
  623. access: { type: "zhipu-coding-plan-api-key" },
  624. });
  625. } finally {
  626. source.dispose();
  627. }
  628. });
  629. it("不保留语义等价的重复 matcher", async () => {
  630. const source = new NodeZCodeBuiltinProviderConfigSource({
  631. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  632. watch: false,
  633. });
  634. try {
  635. // 三组 matcher 规则各自独立,但语义等价的 selector 不能跨组重复书写。
  636. const rules = (await source.read()).models
  637. .rules()
  638. .filter(
  639. (rule) =>
  640. rule.type === "model" || rule.type === "model-api" || rule.type === "provider-site",
  641. );
  642. const selectorKeys = rules.map((rule) =>
  643. JSON.stringify([
  644. rule.modelMatch.toLowerCase(),
  645. "apiTypeMatch" in rule ? (rule.apiTypeMatch ?? "") : "",
  646. "baseUrlMatch" in rule ? rule.baseUrlMatch : "",
  647. ]),
  648. );
  649. expect(new Set(selectorKeys).size).toBe(selectorKeys.length);
  650. } finally {
  651. source.dispose();
  652. }
  653. });
  654. it("Reasoning 公开档位按语义强度从低到高排列", async () => {
  655. const source = new NodeZCodeBuiltinProviderConfigSource({
  656. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  657. watch: false,
  658. });
  659. try {
  660. const levelRank: Readonly<Record<string, number>> = {
  661. disabled: 0,
  662. none: 0,
  663. nothink: 0,
  664. off: 0,
  665. enabled: 1,
  666. minimal: 0.5,
  667. low: 1,
  668. medium: 2,
  669. high: 3,
  670. xhigh: 4,
  671. max: 5,
  672. };
  673. for (const rule of (await source.read()).models.rules()) {
  674. const values = rule.config.optionSpecs?.reasoningLevel?.values;
  675. if (!values) continue;
  676. const ranks = values.map((value) => levelRank[value]);
  677. expect(
  678. ranks.every((rank) => rank !== undefined),
  679. `${rule.modelMatch} 存在未知 Reasoning 档位,需先声明其顺序语义`,
  680. ).toBe(true);
  681. expect(
  682. ranks.every((rank, index) => index === 0 || rank! >= ranks[index - 1]!),
  683. `${rule.modelMatch} 的 Reasoning 档位必须从低到高排列`,
  684. ).toBe(true);
  685. }
  686. } finally {
  687. source.dispose();
  688. }
  689. });
  690. it("解析 OpenAI Responses 与 Anthropic Messages 的模型事实和 reasoning 映射", async () => {
  691. const source = new NodeZCodeBuiltinProviderConfigSource({
  692. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  693. watch: false,
  694. });
  695. try {
  696. const snapshot = await source.read();
  697. const openAi = snapshot.models.resolve({
  698. providerId: "openai",
  699. modelId: "gpt-5.6-sol",
  700. apiType: "openai-responses",
  701. baseUrl: "https://api.openai.com/v1",
  702. });
  703. expect(openAi.properties?.toJSON()).toMatchObject({
  704. requiresMfjsToolSchema: false,
  705. contextWindow: 1_050_000,
  706. inputFormat: {
  707. supportsText: true,
  708. supportsImage: true,
  709. supportsVideo: false,
  710. supportsAudio: false,
  711. supportsPdf: true,
  712. },
  713. outputFormat: { supportsText: true },
  714. supportsToolCall: true,
  715. supportsJsonSchemaOutput: true,
  716. supportsNativeWebSearch: false,
  717. supportsMidConversationSystem: false,
  718. });
  719. expect(openAi.optionSpecs?.reasoningLevel).toMatchObject({
  720. values: ["none", "low", "medium", "high", "xhigh", "max"],
  721. });
  722. for (const effort of ["xhigh", "max"]) {
  723. expect(reasoningPatch(openAi.optionSpecs!.reasoningLevel!.map!, effort)).toEqual({
  724. reasoning: { effort },
  725. });
  726. }
  727. const grok46 = snapshot.models.resolve({
  728. providerId: "xai",
  729. modelId: "grok-4.6",
  730. apiType: "openai-responses",
  731. baseUrl: "https://api.x.ai/v1",
  732. });
  733. expect(grok46.properties?.toJSON()).toMatchObject({
  734. requiresMfjsToolSchema: false,
  735. contextWindow: 500_000,
  736. inputFormat: {
  737. supportsText: true,
  738. supportsImage: true,
  739. supportsVideo: false,
  740. supportsAudio: false,
  741. supportsPdf: false,
  742. },
  743. outputFormat: { supportsText: true },
  744. supportsToolCall: true,
  745. supportsJsonSchemaOutput: true,
  746. supportsNativeWebSearch: false,
  747. supportsMidConversationSystem: false,
  748. });
  749. expect(grok46.optionSpecs?.reasoningLevel).toMatchObject({
  750. values: ["low", "medium", "high", "xhigh"],
  751. });
  752. expect(reasoningPatch(grok46.optionSpecs!.reasoningLevel!.map!, "xhigh")).toEqual({
  753. reasoning: { effort: "xhigh" },
  754. });
  755. const grokBuild = snapshot.models.resolve({
  756. providerId: "xai",
  757. modelId: "grok-build-0.1",
  758. apiType: "openai-responses",
  759. baseUrl: "https://api.x.ai/v1",
  760. });
  761. expect(grokBuild.properties?.toJSON()).toMatchObject({
  762. requiresMfjsToolSchema: false,
  763. contextWindow: 256_000,
  764. inputFormat: { supportsText: true, supportsImage: true },
  765. outputFormat: { supportsText: true },
  766. supportsToolCall: true,
  767. supportsJsonSchemaOutput: true,
  768. });
  769. const grok43 = snapshot.models.resolve({
  770. providerId: "xai",
  771. modelId: "grok-4.3",
  772. apiType: "openai-responses",
  773. baseUrl: "https://api.x.ai/v1",
  774. });
  775. expect(grok43.properties?.contextWindow).toBe(1_000_000);
  776. expect(grok43.optionSpecs?.reasoningLevel?.toJSON()).toMatchObject({
  777. // 此模型未声明专用档位,继续继承 Model 通用兜底。
  778. values: ["disabled", "enabled"],
  779. });
  780. expect(reasoningPatch(grok43.optionSpecs!.reasoningLevel!.map!, "disabled")).toEqual({
  781. reasoning: { effort: "none" },
  782. });
  783. const anthropic = snapshot.models.resolve({
  784. providerId: "anthropic",
  785. modelId: "claude-opus-5",
  786. apiType: "anthropic-messages",
  787. baseUrl: "https://api.anthropic.com/v1",
  788. });
  789. expect(anthropic.properties?.toJSON()).toMatchObject({
  790. requiresMfjsToolSchema: false,
  791. contextWindow: 1_000_000,
  792. inputFormat: {
  793. supportsText: true,
  794. supportsImage: true,
  795. supportsVideo: false,
  796. supportsAudio: false,
  797. supportsPdf: true,
  798. },
  799. outputFormat: { supportsText: true },
  800. supportsToolCall: true,
  801. supportsJsonSchemaOutput: true,
  802. supportsNativeWebSearch: true,
  803. supportsMidConversationSystem: true,
  804. });
  805. expect(anthropic.optionSpecs?.reasoningLevel).toMatchObject({
  806. values: ["low", "medium", "high", "xhigh", "max"],
  807. });
  808. expect(anthropic.optionSpecs?.reasoningLevel?.map).toContain("reasoningLevel");
  809. expect(reasoningPatch(anthropic.optionSpecs!.reasoningLevel!.map!, "max")).toMatchObject({
  810. output_config: { effort: "max" },
  811. });
  812. const deepSeek = snapshot.models.resolve({
  813. providerId: "deepseek",
  814. modelId: "deepseek-v4-flash",
  815. apiType: "anthropic-messages",
  816. baseUrl: "https://api.deepseek.com/anthropic",
  817. });
  818. expect(deepSeek.properties?.supportsJsonSchemaOutput).toBe(false);
  819. expect(deepSeek.optionSpecs?.reasoningLevel).toMatchObject({
  820. values: ["disabled", "low", "high", "max"],
  821. });
  822. expect(deepSeek.optionSpecs?.reasoningLevel?.map).toContain("reasoningLevel");
  823. expect(reasoningPatch(deepSeek.optionSpecs!.reasoningLevel!.map!, "low")).toMatchObject({
  824. output_config: { effort: "low" },
  825. });
  826. for (const modelId of ["qwen3.5-plus", "qwen3.5-flash", "qwen3-vl-plus"]) {
  827. expect(
  828. snapshot.models.resolve({
  829. providerId: "qwen-alibaba-model-studio-cn",
  830. modelId,
  831. apiType: "anthropic-messages",
  832. baseUrl: "https://dashscope.aliyuncs.com/apps/anthropic",
  833. }).properties?.supportsJsonSchemaOutput,
  834. ).toBe(false);
  835. }
  836. const deepSeekResponses = snapshot.models.resolve({
  837. providerId: "deepseek",
  838. modelId: "deepseek-v4-pro",
  839. apiType: "openai-responses",
  840. baseUrl: "https://api.deepseek.com",
  841. });
  842. expect(deepSeekResponses.properties?.supportsJsonSchemaOutput).toBe(true);
  843. expect(reasoningPatch(deepSeekResponses.optionSpecs!.reasoningLevel!.map!, "max")).toEqual({
  844. reasoning: { effort: "max" },
  845. });
  846. } finally {
  847. source.dispose();
  848. }
  849. });
  850. it("每个 Built-in Provider 与模型成员都完整,启用成员在补齐访问材料后进入正式 Registry", async () => {
  851. const source = new NodeZCodeBuiltinProviderConfigSource({
  852. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  853. watch: false,
  854. });
  855. try {
  856. const snapshot = await source.read();
  857. const accountProviders: [string, ProviderConfig][] = [];
  858. const personalProviders: Array<{
  859. providerId: string;
  860. templateId: string;
  861. config: ProviderConfig;
  862. }> = [];
  863. const expectedModelIds = new Map<string, readonly string[]>();
  864. for (const [providerId, provider] of snapshot.providers.entries()) {
  865. const builtinModelIds = provider.builtinModelIds ?? [];
  866. expect(new Set(builtinModelIds).size, `${providerId} 存在重复 Built-in 模型成员`).toBe(
  867. builtinModelIds.length,
  868. );
  869. const enabledModelIds = builtinModelIds.filter((modelId) => {
  870. const model = snapshot.models.resolve({
  871. providerId,
  872. modelId,
  873. apiType: provider.api?.type,
  874. baseUrl: provider.api?.baseUrl,
  875. });
  876. expect(model.validateComplete(["providers", providerId, "models", modelId])).toEqual([]);
  877. return model.enabled !== false;
  878. });
  879. expectedModelIds.set(providerId, enabledModelIds);
  880. if (provider.access?.type === "zhipu-account") {
  881. accountProviders.push([
  882. providerId,
  883. new ProviderConfig({
  884. access: new ZhipuAccountAccessConfig({ entitled: true }),
  885. }),
  886. ]);
  887. }
  888. }
  889. for (const [templateId, template] of snapshot.providerTemplates?.entries() ?? []) {
  890. const providerId = `template:${templateId}`;
  891. const builtinModelIds = template.config.builtinModelIds ?? [];
  892. expect(new Set(builtinModelIds).size, `${templateId} 存在重复 Template 模型成员`).toBe(
  893. builtinModelIds.length,
  894. );
  895. const enabledModelIds = builtinModelIds.filter((modelId) => {
  896. const model = snapshot.models.resolve({
  897. providerId,
  898. templateId,
  899. modelId,
  900. apiType: template.config.api?.type,
  901. baseUrl: template.config.api?.baseUrl,
  902. });
  903. expect(model.validateComplete(["templates", templateId, "models", modelId])).toEqual([]);
  904. return model.enabled !== false;
  905. });
  906. expectedModelIds.set(providerId, enabledModelIds);
  907. expect(["api-key", "zhipu-coding-plan-api-key"]).toContain(template.config.access?.type);
  908. if (template.config.access?.type !== "zhipu-account") {
  909. expect(template.config.access.apiKey, `${templateId} 不得保存 API Key`).toBeUndefined();
  910. }
  911. personalProviders.push({
  912. providerId,
  913. templateId,
  914. config: new ProviderConfig({
  915. group: "standard-personal",
  916. access: new ApiKeyAccessConfig({
  917. type: template.config.access?.type,
  918. apiKey: `test-key-for-${templateId}`,
  919. }),
  920. }),
  921. });
  922. }
  923. const resolution = new ProviderConfigResolver().resolve({
  924. zcodeBuiltinProviders: snapshot.providers,
  925. zcodeBuiltinProviderTemplates: snapshot.providerTemplates,
  926. accountProviders: new ProviderConfigMap(accountProviders),
  927. personalProviders: new ProviderConfigMap(personalProviders),
  928. zcodeBuiltinModelRules: snapshot.models,
  929. personalModels: ModelConfigRules.empty(),
  930. });
  931. expect(resolution.issues).toEqual([]);
  932. expect(resolution.registryProviders.map(({ providerId }) => providerId)).toEqual([
  933. "account:zai-individual-coding-plan",
  934. "account:zai-team-coding-plan",
  935. "account:zai-start-plan",
  936. "account:bigmodel-individual-coding-plan",
  937. "account:bigmodel-team-coding-plan",
  938. "account:bigmodel-start-plan",
  939. "account:zai-offpeak-idle-plan",
  940. "account:bigmodel-offpeak-idle-plan",
  941. "account:zai-highspeed-card",
  942. "account:bigmodel-highspeed-card",
  943. "template:zai-api",
  944. "template:zai-standard-api",
  945. "template:bigmodel-api",
  946. "template:bigmodel-standard-api",
  947. "template:moonshot-kimi",
  948. "template:minimax",
  949. "template:deepseek",
  950. "template:qwen-alibaba-model-studio-cn",
  951. "template:qwen-alibaba-model-studio-intl",
  952. "template:xiaomi-mimo",
  953. "template:xiaomi-mimo-token-plan",
  954. "template:openai",
  955. "template:anthropic",
  956. "template:xai",
  957. "template:openrouter",
  958. "template:opencode-go-chat",
  959. "template:opencode-go-messages",
  960. "template:opencode-go-responses",
  961. "template:opencode-zen-responses",
  962. "template:opencode-zen-messages",
  963. "template:opencode-zen-chat",
  964. ]);
  965. for (const provider of resolution.registryProviders) {
  966. expect(
  967. provider.models.map(({ modelId }) => modelId),
  968. `${provider.providerId} 的 Registry 模型成员必须与 Built-in Config 一致`,
  969. ).toEqual(expectedModelIds.get(provider.providerId));
  970. for (const model of provider.models) {
  971. expect(
  972. model.config.validateComplete([
  973. "providers",
  974. provider.providerId,
  975. "models",
  976. model.modelId,
  977. ]),
  978. ).toEqual([]);
  979. }
  980. }
  981. for (const providerId of [
  982. "account:zai-offpeak-idle-plan",
  983. "account:bigmodel-offpeak-idle-plan",
  984. ]) {
  985. const offPeak = resolution.resolvedProviders.find(
  986. (provider) => provider.providerId === providerId,
  987. );
  988. expect(offPeak?.models).not.toHaveLength(0);
  989. for (const model of offPeak?.models ?? []) {
  990. expect(model).toMatchObject({ executable: true, selectable: false });
  991. }
  992. }
  993. } finally {
  994. source.dispose();
  995. }
  996. });
  997. it("完整兜底让任意未知模型 ID 在各 API type 与模板下完整并进入 Registry(R3A-B-03)", async () => {
  998. const source = new NodeZCodeBuiltinProviderConfigSource({
  999. bundledFilePath: zcodeBuiltinProviderConfigFilePath,
  1000. watch: false,
  1001. });
  1002. try {
  1003. const snapshot = await source.read();
  1004. const fallbackRule = snapshot.models
  1005. .rules()
  1006. .find((rule) => rule.type === "model" && rule.modelMatch === ".*");
  1007. const fallbackMax = fallbackRule?.config.optionSpecs?.maxOutputTokens?.max;
  1008. expect(fallbackMax, "兜底规则必须声明 maxOutputTokens.max").toEqual(expect.any(Number));
  1009. const unknownModelId = "b03-unknown-model-x";
  1010. const scenarios = [
  1011. { providerId: "custom-chat", apiType: "openai-chat-completions" as const },
  1012. { providerId: "custom-messages", apiType: "anthropic-messages" as const },
  1013. { providerId: "custom-responses", apiType: "openai-responses" as const },
  1014. {
  1015. providerId: "custom-deepseek",
  1016. templateId: "deepseek",
  1017. apiType: "openai-chat-completions" as const,
  1018. baseURL: "https://api.deepseek.com",
  1019. // 只能用目录默认启用的已知模型做探针:deepseek-v4-flash 已被 Built-in 目录标为
  1020. // enabled:false,稀疏 Personal 规则不会覆盖它,Registry 里自然取不到。
  1021. knownModelId: "deepseek-v4-pro",
  1022. },
  1023. {
  1024. providerId: "custom-zai",
  1025. templateId: "zai-api",
  1026. apiType: "anthropic-messages" as const,
  1027. baseURL: "https://api.z.ai/api/anthropic",
  1028. knownModelId: "glm-5.3",
  1029. },
  1030. ];
  1031. const personalProviders = scenarios.map((scenario) => ({
  1032. providerId: scenario.providerId,
  1033. ...(scenario.templateId ? { templateId: scenario.templateId } : {}),
  1034. config: new ProviderConfig({
  1035. group: "standard-personal",
  1036. access: new ApiKeyAccessConfig({ apiKey: `test-key-for-${scenario.providerId}` }),
  1037. api: new ProviderApiConfig({
  1038. type: scenario.apiType,
  1039. baseUrl: scenario.baseURL ?? "https://custom.example.com/v1",
  1040. }),
  1041. personalModelIds: [
  1042. unknownModelId,
  1043. ...(scenario.knownModelId ? [scenario.knownModelId] : []),
  1044. ],
  1045. }),
  1046. }));
  1047. // 设置页新增模型时 config-service 只写入 {enabled:true},这里复用同一稀疏 Personal 规则。
  1048. const personalModels = new ModelConfigRules(
  1049. scenarios.map((scenario) => ({
  1050. type: "provider-model" as const,
  1051. providerId: scenario.providerId,
  1052. modelId: unknownModelId,
  1053. config: parseModelConfig({ enabled: true }),
  1054. })),
  1055. );
  1056. const resolution = new ProviderConfigResolver().resolve({
  1057. zcodeBuiltinProviders: ProviderConfigMap.empty(),
  1058. zcodeBuiltinProviderTemplates: snapshot.providerTemplates,
  1059. accountProviders: ProviderConfigMap.empty(),
  1060. personalProviders: new ProviderConfigMap(personalProviders),
  1061. zcodeBuiltinModelRules: snapshot.models,
  1062. personalModels,
  1063. });
  1064. const registry = new ProviderRegistry(resolution.registryProviders);
  1065. expect(resolution.issues).toEqual([]);
  1066. for (const scenario of scenarios) {
  1067. const unknown = registry.getModel(scenario.providerId, unknownModelId);
  1068. expect(unknown, `${scenario.providerId}/${unknownModelId} 应进入 Registry`).toBeDefined();
  1069. expect(unknown?.config.optionSpecs.maxOutputTokens).toEqual({
  1070. max: fallbackMax,
  1071. map: expect.any(String),
  1072. });
  1073. if (scenario.knownModelId) {
  1074. const known = registry.getModel(scenario.providerId, scenario.knownModelId);
  1075. expect(
  1076. known,
  1077. `${scenario.providerId}/${scenario.knownModelId} 应进入 Registry`,
  1078. ).toBeDefined();
  1079. expect(known?.config.optionSpecs.maxOutputTokens.max).toEqual(expect.any(Number));
  1080. // 新通用上限可与已知型号一致;已知型号仍由自身规则解析,不能以数值不同判断是否命中。
  1081. expect(known?.config.optionSpecs.reasoningLevel.values).not.toEqual([
  1082. "disabled",
  1083. "enabled",
  1084. ]);
  1085. }
  1086. }
  1087. } finally {
  1088. source.dispose();
  1089. }
  1090. });
  1091. it("正式 Built-in JSON 不携带旧模型能力字段", async () => {
  1092. const document = JSON.parse(
  1093. await readFile(zcodeBuiltinProviderConfigFilePath, "utf8"),
  1094. ) as unknown;
  1095. const keys = collectObjectKeys(document);
  1096. expect(keys).not.toContain("apiKey");
  1097. expect(keys).not.toContain("modalities");
  1098. expect(keys).not.toContain("magic_name");
  1099. expect(keys).not.toContain("supportsImages");
  1100. // Todo104:supportsPdf/Video 是 inputFormat 内的正式字段,不能因同名误判成旧扁平能力。
  1101. for (const key of [
  1102. "input_format",
  1103. "output_format",
  1104. "support_text",
  1105. "support_image",
  1106. "support_pdf",
  1107. "support_video",
  1108. "support_audio",
  1109. ]) {
  1110. expect(keys).not.toContain(key);
  1111. }
  1112. expect(keys).not.toContain("budget_tokens");
  1113. expect(keys).not.toContain("budgetTokens");
  1114. });
  1115. });
  1116. function collectObjectKeys(value: unknown): string[] {
  1117. if (Array.isArray(value)) return value.flatMap(collectObjectKeys);
  1118. if (!value || typeof value !== "object") return [];
  1119. return Object.entries(value).flatMap(([key, child]) => [key, ...collectObjectKeys(child)]);
  1120. }