codexmate 0.0.39 → 0.0.41

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 (155) hide show
  1. package/README.md +168 -156
  2. package/README.zh.md +168 -156
  3. package/cli/agents-files.js +230 -230
  4. package/cli/analytics-export-args.js +68 -68
  5. package/cli/archive-helpers.js +453 -453
  6. package/cli/auth-profiles.js +375 -375
  7. package/cli/builtin-proxy.js +2144 -2144
  8. package/cli/claude-proxy.js +1022 -1022
  9. package/cli/config-bootstrap.js +407 -402
  10. package/cli/config-health.js +454 -454
  11. package/cli/doctor-core.js +903 -903
  12. package/cli/import-skills-url.js +356 -356
  13. package/cli/local-bridge.js +556 -556
  14. package/cli/openai-bridge.js +1984 -1984
  15. package/cli/openclaw-config.js +629 -629
  16. package/cli/session-convert-args.js +69 -69
  17. package/cli/session-convert-io.js +82 -82
  18. package/cli/session-convert.js +150 -150
  19. package/cli/session-usage.concurrent.js +28 -28
  20. package/cli/session-usage.js +304 -304
  21. package/cli/session-usage.models.js +176 -176
  22. package/cli/skills.js +1141 -1141
  23. package/cli/update.js +171 -171
  24. package/cli/zip-commands.js +510 -510
  25. package/cli.js +16458 -16174
  26. package/lib/automation.js +404 -404
  27. package/lib/cli-file-utils.js +151 -151
  28. package/lib/cli-models-utils.js +440 -440
  29. package/lib/cli-network-utils.js +190 -190
  30. package/lib/cli-path-utils.js +85 -85
  31. package/lib/cli-session-utils.js +121 -121
  32. package/lib/cli-sessions.js +427 -427
  33. package/lib/cli-utils.js +155 -155
  34. package/lib/cli-webhook.js +154 -154
  35. package/lib/download-artifacts.js +92 -92
  36. package/lib/mcp-stdio.js +453 -453
  37. package/lib/task-orchestrator.js +869 -869
  38. package/lib/text-diff.js +303 -303
  39. package/lib/win-tray.js +119 -119
  40. package/lib/workflow-engine.js +340 -340
  41. package/package.json +77 -77
  42. package/plugins/README.md +20 -20
  43. package/plugins/README.zh-CN.md +20 -20
  44. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  45. package/plugins/prompt-templates/computed.mjs +311 -253
  46. package/plugins/prompt-templates/index.mjs +8 -8
  47. package/plugins/prompt-templates/manifest.mjs +18 -15
  48. package/plugins/prompt-templates/methods.mjs +553 -553
  49. package/plugins/prompt-templates/overview.mjs +91 -91
  50. package/plugins/prompt-templates/ownership.mjs +19 -19
  51. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  52. package/plugins/prompt-templates/storage.mjs +64 -64
  53. package/plugins/registry.mjs +16 -16
  54. package/web-ui/app.js +695 -693
  55. package/web-ui/index.html +37 -37
  56. package/web-ui/logic.agents-diff.mjs +386 -386
  57. package/web-ui/logic.claude.mjs +172 -172
  58. package/web-ui/logic.codex.mjs +69 -69
  59. package/web-ui/logic.mjs +5 -5
  60. package/web-ui/logic.runtime.mjs +128 -128
  61. package/web-ui/logic.session-convert.mjs +70 -70
  62. package/web-ui/logic.sessions.mjs +782 -782
  63. package/web-ui/modules/api.mjs +90 -90
  64. package/web-ui/modules/app.computed.dashboard.mjs +252 -252
  65. package/web-ui/modules/app.computed.index.mjs +17 -17
  66. package/web-ui/modules/app.computed.main-tabs.mjs +214 -212
  67. package/web-ui/modules/app.computed.session.mjs +876 -876
  68. package/web-ui/modules/app.constants.mjs +15 -15
  69. package/web-ui/modules/app.methods.agents.mjs +651 -651
  70. package/web-ui/modules/app.methods.claude-config.mjs +412 -412
  71. package/web-ui/modules/app.methods.codex-config.mjs +869 -869
  72. package/web-ui/modules/app.methods.index.mjs +96 -94
  73. package/web-ui/modules/app.methods.install.mjs +205 -205
  74. package/web-ui/modules/app.methods.navigation.mjs +804 -804
  75. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  76. package/web-ui/modules/app.methods.openclaw-editing.mjs +420 -420
  77. package/web-ui/modules/app.methods.openclaw-persist.mjs +375 -375
  78. package/web-ui/modules/app.methods.providers.mjs +601 -601
  79. package/web-ui/modules/app.methods.runtime.mjs +420 -420
  80. package/web-ui/modules/app.methods.session-actions.mjs +591 -591
  81. package/web-ui/modules/app.methods.session-browser.mjs +1018 -1018
  82. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  83. package/web-ui/modules/app.methods.session-trash.mjs +468 -468
  84. package/web-ui/modules/app.methods.startup-claude.mjs +554 -548
  85. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  86. package/web-ui/modules/app.methods.tool-config-permissions.mjs +87 -0
  87. package/web-ui/modules/app.methods.webhook.mjs +87 -87
  88. package/web-ui/modules/config-mode.computed.mjs +124 -124
  89. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  90. package/web-ui/modules/i18n/locales/en.mjs +1140 -0
  91. package/web-ui/modules/i18n/locales/ja.mjs +1130 -0
  92. package/web-ui/modules/i18n/locales/vi.mjs +239 -0
  93. package/web-ui/modules/i18n/locales/zh.mjs +1143 -0
  94. package/web-ui/modules/i18n.dict.mjs +14 -3213
  95. package/web-ui/modules/i18n.mjs +111 -111
  96. package/web-ui/modules/plugins.computed.mjs +3 -3
  97. package/web-ui/modules/plugins.methods.mjs +3 -3
  98. package/web-ui/modules/plugins.storage.mjs +11 -11
  99. package/web-ui/modules/provider-url-display.mjs +17 -17
  100. package/web-ui/modules/sessions-filters-url.mjs +138 -138
  101. package/web-ui/modules/skills.computed.mjs +107 -107
  102. package/web-ui/modules/skills.methods.mjs +513 -513
  103. package/web-ui/partials/index/layout-footer.html +13 -13
  104. package/web-ui/partials/index/layout-header.html +478 -478
  105. package/web-ui/partials/index/modal-config-template-agents.html +185 -185
  106. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  107. package/web-ui/partials/index/modal-health-check.html +45 -45
  108. package/web-ui/partials/index/modal-openclaw-config.html +344 -344
  109. package/web-ui/partials/index/modal-skills.html +200 -200
  110. package/web-ui/partials/index/modal-webhook.html +42 -42
  111. package/web-ui/partials/index/modals-basic.html +263 -263
  112. package/web-ui/partials/index/panel-config-claude.html +187 -157
  113. package/web-ui/partials/index/panel-config-codex.html +205 -176
  114. package/web-ui/partials/index/panel-config-openclaw.html +89 -89
  115. package/web-ui/partials/index/panel-dashboard.html +171 -171
  116. package/web-ui/partials/index/panel-docs.html +114 -114
  117. package/web-ui/partials/index/panel-market.html +104 -104
  118. package/web-ui/partials/index/panel-orchestration.html +391 -391
  119. package/web-ui/partials/index/panel-plugins.html +253 -253
  120. package/web-ui/partials/index/panel-sessions.html +319 -319
  121. package/web-ui/partials/index/panel-settings.html +181 -181
  122. package/web-ui/partials/index/panel-trash.html +82 -82
  123. package/web-ui/partials/index/panel-usage.html +181 -181
  124. package/web-ui/res/json5.min.js +1 -1
  125. package/web-ui/res/vue.global.prod.js +13 -13
  126. package/web-ui/res/vue.runtime.global.prod.js +7 -7
  127. package/web-ui/res/web-ui-render.precompiled.js +7666 -7576
  128. package/web-ui/session-helpers.mjs +602 -602
  129. package/web-ui/source-bundle.cjs +305 -305
  130. package/web-ui/styles/base-theme.css +291 -291
  131. package/web-ui/styles/bridge-pool.css +266 -266
  132. package/web-ui/styles/controls-forms.css +532 -439
  133. package/web-ui/styles/dashboard.css +438 -438
  134. package/web-ui/styles/docs-panel.css +245 -245
  135. package/web-ui/styles/feedback.css +108 -108
  136. package/web-ui/styles/health-check-dialog.css +144 -144
  137. package/web-ui/styles/layout-shell.css +711 -711
  138. package/web-ui/styles/modals-core.css +499 -499
  139. package/web-ui/styles/navigation-panels.css +399 -399
  140. package/web-ui/styles/openclaw-structured.css +616 -616
  141. package/web-ui/styles/plugins-panel.css +564 -564
  142. package/web-ui/styles/responsive.css +501 -501
  143. package/web-ui/styles/sessions-list.css +683 -683
  144. package/web-ui/styles/sessions-preview.css +407 -407
  145. package/web-ui/styles/sessions-toolbar-trash.css +518 -518
  146. package/web-ui/styles/sessions-usage.css +849 -849
  147. package/web-ui/styles/settings-panel.css +419 -419
  148. package/web-ui/styles/skills-list.css +305 -305
  149. package/web-ui/styles/skills-market.css +723 -723
  150. package/web-ui/styles/task-orchestration.css +822 -822
  151. package/web-ui/styles/titles-cards.css +486 -486
  152. package/web-ui/styles/trash-panel.css +90 -90
  153. package/web-ui/styles/webhook.css +115 -115
  154. package/web-ui/styles.css +24 -24
  155. package/web-ui.html +17 -17
