@fenlyin/pi-subscription-usage 0.0.0-stage → 2.0.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/LICENSE +24 -0
- package/README.md +156 -3
- package/README.zh-CN.md +156 -0
- package/THIRD_PARTY_NOTICES.md +28 -0
- package/index.ts +1 -0
- package/package.json +52 -6
- package/src/codex-reset-core.ts +249 -0
- package/src/codex-resets.ts +145 -0
- package/src/config-core.ts +43 -0
- package/src/config.ts +48 -0
- package/src/core.ts +182 -0
- package/src/display.ts +55 -0
- package/src/format.ts +225 -0
- package/src/providers/claude-bridge.ts +118 -0
- package/src/providers/codex-constants.ts +2 -0
- package/src/providers/codex.ts +227 -0
- package/src/providers/grok.ts +358 -0
- package/src/providers/kimi.ts +155 -0
- package/src/providers/openai.ts +145 -0
- package/src/providers/opencode-go.ts +88 -0
- package/src/query.ts +511 -0
- package/src/status.ts +255 -0
- package/src/types.ts +90 -0
- package/src/usage.ts +679 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fenlyin
|
|
4
|
+
Copyright (c) 2026 specode
|
|
5
|
+
Copyright (c) 2026 narumiruna
|
|
6
|
+
Copyright (c) 2026 puetsua
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# pi-subscription-usage
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
A [Pi](https://github.com/earendil-works/pi-mono) extension that shows your active provider's subscription quota in the footer status bar and a full breakdown via `/usage`.
|
|
6
|
+
|
|
7
|
+
> Maintained fork of [`specode/pi-subscription-usage`](https://github.com/specode/pi-subscription-usage), published as [`@fenlyin/pi-subscription-usage`](https://www.npmjs.com/package/@fenlyin/pi-subscription-usage). Upstream credit is kept in [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md).
|
|
8
|
+
|
|
9
|
+
Footer status bar:
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
5h 99% ↻ 2h13m · 1w 85% ↻ 3d4h
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`/usage` output:
|
|
16
|
+
|
|
17
|
+
```text
|
|
18
|
+
OpenAI Codex
|
|
19
|
+
Shared Across Models:
|
|
20
|
+
5h Window ████████████ 99% left · resets 08/27 12:13
|
|
21
|
+
1w Window ██████████░░ 85% left · resets 08/30 14:00
|
|
22
|
+
|
|
23
|
+
gpt-5.6-spark:
|
|
24
|
+
5h Window █░░░░░░░░░░░ 10% left · resets 08/27 11:40
|
|
25
|
+
|
|
26
|
+
Account:
|
|
27
|
+
Email user@example.com
|
|
28
|
+
Plan Pro
|
|
29
|
+
Resets Left 2
|
|
30
|
+
Extra Credit none
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Supported Providers
|
|
34
|
+
|
|
35
|
+
To sign in, run `/login`, pick `Sign in with an account` (Account below) or `Sign in with an API key` (API key below), then pick the name shown in the table.
|
|
36
|
+
|
|
37
|
+
| Provider | `/login` option | Quota shown | Reset credits | Extra extension |
|
|
38
|
+
| --- | --- | --- | --- | --- |
|
|
39
|
+
| OpenAI (ChatGPT) | Account → `OpenAI`, and Account → `OpenAI Codex (legacy)` | ChatGPT plan limits, current app limits | Yes (`Account Resets`) | |
|
|
40
|
+
| OpenAI Codex | Account → `OpenAI Codex (legacy)` | `5h`, `1w`, model-specific windows, account email | Yes (`Resets Left`) | |
|
|
41
|
+
| OpenCode Go | API key → `OpenCode Go` | `5h`, `1w`, `1m` | — | |
|
|
42
|
+
| xAI Grok | Account → `xAI` | `1w` and/or `1m` | — | |
|
|
43
|
+
| Kimi Coding | Account → `Kimi For Coding` | `5h`, `1w`, membership level | — | |
|
|
44
|
+
| 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 |
|
|
45
|
+
|
|
46
|
+
### Provider Notes
|
|
47
|
+
|
|
48
|
+
#### OpenAI (ChatGPT)
|
|
49
|
+
|
|
50
|
+
Sign in to both `OpenAI` and `OpenAI Codex (legacy)` with the same ChatGPT account and workspace, then keep chatting with an `OpenAI` model. OpenAI rejects the `OpenAI` sign-in on its usage endpoints, so the extension uses the `OpenAI Codex (legacy)` sign-in only to read usage. `/usage` lists website `Plan limits` separately from the current app's limits (`App Allowance` shows the configured cap share, not remaining quota), and the footer tracks `Plan limits`.
|
|
51
|
+
|
|
52
|
+
#### xAI Grok
|
|
53
|
+
|
|
54
|
+
Sign in with your SuperGrok account (Account → `xAI`). Signing in with an xAI API key does not show subscription quota. If you use the third-party `pi-xai-oauth` extension instead, its sign-in works too.
|
|
55
|
+
|
|
56
|
+
#### Claude (Claude Code)
|
|
57
|
+
|
|
58
|
+
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.
|
|
59
|
+
|
|
60
|
+
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.
|
|
61
|
+
|
|
62
|
+
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.
|
|
63
|
+
|
|
64
|
+
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:
|
|
65
|
+
|
|
66
|
+
| Package | Result |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| `pi-claude-bridge` 0.9.3 or later | Works |
|
|
69
|
+
| `@viniciosrab/pi-claude-bridge` 0.9.4-fork.3 | Same usage code as 0.9.3; expected to work, untested |
|
|
70
|
+
| `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` |
|
|
71
|
+
| `@vanillagreen/pi-claude-bridge`, `pi-claude-subscription`, `pi-claude-directsdk`, `@ramtinj95/pi-claude-delegation` | Register a different provider id; not supported |
|
|
72
|
+
|
|
73
|
+
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.
|
|
74
|
+
|
|
75
|
+
## Installation
|
|
76
|
+
|
|
77
|
+
From npm:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pi install npm:@fenlyin/pi-subscription-usage
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
From GitHub:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pi install git:github.com/fenlyin0420/pi-subscription-usage
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
From a local directory for development:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pi install /path/to/pi-subscription-usage
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Usage
|
|
96
|
+
|
|
97
|
+
Sign in through `/login` as described above, then select a model from that provider. From then on:
|
|
98
|
+
|
|
99
|
+
- The footer status bar shows the active quota windows and refreshes after each turn and every 5 minutes (using a 5-minute cache).
|
|
100
|
+
- Running `/usage` always fetches fresh data for the active provider and shows progress bars, reset times (`MM/DD HH:mm`), and account details.
|
|
101
|
+
- Claude is the exception: its quota is never fetched, only read from Claude Code's responses (see [Claude (Claude Code)](#claude-claude-code)).
|
|
102
|
+
|
|
103
|
+
### Reset Credits (OpenAI and Codex)
|
|
104
|
+
|
|
105
|
+
When your OpenAI or Codex account has redeemable rate-limit reset tickets, `/usage` offers to use one:
|
|
106
|
+
|
|
107
|
+
- `Cancel (Default)` is selected by default, and an explicit confirmation is required before a ticket is consumed.
|
|
108
|
+
- Before redeeming, the extension checks that the active model and account have not changed and that the runtime token matches Pi's stored OAuth login.
|
|
109
|
+
- In OpenAI mode, a ticket resets server-defined account windows rather than specifically clearing the current app's limits.
|
|
110
|
+
- Redeeming a reset ticket is the only write operation in this extension.
|
|
111
|
+
|
|
112
|
+
## Configuration
|
|
113
|
+
|
|
114
|
+
Percentages show remaining quota by default. To show used quota instead, create `~/.pi/agent/subscription-usage.json` (global) or `.pi/subscription-usage.json` (trusted project override):
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"displayMode": "used"
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
| Value | Description |
|
|
123
|
+
| --- | --- |
|
|
124
|
+
| `"remaining"` | Show remaining quota percentage (default) |
|
|
125
|
+
| `"used"` | Show used quota percentage |
|
|
126
|
+
|
|
127
|
+
Run `/reload` after changing the config file. `displayMode` applies to the footer status bar, `/usage` progress bars, and status events.
|
|
128
|
+
|
|
129
|
+
## Privacy and Safety
|
|
130
|
+
|
|
131
|
+
- 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).
|
|
132
|
+
- Requests go only to each provider's official endpoints. Custom proxies and custom base URLs are refused.
|
|
133
|
+
- Tokens are never written to caches, session files, the status bar, or error messages.
|
|
134
|
+
- The extension does not modify model requests.
|
|
135
|
+
- Several provider usage endpoints are undocumented. If an endpoint changes, the extension shows an error rather than guessing.
|
|
136
|
+
|
|
137
|
+
## Development
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
# Run tests (requires Node.js with native TypeScript support)
|
|
141
|
+
npm test
|
|
142
|
+
|
|
143
|
+
# Check package contents
|
|
144
|
+
npm run pack:check
|
|
145
|
+
|
|
146
|
+
# Load the extension directly in Pi without installing
|
|
147
|
+
pi --no-extensions --offline -e ./index.ts --list-models
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Status Events for Theme and Extension Authors
|
|
151
|
+
|
|
152
|
+
The extension emits structured events on `subscription-usage/status/v1` so custom themes or status extensions can render quota data without parsing the footer string. Windows are ordered `5h` -> `1w` -> `1m` -> other. Ready payloads include `displayMode` and, for each window, `displayPercent`, `remainingPercent`, `usedPercent`, and `resetCountdown`.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
[MIT](./LICENSE). See [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md) for adapted sources and licenses.
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# pi-subscription-usage
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
一个 [Pi](https://github.com/earendil-works/pi-mono) 扩展:在底部状态栏显示当前订阅还剩多少额度,输入 `/usage` 查看完整明细。
|
|
6
|
+
|
|
7
|
+
> 本仓库是 [`specode/pi-subscription-usage`](https://github.com/specode/pi-subscription-usage) 的维护分支,发布为 [`@fenlyin/pi-subscription-usage`](https://www.npmjs.com/package/@fenlyin/pi-subscription-usage)。上游署名保留在 [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md)。
|
|
8
|
+
|
|
9
|
+
底部状态栏效果:
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
5h 99% ↻ 2h13m · 1w 85% ↻ 3d4h
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`/usage` 输出示例:
|
|
16
|
+
|
|
17
|
+
```text
|
|
18
|
+
OpenAI Codex
|
|
19
|
+
Shared Across Models:
|
|
20
|
+
5h Window ████████████ 99% left · resets 08/27 12:13
|
|
21
|
+
1w Window ██████████░░ 85% left · resets 08/30 14:00
|
|
22
|
+
|
|
23
|
+
gpt-5.6-spark:
|
|
24
|
+
5h Window █░░░░░░░░░░░ 10% left · resets 08/27 11:40
|
|
25
|
+
|
|
26
|
+
Account:
|
|
27
|
+
Email user@example.com
|
|
28
|
+
Plan Pro
|
|
29
|
+
Resets Left 2
|
|
30
|
+
Extra Credit none
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 支持的提供商
|
|
34
|
+
|
|
35
|
+
登录方法:输入 `/login`,先选 `Sign in with an account`(下表记为 Account)或 `Sign in with an API key`(下表记为 API key),再在列表里选下表中的名称。Pi 界面只有英文,这里的选项名称都按界面原文写。
|
|
36
|
+
|
|
37
|
+
| 提供商 | `/login` 中选择 | 显示内容 | 重置券 | 额外扩展 |
|
|
38
|
+
| --- | --- | --- | --- | --- |
|
|
39
|
+
| OpenAI (ChatGPT) | Account → `OpenAI`,以及 Account → `OpenAI Codex (legacy)` | ChatGPT 套餐限额、当前应用限额 | 支持(`Account Resets`) | |
|
|
40
|
+
| OpenAI Codex | Account → `OpenAI Codex (legacy)` | `5h`、`1w`、模型专属窗口、账号邮箱 | 支持(`Resets Left`) | |
|
|
41
|
+
| OpenCode Go | API key → `OpenCode Go` | `5h`、`1w`、`1m` | — | |
|
|
42
|
+
| xAI Grok | Account → `xAI` | `1w` 和/或 `1m` | — | |
|
|
43
|
+
| Kimi Coding | Account → `Kimi For Coding` | `5h`、`1w`、会员等级 | — | |
|
|
44
|
+
| Claude (Claude Code) | 无需在 Pi 登录,使用 Claude Code 的登录 | `5h`、`1w` | — | [`pi-claude-bridge`](https://www.npmjs.com/package/pi-claude-bridge) 0.9.3 及以上 |
|
|
45
|
+
|
|
46
|
+
### 提供商说明
|
|
47
|
+
|
|
48
|
+
#### OpenAI (ChatGPT)
|
|
49
|
+
|
|
50
|
+
用同一个 ChatGPT 账号和工作区,分别登录 `OpenAI` 和 `OpenAI Codex (legacy)`,平时对话继续用 `OpenAI` 下的模型就行。OpenAI 的用量接口不认 `OpenAI` 这个登录,所以扩展只借 `OpenAI Codex (legacy)` 的登录来读额度。`/usage` 会将网页端的套餐总限额(`Plan limits`)与当前应用限额分开显示(`App Allowance` 表示该应用的配额上限比例,而非剩余额度),底部状态栏显示 `Plan limits`。
|
|
51
|
+
|
|
52
|
+
#### xAI Grok
|
|
53
|
+
|
|
54
|
+
用 SuperGrok 账号登录(Account → `xAI`)。用 xAI 的 API Key 登录看不到订阅额度。如果你装的是第三方扩展 `pi-xai-oauth`,用它登录也可以。
|
|
55
|
+
|
|
56
|
+
#### Claude (Claude Code)
|
|
57
|
+
|
|
58
|
+
需要 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` 提供商下的模型。
|
|
59
|
+
|
|
60
|
+
bridge 没有可查询的用量接口:Claude Code 会在回复中附带当前套餐用量,扩展读取的就是这份数据。因此每个新会话要等收到第一份用量数据,底部状态栏才有内容;`/usage` 不会重新查询,只显示最近一次收到的数值和对应时间;如果某个窗口的重置时间已过,在收到下一份数据之前按未使用显示。扩展不会读取 Claude Code 的凭据。
|
|
61
|
+
|
|
62
|
+
已用 `pi-claude-bridge` 0.9.3 和 Claude Code 2.1.311(bridge 所带 Agent SDK 内置的版本)测试。如果你让 bridge 改用自己安装的 `claude` 可执行文件(`pathToClaudeCodeExecutable`),较旧的 Claude Code 版本未经测试。
|
|
63
|
+
|
|
64
|
+
npm 上有好几个名字相近的包。扩展按 `claude-bridge` 这个 provider id 识别,所以所用的包既要注册这个 id,也要转交用量数据。截至 2026 年 10 月 31 日:
|
|
65
|
+
|
|
66
|
+
| 包 | 结果 |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| `pi-claude-bridge` 0.9.3 及以上 | 可用 |
|
|
69
|
+
| `@viniciosrab/pi-claude-bridge` 0.9.4-fork.3 | 用量相关代码与 0.9.3 相同,预计可用,未测试 |
|
|
70
|
+
| `pi-claude-bridge` 0.9.3 以前的版本、`@fractaal/pi-claude-bridge`、`@yassimba/pi-claude-bridge`、`@agimon-ai/po-claude-bridge` | 注册了 `claude-bridge`,但不转交用量:底部状态栏一直为空,`/usage` 一直显示 `Waiting` |
|
|
71
|
+
| `@vanillagreen/pi-claude-bridge`、`pi-claude-subscription`、`pi-claude-directsdk`、`@ramtinj95/pi-claude-delegation` | 注册的是别的 provider id,不支持 |
|
|
72
|
+
|
|
73
|
+
不带 `pi-` 前缀的 npm 包 `claude-bridge` 是一个与此无关的 Claude Code CLI 代理服务器,`pi install npm:claude-bridge` 会装错包。可以用 `pi list` 确认装的是哪个。
|
|
74
|
+
|
|
75
|
+
## 安装
|
|
76
|
+
|
|
77
|
+
从 npm 安装:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pi install npm:@fenlyin/pi-subscription-usage
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
从 GitHub 安装:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pi install git:github.com/fenlyin0420/pi-subscription-usage
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
从本地目录安装(开发调试):
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pi install /path/to/pi-subscription-usage
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 使用说明
|
|
96
|
+
|
|
97
|
+
按上面的方法登录,再选一个该提供商的模型,之后:
|
|
98
|
+
|
|
99
|
+
- 底部状态栏会自动显示当前额度窗口,在每轮对话结束后及每 5 分钟刷新一次(带 5 分钟缓存)。
|
|
100
|
+
- 执行 `/usage` 会跳过缓存直接拉取最新数据,显示进度条、重置时间(`MM/DD HH:mm`)及账号信息。
|
|
101
|
+
- Claude 例外:不会主动查询额度,只读取 Claude Code 回复中附带的数据(见 [Claude (Claude Code)](#claude-claude-code))。
|
|
102
|
+
|
|
103
|
+
### 使用重置券(OpenAI 与 Codex)
|
|
104
|
+
|
|
105
|
+
OpenAI 或 Codex 账号里有可用的限额重置券时,`/usage` 会提供使用入口:
|
|
106
|
+
|
|
107
|
+
- 确认弹窗默认选中 `Cancel (Default)`,必须手动确认才会消耗重置券。
|
|
108
|
+
- 提交兑换前,扩展会检查当前模型和账号是否发生变动,并核对运行时令牌是否与 Pi 保存的 OAuth 登录信息一致。
|
|
109
|
+
- 在 OpenAI 模式下,重置券作用于服务端定义的账户级窗口,不一定单独清空当前应用的限额。
|
|
110
|
+
- 兑换重置券是本扩展唯一的写操作。
|
|
111
|
+
|
|
112
|
+
## 配置
|
|
113
|
+
|
|
114
|
+
百分比默认显示剩余额度。如需改为显示已用额度,可在 `~/.pi/agent/subscription-usage.json`(全局)或 `.pi/subscription-usage.json`(受信任项目的局部覆盖)中配置:
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"displayMode": "used"
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
| 配置值 | 说明 |
|
|
123
|
+
| --- | --- |
|
|
124
|
+
| `"remaining"` | 显示剩余额度百分比(默认) |
|
|
125
|
+
| `"used"` | 显示已用额度百分比 |
|
|
126
|
+
|
|
127
|
+
修改配置文件后执行 `/reload` 生效。`displayMode` 同时作用于底部状态栏、`/usage` 进度条和状态事件。
|
|
128
|
+
|
|
129
|
+
## 隐私与安全
|
|
130
|
+
|
|
131
|
+
- 仅从 Pi 自身的登录注册表读取凭据,不读取浏览器 Cookie 或其他工具的凭据文件(例如 Grok 不会读取 `~/.grok/auth.json`;Claude 用量来自 bridge 返回的数据,不读取 Claude Code 存在钥匙串里的凭据)。
|
|
132
|
+
- 仅向各提供商的官方接口发起请求,拒绝自定义代理和自定义 Base URL。
|
|
133
|
+
- 令牌不会写入缓存、会话文件、状态栏或报错信息。
|
|
134
|
+
- 不修改任何模型请求。
|
|
135
|
+
- 部分提供商的用量接口属于未公开接口;若接口变更,扩展会直接报错提示,不会猜测或伪造数据。
|
|
136
|
+
|
|
137
|
+
## 本地开发
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
# 运行测试(需要原生支持 TypeScript 的 Node.js 版本)
|
|
141
|
+
npm test
|
|
142
|
+
|
|
143
|
+
# 检查打包文件列表
|
|
144
|
+
npm run pack:check
|
|
145
|
+
|
|
146
|
+
# 不安装直接加载扩展验证
|
|
147
|
+
pi --no-extensions --offline -e ./index.ts --list-models
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### 主题与状态栏扩展接入
|
|
151
|
+
|
|
152
|
+
扩展会在 `subscription-usage/status/v1` 事件通道发布结构化状态事件,供自定义主题或状态栏扩展直接渲染额度数据,无需解析状态栏文本。窗口固定按 `5h` -> `1w` -> `1m` -> 其他顺序排列;就绪事件包含 `displayMode`,每个窗口提供 `displayPercent`、`remainingPercent`、`usedPercent` 和 `resetCountdown`。
|
|
153
|
+
|
|
154
|
+
## 许可证
|
|
155
|
+
|
|
156
|
+
[MIT](./LICENSE)。改编的第三方源码及许可证信息见 [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md)。
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
This package selectively adapts MIT-licensed implementation ideas and source
|
|
4
|
+
from the following projects. The original copyright notices and MIT permission
|
|
5
|
+
notice are retained in `LICENSE`.
|
|
6
|
+
|
|
7
|
+
- `@specode/pi-subscription-usage` 2.0.0 (upstream of this fork)
|
|
8
|
+
- Copyright (c) 2026 specode
|
|
9
|
+
- <https://github.com/specode/pi-subscription-usage>
|
|
10
|
+
- This package is a fork of that project; the upstream commit history is
|
|
11
|
+
preserved in this repository.
|
|
12
|
+
|
|
13
|
+
- `@narumitw/pi-usage` 0.53.0
|
|
14
|
+
- Copyright (c) 2026 narumiruna
|
|
15
|
+
- <https://github.com/narumiruna/pi-extensions/tree/main/packages/pi-usage>
|
|
16
|
+
- Adapted areas: usage model, cache/redaction helpers, Codex and OpenCode Go
|
|
17
|
+
normalization, runtime-auth origin checks, and Codex reset flow.
|
|
18
|
+
- `pi-grok-usage` 1.0.4
|
|
19
|
+
- Copyright (c) 2026 puetsua
|
|
20
|
+
- <https://github.com/puetsua/pi-grok-usage>
|
|
21
|
+
- Adapted areas: identity-first Grok billing lookup and bounded response
|
|
22
|
+
normalization.
|
|
23
|
+
- `@sumhou/pi-kimi-usage` 1.0.1
|
|
24
|
+
- MIT license
|
|
25
|
+
- <https://github.com/ShumaoHou/pi-kimi-usage>
|
|
26
|
+
- Adapted areas: Kimi Coding quota response normalization.
|
|
27
|
+
|
|
28
|
+
No third-party package is bundled as a runtime dependency.
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./src/usage.ts";
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@fenlyin/pi-subscription-usage",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@fenlyin/pi-subscription-usage",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Show subscription quota for OpenAI, Codex, OpenCode Go, Grok, Kimi, and Claude (via pi-claude-bridge), plus OpenAI and Codex account reset credits in Pi",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Fenlyin <fenlyin0420@gmail.com>",
|
|
8
|
+
"homepage": "https://github.com/fenlyin0420/pi-subscription-usage#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/fenlyin0420/pi-subscription-usage.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/fenlyin0420/pi-subscription-usage/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"pi-package",
|
|
18
|
+
"pi-extension",
|
|
19
|
+
"subscription",
|
|
20
|
+
"usage",
|
|
21
|
+
"quota",
|
|
22
|
+
"codex",
|
|
23
|
+
"opencode",
|
|
24
|
+
"grok",
|
|
25
|
+
"kimi",
|
|
26
|
+
"claude"
|
|
27
|
+
],
|
|
28
|
+
"files": [
|
|
29
|
+
"index.ts",
|
|
30
|
+
"src",
|
|
31
|
+
"README.md",
|
|
32
|
+
"README.zh-CN.md",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"THIRD_PARTY_NOTICES.md"
|
|
35
|
+
],
|
|
36
|
+
"pi": {
|
|
37
|
+
"extensions": [
|
|
38
|
+
"./index.ts"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"test": "node --test test/*.test.ts",
|
|
43
|
+
"pack:check": "npm pack --dry-run",
|
|
44
|
+
"prepublishOnly": "npm test"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// Adapted from @narumitw/pi-usage@0.53.0 (MIT).
|
|
2
|
+
import { sanitizeDisplayText } from "./core.ts";
|
|
3
|
+
import type { UsageReport } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
export type CodexResetOutcomeCode =
|
|
6
|
+
| "reset"
|
|
7
|
+
| "nothing_to_reset"
|
|
8
|
+
| "no_credit"
|
|
9
|
+
| "already_redeemed";
|
|
10
|
+
|
|
11
|
+
export interface CodexResetOption {
|
|
12
|
+
creditId?: string;
|
|
13
|
+
title: string;
|
|
14
|
+
description: string;
|
|
15
|
+
expiresAt?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CodexResetAvailability {
|
|
19
|
+
availableCount: number;
|
|
20
|
+
options: CodexResetOption[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CodexResetOutcome {
|
|
24
|
+
code: CodexResetOutcomeCode;
|
|
25
|
+
windowsReset: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const CODEX_RESET_CONFIRMATION_OPTIONS = [
|
|
29
|
+
"Cancel (Default)",
|
|
30
|
+
"Redeem 1 Reset (Irreversible)",
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
export function isCodexResetConfirmed(value: string | undefined): boolean {
|
|
34
|
+
return value === CODEX_RESET_CONFIRMATION_OPTIONS[1];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function codexResetCount(report: UsageReport): number | undefined {
|
|
38
|
+
const value = report.metrics.find(
|
|
39
|
+
(metric) => metric.id === "reset-credits",
|
|
40
|
+
)?.value;
|
|
41
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0
|
|
42
|
+
? value
|
|
43
|
+
: undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function resetOptionExpiration(option: CodexResetOption): string {
|
|
47
|
+
if (option.expiresAt === undefined) return "No Expiry";
|
|
48
|
+
const expiration = new Date(option.expiresAt * 1_000);
|
|
49
|
+
return Number.isNaN(expiration.getTime())
|
|
50
|
+
? "Expiry Unavailable"
|
|
51
|
+
: `Expires ${expiration.toLocaleString("en-US")}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function formatCodexResetOutcome(
|
|
55
|
+
outcome: CodexResetOutcome | undefined,
|
|
56
|
+
remainingCount: number | undefined,
|
|
57
|
+
): string {
|
|
58
|
+
const remaining =
|
|
59
|
+
remainingCount === undefined ? "" : `, ${remainingCount} left`;
|
|
60
|
+
if (!outcome) return "No reset credits available.";
|
|
61
|
+
if (outcome.code === "reset") return `Reset redeemed${remaining}.`;
|
|
62
|
+
if (outcome.code === "already_redeemed") {
|
|
63
|
+
return `Reset already redeemed${remaining}.`;
|
|
64
|
+
}
|
|
65
|
+
if (outcome.code === "nothing_to_reset") return "Nothing to reset right now.";
|
|
66
|
+
return "No reset credits available.";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function verifyCodexStoredOAuthCredential(
|
|
70
|
+
resolvedAccess: string,
|
|
71
|
+
storedCredential: unknown,
|
|
72
|
+
): string {
|
|
73
|
+
const resolvedAccountId = codexAccountIdFromAccessToken(resolvedAccess);
|
|
74
|
+
if (!resolvedAccountId) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
"The active OpenAI Codex access token did not contain a valid account ID.",
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const credential = asObject(storedCredential);
|
|
80
|
+
if (credential?.type !== "oauth") {
|
|
81
|
+
throw new Error(
|
|
82
|
+
"Codex resets require the OAuth account configured through Pi /login.",
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const storedAccess = nonemptyString(credential.access);
|
|
86
|
+
const accountId = validHeaderValue(credential.accountId);
|
|
87
|
+
const refresh = nonemptyString(credential.refresh);
|
|
88
|
+
if (
|
|
89
|
+
storedAccess !== resolvedAccess ||
|
|
90
|
+
!accountId ||
|
|
91
|
+
accountId !== resolvedAccountId ||
|
|
92
|
+
!refresh
|
|
93
|
+
) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
"The active Codex runtime account does not match Pi's stored OAuth account.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return accountId;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Offer only explicit, plan-supported, unexpired tickets. Redemption is
|
|
103
|
+
* irreversible, so a summary count alone never becomes a server-chosen reset.
|
|
104
|
+
*/
|
|
105
|
+
export function normalizeCodexResetCreditsPayload(
|
|
106
|
+
payload: Record<string, unknown>,
|
|
107
|
+
options: { now?: number } = {},
|
|
108
|
+
): CodexResetAvailability {
|
|
109
|
+
const availableCount = nonnegativeInteger(payload.available_count);
|
|
110
|
+
if (availableCount === undefined) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
"Codex reset credits response returned an invalid available_count.",
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (payload.credits !== undefined && !Array.isArray(payload.credits)) {
|
|
116
|
+
throw new Error("Codex reset credits response returned invalid credits.");
|
|
117
|
+
}
|
|
118
|
+
const resetOptions: CodexResetOption[] = [];
|
|
119
|
+
for (const rawCredit of payload.credits ?? []) {
|
|
120
|
+
const credit = asObject(rawCredit);
|
|
121
|
+
if (
|
|
122
|
+
!credit ||
|
|
123
|
+
credit.status !== "available" ||
|
|
124
|
+
credit.reset_type !== "codex_rate_limits"
|
|
125
|
+
) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (credit.is_supported_by_plan !== true) continue;
|
|
129
|
+
const option = normalizeResetOption(credit);
|
|
130
|
+
if (option.expiresAt !== undefined &&
|
|
131
|
+
option.expiresAt * 1_000 <= (options.now ?? Date.now())) continue;
|
|
132
|
+
resetOptions.push(option);
|
|
133
|
+
}
|
|
134
|
+
resetOptions.sort(
|
|
135
|
+
(left, right) =>
|
|
136
|
+
(left.expiresAt ?? Number.MAX_SAFE_INTEGER) -
|
|
137
|
+
(right.expiresAt ?? Number.MAX_SAFE_INTEGER),
|
|
138
|
+
);
|
|
139
|
+
resetOptions.splice(Math.min(availableCount, 32));
|
|
140
|
+
return { availableCount: resetOptions.length, options: resetOptions };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function parseCodexResetOutcome(
|
|
144
|
+
payload: Record<string, unknown>,
|
|
145
|
+
): CodexResetOutcome {
|
|
146
|
+
const code = payload.code;
|
|
147
|
+
if (!isOutcomeCode(code)) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
"Codex reset consume endpoint returned an unknown outcome code.",
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
const windowsReset =
|
|
153
|
+
payload.windows_reset === undefined
|
|
154
|
+
? 0
|
|
155
|
+
: nonnegativeInteger(payload.windows_reset);
|
|
156
|
+
if (windowsReset === undefined) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
"Codex reset consume endpoint returned an invalid windows_reset value.",
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return { code, windowsReset };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function normalizeResetOption(
|
|
165
|
+
credit: Record<string, unknown>,
|
|
166
|
+
): CodexResetOption {
|
|
167
|
+
const creditId = opaqueId(credit.id);
|
|
168
|
+
if (!creditId)
|
|
169
|
+
throw new Error(
|
|
170
|
+
"Codex reset credits response returned an invalid credit ID.",
|
|
171
|
+
);
|
|
172
|
+
let expiresAt: number | undefined;
|
|
173
|
+
if (credit.expires_at !== undefined && credit.expires_at !== null) {
|
|
174
|
+
if (typeof credit.expires_at !== "string") {
|
|
175
|
+
throw new Error(
|
|
176
|
+
"Codex reset credits response returned an invalid expiration time.",
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
const parsed = Date.parse(credit.expires_at);
|
|
180
|
+
if (!Number.isFinite(parsed)) {
|
|
181
|
+
throw new Error(
|
|
182
|
+
"Codex reset credits response returned an invalid expiration time.",
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
expiresAt = Math.floor(parsed / 1_000);
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
creditId,
|
|
189
|
+
title: displayString(credit.title) ?? "Full Reset",
|
|
190
|
+
description:
|
|
191
|
+
displayString(credit.description) ?? "Resets the current usage windows.",
|
|
192
|
+
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function isOutcomeCode(value: unknown): value is CodexResetOutcomeCode {
|
|
197
|
+
return ["reset", "nothing_to_reset", "no_credit", "already_redeemed"].includes(
|
|
198
|
+
value as string,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function codexAccountIdFromAccessToken(access: string): string | undefined {
|
|
203
|
+
try {
|
|
204
|
+
const parts = access.split(".");
|
|
205
|
+
if (parts.length !== 3 || !parts[1]) return undefined;
|
|
206
|
+
const payload = JSON.parse(
|
|
207
|
+
Buffer.from(parts[1], "base64url").toString("utf8"),
|
|
208
|
+
) as unknown;
|
|
209
|
+
const claims = asObject(asObject(payload)?.["https://api.openai.com/auth"]);
|
|
210
|
+
return validHeaderValue(claims?.chatgpt_account_id);
|
|
211
|
+
} catch {
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
217
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
218
|
+
? (value as Record<string, unknown>)
|
|
219
|
+
: undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function nonemptyString(value: unknown): string | undefined {
|
|
223
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function validHeaderValue(value: unknown): string | undefined {
|
|
227
|
+
if (typeof value !== "string" || !value || value.length > 512)
|
|
228
|
+
return undefined;
|
|
229
|
+
return /[^\x20-\x7e]/u.test(value) ? undefined : value;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function opaqueId(value: unknown): string | undefined {
|
|
233
|
+
return typeof value === "string" && value.length > 0 && value.length <= 1_024
|
|
234
|
+
? value
|
|
235
|
+
: undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function displayString(value: unknown): string | undefined {
|
|
239
|
+
return typeof value === "string"
|
|
240
|
+
? sanitizeDisplayText(value, 160) || undefined
|
|
241
|
+
: undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function nonnegativeInteger(value: unknown): number | undefined {
|
|
245
|
+
let parsed = Number.NaN;
|
|
246
|
+
if (typeof value === "number") parsed = value;
|
|
247
|
+
else if (typeof value === "string" && value.trim()) parsed = Number(value);
|
|
248
|
+
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;
|
|
249
|
+
}
|