codexmate 0.0.21 → 0.0.23

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 (115) hide show
  1. package/README.md +390 -284
  2. package/README.zh.md +322 -0
  3. package/cli/agents-files.js +224 -162
  4. package/cli/archive-helpers.js +446 -446
  5. package/cli/auth-profiles.js +359 -359
  6. package/cli/builtin-proxy.js +1044 -580
  7. package/cli/claude-proxy.js +998 -998
  8. package/cli/config-bootstrap.js +384 -384
  9. package/cli/config-health.js +338 -338
  10. package/cli/openai-bridge.js +950 -0
  11. package/cli/openclaw-config.js +629 -629
  12. package/cli/session-usage.concurrent.js +28 -0
  13. package/cli/session-usage.js +112 -0
  14. package/cli/session-usage.models.js +176 -0
  15. package/cli/skills.js +1141 -1141
  16. package/cli/zip-commands.js +510 -510
  17. package/cli.js +13214 -13129
  18. package/lib/cli-file-utils.js +151 -151
  19. package/lib/cli-models-utils.js +419 -419
  20. package/lib/cli-network-utils.js +164 -164
  21. package/lib/cli-path-utils.js +69 -69
  22. package/lib/cli-session-utils.js +121 -121
  23. package/lib/cli-sessions.js +386 -386
  24. package/lib/cli-utils.js +155 -155
  25. package/lib/download-artifacts.js +77 -77
  26. package/lib/mcp-stdio.js +440 -440
  27. package/lib/task-orchestrator.js +869 -869
  28. package/lib/text-diff.js +303 -303
  29. package/lib/workflow-engine.js +340 -340
  30. package/package.json +74 -74
  31. package/res/json5.min.js +1 -1
  32. package/res/logo.png +0 -0
  33. package/res/vue.global.prod.js +13 -13
  34. package/web-ui/app.js +626 -530
  35. package/web-ui/index.html +34 -33
  36. package/web-ui/logic.agents-diff.mjs +386 -386
  37. package/web-ui/logic.claude.mjs +168 -168
  38. package/web-ui/logic.mjs +5 -5
  39. package/web-ui/logic.runtime.mjs +128 -124
  40. package/web-ui/logic.sessions.mjs +614 -581
  41. package/web-ui/modules/api.mjs +90 -90
  42. package/web-ui/modules/app.computed.dashboard.mjs +126 -113
  43. package/web-ui/modules/app.computed.index.mjs +17 -15
  44. package/web-ui/modules/app.computed.main-tabs.mjs +202 -195
  45. package/web-ui/modules/app.computed.session.mjs +653 -507
  46. package/web-ui/modules/app.constants.mjs +15 -15
  47. package/web-ui/modules/app.methods.agents.mjs +544 -493
  48. package/web-ui/modules/app.methods.claude-config.mjs +174 -174
  49. package/web-ui/modules/app.methods.codex-config.mjs +795 -640
  50. package/web-ui/modules/app.methods.index.mjs +92 -88
  51. package/web-ui/modules/app.methods.install.mjs +177 -149
  52. package/web-ui/modules/app.methods.navigation.mjs +662 -619
  53. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  54. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  55. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  56. package/web-ui/modules/app.methods.providers.mjs +404 -363
  57. package/web-ui/modules/app.methods.runtime.mjs +323 -323
  58. package/web-ui/modules/app.methods.session-actions.mjs +537 -520
  59. package/web-ui/modules/app.methods.session-browser.mjs +691 -626
  60. package/web-ui/modules/app.methods.session-timeline.mjs +448 -448
  61. package/web-ui/modules/app.methods.session-trash.mjs +422 -422
  62. package/web-ui/modules/app.methods.startup-claude.mjs +417 -412
  63. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -471
  64. package/web-ui/modules/config-mode.computed.mjs +126 -126
  65. package/web-ui/modules/config-template-confirm-pref.mjs +33 -0
  66. package/web-ui/modules/i18n.mjs +1823 -0
  67. package/web-ui/modules/plugins.computed.mjs +3 -0
  68. package/web-ui/modules/plugins.methods.mjs +3 -0
  69. package/web-ui/modules/plugins.storage.mjs +11 -0
  70. package/web-ui/modules/sessions-filters-url.mjs +85 -0
  71. package/web-ui/modules/skills.computed.mjs +107 -107
  72. package/web-ui/modules/skills.methods.mjs +481 -481
  73. package/web-ui/partials/index/layout-footer.html +13 -13
  74. package/web-ui/partials/index/layout-header.html +461 -402
  75. package/web-ui/partials/index/modal-config-template-agents.html +175 -125
  76. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  77. package/web-ui/partials/index/modal-health-check.html +72 -72
  78. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  79. package/web-ui/partials/index/modal-skills.html +200 -184
  80. package/web-ui/partials/index/modals-basic.html +165 -156
  81. package/web-ui/partials/index/panel-config-claude.html +159 -126
  82. package/web-ui/partials/index/panel-config-codex.html +255 -237
  83. package/web-ui/partials/index/panel-config-openclaw.html +78 -78
  84. package/web-ui/partials/index/panel-docs.html +147 -130
  85. package/web-ui/partials/index/panel-market.html +174 -174
  86. package/web-ui/partials/index/panel-orchestration.html +388 -397
  87. package/web-ui/partials/index/panel-plugins.html +273 -0
  88. package/web-ui/partials/index/panel-sessions.html +298 -292
  89. package/web-ui/partials/index/panel-settings.html +258 -190
  90. package/web-ui/partials/index/panel-usage.html +353 -213
  91. package/web-ui/session-helpers.mjs +573 -559
  92. package/web-ui/source-bundle.cjs +233 -233
  93. package/web-ui/styles/base-theme.css +264 -271
  94. package/web-ui/styles/controls-forms.css +369 -360
  95. package/web-ui/styles/docs-panel.css +247 -182
  96. package/web-ui/styles/feedback.css +108 -108
  97. package/web-ui/styles/health-check-dialog.css +144 -144
  98. package/web-ui/styles/layout-shell.css +602 -376
  99. package/web-ui/styles/modals-core.css +464 -464
  100. package/web-ui/styles/navigation-panels.css +390 -348
  101. package/web-ui/styles/openclaw-structured.css +266 -266
  102. package/web-ui/styles/plugins-panel.css +523 -0
  103. package/web-ui/styles/responsive.css +456 -450
  104. package/web-ui/styles/sessions-list.css +400 -400
  105. package/web-ui/styles/sessions-preview.css +411 -411
  106. package/web-ui/styles/sessions-toolbar-trash.css +268 -243
  107. package/web-ui/styles/sessions-usage.css +879 -628
  108. package/web-ui/styles/settings-panel.css +166 -0
  109. package/web-ui/styles/skills-list.css +303 -296
  110. package/web-ui/styles/skills-market.css +406 -335
  111. package/web-ui/styles/task-orchestration.css +822 -776
  112. package/web-ui/styles/titles-cards.css +408 -408
  113. package/web-ui/styles.css +20 -18
  114. package/web-ui.html +17 -17
  115. package/README.en.md +0 -349
