pi-usage-hub 1.1.0 → 1.1.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.
- package/.github/workflows/publish.yml +5 -8
- package/README.md +4 -2
- package/core/config.ts +1 -0
- package/core/registry.ts +17 -9
- package/package.json +1 -1
- package/providers/codex.ts +152 -0
- package/types.ts +6 -2
|
@@ -11,13 +11,10 @@ jobs:
|
|
|
11
11
|
contents: read
|
|
12
12
|
id-token: write
|
|
13
13
|
steps:
|
|
14
|
-
- uses: actions/checkout@
|
|
15
|
-
|
|
16
|
-
- uses: actions/setup-node@v4
|
|
14
|
+
- uses: actions/checkout@v6
|
|
15
|
+
- uses: actions/setup-node@v6
|
|
17
16
|
with:
|
|
18
|
-
node-version:
|
|
17
|
+
node-version: 24
|
|
19
18
|
registry-url: https://registry.npmjs.org
|
|
20
|
-
|
|
21
|
-
- run: npm publish --
|
|
22
|
-
env:
|
|
23
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
19
|
+
package-manager-cache: false
|
|
20
|
+
- run: npm publish --access public
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
## Why
|
|
9
9
|
|
|
10
|
-
Checking separate dashboards for DeepSeek, NewAPI relays, xAI, Kiro, OpenCode Go, and ARK breaks flow. pi-usage-hub brings their quotas and balances into one TUI panel while keeping footer integration optional and pull-based.
|
|
10
|
+
Checking separate dashboards for DeepSeek, NewAPI relays, OpenAI Codex, xAI, Kiro, OpenCode Go, and ARK breaks flow. pi-usage-hub brings their quotas and balances into one TUI panel while keeping footer integration optional and pull-based.
|
|
11
11
|
|
|
12
12
|
## Install
|
|
13
13
|
|
|
@@ -61,7 +61,8 @@ Create `~/.pi/agent/pi-usage-hub.json`. Provider order is preserved in the panel
|
|
|
61
61
|
},
|
|
62
62
|
{ "type": "kiro" },
|
|
63
63
|
{ "type": "kimi-coding" },
|
|
64
|
-
{ "type": "zai" }
|
|
64
|
+
{ "type": "zai" },
|
|
65
|
+
{ "type": "codex" }
|
|
65
66
|
]
|
|
66
67
|
}
|
|
67
68
|
```
|
|
@@ -89,6 +90,7 @@ Create `~/.pi/agent/pi-usage-hub.json`. Provider order is preserved in the panel
|
|
|
89
90
|
| `kiro` | — | Reads the Kiro OAuth entry from `auth.json` | [kiro.dev](https://kiro.dev) |
|
|
90
91
|
| `kimi-coding` | optional `apiKey` | 5h rolling window + weekly quota; falls back to the `kimi-coding` entry in `auth.json` (Kimi Code key, not the open-platform key) | [kimi.com/code](https://www.kimi.com/code) |
|
|
91
92
|
| `zai` | optional `apiKey` | 5h window, weekly, and monthly search quota; falls back to the `zai` entry in `auth.json` | [z.ai](https://z.ai) |
|
|
93
|
+
| `codex` | — | Codex rolling and 7-day usage windows; reads the `openai-codex` OAuth entry in `auth.json` | [OpenAI Codex](https://openai.com/codex/) |
|
|
92
94
|
|
|
93
95
|
For NewAPI, `token` is the **system access token** (not a chat `sk-` token), and `userId` is sent as `New-Api-User`. See [Authentication](https://www.newapi.ai/en/docs/api/management/auth) and [Generate access token](https://www.newapi.ai/en/docs/api/management/user-management/user-token-get).
|
|
94
96
|
|
package/core/config.ts
CHANGED
package/core/registry.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { makeXaiProvider } from "../providers/xai";
|
|
|
14
14
|
import { makeKiroProvider } from "../providers/kiro";
|
|
15
15
|
import { makeKimiCodingProvider } from "../providers/kimi-coding";
|
|
16
16
|
import { makeZaiProvider } from "../providers/zai";
|
|
17
|
+
import { makeCodexProvider } from "../providers/codex";
|
|
17
18
|
|
|
18
19
|
const FACTORIES = {
|
|
19
20
|
deepseek: makeDeepseekProvider,
|
|
@@ -24,12 +25,7 @@ const FACTORIES = {
|
|
|
24
25
|
kiro: makeKiroProvider,
|
|
25
26
|
"kimi-coding": makeKimiCodingProvider,
|
|
26
27
|
zai: makeZaiProvider,
|
|
27
|
-
} satisfies Record<ProviderType, (name: string, cfg: any) => UsageProvider>;
|
|
28
|
-
|
|
29
|
-
function makeProvider(cfg: BuiltinProviderConfig): UsageProvider {
|
|
30
|
-
const factory = FACTORIES[cfg.type] as (name: string, cfg: unknown) => UsageProvider;
|
|
31
|
-
return factory(cfg.name, cfg);
|
|
32
|
-
}
|
|
28
|
+
} satisfies Record<Exclude<ProviderType, "codex">, (name: string, cfg: any) => UsageProvider>;
|
|
33
29
|
|
|
34
30
|
/** Config-entry meta wins over the provider's own values, per explicitly-set field. */
|
|
35
31
|
function applyMeta(p: UsageProvider, cfg: ExtensionProviderConfig): UsageProvider {
|
|
@@ -53,6 +49,14 @@ export class ProviderRegistry {
|
|
|
53
49
|
private providers = new Map<string, UsageProvider>();
|
|
54
50
|
private extensionConfigs = new Map<string, ExtensionProviderConfig>();
|
|
55
51
|
|
|
52
|
+
private makeBuiltinProvider(cfg: BuiltinProviderConfig): UsageProvider {
|
|
53
|
+
if (cfg.type === "codex") {
|
|
54
|
+
return makeCodexProvider(cfg.name, cfg);
|
|
55
|
+
}
|
|
56
|
+
const factory = FACTORIES[cfg.type] as (name: string, config: unknown) => UsageProvider;
|
|
57
|
+
return factory(cfg.name, cfg);
|
|
58
|
+
}
|
|
59
|
+
|
|
56
60
|
register(provider: UsageProvider): void {
|
|
57
61
|
const cfg = this.extensionConfigs.get(provider.key);
|
|
58
62
|
if (cfg?.disabled) return;
|
|
@@ -80,12 +84,16 @@ export class ProviderRegistry {
|
|
|
80
84
|
}
|
|
81
85
|
|
|
82
86
|
registerBuiltin(type: ProviderType, cfg: ProviderMeta & Record<string, unknown> = {}): string | null {
|
|
83
|
-
|
|
84
|
-
if (!factory || cfg.disabled) return null;
|
|
87
|
+
if (cfg.disabled) return null;
|
|
85
88
|
const preferred = typeof (cfg as { name?: unknown }).name === "string"
|
|
86
89
|
? (cfg as { name: string }).name.trim()
|
|
87
90
|
: "";
|
|
88
91
|
const name = allocateName(preferred || type, n => this.providers.has(n));
|
|
92
|
+
if (type === "codex") {
|
|
93
|
+
this.register(makeCodexProvider(name, cfg));
|
|
94
|
+
return name;
|
|
95
|
+
}
|
|
96
|
+
const factory = FACTORIES[type] as (name: string, config: unknown) => UsageProvider;
|
|
89
97
|
this.register(factory(name, cfg));
|
|
90
98
|
return name;
|
|
91
99
|
}
|
|
@@ -115,7 +123,7 @@ export class ProviderRegistry {
|
|
|
115
123
|
const builtin = cfg as BuiltinProviderConfig;
|
|
116
124
|
if (builtin.disabled) continue;
|
|
117
125
|
// Name pre-allocated by the config parser; register verbatim (last write wins).
|
|
118
|
-
this.register(
|
|
126
|
+
this.register(this.makeBuiltinProvider(builtin));
|
|
119
127
|
} else {
|
|
120
128
|
this.extensionConfigs.set(cfg.name, cfg as ExtensionProviderConfig);
|
|
121
129
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type { CodexConfig, UsageData, UsageProvider, UsageReport } from "../types";
|
|
4
|
+
import { agentDir } from "../core/config";
|
|
5
|
+
import { formatDurationSeconds } from "../utils/duration";
|
|
6
|
+
|
|
7
|
+
const USAGE_URL = "https://chatgpt.com/backend-api/wham/usage";
|
|
8
|
+
const FETCH_TIMEOUT_MS = 15_000;
|
|
9
|
+
const WEEK_SECONDS = 7 * 24 * 60 * 60;
|
|
10
|
+
|
|
11
|
+
interface StoredCodexCredential {
|
|
12
|
+
type?: string;
|
|
13
|
+
access?: string;
|
|
14
|
+
accountId?: string;
|
|
15
|
+
account_id?: string;
|
|
16
|
+
expires?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface UsageWindow {
|
|
20
|
+
used_percent?: number | null;
|
|
21
|
+
reset_after_seconds?: number | null;
|
|
22
|
+
reset_at?: number | null;
|
|
23
|
+
limit_window_seconds?: number | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface UsagePayload {
|
|
27
|
+
rate_limit?: {
|
|
28
|
+
primary_window?: UsageWindow | null;
|
|
29
|
+
secondary_window?: UsageWindow | null;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface Credentials {
|
|
34
|
+
accessToken: string;
|
|
35
|
+
accountId: string;
|
|
36
|
+
expires?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function readCredentials(): Credentials | null {
|
|
40
|
+
const path = join(agentDir(), "auth.json");
|
|
41
|
+
if (!existsSync(path)) return null;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const auth = JSON.parse(readFileSync(path, "utf-8")) as { "openai-codex"?: StoredCodexCredential };
|
|
45
|
+
const entry = auth["openai-codex"];
|
|
46
|
+
const accountId = entry?.accountId ?? entry?.account_id;
|
|
47
|
+
if (entry?.type !== "oauth" || typeof entry.access !== "string" || !entry.access || typeof accountId !== "string" || !accountId) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return { accessToken: entry.access, accountId, expires: entry.expires };
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isFresh(credentials: Credentials): boolean {
|
|
57
|
+
return credentials.expires == null || Date.now() < credentials.expires;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function toUsageData(window: UsageWindow | null | undefined): UsageData | undefined {
|
|
61
|
+
if (!window || typeof window.used_percent !== "number" || !Number.isFinite(window.used_percent)) return undefined;
|
|
62
|
+
|
|
63
|
+
let resetSeconds: number | undefined;
|
|
64
|
+
if (typeof window.reset_after_seconds === "number" && Number.isFinite(window.reset_after_seconds)) {
|
|
65
|
+
resetSeconds = window.reset_after_seconds;
|
|
66
|
+
} else if (typeof window.reset_at === "number" && Number.isFinite(window.reset_at)) {
|
|
67
|
+
const resetAtMs = window.reset_at > 100_000_000_000 ? window.reset_at : window.reset_at * 1000;
|
|
68
|
+
resetSeconds = (resetAtMs - Date.now()) / 1000;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
pct: Math.min(100, Math.max(0, window.used_percent)),
|
|
73
|
+
resetsIn: resetSeconds != null && resetSeconds > 0 ? formatDurationSeconds(resetSeconds) : undefined,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isRollingWindow(window: UsageWindow | null | undefined): boolean {
|
|
78
|
+
const seconds = window?.limit_window_seconds;
|
|
79
|
+
return typeof seconds === "number" && seconds > 0 && seconds < WEEK_SECONDS - 24 * 60 * 60;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function isWeeklyWindow(window: UsageWindow | null | undefined): boolean {
|
|
83
|
+
const seconds = window?.limit_window_seconds;
|
|
84
|
+
return typeof seconds === "number" && Math.abs(seconds - WEEK_SECONDS) <= 24 * 60 * 60;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function parseCodexUsage(payload: UsagePayload): UsageReport {
|
|
88
|
+
const rateLimit = payload.rate_limit;
|
|
89
|
+
const primary = rateLimit?.primary_window;
|
|
90
|
+
const secondary = rateLimit?.secondary_window;
|
|
91
|
+
|
|
92
|
+
let session = isRollingWindow(primary) ? toUsageData(primary) : undefined;
|
|
93
|
+
let weekly = isWeeklyWindow(primary) ? toUsageData(primary) : undefined;
|
|
94
|
+
|
|
95
|
+
if (!session && isRollingWindow(secondary)) session = toUsageData(secondary);
|
|
96
|
+
if (!weekly && isWeeklyWindow(secondary)) weekly = toUsageData(secondary);
|
|
97
|
+
|
|
98
|
+
// Old responses omit duration metadata. With two windows, their documented order is rolling then weekly.
|
|
99
|
+
if (!session && primary && secondary) session = toUsageData(primary);
|
|
100
|
+
if (!weekly && secondary) weekly = toUsageData(secondary);
|
|
101
|
+
// A single undifferentiated window is treated as weekly so the UI never invents a 5h allowance.
|
|
102
|
+
if (!session && !weekly && primary) weekly = toUsageData(primary);
|
|
103
|
+
|
|
104
|
+
if (!session && !weekly) return { error: "could not parse usage" };
|
|
105
|
+
return {
|
|
106
|
+
...(session ? { session } : {}),
|
|
107
|
+
...(weekly ? { weekly } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function makeCodexProvider(name: string, cfg: CodexConfig = {}): UsageProvider {
|
|
112
|
+
return {
|
|
113
|
+
key: name,
|
|
114
|
+
matchProviders: cfg.matchProviders ?? [name, "openai-codex"],
|
|
115
|
+
shortLabel: cfg.shortLabel ?? "Codex",
|
|
116
|
+
label: cfg.label ?? "OpenAI Codex",
|
|
117
|
+
hidden: cfg.hidden,
|
|
118
|
+
detect: () => readCredentials() !== null,
|
|
119
|
+
|
|
120
|
+
fetchUsage: async (): Promise<UsageReport> => {
|
|
121
|
+
const stored = readCredentials();
|
|
122
|
+
if (!stored) return { error: "not logged in — run /login openai-codex" };
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
if (!isFresh(stored)) return { error: "session expired — run /login openai-codex" };
|
|
126
|
+
const accessToken = stored.accessToken;
|
|
127
|
+
|
|
128
|
+
const controller = new AbortController();
|
|
129
|
+
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
130
|
+
try {
|
|
131
|
+
const res = await fetch(USAGE_URL, {
|
|
132
|
+
headers: {
|
|
133
|
+
accept: "application/json",
|
|
134
|
+
authorization: `Bearer ${accessToken}`,
|
|
135
|
+
"chatgpt-account-id": stored.accountId,
|
|
136
|
+
},
|
|
137
|
+
signal: controller.signal,
|
|
138
|
+
});
|
|
139
|
+
if (res.status === 401 || res.status === 403) return { error: "session expired — run /login openai-codex" };
|
|
140
|
+
if (!res.ok) return { error: `HTTP ${res.status}` };
|
|
141
|
+
return parseCodexUsage(await res.json() as UsagePayload);
|
|
142
|
+
} finally {
|
|
143
|
+
clearTimeout(timer);
|
|
144
|
+
}
|
|
145
|
+
} catch (err: any) {
|
|
146
|
+
if (err?.name === "AbortError") return { error: "timeout" };
|
|
147
|
+
return { error: err?.message ?? String(err) };
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
package/types.ts
CHANGED
|
@@ -33,7 +33,8 @@ export type ProviderType =
|
|
|
33
33
|
| "xai"
|
|
34
34
|
| "kiro"
|
|
35
35
|
| "kimi-coding"
|
|
36
|
-
| "zai"
|
|
36
|
+
| "zai"
|
|
37
|
+
| "codex";
|
|
37
38
|
|
|
38
39
|
/** Shared optional fields for factories and config entries. */
|
|
39
40
|
export interface ProviderMeta {
|
|
@@ -84,6 +85,8 @@ export interface ZaiConfig extends ProviderMeta {
|
|
|
84
85
|
apiKey?: string;
|
|
85
86
|
}
|
|
86
87
|
|
|
88
|
+
export interface CodexConfig extends ProviderMeta {}
|
|
89
|
+
|
|
87
90
|
/** L1 entry after parse — name always set (auto: type, type-2, … when omitted/clashing). */
|
|
88
91
|
export type BuiltinProviderConfig =
|
|
89
92
|
| ({ name: string; type: "deepseek" } & DeepseekConfig)
|
|
@@ -93,7 +96,8 @@ export type BuiltinProviderConfig =
|
|
|
93
96
|
| ({ name: string; type: "xai" } & XaiConfig)
|
|
94
97
|
| ({ name: string; type: "kiro" } & KiroConfig)
|
|
95
98
|
| ({ name: string; type: "kimi-coding" } & KimiCodingConfig)
|
|
96
|
-
| ({ name: string; type: "zai" } & ZaiConfig)
|
|
99
|
+
| ({ name: string; type: "zai" } & ZaiConfig)
|
|
100
|
+
| ({ name: string; type: "codex" } & CodexConfig);
|
|
97
101
|
|
|
98
102
|
/**
|
|
99
103
|
* L2 entry: no `type` — targets an externally-registered (custom) provider.
|