codexmate 0.0.21 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +390 -284
  2. package/README.zh.md +322 -0
  3. package/cli/agents-files.js +224 -162
  4. package/cli/archive-helpers.js +446 -446
  5. package/cli/auth-profiles.js +359 -359
  6. package/cli/builtin-proxy.js +1044 -580
  7. package/cli/claude-proxy.js +998 -998
  8. package/cli/config-bootstrap.js +384 -384
  9. package/cli/config-health.js +338 -338
  10. package/cli/openai-bridge.js +950 -0
  11. package/cli/openclaw-config.js +629 -629
  12. package/cli/session-usage.concurrent.js +28 -0
  13. package/cli/session-usage.js +112 -0
  14. package/cli/session-usage.models.js +176 -0
  15. package/cli/skills.js +1141 -1141
  16. package/cli/zip-commands.js +510 -510
  17. package/cli.js +13214 -13129
  18. package/lib/cli-file-utils.js +151 -151
  19. package/lib/cli-models-utils.js +419 -419
  20. package/lib/cli-network-utils.js +164 -164
  21. package/lib/cli-path-utils.js +69 -69
  22. package/lib/cli-session-utils.js +121 -121
  23. package/lib/cli-sessions.js +386 -386
  24. package/lib/cli-utils.js +155 -155
  25. package/lib/download-artifacts.js +77 -77
  26. package/lib/mcp-stdio.js +440 -440
  27. package/lib/task-orchestrator.js +869 -869
  28. package/lib/text-diff.js +303 -303
  29. package/lib/workflow-engine.js +340 -340
  30. package/package.json +74 -74
  31. package/res/json5.min.js +1 -1
  32. package/res/logo.png +0 -0
  33. package/res/vue.global.prod.js +13 -13
  34. package/web-ui/app.js +626 -530
  35. package/web-ui/index.html +34 -33
  36. package/web-ui/logic.agents-diff.mjs +386 -386
  37. package/web-ui/logic.claude.mjs +168 -168
  38. package/web-ui/logic.mjs +5 -5
  39. package/web-ui/logic.runtime.mjs +128 -124
  40. package/web-ui/logic.sessions.mjs +614 -581
  41. package/web-ui/modules/api.mjs +90 -90
  42. package/web-ui/modules/app.computed.dashboard.mjs +126 -113
  43. package/web-ui/modules/app.computed.index.mjs +17 -15
  44. package/web-ui/modules/app.computed.main-tabs.mjs +202 -195
  45. package/web-ui/modules/app.computed.session.mjs +653 -507
  46. package/web-ui/modules/app.constants.mjs +15 -15
  47. package/web-ui/modules/app.methods.agents.mjs +544 -493
  48. package/web-ui/modules/app.methods.claude-config.mjs +174 -174
  49. package/web-ui/modules/app.methods.codex-config.mjs +795 -640
  50. package/web-ui/modules/app.methods.index.mjs +92 -88
  51. package/web-ui/modules/app.methods.install.mjs +177 -149
  52. package/web-ui/modules/app.methods.navigation.mjs +662 -619
  53. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  54. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  55. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  56. package/web-ui/modules/app.methods.providers.mjs +404 -363
  57. package/web-ui/modules/app.methods.runtime.mjs +323 -323
  58. package/web-ui/modules/app.methods.session-actions.mjs +537 -520
  59. package/web-ui/modules/app.methods.session-browser.mjs +691 -626
  60. package/web-ui/modules/app.methods.session-timeline.mjs +448 -448
  61. package/web-ui/modules/app.methods.session-trash.mjs +422 -422
  62. package/web-ui/modules/app.methods.startup-claude.mjs +417 -412
  63. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -471
  64. package/web-ui/modules/config-mode.computed.mjs +126 -126
  65. package/web-ui/modules/config-template-confirm-pref.mjs +33 -0
  66. package/web-ui/modules/i18n.mjs +1823 -0
  67. package/web-ui/modules/plugins.computed.mjs +3 -0
  68. package/web-ui/modules/plugins.methods.mjs +3 -0
  69. package/web-ui/modules/plugins.storage.mjs +11 -0
  70. package/web-ui/modules/sessions-filters-url.mjs +85 -0
  71. package/web-ui/modules/skills.computed.mjs +107 -107
  72. package/web-ui/modules/skills.methods.mjs +481 -481
  73. package/web-ui/partials/index/layout-footer.html +13 -13
  74. package/web-ui/partials/index/layout-header.html +461 -402
  75. package/web-ui/partials/index/modal-config-template-agents.html +175 -125
  76. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  77. package/web-ui/partials/index/modal-health-check.html +72 -72
  78. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  79. package/web-ui/partials/index/modal-skills.html +200 -184
  80. package/web-ui/partials/index/modals-basic.html +165 -156
  81. package/web-ui/partials/index/panel-config-claude.html +159 -126
  82. package/web-ui/partials/index/panel-config-codex.html +255 -237
  83. package/web-ui/partials/index/panel-config-openclaw.html +78 -78
  84. package/web-ui/partials/index/panel-docs.html +147 -130
  85. package/web-ui/partials/index/panel-market.html +174 -174
  86. package/web-ui/partials/index/panel-orchestration.html +388 -397
  87. package/web-ui/partials/index/panel-plugins.html +273 -0
  88. package/web-ui/partials/index/panel-sessions.html +298 -292
  89. package/web-ui/partials/index/panel-settings.html +258 -190
  90. package/web-ui/partials/index/panel-usage.html +353 -213
  91. package/web-ui/session-helpers.mjs +573 -559
  92. package/web-ui/source-bundle.cjs +233 -233
  93. package/web-ui/styles/base-theme.css +264 -271
  94. package/web-ui/styles/controls-forms.css +369 -360
  95. package/web-ui/styles/docs-panel.css +247 -182
  96. package/web-ui/styles/feedback.css +108 -108
  97. package/web-ui/styles/health-check-dialog.css +144 -144
  98. package/web-ui/styles/layout-shell.css +602 -376
  99. package/web-ui/styles/modals-core.css +464 -464
  100. package/web-ui/styles/navigation-panels.css +390 -348
  101. package/web-ui/styles/openclaw-structured.css +266 -266
  102. package/web-ui/styles/plugins-panel.css +523 -0
  103. package/web-ui/styles/responsive.css +456 -450
  104. package/web-ui/styles/sessions-list.css +400 -400
  105. package/web-ui/styles/sessions-preview.css +411 -411
  106. package/web-ui/styles/sessions-toolbar-trash.css +268 -243
  107. package/web-ui/styles/sessions-usage.css +879 -628
  108. package/web-ui/styles/settings-panel.css +166 -0
  109. package/web-ui/styles/skills-list.css +303 -296
  110. package/web-ui/styles/skills-market.css +406 -335
  111. package/web-ui/styles/task-orchestration.css +822 -776
  112. package/web-ui/styles/titles-cards.css +408 -408
  113. package/web-ui/styles.css +20 -18
  114. package/web-ui.html +17 -17
  115. package/README.en.md +0 -349
