@tsa-group/claude-usage 0.4.1 → 0.4.2

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 (2) hide show
  1. package/README.md +132 -41
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -39,76 +39,168 @@ ingest server,彙整成團隊用量儀表。
39
39
 
40
40
  ## 需求
41
41
 
42
- - **Node.js >= 20**
43
- - **Claude Code** 已登入(工具讀它的憑證來認證,見上方隱私說明)
44
- - macOS:完整支援並實測
45
- - Windows:CLI 與**桌面版(Microsoft Store / MSIX)**都支援;桌面版憑證路徑見「已知限制」
46
- - Linux:背景任務(systemd timer)未實作,其餘指令可用
42
+ | | |
43
+ |---|---|
44
+ | **Node.js** | >= 20(`node -v` 確認) |
45
+ | **Claude 帳號登入** | 需要**帳號登入**才拿得到額度資料。用 API key / Bedrock / Vertex 時,token 明細仍可收集,但沒有 5 小時 / 每週額度 % |
46
+ | **平台** | macOS ✅ 完整支援 Windows ✅ CLI 與桌面版皆支援 Linux ⚠️ 指令可用,背景任務未實作 |
47
+
48
+ ---
49
+
50
+ # 安裝
51
+
52
+ **三步都一樣**,只有平台細節不同:
53
+
54
+ ```
55
+ 1. configure 告訴它你們的 ingest server 在哪
56
+ 2. install 取得身份 + 註冊 hook + 裝背景任務
57
+ 3. status 確認真的在跑
58
+ ```
59
+
60
+ 管理者會給你 **ingest server 位址**。下面用 `https://<你們的 ingest host>` 代表它。
47
61
 
48
62
  ---
49
63
 
50
- ## 安裝(3 步)
64
+ ## macOS
51
65
 
52
66
  ```bash
53
67
  npm i -g @tsa-group/claude-usage
54
68
 
55
- # 1) 指向你們的 ingest server
56
69
  claude-usage configure --server https://<你們的 ingest host>
57
-
58
- # 2) 一鍵安裝:enroll(取得身份)+ 註冊 SessionStart hook + 背景任務
59
- # 先加 --dry-run 可預覽會做什麼、不動任何系統設定
60
- claude-usage install --dry-run
70
+ claude-usage install --dry-run # 先看它會做什麼,不動任何設定
61
71
  claude-usage install
62
-
63
- # 3) 確認狀態
64
72
  claude-usage status
65
73
  ```
66
74
 
67
- ### Windows / PowerShell 首次執行
75
+ 憑證走 **Keychain**(Claude Code 的主儲存)。首次執行可能跳出鑰匙圈授權,允許即可 ——
76
+ 背景任務要能在沒有視窗的情況下讀到它。
77
+
78
+ ---
79
+
80
+ ## Windows
81
+
82
+ ### 第 0 步:先確認 PowerShell 不會擋
68
83
 
69
- 多數 Windows PowerShell 執行原則預設是 `Restricted`,會擋掉 npm 產生的 `.ps1`
70
- 啟動器 —— **安裝成功、一執行就被擋**:
84
+ 多數 Windows 的執行原則預設是 `Restricted`,會擋掉 npm 產生的 `.ps1` 啟動器 ——
85
+ **安裝成功、一執行就被擋**:
71
86
 
72
87
  ```
73
88
  因為這個系統上已停用指令碼執行,所以無法載入 ...\npm\claude-usage.ps1
74
89
  + FullyQualifiedErrorId : UnauthorizedAccess (PSSecurityException)
75
90
  ```
76
91
 
77
- 這**不是套件的問題**,`cmd.exe` 與 Node 本身都不受影響。擇一處理:
92
+ 這不是套件的問題(`cmd.exe` 與 Node 本身都不受影響)。先查現況:
93
+
94
+ ```powershell
95
+ Get-ExecutionPolicy
96
+ ```
97
+
98
+ | 結果 | 怎麼辦 |
99
+ |---|---|
100
+ | `RemoteSigned` / `Unrestricted` / `Bypass` | 不用處理,直接往下 |
101
+ | `Restricted` / `AllSigned` | 用下面 **A** 或 **B** |
102
+
103
+ **A. 改原則(推薦,免系統管理員、一次搞定)**
78
104
 
