codexmate 0.0.39 → 0.0.41

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.
Files changed (155) hide show
  1. package/README.md +168 -156
  2. package/README.zh.md +168 -156
  3. package/cli/agents-files.js +230 -230
  4. package/cli/analytics-export-args.js +68 -68
  5. package/cli/archive-helpers.js +453 -453
  6. package/cli/auth-profiles.js +375 -375
  7. package/cli/builtin-proxy.js +2144 -2144
  8. package/cli/claude-proxy.js +1022 -1022
  9. package/cli/config-bootstrap.js +407 -402
  10. package/cli/config-health.js +454 -454
  11. package/cli/doctor-core.js +903 -903
  12. package/cli/import-skills-url.js +356 -356
  13. package/cli/local-bridge.js +556 -556
  14. package/cli/openai-bridge.js +1984 -1984
  15. package/cli/openclaw-config.js +629 -629
  16. package/cli/session-convert-args.js +69 -69
  17. package/cli/session-convert-io.js +82 -82
  18. package/cli/session-convert.js +150 -150
  19. package/cli/session-usage.concurrent.js +28 -28
  20. package/cli/session-usage.js +304 -304
  21. package/cli/session-usage.models.js +176 -176
  22. package/cli/skills.js +1141 -1141
  23. package/cli/update.js +171 -171
  24. package/cli/zip-commands.js +510 -510
  25. package/cli.js +16458 -16174
  26. package/lib/automation.js +404 -404
  27. package/lib/cli-file-utils.js +151 -151
  28. package/lib/cli-models-utils.js +440 -440
  29. package/lib/cli-network-utils.js +190 -190
  30. package/lib/cli-path-utils.js +85 -85
  31. package/lib/cli-session-utils.js +121 -121
  32. package/lib/cli-sessions.js +427 -427
  33. package/lib/cli-utils.js +155 -155
  34. package/lib/cli-webhook.js +154 -154
  35. package/lib/download-artifacts.js +92 -92
  36. package/lib/mcp-stdio.js +453 -453
  37. package/lib/task-orchestrator.js +869 -869
  38. package/lib/text-diff.js +303 -303
  39. package/lib/win-tray.js +119 -119
  40. package/lib/workflow-engine.js +340 -340
  41. package/package.json +77 -77
  42. package/plugins/README.md +20 -20
  43. package/plugins/README.zh-CN.md +20 -20
  44. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  45. package/plugins/prompt-templates/computed.mjs +311 -253
  46. package/plugins/prompt-templates/index.mjs +8 -8
  47. package/plugins/prompt-templates/manifest.mjs +18 -15
  48. package/plugins/prompt-templates/methods.mjs +553 -553
  49. package/plugins/prompt-templates/overview.mjs +91 -91
  50. package/plugins/prompt-templates/ownership.mjs +19 -19
  51. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  52. package/plugins/prompt-templates/storage.mjs +64 -64
  53. package/plugins/registry.mjs +16 -16
  54. package/web-ui/app.js +695 -693
  55. package/web-ui/index.html +37 -37
  56. package/web-ui/logic.agents-diff.mjs +386 -386
  57. package/web-ui/logic.claude.mjs +172 -172
  58. package/web-ui/logic.codex.mjs +69 -69
  59. package/web-ui/logic.mjs +5 -5
  60. package/web-ui/logic.runtime.mjs +128 -128
  61. package/web-ui/logic.session-convert.mjs +70 -70
  62. package/web-ui/logic.sessions.mjs +782 -782
  63. package/web-ui/modules/api.mjs +90 -90
  64. package/web-ui/modules/app.computed.dashboard.mjs +252 -252
  65. package/web-ui/modules/app.computed.index.mjs +17 -17
  66. package/web-ui/modules/app.computed.main-tabs.mjs +214 -212
  67. package/web-ui/modules/app.computed.session.mjs +876 -876
  68. package/web-ui/modules/app.constants.mjs +15 -15
  69. package/web-ui/modules/app.methods.agents.mjs +651 -651
  70. package/web-ui/modules/app.methods.claude-config.mjs +412 -412
  71. package/web-ui/modules/app.methods.codex-config.mjs +869 -869
  72. package/web-ui/modules/app.methods.index.mjs +96 -94
  73. package/web-ui/modules/app.methods.install.mjs +205 -205
  74. package/web-ui/modules/app.methods.navigation.mjs +804 -804
  75. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  76. package/web-ui/modules/app.methods.openclaw-editing.mjs +420 -420
  77. package/web-ui/modules/app.methods.openclaw-persist.mjs +375 -375
  78. package/web-ui/modules/app.methods.providers.mjs +601 -601
  79. package/web-ui/modules/app.methods.runtime.mjs +420 -420
  80. package/web-ui/modules/app.methods.session-actions.mjs +591 -591
  81. package/web-ui/modules/app.methods.session-browser.mjs +1018 -1018
  82. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  83. package/web-ui/modules/app.methods.session-trash.mjs +468 -468
  84. package/web-ui/modules/app.methods.startup-claude.mjs +554 -548
  85. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  86. package/web-ui/modules/app.methods.tool-config-permissions.mjs +87 -0
  87. package/web-ui/modules/app.methods.webhook.mjs +87 -87
  88. package/web-ui/modules/config-mode.computed.mjs +124 -124
  89. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  90. package/web-ui/modules/i18n/locales/en.mjs +1140 -0
  91. package/web-ui/modules/i18n/locales/ja.mjs +1130 -0
  92. package/web-ui/modules/i18n/locales/vi.mjs +239 -0
  93. package/web-ui/modules/i18n/locales/zh.mjs +1143 -0
  94. package/web-ui/modules/i18n.dict.mjs +14 -3213
  95. package/web-ui/modules/i18n.mjs +111 -111
  96. package/web-ui/modules/plugins.computed.mjs +3 -3
  97. package/web-ui/modules/plugins.methods.mjs +3 -3
  98. package/web-ui/modules/plugins.storage.mjs +11 -11
  99. package/web-ui/modules/provider-url-display.mjs +17 -17
  100. package/web-ui/modules/sessions-filters-url.mjs +138 -138
  101. package/web-ui/modules/skills.computed.mjs +107 -107
  102. package/web-ui/modules/skills.methods.mjs +513 -513
  103. package/web-ui/partials/index/layout-footer.html +13 -13
  104. package/web-ui/partials/index/layout-header.html +478 -478
  105. package/web-ui/partials/index/modal-config-template-agents.html +185 -185
  106. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  107. package/web-ui/partials/index/modal-health-check.html +45 -45
  108. package/web-ui/partials/index/modal-openclaw-config.html +344 -344
  109. package/web-ui/partials/index/modal-skills.html +200 -200
  110. package/web-ui/partials/index/modal-webhook.html +42 -42
  111. package/web-ui/partials/index/modals-basic.html +263 -263
  112. package/web-ui/partials/index/panel-config-claude.html +187 -157
  113. package/web-ui/partials/index/panel-config-codex.html +205 -176
  114. package/web-ui/partials/index/panel-config-openclaw.html +89 -89
  115. package/web-ui/partials/index/panel-dashboard.html +171 -171
  116. package/web-ui/partials/index/panel-docs.html +114 -114
  117. package/web-ui/partials/index/panel-market.html +104 -104
  118. package/web-ui/partials/index/panel-orchestration.html +391 -391
  119. package/web-ui/partials/index/panel-plugins.html +253 -253
  120. package/web-ui/partials/index/panel-sessions.html +319 -319
  121. package/web-ui/partials/index/panel-settings.html +181 -181
  122. package/web-ui/partials/index/panel-trash.html +82 -82
  123. package/web-ui/partials/index/panel-usage.html +181 -181
  124. package/web-ui/res/json5.min.js +1 -1
  125. package/web-ui/res/vue.global.prod.js +13 -13
  126. package/web-ui/res/vue.runtime.global.prod.js +7 -7
  127. package/web-ui/res/web-ui-render.precompiled.js +7666 -7576
  128. package/web-ui/session-helpers.mjs +602 -602
  129. package/web-ui/source-bundle.cjs +305 -305
  130. package/web-ui/styles/base-theme.css +291 -291
  131. package/web-ui/styles/bridge-pool.css +266 -266
  132. package/web-ui/styles/controls-forms.css +532 -439
  133. package/web-ui/styles/dashboard.css +438 -438
  134. package/web-ui/styles/docs-panel.css +245 -245
  135. package/web-ui/styles/feedback.css +108 -108
  136. package/web-ui/styles/health-check-dialog.css +144 -144
  137. package/web-ui/styles/layout-shell.css +711 -711
  138. package/web-ui/styles/modals-core.css +499 -499
  139. package/web-ui/styles/navigation-panels.css +399 -399
  140. package/web-ui/styles/openclaw-structured.css +616 -616
  141. package/web-ui/styles/plugins-panel.css +564 -564
  142. package/web-ui/styles/responsive.css +501 -501
  143. package/web-ui/styles/sessions-list.css +683 -683
  144. package/web-ui/styles/sessions-preview.css +407 -407
  145. package/web-ui/styles/sessions-toolbar-trash.css +518 -518
  146. package/web-ui/styles/sessions-usage.css +849 -849
  147. package/web-ui/styles/settings-panel.css +419 -419
  148. package/web-ui/styles/skills-list.css +305 -305
  149. package/web-ui/styles/skills-market.css +723 -723
  150. package/web-ui/styles/task-orchestration.css +822 -822
  151. package/web-ui/styles/titles-cards.css +486 -486
  152. package/web-ui/styles/trash-panel.css +90 -90
  153. package/web-ui/styles/webhook.css +115 -115
  154. package/web-ui/styles.css +24 -24
  155. package/web-ui.html +17 -17
