codexmate 0.0.41 → 0.0.42
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.
- package/README.md +168 -168
- package/README.zh.md +168 -168
- package/cli/agents-files.js +230 -230
- package/cli/analytics-export-args.js +68 -68
- package/cli/archive-helpers.js +453 -453
- package/cli/auth-profiles.js +375 -375
- package/cli/builtin-proxy.js +2144 -2144
- package/cli/claude-proxy.js +1022 -1022
- package/cli/config-bootstrap.js +407 -407
- package/cli/config-health.js +454 -454
- package/cli/doctor-core.js +903 -903
- package/cli/import-skills-url.js +356 -356
- package/cli/local-bridge.js +556 -556
- package/cli/openai-bridge.js +1984 -1984
- package/cli/openclaw-config.js +629 -629
- package/cli/session-convert-args.js +69 -69
- package/cli/session-convert-io.js +82 -82
- package/cli/session-convert.js +150 -150
- package/cli/session-usage.concurrent.js +28 -28
- package/cli/session-usage.js +304 -304
- package/cli/session-usage.models.js +176 -176
- package/cli/skills.js +1141 -1141
- package/cli/update.js +171 -171
- package/cli/zip-commands.js +510 -510
- package/cli.js +16494 -16458
- package/lib/automation.js +404 -404
- package/lib/cli-file-utils.js +151 -151
- package/lib/cli-models-utils.js +440 -440
- package/lib/cli-network-utils.js +190 -190
- package/lib/cli-path-utils.js +85 -85
- package/lib/cli-session-utils.js +121 -121
- package/lib/cli-sessions.js +427 -427
- package/lib/cli-utils.js +155 -155
- package/lib/cli-webhook.js +154 -154
- package/lib/download-artifacts.js +92 -92
- package/lib/mcp-stdio.js +453 -453
- package/lib/task-orchestrator.js +869 -869
- package/lib/text-diff.js +303 -303
- package/lib/win-tray.js +119 -119
- package/lib/workflow-engine.js +340 -340
- package/package.json +77 -77
- package/plugins/README.md +20 -20
- package/plugins/README.zh-CN.md +20 -20
- package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
- package/plugins/prompt-templates/computed.mjs +311 -311
- package/plugins/prompt-templates/index.mjs +8 -8
- package/plugins/prompt-templates/manifest.mjs +18 -18
- package/plugins/prompt-templates/methods.mjs +553 -553
- package/plugins/prompt-templates/overview.mjs +91 -91
- package/plugins/prompt-templates/ownership.mjs +19 -19
- package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
- package/plugins/prompt-templates/storage.mjs +64 -64
- package/plugins/registry.mjs +16 -16
- package/web-ui/app.js +695 -695
- package/web-ui/index.html +37 -37
- package/web-ui/logic.agents-diff.mjs +386 -386
- package/web-ui/logic.claude.mjs +172 -172
- package/web-ui/logic.codex.mjs +69 -69
- package/web-ui/logic.mjs +5 -5
- package/web-ui/logic.runtime.mjs +128 -128
- package/web-ui/logic.session-convert.mjs +70 -70
- package/web-ui/logic.sessions.mjs +782 -782
- package/web-ui/modules/api.mjs +90 -90
- package/web-ui/modules/app.computed.dashboard.mjs +252 -252
- package/web-ui/modules/app.computed.index.mjs +17 -17
- package/web-ui/modules/app.computed.main-tabs.mjs +214 -214
- package/web-ui/modules/app.computed.session.mjs +876 -876
- package/web-ui/modules/app.constants.mjs +15 -15
- package/web-ui/modules/app.methods.agents.mjs +651 -651
- package/web-ui/modules/app.methods.claude-config.mjs +412 -412
- package/web-ui/modules/app.methods.codex-config.mjs +869 -869
- package/web-ui/modules/app.methods.index.mjs +96 -96
- package/web-ui/modules/app.methods.install.mjs +205 -205
- package/web-ui/modules/app.methods.navigation.mjs +804 -804
- package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
- package/web-ui/modules/app.methods.openclaw-editing.mjs +420 -420
- package/web-ui/modules/app.methods.openclaw-persist.mjs +375 -375
- package/web-ui/modules/app.methods.providers.mjs +601 -601
- package/web-ui/modules/app.methods.runtime.mjs +420 -420
- package/web-ui/modules/app.methods.session-actions.mjs +617 -591
- package/web-ui/modules/app.methods.session-browser.mjs +1018 -1018
- package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
- package/web-ui/modules/app.methods.session-trash.mjs +468 -468
- package/web-ui/modules/app.methods.startup-claude.mjs +554 -554
- package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
- package/web-ui/modules/app.methods.tool-config-permissions.mjs +87 -87
- package/web-ui/modules/app.methods.webhook.mjs +87 -87
- package/web-ui/modules/config-mode.computed.mjs +124 -124
- package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
- package/web-ui/modules/i18n/locales/en.mjs +1141 -1140
- package/web-ui/modules/i18n/locales/ja.mjs +1132 -1130
- package/web-ui/modules/i18n/locales/vi.mjs +239 -239
- package/web-ui/modules/i18n/locales/zh.mjs +1144 -1143
- package/web-ui/modules/i18n.dict.mjs +14 -14
- package/web-ui/modules/i18n.mjs +111 -111
- package/web-ui/modules/plugins.computed.mjs +3 -3
- package/web-ui/modules/plugins.methods.mjs +3 -3
- package/web-ui/modules/plugins.storage.mjs +11 -11
- package/web-ui/modules/provider-url-display.mjs +17 -17
- package/web-ui/modules/sessions-filters-url.mjs +138 -138
- package/web-ui/modules/skills.computed.mjs +107 -107
- package/web-ui/modules/skills.methods.mjs +513 -513
- package/web-ui/partials/index/layout-footer.html +13 -13
- package/web-ui/partials/index/layout-header.html +478 -478
- package/web-ui/partials/index/modal-config-template-agents.html +185 -185
- package/web-ui/partials/index/modal-confirm-toast.html +32 -32
- package/web-ui/partials/index/modal-health-check.html +45 -45
- package/web-ui/partials/index/modal-openclaw-config.html +344 -344
- package/web-ui/partials/index/modal-skills.html +200 -200
- package/web-ui/partials/index/modal-webhook.html +42 -42
- package/web-ui/partials/index/modals-basic.html +263 -263
- package/web-ui/partials/index/panel-config-claude.html +187 -187
- package/web-ui/partials/index/panel-config-codex.html +205 -205
- package/web-ui/partials/index/panel-config-openclaw.html +89 -89
- package/web-ui/partials/index/panel-dashboard.html +171 -171
- package/web-ui/partials/index/panel-docs.html +114 -114
- package/web-ui/partials/index/panel-market.html +104 -104
- package/web-ui/partials/index/panel-orchestration.html +391 -391
- package/web-ui/partials/index/panel-plugins.html +253 -253
- package/web-ui/partials/index/panel-sessions.html +325 -319
- package/web-ui/partials/index/panel-settings.html +181 -181
- package/web-ui/partials/index/panel-trash.html +82 -82
- package/web-ui/partials/index/panel-usage.html +181 -181
- package/web-ui/res/json5.min.js +1 -1
- package/web-ui/res/vue.global.prod.js +13 -13
- package/web-ui/res/vue.runtime.global.prod.js +7 -7
- package/web-ui/res/web-ui-render.precompiled.js +7671 -7666
- package/web-ui/session-helpers.mjs +602 -602
- package/web-ui/source-bundle.cjs +305 -305
- package/web-ui/styles/base-theme.css +291 -291
- package/web-ui/styles/bridge-pool.css +266 -266
- package/web-ui/styles/controls-forms.css +532 -532
- package/web-ui/styles/dashboard.css +438 -438
- package/web-ui/styles/docs-panel.css +245 -245
- package/web-ui/styles/feedback.css +108 -108
- package/web-ui/styles/health-check-dialog.css +144 -144
- package/web-ui/styles/layout-shell.css +711 -711
- package/web-ui/styles/modals-core.css +499 -499
- package/web-ui/styles/navigation-panels.css +399 -399
- package/web-ui/styles/openclaw-structured.css +616 -616
- package/web-ui/styles/plugins-panel.css +564 -564
- package/web-ui/styles/responsive.css +501 -501
- package/web-ui/styles/sessions-list.css +683 -683
- package/web-ui/styles/sessions-preview.css +407 -407
- package/web-ui/styles/sessions-toolbar-trash.css +518 -518
- package/web-ui/styles/sessions-usage.css +849 -849
- package/web-ui/styles/settings-panel.css +419 -419
- package/web-ui/styles/skills-list.css +305 -305
- package/web-ui/styles/skills-market.css +723 -723
- package/web-ui/styles/task-orchestration.css +822 -822
- package/web-ui/styles/titles-cards.css +486 -486
- package/web-ui/styles/trash-panel.css +90 -90
- package/web-ui/styles/webhook.css +115 -115
- package/web-ui/styles.css +24 -24
- 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
|
+
|
package/cli/session-usage.js
CHANGED
|
@@ -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
|
+
};
|