79
105
  ```powershell
80
- # 1) 設定一次、永久生效、免系統管理員(推薦)
81
106
  Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
107
+ ```
108
+
109
+ `RemoteSigned` = 本機與 npm 的腳本可跑,只擋網路下載的未簽章腳本。
110
+
111
+ **B. 不改原則,全程加 `.cmd`**(功能完全一樣)
82
112
 
83
- # 2) 不改原則,改叫 .cmd 啟動器(功能完全一樣)
113
+ ```powershell
114
+ npm.cmd i -g @tsa-group/claude-usage
115
+ claude-usage.cmd configure --server https://<你們的 ingest host>
116
+ claude-usage.cmd install
84
117
  claude-usage.cmd status
85
118
  ```
86
119
 
87
- `RemoteSigned` = 本機與 npm 的腳本可跑,只擋網路下載的未簽章腳本。
88
- 公司用 GPO 鎖了機器層級原則時 (1) 會被覆寫 —— 直接用 (2)。
89
- 現況查詢:`Get-ExecutionPolicy -List`
120
+ > 公司用 GPO 鎖了機器層級原則時,**A 會被覆寫** —— 直接用 B。
121
+ > 查誰在管:`Get-ExecutionPolicy -List`
122
+
123
+ ### 第 1 步:安裝
124
+
125
+ ```powershell
126
+ npm i -g @tsa-group/claude-usage
90
127
 
91
- > 安裝時若偵測到會擋的原則,`postinstall` 會主動提醒;事後也可用
92
- > `claude-usage.cmd doctor` 檢查。**但工具無法在被擋的當下自己說話** ——
93
- > PowerShell 在 Node 啟動前就拒絕了,所以這段提示只能寫在這裡。
128
+ claude-usage configure --server https://<你們的 ingest host>
129
+ claude-usage install --dry-run
130
+ claude-usage install
131
+ claude-usage status
132
+ ```
133
+
134
+ (走 B 方案的人全部加 `.cmd`。)
135
+
136
+ ### 你是用終端機的 `claude`,還是桌面版 App?
137
+
138
+ **兩種都支援,不必改用哪一個** —— 但憑證存放位置完全不同,卡住時要知道差別:
139
+
140
+ | | 終端機 CLI | 桌面版 App |
141
+ |---|---|---|
142
+ | 憑證位置 | `%USERPROFILE%\.claude\.credentials.json` | 桌面版自己的加密儲存 |
143
+ | 形式 | 純文字 | DPAPI + AES-256-GCM 加密 |
144
+ | 需要什麼 | 跑過 `claude` 並完成登入 | 桌面版登入過 |
145
+
146
+ **只用桌面版、從沒登入過終端機 `claude` 的人也能用** —— 工具會自動去解桌面版的憑證庫
147
+ (見「隱私」段)。解密只在你自己的機器、你自己的 Windows 帳號下進行。
148
+
149
+ 不想走那條路的話,在終端機跑一次 `claude` 登入即可,工具會優先用那份純文字憑證。
150
+
151
+ ---
152
+
153
+ ## 確認安裝成功
154
+
155
+ ```bash
156
+ claude-usage status
157
+ ```
158
+
159
+ 成功長這樣(重點是標示的那三行):
160
+
161
+ ```
162
+ state dir: /Users/you/.claude-usage
163
+ server: https://<你們的 ingest host>
164
+ enrolled: yes <-- 拿到身份了
165
+ hook: registered <-- 開 session 時會自動採樣
166
+ last_sample_status : ok
167
+ last_upload_state : uploaded
168
+ status : ok <-- 採集正常
169
+ state = not running (背景任務平常就是閒置,每 5 分鐘才醒一次)
170
+ runs = 12
171
+ ```
172
+
173
+ `state = not running` 是**正常的** —— 背景任務不是常駐程式,每 5 分鐘被叫醒一次。
174
+ `runs` 有在累加就代表它真的有跑。
175
+
176
+ `status` 在採集不健康時會 **exit 1**,可以接監控。
94
177
 
95
- **兩個可能讓你以為「裝壞了」的情況:**
178
+ **接著還有一步不在你這邊**:若你們的 server 開了人工核准,`enroll` 會回
179
+ `status: pending` —— 你這邊一切正常,但要等管理者按核准,資料才會開始進去。
180
+ **這是正常的,不是裝壞了。**
96
181
 
