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,493 @@
1
+ import {
2
+ buildAgentsDiffPreview,
3
+ buildAgentsDiffPreviewRequest,
4
+ isAgentsDiffPreviewPayloadTooLarge,
5
+ shouldApplyAgentsDiffPreviewResponse
6
+ } from '../logic.mjs';
7
+
8
+ function isValidOpenclawWorkspaceFileName(fileName) {
9
+ if (typeof fileName !== 'string') {
10
+ return false;
11
+ }
12
+ const normalized = fileName.trim();
13
+ if (!normalized || !normalized.endsWith('.md')) {
14
+ return false;
15
+ }
16
+ if (normalized.startsWith('/') || normalized.includes('\\') || normalized.includes('/') || normalized.includes('..')) {
17
+ return false;
18
+ }
19
+ return true;
20
+ }
21
+
22
+ function issueLatestRequestToken(context, key) {
23
+ const token = (Number(context[key]) || 0) + 1;
24
+ context[key] = token;
25
+ return token;
26
+ }
27
+
28
+ function isLatestRequestToken(context, key, token) {
29
+ return !!context && context[key] === token;
30
+ }
31
+
32
+ export function createAgentsMethods(options = {}) {
33
+ const {
34
+ api,
35
+ apiWithMeta
36
+ } = options;
37
+
38
+ return {
39
+ async openAgentsEditor() {
40
+ this.setAgentsModalContext('codex');
41
+ const requestToken = issueLatestRequestToken(this, '_agentsOpenRequestToken');
42
+ this.agentsLoading = true;
43
+ try {
44
+ const res = await api('get-agents-file');
45
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
46
+ return;
47
+ }
48
+ if (res.error) {
49
+ this.showMessage(res.error, 'error');
50
+ return;
51
+ }
52
+ this.agentsContent = res.content || '';
53
+ this.agentsOriginalContent = this.agentsContent;
54
+ this.agentsPath = res.path || '';
55
+ this.agentsExists = !!res.exists;
56
+ this.agentsLineEnding = res.lineEnding === '\r\n' ? '\r\n' : '\n';
57
+ this.resetAgentsDiffState();
58
+ this.showAgentsModal = true;
59
+ } catch (e) {
60
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
61
+ return;
62
+ }
63
+ this.showMessage('加载文件失败', 'error');
64
+ } finally {
65
+ if (isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
66
+ this.agentsLoading = false;
67
+ }
68
+ }
69
+ },
70
+
71
+ async openOpenclawAgentsEditor() {
72
+ this.setAgentsModalContext('openclaw');
73
+ const requestToken = issueLatestRequestToken(this, '_agentsOpenRequestToken');
74
+ this.agentsLoading = true;
75
+ try {
76
+ const res = await api('get-openclaw-agents-file');
77
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
78
+ return;
79
+ }
80
+ if (res.error) {
81
+ this.showMessage(res.error, 'error');
82
+ return;
83
+ }
84
+ if (res.configError) {
85
+ this.showMessage(`OpenClaw 配置解析失败,已使用默认 Workspace:${res.configError}`, 'error');
86
+ }
87
+ this.agentsContent = res.content || '';
88
+ this.agentsOriginalContent = this.agentsContent;
89
+ this.agentsPath = res.path || '';
90
+ this.agentsExists = !!res.exists;
91
+ this.agentsLineEnding = res.lineEnding === '\r\n' ? '\r\n' : '\n';
92
+ this.resetAgentsDiffState();
93
+ this.showAgentsModal = true;
94
+ } catch (e) {
95
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
96
+ return;
97
+ }
98
+ this.showMessage('加载文件失败', 'error');
99
+ } finally {
100
+ if (isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
101
+ this.agentsLoading = false;
102
+ }
103
+ }
104
+ },
105
+
106
+ async openOpenclawWorkspaceEditor() {
107
+ const fileName = (this.openclawWorkspaceFileName || '').trim();
108
+ if (!fileName) {
109
+ this.showMessage('请输入文件名', 'error');
110
+ return;
111
+ }
112
+ if (!isValidOpenclawWorkspaceFileName(fileName)) {
113
+ this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
114
+ return;
115
+ }
116
+ this.setAgentsModalContext('openclaw-workspace', { fileName });
117
+ const requestToken = issueLatestRequestToken(this, '_agentsOpenRequestToken');
118
+ this.agentsLoading = true;
119
+ try {
120
+ const res = await api('get-openclaw-workspace-file', { fileName });
121
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
122
+ return;
123
+ }
124
+ if (res.error) {
125
+ this.showMessage(res.error, 'error');
126
+ return;
127
+ }
128
+ if (res.configError) {
129
+ this.showMessage(`OpenClaw 配置解析失败,已使用默认 Workspace:${res.configError}`, 'error');
130
+ }
131
+ this.agentsContent = res.content || '';
132
+ this.agentsOriginalContent = this.agentsContent;
133
+ this.agentsPath = res.path || '';
134
+ this.agentsExists = !!res.exists;
135
+ this.agentsLineEnding = res.lineEnding === '\r\n' ? '\r\n' : '\n';
136
+ this.resetAgentsDiffState();
137
+ this.showAgentsModal = true;
138
+ } catch (e) {
139
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
140
+ return;
141
+ }
142
+ this.showMessage('加载文件失败', 'error');
143
+ } finally {
144
+ if (isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
145
+ this.agentsLoading = false;
146
+ }
147
+ }
148
+ },
149
+
150
+ setAgentsModalContext(context, options = {}) {
151
+ if (context === 'openclaw-workspace') {
152
+ const fileName = (options.fileName || this.openclawWorkspaceFileName || 'AGENTS.md').trim();
153
+ this.agentsContext = 'openclaw-workspace';
154
+ this.agentsWorkspaceFileName = fileName;
155
+ this.agentsModalTitle = `OpenClaw 工作区文件: ${fileName}`;
156
+ this.agentsModalHint = `保存后会写入 OpenClaw Workspace 下的 ${fileName}。`;
157
+ return;
158
+ }
159
+ this.agentsContext = context === 'openclaw' ? 'openclaw' : 'codex';
160
+ if (this.agentsContext === 'openclaw') {
161
+ this.agentsModalTitle = 'OpenClaw AGENTS.md 编辑器';
162
+ this.agentsModalHint = '保存后会写入 OpenClaw Workspace 下的 AGENTS.md。';
163
+ } else {
164
+ this.agentsModalTitle = 'AGENTS.md 编辑器';
165
+ this.agentsModalHint = '保存后会写入目标 AGENTS.md(与 config.toml 同级)。';
166
+ }
167
+ this.agentsWorkspaceFileName = '';
168
+ },
169
+
170
+ resetAgentsDiffState() {
171
+ this.agentsDiffVisible = false;
172
+ this.agentsDiffLoading = false;
173
+ this.agentsDiffError = '';
174
+ this.agentsDiffLines = [];
175
+ this.agentsDiffStats = {
176
+ added: 0,
177
+ removed: 0,
178
+ unchanged: 0
179
+ };
180
+ this.agentsDiffTruncated = false;
181
+ this.agentsDiffHasChangesValue = false;
182
+ this.agentsDiffFingerprint = '';
183
+ this._agentsDiffPreviewRequestToken = null;
184
+ },
185
+ handleGlobalKeydown(event) {
186
+ if (!event || event.key !== 'Escape') {
187
+ return;
188
+ }
189
+ if (this.showConfirmDialog) {
190
+ event.preventDefault();
191
+ event.stopPropagation();
192
+ this.resolveConfirmDialog(false);
193
+ return;
194
+ }
195
+ if (!this.showAgentsModal) {
196
+ return;
197
+ }
198
+ event.preventDefault();
199
+ event.stopPropagation();
200
+ if (this.agentsSaving || this.agentsDiffLoading) {
201
+ return;
202
+ }
203
+ if (this.agentsDiffVisible) {
204
+ this.resetAgentsDiffState();
205
+ return;
206
+ }
207
+ this.closeAgentsModal();
208
+ },
209
+ hasPendingAgentsDraft() {
210
+ if (!this.showAgentsModal || this.agentsLoading) {
211
+ return false;
212
+ }
213
+ return !!this.agentsSaving || this.hasAgentsContentChanged() || this.agentsDiffVisible;
214
+ },
215
+ handleBeforeUnload(event) {
216
+ if (!this.hasPendingAgentsDraft()) {
217
+ return;
218
+ }
219
+ if (event && typeof event.preventDefault === 'function') {
220
+ event.preventDefault();
221
+ event.returnValue = '';
222
+ }
223
+ return '';
224
+ },
225
+ hasAgentsContentChanged() {
226
+ const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
227
+ const current = typeof this.agentsContent === 'string' ? this.agentsContent : '';
228
+ return original !== current;
229
+ },
230
+ requestConfirmDialog(options = {}) {
231
+ if (typeof this.confirmDialogResolver === 'function') {
232
+ this.confirmDialogResolver(false);
233
+ }
234
+ const confirmDisabled = options.confirmDisabled;
235
+ this.confirmDialogTitle = typeof options.title === 'string' && options.title.trim()
236
+ ? options.title.trim()
237
+ : '请确认操作';
238
+ this.confirmDialogMessage = typeof options.message === 'string' ? options.message : '';
239
+ this.confirmDialogConfirmText = typeof options.confirmText === 'string' && options.confirmText.trim()
240
+ ? options.confirmText.trim()
241
+ : '确认';
242
+ this.confirmDialogCancelText = typeof options.cancelText === 'string' && options.cancelText.trim()
243
+ ? options.cancelText.trim()
244
+ : '取消';
245
+ this.confirmDialogDanger = !!options.danger;
246
+ this.confirmDialogConfirmDisabled = typeof confirmDisabled === 'function' ? false : !!confirmDisabled;
247
+ this.confirmDialogDisableWhen = typeof confirmDisabled === 'function' ? confirmDisabled : null;
248
+ this.showConfirmDialog = true;
249
+ return new Promise((resolve) => {
250
+ this.confirmDialogResolver = resolve;
251
+ });
252
+ },
253
+ isConfirmDialogDisabled() {
254
+ if (typeof this.confirmDialogDisableWhen === 'function') {
255
+ try {
256
+ return !!this.confirmDialogDisableWhen.call(this);
257
+ } catch (_) {
258
+ return true;
259
+ }
260
+ }
261
+ return !!this.confirmDialogConfirmDisabled;
262
+ },
263
+ resolveConfirmDialog(confirmed) {
264
+ const resolver = typeof this.confirmDialogResolver === 'function'
265
+ ? this.confirmDialogResolver
266
+ : null;
267
+ this.showConfirmDialog = false;
268
+ this.confirmDialogTitle = '';
269
+ this.confirmDialogMessage = '';
270
+ this.confirmDialogConfirmText = '确认';
271
+ this.confirmDialogCancelText = '取消';
272
+ this.confirmDialogDanger = false;
273
+ this.confirmDialogConfirmDisabled = false;
274
+ this.confirmDialogDisableWhen = null;
275
+ this.confirmDialogResolver = null;
276
+ if (resolver) {
277
+ resolver(!!confirmed);
278
+ }
279
+ },
280
+ closeConfirmDialog() {
281
+ this.resolveConfirmDialog(false);
282
+ },
283
+ onAgentsContentInput() {
284
+ if (this.agentsDiffVisible || this.agentsDiffLines.length) {
285
+ this.resetAgentsDiffState();
286
+ }
287
+ },
288
+ buildAgentsDiffFingerprint() {
289
+ const context = this.agentsContext || 'codex';
290
+ const fileName = context === 'openclaw-workspace'
291
+ ? (this.agentsWorkspaceFileName || '')
292
+ : '';
293
+ const lineEnding = this.agentsLineEnding || '\n';
294
+ const content = typeof this.agentsContent === 'string' ? this.agentsContent : '';
295
+ const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
296
+ return `${context}::${fileName}::${lineEnding}::${content.length}::${content}::${original.length}::${original}`;
297
+ },
298
+ async prepareAgentsDiff() {
299
+ const requestFingerprint = this.buildAgentsDiffFingerprint();
300
+ const requestToken = Symbol('agents-diff-preview');
301
+ this._agentsDiffPreviewRequestToken = requestToken;
302
+ this.agentsDiffVisible = true;
303
+ this.agentsDiffLoading = true;
304
+ this.agentsDiffError = '';
305
+ this.agentsDiffLines = [];
306
+ this.agentsDiffStats = {
307
+ added: 0,
308
+ removed: 0,
309
+ unchanged: 0
310
+ };
311
+ this.agentsDiffTruncated = false;
312
+ this.agentsDiffHasChangesValue = false;
313
+ try {
314
+ const shouldApplyPreviewState = () => shouldApplyAgentsDiffPreviewResponse({
315
+ isVisible: this.agentsDiffVisible,
316
+ requestToken,
317
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
318
+ requestFingerprint,
319
+ currentFingerprint: this.buildAgentsDiffFingerprint()
320
+ });
321
+ const applyPreviewState = (diff) => {
322
+ if (!shouldApplyPreviewState()) {
323
+ return false;
324
+ }
325
+ const normalizedDiff = diff && typeof diff === 'object' ? diff : {};
326
+ const rawLines = Array.isArray(normalizedDiff.lines) ? normalizedDiff.lines : [];
327
+ this.agentsDiffLines = rawLines.filter(line => line && line.type);
328
+ this.agentsDiffTruncated = !!normalizedDiff.truncated;
329
+ this.agentsDiffHasChangesValue = !!normalizedDiff.hasChanges;
330
+ if (normalizedDiff.stats && typeof normalizedDiff.stats === 'object') {
331
+ this.agentsDiffStats = {
332
+ added: Number(normalizedDiff.stats.added || 0),
333
+ removed: Number(normalizedDiff.stats.removed || 0),
334
+ unchanged: Number(normalizedDiff.stats.unchanged || 0)
335
+ };
336
+ } else {
337
+ const stats = { added: 0, removed: 0, unchanged: 0 };
338
+ for (const line of this.agentsDiffLines) {
339
+ if (line && line.type === 'add') stats.added += 1;
340
+ else if (line && line.type === 'del') stats.removed += 1;
341
+ else stats.unchanged += 1;
342
+ }
343
+ this.agentsDiffStats = stats;
344
+ }
345
+ this.agentsDiffFingerprint = requestFingerprint;
346
+ return true;
347
+ };
348
+ const previewRequest = buildAgentsDiffPreviewRequest({
349
+ baseContent: this.agentsOriginalContent,
350
+ content: this.agentsContent,
351
+ lineEnding: this.agentsLineEnding,
352
+ context: this.agentsContext,
353
+ fileName: this.agentsWorkspaceFileName
354
+ });
355
+ if (previewRequest.exceedsBodyLimit) {
356
+ applyPreviewState(buildAgentsDiffPreview({
357
+ baseContent: this.agentsOriginalContent,
358
+ content: this.agentsContent
359
+ }));
360
+ return;
361
+ }
362
+ const res = await apiWithMeta('preview-agents-diff', previewRequest.params);
363
+ if (!shouldApplyPreviewState()) {
364
+ return;
365
+ }
366
+ if (res.error) {
367
+ if (isAgentsDiffPreviewPayloadTooLarge(res)) {
368
+ applyPreviewState(buildAgentsDiffPreview({
369
+ baseContent: this.agentsOriginalContent,
370
+ content: this.agentsContent
371
+ }));
372
+ return;
373
+ }
374
+ this.agentsDiffError = res.error;
375
+ return;
376
+ }
377
+ applyPreviewState(res.diff);
378
+ } catch (e) {
379
+ if (shouldApplyAgentsDiffPreviewResponse({
380
+ isVisible: this.agentsDiffVisible,
381
+ requestToken,
382
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
383
+ requestFingerprint,
384
+ currentFingerprint: this.buildAgentsDiffFingerprint()
385
+ })) {
386
+ this.agentsDiffError = '生成差异失败';
387
+ }
388
+ } finally {
389
+ if (this._agentsDiffPreviewRequestToken === requestToken) {
390
+ this.agentsDiffLoading = false;
391
+ }
392
+ }
393
+ },
394
+
395
+ async closeAgentsModal(options = {}) {
396
+ const force = !!(options && options.force);
397
+ if (!force && (this.agentsSaving || this.agentsDiffLoading)) {
398
+ return;
399
+ }
400
+ const shouldConfirmClose = !force
401
+ && this.hasPendingAgentsDraft();
402
+ if (shouldConfirmClose) {
403
+ const message = this.agentsDiffVisible
404
+ ? '当前处于差异预览模式,改动尚未保存。确认放弃改动并关闭吗?'
405
+ : '存在未保存改动,确认放弃改动并关闭吗?(关闭页面或应用也会丢失改动)';
406
+ const confirmed = await this.requestConfirmDialog({
407
+ title: '放弃未保存改动',
408
+ message,
409
+ confirmText: '放弃并关闭',
410
+ cancelText: '继续编辑',
411
+ danger: true
412
+ });
413
+ if (!confirmed) {
414
+ return;
415
+ }
416
+ }
417
+ issueLatestRequestToken(this, '_agentsOpenRequestToken');
418
+ this.agentsLoading = false;
419
+ this.showAgentsModal = false;
420
+ this.agentsContent = '';
421
+ this.agentsOriginalContent = '';
422
+ this.agentsPath = '';
423
+ this.agentsExists = false;
424
+ this.agentsLineEnding = '\n';
425
+ this.agentsSaving = false;
426
+ this.agentsWorkspaceFileName = '';
427
+ this.resetAgentsDiffState();
428
+ this.setAgentsModalContext('codex');
429
+ },
430
+
431
+ async applyAgentsContent() {
432
+ if (this.agentsSaving) {
433
+ return;
434
+ }
435
+ if (!this.agentsDiffVisible) {
436
+ if (!this.hasAgentsContentChanged()) {
437
+ this.showMessage('未检测到改动', 'info');
438
+ return;
439
+ }
440
+ await this.prepareAgentsDiff();
441
+ return;
442
+ }
443
+ if (this.agentsDiffLoading) {
444
+ return;
445
+ }
446
+ if (this.agentsDiffError) {
447
+ this.showMessage(this.agentsDiffError, 'error');
448
+ return;
449
+ }
450
+ const fingerprint = this.buildAgentsDiffFingerprint();
451
+ if (this.agentsDiffFingerprint !== fingerprint) {
452
+ await this.prepareAgentsDiff();
453
+ return;
454
+ }
455
+ if (!this.agentsDiffHasChanges) {
456
+ this.showMessage('未检测到改动', 'info');
457
+ return;
458
+ }
459
+ if (this.agentsContext === 'openclaw-workspace' && !isValidOpenclawWorkspaceFileName(this.agentsWorkspaceFileName)) {
460
+ this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
461
+ return;
462
+ }
463
+ this.agentsSaving = true;
464
+ try {
465
+ let action = 'apply-agents-file';
466
+ const params = {
467
+ content: this.agentsContent,
468
+ lineEnding: this.agentsLineEnding
469
+ };
470
+ if (this.agentsContext === 'openclaw') {
471
+ action = 'apply-openclaw-agents-file';
472
+ } else if (this.agentsContext === 'openclaw-workspace') {
473
+ action = 'apply-openclaw-workspace-file';
474
+ params.fileName = this.agentsWorkspaceFileName;
475
+ }
476
+ const res = await api(action, params);
477
+ if (res.error) {
478
+ this.showMessage(res.error, 'error');
479
+ return;
480
+ }
481
+ const successLabel = this.agentsContext === 'openclaw-workspace'
482
+ ? `工作区文件已保存${this.agentsWorkspaceFileName ? `: ${this.agentsWorkspaceFileName}` : ''}`
483
+ : (this.agentsContext === 'openclaw' ? 'OpenClaw AGENTS.md 已保存' : 'AGENTS.md 已保存');
484
+ this.showMessage(successLabel, 'success');
485
+ this.closeAgentsModal({ force: true });
486
+ } catch (e) {
487
+ this.showMessage('保存失败', 'error');
488
+ } finally {
489
+ this.agentsSaving = false;
490
+ }
491
+ }
492
+ };
493
+ }
@@ -0,0 +1,174 @@
1
+ export function createClaudeConfigMethods(options = {}) {
2
+ const { api } = options;
3
+
4
+ return {
5
+ switchClaudeConfig(name) {
6
+ this.currentClaudeConfig = name;
7
+ this.refreshClaudeModelContext();
8
+ },
9
+
10
+ onClaudeModelChange() {
11
+ const name = this.currentClaudeConfig;
12
+ if (!name) {
13
+ this.showMessage('请先选择配置', 'error');
14
+ return;
15
+ }
16
+ const model = (this.currentClaudeModel || '').trim();
17
+ if (!model) {
18
+ this.showMessage('请输入模型', 'error');
19
+ return;
20
+ }
21
+ const existing = this.claudeConfigs[name] || {};
22
+ this.currentClaudeModel = model;
23
+ this.claudeConfigs[name] = this.mergeClaudeConfig(existing, { model });
24
+ this.saveClaudeConfigs();
25
+ this.updateClaudeModelsCurrent();
26
+ if (!this.claudeConfigs[name].apiKey && !this.claudeConfigs[name].externalCredentialType) {
27
+ this.showMessage('请先配置 API Key', 'error');
28
+ return;
29
+ }
30
+ this.applyClaudeConfig(name);
31
+ },
32
+
33
+ saveClaudeConfigs() {
34
+ localStorage.setItem('claudeConfigs', JSON.stringify(this.claudeConfigs));
35
+ },
36
+
37
+ openEditConfigModal(name) {
38
+ const config = this.claudeConfigs[name];
39
+ this.editingConfig = {
40
+ name: name,
41
+ apiKey: config.apiKey || '',
42
+ baseUrl: config.baseUrl || '',
43
+ model: config.model || ''
44
+ };
45
+ this.showEditConfigModal = true;
46
+ },
47
+
48
+ updateConfig() {
49
+ const name = this.editingConfig.name;
50
+ this.claudeConfigs[name] = this.mergeClaudeConfig(this.claudeConfigs[name], this.editingConfig);
51
+ this.saveClaudeConfigs();
52
+ this.showMessage('操作成功', 'success');
53
+ this.closeEditConfigModal();
54
+ if (name === this.currentClaudeConfig) {
55
+ this.refreshClaudeModelContext();
56
+ }
57
+ },
58
+
59
+ closeEditConfigModal() {
60
+ this.showEditConfigModal = false;
61
+ this.editingConfig = { name: '', apiKey: '', baseUrl: '', model: '' };
62
+ },
63
+
64
+ async saveAndApplyConfig() {
65
+ const name = this.editingConfig.name;
66
+ this.claudeConfigs[name] = this.mergeClaudeConfig(this.claudeConfigs[name], this.editingConfig);
67
+ this.saveClaudeConfigs();
68
+
69
+ const config = this.claudeConfigs[name];
70
+ if (!config.apiKey) {
71
+ this.showMessage('已保存,未应用', 'info');
72
+ this.closeEditConfigModal();
73
+ if (name === this.currentClaudeConfig) {
74
+ this.refreshClaudeModelContext();
75
+ }
76
+ return;
77
+ }
78
+
79
+ try {
80
+ const res = await api('apply-claude-config', { config });
81
+ if (res.error || res.success === false) {
82
+ this.showMessage(res.error || '应用配置失败', 'error');
83
+ } else {
84
+ this.currentClaudeConfig = name;
85
+ const targetTip = res.targetPath ? `(${res.targetPath})` : '';
86
+ this.showMessage(`已保存并应用到 Claude 配置${targetTip}`, 'success');
87
+ this.closeEditConfigModal();
88
+ this.refreshClaudeModelContext();
89
+ }
90
+ } catch (_) {
91
+ this.showMessage('应用配置失败', 'error');
92
+ }
93
+ },
94
+
95
+ addClaudeConfig() {
96
+ if (!this.newClaudeConfig.name || !this.newClaudeConfig.name.trim()) {
97
+ return this.showMessage('请输入名称', 'error');
98
+ }
99
+ const name = this.newClaudeConfig.name.trim();
100
+ if (this.claudeConfigs[name]) {
101
+ return this.showMessage('名称已存在', 'error');
102
+ }
103
+ const duplicateName = this.findDuplicateClaudeConfigName(this.newClaudeConfig);
104
+ if (duplicateName) {
105
+ return this.showMessage('配置已存在', 'info');
106
+ }
107
+
108
+ this.claudeConfigs[name] = this.mergeClaudeConfig({}, this.newClaudeConfig);
109
+
110
+ this.currentClaudeConfig = name;
111
+ this.saveClaudeConfigs();
112
+ this.showMessage('操作成功', 'success');
113
+ this.closeClaudeConfigModal();
114
+ this.refreshClaudeModelContext();
115
+ },
116
+
117
+ async deleteClaudeConfig(name) {
118
+ if (Object.keys(this.claudeConfigs).length <= 1) {
119
+ return this.showMessage('至少保留一项', 'error');
120
+ }
121
+ const confirmed = await this.requestConfirmDialog({
122
+ title: '删除 Claude 配置',
123
+ message: `确定删除配置 "${name}"?`,
124
+ confirmText: '删除',
125
+ cancelText: '取消',
126
+ danger: true
127
+ });
128
+ if (!confirmed) return;
129
+
130
+ delete this.claudeConfigs[name];
131
+ if (this.currentClaudeConfig === name) {
132
+ this.currentClaudeConfig = Object.keys(this.claudeConfigs)[0];
133
+ }
134
+ this.saveClaudeConfigs();
135
+ this.showMessage('操作成功', 'success');
136
+ this.refreshClaudeModelContext();
137
+ },
138
+
139
+ async applyClaudeConfig(name) {
140
+ this.currentClaudeConfig = name;
141
+ this.refreshClaudeModelContext();
142
+ const config = this.claudeConfigs[name];
143
+
144
+ if (!config.apiKey) {
145
+ if (config.externalCredentialType) {
146
+ return this.showMessage('检测到外部 Claude 认证状态;当前仅支持展示,若需由 codexmate 接管请补充 API Key', 'info');
147
+ }
148
+ return this.showMessage('请先配置 API Key', 'error');
149
+ }
150
+
151
+ try {
152
+ const res = await api('apply-claude-config', { config });
153
+ if (res.error || res.success === false) {
154
+ this.showMessage(res.error || '应用配置失败', 'error');
155
+ } else {
156
+ const targetTip = res.targetPath ? `(${res.targetPath})` : '';
157
+ this.showMessage(`已应用配置到 Claude 设置: ${name}${targetTip}`, 'success');
158
+ }
159
+ } catch (_) {
160
+ this.showMessage('应用配置失败', 'error');
161
+ }
162
+ },
163
+
164
+ closeClaudeConfigModal() {
165
+ this.showClaudeConfigModal = false;
166
+ this.newClaudeConfig = {
167
+ name: '',
168
+ apiKey: '',
169
+ baseUrl: 'https://open.bigmodel.cn/api/anthropic',
170
+ model: 'glm-4.7'
171
+ };
172
+ }
173
+ };
174
+ }