@scnewma/pi-codex-rate-limits 0.1.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/.github/workflows/publish.yml +28 -0
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/assets/example.png +0 -0
- package/package.json +18 -0
- package/src/index.ts +138 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
id-token: write
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
publish:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- uses: actions/setup-node@v4
|
|
20
|
+
with:
|
|
21
|
+
node-version: 22
|
|
22
|
+
registry-url: https://registry.npmjs.org
|
|
23
|
+
|
|
24
|
+
- run: npm install
|
|
25
|
+
|
|
26
|
+
- run: npm publish --provenance --access public
|
|
27
|
+
env:
|
|
28
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shaun Newman
|
|
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,17 @@
|
|
|
1
|
+
# pi-codex-rate-limits
|
|
2
|
+
|
|
3
|
+
See your rate limits from your Codex subscription in [pi](https://pi.dev/).
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
pi install npm:@scnewma/pi-codex-rate-limits
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
/codex-rate-limits
|
|
17
|
+
```
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scnewma/pi-codex-rate-limits",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "Pi extension that shows OpenAI Codex rate limit usage via /codex-rate-limits",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/scnewma/pi-codex-rate-limits"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"pi-package"
|
|
12
|
+
],
|
|
13
|
+
"pi": {
|
|
14
|
+
"extensions": [
|
|
15
|
+
"./src/index.ts"
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
const API_BASE = "https://chatgpt.com/backend-api";
|
|
4
|
+
const USAGE_URL = `${API_BASE}/wham/usage`;
|
|
5
|
+
const PROVIDER = "openai-codex";
|
|
6
|
+
|
|
7
|
+
interface UsageWindow {
|
|
8
|
+
used_percent: number;
|
|
9
|
+
limit_window_seconds: number;
|
|
10
|
+
reset_after_seconds: number;
|
|
11
|
+
reset_at: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface UsageResponse {
|
|
15
|
+
plan_type: string;
|
|
16
|
+
rate_limit: {
|
|
17
|
+
allowed: boolean;
|
|
18
|
+
limit_reached: boolean;
|
|
19
|
+
primary_window: UsageWindow;
|
|
20
|
+
secondary_window?: UsageWindow | null;
|
|
21
|
+
} | null;
|
|
22
|
+
credits?: { has_credits: boolean; unlimited?: boolean; balance?: number } | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function formatTimeRemaining(secs: number): string {
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
const diff = secs * 1000 - now;
|
|
28
|
+
const d = new Date(secs * 1000);
|
|
29
|
+
const hours = Math.floor(diff / 3_600_000);
|
|
30
|
+
const mins = Math.floor((diff % 3_600_000) / 60_000);
|
|
31
|
+
const t = d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
|
32
|
+
const date = d.toLocaleDateString([], { month: "short", day: "numeric" });
|
|
33
|
+
|
|
34
|
+
if (diff <= 0) return "now";
|
|
35
|
+
if (hours >= 24) {
|
|
36
|
+
const days = Math.floor(hours / 24);
|
|
37
|
+
return `${days}d ${hours % 24}h (${date} ${t})`;
|
|
38
|
+
}
|
|
39
|
+
if (hours > 0) return `${hours}h ${mins}m (${t})`;
|
|
40
|
+
return `${mins}m (${t})`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function windowLabel(secs: number): string {
|
|
44
|
+
if (secs >= 604800) return "Weekly";
|
|
45
|
+
const hours = secs / 3600;
|
|
46
|
+
if (hours === 5) return "5h";
|
|
47
|
+
if (hours >= 24) return `${Math.floor(hours / 24)}d`;
|
|
48
|
+
return `${hours}h`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function bar(percent: number, width = 16): string {
|
|
52
|
+
const filled = Math.round((percent / 100) * width);
|
|
53
|
+
return "\u2588".repeat(filled) + "\u2591".repeat(width - filled);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default function(pi: ExtensionAPI) {
|
|
57
|
+
pi.registerCommand("codex-rate-limits", {
|
|
58
|
+
description: "Show OpenAI Codex rate limit usage and reset times",
|
|
59
|
+
handler: async (_args: string[], ctx: ExtensionCommandContext) => {
|
|
60
|
+
const token = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER);
|
|
61
|
+
if (!token) {
|
|
62
|
+
ctx.ui.notify("No Codex login found. Use an OpenAI Codex (oauth) provider.", "warning");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let resp: Response;
|
|
67
|
+
try {
|
|
68
|
+
resp = await fetch(USAGE_URL, {
|
|
69
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
70
|
+
});
|
|
71
|
+
} catch (err) {
|
|
72
|
+
ctx.ui.notify(`Usage API network error: ${err instanceof Error ? err.message : String(err)}`, "error");
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!resp.ok) {
|
|
77
|
+
const body = await resp.text().catch(() => "<failed to read response body>");
|
|
78
|
+
ctx.ui.notify(`Usage API error: ${resp.status}: ${body}`, "error");
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let data: UsageResponse;
|
|
83
|
+
try {
|
|
84
|
+
data = (await resp.json()) as UsageResponse;
|
|
85
|
+
} catch (err) {
|
|
86
|
+
ctx.ui.notify(`Usage API returned invalid JSON: ${err instanceof Error ? err.message : String(err)}`, "error");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!data?.rate_limit) {
|
|
91
|
+
ctx.ui.notify("No usage limit data", "warning");
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const p = data.rate_limit.primary_window;
|
|
96
|
+
const s = data.rate_limit.secondary_window;
|
|
97
|
+
const LABEL_W = 12;
|
|
98
|
+
|
|
99
|
+
// Build structured rows: [label, barAndPct, reset?]
|
|
100
|
+
const rows: string[][] = [];
|
|
101
|
+
const addWindow = (w: UsageWindow) => {
|
|
102
|
+
const pct = (100 - w.used_percent).toFixed(0);
|
|
103
|
+
rows.push([
|
|
104
|
+
windowLabel(w.limit_window_seconds).padStart(LABEL_W),
|
|
105
|
+
`${bar(w.used_percent)} ${pct}% left`,
|
|
106
|
+
]);
|
|
107
|
+
rows.push([
|
|
108
|
+
"".padEnd(LABEL_W),
|
|
109
|
+
`resets ${formatTimeRemaining(w.reset_at)}`,
|
|
110
|
+
]);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (p) addWindow(p);
|
|
114
|
+
if (p && s) rows.push(["", ""]); // gap
|
|
115
|
+
if (s) addWindow(s);
|
|
116
|
+
|
|
117
|
+
if (data.credits?.balance && data.credits.balance > 0) {
|
|
118
|
+
rows.push(["Credits", `\u2500 ${data.credits.balance}`]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Flatten to content lines and measure
|
|
122
|
+
const content: string[] = [
|
|
123
|
+
` Codex Rate Limits \u00b7 ${data.plan_type} `,
|
|
124
|
+
...rows.map(([a, b]) => ` ${a} ${b} `),
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
const w = Math.max(...content.map((l) => l.length));
|
|
128
|
+
const sep = "\u2500".repeat(w);
|
|
129
|
+
const lines: string[] = [
|
|
130
|
+
`\u250c${sep}\u2510`,
|
|
131
|
+
...content.map((l) => `\u2502${l.padEnd(w)}\u2502`),
|
|
132
|
+
`\u2514${sep}\u2518`,
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
ctx.ui.notify(lines.join("\n"), "info");
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
}
|