@aiusage/cli 0.2.0 → 0.3.0
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 +13 -6
- package/dist/cli.js +202 -101
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,16 +32,19 @@ aiusage --help
|
|
|
32
32
|
Local usage report. No cloud upload required.
|
|
33
33
|
|
|
34
34
|
```bash
|
|
35
|
-
aiusage report
|
|
36
|
-
aiusage report --range 1m
|
|
37
|
-
aiusage report --range 3m
|
|
38
|
-
aiusage report --range all
|
|
39
|
-
aiusage report --
|
|
35
|
+
aiusage report # default: last 7 days, English, compact
|
|
36
|
+
aiusage report --range 1m # last 30 days
|
|
37
|
+
aiusage report --range 3m # last 90 days
|
|
38
|
+
aiusage report --range all # all history
|
|
39
|
+
aiusage report --detail # show all columns, top models, pricing notes
|
|
40
|
+
aiusage report --lang zh # Chinese output
|
|
41
|
+
aiusage report --no-emoji # disable emoji in title
|
|
42
|
+
aiusage report --json # JSON output
|
|
40
43
|
```
|
|
41
44
|
|
|
42
45
|
Reads data from `~/.claude/projects` (Claude Code) and `~/.codex` (Codex).
|
|
43
46
|
|
|
44
|
-
|
|
47
|
+
**Compact mode** (default) shows Sources and Daily tables with merged Cache column and 2-decimal cost. **Detail mode** (`--detail`) expands all columns (CacheRead, CacheWrite, Reasoning), adds Top Models and Pricing Notes sections, and shows 4-decimal cost.
|
|
45
48
|
|
|
46
49
|
### scan
|
|
47
50
|
|
|
@@ -122,11 +125,15 @@ aiusage doctor
|
|
|
122
125
|
Manage local settings.
|
|
123
126
|
|
|
124
127
|
```bash
|
|
128
|
+
aiusage config set lang zh # default language: en or zh
|
|
129
|
+
aiusage config set emoji false # disable emoji in report title
|
|
125
130
|
aiusage config set device.alias "MacBook Pro 工作机"
|
|
126
131
|
aiusage config set privacy.projectVisibility masked
|
|
127
132
|
aiusage config set project.alias /Users/me/Projects/MyApp MyApp
|
|
128
133
|
```
|
|
129
134
|
|
|
135
|
+
CLI flags (`--lang`, `--no-emoji`) override config values for a single run.
|
|
136
|
+
|
|
130
137
|
## Configuration
|
|
131
138
|
|
|
132
139
|
Config file: `~/.aiusage/config.json`
|
package/dist/cli.js
CHANGED
|
@@ -394,7 +394,7 @@ async function scanGeminiDates(targetDates, baseDir, projectAliases) {
|
|
|
394
394
|
}
|
|
395
395
|
function extractTokens(msg) {
|
|
396
396
|
if (msg.tokens) {
|
|
397
|
-
let
|
|
397
|
+
let t2 = msg.tokens, cached2 = t2.cached ?? 0, input = Math.max((t2.input ?? 0) - cached2, 0), thoughts = t2.thoughts ?? 0, output = Math.max((t2.output ?? 0) - thoughts, 0);
|
|
398
398
|
return { input, cached: cached2, cacheWrite: 0, output, reasoning: thoughts };
|
|
399
399
|
}
|
|
400
400
|
let u = msg.usage ?? msg.usageMetadata;
|
|
@@ -931,72 +931,6 @@ async function buildLocalReport(range, options = {}) {
|
|
|
931
931
|
pricingWarnings: [...pricingWarnings].sort()
|
|
932
932
|
};
|
|
933
933
|
}
|
|
934
|
-
function renderLocalReport(report) {
|
|
935
|
-
let lines = [];
|
|
936
|
-
if (lines.push("\u{1F4CA} AIUsage \u672C\u5730\u7EDF\u8BA1"), lines.push(`\u8303\u56F4 ${report.rangeLabel}`), lines.push(`\u65F6\u95F4 ${report.startDate ?? "-"} ~ ${report.endDate ?? "-"}`), lines.push(`\u8BF7\u6C42\u5929\u6570 ${fmtInt(report.requestedDays)}`), lines.push(`\u6709\u6548\u5929\u6570 ${fmtInt(report.daysWithData)}`), lines.push(""), report.requestedDays === 0 || report.daysWithData === 0)
|
|
937
|
-
return lines.push("\u8BE5\u8303\u56F4\u6682\u65E0\u672C\u5730 token \u6570\u636E\u3002"), lines.join(`
|
|
938
|
-
`);
|
|
939
|
-
if (lines.push("Summary"), lines.push(renderTable(
|
|
940
|
-
["Metric", "Value"],
|
|
941
|
-
[
|
|
942
|
-
["Events", fmtInt(report.totals.eventCount)],
|
|
943
|
-
["Input", fmtToken(report.totals.inputTokens)],
|
|
944
|
-
["CacheRead", fmtToken(report.totals.cachedInputTokens)],
|
|
945
|
-
["CacheWrite", fmtToken(report.totals.cacheWriteTokens)],
|
|
946
|
-
["Output", fmtToken(report.totals.outputTokens)],
|
|
947
|
-
["Reasoning", fmtToken(report.totals.reasoningOutputTokens)],
|
|
948
|
-
["TotalTokens", fmtToken(report.totals.totalTokens)],
|
|
949
|
-
["CostUsd", fmtUsd(report.totals.estimatedCostUsd)]
|
|
950
|
-
],
|
|
951
|
-
["left", "right"]
|
|
952
|
-
)), lines.push(""), lines.push("By Source"), lines.push(renderTable(
|
|
953
|
-
["Source", "Events", "Input", "CacheRead", "CacheWrite", "Output", "Reasoning", "Total", "Cost"],
|
|
954
|
-
report.bySource.map((item) => [
|
|
955
|
-
item.source,
|
|
956
|
-
fmtInt(item.eventCount),
|
|
957
|
-
fmtToken(item.inputTokens),
|
|
958
|
-
fmtToken(item.cachedInputTokens),
|
|
959
|
-
fmtToken(item.cacheWriteTokens),
|
|
960
|
-
fmtToken(item.outputTokens),
|
|
961
|
-
fmtToken(item.reasoningOutputTokens),
|
|
962
|
-
fmtToken(item.totalTokens),
|
|
963
|
-
fmtUsd(item.estimatedCostUsd)
|
|
964
|
-
]),
|
|
965
|
-
["left", "right", "right", "right", "right", "right", "right", "right", "right"]
|
|
966
|
-
)), lines.push(""), lines.push("By Day"), lines.push(renderTable(
|
|
967
|
-
["Date", "Events", "Input", "CacheRead", "CacheWrite", "Output", "Reasoning", "Total", "Cost"],
|
|
968
|
-
report.daily.map((item) => [
|
|
969
|
-
item.usageDate,
|
|
970
|
-
fmtInt(item.eventCount),
|
|
971
|
-
fmtToken(item.inputTokens),
|
|
972
|
-
fmtToken(item.cachedInputTokens),
|
|
973
|
-
fmtToken(item.cacheWriteTokens),
|
|
974
|
-
fmtToken(item.outputTokens),
|
|
975
|
-
fmtToken(item.reasoningOutputTokens),
|
|
976
|
-
fmtToken(item.totalTokens),
|
|
977
|
-
fmtUsd(item.estimatedCostUsd)
|
|
978
|
-
]),
|
|
979
|
-
["left", "right", "right", "right", "right", "right", "right", "right", "right"]
|
|
980
|
-
)), lines.push(""), lines.push("Top Models"), lines.push(renderTable(
|
|
981
|
-
["Model", "Source", "Events", "Total", "Input", "Output", "Cost"],
|
|
982
|
-
report.byModel.slice(0, 12).map((item) => [
|
|
983
|
-
item.model,
|
|
984
|
-
item.source,
|
|
985
|
-
fmtInt(item.eventCount),
|
|
986
|
-
fmtToken(item.totalTokens),
|
|
987
|
-
fmtToken(item.inputTokens),
|
|
988
|
-
fmtToken(item.outputTokens),
|
|
989
|
-
fmtUsd(item.estimatedCostUsd)
|
|
990
|
-
]),
|
|
991
|
-
["left", "left", "right", "right", "right", "right", "right"]
|
|
992
|
-
)), report.pricingWarnings.length > 0) {
|
|
993
|
-
lines.push(""), lines.push("Pricing Notes");
|
|
994
|
-
for (let warning of report.pricingWarnings)
|
|
995
|
-
lines.push(`- ${warning}`);
|
|
996
|
-
}
|
|
997
|
-
return lines.join(`
|
|
998
|
-
`);
|
|
999
|
-
}
|
|
1000
934
|
function parseReportRange(value) {
|
|
1001
935
|
if (value === void 0 || value === !0) return "7d";
|
|
1002
936
|
if (value === "7d" || value === "1m" || value === "3m" || value === "all") return value;
|
|
@@ -1155,38 +1089,6 @@ function createEmptyTotals2() {
|
|
|
1155
1089
|
function mergeTotals(target, source) {
|
|
1156
1090
|
return target.eventCount += source.eventCount, target.inputTokens += source.inputTokens, target.cachedInputTokens += source.cachedInputTokens, target.cacheWriteTokens += source.cacheWriteTokens, target.outputTokens += source.outputTokens, target.reasoningOutputTokens += source.reasoningOutputTokens, target.totalTokens += source.totalTokens, target.estimatedCostUsd += source.estimatedCostUsd, target;
|
|
1157
1091
|
}
|
|
1158
|
-
function fmtInt(value) {
|
|
1159
|
-
return value.toLocaleString("en-US");
|
|
1160
|
-
}
|
|
1161
|
-
function fmtToken(value) {
|
|
1162
|
-
let abs = Math.abs(value);
|
|
1163
|
-
return abs < 1e3 ? fmtInt(value) : abs < 1e6 ? formatCompact(value / 1e3, "K") : abs < 1e9 ? formatCompact(value / 1e6, "M") : formatCompact(value / 1e9, "B");
|
|
1164
|
-
}
|
|
1165
|
-
function fmtUsd(value) {
|
|
1166
|
-
return `$${value.toFixed(4)}`;
|
|
1167
|
-
}
|
|
1168
|
-
function formatCompact(value, suffix) {
|
|
1169
|
-
let digits = Math.abs(value) >= 100 ? 1 : 2;
|
|
1170
|
-
return `${value.toFixed(digits).replace(/\.0+$/, "").replace(/(\.\d*[1-9])0+$/, "$1")}${suffix}`;
|
|
1171
|
-
}
|
|
1172
|
-
function renderTable(headers, rows, aligns) {
|
|
1173
|
-
if (rows.length === 0)
|
|
1174
|
-
return "(\u6682\u65E0\u6570\u636E)";
|
|
1175
|
-
let widths = headers.map(
|
|
1176
|
-
(header, index) => Math.max(
|
|
1177
|
-
header.length,
|
|
1178
|
-
...rows.map((row) => truncate(row[index] ?? "").length)
|
|
1179
|
-
)
|
|
1180
|
-
), formatRow = (row) => row.map((value, index) => {
|
|
1181
|
-
let cell = truncate(value ?? "", 42);
|
|
1182
|
-
return aligns[index] === "right" ? cell.padStart(widths[index], " ") : cell.padEnd(widths[index], " ");
|
|
1183
|
-
}).join(" "), divider = widths.map((width) => "-".repeat(width)).join(" ");
|
|
1184
|
-
return [formatRow(headers), divider, ...rows.map(formatRow)].join(`
|
|
1185
|
-
`);
|
|
1186
|
-
}
|
|
1187
|
-
function truncate(value, maxLength = 42) {
|
|
1188
|
-
return value.length <= maxLength ? value : `${value.slice(0, maxLength - 1)}\u2026`;
|
|
1189
|
-
}
|
|
1190
1092
|
var CLAUDE_PRICING = {
|
|
1191
1093
|
"claude-opus-4-6": { input: 5, cache_write_5m: 6.25, cache_write_1h: 10, cache_read: 0.5, output: 25 },
|
|
1192
1094
|
"claude-opus-4-5": { input: 5, cache_write_5m: 6.25, cache_write_1h: 10, cache_read: 0.5, output: 25 },
|
|
@@ -1295,6 +1197,190 @@ function resolveModel(model, pricingTable) {
|
|
|
1295
1197
|
return null;
|
|
1296
1198
|
}
|
|
1297
1199
|
|
|
1200
|
+
// src/i18n.ts
|
|
1201
|
+
var en = {
|
|
1202
|
+
reportTitle: "AIUsage Report",
|
|
1203
|
+
period: "Period",
|
|
1204
|
+
events: "Events",
|
|
1205
|
+
tokens: "Tokens",
|
|
1206
|
+
cost: "Cost",
|
|
1207
|
+
sources: "Sources",
|
|
1208
|
+
daily: "Daily",
|
|
1209
|
+
topModels: "Top Models",
|
|
1210
|
+
pricingNotes: "Pricing Notes",
|
|
1211
|
+
noData: "No token data in this range.",
|
|
1212
|
+
rangeLast7d: "Last 7 days",
|
|
1213
|
+
rangeLast1m: "Last 30 days",
|
|
1214
|
+
rangeLast3m: "Last 90 days",
|
|
1215
|
+
rangeAll: "All time",
|
|
1216
|
+
hdrSource: "Source",
|
|
1217
|
+
hdrDate: "Date",
|
|
1218
|
+
hdrModel: "Model",
|
|
1219
|
+
hdrEvents: "Events",
|
|
1220
|
+
hdrInput: "Input",
|
|
1221
|
+
hdrCache: "Cache",
|
|
1222
|
+
hdrCacheRead: "CacheRead",
|
|
1223
|
+
hdrCacheWrite: "CacheWrite",
|
|
1224
|
+
hdrOutput: "Output",
|
|
1225
|
+
hdrReasoning: "Reasoning",
|
|
1226
|
+
hdrTotal: "Total",
|
|
1227
|
+
hdrCost: "Cost"
|
|
1228
|
+
}, zh = {
|
|
1229
|
+
reportTitle: "AIUsage \u62A5\u544A",
|
|
1230
|
+
period: "\u65F6\u6BB5",
|
|
1231
|
+
events: "\u4E8B\u4EF6",
|
|
1232
|
+
tokens: "\u4EE4\u724C",
|
|
1233
|
+
cost: "\u8D39\u7528",
|
|
1234
|
+
sources: "\u6765\u6E90",
|
|
1235
|
+
daily: "\u6BCF\u65E5",
|
|
1236
|
+
topModels: "\u6A21\u578B\u6392\u884C",
|
|
1237
|
+
pricingNotes: "\u5B9A\u4EF7\u8BF4\u660E",
|
|
1238
|
+
noData: "\u8BE5\u8303\u56F4\u6682\u65E0 token \u6570\u636E\u3002",
|
|
1239
|
+
rangeLast7d: "\u6700\u8FD1 7 \u5929",
|
|
1240
|
+
rangeLast1m: "\u6700\u8FD1 30 \u5929",
|
|
1241
|
+
rangeLast3m: "\u6700\u8FD1 90 \u5929",
|
|
1242
|
+
rangeAll: "\u5168\u90E8\u5386\u53F2",
|
|
1243
|
+
hdrSource: "\u6765\u6E90",
|
|
1244
|
+
hdrDate: "\u65E5\u671F",
|
|
1245
|
+
hdrModel: "\u6A21\u578B",
|
|
1246
|
+
hdrEvents: "\u4E8B\u4EF6",
|
|
1247
|
+
hdrInput: "\u8F93\u5165",
|
|
1248
|
+
hdrCache: "\u7F13\u5B58",
|
|
1249
|
+
hdrCacheRead: "\u7F13\u5B58\u8BFB",
|
|
1250
|
+
hdrCacheWrite: "\u7F13\u5B58\u5199",
|
|
1251
|
+
hdrOutput: "\u8F93\u51FA",
|
|
1252
|
+
hdrReasoning: "\u63A8\u7406",
|
|
1253
|
+
hdrTotal: "\u5408\u8BA1",
|
|
1254
|
+
hdrCost: "\u8D39\u7528"
|
|
1255
|
+
}, tables = { en, zh };
|
|
1256
|
+
function t(lang) {
|
|
1257
|
+
return tables[lang];
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
// src/render.ts
|
|
1261
|
+
function renderReport(report, opts) {
|
|
1262
|
+
let s = t(opts.lang), lines = [], title = opts.emoji ? `\u{1F4CA} ${s.reportTitle}` : s.reportTitle;
|
|
1263
|
+
lines.push(title), lines.push("\u2500".repeat(title.length));
|
|
1264
|
+
let rangeLabel = getRangeLabel2(report.range, opts.lang), periodStart = report.startDate ? fmtDateShort(report.startDate) : "-", periodEnd = report.endDate ? fmtDateShort(report.endDate) : "-";
|
|
1265
|
+
if (lines.push(`${s.period.padEnd(10)}${rangeLabel} (${periodStart} \u2013 ${periodEnd})`), lines.push(`${s.events.padEnd(10)}${fmtInt(report.totals.eventCount)}`), lines.push(`${s.tokens.padEnd(10)}${fmtToken(report.totals.totalTokens)}`), lines.push(`${s.cost.padEnd(10)}${fmtUsd(report.totals.estimatedCostUsd, opts.detail)}`), report.daysWithData === 0)
|
|
1266
|
+
return lines.push(""), lines.push(s.noData), lines.join(`
|
|
1267
|
+
`);
|
|
1268
|
+
if (lines.push(""), lines.push(s.sources), opts.detail ? lines.push(renderTable(
|
|
1269
|
+
[s.hdrSource, s.hdrEvents, s.hdrInput, s.hdrCacheRead, s.hdrCacheWrite, s.hdrOutput, s.hdrReasoning, s.hdrTotal, s.hdrCost],
|
|
1270
|
+
report.bySource.map((r) => [
|
|
1271
|
+
r.source,
|
|
1272
|
+
fmtInt(r.eventCount),
|
|
1273
|
+
fmtToken(r.inputTokens),
|
|
1274
|
+
fmtToken(r.cachedInputTokens),
|
|
1275
|
+
fmtToken(r.cacheWriteTokens),
|
|
1276
|
+
fmtToken(r.outputTokens),
|
|
1277
|
+
fmtToken(r.reasoningOutputTokens),
|
|
1278
|
+
fmtToken(r.totalTokens),
|
|
1279
|
+
fmtUsd(r.estimatedCostUsd, !0)
|
|
1280
|
+
]),
|
|
1281
|
+
["left", "right", "right", "right", "right", "right", "right", "right", "right"]
|
|
1282
|
+
)) : lines.push(renderTable(
|
|
1283
|
+
[s.hdrSource, s.hdrEvents, s.hdrInput, s.hdrCache, s.hdrOutput, s.hdrTotal, s.hdrCost],
|
|
1284
|
+
report.bySource.map((r) => [
|
|
1285
|
+
r.source,
|
|
1286
|
+
fmtInt(r.eventCount),
|
|
1287
|
+
fmtToken(r.inputTokens),
|
|
1288
|
+
fmtToken(r.cachedInputTokens + r.cacheWriteTokens),
|
|
1289
|
+
fmtToken(r.outputTokens),
|
|
1290
|
+
fmtToken(r.totalTokens),
|
|
1291
|
+
fmtUsd(r.estimatedCostUsd, !1)
|
|
1292
|
+
]),
|
|
1293
|
+
["left", "right", "right", "right", "right", "right", "right"]
|
|
1294
|
+
)), lines.push(""), lines.push(s.daily), opts.detail ? lines.push(renderTable(
|
|
1295
|
+
[s.hdrDate, s.hdrEvents, s.hdrInput, s.hdrCacheRead, s.hdrCacheWrite, s.hdrOutput, s.hdrReasoning, s.hdrTotal, s.hdrCost],
|
|
1296
|
+
report.daily.map((r) => [
|
|
1297
|
+
r.usageDate,
|
|
1298
|
+
fmtInt(r.eventCount),
|
|
1299
|
+
fmtToken(r.inputTokens),
|
|
1300
|
+
fmtToken(r.cachedInputTokens),
|
|
1301
|
+
fmtToken(r.cacheWriteTokens),
|
|
1302
|
+
fmtToken(r.outputTokens),
|
|
1303
|
+
fmtToken(r.reasoningOutputTokens),
|
|
1304
|
+
fmtToken(r.totalTokens),
|
|
1305
|
+
fmtUsd(r.estimatedCostUsd, !0)
|
|
1306
|
+
]),
|
|
1307
|
+
["left", "right", "right", "right", "right", "right", "right", "right", "right"]
|
|
1308
|
+
)) : lines.push(renderTable(
|
|
1309
|
+
[s.hdrDate, s.hdrEvents, s.hdrInput, s.hdrCache, s.hdrOutput, s.hdrTotal, s.hdrCost],
|
|
1310
|
+
report.daily.map((r) => [
|
|
1311
|
+
r.usageDate,
|
|
1312
|
+
fmtInt(r.eventCount),
|
|
1313
|
+
fmtToken(r.inputTokens),
|
|
1314
|
+
fmtToken(r.cachedInputTokens + r.cacheWriteTokens),
|
|
1315
|
+
fmtToken(r.outputTokens),
|
|
1316
|
+
fmtToken(r.totalTokens),
|
|
1317
|
+
fmtUsd(r.estimatedCostUsd, !1)
|
|
1318
|
+
]),
|
|
1319
|
+
["left", "right", "right", "right", "right", "right", "right"]
|
|
1320
|
+
)), opts.detail && report.byModel.length > 0 && (lines.push(""), lines.push(s.topModels), lines.push(renderTable(
|
|
1321
|
+
[s.hdrModel, s.hdrSource, s.hdrEvents, s.hdrTotal, s.hdrInput, s.hdrOutput, s.hdrCost],
|
|
1322
|
+
report.byModel.slice(0, 12).map((r) => [
|
|
1323
|
+
r.model,
|
|
1324
|
+
r.source,
|
|
1325
|
+
fmtInt(r.eventCount),
|
|
1326
|
+
fmtToken(r.totalTokens),
|
|
1327
|
+
fmtToken(r.inputTokens),
|
|
1328
|
+
fmtToken(r.outputTokens),
|
|
1329
|
+
fmtUsd(r.estimatedCostUsd, !0)
|
|
1330
|
+
]),
|
|
1331
|
+
["left", "left", "right", "right", "right", "right", "right"]
|
|
1332
|
+
))), opts.detail && report.pricingWarnings.length > 0) {
|
|
1333
|
+
lines.push(""), lines.push(s.pricingNotes);
|
|
1334
|
+
for (let w of report.pricingWarnings)
|
|
1335
|
+
lines.push(`- ${w}`);
|
|
1336
|
+
}
|
|
1337
|
+
return lines.join(`
|
|
1338
|
+
`);
|
|
1339
|
+
}
|
|
1340
|
+
function getRangeLabel2(range, lang) {
|
|
1341
|
+
let s = t(lang);
|
|
1342
|
+
switch (range) {
|
|
1343
|
+
case "7d":
|
|
1344
|
+
return s.rangeLast7d;
|
|
1345
|
+
case "1m":
|
|
1346
|
+
return s.rangeLast1m;
|
|
1347
|
+
case "3m":
|
|
1348
|
+
return s.rangeLast3m;
|
|
1349
|
+
case "all":
|
|
1350
|
+
return s.rangeAll;
|
|
1351
|
+
default:
|
|
1352
|
+
return range;
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
function renderTable(headers, rows, aligns) {
|
|
1356
|
+
if (rows.length === 0) return "(no data)";
|
|
1357
|
+
let widths = headers.map(
|
|
1358
|
+
(h, i) => Math.max(h.length, ...rows.map((r) => (r[i] ?? "").length))
|
|
1359
|
+
), fmtRow = (row) => row.map((val, i) => {
|
|
1360
|
+
let cell = val ?? "";
|
|
1361
|
+
return aligns[i] === "right" ? cell.padStart(widths[i]) : cell.padEnd(widths[i]);
|
|
1362
|
+
}).join(" "), divider = widths.map((w) => "\u2500".repeat(w)).join(" ");
|
|
1363
|
+
return [fmtRow(headers), divider, ...rows.map(fmtRow)].join(`
|
|
1364
|
+
`);
|
|
1365
|
+
}
|
|
1366
|
+
function fmtInt(n) {
|
|
1367
|
+
return n.toLocaleString("en-US");
|
|
1368
|
+
}
|
|
1369
|
+
function fmtToken(n) {
|
|
1370
|
+
let abs = Math.abs(n);
|
|
1371
|
+
return abs < 1e3 ? fmtInt(n) : abs < 1e6 ? fmtCompact(n / 1e3, "K") : abs < 1e9 ? fmtCompact(n / 1e6, "M") : fmtCompact(n / 1e9, "B");
|
|
1372
|
+
}
|
|
1373
|
+
function fmtCompact(value, suffix) {
|
|
1374
|
+
let digits = Math.abs(value) >= 100 ? 1 : 2;
|
|
1375
|
+
return `${value.toFixed(digits).replace(/\.0+$/, "").replace(/(\.\d*[1-9])0+$/, "$1")}${suffix}`;
|
|
1376
|
+
}
|
|
1377
|
+
function fmtUsd(value, detail) {
|
|
1378
|
+
return `$${value.toFixed(detail ? 4 : 2)}`;
|
|
1379
|
+
}
|
|
1380
|
+
function fmtDateShort(dateStr) {
|
|
1381
|
+
return (/* @__PURE__ */ new Date(dateStr + "T00:00:00")).toLocaleDateString("en-US", { month: "short", day: "numeric" });
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1298
1384
|
// src/config.ts
|
|
1299
1385
|
import { mkdir, readFile as readFile12, writeFile } from "node:fs/promises";
|
|
1300
1386
|
import { homedir as homedir12, hostname } from "node:os";
|
|
@@ -1351,6 +1437,18 @@ function setConfigValue(config, keyPath, values) {
|
|
|
1351
1437
|
if (!alias) throw new Error("project.alias \u522B\u540D\u4E0D\u80FD\u4E3A\u7A7A");
|
|
1352
1438
|
return next.projectAliases = { ...next.projectAliases ?? {}, [from]: alias }, next;
|
|
1353
1439
|
}
|
|
1440
|
+
if (keyPath === "lang") {
|
|
1441
|
+
let value = requireSingleValue(keyPath, values);
|
|
1442
|
+
if (value !== "en" && value !== "zh")
|
|
1443
|
+
throw new Error("lang only supports en or zh");
|
|
1444
|
+
return next.lang = value, next;
|
|
1445
|
+
}
|
|
1446
|
+
if (keyPath === "emoji") {
|
|
1447
|
+
let value = requireSingleValue(keyPath, values);
|
|
1448
|
+
if (value !== "true" && value !== "false")
|
|
1449
|
+
throw new Error("emoji only supports true or false");
|
|
1450
|
+
return next.emoji = value === "true", next;
|
|
1451
|
+
}
|
|
1354
1452
|
throw new Error(`\u4E0D\u652F\u6301\u7684\u914D\u7F6E\u9879: ${keyPath}`);
|
|
1355
1453
|
}
|
|
1356
1454
|
function requireSingleValue(keyPath, values) {
|
|
@@ -1781,7 +1879,10 @@ async function runReport(flags) {
|
|
|
1781
1879
|
console.log(JSON.stringify(report, null, 2));
|
|
1782
1880
|
return;
|
|
1783
1881
|
}
|
|
1784
|
-
|
|
1882
|
+
let lang = (typeof flags.lang == "string" ? flags.lang : config.lang) || "en";
|
|
1883
|
+
if (lang !== "en" && lang !== "zh") throw new Error("--lang only supports en or zh");
|
|
1884
|
+
let emoji = flags["no-emoji"] === !0 ? !1 : config.emoji ?? !0, detail = flags.detail === !0;
|
|
1885
|
+
console.log(renderReport(report, { lang, emoji, detail }));
|
|
1785
1886
|
}
|
|
1786
1887
|
function fmt(n) {
|
|
1787
1888
|
return n.toLocaleString("en-US");
|
|
@@ -1887,7 +1988,7 @@ async function runConfigSet(args) {
|
|
|
1887
1988
|
function printHelp() {
|
|
1888
1989
|
let initialized = existsSync(getConfigPath());
|
|
1889
1990
|
console.log(`aiusage v${getVersion()}
|
|
1890
|
-
`), console.log("Usage: aiusage <command>"), console.log(""), console.log("Commands:"), console.log(" aiusage init [--server URL] [--site-id ID] [--device-id ID] [--device-name NAME] [--lookback N]"), console.log(" aiusage health [--server URL]"), console.log(" aiusage enroll --server URL --site-id ID --enroll-token TOKEN [--device-id ID] [--device-name NAME]"), console.log(" aiusage sync [--date YYYY-MM-DD] [--from YYYY-MM-DD [--to YYYY-MM-DD]] [--lookback N] [--today] [--server URL]"), console.log(" aiusage scan [--date YYYY-MM-DD] [--json]"), console.log(" aiusage report [--range 7d|1m|3m|all] [--json]"), console.log(" aiusage schedule [on|off|status] [--every 5m]"), console.log(" aiusage doctor"), console.log(" aiusage config set <key> <value...>"), console.log(""), console.log(`\u914D\u7F6E\u6587\u4EF6: ${getConfigPath()}${initialized ? "" : " (\u5C1A\u672A\u521D\u59CB\u5316)"}`);
|
|
1991
|
+
`), console.log("Usage: aiusage <command>"), console.log(""), console.log("Commands:"), console.log(" aiusage init [--server URL] [--site-id ID] [--device-id ID] [--device-name NAME] [--lookback N]"), console.log(" aiusage health [--server URL]"), console.log(" aiusage enroll --server URL --site-id ID --enroll-token TOKEN [--device-id ID] [--device-name NAME]"), console.log(" aiusage sync [--date YYYY-MM-DD] [--from YYYY-MM-DD [--to YYYY-MM-DD]] [--lookback N] [--today] [--server URL]"), console.log(" aiusage scan [--date YYYY-MM-DD] [--json]"), console.log(" aiusage report [--range 7d|1m|3m|all] [--detail] [--lang en|zh] [--no-emoji] [--json]"), console.log(" aiusage schedule [on|off|status] [--every 5m]"), console.log(" aiusage doctor"), console.log(" aiusage config set <key> <value...>"), console.log(""), console.log(`\u914D\u7F6E\u6587\u4EF6: ${getConfigPath()}${initialized ? "" : " (\u5C1A\u672A\u521D\u59CB\u5316)"}`);
|
|
1891
1992
|
}
|
|
1892
1993
|
function parseArgs(args) {
|
|
1893
1994
|
let flags = {}, positionals = [];
|