codexmate 0.0.28 → 0.0.29

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