codexmate 0.0.26 → 0.0.27

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 (135) hide show
  1. package/README.md +421 -416
  2. package/README.zh.md +354 -349
  3. package/cli/agents-files.js +224 -224
  4. package/cli/archive-helpers.js +446 -446
  5. package/cli/auth-profiles.js +375 -375
  6. package/cli/builtin-proxy.js +1299 -1079
  7. package/cli/claude-proxy.js +1022 -1022
  8. package/cli/config-bootstrap.js +384 -384
  9. package/cli/config-health.js +338 -338
  10. package/cli/doctor-core.js +903 -903
  11. package/cli/import-skills-url.js +356 -356
  12. package/cli/openai-bridge.js +1091 -997
  13. package/cli/openclaw-config.js +629 -629
  14. package/cli/session-convert-args.js +65 -65
  15. package/cli/session-convert-io.js +82 -82
  16. package/cli/session-convert.js +43 -43
  17. package/cli/session-usage.concurrent.js +28 -28
  18. package/cli/session-usage.js +118 -118
  19. package/cli/session-usage.models.js +176 -176
  20. package/cli/skills.js +1141 -1141
  21. package/cli/zip-commands.js +510 -510
  22. package/cli.js +15251 -15218
  23. package/lib/automation.js +404 -404
  24. package/lib/cli-file-utils.js +151 -151
  25. package/lib/cli-models-utils.js +379 -379
  26. package/lib/cli-network-utils.js +190 -190
  27. package/lib/cli-path-utils.js +85 -85
  28. package/lib/cli-session-utils.js +121 -121
  29. package/lib/cli-sessions.js +417 -417
  30. package/lib/cli-utils.js +155 -155
  31. package/lib/download-artifacts.js +92 -92
  32. package/lib/mcp-stdio.js +453 -453
  33. package/lib/task-orchestrator.js +869 -869
  34. package/lib/text-diff.js +303 -303
  35. package/lib/workflow-engine.js +340 -340
  36. package/package.json +1 -1
  37. package/plugins/README.md +20 -20
  38. package/plugins/README.zh-CN.md +20 -20
  39. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  40. package/plugins/prompt-templates/computed.mjs +253 -253
  41. package/plugins/prompt-templates/index.mjs +8 -8
  42. package/plugins/prompt-templates/manifest.mjs +15 -15
  43. package/plugins/prompt-templates/methods.mjs +619 -619
  44. package/plugins/prompt-templates/overview.mjs +90 -90
  45. package/plugins/prompt-templates/ownership.mjs +19 -19
  46. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  47. package/plugins/prompt-templates/storage.mjs +64 -64
  48. package/plugins/registry.mjs +16 -16
  49. package/web-ui/app.js +634 -625
  50. package/web-ui/index.html +35 -35
  51. package/web-ui/logic.agents-diff.mjs +386 -386
  52. package/web-ui/logic.claude.mjs +168 -168
  53. package/web-ui/logic.mjs +5 -5
  54. package/web-ui/logic.runtime.mjs +128 -128
  55. package/web-ui/logic.session-convert.mjs +70 -70
  56. package/web-ui/logic.sessions.mjs +765 -709
  57. package/web-ui/modules/api.mjs +90 -90
  58. package/web-ui/modules/app.computed.dashboard.mjs +171 -171
  59. package/web-ui/modules/app.computed.index.mjs +17 -17
  60. package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
  61. package/web-ui/modules/app.computed.session.mjs +994 -946
  62. package/web-ui/modules/app.constants.mjs +15 -15
  63. package/web-ui/modules/app.methods.agents.mjs +632 -632
  64. package/web-ui/modules/app.methods.claude-config.mjs +184 -179
  65. package/web-ui/modules/app.methods.codex-config.mjs +860 -860
  66. package/web-ui/modules/app.methods.index.mjs +92 -92
  67. package/web-ui/modules/app.methods.install.mjs +205 -205
  68. package/web-ui/modules/app.methods.navigation.mjs +743 -743
  69. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  70. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  71. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  72. package/web-ui/modules/app.methods.providers.mjs +404 -404
  73. package/web-ui/modules/app.methods.runtime.mjs +345 -345
  74. package/web-ui/modules/app.methods.session-actions.mjs +596 -596
  75. package/web-ui/modules/app.methods.session-browser.mjs +989 -985
  76. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  77. package/web-ui/modules/app.methods.session-trash.mjs +439 -424
  78. package/web-ui/modules/app.methods.startup-claude.mjs +526 -522
  79. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  80. package/web-ui/modules/config-mode.computed.mjs +124 -124
  81. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  82. package/web-ui/modules/i18n.dict.mjs +2131 -2113
  83. package/web-ui/modules/i18n.mjs +56 -56
  84. package/web-ui/modules/plugins.computed.mjs +3 -3
  85. package/web-ui/modules/plugins.methods.mjs +3 -3
  86. package/web-ui/modules/plugins.storage.mjs +11 -11
  87. package/web-ui/modules/sessions-filters-url.mjs +85 -85
  88. package/web-ui/modules/skills.computed.mjs +107 -107
  89. package/web-ui/modules/skills.methods.mjs +481 -481
  90. package/web-ui/partials/index/layout-footer.html +13 -13
  91. package/web-ui/partials/index/layout-header.html +475 -475
  92. package/web-ui/partials/index/modal-config-template-agents.html +174 -174
  93. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  94. package/web-ui/partials/index/modal-health-check.html +45 -45
  95. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  96. package/web-ui/partials/index/modal-skills.html +200 -200
  97. package/web-ui/partials/index/modals-basic.html +165 -165
  98. package/web-ui/partials/index/panel-config-claude.html +187 -184
  99. package/web-ui/partials/index/panel-config-codex.html +283 -283
  100. package/web-ui/partials/index/panel-config-openclaw.html +83 -83
  101. package/web-ui/partials/index/panel-dashboard.html +186 -186
  102. package/web-ui/partials/index/panel-docs.html +147 -147
  103. package/web-ui/partials/index/panel-market.html +177 -177
  104. package/web-ui/partials/index/panel-orchestration.html +391 -391
  105. package/web-ui/partials/index/panel-plugins.html +279 -279
  106. package/web-ui/partials/index/panel-sessions.html +326 -326
  107. package/web-ui/partials/index/panel-settings.html +274 -258
  108. package/web-ui/partials/index/panel-usage.html +371 -342
  109. package/web-ui/res/json5.min.js +1 -1
  110. package/web-ui/res/vue.global.prod.js +13 -13
  111. package/web-ui/session-helpers.mjs +576 -576
  112. package/web-ui/source-bundle.cjs +233 -233
  113. package/web-ui/styles/base-theme.css +268 -268
  114. package/web-ui/styles/controls-forms.css +423 -423
  115. package/web-ui/styles/dashboard.css +274 -274
  116. package/web-ui/styles/docs-panel.css +247 -247
  117. package/web-ui/styles/feedback.css +108 -108
  118. package/web-ui/styles/health-check-dialog.css +144 -144
  119. package/web-ui/styles/layout-shell.css +603 -603
  120. package/web-ui/styles/modals-core.css +464 -464
  121. package/web-ui/styles/navigation-panels.css +390 -390
  122. package/web-ui/styles/openclaw-structured.css +266 -266
  123. package/web-ui/styles/plugins-panel.css +523 -523
  124. package/web-ui/styles/responsive.css +454 -454
  125. package/web-ui/styles/sessions-list.css +415 -415
  126. package/web-ui/styles/sessions-preview.css +411 -411
  127. package/web-ui/styles/sessions-toolbar-trash.css +330 -330
  128. package/web-ui/styles/sessions-usage.css +1040 -945
  129. package/web-ui/styles/settings-panel.css +185 -166
  130. package/web-ui/styles/skills-list.css +303 -303
  131. package/web-ui/styles/skills-market.css +406 -406
  132. package/web-ui/styles/task-orchestration.css +822 -822
  133. package/web-ui/styles/titles-cards.css +408 -408
  134. package/web-ui/styles.css +21 -21
  135. package/web-ui.html +17 -17
