codexmate 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.en.md +349 -259
  2. package/README.md +284 -252
  3. package/cli/agents-files.js +162 -0
  4. package/cli/archive-helpers.js +446 -0
  5. package/cli/auth-profiles.js +359 -0
  6. package/cli/builtin-proxy.js +580 -0
  7. package/cli/claude-proxy.js +998 -0
  8. package/cli/config-bootstrap.js +384 -0
  9. package/cli/config-health.js +338 -338
  10. package/cli/openclaw-config.js +629 -0
  11. package/cli/skills.js +1141 -0
  12. package/cli/zip-commands.js +510 -0
  13. package/cli.js +13101 -13497
  14. package/lib/cli-file-utils.js +151 -151
  15. package/lib/cli-models-utils.js +419 -311
  16. package/lib/cli-network-utils.js +164 -164
  17. package/lib/cli-path-utils.js +69 -0
  18. package/lib/cli-session-utils.js +121 -121
  19. package/lib/cli-sessions.js +386 -0
  20. package/lib/cli-utils.js +155 -155
  21. package/lib/download-artifacts.js +77 -0
  22. package/lib/mcp-stdio.js +440 -440
  23. package/lib/task-orchestrator.js +869 -0
  24. package/lib/text-diff.js +303 -303
  25. package/lib/workflow-engine.js +340 -340
  26. package/package.json +74 -70
  27. package/res/json5.min.js +1 -1
  28. package/res/vue.global.prod.js +13 -0
  29. package/web-ui/app.js +530 -397
  30. package/web-ui/index.html +33 -30
  31. package/web-ui/logic.agents-diff.mjs +386 -386
  32. package/web-ui/logic.claude.mjs +168 -108
  33. package/web-ui/logic.mjs +5 -5
  34. package/web-ui/logic.runtime.mjs +124 -124
  35. package/web-ui/logic.sessions.mjs +581 -263
  36. package/web-ui/modules/api.mjs +90 -69
  37. package/web-ui/modules/app.computed.dashboard.mjs +113 -113
  38. package/web-ui/modules/app.computed.index.mjs +15 -13
  39. package/web-ui/modules/app.computed.main-tabs.mjs +195 -0
  40. package/web-ui/modules/app.computed.session.mjs +507 -141
  41. package/web-ui/modules/app.constants.mjs +15 -15
  42. package/web-ui/modules/app.methods.agents.mjs +493 -493
  43. package/web-ui/modules/app.methods.claude-config.mjs +174 -174
  44. package/web-ui/modules/app.methods.codex-config.mjs +640 -640
  45. package/web-ui/modules/app.methods.index.mjs +88 -86
  46. package/web-ui/modules/app.methods.install.mjs +149 -157
  47. package/web-ui/modules/app.methods.navigation.mjs +619 -478
  48. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -514
  49. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -337
  50. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -251
  51. package/web-ui/modules/app.methods.providers.mjs +363 -265
  52. package/web-ui/modules/app.methods.runtime.mjs +323 -323
  53. package/web-ui/modules/app.methods.session-actions.mjs +520 -457
  54. package/web-ui/modules/app.methods.session-browser.mjs +626 -435
  55. package/web-ui/modules/app.methods.session-timeline.mjs +448 -441
  56. package/web-ui/modules/app.methods.session-trash.mjs +422 -419
  57. package/web-ui/modules/app.methods.startup-claude.mjs +412 -406
  58. package/web-ui/modules/app.methods.task-orchestration.mjs +471 -0
  59. package/web-ui/modules/config-mode.computed.mjs +126 -124
  60. package/web-ui/modules/skills.computed.mjs +107 -107
  61. package/web-ui/modules/skills.methods.mjs +481 -481
  62. package/web-ui/partials/index/layout-footer.html +13 -69
  63. package/web-ui/partials/index/layout-header.html +402 -337
  64. package/web-ui/partials/index/modal-config-template-agents.html +125 -125
  65. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  66. package/web-ui/partials/index/modal-health-check.html +72 -72
  67. package/web-ui/partials/index/modal-openclaw-config.html +280 -275
  68. package/web-ui/partials/index/modal-skills.html +184 -184
  69. package/web-ui/partials/index/modals-basic.html +156 -196
  70. package/web-ui/partials/index/panel-config-claude.html +126 -100
  71. package/web-ui/partials/index/panel-config-codex.html +237 -237
  72. package/web-ui/partials/index/panel-config-openclaw.html +78 -84
  73. package/web-ui/partials/index/panel-docs.html +130 -0
  74. package/web-ui/partials/index/panel-market.html +174 -174
  75. package/web-ui/partials/index/panel-orchestration.html +397 -0
  76. package/web-ui/partials/index/panel-sessions.html +292 -387
  77. package/web-ui/partials/index/panel-settings.html +190 -166
  78. package/web-ui/partials/index/panel-usage.html +213 -0
  79. package/web-ui/session-helpers.mjs +559 -362
  80. package/web-ui/source-bundle.cjs +233 -233
  81. package/web-ui/styles/base-theme.css +271 -373
  82. package/web-ui/styles/controls-forms.css +360 -354
  83. package/web-ui/styles/docs-panel.css +182 -0
  84. package/web-ui/styles/feedback.css +108 -108
  85. package/web-ui/styles/health-check-dialog.css +144 -144
  86. package/web-ui/styles/layout-shell.css +376 -330
  87. package/web-ui/styles/modals-core.css +464 -449
  88. package/web-ui/styles/navigation-panels.css +348 -381
  89. package/web-ui/styles/openclaw-structured.css +266 -266
  90. package/web-ui/styles/responsive.css +450 -416
  91. package/web-ui/styles/sessions-list.css +400 -414
  92. package/web-ui/styles/sessions-preview.css +411 -405
  93. package/web-ui/styles/sessions-toolbar-trash.css +243 -243
  94. package/web-ui/styles/sessions-usage.css +628 -276
  95. package/web-ui/styles/skills-list.css +296 -298
  96. package/web-ui/styles/skills-market.css +335 -335
  97. package/web-ui/styles/task-orchestration.css +776 -0
  98. package/web-ui/styles/titles-cards.css +408 -407
  99. package/web-ui/styles.css +18 -16
  100. package/web-ui.html +17 -17
  101. package/res/screenshot.png +0 -0
  102. package/res/vue.global.js +0 -18552
