@aiusage/cli 1.5.0 → 1.5.1
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.zh-CN.md +2 -2
- package/dist/cli.js +63 -31
- package/package.json +1 -1
package/README.zh-CN.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
`@aiusage/cli` 是 AIUsage 命令行工具,用于:
|
|
4
4
|
|
|
5
5
|
- 发现和管理本机 AI 工具项目
|
|
6
|
-
- 扫描本地 AI 编程工具的 Token 用量(Claude Code、Codex、Copilot CLI、Gemini CLI
|
|
6
|
+
- 扫描本地 AI 编程工具的 Token 用量(Claude Code、Codex、Cursor、Copilot CLI、Copilot VS Code、Gemini CLI、Antigravity、Amp、Kimi Code、Qwen Code、Droid、OpenCode、Pi)
|
|
7
7
|
- 通过 Anthropic Admin API 导入历史用量
|
|
8
8
|
- 生成本地用量报告(最近 7 天、30 天、90 天或全部历史)
|
|
9
9
|
- 定时自动同步数据到 AIUsage Worker
|
|
@@ -41,7 +41,7 @@ aiusage project alias # 查看所有已配置的别名
|
|
|
41
41
|
aiusage project alias --remove myapp # 移除别名
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
扫描所有已支持 AI 工具的数据目录,列出发现的项目及其别名和来源。
|
|
45
45
|
|
|
46
46
|
项目别名在上传前本地解析。两台设备对各自项目目录设置相同的别名,服务端会将其合并为一个项目。
|
|
47
47
|
|
package/dist/cli.js
CHANGED
|
@@ -1588,7 +1588,7 @@ import { readdir as readdir6, readFile as readFile13 } from "node:fs/promises";
|
|
|
1588
1588
|
import { homedir as homedir14 } from "node:os";
|
|
1589
1589
|
import { basename as basename8, join as join14 } from "node:path";
|
|
1590
1590
|
async function buildLocalReport(range, options = {}) {
|
|
1591
|
-
let requestedDates = range === "all" ? await discoverAllDates() : buildPresetDates(range), daily = [], totals = createEmptyTotals2(), bySource = /* @__PURE__ */ new Map(), byModel = /* @__PURE__ */ new Map(), pricingWarnings = /* @__PURE__ */ new Set(), daysWithData = 0, results = await scanDates(requestedDates, { projectAliases: options.projectAliases });
|
|
1591
|
+
let requestedDates = options.dates ? options.dates : range === "all" ? await discoverAllDates() : range === "today" ? [toDateKey3(getTodayLocalDate())] : buildPresetDates(range), daily = [], totals = createEmptyTotals2(), bySource = /* @__PURE__ */ new Map(), byModel = /* @__PURE__ */ new Map(), pricingWarnings = /* @__PURE__ */ new Set(), daysWithData = 0, results = await scanDates(requestedDates, { projectAliases: options.projectAliases });
|
|
1592
1592
|
for (let result of results) {
|
|
1593
1593
|
let usageDate = result.usageDate, dayTotals = withTotalTokens(result.totals), hasData = result.breakdowns.length > 0 || dayTotals.eventCount > 0 || dayTotals.totalTokens > 0;
|
|
1594
1594
|
if (hasData) {
|
|
@@ -1621,10 +1621,11 @@ async function buildLocalReport(range, options = {}) {
|
|
|
1621
1621
|
pricingWarnings: [...pricingWarnings].sort()
|
|
1622
1622
|
};
|
|
1623
1623
|
}
|
|
1624
|
-
function parseReportRange(value) {
|
|
1624
|
+
function parseReportRange(value, today) {
|
|
1625
|
+
if (today) return "today";
|
|
1625
1626
|
if (value === void 0 || value === !0) return "7d";
|
|
1626
|
-
if (value === "7d" || value === "1m" || value === "3m" || value === "all") return value;
|
|
1627
|
-
throw new Error("--range \u4EC5\u652F\u6301 7d\u30011m\u30013m\u3001all");
|
|
1627
|
+
if (value === "7d" || value === "1m" || value === "3m" || value === "all" || value === "today") return value;
|
|
1628
|
+
throw new Error("--range \u4EC5\u652F\u6301 7d\u30011m\u30013m\u3001all\u3001today");
|
|
1628
1629
|
}
|
|
1629
1630
|
function getRangeLabel(range) {
|
|
1630
1631
|
switch (range) {
|
|
@@ -2098,6 +2099,7 @@ var en = {
|
|
|
2098
2099
|
rangeLast1m: "Last 30 days",
|
|
2099
2100
|
rangeLast3m: "Last 90 days",
|
|
2100
2101
|
rangeAll: "All time",
|
|
2102
|
+
rangeToday: "Today",
|
|
2101
2103
|
hdrSource: "Source",
|
|
2102
2104
|
hdrDate: "Date",
|
|
2103
2105
|
hdrModel: "Model",
|
|
@@ -2125,6 +2127,7 @@ var en = {
|
|
|
2125
2127
|
rangeLast1m: "\u6700\u8FD1 30 \u5929",
|
|
2126
2128
|
rangeLast3m: "\u6700\u8FD1 90 \u5929",
|
|
2127
2129
|
rangeAll: "\u5168\u90E8\u5386\u53F2",
|
|
2130
|
+
rangeToday: "\u4ECA\u5929",
|
|
2128
2131
|
hdrSource: "\u6765\u6E90",
|
|
2129
2132
|
hdrDate: "\u65E5\u671F",
|
|
2130
2133
|
hdrModel: "\u6A21\u578B",
|
|
@@ -2233,17 +2236,35 @@ function getRangeLabel2(range, lang) {
|
|
|
2233
2236
|
return s.rangeLast3m;
|
|
2234
2237
|
case "all":
|
|
2235
2238
|
return s.rangeAll;
|
|
2239
|
+
case "today":
|
|
2240
|
+
return s.rangeToday;
|
|
2236
2241
|
default:
|
|
2237
2242
|
return range;
|
|
2238
2243
|
}
|
|
2239
2244
|
}
|
|
2245
|
+
function displayWidth(str) {
|
|
2246
|
+
let w = 0;
|
|
2247
|
+
for (let ch of str) {
|
|
2248
|
+
let code = ch.codePointAt(0);
|
|
2249
|
+
code >= 11904 && code <= 40959 || code >= 63744 && code <= 64255 || code >= 65072 && code <= 65103 || code >= 65281 && code <= 65376 || code >= 65504 && code <= 65510 || code >= 131072 && code <= 195103 ? w += 2 : w += 1;
|
|
2250
|
+
}
|
|
2251
|
+
return w;
|
|
2252
|
+
}
|
|
2253
|
+
function padStartDisplay(str, width) {
|
|
2254
|
+
let gap = width - displayWidth(str);
|
|
2255
|
+
return gap > 0 ? " ".repeat(gap) + str : str;
|
|
2256
|
+
}
|
|
2257
|
+
function padEndDisplay(str, width) {
|
|
2258
|
+
let gap = width - displayWidth(str);
|
|
2259
|
+
return gap > 0 ? str + " ".repeat(gap) : str;
|
|
2260
|
+
}
|
|
2240
2261
|
function renderTable(headers, rows, aligns) {
|
|
2241
2262
|
if (rows.length === 0) return "(no data)";
|
|
2242
2263
|
let widths = headers.map(
|
|
2243
|
-
(h, i) => Math.max(h
|
|
2264
|
+
(h, i) => Math.max(displayWidth(h), ...rows.map((r) => displayWidth(r[i] ?? "")))
|
|
2244
2265
|
), fmtRow = (row) => row.map((val, i) => {
|
|
2245
2266
|
let cell = val ?? "";
|
|
2246
|
-
return aligns[i] === "right" ? cell
|
|
2267
|
+
return aligns[i] === "right" ? padStartDisplay(cell, widths[i]) : padEndDisplay(cell, widths[i]);
|
|
2247
2268
|
}).join(" "), divider = widths.map((w) => "\u2500".repeat(w)).join(" ");
|
|
2248
2269
|
return [fmtRow(headers), divider, ...rows.map(fmtRow)].join(`
|
|
2249
2270
|
`);
|
|
@@ -3173,14 +3194,12 @@ async function runScan(date, isJson) {
|
|
|
3173
3194
|
console.log("\u2500\u2500 \u5408\u8BA1 \u2500\u2500"), console.log(` \u4E8B\u4EF6: ${result.totals.eventCount} \u8F93\u5165: ${fmt(result.totals.inputTokens)} \u7F13\u5B58\u8BFB: ${fmt(result.totals.cachedInputTokens)} \u7F13\u5B58\u5199: ${fmt(result.totals.cacheWriteTokens)} \u8F93\u51FA: ${fmt(result.totals.outputTokens)} \u63A8\u7406: ${fmt(result.totals.reasoningOutputTokens)}`);
|
|
3174
3195
|
}
|
|
3175
3196
|
async function runReport(flags) {
|
|
3176
|
-
let config = await readConfig(), range =
|
|
3197
|
+
let config = await readConfig(), { dates, range } = resolveDateParams(flags, config), report = await buildLocalReport(range, { projectAliases: config.projectAliases, dates });
|
|
3177
3198
|
if (flags.json) {
|
|
3178
3199
|
console.log(JSON.stringify(report, null, 2));
|
|
3179
3200
|
return;
|
|
3180
3201
|
}
|
|
3181
|
-
let lang = (
|
|
3182
|
-
if (lang !== "en" && lang !== "zh") throw new Error("--lang only supports en or zh");
|
|
3183
|
-
let emoji = flags["no-emoji"] === !0 ? !1 : config.emoji ?? !0, detail = flags.detail === !0;
|
|
3202
|
+
let lang = resolveGlobalLang(flags, config), emoji = flags["no-emoji"] === !0 ? !1 : config.emoji ?? !0, detail = flags.detail === !0;
|
|
3184
3203
|
console.log(renderReport(report, { lang, emoji, detail }));
|
|
3185
3204
|
}
|
|
3186
3205
|
function fmt(n) {
|
|
@@ -3230,17 +3249,7 @@ async function runSync(flags) {
|
|
|
3230
3249
|
let config = await readConfig(), allTargets = config.targets ?? [];
|
|
3231
3250
|
if (allTargets.length === 0)
|
|
3232
3251
|
throw new Error("\u672A\u914D\u7F6E\u4EFB\u4F55\u4E0A\u62A5\u76EE\u6807\uFF0C\u8BF7\u5148\u6267\u884C aiusage enroll");
|
|
3233
|
-
let deviceId = resolveRequiredString(void 0, config.deviceId, "\u7F3A\u5C11 deviceId\uFF0C\u8BF7\u5148\u6267\u884C enroll"), targetName = resolveOptionalString(flags.target, void 0), targets = targetName ? [findTargetOrThrow(config, targetName)] : allTargets,
|
|
3234
|
-
if (requestedDate)
|
|
3235
|
-
targetDates = [requestedDate];
|
|
3236
|
-
else if (fromDate)
|
|
3237
|
-
targetDates = buildDateRange(fromDate, toDate ?? getTodayDate());
|
|
3238
|
-
else if (flags.today === !0)
|
|
3239
|
-
targetDates = [getTodayDate()];
|
|
3240
|
-
else {
|
|
3241
|
-
let lookbackDays = typeof flags.lookback == "string" ? parsePositiveInt2(flags.lookback, "--lookback") : defaultLookbackDays(config);
|
|
3242
|
-
targetDates = getClosedDates(lookbackDays), targetDates.push(getTodayDate());
|
|
3243
|
-
}
|
|
3252
|
+
let deviceId = resolveRequiredString(void 0, config.deviceId, "\u7F3A\u5C11 deviceId\uFF0C\u8BF7\u5148\u6267\u884C enroll"), targetName = resolveOptionalString(flags.target, void 0), targets = targetName ? [findTargetOrThrow(config, targetName)] : allTargets, { dates: targetDates } = resolveDateParams(flags, config);
|
|
3244
3253
|
console.log(`\u626B\u63CF ${targetDates.length} \u5929 (${targetDates[0]} ~ ${targetDates[targetDates.length - 1]}) ...`);
|
|
3245
3254
|
let allDays = (await scanDates(targetDates, { projectAliases: config.projectAliases })).filter((r) => r.breakdowns.length > 0).map((r) => ({ usageDate: r.usageDate, breakdowns: r.breakdowns }));
|
|
3246
3255
|
if (allDays.length === 0) {
|
|
@@ -3432,9 +3441,9 @@ function printHelp(zh2 = !1) {
|
|
|
3432
3441
|
`);
|
|
3433
3442
|
let cmds = zh2 ? [
|
|
3434
3443
|
["scan [--date YYYY-MM-DD] [--json]", "\u626B\u63CF\u67D0\u65E5\u7528\u91CF\u660E\u7EC6"],
|
|
3435
|
-
["report [--range 7d|1m|3m|all] [--detail] [--json]", "\u672C\u5730\u7528\u91CF\u62A5\u544A"],
|
|
3436
|
-
["sync [--today] [--
|
|
3437
|
-
["sync --from YYYY-MM-DD [--to YYYY-MM-DD]", "\
|
|
3444
|
+
["report [--today] [--range 7d|1m|3m|all] [--detail] [--json]", "\u672C\u5730\u7528\u91CF\u62A5\u544A"],
|
|
3445
|
+
["sync [--today] [--range 7d|1m|3m|all]", "\u4E0A\u4F20\u7528\u91CF\u5230\u670D\u52A1\u7AEF"],
|
|
3446
|
+
["report/sync --from YYYY-MM-DD [--to YYYY-MM-DD]", "\u6307\u5B9A\u65E5\u671F\u8303\u56F4\uFF08--start/--end \u540C\u4E49\uFF09"],
|
|
3438
3447
|
["project [list|alias]", "\u9879\u76EE\u7BA1\u7406\u4E0E\u522B\u540D\u8BBE\u7F6E"],
|
|
3439
3448
|
["schedule [on|off|status] [--every 5m]", "\u5B9A\u65F6\u540C\u6B65\u7BA1\u7406"],
|
|
3440
3449
|
["doctor", "\u8BCA\u65AD\u68C0\u67E5"],
|
|
@@ -3444,9 +3453,9 @@ function printHelp(zh2 = !1) {
|
|
|
3444
3453
|
["health [--server URL]", "\u6D4B\u8BD5\u670D\u52A1\u7AEF\u8FDE\u901A\u6027"]
|
|
3445
3454
|
] : [
|
|
3446
3455
|
["scan [--date YYYY-MM-DD] [--json]", "Scan daily usage breakdown"],
|
|
3447
|
-
["report [--range 7d|1m|3m|all] [--detail] [--json]", "Local usage report"],
|
|
3448
|
-
["sync [--today] [--
|
|
3449
|
-
["sync --from YYYY-MM-DD [--to YYYY-MM-DD]", "
|
|
3456
|
+
["report [--today] [--range 7d|1m|3m|all] [--detail] [--json]", "Local usage report"],
|
|
3457
|
+
["sync [--today] [--range 7d|1m|3m|all]", "Upload usage to server"],
|
|
3458
|
+
["report/sync --from YYYY-MM-DD [--to YYYY-MM-DD]", "Date range (--start/--end aliases)"],
|
|
3450
3459
|
["project [list|alias]", "Project management & aliases"],
|
|
3451
3460
|
["schedule [on|off|status] [--every 5m]", "Scheduled sync management"],
|
|
3452
3461
|
["doctor", "Run diagnostics"],
|
|
@@ -3465,16 +3474,16 @@ function printUsageHint(zh2 = !1) {
|
|
|
3465
3474
|
`);
|
|
3466
3475
|
let cmds = zh2 ? [
|
|
3467
3476
|
["scan [--date YYYY-MM-DD]", "\u626B\u63CF\u67D0\u65E5\u7528\u91CF\u660E\u7EC6"],
|
|
3468
|
-
["report [--range 7d|1m|3m|all]", "\u672C\u5730\u7528\u91CF\u62A5\u544A"],
|
|
3469
|
-
["sync [--today]", "\u4E0A\u4F20\u7528\u91CF\u5230\u670D\u52A1\u7AEF"],
|
|
3477
|
+
["report [--today] [--range 7d|1m|3m|all]", "\u672C\u5730\u7528\u91CF\u62A5\u544A"],
|
|
3478
|
+
["sync [--today] [--range 7d|1m|3m|all]", "\u4E0A\u4F20\u7528\u91CF\u5230\u670D\u52A1\u7AEF"],
|
|
3470
3479
|
["project [list|alias]", "\u9879\u76EE\u7BA1\u7406\u4E0E\u522B\u540D\u8BBE\u7F6E"],
|
|
3471
3480
|
["schedule [on|off|status]", "\u5B9A\u65F6\u540C\u6B65\u7BA1\u7406"],
|
|
3472
3481
|
["doctor", "\u8BCA\u65AD\u68C0\u67E5"],
|
|
3473
3482
|
["config set <key> <value>", "\u4FEE\u6539\u914D\u7F6E"]
|
|
3474
3483
|
] : [
|
|
3475
3484
|
["scan [--date YYYY-MM-DD]", "Scan daily usage breakdown"],
|
|
3476
|
-
["report [--range 7d|1m|3m|all]", "Local usage report"],
|
|
3477
|
-
["sync [--today]", "Upload usage to server"],
|
|
3485
|
+
["report [--today] [--range 7d|1m|3m|all]", "Local usage report"],
|
|
3486
|
+
["sync [--today] [--range 7d|1m|3m|all]", "Upload usage to server"],
|
|
3478
3487
|
["project [list|alias]", "Project management & aliases"],
|
|
3479
3488
|
["schedule [on|off|status]", "Scheduled sync management"],
|
|
3480
3489
|
["doctor", "Run diagnostics"],
|
|
@@ -3514,6 +3523,29 @@ function getYesterdayDate() {
|
|
|
3514
3523
|
let yesterday = /* @__PURE__ */ new Date();
|
|
3515
3524
|
return yesterday.setDate(yesterday.getDate() - 1), localDateKey(yesterday);
|
|
3516
3525
|
}
|
|
3526
|
+
function resolveDateParams(flags, config) {
|
|
3527
|
+
let requestedDate = typeof flags.date == "string" ? flags.date : void 0, fromDate = typeof flags.from == "string" ? flags.from : typeof flags.start == "string" ? flags.start : void 0, toDate = typeof flags.to == "string" ? flags.to : typeof flags.end == "string" ? flags.end : void 0;
|
|
3528
|
+
if (requestedDate)
|
|
3529
|
+
return { dates: [requestedDate], range: "today" };
|
|
3530
|
+
if (fromDate)
|
|
3531
|
+
return { dates: buildDateRange(fromDate, toDate ?? getTodayDate()), range: "all" };
|
|
3532
|
+
if (flags.today === !0)
|
|
3533
|
+
return { dates: [getTodayDate()], range: "today" };
|
|
3534
|
+
let rangeFlag = flags.range;
|
|
3535
|
+
if (typeof rangeFlag == "string")
|
|
3536
|
+
return { range: parseReportRange(rangeFlag) };
|
|
3537
|
+
if (typeof flags.lookback == "string") {
|
|
3538
|
+
let lookbackDays2 = parsePositiveInt2(flags.lookback, "--lookback"), dates2 = getClosedDates(lookbackDays2);
|
|
3539
|
+
return dates2.push(getTodayDate()), { dates: dates2, range: "7d" };
|
|
3540
|
+
}
|
|
3541
|
+
let lookbackDays = defaultLookbackDays(config), dates = getClosedDates(lookbackDays);
|
|
3542
|
+
return dates.push(getTodayDate()), { dates, range: "7d" };
|
|
3543
|
+
}
|
|
3544
|
+
function resolveGlobalLang(flags, config) {
|
|
3545
|
+
let lang = (typeof flags.lang == "string" ? flags.lang : config.lang) || "en";
|
|
3546
|
+
if (lang !== "en" && lang !== "zh") throw new Error("--lang only supports en or zh");
|
|
3547
|
+
return lang;
|
|
3548
|
+
}
|
|
3517
3549
|
function getTodayDate() {
|
|
3518
3550
|
return localDateKey(/* @__PURE__ */ new Date());
|
|
3519
3551
|
}
|