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,626 +1,626 @@
1
- import {
2
- buildSessionFilterCacheState,
3
- isSessionQueryEnabled,
4
- normalizeSessionMessageRole,
5
- normalizeSessionPathFilter
6
- } from '../logic.mjs';
7
-
8
- function isSessionLoadNativeDialogEnabled(vm) {
9
- if (vm && typeof vm.isSessionLoadNativeDialogEnabled === 'function' && vm.isSessionLoadNativeDialogEnabled !== isSessionLoadNativeDialogEnabled) {
10
- try {
11
- return !!vm.isSessionLoadNativeDialogEnabled();
12
- } catch (_) {
13
- // fall through to shared detection
14
- }
15
- }
16
-
17
- try {
18
- if (globalThis && globalThis.__CODEXMATE_SESSION_LOAD_NATIVE_DIALOG__ === true) {
19
- return true;
20
- }
21
- } catch (_) {
22
- // ignore global flag lookup failures
23
- }
24
-
25
- try {
26
- if (typeof localStorage !== 'undefined' && typeof localStorage.getItem === 'function') {
27
- const stored = String(localStorage.getItem('codexmateSessionLoadNativeDialog') || '').trim().toLowerCase();
28
- if (stored === '1' || stored === 'true' || stored === 'yes' || stored === 'on') {
29
- return true;
30
- }
31
- }
32
- } catch (_) {
33
- // ignore storage lookup failures
34
- }
35
-
36
- try {
37
- const search = typeof location !== 'undefined' && location && typeof location.search === 'string'
38
- ? location.search
39
- : (typeof window !== 'undefined' && window.location && typeof window.location.search === 'string'
40
- ? window.location.search
41
- : '');
42
- if (!search) {
43
- return false;
44
- }
45
- const params = new URLSearchParams(search);
46
- const value = String(params.get('sessionLoadNativeDialog') || '').trim().toLowerCase();
47
- return value === '1' || value === 'true' || value === 'yes' || value === 'on';
48
- } catch (_) {
49
- return false;
50
- }
51
- }
52
-
53
- function emitSessionLoadNativeDialog(vm, step, details = '') {
54
- if (!isSessionLoadNativeDialogEnabled(vm)) {
55
- return;
56
- }
57
- const alertFn = typeof globalThis.alert === 'function'
58
- ? globalThis.alert.bind(globalThis)
59
- : (typeof window !== 'undefined' && typeof window.alert === 'function'
60
- ? window.alert.bind(window)
61
- : null);
62
- if (!alertFn) {
63
- return;
64
- }
65
- const message = details
66
- ? `[codexmate][session-load] ${step}\n${details}`
67
- : `[codexmate][session-load] ${step}`;
68
- alertFn(message);
69
- }
70
-
71
- export function createSessionBrowserMethods(options = {}) {
72
- const {
73
- api,
74
- loadSessionsHelper,
75
- loadActiveSessionDetailHelper
76
- } = options;
77
-
78
- return {
79
- normalizeSessionPathValue(value) {
80
- return normalizeSessionPathFilter(value);
81
- },
82
-
83
- mergeSessionPathOptions(baseList = [], incomingList = []) {
84
- const merged = [];
85
- const seen = new Set();
86
- const append = (items) => {
87
- if (!Array.isArray(items)) return;
88
- for (const item of items) {
89
- const value = this.normalizeSessionPathValue(item);
90
- if (!value) continue;
91
- const key = value.toLowerCase();
92
- if (seen.has(key)) continue;
93
- seen.add(key);
94
- merged.push(value);
95
- }
96
- };
97
-
98
- append(baseList);
99
- append(incomingList);
100
- return merged;
101
- },
102
-
103
- extractPathOptionsFromSessions(sessions) {
104
- const paths = [];
105
- if (!Array.isArray(sessions)) {
106
- return paths;
107
- }
108
-
109
- const seen = new Set();
110
- for (const session of sessions) {
111
- const value = this.normalizeSessionPathValue(session && session.cwd ? session.cwd : '');
112
- if (!value) continue;
113
- const key = value.toLowerCase();
114
- if (seen.has(key)) continue;
115
- seen.add(key);
116
- paths.push(value);
117
- }
118
- return paths;
119
- },
120
-
121
- syncSessionPathOptionsForSource(source, nextOptions, mergeWithExisting = false) {
122
- const targetSource = source === 'claude' ? 'claude' : (source === 'all' ? 'all' : 'codex');
123
- const current = Array.isArray(this.sessionPathOptionsMap[targetSource])
124
- ? this.sessionPathOptionsMap[targetSource]
125
- : [];
126
- const merged = mergeWithExisting
127
- ? this.mergeSessionPathOptions(current, nextOptions)
128
- : this.mergeSessionPathOptions([], nextOptions);
129
- this.sessionPathOptionsMap = {
130
- ...this.sessionPathOptionsMap,
131
- [targetSource]: merged
132
- };
133
- this.refreshSessionPathOptions(targetSource);
134
- },
135
-
136
- refreshSessionPathOptions(source) {
137
- const targetSource = source === 'claude' ? 'claude' : (source === 'all' ? 'all' : 'codex');
138
- const base = Array.isArray(this.sessionPathOptionsMap[targetSource])
139
- ? [...this.sessionPathOptionsMap[targetSource]]
140
- : [];
141
- const selected = this.normalizeSessionPathValue(this.sessionPathFilter);
142
- if (selected && !base.some(item => item.toLowerCase() === selected.toLowerCase())) {
143
- base.unshift(selected);
144
- }
145
- if (targetSource === this.sessionFilterSource) {
146
- this.sessionPathOptions = base;
147
- }
148
- },
149
-
150
- isSessionLoadNativeDialogEnabled() {
151
- return isSessionLoadNativeDialogEnabled(this);
152
- },
153
-
154
- emitSessionLoadNativeDialog(step, details = '') {
155
- emitSessionLoadNativeDialog(this, step, details);
156
- },
157
-
158
- async loadSessionPathOptions(options = {}) {
159
- const source = options.source === 'claude' ? 'claude' : (options.source === 'all' ? 'all' : 'codex');
160
- const forceRefresh = !!options.forceRefresh;
161
- const loaded = !!this.sessionPathOptionsLoadedMap[source];
162
- if (!forceRefresh && loaded) {
163
- emitSessionLoadNativeDialog(this, 'loadSessionPathOptions:cache-hit', `source=${source}`);
164
- if (source === this.sessionFilterSource) {
165
- this.sessionPathOptionsLoading = false;
166
- }
167
- return;
168
- }
169
-
170
- const nextSeqMap = {
171
- ...(this.sessionPathRequestSeqMap || {})
172
- };
173
- const requestSeq = (Number(nextSeqMap[source]) || 0) + 1;
174
- nextSeqMap[source] = requestSeq;
175
- this.sessionPathRequestSeqMap = nextSeqMap;
176
- emitSessionLoadNativeDialog(
177
- this,
178
- 'loadSessionPathOptions:start',
179
- `source=${source}\nforceRefresh=${forceRefresh}\nrequestSeq=${requestSeq}`
180
- );
181
- if (source === this.sessionFilterSource) {
182
- this.sessionPathOptionsLoading = true;
183
- }
184
- try {
185
- const res = await api('list-session-paths', {
186
- source,
187
- limit: 500,
188
- forceRefresh
189
- });
190
- if (requestSeq !== Number(((this.sessionPathRequestSeqMap || {})[source]) || 0)) {
191
- emitSessionLoadNativeDialog(
192
- this,
193
- 'loadSessionPathOptions:stale-response',
194
- `source=${source}\nrequestSeq=${requestSeq}`
195
- );
196
- return;
197
- }
198
- if (res && !res.error && Array.isArray(res.paths)) {
199
- this.syncSessionPathOptionsForSource(source, res.paths, true);
200
- this.sessionPathOptionsLoadedMap = {
201
- ...this.sessionPathOptionsLoadedMap,
202
- [source]: true
203
- };
204
- emitSessionLoadNativeDialog(
205
- this,
206
- 'loadSessionPathOptions:success',
207
- `source=${source}\npaths=${res.paths.length}`
208
- );
209
- } else if (res && res.error) {
210
- emitSessionLoadNativeDialog(
211
- this,
212
- 'loadSessionPathOptions:error',
213
- `source=${source}\nerror=${res.error}`
214
- );
215
- }
216
- } catch (error) {
217
- emitSessionLoadNativeDialog(
218
- this,
219
- 'loadSessionPathOptions:exception',
220
- `source=${source}\nerror=${error && error.message ? error.message : String(error)}`
221
- );
222
- // 路径补全失败不影响会话主流程
223
- } finally {
224
- if (
225
- source === this.sessionFilterSource
226
- && requestSeq === Number(((this.sessionPathRequestSeqMap || {})[source]) || 0)
227
- ) {
228
- this.sessionPathOptionsLoading = false;
229
- }
230
- emitSessionLoadNativeDialog(
231
- this,
232
- 'loadSessionPathOptions:complete',
233
- `source=${source}\nrequestSeq=${requestSeq}`
234
- );
235
- }
236
- },
237
-
238
- onSessionResumeYoloChange() {
239
- const value = this.sessionResumeWithYolo ? '1' : '0';
240
- localStorage.setItem('codexmateSessionResumeYolo', value);
241
- },
242
-
243
- restoreSessionFilterCache() {
244
- const sourceCache = localStorage.getItem('codexmateSessionFilterSource');
245
- const pathCache = localStorage.getItem('codexmateSessionPathFilter');
246
- const cached = buildSessionFilterCacheState(sourceCache, pathCache);
247
- this.sessionFilterSource = cached.source;
248
- this.sessionPathFilter = cached.pathFilter;
249
- this.refreshSessionPathOptions(this.sessionFilterSource);
250
- },
251
-
252
- persistSessionFilterCache() {
253
- const cached = buildSessionFilterCacheState(this.sessionFilterSource, this.sessionPathFilter);
254
- localStorage.setItem('codexmateSessionFilterSource', cached.source);
255
- if (cached.pathFilter) {
256
- localStorage.setItem('codexmateSessionPathFilter', cached.pathFilter);
257
- } else {
258
- localStorage.removeItem('codexmateSessionPathFilter');
259
- }
260
- },
261
-
262
- normalizeSessionPinnedMap(raw) {
263
- if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
264
- return {};
265
- }
266
- const next = {};
267
- for (const [key, value] of Object.entries(raw)) {
268
- if (!key) continue;
269
- const numeric = Number(value);
270
- if (!Number.isFinite(numeric) || numeric <= 0) continue;
271
- next[key] = Math.floor(numeric);
272
- }
273
- return next;
274
- },
275
-
276
- restoreSessionPinnedMap() {
277
- const cached = localStorage.getItem('codexmateSessionPinnedMap');
278
- if (!cached) {
279
- this.sessionPinnedMap = {};
280
- return;
281
- }
282
- try {
283
- const parsed = JSON.parse(cached);
284
- this.sessionPinnedMap = this.normalizeSessionPinnedMap(parsed);
285
- } catch (_) {
286
- this.sessionPinnedMap = {};
287
- localStorage.removeItem('codexmateSessionPinnedMap');
288
- }
289
- },
290
-
291
- persistSessionPinnedMap() {
292
- const payload = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
293
- ? this.sessionPinnedMap
294
- : {};
295
- localStorage.setItem('codexmateSessionPinnedMap', JSON.stringify(payload));
296
- },
297
-
298
- shouldPruneSessionPinnedMap(sessions = this.sessionsList) {
299
- if (!Array.isArray(sessions) || sessions.length === 0) {
300
- return false;
301
- }
302
- if (this.sessionFilterSource !== 'all') {
303
- return false;
304
- }
305
- if (this.sessionPathFilter) {
306
- return false;
307
- }
308
- if (this.sessionQuery && isSessionQueryEnabled(this.sessionFilterSource)) {
309
- return false;
310
- }
311
- if (this.sessionRoleFilter && this.sessionRoleFilter !== 'all') {
312
- return false;
313
- }
314
- if (this.sessionTimePreset && this.sessionTimePreset !== 'all') {
315
- return false;
316
- }
317
- return true;
318
- },
319
-
320
- pruneSessionPinnedMap(sessions = this.sessionsList) {
321
- const current = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
322
- ? this.sessionPinnedMap
323
- : {};
324
- const list = Array.isArray(sessions) ? sessions : [];
325
- if (Object.keys(current).length === 0 || !this.shouldPruneSessionPinnedMap(list)) {
326
- return;
327
- }
328
- const validKeys = new Set(list.map((session) => this.getSessionExportKey(session)).filter(Boolean));
329
- const next = {};
330
- let changed = false;
331
- for (const [key, value] of Object.entries(current)) {
332
- if (!validKeys.has(key)) {
333
- changed = true;
334
- continue;
335
- }
336
- next[key] = value;
337
- }
338
- if (!changed) {
339
- return;
340
- }
341
- this.sessionPinnedMap = next;
342
- this.persistSessionPinnedMap();
343
- },
344
-
345
- getSessionPinTimestamp(session) {
346
- if (!session) return 0;
347
- const key = this.getSessionExportKey(session);
348
- if (!key) return 0;
349
- const raw = this.sessionPinnedMap && this.sessionPinnedMap[key];
350
- const numeric = Number(raw);
351
- return Number.isFinite(numeric) && numeric > 0 ? Math.floor(numeric) : 0;
352
- },
353
-
354
- isSessionPinned(session) {
355
- return this.getSessionPinTimestamp(session) > 0;
356
- },
357
-
358
- toggleSessionPin(session) {
359
- if (!session) return;
360
- const key = this.getSessionExportKey(session);
361
- if (!key) return;
362
- const current = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
363
- ? this.sessionPinnedMap
364
- : {};
365
- const next = { ...current };
366
- if (next[key]) {
367
- delete next[key];
368
- } else {
369
- next[key] = Date.now();
370
- }
371
- this.sessionPinnedMap = next;
372
- this.persistSessionPinnedMap();
373
- },
374
-
375
- removeSessionPin(session) {
376
- if (!session) return;
377
- const key = this.getSessionExportKey(session);
378
- if (!key) return;
379
- const current = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
380
- ? this.sessionPinnedMap
381
- : {};
382
- if (!current[key]) return;
383
- const next = { ...current };
384
- delete next[key];
385
- this.sessionPinnedMap = next;
386
- this.persistSessionPinnedMap();
387
- },
388
-
389
- async onSessionSourceChange() {
390
- this.refreshSessionPathOptions(this.sessionFilterSource);
391
- this.persistSessionFilterCache();
392
- await this.loadSessions();
393
- },
394
-
395
- async onSessionPathFilterChange() {
396
- this.persistSessionFilterCache();
397
- await this.loadSessions();
398
- },
399
-
400
- async onSessionFilterChange() {
401
- await this.loadSessions();
402
- },
403
-
404
- async clearSessionFilters() {
405
- this.sessionFilterSource = 'all';
406
- this.sessionPathFilter = '';
407
- this.sessionQuery = '';
408
- this.sessionRoleFilter = 'all';
409
- this.sessionTimePreset = 'all';
410
- this.persistSessionFilterCache();
411
- await this.onSessionSourceChange();
412
- },
413
-
414
- normalizeSessionMessage(message) {
415
- const fallback = {
416
- role: 'assistant',
417
- normalizedRole: 'assistant',
418
- roleLabel: 'Assistant',
419
- text: typeof message === 'string' ? message : '',
420
- timestamp: ''
421
- };
422
- const safeMessage = message && typeof message === 'object' ? message : fallback;
423
- const normalizedRole = normalizeSessionMessageRole(
424
- safeMessage.normalizedRole || safeMessage.role
425
- );
426
- const roleLabel = normalizedRole === 'user'
427
- ? 'User'
428
- : (normalizedRole === 'system' ? 'System' : 'Assistant');
429
- return {
430
- ...safeMessage,
431
- role: normalizedRole,
432
- normalizedRole,
433
- roleLabel
434
- };
435
- },
436
-
437
- getRecordKey(message) {
438
- if (!message || !Number.isInteger(message.recordLineIndex) || message.recordLineIndex < 0) {
439
- return '';
440
- }
441
- return String(message.recordLineIndex);
442
- },
443
-
444
- getRecordRenderKey(message, idx) {
445
- const recordKey = this.getRecordKey(message);
446
- if (recordKey) {
447
- return `record-${recordKey}`;
448
- }
449
- return `record-fallback-${idx}-${message && message.timestamp ? message.timestamp : ''}`;
450
- },
451
-
452
- syncActiveSessionMessageCount(messageCount) {
453
- if (!Number.isFinite(messageCount) || messageCount < 0) return;
454
- if (this.activeSession) {
455
- this.activeSession.messageCount = messageCount;
456
- }
457
- const activeKey = this.activeSession ? this.getSessionExportKey(this.activeSession) : '';
458
- if (!activeKey) return;
459
- const matched = this.sessionsList.find(item => this.getSessionExportKey(item) === activeKey);
460
- if (matched) {
461
- matched.messageCount = messageCount;
462
- }
463
- },
464
-
465
- invalidateSessionsUsageData(options = {}) {
466
- this.sessionsUsageLoadedOnce = false;
467
- this.sessionsUsageError = '';
468
- if (options.preserveList !== true) {
469
- this.sessionsUsageList = [];
470
- }
471
- },
472
-
473
- async loadSessionsUsage(options = {}) {
474
- if (this.sessionsUsageLoading) return;
475
- this.sessionsUsageLoading = true;
476
- this.sessionsUsageError = '';
477
- let loadSucceeded = false;
478
- try {
479
- const res = await api('list-sessions-usage', {
480
- source: 'all',
481
- limit: 2000,
482
- forceRefresh: !!options.forceRefresh
483
- });
484
- if (res.error) {
485
- this.sessionsUsageError = res.error;
486
- this.showMessage(res.error, 'error');
487
- return;
488
- }
489
- this.sessionsUsageList = Array.isArray(res.sessions) ? res.sessions : [];
490
- loadSucceeded = true;
491
- } catch (e) {
492
- this.sessionsUsageError = '加载 usage 统计失败';
493
- this.showMessage('加载 usage 统计失败', 'error');
494
- } finally {
495
- this.sessionsUsageLoading = false;
496
- if (loadSucceeded) {
497
- this.sessionsUsageLoadedOnce = true;
498
- }
499
- }
500
- },
501
-
502
- async loadSessions(options = {}) {
503
- const result = await loadSessionsHelper.call(this, api, options || {});
504
- this.pruneSessionPinnedMap(this.sessionsList);
505
- return result;
506
- },
507
-
508
- async selectSession(session) {
509
- if (!session) {
510
- emitSessionLoadNativeDialog(this, 'selectSession:skip-empty');
511
- return;
512
- }
513
- emitSessionLoadNativeDialog(
514
- this,
515
- 'selectSession:start',
516
- `sessionId=${session.sessionId || ''}\nsource=${session.source || ''}`
517
- );
518
- const isSameSession = this.activeSession
519
- && this.getSessionExportKey(this.activeSession) === this.getSessionExportKey(session);
520
- if (isSameSession) {
521
- emitSessionLoadNativeDialog(this, 'selectSession:same-session', `sessionId=${session.sessionId || ''}`);
522
- if (this.sessionDetailLoading) {
523
- emitSessionLoadNativeDialog(this, 'selectSession:skip-detail-loading', `sessionId=${session.sessionId || ''}`);
524
- return;
525
- }
526
- const currentMessages = Array.isArray(this.activeSessionMessages) ? this.activeSessionMessages : [];
527
- if (currentMessages.length > 0) {
528
- emitSessionLoadNativeDialog(
529
- this,
530
- 'selectSession:skip-existing-messages',
531
- `sessionId=${session.sessionId || ''}\nmessages=${currentMessages.length}`
532
- );
533
- return;
534
- }
535
- if (typeof this.scheduleAfterFrame === 'function') {
536
- const selectedSession = this.activeSession;
537
- emitSessionLoadNativeDialog(this, 'selectSession:schedule-detail', `sessionId=${session.sessionId || ''}`);
538
- this.scheduleAfterFrame(() => {
539
- if (this.activeSession !== selectedSession) return;
540
- void this.loadActiveSessionDetail();
541
- });
542
- return;
543
- }
544
- emitSessionLoadNativeDialog(this, 'selectSession:load-detail-now', `sessionId=${session.sessionId || ''}`);
545
- await this.loadActiveSessionDetail();
546
- return;
547
- }
548
- this.activeSession = session;
549
- emitSessionLoadNativeDialog(this, 'selectSession:activate', `sessionId=${session.sessionId || ''}`);
550
- if (typeof this.expandVisibleSessionList === 'function') {
551
- this.expandVisibleSessionList(0, { ensureActive: true });
552
- }
553
- this.activeSessionMessages = [];
554
- this.resetSessionDetailPagination();
555
- this.resetSessionPreviewMessageRender();
556
- this.activeSessionDetailError = '';
557
- this.activeSessionDetailClipped = false;
558
- this.cancelSessionTimelineSync();
559
- this.sessionTimelineActiveKey = '';
560
- this.clearSessionTimelineRefs();
561
- if (typeof this.scheduleAfterFrame === 'function') {
562
- const selectedSession = this.activeSession;
563
- emitSessionLoadNativeDialog(this, 'selectSession:schedule-detail', `sessionId=${session.sessionId || ''}`);
564
- this.scheduleAfterFrame(() => {
565
- if (this.activeSession !== selectedSession) return;
566
- void this.loadActiveSessionDetail();
567
- });
568
- return;
569
- }
570
- emitSessionLoadNativeDialog(this, 'selectSession:load-detail-now', `sessionId=${session.sessionId || ''}`);
571
- await this.loadActiveSessionDetail();
572
- },
573
-
574
- async loadSessionStandalonePlain() {
575
- if (!this.activeSession) {
576
- this.sessionStandaloneRequestSeq += 1;
577
- this.sessionStandaloneLoading = false;
578
- this.sessionStandaloneText = '';
579
- this.sessionStandaloneTitle = '会话';
580
- this.sessionStandaloneSourceLabel = '';
581
- this.sessionStandaloneError = '';
582
- return;
583
- }
584
-
585
- const requestSeq = ++this.sessionStandaloneRequestSeq;
586
- const sessionSnapshot = this.activeSession;
587
- this.sessionStandaloneLoading = true;
588
- this.sessionStandaloneError = '';
589
- try {
590
- const res = await api('session-plain', {
591
- source: sessionSnapshot.source,
592
- sessionId: sessionSnapshot.sessionId,
593
- filePath: sessionSnapshot.filePath
594
- });
595
-
596
- if (requestSeq !== this.sessionStandaloneRequestSeq) {
597
- return;
598
- }
599
-
600
- if (res.error) {
601
- this.sessionStandaloneText = '';
602
- this.sessionStandaloneError = res.error;
603
- return;
604
- }
605
-
606
- this.sessionStandaloneSourceLabel = res.sourceLabel || sessionSnapshot.sourceLabel || '';
607
- this.sessionStandaloneTitle = res.sessionId || sessionSnapshot.title || '会话';
608
- this.sessionStandaloneText = typeof res.text === 'string' ? res.text : '';
609
- } catch (e) {
610
- if (requestSeq !== this.sessionStandaloneRequestSeq) {
611
- return;
612
- }
613
- this.sessionStandaloneText = '';
614
- this.sessionStandaloneError = '加载会话内容失败: ' + e.message;
615
- } finally {
616
- if (requestSeq === this.sessionStandaloneRequestSeq) {
617
- this.sessionStandaloneLoading = false;
618
- }
619
- }
620
- },
621
-
622
- async loadActiveSessionDetail(options = {}) {
623
- return loadActiveSessionDetailHelper.call(this, api, options);
624
- }
625
- };
626
- }
1
+ import {
2
+ buildSessionFilterCacheState,
3
+ isSessionQueryEnabled,
4
+ normalizeSessionMessageRole,
5
+ normalizeSessionPathFilter
6
+ } from '../logic.mjs';
7
+
8
+ function isSessionLoadNativeDialogEnabled(vm) {
9
+ if (vm && typeof vm.isSessionLoadNativeDialogEnabled === 'function' && vm.isSessionLoadNativeDialogEnabled !== isSessionLoadNativeDialogEnabled) {
10
+ try {
11
+ return !!vm.isSessionLoadNativeDialogEnabled();
12
+ } catch (_) {
13
+ // fall through to shared detection
14
+ }
15
+ }
16
+
17
+ try {
18
+ if (globalThis && globalThis.__CODEXMATE_SESSION_LOAD_NATIVE_DIALOG__ === true) {
19
+ return true;
20
+ }
21
+ } catch (_) {
22
+ // ignore global flag lookup failures
23
+ }
24
+
25
+ try {
26
+ if (typeof localStorage !== 'undefined' && typeof localStorage.getItem === 'function') {
27
+ const stored = String(localStorage.getItem('codexmateSessionLoadNativeDialog') || '').trim().toLowerCase();
28
+ if (stored === '1' || stored === 'true' || stored === 'yes' || stored === 'on') {
29
+ return true;
30
+ }
31
+ }
32
+ } catch (_) {
33
+ // ignore storage lookup failures
34
+ }
35
+
36
+ try {
37
+ const search = typeof location !== 'undefined' && location && typeof location.search === 'string'
38
+ ? location.search
39
+ : (typeof window !== 'undefined' && window.location && typeof window.location.search === 'string'
40
+ ? window.location.search
41
+ : '');
42
+ if (!search) {
43
+ return false;
44
+ }
45
+ const params = new URLSearchParams(search);
46
+ const value = String(params.get('sessionLoadNativeDialog') || '').trim().toLowerCase();
47
+ return value === '1' || value === 'true' || value === 'yes' || value === 'on';
48
+ } catch (_) {
49
+ return false;
50
+ }
51
+ }
52
+
53
+ function emitSessionLoadNativeDialog(vm, step, details = '') {
54
+ if (!isSessionLoadNativeDialogEnabled(vm)) {
55
+ return;
56
+ }
57
+ const alertFn = typeof globalThis.alert === 'function'
58
+ ? globalThis.alert.bind(globalThis)
59
+ : (typeof window !== 'undefined' && typeof window.alert === 'function'
60
+ ? window.alert.bind(window)
61
+ : null);
62
+ if (!alertFn) {
63
+ return;
64
+ }
65
+ const message = details
66
+ ? `[codexmate][session-load] ${step}\n${details}`
67
+ : `[codexmate][session-load] ${step}`;
68
+ alertFn(message);
69
+ }
70
+
71
+ export function createSessionBrowserMethods(options = {}) {
72
+ const {
73
+ api,
74
+ loadSessionsHelper,
75
+ loadActiveSessionDetailHelper
76
+ } = options;
77
+
78
+ return {
79
+ normalizeSessionPathValue(value) {
80
+ return normalizeSessionPathFilter(value);
81
+ },
82
+
83
+ mergeSessionPathOptions(baseList = [], incomingList = []) {
84
+ const merged = [];
85
+ const seen = new Set();
86
+ const append = (items) => {
87
+ if (!Array.isArray(items)) return;
88
+ for (const item of items) {
89
+ const value = this.normalizeSessionPathValue(item);
90
+ if (!value) continue;
91
+ const key = value.toLowerCase();
92
+ if (seen.has(key)) continue;
93
+ seen.add(key);
94
+ merged.push(value);
95
+ }
96
+ };
97
+
98
+ append(baseList);
99
+ append(incomingList);
100
+ return merged;
101
+ },
102
+
103
+ extractPathOptionsFromSessions(sessions) {
104
+ const paths = [];
105
+ if (!Array.isArray(sessions)) {
106
+ return paths;
107
+ }
108
+
109
+ const seen = new Set();
110
+ for (const session of sessions) {
111
+ const value = this.normalizeSessionPathValue(session && session.cwd ? session.cwd : '');
112
+ if (!value) continue;
113
+ const key = value.toLowerCase();
114
+ if (seen.has(key)) continue;
115
+ seen.add(key);
116
+ paths.push(value);
117
+ }
118
+ return paths;
119
+ },
120
+
121
+ syncSessionPathOptionsForSource(source, nextOptions, mergeWithExisting = false) {
122
+ const targetSource = source === 'claude' ? 'claude' : (source === 'all' ? 'all' : 'codex');
123
+ const current = Array.isArray(this.sessionPathOptionsMap[targetSource])
124
+ ? this.sessionPathOptionsMap[targetSource]
125
+ : [];
126
+ const merged = mergeWithExisting
127
+ ? this.mergeSessionPathOptions(current, nextOptions)
128
+ : this.mergeSessionPathOptions([], nextOptions);
129
+ this.sessionPathOptionsMap = {
130
+ ...this.sessionPathOptionsMap,
131
+ [targetSource]: merged
132
+ };
133
+ this.refreshSessionPathOptions(targetSource);
134
+ },
135
+
136
+ refreshSessionPathOptions(source) {
137
+ const targetSource = source === 'claude' ? 'claude' : (source === 'all' ? 'all' : 'codex');
138
+ const base = Array.isArray(this.sessionPathOptionsMap[targetSource])
139
+ ? [...this.sessionPathOptionsMap[targetSource]]
140
+ : [];
141
+ const selected = this.normalizeSessionPathValue(this.sessionPathFilter);
142
+ if (selected && !base.some(item => item.toLowerCase() === selected.toLowerCase())) {
143
+ base.unshift(selected);
144
+ }
145
+ if (targetSource === this.sessionFilterSource) {
146
+ this.sessionPathOptions = base;
147
+ }
148
+ },
149
+
150
+ isSessionLoadNativeDialogEnabled() {
151
+ return isSessionLoadNativeDialogEnabled(this);
152
+ },
153
+
154
+ emitSessionLoadNativeDialog(step, details = '') {
155
+ emitSessionLoadNativeDialog(this, step, details);
156
+ },
157
+
158
+ async loadSessionPathOptions(options = {}) {
159
+ const source = options.source === 'claude' ? 'claude' : (options.source === 'all' ? 'all' : 'codex');
160
+ const forceRefresh = !!options.forceRefresh;
161
+ const loaded = !!this.sessionPathOptionsLoadedMap[source];
162
+ if (!forceRefresh && loaded) {
163
+ emitSessionLoadNativeDialog(this, 'loadSessionPathOptions:cache-hit', `source=${source}`);
164
+ if (source === this.sessionFilterSource) {
165
+ this.sessionPathOptionsLoading = false;
166
+ }
167
+ return;
168
+ }
169
+
170
+ const nextSeqMap = {
171
+ ...(this.sessionPathRequestSeqMap || {})
172
+ };
173
+ const requestSeq = (Number(nextSeqMap[source]) || 0) + 1;
174
+ nextSeqMap[source] = requestSeq;
175
+ this.sessionPathRequestSeqMap = nextSeqMap;
176
+ emitSessionLoadNativeDialog(
177
+ this,
178
+ 'loadSessionPathOptions:start',
179
+ `source=${source}\nforceRefresh=${forceRefresh}\nrequestSeq=${requestSeq}`
180
+ );
181
+ if (source === this.sessionFilterSource) {
182
+ this.sessionPathOptionsLoading = true;
183
+ }
184
+ try {
185
+ const res = await api('list-session-paths', {
186
+ source,
187
+ limit: 500,
188
+ forceRefresh
189
+ });
190
+ if (requestSeq !== Number(((this.sessionPathRequestSeqMap || {})[source]) || 0)) {
191
+ emitSessionLoadNativeDialog(
192
+ this,
193
+ 'loadSessionPathOptions:stale-response',
194
+ `source=${source}\nrequestSeq=${requestSeq}`
195
+ );
196
+ return;
197
+ }
198
+ if (res && !res.error && Array.isArray(res.paths)) {
199
+ this.syncSessionPathOptionsForSource(source, res.paths, true);
200
+ this.sessionPathOptionsLoadedMap = {
201
+ ...this.sessionPathOptionsLoadedMap,
202
+ [source]: true
203
+ };
204
+ emitSessionLoadNativeDialog(
205
+ this,
206
+ 'loadSessionPathOptions:success',
207
+ `source=${source}\npaths=${res.paths.length}`
208
+ );
209
+ } else if (res && res.error) {
210
+ emitSessionLoadNativeDialog(
211
+ this,
212
+ 'loadSessionPathOptions:error',
213
+ `source=${source}\nerror=${res.error}`
214
+ );
215
+ }
216
+ } catch (error) {
217
+ emitSessionLoadNativeDialog(
218
+ this,
219
+ 'loadSessionPathOptions:exception',
220
+ `source=${source}\nerror=${error && error.message ? error.message : String(error)}`
221
+ );
222
+ // 路径补全失败不影响会话主流程
223
+ } finally {
224
+ if (
225
+ source === this.sessionFilterSource
226
+ && requestSeq === Number(((this.sessionPathRequestSeqMap || {})[source]) || 0)
227
+ ) {
228
+ this.sessionPathOptionsLoading = false;
229
+ }
230
+ emitSessionLoadNativeDialog(
231
+ this,
232
+ 'loadSessionPathOptions:complete',
233
+ `source=${source}\nrequestSeq=${requestSeq}`
234
+ );
235
+ }
236
+ },
237
+
238
+ onSessionResumeYoloChange() {
239
+ const value = this.sessionResumeWithYolo ? '1' : '0';
240
+ localStorage.setItem('codexmateSessionResumeYolo', value);
241
+ },
242
+
243
+ restoreSessionFilterCache() {
244
+ const sourceCache = localStorage.getItem('codexmateSessionFilterSource');
245
+ const pathCache = localStorage.getItem('codexmateSessionPathFilter');
246
+ const cached = buildSessionFilterCacheState(sourceCache, pathCache);
247
+ this.sessionFilterSource = cached.source;
248
+ this.sessionPathFilter = cached.pathFilter;
249
+ this.refreshSessionPathOptions(this.sessionFilterSource);
250
+ },
251
+
252
+ persistSessionFilterCache() {
253
+ const cached = buildSessionFilterCacheState(this.sessionFilterSource, this.sessionPathFilter);
254
+ localStorage.setItem('codexmateSessionFilterSource', cached.source);
255
+ if (cached.pathFilter) {
256
+ localStorage.setItem('codexmateSessionPathFilter', cached.pathFilter);
257
+ } else {
258
+ localStorage.removeItem('codexmateSessionPathFilter');
259
+ }
260
+ },
261
+
262
+ normalizeSessionPinnedMap(raw) {
263
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
264
+ return {};
265
+ }
266
+ const next = {};
267
+ for (const [key, value] of Object.entries(raw)) {
268
+ if (!key) continue;
269
+ const numeric = Number(value);
270
+ if (!Number.isFinite(numeric) || numeric <= 0) continue;
271
+ next[key] = Math.floor(numeric);
272
+ }
273
+ return next;
274
+ },
275
+
276
+ restoreSessionPinnedMap() {
277
+ const cached = localStorage.getItem('codexmateSessionPinnedMap');
278
+ if (!cached) {
279
+ this.sessionPinnedMap = {};
280
+ return;
281
+ }
282
+ try {
283
+ const parsed = JSON.parse(cached);
284
+ this.sessionPinnedMap = this.normalizeSessionPinnedMap(parsed);
285
+ } catch (_) {
286
+ this.sessionPinnedMap = {};
287
+ localStorage.removeItem('codexmateSessionPinnedMap');
288
+ }
289
+ },
290
+
291
+ persistSessionPinnedMap() {
292
+ const payload = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
293
+ ? this.sessionPinnedMap
294
+ : {};
295
+ localStorage.setItem('codexmateSessionPinnedMap', JSON.stringify(payload));
296
+ },
297
+
298
+ shouldPruneSessionPinnedMap(sessions = this.sessionsList) {
299
+ if (!Array.isArray(sessions) || sessions.length === 0) {
300
+ return false;
301
+ }
302
+ if (this.sessionFilterSource !== 'all') {
303
+ return false;
304
+ }
305
+ if (this.sessionPathFilter) {
306
+ return false;
307
+ }
308
+ if (this.sessionQuery && isSessionQueryEnabled(this.sessionFilterSource)) {
309
+ return false;
310
+ }
311
+ if (this.sessionRoleFilter && this.sessionRoleFilter !== 'all') {
312
+ return false;
313
+ }
314
+ if (this.sessionTimePreset && this.sessionTimePreset !== 'all') {
315
+ return false;
316
+ }
317
+ return true;
318
+ },
319
+
320
+ pruneSessionPinnedMap(sessions = this.sessionsList) {
321
+ const current = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
322
+ ? this.sessionPinnedMap
323
+ : {};
324
+ const list = Array.isArray(sessions) ? sessions : [];
325
+ if (Object.keys(current).length === 0 || !this.shouldPruneSessionPinnedMap(list)) {
326
+ return;
327
+ }
328
+ const validKeys = new Set(list.map((session) => this.getSessionExportKey(session)).filter(Boolean));
329
+ const next = {};
330
+ let changed = false;
331
+ for (const [key, value] of Object.entries(current)) {
332
+ if (!validKeys.has(key)) {
333
+ changed = true;
334
+ continue;
335
+ }
336
+ next[key] = value;
337
+ }
338
+ if (!changed) {
339
+ return;
340
+ }
341
+ this.sessionPinnedMap = next;
342
+ this.persistSessionPinnedMap();
343
+ },
344
+
345
+ getSessionPinTimestamp(session) {
346
+ if (!session) return 0;
347
+ const key = this.getSessionExportKey(session);
348
+ if (!key) return 0;
349
+ const raw = this.sessionPinnedMap && this.sessionPinnedMap[key];
350
+ const numeric = Number(raw);
351
+ return Number.isFinite(numeric) && numeric > 0 ? Math.floor(numeric) : 0;
352
+ },
353
+
354
+ isSessionPinned(session) {
355
+ return this.getSessionPinTimestamp(session) > 0;
356
+ },
357
+
358
+ toggleSessionPin(session) {
359
+ if (!session) return;
360
+ const key = this.getSessionExportKey(session);
361
+ if (!key) return;
362
+ const current = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
363
+ ? this.sessionPinnedMap
364
+ : {};
365
+ const next = { ...current };
366
+ if (next[key]) {
367
+ delete next[key];
368
+ } else {
369
+ next[key] = Date.now();
370
+ }
371
+ this.sessionPinnedMap = next;
372
+ this.persistSessionPinnedMap();
373
+ },
374
+
375
+ removeSessionPin(session) {
376
+ if (!session) return;
377
+ const key = this.getSessionExportKey(session);
378
+ if (!key) return;
379
+ const current = (this.sessionPinnedMap && typeof this.sessionPinnedMap === 'object')
380
+ ? this.sessionPinnedMap
381
+ : {};
382
+ if (!current[key]) return;
383
+ const next = { ...current };
384
+ delete next[key];
385
+ this.sessionPinnedMap = next;
386
+ this.persistSessionPinnedMap();
387
+ },
388
+
389
+ async onSessionSourceChange() {
390
+ this.refreshSessionPathOptions(this.sessionFilterSource);
391
+ this.persistSessionFilterCache();
392
+ await this.loadSessions();
393
+ },
394
+
395
+ async onSessionPathFilterChange() {
396
+ this.persistSessionFilterCache();
397
+ await this.loadSessions();
398
+ },
399
+
400
+ async onSessionFilterChange() {
401
+ await this.loadSessions();
402
+ },
403
+
404
+ async clearSessionFilters() {
405
+ this.sessionFilterSource = 'all';
406
+ this.sessionPathFilter = '';
407
+ this.sessionQuery = '';
408
+ this.sessionRoleFilter = 'all';
409
+ this.sessionTimePreset = 'all';
410
+ this.persistSessionFilterCache();
411
+ await this.onSessionSourceChange();
412
+ },
413
+
414
+ normalizeSessionMessage(message) {
415
+ const fallback = {
416
+ role: 'assistant',
417
+ normalizedRole: 'assistant',
418
+ roleLabel: 'Assistant',
419
+ text: typeof message === 'string' ? message : '',
420
+ timestamp: ''
421
+ };
422
+ const safeMessage = message && typeof message === 'object' ? message : fallback;
423
+ const normalizedRole = normalizeSessionMessageRole(
424
+ safeMessage.normalizedRole || safeMessage.role
425
+ );
426
+ const roleLabel = normalizedRole === 'user'
427
+ ? 'User'
428
+ : (normalizedRole === 'system' ? 'System' : 'Assistant');
429
+ return {
430
+ ...safeMessage,
431
+ role: normalizedRole,
432
+ normalizedRole,
433
+ roleLabel
434
+ };
435
+ },
436
+
437
+ getRecordKey(message) {
438
+ if (!message || !Number.isInteger(message.recordLineIndex) || message.recordLineIndex < 0) {
439
+ return '';
440
+ }
441
+ return String(message.recordLineIndex);
442
+ },
443
+
444
+ getRecordRenderKey(message, idx) {
445
+ const recordKey = this.getRecordKey(message);
446
+ if (recordKey) {
447
+ return `record-${recordKey}`;
448
+ }
449
+ return `record-fallback-${idx}-${message && message.timestamp ? message.timestamp : ''}`;
450
+ },
451
+
452
+ syncActiveSessionMessageCount(messageCount) {
453
+ if (!Number.isFinite(messageCount) || messageCount < 0) return;
454
+ if (this.activeSession) {
455
+ this.activeSession.messageCount = messageCount;
456
+ }
457
+ const activeKey = this.activeSession ? this.getSessionExportKey(this.activeSession) : '';
458
+ if (!activeKey) return;
459
+ const matched = this.sessionsList.find(item => this.getSessionExportKey(item) === activeKey);
460
+ if (matched) {
461
+ matched.messageCount = messageCount;
462
+ }
463
+ },
464
+
465
+ invalidateSessionsUsageData(options = {}) {
466
+ this.sessionsUsageLoadedOnce = false;
467
+ this.sessionsUsageError = '';
468
+ if (options.preserveList !== true) {
469
+ this.sessionsUsageList = [];
470
+ }
471
+ },
472
+
473
+ async loadSessionsUsage(options = {}) {
474
+ if (this.sessionsUsageLoading) return;
475
+ this.sessionsUsageLoading = true;
476
+ this.sessionsUsageError = '';
477
+ let loadSucceeded = false;
478
+ try {
479
+ const res = await api('list-sessions-usage', {
480
+ source: 'all',
481
+ limit: 2000,
482
+ forceRefresh: !!options.forceRefresh
483
+ });
484
+ if (res.error) {
485
+ this.sessionsUsageError = res.error;
486
+ this.showMessage(res.error, 'error');
487
+ return;
488
+ }
489
+ this.sessionsUsageList = Array.isArray(res.sessions) ? res.sessions : [];
490
+ loadSucceeded = true;
491
+ } catch (e) {
492
+ this.sessionsUsageError = '加载 usage 统计失败';
493
+ this.showMessage('加载 usage 统计失败', 'error');
494
+ } finally {
495
+ this.sessionsUsageLoading = false;
496
+ if (loadSucceeded) {
497
+ this.sessionsUsageLoadedOnce = true;
498
+ }
499
+ }
500
+ },
501
+
502
+ async loadSessions(options = {}) {
503
+ const result = await loadSessionsHelper.call(this, api, options || {});
504
+ this.pruneSessionPinnedMap(this.sessionsList);
505
+ return result;
506
+ },
507
+
508
+ async selectSession(session) {
509
+ if (!session) {
510
+ emitSessionLoadNativeDialog(this, 'selectSession:skip-empty');
511
+ return;
512
+ }
513
+ emitSessionLoadNativeDialog(
514
+ this,
515
+ 'selectSession:start',
516
+ `sessionId=${session.sessionId || ''}\nsource=${session.source || ''}`
517
+ );
518
+ const isSameSession = this.activeSession
519
+ && this.getSessionExportKey(this.activeSession) === this.getSessionExportKey(session);
520
+ if (isSameSession) {
521
+ emitSessionLoadNativeDialog(this, 'selectSession:same-session', `sessionId=${session.sessionId || ''}`);
522
+ if (this.sessionDetailLoading) {
523
+ emitSessionLoadNativeDialog(this, 'selectSession:skip-detail-loading', `sessionId=${session.sessionId || ''}`);
524
+ return;
525
+ }
526
+ const currentMessages = Array.isArray(this.activeSessionMessages) ? this.activeSessionMessages : [];
527
+ if (currentMessages.length > 0) {
528
+ emitSessionLoadNativeDialog(
529
+ this,
530
+ 'selectSession:skip-existing-messages',
531
+ `sessionId=${session.sessionId || ''}\nmessages=${currentMessages.length}`
532
+ );
533
+ return;
534
+ }
535
+ if (typeof this.scheduleAfterFrame === 'function') {
536
+ const selectedSession = this.activeSession;
537
+ emitSessionLoadNativeDialog(this, 'selectSession:schedule-detail', `sessionId=${session.sessionId || ''}`);
538
+ this.scheduleAfterFrame(() => {
539
+ if (this.activeSession !== selectedSession) return;
540
+ void this.loadActiveSessionDetail();
541
+ });
542
+ return;
543
+ }
544
+ emitSessionLoadNativeDialog(this, 'selectSession:load-detail-now', `sessionId=${session.sessionId || ''}`);
545
+ await this.loadActiveSessionDetail();
546
+ return;
547
+ }
548
+ this.activeSession = session;
549
+ emitSessionLoadNativeDialog(this, 'selectSession:activate', `sessionId=${session.sessionId || ''}`);
550
+ if (typeof this.expandVisibleSessionList === 'function') {
551
+ this.expandVisibleSessionList(0, { ensureActive: true });
552
+ }
553
+ this.activeSessionMessages = [];
554
+ this.resetSessionDetailPagination();
555
+ this.resetSessionPreviewMessageRender();
556
+ this.activeSessionDetailError = '';
557
+ this.activeSessionDetailClipped = false;
558
+ this.cancelSessionTimelineSync();
559
+ this.sessionTimelineActiveKey = '';
560
+ this.clearSessionTimelineRefs();
561
+ if (typeof this.scheduleAfterFrame === 'function') {
562
+ const selectedSession = this.activeSession;
563
+ emitSessionLoadNativeDialog(this, 'selectSession:schedule-detail', `sessionId=${session.sessionId || ''}`);
564
+ this.scheduleAfterFrame(() => {
565
+ if (this.activeSession !== selectedSession) return;
566
+ void this.loadActiveSessionDetail();
567
+ });
568
+ return;
569
+ }
570
+ emitSessionLoadNativeDialog(this, 'selectSession:load-detail-now', `sessionId=${session.sessionId || ''}`);
571
+ await this.loadActiveSessionDetail();
572
+ },
573
+
574
+ async loadSessionStandalonePlain() {
575
+ if (!this.activeSession) {
576
+ this.sessionStandaloneRequestSeq += 1;
577
+ this.sessionStandaloneLoading = false;
578
+ this.sessionStandaloneText = '';
579
+ this.sessionStandaloneTitle = '会话';
580
+ this.sessionStandaloneSourceLabel = '';
581
+ this.sessionStandaloneError = '';
582
+ return;
583
+ }
584
+
585
+ const requestSeq = ++this.sessionStandaloneRequestSeq;
586
+ const sessionSnapshot = this.activeSession;
587
+ this.sessionStandaloneLoading = true;
588
+ this.sessionStandaloneError = '';
589
+ try {
590
+ const res = await api('session-plain', {
591
+ source: sessionSnapshot.source,
592
+ sessionId: sessionSnapshot.sessionId,
593
+ filePath: sessionSnapshot.filePath
594
+ });
595
+
596
+ if (requestSeq !== this.sessionStandaloneRequestSeq) {
597
+ return;
598
+ }
599
+
600
+ if (res.error) {
601
+ this.sessionStandaloneText = '';
602
+ this.sessionStandaloneError = res.error;
603
+ return;
604
+ }
605
+
606
+ this.sessionStandaloneSourceLabel = res.sourceLabel || sessionSnapshot.sourceLabel || '';
607
+ this.sessionStandaloneTitle = res.sessionId || sessionSnapshot.title || '会话';
608
+ this.sessionStandaloneText = typeof res.text === 'string' ? res.text : '';
609
+ } catch (e) {
610
+ if (requestSeq !== this.sessionStandaloneRequestSeq) {
611
+ return;
612
+ }
613
+ this.sessionStandaloneText = '';
614
+ this.sessionStandaloneError = '加载会话内容失败: ' + e.message;
615
+ } finally {
616
+ if (requestSeq === this.sessionStandaloneRequestSeq) {
617
+ this.sessionStandaloneLoading = false;
618
+ }
619
+ }
620
+ },
621
+
622
+ async loadActiveSessionDetail(options = {}) {
623
+ return loadActiveSessionDetailHelper.call(this, api, options);
624
+ }
625
+ };
626
+ }