codexmate 0.0.32 → 0.0.33

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 (147) hide show
  1. package/README.md +147 -363
  2. package/README.zh.md +147 -371
  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 +1725 -1725
  7. package/cli/claude-proxy.js +1022 -1022
  8. package/cli/config-bootstrap.js +402 -402
  9. package/cli/config-health.js +454 -454
  10. package/cli/doctor-core.js +903 -903
  11. package/cli/import-skills-url.js +356 -356
  12. package/cli/local-bridge.js +551 -324
  13. package/cli/openai-bridge.js +1653 -1653
  14. package/cli/openclaw-config.js +629 -629
  15. package/cli/session-convert-args.js +69 -69
  16. package/cli/session-convert-io.js +82 -82
  17. package/cli/session-convert.js +150 -150
  18. package/cli/session-usage.concurrent.js +28 -28
  19. package/cli/session-usage.js +118 -118
  20. package/cli/session-usage.models.js +176 -176
  21. package/cli/skills.js +1141 -1141
  22. package/cli/update.js +162 -0
  23. package/cli/zip-commands.js +510 -510
  24. package/cli.js +16023 -15829
  25. package/lib/automation.js +404 -404
  26. package/lib/cli-file-utils.js +151 -151
  27. package/lib/cli-models-utils.js +440 -440
  28. package/lib/cli-network-utils.js +190 -190
  29. package/lib/cli-path-utils.js +85 -85
  30. package/lib/cli-session-utils.js +121 -121
  31. package/lib/cli-sessions.js +427 -426
  32. package/lib/cli-utils.js +155 -155
  33. package/lib/cli-webhook.js +126 -126
  34. package/lib/download-artifacts.js +92 -92
  35. package/lib/mcp-stdio.js +453 -453
  36. package/lib/task-orchestrator.js +869 -869
  37. package/lib/text-diff.js +303 -303
  38. package/lib/win-tray.js +119 -0
  39. package/lib/workflow-engine.js +340 -340
  40. package/package.json +76 -76
  41. package/plugins/README.md +20 -20
  42. package/plugins/README.zh-CN.md +20 -20
  43. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  44. package/plugins/prompt-templates/computed.mjs +253 -253
  45. package/plugins/prompt-templates/index.mjs +8 -8
  46. package/plugins/prompt-templates/manifest.mjs +15 -15
  47. package/plugins/prompt-templates/methods.mjs +553 -553
  48. package/plugins/prompt-templates/overview.mjs +91 -91
  49. package/plugins/prompt-templates/ownership.mjs +19 -19
  50. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  51. package/plugins/prompt-templates/storage.mjs +64 -64
  52. package/plugins/registry.mjs +16 -16
  53. package/web-ui/app.js +650 -647
  54. package/web-ui/index.html +36 -36
  55. package/web-ui/logic.agents-diff.mjs +386 -386
  56. package/web-ui/logic.claude.mjs +168 -168
  57. package/web-ui/logic.codex.mjs +69 -69
  58. package/web-ui/logic.mjs +5 -5
  59. package/web-ui/logic.runtime.mjs +128 -128
  60. package/web-ui/logic.session-convert.mjs +70 -70
  61. package/web-ui/logic.sessions.mjs +781 -781
  62. package/web-ui/modules/api.mjs +90 -90
  63. package/web-ui/modules/app.computed.dashboard.mjs +248 -248
  64. package/web-ui/modules/app.computed.index.mjs +17 -17
  65. package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
  66. package/web-ui/modules/app.computed.session.mjs +735 -693
  67. package/web-ui/modules/app.constants.mjs +15 -15
  68. package/web-ui/modules/app.methods.agents.mjs +651 -651
  69. package/web-ui/modules/app.methods.claude-config.mjs +269 -200
  70. package/web-ui/modules/app.methods.codex-config.mjs +861 -861
  71. package/web-ui/modules/app.methods.index.mjs +94 -94
  72. package/web-ui/modules/app.methods.install.mjs +205 -205
  73. package/web-ui/modules/app.methods.navigation.mjs +774 -774
  74. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  75. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  76. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  77. package/web-ui/modules/app.methods.providers.mjs +575 -529
  78. package/web-ui/modules/app.methods.runtime.mjs +345 -345
  79. package/web-ui/modules/app.methods.session-actions.mjs +591 -591
  80. package/web-ui/modules/app.methods.session-browser.mjs +1012 -1012
  81. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  82. package/web-ui/modules/app.methods.session-trash.mjs +438 -438
  83. package/web-ui/modules/app.methods.startup-claude.mjs +538 -537
  84. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  85. package/web-ui/modules/app.methods.webhook.mjs +79 -79
  86. package/web-ui/modules/config-mode.computed.mjs +124 -124
  87. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  88. package/web-ui/modules/i18n.dict.mjs +3192 -3177
  89. package/web-ui/modules/i18n.mjs +62 -62
  90. package/web-ui/modules/plugins.computed.mjs +3 -3
  91. package/web-ui/modules/plugins.methods.mjs +3 -3
  92. package/web-ui/modules/plugins.storage.mjs +11 -11
  93. package/web-ui/modules/provider-url-display.mjs +17 -17
  94. package/web-ui/modules/sessions-filters-url.mjs +85 -85
  95. package/web-ui/modules/skills.computed.mjs +107 -107
  96. package/web-ui/modules/skills.methods.mjs +482 -482
  97. package/web-ui/partials/index/layout-footer.html +13 -13
  98. package/web-ui/partials/index/layout-header.html +499 -503
  99. package/web-ui/partials/index/modal-config-template-agents.html +185 -185
  100. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  101. package/web-ui/partials/index/modal-health-check.html +45 -45
  102. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  103. package/web-ui/partials/index/modal-skills.html +200 -200
  104. package/web-ui/partials/index/modals-basic.html +173 -162
  105. package/web-ui/partials/index/panel-config-claude.html +166 -136
  106. package/web-ui/partials/index/panel-config-codex.html +190 -196
  107. package/web-ui/partials/index/panel-config-codex.html.bak +337 -0
  108. package/web-ui/partials/index/panel-config-openclaw.html +83 -83
  109. package/web-ui/partials/index/panel-dashboard.html +186 -219
  110. package/web-ui/partials/index/panel-docs.html +114 -114
  111. package/web-ui/partials/index/panel-market.html +177 -177
  112. package/web-ui/partials/index/panel-orchestration.html +391 -391
  113. package/web-ui/partials/index/panel-plugins.html +253 -253
  114. package/web-ui/partials/index/panel-sessions.html +319 -313
  115. package/web-ui/partials/index/panel-settings.html +190 -190
  116. package/web-ui/partials/index/panel-trash.html +82 -82
  117. package/web-ui/partials/index/panel-usage.html +137 -137
  118. package/web-ui/res/json5.min.js +1 -1
  119. package/web-ui/res/vue.global.prod.js +13 -13
  120. package/web-ui/session-helpers.mjs +591 -591
  121. package/web-ui/source-bundle.cjs +233 -233
  122. package/web-ui/styles/base-theme.css +281 -281
  123. package/web-ui/styles/bridge-pool.css +197 -197
  124. package/web-ui/styles/controls-forms.css +433 -433
  125. package/web-ui/styles/dashboard.css +406 -406
  126. package/web-ui/styles/docs-panel.css +245 -245
  127. package/web-ui/styles/feedback.css +108 -108
  128. package/web-ui/styles/health-check-dialog.css +144 -144
  129. package/web-ui/styles/layout-shell.css +628 -638
  130. package/web-ui/styles/modals-core.css +499 -466
  131. package/web-ui/styles/navigation-panels.css +391 -391
  132. package/web-ui/styles/openclaw-structured.css +266 -266
  133. package/web-ui/styles/plugins-panel.css +564 -564
  134. package/web-ui/styles/responsive.css +392 -392
  135. package/web-ui/styles/sessions-list.css +683 -647
  136. package/web-ui/styles/sessions-preview.css +407 -407
  137. package/web-ui/styles/sessions-toolbar-trash.css +518 -518
  138. package/web-ui/styles/sessions-usage.css +588 -588
  139. package/web-ui/styles/settings-panel.css +349 -349
  140. package/web-ui/styles/skills-list.css +305 -305
  141. package/web-ui/styles/skills-market.css +429 -429
  142. package/web-ui/styles/task-orchestration.css +822 -822
  143. package/web-ui/styles/titles-cards.css +472 -472
  144. package/web-ui/styles/trash-panel.css +90 -90
  145. package/web-ui/styles/webhook.css +81 -81
  146. package/web-ui/styles.css +24 -24
  147. package/web-ui.html +17 -17
