codexmate 0.0.19 → 0.0.21

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 (102) hide show
  1. package/README.en.md +349 -255
  2. package/README.md +284 -248
  3. package/cli/agents-files.js +162 -0
  4. package/cli/archive-helpers.js +446 -0
  5. package/cli/auth-profiles.js +359 -0
  6. package/cli/builtin-proxy.js +580 -0
  7. package/cli/claude-proxy.js +998 -0
  8. package/cli/config-bootstrap.js +384 -0
  9. package/cli/config-health.js +338 -0
  10. package/cli/openclaw-config.js +629 -0
  11. package/cli/skills.js +1141 -0
  12. package/cli/zip-commands.js +510 -0
  13. package/cli.js +13129 -12973
  14. package/lib/cli-file-utils.js +151 -151
  15. package/lib/cli-models-utils.js +419 -152
  16. package/lib/cli-network-utils.js +164 -148
  17. package/lib/cli-path-utils.js +69 -0
  18. package/lib/cli-session-utils.js +121 -121
  19. package/lib/cli-sessions.js +386 -0
  20. package/lib/cli-utils.js +155 -155
  21. package/lib/download-artifacts.js +77 -0
  22. package/lib/mcp-stdio.js +440 -440
  23. package/lib/task-orchestrator.js +869 -0
  24. package/lib/text-diff.js +303 -303
  25. package/lib/workflow-engine.js +340 -340
  26. package/package.json +74 -63
  27. package/res/json5.min.js +1 -1
  28. package/res/vue.global.prod.js +13 -0
  29. package/web-ui/app.js +530 -5548
  30. package/web-ui/index.html +33 -2246
  31. package/web-ui/logic.agents-diff.mjs +386 -0
  32. package/web-ui/logic.claude.mjs +168 -0
  33. package/web-ui/logic.mjs +5 -793
  34. package/web-ui/logic.runtime.mjs +124 -0
  35. package/web-ui/logic.sessions.mjs +581 -0
  36. package/web-ui/modules/api.mjs +90 -0
  37. package/web-ui/modules/app.computed.dashboard.mjs +113 -0
  38. package/web-ui/modules/app.computed.index.mjs +15 -0
  39. package/web-ui/modules/app.computed.main-tabs.mjs +195 -0
  40. package/web-ui/modules/app.computed.session.mjs +507 -0
  41. package/web-ui/modules/app.constants.mjs +15 -0
  42. package/web-ui/modules/app.methods.agents.mjs +493 -0
  43. package/web-ui/modules/app.methods.claude-config.mjs +174 -0
  44. package/web-ui/modules/app.methods.codex-config.mjs +640 -0
  45. package/web-ui/modules/app.methods.index.mjs +88 -0
  46. package/web-ui/modules/app.methods.install.mjs +149 -0
  47. package/web-ui/modules/app.methods.navigation.mjs +619 -0
  48. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -0
  49. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -0
  50. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -0
  51. package/web-ui/modules/app.methods.providers.mjs +363 -0
  52. package/web-ui/modules/app.methods.runtime.mjs +323 -0
  53. package/web-ui/modules/app.methods.session-actions.mjs +520 -0
  54. package/web-ui/modules/app.methods.session-browser.mjs +626 -0
  55. package/web-ui/modules/app.methods.session-timeline.mjs +448 -0
  56. package/web-ui/modules/app.methods.session-trash.mjs +422 -0
  57. package/web-ui/modules/app.methods.startup-claude.mjs +412 -0
  58. package/web-ui/modules/app.methods.task-orchestration.mjs +471 -0
  59. package/web-ui/modules/config-mode.computed.mjs +126 -124
  60. package/web-ui/modules/skills.computed.mjs +107 -107
  61. package/web-ui/modules/skills.methods.mjs +481 -481
  62. package/web-ui/partials/index/layout-footer.html +13 -0
  63. package/web-ui/partials/index/layout-header.html +402 -0
  64. package/web-ui/partials/index/modal-config-template-agents.html +125 -0
  65. package/web-ui/partials/index/modal-confirm-toast.html +32 -0
  66. package/web-ui/partials/index/modal-health-check.html +72 -0
  67. package/web-ui/partials/index/modal-openclaw-config.html +280 -0
  68. package/web-ui/partials/index/modal-skills.html +184 -0
  69. package/web-ui/partials/index/modals-basic.html +156 -0
  70. package/web-ui/partials/index/panel-config-claude.html +126 -0
  71. package/web-ui/partials/index/panel-config-codex.html +237 -0
  72. package/web-ui/partials/index/panel-config-openclaw.html +78 -0
  73. package/web-ui/partials/index/panel-docs.html +130 -0
  74. package/web-ui/partials/index/panel-market.html +174 -0
  75. package/web-ui/partials/index/panel-orchestration.html +397 -0
  76. package/web-ui/partials/index/panel-sessions.html +292 -0
  77. package/web-ui/partials/index/panel-settings.html +190 -0
  78. package/web-ui/partials/index/panel-usage.html +213 -0
  79. package/web-ui/session-helpers.mjs +559 -362
  80. package/web-ui/source-bundle.cjs +233 -0
  81. package/web-ui/styles/base-theme.css +271 -0
  82. package/web-ui/styles/controls-forms.css +360 -0
  83. package/web-ui/styles/docs-panel.css +182 -0
  84. package/web-ui/styles/feedback.css +108 -0
  85. package/web-ui/styles/health-check-dialog.css +144 -0
  86. package/web-ui/styles/layout-shell.css +376 -0
  87. package/web-ui/styles/modals-core.css +464 -0
  88. package/web-ui/styles/navigation-panels.css +348 -0
  89. package/web-ui/styles/openclaw-structured.css +266 -0
  90. package/web-ui/styles/responsive.css +450 -0
  91. package/web-ui/styles/sessions-list.css +400 -0
  92. package/web-ui/styles/sessions-preview.css +411 -0
  93. package/web-ui/styles/sessions-toolbar-trash.css +243 -0
  94. package/web-ui/styles/sessions-usage.css +628 -0
  95. package/web-ui/styles/skills-list.css +296 -0
  96. package/web-ui/styles/skills-market.css +335 -0
  97. package/web-ui/styles/task-orchestration.css +776 -0
  98. package/web-ui/styles/titles-cards.css +408 -0
  99. package/web-ui/styles.css +18 -4668
  100. package/web-ui.html +17 -17
  101. package/res/screenshot.png +0 -0
  102. package/res/vue.global.js +0 -18552