97
- - **server 開了人工核准** → `enroll` 會回 `status: pending`,你的資料要等管理者核准後才
98
- 開始進來。`status` 會顯示已 enroll、心跳正常,但 server 端看不到你的用量。這是正常的。
99
- - **server 要求 enroll 密語**(多數部署不需要)→ `install --enroll-secret <密語>`,或用
100
- 環境變數 `CLAUDE_USAGE_ENROLL_SECRET`。⚠️ 寫在指令列會留在你的 shell history 裡,
101
- 能用環境變數就用環境變數。
182
+ ---
183
+
184
+ ## 卡住了?先跑這個
185
+
186
+ ```bash
187
+ claude-usage doctor # Windows 走 B 方案的人:claude-usage.cmd doctor
188
+ ```
189
+
190
+ 它會印出**解析到的每個路徑,與那裡實際有什麼** —— 路徑對不對通常你一眼就看得出來
191
+ (「那不是我 Claude Code 裝的地方」),前提是有人把路徑印出來。
102
192
 
103
- 安裝後會發生:
193
+ 把整段輸出貼給管理者即可,裡面**不含任何 token 值**。
104
194
 
105
- - **hook**(開 session 時):抓一筆即時額度快照。註冊在 `~/.claude/settings.json` 的
106
- `SessionStart`,**fire-and-forget、失敗永遠安靜、10 分鐘去抖動** —— 採集用量絕不該擋住或
107
- 拖慢你開 session。
108
- - **背景任務**(macOS launchd / Windows 工作排程器):每 5 分鐘喚醒一次,**自我判斷**是否要
109
- 採樣,並非同步上傳。採樣間隔自適應:有在用 15 分鐘、閒置退避到 60 分鐘。
110
- - 升級(`npm update -g @tsa-group/claude-usage`)後**請重跑一次 `claude-usage install`**:
111
- 背景任務與 hook 記的是絕對路徑,換了 Node 版本或安裝位置就要重新註冊。
195
+ 常見症狀對照:
196
+
197
+ | 症狀 | 多半是 |
198
+ |---|---|
199
+ | `UnauthorizedAccess` / `PSSecurityException` | PowerShell 執行原則,見 Windows 第 0 步 |
200
+ | `cannot read credentials (… file-no-oauth-key …)` | 只用桌面版、沒登入過終端機 `claude`。0.4.0 起會自動去讀桌面版憑證;若仍失敗,`doctor` 會指出卡在哪一步 |
201
+ | `access token EXPIRED` | 重新登入 Claude Code(終端機 `/login` 或桌面版)即可換發 |
202
+ | `status` 一切正常但 server 看不到我的用量 | 等管理者核准(見上) |
203
+ | `沒有任何 .jsonl` | 本工具與 Claude Code 不在同一個環境(WSL?另一個帳號?`CLAUDE_CONFIG_DIR`?) |
112
204
 
113
205
  ---
114
206
 
@@ -251,8 +343,7 @@ Client 對 server 只用兩個端點,皆為 `application/json`:
251
343
  - **Windows 背景任務未完整實機驗證**:schtasks + VBS 隱藏視窗啟動器。裝完請用
252
344
  `claude-usage status` 確認有心跳。
253
345
  - **PowerShell 執行原則**:預設 `Restricted` 會同時擋掉 `npm.ps1`(安裝時)與
254
- `claude-usage.ps1`(執行時)。安裝用 `npm.cmd i -g …`;其餘見上面
255
- 「Windows / PowerShell 首次執行」。
346
+ `claude-usage.ps1`(執行時)。處理方式見上面 **Windows 0 步**。
256
347
  - **Linux 背景任務未實作**(systemd --user timer)。其餘指令可用。
257
348
  - **`session_id` 跨 compaction / resume 不穩定**:session **數**會高估。token 與成本不受影響
258
349
  (那是 per-event 去重的,與 session 身份無關)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsa-group/claude-usage",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Per-user Claude usage collector — measures Claude Code token detail and account-level rate-limit utilization locally, reports to your own ingest server.",
5
5
  "type": "module",
6
6
  "bin": {