@@ -1,493 +1,544 @@
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
- }
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 openClaudeMdEditor() {
40
+ this.setAgentsModalContext('claude-md');
41
+ const requestToken = issueLatestRequestToken(this, '_agentsOpenRequestToken');
42
+ this.agentsLoading = true;
43
+ try {
44
+ const res = await api('get-claude-md-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 openAgentsEditor() {
72
+ this.setAgentsModalContext('codex');
73
+ const requestToken = issueLatestRequestToken(this, '_agentsOpenRequestToken');
74
+ this.agentsLoading = true;
75
+ try {
76
+ const res = await api('get-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
+ this.agentsContent = res.content || '';
85
+ this.agentsOriginalContent = this.agentsContent;
86
+ this.agentsPath = res.path || '';
87
+ this.agentsExists = !!res.exists;
88
+ this.agentsLineEnding = res.lineEnding === '\r\n' ? '\r\n' : '\n';
89
+ this.resetAgentsDiffState();
90
+ this.showAgentsModal = true;
91
+ } catch (e) {
92
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
93
+ return;
94
+ }
95
+ this.showMessage('加载文件失败', 'error');
96
+ } finally {
97
+ if (isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
98
+ this.agentsLoading = false;
99
+ }
100
+ }
101
+ },
102
+
103
+ async openOpenclawAgentsEditor() {
104
+ this.setAgentsModalContext('openclaw');
105
+ const requestToken = issueLatestRequestToken(this, '_agentsOpenRequestToken');
106
+ this.agentsLoading = true;
107
+ try {
108
+ const res = await api('get-openclaw-agents-file');
109
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
110
+ return;
111
+ }
112
+ if (res.error) {
113
+ this.showMessage(res.error, 'error');
114
+ return;
115
+ }
116
+ if (res.configError) {
117
+ this.showMessage(`OpenClaw 配置解析失败,已使用默认 Workspace:${res.configError}`, 'error');
118
+ }
119
+ this.agentsContent = res.content || '';
120
+ this.agentsOriginalContent = this.agentsContent;
121
+ this.agentsPath = res.path || '';
122
+ this.agentsExists = !!res.exists;
123
+ this.agentsLineEnding = res.lineEnding === '\r\n' ? '\r\n' : '\n';
124
+ this.resetAgentsDiffState();
125
+ this.showAgentsModal = true;
126
+ } catch (e) {
127
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
128
+ return;
129
+ }
130
+ this.showMessage('加载文件失败', 'error');
131
+ } finally {
132
+ if (isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
133
+ this.agentsLoading = false;
134
+ }
135
+ }
136
+ },
137
+
138
+ async openOpenclawWorkspaceEditor() {
139
+ const fileName = (this.openclawWorkspaceFileName || '').trim();
140
+ if (!fileName) {
141
+ this.showMessage('请输入文件名', 'error');
142
+ return;
143
+ }
144
+ if (!isValidOpenclawWorkspaceFileName(fileName)) {
145
+ this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
146
+ return;
147
+ }
148
+ this.setAgentsModalContext('openclaw-workspace', { fileName });
149
+ const requestToken = issueLatestRequestToken(this, '_agentsOpenRequestToken');
150
+ this.agentsLoading = true;
151
+ try {
152
+ const res = await api('get-openclaw-workspace-file', { fileName });
153
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
154
+ return;
155
+ }
156
+ if (res.error) {
157
+ this.showMessage(res.error, 'error');
158
+ return;
159
+ }
160
+ if (res.configError) {
161
+ this.showMessage(`OpenClaw 配置解析失败,已使用默认 Workspace:${res.configError}`, 'error');
162
+ }
163
+ this.agentsContent = res.content || '';
164
+ this.agentsOriginalContent = this.agentsContent;
165
+ this.agentsPath = res.path || '';
166
+ this.agentsExists = !!res.exists;
167
+ this.agentsLineEnding = res.lineEnding === '\r\n' ? '\r\n' : '\n';
168
+ this.resetAgentsDiffState();
169
+ this.showAgentsModal = true;
170
+ } catch (e) {
171
+ if (!isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
172
+ return;
173
+ }
174
+ this.showMessage('加载文件失败', 'error');
175
+ } finally {
176
+ if (isLatestRequestToken(this, '_agentsOpenRequestToken', requestToken)) {
177
+ this.agentsLoading = false;
178
+ }
179
+ }
180
+ },
181
+
182
+ setAgentsModalContext(context, options = {}) {
183
+ const t = typeof this.t === 'function' ? this.t : null;
184
+ const tr = (key, fallback, params = null) => (t ? t(key, params) : fallback);
185
+ if (context === 'claude-md') {
186
+ this.agentsContext = 'claude-md';
187
+ this.agentsWorkspaceFileName = '';
188
+ this.agentsModalTitle = tr('modal.agents.title.claudeMd', 'CLAUDE.md 编辑器');
189
+ this.agentsModalHint = tr('modal.agents.hint.claudeMd', '保存后会写入 ~/.claude/CLAUDE.md。');
190
+ return;
191
+ }
192
+ if (context === 'openclaw-workspace') {
193
+ const fileName = (options.fileName || this.openclawWorkspaceFileName || 'AGENTS.md').trim();
194
+ this.agentsContext = 'openclaw-workspace';
195
+ this.agentsWorkspaceFileName = fileName;
196
+ this.agentsModalTitle = `OpenClaw 工作区文件: ${fileName}`;
197
+ this.agentsModalHint = `保存后会写入 OpenClaw Workspace 下的 ${fileName}。`;
198
+ return;
199
+ }
200
+ this.agentsContext = context === 'openclaw' ? 'openclaw' : 'codex';
201
+ if (this.agentsContext === 'openclaw') {
202
+ this.agentsModalTitle = tr('modal.agents.title.openclaw', 'OpenClaw AGENTS.md 编辑器');
203
+ this.agentsModalHint = tr('modal.agents.hint.openclaw', '保存后会写入 OpenClaw Workspace 下的 AGENTS.md。');
204
+ } else {
205
+ this.agentsModalTitle = tr('modal.agents.title.default', 'AGENTS.md 编辑器');
206
+ this.agentsModalHint = tr('modal.agents.hint.default', '保存后会写入目标 AGENTS.md(与 config.toml 同级)。');
207
+ }
208
+ this.agentsWorkspaceFileName = '';
209
+ },
210
+
211
+ resetAgentsDiffState() {
212
+ this.agentsDiffVisible = false;
213
+ this.agentsDiffLoading = false;
214
+ this.agentsDiffError = '';
215
+ this.agentsDiffLines = [];
216
+ this.agentsDiffStats = {
217
+ added: 0,
218
+ removed: 0,
219
+ unchanged: 0
220
+ };
221
+ this.agentsDiffTruncated = false;
222
+ this.agentsDiffHasChangesValue = false;
223
+ this.agentsDiffFingerprint = '';
224
+ this._agentsDiffPreviewRequestToken = null;
225
+ },
226
+ handleGlobalKeydown(event) {
227
+ if (!event || event.key !== 'Escape') {
228
+ return;
229
+ }
230
+ if (this.showConfirmDialog) {
231
+ event.preventDefault();
232
+ event.stopPropagation();
233
+ this.resolveConfirmDialog(false);
234
+ return;
235
+ }
236
+ if (!this.showAgentsModal) {
237
+ return;
238
+ }
239
+ event.preventDefault();
240
+ event.stopPropagation();
241
+ if (this.agentsSaving || this.agentsDiffLoading) {
242
+ return;
243
+ }
244
+ if (this.agentsDiffVisible) {
245
+ this.resetAgentsDiffState();
246
+ return;
247
+ }
248
+ this.closeAgentsModal();
249
+ },
250
+ hasPendingAgentsDraft() {
251
+ if (!this.showAgentsModal || this.agentsLoading) {
252
+ return false;
253
+ }
254
+ return !!this.agentsSaving || this.hasAgentsContentChanged() || this.agentsDiffVisible;
255
+ },
256
+ handleBeforeUnload(event) {
257
+ if (!this.hasPendingAgentsDraft()) {
258
+ return;
259
+ }
260
+ if (event && typeof event.preventDefault === 'function') {
261
+ event.preventDefault();
262
+ event.returnValue = '';
263
+ }
264
+ return '';
265
+ },
266
+ hasAgentsContentChanged() {
267
+ const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
268
+ const current = typeof this.agentsContent === 'string' ? this.agentsContent : '';
269
+ return original !== current;
270
+ },
271
+ requestConfirmDialog(options = {}) {
272
+ if (typeof this.confirmDialogResolver === 'function') {
273
+ this.confirmDialogResolver(false);
274
+ }
275
+ const confirmDisabled = options.confirmDisabled;
276
+ const t = typeof this.t === 'function' ? this.t : null;
277
+ this.confirmDialogTitle = typeof options.title === 'string' && options.title.trim()
278
+ ? options.title.trim()
279
+ : (t ? t('confirm.title.default') : '请确认操作');
280
+ this.confirmDialogMessage = typeof options.message === 'string' ? options.message : '';
281
+ this.confirmDialogConfirmText = typeof options.confirmText === 'string' && options.confirmText.trim()
282
+ ? options.confirmText.trim()
283
+ : (t ? t('confirm.ok') : '确认');
284
+ this.confirmDialogCancelText = typeof options.cancelText === 'string' && options.cancelText.trim()
285
+ ? options.cancelText.trim()
286
+ : (t ? t('confirm.cancel') : '取消');
287
+ this.confirmDialogDanger = !!options.danger;
288
+ this.confirmDialogConfirmDisabled = typeof confirmDisabled === 'function' ? false : !!confirmDisabled;
289
+ this.confirmDialogDisableWhen = typeof confirmDisabled === 'function' ? confirmDisabled : null;
290
+ this.showConfirmDialog = true;
291
+ return new Promise((resolve) => {
292
+ this.confirmDialogResolver = resolve;
293
+ });
294
+ },
295
+ isConfirmDialogDisabled() {
296
+ if (typeof this.confirmDialogDisableWhen === 'function') {
297
+ try {
298
+ return !!this.confirmDialogDisableWhen.call(this);
299
+ } catch (_) {
300
+ return true;
301
+ }
302
+ }
303
+ return !!this.confirmDialogConfirmDisabled;
304
+ },
305
+ resolveConfirmDialog(confirmed) {
306
+ const resolver = typeof this.confirmDialogResolver === 'function'
307
+ ? this.confirmDialogResolver
308
+ : null;
309
+ this.showConfirmDialog = false;
310
+ this.confirmDialogTitle = '';
311
+ this.confirmDialogMessage = '';
312
+ if (typeof this.t === 'function') {
313
+ this.confirmDialogConfirmText = this.t('confirm.ok');
314
+ this.confirmDialogCancelText = this.t('confirm.cancel');
315
+ } else {
316
+ this.confirmDialogConfirmText = '确认';
317
+ this.confirmDialogCancelText = '取消';
318
+ }
319
+ this.confirmDialogDanger = false;
320
+ this.confirmDialogConfirmDisabled = false;
321
+ this.confirmDialogDisableWhen = null;
322
+ this.confirmDialogResolver = null;
323
+ if (resolver) {
324
+ resolver(!!confirmed);
325
+ }
326
+ },
327
+ closeConfirmDialog() {
328
+ this.resolveConfirmDialog(false);
329
+ },
330
+ onAgentsContentInput() {
331
+ if (this.agentsDiffVisible || this.agentsDiffLines.length) {
332
+ this.resetAgentsDiffState();
333
+ }
334
+ },
335
+ buildAgentsDiffFingerprint() {
336
+ const context = this.agentsContext || 'codex';
337
+ const fileName = context === 'openclaw-workspace'
338
+ ? (this.agentsWorkspaceFileName || '')
339
+ : '';
340
+ const lineEnding = this.agentsLineEnding || '\n';
341
+ const content = typeof this.agentsContent === 'string' ? this.agentsContent : '';
342
+ const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
343
+ return `${context}::${fileName}::${lineEnding}::${content.length}::${content}::${original.length}::${original}`;
344
+ },
345
+ async prepareAgentsDiff() {
346
+ const requestFingerprint = this.buildAgentsDiffFingerprint();
347
+ const requestToken = Symbol('agents-diff-preview');
348
+ this._agentsDiffPreviewRequestToken = requestToken;
349
+ this.agentsDiffVisible = true;
350
+ this.agentsDiffLoading = true;
351
+ this.agentsDiffError = '';
352
+ this.agentsDiffLines = [];
353
+ this.agentsDiffStats = {
354
+ added: 0,
355
+ removed: 0,
356
+ unchanged: 0
357
+ };
358
+ this.agentsDiffTruncated = false;
359
+ this.agentsDiffHasChangesValue = false;
360
+ try {
361
+ const shouldApplyPreviewState = () => shouldApplyAgentsDiffPreviewResponse({
362
+ isVisible: this.agentsDiffVisible,
363
+ requestToken,
364
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
365
+ requestFingerprint,
366
+ currentFingerprint: this.buildAgentsDiffFingerprint()
367
+ });
368
+ const applyPreviewState = (diff) => {
369
+ if (!shouldApplyPreviewState()) {
370
+ return false;
371
+ }
372
+ const normalizedDiff = diff && typeof diff === 'object' ? diff : {};
373
+ const rawLines = Array.isArray(normalizedDiff.lines) ? normalizedDiff.lines : [];
374
+ this.agentsDiffLines = rawLines.filter(line => line && line.type);
375
+ this.agentsDiffTruncated = !!normalizedDiff.truncated;
376
+ this.agentsDiffHasChangesValue = !!normalizedDiff.hasChanges;
377
+ if (normalizedDiff.stats && typeof normalizedDiff.stats === 'object') {
378
+ this.agentsDiffStats = {
379
+ added: Number(normalizedDiff.stats.added || 0),
380
+ removed: Number(normalizedDiff.stats.removed || 0),
381
+ unchanged: Number(normalizedDiff.stats.unchanged || 0)
382
+ };
383
+ } else {
384
+ const stats = { added: 0, removed: 0, unchanged: 0 };
385
+ for (const line of this.agentsDiffLines) {
386
+ if (line && line.type === 'add') stats.added += 1;
387
+ else if (line && line.type === 'del') stats.removed += 1;
388
+ else stats.unchanged += 1;
389
+ }
390
+ this.agentsDiffStats = stats;
391
+ }
392
+ this.agentsDiffFingerprint = requestFingerprint;
393
+ return true;
394
+ };
395
+ const previewRequest = buildAgentsDiffPreviewRequest({
396
+ baseContent: this.agentsOriginalContent,
397
+ content: this.agentsContent,
398
+ lineEnding: this.agentsLineEnding,
399
+ context: this.agentsContext,
400
+ fileName: this.agentsWorkspaceFileName
401
+ });
402
+ if (previewRequest.exceedsBodyLimit) {
403
+ applyPreviewState(buildAgentsDiffPreview({
404
+ baseContent: this.agentsOriginalContent,
405
+ content: this.agentsContent
406
+ }));
407
+ return;
408
+ }
409
+ const res = await apiWithMeta('preview-agents-diff', previewRequest.params);
410
+ if (!shouldApplyPreviewState()) {
411
+ return;
412
+ }
413
+ if (res.error) {
414
+ if (isAgentsDiffPreviewPayloadTooLarge(res)) {
415
+ applyPreviewState(buildAgentsDiffPreview({
416
+ baseContent: this.agentsOriginalContent,
417
+ content: this.agentsContent
418
+ }));
419
+ return;
420
+ }
421
+ this.agentsDiffError = res.error;
422
+ return;
423
+ }
424
+ applyPreviewState(res.diff);
425
+ } catch (e) {
426
+ if (shouldApplyAgentsDiffPreviewResponse({
427
+ isVisible: this.agentsDiffVisible,
428
+ requestToken,
429
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
430
+ requestFingerprint,
431
+ currentFingerprint: this.buildAgentsDiffFingerprint()
432
+ })) {
433
+ this.agentsDiffError = '生成差异失败';
434
+ }
435
+ } finally {
436
+ if (this._agentsDiffPreviewRequestToken === requestToken) {
437
+ this.agentsDiffLoading = false;
438
+ }
439
+ }
440
+ },
441
+
442
+ async closeAgentsModal(options = {}) {
443
+ const force = !!(options && options.force);
444
+ if (!force && (this.agentsSaving || this.agentsDiffLoading)) {
445
+ return;
446
+ }
447
+ const shouldConfirmClose = !force
448
+ && this.hasPendingAgentsDraft();
449
+ if (shouldConfirmClose) {
450
+ const message = this.agentsDiffVisible
451
+ ? '当前处于差异预览模式,改动尚未保存。确认放弃改动并关闭吗?'
452
+ : '存在未保存改动,确认放弃改动并关闭吗?(关闭页面或应用也会丢失改动)';
453
+ const confirmed = await this.requestConfirmDialog({
454
+ title: '放弃未保存改动',
455
+ message,
456
+ confirmText: '放弃并关闭',
457
+ cancelText: '继续编辑',
458
+ danger: true
459
+ });
460
+ if (!confirmed) {
461
+ return;
462
+ }
463
+ }
464
+ issueLatestRequestToken(this, '_agentsOpenRequestToken');
465
+ this.agentsLoading = false;
466
+ this.showAgentsModal = false;
467
+ this.agentsContent = '';
468
+ this.agentsOriginalContent = '';
469
+ this.agentsPath = '';
470
+ this.agentsExists = false;
471
+ this.agentsLineEnding = '\n';
472
+ this.agentsSaving = false;
473
+ this.agentsWorkspaceFileName = '';
474
+ this.resetAgentsDiffState();
475
+ this.setAgentsModalContext('codex');
476
+ },
477
+
478
+ async applyAgentsContent() {
479
+ if (this.agentsSaving) {
480
+ return;
481
+ }
482
+ if (!this.agentsDiffVisible) {
483
+ if (!this.hasAgentsContentChanged()) {
484
+ this.showMessage('未检测到改动', 'info');
485
+ return;
486
+ }
487
+ await this.prepareAgentsDiff();
488
+ return;
489
+ }
490
+ if (this.agentsDiffLoading) {
491
+ return;
492
+ }
493
+ if (this.agentsDiffError) {
494
+ this.showMessage(this.agentsDiffError, 'error');
495
+ return;
496
+ }
497
+ const fingerprint = this.buildAgentsDiffFingerprint();
498
+ if (this.agentsDiffFingerprint !== fingerprint) {
499
+ await this.prepareAgentsDiff();
500
+ return;
501
+ }
502
+ if (!this.agentsDiffHasChanges) {
503
+ this.showMessage('未检测到改动', 'info');
504
+ return;
505
+ }
506
+ if (this.agentsContext === 'openclaw-workspace' && !isValidOpenclawWorkspaceFileName(this.agentsWorkspaceFileName)) {
507
+ this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
508
+ return;
509
+ }
510
+ this.agentsSaving = true;
511
+ try {
512
+ let action = 'apply-agents-file';
513
+ const params = {
514
+ content: this.agentsContent,
515
+ lineEnding: this.agentsLineEnding
516
+ };
517
+ if (this.agentsContext === 'claude-md') {
518
+ action = 'apply-claude-md-file';
519
+ } else if (this.agentsContext === 'openclaw') {
520
+ action = 'apply-openclaw-agents-file';
521
+ } else if (this.agentsContext === 'openclaw-workspace') {
522
+ action = 'apply-openclaw-workspace-file';
523
+ params.fileName = this.agentsWorkspaceFileName;
524
+ }
525
+ const res = await api(action, params);
526
+ if (res.error) {
527
+ this.showMessage(res.error, 'error');
528
+ return;
529
+ }
530
+ const successLabel = this.agentsContext === 'openclaw-workspace'
531
+ ? `工作区文件已保存${this.agentsWorkspaceFileName ? `: ${this.agentsWorkspaceFileName}` : ''}`
532
+ : (this.agentsContext === 'claude-md'
533
+ ? 'CLAUDE.md 已保存'
534
+ : (this.agentsContext === 'openclaw' ? 'OpenClaw AGENTS.md 已保存' : 'AGENTS.md 已保存'));
535
+ this.showMessage(successLabel, 'success');
536
+ this.closeAgentsModal({ force: true });
537
+ } catch (e) {
538
+ this.showMessage('保存失败', 'error');
539
+ } finally {
540
+ this.agentsSaving = false;
541
+ }
542
+ }
543
+ };
544
+ }