@@ -1,632 +1,632 @@
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 = tr('modal.agents.title.openclawWorkspaceFile', `OpenClaw 工作区文件: ${fileName}`, { fileName });
197
- this.agentsModalHint = tr('modal.agents.hint.openclawWorkspaceFile', `保存后会写入 OpenClaw Workspace 下的 ${fileName}。`, { 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) {
228
- return;
229
- }
230
- const isCmdLike = !!(event.metaKey || event.ctrlKey);
231
- const key = typeof event.key === 'string' ? event.key : '';
232
- const isSearchHotkey = isCmdLike && !event.altKey && (key === 'k' || key === 'K');
233
- if (isSearchHotkey) {
234
- const target = event.target;
235
- const tag = target && target.tagName ? String(target.tagName).toUpperCase() : '';
236
- const isTypingTarget = !!(
237
- tag === 'INPUT'
238
- || tag === 'TEXTAREA'
239
- || tag === 'SELECT'
240
- || (target && target.isContentEditable)
241
- );
242
- if (!isTypingTarget) {
243
- event.preventDefault();
244
- event.stopPropagation();
245
- try {
246
- const focusSelector = (() => {
247
- if (this.showSkillsModal) return '.skills-filter-row input.form-input';
248
- if (this.mainTab === 'sessions') return '#panel-sessions .session-query-input';
249
- if (this.mainTab === 'plugins' && this.pluginsActiveId === 'prompt-templates' && this.promptTemplatesMode !== 'compose') {
250
- return '#panel-plugins .prompt-templates-toolbar input.form-input';
251
- }
252
- return '';
253
- })();
254
- if (focusSelector) {
255
- const el = document.querySelector(focusSelector);
256
- if (el && typeof el.focus === 'function') {
257
- el.focus();
258
- if (typeof el.select === 'function') {
259
- el.select();
260
- }
261
- }
262
- }
263
- } catch (_) {}
264
- }
265
- return;
266
- }
267
- if (key !== 'Escape') {
268
- return;
269
- }
270
- if (this.showConfirmDialog) {
271
- event.preventDefault();
272
- event.stopPropagation();
273
- this.resolveConfirmDialog(false);
274
- return;
275
- }
276
- if (this.showSkillsModal && typeof this.closeSkillsModal === 'function') {
277
- event.preventDefault();
278
- event.stopPropagation();
279
- this.closeSkillsModal();
280
- return;
281
- }
282
- if (this.showOpenclawConfigModal && typeof this.closeOpenclawConfigModal === 'function') {
283
- event.preventDefault();
284
- event.stopPropagation();
285
- this.closeOpenclawConfigModal();
286
- return;
287
- }
288
- if (this.showConfigTemplateModal && typeof this.closeConfigTemplateModal === 'function') {
289
- event.preventDefault();
290
- event.stopPropagation();
291
- this.closeConfigTemplateModal();
292
- return;
293
- }
294
- if (this.showEditConfigModal && typeof this.closeEditConfigModal === 'function') {
295
- event.preventDefault();
296
- event.stopPropagation();
297
- this.closeEditConfigModal();
298
- return;
299
- }
300
- if (this.showClaudeConfigModal && typeof this.closeClaudeConfigModal === 'function') {
301
- event.preventDefault();
302
- event.stopPropagation();
303
- this.closeClaudeConfigModal();
304
- return;
305
- }
306
- if (this.showModelModal && typeof this.closeModelModal === 'function') {
307
- event.preventDefault();
308
- event.stopPropagation();
309
- this.closeModelModal();
310
- return;
311
- }
312
- if (this.showAddModal && typeof this.closeAddModal === 'function') {
313
- event.preventDefault();
314
- event.stopPropagation();
315
- this.closeAddModal();
316
- return;
317
- }
318
- if (this.showEditModal && typeof this.closeEditModal === 'function') {
319
- event.preventDefault();
320
- event.stopPropagation();
321
- this.closeEditModal();
322
- return;
323
- }
324
- if (!this.showAgentsModal) {
325
- return;
326
- }
327
- event.preventDefault();
328
- event.stopPropagation();
329
- if (this.agentsSaving || this.agentsDiffLoading) {
330
- return;
331
- }
332
- if (this.agentsDiffVisible) {
333
- this.resetAgentsDiffState();
334
- return;
335
- }
336
- this.closeAgentsModal();
337
- },
338
- hasPendingAgentsDraft() {
339
- if (!this.showAgentsModal || this.agentsLoading) {
340
- return false;
341
- }
342
- return !!this.agentsSaving || this.hasAgentsContentChanged() || this.agentsDiffVisible;
343
- },
344
- handleBeforeUnload(event) {
345
- if (!this.hasPendingAgentsDraft()) {
346
- return;
347
- }
348
- if (event && typeof event.preventDefault === 'function') {
349
- event.preventDefault();
350
- event.returnValue = '';
351
- }
352
- return '';
353
- },
354
- hasAgentsContentChanged() {
355
- const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
356
- const current = typeof this.agentsContent === 'string' ? this.agentsContent : '';
357
- return original !== current;
358
- },
359
- requestConfirmDialog(options = {}) {
360
- if (typeof this.confirmDialogResolver === 'function') {
361
- this.confirmDialogResolver(false);
362
- }
363
- const confirmDisabled = options.confirmDisabled;
364
- const t = typeof this.t === 'function' ? this.t : null;
365
- this.confirmDialogTitle = typeof options.title === 'string' && options.title.trim()
366
- ? options.title.trim()
367
- : (t ? t('confirm.title.default') : '请确认操作');
368
- this.confirmDialogMessage = typeof options.message === 'string' ? options.message : '';
369
- this.confirmDialogConfirmText = typeof options.confirmText === 'string' && options.confirmText.trim()
370
- ? options.confirmText.trim()
371
- : (t ? t('confirm.ok') : '确认');
372
- this.confirmDialogCancelText = typeof options.cancelText === 'string' && options.cancelText.trim()
373
- ? options.cancelText.trim()
374
- : (t ? t('confirm.cancel') : '取消');
375
- this.confirmDialogDanger = !!options.danger;
376
- this.confirmDialogConfirmDisabled = typeof confirmDisabled === 'function' ? false : !!confirmDisabled;
377
- this.confirmDialogDisableWhen = typeof confirmDisabled === 'function' ? confirmDisabled : null;
378
- this.showConfirmDialog = true;
379
- return new Promise((resolve) => {
380
- this.confirmDialogResolver = resolve;
381
- });
382
- },
383
- isConfirmDialogDisabled() {
384
- if (typeof this.confirmDialogDisableWhen === 'function') {
385
- try {
386
- return !!this.confirmDialogDisableWhen.call(this);
387
- } catch (_) {
388
- return true;
389
- }
390
- }
391
- return !!this.confirmDialogConfirmDisabled;
392
- },
393
- resolveConfirmDialog(confirmed) {
394
- const resolver = typeof this.confirmDialogResolver === 'function'
395
- ? this.confirmDialogResolver
396
- : null;
397
- this.showConfirmDialog = false;
398
- this.confirmDialogTitle = '';
399
- this.confirmDialogMessage = '';
400
- if (typeof this.t === 'function') {
401
- this.confirmDialogConfirmText = this.t('confirm.ok');
402
- this.confirmDialogCancelText = this.t('confirm.cancel');
403
- } else {
404
- this.confirmDialogConfirmText = '确认';
405
- this.confirmDialogCancelText = '取消';
406
- }
407
- this.confirmDialogDanger = false;
408
- this.confirmDialogConfirmDisabled = false;
409
- this.confirmDialogDisableWhen = null;
410
- this.confirmDialogResolver = null;
411
- if (resolver) {
412
- resolver(!!confirmed);
413
- }
414
- },
415
- closeConfirmDialog() {
416
- this.resolveConfirmDialog(false);
417
- },
418
- onAgentsContentInput() {
419
- if (this.agentsDiffVisible || this.agentsDiffLines.length) {
420
- this.resetAgentsDiffState();
421
- }
422
- },
423
- buildAgentsDiffFingerprint() {
424
- const context = this.agentsContext || 'codex';
425
- const fileName = context === 'openclaw-workspace'
426
- ? (this.agentsWorkspaceFileName || '')
427
- : '';
428
- const lineEnding = this.agentsLineEnding || '\n';
429
- const content = typeof this.agentsContent === 'string' ? this.agentsContent : '';
430
- const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
431
- return `${context}::${fileName}::${lineEnding}::${content.length}::${content}::${original.length}::${original}`;
432
- },
433
- async prepareAgentsDiff() {
434
- const requestFingerprint = this.buildAgentsDiffFingerprint();
435
- const requestToken = Symbol('agents-diff-preview');
436
- this._agentsDiffPreviewRequestToken = requestToken;
437
- this.agentsDiffVisible = true;
438
- this.agentsDiffLoading = true;
439
- this.agentsDiffError = '';
440
- this.agentsDiffLines = [];
441
- this.agentsDiffStats = {
442
- added: 0,
443
- removed: 0,
444
- unchanged: 0
445
- };
446
- this.agentsDiffTruncated = false;
447
- this.agentsDiffHasChangesValue = false;
448
- try {
449
- const shouldApplyPreviewState = () => shouldApplyAgentsDiffPreviewResponse({
450
- isVisible: this.agentsDiffVisible,
451
- requestToken,
452
- activeRequestToken: this._agentsDiffPreviewRequestToken,
453
- requestFingerprint,
454
- currentFingerprint: this.buildAgentsDiffFingerprint()
455
- });
456
- const applyPreviewState = (diff) => {
457
- if (!shouldApplyPreviewState()) {
458
- return false;
459
- }
460
- const normalizedDiff = diff && typeof diff === 'object' ? diff : {};
461
- const rawLines = Array.isArray(normalizedDiff.lines) ? normalizedDiff.lines : [];
462
- this.agentsDiffLines = rawLines.filter(line => line && line.type);
463
- this.agentsDiffTruncated = !!normalizedDiff.truncated;
464
- this.agentsDiffHasChangesValue = !!normalizedDiff.hasChanges;
465
- if (normalizedDiff.stats && typeof normalizedDiff.stats === 'object') {
466
- this.agentsDiffStats = {
467
- added: Number(normalizedDiff.stats.added || 0),
468
- removed: Number(normalizedDiff.stats.removed || 0),
469
- unchanged: Number(normalizedDiff.stats.unchanged || 0)
470
- };
471
- } else {
472
- const stats = { added: 0, removed: 0, unchanged: 0 };
473
- for (const line of this.agentsDiffLines) {
474
- if (line && line.type === 'add') stats.added += 1;
475
- else if (line && line.type === 'del') stats.removed += 1;
476
- else stats.unchanged += 1;
477
- }
478
- this.agentsDiffStats = stats;
479
- }
480
- this.agentsDiffFingerprint = requestFingerprint;
481
- return true;
482
- };
483
- const previewRequest = buildAgentsDiffPreviewRequest({
484
- baseContent: this.agentsOriginalContent,
485
- content: this.agentsContent,
486
- lineEnding: this.agentsLineEnding,
487
- context: this.agentsContext,
488
- fileName: this.agentsWorkspaceFileName
489
- });
490
- if (previewRequest.exceedsBodyLimit) {
491
- applyPreviewState(buildAgentsDiffPreview({
492
- baseContent: this.agentsOriginalContent,
493
- content: this.agentsContent
494
- }));
495
- return;
496
- }
497
- const res = await apiWithMeta('preview-agents-diff', previewRequest.params);
498
- if (!shouldApplyPreviewState()) {
499
- return;
500
- }
501
- if (res.error) {
502
- if (isAgentsDiffPreviewPayloadTooLarge(res)) {
503
- applyPreviewState(buildAgentsDiffPreview({
504
- baseContent: this.agentsOriginalContent,
505
- content: this.agentsContent
506
- }));
507
- return;
508
- }
509
- this.agentsDiffError = res.error;
510
- return;
511
- }
512
- applyPreviewState(res.diff);
513
- } catch (e) {
514
- if (shouldApplyAgentsDiffPreviewResponse({
515
- isVisible: this.agentsDiffVisible,
516
- requestToken,
517
- activeRequestToken: this._agentsDiffPreviewRequestToken,
518
- requestFingerprint,
519
- currentFingerprint: this.buildAgentsDiffFingerprint()
520
- })) {
521
- this.agentsDiffError = '生成差异失败';
522
- }
523
- } finally {
524
- if (this._agentsDiffPreviewRequestToken === requestToken) {
525
- this.agentsDiffLoading = false;
526
- }
527
- }
528
- },
529
-
530
- async closeAgentsModal(options = {}) {
531
- const force = !!(options && options.force);
532
- if (!force && (this.agentsSaving || this.agentsDiffLoading)) {
533
- return;
534
- }
535
- const shouldConfirmClose = !force
536
- && this.hasPendingAgentsDraft();
537
- if (shouldConfirmClose) {
538
- const message = this.agentsDiffVisible
539
- ? '当前处于差异预览模式,改动尚未保存。确认放弃改动并关闭吗?'
540
- : '存在未保存改动,确认放弃改动并关闭吗?(关闭页面或应用也会丢失改动)';
541
- const confirmed = await this.requestConfirmDialog({
542
- title: '放弃未保存改动',
543
- message,
544
- confirmText: '放弃并关闭',
545
- cancelText: '继续编辑',
546
- danger: true
547
- });
548
- if (!confirmed) {
549
- return;
550
- }
551
- }
552
- issueLatestRequestToken(this, '_agentsOpenRequestToken');
553
- this.agentsLoading = false;
554
- this.showAgentsModal = false;
555
- this.agentsContent = '';
556
- this.agentsOriginalContent = '';
557
- this.agentsPath = '';
558
- this.agentsExists = false;
559
- this.agentsLineEnding = '\n';
560
- this.agentsSaving = false;
561
- this.agentsWorkspaceFileName = '';
562
- this.resetAgentsDiffState();
563
- this.setAgentsModalContext('codex');
564
- },
565
-
566
- async applyAgentsContent() {
567
- if (this.agentsSaving) {
568
- return;
569
- }
570
- if (!this.agentsDiffVisible) {
571
- if (!this.hasAgentsContentChanged()) {
572
- this.showMessage('未检测到改动', 'info');
573
- return;
574
- }
575
- await this.prepareAgentsDiff();
576
- return;
577
- }
578
- if (this.agentsDiffLoading) {
579
- return;
580
- }
581
- if (this.agentsDiffError) {
582
- this.showMessage(this.agentsDiffError, 'error');
583
- return;
584
- }
585
- const fingerprint = this.buildAgentsDiffFingerprint();
586
- if (this.agentsDiffFingerprint !== fingerprint) {
587
- await this.prepareAgentsDiff();
588
- return;
589
- }
590
- if (!this.agentsDiffHasChanges) {
591
- this.showMessage('未检测到改动', 'info');
592
- return;
593
- }
594
- if (this.agentsContext === 'openclaw-workspace' && !isValidOpenclawWorkspaceFileName(this.agentsWorkspaceFileName)) {
595
- this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
596
- return;
597
- }
598
- this.agentsSaving = true;
599
- try {
600
- let action = 'apply-agents-file';
601
- const params = {
602
- content: this.agentsContent,
603
- lineEnding: this.agentsLineEnding
604
- };
605
- if (this.agentsContext === 'claude-md') {
606
- action = 'apply-claude-md-file';
607
- } else if (this.agentsContext === 'openclaw') {
608
- action = 'apply-openclaw-agents-file';
609
- } else if (this.agentsContext === 'openclaw-workspace') {
610
- action = 'apply-openclaw-workspace-file';
611
- params.fileName = this.agentsWorkspaceFileName;
612
- }
613
- const res = await api(action, params);
614
- if (res.error) {
615
- this.showMessage(res.error, 'error');
616
- return;
617
- }
618
- const successLabel = this.agentsContext === 'openclaw-workspace'
619
- ? `工作区文件已保存${this.agentsWorkspaceFileName ? `: ${this.agentsWorkspaceFileName}` : ''}`
620
- : (this.agentsContext === 'claude-md'
621
- ? 'CLAUDE.md 已保存'
622
- : (this.agentsContext === 'openclaw' ? 'OpenClaw AGENTS.md 已保存' : 'AGENTS.md 已保存'));
623
- this.showMessage(successLabel, 'success');
624
- this.closeAgentsModal({ force: true });
625
- } catch (e) {
626
- this.showMessage('保存失败', 'error');
627
- } finally {
628
- this.agentsSaving = false;
629
- }
630
- }
631
- };
632
- }
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 = tr('modal.agents.title.openclawWorkspaceFile', `OpenClaw 工作区文件: ${fileName}`, { fileName });
197
+ this.agentsModalHint = tr('modal.agents.hint.openclawWorkspaceFile', `保存后会写入 OpenClaw Workspace 下的 ${fileName}。`, { 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) {
228
+ return;
229
+ }
230
+ const isCmdLike = !!(event.metaKey || event.ctrlKey);
231
+ const key = typeof event.key === 'string' ? event.key : '';
232
+ const isSearchHotkey = isCmdLike && !event.altKey && (key === 'k' || key === 'K');
233
+ if (isSearchHotkey) {
234
+ const target = event.target;
235
+ const tag = target && target.tagName ? String(target.tagName).toUpperCase() : '';
236
+ const isTypingTarget = !!(
237
+ tag === 'INPUT'
238
+ || tag === 'TEXTAREA'
239
+ || tag === 'SELECT'
240
+ || (target && target.isContentEditable)
241
+ );
242
+ if (!isTypingTarget) {
243
+ event.preventDefault();
244
+ event.stopPropagation();
245
+ try {
246
+ const focusSelector = (() => {
247
+ if (this.showSkillsModal) return '.skills-filter-row input.form-input';
248
+ if (this.mainTab === 'sessions') return '#panel-sessions .session-query-input';
249
+ if (this.mainTab === 'plugins' && this.pluginsActiveId === 'prompt-templates' && this.promptTemplatesMode !== 'compose') {
250
+ return '#panel-plugins .prompt-templates-toolbar input.form-input';
251
+ }
252
+ return '';
253
+ })();
254
+ if (focusSelector) {
255
+ const el = document.querySelector(focusSelector);
256
+ if (el && typeof el.focus === 'function') {
257
+ el.focus();
258
+ if (typeof el.select === 'function') {
259
+ el.select();
260
+ }
261
+ }
262
+ }
263
+ } catch (_) {}
264
+ }
265
+ return;
266
+ }
267
+ if (key !== 'Escape') {
268
+ return;
269
+ }
270
+ if (this.showConfirmDialog) {
271
+ event.preventDefault();
272
+ event.stopPropagation();
273
+ this.resolveConfirmDialog(false);
274
+ return;
275
+ }
276
+ if (this.showSkillsModal && typeof this.closeSkillsModal === 'function') {
277
+ event.preventDefault();
278
+ event.stopPropagation();
279
+ this.closeSkillsModal();
280
+ return;
281
+ }
282
+ if (this.showOpenclawConfigModal && typeof this.closeOpenclawConfigModal === 'function') {
283
+ event.preventDefault();
284
+ event.stopPropagation();
285
+ this.closeOpenclawConfigModal();
286
+ return;
287
+ }
288
+ if (this.showConfigTemplateModal && typeof this.closeConfigTemplateModal === 'function') {
289
+ event.preventDefault();
290
+ event.stopPropagation();
291
+ this.closeConfigTemplateModal();
292
+ return;
293
+ }
294
+ if (this.showEditConfigModal && typeof this.closeEditConfigModal === 'function') {
295
+ event.preventDefault();
296
+ event.stopPropagation();
297
+ this.closeEditConfigModal();
298
+ return;
299
+ }
300
+ if (this.showClaudeConfigModal && typeof this.closeClaudeConfigModal === 'function') {
301
+ event.preventDefault();
302
+ event.stopPropagation();
303
+ this.closeClaudeConfigModal();
304
+ return;
305
+ }
306
+ if (this.showModelModal && typeof this.closeModelModal === 'function') {
307
+ event.preventDefault();
308
+ event.stopPropagation();
309
+ this.closeModelModal();
310
+ return;
311
+ }
312
+ if (this.showAddModal && typeof this.closeAddModal === 'function') {
313
+ event.preventDefault();
314
+ event.stopPropagation();
315
+ this.closeAddModal();
316
+ return;
317
+ }
318
+ if (this.showEditModal && typeof this.closeEditModal === 'function') {
319
+ event.preventDefault();
320
+ event.stopPropagation();
321
+ this.closeEditModal();
322
+ return;
323
+ }
324
+ if (!this.showAgentsModal) {
325
+ return;
326
+ }
327
+ event.preventDefault();
328
+ event.stopPropagation();
329
+ if (this.agentsSaving || this.agentsDiffLoading) {
330
+ return;
331
+ }
332
+ if (this.agentsDiffVisible) {
333
+ this.resetAgentsDiffState();
334
+ return;
335
+ }
336
+ this.closeAgentsModal();
337
+ },
338
+ hasPendingAgentsDraft() {
339
+ if (!this.showAgentsModal || this.agentsLoading) {
340
+ return false;
341
+ }
342
+ return !!this.agentsSaving || this.hasAgentsContentChanged() || this.agentsDiffVisible;
343
+ },
344
+ handleBeforeUnload(event) {
345
+ if (!this.hasPendingAgentsDraft()) {
346
+ return;
347
+ }
348
+ if (event && typeof event.preventDefault === 'function') {
349
+ event.preventDefault();
350
+ event.returnValue = '';
351
+ }
352
+ return '';
353
+ },
354
+ hasAgentsContentChanged() {
355
+ const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
356
+ const current = typeof this.agentsContent === 'string' ? this.agentsContent : '';
357
+ return original !== current;
358
+ },
359
+ requestConfirmDialog(options = {}) {
360
+ if (typeof this.confirmDialogResolver === 'function') {
361
+ this.confirmDialogResolver(false);
362
+ }
363
+ const confirmDisabled = options.confirmDisabled;
364
+ const t = typeof this.t === 'function' ? this.t : null;
365
+ this.confirmDialogTitle = typeof options.title === 'string' && options.title.trim()
366
+ ? options.title.trim()
367
+ : (t ? t('confirm.title.default') : '请确认操作');
368
+ this.confirmDialogMessage = typeof options.message === 'string' ? options.message : '';
369
+ this.confirmDialogConfirmText = typeof options.confirmText === 'string' && options.confirmText.trim()
370
+ ? options.confirmText.trim()
371
+ : (t ? t('confirm.ok') : '确认');
372
+ this.confirmDialogCancelText = typeof options.cancelText === 'string' && options.cancelText.trim()
373
+ ? options.cancelText.trim()
374
+ : (t ? t('confirm.cancel') : '取消');
375
+ this.confirmDialogDanger = !!options.danger;
376
+ this.confirmDialogConfirmDisabled = typeof confirmDisabled === 'function' ? false : !!confirmDisabled;
377
+ this.confirmDialogDisableWhen = typeof confirmDisabled === 'function' ? confirmDisabled : null;
378
+ this.showConfirmDialog = true;
379
+ return new Promise((resolve) => {
380
+ this.confirmDialogResolver = resolve;
381
+ });
382
+ },
383
+ isConfirmDialogDisabled() {
384
+ if (typeof this.confirmDialogDisableWhen === 'function') {
385
+ try {
386
+ return !!this.confirmDialogDisableWhen.call(this);
387
+ } catch (_) {
388
+ return true;
389
+ }
390
+ }
391
+ return !!this.confirmDialogConfirmDisabled;
392
+ },
393
+ resolveConfirmDialog(confirmed) {
394
+ const resolver = typeof this.confirmDialogResolver === 'function'
395
+ ? this.confirmDialogResolver
396
+ : null;
397
+ this.showConfirmDialog = false;
398
+ this.confirmDialogTitle = '';
399
+ this.confirmDialogMessage = '';
400
+ if (typeof this.t === 'function') {
401
+ this.confirmDialogConfirmText = this.t('confirm.ok');
402
+ this.confirmDialogCancelText = this.t('confirm.cancel');
403
+ } else {
404
+ this.confirmDialogConfirmText = '确认';
405
+ this.confirmDialogCancelText = '取消';
406
+ }
407
+ this.confirmDialogDanger = false;
408
+ this.confirmDialogConfirmDisabled = false;
409
+ this.confirmDialogDisableWhen = null;
410
+ this.confirmDialogResolver = null;
411
+ if (resolver) {
412
+ resolver(!!confirmed);
413
+ }
414
+ },
415
+ closeConfirmDialog() {
416
+ this.resolveConfirmDialog(false);
417
+ },
418
+ onAgentsContentInput() {
419
+ if (this.agentsDiffVisible || this.agentsDiffLines.length) {
420
+ this.resetAgentsDiffState();
421
+ }
422
+ },
423
+ buildAgentsDiffFingerprint() {
424
+ const context = this.agentsContext || 'codex';
425
+ const fileName = context === 'openclaw-workspace'
426
+ ? (this.agentsWorkspaceFileName || '')
427
+ : '';
428
+ const lineEnding = this.agentsLineEnding || '\n';
429
+ const content = typeof this.agentsContent === 'string' ? this.agentsContent : '';
430
+ const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
431
+ return `${context}::${fileName}::${lineEnding}::${content.length}::${content}::${original.length}::${original}`;
432
+ },
433
+ async prepareAgentsDiff() {
434
+ const requestFingerprint = this.buildAgentsDiffFingerprint();
435
+ const requestToken = Symbol('agents-diff-preview');
436
+ this._agentsDiffPreviewRequestToken = requestToken;
437
+ this.agentsDiffVisible = true;
438
+ this.agentsDiffLoading = true;
439
+ this.agentsDiffError = '';
440
+ this.agentsDiffLines = [];
441
+ this.agentsDiffStats = {
442
+ added: 0,
443
+ removed: 0,
444
+ unchanged: 0
445
+ };
446
+ this.agentsDiffTruncated = false;
447
+ this.agentsDiffHasChangesValue = false;
448
+ try {
449
+ const shouldApplyPreviewState = () => shouldApplyAgentsDiffPreviewResponse({
450
+ isVisible: this.agentsDiffVisible,
451
+ requestToken,
452
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
453
+ requestFingerprint,
454
+ currentFingerprint: this.buildAgentsDiffFingerprint()
455
+ });
456
+ const applyPreviewState = (diff) => {
457
+ if (!shouldApplyPreviewState()) {
458
+ return false;
459
+ }
460
+ const normalizedDiff = diff && typeof diff === 'object' ? diff : {};
461
+ const rawLines = Array.isArray(normalizedDiff.lines) ? normalizedDiff.lines : [];
462
+ this.agentsDiffLines = rawLines.filter(line => line && line.type);
463
+ this.agentsDiffTruncated = !!normalizedDiff.truncated;
464
+ this.agentsDiffHasChangesValue = !!normalizedDiff.hasChanges;
465
+ if (normalizedDiff.stats && typeof normalizedDiff.stats === 'object') {
466
+ this.agentsDiffStats = {
467
+ added: Number(normalizedDiff.stats.added || 0),
468
+ removed: Number(normalizedDiff.stats.removed || 0),
469
+ unchanged: Number(normalizedDiff.stats.unchanged || 0)
470
+ };
471
+ } else {
472
+ const stats = { added: 0, removed: 0, unchanged: 0 };
473
+ for (const line of this.agentsDiffLines) {
474
+ if (line && line.type === 'add') stats.added += 1;
475
+ else if (line && line.type === 'del') stats.removed += 1;
476
+ else stats.unchanged += 1;
477
+ }
478
+ this.agentsDiffStats = stats;
479
+ }
480
+ this.agentsDiffFingerprint = requestFingerprint;
481
+ return true;
482
+ };
483
+ const previewRequest = buildAgentsDiffPreviewRequest({
484
+ baseContent: this.agentsOriginalContent,
485
+ content: this.agentsContent,
486
+ lineEnding: this.agentsLineEnding,
487
+ context: this.agentsContext,
488
+ fileName: this.agentsWorkspaceFileName
489
+ });
490
+ if (previewRequest.exceedsBodyLimit) {
491
+ applyPreviewState(buildAgentsDiffPreview({
492
+ baseContent: this.agentsOriginalContent,
493
+ content: this.agentsContent
494
+ }));
495
+ return;
496
+ }
497
+ const res = await apiWithMeta('preview-agents-diff', previewRequest.params);
498
+ if (!shouldApplyPreviewState()) {
499
+ return;
500
+ }
501
+ if (res.error) {
502
+ if (isAgentsDiffPreviewPayloadTooLarge(res)) {
503
+ applyPreviewState(buildAgentsDiffPreview({
504
+ baseContent: this.agentsOriginalContent,
505
+ content: this.agentsContent
506
+ }));
507
+ return;
508
+ }
509
+ this.agentsDiffError = res.error;
510
+ return;
511
+ }
512
+ applyPreviewState(res.diff);
513
+ } catch (e) {
514
+ if (shouldApplyAgentsDiffPreviewResponse({
515
+ isVisible: this.agentsDiffVisible,
516
+ requestToken,
517
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
518
+ requestFingerprint,
519
+ currentFingerprint: this.buildAgentsDiffFingerprint()
520
+ })) {
521
+ this.agentsDiffError = '生成差异失败';
522
+ }
523
+ } finally {
524
+ if (this._agentsDiffPreviewRequestToken === requestToken) {
525
+ this.agentsDiffLoading = false;
526
+ }
527
+ }
528
+ },
529
+
530
+ async closeAgentsModal(options = {}) {
531
+ const force = !!(options && options.force);
532
+ if (!force && (this.agentsSaving || this.agentsDiffLoading)) {
533
+ return;
534
+ }
535
+ const shouldConfirmClose = !force
536
+ && this.hasPendingAgentsDraft();
537
+ if (shouldConfirmClose) {
538
+ const message = this.agentsDiffVisible
539
+ ? '当前处于差异预览模式,改动尚未保存。确认放弃改动并关闭吗?'
540
+ : '存在未保存改动,确认放弃改动并关闭吗?(关闭页面或应用也会丢失改动)';
541
+ const confirmed = await this.requestConfirmDialog({
542
+ title: '放弃未保存改动',
543
+ message,
544
+ confirmText: '放弃并关闭',
545
+ cancelText: '继续编辑',
546
+ danger: true
547
+ });
548
+ if (!confirmed) {
549
+ return;
550
+ }
551
+ }
552
+ issueLatestRequestToken(this, '_agentsOpenRequestToken');
553
+ this.agentsLoading = false;
554
+ this.showAgentsModal = false;
555
+ this.agentsContent = '';
556
+ this.agentsOriginalContent = '';
557
+ this.agentsPath = '';
558
+ this.agentsExists = false;
559
+ this.agentsLineEnding = '\n';
560
+ this.agentsSaving = false;
561
+ this.agentsWorkspaceFileName = '';
562
+ this.resetAgentsDiffState();
563
+ this.setAgentsModalContext('codex');
564
+ },
565
+
566
+ async applyAgentsContent() {
567
+ if (this.agentsSaving) {
568
+ return;
569
+ }
570
+ if (!this.agentsDiffVisible) {
571
+ if (!this.hasAgentsContentChanged()) {
572
+ this.showMessage('未检测到改动', 'info');
573
+ return;
574
+ }
575
+ await this.prepareAgentsDiff();
576
+ return;
577
+ }
578
+ if (this.agentsDiffLoading) {
579
+ return;
580
+ }
581
+ if (this.agentsDiffError) {
582
+ this.showMessage(this.agentsDiffError, 'error');
583
+ return;
584
+ }
585
+ const fingerprint = this.buildAgentsDiffFingerprint();
586
+ if (this.agentsDiffFingerprint !== fingerprint) {
587
+ await this.prepareAgentsDiff();
588
+ return;
589
+ }
590
+ if (!this.agentsDiffHasChanges) {
591
+ this.showMessage('未检测到改动', 'info');
592
+ return;
593
+ }
594
+ if (this.agentsContext === 'openclaw-workspace' && !isValidOpenclawWorkspaceFileName(this.agentsWorkspaceFileName)) {
595
+ this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
596
+ return;
597
+ }
598
+ this.agentsSaving = true;
599
+ try {
600
+ let action = 'apply-agents-file';
601
+ const params = {
602
+ content: this.agentsContent,
603
+ lineEnding: this.agentsLineEnding
604
+ };
605
+ if (this.agentsContext === 'claude-md') {
606
+ action = 'apply-claude-md-file';
607
+ } else if (this.agentsContext === 'openclaw') {
608
+ action = 'apply-openclaw-agents-file';
609
+ } else if (this.agentsContext === 'openclaw-workspace') {
610
+ action = 'apply-openclaw-workspace-file';
611
+ params.fileName = this.agentsWorkspaceFileName;
612
+ }
613
+ const res = await api(action, params);
614
+ if (res.error) {
615
+ this.showMessage(res.error, 'error');
616
+ return;
617
+ }
618
+ const successLabel = this.agentsContext === 'openclaw-workspace'
619
+ ? `工作区文件已保存${this.agentsWorkspaceFileName ? `: ${this.agentsWorkspaceFileName}` : ''}`
620
+ : (this.agentsContext === 'claude-md'
621
+ ? 'CLAUDE.md 已保存'
622
+ : (this.agentsContext === 'openclaw' ? 'OpenClaw AGENTS.md 已保存' : 'AGENTS.md 已保存'));
623
+ this.showMessage(successLabel, 'success');
624
+ this.closeAgentsModal({ force: true });
625
+ } catch (e) {
626
+ this.showMessage('保存失败', 'error');
627
+ } finally {
628
+ this.agentsSaving = false;
629
+ }
630
+ }
631
+ };
632
+ }