codexmate 0.0.28 → 0.0.29

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