@@ -1,457 +1,520 @@
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
- fallbackCopyText(text) {
157
- let textarea = null;
158
- try {
159
- textarea = document.createElement('textarea');
160
- textarea.value = text;
161
- textarea.setAttribute('readonly', '');
162
- textarea.style.position = 'fixed';
163
- textarea.style.top = '-9999px';
164
- textarea.style.left = '-9999px';
165
- textarea.style.opacity = '0';
166
- document.body.appendChild(textarea);
167
- textarea.select();
168
- textarea.setSelectionRange(0, textarea.value.length);
169
- return document.execCommand('copy');
170
- } catch (_) {
171
- return false;
172
- } finally {
173
- if (textarea && textarea.parentNode) {
174
- textarea.parentNode.removeChild(textarea);
175
- }
176
- }
177
- },
178
-
179
- copyAgentsContent() {
180
- const text = typeof this.agentsContent === 'string' ? this.agentsContent : '';
181
- if (!text) {
182
- this.showMessage('没有可复制内容', 'info');
183
- return;
184
- }
185
- const ok = this.fallbackCopyText(text);
186
- if (ok) {
187
- this.showMessage('已复制', 'success');
188
- return;
189
- }
190
- this.showMessage('复制失败', 'error');
191
- },
192
-
193
- exportAgentsContent() {
194
- const text = typeof this.agentsContent === 'string' ? this.agentsContent : '';
195
- if (!text) {
196
- this.showMessage('没有可导出内容', 'info');
197
- return;
198
- }
199
- const now = new Date();
200
- const year = String(now.getFullYear());
201
- const month = String(now.getMonth() + 1).padStart(2, '0');
202
- const day = String(now.getDate()).padStart(2, '0');
203
- const hour = String(now.getHours()).padStart(2, '0');
204
- const minute = String(now.getMinutes()).padStart(2, '0');
205
- const second = String(now.getSeconds()).padStart(2, '0');
206
- const fileName = `agent-${year}${month}${day}-${hour}${minute}${second}.txt`;
207
- this.downloadTextFile(fileName, text, 'text/plain;charset=utf-8');
208
- this.showMessage(`已导出 ${fileName}`, 'success');
209
- },
210
-
211
- async copyInstallCommand(cmd) {
212
- const text = typeof cmd === 'string' ? cmd.trim() : '';
213
- if (!text) {
214
- this.showMessage('没有可复制内容', 'info');
215
- return;
216
- }
217
- try {
218
- if (navigator.clipboard && window.isSecureContext) {
219
- await navigator.clipboard.writeText(text);
220
- this.showMessage('已复制命令', 'success');
221
- return;
222
- }
223
- } catch (_) {}
224
- const ok = this.fallbackCopyText(text);
225
- if (ok) {
226
- this.showMessage('已复制命令', 'success');
227
- return;
228
- }
229
- this.showMessage('复制失败', 'error');
230
- },
231
-
232
- async copyResumeCommand(session) {
233
- if (!this.isResumeCommandAvailable(session)) {
234
- this.showMessage('不支持此操作', 'error');
235
- return;
236
- }
237
- const command = this.buildResumeCommand(session);
238
- const ok = this.fallbackCopyText(command);
239
- if (ok) {
240
- this.showMessage('已复制', 'success');
241
- return;
242
- }
243
- try {
244
- if (navigator.clipboard && window.isSecureContext) {
245
- await navigator.clipboard.writeText(command);
246
- this.showMessage('已复制', 'success');
247
- return;
248
- }
249
- } catch (_) {}
250
- this.showMessage('复制失败', 'error');
251
- },
252
-
253
- buildProviderShareCommand(payload) {
254
- if (!payload || typeof payload !== 'object') return '';
255
- const name = typeof payload.name === 'string' ? payload.name.trim() : '';
256
- const baseUrl = typeof payload.baseUrl === 'string' ? payload.baseUrl.trim() : '';
257
- const apiKey = typeof payload.apiKey === 'string' ? payload.apiKey : '';
258
- const model = typeof payload.model === 'string' ? payload.model.trim() : '';
259
- if (!name || !baseUrl) return '';
260
-
261
- const nameArg = this.quoteShellArg(name);
262
- const urlArg = this.quoteShellArg(baseUrl);
263
- const keyArg = apiKey ? this.quoteShellArg(apiKey) : '';
264
- const switchCmd = `codexmate switch ${nameArg}`;
265
- const addCmd = apiKey
266
- ? `codexmate add ${nameArg} ${urlArg} ${keyArg}`
267
- : `codexmate add ${nameArg} ${urlArg}`;
268
- const modelCmd = model ? ` && codexmate use ${this.quoteShellArg(model)}` : '';
269
- return `${addCmd} && ${switchCmd}${modelCmd}`;
270
- },
271
-
272
- buildClaudeShareCommand(payload) {
273
- if (!payload || typeof payload !== 'object') return '';
274
- const baseUrl = typeof payload.baseUrl === 'string' ? payload.baseUrl.trim() : '';
275
- const apiKey = typeof payload.apiKey === 'string' ? payload.apiKey : '';
276
- const model = typeof payload.model === 'string' && payload.model.trim()
277
- ? payload.model.trim()
278
- : 'glm-4.7';
279
- if (!baseUrl || !apiKey) return '';
280
- const urlArg = this.quoteShellArg(baseUrl);
281
- const keyArg = this.quoteShellArg(apiKey);
282
- const modelArg = this.quoteShellArg(model);
283
- return `codexmate claude ${urlArg} ${keyArg} ${modelArg}`;
284
- },
285
-
286
- async copyProviderShareCommand(provider) {
287
- const name = provider && typeof provider.name === 'string' ? provider.name.trim() : '';
288
- if (!name) {
289
- this.showMessage('参数无效', 'error');
290
- return;
291
- }
292
- if (!this.shouldAllowProviderShare(provider)) {
293
- this.showMessage('本地入口不可分享', 'info');
294
- return;
295
- }
296
- if (this.providerShareLoading[name]) {
297
- return;
298
- }
299
- this.providerShareLoading[name] = true;
300
- try {
301
- const res = await api('export-provider', { name });
302
- if (res && res.error) {
303
- this.showMessage(res.error, 'error');
304
- return;
305
- }
306
- const command = this.buildProviderShareCommand(res && res.payload ? res.payload : null);
307
- if (!command) {
308
- this.showMessage('生成命令失败', 'error');
309
- return;
310
- }
311
- const ok = this.fallbackCopyText(command);
312
- if (ok) {
313
- this.showMessage('已复制', 'success');
314
- return;
315
- }
316
- try {
317
- if (navigator.clipboard && window.isSecureContext) {
318
- await navigator.clipboard.writeText(command);
319
- this.showMessage('已复制', 'success');
320
- return;
321
- }
322
- } catch (_) {}
323
- this.showMessage('复制失败', 'error');
324
- } catch (_) {
325
- this.showMessage('生成命令失败', 'error');
326
- } finally {
327
- this.providerShareLoading[name] = false;
328
- }
329
- },
330
-
331
- async copyClaudeShareCommand(name) {
332
- const config = this.claudeConfigs[name];
333
- if (!config) {
334
- this.showMessage('配置不存在', 'error');
335
- return;
336
- }
337
- if (this.claudeShareLoading[name]) return;
338
- this.claudeShareLoading[name] = true;
339
- try {
340
- const res = await api('export-claude-share', { config });
341
- if (res && res.error) {
342
- this.showMessage(res.error, 'error');
343
- return;
344
- }
345
- const command = this.buildClaudeShareCommand(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.claudeShareLoading[name] = false;
367
- }
368
- },
369
-
370
- async cloneSession(session) {
371
- if (!this.isCloneAvailable(session)) {
372
- this.showMessage('不支持此操作', 'error');
373
- return;
374
- }
375
- const key = this.getSessionExportKey(session);
376
- if (this.sessionCloning[key]) {
377
- return;
378
- }
379
- this.sessionCloning[key] = true;
380
- try {
381
- const res = await api('clone-session', {
382
- source: session.source,
383
- sessionId: session.sessionId,
384
- filePath: session.filePath
385
- });
386
- if (res.error) {
387
- this.showMessage(res.error, 'error');
388
- return;
389
- }
390
-
391
- this.showMessage('操作成功', 'success');
392
- try {
393
- await this.loadSessions();
394
- if (res.sessionId) {
395
- const matched = this.sessionsList.find(item => item.source === 'codex' && item.sessionId === res.sessionId);
396
- if (matched) {
397
- await this.selectSession(matched);
398
- }
399
- }
400
- } catch (_) {
401
- // The clone already succeeded remotely; keep the success result.
402
- }
403
- } catch (_) {
404
- this.showMessage('克隆失败', 'error');
405
- } finally {
406
- this.sessionCloning[key] = false;
407
- }
408
- },
409
-
410
- async deleteSession(session) {
411
- if (!this.isDeleteAvailable(session)) {
412
- this.showMessage('不支持此操作', 'error');
413
- return;
414
- }
415
- const key = this.getSessionExportKey(session);
416
- if (this.sessionDeleting[key]) {
417
- return;
418
- }
419
- this.sessionDeleting[key] = true;
420
- try {
421
- const res = await api('trash-session', {
422
- source: session.source,
423
- sessionId: session.sessionId,
424
- filePath: session.filePath
425
- });
426
- if (!res || res.error) {
427
- this.showMessage((res && res.error) || '删除失败', 'error');
428
- return;
429
- }
430
- this.removeSessionPin(session);
431
- this.invalidateSessionTrashRequests();
432
- this.showMessage('已移入回收站', 'success');
433
- if (this.sessionTrashLoadedOnce) {
434
- this.prependSessionTrashItem(this.buildSessionTrashItemFromSession(session, res), {
435
- totalCount: res && res.totalCount !== undefined ? res.totalCount : undefined
436
- });
437
- } else {
438
- this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(
439
- res && res.totalCount !== undefined
440
- ? res.totalCount
441
- : (this.normalizeSessionTrashTotalCount(this.sessionTrashTotalCount, this.sessionTrashItems) + 1),
442
- this.sessionTrashItems
443
- );
444
- }
445
- try {
446
- await this.removeSessionFromCurrentList(session);
447
- } catch (_) {
448
- // The delete already succeeded remotely; keep the success result.
449
- }
450
- } catch (_) {
451
- this.showMessage('删除失败', 'error');
452
- } finally {
453
- this.sessionDeleting[key] = false;
454
- }
455
- }
456
- };
457
- }
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
+ }