codexmate 0.0.27 → 0.0.28

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 (137) 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 +1620 -1299
  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 +1489 -1091
  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 +15264 -15251
  23. package/lib/automation.js +404 -404
  24. package/lib/cli-file-utils.js +151 -151
  25. package/lib/cli-models-utils.js +440 -379
  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/download-artifacts.js +92 -92
  32. package/lib/mcp-stdio.js +453 -453
  33. package/lib/task-orchestrator.js +869 -869
  34. package/lib/text-diff.js +303 -303
  35. package/lib/workflow-engine.js +340 -340
  36. package/package.json +3 -1
  37. package/plugins/README.md +20 -20
  38. package/plugins/README.zh-CN.md +20 -20
  39. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  40. package/plugins/prompt-templates/computed.mjs +253 -253
  41. package/plugins/prompt-templates/index.mjs +8 -8
  42. package/plugins/prompt-templates/manifest.mjs +15 -15
  43. package/plugins/prompt-templates/methods.mjs +553 -619
  44. package/plugins/prompt-templates/overview.mjs +91 -90
  45. package/plugins/prompt-templates/ownership.mjs +19 -19
  46. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  47. package/plugins/prompt-templates/storage.mjs +64 -64
  48. package/plugins/registry.mjs +16 -16
  49. package/web-ui/app.js +625 -634
  50. package/web-ui/index.html +35 -35
  51. package/web-ui/logic.agents-diff.mjs +386 -386
  52. package/web-ui/logic.claude.mjs +168 -168
  53. package/web-ui/logic.codex.mjs +56 -0
  54. package/web-ui/logic.mjs +5 -5
  55. package/web-ui/logic.runtime.mjs +128 -128
  56. package/web-ui/logic.session-convert.mjs +70 -70
  57. package/web-ui/logic.sessions.mjs +765 -765
  58. package/web-ui/modules/api.mjs +90 -90
  59. package/web-ui/modules/app.computed.dashboard.mjs +225 -171
  60. package/web-ui/modules/app.computed.index.mjs +17 -17
  61. package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
  62. package/web-ui/modules/app.computed.session.mjs +994 -994
  63. package/web-ui/modules/app.constants.mjs +15 -15
  64. package/web-ui/modules/app.methods.agents.mjs +632 -632
  65. package/web-ui/modules/app.methods.claude-config.mjs +190 -184
  66. package/web-ui/modules/app.methods.codex-config.mjs +892 -860
  67. package/web-ui/modules/app.methods.index.mjs +92 -92
  68. package/web-ui/modules/app.methods.install.mjs +205 -205
  69. package/web-ui/modules/app.methods.navigation.mjs +743 -743
  70. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  71. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  72. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  73. package/web-ui/modules/app.methods.providers.mjs +412 -404
  74. package/web-ui/modules/app.methods.runtime.mjs +345 -345
  75. package/web-ui/modules/app.methods.session-actions.mjs +593 -596
  76. package/web-ui/modules/app.methods.session-browser.mjs +984 -989
  77. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  78. package/web-ui/modules/app.methods.session-trash.mjs +439 -439
  79. package/web-ui/modules/app.methods.startup-claude.mjs +533 -526
  80. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  81. package/web-ui/modules/config-mode.computed.mjs +124 -124
  82. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  83. package/web-ui/modules/i18n.dict.mjs +2109 -2131
  84. package/web-ui/modules/i18n.mjs +56 -56
  85. package/web-ui/modules/plugins.computed.mjs +3 -3
  86. package/web-ui/modules/plugins.methods.mjs +3 -3
  87. package/web-ui/modules/plugins.storage.mjs +11 -11
  88. package/web-ui/modules/provider-url-display.mjs +17 -0
  89. package/web-ui/modules/sessions-filters-url.mjs +85 -85
  90. package/web-ui/modules/skills.computed.mjs +107 -107
  91. package/web-ui/modules/skills.methods.mjs +481 -481
  92. package/web-ui/partials/index/layout-footer.html +13 -13
  93. package/web-ui/partials/index/layout-header.html +475 -475
  94. package/web-ui/partials/index/modal-config-template-agents.html +174 -174
  95. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  96. package/web-ui/partials/index/modal-health-check.html +45 -45
  97. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  98. package/web-ui/partials/index/modal-skills.html +200 -200
  99. package/web-ui/partials/index/modals-basic.html +165 -165
  100. package/web-ui/partials/index/panel-config-claude.html +188 -187
  101. package/web-ui/partials/index/panel-config-codex.html +312 -283
  102. package/web-ui/partials/index/panel-config-openclaw.html +83 -83
  103. package/web-ui/partials/index/panel-dashboard.html +186 -186
  104. package/web-ui/partials/index/panel-docs.html +147 -147
  105. package/web-ui/partials/index/panel-market.html +177 -177
  106. package/web-ui/partials/index/panel-orchestration.html +391 -391
  107. package/web-ui/partials/index/panel-plugins.html +253 -279
  108. package/web-ui/partials/index/panel-sessions.html +316 -326
  109. package/web-ui/partials/index/panel-settings.html +253 -274
  110. package/web-ui/partials/index/panel-usage.html +371 -371
  111. package/web-ui/res/json5.min.js +1 -1
  112. package/web-ui/res/vue.global.prod.js +13 -13
  113. package/web-ui/session-helpers.mjs +576 -576
  114. package/web-ui/source-bundle.cjs +233 -233
  115. package/web-ui/styles/base-theme.css +281 -268
  116. package/web-ui/styles/controls-forms.css +422 -423
  117. package/web-ui/styles/dashboard.css +274 -274
  118. package/web-ui/styles/docs-panel.css +247 -247
  119. package/web-ui/styles/feedback.css +108 -108
  120. package/web-ui/styles/health-check-dialog.css +144 -144
  121. package/web-ui/styles/layout-shell.css +606 -603
  122. package/web-ui/styles/modals-core.css +466 -464
  123. package/web-ui/styles/navigation-panels.css +391 -390
  124. package/web-ui/styles/openclaw-structured.css +266 -266
  125. package/web-ui/styles/plugins-panel.css +523 -523
  126. package/web-ui/styles/responsive.css +454 -454
  127. package/web-ui/styles/sessions-list.css +419 -415
  128. package/web-ui/styles/sessions-preview.css +411 -411
  129. package/web-ui/styles/sessions-toolbar-trash.css +330 -330
  130. package/web-ui/styles/sessions-usage.css +1040 -1040
  131. package/web-ui/styles/settings-panel.css +185 -185
  132. package/web-ui/styles/skills-list.css +303 -303
  133. package/web-ui/styles/skills-market.css +406 -406
  134. package/web-ui/styles/task-orchestration.css +822 -822
  135. package/web-ui/styles/titles-cards.css +472 -408
  136. package/web-ui/styles.css +21 -21
  137. package/web-ui.html +17 -17
