@specode/pi-subscription-usage 1.4.0 → 1.5.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 +35 -3
- package/README.zh-CN.md +35 -3
- package/package.json +4 -3
- package/src/format.ts +1 -0
- package/src/providers/claude-bridge.ts +118 -0
- package/src/query.ts +22 -0
- package/src/types.ts +2 -1
- package/src/usage.ts +51 -3
package/README.md
CHANGED
|
@@ -39,7 +39,8 @@ To sign in, run `/login`, pick `Sign in with an account` (Account below) or `Sig
|
|
|
39
39
|
| OpenCode Go | API key → `OpenCode Go` | `5h`, `1w`, `1m` | — | |
|
|
40
40
|
| xAI Grok | Account → `xAI` | `1w` and/or `1m` | — | |
|
|
41
41
|
| Kimi Coding | Account → `Kimi For Coding` | `5h`, `1w`, membership level | — | |
|
|
42
|
-
| Antigravity | Account → `Antigravity` | Gemini (`5h`, `1w`), Claude and GPT (`5h`, `1w`), Gemini 4 Argon (`1w`), plan, email, project | — | [`pi-antigravity`](https://
|
|
42
|
+
| Antigravity | Account → `Antigravity` | Gemini (`5h`, `1w`), Claude and GPT (`5h`, `1w`), Gemini 4 Argon (`1w`), plan, email, project | — | [`pi-antigravity`](https://www.npmjs.com/package/pi-antigravity) |
|
|
43
|
+
| Claude (Claude Code) | None; signed in through Claude Code | `5h`, `1w` | — | [`pi-claude-bridge`](https://www.npmjs.com/package/pi-claude-bridge) 0.9.3 or later |
|
|
43
44
|
|
|
44
45
|
### Provider Notes
|
|
45
46
|
|
|
@@ -53,7 +54,37 @@ Sign in with your SuperGrok account (Account → `xAI`). Signing in with an xAI
|
|
|
53
54
|
|
|
54
55
|
#### Antigravity
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
Requires [`pi-antigravity`](https://www.npmjs.com/package/pi-antigravity) by Rahul Arya ([GitHub](https://github.com/Rahularya01/pi-antigravity)). Install it with `pi install npm:pi-antigravity`; `Antigravity` then appears under `Sign in with an account` in `/login`. Quota is split into three separate shared pools: Gemini models (`5h` and `1w`), Claude and GPT models (`5h` and `1w`), and Gemini 4 Argon (`1w`). The footer shows the pool of the model you are using: Claude and GPT-OSS models show the Claude and GPT pool, Gemini 4 Argon shows the Argon pool, and other Gemini models show the Gemini pool. Quota data requires a paid Google AI subscription; free-tier accounts show `Unsupported` in `/usage` and display nothing in the footer instead of an error.
|
|
58
|
+
|
|
59
|
+
Tested with `pi-antigravity` 0.11.0. Every earlier version back to 0.1.0 registers the same provider id, Google endpoint, and credential format, so they are expected to work too.
|
|
60
|
+
|
|
61
|
+
Several packages have similar names. The extension recognizes the `antigravity` provider id and reads quota from Google's Cloud Code API with the credential Pi resolves for it, so the package must register that id and serve models from Google's endpoint. Install only one package that registers `antigravity`. As of October 31, 2026:
|
|
62
|
+
|
|
63
|
+
| Package | Result |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `pi-antigravity` | Works |
|
|
66
|
+
| `@gwylook/pi-antigravity`, `@dereknex/pi-antigravity` (forks of `pi-antigravity`), `@heyhuynhgiabuu/pi-oauth-antigravity`, `@raquezha/antigravity`, `pi-provider-antigravity` | Same provider id, Google endpoint, and credential format; expected to work, untested |
|
|
67
|
+
| `@estebanforge/pi-antigravity-bridge`, `@thoth-agents/pi-antigravity-bridge` | Register `antigravity` but run models through a local Antigravity bridge (`agy-bridge://`): unsupported, so the footer stays empty and `/usage` explains why |
|
|
68
|
+
| `@cortexkit/pi-antigravity-auth` (`google`), `pi-antigravity-acp-provider` and `@dapidev/pi-antigravity-pure` (`antigravity-acp`), `@ackneal/pi-agy-bridge` (`agy`) | Register a different provider id; not supported |
|
|
69
|
+
|
|
70
|
+
#### Claude (Claude Code)
|
|
71
|
+
|
|
72
|
+
Requires [`pi-claude-bridge`](https://www.npmjs.com/package/pi-claude-bridge) by elidickinson ([GitHub](https://github.com/elidickinson/pi-claude-bridge)), version 0.9.3 or later. 0.9.3 is the first version that passes Claude Code's usage reports on to Pi. Install it with `pi install npm:pi-claude-bridge` and sign in to Claude Code itself; there is no Pi login. Usage covers models under the `claude-bridge` provider; Claude models served by Antigravity use the Antigravity pool above.
|
|
73
|
+
|
|
74
|
+
The bridge has no usage endpoint to query: Claude Code attaches your plan usage to its responses, and the extension reads that report. So the footer stays empty in each new session until the first report arrives, `/usage` shows the last reported values with the time they were reported instead of fetching, and a window whose reset time has passed shows as unused until the next report. The extension never reads Claude Code's credentials.
|
|
75
|
+
|
|
76
|
+
Tested with `pi-claude-bridge` 0.9.3 and Claude Code 2.1.311, the version bundled with the bridge's Agent SDK. If you point the bridge at your own `claude` executable (`pathToClaudeCodeExecutable`), older Claude Code versions are untested.
|
|
77
|
+
|
|
78
|
+
Several packages have similar names. The extension recognizes the `claude-bridge` provider id, so the package must both register that id and pass usage reports on. As of October 31, 2026:
|
|
79
|
+
|
|
80
|
+
| Package | Result |
|
|
81
|
+
| --- | --- |
|
|
82
|
+
| `pi-claude-bridge` 0.9.3 or later | Works |
|
|
83
|
+
| `@viniciosrab/pi-claude-bridge` 0.9.4-fork.3 | Same usage code as 0.9.3; expected to work, untested |
|
|
84
|
+
| `pi-claude-bridge` before 0.9.3, `@fractaal/pi-claude-bridge`, `@yassimba/pi-claude-bridge`, `@agimon-ai/po-claude-bridge` | Register `claude-bridge` but never pass usage on: the footer stays empty and `/usage` keeps showing `Waiting` |
|
|
85
|
+
| `@vanillagreen/pi-claude-bridge`, `pi-claude-subscription`, `pi-claude-directsdk`, `@ramtinj95/pi-claude-delegation` | Register a different provider id; not supported |
|
|
86
|
+
|
|
87
|
+
The npm package `claude-bridge`, without the `pi-` prefix, is an unrelated proxy server for the Claude Code CLI; `pi install npm:claude-bridge` installs the wrong package. Run `pi list` to check which package you have.
|
|
57
88
|
|
|
58
89
|
## Installation
|
|
59
90
|
|
|
@@ -81,6 +112,7 @@ Sign in through `/login` as described above, then select a model from that provi
|
|
|
81
112
|
|
|
82
113
|
- The footer status bar shows the active quota windows and refreshes after each turn and every 5 minutes (using a 5-minute cache).
|
|
83
114
|
- Running `/usage` always fetches fresh data for the active provider and shows progress bars, reset times (`MM/DD HH:mm`), and account details.
|
|
115
|
+
- Claude is the exception: its quota is never fetched, only read from Claude Code's responses (see [Claude (Claude Code)](#claude-claude-code)).
|
|
84
116
|
|
|
85
117
|
### Reset Credits (OpenAI and Codex)
|
|
86
118
|
|
|
@@ -110,7 +142,7 @@ Run `/reload` after changing the config file. `displayMode` applies to the foote
|
|
|
110
142
|
|
|
111
143
|
## Privacy and Safety
|
|
112
144
|
|
|
113
|
-
- Credentials come only from Pi's own login (provider registry). The extension never reads browser cookies or other tools' credential files (for example, Grok never reads `~/.grok/auth.json
|
|
145
|
+
- Credentials come only from Pi's own login (provider registry). The extension never reads browser cookies or other tools' credential files (for example, Grok never reads `~/.grok/auth.json`, and Claude usage comes from the bridge's responses, not Claude Code's keychain entry).
|
|
114
146
|
- Requests go only to each provider's official endpoints. Custom proxies and custom base URLs are refused.
|
|
115
147
|
- Tokens are never written to caches, session files, the status bar, or error messages.
|
|
116
148
|
- The extension does not modify model requests.
|
package/README.zh-CN.md
CHANGED
|
@@ -39,7 +39,8 @@ OpenAI Codex
|
|
|
39
39
|
| OpenCode Go | API key → `OpenCode Go` | `5h`、`1w`、`1m` | — | |
|
|
40
40
|
| xAI Grok | Account → `xAI` | `1w` 和/或 `1m` | — | |
|
|
41
41
|
| Kimi Coding | Account → `Kimi For Coding` | `5h`、`1w`、会员等级 | — | |
|
|
42
|
-
| Antigravity | Account → `Antigravity` | Gemini(`5h`、`1w`)、Claude 与 GPT(`5h`、`1w`)、Gemini 4 Argon(`1w`)、套餐、邮箱、项目 | — | [`pi-antigravity`](https://
|
|
42
|
+
| Antigravity | Account → `Antigravity` | Gemini(`5h`、`1w`)、Claude 与 GPT(`5h`、`1w`)、Gemini 4 Argon(`1w`)、套餐、邮箱、项目 | — | [`pi-antigravity`](https://www.npmjs.com/package/pi-antigravity) |
|
|
43
|
+
| Claude (Claude Code) | 无需在 Pi 登录,使用 Claude Code 的登录 | `5h`、`1w` | — | [`pi-claude-bridge`](https://www.npmjs.com/package/pi-claude-bridge) 0.9.3 及以上 |
|
|
43
44
|
|
|
44
45
|
### 提供商说明
|
|
45
46
|
|
|
@@ -53,7 +54,37 @@ OpenAI Codex
|
|
|
53
54
|
|
|
54
55
|
#### Antigravity
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
需要 Rahul Arya 发布的 [`pi-antigravity`](https://www.npmjs.com/package/pi-antigravity)([GitHub](https://github.com/Rahularya01/pi-antigravity))。用 `pi install npm:pi-antigravity` 安装,之后 `/login` 的 `Sign in with an account` 列表里才会出现 `Antigravity`。额度分为三个独立的共享池:Gemini 模型(`5h` 与 `1w`)、Claude 与 GPT 模型(`5h` 与 `1w`)、Gemini 4 Argon(`1w`)。底部状态栏显示当前所用模型对应的额度池:用 Claude 或 GPT-OSS 模型时显示 Claude 与 GPT 池,用 Gemini 4 Argon 时显示 Argon 池,其他 Gemini 模型显示 Gemini 池。额度数据只对 Google AI 付费订阅开放;免费账号在 `/usage` 里显示 `Unsupported`,底部状态栏不显示内容,不会报错。
|
|
58
|
+
|
|
59
|
+
已用 `pi-antigravity` 0.11.0 测试。0.1.0 以来的各个版本注册的 provider id、Google 接口地址和凭据格式都相同,预计同样可用。
|
|
60
|
+
|
|
61
|
+
npm 上有好几个名字相近的包。扩展按 `antigravity` 这个 provider id 识别,并用 Pi 为它提供的凭据向 Google 的 Cloud Code 接口查询额度,所以所用的包既要注册这个 id,模型也要走 Google 的接口。注册 `antigravity` 的包只装一个。截至 2026 年 10 月 31 日:
|
|
62
|
+
|
|
63
|
+
| 包 | 结果 |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `pi-antigravity` | 可用 |
|
|
66
|
+
| `@gwylook/pi-antigravity`、`@dereknex/pi-antigravity`(`pi-antigravity` 的 fork)、`@heyhuynhgiabuu/pi-oauth-antigravity`、`@raquezha/antigravity`、`pi-provider-antigravity` | provider id、Google 接口地址和凭据格式都相同,预计可用,未测试 |
|
|
67
|
+
| `@estebanforge/pi-antigravity-bridge`、`@thoth-agents/pi-antigravity-bridge` | 注册了 `antigravity`,但模型经由本地 Antigravity 桥(`agy-bridge://`)运行:不支持,底部状态栏为空,`/usage` 会说明原因 |
|
|
68
|
+
| `@cortexkit/pi-antigravity-auth`(`google`)、`pi-antigravity-acp-provider` 与 `@dapidev/pi-antigravity-pure`(`antigravity-acp`)、`@ackneal/pi-agy-bridge`(`agy`) | 注册的是别的 provider id,不支持 |
|
|
69
|
+
|
|
70
|
+
#### Claude (Claude Code)
|
|
71
|
+
|
|
72
|
+
需要 elidickinson 发布的 [`pi-claude-bridge`](https://www.npmjs.com/package/pi-claude-bridge)([GitHub](https://github.com/elidickinson/pi-claude-bridge))0.9.3 及以上版本,0.9.3 是第一个把 Claude Code 的用量数据转交给 Pi 的版本。用 `pi install npm:pi-claude-bridge` 安装,并在 Claude Code 里登录,Pi 这边不用登录。这里指 `claude-bridge` 提供商下的模型;通过 Antigravity 使用的 Claude 模型按上文的 Antigravity 额度池显示。
|
|
73
|
+
|
|
74
|
+
bridge 没有可查询的用量接口:Claude Code 会在回复中附带当前套餐用量,扩展读取的就是这份数据。因此每个新会话要等收到第一份用量数据,底部状态栏才有内容;`/usage` 不会重新查询,只显示最近一次收到的数值和对应时间;如果某个窗口的重置时间已过,在收到下一份数据之前按未使用显示。扩展不会读取 Claude Code 的凭据。
|
|
75
|
+
|
|
76
|
+
已用 `pi-claude-bridge` 0.9.3 和 Claude Code 2.1.311(bridge 所带 Agent SDK 内置的版本)测试。如果你让 bridge 改用自己安装的 `claude` 可执行文件(`pathToClaudeCodeExecutable`),较旧的 Claude Code 版本未经测试。
|
|
77
|
+
|
|
78
|
+
npm 上有好几个名字相近的包。扩展按 `claude-bridge` 这个 provider id 识别,所以所用的包既要注册这个 id,也要转交用量数据。截至 2026 年 10 月 31 日:
|
|
79
|
+
|
|
80
|
+
| 包 | 结果 |
|
|
81
|
+
| --- | --- |
|
|
82
|
+
| `pi-claude-bridge` 0.9.3 及以上 | 可用 |
|
|
83
|
+
| `@viniciosrab/pi-claude-bridge` 0.9.4-fork.3 | 用量相关代码与 0.9.3 相同,预计可用,未测试 |
|
|
84
|
+
| `pi-claude-bridge` 0.9.3 以前的版本、`@fractaal/pi-claude-bridge`、`@yassimba/pi-claude-bridge`、`@agimon-ai/po-claude-bridge` | 注册了 `claude-bridge`,但不转交用量:底部状态栏一直为空,`/usage` 一直显示 `Waiting` |
|
|
85
|
+
| `@vanillagreen/pi-claude-bridge`、`pi-claude-subscription`、`pi-claude-directsdk`、`@ramtinj95/pi-claude-delegation` | 注册的是别的 provider id,不支持 |
|
|
86
|
+
|
|
87
|
+
不带 `pi-` 前缀的 npm 包 `claude-bridge` 是一个与此无关的 Claude Code CLI 代理服务器,`pi install npm:claude-bridge` 会装错包。可以用 `pi list` 确认装的是哪个。
|
|
57
88
|
|
|
58
89
|
## 安装
|
|
59
90
|
|
|
@@ -81,6 +112,7 @@ pi install /path/to/pi-subscription-usage
|
|
|
81
112
|
|
|
82
113
|
- 底部状态栏会自动显示当前额度窗口,在每轮对话结束后及每 5 分钟刷新一次(带 5 分钟缓存)。
|
|
83
114
|
- 执行 `/usage` 会跳过缓存直接拉取最新数据,显示进度条、重置时间(`MM/DD HH:mm`)及账号信息。
|
|
115
|
+
- Claude 例外:不会主动查询额度,只读取 Claude Code 回复中附带的数据(见 [Claude (Claude Code)](#claude-claude-code))。
|
|
84
116
|
|
|
85
117
|
### 使用重置券(OpenAI 与 Codex)
|
|
86
118
|
|
|
@@ -110,7 +142,7 @@ OpenAI 或 Codex 账号里有可用的限额重置券时,`/usage` 会提供使
|
|
|
110
142
|
|
|
111
143
|
## 隐私与安全
|
|
112
144
|
|
|
113
|
-
- 仅从 Pi 自身的登录注册表读取凭据,不读取浏览器 Cookie 或其他工具的凭据文件(例如 Grok 不会读取 `~/.grok/auth.json
|
|
145
|
+
- 仅从 Pi 自身的登录注册表读取凭据,不读取浏览器 Cookie 或其他工具的凭据文件(例如 Grok 不会读取 `~/.grok/auth.json`;Claude 用量来自 bridge 返回的数据,不读取 Claude Code 存在钥匙串里的凭据)。
|
|
114
146
|
- 仅向各提供商的官方接口发起请求,拒绝自定义代理和自定义 Base URL。
|
|
115
147
|
- 令牌不会写入缓存、会话文件、状态栏或报错信息。
|
|
116
148
|
- 不修改任何模型请求。
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@specode/pi-subscription-usage",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Show subscription quota for OpenAI, Codex, OpenCode Go, Grok, Kimi, and
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"description": "Show subscription quota for OpenAI, Codex, OpenCode Go, Grok, Kimi, Antigravity, and Claude (via pi-claude-bridge), plus OpenAI and Codex account reset credits in Pi",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Specode",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"opencode",
|
|
24
24
|
"grok",
|
|
25
25
|
"kimi",
|
|
26
|
-
"antigravity"
|
|
26
|
+
"antigravity",
|
|
27
|
+
"claude"
|
|
27
28
|
],
|
|
28
29
|
"files": [
|
|
29
30
|
"index.ts",
|
package/src/format.ts
CHANGED
|
@@ -79,6 +79,7 @@ export function formatProviderState(
|
|
|
79
79
|
let status = "Query Failed";
|
|
80
80
|
if (state.status === "auth-unavailable") status = "Auth Unavailable";
|
|
81
81
|
else if (state.status === "unsupported") status = "Unsupported";
|
|
82
|
+
else if (state.status === "pending") status = "Waiting";
|
|
82
83
|
return `${state.providerName}\n ${status}: ${state.message}`;
|
|
83
84
|
}
|
|
84
85
|
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// pi-claude-bridge has no usage endpoint Pi can call: Claude Code reports plan
|
|
2
|
+
// usage as rate_limit_event messages, which the bridge replays to Pi as
|
|
3
|
+
// `anthropic-ratelimit-unified-*` headers on after_provider_response.
|
|
4
|
+
// This module turns those headers into a report; nothing here sends a request.
|
|
5
|
+
import type { UsageBucket, UsageReport } from "../types.ts";
|
|
6
|
+
|
|
7
|
+
export const CLAUDE_BRIDGE_PROVIDER_ID = "claude-bridge";
|
|
8
|
+
|
|
9
|
+
const HEADER_PREFIX = "anthropic-ratelimit-unified-";
|
|
10
|
+
// Observed: utilization is a 0..1 fraction. Anything far above 1 means the
|
|
11
|
+
// scale changed, and a guessed percentage would be worse than none.
|
|
12
|
+
const MAX_UTILIZATION = 1.5;
|
|
13
|
+
// Unix seconds; a millisecond value would land past this bound.
|
|
14
|
+
const MAX_EPOCH_SECONDS = 10_000_000_000;
|
|
15
|
+
|
|
16
|
+
const CLAUDE_WINDOWS: readonly {
|
|
17
|
+
abbreviation: string;
|
|
18
|
+
id: string;
|
|
19
|
+
label: string;
|
|
20
|
+
windowMinutes: number;
|
|
21
|
+
}[] = [
|
|
22
|
+
{ abbreviation: "5h", id: "five-hour", label: "5h window", windowMinutes: 300 },
|
|
23
|
+
{ abbreviation: "7d", id: "seven-day", label: "Weekly window", windowMinutes: 10_080 },
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/** Undefined when the headers carry no usable window, so a non-Claude response leaves the last observation alone. */
|
|
27
|
+
export function normalizeClaudeRateLimitHeaders(
|
|
28
|
+
headers: Record<string, string | undefined>,
|
|
29
|
+
capturedAt: number,
|
|
30
|
+
): UsageReport | undefined {
|
|
31
|
+
const lookup = new Map(
|
|
32
|
+
Object.entries(headers).map(([name, value]) => [name.toLowerCase(), value]),
|
|
33
|
+
);
|
|
34
|
+
// The event names no provider, and a model switch mid-stream can deliver a
|
|
35
|
+
// real Anthropic response under claude-bridge with the same header names.
|
|
36
|
+
// Only the bridge's synthetic response consists of nothing but these.
|
|
37
|
+
if ([...lookup.keys()].some((name) => !name.startsWith(HEADER_PREFIX))) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const buckets: UsageBucket[] = [];
|
|
41
|
+
for (const window of CLAUDE_WINDOWS) {
|
|
42
|
+
const utilization = parseNumber(
|
|
43
|
+
lookup.get(`${HEADER_PREFIX}${window.abbreviation}-utilization`),
|
|
44
|
+
);
|
|
45
|
+
if (
|
|
46
|
+
utilization === undefined ||
|
|
47
|
+
utilization < 0 ||
|
|
48
|
+
utilization > MAX_UTILIZATION
|
|
49
|
+
) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const used = utilization * 100;
|
|
53
|
+
const resetsAt = parseNumber(
|
|
54
|
+
lookup.get(`${HEADER_PREFIX}${window.abbreviation}-reset`),
|
|
55
|
+
);
|
|
56
|
+
buckets.push({
|
|
57
|
+
id: window.id,
|
|
58
|
+
label: window.label,
|
|
59
|
+
used,
|
|
60
|
+
remaining: Math.max(0, 100 - used),
|
|
61
|
+
unit: "percent",
|
|
62
|
+
windowMinutes: window.windowMinutes,
|
|
63
|
+
...(resetsAt !== undefined && resetsAt > 0 && resetsAt < MAX_EPOCH_SECONDS
|
|
64
|
+
? { resetsAt: Math.floor(resetsAt) }
|
|
65
|
+
: {}),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (buckets.length === 0) return undefined;
|
|
69
|
+
return {
|
|
70
|
+
providerId: CLAUDE_BRIDGE_PROVIDER_ID,
|
|
71
|
+
providerName: "Claude (Claude Code)",
|
|
72
|
+
capturedAt,
|
|
73
|
+
source: "claude-code-rate-limit-event",
|
|
74
|
+
semantics: {
|
|
75
|
+
kind: "consumer-subscription",
|
|
76
|
+
label: "Claude subscription limits",
|
|
77
|
+
},
|
|
78
|
+
buckets,
|
|
79
|
+
metrics: [],
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Claude Code only reports usage after a request, so an observation outlives
|
|
85
|
+
* its windows. A window whose reset has passed starts empty; showing the old
|
|
86
|
+
* percentage would overstate usage until the next response.
|
|
87
|
+
*/
|
|
88
|
+
export function settleClaudeReport(
|
|
89
|
+
report: UsageReport,
|
|
90
|
+
nowMs: number,
|
|
91
|
+
): UsageReport {
|
|
92
|
+
const nowSeconds = nowMs / 1_000;
|
|
93
|
+
return {
|
|
94
|
+
...report,
|
|
95
|
+
buckets: report.buckets.map((bucket) => {
|
|
96
|
+
if (bucket.resetsAt === undefined || bucket.resetsAt > nowSeconds) {
|
|
97
|
+
return bucket;
|
|
98
|
+
}
|
|
99
|
+
const { resetsAt: _expired, ...rest } = bucket;
|
|
100
|
+
return { ...rest, used: 0, remaining: 100 };
|
|
101
|
+
}),
|
|
102
|
+
notes: [`Observed after the last Claude response at ${formatClock(report.capturedAt)}; Claude Code reports usage only with its responses.`],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function parseNumber(value: string | undefined): number | undefined {
|
|
107
|
+
if (value === undefined || !value.trim()) return undefined;
|
|
108
|
+
const parsed = Number(value);
|
|
109
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function formatClock(epochMs: number): string {
|
|
113
|
+
const date = new Date(epochMs);
|
|
114
|
+
if (Number.isNaN(date.getTime())) return "an unknown time";
|
|
115
|
+
const hour = date.getHours().toString().padStart(2, "0");
|
|
116
|
+
const minute = date.getMinutes().toString().padStart(2, "0");
|
|
117
|
+
return `${hour}:${minute}`;
|
|
118
|
+
}
|
package/src/query.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
fingerprintResolvedAuth,
|
|
8
8
|
isAbortError,
|
|
9
9
|
redactUsageError,
|
|
10
|
+
sanitizeDisplayText,
|
|
10
11
|
} from "./core.ts";
|
|
11
12
|
import { CODEX_PROVIDER_ID } from "./providers/codex-constants.ts";
|
|
12
13
|
import {
|
|
@@ -346,6 +347,18 @@ export async function resolveUsageAuth(
|
|
|
346
347
|
const UnsupportedModeError = adapter.id === OPENAI_PROVIDER_ID
|
|
347
348
|
? UsageUnsupportedError
|
|
348
349
|
: Error;
|
|
350
|
+
// Packages that reuse a provider id over another transport (for example
|
|
351
|
+
// pi-antigravity-bridge's agy-bridge://) are not a proxy to refuse loudly.
|
|
352
|
+
if (
|
|
353
|
+
current &&
|
|
354
|
+
adapter.providerIds.includes(current.provider) &&
|
|
355
|
+
current.baseUrl &&
|
|
356
|
+
!isHttpUrl(current.baseUrl)
|
|
357
|
+
) {
|
|
358
|
+
throw new UsageUnsupportedError(
|
|
359
|
+
`${adapter.displayName} usage is unavailable for models served over ${sanitizeDisplayText(current.baseUrl, 60)}; only the provider's own HTTPS API reports quota.`,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
349
362
|
if (
|
|
350
363
|
current &&
|
|
351
364
|
adapter.providerIds.includes(current.provider) &&
|
|
@@ -626,6 +639,15 @@ function candidateModels(
|
|
|
626
639
|
return candidates;
|
|
627
640
|
}
|
|
628
641
|
|
|
642
|
+
function isHttpUrl(value: string): boolean {
|
|
643
|
+
try {
|
|
644
|
+
const { protocol } = new URL(value);
|
|
645
|
+
return protocol === "https:" || protocol === "http:";
|
|
646
|
+
} catch {
|
|
647
|
+
return false;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
629
651
|
function hasOfficialOrigin(
|
|
630
652
|
value: string | undefined,
|
|
631
653
|
adapter: UsageProviderAdapter,
|
package/src/types.ts
CHANGED
|
@@ -85,6 +85,7 @@ export type ProviderUsageState =
|
|
|
85
85
|
| {
|
|
86
86
|
providerId: string;
|
|
87
87
|
providerName: string;
|
|
88
|
-
|
|
88
|
+
/** `pending`: an observed provider has not reported usage yet. */
|
|
89
|
+
status: "unsupported" | "auth-unavailable" | "query-failed" | "pending";
|
|
89
90
|
message: string;
|
|
90
91
|
};
|
package/src/usage.ts
CHANGED
|
@@ -30,6 +30,11 @@ import {
|
|
|
30
30
|
resolveCodexResetAuth,
|
|
31
31
|
} from "./codex-resets.ts";
|
|
32
32
|
import { formatProviderState } from "./format.ts";
|
|
33
|
+
import {
|
|
34
|
+
CLAUDE_BRIDGE_PROVIDER_ID,
|
|
35
|
+
normalizeClaudeRateLimitHeaders,
|
|
36
|
+
settleClaudeReport,
|
|
37
|
+
} from "./providers/claude-bridge.ts";
|
|
33
38
|
import { CODEX_PROVIDER_ID } from "./providers/codex-constants.ts";
|
|
34
39
|
import { OPENAI_PROVIDER_ID } from "./providers/openai.ts";
|
|
35
40
|
import {
|
|
@@ -50,6 +55,7 @@ import type {
|
|
|
50
55
|
ResolvedUsageAuth,
|
|
51
56
|
UsageModel,
|
|
52
57
|
UsageProviderAdapter,
|
|
58
|
+
UsageReport,
|
|
53
59
|
} from "./types.ts";
|
|
54
60
|
|
|
55
61
|
const CACHE_TTL_MS = 5 * 60 * 1_000;
|
|
@@ -79,6 +85,8 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
79
85
|
let statusGeneration = 0;
|
|
80
86
|
let statusController: AbortController | undefined;
|
|
81
87
|
let statusTimer: ReturnType<typeof setTimeout> | undefined;
|
|
88
|
+
// Holds no credential, so session_shutdown leaves it; nothing reads it after.
|
|
89
|
+
let claudeObservation: UsageReport | undefined;
|
|
82
90
|
|
|
83
91
|
function emitUsageStatus(event: UsageStatusEvent): void {
|
|
84
92
|
pi.events.emit(USAGE_STATUS_EVENT, event);
|
|
@@ -135,7 +143,10 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
135
143
|
model: UsageModel,
|
|
136
144
|
schedule: boolean,
|
|
137
145
|
): void {
|
|
138
|
-
if (
|
|
146
|
+
if (
|
|
147
|
+
outcome.state.status === "unsupported" ||
|
|
148
|
+
outcome.state.status === "pending"
|
|
149
|
+
) {
|
|
139
150
|
clearStatusTimer();
|
|
140
151
|
safeSetStatus(ctx, undefined);
|
|
141
152
|
emitUnavailableUsage();
|
|
@@ -280,6 +291,7 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
280
291
|
force: boolean,
|
|
281
292
|
signal: AbortSignal,
|
|
282
293
|
): Promise<QueryOutcome> {
|
|
294
|
+
if (isObservedProvider(model)) return observedClaudeOutcome();
|
|
283
295
|
const adapter = adapterForProvider(model?.provider);
|
|
284
296
|
if (!adapter) {
|
|
285
297
|
return {
|
|
@@ -296,6 +308,29 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
296
308
|
return queryAdapterState(ctx, adapter, force, signal);
|
|
297
309
|
}
|
|
298
310
|
|
|
311
|
+
/** Claude has no query to force; /usage and refreshes reread the last observation. */
|
|
312
|
+
function observedClaudeOutcome(): QueryOutcome {
|
|
313
|
+
const providerName = "Claude (Claude Code)";
|
|
314
|
+
if (!claudeObservation) {
|
|
315
|
+
return {
|
|
316
|
+
state: {
|
|
317
|
+
providerId: CLAUDE_BRIDGE_PROVIDER_ID,
|
|
318
|
+
providerName,
|
|
319
|
+
status: "pending",
|
|
320
|
+
message: "Claude Code has not reported usage in this session yet. It reports usage with its responses, which requires pi-claude-bridge 0.9.3 or later.",
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
state: {
|
|
326
|
+
providerId: CLAUDE_BRIDGE_PROVIDER_ID,
|
|
327
|
+
providerName,
|
|
328
|
+
status: "ready",
|
|
329
|
+
report: settleClaudeReport(claudeObservation, Date.now()),
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
299
334
|
async function outcomeStillCurrent(
|
|
300
335
|
ctx: ExtensionContext,
|
|
301
336
|
model: UsageModel | undefined,
|
|
@@ -344,7 +379,8 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
344
379
|
model: UsageModel | undefined,
|
|
345
380
|
force: boolean,
|
|
346
381
|
): Promise<void> {
|
|
347
|
-
|
|
382
|
+
const observed = isObservedProvider(model);
|
|
383
|
+
if ((!observed && !adapterForProvider(model?.provider)) || !model) {
|
|
348
384
|
clearStatus(ctx);
|
|
349
385
|
return;
|
|
350
386
|
}
|
|
@@ -360,7 +396,8 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
360
396
|
!controller.signal.aborted &&
|
|
361
397
|
modelIdentity(ctx.model) === modelIdentity(model);
|
|
362
398
|
try {
|
|
363
|
-
|
|
399
|
+
// An observed provider answers immediately; a placeholder would only flicker.
|
|
400
|
+
if (!observed) safeSetStatus(ctx, "usage …");
|
|
364
401
|
const outcome = await queryCurrentState(
|
|
365
402
|
ctx,
|
|
366
403
|
model,
|
|
@@ -623,6 +660,13 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
623
660
|
pi.on("agent_settled", (_event, ctx) => {
|
|
624
661
|
if (ctx.hasUI) startStatusRefresh(ctx, ctx.model, false);
|
|
625
662
|
});
|
|
663
|
+
pi.on("after_provider_response", (event, ctx) => {
|
|
664
|
+
if (!isObservedProvider(ctx.model)) return;
|
|
665
|
+
const report = normalizeClaudeRateLimitHeaders(event.headers, Date.now());
|
|
666
|
+
if (!report) return;
|
|
667
|
+
claudeObservation = report;
|
|
668
|
+
if (ctx.hasUI && sessionActive) startStatusRefresh(ctx, ctx.model, false);
|
|
669
|
+
});
|
|
626
670
|
pi.on("session_shutdown", (_event, ctx) => {
|
|
627
671
|
sessionActive = false;
|
|
628
672
|
statusGeneration += 1;
|
|
@@ -636,3 +680,7 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
636
680
|
emitUnavailableUsage();
|
|
637
681
|
});
|
|
638
682
|
}
|
|
683
|
+
|
|
684
|
+
function isObservedProvider(model: UsageModel | undefined): boolean {
|
|
685
|
+
return model?.provider === CLAUDE_BRIDGE_PROVIDER_ID;
|
|
686
|
+
}
|