@@ -0,0 +1,363 @@
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 res = await api('add-provider', {
145
+ name: validation.name,
146
+ url: validation.url,
147
+ key: this.newProvider.key || ''
148
+ });
149
+ if (res.error) {
150
+ this.showMessage(res.error, 'error');
151
+ return;
152
+ }
153
+
154
+ this.showMessage('操作成功', 'success');
155
+ this.closeAddModal();
156
+ await this.loadAll();
157
+ } catch (e) {
158
+ this.showMessage('添加失败', 'error');
159
+ }
160
+ },
161
+
162
+ getCurrentCodexAuthProfile() {
163
+ const list = Array.isArray(this.codexAuthProfiles) ? this.codexAuthProfiles : [];
164
+ return list.find((item) => !!(item && item.current)) || null;
165
+ },
166
+
167
+ providerPillState(provider) {
168
+ const configured = !!(provider && provider.hasKey);
169
+ return {
170
+ configured,
171
+ text: configured ? '已配置' : '未配置'
172
+ };
173
+ },
174
+
175
+ providerPillConfigured(provider) {
176
+ return this.providerPillState(provider).configured;
177
+ },
178
+
179
+ providerPillText(provider) {
180
+ return this.providerPillState(provider).text;
181
+ },
182
+
183
+ isReadOnlyProvider(providerOrName) {
184
+ if (!providerOrName) return false;
185
+ if (typeof providerOrName === 'object') {
186
+ return !!providerOrName.readOnly;
187
+ }
188
+ const name = String(providerOrName).trim();
189
+ if (!name) return false;
190
+ const target = (this.providersList || []).find((item) => item && item.name === name);
191
+ return !!(target && target.readOnly);
192
+ },
193
+
194
+ isNonDeletableProvider(providerOrName) {
195
+ if (!providerOrName) return false;
196
+ if (typeof providerOrName === 'object') {
197
+ return !!providerOrName.nonDeletable;
198
+ }
199
+ const name = String(providerOrName).trim();
200
+ if (!name) return false;
201
+ const target = (this.providersList || []).find((item) => item && item.name === name);
202
+ return !!(target && target.nonDeletable);
203
+ },
204
+
205
+ shouldShowProviderDelete(provider) {
206
+ return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
207
+ },
208
+
209
+ shouldShowProviderEdit(provider) {
210
+ return !this.isReadOnlyProvider(provider) && !this.isNonDeletableProvider(provider);
211
+ },
212
+
213
+ shouldAllowProviderShare(provider) {
214
+ return !this.isReadOnlyProvider(provider);
215
+ },
216
+
217
+ async deleteProvider(name) {
218
+ if (this.isNonDeletableProvider(name)) {
219
+ this.showMessage('该 provider 为保留项,不可删除', 'info');
220
+ return;
221
+ }
222
+ try {
223
+ const res = await api('delete-provider', { name });
224
+ if (res.error) {
225
+ this.showMessage(res.error, 'error');
226
+ return;
227
+ }
228
+ if (res.switched && res.provider) {
229
+ this.showMessage(`已删除提供商,自动切换到 ${res.provider}${res.model ? ` / ${res.model}` : ''}`, 'success');
230
+ } else {
231
+ this.showMessage('操作成功', 'success');
232
+ }
233
+ await this.loadAll();
234
+ } catch (_) {
235
+ this.showMessage('删除失败', 'error');
236
+ }
237
+ },
238
+
239
+ openEditModal(provider) {
240
+ if (!this.shouldShowProviderEdit(provider)) {
241
+ this.showMessage('该 provider 为保留项,不可编辑', 'info');
242
+ return;
243
+ }
244
+ this.editingProvider = {
245
+ name: provider.name,
246
+ url: normalizeProviderUrl(provider.url || ''),
247
+ key: '',
248
+ readOnly: !!provider.readOnly,
249
+ nonEditable: this.isNonDeletableProvider(provider)
250
+ };
251
+ this.showEditModal = true;
252
+ },
253
+
254
+ async updateProvider() {
255
+ if (this.editingProvider.readOnly || this.editingProvider.nonEditable) {
256
+ this.showMessage('该 provider 为保留项,不可编辑', 'error');
257
+ this.closeEditModal();
258
+ return;
259
+ }
260
+ normalizeProviderDraftState(this.editingProvider);
261
+ const validation = getProviderValidationForContext(this, 'edit');
262
+ if (!validation.ok) {
263
+ return this.showMessage(validation.errors.name || validation.errors.url || 'URL 必填', 'error');
264
+ }
265
+
266
+ const params = { name: validation.name, url: validation.url };
267
+ if (typeof this.editingProvider.key === 'string' && this.editingProvider.key.trim()) {
268
+ params.key = this.editingProvider.key;
269
+ }
270
+ try {
271
+ const res = await api('update-provider', params);
272
+ if (res.error) {
273
+ this.showMessage(res.error, 'error');
274
+ return;
275
+ }
276
+ this.closeEditModal();
277
+ this.showMessage('操作成功', 'success');
278
+ await this.loadAll();
279
+ } catch (e) {
280
+ this.showMessage('更新失败', 'error');
281
+ }
282
+ },
283
+
284
+ closeEditModal() {
285
+ this.showEditModal = false;
286
+ this.editingProvider = { name: '', url: '', key: '', readOnly: false, nonEditable: false };
287
+ },
288
+
289
+ async resetConfig() {
290
+ if (this.resetConfigLoading) return;
291
+ this.resetConfigLoading = true;
292
+ try {
293
+ const res = await api('reset-config');
294
+ if (res.error) {
295
+ this.showMessage(res.error, 'error');
296
+ return;
297
+ }
298
+ const backup = res.backupFile ? `(已备份: ${res.backupFile})` : '';
299
+ this.showMessage(`配置已重装${backup}`, 'success');
300
+ await this.loadAll();
301
+ } catch (e) {
302
+ this.showMessage('重装失败', 'error');
303
+ } finally {
304
+ this.resetConfigLoading = false;
305
+ }
306
+ },
307
+
308
+ async addModel() {
309
+ if (!this.newModelName || !this.newModelName.trim()) {
310
+ return this.showMessage('请输入模型', 'error');
311
+ }
312
+ try {
313
+ const res = await api('add-model', { model: this.newModelName.trim() });
314
+ if (res.error) {
315
+ this.showMessage(res.error, 'error');
316
+ } else {
317
+ this.showMessage('操作成功', 'success');
318
+ this.closeModelModal();
319
+ await this.loadAll();
320
+ }
321
+ } catch (_) {
322
+ this.showMessage('新增模型失败', 'error');
323
+ }
324
+ },
325
+
326
+ async removeModel(model) {
327
+ try {
328
+ const res = await api('delete-model', { model });
329
+ if (res.error) {
330
+ this.showMessage(res.error, 'error');
331
+ } else {
332
+ this.showMessage('操作成功', 'success');
333
+ await this.loadAll();
334
+ }
335
+ } catch (_) {
336
+ this.showMessage('删除模型失败', 'error');
337
+ }
338
+ },
339
+
340
+ closeAddModal() {
341
+ this.showAddModal = false;
342
+ this.newProvider = { name: '', url: '', key: '' };
343
+ },
344
+
345
+ closeModelModal() {
346
+ this.showModelModal = false;
347
+ this.newModelName = '';
348
+ },
349
+
350
+ formatKey(key) {
351
+ if (!key) return '(未设置)';
352
+ if (key.length > 10) {
353
+ return key.substring(0, 3) + '****' + key.substring(key.length - 3);
354
+ }
355
+ return '****';
356
+ },
357
+
358
+ displayApiKey(configName) {
359
+ const key = this.claudeConfigs[configName]?.apiKey;
360
+ return this.formatKey(key);
361
+ }
362
+ };
363
+ }
@@ -0,0 +1,323 @@
1
+ import {
2
+ buildSpeedTestIssue,
3
+ formatLatency
4
+ } from '../logic.mjs';
5
+
6
+ function clearProgressResetTimer(context, timerKey) {
7
+ if (!context || !timerKey || !context[timerKey]) {
8
+ return;
9
+ }
10
+ clearTimeout(context[timerKey]);
11
+ context[timerKey] = null;
12
+ }
13
+
14
+ function scheduleProgressResetTimer(context, timerKey, progressKey, delayMs = 800) {
15
+ if (!context || !timerKey || !progressKey) {
16
+ return;
17
+ }
18
+ clearProgressResetTimer(context, timerKey);
19
+ context[timerKey] = setTimeout(() => {
20
+ context[progressKey] = 0;
21
+ context[timerKey] = null;
22
+ }, delayMs);
23
+ }
24
+
25
+ export function createRuntimeMethods(options = {}) {
26
+ const { api } = options;
27
+
28
+ return {
29
+ formatLatency,
30
+
31
+ buildSpeedTestIssue(name, result) {
32
+ return buildSpeedTestIssue(name, result);
33
+ },
34
+
35
+ async runSpeedTest(name, options = {}) {
36
+ if (!name || this.speedLoading[name]) return null;
37
+ const silent = !!options.silent;
38
+ this.speedLoading[name] = true;
39
+ try {
40
+ const res = await api('speed-test', { name });
41
+ if (res.error) {
42
+ this.speedResults[name] = { ok: false, error: res.error };
43
+ if (!silent) {
44
+ this.showMessage(res.error, 'error');
45
+ }
46
+ return { ok: false, error: res.error };
47
+ }
48
+ this.speedResults[name] = res;
49
+ if (!silent) {
50
+ const status = res.status ? ` (${res.status})` : '';
51
+ this.showMessage(`Speed ${name}: ${this.formatLatency(res)}${status}`, 'success');
52
+ }
53
+ return res;
54
+ } catch (e) {
55
+ const message = e && e.message ? e.message : 'Speed test failed';
56
+ this.speedResults[name] = { ok: false, error: message };
57
+ if (!silent) {
58
+ this.showMessage(message, 'error');
59
+ }
60
+ return { ok: false, error: message };
61
+ } finally {
62
+ this.speedLoading[name] = false;
63
+ }
64
+ },
65
+
66
+ async runClaudeSpeedTest(name, config) {
67
+ if (!name || this.claudeSpeedLoading[name]) return null;
68
+ const baseUrl = config && typeof config.baseUrl === 'string' ? config.baseUrl.trim() : '';
69
+ this.claudeSpeedLoading[name] = true;
70
+ try {
71
+ if (!baseUrl) {
72
+ const res = { ok: false, error: 'Missing base URL' };
73
+ this.claudeSpeedResults[name] = res;
74
+ return res;
75
+ }
76
+ const res = await api('speed-test', { url: baseUrl });
77
+ if (res.error) {
78
+ this.claudeSpeedResults[name] = { ok: false, error: res.error };
79
+ return { ok: false, error: res.error };
80
+ }
81
+ this.claudeSpeedResults[name] = res;
82
+ return res;
83
+ } catch (e) {
84
+ const message = e && e.message ? e.message : 'Speed test failed';
85
+ const res = { ok: false, error: message };
86
+ this.claudeSpeedResults[name] = res;
87
+ return res;
88
+ } finally {
89
+ this.claudeSpeedLoading[name] = false;
90
+ }
91
+ },
92
+
93
+ async downloadClaudeDirectory() {
94
+ if (this.claudeDownloadLoading) return;
95
+ clearProgressResetTimer(this, '__claudeDownloadResetTimer');
96
+ this.claudeDownloadLoading = true;
97
+ this.claudeDownloadProgress = 5;
98
+ this.claudeDownloadTimer = setInterval(() => {
99
+ if (this.claudeDownloadProgress < 90) {
100
+ this.claudeDownloadProgress += 5;
101
+ }
102
+ }, 400);
103
+ try {
104
+ const res = await api('download-claude-dir');
105
+ if (res && res.error) {
106
+ this.showMessage(res.error, 'error');
107
+ return;
108
+ }
109
+ if (!res || res.success !== true || !res.fileName) {
110
+ this.showMessage('备份失败', 'error');
111
+ return;
112
+ }
113
+ this.claudeDownloadProgress = 100;
114
+ const downloadUrl = `/download/${encodeURIComponent(res.fileName)}`;
115
+ const link = document.createElement('a');
116
+ link.href = downloadUrl;
117
+ link.download = res.fileName;
118
+ document.body.appendChild(link);
119
+ link.click();
120
+ document.body.removeChild(link);
121
+ this.showMessage('备份成功,开始下载', 'success');
122
+ } catch (e) {
123
+ this.showMessage('备份失败:' + (e && e.message ? e.message : '未知错误'), 'error');
124
+ } finally {
125
+ if (this.claudeDownloadTimer) {
126
+ clearInterval(this.claudeDownloadTimer);
127
+ this.claudeDownloadTimer = null;
128
+ }
129
+ this.claudeDownloadLoading = false;
130
+ scheduleProgressResetTimer(this, '__claudeDownloadResetTimer', 'claudeDownloadProgress');
131
+ }
132
+ },
133
+
134
+ async downloadCodexDirectory() {
135
+ if (this.codexDownloadLoading) return;
136
+ clearProgressResetTimer(this, '__codexDownloadResetTimer');
137
+ this.codexDownloadLoading = true;
138
+ this.codexDownloadProgress = 5;
139
+ this.codexDownloadTimer = setInterval(() => {
140
+ if (this.codexDownloadProgress < 90) {
141
+ this.codexDownloadProgress += 5;
142
+ }
143
+ }, 400);
144
+ try {
145
+ const res = await api('download-codex-dir');
146
+ if (res && res.error) {
147
+ this.showMessage(res.error, 'error');
148
+ return;
149
+ }
150
+ if (!res || res.success !== true || !res.fileName) {
151
+ this.showMessage('备份失败', 'error');
152
+ return;
153
+ }
154
+ this.codexDownloadProgress = 100;
155
+ const downloadUrl = `/download/${encodeURIComponent(res.fileName)}`;
156
+ const link = document.createElement('a');
157
+ link.href = downloadUrl;
158
+ link.download = res.fileName;
159
+ document.body.appendChild(link);
160
+ link.click();
161
+ document.body.removeChild(link);
162
+ this.showMessage('备份成功,开始下载', 'success');
163
+ } catch (e) {
164
+ this.showMessage('备份失败:' + (e && e.message ? e.message : '未知错误'), 'error');
165
+ } finally {
166
+ if (this.codexDownloadTimer) {
167
+ clearInterval(this.codexDownloadTimer);
168
+ this.codexDownloadTimer = null;
169
+ }
170
+ this.codexDownloadLoading = false;
171
+ scheduleProgressResetTimer(this, '__codexDownloadResetTimer', 'codexDownloadProgress');
172
+ }
173
+ },
174
+
175
+ triggerClaudeImport() {
176
+ const input = this.$refs.claudeImportInput;
177
+ if (input) {
178
+ input.value = '';
179
+ input.click();
180
+ }
181
+ },
182
+
183
+ triggerCodexImport() {
184
+ const input = this.$refs.codexImportInput;
185
+ if (input) {
186
+ input.value = '';
187
+ input.click();
188
+ }
189
+ },
190
+
191
+ handleClaudeImportChange(event) {
192
+ const file = event && event.target && event.target.files ? event.target.files[0] : null;
193
+ if (file) {
194
+ void this.importBackupFile('claude', file);
195
+ }
196
+ },
197
+
198
+ handleCodexImportChange(event) {
199
+ const file = event && event.target && event.target.files ? event.target.files[0] : null;
200
+ if (file) {
201
+ void this.importBackupFile('codex', file);
202
+ }
203
+ },
204
+
205
+ async importBackupFile(type, file) {
206
+ const maxSize = 200 * 1024 * 1024;
207
+ const loadingKey = type === 'claude' ? 'claudeImportLoading' : 'codexImportLoading';
208
+ if (this[loadingKey]) {
209
+ this.resetImportInput(type);
210
+ return;
211
+ }
212
+ if (file.size > maxSize) {
213
+ this.showMessage('备份文件过大,限制 200MB', 'error');
214
+ this.resetImportInput(type);
215
+ return;
216
+ }
217
+ this[loadingKey] = true;
218
+ try {
219
+ const base64 = await this.readFileAsBase64(file);
220
+ const action = type === 'claude' ? 'restore-claude-dir' : 'restore-codex-dir';
221
+ const res = await api(action, {
222
+ fileName: file.name || `${type}-backup.zip`,
223
+ fileBase64: base64
224
+ });
225
+ if (res && res.error) {
226
+ this.showMessage(res.error, 'error');
227
+ return;
228
+ }
229
+ const backupTip = res && res.backupPath ? `,原配置已备份到临时文件:${res.backupPath}` : '';
230
+ this.showMessage(`导入成功${backupTip}`, 'success');
231
+ try {
232
+ if (type === 'claude') {
233
+ await this.refreshClaudeSelectionFromSettings({ silent: true });
234
+ } else {
235
+ await this.loadAll();
236
+ }
237
+ } catch (_) {
238
+ this.showMessage('导入已完成,但界面刷新失败,请手动刷新', 'error');
239
+ }
240
+ } catch (e) {
241
+ this.showMessage('导入失败:' + (e && e.message ? e.message : '未知错误'), 'error');
242
+ } finally {
243
+ this[loadingKey] = false;
244
+ this.resetImportInput(type);
245
+ }
246
+ },
247
+
248
+ readFileAsBase64(file) {
249
+ return new Promise((resolve, reject) => {
250
+ const reader = new FileReader();
251
+ reader.onload = () => {
252
+ const result = reader.result;
253
+ if (result instanceof ArrayBuffer) {
254
+ resolve(this.arrayBufferToBase64(result));
255
+ return;
256
+ }
257
+ if (typeof result === 'string') {
258
+ const idx = result.indexOf('base64,');
259
+ resolve(idx >= 0 ? result.slice(idx + 7) : result);
260
+ return;
261
+ }
262
+ reject(new Error('不支持的文件读取结果'));
263
+ };
264
+ reader.onerror = () => reject(new Error('读取文件失败'));
265
+ reader.readAsArrayBuffer(file);
266
+ });
267
+ },
268
+
269
+ arrayBufferToBase64(buffer) {
270
+ const bytes = new Uint8Array(buffer);
271
+ const chunkSize = 0x8000;
272
+ let binary = '';
273
+ for (let i = 0; i < bytes.byteLength; i += chunkSize) {
274
+ binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
275
+ }
276
+ return btoa(binary);
277
+ },
278
+
279
+ resetImportInput(type) {
280
+ const refName = type === 'claude' ? 'claudeImportInput' : 'codexImportInput';
281
+ const el = this.$refs[refName];
282
+ if (el) {
283
+ el.value = '';
284
+ }
285
+ },
286
+
287
+ async loadCodexAuthProfiles(options = {}) {
288
+ const silent = !!options.silent;
289
+ try {
290
+ const res = await api('list-auth-profiles');
291
+ if (res && res.error) {
292
+ if (!silent) {
293
+ this.showMessage(res.error, 'error');
294
+ }
295
+ return;
296
+ }
297
+ const list = Array.isArray(res && res.profiles) ? res.profiles : [];
298
+ this.codexAuthProfiles = list.sort((a, b) => {
299
+ if (!!a.current !== !!b.current) {
300
+ return a.current ? -1 : 1;
301
+ }
302
+ return String(a.name || '').localeCompare(String(b.name || ''));
303
+ });
304
+ } catch (e) {
305
+ if (!silent) {
306
+ this.showMessage('读取认证列表失败', 'error');
307
+ }
308
+ }
309
+ },
310
+
311
+ showMessage(text, type) {
312
+ if (this._messageTimer) {
313
+ clearTimeout(this._messageTimer);
314
+ }
315
+ this.message = text;
316
+ this.messageType = type || 'info';
317
+ this._messageTimer = setTimeout(() => {
318
+ this.message = '';
319
+ this._messageTimer = null;
320
+ }, 3000);
321
+ }
322
+ };
323
+ }