@@ -1,404 +1,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 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 res = await api('add-provider', payload);
153
- if (res.error) {
154
- this.showMessage(res.error, 'error');
155
- return;
156
- }
157
-
158
- this.showMessage('操作成功', 'success');
159
- this.closeAddModal();
160
- await this.loadAll();
161
- } catch (e) {
162
- this.showMessage('添加失败', 'error');
163
- }
164
- },
165
-
166
- getCurrentCodexAuthProfile() {
167
- const list = Array.isArray(this.codexAuthProfiles) ? this.codexAuthProfiles : [];
168
- return list.find((item) => !!(item && item.current)) || null;
169
- },
170
-
171
- providerPillState(provider) {
172
- const configured = !!(provider && provider.hasKey);
173
- return {
174
- configured,
175
- text: configured ? '已配置' : '未配置'
176
- };
177
- },
178
-
179
- providerPillConfigured(provider) {
180
- return this.providerPillState(provider).configured;
181
- },
182
-
183
- providerPillText(provider) {
184
- return this.providerPillState(provider).text;
185
- },
186
-
187
- isReadOnlyProvider(providerOrName) {
188
- if (!providerOrName) return false;
189
- if (typeof providerOrName === 'object') {
190
- return !!providerOrName.readOnly;
191
- }
192
- const name = String(providerOrName).trim();
193
- if (!name) return false;
194
- const target = (this.providersList || []).find((item) => item && item.name === name);
195
- return !!(target && target.readOnly);
196
- },
197
-
198
- isNonDeletableProvider(providerOrName) {
199
- if (!providerOrName) return false;
200
- if (typeof providerOrName === 'object') {
201
- return !!providerOrName.nonDeletable;
202
- }
203
- const name = String(providerOrName).trim();
204
- if (!name) return false;
205
- const target = (this.providersList || []).find((item) => item && item.name === name);
206
- return !!(target && target.nonDeletable);
207
- },
208
-
209
- shouldShowProviderDelete(provider) {
210
- return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
211
- },
212
-
213
- shouldShowProviderEdit(provider) {
214
- return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
215
- },
216
-
217
- shouldAllowProviderShare(provider) {
218
- return !this.isReadOnlyProvider(provider);
219
- },
220
-
221
- async deleteProvider(name) {
222
- if (this.isNonDeletableProvider(name)) {
223
- this.showMessage('该 provider 为保留项,不可删除', 'info');
224
- return;
225
- }
226
- try {
227
- const res = await api('delete-provider', { name });
228
- if (res.error) {
229
- this.showMessage(res.error, 'error');
230
- return;
231
- }
232
- if (res.switched && res.provider) {
233
- this.showMessage(`已删除提供商,自动切换到 ${res.provider}${res.model ? ` / ${res.model}` : ''}`, 'success');
234
- } else {
235
- this.showMessage('操作成功', 'success');
236
- }
237
- await this.loadAll();
238
- } catch (_) {
239
- this.showMessage('删除失败', 'error');
240
- }
241
- },
242
-
243
- async openEditModal(provider) {
244
- const requestId = Symbol('openEditModal');
245
- this._openEditModalRequestId = requestId;
246
- if (!this.shouldShowProviderEdit(provider)) {
247
- this.showMessage('该 provider 为保留项,不可编辑', 'info');
248
- return;
249
- }
250
- const isTransformProvider = (() => {
251
- if (!provider || typeof provider !== 'object') return false;
252
- const bridge = typeof provider.codexmate_bridge === 'string' ? provider.codexmate_bridge.trim() : '';
253
- if (bridge === 'openai') return true;
254
- const url = String(provider.url || '');
255
- return url.includes('/bridge/openai/');
256
- })();
257
- this.editingProvider = {
258
- name: provider.name,
259
- url: normalizeProviderUrl(provider.url || ''),
260
- key: '',
261
- readOnly: !!provider.readOnly,
262
- nonEditable: typeof provider.nonEditable === 'boolean'
263
- ? provider.nonEditable
264
- : this.isNonDeletableProvider(provider),
265
- useTransform: isTransformProvider
266
- };
267
- this.showEditModal = true;
268
-
269
- if (isTransformProvider) {
270
- try {
271
- const res = await api('openai-bridge-get-provider', { name: provider.name });
272
- if (
273
- this._openEditModalRequestId === requestId
274
- && this.showEditModal
275
- && this.editingProvider
276
- && this.editingProvider.name === provider.name
277
- && res && !res.error
278
- && typeof res.baseUrl === 'string'
279
- && res.baseUrl.trim()
280
- ) {
281
- this.editingProvider.url = normalizeProviderUrl(res.baseUrl);
282
- }
283
- } catch (_) {
284
- // ignore
285
- }
286
- }
287
- },
288
-
289
- async updateProvider() {
290
- if (this.editingProvider.readOnly || this.editingProvider.nonEditable) {
291
- this.showMessage('该 provider 为保留项,不可编辑', 'error');
292
- this.closeEditModal();
293
- return;
294
- }
295
- normalizeProviderDraftState(this.editingProvider);
296
- const validation = getProviderValidationForContext(this, 'edit');
297
- if (!validation.ok) {
298
- return this.showMessage(validation.errors.name || validation.errors.url || 'URL 必填', 'error');
299
- }
300
-
301
- const params = { name: validation.name, url: validation.url };
302
- if (this.editingProvider && this.editingProvider.useTransform) {
303
- params.useTransform = true;
304
- }
305
- if (typeof this.editingProvider.key === 'string' && this.editingProvider.key.trim()) {
306
- params.key = this.editingProvider.key;
307
- }
308
- try {
309
- const res = await api('update-provider', params);
310
- if (res.error) {
311
- this.showMessage(res.error, 'error');
312
- return;
313
- }
314
- this.closeEditModal();
315
- this.showMessage('操作成功', 'success');
316
- await this.loadAll();
317
- } catch (e) {
318
- this.showMessage('更新失败', 'error');
319
- }
320
- },
321
-
322
- closeEditModal() {
323
- this.showEditModal = false;
324
- this.editingProvider = { name: '', url: '', key: '', readOnly: false, nonEditable: false, useTransform: false };
325
- },
326
-
327
- async resetConfig() {
328
- if (this.resetConfigLoading) return;
329
- this.resetConfigLoading = true;
330
- try {
331
- const res = await api('reset-config');
332
- if (res.error) {
333
- this.showMessage(res.error, 'error');
334
- return;
335
- }
336
- const backup = res.backupFile ? `(已备份: ${res.backupFile})` : '';
337
- this.showMessage(`配置已重装${backup}`, 'success');
338
- await this.loadAll();
339
- } catch (e) {
340
- this.showMessage('重装失败', 'error');
341
- } finally {
342
- this.resetConfigLoading = false;
343
- }
344
- },
345
-
346
- async addModel() {
347
- if (!this.newModelName || !this.newModelName.trim()) {
348
- return this.showMessage('请输入模型', 'error');
349
- }
350
- try {
351
- const res = await api('add-model', { model: this.newModelName.trim() });
352
- if (res.error) {
353
- this.showMessage(res.error, 'error');
354
- } else {
355
- this.showMessage('操作成功', 'success');
356
- this.closeModelModal();
357
- await this.loadAll();
358
- }
359
- } catch (_) {
360
- this.showMessage('新增模型失败', 'error');
361
- }
362
- },
363
-
364
- async removeModel(model) {
365
- try {
366
- const res = await api('delete-model', { model });
367
- if (res.error) {
368
- this.showMessage(res.error, 'error');
369
- } else {
370
- this.showMessage('操作成功', 'success');
371
- await this.loadAll();
372
- }
373
- } catch (_) {
374
- this.showMessage('删除模型失败', 'error');
375
- }
376
- },
377
-
378
- closeAddModal() {
379
- this.showAddModal = false;
380
- this.newProvider = { name: '', url: '', key: '', useTransform: false };
381
- },
382
-
383
- closeModelModal() {
384
- this.showModelModal = false;
385
- this.newModelName = '';
386
- },
387
-
388
- formatKey(key) {
389
- if (!key) return '(未设置)';
390
- if (key.length > 10) {
391
- return key.substring(0, 3) + '****' + key.substring(key.length - 3);
392
- }
393
- return '****';
394
- },
395
-
396
- displayApiKey(configName) {
397
- const config = this.claudeConfigs && this.claudeConfigs[configName]
398
- ? this.claudeConfigs[configName]
399
- : null;
400
- const key = config ? config.apiKey : '';
401
- return this.formatKey(key);
402
- }
403
- };
404
- }
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
+ }