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,386 +1,386 @@
1
- const fs = require('fs');
2
-
3
- const {
4
- toIsoTime,
5
- truncateText,
6
- extractMessageText,
7
- normalizeRole
8
- } = require('./cli-session-utils');
9
-
10
- const DEFAULT_SESSION_DETAIL_MESSAGES = 300;
11
- const FAST_SESSION_DETAIL_PREVIEW_FILE_BYTES = 256 * 1024;
12
- const FAST_SESSION_DETAIL_PREVIEW_CHUNK_BYTES = 64 * 1024;
13
- const FAST_SESSION_DETAIL_PREVIEW_MAX_BYTES = 1024 * 1024;
14
-
15
- const BOOTSTRAP_TEXT_MARKERS = [
16
- 'agents.md instructions',
17
- '<instructions>',
18
- '<environment_context>',
19
- 'you are a coding agent',
20
- 'codex cli'
21
- ];
22
-
23
- function getFileStatSafe(filePath) {
24
- try {
25
- return fs.statSync(filePath);
26
- } catch (e) {
27
- return null;
28
- }
29
- }
30
-
31
- function isBootstrapLikeText(text) {
32
- if (!text || typeof text !== 'string') {
33
- return false;
34
- }
35
-
36
- const normalized = text.toLowerCase().replace(/\s+/g, ' ').trim();
37
- if (!normalized) {
38
- return false;
39
- }
40
-
41
- return BOOTSTRAP_TEXT_MARKERS.some(marker => normalized.includes(marker));
42
- }
43
-
44
- function removeLeadingSystemMessage(messages) {
45
- if (!Array.isArray(messages) || messages.length === 0) {
46
- return [];
47
- }
48
-
49
- let startIndex = 0;
50
- while (startIndex < messages.length) {
51
- const item = messages[startIndex];
52
- const role = item ? normalizeRole(item.role) : '';
53
- const text = item && typeof item.text === 'string' ? item.text : '';
54
- const isSystemRole = role === 'system';
55
- const isBootstrapText = isBootstrapLikeText(text);
56
- if (!item || isSystemRole || isBootstrapText) {
57
- startIndex += 1;
58
- continue;
59
- }
60
- break;
61
- }
62
-
63
- if (startIndex <= 0) {
64
- return messages;
65
- }
66
- return messages.slice(startIndex);
67
- }
68
-
69
- function normalizeQueryTokens(query) {
70
- if (typeof query !== 'string') {
71
- return [];
72
- }
73
- return query
74
- .split(/\s+/)
75
- .map(item => item.trim())
76
- .map(item => item.toLowerCase())
77
- .filter(Boolean);
78
- }
79
-
80
- function expandSessionQueryTokens(tokens) {
81
- const base = Array.isArray(tokens) ? tokens.map(t => String(t || '').toLowerCase()).filter(Boolean) : [];
82
- const result = [];
83
- const seen = new Set();
84
- let hasClaudeAlias = false;
85
- let hasDaudeAlias = false;
86
-
87
- // First pass: detect multi-token aliases (e.g., "claude code", "daude code")
88
- for (let i = 0; i < base.length; i++) {
89
- const token = base[i];
90
- const nextToken = base[i + 1] || '';
91
-
92
- // Check for "claude code" pattern (two separate tokens)
93
- if (token === 'claude' && nextToken === 'code') {
94
- hasClaudeAlias = true;
95
- i++; // Skip next token
96
- continue;
97
- }
98
- // Check for "daude code" pattern (two separate tokens)
99
- if (token === 'daude' && nextToken === 'code') {
100
- hasDaudeAlias = true;
101
- i++; // Skip next token
102
- continue;
103
- }
104
- // Check for combined patterns (e.g., "claude-code", "claude_code", "claudecode")
105
- if (/^claude[-_ ]?code$/.test(token) || token === 'claudecode') {
106
- hasClaudeAlias = true;
107
- continue;
108
- }
109
- if (/^daude[-_ ]?code$/.test(token) || token === 'daudecode') {
110
- hasDaudeAlias = true;
111
- continue;
112
- }
113
- if (!seen.has(token)) {
114
- seen.add(token);
115
- result.push(token);
116
- }
117
- }
118
-
119
- const push = (token) => {
120
- const normalized = String(token || '').toLowerCase();
121
- if (!normalized || seen.has(normalized)) return;
122
- seen.add(normalized);
123
- result.push(normalized);
124
- };
125
-
126
- if (hasClaudeAlias) {
127
- push('claude');
128
- push('code');
129
- }
130
- if (hasDaudeAlias) {
131
- push('daude');
132
- push('code');
133
- }
134
-
135
- return result;
136
- }
137
-
138
- function matchTokensInText(text, tokens, mode = 'and') {
139
- if (!Array.isArray(tokens) || tokens.length === 0) {
140
- return true;
141
- }
142
- const haystack = String(text || '').toLowerCase();
143
- if (!haystack) {
144
- return false;
145
- }
146
- if (mode === 'or') {
147
- return tokens.some(token => haystack.includes(token));
148
- }
149
- return tokens.every(token => haystack.includes(token));
150
- }
151
-
152
- function extractMessageFromRecord(record, source) {
153
- if (!record) {
154
- return null;
155
- }
156
- if (source === 'codex') {
157
- if (record.type === 'response_item' && record.payload && record.payload.type === 'message') {
158
- const role = normalizeRole(record.payload.role);
159
- const text = extractMessageText(record.payload.content);
160
- if (!role || !text) {
161
- return null;
162
- }
163
- return { role, text };
164
- }
165
- return null;
166
- }
167
-
168
- const role = normalizeRole(record.type);
169
- if (!role) {
170
- return null;
171
- }
172
- const content = record.message ? record.message.content : '';
173
- const text = extractMessageText(content);
174
- if (!text) {
175
- return null;
176
- }
177
- return { role, text };
178
- }
179
-
180
- function appendSessionDetailTailMessage(state, record, source, lineIndex = -1) {
181
- if (!state || typeof state !== 'object') {
182
- return;
183
- }
184
-
185
- const message = extractMessageFromRecord(record, source);
186
- if (!message) {
187
- return;
188
- }
189
-
190
- const role = normalizeRole(message.role);
191
- const text = typeof message.text === 'string' ? message.text : '';
192
- if (!role || !text) {
193
- return;
194
- }
195
-
196
- if (state.leadingSystem && (role === 'system' || isBootstrapLikeText(text))) {
197
- return;
198
- }
199
-
200
- state.leadingSystem = false;
201
- state.totalMessages += 1;
202
- if (!Number.isFinite(state.tailLimit) || state.tailLimit <= 0) {
203
- return;
204
- }
205
-
206
- if (state.messages.length >= state.tailLimit) {
207
- state.messages.shift();
208
- }
209
- state.messages.push({
210
- role,
211
- text,
212
- timestamp: toIsoTime(record && record.timestamp, ''),
213
- recordLineIndex: Number.isInteger(lineIndex) ? lineIndex : -1
214
- });
215
- }
216
-
217
- function applySessionDetailRecordMetadata(record, source, state) {
218
- if (!state || typeof state !== 'object' || !record) {
219
- return;
220
- }
221
-
222
- if (record.timestamp) {
223
- state.updatedAt = toIsoTime(record.timestamp, state.updatedAt);
224
- }
225
-
226
- if (source === 'codex') {
227
- if (record.type === 'session_meta' && record.payload) {
228
- state.sessionId = record.payload.id || state.sessionId;
229
- state.cwd = record.payload.cwd || state.cwd;
230
- }
231
- return;
232
- }
233
-
234
- if (!state.sessionId && record.sessionId) {
235
- state.sessionId = record.sessionId;
236
- }
237
- if (!state.cwd && record.cwd) {
238
- state.cwd = record.cwd;
239
- }
240
- }
241
-
242
- function extractSessionDetailPreviewFromTailText(text, source, messageLimit) {
243
- const safeMessageLimit = Number.isFinite(Number(messageLimit))
244
- ? Math.max(1, Math.floor(Number(messageLimit)))
245
- : DEFAULT_SESSION_DETAIL_MESSAGES;
246
- const state = {
247
- sessionId: '',
248
- cwd: '',
249
- updatedAt: '',
250
- messages: [],
251
- tailLimit: safeMessageLimit,
252
- totalMessages: null,
253
- clipped: false
254
- };
255
- const lines = typeof text === 'string' && text
256
- ? text.split(/\r?\n/)
257
- : [];
258
-
259
- for (let lineIndex = lines.length - 1; lineIndex >= 0; lineIndex -= 1) {
260
- const trimmed = lines[lineIndex].trim();
261
- if (!trimmed) {
262
- continue;
263
- }
264
-
265
- let record;
266
- try {
267
- record = JSON.parse(trimmed);
268
- } catch (_) {
269
- continue;
270
- }
271
-
272
- if (record && record.timestamp && !state.updatedAt) {
273
- state.updatedAt = toIsoTime(record.timestamp, '');
274
- }
275
- if ((!state.sessionId || !state.cwd) && record) {
276
- applySessionDetailRecordMetadata(record, source, state);
277
- }
278
-
279
- const message = extractMessageFromRecord(record, source);
280
- if (!message) {
281
- continue;
282
- }
283
-
284
- const role = normalizeRole(message.role);
285
- const textValue = typeof message.text === 'string' ? message.text : '';
286
- if (!role || !textValue) {
287
- continue;
288
- }
289
-
290
- if (state.messages.length >= safeMessageLimit) {
291
- state.clipped = true;
292
- break;
293
- }
294
-
295
- state.messages.unshift({
296
- role,
297
- text: textValue,
298
- timestamp: toIsoTime(record && record.timestamp, ''),
299
- recordLineIndex: -1
300
- });
301
- }
302
-
303
- return state;
304
- }
305
-
306
- function extractSessionDetailPreviewFromFileFast(filePath, source, messageLimit) {
307
- const fileStat = getFileStatSafe(filePath);
308
- if (!fileStat || !Number.isFinite(fileStat.size) || fileStat.size <= FAST_SESSION_DETAIL_PREVIEW_FILE_BYTES) {
309
- return null;
310
- }
311
- const safeMessageLimit = Number.isFinite(Number(messageLimit))
312
- ? Math.max(1, Math.floor(Number(messageLimit)))
313
- : DEFAULT_SESSION_DETAIL_MESSAGES;
314
-
315
- let fd = null;
316
- let position = fileStat.size;
317
- let totalBytesRead = 0;
318
- let combined = Buffer.alloc(0);
319
- let latest = {
320
- sessionId: '',
321
- cwd: '',
322
- updatedAt: '',
323
- messages: [],
324
- totalMessages: null,
325
- clipped: false
326
- };
327
-
328
- try {
329
- fd = fs.openSync(filePath, 'r');
330
- while (position > 0 && totalBytesRead < FAST_SESSION_DETAIL_PREVIEW_MAX_BYTES) {
331
- const remainingBudget = FAST_SESSION_DETAIL_PREVIEW_MAX_BYTES - totalBytesRead;
332
- const chunkSize = Math.min(FAST_SESSION_DETAIL_PREVIEW_CHUNK_BYTES, position, remainingBudget);
333
- if (chunkSize <= 0) {
334
- break;
335
- }
336
-
337
- position -= chunkSize;
338
- const chunk = Buffer.allocUnsafe(chunkSize);
339
- const bytesRead = fs.readSync(fd, chunk, 0, chunkSize, position);
340
- if (bytesRead <= 0) {
341
- break;
342
- }
343
-
344
- totalBytesRead += bytesRead;
345
- combined = Buffer.concat([chunk.subarray(0, bytesRead), combined]);
346
- latest = extractSessionDetailPreviewFromTailText(combined.toString('utf-8'), source, safeMessageLimit);
347
- if (latest.messages.length >= safeMessageLimit) {
348
- latest.clipped = latest.clipped || position > 0;
349
- return latest;
350
- }
351
- }
352
-
353
- if (position > 0) {
354
- latest.clipped = latest.clipped || position > 0;
355
- return latest;
356
- }
357
- const normalizedMessages = removeLeadingSystemMessage(latest.messages);
358
- latest.messages = normalizedMessages.length > safeMessageLimit
359
- ? normalizedMessages.slice(-safeMessageLimit)
360
- : normalizedMessages;
361
- latest.totalMessages = normalizedMessages.length;
362
- latest.clipped = latest.totalMessages > latest.messages.length;
363
- return latest;
364
- } catch (_) {
365
- return null;
366
- } finally {
367
- if (fd !== null) {
368
- try { fs.closeSync(fd); } catch (e) {}
369
- }
370
- }
371
- }
372
-
373
- module.exports = {
374
- BOOTSTRAP_TEXT_MARKERS,
375
- getFileStatSafe,
376
- isBootstrapLikeText,
377
- removeLeadingSystemMessage,
378
- normalizeQueryTokens,
379
- expandSessionQueryTokens,
380
- matchTokensInText,
381
- extractMessageFromRecord,
382
- appendSessionDetailTailMessage,
383
- applySessionDetailRecordMetadata,
384
- extractSessionDetailPreviewFromTailText,
385
- extractSessionDetailPreviewFromFileFast
386
- };
1
+ const fs = require('fs');
2
+
3
+ const {
4
+ toIsoTime,
5
+ truncateText,
6
+ extractMessageText,
7
+ normalizeRole
8
+ } = require('./cli-session-utils');
9
+
10
+ const DEFAULT_SESSION_DETAIL_MESSAGES = 300;
11
+ const FAST_SESSION_DETAIL_PREVIEW_FILE_BYTES = 256 * 1024;
12
+ const FAST_SESSION_DETAIL_PREVIEW_CHUNK_BYTES = 64 * 1024;
13
+ const FAST_SESSION_DETAIL_PREVIEW_MAX_BYTES = 1024 * 1024;
14
+
15
+ const BOOTSTRAP_TEXT_MARKERS = [
16
+ 'agents.md instructions',
17
+ '<instructions>',
18
+ '<environment_context>',
19
+ 'you are a coding agent',
20
+ 'codex cli'
21
+ ];
22
+
23
+ function getFileStatSafe(filePath) {
24
+ try {
25
+ return fs.statSync(filePath);
26
+ } catch (e) {
27
+ return null;
28
+ }
29
+ }
30
+
31
+ function isBootstrapLikeText(text) {
32
+ if (!text || typeof text !== 'string') {
33
+ return false;
34
+ }
35
+
36
+ const normalized = text.toLowerCase().replace(/\s+/g, ' ').trim();
37
+ if (!normalized) {
38
+ return false;
39
+ }
40
+
41
+ return BOOTSTRAP_TEXT_MARKERS.some(marker => normalized.includes(marker));
42
+ }
43
+
44
+ function removeLeadingSystemMessage(messages) {
45
+ if (!Array.isArray(messages) || messages.length === 0) {
46
+ return [];
47
+ }
48
+
49
+ let startIndex = 0;
50
+ while (startIndex < messages.length) {
51
+ const item = messages[startIndex];
52
+ const role = item ? normalizeRole(item.role) : '';
53
+ const text = item && typeof item.text === 'string' ? item.text : '';
54
+ const isSystemRole = role === 'system';
55
+ const isBootstrapText = isBootstrapLikeText(text);
56
+ if (!item || isSystemRole || isBootstrapText) {
57
+ startIndex += 1;
58
+ continue;
59
+ }
60
+ break;
61
+ }
62
+
63
+ if (startIndex <= 0) {
64
+ return messages;
65
+ }
66
+ return messages.slice(startIndex);
67
+ }
68
+
69
+ function normalizeQueryTokens(query) {
70
+ if (typeof query !== 'string') {
71
+ return [];
72
+ }
73
+ return query
74
+ .split(/\s+/)
75
+ .map(item => item.trim())
76
+ .map(item => item.toLowerCase())
77
+ .filter(Boolean);
78
+ }
79
+
80
+ function expandSessionQueryTokens(tokens) {
81
+ const base = Array.isArray(tokens) ? tokens.map(t => String(t || '').toLowerCase()).filter(Boolean) : [];
82
+ const result = [];
83
+ const seen = new Set();
84
+ let hasClaudeAlias = false;
85
+ let hasDaudeAlias = false;
86
+
87
+ // First pass: detect multi-token aliases (e.g., "claude code", "daude code")
88
+ for (let i = 0; i < base.length; i++) {
89
+ const token = base[i];
90
+ const nextToken = base[i + 1] || '';
91
+
92
+ // Check for "claude code" pattern (two separate tokens)
93
+ if (token === 'claude' && nextToken === 'code') {
94
+ hasClaudeAlias = true;
95
+ i++; // Skip next token
96
+ continue;
97
+ }
98
+ // Check for "daude code" pattern (two separate tokens)
99
+ if (token === 'daude' && nextToken === 'code') {
100
+ hasDaudeAlias = true;
101
+ i++; // Skip next token
102
+ continue;
103
+ }
104
+ // Check for combined patterns (e.g., "claude-code", "claude_code", "claudecode")
105
+ if (/^claude[-_ ]?code$/.test(token) || token === 'claudecode') {
106
+ hasClaudeAlias = true;
107
+ continue;
108
+ }
109
+ if (/^daude[-_ ]?code$/.test(token) || token === 'daudecode') {
110
+ hasDaudeAlias = true;
111
+ continue;
112
+ }
113
+ if (!seen.has(token)) {
114
+ seen.add(token);
115
+ result.push(token);
116
+ }
117
+ }
118
+
119
+ const push = (token) => {
120
+ const normalized = String(token || '').toLowerCase();
121
+ if (!normalized || seen.has(normalized)) return;
122
+ seen.add(normalized);
123
+ result.push(normalized);
124
+ };
125
+
126
+ if (hasClaudeAlias) {
127
+ push('claude');
128
+ push('code');
129
+ }
130
+ if (hasDaudeAlias) {
131
+ push('daude');
132
+ push('code');
133
+ }
134
+
135
+ return result;
136
+ }
137
+
138
+ function matchTokensInText(text, tokens, mode = 'and') {
139
+ if (!Array.isArray(tokens) || tokens.length === 0) {
140
+ return true;
141
+ }
142
+ const haystack = String(text || '').toLowerCase();
143
+ if (!haystack) {
144
+ return false;
145
+ }
146
+ if (mode === 'or') {
147
+ return tokens.some(token => haystack.includes(token));
148
+ }
149
+ return tokens.every(token => haystack.includes(token));
150
+ }
151
+
152
+ function extractMessageFromRecord(record, source) {
153
+ if (!record) {
154
+ return null;
155
+ }
156
+ if (source === 'codex') {
157
+ if (record.type === 'response_item' && record.payload && record.payload.type === 'message') {
158
+ const role = normalizeRole(record.payload.role);
159
+ const text = extractMessageText(record.payload.content);
160
+ if (!role || !text) {
161
+ return null;
162
+ }
163
+ return { role, text };
164
+ }
165
+ return null;
166
+ }
167
+
168
+ const role = normalizeRole(record.type);
169
+ if (!role) {
170
+ return null;
171
+ }
172
+ const content = record.message ? record.message.content : '';
173
+ const text = extractMessageText(content);
174
+ if (!text) {
175
+ return null;
176
+ }
177
+ return { role, text };
178
+ }
179
+
180
+ function appendSessionDetailTailMessage(state, record, source, lineIndex = -1) {
181
+ if (!state || typeof state !== 'object') {
182
+ return;
183
+ }
184
+
185
+ const message = extractMessageFromRecord(record, source);
186
+ if (!message) {
187
+ return;
188
+ }
189
+
190
+ const role = normalizeRole(message.role);
191
+ const text = typeof message.text === 'string' ? message.text : '';
192
+ if (!role || !text) {
193
+ return;
194
+ }
195
+
196
+ if (state.leadingSystem && (role === 'system' || isBootstrapLikeText(text))) {
197
+ return;
198
+ }
199
+
200
+ state.leadingSystem = false;
201
+ state.totalMessages += 1;
202
+ if (!Number.isFinite(state.tailLimit) || state.tailLimit <= 0) {
203
+ return;
204
+ }
205
+
206
+ if (state.messages.length >= state.tailLimit) {
207
+ state.messages.shift();
208
+ }
209
+ state.messages.push({
210
+ role,
211
+ text,
212
+ timestamp: toIsoTime(record && record.timestamp, ''),
213
+ recordLineIndex: Number.isInteger(lineIndex) ? lineIndex : -1
214
+ });
215
+ }
216
+
217
+ function applySessionDetailRecordMetadata(record, source, state) {
218
+ if (!state || typeof state !== 'object' || !record) {
219
+ return;
220
+ }
221
+
222
+ if (record.timestamp) {
223
+ state.updatedAt = toIsoTime(record.timestamp, state.updatedAt);
224
+ }
225
+
226
+ if (source === 'codex') {
227
+ if (record.type === 'session_meta' && record.payload) {
228
+ state.sessionId = record.payload.id || state.sessionId;
229
+ state.cwd = record.payload.cwd || state.cwd;
230
+ }
231
+ return;
232
+ }
233
+
234
+ if (!state.sessionId && record.sessionId) {
235
+ state.sessionId = record.sessionId;
236
+ }
237
+ if (!state.cwd && record.cwd) {
238
+ state.cwd = record.cwd;
239
+ }
240
+ }
241
+
242
+ function extractSessionDetailPreviewFromTailText(text, source, messageLimit) {
243
+ const safeMessageLimit = Number.isFinite(Number(messageLimit))
244
+ ? Math.max(1, Math.floor(Number(messageLimit)))
245
+ : DEFAULT_SESSION_DETAIL_MESSAGES;
246
+ const state = {
247
+ sessionId: '',
248
+ cwd: '',
249
+ updatedAt: '',
250
+ messages: [],
251
+ tailLimit: safeMessageLimit,
252
+ totalMessages: null,
253
+ clipped: false
254
+ };
255
+ const lines = typeof text === 'string' && text
256
+ ? text.split(/\r?\n/)
257
+ : [];
258
+
259
+ for (let lineIndex = lines.length - 1; lineIndex >= 0; lineIndex -= 1) {
260
+ const trimmed = lines[lineIndex].trim();
261
+ if (!trimmed) {
262
+ continue;
263
+ }
264
+
265
+ let record;
266
+ try {
267
+ record = JSON.parse(trimmed);
268
+ } catch (_) {
269
+ continue;
270
+ }
271
+
272
+ if (record && record.timestamp && !state.updatedAt) {
273
+ state.updatedAt = toIsoTime(record.timestamp, '');
274
+ }
275
+ if ((!state.sessionId || !state.cwd) && record) {
276
+ applySessionDetailRecordMetadata(record, source, state);
277
+ }
278
+
279
+ const message = extractMessageFromRecord(record, source);
280
+ if (!message) {
281
+ continue;
282
+ }
283
+
284
+ const role = normalizeRole(message.role);
285
+ const textValue = typeof message.text === 'string' ? message.text : '';
286
+ if (!role || !textValue) {
287
+ continue;
288
+ }
289
+
290
+ if (state.messages.length >= safeMessageLimit) {
291
+ state.clipped = true;
292
+ break;
293
+ }
294
+
295
+ state.messages.unshift({
296
+ role,
297
+ text: textValue,
298
+ timestamp: toIsoTime(record && record.timestamp, ''),
299
+ recordLineIndex: -1
300
+ });
301
+ }
302
+
303
+ return state;
304
+ }
305
+
306
+ function extractSessionDetailPreviewFromFileFast(filePath, source, messageLimit) {
307
+ const fileStat = getFileStatSafe(filePath);
308
+ if (!fileStat || !Number.isFinite(fileStat.size) || fileStat.size <= FAST_SESSION_DETAIL_PREVIEW_FILE_BYTES) {
309
+ return null;
310
+ }
311
+ const safeMessageLimit = Number.isFinite(Number(messageLimit))
312
+ ? Math.max(1, Math.floor(Number(messageLimit)))
313
+ : DEFAULT_SESSION_DETAIL_MESSAGES;
314
+
315
+ let fd = null;
316
+ let position = fileStat.size;
317
+ let totalBytesRead = 0;
318
+ let combined = Buffer.alloc(0);
319
+ let latest = {
320
+ sessionId: '',
321
+ cwd: '',
322
+ updatedAt: '',
323
+ messages: [],
324
+ totalMessages: null,
325
+ clipped: false
326
+ };
327
+
328
+ try {
329
+ fd = fs.openSync(filePath, 'r');
330
+ while (position > 0 && totalBytesRead < FAST_SESSION_DETAIL_PREVIEW_MAX_BYTES) {
331
+ const remainingBudget = FAST_SESSION_DETAIL_PREVIEW_MAX_BYTES - totalBytesRead;
332
+ const chunkSize = Math.min(FAST_SESSION_DETAIL_PREVIEW_CHUNK_BYTES, position, remainingBudget);
333
+ if (chunkSize <= 0) {
334
+ break;
335
+ }
336
+
337
+ position -= chunkSize;
338
+ const chunk = Buffer.allocUnsafe(chunkSize);
339
+ const bytesRead = fs.readSync(fd, chunk, 0, chunkSize, position);
340
+ if (bytesRead <= 0) {
341
+ break;
342
+ }
343
+
344
+ totalBytesRead += bytesRead;
345
+ combined = Buffer.concat([chunk.subarray(0, bytesRead), combined]);
346
+ latest = extractSessionDetailPreviewFromTailText(combined.toString('utf-8'), source, safeMessageLimit);
347
+ if (latest.messages.length >= safeMessageLimit) {
348
+ latest.clipped = latest.clipped || position > 0;
349
+ return latest;
350
+ }
351
+ }
352
+
353
+ if (position > 0) {
354
+ latest.clipped = latest.clipped || position > 0;
355
+ return latest;
356
+ }
357
+ const normalizedMessages = removeLeadingSystemMessage(latest.messages);
358
+ latest.messages = normalizedMessages.length > safeMessageLimit
359
+ ? normalizedMessages.slice(-safeMessageLimit)
360
+ : normalizedMessages;
361
+ latest.totalMessages = normalizedMessages.length;
362
+ latest.clipped = latest.totalMessages > latest.messages.length;
363
+ return latest;
364
+ } catch (_) {
365
+ return null;
366
+ } finally {
367
+ if (fd !== null) {
368
+ try { fs.closeSync(fd); } catch (e) {}
369
+ }
370
+ }
371
+ }
372
+
373
+ module.exports = {
374
+ BOOTSTRAP_TEXT_MARKERS,
375
+ getFileStatSafe,
376
+ isBootstrapLikeText,
377
+ removeLeadingSystemMessage,
378
+ normalizeQueryTokens,
379
+ expandSessionQueryTokens,
380
+ matchTokensInText,
381
+ extractMessageFromRecord,
382
+ appendSessionDetailTailMessage,
383
+ applySessionDetailRecordMetadata,
384
+ extractSessionDetailPreviewFromTailText,
385
+ extractSessionDetailPreviewFromFileFast
386
+ };