omp-config-crypt 0.9.3 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -123,6 +123,11 @@ checked subset, Esc returns to source actions. Other/manual IDs also require gen
123
123
  Progress and model rows show full short-generation latency in milliseconds, including the final
124
124
  stream event. Esc during discovery or testing cancels acquisition. A supported native askDialog
125
125
  host is required. The local registry is read without refreshing it or accessing other credentials.
126
+ Managing an existing provider no longer re-tests configured models by default: the edit "模型"
127
+ entry first offers "保留已配置模型", which keeps every configured model checked and verified
128
+ without generation requests. "重新选择并测试模型" reopens the full acquisition flow, where
129
+ configured models are marked "已配置(默认保留)", skip candidate testing, and only new
130
+ candidates require generation checks.
126
131
  Manage existing providers to view details, edit a draft, verify a selected model, or confirm deletion.
127
132
  Role-bound providers must be rebound through `/role` before deletion (both in interactive UI and non-UI
128
133
  slash command `/provider rm <name>`, preventing dangling role references in `config.yml`; `omp-sync provider rm`
@@ -143,7 +148,7 @@ Pika, Vidu, Wanx, SDXL, Imagen, Ideogram, Kolors, `*-image`, `*-video`) while st
143
148
  The summary supports rechecking all selected models; failed, cancelled or unstarted rechecks cannot inherit
144
149
  previous successes. Check results, source labels and latency remain in memory and are not saved. `/provider test`
145
150
  checks one model and reports its latency; it does not claim the entire provider is verified.
146
- Draft cancellation does not write; saved changes load after restarting OMP.
151
+ Draft cancellation does not write. In-session interactive and non-UI `/provider` add, edit, and deletion synchronize immediately into the active OMP session's model catalog (`/models`). If the current session model belongs to a provider whose configuration is deleted or edited without that model ID, an alternative model must be selected before writing; if no replacement model is available, or if the current model is actively generating, the operation is blocked with zero configuration writes. If model catalog synchronization fails, configuration writes remain preserved on disk and a warning notification is displayed. Standalone `omp-sync` and other external OMP processes operate strictly at the file level and load changes upon their next startup.
147
152
  PRIVATE is the default and is excluded by encrypted-sync filtering; PUBLIC includes credentials
148
153
  in encrypted synchronization. Private providers remain mirrored in both existing stores.
149
154
  Background maintenance preserves the active `models.local.json` store; only obsolete root-level
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "omp-config-crypt",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
4
4
  "description": "Universal cross-platform encrypted portability for omp config, extensions, and instructions",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -11,7 +11,9 @@
11
11
  "omp-sync": "./bin/omp-sync.js"
12
12
  },
13
13
  "scripts": {
14
- "test": "node test/sync-portable.test.mjs"
14
+ "test": "node test/sync-portable.test.mjs",
15
+ "eval": "node eval/change-eval.mjs",
16
+ "eval:gate": "omp-agent-eval"
15
17
  },
