@aiusage/cli 1.4.2 → 1.4.4

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 CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  - discovering and managing projects across AI tools
6
6
  - scanning local Claude Code, Codex, Copilot CLI, and Gemini CLI token usage
7
+ - importing historical usage from Anthropic Admin API
7
8
  - printing local usage summaries for the last 7 days, 30 days, 90 days, or all history
8
9
  - scheduling automatic sync to an AIUsage Worker
9
10
  - diagnosing configuration and connectivity issues
@@ -104,16 +105,36 @@ aiusage enroll \
104
105
 
105
106
  ### sync
106
107
 
107
- Upload usage data to the Worker. Covers the last 7 closed days by default.
108
+ Upload usage data to the Worker. Default: last 7 days + today.
108
109
 
109
110
  ```bash
110
- aiusage sync
111
- aiusage sync --today # include today's live data
112
- aiusage sync --date 2026-03-31
113
- aiusage sync --lookback 14
111
+ aiusage sync # last 7 days + today
112
+ aiusage sync --today # today only
113
+ aiusage sync --date 2026-03-31 # specific date
114
+ aiusage sync --lookback 14 # last 14 days + today
115
+ aiusage sync --from 2025-01-01 --to 2026-04-05 # date range
114
116
  ```
115
117
 
116
- Use `--today` to upload the current (incomplete) day. The server upserts, so partial data is updated on next sync.
118
+ The server upserts, so re-syncing the same dates safely updates existing data.
119
+
120
+ ### import
121
+
122
+ Import historical Claude usage from the Anthropic Admin API. Useful for recovering data from periods where local JSONL logs were rotated or deleted.
123
+
124
+ ```bash
125
+ aiusage import --start 2025-06-01 --end 2025-09-15
126
+ aiusage import --key sk-ant-admin... --start 2025-06-01 --end 2025-09-15
127
+ ```
128
+
129
+ Requires an **Admin API key** (`sk-ant-admin...`), not a regular API key. Get one at [console.anthropic.com](https://console.anthropic.com) → Settings → Admin Keys.
130
+
131
+ Save the key once:
132
+
133
+ ```bash
134
+ aiusage config set anthropic-admin-key sk-ant-admin...
135
+ ```
136
+
137
+ **Important:** Do not use `import` for dates already covered by local scan data — it will double-count.
117
138
 
118
139
  ### schedule
119
140
 
@@ -147,6 +168,7 @@ aiusage config set emoji false # disable emoji in repor
147
168
  aiusage config set device.alias "MacBook Pro 工作机"
148
169
  aiusage config set privacy.projectVisibility masked
149
170
  aiusage config set project.alias MyApp "我的应用" # prefer: aiusage project alias
171
+ aiusage config set anthropic-admin-key sk-ant-admin... # for aiusage import
150
172
  ```
151
173
 
152
174
  CLI flags (`--lang`, `--no-emoji`) override config values for a single run.
package/README.zh-CN.md CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  `@aiusage/cli` 是 AIUsage 命令行工具,用于:
4
4
 
5
- - 扫描本地 AI 编程工具的 Token 用量(Claude Code、Codex、Copilot CLI、Gemini CLI、Qwen Code、Kimi Code、Amp、Droid、OpenCode、Pi)
5
+ - 发现和管理本机 AI 工具项目
6
+ - 扫描本地 AI 编程工具的 Token 用量(Claude Code、Codex、Copilot CLI、Gemini CLI 等)
7
+ - 通过 Anthropic Admin API 导入历史用量
6
8
  - 生成本地用量报告(最近 7 天、30 天、90 天或全部历史)
7
9
  - 定时自动同步数据到 AIUsage Worker
8
10
  - 诊断配置与连接问题
@@ -27,6 +29,22 @@ aiusage --help
27
29
 
28
30
  ## 命令
29
31
 
32
+ ### project
33
+
34
+ 发现和管理本机项目。
35
+
36
+ ```bash
37
+ aiusage project # 列出所有发现的项目(默认)
38
+ aiusage project list # 同上
39
+ aiusage project alias myapp "我的应用" # 设置项目别名
40
+ aiusage project alias # 查看所有已配置的别名
41
+ aiusage project alias --remove myapp # 移除别名
42
+ ```
43
+
44
+ 扫描 Claude Code、Codex、Copilot CLI、Gemini CLI 的数据目录,列出发现的项目及其别名和来源。
45
+
46
+ 项目别名在上传前本地解析。两台设备对各自项目目录设置相同的别名,服务端会将其合并为一个项目。
47
+
30
48
  ### report
31
49
 
32
50
  本地用量报告,无需服务端。
@@ -55,6 +73,39 @@ aiusage scan --date 2026-03-31 --json
55
73
 
56
74
  省略 `--date` 时默认扫描昨天。
57
75
 
76
+ ### sync
77
+
78
+ 上传用量数据到 Worker。默认:最近 7 天 + 今天。
79
+
80
+ ```bash
81
+ aiusage sync # 最近 7 天 + 今天
82
+ aiusage sync --today # 仅今天
83
+ aiusage sync --date 2026-03-31 # 指定日期
84
+ aiusage sync --lookback 14 # 最近 14 天 + 今天
85
+ aiusage sync --from 2025-01-01 --to 2026-04-05 # 指定日期范围
86
+ ```
87
+
88
+ 服务端使用 upsert,重复同步相同日期会安全更新已有数据。
89
+
90
+ ### import
91
+
92
+ 通过 Anthropic Admin API 导入历史 Claude 用量。适用于本地 JSONL 日志已被清理或删除的时间段。
93
+
94
+ ```bash
95
+ aiusage import --start 2025-06-01 --end 2025-09-15
96
+ aiusage import --key sk-ant-admin... --start 2025-06-01 --end 2025-09-15
97
+ ```
98
+
99
+ 需要 **Admin API 密钥**(`sk-ant-admin...`),而非普通 API 密钥。前往 [console.anthropic.com](https://console.anthropic.com) → Settings → Admin Keys 获取。
100
+
101
+ 保存密钥(一次性):
102
+
103
+ ```bash
104
+ aiusage config set anthropic-admin-key sk-ant-admin...
105
+ ```
106
+
107
+ **注意:** 不要对已有本地扫描数据的日期使用 `import`,否则会重复计数。
108
+
58
109
  ### init
59
110
 
60
111
  初始化本地配置。
@@ -83,19 +134,6 @@ aiusage enroll \
83
134
  --device-name "MacBook Pro"
84
135
  ```
85
136
 
86
- ### sync
87
-
88
- 上传用量数据到 Worker。默认覆盖最近 7 个已结束的日期。
89
-
90
- ```bash
91
- aiusage sync
92
- aiusage sync --today # 包含今日实时数据
93
- aiusage sync --date 2026-03-31
94
- aiusage sync --lookback 14
95
- ```
96
-
97
- 使用 `--today` 上传当日(未完成)数据。服务端使用 upsert,部分数据会在下次同步时更新。
98
-
99
137
  ### schedule
100
138
 
101
139
  管理定时同步。macOS 使用 `launchd`,Linux 使用 `cron`。
@@ -104,21 +142,10 @@ aiusage sync --lookback 14
104
142
  aiusage schedule on # 启用,默认每 5 分钟
105
143
  aiusage schedule on --every 30m # 自定义间隔
106
144
  aiusage schedule off # 关闭
107
- aiusage schedule status # 查看当前定时任务详情
145
+ aiusage schedule status # 查看当前状态
108
146
  ```
109
147
 
110
- 支持间隔:`5m` – `1d`。定时同步始终包含今日实时数据(`--today`),确保看板数据及时更新。
111
-
112
- `schedule status` 输出示例:
113
-
114
- ```
115
- 状态: 已启用
116
- 间隔: 每 5m
117
- 含今日: 是
118
- 命令: /usr/local/bin/node /usr/local/bin/aiusage sync --today
119
- 配置: ~/Library/LaunchAgents/com.aiusage.sync.plist
120
- 日志: ~/.aiusage/sync.log
121
- ```
148
+ 支持间隔:`5m` – `1d`。定时同步始终包含今日实时数据,确保看板数据及时更新。
122
149
 
123
150
  ### doctor
124
151
 
@@ -137,7 +164,8 @@ aiusage config set lang zh # 默认语言: en 或 z
137
164
  aiusage config set emoji false # 禁用报告标题 emoji
138
165
  aiusage config set device.alias "MacBook Pro 工作机"
139
166
  aiusage config set privacy.projectVisibility masked
140
- aiusage config set project.alias /Users/me/Projects/MyApp MyApp
167
+ aiusage config set project.alias MyApp "我的应用" # 推荐用 aiusage project alias
168
+ aiusage config set anthropic-admin-key sk-ant-admin... # 用于 aiusage import
141
169
  ```
142
170
 
143
171
  CLI 标志(`--lang`、`--no-emoji`)会覆盖配置值(仅当次生效)。
package/dist/cli.js CHANGED
@@ -7,7 +7,6 @@ var __require = /* @__PURE__ */ ((x) => typeof require < "u" ? require : typeof
7
7
  });