@@ -1,520 +1,537 @@
1
- export function createSessionActionMethods(options = {}) {
2
- const {
3
- api,
4
- apiBase
5
- } = options;
6
-
7
- return {
8
- getSessionStandaloneContext() {
9
- try {
10
- const url = new URL(window.location.href);
11
- if (url.pathname !== '/session') {
12
- return { requested: false, params: null, error: '' };
13
- }
14
-
15
- const source = (url.searchParams.get('source') || '').trim().toLowerCase();
16
- const sessionId = (url.searchParams.get('sessionId') || url.searchParams.get('id') || '').trim();
17
- const filePath = (url.searchParams.get('filePath') || url.searchParams.get('path') || '').trim();
18
- let error = '';
19
- if (!source) {
20
- error = '缺少 source 参数';
21
- } else if (source !== 'codex' && source !== 'claude') {
22
- error = 'source 仅支持 codex claude';
23
- }
24
- if (!sessionId && !filePath) {
25
- error = error ? `${error},还缺少 sessionId 或 filePath` : '缺少 sessionId 或 filePath 参数';
26
- }
27
-
28
- if (error) {
29
- return { requested: true, params: null, error };
30
- }
31
-
32
- return {
33
- requested: true,
34
- params: {
35
- source,
36
- sessionId,
37
- filePath
38
- },
39
- error: ''
40
- };
41
- } catch (_) {
42
- return { requested: false, params: null, error: '' };
43
- }
44
- },
45
-
46
- initSessionStandalone() {
47
- const context = this.getSessionStandaloneContext();
48
- if (!context.requested) return;
49
-
50
- this.sessionStandalone = true;
51
- this.mainTab = 'sessions';
52
- this.prepareSessionTabRender();
53
-
54
- if (context.error || !context.params) {
55
- this.sessionStandaloneError = `会话链接参数不完整:${context.error || '参数解析失败'}`;
56
- return;
57
- }
58
-
59
- const sourceLabel = context.params.source === 'codex' ? 'Codex' : 'Claude Code';
60
- this.activeSession = {
61
- source: context.params.source,
62
- sourceLabel,
63
- sessionId: context.params.sessionId,
64
- filePath: context.params.filePath,
65
- title: context.params.sessionId || context.params.filePath || '会话'
66
- };
67
- this.activeSessionMessages = [];
68
- this.activeSessionDetailError = '';
69
- this.activeSessionDetailClipped = false;
70
- this.cancelSessionTimelineSync();
71
- this.sessionTimelineActiveKey = '';
72
- this.clearSessionTimelineRefs();
73
- this.sessionStandaloneError = '';
74
- this.sessionStandaloneText = '';
75
- this.sessionStandaloneTitle = this.activeSession.title || '会话';
76
- this.sessionStandaloneSourceLabel = sourceLabel;
77
- this.loadSessionStandalonePlain();
78
- },
79
-
80
- buildSessionStandaloneUrl(session) {
81
- if (!session) return '';
82
- const source = typeof session.source === 'string' ? session.source.trim().toLowerCase() : '';
83
- if (!source || (source !== 'codex' && source !== 'claude')) return '';
84
- const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
85
- const filePath = typeof session.filePath === 'string' ? session.filePath.trim() : '';
86
- if (!sessionId && !filePath) return '';
87
- const origin = window.location.origin && window.location.origin !== 'null'
88
- ? window.location.origin
89
- : (typeof apiBase === 'string' ? apiBase.trim() : '');
90
- if (!origin) return '';
91
- const params = new URLSearchParams();
92
- params.set('source', source);
93
- if (sessionId) params.set('sessionId', sessionId);
94
- if (filePath) params.set('filePath', filePath);
95
- return `${origin}/session?${params.toString()}`;
96
- },
97
-
98
- openSessionStandalone(session) {
99
- const url = this.buildSessionStandaloneUrl(session);
100
- if (!url) {
101
- this.showMessage('无法生成链接', 'error');
102
- return;
103
- }
104
- window.open(url, '_blank', 'noopener');
105
- },
106
-
107
- getSessionExportKey(session) {
108
- return `${session.source || 'unknown'}:${session.sessionId || ''}:${session.filePath || ''}`;
109
- },
110
-
111
- isResumeCommandAvailable(session) {
112
- if (!session) return false;
113
- const source = String(session.source || '').trim().toLowerCase();
114
- const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
115
- return source === 'codex' && !!sessionId;
116
- },
117
-
118
- isCloneAvailable(session) {
119
- if (!session) return false;
120
- const source = String(session.source || '').trim().toLowerCase();
121
- const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
122
- const filePath = typeof session.filePath === 'string' ? session.filePath.trim() : '';
123
- return source === 'codex' && (!!sessionId || !!filePath);
124
- },
125
-
126
- isDeleteAvailable(session) {
127
- if (!session) return false;
128
- const source = String(session.source || '').trim().toLowerCase();
129
- if (source !== 'codex' && source !== 'claude') return false;
130
- const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
131
- const filePath = typeof session.filePath === 'string' ? session.filePath.trim() : '';
132
- return !!sessionId || !!filePath;
133
- },
134
-
135
- buildResumeCommand(session) {
136
- const sessionId = session && session.sessionId ? String(session.sessionId).trim() : '';
137
- const arg = this.quoteResumeArg(sessionId);
138
- if (this.sessionResumeWithYolo) {
139
- return `codex --yolo resume ${arg}`;
140
- }
141
- return `codex resume ${arg}`;
142
- },
143
-
144
- quoteShellArg(value) {
145
- const text = typeof value === 'string' ? value : String(value || '');
146
- if (!text) return "''";
147
- if (/^[a-zA-Z0-9._-]+$/.test(text)) return text;
148
- const escaped = text.replace(/'/g, "'\\''");
149
- return `'${escaped}'`;
150
- },
151
-
152
- quoteResumeArg(value) {
153
- return this.quoteShellArg(value);
154
- },
155
-
156
- normalizeShareCommandPrefix(value) {
157
- const normalized = typeof value === 'string' ? value.trim().toLowerCase() : '';
158
- if (normalized === 'codexmate') {
159
- return 'codexmate';
160
- }
161
- return 'npm start';
162
- },
163
-
164
- normalizeSessionTrashEnabled(value) {
165
- if (value === false) return false;
166
- const normalized = typeof value === 'string' ? value.trim().toLowerCase() : '';
167
- if (normalized === '0' || normalized === 'false' || normalized === 'off' || normalized === 'no') {
168
- return false;
169
- }
170
- return true;
171
- },
172
-
173
- setSessionTrashEnabled(value) {
174
- const enabled = this.normalizeSessionTrashEnabled(value);
175
- this.sessionTrashEnabled = enabled;
176
- try {
177
- localStorage.setItem('codexmateSessionTrashEnabled', enabled ? 'true' : 'false');
178
- } catch (_) {}
179
- },
180
-
181
- getShareCommandPrefixInvocation() {
182
- const prefix = this.normalizeShareCommandPrefix(this.shareCommandPrefix);
183
- return prefix === 'codexmate' ? 'codexmate' : 'npm start';
184
- },
185
-
186
- setShareCommandPrefix(value) {
187
- const normalized = this.normalizeShareCommandPrefix(value);
188
- this.shareCommandPrefix = normalized;
189
- try {
190
- localStorage.setItem('codexmateShareCommandPrefix', normalized);
191
- } catch (_) {}
192
- },
193
-
194
- fallbackCopyText(text) {
195
- let textarea = null;
196
- try {
197
- textarea = document.createElement('textarea');
198
- textarea.value = text;
199
- textarea.setAttribute('readonly', '');
200
- textarea.style.position = 'fixed';
201
- textarea.style.top = '-9999px';
202
- textarea.style.left = '-9999px';
203
- textarea.style.opacity = '0';
204
- document.body.appendChild(textarea);
205
- textarea.select();
206
- textarea.setSelectionRange(0, textarea.value.length);
207
- return document.execCommand('copy');
208
- } catch (_) {
209
- return false;
210
- } finally {
211
- if (textarea && textarea.parentNode) {
212
- textarea.parentNode.removeChild(textarea);
213
- }
214
- }
215
- },
216
-
217
- copyAgentsContent() {
218
- const text = typeof this.agentsContent === 'string' ? this.agentsContent : '';
219
- if (!text) {
220
- this.showMessage('没有可复制内容', 'info');
221
- return;
222
- }
223
- const ok = this.fallbackCopyText(text);
224
- if (ok) {
225
- this.showMessage('已复制', 'success');
226
- return;
227
- }
228
- this.showMessage('复制失败', 'error');
229
- },
230
-
231
- exportAgentsContent() {
232
- const text = typeof this.agentsContent === 'string' ? this.agentsContent : '';
233
- if (!text) {
234
- this.showMessage('没有可导出内容', 'info');
235
- return;
236
- }
237
- const now = new Date();
238
- const year = String(now.getFullYear());
239
- const month = String(now.getMonth() + 1).padStart(2, '0');
240
- const day = String(now.getDate()).padStart(2, '0');
241
- const hour = String(now.getHours()).padStart(2, '0');
242
- const minute = String(now.getMinutes()).padStart(2, '0');
243
- const second = String(now.getSeconds()).padStart(2, '0');
244
- const fileName = `agent-${year}${month}${day}-${hour}${minute}${second}.txt`;
245
- this.downloadTextFile(fileName, text, 'text/plain;charset=utf-8');
246
- this.showMessage(`已导出 ${fileName}`, 'success');
247
- },
248
-
249
- async copyInstallCommand(cmd) {
250
- const text = typeof cmd === 'string' ? cmd.trim() : '';
251
- if (!text) {
252
- this.showMessage('没有可复制内容', 'info');
253
- return;
254
- }
255
- try {
256
- if (navigator.clipboard && window.isSecureContext) {
257
- await navigator.clipboard.writeText(text);
258
- this.showMessage('已复制命令', 'success');
259
- return;
260
- }
261
- } catch (_) {}
262
- const ok = this.fallbackCopyText(text);
263
- if (ok) {
264
- this.showMessage('已复制命令', 'success');
265
- return;
266
- }
267
- this.showMessage('复制失败', 'error');
268
- },
269
-
270
- async copyResumeCommand(session) {
271
- if (!this.isResumeCommandAvailable(session)) {
272
- this.showMessage('不支持此操作', 'error');
273
- return;
274
- }
275
- const command = this.buildResumeCommand(session);
276
- const ok = this.fallbackCopyText(command);
277
- if (ok) {
278
- this.showMessage('已复制', 'success');
279
- return;
280
- }
281
- try {
282
- if (navigator.clipboard && window.isSecureContext) {
283
- await navigator.clipboard.writeText(command);
284
- this.showMessage('已复制', 'success');
285
- return;
286
- }
287
- } catch (_) {}
288
- this.showMessage('复制失败', 'error');
289
- },
290
-
291
- buildProviderShareCommand(payload) {
292
- if (!payload || typeof payload !== 'object') return '';
293
- const name = typeof payload.name === 'string' ? payload.name.trim() : '';
294
- const baseUrl = typeof payload.baseUrl === 'string' ? payload.baseUrl.trim() : '';
295
- const apiKey = typeof payload.apiKey === 'string' ? payload.apiKey : '';
296
- const model = typeof payload.model === 'string' ? payload.model.trim() : '';
297
- if (!name || !baseUrl) return '';
298
-
299
- const cli = this.getShareCommandPrefixInvocation();
300
- const nameArg = this.quoteShellArg(name);
301
- const urlArg = this.quoteShellArg(baseUrl);
302
- const keyArg = apiKey ? this.quoteShellArg(apiKey) : '';
303
- const switchCmd = `${cli} switch ${nameArg}`;
304
- const addCmd = apiKey
305
- ? `${cli} add ${nameArg} ${urlArg} ${keyArg}`
306
- : `${cli} add ${nameArg} ${urlArg}`;
307
- const modelCmd = model ? ` && ${cli} use ${this.quoteShellArg(model)}` : '';
308
- return `${addCmd} && ${switchCmd}${modelCmd}`;
309
- },
310
-
311
- buildClaudeShareCommand(payload) {
312
- if (!payload || typeof payload !== 'object') return '';
313
- const baseUrl = typeof payload.baseUrl === 'string' ? payload.baseUrl.trim() : '';
314
- const apiKey = typeof payload.apiKey === 'string' ? payload.apiKey : '';
315
- const model = typeof payload.model === 'string' && payload.model.trim()
316
- ? payload.model.trim()
317
- : 'glm-4.7';
318
- if (!baseUrl || !apiKey) return '';
319
- const urlArg = this.quoteShellArg(baseUrl);
320
- const keyArg = this.quoteShellArg(apiKey);
321
- const modelArg = this.quoteShellArg(model);
322
- return `${this.getShareCommandPrefixInvocation()} claude ${urlArg} ${keyArg} ${modelArg}`;
323
- },
324
-
325
- async copyProviderShareCommand(provider) {
326
- const name = provider && typeof provider.name === 'string' ? provider.name.trim() : '';
327
- if (!name) {
328
- this.showMessage('参数无效', 'error');
329
- return;
330
- }
331
- if (!this.shouldAllowProviderShare(provider)) {
332
- this.showMessage('不可分享', 'info');
333
- return;
334
- }
335
- if (this.providerShareLoading[name]) {
336
- return;
337
- }
338
- this.providerShareLoading[name] = true;
339
- try {
340
- const res = await api('export-provider', { name });
341
- if (res && res.error) {
342
- this.showMessage(res.error, 'error');
343
- return;
344
- }
345
- const command = this.buildProviderShareCommand(res && res.payload ? res.payload : null);
346
- if (!command) {
347
- this.showMessage('生成命令失败', 'error');
348
- return;
349
- }
350
- const ok = this.fallbackCopyText(command);
351
- if (ok) {
352
- this.showMessage('已复制', 'success');
353
- return;
354
- }
355
- try {
356
- if (navigator.clipboard && window.isSecureContext) {
357
- await navigator.clipboard.writeText(command);
358
- this.showMessage('已复制', 'success');
359
- return;
360
- }
361
- } catch (_) {}
362
- this.showMessage('复制失败', 'error');
363
- } catch (_) {
364
- this.showMessage('生成命令失败', 'error');
365
- } finally {
366
- this.providerShareLoading[name] = false;
367
- }
368
- },
369
-
370
- async copyClaudeShareCommand(name) {
371
- const config = this.claudeConfigs[name];
372
- if (!config) {
373
- this.showMessage('配置不存在', 'error');
374
- return;
375
- }
376
- if (this.claudeShareLoading[name]) return;
377
- this.claudeShareLoading[name] = true;
378
- try {
379
- const res = await api('export-claude-share', { config });
380
- if (res && res.error) {
381
- this.showMessage(res.error, 'error');
382
- return;
383
- }
384
- const command = this.buildClaudeShareCommand(res && res.payload ? res.payload : null);
385
- if (!command) {
386
- this.showMessage('生成命令失败', 'error');
387
- return;
388
- }
389
- const ok = this.fallbackCopyText(command);
390
- if (ok) {
391
- this.showMessage('已复制', 'success');
392
- return;
393
- }
394
- try {
395
- if (navigator.clipboard && window.isSecureContext) {
396
- await navigator.clipboard.writeText(command);
397
- this.showMessage('已复制', 'success');
398
- return;
399
- }
400
- } catch (_) {}
401
- this.showMessage('复制失败', 'error');
402
- } catch (_) {
403
- this.showMessage('生成命令失败', 'error');
404
- } finally {
405
- this.claudeShareLoading[name] = false;
406
- }
407
- },
408
-
409
- async cloneSession(session) {
410
- if (!this.isCloneAvailable(session)) {
411
- this.showMessage('不支持此操作', 'error');
412
- return;
413
- }
414
- const key = this.getSessionExportKey(session);
415
- if (this.sessionCloning[key]) {
416
- return;
417
- }
418
- this.sessionCloning[key] = true;
419
- try {
420
- const res = await api('clone-session', {
421
- source: session.source,
422
- sessionId: session.sessionId,
423
- filePath: session.filePath
424
- });
425
- if (res.error) {
426
- this.showMessage(res.error, 'error');
427
- return;
428
- }
429
-
430
- this.showMessage('操作成功', 'success');
431
- if (typeof this.invalidateSessionsUsageData === 'function') {
432
- this.invalidateSessionsUsageData({ preserveList: true });
433
- }
434
- try {
435
- await this.loadSessions();
436
- if (res.sessionId) {
437
- const matched = this.sessionsList.find(item => item.source === 'codex' && item.sessionId === res.sessionId);
438
- if (matched) {
439
- await this.selectSession(matched);
440
- }
441
- }
442
- } catch (_) {
443
- // The clone already succeeded remotely; keep the success result.
444
- }
445
- } catch (_) {
446
- this.showMessage('克隆失败', 'error');
447
- } finally {
448
- this.sessionCloning[key] = false;
449
- }
450
- },
451
-
452
- async deleteSession(session) {
453
- if (!this.isDeleteAvailable(session)) {
454
- this.showMessage('不支持此操作', 'error');
455
- return;
456
- }
457
- const useTrash = this.sessionTrashEnabled !== false;
458
- if (!useTrash && typeof this.requestConfirmDialog === 'function') {
459
- const confirmed = await this.requestConfirmDialog({
460
- title: '直接删除会话',
461
- message: '关闭回收站后,删除会话将直接永久删除,且无法恢复。',
462
- confirmText: '直接删除',
463
- cancelText: '取消',
464
- danger: true
465
- });
466
- if (!confirmed) {
467
- return;
468
- }
469
- }
470
- const key = this.getSessionExportKey(session);
471
- if (this.sessionDeleting[key]) {
472
- return;
473
- }
474
- this.sessionDeleting[key] = true;
475
- try {
476
- const action = useTrash ? 'trash-session' : 'delete-session';
477
- const res = await api(action, {
478
- source: session.source,
479
- sessionId: session.sessionId,
480
- filePath: session.filePath
481
- });
482
- if (!res || res.error) {
483
- this.showMessage((res && res.error) || '删除失败', 'error');
484
- return;
485
- }
486
- this.removeSessionPin(session);
487
- if (useTrash) {
488
- this.invalidateSessionTrashRequests();
489
- this.showMessage('已移入回收站', 'success');
490
- if (this.sessionTrashLoadedOnce) {
491
- this.prependSessionTrashItem(this.buildSessionTrashItemFromSession(session, res), {
492
- totalCount: res && res.totalCount !== undefined ? res.totalCount : undefined
493
- });
494
- } else {
495
- this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(
496
- res && res.totalCount !== undefined
497
- ? res.totalCount
498
- : (this.normalizeSessionTrashTotalCount(this.sessionTrashTotalCount, this.sessionTrashItems) + 1),
499
- this.sessionTrashItems
500
- );
501
- }
502
- } else {
503
- this.showMessage('已删除', 'success');
504
- }
505
- if (typeof this.invalidateSessionsUsageData === 'function') {
506
- this.invalidateSessionsUsageData({ preserveList: true });
507
- }
508
- try {
509
- await this.removeSessionFromCurrentList(session);
510
- } catch (_) {
511
- // The delete already succeeded remotely; keep the success result.
512
- }
513
- } catch (_) {
514
- this.showMessage('删除失败', 'error');
515
- } finally {
516
- this.sessionDeleting[key] = false;
517
- }
518
- }
519
- };
520
- }
1
+ import {
2
+ normalizeConfigTemplateDiffConfirmEnabled,
3
+ persistConfigTemplateDiffConfirmEnabledToStorage
4
+ } from './config-template-confirm-pref.mjs';
5
+
6
+ export function createSessionActionMethods(options = {}) {
7
+ const {
8
+ api,
9
+ apiBase
10
+ } = options;
11
+
12
+ return {
13
+ getSessionStandaloneContext() {
14
+ try {
15
+ const url = new URL(window.location.href);
16
+ if (url.pathname !== '/session') {
17
+ return { requested: false, params: null, error: '' };
18
+ }
19
+
20
+ const source = (url.searchParams.get('source') || '').trim().toLowerCase();
21
+ const sessionId = (url.searchParams.get('sessionId') || url.searchParams.get('id') || '').trim();
22
+ const filePath = (url.searchParams.get('filePath') || url.searchParams.get('path') || '').trim();
23
+ let error = '';
24
+ if (!source) {
25
+ error = '缺少 source 参数';
26
+ } else if (source !== 'codex' && source !== 'claude') {
27
+ error = 'source 仅支持 codex 或 claude';
28
+ }
29
+ if (!sessionId && !filePath) {
30
+ error = error ? `${error},还缺少 sessionId 或 filePath` : '缺少 sessionId 或 filePath 参数';
31
+ }
32
+
33
+ if (error) {
34
+ return { requested: true, params: null, error };
35
+ }
36
+
37
+ return {
38
+ requested: true,
39
+ params: {
40
+ source,
41
+ sessionId,
42
+ filePath
43
+ },
44
+ error: ''
45
+ };
46
+ } catch (_) {
47
+ return { requested: false, params: null, error: '' };
48
+ }
49
+ },
50
+
51
+ initSessionStandalone() {
52
+ const context = this.getSessionStandaloneContext();
53
+ if (!context.requested) return;
54
+
55
+ this.sessionStandalone = true;
56
+ this.mainTab = 'sessions';
57
+ this.prepareSessionTabRender();
58
+
59
+ if (context.error || !context.params) {
60
+ this.sessionStandaloneError = `会话链接参数不完整:${context.error || '参数解析失败'}`;
61
+ return;
62
+ }
63
+
64
+ const sourceLabel = context.params.source === 'codex' ? 'Codex' : 'Claude Code';
65
+ this.activeSession = {
66
+ source: context.params.source,
67
+ sourceLabel,
68
+ sessionId: context.params.sessionId,
69
+ filePath: context.params.filePath,
70
+ title: context.params.sessionId || context.params.filePath || '会话'
71
+ };
72
+ this.activeSessionMessages = [];
73
+ this.activeSessionDetailError = '';
74
+ this.activeSessionDetailClipped = false;
75
+ this.cancelSessionTimelineSync();
76
+ this.sessionTimelineActiveKey = '';
77
+ this.clearSessionTimelineRefs();
78
+ this.sessionStandaloneError = '';
79
+ this.sessionStandaloneText = '';
80
+ this.sessionStandaloneTitle = this.activeSession.title || '会话';
81
+ this.sessionStandaloneSourceLabel = sourceLabel;
82
+ this.loadSessionStandalonePlain();
83
+ },
84
+
85
+ buildSessionStandaloneUrl(session) {
86
+ if (!session) return '';
87
+ const source = typeof session.source === 'string' ? session.source.trim().toLowerCase() : '';
88
+ if (!source || (source !== 'codex' && source !== 'claude')) return '';
89
+ const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
90
+ const filePath = typeof session.filePath === 'string' ? session.filePath.trim() : '';
91
+ if (!sessionId && !filePath) return '';
92
+ const origin = window.location.origin && window.location.origin !== 'null'
93
+ ? window.location.origin
94
+ : (typeof apiBase === 'string' ? apiBase.trim() : '');
95
+ if (!origin) return '';
96
+ const params = new URLSearchParams();
97
+ params.set('source', source);
98
+ if (sessionId) params.set('sessionId', sessionId);
99
+ if (filePath) params.set('filePath', filePath);
100
+ return `${origin}/session?${params.toString()}`;
101
+ },
102
+
103
+ openSessionStandalone(session) {
104
+ const url = this.buildSessionStandaloneUrl(session);
105
+ if (!url) {
106
+ this.showMessage('无法生成链接', 'error');
107
+ return;
108
+ }
109
+ window.open(url, '_blank', 'noopener');
110
+ },
111
+
112
+ getSessionExportKey(session) {
113
+ return `${session.source || 'unknown'}:${session.sessionId || ''}:${session.filePath || ''}`;
114
+ },
115
+
116
+ isResumeCommandAvailable(session) {
117
+ if (!session) return false;
118
+ const source = String(session.source || '').trim().toLowerCase();
119
+ const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
120
+ return source === 'codex' && !!sessionId;
121
+ },
122
+
123
+ isCloneAvailable(session) {
124
+ if (!session) return false;
125
+ const source = String(session.source || '').trim().toLowerCase();
126
+ const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
127
+ const filePath = typeof session.filePath === 'string' ? session.filePath.trim() : '';
128
+ return source === 'codex' && (!!sessionId || !!filePath);
129
+ },
130
+
131
+ isDeleteAvailable(session) {
132
+ if (!session) return false;
133
+ const source = String(session.source || '').trim().toLowerCase();
134
+ if (source !== 'codex' && source !== 'claude') return false;
135
+ const sessionId = typeof session.sessionId === 'string' ? session.sessionId.trim() : '';
136
+ const filePath = typeof session.filePath === 'string' ? session.filePath.trim() : '';
137
+ return !!sessionId || !!filePath;
138
+ },
139
+
140
+ buildResumeCommand(session) {
141
+ const sessionId = session && session.sessionId ? String(session.sessionId).trim() : '';
142
+ const arg = this.quoteResumeArg(sessionId);
143
+ if (this.sessionResumeWithYolo) {
144
+ return `codex --yolo resume ${arg}`;
145
+ }
146
+ return `codex resume ${arg}`;
147
+ },
148
+
149
+ quoteShellArg(value) {
150
+ const text = typeof value === 'string' ? value : String(value || '');
151
+ if (!text) return "''";
152
+ if (/^[a-zA-Z0-9._-]+$/.test(text)) return text;
153
+ const escaped = text.replace(/'/g, "'\\''");
154
+ return `'${escaped}'`;
155
+ },
156
+
157
+ quoteResumeArg(value) {
158
+ return this.quoteShellArg(value);
159
+ },
160
+
161
+ normalizeShareCommandPrefix(value) {
162
+ const normalized = typeof value === 'string' ? value.trim().toLowerCase() : '';
163
+ if (normalized === 'codexmate') {
164
+ return 'codexmate';
165
+ }
166
+ return 'npm start';
167
+ },
168
+
169
+ normalizeSessionTrashEnabled(value) {
170
+ if (value === false) return false;
171
+ const normalized = typeof value === 'string' ? value.trim().toLowerCase() : '';
172
+ if (normalized === '0' || normalized === 'false' || normalized === 'off' || normalized === 'no') {
173
+ return false;
174
+ }
175
+ return true;
176
+ },
177
+
178
+ normalizeConfigTemplateDiffConfirmEnabled(value) {
179
+ return normalizeConfigTemplateDiffConfirmEnabled(value);
180
+ },
181
+
182
+ setSessionTrashEnabled(value) {
183
+ const enabled = this.normalizeSessionTrashEnabled(value);
184
+ this.sessionTrashEnabled = enabled;
185
+ try {
186
+ localStorage.setItem('codexmateSessionTrashEnabled', enabled ? 'true' : 'false');
187
+ } catch (_) {}
188
+ },
189
+
190
+ setConfigTemplateDiffConfirmEnabled(value) {
191
+ const enabled = this.normalizeConfigTemplateDiffConfirmEnabled(value);
192
+ this.configTemplateDiffConfirmEnabled = enabled;
193
+ persistConfigTemplateDiffConfirmEnabledToStorage(enabled);
194
+ },
195
+
196
+ getShareCommandPrefixInvocation() {
197
+ const prefix = this.normalizeShareCommandPrefix(this.shareCommandPrefix);
198
+ return prefix === 'codexmate' ? 'codexmate' : 'npm start';
199
+ },
200
+
201
+ setShareCommandPrefix(value) {
202
+ const normalized = this.normalizeShareCommandPrefix(value);
203
+ this.shareCommandPrefix = normalized;
204
+ try {
205
+ localStorage.setItem('codexmateShareCommandPrefix', normalized);
206
+ } catch (_) {}
207
+ },
208
+
209
+ fallbackCopyText(text) {
210
+ let textarea = null;
211
+ try {
212
+ textarea = document.createElement('textarea');
213
+ textarea.value = text;
214
+ textarea.setAttribute('readonly', '');
215
+ textarea.style.position = 'fixed';
216
+ textarea.style.top = '-9999px';
217
+ textarea.style.left = '-9999px';
218
+ textarea.style.opacity = '0';
219
+ document.body.appendChild(textarea);
220
+ textarea.select();
221
+ textarea.setSelectionRange(0, textarea.value.length);
222
+ return document.execCommand('copy');
223
+ } catch (_) {
224
+ return false;
225
+ } finally {
226
+ if (textarea && textarea.parentNode) {
227
+ textarea.parentNode.removeChild(textarea);
228
+ }
229
+ }
230
+ },
231
+
232
+ copyAgentsContent() {
233
+ const text = typeof this.agentsContent === 'string' ? this.agentsContent : '';
234
+ if (!text) {
235
+ this.showMessage('没有可复制内容', 'info');
236
+ return;
237
+ }
238
+ const ok = this.fallbackCopyText(text);
239
+ if (ok) {
240
+ this.showMessage('已复制', 'success');
241
+ return;
242
+ }
243
+ this.showMessage('复制失败', 'error');
244
+ },
245
+
246
+ exportAgentsContent() {
247
+ const text = typeof this.agentsContent === 'string' ? this.agentsContent : '';
248
+ if (!text) {
249
+ this.showMessage('没有可导出内容', 'info');
250
+ return;
251
+ }
252
+ const now = new Date();
253
+ const year = String(now.getFullYear());
254
+ const month = String(now.getMonth() + 1).padStart(2, '0');
255
+ const day = String(now.getDate()).padStart(2, '0');
256
+ const hour = String(now.getHours()).padStart(2, '0');
257
+ const minute = String(now.getMinutes()).padStart(2, '0');
258
+ const second = String(now.getSeconds()).padStart(2, '0');
259
+ const fileName = `agent-${year}${month}${day}-${hour}${minute}${second}.txt`;
260
+ this.downloadTextFile(fileName, text, 'text/plain;charset=utf-8');
261
+ this.showMessage(`已导出 ${fileName}`, 'success');
262
+ },
263
+
264
+ async copyInstallCommand(cmd) {
265
+ const text = typeof cmd === 'string' ? cmd.trim() : '';
266
+ if (!text) {
267
+ this.showMessage('没有可复制内容', 'info');
268
+ return;
269
+ }
270
+ try {
271
+ if (navigator.clipboard && window.isSecureContext) {
272
+ await navigator.clipboard.writeText(text);
273
+ this.showMessage('已复制命令', 'success');
274
+ return;
275
+ }
276
+ } catch (_) {}
277
+ const ok = this.fallbackCopyText(text);
278
+ if (ok) {
279
+ this.showMessage('已复制命令', 'success');
280
+ return;
281
+ }
282
+ this.showMessage('复制失败', 'error');
283
+ },
284
+
285
+ async copyResumeCommand(session) {
286
+ if (!this.isResumeCommandAvailable(session)) {
287
+ this.showMessage('不支持此操作', 'error');
288
+ return;
289
+ }
290
+ const command = this.buildResumeCommand(session);
291
+ const ok = this.fallbackCopyText(command);
292
+ if (ok) {
293
+ this.showMessage('已复制', 'success');
294
+ return;
295
+ }
296
+ try {
297
+ if (navigator.clipboard && window.isSecureContext) {
298
+ await navigator.clipboard.writeText(command);
299
+ this.showMessage('已复制', 'success');
300
+ return;
301
+ }
302
+ } catch (_) {}
303
+ this.showMessage('复制失败', 'error');
304
+ },
305
+
306
+ buildProviderShareCommand(payload) {
307
+ if (!payload || typeof payload !== 'object') return '';
308
+ const name = typeof payload.name === 'string' ? payload.name.trim() : '';
309
+ const baseUrl = typeof payload.baseUrl === 'string' ? payload.baseUrl.trim() : '';
310
+ const apiKey = typeof payload.apiKey === 'string' ? payload.apiKey : '';
311
+ const model = typeof payload.model === 'string' ? payload.model.trim() : '';
312
+ const bridge = typeof payload.bridge === 'string' ? payload.bridge.trim() : '';
313
+ if (!name || !baseUrl) return '';
314
+
315
+ const cli = this.getShareCommandPrefixInvocation();
316
+ const nameArg = this.quoteShellArg(name);
317
+ const urlArg = this.quoteShellArg(baseUrl);
318
+ const keyArg = apiKey ? this.quoteShellArg(apiKey) : '';
319
+ const switchCmd = `${cli} switch ${nameArg}`;
320
+ const bridgeArgs = bridge ? ` --bridge ${this.quoteShellArg(bridge)}` : '';
321
+ const addCmd = apiKey
322
+ ? `${cli} add ${nameArg} ${urlArg} ${keyArg}${bridgeArgs}`
323
+ : `${cli} add ${nameArg} ${urlArg}${bridgeArgs}`;
324
+ const modelCmd = model ? ` && ${cli} use ${this.quoteShellArg(model)}` : '';
325
+ return `${addCmd} && ${switchCmd}${modelCmd}`;
326
+ },
327
+
328
+ buildClaudeShareCommand(payload) {
329
+ if (!payload || typeof payload !== 'object') return '';
330
+ const baseUrl = typeof payload.baseUrl === 'string' ? payload.baseUrl.trim() : '';
331
+ const apiKey = typeof payload.apiKey === 'string' ? payload.apiKey : '';
332
+ const model = typeof payload.model === 'string' && payload.model.trim()
333
+ ? payload.model.trim()
334
+ : 'glm-4.7';
335
+ if (!baseUrl || !apiKey) return '';
336
+ const urlArg = this.quoteShellArg(baseUrl);
337
+ const keyArg = this.quoteShellArg(apiKey);
338
+ const modelArg = this.quoteShellArg(model);
339
+ return `${this.getShareCommandPrefixInvocation()} claude ${urlArg} ${keyArg} ${modelArg}`;
340
+ },
341
+
342
+ async copyProviderShareCommand(provider) {
343
+ const name = provider && typeof provider.name === 'string' ? provider.name.trim() : '';
344
+ if (!name) {
345
+ this.showMessage('参数无效', 'error');
346
+ return;
347
+ }
348
+ if (!this.shouldAllowProviderShare(provider)) {
349
+ this.showMessage('不可分享', 'info');
350
+ return;
351
+ }
352
+ if (this.providerShareLoading[name]) {
353
+ return;
354
+ }
355
+ this.providerShareLoading[name] = true;
356
+ try {
357
+ const res = await api('export-provider', { name });
358
+ if (res && res.error) {
359
+ this.showMessage(res.error, 'error');
360
+ return;
361
+ }
362
+ const command = this.buildProviderShareCommand(res && res.payload ? res.payload : null);
363
+ if (!command) {
364
+ this.showMessage('生成命令失败', 'error');
365
+ return;
366
+ }
367
+ const ok = this.fallbackCopyText(command);
368
+ if (ok) {
369
+ this.showMessage('已复制', 'success');
370
+ return;
371
+ }
372
+ try {
373
+ if (navigator.clipboard && window.isSecureContext) {
374
+ await navigator.clipboard.writeText(command);
375
+ this.showMessage('已复制', 'success');
376
+ return;
377
+ }
378
+ } catch (_) {}
379
+ this.showMessage('复制失败', 'error');
380
+ } catch (_) {
381
+ this.showMessage('生成命令失败', 'error');
382
+ } finally {
383
+ this.providerShareLoading[name] = false;
384
+ }
385
+ },
386
+
387
+ async copyClaudeShareCommand(name) {
388
+ const config = this.claudeConfigs[name];
389
+ if (!config) {
390
+ this.showMessage('配置不存在', 'error');
391
+ return;
392
+ }
393
+ if (this.claudeShareLoading[name]) return;
394
+ this.claudeShareLoading[name] = true;
395
+ try {
396
+ const res = await api('export-claude-share', { config });
397
+ if (res && res.error) {
398
+ this.showMessage(res.error, 'error');
399
+ return;
400
+ }
401
+ const command = this.buildClaudeShareCommand(res && res.payload ? res.payload : null);
402
+ if (!command) {
403
+ this.showMessage('生成命令失败', 'error');
404
+ return;
405
+ }
406
+ const ok = this.fallbackCopyText(command);
407
+ if (ok) {
408
+ this.showMessage('已复制', 'success');
409
+ return;
410
+ }
411
+ try {
412
+ if (navigator.clipboard && window.isSecureContext) {
413
+ await navigator.clipboard.writeText(command);
414
+ this.showMessage('已复制', 'success');
415
+ return;
416
+ }
417
+ } catch (_) {}
418
+ this.showMessage('复制失败', 'error');
419
+ } catch (_) {
420
+ this.showMessage('生成命令失败', 'error');
421
+ } finally {
422
+ this.claudeShareLoading[name] = false;
423
+ }
424
+ },
425
+
426
+ async cloneSession(session) {
427
+ if (!this.isCloneAvailable(session)) {
428
+ this.showMessage('不支持此操作', 'error');
429
+ return;
430
+ }
431
+ const key = this.getSessionExportKey(session);
432
+ if (this.sessionCloning[key]) {
433
+ return;
434
+ }
435
+ this.sessionCloning[key] = true;
436
+ try {
437
+ const res = await api('clone-session', {
438
+ source: session.source,
439
+ sessionId: session.sessionId,
440
+ filePath: session.filePath
441
+ });
442
+ if (res.error) {
443
+ this.showMessage(res.error, 'error');
444
+ return;
445
+ }
446
+
447
+ this.showMessage('操作成功', 'success');
448
+ if (typeof this.invalidateSessionsUsageData === 'function') {
449
+ this.invalidateSessionsUsageData({ preserveList: true });
450
+ }
451
+ try {
452
+ await this.loadSessions();
453
+ if (res.sessionId) {
454
+ const matched = this.sessionsList.find(item => item.source === 'codex' && item.sessionId === res.sessionId);
455
+ if (matched) {
456
+ await this.selectSession(matched);
457
+ }
458
+ }
459
+ } catch (_) {
460
+ // The clone already succeeded remotely; keep the success result.
461
+ }
462
+ } catch (_) {
463
+ this.showMessage('克隆失败', 'error');
464
+ } finally {
465
+ this.sessionCloning[key] = false;
466
+ }
467
+ },
468
+
469
+ async deleteSession(session) {
470
+ if (!this.isDeleteAvailable(session)) {
471
+ this.showMessage('不支持此操作', 'error');
472
+ return;
473
+ }
474
+ const useTrash = this.sessionTrashEnabled !== false;
475
+ if (!useTrash && typeof this.requestConfirmDialog === 'function') {
476
+ const confirmed = await this.requestConfirmDialog({
477
+ title: '直接删除会话',
478
+ message: '关闭回收站后,删除会话将直接永久删除,且无法恢复。',
479
+ confirmText: '直接删除',
480
+ cancelText: '取消',
481
+ danger: true
482
+ });
483
+ if (!confirmed) {
484
+ return;
485
+ }
486
+ }
487
+ const key = this.getSessionExportKey(session);
488
+ if (this.sessionDeleting[key]) {
489
+ return;
490
+ }
491
+ this.sessionDeleting[key] = true;
492
+ try {
493
+ const action = useTrash ? 'trash-session' : 'delete-session';
494
+ const res = await api(action, {
495
+ source: session.source,
496
+ sessionId: session.sessionId,
497
+ filePath: session.filePath
498
+ });
499
+ if (!res || res.error) {
500
+ this.showMessage((res && res.error) || '删除失败', 'error');
501
+ return;
502
+ }
503
+ this.removeSessionPin(session);
504
+ if (useTrash) {
505
+ this.invalidateSessionTrashRequests();
506
+ this.showMessage('已移入回收站', 'success');
507
+ if (this.sessionTrashLoadedOnce) {
508
+ this.prependSessionTrashItem(this.buildSessionTrashItemFromSession(session, res), {
509
+ totalCount: res && res.totalCount !== undefined ? res.totalCount : undefined
510
+ });
511
+ } else {
512
+ this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(
513
+ res && res.totalCount !== undefined
514
+ ? res.totalCount
515
+ : (this.normalizeSessionTrashTotalCount(this.sessionTrashTotalCount, this.sessionTrashItems) + 1),
516
+ this.sessionTrashItems
517
+ );
518
+ }
519
+ } else {
520
+ this.showMessage('已删除', 'success');
521
+ }
522
+ if (typeof this.invalidateSessionsUsageData === 'function') {
523
+ this.invalidateSessionsUsageData({ preserveList: true });
524
+ }
525
+ try {
526
+ await this.removeSessionFromCurrentList(session);
527
+ } catch (_) {
528
+ // The delete already succeeded remotely; keep the success result.
529
+ }
530
+ } catch (_) {
531
+ this.showMessage('删除失败', 'error');
532
+ } finally {
533
+ this.sessionDeleting[key] = false;
534
+ }
535
+ }
536
+ };
537
+ }