16
18
  "keywords": [
17
19
  "omp",
@@ -8,6 +8,214 @@ const DIALOG = { outline: true, helpText: "↑↓ 选择 · Enter 确认 · Esc
8
8
  const select = (ctx, title, options) => ctx.ui.select(title, options, DIALOG);
9
9
  const rolesFor = (name, agentDir) => Object.entries(getRoles(agentDir))
10
10
  .filter(([, ref]) => ref.startsWith(`${name}/`)).map(([role]) => role);
11
+ const DEFAULT_CONTEXT_WINDOW = 256000;
12
+ const DEFAULT_MAX_TOKENS = 65536;
13
+
14
+ function inferModelSpec(modelId) {
15
+ const id = (modelId || "").toLowerCase();
16
+ if (/gemini.*pro/.test(id)) {
17
+ return { contextWindow: 2000000, maxTokens: 65536, hasImage: true };
18
+ }
19
+ if (/gemini.*(flash|lite)/.test(id)) {
20
+ return { contextWindow: 1000000, maxTokens: 65536, hasImage: true };
21
+ }
22
+ if (/deepseek.*(v4|pro|flash)/.test(id)) {
23
+ return { contextWindow: 1048576, maxTokens: 65536, hasImage: /flash|vl/.test(id) };
24
+ }
25
+ if (/deepseek/.test(id)) {
26
+ return { contextWindow: 1048576, maxTokens: 32000, hasImage: /vl/.test(id) };
27
+ }
28
+ if (/kimi|moonshot/.test(id)) {
29
+ return { contextWindow: 512000, maxTokens: 32000, hasImage: false };
30
+ }
31
+ if (/claude/.test(id)) {
32
+ return { contextWindow: 200000, maxTokens: 64000, hasImage: true };
33
+ }
34
+ if (/(gpt-6|gpt-5\.[0-9]).*(sol|terra)/.test(id)) {
35
+ return { contextWindow: 256000, maxTokens: 384000, reasoning: true, hasImage: false };
36
+ }
37
+ if (/gpt-6|astra/.test(id)) {
38
+ return { contextWindow: 256000, maxTokens: 32000, hasImage: false };
39
+ }
40
+ if (/qwen.*(max|plus|turbo|2\.5)/.test(id)) {
41
+ return { contextWindow: 131072, maxTokens: 32000, hasImage: /vl/.test(id) };
42
+ }
43
+ return { contextWindow: DEFAULT_CONTEXT_WINDOW, maxTokens: DEFAULT_MAX_TOKENS, hasImage: false };
44
+ }
45
+
46
+ function optimizeRuntimeModel(model) {
47
+ const id = model.id || "";
48
+ const inferred = inferModelSpec(id);
49
+ let cw = model.contextWindow;
50
+ let mt = model.maxTokens;
51
+ let input = Array.isArray(model.input) ? [...model.input] : ["text"];
52
+ let reasoning = model.reasoning;
53
+
54
+ if (!cw || cw <= 128000) {
55
+ cw = inferred.contextWindow || DEFAULT_CONTEXT_WINDOW;
56
+ }
57
+ if (cw < DEFAULT_CONTEXT_WINDOW) {
58
+ cw = DEFAULT_CONTEXT_WINDOW;
59
+ }
60
+ if (!mt || mt === 32000) {
61
+ mt = inferred.maxTokens || DEFAULT_MAX_TOKENS;
62
+ }
63
+ if (inferred.hasImage && !input.includes("image")) {
64
+ input.push("image");
65
+ }
66
+ if (inferred.reasoning && reasoning === undefined) {
67
+ reasoning = true;
68
+ }
69
+ return {
70
+ ...model,
71
+ contextWindow: cw,
72
+ maxTokens: mt,
73
+ input,
74
+ ...(reasoning !== undefined ? { reasoning } : {})
75
+ };
76
+ }
77
+
78
+ export function runtimeConfig(provider) {
79
+ const isPrivate = Boolean(provider.isPrivate || provider.private);
80
+ let models = Array.isArray(provider.models) ? provider.models : [];
81
+ if (isPrivate) {
82
+ models = models.map(m => optimizeRuntimeModel(m));
83
+ } else {
84
+ models = models.map(m => ({ ...m }));
85
+ }
86
+ const cfg = {
87
+ api: provider.api,
88
+ baseUrl: provider.baseUrl,
89
+ apiKey: provider.apiKey,
90
+ models,
91
+ };
92
+ if (provider.authHeader !== undefined) {
93
+ cfg.authHeader = provider.authHeader;
94
+ }
95
+ return cfg;
96
+ }
97
+
98
+ export function currentModel(ctx) {
99
+ try {
100
+ if (typeof ctx?.models?.current === "function") {
101
+ const cur = ctx.models.current();
102
+ if (cur) return cur;
103
+ }
104
+ } catch {}
105
+ try {
106
+ if (ctx?.model) return ctx.model;
107
+ } catch {}
108
+ return null;
109
+ }
110
+
111
+ export async function refreshRegistry(ctx) {
112
+ if (typeof ctx?.modelRegistry?.refresh === "function") {
113
+ await ctx.modelRegistry.refresh();
114
+ }
115
+ }
116
+
117
+ export async function registerRuntimeProvider(pi, ctx, provider) {
118
+ const name = provider.name || provider.id;
119
+ const isPrivate = Boolean(provider.isPrivate || provider.private);
120
+ if (isPrivate) {
121
+ if (typeof pi?.registerProvider !== "function") {
122
+ throw new Error("模型目录同步不可用");
123
+ }
124
+ pi.registerProvider(name, runtimeConfig(provider));
125
+ }
126
+ if (!isPrivate && typeof ctx?.modelRegistry?.refresh !== "function") {
127
+ throw new Error("模型目录同步不可用");
128
+ }
129
+ await refreshRegistry(ctx);
130
+ }
131
+
132
+ export async function syncRemovedProvider(pi, ctx, name, wasPrivate) {
133
+ if (wasPrivate) {
134
+ if (typeof pi?.unregisterProvider !== "function") {
135
+ throw new Error("模型目录同步不可用");
136
+ }
137
+ await pi.unregisterProvider(name);
138
+ }
139
+ if (!wasPrivate && typeof ctx?.modelRegistry?.refresh !== "function") {
140
+ throw new Error("模型目录同步不可用");
141
+ }
142
+ await refreshRegistry(ctx);
143
+ }
144
+
145
+ export function availableModels(ctx) {
146
+ try {
147
+ if (typeof ctx?.modelRegistry?.getAvailable === "function") {
148
+ const list = ctx.modelRegistry.getAvailable();
149
+ if (Array.isArray(list)) return list;
150
+ }
151
+ } catch {}
152
+ return [];
153
+ }
154
+
155
+ export async function ensureCurrentModel(pi, ctx, { provider, modelId, modelIds, action }) {
156
+ const cur = currentModel(ctx);
157
+ if (!cur || cur.provider !== provider) return true;
158
+
159
+ if (typeof ctx?.waitForIdle === "function") {
160
+ await ctx.waitForIdle();
161
+ }
162
+ if (typeof ctx?.isIdle === "function" && !ctx.isIdle()) {
163
+ ctx.ui?.notify?.(`当前模型正在响应,完成后再${action}`, "warning");
164
+ return false;
165
+ }
166
+
167
+ if (action === "保存") {
168
+ const ids = Array.isArray(modelIds)
169
+ ? modelIds
170
+ : (Array.isArray(modelId) ? modelId : (modelId ? [modelId] : []));
171
+ if (ids.includes(cur.id)) {
172
+ return true;
173
+ }
174
+ }
175
+
176
+ const candidateMap = new Map();
177
+ for (const m of availableModels(ctx)) {
178
+ if (m?.provider && m?.id && m.provider !== provider) {
179
+ const key = `${m.provider}/${m.id}`;
180
+ if (!candidateMap.has(key)) candidateMap.set(key, m);
181
+ }
182
+ }
183
+
184
+ const options = Array.from(candidateMap.keys());
185
+ if (!options.length) {
186
+ ctx.ui?.notify?.(`没有可切换的其它模型,未${action}`, "warning");
187
+ return false;
188
+ }
189
+
190
+ if (!ctx?.hasUI) {
191
+ ctx.ui?.notify?.(`当前模型属于 ${provider},请先使用 /model 切换后再${action}`, "warning");
192
+ return false;
193
+ }
194
+
195
+ const chosenKey = await select(ctx, "当前模型将被移除,请选择替代模型", options);
196
+ if (!chosenKey) return false;
197
+
198
+ const chosenModel = candidateMap.get(chosenKey);
199
+ try {
200
+ if (typeof pi?.setModel !== "function") {
201
+ throw new Error("宿主不支持 setModel");
202
+ }
203
+ await pi.setModel(chosenModel);
204
+ } catch (err) {
205
+ ctx.ui?.notify?.(`切换模型失败: ${err.message}`, "error");
206
+ return false;
207
+ }
208
+
209
+ const newCur = currentModel(ctx);
210
+ if (!newCur || newCur.provider !== chosenModel.provider || newCur.id !== chosenModel.id) {
211
+ ctx.ui?.notify?.(`切换模型失败: 无法激活 ${chosenKey}`, "error");
212
+ return false;
213
+ }
214
+
215
+ ctx.ui?.notify?.(`当前模型已切换到 ${chosenKey}`, "info");
216
+ return true;
217
+ }
218
+
11
219
 
12
220
  function validName(name) {
13
221
  if (!/^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(name)) throw new Error("名称须以字母或数字开头,仅含字母、数字、_、-");
@@ -71,7 +279,8 @@ async function verifyDraft(ctx, draft) {
71
279
  }
72
280
  const checkIdentity = config => JSON.stringify([config.api, config.baseUrl, config.apiKey || ""]);
73
281
  async function acquireDraftModels(ctx, draft, agentDir, seedIds = draft.config.models.map(model => model.id)) {
74
- const acquired = await acquireProviderModels(ctx, { name: draft.name, config: draft.config, agentDir, seedIds });
282
+ const acquired = await acquireProviderModels(ctx, { name: draft.name, config: draft.config, agentDir, seedIds,
283
+ preservedIds: draft.config.models.map(model => model.id) });
75
284
  if (!acquired) return false;
76
285
  draft.config.models = acquired.models;
77
286
  draft.config.baseUrl = acquired.baseUrl;
@@ -89,7 +298,7 @@ async function recheckDraftModels(ctx, draft) {
89
298
  for (const result of batch.results) if (result.ok) draft.checks.set(result.id, { ok: true, latencyMs: result.latencyMs });
90
299
  draft.checkIdentity = identity;
91
300
  }
92
- async function saveSummary(ctx, draft, agentDir, isNew) {
301
+ export async function saveSummary(pi, ctx, draft, agentDir, isNew) {
93
302
  if (!draft.requireVerifiedModels && draft.config.api === "anthropic-messages") draft.config.baseUrl = draft.config.baseUrl.replace(/\/v1$/i, "");
94
303
  for (;;) {
95
304
  if (draft.requireVerifiedModels && draft.checkIdentity !== checkIdentity(draft.config)) draft.checks = new Map();
@@ -108,13 +317,53 @@ async function saveSummary(ctx, draft, agentDir, isNew) {
108
317
  try {
109
318
  if (isNew && getProvider(draft.name, { agentDir })) throw new Error("名称已存在,请进入已有项编辑");
110
319
  if (!draft.config.models.length) throw new Error("请先配置实际模型 ID");
320
+
321
+ const cur = currentModel(ctx);
322
+ const curBelongsToThis = Boolean(cur && cur.provider === draft.name);
323
+ const newModelIds = draft.config.models.map(model => model.id);
324
+ const retainsCurrentId = curBelongsToThis && newModelIds.includes(cur.id);
325
+
326
+ if (curBelongsToThis) {
327
+ const ok = await ensureCurrentModel(pi, ctx, { provider: draft.name, modelId: newModelIds, action: "保存" });
328
+ if (!ok) return false;
329
+ }
330
+
111
331
  saveProvider({ name: draft.name, providerConfig: draft.config, isPrivate: draft.isPrivate, agentDir });
112
- ctx.ui.notify("已保存,重启 OMP 后加载配置", "info");
332
+ const savedProvider = getProvider(draft.name, { agentDir }) || {
333
+ name: draft.name,
334
+ isPrivate: draft.isPrivate,
335
+ ...draft.config,
336
+ };
337
+
338
+ let syncFailed = false;
339
+ try {
340
+ await registerRuntimeProvider(pi, ctx, savedProvider);
341
+ } catch (syncErr) {
342
+ syncFailed = true;
343
+ ctx.ui.notify(`配置已保存,但模型目录同步失败: ${syncErr.message}`, "error");
344
+ }
345
+
346
+ if (!syncFailed) {
347
+ if (retainsCurrentId) {
348
+ const available = availableModels(ctx);
349
+ const resolved = available.find(m => m.provider === draft.name && m.id === cur.id);
350
+ if (resolved && typeof pi?.setModel === "function") {
351
+ try {
352
+ await pi.setModel(resolved);
353
+ } catch {
354
+ ctx.ui.notify("配置已保存,但当前模型未能重新绑定", "warning");
355
+ }
356
+ } else {
357
+ ctx.ui.notify("配置已保存,但当前模型未能重新绑定", "warning");
358
+ }
359
+ }
360
+ ctx.ui.notify("已保存,模型目录已同步", "info");
361
+ }
113
362
  return true;
114
363
  } catch (error) { ctx.ui.notify(`保存失败,未完成保存: ${error.message}`, "error"); }
115
364
  }
116
365
  }
117
- async function addDraft(ctx, agentDir, seed = {}) {
366
+ async function addDraft(pi, ctx, agentDir, seed = {}) {
118
367
  const name = await inputField(ctx, "Provider 名称", seed.name || "例如 my-provider", value => {
119
368
  validName(value);
120
369
  if (getProvider(value, { agentDir })) throw new Error("名称已存在,请进入已有项编辑");
@@ -131,9 +380,9 @@ async function addDraft(ctx, agentDir, seed = {}) {
131
380
  if (isPrivate === undefined) return;
132
381
  const draft = { name, isPrivate, config: { baseUrl, api, apiKey, models: [] }, verified: false };
133
382
  if (!await acquireDraftModels(ctx, draft, agentDir, seed.model ? [seed.model] : [])) return;
134
- await saveSummary(ctx, draft, agentDir, true);
383
+ await saveSummary(pi, ctx, draft, agentDir, true);
135
384
  }
136
- async function editDraft(ctx, name, agentDir, supplied) {
385
+ async function editDraft(pi, ctx, name, agentDir, supplied) {
137
386
  const provider = getProvider(name, { agentDir });
138
387
  if (!provider) return ctx.ui.notify(`Provider '${name}' not found`, "warning");
139
388
  const draft = supplied || fromProvider(provider);
@@ -173,32 +422,55 @@ async function editDraft(ctx, name, agentDir, supplied) {
173
422
  } else if (field === "存储范围") {
174
423
  const scope = await chooseScope(ctx);
175
424
  if (scope !== undefined) draft.isPrivate = scope;
176
- } else if (field === "模型") await acquireDraftModels(ctx, draft, agentDir);
177
- else if (field === "保存" && await saveSummary(ctx, draft, agentDir, false)) return;
425
+ } else if (field === "模型") {
426
+ if (!draft.config.models.length) { await acquireDraftModels(ctx, draft, agentDir); continue; }
427
+ const operation = await select(ctx, `MODELS // ${name} · ${draft.config.models.length} 个已配置模型`,
428
+ ["保留已配置模型", "重新选择并测试模型", "返回"]);
429
+ if (operation === "保留已配置模型") {
430
+ // Default for edit: keep every configured model verified without re-testing.
431
+ draft.checks = new Map(draft.config.models.map(model => [model.id, { ok: true }]));
432
+ draft.checkIdentity = checkIdentity(draft.config);
433
+ draft.requireVerifiedModels = true;
434
+ ctx.ui.notify("已保留全部已配置模型,未重新测试", "info");
435
+ } else if (operation === "重新选择并测试模型") await acquireDraftModels(ctx, draft, agentDir);
436
+ }
437
+ else if (field === "保存" && await saveSummary(pi, ctx, draft, agentDir, false)) return;
178
438
  }
179
439
  }
180
- async function deleteProvider(ctx, name, agentDir) {
440
+ export async function deleteProvider(pi, ctx, name, agentDir) {
181
441
  const provider = getProvider(name, { agentDir });
182
442
  if (!provider) return ctx.ui.notify(`Provider '${name}' not found`, "warning");
443
+ const wasPrivate = Boolean(provider.isPrivate || provider.private);
183
444
  const roles = rolesFor(name, agentDir);
184
445
  if (roles.length) {
185
446
  ctx.ui.notify(`${name} 绑定角色: ${roles.join(", ")};请先通过 /role 改绑`, "warning"); return;
186
447
  }
187
- if (await ctx.ui.confirm(`删除 ${name}`, `名称: ${name}\n绑定角色: 无\n确认从两处配置删除?`)) {
188
- removeProvider({ name, agentDir });
189
- ctx.ui.notify(`已删除 ${name},重启 OMP 后加载配置`, "info");
448
+ if (!await ctx.ui.confirm(`删除 ${name}`, `名称: ${name}\n绑定角色: 无\n确认从两处配置删除?`)) {
449
+ return;
450
+ }
451
+ const modelOk = await ensureCurrentModel(pi, ctx, { provider: name, action: "删除" });
452
+ if (!modelOk) return;
453
+
454
+ const removed = removeProvider({ name, agentDir });
455
+ if (!removed) return;
456
+
457
+ try {
458
+ await syncRemovedProvider(pi, ctx, name, wasPrivate);
459
+ ctx.ui.notify(`已删除 ${name},模型目录已同步`, "info");
460
+ } catch (err) {
461
+ ctx.ui.notify(`配置已删除,但模型目录同步失败: ${err.message}`, "error");
190
462
  }
191
463
  }
192
464
 
193
465
  export async function runProviderMenu(pi, ctx, { agentDir, action, name, draft } = {}) {
194
466
  if (!ctx.hasUI) return;
195
- if (action === "add" || action === "wizard") return addDraft(ctx, agentDir, draft);
196
- if (action === "edit") return editDraft(ctx, name, agentDir, draft);
467
+ if (action === "add" || action === "wizard") return addDraft(pi, ctx, agentDir, draft);
468
+ if (action === "edit") return editDraft(pi, ctx, name, agentDir, draft);
197
469
  if (action === "test") {
198
470
  const provider = getProvider(name, { agentDir });
199
471
  return provider ? verifyDraft(ctx, fromProvider(provider)) : ctx.ui.notify(`Provider '${name}' not found`, "warning");
200
472
  }
201
- if (action === "rm" || action === "remove") return deleteProvider(ctx, name, agentDir);
473
+ if (action === "rm" || action === "remove") return deleteProvider(pi, ctx, name, agentDir);
202
474
  for (;;) {
203
475
  const providers = listProviders({ agentDir });
204
476
  const choice = await select(ctx, "PROVIDER // CONTROL", [
@@ -206,7 +478,7 @@ export async function runProviderMenu(pi, ctx, { agentDir, action, name, draft }
206
478
  { label: "≡ 管理已有 Provider", description: `${providers.length} 个 Provider` }, "× 退出",
207
479
  ]);
208
480
  if (!choice || choice === "× 退出") return;
209
- if (choice === "+ 添加 Provider") { await addDraft(ctx, agentDir); continue; }
481
+ if (choice === "+ 添加 Provider") { await addDraft(pi, ctx, agentDir); continue; }
210
482
  for (;;) {
211
483
  const current = listProviders({ agentDir });
212
484
  if (!current.length) { ctx.ui.notify("尚未配置 Provider", "info"); break; }
@@ -219,9 +491,9 @@ export async function runProviderMenu(pi, ctx, { agentDir, action, name, draft }
219
491
  const operation = await select(ctx, `PROVIDER // ${chosen}`, ["查看详情", "编辑配置", "验证连接", "删除", "返回"]);
220
492
  if (!operation || operation === "返回") break;
221
493
  if (operation === "查看详情") ctx.ui.notify(summary(fromProvider(provider), agentDir), "info");
222
- if (operation === "编辑配置") await editDraft(ctx, chosen, agentDir);
494
+ if (operation === "编辑配置") await editDraft(pi, ctx, chosen, agentDir);
223
495
  if (operation === "验证连接") await verifyDraft(ctx, fromProvider(provider));
224
- if (operation === "删除") { await deleteProvider(ctx, chosen, agentDir); if (!getProvider(chosen, { agentDir })) break; }
496
+ if (operation === "删除") { await deleteProvider(pi, ctx, chosen, agentDir); if (!getProvider(chosen, { agentDir })) break; }
225
497
  }
226
498
  }
227
499
  }
@@ -273,12 +545,28 @@ export async function handleProviderCommand(pi, ctx, args, agentDir) {
273
545
  if (!name || tokens.length !== 2) throw new Error(`Usage: /provider ${sub} <name>`);
274
546
  if (ctx.hasUI) return await runProviderMenu(pi, ctx, { agentDir, action: sub, name });
275
547
  if (sub === "edit" || sub === "test") throw new Error(`Usage: /provider ${sub} <name> (requires interactive UI)`);
548
+ const provider = getProvider(name, { agentDir });
549
+ if (!provider) {
550
+ ctx.ui.notify(`Provider '${name}' not found`, "info");
551
+ return;
552
+ }
553
+ const wasPrivate = Boolean(provider.isPrivate || provider.private);
276
554
  const roles = rolesFor(name, agentDir);
277
555
  if (roles.length) {
278
556
  ctx.ui.notify(`Provider '${name}' 绑定角色: ${roles.join(", ")};请先通过 /role 改绑`, "warning");
279
557
  return;
280
558
  }
281
- ctx.ui.notify(removeProvider({ name, agentDir }) ? `Removed provider '${name}'` : `Provider '${name}' not found`, "info"); return;
559
+ const modelOk = await ensureCurrentModel(pi, ctx, { provider: name, action: "删除" });
560
+ if (!modelOk) return;
561
+
562
+ removeProvider({ name, agentDir });
563
+ try {
564
+ await syncRemovedProvider(pi, ctx, name, wasPrivate);
565
+ ctx.ui.notify(`Removed provider '${name}' and synchronized the model catalog`, "info");
566
+ } catch (err) {
567
+ ctx.ui.notify(`配置已删除,但模型目录同步失败: ${err.message}`, "error");
568
+ }
569
+ return;
282
570
  }
283
571
  if (sub === "add" || sub === "wizard") {
284
572
  const options = parseAdd(tokens.slice(1));
@@ -292,16 +580,32 @@ export async function handleProviderCommand(pi, ctx, args, agentDir) {
292
580
  const draft = { name: options.name, isPrivate: options.isPrivate, verified: false,
293
581
  config: { api: options.api, baseUrl: options.baseUrl, apiKey: options.apiKey, models: [] } };
294
582
  if (!await acquireDraftModels(ctx, draft, agentDir, options.model ? [options.model] : [])) return;
295
- await saveSummary(ctx, draft, agentDir, true);
583
+ await saveSummary(pi, ctx, draft, agentDir, true);
296
584
  return;
297
585
  }
298
586
  const result = await probeProvider({ ...options, testModelId: options.model, providerName: options.name });
299
587
  if (!result.ok && !options.model) throw new Error(result.error);
300
588
  const models = result.ok ? result.models : [inferModelCapabilities(options.model, options.name)];
301
589
  if (options.model && !models.some(model => model.id === options.model)) models.push(inferModelCapabilities(options.model, options.name));
302
- saveProvider({ name: options.name, providerConfig: { baseUrl: result.ok ? result.normalizedBaseUrl : options.baseUrl,
303
- api: options.api, apiKey: options.apiKey, models }, isPrivate: options.isPrivate, agentDir });
304
- ctx.ui.notify("已保存(未验证),重启 OMP 后加载配置", "info"); return;
590
+ const providerConfig = {
591
+ baseUrl: result.ok ? result.normalizedBaseUrl : options.baseUrl,
592
+ api: options.api,
593
+ apiKey: options.apiKey,
594
+ models,
595
+ };
596
+ saveProvider({ name: options.name, providerConfig, isPrivate: options.isPrivate, agentDir });
597
+ const savedProvider = getProvider(options.name, { agentDir }) || {
598
+ name: options.name,
599
+ isPrivate: options.isPrivate,
600
+ ...providerConfig,
601
+ };
602
+ try {
603
+ await registerRuntimeProvider(pi, ctx, savedProvider);
604
+ ctx.ui.notify("已保存(未验证),模型目录已同步", "info");
605
+ } catch (syncErr) {
606
+ ctx.ui.notify(`配置已保存,但模型目录同步失败: ${syncErr.message}`, "error");
607
+ }
608
+ return;
305
609
  }
306
610
  throw new Error("Usage: /provider [ls|add|edit <name>|test <name>|rm <name>]");
307
611
  } catch (error) { ctx.ui.notify(`Provider command error: ${error.message}`, "error"); }
@@ -116,12 +116,18 @@ async function showFailures(ctx, cache) {
116
116
  }
117
117
 
118
118
  /** Acquire a verified subset without mutating config, the registry, or either store. */
119
- export async function acquireProviderModels(ctx, { name, config, agentDir, seedIds = [] }) {
119
+ export async function acquireProviderModels(ctx, { name, config, agentDir, seedIds = [], preservedIds = [] }) {
120
120
  if (typeof ctx.ui.askDialog !== "function") {
121
121
  ctx.ui.notify("当前宿主不支持空格多选,请升级 OMP", "error");
122
122
  return;
123
123
  }
124
124
  const working = { ...config }, existing = new Map((config.models || []).map(model => [model.id, model]));
125
+ const cache = new Map(), sources = new Map();
126
+ // Already-configured models keep verified status without any generation test (edit flow).
127
+ for (const id of unique(preservedIds)) {
128
+ if (!cache.has(id)) cache.set(id, { ok: true, latencyMs: null, preserved: true });
129
+ if (!sources.has(id)) sources.set(id, "已配置");
130
+ }
125
131
  if (working.api === "anthropic-messages") working.baseUrl = working.baseUrl.trim().replace(/\/+$/, "").replace(/\/v1$/i, "");
126
132
  let configuredProviders;
127
133
  try { configuredProviders = listProviders({ agentDir }); }
@@ -132,7 +138,7 @@ export async function acquireProviderModels(ctx, { name, config, agentDir, seedI
132
138
  catalog = ctx.modelRegistry.getAll("chat");
133
139
  if (!Array.isArray(catalog)) throw new Error("Invalid registry");
134
140
  } catch { catalog = []; ctx.ui.notify("本机模型目录不可用", "warning"); }
135
- const cache = new Map(), sources = new Map(), stageIds = [[], [], [], []];
141
+ const stageIds = [[], [], [], []];
136
142
  let stage = 0, mode = "stage", retry = false;
137
143
  const available = () => [...sources.keys()].filter(id => cache.get(id)?.ok);
138
144
  const check = async (ids, source) => {
@@ -186,8 +192,11 @@ export async function acquireProviderModels(ctx, { name, config, agentDir, seedI
186
192
  const labels = new Map(available().map(id => [id, id]));
187
193
  const result = await ctx.ui.askDialog([{
188
194
  id: "provider-models", header: "MODELS", question: "选择已验证模型 · 空格勾选 · Enter 确认 · Esc 返回", multi: true,
189
- options: [...labels.keys()].map(id => ({ label: id,
190
- description: `可用 · ${cache.get(id).latencyMs} ms · 来源: ${sources.get(id)}` })),
195
+ options: [...labels.keys()].map(id => {
196
+ const entry = cache.get(id);
197
+ return { label: id,
198
+ description: entry.preserved ? "已配置(默认保留)" : `可用 · ${entry.latencyMs} ms · 来源: ${sources.get(id)}` };
199
+ }),
191
200
  }]);
192
201
  if (!result || result.kind !== "submit") { mode = "menu"; continue; }
193
202
  const answer = result.results?.find(item => item.id === "provider-models");