8
8
 
9
9
  // src/cli.ts
10
- import { existsSync as existsSync2 } from "node:fs";
11
10
  import { createInterface as createInterface3 } from "node:readline";
12
11
  import { hostname as hostname3 } from "node:os";
13
12
 
@@ -2661,9 +2660,10 @@ try {
2661
2660
  console.log(`To deploy the server, clone the repo and run the setup wizard:
2662
2661
  `), console.log(" git clone https://github.com/ennann/aiusage.git"), console.log(" cd aiusage && pnpm install"), console.log(` pnpm setup
2663
2662
  `), console.log("See: https://github.com/ennann/aiusage#deploy-your-own-server");
2664
- else if (command === "--help" || command === "-h" || command === "help")
2665
- printHelp();
2666
- else {
2663
+ else if (command === "--help" || command === "-h" || command === "help") {
2664
+ let zh2 = (await readConfig()).lang === "zh";
2665
+ printHelp(zh2);
2666
+ } else {
2667
2667
  let zh2 = ((await readConfig()).lang || "en") === "zh";
2668
2668
  command && console.error(`${zh2 ? "\u672A\u77E5\u547D\u4EE4" : "Unknown command"}: "${command}"
2669
2669
  `), printUsageHint(zh2);
@@ -2932,14 +2932,63 @@ async function runProjectAlias(args) {
2932
2932
  if (!alias) throw new Error(zh2 ? "\u522B\u540D\u4E0D\u80FD\u4E3A\u7A7A" : "Alias cannot be empty");
2933
2933
  config.projectAliases = { ...config.projectAliases ?? {}, [name]: alias }, await writeConfig(config), console.log(zh2 ? `\u5DF2\u8BBE\u7F6E: ${name} \u2192 ${alias}` : `Set: ${name} \u2192 ${alias}`);
2934
2934
  }
2935
- function printHelp() {
2936
- let initialized = existsSync2(getConfigPath());
2935
+ function printHelp(zh2 = !1) {
2937
2936
  console.log(`aiusage v${getVersion()}
2938
- `), console.log("Usage: aiusage <command>"), console.log(""), console.log("Commands:"), console.log(" aiusage init [--device-id ID] [--device-name NAME] [--lookback N]"), console.log(" aiusage enroll --server URL --site-id ID --enroll-token TOKEN [--target NAME]"), console.log(" aiusage sync [--target NAME] [--date YYYY-MM-DD] [--from YYYY-MM-DD [--to YYYY-MM-DD]] [--lookback N] [--today]"), console.log(" aiusage health [--target NAME] [--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 project [list]"), console.log(" aiusage project alias [<\u9879\u76EE\u540D> <\u522B\u540D>] [--remove <\u9879\u76EE\u540D>]"), console.log(" aiusage config set <key> <value...>"), console.log(""), console.log(`\u914D\u7F6E\u6587\u4EF6: ${getConfigPath()}${initialized ? "" : " (\u5C1A\u672A\u521D\u59CB\u5316)"}`);
2937
+ `);
2938
+ let cmds = zh2 ? [
2939
+ ["scan [--date YYYY-MM-DD] [--json]", "\u626B\u63CF\u67D0\u65E5\u7528\u91CF\u660E\u7EC6"],
2940
+ ["report [--range 7d|1m|3m|all] [--detail] [--json]", "\u672C\u5730\u7528\u91CF\u62A5\u544A"],
2941
+ ["sync [--today] [--lookback N] [--date YYYY-MM-DD]", "\u4E0A\u4F20\u7528\u91CF\u5230\u670D\u52A1\u7AEF"],
2942
+ ["sync --from YYYY-MM-DD [--to YYYY-MM-DD]", "\u4E0A\u4F20\u6307\u5B9A\u65E5\u671F\u8303\u56F4"],
2943
+ ["project [list|alias]", "\u9879\u76EE\u7BA1\u7406\u4E0E\u522B\u540D\u8BBE\u7F6E"],
2944
+ ["schedule [on|off|status] [--every 5m]", "\u5B9A\u65F6\u540C\u6B65\u7BA1\u7406"],
2945
+ ["doctor", "\u8BCA\u65AD\u68C0\u67E5"],
2946
+ ["config set <key> <value>", "\u4FEE\u6539\u914D\u7F6E"],
2947
+ ["init [--device-id ID] [--device-name NAME]", "\u521D\u59CB\u5316\u672C\u5730\u914D\u7F6E"],
2948
+ ["enroll --server URL --site-id ID --enroll-token TOKEN", "\u6CE8\u518C\u8BBE\u5907\u5230\u670D\u52A1\u7AEF"],
2949
+ ["health [--server URL]", "\u6D4B\u8BD5\u670D\u52A1\u7AEF\u8FDE\u901A\u6027"]
2950
+ ] : [
2951
+ ["scan [--date YYYY-MM-DD] [--json]", "Scan daily usage breakdown"],
2952
+ ["report [--range 7d|1m|3m|all] [--detail] [--json]", "Local usage report"],
2953
+ ["sync [--today] [--lookback N] [--date YYYY-MM-DD]", "Upload usage to server"],
2954
+ ["sync --from YYYY-MM-DD [--to YYYY-MM-DD]", "Upload specific date range"],
2955
+ ["project [list|alias]", "Project management & aliases"],
2956
+ ["schedule [on|off|status] [--every 5m]", "Scheduled sync management"],
2957
+ ["doctor", "Run diagnostics"],
2958
+ ["config set <key> <value>", "Update config"],
2959
+ ["init [--device-id ID] [--device-name NAME]", "Initialize local config"],
2960
+ ["enroll --server URL --site-id ID --enroll-token TOKEN", "Register device with server"],
2961
+ ["health [--server URL]", "Test server connectivity"]
2962
+ ], dw = (s) => [...s].reduce((w, c) => w + (c.charCodeAt(0) > 127 ? 2 : 1), 0), pad = (s, width) => s + " ".repeat(Math.max(0, width - dw(s))), maxCmd = Math.max(...cmds.map((c) => dw(c[0])));
2963
+ console.log(zh2 ? "\u547D\u4EE4:" : "Commands:");
2964
+ for (let [cmd, desc] of cmds)
2965
+ console.log(` ${pad(cmd, maxCmd + 2)} ${desc}`);
2966
+ console.log(""), console.log(`${zh2 ? "\u914D\u7F6E\u6587\u4EF6" : "Config"}: ${getConfigPath()}`);
2939
2967
  }
2940
2968
  function printUsageHint(zh2 = !1) {
2941
2969
  console.log(`aiusage v${getVersion()}
2942
- `), zh2 ? (console.log("\u5E38\u7528\u547D\u4EE4:"), console.log(" scan [--date YYYY-MM-DD] \u626B\u63CF\u67D0\u65E5\u7528\u91CF\u660E\u7EC6"), console.log(" report [--range 7d|1m|3m|all] \u672C\u5730\u7528\u91CF\u62A5\u544A"), console.log(" project [list] \u5217\u51FA\u672C\u673A\u6240\u6709\u9879\u76EE"), console.log(" project alias <\u540D\u79F0> <\u522B\u540D> \u8BBE\u7F6E\u9879\u76EE\u522B\u540D"), console.log(" sync \u4E0A\u4F20\u7528\u91CF\u5230\u670D\u52A1\u7AEF"), console.log(" schedule [on|off|status] \u5B9A\u65F6\u540C\u6B65"), console.log(" doctor \u8BCA\u65AD\u68C0\u67E5"), console.log(" config set <key> <value> \u4FEE\u6539\u914D\u7F6E"), console.log(""), console.log(`\u914D\u7F6E\u6587\u4EF6: ${getConfigPath()}`)) : (console.log("Commands:"), console.log(" scan [--date YYYY-MM-DD] Scan daily usage breakdown"), console.log(" report [--range 7d|1m|3m|all] Local usage report"), console.log(" project [list] List all projects"), console.log(" project alias <name> <alias> Set project alias"), console.log(" sync Upload usage to server"), console.log(" schedule [on|off|status] Scheduled sync"), console.log(" doctor Diagnostics"), console.log(" config set <key> <value> Update config"), console.log(""), console.log(`Config: ${getConfigPath()}`));
2970
+ `);
2971
+ let cmds = zh2 ? [
2972
+ ["scan [--date YYYY-MM-DD]", "\u626B\u63CF\u67D0\u65E5\u7528\u91CF\u660E\u7EC6"],
2973
+ ["report [--range 7d|1m|3m|all]", "\u672C\u5730\u7528\u91CF\u62A5\u544A"],
2974
+ ["sync [--today]", "\u4E0A\u4F20\u7528\u91CF\u5230\u670D\u52A1\u7AEF"],
2975
+ ["project [list|alias]", "\u9879\u76EE\u7BA1\u7406\u4E0E\u522B\u540D\u8BBE\u7F6E"],
2976
+ ["schedule [on|off|status]", "\u5B9A\u65F6\u540C\u6B65\u7BA1\u7406"],
2977
+ ["doctor", "\u8BCA\u65AD\u68C0\u67E5"],
2978
+ ["config set <key> <value>", "\u4FEE\u6539\u914D\u7F6E"]
2979
+ ] : [
2980
+ ["scan [--date YYYY-MM-DD]", "Scan daily usage breakdown"],
2981
+ ["report [--range 7d|1m|3m|all]", "Local usage report"],
2982
+ ["sync [--today]", "Upload usage to server"],
2983
+ ["project [list|alias]", "Project management & aliases"],
2984
+ ["schedule [on|off|status]", "Scheduled sync management"],
2985
+ ["doctor", "Run diagnostics"],
2986
+ ["config set <key> <value>", "Update config"]
2987
+ ], dw2 = (s) => [...s].reduce((w, c) => w + (c.charCodeAt(0) > 127 ? 2 : 1), 0), pad2 = (s, width) => s + " ".repeat(Math.max(0, width - dw2(s))), maxCmd2 = Math.max(...cmds.map((c) => dw2(c[0])));
2988
+ console.log(zh2 ? "\u5E38\u7528\u547D\u4EE4:" : "Commands:");
2989
+ for (let [cmd, desc] of cmds)
2990
+ console.log(` ${pad2(cmd, maxCmd2 + 2)} ${desc}`);
2991
+ console.log(""), console.log(`${zh2 ? "\u914D\u7F6E\u6587\u4EF6" : "Config"}: ${getConfigPath()}`);
2943
2992
  }
2944
2993
  function parseArgs(args) {
2945
2994
  let flags = {}, positionals = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiusage/cli",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "Track and sync AI tool token usage across devices.",
5
5
  "license": "MIT",
6
6
  "private": false,