codexmate 0.0.21 → 0.0.22

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 (114) hide show
  1. package/README.md +389 -284
  2. package/README.zh.md +321 -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 +575 -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 +198 -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 +161 -149
  52. package/web-ui/modules/app.methods.navigation.mjs +619 -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 +626 -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 +405 -412
  63. package/web-ui/modules/app.methods.task-orchestration.mjs +471 -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 +1609 -0
  67. package/web-ui/modules/plugins.computed.mjs +220 -0
  68. package/web-ui/modules/plugins.methods.mjs +620 -0
  69. package/web-ui/modules/plugins.storage.mjs +37 -0
  70. package/web-ui/modules/skills.computed.mjs +107 -107
  71. package/web-ui/modules/skills.methods.mjs +481 -481
  72. package/web-ui/partials/index/layout-footer.html +13 -13
  73. package/web-ui/partials/index/layout-header.html +461 -402
  74. package/web-ui/partials/index/modal-config-template-agents.html +175 -125
  75. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  76. package/web-ui/partials/index/modal-health-check.html +72 -72
  77. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  78. package/web-ui/partials/index/modal-skills.html +200 -184
  79. package/web-ui/partials/index/modals-basic.html +165 -156
  80. package/web-ui/partials/index/panel-config-claude.html +138 -126
  81. package/web-ui/partials/index/panel-config-codex.html +234 -237
  82. package/web-ui/partials/index/panel-config-openclaw.html +78 -78
  83. package/web-ui/partials/index/panel-docs.html +147 -130
  84. package/web-ui/partials/index/panel-market.html +174 -174
  85. package/web-ui/partials/index/panel-orchestration.html +397 -397
  86. package/web-ui/partials/index/panel-plugins.html +243 -0
  87. package/web-ui/partials/index/panel-sessions.html +292 -292
  88. package/web-ui/partials/index/panel-settings.html +258 -190
  89. package/web-ui/partials/index/panel-usage.html +353 -213
  90. package/web-ui/session-helpers.mjs +573 -559
  91. package/web-ui/source-bundle.cjs +233 -233
  92. package/web-ui/styles/base-theme.css +264 -271
  93. package/web-ui/styles/controls-forms.css +362 -360
  94. package/web-ui/styles/docs-panel.css +247 -182
  95. package/web-ui/styles/feedback.css +108 -108
  96. package/web-ui/styles/health-check-dialog.css +144 -144
  97. package/web-ui/styles/layout-shell.css +596 -376
  98. package/web-ui/styles/modals-core.css +464 -464
  99. package/web-ui/styles/navigation-panels.css +382 -348
  100. package/web-ui/styles/openclaw-structured.css +266 -266
  101. package/web-ui/styles/plugins-panel.css +518 -0
  102. package/web-ui/styles/responsive.css +456 -450
  103. package/web-ui/styles/sessions-list.css +400 -400
  104. package/web-ui/styles/sessions-preview.css +411 -411
  105. package/web-ui/styles/sessions-toolbar-trash.css +268 -243
  106. package/web-ui/styles/sessions-usage.css +851 -628
  107. package/web-ui/styles/settings-panel.css +166 -0
  108. package/web-ui/styles/skills-list.css +303 -296
  109. package/web-ui/styles/skills-market.css +396 -335
  110. package/web-ui/styles/task-orchestration.css +776 -776
  111. package/web-ui/styles/titles-cards.css +408 -408
  112. package/web-ui/styles.css +20 -18
  113. package/web-ui.html +17 -17
  114. package/README.en.md +0 -349
