| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832 |
- import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { fileURLToPath } from "node:url";
- import {
- ProviderConfig,
- ProviderConfigMap,
- ZhipuAccountAccessConfig,
- createAccountProviderConfigSnapshot,
- } from "@zcode/provider";
- import { zcodeProviderUpdateAccountConfigParamsSchema } from "@zcode/shared";
- import {
- ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV,
- ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV,
- ZCODE_BUILTIN_RELEASE_SCHEMA_VERSION,
- createNodeModelSelectionFacade,
- } from "@zcode/provider-node";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- createSharedZCodeCredentialStore,
- type SharedZCodeCredentialStore,
- } from "@zcode/adapters/auth";
- import {
- parseProcessAccountProviderConfigSnapshot,
- startProcessProviderRegistryRuntime,
- } from "../src/app/process-provider-registry-runtime.js";
- import {
- standaloneAccountAuthSourceCredentialKey,
- standaloneAccountIdentityCredentialKey,
- standaloneAccountProviderCredentialKey,
- } from "../src/app/standalone-account-provider-runtime.js";
- const temporaryDirectories: string[] = [];
- afterEach(async () => {
- await Promise.all(
- temporaryDirectories
- .splice(0)
- .map((directory) => rm(directory, { force: true, recursive: true })),
- );
- });
- describe("process provider registry runtime", () => {
- it("Standalone 长生命周期下载 URL Release 后跟进 Registry;Managed Worker 不下载", async () => {
- const root = await temporaryDirectory();
- const builtin = JSON.parse(
- await readFile(
- new URL("../../../../../config/provider/zcode-builtin.json", import.meta.url),
- "utf8",
- ),
- );
- const bundled = join(root, "bundled.json");
- const active = join(root, "active.json");
- await writeFile(bundled, JSON.stringify(builtin));
- await writeFile(active, JSON.stringify(builtin));
- builtin.revision += 1;
- let releaseDownload!: () => void;
- const wait = new Promise<void>((resolve) => {
- releaseDownload = resolve;
- });
- const request = vi.fn(async (url: string | URL | Request, init?: RequestInit) => {
- expect(init?.headers).toBeUndefined();
- if (String(url).includes("/api/v1/client/configs?"))
- return new Response(
- JSON.stringify({
- code: 0,
- data: { configs: { builtin_provider_config_json: "https://cdn.example.com/new.json" } },
- }),
- );
- await wait;
- return new Response(JSON.stringify(builtin));
- });
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_DATA_BASE_DIR: root },
- });
- const env = {
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: active,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: join(root, "personal.json"),
- ZCODE_BUILTIN_PROVIDER_BUNDLED_CONFIG_FILE: bundled,
- };
- const runtime = await startProcessProviderRegistryRuntime(env, {
- standalone: {
- credentialStore,
- request,
- legacyCliUserConfigFilePath: join(root, "absent.json"),
- },
- });
- const old = runtime.runtime.registryService.getSnapshot()!;
- try {
- await vi.waitFor(() => expect(request).toHaveBeenCalledTimes(2));
- expect(runtime.runtime.registryService.getSnapshot()).toBe(old);
- releaseDownload();
- await vi.waitFor(() =>
- expect(runtime.runtime.registryService.getSnapshot()!.config.zcodeBuiltinRevision).toMatch(
- `zcode-builtin:${builtin.revision}:`,
- ),
- );
- const next = runtime.runtime.registryService.getSnapshot()!;
- expect(next.account.basedOnZCodeBuiltinRevision).toBe(next.config.zcodeBuiltinRevision);
- expect(JSON.parse(await readFile(active, "utf8")).revision).toBe(builtin.revision);
- expect(JSON.parse(await readFile(bundled, "utf8")).revision).toBe(builtin.revision - 1);
- const worker = await startProcessProviderRegistryRuntime(env);
- try {
- expect(worker.snapshot.config.zcodeBuiltinRevision).toBe(next.config.zcodeBuiltinRevision);
- expect(request).toHaveBeenCalledTimes(2);
- } finally {
- worker.dispose();
- }
- } finally {
- releaseDownload();
- runtime.dispose();
- }
- });
- it("Standalone 无凭据时 Built-in 文件更新仍重新解析 Account 并整体发布 Registry", async () => {
- const root = await temporaryDirectory();
- const builtin = JSON.parse(
- await readFile(
- new URL("../../../../../config/provider/zcode-builtin.json", import.meta.url),
- "utf8",
- ),
- );
- const filePath = join(root, "builtin.json");
- await writeFile(filePath, JSON.stringify(builtin));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_DATA_BASE_DIR: root },
- });
- const runtime = await startProcessProviderRegistryRuntime(
- {
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: filePath,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: join(root, "personal.json"),
- },
- { standalone: { credentialStore, legacyCliUserConfigFilePath: join(root, "absent.json") } },
- );
- try {
- builtin.revision += 1;
- await writeFile(filePath, JSON.stringify(builtin));
- await vi.waitFor(() => {
- const next = runtime.runtime.registryService.getSnapshot()!;
- expect(next.config.zcodeBuiltinRevision).toMatch(`zcode-builtin:${builtin.revision}:`);
- expect(next.account.basedOnZCodeBuiltinRevision).toBe(next.config.zcodeBuiltinRevision);
- });
- expect(
- runtime.runtime.registryService.getProvider("account:bigmodel-individual-coding-plan"),
- ).toBeUndefined();
- } finally {
- runtime.dispose();
- }
- });
- it("真实 Built-in 下完整状态跨协议往返;仅切 current 即更新 Worker 的有效选择", async () => {
- const root = await temporaryDirectory();
- const runtime = await startProcessProviderRegistryRuntime({
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: fileURLToPath(
- new URL("../../../../../config/provider/zcode-builtin.json", import.meta.url),
- ),
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: join(root, "personal.json"),
- });
- const personal = "account:bigmodel-individual-coding-plan";
- const team = "account:bigmodel-team-coding-plan";
- const original = Object.freeze({
- providerId: personal,
- modelId: "GLM-5.3",
- options: Object.freeze({ reasoningLevel: "high" }),
- });
- const facade = createNodeModelSelectionFacade(runtime.runtime.registryService);
- const providers = new ProviderConfigMap(
- [personal, team].map((id) => [
- id,
- new ProviderConfig({ access: new ZhipuAccountAccessConfig({ entitled: true }) }),
- ]),
- );
- const revisions = new Set<string>();
- try {
- for (const current of [personal, team, personal]) {
- const snapshot = createAccountProviderConfigSnapshot(
- runtime.snapshot.config.zcodeBuiltinRevision,
- providers,
- {
- [personal]: {
- availability: "available",
- entitled: true,
- current: current === personal,
- },
- [team]: { availability: "available", entitled: true, current: current === team },
- },
- );
- revisions.add(snapshot.revision);
- const wire = zcodeProviderUpdateAccountConfigParamsSchema.parse(
- JSON.parse(
- JSON.stringify({
- ...snapshot,
- providers: Object.fromEntries(
- [...snapshot.providers.entries()].map(([id, config]) => [id, config.toJSON()]),
- ),
- }),
- ),
- );
- await runtime.syncAccountProviderConfig(parseProcessAccountProviderConfigSnapshot(wire));
- expect(runtime.runtime.registryService.getProvider(current)).toBeDefined();
- expect(
- runtime.runtime.registryService.getProvider(current === personal ? team : personal),
- ).toBeUndefined();
- expect(
- facade.getView(undefined, undefined, { selection: original }).effectiveSelection,
- ).toEqual({ ...original, providerId: current });
- expect(original.providerId).toBe(personal);
- }
- expect(revisions.size).toBe(2);
- } finally {
- runtime.dispose();
- }
- });
- it("解析独立同步的 Account Provider Config", () => {
- const states = {
- account: { availability: "available", entitled: true, current: false },
- } as const;
- const parsed = parseProcessAccountProviderConfigSnapshot({
- revision: "account-1",
- basedOnZCodeBuiltinRevision: "zcode-builtin:1",
- states,
- providers: {
- account: {
- access: { type: "zhipu-account", entitled: true },
- builtinModelIds: ["model-a"],
- },
- },
- });
- expect(
- Object.fromEntries(
- [...parsed.providers.entries()].map(([id, config]) => [id, config.toJSON()]),
- ),
- ).toEqual({
- account: {
- access: { type: "zhipu-account", entitled: true },
- builtinModelIds: ["model-a"],
- },
- });
- expect(parsed.basedOnZCodeBuiltinRevision).toBe("zcode-builtin:1");
- expect(parsed.states).toEqual(states);
- });
- it("独立 Account Config 同步拒绝 API Provider", () => {
- expect(() =>
- parseProcessAccountProviderConfigSnapshot({
- revision: "account-invalid",
- basedOnZCodeBuiltinRevision: "zcode-builtin:1",
- providers: {
- api: {
- access: {
- type: "api-key",
- apiKey: "must-not-enter-account-source",
- },
- },
- },
- }),
- ).toThrow();
- });
- it("独立 Account Config 只接受 entitlement 与账号特定模型成员", () => {
- expect(() =>
- parseProcessAccountProviderConfigSnapshot({
- revision: "account-invalid-static-field",
- basedOnZCodeBuiltinRevision: "zcode-builtin:1",
- providers: {
- account: {
- access: { type: "zhipu-account", entitled: true },
- label: "不得由 Account Source 覆盖",
- },
- },
- }),
- ).toThrow();
- });
- it("未注入 Provider Config 路径时拒绝启动", async () => {
- await expect(startProcessProviderRegistryRuntime({})).rejects.toThrow(
- "缺少进程 Provider Registry",
- );
- });
- it("Personal 文件损坏时保留原文件并以内存空配置启动", async () => {
- const root = await temporaryDirectory();
- const zcodeBuiltinFilePath = join(root, "zcode-builtin.json");
- const personalFilePath = join(root, "personal.json");
- await writeFile(
- zcodeBuiltinFilePath,
- JSON.stringify(zcodeBuiltinRelease({ providers: {}, templates: {}, models: [] })),
- );
- await writeFile(personalFilePath, "{not-json");
- const started = await startProcessProviderRegistryRuntime({
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: zcodeBuiltinFilePath,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: personalFilePath,
- });
- try {
- expect(started.configuredDefaultModelSelection).toBeUndefined();
- expect(await readFile(personalFilePath, "utf8")).toBe("{not-json");
- await expect(
- started.modelSelectionConfigRepository.saveConfiguredDefault(undefined),
- ).rejects.toThrow();
- } finally {
- started.dispose();
- }
- await expect(started.modelSelectionConfigRepository.read()).rejects.toThrow("已 dispose");
- await expect(started.runtime.start()).rejects.toThrow("已 dispose");
- });
- it("从进程环境启动一份 Registry,并由调用方持有生命周期", async () => {
- const root = await temporaryDirectory();
- const zcodeBuiltinFilePath = join(root, "zcode-builtin.json");
- const personalFilePath = join(root, "personal.json");
- await writeFile(
- zcodeBuiltinFilePath,
- JSON.stringify(
- zcodeBuiltinRelease({
- providers: {
- account: {
- group: "zai-family",
- access: {
- type: "zhipu-account",
- accountType: "zai",
- mode: "individual-coding-plan",
- },
- api: {
- type: "anthropic-messages",
- baseUrl: "https://account.example.com",
- },
- builtinModelIds: ["model-a"],
- },
- },
- templates: {
- "zcode-template": {
- access: { type: "api-key" },
- api: {
- type: "anthropic-messages",
- baseUrl: "https://api.example.com",
- },
- builtinModelIds: ["model-a"],
- },
- },
- models: [
- {
- modelMatch: "model-a",
- config: {
- enabled: true,
- properties: {
- requiresMfjsToolSchema: false,
- contextWindow: 200000,
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- },
- optionSpecs: {
- reasoningLevel: { values: ["disabled"], map: "{}" },
- maxOutputTokens: {
- max: 32000,
- map: '{"max_completion_tokens":maxOutputTokens}',
- },
- },
- },
- },
- ],
- }),
- ),
- "utf8",
- );
- await writeFile(
- personalFilePath,
- JSON.stringify(
- personalRelease([
- {
- providerId: "personal-api",
- templateId: "zcode-template",
- config: {
- group: "standard-personal",
- access: { type: "api-key", apiKey: "secret" },
- },
- },
- ]),
- ),
- "utf8",
- );
- const started = await startProcessProviderRegistryRuntime({
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: zcodeBuiltinFilePath,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: personalFilePath,
- });
- expect(started?.snapshot.registry.providers).toHaveLength(1);
- expect(started?.runtime.registryService.getModel("personal-api", "model-a")).toBeDefined();
- started?.accountSource.replace(
- {
- revision: "account-access-1",
- basedOnZCodeBuiltinRevision: started!.snapshot.config.zcodeBuiltinRevision,
- providers: new ProviderConfigMap([
- [
- "account",
- new ProviderConfig({
- access: new ZhipuAccountAccessConfig({ entitled: true }),
- }),
- ],
- ]),
- },
- "test",
- );
- await started?.runtime.registryService.refresh("wait-for-account-access");
- expect(started?.runtime.registryService.getProvider("account")?.config).toMatchObject({
- access: {
- type: "zhipu-account",
- },
- builtinModelIds: ["model-a"],
- });
- const beforeSwitch = await started.accountSource.read();
- const switched = parseProcessAccountProviderConfigSnapshot({
- revision: "account-access-2",
- basedOnZCodeBuiltinRevision: beforeSwitch.basedOnZCodeBuiltinRevision,
- providers: Object.fromEntries(
- [...beforeSwitch.providers.entries()].map(([id, config]) => [id, config.toJSON()]),
- ),
- states: { account: { availability: "available", entitled: true, current: false } },
- });
- started.accountSource.replace(switched, "account-no-longer-current");
- await started.runtime.registryService.refresh("wait-for-account-selection");
- expect(started.runtime.registryService.getProvider("account")).toBeUndefined();
- expect(started.runtime.registryService.getProvider("personal-api")).toBeDefined();
- started?.dispose();
- });
- it("Standalone 从账号凭据生成 Account Overlay,并在请求前返回当前 API Key", async () => {
- const root = await temporaryDirectory();
- const zcodeBuiltinFilePath = join(root, "zcode-builtin.json");
- const personalFilePath = join(root, "personal.json");
- const credentialStore = createSharedZCodeCredentialStore({
- baseDir: join(root, "home"),
- env: { ZCODE_CREDENTIAL_SECRET: "process-registry-test" },
- });
- const providerId = "configured-standalone-provider";
- const accountIdentity = "account-1";
- const accountProviderCredentialKey = standaloneAccountProviderCredentialKey({
- providerId,
- accountIdentity,
- });
- await writeFile(
- zcodeBuiltinFilePath,
- JSON.stringify(
- zcodeBuiltinRelease({
- providers: {
- [providerId]: {
- group: "zai-family",
- access: {
- type: "zhipu-account",
- accountType: "zai",
- mode: "individual-coding-plan",
- },
- api: {
- type: "anthropic-messages",
- baseUrl: "https://account.example.com",
- },
- builtinModelIds: ["model-a"],
- },
- },
- templates: {},
- models: [
- {
- modelMatch: "model-a",
- config: {
- enabled: true,
- properties: {
- requiresMfjsToolSchema: false,
- contextWindow: 200000,
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- },
- optionSpecs: {
- reasoningLevel: { values: ["disabled"], map: "{}" },
- maxOutputTokens: {
- max: 32000,
- map: '{"max_completion_tokens":maxOutputTokens}',
- },
- },
- },
- },
- ],
- }),
- ),
- "utf8",
- );
- await writeFile(personalFilePath, JSON.stringify(personalRelease([])), "utf8");
- await credentialStore.saveMany({
- [standaloneAccountIdentityCredentialKey(providerId)]: accountIdentity,
- [accountProviderCredentialKey]: "runtime-api-key",
- [standaloneAccountAuthSourceCredentialKey(providerId)]: "manual",
- });
- const started = await startProcessProviderRegistryRuntime(
- {
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: zcodeBuiltinFilePath,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: personalFilePath,
- },
- { standalone: { credentialStore } },
- );
- expect(started?.runtime.registryService.getProvider(providerId)?.config.access).toMatchObject({
- type: "zhipu-account",
- accountType: "zai",
- mode: "individual-coding-plan",
- });
- await expect(
- started?.providerRuntimeHeadersPort.refreshBeforeModelRequest({
- accountAccess: {
- type: "zhipu-account",
- accountType: "zai",
- entitled: true,
- mode: "individual-coding-plan",
- },
- modelId: "model-a",
- providerId,
- reason: "model-request",
- sessionId: "session-1" as never,
- traceContext: { traceId: "trace-1" } as never,
- }),
- ).resolves.toMatchObject({
- headersApplied: true,
- requestAuth: { apiKey: "runtime-api-key" },
- });
- await credentialStore.save(accountProviderCredentialKey, "rotated-runtime-api-key");
- await expect(
- started?.providerRuntimeHeadersPort.refreshBeforeModelRequest({
- accountAccess: {
- type: "zhipu-account",
- accountType: "zai",
- entitled: true,
- mode: "individual-coding-plan",
- },
- modelId: "model-a",
- providerId,
- reason: "model-request",
- sessionId: "session-1" as never,
- traceContext: { traceId: "trace-2" } as never,
- }),
- ).resolves.toMatchObject({
- requestAuth: { apiKey: "rotated-runtime-api-key" },
- });
- await credentialStore.deleteIfValues({
- [standaloneAccountIdentityCredentialKey(providerId)]: accountIdentity,
- [accountProviderCredentialKey]: "rotated-runtime-api-key",
- });
- await vi.waitFor(() => {
- expect(started?.runtime.registryService.getProvider(providerId)).toBeUndefined();
- });
- started?.dispose();
- });
- it("Standalone 首次账号凭据读取失败时仍以 fail-closed Account 启动 Personal API Provider", async () => {
- const root = await temporaryDirectory();
- const zcodeBuiltinFilePath = join(root, "zcode-builtin.json");
- const personalFilePath = join(root, "personal.json");
- await writeFile(
- zcodeBuiltinFilePath,
- JSON.stringify(
- zcodeBuiltinRelease({
- providers: {},
- templates: {
- api: {
- access: { type: "api-key" },
- api: { type: "anthropic-messages", baseUrl: "https://api.example.com" },
- builtinModelIds: ["model-a"],
- },
- },
- models: [completeZCodeBuiltinModelRule()],
- }),
- ),
- "utf8",
- );
- await writeFile(
- personalFilePath,
- JSON.stringify(
- personalRelease([
- {
- providerId: "api",
- templateId: "api",
- config: {
- group: "standard-personal",
- access: { type: "api-key", apiKey: "secret" },
- },
- },
- ]),
- ),
- "utf8",
- );
- const accountError = new Error("credential store unavailable");
- const onAccountInitializationError = vi.fn();
- const credentialStore = {
- loadMany: vi.fn(async () => {
- throw accountError;
- }),
- } as unknown as SharedZCodeCredentialStore;
- const started = await startProcessProviderRegistryRuntime(
- {
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: zcodeBuiltinFilePath,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: personalFilePath,
- },
- { standalone: { credentialStore, onAccountInitializationError } },
- );
- expect(started.runtime.registryService.getProvider("api")).toBeDefined();
- expect(onAccountInitializationError).toHaveBeenCalledWith(accountError);
- started.dispose();
- });
- it("只有显式开启 Standalone 导入时才迁移旧 CLI 用户 Provider", async () => {
- const root = await temporaryDirectory();
- const zcodeBuiltinFilePath = join(root, "zcode-builtin.json");
- const personalFilePath = join(root, "personal.json");
- const legacyFilePath = join(root, "legacy-cli.json");
- await writeFile(
- zcodeBuiltinFilePath,
- JSON.stringify(
- zcodeBuiltinRelease({
- providers: {},
- templates: {},
- // Personal Provider 仍由 Built-in 通用规则补齐静态模型事实;迁移器只搬运旧文件显式字段。
- models: [completeZCodeBuiltinModelRule()],
- }),
- ),
- "utf8",
- );
- await writeFile(
- legacyFilePath,
- JSON.stringify({
- model: "custom/model-a",
- provider: {
- custom: {
- kind: "anthropic",
- options: {
- apiKey: "secret",
- baseURL: "https://custom.example.com",
- },
- models: {
- "model-a": {
- limit: { context: 64_000, output: 8_000 },
- },
- },
- },
- },
- }),
- "utf8",
- );
- const env = {
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: zcodeBuiltinFilePath,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: personalFilePath,
- };
- // Protocol 启动会创建自己的空目标文件;若与 Standalone 共用目标,就会提前消费
- // “仅目标不存在时导入”的机会,无法再证明 Standalone importer 的独立边界。
- const protocolRuntime = await startProcessProviderRegistryRuntime({
- ...env,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: join(root, "protocol-personal.json"),
- });
- expect(protocolRuntime?.snapshot.registry.providers).toEqual([]);
- expect(protocolRuntime?.configuredDefaultModelSelection).toBeUndefined();
- protocolRuntime?.dispose();
- const standaloneRuntime = await startProcessProviderRegistryRuntime(env, {
- standalone: { legacyCliUserConfigFilePath: legacyFilePath },
- });
- expect(standaloneRuntime?.snapshot.registry.providers.map((entry) => entry.providerId)).toEqual(
- ["custom"],
- );
- expect(JSON.parse(await readFile(personalFilePath, "utf8"))).toMatchObject({
- schemaVersion: 1,
- config: {
- providerConfigRules: {
- providerRules: [
- { providerId: "custom", config: { access: { type: "api-key", apiKey: "secret" } } },
- ],
- },
- },
- });
- expect(standaloneRuntime?.configuredDefaultModelSelection).toEqual({
- providerId: "custom",
- modelId: "model-a",
- });
- expect(
- JSON.parse(await readFile(personalFilePath, "utf8")).config.defaultModelSelection,
- ).toEqual({
- providerId: "custom",
- modelId: "model-a",
- });
- standaloneRuntime?.dispose();
- const protocolRestart = await startProcessProviderRegistryRuntime(env);
- expect(protocolRestart?.configuredDefaultModelSelection).toEqual({
- providerId: "custom",
- modelId: "model-a",
- });
- protocolRestart?.dispose();
- await writeFile(
- legacyFilePath,
- JSON.stringify({
- provider: {
- replacement: {
- kind: "anthropic",
- options: {
- apiKey: "replacement-secret",
- baseURL: "https://replacement.example.com",
- },
- models: {
- "model-b": { limit: { context: 32_000, output: 4_000 } },
- },
- },
- },
- }),
- "utf8",
- );
- const restarted = await startProcessProviderRegistryRuntime(env, {
- standalone: { legacyCliUserConfigFilePath: legacyFilePath },
- });
- expect(restarted?.snapshot.registry.providers.map((entry) => entry.providerId)).toEqual([
- "custom",
- ]);
- expect(restarted?.configuredDefaultModelSelection).toEqual({
- providerId: "custom",
- modelId: "model-a",
- });
- restarted?.dispose();
- });
- });
- async function temporaryDirectory(): Promise<string> {
- const directory = await mkdtemp(join(tmpdir(), "zcode-process-provider-registry-"));
- temporaryDirectories.push(directory);
- return directory;
- }
- function completeZCodeBuiltinModelRule() {
- return {
- modelMatch: ".*",
- config: {
- enabled: true,
- properties: {
- requiresMfjsToolSchema: false,
- contextWindow: 200_000,
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: false,
- },
- optionSpecs: {
- reasoningLevel: { values: ["disabled"], map: "{}" },
- maxOutputTokens: {
- max: 32_000,
- map: '{"max_completion_tokens":maxOutputTokens}',
- },
- },
- },
- } as const;
- }
- function zcodeBuiltinRelease(input: {
- readonly providers: object;
- readonly templates: object;
- readonly models: readonly unknown[];
- }): object {
- return {
- schemaVersion: ZCODE_BUILTIN_RELEASE_SCHEMA_VERSION,
- revision: 1,
- config: {
- providerConfigRules: {
- providerRules: Object.entries(input.providers).map(([providerId, config]) => ({
- providerId,
- config,
- })),
- templateRules: Object.entries(input.templates).map(([templateId, config]) => ({
- templateId,
- templateNameMap: { "en-US": templateId, "zh-CN": templateId },
- config,
- })),
- },
- modelConfigRules: {
- modelRules: input.models,
- modelApiRules: [],
- providerSiteRules: [],
- templateModelRules: [],
- builtinProviderModelRules: [],
- },
- },
- };
- }
- function personalRelease(providerRules: readonly unknown[]) {
- return {
- schemaVersion: 1,
- config: {
- providerConfigRules: { providerRules },
- modelConfigRules: { providerModelRules: [], manualProviderModelRules: [] },
- },
- };
- }
|