@@ -1,601 +1,601 @@
1
- const PROVIDER_NAME_PATTERN = /^[a-zA-Z0-9._-]+$/;
2
- const RESERVED_PROXY_PROVIDER_NAME = 'codexmate-proxy';
3
- const RESERVED_LOCAL_PROVIDER_NAME = 'local';
4
-
5
- function normalizeText(value) {
6
- return typeof value === 'string' ? value.trim() : '';
7
- }
8
-
9
- function normalizeProviderUrl(value) {
10
- return normalizeText(value).replace(/\/+$/g, '');
11
- }
12
-
13
- function isValidHttpUrl(value) {
14
- if (!value) return false;
15
- try {
16
- const parsed = new URL(value);
17
- return parsed.protocol === 'http:' || parsed.protocol === 'https:';
18
- } catch (_) {
19
- return false;
20
- }
21
- }
22
-
23
- function isReservedProviderCreationNameInput(name) {
24
- const normalized = normalizeText(name).toLowerCase();
25
- return normalized === RESERVED_PROXY_PROVIDER_NAME || normalized === RESERVED_LOCAL_PROVIDER_NAME;
26
- }
27
-
28
- function isValidProviderNameInputValue(name) {
29
- return PROVIDER_NAME_PATTERN.test(normalizeText(name));
30
- }
31
-
32
- function isValidProviderUrlInputValue(url) {
33
- return isValidHttpUrl(normalizeProviderUrl(url));
34
- }
35
-
36
- function findProviderByName(list, name) {
37
- const target = normalizeText(name);
38
- if (!target) return null;
39
- return (Array.isArray(list) ? list : []).find((item) => item && normalizeText(item.name) === target) || null;
40
- }
41
-
42
- function normalizeProviderDraftState(target) {
43
- if (!target || typeof target !== 'object') return;
44
- if (typeof target.name === 'string') {
45
- target.name = target.name.trim();
46
- }
47
- if (typeof target.url === 'string') {
48
- target.url = normalizeProviderUrl(target.url);
49
- }
50
- if (typeof target.model === 'string') {
51
- target.model = target.model.trim();
52
- }
53
- if (typeof target.key === 'string') {
54
- target.key = target.key.trim();
55
- }
56
- }
57
-
58
- function maskKeyLocal(key) {
59
- if (!key) return '';
60
- if (key.length <= 8) return '****';
61
- return key.substring(0, 4) + '...' + key.substring(key.length - 4);
62
- }
63
-
64
- function maskKeyForEdit(key) {
65
- if (!key) return '';
66
- if (key.length <= 12) return key.substring(0, 4) + '...' + key.substring(key.length - 4);
67
- return key.substring(0, 8) + '...' + key.substring(key.length - 4);
68
- }
69
-
70
- function getProviderValidationForContext(vm, mode = 'add') {
71
- const draft = mode === 'edit' ? vm.editingProvider : vm.newProvider;
72
- const editingName = mode === 'edit' ? normalizeText(draft && draft.name) : '';
73
- const name = normalizeText(draft && draft.name);
74
- const url = normalizeProviderUrl(draft && draft.url);
75
- const model = normalizeText(draft && draft.model);
76
- const key = normalizeText(draft && draft.key);
77
- const errors = {
78
- name: '',
79
- url: '',
80
- key: '',
81
- model: ''
82
- };
83
-
84
- if (mode === 'add') {
85
- if (!name) {
86
- errors.name = '名称不能为空';
87
- } else if (!isValidProviderNameInputValue(name)) {
88
- errors.name = '名称仅支持字母/数字/._-';
89
- } else if (isReservedProviderCreationNameInput(name)) {
90
- errors.name = 'codexmate-proxy 为保留名称,不可手动添加';
91
- } else if (findProviderByName(vm.providersList, name)) {
92
- errors.name = '名称已存在';
93
- }
94
- } else if (!editingName) {
95
- errors.name = '提供商名称不能为空';
96
- }
97
-
98
- if (!url) {
99
- errors.url = 'URL 必填';
100
- } else if (!isValidProviderUrlInputValue(url)) {
101
- errors.url = 'URL 仅支持 http/https';
102
- }
103
-
104
- if (mode === 'add' && !key) {
105
- errors.key = 'API Key 必填';
106
- }
107
-
108
- if (mode === 'add' && !model) {
109
- errors.model = '模型名称必填';
110
- }
111
-
112
- return {
113
- mode,
114
- name,
115
- url,
116
- key,
117
- model,
118
- errors,
119
- ok: !errors.name && !errors.url && !errors.key && !errors.model
120
- };
121
- }
122
-
123
- function canSubmitProviderForContext(vm, mode = 'add') {
124
- if (mode === 'edit' && vm.editingProvider && (vm.editingProvider.readOnly || vm.editingProvider.nonEditable)) {
125
- return false;
126
- }
127
- return getProviderValidationForContext(vm, mode).ok;
128
- }
129
-
130
- export function createProvidersMethods(options = {}) {
131
- const { api } = options;
132
-
133
- return {
134
- normalizeProviderDraft(mode = 'add') {
135
- normalizeProviderDraftState(mode === 'edit' ? this.editingProvider : this.newProvider);
136
- },
137
-
138
- isReservedProviderCreationName(name) {
139
- return isReservedProviderCreationNameInput(name);
140
- },
141
-
142
- isValidProviderNameInput(name) {
143
- return isValidProviderNameInputValue(name);
144
- },
145
-
146
- isValidProviderUrlInput(url) {
147
- return isValidProviderUrlInputValue(url);
148
- },
149
-
150
- findProviderByName(name) {
151
- return findProviderByName(this.providersList, name);
152
- },
153
-
154
- getProviderValidation(mode = 'add') {
155
- return getProviderValidationForContext(this, mode);
156
- },
157
-
158
- providerFieldError(mode, fieldName) {
159
- const validation = getProviderValidationForContext(this, mode);
160
- return validation && validation.errors && typeof validation.errors[fieldName] === 'string'
161
- ? validation.errors[fieldName]
162
- : '';
163
- },
164
-
165
- canSubmitProvider(mode = 'add') {
166
- return canSubmitProviderForContext(this, mode);
167
- },
168
-
169
- async addProvider() {
170
- normalizeProviderDraftState(this.newProvider);
171
- const validation = getProviderValidationForContext(this, 'add');
172
- if (!validation.ok) {
173
- return this.showMessage(validation.errors.name || validation.errors.url || validation.errors.key || validation.errors.model || '名称、URL、API Key 和模型名称必填', 'error');
174
- }
175
-
176
- try {
177
- const payload = {
178
- name: validation.name,
179
- url: validation.url,
180
- key: validation.key,
181
- model: validation.model
182
- };
183
- if (this.newProvider && this.newProvider.useTransform) {
184
- payload.useTransform = true;
185
- }
186
- const suggestedModel = validation.model;
187
- const res = await api('add-provider', payload);
188
- if (res.error) {
189
- this.showMessage(res.error, 'error');
190
- return;
191
- }
192
-
193
- // 本地更新:构造新 provider 对象并追加到列表
194
- const newProvider = {
195
- name: validation.name,
196
- url: validation.url,
197
- upstreamUrl: '',
198
- codexmate_bridge: payload.useTransform ? 'openai' : '',
199
- key: maskKeyLocal(payload.key),
200
- hasKey: !!payload.key,
201
- models: suggestedModel ? [{ id: suggestedModel, name: suggestedModel, cost: null, contextWindow: undefined, maxTokens: undefined }] : [],
202
- current: false,
203
- readOnly: false,
204
- nonDeletable: false,
205
- nonEditable: false
206
- };
207
- this.providersList = [...this.providersList, newProvider];
208
-
209
- this.showMessage('操作成功', 'success');
210
- this.closeAddModal();
211
-
212
- if (suggestedModel) {
213
- if (!this.currentModels || typeof this.currentModels !== 'object') this.currentModels = {};
214
- this.currentModels[validation.name] = suggestedModel;
215
- }
216
- } catch (e) {
217
- this.showMessage('添加失败', 'error');
218
- }
219
- },
220
-
221
- getCurrentCodexAuthProfile() {
222
- const list = Array.isArray(this.codexAuthProfiles) ? this.codexAuthProfiles : [];
223
- return list.find((item) => !!(item && item.current)) || null;
224
- },
225
-
226
- providerPillState(provider) {
227
- const configured = !!(provider && provider.hasKey);
228
- return {
229
- configured,
230
- text: configured ? this.t('common.configured') : this.t('common.notConfigured')
231
- };
232
- },
233
-
234
- providerPillConfigured(provider) {
235
- return this.providerPillState(provider).configured;
236
- },
237
-
238
- providerPillText(provider) {
239
- return this.providerPillState(provider).text;
240
- },
241
-
242
- isReadOnlyProvider(providerOrName) {
243
- if (!providerOrName) return false;
244
- if (typeof providerOrName === 'object') {
245
- return !!providerOrName.readOnly;
246
- }
247
- const name = String(providerOrName).trim();
248
- if (!name) return false;
249
- const target = (this.providersList || []).find((item) => item && item.name === name);
250
- return !!(target && target.readOnly);
251
- },
252
-
253
- isNonDeletableProvider(providerOrName) {
254
- if (!providerOrName) return false;
255
- if (typeof providerOrName === 'object') {
256
- return !!providerOrName.nonDeletable;
257
- }
258
- const name = String(providerOrName).trim();
259
- if (!name) return false;
260
- const target = (this.providersList || []).find((item) => item && item.name === name);
261
- return !!(target && target.nonDeletable);
262
- },
263
-
264
- shouldShowProviderDelete(provider) {
265
- return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
266
- },
267
-
268
- shouldShowProviderEdit(provider) {
269
- return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
270
- },
271
-
272
- shouldAllowProviderShare(provider) {
273
- return !this.isReadOnlyProvider(provider);
274
- },
275
-
276
- async deleteProvider(name) {
277
- if (this.isNonDeletableProvider(name)) {
278
- this.showMessage('该 provider 为保留项,不可删除', 'info');
279
- return;
280
- }
281
- try {
282
- const res = await api('delete-provider', { name });
283
- if (res.error) {
284
- this.showMessage(res.error, 'error');
285
- return;
286
- }
287
-
288
- // 本地更新:从列表中移除
289
- this.providersList = this.providersList.filter(p => p.name !== name);
290
-
291
- // 清理 currentModels
292
- if (this.currentModels && this.currentModels[name]) {
293
- delete this.currentModels[name];
294
- }
295
-
296
- if (res.switched && res.provider) {
297
- this.currentProvider = res.provider;
298
- if (res.model) this.currentModel = res.model;
299
- // 更新 current 标记
300
- this.providersList = this.providersList.map(p => ({
301
- ...p,
302
- current: p.name === res.provider
303
- }));
304
- this.showMessage(`已删除提供商,自动切换到 ${res.provider}${res.model ? ` / ${res.model}` : ''}`, 'success');
305
- } else {
306
- this.showMessage('操作成功', 'success');
307
- }
308
- } catch (_) {
309
- this.showMessage('删除失败', 'error');
310
- }
311
- },
312
-
313
- openCloneProviderModal(provider) {
314
- const isTransform = !!(provider.codexmate_bridge || '').trim() || /\/bridge\/openai\//.test(provider.url || '');
315
- const cloneUrl = isTransform && provider.upstreamUrl
316
- ? normalizeProviderUrl(provider.upstreamUrl)
317
- : normalizeProviderUrl(provider.url || '');
318
- this.newProvider = {
319
- name: '',
320
- url: cloneUrl,
321
- key: '',
322
- model: '',
323
- useTransform: isTransform
324
- };
325
- this.showAddProviderKey = false;
326
- this.showAddModal = true;
327
- },
328
-
329
- async openEditModal(provider) {
330
- const requestId = Symbol('openEditModal');
331
- this._openEditModalRequestId = requestId;
332
- if (!this.shouldShowProviderEdit(provider)) {
333
- this.showMessage('该 provider 为保留项,不可编辑', 'info');
334
- return;
335
- }
336
- const isTransformProvider = (() => {
337
- if (!provider || typeof provider !== 'object') return false;
338
- const bridge = typeof provider.codexmate_bridge === 'string' ? provider.codexmate_bridge.trim() : '';
339
- if (bridge === 'openai') return true;
340
- const url = String(provider.url || '');
341
- return url.includes('/bridge/openai/');
342
- })();
343
- this.editingProvider = {
344
- name: provider.name,
345
- url: normalizeProviderUrl(provider.url || ''),
346
- key: maskKeyForEdit(provider.key || ''),
347
- readOnly: !!provider.readOnly,
348
- nonEditable: typeof provider.nonEditable === 'boolean'
349
- ? provider.nonEditable
350
- : this.isNonDeletableProvider(provider),
351
- useTransform: isTransformProvider
352
- };
353
- this._editProviderOriginalKey = '';
354
- this._editProviderRealKeyLoaded = false;
355
- this.showEditProviderKey = false;
356
- this.showEditModal = true;
357
-
358
- // 后台加载真实密钥
359
- try {
360
- const res = await api('get-provider-key', { name: provider.name });
361
- if (
362
- this._openEditModalRequestId === requestId
363
- && this.showEditModal
364
- && this.editingProvider
365
- && this.editingProvider.name === provider.name
366
- && res && !res.error
367
- ) {
368
- this._editProviderOriginalKey = typeof res.key === 'string' ? res.key : '';
369
- this._editProviderRealKeyLoaded = true;
370
- // 如果用户未修改输入框,替换为真实密钥
371
- if (this.editingProvider.key === maskKeyForEdit(provider.key || '')) {
372
- this.editingProvider.key = this._editProviderOriginalKey;
373
- }
374
- }
375
- } catch (_) {
376
- // ignore
377
- }
378
-
379
- if (isTransformProvider) {
380
- try {
381
- const res = await api('openai-bridge-get-provider', { name: provider.name });
382
- if (
383
- this._openEditModalRequestId === requestId
384
- && this.showEditModal
385
- && this.editingProvider
386
- && this.editingProvider.name === provider.name
387
- && res && !res.error
388
- && typeof res.baseUrl === 'string'
389
- && res.baseUrl.trim()
390
- ) {
391
- this.editingProvider.url = normalizeProviderUrl(res.baseUrl);
392
- }
393
- } catch (_) {
394
- // ignore
395
- }
396
- }
397
- },
398
-
399
- async updateProvider() {
400
- if (this.editingProvider.readOnly || this.editingProvider.nonEditable) {
401
- this.showMessage('该 provider 为保留项,不可编辑', 'error');
402
- this.closeEditModal();
403
- return;
404
- }
405
- normalizeProviderDraftState(this.editingProvider);
406
- const validation = getProviderValidationForContext(this, 'edit');
407
- if (!validation.ok) {
408
- return this.showMessage(validation.errors.name || validation.errors.url || 'URL 必填', 'error');
409
- }
410
-
411
- const params = { name: validation.name, url: validation.url };
412
- if (this.editingProvider && this.editingProvider.useTransform) {
413
- params.useTransform = true;
414
- }
415
- if (this._editProviderRealKeyLoaded) {
416
- const currentKey = typeof this.editingProvider.key === 'string' ? this.editingProvider.key : '';
417
- const originalKey = typeof this._editProviderOriginalKey === 'string' ? this._editProviderOriginalKey : '';
418
- if (currentKey !== originalKey) {
419
- params.key = currentKey;
420
- }
421
- }
422
- try {
423
- const res = await api('update-provider', params);
424
- if (res.error) {
425
- this.showMessage(res.error, 'error');
426
- return;
427
- }
428
-
429
- // 本地更新:更新列表中对应 provider 的 url 和 key
430
- this.providersList = this.providersList.map(p => {
431
- if (p.name === validation.name) {
432
- const keyUpdated = typeof params.key === 'string';
433
- return {
434
- ...p,
435
- url: validation.url,
436
- key: keyUpdated ? maskKeyLocal(params.key) : p.key,
437
- hasKey: keyUpdated ? !!params.key : p.hasKey
438
- };
439
- }
440
- return p;
441
- });
442
-
443
- this.closeEditModal();
444
- this.showMessage('操作成功', 'success');
445
- } catch (e) {
446
- this.showMessage('更新失败', 'error');
447
- }
448
- },
449
-
450
- closeEditModal() {
451
- this.showEditModal = false;
452
- this.showEditProviderKey = false;
453
- this._editProviderOriginalKey = '';
454
- this._editProviderRealKeyLoaded = false;
455
- this.editingProvider = { name: '', url: '', key: '', readOnly: false, nonEditable: false, useTransform: false };
456
- },
457
-
458
- toggleEditProviderKey() {
459
- this.showEditProviderKey = !this.showEditProviderKey;
460
- },
461
-
462
- async resetConfig() {
463
- if (this.resetConfigLoading) return;
464
- this.resetConfigLoading = true;
465
- try {
466
- const res = await api('reset-config');
467
- if (res.error) {
468
- this.showMessage(res.error, 'error');
469
- return;
470
- }
471
- const backup = res.backupFile ? `(已备份: ${res.backupFile})` : '';
472
- this.showMessage(`配置已重装${backup}`, 'success');
473
- await this.loadAll();
474
- } catch (e) {
475
- this.showMessage('重装失败', 'error');
476
- } finally {
477
- this.resetConfigLoading = false;
478
- }
479
- },
480
-
481
- async addModel() {
482
- if (!this.newModelName || !this.newModelName.trim()) {
483
- return this.showMessage('请输入模型', 'error');
484
- }
485
- try {
486
- const modelName = this.newModelName.trim();
487
- const res = await api('add-model', { model: modelName });
488
- if (res.error) {
489
- this.showMessage(res.error, 'error');
490
- } else {
491
- // 本地更新:在当前 provider 的 models 中追加
492
- this.providersList = this.providersList.map(p => {
493
- if (p.name === this.currentProvider) {
494
- const exists = p.models.some(m => m.id === modelName);
495
- if (!exists) {
496
- return {
497
- ...p,
498
- models: [...p.models, { id: modelName, name: modelName, cost: null, contextWindow: undefined, maxTokens: undefined }]
499
- };
500
- }
501
- }
502
- return p;
503
- });
504
- this.showMessage('操作成功', 'success');
505
- this.closeModelModal();
506
- }
507
- } catch (_) {
508
- this.showMessage('新增模型失败', 'error');
509
- }
510
- },
511
-
512
- async removeModel(model) {
513
- try {
514
- const res = await api('delete-model', { model });
515
- if (res.error) {
516
- this.showMessage(res.error, 'error');
517
- } else {
518
- // 本地更新:从当前 provider 的 models 中移除
519
- this.providersList = this.providersList.map(p => {
520
- if (p.name === this.currentProvider) {
521
- return {
522
- ...p,
523
- models: p.models.filter(m => m.id !== model)
524
- };
525
- }
526
- return p;
527
- });
528
- this.showMessage('操作成功', 'success');
529
- }
530
- } catch (_) {
531
- this.showMessage('删除模型失败', 'error');
532
- }
533
- },
534
-
535
- closeAddModal() {
536
- this.showAddModal = false;
537
- this.showAddProviderKey = false;
538
- this.newProvider = { name: '', url: '', key: '', model: '', useTransform: false };
539
- },
540
-
541
- toggleAddProviderKey() {
542
- this.showAddProviderKey = !this.showAddProviderKey;
543
- },
544
-
545
- closeModelModal() {
546
- this.showModelModal = false;
547
- this.newModelName = '';
548
- },
549
-
550
- formatKey(key) {
551
- if (!key) return '(未设置)';
552
- if (key.length > 10) {
553
- return key.substring(0, 3) + '****' + key.substring(key.length - 3);
554
- }
555
- return '****';
556
- },
557
-
558
- displayApiKey(configName) {
559
- const config = this.claudeConfigs && this.claudeConfigs[configName]
560
- ? this.claudeConfigs[configName]
561
- : null;
562
- const key = config ? config.apiKey : '';
563
- return this.formatKey(key);
564
- },
565
-
566
- async loadLocalBridgeExcluded() {
567
- try {
568
- const res = await api('local-bridge-get-excluded');
569
- if (res && Array.isArray(res.excludedProviders)) {
570
- this.localBridgeExcluded = res.excludedProviders;
571
- }
572
- } catch (e) { /* ignore */ }
573
- },
574
-
575
- async toggleLocalBridgeExcluded(providerName) {
576
- const name = String(providerName || '').trim();
577
- if (!name) return;
578
- const idx = this.localBridgeExcluded.indexOf(name);
579
- const next = [...this.localBridgeExcluded];
580
- if (idx >= 0) {
581
- next.splice(idx, 1);
582
- } else {
583
- next.push(name);
584
- }
585
- try {
586
- const res = await api('local-bridge-set-excluded', { names: next });
587
- if (res && !res.error) {
588
- this.localBridgeExcluded = next;
589
- }
590
- } catch (e) { /* ignore */ }
591
- },
592
-
593
- isLocalBridgeExcluded(providerName) {
594
- return this.localBridgeExcluded.indexOf(String(providerName || '').trim()) >= 0;
595
- },
596
-
597
- localBridgeCandidateProviders() {
598
- return (this.providersList || []).filter(p => p && p.name !== 'local' && p.name !== 'codexmate-proxy' && p.codexmate_bridge !== 'local');
599
- }
600
- };
601
- }
1
+ const PROVIDER_NAME_PATTERN = /^[a-zA-Z0-9._-]+$/;
2
+ const RESERVED_PROXY_PROVIDER_NAME = 'codexmate-proxy';
3
+ const RESERVED_LOCAL_PROVIDER_NAME = 'local';
4
+
5
+ function normalizeText(value) {
6
+ return typeof value === 'string' ? value.trim() : '';
7
+ }
8
+
9
+ function normalizeProviderUrl(value) {
10
+ return normalizeText(value).replace(/\/+$/g, '');
11
+ }
12
+
13
+ function isValidHttpUrl(value) {
14
+ if (!value) return false;
15
+ try {
16
+ const parsed = new URL(value);
17
+ return parsed.protocol === 'http:' || parsed.protocol === 'https:';
18
+ } catch (_) {
19
+ return false;
20
+ }
21
+ }
22
+
23
+ function isReservedProviderCreationNameInput(name) {
24
+ const normalized = normalizeText(name).toLowerCase();
25
+ return normalized === RESERVED_PROXY_PROVIDER_NAME || normalized === RESERVED_LOCAL_PROVIDER_NAME;
26
+ }
27
+
28
+ function isValidProviderNameInputValue(name) {
29
+ return PROVIDER_NAME_PATTERN.test(normalizeText(name));
30
+ }
31
+
32
+ function isValidProviderUrlInputValue(url) {
33
+ return isValidHttpUrl(normalizeProviderUrl(url));
34
+ }
35
+
36
+ function findProviderByName(list, name) {
37
+ const target = normalizeText(name);
38
+ if (!target) return null;
39
+ return (Array.isArray(list) ? list : []).find((item) => item && normalizeText(item.name) === target) || null;
40
+ }
41
+
42
+ function normalizeProviderDraftState(target) {
43
+ if (!target || typeof target !== 'object') return;
44
+ if (typeof target.name === 'string') {
45
+ target.name = target.name.trim();
46
+ }
47
+ if (typeof target.url === 'string') {
48
+ target.url = normalizeProviderUrl(target.url);
49
+ }
50
+ if (typeof target.model === 'string') {
51
+ target.model = target.model.trim();
52
+ }
53
+ if (typeof target.key === 'string') {
54
+ target.key = target.key.trim();
55
+ }
56
+ }
57
+
58
+ function maskKeyLocal(key) {
59
+ if (!key) return '';
60
+ if (key.length <= 8) return '****';
61
+ return key.substring(0, 4) + '...' + key.substring(key.length - 4);
62
+ }
63
+
64
+ function maskKeyForEdit(key) {
65
+ if (!key) return '';
66
+ if (key.length <= 12) return key.substring(0, 4) + '...' + key.substring(key.length - 4);
67
+ return key.substring(0, 8) + '...' + key.substring(key.length - 4);
68
+ }
69
+
70
+ function getProviderValidationForContext(vm, mode = 'add') {
71
+ const draft = mode === 'edit' ? vm.editingProvider : vm.newProvider;
72
+ const editingName = mode === 'edit' ? normalizeText(draft && draft.name) : '';
73
+ const name = normalizeText(draft && draft.name);
74
+ const url = normalizeProviderUrl(draft && draft.url);
75
+ const model = normalizeText(draft && draft.model);
76
+ const key = normalizeText(draft && draft.key);
77
+ const errors = {
78
+ name: '',
79
+ url: '',
80
+ key: '',
81
+ model: ''
82
+ };
83
+
84
+ if (mode === 'add') {
85
+ if (!name) {
86
+ errors.name = '名称不能为空';
87
+ } else if (!isValidProviderNameInputValue(name)) {
88
+ errors.name = '名称仅支持字母/数字/._-';
89
+ } else if (isReservedProviderCreationNameInput(name)) {
90
+ errors.name = 'codexmate-proxy 为保留名称,不可手动添加';
91
+ } else if (findProviderByName(vm.providersList, name)) {
92
+ errors.name = '名称已存在';
93
+ }
94
+ } else if (!editingName) {
95
+ errors.name = '提供商名称不能为空';
96
+ }
97
+
98
+ if (!url) {
99
+ errors.url = 'URL 必填';
100
+ } else if (!isValidProviderUrlInputValue(url)) {
101
+ errors.url = 'URL 仅支持 http/https';
102
+ }
103
+
104
+ if (mode === 'add' && !key) {
105
+ errors.key = 'API Key 必填';
106
+ }
107
+
108
+ if (mode === 'add' && !model) {
109
+ errors.model = '模型名称必填';
110
+ }
111
+
112
+ return {
113
+ mode,
114
+ name,
115
+ url,
116
+ key,
117
+ model,
118
+ errors,
119
+ ok: !errors.name && !errors.url && !errors.key && !errors.model
120
+ };
121
+ }
122
+
123
+ function canSubmitProviderForContext(vm, mode = 'add') {
124
+ if (mode === 'edit' && vm.editingProvider && (vm.editingProvider.readOnly || vm.editingProvider.nonEditable)) {
125
+ return false;
126
+ }
127
+ return getProviderValidationForContext(vm, mode).ok;
128
+ }
129
+
130
+ export function createProvidersMethods(options = {}) {
131
+ const { api } = options;
132
+
133
+ return {
134
+ normalizeProviderDraft(mode = 'add') {
135
+ normalizeProviderDraftState(mode === 'edit' ? this.editingProvider : this.newProvider);
136
+ },
137
+
138
+ isReservedProviderCreationName(name) {
139
+ return isReservedProviderCreationNameInput(name);
140
+ },
141
+
142
+ isValidProviderNameInput(name) {
143
+ return isValidProviderNameInputValue(name);
144
+ },
145
+
146
+ isValidProviderUrlInput(url) {
147
+ return isValidProviderUrlInputValue(url);
148
+ },
149
+
150
+ findProviderByName(name) {
151
+ return findProviderByName(this.providersList, name);
152
+ },
153
+
154
+ getProviderValidation(mode = 'add') {
155
+ return getProviderValidationForContext(this, mode);
156
+ },
157
+
158
+ providerFieldError(mode, fieldName) {
159
+ const validation = getProviderValidationForContext(this, mode);
160
+ return validation && validation.errors && typeof validation.errors[fieldName] === 'string'
161
+ ? validation.errors[fieldName]
162
+ : '';
163
+ },
164
+
165
+ canSubmitProvider(mode = 'add') {
166
+ return canSubmitProviderForContext(this, mode);
167
+ },
168
+
169
+ async addProvider() {
170
+ normalizeProviderDraftState(this.newProvider);
171
+ const validation = getProviderValidationForContext(this, 'add');
172
+ if (!validation.ok) {
173
+ return this.showMessage(validation.errors.name || validation.errors.url || validation.errors.key || validation.errors.model || '名称、URL、API Key 和模型名称必填', 'error');
174
+ }
175
+
176
+ try {
177
+ const payload = {
178
+ name: validation.name,
179
+ url: validation.url,
180
+ key: validation.key,
181
+ model: validation.model
182
+ };
183
+ if (this.newProvider && this.newProvider.useTransform) {
184
+ payload.useTransform = true;
185
+ }
186
+ const suggestedModel = validation.model;
187
+ const res = await api('add-provider', payload);
188
+ if (res.error) {
189
+ this.showMessage(res.error, 'error');
190
+ return;
191
+ }
192
+
193
+ // 本地更新:构造新 provider 对象并追加到列表
194
+ const newProvider = {
195
+ name: validation.name,
196
+ url: validation.url,
197
+ upstreamUrl: '',
198
+ codexmate_bridge: payload.useTransform ? 'openai' : '',
199
+ key: maskKeyLocal(payload.key),
200
+ hasKey: !!payload.key,
201
+ models: suggestedModel ? [{ id: suggestedModel, name: suggestedModel, cost: null, contextWindow: undefined, maxTokens: undefined }] : [],
202
+ current: false,
203
+ readOnly: false,
204
+ nonDeletable: false,
205
+ nonEditable: false
206
+ };
207
+ this.providersList = [...this.providersList, newProvider];
208
+
209
+ this.showMessage('操作成功', 'success');
210
+ this.closeAddModal();
211
+
212
+ if (suggestedModel) {
213
+ if (!this.currentModels || typeof this.currentModels !== 'object') this.currentModels = {};
214
+ this.currentModels[validation.name] = suggestedModel;
215
+ }
216
+ } catch (e) {
217
+ this.showMessage('添加失败', 'error');
218
+ }
219
+ },
220
+
221
+ getCurrentCodexAuthProfile() {
222
+ const list = Array.isArray(this.codexAuthProfiles) ? this.codexAuthProfiles : [];
223
+ return list.find((item) => !!(item && item.current)) || null;
224
+ },
225
+
226
+ providerPillState(provider) {
227
+ const configured = !!(provider && provider.hasKey);
228
+ return {
229
+ configured,
230
+ text: configured ? this.t('common.configured') : this.t('common.notConfigured')
231
+ };
232
+ },
233
+
234
+ providerPillConfigured(provider) {
235
+ return this.providerPillState(provider).configured;
236
+ },
237
+
238
+ providerPillText(provider) {
239
+ return this.providerPillState(provider).text;
240
+ },
241
+
242
+ isReadOnlyProvider(providerOrName) {
243
+ if (!providerOrName) return false;
244
+ if (typeof providerOrName === 'object') {
245
+ return !!providerOrName.readOnly;
246
+ }
247
+ const name = String(providerOrName).trim();
248
+ if (!name) return false;
249
+ const target = (this.providersList || []).find((item) => item && item.name === name);
250
+ return !!(target && target.readOnly);
251
+ },
252
+
253
+ isNonDeletableProvider(providerOrName) {
254
+ if (!providerOrName) return false;
255
+ if (typeof providerOrName === 'object') {
256
+ return !!providerOrName.nonDeletable;
257
+ }
258
+ const name = String(providerOrName).trim();
259
+ if (!name) return false;
260
+ const target = (this.providersList || []).find((item) => item && item.name === name);
261
+ return !!(target && target.nonDeletable);
262
+ },
263
+
264
+ shouldShowProviderDelete(provider) {
265
+ return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
266
+ },
267
+
268
+ shouldShowProviderEdit(provider) {
269
+ return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
270
+ },
271
+
272
+ shouldAllowProviderShare(provider) {
273
+ return !this.isReadOnlyProvider(provider);
274
+ },
275
+
276
+ async deleteProvider(name) {
277
+ if (this.isNonDeletableProvider(name)) {
278
+ this.showMessage('该 provider 为保留项,不可删除', 'info');
279
+ return;
280
+ }
281
+ try {
282
+ const res = await api('delete-provider', { name });
283
+ if (res.error) {
284
+ this.showMessage(res.error, 'error');
285
+ return;
286
+ }
287
+
288
+ // 本地更新:从列表中移除
289
+ this.providersList = this.providersList.filter(p => p.name !== name);
290
+
291
+ // 清理 currentModels
292
+ if (this.currentModels && this.currentModels[name]) {
293
+ delete this.currentModels[name];
294
+ }
295
+
296
+ if (res.switched && res.provider) {
297
+ this.currentProvider = res.provider;
298
+ if (res.model) this.currentModel = res.model;
299
+ // 更新 current 标记
300
+ this.providersList = this.providersList.map(p => ({
301
+ ...p,
302
+ current: p.name === res.provider
303
+ }));
304
+ this.showMessage(`已删除提供商,自动切换到 ${res.provider}${res.model ? ` / ${res.model}` : ''}`, 'success');
305
+ } else {
306
+ this.showMessage('操作成功', 'success');
307
+ }
308
+ } catch (_) {
309
+ this.showMessage('删除失败', 'error');
310
+ }
311
+ },
312
+
313
+ openCloneProviderModal(provider) {
314
+ const isTransform = !!(provider.codexmate_bridge || '').trim() || /\/bridge\/openai\//.test(provider.url || '');
315
+ const cloneUrl = isTransform && provider.upstreamUrl
316
+ ? normalizeProviderUrl(provider.upstreamUrl)
317
+ : normalizeProviderUrl(provider.url || '');
318
+ this.newProvider = {
319
+ name: '',
320
+ url: cloneUrl,
321
+ key: '',
322
+ model: '',
323
+ useTransform: isTransform
324
+ };
325
+ this.showAddProviderKey = false;
326
+ this.showAddModal = true;
327
+ },
328
+
329
+ async openEditModal(provider) {
330
+ const requestId = Symbol('openEditModal');
331
+ this._openEditModalRequestId = requestId;
332
+ if (!this.shouldShowProviderEdit(provider)) {
333
+ this.showMessage('该 provider 为保留项,不可编辑', 'info');
334
+ return;
335
+ }
336
+ const isTransformProvider = (() => {
337
+ if (!provider || typeof provider !== 'object') return false;
338
+ const bridge = typeof provider.codexmate_bridge === 'string' ? provider.codexmate_bridge.trim() : '';
339
+ if (bridge === 'openai') return true;
340
+ const url = String(provider.url || '');
341
+ return url.includes('/bridge/openai/');
342
+ })();
343
+ this.editingProvider = {
344
+ name: provider.name,
345
+ url: normalizeProviderUrl(provider.url || ''),
346
+ key: maskKeyForEdit(provider.key || ''),
347
+ readOnly: !!provider.readOnly,
348
+ nonEditable: typeof provider.nonEditable === 'boolean'
349
+ ? provider.nonEditable
350
+ : this.isNonDeletableProvider(provider),
351
+ useTransform: isTransformProvider
352
+ };
353
+ this._editProviderOriginalKey = '';
354
+ this._editProviderRealKeyLoaded = false;
355
+ this.showEditProviderKey = false;
356
+ this.showEditModal = true;
357
+
358
+ // 后台加载真实密钥
359
+ try {
360
+ const res = await api('get-provider-key', { name: provider.name });
361
+ if (
362
+ this._openEditModalRequestId === requestId
363
+ && this.showEditModal
364
+ && this.editingProvider
365
+ && this.editingProvider.name === provider.name
366
+ && res && !res.error
367
+ ) {
368
+ this._editProviderOriginalKey = typeof res.key === 'string' ? res.key : '';
369
+ this._editProviderRealKeyLoaded = true;
370
+ // 如果用户未修改输入框,替换为真实密钥
371
+ if (this.editingProvider.key === maskKeyForEdit(provider.key || '')) {
372
+ this.editingProvider.key = this._editProviderOriginalKey;
373
+ }
374
+ }
375
+ } catch (_) {
376
+ // ignore
377
+ }
378
+
379
+ if (isTransformProvider) {
380
+ try {
381
+ const res = await api('openai-bridge-get-provider', { name: provider.name });
382
+ if (
383
+ this._openEditModalRequestId === requestId
384
+ && this.showEditModal
385
+ && this.editingProvider
386
+ && this.editingProvider.name === provider.name
387
+ && res && !res.error
388
+ && typeof res.baseUrl === 'string'
389
+ && res.baseUrl.trim()
390
+ ) {
391
+ this.editingProvider.url = normalizeProviderUrl(res.baseUrl);
392
+ }
393
+ } catch (_) {
394
+ // ignore
395
+ }
396
+ }
397
+ },
398
+
399
+ async updateProvider() {
400
+ if (this.editingProvider.readOnly || this.editingProvider.nonEditable) {
401
+ this.showMessage('该 provider 为保留项,不可编辑', 'error');
402
+ this.closeEditModal();
403
+ return;
404
+ }
405
+ normalizeProviderDraftState(this.editingProvider);
406
+ const validation = getProviderValidationForContext(this, 'edit');
407
+ if (!validation.ok) {
408
+ return this.showMessage(validation.errors.name || validation.errors.url || 'URL 必填', 'error');
409
+ }
410
+
411
+ const params = { name: validation.name, url: validation.url };
412
+ if (this.editingProvider && this.editingProvider.useTransform) {
413
+ params.useTransform = true;
414
+ }
415
+ if (this._editProviderRealKeyLoaded) {
416
+ const currentKey = typeof this.editingProvider.key === 'string' ? this.editingProvider.key : '';
417
+ const originalKey = typeof this._editProviderOriginalKey === 'string' ? this._editProviderOriginalKey : '';
418
+ if (currentKey !== originalKey) {
419
+ params.key = currentKey;
420
+ }
421
+ }
422
+ try {
423
+ const res = await api('update-provider', params);
424
+ if (res.error) {
425
+ this.showMessage(res.error, 'error');
426
+ return;
427
+ }
428
+
429
+ // 本地更新:更新列表中对应 provider 的 url 和 key
430
+ this.providersList = this.providersList.map(p => {
431
+ if (p.name === validation.name) {
432
+ const keyUpdated = typeof params.key === 'string';
433
+ return {
434
+ ...p,
435
+ url: validation.url,
436
+ key: keyUpdated ? maskKeyLocal(params.key) : p.key,
437
+ hasKey: keyUpdated ? !!params.key : p.hasKey
438
+ };
439
+ }
440
+ return p;
441
+ });
442
+
443
+ this.closeEditModal();
444
+ this.showMessage('操作成功', 'success');
445
+ } catch (e) {
446
+ this.showMessage('更新失败', 'error');
447
+ }
448
+ },
449
+
450
+ closeEditModal() {
451
+ this.showEditModal = false;
452
+ this.showEditProviderKey = false;
453
+ this._editProviderOriginalKey = '';
454
+ this._editProviderRealKeyLoaded = false;
455
+ this.editingProvider = { name: '', url: '', key: '', readOnly: false, nonEditable: false, useTransform: false };
456
+ },
457
+
458
+ toggleEditProviderKey() {
459
+ this.showEditProviderKey = !this.showEditProviderKey;
460
+ },
461
+
462
+ async resetConfig() {
463
+ if (this.resetConfigLoading) return;
464
+ this.resetConfigLoading = true;
465
+ try {
466
+ const res = await api('reset-config');
467
+ if (res.error) {
468
+ this.showMessage(res.error, 'error');
469
+ return;
470
+ }
471
+ const backup = res.backupFile ? `(已备份: ${res.backupFile})` : '';
472
+ this.showMessage(`配置已重装${backup}`, 'success');
473
+ await this.loadAll();
474
+ } catch (e) {
475
+ this.showMessage('重装失败', 'error');
476
+ } finally {
477
+ this.resetConfigLoading = false;
478
+ }
479
+ },
480
+
481
+ async addModel() {
482
+ if (!this.newModelName || !this.newModelName.trim()) {
483
+ return this.showMessage('请输入模型', 'error');
484
+ }
485
+ try {
486
+ const modelName = this.newModelName.trim();
487
+ const res = await api('add-model', { model: modelName });
488
+ if (res.error) {
489
+ this.showMessage(res.error, 'error');
490
+ } else {
491
+ // 本地更新:在当前 provider 的 models 中追加
492
+ this.providersList = this.providersList.map(p => {
493
+ if (p.name === this.currentProvider) {
494
+ const exists = p.models.some(m => m.id === modelName);
495
+ if (!exists) {
496
+ return {
497
+ ...p,
498
+ models: [...p.models, { id: modelName, name: modelName, cost: null, contextWindow: undefined, maxTokens: undefined }]
499
+ };
500
+ }
501
+ }
502
+ return p;
503
+ });
504
+ this.showMessage('操作成功', 'success');
505
+ this.closeModelModal();
506
+ }
507
+ } catch (_) {
508
+ this.showMessage('新增模型失败', 'error');
509
+ }
510
+ },
511
+
512
+ async removeModel(model) {
513
+ try {
514
+ const res = await api('delete-model', { model });
515
+ if (res.error) {
516
+ this.showMessage(res.error, 'error');
517
+ } else {
518
+ // 本地更新:从当前 provider 的 models 中移除
519
+ this.providersList = this.providersList.map(p => {
520
+ if (p.name === this.currentProvider) {
521
+ return {
522
+ ...p,
523
+ models: p.models.filter(m => m.id !== model)
524
+ };
525
+ }
526
+ return p;
527
+ });
528
+ this.showMessage('操作成功', 'success');
529
+ }
530
+ } catch (_) {
531
+ this.showMessage('删除模型失败', 'error');
532
+ }
533
+ },
534
+
535
+ closeAddModal() {
536
+ this.showAddModal = false;
537
+ this.showAddProviderKey = false;
538
+ this.newProvider = { name: '', url: '', key: '', model: '', useTransform: false };
539
+ },
540
+
541
+ toggleAddProviderKey() {
542
+ this.showAddProviderKey = !this.showAddProviderKey;
543
+ },
544
+
545
+ closeModelModal() {
546
+ this.showModelModal = false;
547
+ this.newModelName = '';
548
+ },
549
+
550
+ formatKey(key) {
551
+ if (!key) return '(未设置)';
552
+ if (key.length > 10) {
553
+ return key.substring(0, 3) + '****' + key.substring(key.length - 3);
554
+ }
555
+ return '****';
556
+ },
557
+
558
+ displayApiKey(configName) {
559
+ const config = this.claudeConfigs && this.claudeConfigs[configName]
560
+ ? this.claudeConfigs[configName]
561
+ : null;
562
+ const key = config ? config.apiKey : '';
563
+ return this.formatKey(key);
564
+ },
565
+
566
+ async loadLocalBridgeExcluded() {
567
+ try {
568
+ const res = await api('local-bridge-get-excluded');
569
+ if (res && Array.isArray(res.excludedProviders)) {
570
+ this.localBridgeExcluded = res.excludedProviders;
571
+ }
572
+ } catch (e) { /* ignore */ }
573
+ },
574
+
575
+ async toggleLocalBridgeExcluded(providerName) {
576
+ const name = String(providerName || '').trim();
577
+ if (!name) return;
578
+ const idx = this.localBridgeExcluded.indexOf(name);
579
+ const next = [...this.localBridgeExcluded];
580
+ if (idx >= 0) {
581
+ next.splice(idx, 1);
582
+ } else {
583
+ next.push(name);
584
+ }
585
+ try {
586
+ const res = await api('local-bridge-set-excluded', { names: next });
587
+ if (res && !res.error) {
588
+ this.localBridgeExcluded = next;
589
+ }
590
+ } catch (e) { /* ignore */ }
591
+ },
592
+
593
+ isLocalBridgeExcluded(providerName) {
594
+ return this.localBridgeExcluded.indexOf(String(providerName || '').trim()) >= 0;
595
+ },
596
+
597
+ localBridgeCandidateProviders() {
598
+ return (this.providersList || []).filter(p => p && p.name !== 'local' && p.name !== 'codexmate-proxy' && p.codexmate_bridge !== 'local');
599
+ }
600
+ };
601
+ }