@@ -1,422 +1,422 @@
1
- export function createSessionTrashMethods(options = {}) {
2
- const {
3
- api,
4
- sessionTrashListLimit = 500,
5
- sessionTrashPageSize = 200
6
- } = options;
7
-
8
- return {
9
- buildSessionTrashItemFromSession(session, result = {}) {
10
- const deletedAt = typeof result.deletedAt === 'string' && result.deletedAt
11
- ? result.deletedAt
12
- : new Date().toISOString();
13
- const source = session && session.source === 'claude' ? 'claude' : 'codex';
14
- return {
15
- trashId: typeof result.trashId === 'string' ? result.trashId : '',
16
- source,
17
- sourceLabel: session && typeof session.sourceLabel === 'string' && session.sourceLabel
18
- ? session.sourceLabel
19
- : (source === 'claude' ? 'Claude Code' : 'Codex'),
20
- sessionId: session && typeof session.sessionId === 'string' ? session.sessionId : '',
21
- title: session && typeof session.title === 'string' && session.title
22
- ? session.title
23
- : (session && typeof session.sessionId === 'string' ? session.sessionId : ''),
24
- cwd: session && typeof session.cwd === 'string' ? session.cwd : '',
25
- createdAt: session && typeof session.createdAt === 'string' ? session.createdAt : '',
26
- updatedAt: session && typeof session.updatedAt === 'string' ? session.updatedAt : '',
27
- deletedAt,
28
- messageCount: Number.isFinite(Number(result && result.messageCount))
29
- ? Math.max(0, Math.floor(Number(result.messageCount)))
30
- : (Number.isFinite(Number(session && session.messageCount))
31
- ? Math.max(0, Math.floor(Number(session.messageCount)))
32
- : 0),
33
- originalFilePath: session && typeof session.filePath === 'string' ? session.filePath : '',
34
- provider: session && typeof session.provider === 'string' ? session.provider : source,
35
- keywords: Array.isArray(session && session.keywords) ? session.keywords : [],
36
- capabilities: session && typeof session.capabilities === 'object' && session.capabilities
37
- ? session.capabilities
38
- : {},
39
- claudeIndexPath: '',
40
- claudeIndexEntry: null,
41
- trashFilePath: ''
42
- };
43
- },
44
-
45
- prependSessionTrashItem(item, options = {}) {
46
- if (!item || !item.trashId) {
47
- return;
48
- }
49
- const existing = Array.isArray(this.sessionTrashItems) ? this.sessionTrashItems : [];
50
- const filtered = existing.filter((entry) => this.getSessionTrashActionKey(entry) !== item.trashId);
51
- const nextItems = [item, ...filtered].slice(0, sessionTrashListLimit);
52
- const previousTotalCount = Number(this.sessionTrashTotalCount);
53
- const normalizedPreviousTotal = Number.isFinite(previousTotalCount) && previousTotalCount >= 0
54
- ? Math.max(existing.length, Math.floor(previousTotalCount))
55
- : existing.length;
56
- this.sessionTrashItems = nextItems;
57
- const previousVisibleCount = Number(this.sessionTrashVisibleCount);
58
- const normalizedPreviousVisibleCount = Number.isFinite(previousVisibleCount) && previousVisibleCount > 0
59
- ? Math.floor(previousVisibleCount)
60
- : sessionTrashPageSize;
61
- const wasFullyExpanded = normalizedPreviousVisibleCount >= existing.length
62
- || normalizedPreviousVisibleCount >= normalizedPreviousTotal;
63
- if (wasFullyExpanded) {
64
- this.sessionTrashVisibleCount = Math.min(
65
- normalizedPreviousVisibleCount + 1,
66
- nextItems.length || (normalizedPreviousVisibleCount + 1)
67
- );
68
- }
69
- const fallbackTotalCount = filtered.length === existing.length
70
- ? normalizedPreviousTotal + 1
71
- : normalizedPreviousTotal;
72
- this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(
73
- options && options.totalCount !== undefined
74
- ? options.totalCount
75
- : fallbackTotalCount,
76
- nextItems
77
- );
78
- },
79
-
80
- normalizeSessionTrashTotalCount(totalCount, fallbackItems = this.sessionTrashItems) {
81
- const fallbackCount = Array.isArray(fallbackItems) ? fallbackItems.length : 0;
82
- const numericTotal = Number(totalCount);
83
- if (!Number.isFinite(numericTotal) || numericTotal < 0) {
84
- return fallbackCount;
85
- }
86
- return Math.floor(numericTotal);
87
- },
88
-
89
- getSessionTrashViewState() {
90
- if (this.sessionTrashLoading && !this.sessionTrashLoadedOnce) {
91
- return 'loading';
92
- }
93
- const totalCount = Number(this.sessionTrashCount);
94
- const normalizedTotalCount = Number.isFinite(totalCount) && totalCount >= 0
95
- ? Math.floor(totalCount)
96
- : 0;
97
- const hasVisibleItems = Array.isArray(this.sessionTrashItems) && this.sessionTrashItems.length > 0;
98
- if (this.sessionTrashLastLoadFailed && (!this.sessionTrashLoadedOnce || !hasVisibleItems)) {
99
- return 'retry';
100
- }
101
- if (!this.sessionTrashLoadedOnce) {
102
- return normalizedTotalCount > 0 ? 'retry' : 'empty';
103
- }
104
- if (normalizedTotalCount === 0) {
105
- return 'empty';
106
- }
107
- return hasVisibleItems ? 'list' : 'retry';
108
- },
109
-
110
- issueSessionTrashCountRequestToken() {
111
- const currentToken = Number(this.sessionTrashCountRequestToken);
112
- const nextToken = Number.isFinite(currentToken) && currentToken >= 0
113
- ? Math.floor(currentToken) + 1
114
- : 1;
115
- this.sessionTrashCountRequestToken = nextToken;
116
- return nextToken;
117
- },
118
-
119
- issueSessionTrashListRequestToken() {
120
- const currentToken = Number(this.sessionTrashListRequestToken);
121
- const nextToken = Number.isFinite(currentToken) && currentToken >= 0
122
- ? Math.floor(currentToken) + 1
123
- : 1;
124
- this.sessionTrashListRequestToken = nextToken;
125
- return nextToken;
126
- },
127
-
128
- invalidateSessionTrashRequests() {
129
- this.issueSessionTrashCountRequestToken();
130
- return this.issueSessionTrashListRequestToken();
131
- },
132
-
133
- isLatestSessionTrashCountRequestToken(token) {
134
- return Number(token) === Number(this.sessionTrashCountRequestToken);
135
- },
136
-
137
- isLatestSessionTrashListRequestToken(token) {
138
- return Number(token) === Number(this.sessionTrashListRequestToken);
139
- },
140
-
141
- resetSessionTrashVisibleCount() {
142
- const totalItems = Array.isArray(this.sessionTrashItems) ? this.sessionTrashItems.length : 0;
143
- this.sessionTrashVisibleCount = Math.min(totalItems, sessionTrashPageSize) || sessionTrashPageSize;
144
- },
145
-
146
- loadMoreSessionTrashItems() {
147
- const totalItems = Array.isArray(this.sessionTrashItems) ? this.sessionTrashItems.length : 0;
148
- const visibleCount = Number(this.sessionTrashVisibleCount);
149
- const safeVisibleCount = Number.isFinite(visibleCount) && visibleCount > 0
150
- ? Math.floor(visibleCount)
151
- : sessionTrashPageSize;
152
- this.sessionTrashVisibleCount = Math.min(totalItems, safeVisibleCount + sessionTrashPageSize);
153
- },
154
-
155
- clearActiveSessionState() {
156
- this.activeSession = null;
157
- this.activeSessionMessages = [];
158
- this.resetSessionDetailPagination();
159
- this.resetSessionPreviewMessageRender();
160
- this.activeSessionDetailError = '';
161
- this.activeSessionDetailClipped = false;
162
- this.cancelSessionTimelineSync();
163
- this.sessionTimelineActiveKey = '';
164
- this.clearSessionTimelineRefs();
165
- },
166
-
167
- async removeSessionFromCurrentList(session) {
168
- const sessionKey = this.getSessionExportKey(session);
169
- if (!sessionKey) {
170
- return;
171
- }
172
- const currentList = Array.isArray(this.sessionsList) ? [...this.sessionsList] : [];
173
- const removedIndex = currentList.findIndex((item) => this.getSessionExportKey(item) === sessionKey);
174
- if (removedIndex < 0) {
175
- return;
176
- }
177
- const activeKey = this.activeSession ? this.getSessionExportKey(this.activeSession) : '';
178
- const renderedList = Array.isArray(this.sortedSessionsList) ? this.sortedSessionsList : [];
179
- const renderedIndex = renderedList.findIndex((item) => this.getSessionExportKey(item) === sessionKey);
180
- let nextActiveKey = '';
181
- if (activeKey === sessionKey && renderedIndex >= 0) {
182
- const fallbackSession = renderedList[renderedIndex - 1] || renderedList[renderedIndex + 1] || null;
183
- nextActiveKey = fallbackSession ? this.getSessionExportKey(fallbackSession) : '';
184
- }
185
- currentList.splice(removedIndex, 1);
186
- this.sessionsList = currentList;
187
- this.syncSessionPathOptionsForSource(
188
- this.sessionFilterSource,
189
- this.extractPathOptionsFromSessions(currentList),
190
- false
191
- );
192
- if (activeKey !== sessionKey) {
193
- return;
194
- }
195
- if (currentList.length === 0) {
196
- this.clearActiveSessionState();
197
- return;
198
- }
199
- const nextSession = currentList.find((item) => this.getSessionExportKey(item) === nextActiveKey)
200
- || currentList[Math.min(removedIndex, currentList.length - 1)];
201
- if (!nextSession) {
202
- this.clearActiveSessionState();
203
- return;
204
- }
205
- await this.selectSession(nextSession);
206
- },
207
-
208
- normalizeSettingsTab(tab) {
209
- if (tab === 'trash' || tab === 'device') {
210
- return tab;
211
- }
212
- return 'backup';
213
- },
214
-
215
- async onSettingsTabClick(tab) {
216
- await this.switchSettingsTab(tab);
217
- },
218
-
219
- async switchSettingsTab(tab, options = {}) {
220
- const nextTab = this.normalizeSettingsTab(tab);
221
- this.settingsTab = nextTab;
222
- if (nextTab !== 'trash') {
223
- return;
224
- }
225
- const forceRefresh = options.forceRefresh === true;
226
- if (forceRefresh || !this.sessionTrashLoadedOnce) {
227
- await this.loadSessionTrash({ forceRefresh });
228
- }
229
- },
230
-
231
- async loadSessionTrashCount(options = {}) {
232
- if (this.sessionTrashCountLoading) {
233
- this.sessionTrashCountPendingOptions = {
234
- ...(this.sessionTrashCountPendingOptions || {}),
235
- ...(options || {})
236
- };
237
- return;
238
- }
239
- const requestToken = this.issueSessionTrashCountRequestToken();
240
- this.sessionTrashCountLoading = true;
241
- try {
242
- const res = await api('list-session-trash', { countOnly: true });
243
- if (!this.isLatestSessionTrashCountRequestToken(requestToken)) {
244
- return;
245
- }
246
- if (res.error) {
247
- if (options.silent !== true) {
248
- this.showMessage(res.error, 'error');
249
- }
250
- return;
251
- }
252
- this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(
253
- res.totalCount,
254
- this.sessionTrashItems
255
- );
256
- this.sessionTrashCountLoadedOnce = true;
257
- } catch (e) {
258
- if (this.isLatestSessionTrashCountRequestToken(requestToken) && options.silent !== true) {
259
- this.showMessage('加载回收站数量失败', 'error');
260
- }
261
- } finally {
262
- this.sessionTrashCountLoading = false;
263
- const pendingOptions = this.sessionTrashCountPendingOptions;
264
- this.sessionTrashCountPendingOptions = null;
265
- if (pendingOptions) {
266
- await this.loadSessionTrashCount(pendingOptions);
267
- }
268
- }
269
- },
270
-
271
- getSessionTrashActionKey(item) {
272
- return item && typeof item.trashId === 'string' ? item.trashId : '';
273
- },
274
-
275
- isSessionTrashActionBusy(item) {
276
- const key = typeof item === 'string' ? item : this.getSessionTrashActionKey(item);
277
- return !!(key && (this.sessionTrashRestoring[key] || this.sessionTrashPurging[key]));
278
- },
279
-
280
- async loadSessionTrash(options = {}) {
281
- if (this.sessionTrashLoading) {
282
- this.sessionTrashPendingOptions = {
283
- ...(this.sessionTrashPendingOptions || {}),
284
- ...(options || {})
285
- };
286
- return;
287
- }
288
- const requestToken = this.issueSessionTrashListRequestToken();
289
- this.sessionTrashLoading = true;
290
- this.sessionTrashLastLoadFailed = false;
291
- let loadSucceeded = false;
292
- try {
293
- const res = await api('list-session-trash', {
294
- limit: sessionTrashListLimit,
295
- forceRefresh: !!options.forceRefresh
296
- });
297
- if (!this.isLatestSessionTrashListRequestToken(requestToken)) {
298
- return;
299
- }
300
- if (res.error) {
301
- this.sessionTrashLastLoadFailed = true;
302
- this.showMessage(res.error, 'error');
303
- return;
304
- }
305
- const nextItems = Array.isArray(res.items) ? res.items : [];
306
- this.sessionTrashItems = nextItems;
307
- this.resetSessionTrashVisibleCount();
308
- this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(res.totalCount, nextItems);
309
- this.sessionTrashCountLoadedOnce = true;
310
- this.sessionTrashLastLoadFailed = false;
311
- loadSucceeded = true;
312
- } catch (e) {
313
- if (this.isLatestSessionTrashListRequestToken(requestToken)) {
314
- this.sessionTrashLastLoadFailed = true;
315
- this.showMessage('加载回收站失败', 'error');
316
- }
317
- } finally {
318
- this.sessionTrashLoading = false;
319
- if (loadSucceeded) {
320
- this.sessionTrashLoadedOnce = true;
321
- }
322
- const pendingOptions = this.sessionTrashPendingOptions;
323
- this.sessionTrashPendingOptions = null;
324
- if (pendingOptions) {
325
- await this.loadSessionTrash(pendingOptions);
326
- }
327
- }
328
- },
329
-
330
- async restoreSessionTrash(item) {
331
- const key = this.getSessionTrashActionKey(item);
332
- if (!key || this.isSessionTrashActionBusy(key) || this.sessionTrashClearing) {
333
- return;
334
- }
335
- this.sessionTrashRestoring[key] = true;
336
- try {
337
- const res = await api('restore-session-trash', { trashId: key });
338
- if (res.error) {
339
- this.showMessage(res.error, 'error');
340
- return;
341
- }
342
- this.showMessage('会话已恢复', 'success');
343
- if (typeof this.invalidateSessionsUsageData === 'function') {
344
- this.invalidateSessionsUsageData({ preserveList: true });
345
- }
346
- this.invalidateSessionTrashRequests();
347
- await this.loadSessionTrash({ forceRefresh: true });
348
- if (this.sessionsLoadedOnce) {
349
- await this.loadSessions();
350
- }
351
- } catch (e) {
352
- this.showMessage('恢复失败', 'error');
353
- } finally {
354
- this.sessionTrashRestoring[key] = false;
355
- }
356
- },
357
-
358
- async purgeSessionTrash(item) {
359
- const key = this.getSessionTrashActionKey(item);
360
- if (!key || this.isSessionTrashActionBusy(key) || this.sessionTrashClearing) {
361
- return;
362
- }
363
- const confirmed = await this.requestConfirmDialog({
364
- title: '彻底删除回收站记录',
365
- message: '该会话将从回收站永久删除,且无法恢复。',
366
- confirmText: '彻底删除',
367
- cancelText: '取消',
368
- danger: true
369
- });
370
- if (!confirmed) {
371
- return;
372
- }
373
- this.sessionTrashPurging[key] = true;
374
- try {
375
- const res = await api('purge-session-trash', { trashId: key });
376
- if (res.error) {
377
- this.showMessage(res.error, 'error');
378
- return;
379
- }
380
- this.showMessage('已彻底删除', 'success');
381
- this.invalidateSessionTrashRequests();
382
- await this.loadSessionTrash({ forceRefresh: true });
383
- } catch (e) {
384
- this.showMessage('彻底删除失败', 'error');
385
- } finally {
386
- this.sessionTrashPurging[key] = false;
387
- }
388
- },
389
-
390
- async clearSessionTrash() {
391
- const normalizedCount = Number(this.sessionTrashCount);
392
- if (this.sessionTrashClearing || !Number.isFinite(normalizedCount) || normalizedCount <= 0) {
393
- return;
394
- }
395
- const confirmed = await this.requestConfirmDialog({
396
- title: '清空回收站',
397
- message: '该操作会永久删除回收站中的全部会话,且无法恢复。',
398
- confirmText: '全部清空',
399
- cancelText: '取消',
400
- danger: true
401
- });
402
- if (!confirmed) {
403
- return;
404
- }
405
- this.sessionTrashClearing = true;
406
- try {
407
- const res = await api('purge-session-trash', { all: true });
408
- if (res.error) {
409
- this.showMessage(res.error, 'error');
410
- return;
411
- }
412
- this.showMessage('回收站已清空', 'success');
413
- this.invalidateSessionTrashRequests();
414
- await this.loadSessionTrash({ forceRefresh: true });
415
- } catch (e) {
416
- this.showMessage('清空回收站失败', 'error');
417
- } finally {
418
- this.sessionTrashClearing = false;
419
- }
420
- }
421
- };
422
- }
1
+ export function createSessionTrashMethods(options = {}) {
2
+ const {
3
+ api,
4
+ sessionTrashListLimit = 500,
5
+ sessionTrashPageSize = 200
6
+ } = options;
7
+
8
+ return {
9
+ buildSessionTrashItemFromSession(session, result = {}) {
10
+ const deletedAt = typeof result.deletedAt === 'string' && result.deletedAt
11
+ ? result.deletedAt
12
+ : new Date().toISOString();
13
+ const source = session && session.source === 'claude' ? 'claude' : 'codex';
14
+ return {
15
+ trashId: typeof result.trashId === 'string' ? result.trashId : '',
16
+ source,
17
+ sourceLabel: session && typeof session.sourceLabel === 'string' && session.sourceLabel
18
+ ? session.sourceLabel
19
+ : (source === 'claude' ? 'Claude Code' : 'Codex'),
20
+ sessionId: session && typeof session.sessionId === 'string' ? session.sessionId : '',
21
+ title: session && typeof session.title === 'string' && session.title
22
+ ? session.title
23
+ : (session && typeof session.sessionId === 'string' ? session.sessionId : ''),
24
+ cwd: session && typeof session.cwd === 'string' ? session.cwd : '',
25
+ createdAt: session && typeof session.createdAt === 'string' ? session.createdAt : '',
26
+ updatedAt: session && typeof session.updatedAt === 'string' ? session.updatedAt : '',
27
+ deletedAt,
28
+ messageCount: Number.isFinite(Number(result && result.messageCount))
29
+ ? Math.max(0, Math.floor(Number(result.messageCount)))
30
+ : (Number.isFinite(Number(session && session.messageCount))
31
+ ? Math.max(0, Math.floor(Number(session.messageCount)))
32
+ : 0),
33
+ originalFilePath: session && typeof session.filePath === 'string' ? session.filePath : '',
34
+ provider: session && typeof session.provider === 'string' ? session.provider : source,
35
+ keywords: Array.isArray(session && session.keywords) ? session.keywords : [],
36
+ capabilities: session && typeof session.capabilities === 'object' && session.capabilities
37
+ ? session.capabilities
38
+ : {},
39
+ claudeIndexPath: '',
40
+ claudeIndexEntry: null,
41
+ trashFilePath: ''
42
+ };
43
+ },
44
+
45
+ prependSessionTrashItem(item, options = {}) {
46
+ if (!item || !item.trashId) {
47
+ return;
48
+ }
49
+ const existing = Array.isArray(this.sessionTrashItems) ? this.sessionTrashItems : [];
50
+ const filtered = existing.filter((entry) => this.getSessionTrashActionKey(entry) !== item.trashId);
51
+ const nextItems = [item, ...filtered].slice(0, sessionTrashListLimit);
52
+ const previousTotalCount = Number(this.sessionTrashTotalCount);
53
+ const normalizedPreviousTotal = Number.isFinite(previousTotalCount) && previousTotalCount >= 0
54
+ ? Math.max(existing.length, Math.floor(previousTotalCount))
55
+ : existing.length;
56
+ this.sessionTrashItems = nextItems;
57
+ const previousVisibleCount = Number(this.sessionTrashVisibleCount);
58
+ const normalizedPreviousVisibleCount = Number.isFinite(previousVisibleCount) && previousVisibleCount > 0
59
+ ? Math.floor(previousVisibleCount)
60
+ : sessionTrashPageSize;
61
+ const wasFullyExpanded = normalizedPreviousVisibleCount >= existing.length
62
+ || normalizedPreviousVisibleCount >= normalizedPreviousTotal;
63
+ if (wasFullyExpanded) {
64
+ this.sessionTrashVisibleCount = Math.min(
65
+ normalizedPreviousVisibleCount + 1,
66
+ nextItems.length || (normalizedPreviousVisibleCount + 1)
67
+ );
68
+ }
69
+ const fallbackTotalCount = filtered.length === existing.length
70
+ ? normalizedPreviousTotal + 1
71
+ : normalizedPreviousTotal;
72
+ this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(
73
+ options && options.totalCount !== undefined
74
+ ? options.totalCount
75
+ : fallbackTotalCount,
76
+ nextItems
77
+ );
78
+ },
79
+
80
+ normalizeSessionTrashTotalCount(totalCount, fallbackItems = this.sessionTrashItems) {
81
+ const fallbackCount = Array.isArray(fallbackItems) ? fallbackItems.length : 0;
82
+ const numericTotal = Number(totalCount);
83
+ if (!Number.isFinite(numericTotal) || numericTotal < 0) {
84
+ return fallbackCount;
85
+ }
86
+ return Math.floor(numericTotal);
87
+ },
88
+
89
+ getSessionTrashViewState() {
90
+ if (this.sessionTrashLoading && !this.sessionTrashLoadedOnce) {
91
+ return 'loading';
92
+ }
93
+ const totalCount = Number(this.sessionTrashCount);
94
+ const normalizedTotalCount = Number.isFinite(totalCount) && totalCount >= 0
95
+ ? Math.floor(totalCount)
96
+ : 0;
97
+ const hasVisibleItems = Array.isArray(this.sessionTrashItems) && this.sessionTrashItems.length > 0;
98
+ if (this.sessionTrashLastLoadFailed && (!this.sessionTrashLoadedOnce || !hasVisibleItems)) {
99
+ return 'retry';
100
+ }
101
+ if (!this.sessionTrashLoadedOnce) {
102
+ return normalizedTotalCount > 0 ? 'retry' : 'empty';
103
+ }
104
+ if (normalizedTotalCount === 0) {
105
+ return 'empty';
106
+ }
107
+ return hasVisibleItems ? 'list' : 'retry';
108
+ },
109
+
110
+ issueSessionTrashCountRequestToken() {
111
+ const currentToken = Number(this.sessionTrashCountRequestToken);
112
+ const nextToken = Number.isFinite(currentToken) && currentToken >= 0
113
+ ? Math.floor(currentToken) + 1
114
+ : 1;
115
+ this.sessionTrashCountRequestToken = nextToken;
116
+ return nextToken;
117
+ },
118
+
119
+ issueSessionTrashListRequestToken() {
120
+ const currentToken = Number(this.sessionTrashListRequestToken);
121
+ const nextToken = Number.isFinite(currentToken) && currentToken >= 0
122
+ ? Math.floor(currentToken) + 1
123
+ : 1;
124
+ this.sessionTrashListRequestToken = nextToken;
125
+ return nextToken;
126
+ },
127
+
128
+ invalidateSessionTrashRequests() {
129
+ this.issueSessionTrashCountRequestToken();
130
+ return this.issueSessionTrashListRequestToken();
131
+ },
132
+
133
+ isLatestSessionTrashCountRequestToken(token) {
134
+ return Number(token) === Number(this.sessionTrashCountRequestToken);
135
+ },
136
+
137
+ isLatestSessionTrashListRequestToken(token) {
138
+ return Number(token) === Number(this.sessionTrashListRequestToken);
139
+ },
140
+
141
+ resetSessionTrashVisibleCount() {
142
+ const totalItems = Array.isArray(this.sessionTrashItems) ? this.sessionTrashItems.length : 0;
143
+ this.sessionTrashVisibleCount = Math.min(totalItems, sessionTrashPageSize) || sessionTrashPageSize;
144
+ },
145
+
146
+ loadMoreSessionTrashItems() {
147
+ const totalItems = Array.isArray(this.sessionTrashItems) ? this.sessionTrashItems.length : 0;
148
+ const visibleCount = Number(this.sessionTrashVisibleCount);
149
+ const safeVisibleCount = Number.isFinite(visibleCount) && visibleCount > 0
150
+ ? Math.floor(visibleCount)
151
+ : sessionTrashPageSize;
152
+ this.sessionTrashVisibleCount = Math.min(totalItems, safeVisibleCount + sessionTrashPageSize);
153
+ },
154
+
155
+ clearActiveSessionState() {
156
+ this.activeSession = null;
157
+ this.activeSessionMessages = [];
158
+ this.resetSessionDetailPagination();
159
+ this.resetSessionPreviewMessageRender();
160
+ this.activeSessionDetailError = '';
161
+ this.activeSessionDetailClipped = false;
162
+ this.cancelSessionTimelineSync();
163
+ this.sessionTimelineActiveKey = '';
164
+ this.clearSessionTimelineRefs();
165
+ },
166
+
167
+ async removeSessionFromCurrentList(session) {
168
+ const sessionKey = this.getSessionExportKey(session);
169
+ if (!sessionKey) {
170
+ return;
171
+ }
172
+ const currentList = Array.isArray(this.sessionsList) ? [...this.sessionsList] : [];
173
+ const removedIndex = currentList.findIndex((item) => this.getSessionExportKey(item) === sessionKey);
174
+ if (removedIndex < 0) {
175
+ return;
176
+ }
177
+ const activeKey = this.activeSession ? this.getSessionExportKey(this.activeSession) : '';
178
+ const renderedList = Array.isArray(this.sortedSessionsList) ? this.sortedSessionsList : [];
179
+ const renderedIndex = renderedList.findIndex((item) => this.getSessionExportKey(item) === sessionKey);
180
+ let nextActiveKey = '';
181
+ if (activeKey === sessionKey && renderedIndex >= 0) {
182
+ const fallbackSession = renderedList[renderedIndex - 1] || renderedList[renderedIndex + 1] || null;
183
+ nextActiveKey = fallbackSession ? this.getSessionExportKey(fallbackSession) : '';
184
+ }
185
+ currentList.splice(removedIndex, 1);
186
+ this.sessionsList = currentList;
187
+ this.syncSessionPathOptionsForSource(
188
+ this.sessionFilterSource,
189
+ this.extractPathOptionsFromSessions(currentList),
190
+ false
191
+ );
192
+ if (activeKey !== sessionKey) {
193
+ return;
194
+ }
195
+ if (currentList.length === 0) {
196
+ this.clearActiveSessionState();
197
+ return;
198
+ }
199
+ const nextSession = currentList.find((item) => this.getSessionExportKey(item) === nextActiveKey)
200
+ || currentList[Math.min(removedIndex, currentList.length - 1)];
201
+ if (!nextSession) {
202
+ this.clearActiveSessionState();
203
+ return;
204
+ }
205
+ await this.selectSession(nextSession);
206
+ },
207
+
208
+ normalizeSettingsTab(tab) {
209
+ if (tab === 'trash' || tab === 'device') {
210
+ return tab;
211
+ }
212
+ return 'backup';
213
+ },
214
+
215
+ async onSettingsTabClick(tab) {
216
+ await this.switchSettingsTab(tab);
217
+ },
218
+
219
+ async switchSettingsTab(tab, options = {}) {
220
+ const nextTab = this.normalizeSettingsTab(tab);
221
+ this.settingsTab = nextTab;
222
+ if (nextTab !== 'trash') {
223
+ return;
224
+ }
225
+ const forceRefresh = options.forceRefresh === true;
226
+ if (forceRefresh || !this.sessionTrashLoadedOnce) {
227
+ await this.loadSessionTrash({ forceRefresh });
228
+ }
229
+ },
230
+
231
+ async loadSessionTrashCount(options = {}) {
232
+ if (this.sessionTrashCountLoading) {
233
+ this.sessionTrashCountPendingOptions = {
234
+ ...(this.sessionTrashCountPendingOptions || {}),
235
+ ...(options || {})
236
+ };
237
+ return;
238
+ }
239
+ const requestToken = this.issueSessionTrashCountRequestToken();
240
+ this.sessionTrashCountLoading = true;
241
+ try {
242
+ const res = await api('list-session-trash', { countOnly: true });
243
+ if (!this.isLatestSessionTrashCountRequestToken(requestToken)) {
244
+ return;
245
+ }
246
+ if (res.error) {
247
+ if (options.silent !== true) {
248
+ this.showMessage(res.error, 'error');
249
+ }
250
+ return;
251
+ }
252
+ this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(
253
+ res.totalCount,
254
+ this.sessionTrashItems
255
+ );
256
+ this.sessionTrashCountLoadedOnce = true;
257
+ } catch (e) {
258
+ if (this.isLatestSessionTrashCountRequestToken(requestToken) && options.silent !== true) {
259
+ this.showMessage('加载回收站数量失败', 'error');
260
+ }
261
+ } finally {
262
+ this.sessionTrashCountLoading = false;
263
+ const pendingOptions = this.sessionTrashCountPendingOptions;
264
+ this.sessionTrashCountPendingOptions = null;
265
+ if (pendingOptions) {
266
+ await this.loadSessionTrashCount(pendingOptions);
267
+ }
268
+ }
269
+ },
270
+
271
+ getSessionTrashActionKey(item) {
272
+ return item && typeof item.trashId === 'string' ? item.trashId : '';
273
+ },
274
+
275
+ isSessionTrashActionBusy(item) {
276
+ const key = typeof item === 'string' ? item : this.getSessionTrashActionKey(item);
277
+ return !!(key && (this.sessionTrashRestoring[key] || this.sessionTrashPurging[key]));
278
+ },
279
+
280
+ async loadSessionTrash(options = {}) {
281
+ if (this.sessionTrashLoading) {
282
+ this.sessionTrashPendingOptions = {
283
+ ...(this.sessionTrashPendingOptions || {}),
284
+ ...(options || {})
285
+ };
286
+ return;
287
+ }
288
+ const requestToken = this.issueSessionTrashListRequestToken();
289
+ this.sessionTrashLoading = true;
290
+ this.sessionTrashLastLoadFailed = false;
291
+ let loadSucceeded = false;
292
+ try {
293
+ const res = await api('list-session-trash', {
294
+ limit: sessionTrashListLimit,
295
+ forceRefresh: !!options.forceRefresh
296
+ });
297
+ if (!this.isLatestSessionTrashListRequestToken(requestToken)) {
298
+ return;
299
+ }
300
+ if (res.error) {
301
+ this.sessionTrashLastLoadFailed = true;
302
+ this.showMessage(res.error, 'error');
303
+ return;
304
+ }
305
+ const nextItems = Array.isArray(res.items) ? res.items : [];
306
+ this.sessionTrashItems = nextItems;
307
+ this.resetSessionTrashVisibleCount();
308
+ this.sessionTrashTotalCount = this.normalizeSessionTrashTotalCount(res.totalCount, nextItems);
309
+ this.sessionTrashCountLoadedOnce = true;
310
+ this.sessionTrashLastLoadFailed = false;
311
+ loadSucceeded = true;
312
+ } catch (e) {
313
+ if (this.isLatestSessionTrashListRequestToken(requestToken)) {
314
+ this.sessionTrashLastLoadFailed = true;
315
+ this.showMessage('加载回收站失败', 'error');
316
+ }
317
+ } finally {
318
+ this.sessionTrashLoading = false;
319
+ if (loadSucceeded) {
320
+ this.sessionTrashLoadedOnce = true;
321
+ }
322
+ const pendingOptions = this.sessionTrashPendingOptions;
323
+ this.sessionTrashPendingOptions = null;
324
+ if (pendingOptions) {
325
+ await this.loadSessionTrash(pendingOptions);
326
+ }
327
+ }
328
+ },
329
+
330
+ async restoreSessionTrash(item) {
331
+ const key = this.getSessionTrashActionKey(item);
332
+ if (!key || this.isSessionTrashActionBusy(key) || this.sessionTrashClearing) {
333
+ return;
334
+ }
335
+ this.sessionTrashRestoring[key] = true;
336
+ try {
337
+ const res = await api('restore-session-trash', { trashId: key });
338
+ if (res.error) {
339
+ this.showMessage(res.error, 'error');
340
+ return;
341
+ }
342
+ this.showMessage('会话已恢复', 'success');
343
+ if (typeof this.invalidateSessionsUsageData === 'function') {
344
+ this.invalidateSessionsUsageData({ preserveList: true });
345
+ }
346
+ this.invalidateSessionTrashRequests();
347
+ await this.loadSessionTrash({ forceRefresh: true });
348
+ if (this.sessionsLoadedOnce) {
349
+ await this.loadSessions();
350
+ }
351
+ } catch (e) {
352
+ this.showMessage('恢复失败', 'error');
353
+ } finally {
354
+ this.sessionTrashRestoring[key] = false;
355
+ }
356
+ },
357
+
358
+ async purgeSessionTrash(item) {
359
+ const key = this.getSessionTrashActionKey(item);
360
+ if (!key || this.isSessionTrashActionBusy(key) || this.sessionTrashClearing) {
361
+ return;
362
+ }
363
+ const confirmed = await this.requestConfirmDialog({
364
+ title: '彻底删除回收站记录',
365
+ message: '该会话将从回收站永久删除,且无法恢复。',
366
+ confirmText: '彻底删除',
367
+ cancelText: '取消',
368
+ danger: true
369
+ });
370
+ if (!confirmed) {
371
+ return;
372
+ }
373
+ this.sessionTrashPurging[key] = true;
374
+ try {
375
+ const res = await api('purge-session-trash', { trashId: key });
376
+ if (res.error) {
377
+ this.showMessage(res.error, 'error');
378
+ return;
379
+ }
380
+ this.showMessage('已彻底删除', 'success');
381
+ this.invalidateSessionTrashRequests();
382
+ await this.loadSessionTrash({ forceRefresh: true });
383
+ } catch (e) {
384
+ this.showMessage('彻底删除失败', 'error');
385
+ } finally {
386
+ this.sessionTrashPurging[key] = false;
387
+ }
388
+ },
389
+
390
+ async clearSessionTrash() {
391
+ const normalizedCount = Number(this.sessionTrashCount);
392
+ if (this.sessionTrashClearing || !Number.isFinite(normalizedCount) || normalizedCount <= 0) {
393
+ return;
394
+ }
395
+ const confirmed = await this.requestConfirmDialog({
396
+ title: '清空回收站',
397
+ message: '该操作会永久删除回收站中的全部会话,且无法恢复。',
398
+ confirmText: '全部清空',
399
+ cancelText: '取消',
400
+ danger: true
401
+ });
402
+ if (!confirmed) {
403
+ return;
404
+ }
405
+ this.sessionTrashClearing = true;
406
+ try {
407
+ const res = await api('purge-session-trash', { all: true });
408
+ if (res.error) {
409
+ this.showMessage(res.error, 'error');
410
+ return;
411
+ }
412
+ this.showMessage('回收站已清空', 'success');
413
+ this.invalidateSessionTrashRequests();
414
+ await this.loadSessionTrash({ forceRefresh: true });
415
+ } catch (e) {
416
+ this.showMessage('清空回收站失败', 'error');
417
+ } finally {
418
+ this.sessionTrashClearing = false;
419
+ }
420
+ }
421
+ };
422
+ }