@@ -1,651 +1,651 @@
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
- async pasteAgentsContent() {
424
- if (this.agentsLoading || this.agentsSaving || this.agentsDiffVisible) {
425
- return;
426
- }
427
- let text = '';
428
- try {
429
- text = await navigator.clipboard.readText();
430
- } catch (_) {
431
- this.showMessage('无法读取剪贴板', 'error');
432
- return;
433
- }
434
- if (typeof text !== 'string' || !text) {
435
- this.showMessage('剪贴板为空', 'info');
436
- return;
437
- }
438
- this.agentsContent = text;
439
- this.onAgentsContentInput();
440
- this.showMessage('已粘贴', 'success');
441
- },
442
- buildAgentsDiffFingerprint() {
443
- const context = this.agentsContext || 'codex';
444
- const fileName = context === 'openclaw-workspace'
445
- ? (this.agentsWorkspaceFileName || '')
446
- : '';
447
- const lineEnding = this.agentsLineEnding || '\n';
448
- const content = typeof this.agentsContent === 'string' ? this.agentsContent : '';
449
- const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
450
- return `${context}::${fileName}::${lineEnding}::${content.length}::${content}::${original.length}::${original}`;
451
- },
452
- async prepareAgentsDiff() {
453
- const requestFingerprint = this.buildAgentsDiffFingerprint();
454
- const requestToken = Symbol('agents-diff-preview');
455
- this._agentsDiffPreviewRequestToken = requestToken;
456
- this.agentsDiffVisible = true;
457
- this.agentsDiffLoading = true;
458
- this.agentsDiffError = '';
459
- this.agentsDiffLines = [];
460
- this.agentsDiffStats = {
461
- added: 0,
462
- removed: 0,
463
- unchanged: 0
464
- };
465
- this.agentsDiffTruncated = false;
466
- this.agentsDiffHasChangesValue = false;
467
- try {
468
- const shouldApplyPreviewState = () => shouldApplyAgentsDiffPreviewResponse({
469
- isVisible: this.agentsDiffVisible,
470
- requestToken,
471
- activeRequestToken: this._agentsDiffPreviewRequestToken,
472
- requestFingerprint,
473
- currentFingerprint: this.buildAgentsDiffFingerprint()
474
- });
475
- const applyPreviewState = (diff) => {
476
- if (!shouldApplyPreviewState()) {
477
- return false;
478
- }
479
- const normalizedDiff = diff && typeof diff === 'object' ? diff : {};
480
- const rawLines = Array.isArray(normalizedDiff.lines) ? normalizedDiff.lines : [];
481
- this.agentsDiffLines = rawLines.filter(line => line && line.type);
482
- this.agentsDiffTruncated = !!normalizedDiff.truncated;
483
- this.agentsDiffHasChangesValue = !!normalizedDiff.hasChanges;
484
- if (normalizedDiff.stats && typeof normalizedDiff.stats === 'object') {
485
- this.agentsDiffStats = {
486
- added: Number(normalizedDiff.stats.added || 0),
487
- removed: Number(normalizedDiff.stats.removed || 0),
488
- unchanged: Number(normalizedDiff.stats.unchanged || 0)
489
- };
490
- } else {
491
- const stats = { added: 0, removed: 0, unchanged: 0 };
492
- for (const line of this.agentsDiffLines) {
493
- if (line && line.type === 'add') stats.added += 1;
494
- else if (line && line.type === 'del') stats.removed += 1;
495
- else stats.unchanged += 1;
496
- }
497
- this.agentsDiffStats = stats;
498
- }
499
- this.agentsDiffFingerprint = requestFingerprint;
500
- return true;
501
- };
502
- const previewRequest = buildAgentsDiffPreviewRequest({
503
- baseContent: this.agentsOriginalContent,
504
- content: this.agentsContent,
505
- lineEnding: this.agentsLineEnding,
506
- context: this.agentsContext,
507
- fileName: this.agentsWorkspaceFileName
508
- });
509
- if (previewRequest.exceedsBodyLimit) {
510
- applyPreviewState(buildAgentsDiffPreview({
511
- baseContent: this.agentsOriginalContent,
512
- content: this.agentsContent
513
- }));
514
- return;
515
- }
516
- const res = await apiWithMeta('preview-agents-diff', previewRequest.params);
517
- if (!shouldApplyPreviewState()) {
518
- return;
519
- }
520
- if (res.error) {
521
- if (isAgentsDiffPreviewPayloadTooLarge(res)) {
522
- applyPreviewState(buildAgentsDiffPreview({
523
- baseContent: this.agentsOriginalContent,
524
- content: this.agentsContent
525
- }));
526
- return;
527
- }
528
- this.agentsDiffError = res.error;
529
- return;
530
- }
531
- applyPreviewState(res.diff);
532
- } catch (e) {
533
- if (shouldApplyAgentsDiffPreviewResponse({
534
- isVisible: this.agentsDiffVisible,
535
- requestToken,
536
- activeRequestToken: this._agentsDiffPreviewRequestToken,
537
- requestFingerprint,
538
- currentFingerprint: this.buildAgentsDiffFingerprint()
539
- })) {
540
- this.agentsDiffError = '生成差异失败';
541
- }
542
- } finally {
543
- if (this._agentsDiffPreviewRequestToken === requestToken) {
544
- this.agentsDiffLoading = false;
545
- }
546
- }
547
- },
548
-
549
- async closeAgentsModal(options = {}) {
550
- const force = !!(options && options.force);
551
- if (!force && (this.agentsSaving || this.agentsDiffLoading)) {
552
- return;
553
- }
554
- const shouldConfirmClose = !force
555
- && this.hasPendingAgentsDraft();
556
- if (shouldConfirmClose) {
557
- const message = this.agentsDiffVisible
558
- ? '当前处于差异预览模式,改动尚未保存。确认放弃改动并关闭吗?'
559
- : '存在未保存改动,确认放弃改动并关闭吗?(关闭页面或应用也会丢失改动)';
560
- const confirmed = await this.requestConfirmDialog({
561
- title: '放弃未保存改动',
562
- message,
563
- confirmText: '放弃并关闭',
564
- cancelText: '继续编辑',
565
- danger: true
566
- });
567
- if (!confirmed) {
568
- return;
569
- }
570
- }
571
- issueLatestRequestToken(this, '_agentsOpenRequestToken');
572
- this.agentsLoading = false;
573
- this.showAgentsModal = false;
574
- this.agentsContent = '';
575
- this.agentsOriginalContent = '';
576
- this.agentsPath = '';
577
- this.agentsExists = false;
578
- this.agentsLineEnding = '\n';
579
- this.agentsSaving = false;
580
- this.agentsWorkspaceFileName = '';
581
- this.resetAgentsDiffState();
582
- this.setAgentsModalContext('codex');
583
- },
584
-
585
- async applyAgentsContent() {
586
- if (this.agentsSaving) {
587
- return;
588
- }
589
- if (!this.agentsDiffVisible) {
590
- if (!this.hasAgentsContentChanged()) {
591
- this.showMessage('未检测到改动', 'info');
592
- return;
593
- }
594
- await this.prepareAgentsDiff();
595
- return;
596
- }
597
- if (this.agentsDiffLoading) {
598
- return;
599
- }
600
- if (this.agentsDiffError) {
601
- this.showMessage(this.agentsDiffError, 'error');
602
- return;
603
- }
604
- const fingerprint = this.buildAgentsDiffFingerprint();
605
- if (this.agentsDiffFingerprint !== fingerprint) {
606
- await this.prepareAgentsDiff();
607
- return;
608
- }
609
- if (!this.agentsDiffHasChanges) {
610
- this.showMessage('未检测到改动', 'info');
611
- return;
612
- }
613
- if (this.agentsContext === 'openclaw-workspace' && !isValidOpenclawWorkspaceFileName(this.agentsWorkspaceFileName)) {
614
- this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
615
- return;
616
- }
617
- this.agentsSaving = true;
618
- try {
619
- let action = 'apply-agents-file';
620
- const params = {
621
- content: this.agentsContent,
622
- lineEnding: this.agentsLineEnding
623
- };
624
- if (this.agentsContext === 'claude-md') {
625
- action = 'apply-claude-md-file';
626
- } else if (this.agentsContext === 'openclaw') {
627
- action = 'apply-openclaw-agents-file';
628
- } else if (this.agentsContext === 'openclaw-workspace') {
629
- action = 'apply-openclaw-workspace-file';
630
- params.fileName = this.agentsWorkspaceFileName;
631
- }
632
- const res = await api(action, params);
633
- if (res.error) {
634
- this.showMessage(res.error, 'error');
635
- return;
636
- }
637
- const successLabel = this.agentsContext === 'openclaw-workspace'
638
- ? `工作区文件已保存${this.agentsWorkspaceFileName ? `: ${this.agentsWorkspaceFileName}` : ''}`
639
- : (this.agentsContext === 'claude-md'
640
- ? 'CLAUDE.md 已保存'
641
- : (this.agentsContext === 'openclaw' ? 'OpenClaw AGENTS.md 已保存' : 'AGENTS.md 已保存'));
642
- this.showMessage(successLabel, 'success');
643
- this.closeAgentsModal({ force: true });
644
- } catch (e) {
645
- this.showMessage('保存失败', 'error');
646
- } finally {
647
- this.agentsSaving = false;
648
- }
649
- }
650
- };
651
- }
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
+ async pasteAgentsContent() {
424
+ if (this.agentsLoading || this.agentsSaving || this.agentsDiffVisible) {
425
+ return;
426
+ }
427
+ let text = '';
428
+ try {
429
+ text = await navigator.clipboard.readText();
430
+ } catch (_) {
431
+ this.showMessage('无法读取剪贴板', 'error');
432
+ return;
433
+ }
434
+ if (typeof text !== 'string' || !text) {
435
+ this.showMessage('剪贴板为空', 'info');
436
+ return;
437
+ }
438
+ this.agentsContent = text;
439
+ this.onAgentsContentInput();
440
+ this.showMessage('已粘贴', 'success');
441
+ },
442
+ buildAgentsDiffFingerprint() {
443
+ const context = this.agentsContext || 'codex';
444
+ const fileName = context === 'openclaw-workspace'
445
+ ? (this.agentsWorkspaceFileName || '')
446
+ : '';
447
+ const lineEnding = this.agentsLineEnding || '\n';
448
+ const content = typeof this.agentsContent === 'string' ? this.agentsContent : '';
449
+ const original = typeof this.agentsOriginalContent === 'string' ? this.agentsOriginalContent : '';
450
+ return `${context}::${fileName}::${lineEnding}::${content.length}::${content}::${original.length}::${original}`;
451
+ },
452
+ async prepareAgentsDiff() {
453
+ const requestFingerprint = this.buildAgentsDiffFingerprint();
454
+ const requestToken = Symbol('agents-diff-preview');
455
+ this._agentsDiffPreviewRequestToken = requestToken;
456
+ this.agentsDiffVisible = true;
457
+ this.agentsDiffLoading = true;
458
+ this.agentsDiffError = '';
459
+ this.agentsDiffLines = [];
460
+ this.agentsDiffStats = {
461
+ added: 0,
462
+ removed: 0,
463
+ unchanged: 0
464
+ };
465
+ this.agentsDiffTruncated = false;
466
+ this.agentsDiffHasChangesValue = false;
467
+ try {
468
+ const shouldApplyPreviewState = () => shouldApplyAgentsDiffPreviewResponse({
469
+ isVisible: this.agentsDiffVisible,
470
+ requestToken,
471
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
472
+ requestFingerprint,
473
+ currentFingerprint: this.buildAgentsDiffFingerprint()
474
+ });
475
+ const applyPreviewState = (diff) => {
476
+ if (!shouldApplyPreviewState()) {
477
+ return false;
478
+ }
479
+ const normalizedDiff = diff && typeof diff === 'object' ? diff : {};
480
+ const rawLines = Array.isArray(normalizedDiff.lines) ? normalizedDiff.lines : [];
481
+ this.agentsDiffLines = rawLines.filter(line => line && line.type);
482
+ this.agentsDiffTruncated = !!normalizedDiff.truncated;
483
+ this.agentsDiffHasChangesValue = !!normalizedDiff.hasChanges;
484
+ if (normalizedDiff.stats && typeof normalizedDiff.stats === 'object') {
485
+ this.agentsDiffStats = {
486
+ added: Number(normalizedDiff.stats.added || 0),
487
+ removed: Number(normalizedDiff.stats.removed || 0),
488
+ unchanged: Number(normalizedDiff.stats.unchanged || 0)
489
+ };
490
+ } else {
491
+ const stats = { added: 0, removed: 0, unchanged: 0 };
492
+ for (const line of this.agentsDiffLines) {
493
+ if (line && line.type === 'add') stats.added += 1;
494
+ else if (line && line.type === 'del') stats.removed += 1;
495
+ else stats.unchanged += 1;
496
+ }
497
+ this.agentsDiffStats = stats;
498
+ }
499
+ this.agentsDiffFingerprint = requestFingerprint;
500
+ return true;
501
+ };
502
+ const previewRequest = buildAgentsDiffPreviewRequest({
503
+ baseContent: this.agentsOriginalContent,
504
+ content: this.agentsContent,
505
+ lineEnding: this.agentsLineEnding,
506
+ context: this.agentsContext,
507
+ fileName: this.agentsWorkspaceFileName
508
+ });
509
+ if (previewRequest.exceedsBodyLimit) {
510
+ applyPreviewState(buildAgentsDiffPreview({
511
+ baseContent: this.agentsOriginalContent,
512
+ content: this.agentsContent
513
+ }));
514
+ return;
515
+ }
516
+ const res = await apiWithMeta('preview-agents-diff', previewRequest.params);
517
+ if (!shouldApplyPreviewState()) {
518
+ return;
519
+ }
520
+ if (res.error) {
521
+ if (isAgentsDiffPreviewPayloadTooLarge(res)) {
522
+ applyPreviewState(buildAgentsDiffPreview({
523
+ baseContent: this.agentsOriginalContent,
524
+ content: this.agentsContent
525
+ }));
526
+ return;
527
+ }
528
+ this.agentsDiffError = res.error;
529
+ return;
530
+ }
531
+ applyPreviewState(res.diff);
532
+ } catch (e) {
533
+ if (shouldApplyAgentsDiffPreviewResponse({
534
+ isVisible: this.agentsDiffVisible,
535
+ requestToken,
536
+ activeRequestToken: this._agentsDiffPreviewRequestToken,
537
+ requestFingerprint,
538
+ currentFingerprint: this.buildAgentsDiffFingerprint()
539
+ })) {
540
+ this.agentsDiffError = '生成差异失败';
541
+ }
542
+ } finally {
543
+ if (this._agentsDiffPreviewRequestToken === requestToken) {
544
+ this.agentsDiffLoading = false;
545
+ }
546
+ }
547
+ },
548
+
549
+ async closeAgentsModal(options = {}) {
550
+ const force = !!(options && options.force);
551
+ if (!force && (this.agentsSaving || this.agentsDiffLoading)) {
552
+ return;
553
+ }
554
+ const shouldConfirmClose = !force
555
+ && this.hasPendingAgentsDraft();
556
+ if (shouldConfirmClose) {
557
+ const message = this.agentsDiffVisible
558
+ ? '当前处于差异预览模式,改动尚未保存。确认放弃改动并关闭吗?'
559
+ : '存在未保存改动,确认放弃改动并关闭吗?(关闭页面或应用也会丢失改动)';
560
+ const confirmed = await this.requestConfirmDialog({
561
+ title: '放弃未保存改动',
562
+ message,
563
+ confirmText: '放弃并关闭',
564
+ cancelText: '继续编辑',
565
+ danger: true
566
+ });
567
+ if (!confirmed) {
568
+ return;
569
+ }
570
+ }
571
+ issueLatestRequestToken(this, '_agentsOpenRequestToken');
572
+ this.agentsLoading = false;
573
+ this.showAgentsModal = false;
574
+ this.agentsContent = '';
575
+ this.agentsOriginalContent = '';
576
+ this.agentsPath = '';
577
+ this.agentsExists = false;
578
+ this.agentsLineEnding = '\n';
579
+ this.agentsSaving = false;
580
+ this.agentsWorkspaceFileName = '';
581
+ this.resetAgentsDiffState();
582
+ this.setAgentsModalContext('codex');
583
+ },
584
+
585
+ async applyAgentsContent() {
586
+ if (this.agentsSaving) {
587
+ return;
588
+ }
589
+ if (!this.agentsDiffVisible) {
590
+ if (!this.hasAgentsContentChanged()) {
591
+ this.showMessage('未检测到改动', 'info');
592
+ return;
593
+ }
594
+ await this.prepareAgentsDiff();
595
+ return;
596
+ }
597
+ if (this.agentsDiffLoading) {
598
+ return;
599
+ }
600
+ if (this.agentsDiffError) {
601
+ this.showMessage(this.agentsDiffError, 'error');
602
+ return;
603
+ }
604
+ const fingerprint = this.buildAgentsDiffFingerprint();
605
+ if (this.agentsDiffFingerprint !== fingerprint) {
606
+ await this.prepareAgentsDiff();
607
+ return;
608
+ }
609
+ if (!this.agentsDiffHasChanges) {
610
+ this.showMessage('未检测到改动', 'info');
611
+ return;
612
+ }
613
+ if (this.agentsContext === 'openclaw-workspace' && !isValidOpenclawWorkspaceFileName(this.agentsWorkspaceFileName)) {
614
+ this.showMessage('仅支持 OpenClaw Workspace 内的 `.md` 文件', 'error');
615
+ return;
616
+ }
617
+ this.agentsSaving = true;
618
+ try {
619
+ let action = 'apply-agents-file';
620
+ const params = {
621
+ content: this.agentsContent,
622
+ lineEnding: this.agentsLineEnding
623
+ };
624
+ if (this.agentsContext === 'claude-md') {
625
+ action = 'apply-claude-md-file';
626
+ } else if (this.agentsContext === 'openclaw') {
627
+ action = 'apply-openclaw-agents-file';
628
+ } else if (this.agentsContext === 'openclaw-workspace') {
629
+ action = 'apply-openclaw-workspace-file';
630
+ params.fileName = this.agentsWorkspaceFileName;
631
+ }
632
+ const res = await api(action, params);
633
+ if (res.error) {
634
+ this.showMessage(res.error, 'error');
635
+ return;
636
+ }
637
+ const successLabel = this.agentsContext === 'openclaw-workspace'
638
+ ? `工作区文件已保存${this.agentsWorkspaceFileName ? `: ${this.agentsWorkspaceFileName}` : ''}`
639
+ : (this.agentsContext === 'claude-md'
640
+ ? 'CLAUDE.md 已保存'
641
+ : (this.agentsContext === 'openclaw' ? 'OpenClaw AGENTS.md 已保存' : 'AGENTS.md 已保存'));
642
+ this.showMessage(successLabel, 'success');
643
+ this.closeAgentsModal({ force: true });
644
+ } catch (e) {
645
+ this.showMessage('保存失败', 'error');
646
+ } finally {
647
+ this.agentsSaving = false;
648
+ }
649
+ }
650
+ };
651
+ }