| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178 |
- import { readFile } from "node:fs/promises";
- import { compileModelOptionMaps } from "@zcode/model-option-map";
- import { fileURLToPath } from "node:url";
- import {
- ApiKeyAccessConfig,
- ModelConfigRules,
- ProviderApiConfig,
- ProviderConfig,
- ProviderConfigMap,
- ProviderConfigResolver,
- ProviderRegistry,
- ZhipuAccountAccessConfig,
- parseModelConfig,
- } from "@zcode/provider";
- import { describe, expect, it } from "vitest";
- import { NodeZCodeBuiltinProviderConfigSource } from "../src/index.js";
- // 发布验证必须指向最终上传的那份文件,而不是默认悄悄检查仓库源文件。
- const zcodeBuiltinProviderConfigFilePath =
- process.env.ZCODE_BUILTIN_RELEASE_TEST_FILE?.trim() ||
- fileURLToPath(new URL("../../../config/provider/zcode-builtin.json", import.meta.url));
- function reasoningPatch(map: string, reasoningLevel: string) {
- return compileModelOptionMaps({ reasoningLevel: { map }, maxOutputTokens: { map: "{}" } }).apply(
- {},
- { reasoningLevel, maxOutputTokens: 32000 },
- );
- }
- describe("ZCode Built-in Provider Config 完整性", () => {
- it.each(["anthropic-messages", "openai-chat-completions", "openai-responses"] as const)(
- "未知模型在 %s 下拥有完整两档兜底并可进入 Registry",
- async (apiType) => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const model = snapshot.models.resolve({
- providerId: "unknown-provider",
- modelId: "future-unknown-model",
- apiType,
- baseUrl: "https://unknown.example/v1",
- });
- expect(model.validateComplete()).toEqual([]);
- expect(model.optionSpecs?.maxOutputTokens?.max).toBe(128_000);
- expect(model.optionSpecs?.reasoningLevel?.values).toEqual(["disabled", "enabled"]);
- expect(model.properties?.inputFormat.supportsImage).toBe(false);
- const result = new ProviderConfigResolver().resolve({
- zcodeBuiltinProviders: snapshot.providers,
- zcodeBuiltinProviderTemplates: snapshot.providerTemplates,
- accountProviders: ProviderConfigMap.empty(),
- zcodeBuiltinModelRules: snapshot.models,
- personalModels: ModelConfigRules.empty(),
- personalProviders: new ProviderConfigMap([
- [
- "unknown-provider",
- new ProviderConfig({
- group: "standard-personal",
- api: new ProviderApiConfig({
- type: apiType,
- baseUrl: "https://unknown.example/v1",
- }),
- access: new ApiKeyAccessConfig({ apiKey: "test-only" }),
- personalModelIds: ["future-unknown-model"],
- }),
- ],
- ]),
- });
- expect(
- result.registryProviders
- .find((entry) => entry.providerId === "unknown-provider")
- ?.models.map((entry) => entry.modelId),
- ).toContain("future-unknown-model");
- } finally {
- source.dispose();
- }
- },
- );
- it("GLM-5.3-Flash 的媒体输入能力及 GLM-5.3 的套餐桥接保持明确", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const rawRelease = JSON.parse(await readFile(zcodeBuiltinProviderConfigFilePath, "utf8")) as {
- revision: number;
- config: { modelConfigRules: Record<string, unknown> };
- };
- expect(rawRelease.config.modelConfigRules).toMatchObject({
- modelRules: expect.any(Array),
- modelApiRules: expect.any(Array),
- providerSiteRules: expect.any(Array),
- templateModelRules: expect.any(Array),
- builtinProviderModelRules: expect.any(Array),
- });
- expect(rawRelease.config.modelConfigRules).not.toHaveProperty("matchRules");
- expect(snapshot.revision).toMatch(
- new RegExp(`^zcode-builtin:${rawRelease.revision}:[a-f0-9]{64}$`, "u"),
- );
- const apiTemplateIds = [
- "zai-api",
- "bigmodel-api",
- "zai-standard-api",
- "bigmodel-standard-api",
- ] as const;
- const accountProviderIds = [
- "account:zai-individual-coding-plan",
- "account:zai-team-coding-plan",
- "account:zai-start-plan",
- "account:bigmodel-individual-coding-plan",
- "account:bigmodel-team-coding-plan",
- "account:bigmodel-start-plan",
- "account:zai-offpeak-idle-plan",
- "account:bigmodel-offpeak-idle-plan",
- ] as const;
- for (const templateId of apiTemplateIds) {
- const modelIds = snapshot.providerTemplates?.get(templateId)?.config.builtinModelIds ?? [];
- expect(modelIds, `${templateId} 必须同时提供 GLM-5.3 系列`).toEqual(
- expect.arrayContaining(["GLM-5.3", "GLM-5.3-Flash"]),
- );
- }
- for (const providerId of accountProviderIds) {
- const modelIds = snapshot.providers.get(providerId)?.builtinModelIds ?? [];
- expect(modelIds, `${providerId} 必须提供 GLM-5.3-Flash`).toContain("GLM-5.3-Flash");
- }
- for (const providerId of [
- "account:zai-individual-coding-plan",
- "account:zai-team-coding-plan",
- "account:bigmodel-individual-coding-plan",
- "account:bigmodel-team-coding-plan",
- ]) {
- expect(snapshot.providers.get(providerId)?.builtinModelIds).toEqual([
- "GLM-5.3",
- "GLM-5.3-Flash",
- ]);
- const planModel = snapshot.models.resolve({
- providerId,
- modelId: "GLM-5.3",
- apiType: "anthropic-messages",
- baseUrl: snapshot.providers.get(providerId)?.api?.baseUrl,
- });
- expect(planModel.properties?.inputFormat.toJSON()).toMatchObject({
- supportsImage: true,
- supportsVideo: true,
- supportsAudio: false,
- supportsPdf: false,
- });
- }
- // 加速卡换端点不换模型,能力事实必须与同 Family Coding Plan 站点逐项一致;
- // 否则同一会话里普通轮与加速轮的 system 折叠、WebSearch 暴露和附件合法性会互相打脸。
- for (const [highspeedProviderId, planProviderId] of [
- ["account:zai-highspeed-card", "account:zai-individual-coding-plan"],
- ["account:bigmodel-highspeed-card", "account:bigmodel-individual-coding-plan"],
- ] as const) {
- const resolveAt = (providerId: string, modelId: string) =>
- snapshot.models.resolve({
- providerId,
- modelId,
- apiType: "anthropic-messages",
- baseUrl: snapshot.providers.get(providerId)?.api?.baseUrl,
- });
- expect(snapshot.providers.get(highspeedProviderId)?.builtinModelIds).toEqual(
- snapshot.providers.get(planProviderId)?.builtinModelIds,
- );
- for (const modelId of snapshot.providers.get(highspeedProviderId)?.builtinModelIds ?? []) {
- const highspeedModel = resolveAt(highspeedProviderId, modelId);
- const planModel = resolveAt(planProviderId, modelId);
- expect(
- highspeedModel.properties?.inputFormat.toJSON(),
- `${highspeedProviderId}/${modelId} 输入形态偏离 Coding Plan 站点`,
- ).toEqual(planModel.properties?.inputFormat.toJSON());
- expect(
- highspeedModel.properties?.supportsMidConversationSystem,
- `${highspeedProviderId}/${modelId} MCS 偏离 Coding Plan 站点`,
- ).toBe(planModel.properties?.supportsMidConversationSystem);
- expect(
- highspeedModel.properties?.supportsNativeWebSearch,
- `${highspeedProviderId}/${modelId} 原生搜索偏离 Coding Plan 站点`,
- ).toBe(planModel.properties?.supportsNativeWebSearch);
- }
- }
- const resolve = (modelId: string) =>
- snapshot.models.resolve({
- providerId: "personal-zai",
- templateId: "zai-api",
- modelId,
- apiType: "anthropic-messages",
- // 模型层自身能力与 §6.3.1 已批准的站点覆盖分开验证。
- baseUrl: "https://other.example.com/anthropic",
- });
- const glm53 = resolve("glm-5.3").toJSON();
- const glm53Flash = resolve("glm-5.3-flash").toJSON();
- expect(glm53.optionSpecs?.reasoningLevel).toMatchObject({
- values: ["low", "high", "max"],
- });
- expect(glm53.optionSpecs?.reasoningLevel?.map).toContain("reasoningLevel");
- expect(reasoningPatch(glm53.optionSpecs!.reasoningLevel!.map!, "low")).toMatchObject({
- output_config: { effort: "low" },
- });
- expect(glm53.properties?.inputFormat).toMatchObject({
- supportsImage: false,
- supportsVideo: false,
- });
- expect(glm53Flash).toEqual({
- ...glm53,
- properties: {
- ...glm53.properties,
- inputFormat: {
- ...glm53.properties?.inputFormat,
- supportsImage: true,
- supportsVideo: true,
- supportsPdf: true,
- },
- },
- });
- const glm53FlashChat = snapshot.models.resolve({
- providerId: "personal-zai",
- templateId: "zai-api",
- modelId: "glm-5.3-flash",
- apiType: "openai-chat-completions",
- baseUrl: "https://api.z.ai/api/anthropic",
- });
- expect(glm53FlashChat.properties?.inputFormat.supportsPdf).toBe(true);
- } finally {
- source.dispose();
- }
- });
- it("按当前官方证据声明各厂商主推模型和关键静态属性", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- expect(
- snapshot.providerTemplates?.get("moonshot-kimi")?.config.builtinModelIds.slice(0, 3),
- ).toEqual(["kimi-k3", "kimi-k2.7-code", "kimi-k2.6"]);
- expect(
- snapshot.providerTemplates?.get("minimax")?.config.builtinModelIds.slice(0, 3),
- ).toEqual(["MiniMax-M3", "MiniMax-M2.7", "MiniMax-M2.7-highspeed"]);
- for (const providerId of ["qwen-alibaba-model-studio-cn", "qwen-alibaba-model-studio-intl"]) {
- expect(
- snapshot.providerTemplates?.get(providerId)?.config.builtinModelIds.slice(0, 2),
- ).toEqual(["qwen3.8-max", "qwen3.8-flash"]);
- }
- expect(snapshot.providerTemplates?.get("openai")?.config.builtinModelIds.slice(0, 4)).toEqual(
- ["gpt-6-astra", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna"],
- );
- const resolve = (
- providerId: string,
- modelId: string,
- apiType: "anthropic-messages" | "openai-chat-completions" | "openai-responses",
- baseUrl: string,
- ) => snapshot.models.resolve({ providerId, modelId, apiType, baseUrl });
- const qwen38 = resolve(
- "qwen-alibaba-model-studio-intl",
- "qwen3.8-flash",
- "openai-chat-completions",
- "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
- );
- expect(qwen38.properties?.toJSON()).toMatchObject({
- contextWindow: 1_000_000,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: true,
- supportsAudio: false,
- supportsPdf: false,
- },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- });
- expect(qwen38.optionSpecs?.maxOutputTokens).toMatchObject({
- max: 131_072,
- });
- const mimo = resolve(
- "xiaomi-mimo",
- "mimo-v2.5",
- "anthropic-messages",
- "https://api.xiaomimimo.com/anthropic",
- );
- expect(mimo.properties?.inputFormat.toJSON()).toMatchObject({
- supportsImage: true,
- supportsVideo: true,
- supportsAudio: true,
- });
- const kimiCode = resolve(
- "moonshot-kimi",
- "kimi-k2.7-code",
- "anthropic-messages",
- "https://api.moonshot.cn/anthropic",
- );
- expect(kimiCode.properties?.contextWindow).toBe(262_144);
- expect(kimiCode.properties?.inputFormat.toJSON()).toMatchObject({
- supportsText: true,
- supportsImage: true,
- supportsVideo: true,
- });
- const kimiK3 = resolve(
- "moonshot-kimi",
- "kimi-k3",
- "anthropic-messages",
- "https://api.moonshot.cn/anthropic",
- );
- expect(kimiK3.properties?.toJSON()).toMatchObject({
- contextWindow: 1_048_576,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: true,
- },
- });
- const kimiK3Compact = resolve(
- "moonshot-kimi",
- "k3-256k",
- "anthropic-messages",
- "https://api.kimi.com/coding/",
- );
- expect(kimiK3Compact.properties?.toJSON()).toMatchObject({
- contextWindow: 262_144,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: false,
- },
- });
- const minimax = resolve(
- "minimax",
- "MiniMax-M2.7",
- "anthropic-messages",
- "https://api.minimaxi.com/anthropic",
- );
- expect(minimax.properties?.toJSON()).toMatchObject({
- contextWindow: 204_800,
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- },
- });
- const minimaxM3 = resolve(
- "minimax",
- "MiniMax-M3",
- "anthropic-messages",
- "https://api.minimaxi.com/anthropic",
- );
- expect(minimaxM3.properties?.toJSON()).toMatchObject({
- contextWindow: 1_000_000,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: true,
- },
- });
- } finally {
- source.dispose();
- }
- });
- it("聚合供应商按维护清单提供独立模板,不复用上游 Provider 配置", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const openrouter = snapshot.providerTemplates?.get("openrouter")?.config;
- expect(openrouter?.api?.type).toBe("anthropic-messages");
- expect(openrouter?.api?.baseUrl).toBe("https://openrouter.ai/api");
- expect(openrouter?.builtinModelIds).toEqual(
- expect.arrayContaining(["openai/gpt-5.6-sol", "anthropic/claude-opus-5", "z-ai/glm-5.3"]),
- );
- for (const templateId of [
- "opencode-zen-responses",
- "opencode-zen-messages",
- "opencode-zen-chat",
- ]) {
- expect(snapshot.providerTemplates?.has(templateId)).toBe(true);
- }
- } finally {
- source.dispose();
- }
- });
- it("聚合供应商同型号命中模型规则和 API Map,保留完整 ID 且每家至少启用一个代表型号", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const pairs = [
- ["openai/gpt-5.6-sol", "gpt-5.6-sol"],
- ["anthropic/claude-opus-5", "claude-opus-5"],
- ["anthropic/claude-haiku-4.5", "claude-haiku-4-5-20251001"],
- ["deepseek/deepseek-v4-pro", "deepseek-v4-pro"],
- ["moonshotai/kimi-k3", "kimi-k3"],
- ["z-ai/glm-5.3", "glm-5.3"],
- ["qwen/qwen3.8-max", "qwen3.8-max"],
- ["minimax/minimax-m3", "MiniMax-M3"],
- ["xiaomi/mimo-v2.5-pro", "mimo-v2.5-pro"],
- ["x-ai/grok-4.6", "grok-4.6"],
- ];
- for (const [aggregateId, directId] of pairs) {
- const resolve = (modelId: string, templateId?: string) =>
- snapshot.models.resolve({
- providerId: "aggregate-instance",
- templateId,
- modelId,
- apiType: "anthropic-messages",
- baseUrl: "https://openrouter.ai/api",
- });
- const aggregate = resolve(aggregateId!, "openrouter");
- const direct = resolve(directId!);
- expect(aggregate.properties?.toJSON(), aggregateId).toEqual(direct.properties?.toJSON());
- expect(aggregate.optionSpecs?.toJSON(), aggregateId).toEqual(direct.optionSpecs?.toJSON());
- }
- for (const templateId of ["openrouter", "opencode-zen-messages", "opencode-zen-chat"]) {
- const template = snapshot.providerTemplates!.get(templateId)!.config;
- const enabledByVendor = new Map<string, boolean>();
- for (const modelId of template.builtinModelIds!) {
- const vendor =
- templateId === "openrouter" ? modelId.split("/")[0]! : modelId.split("-")[0]!;
- const model = snapshot.models.resolve({
- providerId: "aggregate-instance",
- templateId,
- modelId,
- apiType: template.api!.type,
- baseUrl: template.api!.baseUrl,
- });
- enabledByVendor.set(
- vendor,
- (enabledByVendor.get(vendor) ?? false) || model.enabled === true,
- );
- }
- for (const [vendor, enabled] of enabledByVendor)
- expect(enabled, `${templateId}/${vendor}`).toBe(true);
- }
- const haiku = snapshot.models.resolve({
- providerId: "zen",
- templateId: "opencode-zen-messages",
- modelId: "claude-haiku-4-5",
- apiType: "anthropic-messages",
- baseUrl: "https://opencode.ai/zen/v1",
- });
- expect(haiku.properties?.inputFormat.supportsImage).toBe(true);
- expect(haiku.properties?.contextWindow).toBe(200_000);
- } finally {
- source.dispose();
- }
- });
- it("每个 Provider 的默认 enabled 模型形成成员前缀,历史或 alias 型号默认关闭", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const exactEnabled = new Map(
- snapshot.models
- .rules()
- .flatMap((rule) =>
- rule.config.enabled === undefined ||
- (rule.type !== "provider-model" && rule.type !== "template-model")
- ? []
- : [
- [
- `${rule.type === "provider-model" ? rule.providerId : rule.templateId}\0${rule.modelId}`,
- rule.config.enabled,
- ] as const,
- ],
- ),
- );
- const memberships = [
- ...snapshot.providers.entries(),
- ...(snapshot.providerTemplates
- ?.entries()
- .map(([id, template]) => [id, template.config] as const) ?? []),
- ];
- for (const [ownerId, owner] of memberships) {
- const states = (owner.builtinModelIds ?? []).map((modelId) => {
- const enabled = exactEnabled.get(`${ownerId}\0${modelId}`);
- expect(enabled, `${ownerId}/${modelId} 必须显式声明 Built-in 默认 enabled`).toBeTypeOf(
- "boolean",
- );
- return enabled!;
- });
- const firstDisabled = states.indexOf(false);
- if (firstDisabled >= 0) {
- expect(
- states.slice(firstDisabled).every((enabled) => !enabled),
- `${ownerId} 的 enabled 成员必须在 disabled 成员之前`,
- ).toBe(true);
- }
- }
- expect(exactEnabled.get("openai\0gpt-5.6")).toBe(false);
- expect(exactEnabled.get("openai\0gpt-5.6-sol")).toBe(true);
- expect(exactEnabled.get("minimax\0MiniMax-M3")).toBe(true);
- expect(exactEnabled.get("xai\0grok-4.3")).toBe(false);
- } finally {
- source.dispose();
- }
- });
- it("按产品分组声明 Zhipu 系统 Provider 与外部主流 Provider", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- expect(snapshot.providers.keys()).toEqual([
- "account:zai-individual-coding-plan",
- "account:zai-team-coding-plan",
- "account:zai-start-plan",
- "account:bigmodel-individual-coding-plan",
- "account:bigmodel-team-coding-plan",
- "account:bigmodel-start-plan",
- "account:zai-offpeak-idle-plan",
- "account:bigmodel-offpeak-idle-plan",
- "account:zai-highspeed-card",
- "account:bigmodel-highspeed-card",
- ]);
- expect(snapshot.providerTemplates?.keys()).toEqual([
- "zai-api",
- "zai-standard-api",
- "bigmodel-api",
- "bigmodel-standard-api",
- "moonshot-kimi",
- "minimax",
- "deepseek",
- "qwen-alibaba-model-studio-cn",
- "qwen-alibaba-model-studio-intl",
- "xiaomi-mimo",
- "xiaomi-mimo-token-plan",
- "openai",
- "anthropic",
- "xai",
- "openrouter",
- "opencode-go-chat",
- "opencode-go-messages",
- "opencode-go-responses",
- "opencode-zen-responses",
- "opencode-zen-messages",
- "opencode-zen-chat",
- ]);
- for (const [templateId, template] of snapshot.providerTemplates?.entries() ?? []) {
- expect(
- template.templateNameMap["zh-CN"]?.trim(),
- `${templateId} 缺少中文 Template 名称`,
- ).toBeTruthy();
- expect(
- template.templateNameMap["en-US"]?.trim(),
- `${templateId} 缺少英文 Template 名称`,
- ).toBeTruthy();
- expect(template.config.logo, `${templateId} 缺少 Built-in Logo 引用`).toMatchObject({
- type: "builtin",
- });
- }
- for (const [providerId, provider] of snapshot.providers.entries()) {
- expect(provider.logo, `${providerId} 缺少 Built-in Logo 引用`).toMatchObject({
- type: "builtin",
- });
- }
- expect(snapshot.providerTemplates?.get("moonshot-kimi")?.templateNameMap).toEqual({
- "zh-CN": "Kimi",
- "en-US": "Kimi",
- });
- expect(
- snapshot.providerTemplates?.get("qwen-alibaba-model-studio-cn")?.templateNameMap,
- ).toEqual({
- "zh-CN": "阿里云百炼(中国)",
- "en-US": "Alibaba Cloud (China)",
- });
- expect(
- snapshot.providerTemplates?.get("qwen-alibaba-model-studio-intl")?.templateNameMap,
- ).toEqual({
- "zh-CN": "阿里云百炼(国际)",
- "en-US": "Alibaba Cloud (Global)",
- });
- expect(snapshot.providerTemplates?.get("openai")?.config.toJSON()).toMatchObject({
- builtinModelIds: [
- "gpt-6-astra",
- "gpt-5.6-sol",
- "gpt-5.6-terra",
- "gpt-5.6-luna",
- "gpt-5.6",
- "gpt-5.4",
- "gpt-5.4-pro",
- "gpt-5.4-mini",
- "gpt-5.4-nano",
- "gpt-5.3-codex",
- ],
- access: {
- type: "api-key",
- apiKeyManagementUrl: "https://platform.openai.com/api-keys",
- },
- api: { type: "openai-responses", baseUrl: "https://api.openai.com/v1" },
- });
- expect(snapshot.providerTemplates?.get("anthropic")?.config.toJSON()).toMatchObject({
- builtinModelIds: [
- "claude-fable-5-1",
- "claude-fable-5",
- "claude-opus-5",
- "claude-sonnet-5",
- "claude-haiku-4-5-20251001",
- ],
- access: {
- type: "api-key",
- apiKeyManagementUrl: "https://console.anthropic.com/settings/keys",
- },
- api: { type: "anthropic-messages", baseUrl: "https://api.anthropic.com/v1" },
- });
- expect(snapshot.providerTemplates?.get("xai")?.config.toJSON()).toMatchObject({
- builtinModelIds: ["grok-4.6", "grok-build-0.1", "grok-4.3"],
- access: {
- type: "api-key",
- apiKeyManagementUrl: "https://console.x.ai",
- },
- api: { type: "openai-responses", baseUrl: "https://api.x.ai/v1" },
- });
- expect(snapshot.providerTemplates?.get("zai-api")?.config.toJSON()).toMatchObject({
- access: { type: "zhipu-coding-plan-api-key" },
- });
- expect(snapshot.providerTemplates?.get("bigmodel-api")?.config.toJSON()).toMatchObject({
- access: { type: "zhipu-coding-plan-api-key" },
- });
- } finally {
- source.dispose();
- }
- });
- it("不保留语义等价的重复 matcher", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- // 三组 matcher 规则各自独立,但语义等价的 selector 不能跨组重复书写。
- const rules = (await source.read()).models
- .rules()
- .filter(
- (rule) =>
- rule.type === "model" || rule.type === "model-api" || rule.type === "provider-site",
- );
- const selectorKeys = rules.map((rule) =>
- JSON.stringify([
- rule.modelMatch.toLowerCase(),
- "apiTypeMatch" in rule ? (rule.apiTypeMatch ?? "") : "",
- "baseUrlMatch" in rule ? rule.baseUrlMatch : "",
- ]),
- );
- expect(new Set(selectorKeys).size).toBe(selectorKeys.length);
- } finally {
- source.dispose();
- }
- });
- it("Reasoning 公开档位按语义强度从低到高排列", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const levelRank: Readonly<Record<string, number>> = {
- disabled: 0,
- none: 0,
- nothink: 0,
- off: 0,
- enabled: 1,
- minimal: 0.5,
- low: 1,
- medium: 2,
- high: 3,
- xhigh: 4,
- max: 5,
- };
- for (const rule of (await source.read()).models.rules()) {
- const values = rule.config.optionSpecs?.reasoningLevel?.values;
- if (!values) continue;
- const ranks = values.map((value) => levelRank[value]);
- expect(
- ranks.every((rank) => rank !== undefined),
- `${rule.modelMatch} 存在未知 Reasoning 档位,需先声明其顺序语义`,
- ).toBe(true);
- expect(
- ranks.every((rank, index) => index === 0 || rank! >= ranks[index - 1]!),
- `${rule.modelMatch} 的 Reasoning 档位必须从低到高排列`,
- ).toBe(true);
- }
- } finally {
- source.dispose();
- }
- });
- it("解析 OpenAI Responses 与 Anthropic Messages 的模型事实和 reasoning 映射", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const openAi = snapshot.models.resolve({
- providerId: "openai",
- modelId: "gpt-5.6-sol",
- apiType: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- });
- expect(openAi.properties?.toJSON()).toMatchObject({
- requiresMfjsToolSchema: false,
- contextWindow: 1_050_000,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: true,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- });
- expect(openAi.optionSpecs?.reasoningLevel).toMatchObject({
- values: ["none", "low", "medium", "high", "xhigh", "max"],
- });
- for (const effort of ["xhigh", "max"]) {
- expect(reasoningPatch(openAi.optionSpecs!.reasoningLevel!.map!, effort)).toEqual({
- reasoning: { effort },
- });
- }
- const grok46 = snapshot.models.resolve({
- providerId: "xai",
- modelId: "grok-4.6",
- apiType: "openai-responses",
- baseUrl: "https://api.x.ai/v1",
- });
- expect(grok46.properties?.toJSON()).toMatchObject({
- requiresMfjsToolSchema: false,
- contextWindow: 500_000,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- });
- expect(grok46.optionSpecs?.reasoningLevel).toMatchObject({
- values: ["low", "medium", "high", "xhigh"],
- });
- expect(reasoningPatch(grok46.optionSpecs!.reasoningLevel!.map!, "xhigh")).toEqual({
- reasoning: { effort: "xhigh" },
- });
- const grokBuild = snapshot.models.resolve({
- providerId: "xai",
- modelId: "grok-build-0.1",
- apiType: "openai-responses",
- baseUrl: "https://api.x.ai/v1",
- });
- expect(grokBuild.properties?.toJSON()).toMatchObject({
- requiresMfjsToolSchema: false,
- contextWindow: 256_000,
- inputFormat: { supportsText: true, supportsImage: true },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- });
- const grok43 = snapshot.models.resolve({
- providerId: "xai",
- modelId: "grok-4.3",
- apiType: "openai-responses",
- baseUrl: "https://api.x.ai/v1",
- });
- expect(grok43.properties?.contextWindow).toBe(1_000_000);
- expect(grok43.optionSpecs?.reasoningLevel?.toJSON()).toMatchObject({
- // 此模型未声明专用档位,继续继承 Model 通用兜底。
- values: ["disabled", "enabled"],
- });
- expect(reasoningPatch(grok43.optionSpecs!.reasoningLevel!.map!, "disabled")).toEqual({
- reasoning: { effort: "none" },
- });
- const anthropic = snapshot.models.resolve({
- providerId: "anthropic",
- modelId: "claude-opus-5",
- apiType: "anthropic-messages",
- baseUrl: "https://api.anthropic.com/v1",
- });
- expect(anthropic.properties?.toJSON()).toMatchObject({
- requiresMfjsToolSchema: false,
- contextWindow: 1_000_000,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: true,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: true,
- supportsMidConversationSystem: true,
- });
- expect(anthropic.optionSpecs?.reasoningLevel).toMatchObject({
- values: ["low", "medium", "high", "xhigh", "max"],
- });
- expect(anthropic.optionSpecs?.reasoningLevel?.map).toContain("reasoningLevel");
- expect(reasoningPatch(anthropic.optionSpecs!.reasoningLevel!.map!, "max")).toMatchObject({
- output_config: { effort: "max" },
- });
- const deepSeek = snapshot.models.resolve({
- providerId: "deepseek",
- modelId: "deepseek-v4-flash",
- apiType: "anthropic-messages",
- baseUrl: "https://api.deepseek.com/anthropic",
- });
- expect(deepSeek.properties?.supportsJsonSchemaOutput).toBe(false);
- expect(deepSeek.optionSpecs?.reasoningLevel).toMatchObject({
- values: ["disabled", "low", "high", "max"],
- });
- expect(deepSeek.optionSpecs?.reasoningLevel?.map).toContain("reasoningLevel");
- expect(reasoningPatch(deepSeek.optionSpecs!.reasoningLevel!.map!, "low")).toMatchObject({
- output_config: { effort: "low" },
- });
- for (const modelId of ["qwen3.5-plus", "qwen3.5-flash", "qwen3-vl-plus"]) {
- expect(
- snapshot.models.resolve({
- providerId: "qwen-alibaba-model-studio-cn",
- modelId,
- apiType: "anthropic-messages",
- baseUrl: "https://dashscope.aliyuncs.com/apps/anthropic",
- }).properties?.supportsJsonSchemaOutput,
- ).toBe(false);
- }
- const deepSeekResponses = snapshot.models.resolve({
- providerId: "deepseek",
- modelId: "deepseek-v4-pro",
- apiType: "openai-responses",
- baseUrl: "https://api.deepseek.com",
- });
- expect(deepSeekResponses.properties?.supportsJsonSchemaOutput).toBe(true);
- expect(reasoningPatch(deepSeekResponses.optionSpecs!.reasoningLevel!.map!, "max")).toEqual({
- reasoning: { effort: "max" },
- });
- } finally {
- source.dispose();
- }
- });
- it("每个 Built-in Provider 与模型成员都完整,启用成员在补齐访问材料后进入正式 Registry", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const accountProviders: [string, ProviderConfig][] = [];
- const personalProviders: Array<{
- providerId: string;
- templateId: string;
- config: ProviderConfig;
- }> = [];
- const expectedModelIds = new Map<string, readonly string[]>();
- for (const [providerId, provider] of snapshot.providers.entries()) {
- const builtinModelIds = provider.builtinModelIds ?? [];
- expect(new Set(builtinModelIds).size, `${providerId} 存在重复 Built-in 模型成员`).toBe(
- builtinModelIds.length,
- );
- const enabledModelIds = builtinModelIds.filter((modelId) => {
- const model = snapshot.models.resolve({
- providerId,
- modelId,
- apiType: provider.api?.type,
- baseUrl: provider.api?.baseUrl,
- });
- expect(model.validateComplete(["providers", providerId, "models", modelId])).toEqual([]);
- return model.enabled !== false;
- });
- expectedModelIds.set(providerId, enabledModelIds);
- if (provider.access?.type === "zhipu-account") {
- accountProviders.push([
- providerId,
- new ProviderConfig({
- access: new ZhipuAccountAccessConfig({ entitled: true }),
- }),
- ]);
- }
- }
- for (const [templateId, template] of snapshot.providerTemplates?.entries() ?? []) {
- const providerId = `template:${templateId}`;
- const builtinModelIds = template.config.builtinModelIds ?? [];
- expect(new Set(builtinModelIds).size, `${templateId} 存在重复 Template 模型成员`).toBe(
- builtinModelIds.length,
- );
- const enabledModelIds = builtinModelIds.filter((modelId) => {
- const model = snapshot.models.resolve({
- providerId,
- templateId,
- modelId,
- apiType: template.config.api?.type,
- baseUrl: template.config.api?.baseUrl,
- });
- expect(model.validateComplete(["templates", templateId, "models", modelId])).toEqual([]);
- return model.enabled !== false;
- });
- expectedModelIds.set(providerId, enabledModelIds);
- expect(["api-key", "zhipu-coding-plan-api-key"]).toContain(template.config.access?.type);
- if (template.config.access?.type !== "zhipu-account") {
- expect(template.config.access.apiKey, `${templateId} 不得保存 API Key`).toBeUndefined();
- }
- personalProviders.push({
- providerId,
- templateId,
- config: new ProviderConfig({
- group: "standard-personal",
- access: new ApiKeyAccessConfig({
- type: template.config.access?.type,
- apiKey: `test-key-for-${templateId}`,
- }),
- }),
- });
- }
- const resolution = new ProviderConfigResolver().resolve({
- zcodeBuiltinProviders: snapshot.providers,
- zcodeBuiltinProviderTemplates: snapshot.providerTemplates,
- accountProviders: new ProviderConfigMap(accountProviders),
- personalProviders: new ProviderConfigMap(personalProviders),
- zcodeBuiltinModelRules: snapshot.models,
- personalModels: ModelConfigRules.empty(),
- });
- expect(resolution.issues).toEqual([]);
- expect(resolution.registryProviders.map(({ providerId }) => providerId)).toEqual([
- "account:zai-individual-coding-plan",
- "account:zai-team-coding-plan",
- "account:zai-start-plan",
- "account:bigmodel-individual-coding-plan",
- "account:bigmodel-team-coding-plan",
- "account:bigmodel-start-plan",
- "account:zai-offpeak-idle-plan",
- "account:bigmodel-offpeak-idle-plan",
- "account:zai-highspeed-card",
- "account:bigmodel-highspeed-card",
- "template:zai-api",
- "template:zai-standard-api",
- "template:bigmodel-api",
- "template:bigmodel-standard-api",
- "template:moonshot-kimi",
- "template:minimax",
- "template:deepseek",
- "template:qwen-alibaba-model-studio-cn",
- "template:qwen-alibaba-model-studio-intl",
- "template:xiaomi-mimo",
- "template:xiaomi-mimo-token-plan",
- "template:openai",
- "template:anthropic",
- "template:xai",
- "template:openrouter",
- "template:opencode-go-chat",
- "template:opencode-go-messages",
- "template:opencode-go-responses",
- "template:opencode-zen-responses",
- "template:opencode-zen-messages",
- "template:opencode-zen-chat",
- ]);
- for (const provider of resolution.registryProviders) {
- expect(
- provider.models.map(({ modelId }) => modelId),
- `${provider.providerId} 的 Registry 模型成员必须与 Built-in Config 一致`,
- ).toEqual(expectedModelIds.get(provider.providerId));
- for (const model of provider.models) {
- expect(
- model.config.validateComplete([
- "providers",
- provider.providerId,
- "models",
- model.modelId,
- ]),
- ).toEqual([]);
- }
- }
- for (const providerId of [
- "account:zai-offpeak-idle-plan",
- "account:bigmodel-offpeak-idle-plan",
- ]) {
- const offPeak = resolution.resolvedProviders.find(
- (provider) => provider.providerId === providerId,
- );
- expect(offPeak?.models).not.toHaveLength(0);
- for (const model of offPeak?.models ?? []) {
- expect(model).toMatchObject({ executable: true, selectable: false });
- }
- }
- } finally {
- source.dispose();
- }
- });
- it("完整兜底让任意未知模型 ID 在各 API type 与模板下完整并进入 Registry(R3A-B-03)", async () => {
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinProviderConfigFilePath,
- watch: false,
- });
- try {
- const snapshot = await source.read();
- const fallbackRule = snapshot.models
- .rules()
- .find((rule) => rule.type === "model" && rule.modelMatch === ".*");
- const fallbackMax = fallbackRule?.config.optionSpecs?.maxOutputTokens?.max;
- expect(fallbackMax, "兜底规则必须声明 maxOutputTokens.max").toEqual(expect.any(Number));
- const unknownModelId = "b03-unknown-model-x";
- const scenarios = [
- { providerId: "custom-chat", apiType: "openai-chat-completions" as const },
- { providerId: "custom-messages", apiType: "anthropic-messages" as const },
- { providerId: "custom-responses", apiType: "openai-responses" as const },
- {
- providerId: "custom-deepseek",
- templateId: "deepseek",
- apiType: "openai-chat-completions" as const,
- baseURL: "https://api.deepseek.com",
- // 只能用目录默认启用的已知模型做探针:deepseek-v4-flash 已被 Built-in 目录标为
- // enabled:false,稀疏 Personal 规则不会覆盖它,Registry 里自然取不到。
- knownModelId: "deepseek-v4-pro",
- },
- {
- providerId: "custom-zai",
- templateId: "zai-api",
- apiType: "anthropic-messages" as const,
- baseURL: "https://api.z.ai/api/anthropic",
- knownModelId: "glm-5.3",
- },
- ];
- const personalProviders = scenarios.map((scenario) => ({
- providerId: scenario.providerId,
- ...(scenario.templateId ? { templateId: scenario.templateId } : {}),
- config: new ProviderConfig({
- group: "standard-personal",
- access: new ApiKeyAccessConfig({ apiKey: `test-key-for-${scenario.providerId}` }),
- api: new ProviderApiConfig({
- type: scenario.apiType,
- baseUrl: scenario.baseURL ?? "https://custom.example.com/v1",
- }),
- personalModelIds: [
- unknownModelId,
- ...(scenario.knownModelId ? [scenario.knownModelId] : []),
- ],
- }),
- }));
- // 设置页新增模型时 config-service 只写入 {enabled:true},这里复用同一稀疏 Personal 规则。
- const personalModels = new ModelConfigRules(
- scenarios.map((scenario) => ({
- type: "provider-model" as const,
- providerId: scenario.providerId,
- modelId: unknownModelId,
- config: parseModelConfig({ enabled: true }),
- })),
- );
- const resolution = new ProviderConfigResolver().resolve({
- zcodeBuiltinProviders: ProviderConfigMap.empty(),
- zcodeBuiltinProviderTemplates: snapshot.providerTemplates,
- accountProviders: ProviderConfigMap.empty(),
- personalProviders: new ProviderConfigMap(personalProviders),
- zcodeBuiltinModelRules: snapshot.models,
- personalModels,
- });
- const registry = new ProviderRegistry(resolution.registryProviders);
- expect(resolution.issues).toEqual([]);
- for (const scenario of scenarios) {
- const unknown = registry.getModel(scenario.providerId, unknownModelId);
- expect(unknown, `${scenario.providerId}/${unknownModelId} 应进入 Registry`).toBeDefined();
- expect(unknown?.config.optionSpecs.maxOutputTokens).toEqual({
- max: fallbackMax,
- map: expect.any(String),
- });
- if (scenario.knownModelId) {
- const known = registry.getModel(scenario.providerId, scenario.knownModelId);
- expect(
- known,
- `${scenario.providerId}/${scenario.knownModelId} 应进入 Registry`,
- ).toBeDefined();
- expect(known?.config.optionSpecs.maxOutputTokens.max).toEqual(expect.any(Number));
- // 新通用上限可与已知型号一致;已知型号仍由自身规则解析,不能以数值不同判断是否命中。
- expect(known?.config.optionSpecs.reasoningLevel.values).not.toEqual([
- "disabled",
- "enabled",
- ]);
- }
- }
- } finally {
- source.dispose();
- }
- });
- it("正式 Built-in JSON 不携带旧模型能力字段", async () => {
- const document = JSON.parse(
- await readFile(zcodeBuiltinProviderConfigFilePath, "utf8"),
- ) as unknown;
- const keys = collectObjectKeys(document);
- expect(keys).not.toContain("apiKey");
- expect(keys).not.toContain("modalities");
- expect(keys).not.toContain("magic_name");
- expect(keys).not.toContain("supportsImages");
- // Todo104:supportsPdf/Video 是 inputFormat 内的正式字段,不能因同名误判成旧扁平能力。
- for (const key of [
- "input_format",
- "output_format",
- "support_text",
- "support_image",
- "support_pdf",
- "support_video",
- "support_audio",
- ]) {
- expect(keys).not.toContain(key);
- }
- expect(keys).not.toContain("budget_tokens");
- expect(keys).not.toContain("budgetTokens");
- });
- });
- function collectObjectKeys(value: unknown): string[] {
- if (Array.isArray(value)) return value.flatMap(collectObjectKeys);
- if (!value || typeof value !== "object") return [];
- return Object.entries(value).flatMap(([key, child]) => [key, ...collectObjectKeys(child)]);
- }
|