fix(env-sync): 同步 provider_config.json(UI 分组/顺序/模型级配置)

问题:只同步 v2/config.json 时,只在 v2/provider_config.json 里存在的
provider 分组到对面就是缺失的 —— 表现为「provider 数量对,但 UI 分组少几个」。
实测:9 个分组里有 4 个(GINKA API / zxcbug / zxcbug-p / d1)纯规则型,
密钥只存在 provider_config.json 的 config.access.apiKey 里,旧实现完全没收集。

修复:
- sync-core:新增 V2_PROVIDER_CONFIG、extract/sanitize/collectProviderConfigSecrets、
  providerConfigKeyManifest;密钥命名空间 PROVIDER_CONFIG/<providerId>/<key>
- export:导出 v2.provider-config.json(脱敏),密钥并入加密束,manifest 记摘要与哈希
- import:按 providerId 合并规则、回填密钥空位、按 (providerId,modelId) 合并模型级配置、
  providerOrder 以快照为准并保留本机特有分组;重复导入幂等
- import:provider_config 路径补「护住本机已有密钥 N 个」提示(原来只在 config.json 侧有)
- 测试:单元 6 项 + E2E 2 项(分组完整同步/强制覆盖),全量 72 项通过
This commit is contained in:
2026-09-21 13:00:10 +08:00
parent 6ed5dda847
commit 06891fa726
6 changed files with 450 additions and 10 deletions
+92 -1
View File
@@ -10,12 +10,13 @@ import os from "node:os";
import path from "node:path";
import { createHash } from "node:crypto";
import {
HOME, CLI_CONFIG, PLUGINS_HOME, REPO_DIR, BACKUP_ROOT, V2_CONFIG, V2_SETTING,
HOME, CLI_CONFIG, PLUGINS_HOME, REPO_DIR, BACKUP_ROOT, V2_CONFIG, V2_SETTING, V2_PROVIDER_CONFIG,
WITH_MODELS, APPLY_MODEL_SELECTION, OVERWRITE_MODEL_SECRETS, MODEL_SECRET_REF,
nowTag, readJson, writeJson, decodeJsonPaths, encodeJsonPaths,
decryptSecrets, restoreRelPath, isModelEntry, parseModelRel, requirePassphrase,
ensureRepo, readSnapshotManifest, latestSnapshotDir,
deepMerge, loadOverrides,
isProviderConfigEntry, parseProviderConfigRel,
} from "./sync-core.mjs";
function fail(e) { console.error(`导入失败:${e?.message ?? e}`); process.exitCode = 1; }
@@ -101,9 +102,11 @@ try {
const skipSet = skipSecretsSet();
const skipped = [];
const modelKeys = [];
const pcKeys = [];
let n = 0;
for (const f of bundle.files ?? []) {
if (isModelEntry(f.rel)) { modelKeys.push(f); continue; }
if (isProviderConfigEntry(f.rel)) { pcKeys.push(f); continue; }
if (skipSet.has(f.rel)) { skipped.push(f.rel); continue; }
let dst;
try { dst = restoreRelPath(f.rel); }
@@ -190,6 +193,94 @@ try {
report.push(`- 模型:快照未含模型(导出时未开 ZCODE_SYNC_WITH_MODELS=1)`);
}
// 4b) provider_config.json:UI 的分组/顺序/模型级配置
// config.json 决定「provider 能不能调」,这份决定「UI 怎么分组显示」。
// 只同步前者时,仅在 provider_config 里存在的分组到对面就是缺失的。
const pcFile = path.join(snapDir, "v2.provider-config.json");
if (fs.existsSync(pcFile) && (manifest.providerConfigRuleCount ?? 0) > 0) {
const snapPc = decodeJsonPaths(readJson(pcFile, {}));
if (!WITH_MODELS) {
report.push(`- UI 分组:快照含 ${(manifest.providerConfigOrder ?? []).length} 个分组,`
+ `默认不覆盖本机(设 ZCODE_SYNC_WITH_MODELS=1 应用)`);
} else {
const localPc = fs.existsSync(V2_PROVIDER_CONFIG) ? readJson(V2_PROVIDER_CONFIG, {}) : {};
const b3 = backup(V2_PROVIDER_CONFIG); if (b3) report.push(`- 备份 v2/provider_config.json → ${b3}`);
const keyMapPc = new Map();
for (const f of pcKeys) {
const { provider, key } = parseProviderConfigRel(f.rel);
keyMapPc.set(`${provider}\n${key}`, Buffer.from(f.contentB64, "base64").toString("utf8"));
}
const snapCfg = snapPc?.config ?? {};
const localCfg = localPc.config ?? (localPc.config = {});
const snapRules = snapCfg.providerConfigRules?.providerRules ?? [];
const snapModels = snapCfg.modelConfigRules?.providerModelRules ?? [];
const snapManual = snapCfg.modelConfigRules?.manualProviderModelRules ?? [];
localCfg.providerConfigRules = localCfg.providerConfigRules ?? {};
localCfg.modelConfigRules = localCfg.modelConfigRules ?? {};
const localRules = localCfg.providerConfigRules.providerRules ?? (localCfg.providerConfigRules.providerRules = []);
const localModels = localCfg.modelConfigRules.providerModelRules ?? (localCfg.modelConfigRules.providerModelRules = []);
const localManual = localCfg.modelConfigRules.manualProviderModelRules ?? (localCfg.modelConfigRules.manualProviderModelRules = []);
// 按 id 建索引,避免同一 provider 重复追加
const byId = new Map(localRules.map((r, i) => [r?.providerId, i]));
let addRule = 0, mergeRule = 0, filledPcKey = 0, skippedPcKey = 0, skippedPcOverwrite = 0, addModel = 0;
for (const sr of snapRules) {
const id = sr?.providerId;
if (!id) continue;
if (!byId.has(id)) { localRules.push(JSON.parse(JSON.stringify(sr))); byId.set(id, localRules.length - 1); addRule++; }
else {
// 已有:补缺失字段,不动本机已有的值(含密钥)
const lr = localRules[byId.get(id)];
lr.providerName = lr.providerName ?? sr.providerName;
lr.config = lr.config ?? {};
for (const [k, v] of Object.entries(sr.config ?? {})) {
if (k === "access") continue; // 密钥单独处理
if (lr.config[k] === undefined) lr.config[k] = JSON.parse(JSON.stringify(v));
}
lr.config.access = lr.config.access ?? {};
for (const [k, v] of Object.entries(sr.config?.access ?? {})) {
if (v === MODEL_SECRET_REF) continue;
if (lr.config.access[k] === undefined) lr.config.access[k] = v;
}
mergeRule++;
}
// 密钥回填:占位符/空/过短才填
const lr = localRules[byId.get(id)];
lr.config.access = lr.config.access ?? {};
for (const [kk, vv] of keyMapPc) {
const [pid, k] = kk.split("\n");
if (pid !== id) continue;
if (skipSet.has(`PROVIDER_CONFIG/${pid}/${k}`)) { skippedPcKey++; continue; }
const cur = lr.config.access[k];
if (cur === MODEL_SECRET_REF || cur === undefined || cur === "" || (typeof cur === "string" && cur.length < 8)) {
lr.config.access[k] = vv; filledPcKey++;
} else if (OVERWRITE_MODEL_SECRETS) {
lr.config.access[k] = vv; filledPcKey++;
} else {
skippedPcOverwrite++; // 本机已有非空密钥,默认护住
}
}
}
// 模型级配置:按 (providerId, modelId) 去重合并
const mKey = (r) => `${r?.providerId}\n${r?.modelId}`;
const haveModel = new Set(localModels.map(mKey));
for (const mr of snapModels) { if (!haveModel.has(mKey(mr))) { localModels.push(JSON.parse(JSON.stringify(mr))); haveModel.add(mKey(mr)); addModel++; } }
const haveManual = new Set(localManual.map(mKey));
for (const mr of snapManual) { if (!haveManual.has(mKey(mr))) { localManual.push(JSON.parse(JSON.stringify(mr))); haveManual.add(mKey(mr)); } }
// providerOrder:快照的分组顺序(合并本机多出来的,不让本机特有分组消失)
const snapOrder = snapCfg.providerOrder ?? [];
const merged = [...snapOrder];
for (const id of localCfg.providerOrder ?? []) if (!merged.includes(id)) merged.push(id);
localCfg.providerOrder = merged;
if (localPc.schemaVersion === undefined && snapPc.schemaVersion !== undefined) localPc.schemaVersion = snapPc.schemaVersion;
writeJson(V2_PROVIDER_CONFIG, localPc);
report.push(`- UI 分组:${merged.length} 个分组(新增规则 ${addRule},合并 ${mergeRule},密钥回填 ${filledPcKey}`
+ (skippedPcOverwrite ? `,护住本机已有密钥 ${skippedPcOverwrite} 个(需覆盖设 ZCODE_SYNC_OVERWRITE_MODEL_SECRETS=1)` : "")
+ (skippedPcKey ? `,跳过 ${skippedPcKey}(ZCODE_SYNC_SKIP_SECRETS)` : "") + `),模型级配置 +${addModel} 条`);
}
} else if (manifest.providerConfigRuleCount) {
report.push(`- UI 分组:快照仅记录摘要 ${manifest.providerConfigRuleCount} 条(未含配置本体)`);
}
// 5) 官方插件:只报告差异,不自动执行(去插件市场对照启用,重启生效,避免误装)
if (process.env.ZCODE_SYNC_APPLY_OFFICIAL !== "0") {
const { enabledPlugins } = await import("./sync-core.mjs");