@diousk/pi-usage-fast 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 narumiruna
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,259 @@
1
+ # 📊 pi-usage-fast — Check Provider Usage, API Balance, and Codex Fast Mode
2
+
3
+ [![npm](https://img.shields.io/npm/v/@diousk/pi-usage-fast)](https://www.npmjs.com/package/@diousk/pi-usage-fast) [![Pi extension](https://img.shields.io/badge/Pi-extension-blue)](https://pi.dev) [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)
4
+
5
+ Inspect usage and DeepSeek API balance for Pi's active provider account, query other configured providers, and toggle Fast mode for supported OpenAI Codex models.
6
+ The extension keeps each provider's native quota, allowance, and spending semantics instead of treating unlike values as equivalent.
7
+ xAI OAuth subscription reporting follows the reviewed Grok Build contract and runs only after an explicit `/usage` action.
8
+
9
+ This fork of [pi-usage](https://github.com/narumiruna/pi-extensions/tree/main/packages/pi-usage) adds Fast mode support for `gpt-6-*` models.
10
+ Install it instead of the upstream pi-usage package to avoid duplicate commands and status updates.
11
+ Existing `pi-usage.json` settings remain compatible.
12
+
13
+ ## ✨ Features
14
+
15
+ - Shows active-account usage and next actions through `/usage`.
16
+ - Reports subscription allowances, API balances, and spending for the supported providers listed below without mixing their billing semantics.
17
+ - Toggles persistent Codex Fast routing through `/fast` or the usage menu.
18
+ - Redeems eligible Codex resets only after fresh account matching and explicit confirmation.
19
+ - Refreshes one or all configured providers with bounded concurrency while preserving partial results.
20
+ - Scopes statusline and cache data to the active provider and runtime account.
21
+ - Resolves credentials through Pi or the process-local OAuth credential-source protocol, waits for compatible pending account activation, and validates the effective provider endpoint before sending them.
22
+
23
+ ## 📦 Install
24
+
25
+ Requires Pi 0.81.0 or newer to validate the effective base URL for resolved provider auth before sending credentials to an official usage endpoint.
26
+ The v1 credential-source path is characterized against Pi 0.84.3; other runtimes keep the standalone fallback without its protocol timing guarantee.
27
+
28
+ Like every Pi extension, this package runs with Pi's process permissions.
29
+ Review [Security and privacy](#-security-and-privacy) before installation.
30
+
31
+ ```bash
32
+ pi install npm:@diousk/pi-usage-fast
33
+ ```
34
+
35
+ Try without installing permanently:
36
+
37
+ ```bash
38
+ pi -e npm:@diousk/pi-usage-fast
39
+ ```
40
+
41
+ Build and try this package locally from the repository root:
42
+
43
+ ```bash
44
+ npm --workspace @diousk/pi-usage-fast run build
45
+ pi -e ./packages/pi-usage
46
+ ```
47
+
48
+ The package declares `dist/index.ts`, so an unbuilt local checkout must run the build before Pi loads the package directory.
49
+
50
+ ## 🚀 Quick start
51
+
52
+ Run `/usage` in TUI or RPC mode to inspect the active provider, refresh its usage, or choose another configured provider.
53
+ When a provider exposes several billing targets, `/usage` asks for one target before querying usage.
54
+ Run `/fast` to toggle Fast mode for a supported active Codex model.
55
+
56
+ ## 💬 Commands
57
+
58
+ | Command | Purpose |
59
+ | --- | --- |
60
+ | `/usage` | Query the active provider's usage, then manage provider queries, preferences, or eligible Codex resets. |
61
+ | `/fast` | Toggle Fast mode for the active supported Codex model. |
62
+
63
+ Both commands support TUI and RPC, accept no arguments, and reject print and JSON modes.
64
+ Cross-provider queries require an explicit interactive choice; there are no provider-ID, `--refresh`, or `--all` arguments.
65
+ Requests use the matched provider credentials; see [Security and privacy](#-security-and-privacy).
66
+ Fast mode uses more plan allowance; see [Codex Fast mode](#codex-fast-mode) for eligibility and when changes apply.
67
+
68
+ Codex reset redemption requires a freshly matched current OAuth account and explicit confirmation; **after confirmation, its progress view cannot cancel the reset**.
69
+ Read the [query and reset guide](./docs/operations.md) for target selection, cancellation, and safe retry behavior.
70
+
71
+ ## ⚙️ Settings
72
+
73
+ Choose **Settings** in `/usage` to edit Codex Fast mode, percentage display, and the reset countdown through Pi's settings-list interaction in TUI mode.
74
+ RPC mode reports the active manual settings path instead of opening terminal UI.
75
+
76
+ These preferences live in `pi-usage.json` under Pi's user agent directory, normally `~/.pi/agent/pi-usage.json`.
77
+ The extension reloads this file at every session start and does not create it until the first successful save.
78
+ Within one Pi process, changes save immediately in invocation order.
79
+ Saves preserve unknown JSON fields and publish through a private temporary file plus rename.
80
+ Malformed or invalid files remain untouched.
81
+ A failed save restores the prior displayed and effective value, while shutdown waits for queued writes.
82
+ Separate Pi processes are not mutually locked.
83
+
84
+ Target selections are stored only as IDs in the provider-neutral `selectedTargets` object in this file and are managed through `/usage`, not the Settings screen.
85
+ The former `fireworksAccountId` field remains read-compatible: it supplies `selectedTargets.fireworks` in memory only when the generic value is absent.
86
+ A successful explicit Fireworks account selection writes the generic field and removes the legacy field atomically; ordinary reads do not rewrite the file.
87
+
88
+ ### Codex Fast mode
89
+
90
+ Run `/fast` without arguments to toggle Fast for the active supported Codex model, or use **Turn Fast mode on/off** in `/usage`.
91
+ Fast is about 1.5× faster and uses more of your plan allowance.
92
+ The `codexFastMode` preference defaults to Off.
93
+
94
+ Fast currently applies only to official `openai-codex-responses` requests for `gpt-5.5`, `gpt-5.6-sol`, `gpt-5.6-terra`, `gpt-5.6-luna`, and `gpt-6-*` models at `https://chatgpt.com`.
95
+ It sends `service_tier: "priority"` while enabled and explicit `service_tier: "default"` otherwise.
96
+ The statusline adds `fast` only while the preference is effective, for example `codex fast 59% ↻ 2h30m` with the default reset countdown.
97
+ Unsupported models and custom or proxy origins are left unchanged.
98
+
99
+ A toggle affects provider requests whose payload hook starts after the save; a request already sent is unchanged.
100
+ Repair or remove an invalid file, then run `/reload` before trying the toggle again.
101
+
102
+ ### Codex statusline percentage
103
+
104
+ The `codexStatusPercentage` preference accepts `"remaining"` or `"used"` and defaults to `"remaining"` to preserve existing output.
105
+ Choose **Codex percentage** in the TUI Settings screen, or set the field manually and run `/reload`:
106
+
107
+ ```json
108
+ {
109
+ "codexStatusPercentage": "used"
110
+ }
111
+ ```
112
+
113
+ With `"used"`, a limit with 70% remaining appears as `30%`; its reset countdown or window label stays unchanged.
114
+ This preference changes only Codex statusline formatting, not provider data or the detailed `/usage` report.
115
+
116
+ ### Codex statusline reset countdown
117
+
118
+ The `codexStatusResetCountdown` preference defaults to `true`. It replaces the window labels with the time remaining until each returned limit resets.
119
+ Turn **Codex reset countdown** Off in the TUI Settings screen, or set it to `false` in `pi-usage.json` and run `/reload`, to restore the legacy `5h` and `wk` labels:
120
+
121
+ ```json
122
+ {
123
+ "codexStatusResetCountdown": false
124
+ }
125
+ ```
126
+
127
+ ## 📋 Provider semantics
128
+
129
+ Usage is provider-specific: a subscription allowance, current balance, and rated spend are not interchangeable.
130
+ Currencies and billing targets remain separate.
131
+
132
+ | Provider | Reported data |
133
+ | --- | --- |
134
+ | OpenAI Codex | Subscription windows, credits, resets, and model buckets |
135
+ | Kimi For Coding | Plan request windows and a separate booster wallet |
136
+ | Moonshot AI Global/China | Current API balance in USD/CNY |
137
+ | MiniMax Global/China | Token Plan windows or pay-as-you-go API balance |
138
+ | GitHub Copilot | AI credits, premium requests, or Free-plan chat allowance |
139
+ | OpenRouter | Per-key credit limits and spending windows |
140
+ | DeepSeek | Exact current CNY and USD API balances |
141
+ | Fireworks | Rated trailing 30-day spend for one selected account |
142
+ | Vercel AI Gateway | Team credit balance and lifetime spend |
143
+ | Baseten | Organization-wide trailing 30-day Model APIs spend after credits |
144
+ | OpenCode Go | Rolling, weekly, and monthly plan windows |
145
+ | xAI | OAuth subscription allowance and credits; explicit menu queries only |
146
+ | Z.AI | Coding Plan quota windows, MCP allowance, plan name, and renewal date |
147
+
148
+ Read the [provider reference](./docs/providers.md) for provider IDs, exact endpoints, authentication requirements, normalization rules, statusline examples, limitations, and pinned contract evidence.
149
+ Codex reset redemption requires a freshly matched current OAuth account and explicit confirmation; custom or proxy origins fail before mutation.
150
+
151
+ ## 🧭 Current and configured accounts
152
+
153
+ `Current` identifies the provider and credential used by Pi's selected model.
154
+ `Configured` identifies runtime auth for another supported provider, not an active provider.
155
+
156
+ The extension selects one provider target for one query and never flattens targets into provider rows or aggregates every visible target.
157
+ Provider adapters own target discovery and validation; core owns one-target selection, persistence, cache identity, cancellation, and UI.
158
+ A compatible credential owner may offer the verified active named account through the versioned process-local protocol without exposing its account label or storage.
159
+ When that owner is still activating the requested provider in the current session, `/usage` waits through the extension-neutral `oauth:credential-readiness:v1` protocol before collecting the synchronous credential offer.
160
+ Without such an owner, `pi-usage` retains its standalone Pi `auth.json` behavior.
161
+ An older or incompatible owner degrades to the existing authentication-unavailable result when the stored login does not match runtime auth.
162
+ After the active runtime credential changes, the next command, turn, or scheduled refresh resolves auth again and cannot reuse another account's cached report.
163
+
164
+ ## 📊 Statusline behavior
165
+
166
+ The `usage` status item is active only for selected providers that publish statusline usage.
167
+ It refreshes every five minutes while the session remains on such a provider and is cleared when the model changes to an unsupported or menu-only provider.
168
+ DeepSeek publishes each returned currency as a separate exact balance segment and reports when the API is unavailable.
169
+ Fireworks publishes exact per-currency rated spend totals and reports when no rated usage exists.
170
+ Moonshot AI publishes the available balance with its region-native currency.
171
+ Vercel AI Gateway publishes the exact current USD credit balance.
172
+ MiniMax publishes Token Plan window percentages or the regional pay-as-you-go available balance.
173
+ Baseten publishes the exact trailing 30-day Model APIs net subtotal after credits.
174
+ xAI is always menu-only and never starts a scheduled status refresh.
175
+ Z.AI statusline usage refreshes every five minutes while the selected model remains on Z.AI.
176
+
177
+ Queries for another provider or all providers never publish their results to the statusline.
178
+ `@narumitw/pi-statusline` supplies the default `📊` icon; `pi-usage` publishes text-only values.
179
+
180
+ ## 🔄 Migrating from pi-codex-usage
181
+
182
+ `pi-codex-usage` is deprecated and its source is archived under `deprecated/`.
183
+ To migrate one installation:
184
+
185
+ ```bash
186
+ pi remove npm:@narumitw/pi-codex-usage
187
+ pi install npm:@diousk/pi-usage-fast
188
+ ```
189
+
190
+ Remove the deprecated package rather than loading both usage extensions together.
191
+
192
+ Behavior changes:
193
+
194
+ - Use `/usage` for usage management; `/codex-status` is no longer registered.
195
+ - Refresh and cross-provider operations are menu actions rather than flags.
196
+ - Codex CLI fallback is removed to preserve active-runtime-account correctness.
197
+ - The status key changes from `codex-usage` to `usage`.
198
+
199
+ ## 🔒 Security and privacy
200
+
201
+ Credential candidates are collected synchronously in memory after any compatible readiness promises settle, and are not cached, persisted, logged, formatted, or appended to the Pi session.
202
+ The protocols carry no account name or extension identity.
203
+ Only the selected provider's exact runtime match is used, and secrets are sent only to its validated official origin.
204
+ DeepSeek balance requests require Bearer authentication, send only that resolved credential from Pi's runtime auth to `https://api.deepseek.com/user/balance`, and refuse redirects.
205
+ Fireworks spend requests send only that resolved credential to the official `https://api.fireworks.ai` account-listing and billing-summary endpoints and refuse redirects.
206
+ Moonshot balance requests send only the resolved Bearer credential to the matching official Global or China balance origin and refuse redirects.
207
+ Vercel AI Gateway credit requests send only the resolved Bearer credential to `https://ai-gateway.vercel.sh/v1/credits` and refuse redirects.
208
+ MiniMax usage requests send only the resolved API key to one deterministic endpoint on the matching official Global or China API root and refuse redirects.
209
+ Baseten billing requests send only the resolved Bearer credential to `https://api.baseten.co/v1/billing/usage_summary` for an official Baseten model and refuse redirects.
210
+ Pi extensions run with the user's process privileges, so the shared event bus is not a security boundary between installed extensions.
211
+ Install only trusted extensions because they can read user files and process memory.
212
+ Protocol v1 interoperability is characterized for the repository's supported Pi runtime.
213
+ An absent or incompatible peer preserves standalone fallback and fail-closed mismatch behavior.
214
+
215
+ ## 🚧 Limitations
216
+
217
+ - Only providers with a meaningful usage source and verifiable Pi runtime auth are supported.
218
+ - GitHub Copilot quota, Kimi managed usage, Z.AI quota, and OpenAI Codex reset redemption rely on provider-owned endpoints that may change without notice.
219
+ - Codex reset redemption requires a current ChatGPT OAuth credential from Pi's login or a compatible credential source; Codex API keys cannot redeem earned subscription resets.
220
+ - xAI usage supports only a uniquely matched Pi OAuth subscription credential; xAI API keys and Management API credentials are unsupported.
221
+ - Credentials resolved for custom provider base URLs are never forwarded to the providers' official usage endpoints; effective auth origin validation requires Pi 0.81.0 or newer.
222
+ - Provider reports are snapshots and may themselves be delayed by the provider.
223
+ - DeepSeek reports current API balance only; it does not expose historical usage, quota windows, reset times, or account-wide token totals through the balance endpoint.
224
+ - Fireworks reports rated 30-day spend only; credit balance and spend caps are visible only in the Fireworks web console, and `/usage` must select one visible account before querying a multi-account key.
225
+ - Moonshot AI reports current API balance only; it does not expose historical spend, aggregate token usage, quota windows, or reset times through the balance endpoint.
226
+ - Vercel AI Gateway reports current team credits and lifetime spend only; Custom Reporting and request-rate counters are not queried.
227
+ - MiniMax Token Plan field semantics have changed over time; contradictory counts and percentages are reported as unavailable rather than guessed.
228
+ - Baseten reports organization-wide Model APIs spend, not usage attributable only to Pi's current key; Dedicated and Training spend are excluded.
229
+ - OpenRouter successful inference responses do not expose proactive request-rate counters; `/usage` reports the documented per-key credit/spend fields instead.
230
+ - A provider may not return a safe human-readable account identity.
231
+ In that case the provider and runtime credential state remain visible without exposing secrets.
232
+ - Immediate account-change events are not available from Pi; auth is re-resolved before commands, turns, and scheduled refreshes.
233
+ - Fast model support is intentionally conservative and may require an extension update when Codex adds or removes service tiers.
234
+ - Another later-loaded extension can replace the final provider payload, so arbitrary third-party payload-rewrite conflicts cannot be prevented.
235
+
236
+ ## 🗂️ Package layout
237
+
238
+ ```text
239
+ packages/pi-usage/
240
+ ├── src/ # Provider adapters, auth, settings, and presentation
241
+ │ ├── index.ts # Thin Pi entrypoint
242
+ │ └── usage.ts # Provider queries, cache, and menu
243
+ ├── dist/ # Generated Jiti runtime
244
+ ├── scripts/build-runtime.mjs # Runtime builder
245
+ ├── docs/ # Published reference documentation
246
+ └── test/ # Behavior and lifecycle coverage
247
+ ```
248
+
249
+ `src/index.ts` forwards the default factory from `usage.ts` and retains the package's named helper exports; other source modules are internal.
250
+ The generated runtime is built from `src/index.ts` and does not import back into `src`.
251
+
252
+ ## 🔎 Keywords
253
+
254
+ Pi extension, Pi coding agent, usage, quota, DeepSeek API balance, DeepSeek balance, Fireworks API spend, Fireworks rated spend, Vercel AI Gateway credits, Vercel AI Gateway usage, Baseten Model APIs spend, Baseten usage, OpenAI Codex usage, ChatGPT subscription limits, Kimi For Coding, Kimi Coding Plan usage, Moonshot AI balance, Moonshot API balance, MiniMax Token Plan, MiniMax API balance, GitHub Copilot AI credits, GitHub Copilot premium requests, OpenRouter credits, xAI OAuth usage, Grok subscription allowance, API-key spend limits, TypeScript Pi package, npm Pi extension.
255
+
256
+ ## 📄 License
257
+
258
+ MIT.
259
+ See [`LICENSE`](./LICENSE).