@@ -1,28 +1,28 @@
1
- function createConcurrencyLimiter(maxConcurrency) {
2
- const max = Number.isFinite(Number(maxConcurrency))
3
- ? Math.max(1, Math.floor(Number(maxConcurrency)))
4
- : 8;
5
- let active = 0;
6
- const queue = [];
7
- const next = () => {
8
- const resolve = queue.shift();
9
- if (resolve) resolve();
10
- };
11
- return async (task) => {
12
- if (active >= max) {
13
- await new Promise((resolve) => queue.push(resolve));
14
- }
15
- active += 1;
16
- try {
17
- return await task();
18
- } finally {
19
- active -= 1;
20
- next();
21
- }
22
- };
23
- }
24
-
25
- module.exports = {
26
- createConcurrencyLimiter
27
- };
28
-
1
+ function createConcurrencyLimiter(maxConcurrency) {
2
+ const max = Number.isFinite(Number(maxConcurrency))
3
+ ? Math.max(1, Math.floor(Number(maxConcurrency)))
4
+ : 8;
5
+ let active = 0;
6
+ const queue = [];
7
+ const next = () => {
8
+ const resolve = queue.shift();
9
+ if (resolve) resolve();
10
+ };
11
+ return async (task) => {
12
+ if (active >= max) {
13
+ await new Promise((resolve) => queue.push(resolve));
14
+ }
15
+ active += 1;
16
+ try {
17
+ return await task();
18
+ } finally {
19
+ active -= 1;
20
+ next();
21
+ }
22
+ };
23
+ }
24
+
25
+ module.exports = {
26
+ createConcurrencyLimiter
27
+ };
28
+
@@ -1,304 +1,304 @@
1
- const { createConcurrencyLimiter } = require('./session-usage.concurrent');
2
- const { normalizeSessionModelList, createSessionModelsFileReader } = require('./session-usage.models');
3
-
4
- async function listSessionUsageCore(params = {}, deps = {}) {
5
- const {
6
- fs,
7
- listSessionBrowse,
8
- parseCodexSessionSummary,
9
- parseClaudeSessionSummary,
10
- parseCodeBuddySessionSummary,
11
- parseGeminiSessionSummary,
12
- MAX_SESSION_USAGE_LIST_SIZE,
13
- SESSION_BROWSE_SUMMARY_READ_BYTES
14
- } = deps;
15
-
16
- const source = params.source === 'codex' || params.source === 'claude' || params.source === 'gemini' || params.source === 'codebuddy'
17
- ? params.source
18
- : 'all';
19
- const rawLimit = Number(params.limit);
20
- const limit = Number.isFinite(rawLimit)
21
- ? Math.max(1, Math.min(rawLimit, MAX_SESSION_USAGE_LIST_SIZE))
22
- : MAX_SESSION_USAGE_LIST_SIZE;
23
-
24
- const sessions = await listSessionBrowse({
25
- source,
26
- limit,
27
- forceRefresh: !!params.forceRefresh
28
- });
29
- if (!Array.isArray(sessions) || sessions.length === 0) {
30
- return [];
31
- }
32
-
33
- const { readSessionModelsFromFile } = createSessionModelsFileReader(fs, {
34
- concurrency: 32,
35
- maxEntries: 1500,
36
- probeHeadBytes: 128 * 1024,
37
- probeTailBytes: 128 * 1024
38
- });
39
-
40
- // CPU/IO 优化策略(面向 2000 会话):
41
- // 1) 优先使用 listSessionBrowse 返回的 model/models(零 I/O)
42
- // 2) 仅当缺少模型名时才读取/解析文件(必要时全文件扫描)
43
- const limitNormalize = createConcurrencyLimiter(64);
44
- const normalizedSessions = await Promise.all(
45
- sessions.map((item) => limitNormalize(async () => {
46
- if (!item || typeof item !== 'object' || Array.isArray(item)) {
47
- return null;
48
- }
49
- const normalized = { ...item };
50
- delete normalized.__messageCountExact;
51
-
52
- const baseModels = normalizeSessionModelList([
53
- ...(Array.isArray(normalized.models) ? normalized.models : []),
54
- normalized.model,
55
- normalized.modelName,
56
- normalized.modelId
57
- ]);
58
- if (baseModels.length > 0) {
59
- normalized.models = baseModels;
60
- normalized.model = baseModels[0];
61
- return normalized;
62
- }
63
-
64
- const filePath = typeof normalized.filePath === 'string' ? normalized.filePath.trim() : '';
65
- if (!filePath) {
66
- return null;
67
- }
68
-
69
- // 快速路径:全文件正则扫描(并发 + 缓存)。只对“缺模型”的会话触发。
70
- const fullFileModels = await readSessionModelsFromFile(filePath);
71
- if (fullFileModels.length > 0) {
72
- normalized.models = fullFileModels;
73
- normalized.model = fullFileModels[0];
74
- return normalized;
75
- }
76
-
77
- // 兜底:摘要解析(可能补 provider 等字段)
78
- const summaryOptions = {
79
- summaryReadBytes: SESSION_BROWSE_SUMMARY_READ_BYTES,
80
- titleReadBytes: SESSION_BROWSE_SUMMARY_READ_BYTES
81
- };
82
- let summary = null;
83
- try {
84
- summary = normalized.source === 'claude'
85
- ? parseClaudeSessionSummary(filePath, summaryOptions)
86
- : (normalized.source === 'gemini'
87
- ? parseGeminiSessionSummary(filePath, summaryOptions)
88
- : (normalized.source === 'codebuddy'
89
- ? parseCodeBuddySessionSummary(filePath, summaryOptions)
90
- : parseCodexSessionSummary(filePath, summaryOptions)));
91
- } catch (_) {
92
- summary = null;
93
- }
94
- if (!summary || typeof summary !== 'object' || Array.isArray(summary)) {
95
- return null;
96
- }
97
- const summaryModels = normalizeSessionModelList([
98
- ...(Array.isArray(summary.models) ? summary.models : []),
99
- summary.model
100
- ]);
101
- if (summaryModels.length === 0) {
102
- return null;
103
- }
104
- normalized.models = summaryModels;
105
- normalized.model = summaryModels[0];
106
- if ((!normalized.provider || !String(normalized.provider).trim()) && typeof summary.provider === 'string' && summary.provider.trim()) {
107
- normalized.provider = summary.provider.trim();
108
- }
109
- return normalized;
110
- }))
111
- );
112
-
113
- return normalizedSessions.filter(Boolean);
114
- }
115
-
116
- function readNonNegativeInteger(value) {
117
- const numeric = Number(value);
118
- if (!Number.isFinite(numeric) || numeric < 0) {
119
- return 0;
120
- }
121
- return Math.floor(numeric);
122
- }
123
-
124
- function parseUsageExportDate(value, boundary) {
125
- if (value === undefined || value === null || value === '') {
126
- return null;
127
- }
128
- if (value instanceof Date) {
129
- const time = value.getTime();
130
- return Number.isFinite(time) ? time : NaN;
131
- }
132
- const raw = String(value).trim();
133
- if (!raw) {
134
- return null;
135
- }
136
- const dateOnly = raw.match(/^(\d{4})-(\d{2})-(\d{2})$/);
137
- if (dateOnly) {
138
- const year = Number(dateOnly[1]);
139
- const month = Number(dateOnly[2]) - 1;
140
- const day = Number(dateOnly[3]);
141
- const start = Date.UTC(year, month, day);
142
- const normalized = new Date(start);
143
- if (!Number.isFinite(start)
144
- || normalized.getUTCFullYear() !== year
145
- || normalized.getUTCMonth() !== month
146
- || normalized.getUTCDate() !== day) {
147
- return NaN;
148
- }
149
- return boundary === 'end' ? start + 24 * 60 * 60 * 1000 : start;
150
- }
151
- const parsed = Date.parse(raw);
152
- return Number.isFinite(parsed) ? parsed : NaN;
153
- }
154
-
155
- function formatUsageExportDay(timestamp) {
156
- return new Date(timestamp).toISOString().slice(0, 10);
157
- }
158
-
159
- function normalizeUsageExportFormat(value) {
160
- const normalized = typeof value === 'string' ? value.trim().toLowerCase() : '';
161
- return normalized === 'json' ? 'json' : 'csv';
162
- }
163
-
164
- function normalizeUsageExportModelFilters(params = {}) {
165
- const raw = [];
166
- const push = (value) => {
167
- if (Array.isArray(value)) {
168
- value.forEach(push);
169
- return;
170
- }
171
- if (typeof value !== 'string') {
172
- return;
173
- }
174
- value.split(',').forEach((item) => {
175
- const normalized = item.trim().toLowerCase();
176
- if (normalized) raw.push(normalized);
177
- });
178
- };
179
- push(params.model);
180
- push(params.models);
181
- // API-facing alias: callers may pass modelType when they reuse usage filters
182
- // outside the CLI flag surface.
183
- push(params.modelType);
184
- return [...new Set(raw)];
185
- }
186
-
187
- function sessionMatchesUsageExportModelFilters(session, filters) {
188
- if (!filters.length) {
189
- return true;
190
- }
191
- const models = [];
192
- if (typeof session.model === 'string') models.push(session.model);
193
- if (Array.isArray(session.models)) models.push(...session.models.filter(item => typeof item === 'string'));
194
- const normalizedModels = models.map(item => item.trim().toLowerCase()).filter(Boolean);
195
- return filters.some(filter => normalizedModels.some(model => model === filter || model.includes(filter)));
196
- }
197
-
198
- function escapeUsageCsvCell(value) {
199
- const raw = value === undefined || value === null ? '' : String(value);
200
- if (!/[",\n\r]/.test(raw)) {
201
- return raw;
202
- }
203
- return `"${raw.replace(/"/g, '""')}"`;
204
- }
205
-
206
- function serializeUsageExportRowsToCsv(rows) {
207
- const columns = ['date', 'model', 'tokens', 'sessions'];
208
- const lines = [columns.join(',')];
209
- for (const row of rows) {
210
- lines.push(columns.map(column => escapeUsageCsvCell(row[column])).join(','));
211
- }
212
- return lines.join('\r\n') + '\r\n';
213
- }
214
-
215
- function buildUsageExportRows(sessions = [], params = {}) {
216
- const fromTime = parseUsageExportDate(params.from ?? params.startDate, 'start');
217
- const toTime = parseUsageExportDate(params.to ?? params.endDate, 'end');
218
- if (Number.isNaN(fromTime)) {
219
- return { error: 'Invalid from date' };
220
- }
221
- if (Number.isNaN(toTime)) {
222
- return { error: 'Invalid to date' };
223
- }
224
- if (fromTime !== null && toTime !== null && fromTime >= toTime) {
225
- return { error: 'from date must be before to date' };
226
- }
227
-
228
- const modelFilters = normalizeUsageExportModelFilters(params);
229
- const groups = new Map();
230
- for (const session of Array.isArray(sessions) ? sessions : []) {
231
- if (!session || typeof session !== 'object' || Array.isArray(session)) {
232
- continue;
233
- }
234
- if (!sessionMatchesUsageExportModelFilters(session, modelFilters)) {
235
- continue;
236
- }
237
- const timestamp = Date.parse(session.updatedAt || session.createdAt || '');
238
- if (!Number.isFinite(timestamp)) {
239
- continue;
240
- }
241
- if (fromTime !== null && timestamp < fromTime) {
242
- continue;
243
- }
244
- if (toTime !== null && timestamp >= toTime) {
245
- continue;
246
- }
247
- const model = typeof session.model === 'string' && session.model.trim()
248
- ? session.model.trim()
249
- : (Array.isArray(session.models) && typeof session.models[0] === 'string' ? session.models[0].trim() : 'unknown');
250
- if (!model) {
251
- continue;
252
- }
253
- const date = formatUsageExportDay(timestamp);
254
- const key = `${date}\u0000${model}`;
255
- const current = groups.get(key) || { date, model, tokens: 0, sessions: 0 };
256
- current.tokens += readNonNegativeInteger(session.totalTokens ?? session.tokens);
257
- current.sessions += 1;
258
- groups.set(key, current);
259
- }
260
-
261
- const rows = [...groups.values()].sort((a, b) => {
262
- const dateCompare = a.date.localeCompare(b.date);
263
- if (dateCompare !== 0) return dateCompare;
264
- return a.model.localeCompare(b.model);
265
- });
266
- return { rows };
267
- }
268
-
269
- async function exportSessionUsageCore(params = {}, deps = {}) {
270
- const listSessionUsage = typeof deps.listSessionUsage === 'function'
271
- ? deps.listSessionUsage
272
- : (options) => listSessionUsageCore(options, deps);
273
- const sessions = Array.isArray(params.sessions)
274
- ? params.sessions
275
- : await listSessionUsage({
276
- source: params.source,
277
- limit: params.limit,
278
- forceRefresh: !!params.forceRefresh
279
- });
280
- const built = buildUsageExportRows(sessions, params);
281
- if (built.error) {
282
- return { error: built.error };
283
- }
284
- const format = normalizeUsageExportFormat(params.format);
285
- const rows = built.rows;
286
- const content = format === 'json'
287
- ? JSON.stringify({ rows }, null, 2) + '\n'
288
- : serializeUsageExportRowsToCsv(rows);
289
- const extension = format === 'json' ? 'json' : 'csv';
290
- return {
291
- format,
292
- mimeType: format === 'json' ? 'application/json' : 'text/csv',
293
- fileName: `usage-export.${extension}`,
294
- rows,
295
- content
296
- };
297
- }
298
-
299
- module.exports = {
300
- listSessionUsageCore,
301
- buildUsageExportRows,
302
- exportSessionUsageCore,
303
- serializeUsageExportRowsToCsv
304
- };
1
+ const { createConcurrencyLimiter } = require('./session-usage.concurrent');
2
+ const { normalizeSessionModelList, createSessionModelsFileReader } = require('./session-usage.models');
3
+
4
+ async function listSessionUsageCore(params = {}, deps = {}) {
5
+ const {
6
+ fs,
7
+ listSessionBrowse,
8
+ parseCodexSessionSummary,
9
+ parseClaudeSessionSummary,
10
+ parseCodeBuddySessionSummary,
11
+ parseGeminiSessionSummary,
12
+ MAX_SESSION_USAGE_LIST_SIZE,
13
+ SESSION_BROWSE_SUMMARY_READ_BYTES
14
+ } = deps;
15
+
16
+ const source = params.source === 'codex' || params.source === 'claude' || params.source === 'gemini' || params.source === 'codebuddy'
17
+ ? params.source
18
+ : 'all';
19
+ const rawLimit = Number(params.limit);
20
+ const limit = Number.isFinite(rawLimit)
21
+ ? Math.max(1, Math.min(rawLimit, MAX_SESSION_USAGE_LIST_SIZE))
22
+ : MAX_SESSION_USAGE_LIST_SIZE;
23
+
24
+ const sessions = await listSessionBrowse({
25
+ source,
26
+ limit,
27
+ forceRefresh: !!params.forceRefresh
28
+ });
29
+ if (!Array.isArray(sessions) || sessions.length === 0) {
30
+ return [];
31
+ }
32
+
33
+ const { readSessionModelsFromFile } = createSessionModelsFileReader(fs, {
34
+ concurrency: 32,
35
+ maxEntries: 1500,
36
+ probeHeadBytes: 128 * 1024,
37
+ probeTailBytes: 128 * 1024
38
+ });
39
+
40
+ // CPU/IO 优化策略(面向 2000 会话):
41
+ // 1) 优先使用 listSessionBrowse 返回的 model/models(零 I/O)
42
+ // 2) 仅当缺少模型名时才读取/解析文件(必要时全文件扫描)
43
+ const limitNormalize = createConcurrencyLimiter(64);
44
+ const normalizedSessions = await Promise.all(
45
+ sessions.map((item) => limitNormalize(async () => {
46
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
47
+ return null;
48
+ }
49
+ const normalized = { ...item };
50
+ delete normalized.__messageCountExact;
51
+
52
+ const baseModels = normalizeSessionModelList([
53
+ ...(Array.isArray(normalized.models) ? normalized.models : []),
54
+ normalized.model,
55
+ normalized.modelName,
56
+ normalized.modelId
57
+ ]);
58
+ if (baseModels.length > 0) {
59
+ normalized.models = baseModels;
60
+ normalized.model = baseModels[0];
61
+ return normalized;
62
+ }
63
+
64
+ const filePath = typeof normalized.filePath === 'string' ? normalized.filePath.trim() : '';
65
+ if (!filePath) {
66
+ return null;
67
+ }
68
+
69
+ // 快速路径:全文件正则扫描(并发 + 缓存)。只对“缺模型”的会话触发。
70
+ const fullFileModels = await readSessionModelsFromFile(filePath);
71
+ if (fullFileModels.length > 0) {
72
+ normalized.models = fullFileModels;
73
+ normalized.model = fullFileModels[0];
74
+ return normalized;
75
+ }
76
+
77
+ // 兜底:摘要解析(可能补 provider 等字段)
78
+ const summaryOptions = {
79
+ summaryReadBytes: SESSION_BROWSE_SUMMARY_READ_BYTES,
80
+ titleReadBytes: SESSION_BROWSE_SUMMARY_READ_BYTES
81
+ };
82
+ let summary = null;
83
+ try {
84
+ summary = normalized.source === 'claude'
85
+ ? parseClaudeSessionSummary(filePath, summaryOptions)
86
+ : (normalized.source === 'gemini'
87
+ ? parseGeminiSessionSummary(filePath, summaryOptions)
88
+ : (normalized.source === 'codebuddy'
89
+ ? parseCodeBuddySessionSummary(filePath, summaryOptions)
90
+ : parseCodexSessionSummary(filePath, summaryOptions)));
91
+ } catch (_) {
92
+ summary = null;
93
+ }
94
+ if (!summary || typeof summary !== 'object' || Array.isArray(summary)) {
95
+ return null;
96
+ }
97
+ const summaryModels = normalizeSessionModelList([
98
+ ...(Array.isArray(summary.models) ? summary.models : []),
99
+ summary.model
100
+ ]);
101
+ if (summaryModels.length === 0) {
102
+ return null;
103
+ }
104
+ normalized.models = summaryModels;
105
+ normalized.model = summaryModels[0];
106
+ if ((!normalized.provider || !String(normalized.provider).trim()) && typeof summary.provider === 'string' && summary.provider.trim()) {
107
+ normalized.provider = summary.provider.trim();
108
+ }
109
+ return normalized;
110
+ }))
111
+ );
112
+
113
+ return normalizedSessions.filter(Boolean);
114
+ }
115
+
116
+ function readNonNegativeInteger(value) {
117
+ const numeric = Number(value);
118
+ if (!Number.isFinite(numeric) || numeric < 0) {
119
+ return 0;
120
+ }
121
+ return Math.floor(numeric);
122
+ }
123
+
124
+ function parseUsageExportDate(value, boundary) {
125
+ if (value === undefined || value === null || value === '') {
126
+ return null;
127
+ }
128
+ if (value instanceof Date) {
129
+ const time = value.getTime();
130
+ return Number.isFinite(time) ? time : NaN;
131
+ }
132
+ const raw = String(value).trim();
133
+ if (!raw) {
134
+ return null;
135
+ }
136
+ const dateOnly = raw.match(/^(\d{4})-(\d{2})-(\d{2})$/);
137
+ if (dateOnly) {
138
+ const year = Number(dateOnly[1]);
139
+ const month = Number(dateOnly[2]) - 1;
140
+ const day = Number(dateOnly[3]);
141
+ const start = Date.UTC(year, month, day);
142
+ const normalized = new Date(start);
143
+ if (!Number.isFinite(start)
144
+ || normalized.getUTCFullYear() !== year
145
+ || normalized.getUTCMonth() !== month
146
+ || normalized.getUTCDate() !== day) {
147
+ return NaN;
148
+ }
149
+ return boundary === 'end' ? start + 24 * 60 * 60 * 1000 : start;
150
+ }
151
+ const parsed = Date.parse(raw);
152
+ return Number.isFinite(parsed) ? parsed : NaN;
153
+ }
154
+
155
+ function formatUsageExportDay(timestamp) {
156
+ return new Date(timestamp).toISOString().slice(0, 10);
157
+ }
158
+
159
+ function normalizeUsageExportFormat(value) {
160
+ const normalized = typeof value === 'string' ? value.trim().toLowerCase() : '';
161
+ return normalized === 'json' ? 'json' : 'csv';
162
+ }
163
+
164
+ function normalizeUsageExportModelFilters(params = {}) {
165
+ const raw = [];
166
+ const push = (value) => {
167
+ if (Array.isArray(value)) {
168
+ value.forEach(push);
169
+ return;
170
+ }
171
+ if (typeof value !== 'string') {
172
+ return;
173
+ }
174
+ value.split(',').forEach((item) => {
175
+ const normalized = item.trim().toLowerCase();
176
+ if (normalized) raw.push(normalized);
177
+ });
178
+ };
179
+ push(params.model);
180
+ push(params.models);
181
+ // API-facing alias: callers may pass modelType when they reuse usage filters
182
+ // outside the CLI flag surface.
183
+ push(params.modelType);
184
+ return [...new Set(raw)];
185
+ }
186
+
187
+ function sessionMatchesUsageExportModelFilters(session, filters) {
188
+ if (!filters.length) {
189
+ return true;
190
+ }
191
+ const models = [];
192
+ if (typeof session.model === 'string') models.push(session.model);
193
+ if (Array.isArray(session.models)) models.push(...session.models.filter(item => typeof item === 'string'));
194
+ const normalizedModels = models.map(item => item.trim().toLowerCase()).filter(Boolean);
195
+ return filters.some(filter => normalizedModels.some(model => model === filter || model.includes(filter)));
196
+ }
197
+
198
+ function escapeUsageCsvCell(value) {
199
+ const raw = value === undefined || value === null ? '' : String(value);
200
+ if (!/[",\n\r]/.test(raw)) {
201
+ return raw;
202
+ }
203
+ return `"${raw.replace(/"/g, '""')}"`;
204
+ }
205
+
206
+ function serializeUsageExportRowsToCsv(rows) {
207
+ const columns = ['date', 'model', 'tokens', 'sessions'];
208
+ const lines = [columns.join(',')];
209
+ for (const row of rows) {
210
+ lines.push(columns.map(column => escapeUsageCsvCell(row[column])).join(','));
211
+ }
212
+ return lines.join('\r\n') + '\r\n';
213
+ }
214
+
215
+ function buildUsageExportRows(sessions = [], params = {}) {
216
+ const fromTime = parseUsageExportDate(params.from ?? params.startDate, 'start');
217
+ const toTime = parseUsageExportDate(params.to ?? params.endDate, 'end');
218
+ if (Number.isNaN(fromTime)) {
219
+ return { error: 'Invalid from date' };
220
+ }
221
+ if (Number.isNaN(toTime)) {
222
+ return { error: 'Invalid to date' };
223
+ }
224
+ if (fromTime !== null && toTime !== null && fromTime >= toTime) {
225
+ return { error: 'from date must be before to date' };
226
+ }
227
+
228
+ const modelFilters = normalizeUsageExportModelFilters(params);
229
+ const groups = new Map();
230
+ for (const session of Array.isArray(sessions) ? sessions : []) {
231
+ if (!session || typeof session !== 'object' || Array.isArray(session)) {
232
+ continue;
233
+ }
234
+ if (!sessionMatchesUsageExportModelFilters(session, modelFilters)) {
235
+ continue;
236
+ }
237
+ const timestamp = Date.parse(session.updatedAt || session.createdAt || '');
238
+ if (!Number.isFinite(timestamp)) {
239
+ continue;
240
+ }
241
+ if (fromTime !== null && timestamp < fromTime) {
242
+ continue;
243
+ }
244
+ if (toTime !== null && timestamp >= toTime) {
245
+ continue;
246
+ }
247
+ const model = typeof session.model === 'string' && session.model.trim()
248
+ ? session.model.trim()
249
+ : (Array.isArray(session.models) && typeof session.models[0] === 'string' ? session.models[0].trim() : 'unknown');
250
+ if (!model) {
251
+ continue;
252
+ }
253
+ const date = formatUsageExportDay(timestamp);
254
+ const key = `${date}\u0000${model}`;
255
+ const current = groups.get(key) || { date, model, tokens: 0, sessions: 0 };
256
+ current.tokens += readNonNegativeInteger(session.totalTokens ?? session.tokens);
257
+ current.sessions += 1;
258
+ groups.set(key, current);
259
+ }
260
+
261
+ const rows = [...groups.values()].sort((a, b) => {
262
+ const dateCompare = a.date.localeCompare(b.date);
263
+ if (dateCompare !== 0) return dateCompare;
264
+ return a.model.localeCompare(b.model);
265
+ });
266
+ return { rows };
267
+ }
268
+
269
+ async function exportSessionUsageCore(params = {}, deps = {}) {
270
+ const listSessionUsage = typeof deps.listSessionUsage === 'function'
271
+ ? deps.listSessionUsage
272
+ : (options) => listSessionUsageCore(options, deps);
273
+ const sessions = Array.isArray(params.sessions)
274
+ ? params.sessions
275
+ : await listSessionUsage({
276
+ source: params.source,
277
+ limit: params.limit,
278
+ forceRefresh: !!params.forceRefresh
279
+ });
280
+ const built = buildUsageExportRows(sessions, params);
281
+ if (built.error) {
282
+ return { error: built.error };
283
+ }
284
+ const format = normalizeUsageExportFormat(params.format);
285
+ const rows = built.rows;
286
+ const content = format === 'json'
287
+ ? JSON.stringify({ rows }, null, 2) + '\n'
288
+ : serializeUsageExportRowsToCsv(rows);
289
+ const extension = format === 'json' ? 'json' : 'csv';
290
+ return {
291
+ format,
292
+ mimeType: format === 'json' ? 'application/json' : 'text/csv',
293
+ fileName: `usage-export.${extension}`,
294
+ rows,
295
+ content
296
+ };
297
+ }
298
+
299
+ module.exports = {
300
+ listSessionUsageCore,
301
+ buildUsageExportRows,
302
+ exportSessionUsageCore,
303
+ serializeUsageExportRowsToCsv
304
+ };