pi-harness-runtime 0.9.21 → 0.9.25
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/CHANGELOG.md +24 -0
- package/footer-status.ts +84 -44
- package/harness/auto-quota-resume.ts +15 -12
- package/harness/e2e/minimax-quota-scraper.ts +9 -9
- package/harness/e2e/openai-quota-scraper.ts +442 -0
- package/harness/loop-runtime.ts +11 -7
- package/index.ts +170 -53
- package/package.json +2 -2
- package/packages/a2a-adapter/package.json +1 -1
- package/packages/auth/dist/minimax-browser-auth.d.ts +89 -0
- package/packages/auth/dist/minimax-browser-auth.d.ts.map +1 -0
- package/packages/auth/dist/minimax-browser-auth.js +890 -0
- package/packages/auth/dist/minimax-browser-auth.js.map +1 -0
- package/packages/auth/dist/run-minimax-auth.d.ts +19 -0
- package/packages/auth/dist/run-minimax-auth.d.ts.map +1 -0
- package/packages/auth/dist/run-minimax-auth.js +112 -0
- package/packages/auth/dist/run-minimax-auth.js.map +1 -0
- package/packages/auth/package.json +1 -1
- package/packages/auth/tsconfig.json +9 -0
- package/packages/autonomous-refactor/package.json +1 -1
- package/packages/autonomous-runtime/package.json +1 -1
- package/packages/cache-strategy/package.json +1 -1
- package/packages/capability-registry/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/cli-plugin-sdk/package.json +1 -1
- package/packages/clipboard/package.json +1 -1
- package/packages/code-generation/package.json +1 -1
- package/packages/code-review/package.json +1 -1
- package/packages/codex-adapter/package.json +1 -1
- package/packages/context-compiler/package.json +1 -1
- package/packages/context-discovery/package.json +1 -1
- package/packages/context-manager/package.json +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/cost-optimizer/package.json +1 -1
- package/packages/dependency-analyzer/package.json +1 -1
- package/packages/django-plugin/package.json +1 -1
- package/packages/doc-generator/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/evaluation-runner/package.json +1 -1
- package/packages/event-bus/package.json +1 -1
- package/packages/event-store/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/feedback-collector/package.json +1 -1
- package/packages/framework-detector/package.json +1 -1
- package/packages/framework-plugin-sdk/package.json +1 -1
- package/packages/frappe-plugin/package.json +1 -1
- package/packages/generic-web-plugin/package.json +1 -1
- package/packages/health-monitor/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/laravel-plugin/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/mcp-adapter/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/model-registry/package.json +1 -1
- package/packages/nextjs-plugin/package.json +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/observability/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/privilege-broker/package.json +1 -1
- package/packages/project-analyzer/package.json +1 -1
- package/packages/projection-engine/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/prompt-versioning/package.json +1 -1
- package/packages/provider-adapter-sdk/package.json +1 -1
- package/packages/provider-router/package.json +1 -1
- package/packages/provider-selector/package.json +1 -1
- package/packages/providers/dist/provider-id.d.ts +5 -0
- package/packages/providers/dist/provider-id.d.ts.map +1 -1
- package/packages/providers/dist/provider-id.js +6 -1
- package/packages/providers/dist/provider-id.js.map +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/providers/src/provider-id.ts +6 -1
- package/packages/quota-manager/dist/tui-usage-monitor.d.ts.map +1 -1
- package/packages/quota-manager/dist/tui-usage-monitor.js +0 -6
- package/packages/quota-manager/dist/tui-usage-monitor.js.map +1 -1
- package/packages/quota-manager/package.json +1 -1
- package/packages/quota-manager/src/tui-usage-monitor.ts +0 -21
- package/packages/rate-limiter/package.json +1 -1
- package/packages/react-vite-plugin/package.json +1 -1
- package/packages/release-manager/package.json +1 -1
- package/packages/requirement-compiler/package.json +1 -1
- package/packages/runtime/package.json +1 -1
- package/packages/scheduler/package.json +1 -1
- package/packages/scheduler-adapter/package.json +1 -1
- package/packages/session/package.json +1 -1
- package/packages/session-api/package.json +1 -1
- package/packages/session-export/package.json +1 -1
- package/packages/shared-context/package.json +1 -1
- package/packages/skill-registry/package.json +1 -1
- package/packages/sprint-planner/package.json +1 -1
- package/packages/subscription-engine/package.json +1 -1
- package/packages/task-compiler/package.json +1 -1
- package/packages/test-data-generator/package.json +1 -1
- package/packages/test-generator/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/token-optimizer/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/workflow-events/package.json +1 -1
- package/packages/workspace-scanner/package.json +1 -1
- package/packages/worktree/package.json +1 -1
- package/packages/dependency-analyzer/dist/analyzer.d.ts +0 -17
- package/packages/dependency-analyzer/dist/analyzer.d.ts.map +0 -1
- package/packages/dependency-analyzer/dist/analyzer.js +0 -106
- package/packages/dependency-analyzer/dist/analyzer.js.map +0 -1
- package/packages/dependency-analyzer/dist/imports.d.ts +0 -12
- package/packages/dependency-analyzer/dist/imports.d.ts.map +0 -1
- package/packages/dependency-analyzer/dist/imports.js +0 -73
- package/packages/dependency-analyzer/dist/imports.js.map +0 -1
- package/packages/dependency-analyzer/dist/index.d.ts +0 -4
- package/packages/dependency-analyzer/dist/index.d.ts.map +0 -1
- package/packages/dependency-analyzer/dist/index.js +0 -3
- package/packages/dependency-analyzer/dist/index.js.map +0 -1
- package/packages/dependency-analyzer/dist/test/analyzer.test.js +0 -37
- package/packages/dependency-analyzer/dist/types.d.ts +0 -28
- package/packages/dependency-analyzer/dist/types.d.ts.map +0 -1
- package/packages/dependency-analyzer/dist/types.js +0 -5
- package/packages/dependency-analyzer/dist/types.js.map +0 -1
- package/packages/intent-analyzer/dist/analyzer.d.ts +0 -13
- package/packages/intent-analyzer/dist/analyzer.d.ts.map +0 -1
- package/packages/intent-analyzer/dist/analyzer.js +0 -72
- package/packages/intent-analyzer/dist/analyzer.js.map +0 -1
- package/packages/intent-analyzer/dist/index.d.ts +0 -3
- package/packages/intent-analyzer/dist/index.d.ts.map +0 -1
- package/packages/intent-analyzer/dist/index.js +0 -2
- package/packages/intent-analyzer/dist/index.js.map +0 -1
- package/packages/intent-analyzer/dist/test/analyzer.test.js +0 -56
- package/packages/intent-analyzer/dist/types.d.ts +0 -26
- package/packages/intent-analyzer/dist/types.d.ts.map +0 -1
- package/packages/intent-analyzer/dist/types.js +0 -7
- package/packages/intent-analyzer/dist/types.js.map +0 -1
- package/packages/workspace-scanner/dist/detect.d.ts +0 -3
- package/packages/workspace-scanner/dist/detect.d.ts.map +0 -1
- package/packages/workspace-scanner/dist/detect.js +0 -58
- package/packages/workspace-scanner/dist/detect.js.map +0 -1
- package/packages/workspace-scanner/dist/git.d.ts +0 -7
- package/packages/workspace-scanner/dist/git.d.ts.map +0 -1
- package/packages/workspace-scanner/dist/git.js +0 -75
- package/packages/workspace-scanner/dist/git.js.map +0 -1
- package/packages/workspace-scanner/dist/index.d.ts +0 -5
- package/packages/workspace-scanner/dist/index.d.ts.map +0 -1
- package/packages/workspace-scanner/dist/index.js +0 -4
- package/packages/workspace-scanner/dist/index.js.map +0 -1
- package/packages/workspace-scanner/dist/scanner.d.ts +0 -11
- package/packages/workspace-scanner/dist/scanner.d.ts.map +0 -1
- package/packages/workspace-scanner/dist/scanner.js +0 -46
- package/packages/workspace-scanner/dist/scanner.js.map +0 -1
- package/packages/workspace-scanner/dist/test/scanner.test.js +0 -34
- package/packages/workspace-scanner/dist/types.d.ts +0 -35
- package/packages/workspace-scanner/dist/types.d.ts.map +0 -1
- package/packages/workspace-scanner/dist/types.js +0 -5
- package/packages/workspace-scanner/dist/types.js.map +0 -1
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Quota Scraper — RFC-0031+
|
|
3
|
+
*
|
|
4
|
+
* Scrapes quota data from ChatGPT Codex analytics page using Playwright.
|
|
5
|
+
* Uses cookie-based authentication to access the usage API.
|
|
6
|
+
*
|
|
7
|
+
* Target: https://chatgpt.com/backend-api/wham/usage
|
|
8
|
+
*
|
|
9
|
+
* Response shape:
|
|
10
|
+
* {
|
|
11
|
+
* "rate_limit": {
|
|
12
|
+
* "primary_window": {
|
|
13
|
+
* "used_percent": 7, // Weekly usage percentage (0-100)
|
|
14
|
+
* "limit_window_seconds": 604800, // 7-day window
|
|
15
|
+
* "reset_after_seconds": 491924, // Seconds until reset
|
|
16
|
+
* "reset_at": 1785649269 // Unix timestamp
|
|
17
|
+
* }
|
|
18
|
+
* },
|
|
19
|
+
* "credits": {
|
|
20
|
+
* "has_credits": false,
|
|
21
|
+
* "balance": "0"
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
27
|
+
import { join } from "node:path";
|
|
28
|
+
import { homedir } from "node:os";
|
|
29
|
+
|
|
30
|
+
export interface OpenAIQuotaData {
|
|
31
|
+
provider: "openai";
|
|
32
|
+
/** Weekly used percentage (0-100) */
|
|
33
|
+
weeklyUsedPct: number;
|
|
34
|
+
/** When the weekly window resets (e.g., "5 days 16 hr") */
|
|
35
|
+
weeklyResetsAt: string;
|
|
36
|
+
/** Epoch ms when the weekly window resets — precise UTC timestamp */
|
|
37
|
+
weeklyResetsAtEpoch?: number;
|
|
38
|
+
/** Seconds until reset */
|
|
39
|
+
resetAfterSeconds?: number;
|
|
40
|
+
/** Credits balance (if available) */
|
|
41
|
+
creditBalance?: string;
|
|
42
|
+
/** Raw API response URL for diagnostics */
|
|
43
|
+
apiEndpoint?: string;
|
|
44
|
+
/** Timestamp of scrape */
|
|
45
|
+
scrapedAt: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Typed API response from /backend-api/wham/usage */
|
|
49
|
+
interface OpenAIUsageResponse {
|
|
50
|
+
route_rate_limits?: Array<{
|
|
51
|
+
rate_limit?: { limit_window_seconds?: number; used_percent?: number };
|
|
52
|
+
}>;
|
|
53
|
+
rate_limit?: {
|
|
54
|
+
primary_window?: {
|
|
55
|
+
used_percent?: number;
|
|
56
|
+
limit_window_seconds?: number;
|
|
57
|
+
reset_after_seconds?: number;
|
|
58
|
+
reset_at?: number;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
credits?: {
|
|
62
|
+
has_credits?: boolean;
|
|
63
|
+
balance?: string;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface OpenAIScraperConfig {
|
|
68
|
+
/** Path to Netscape-format cookie file */
|
|
69
|
+
cookieFile?: string;
|
|
70
|
+
/** Headless mode (default: true) */
|
|
71
|
+
headless?: boolean;
|
|
72
|
+
/** Timeout in ms (default: 60000) */
|
|
73
|
+
timeout?: number;
|
|
74
|
+
/** Chrome executable path (optional) */
|
|
75
|
+
chromePath?: string;
|
|
76
|
+
/** Suppress console output — for background/auto-fetch use */
|
|
77
|
+
quiet?: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const DEFAULT_COOKIE_FILE = join(homedir(), ".config", "openai-cookies.txt");
|
|
81
|
+
const USAGE_API = "https://chatgpt.com/backend-api/wham/usage";
|
|
82
|
+
const ANALYTICS_URL =
|
|
83
|
+
"https://chatgpt.com/codex/cloud/settings/analytics#usage";
|
|
84
|
+
|
|
85
|
+
interface NetscapeCookie {
|
|
86
|
+
name: string;
|
|
87
|
+
value: string;
|
|
88
|
+
domain: string;
|
|
89
|
+
path: string;
|
|
90
|
+
secure: boolean;
|
|
91
|
+
httpOnly: boolean;
|
|
92
|
+
expires?: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Load Netscape-format cookies from file
|
|
97
|
+
*/
|
|
98
|
+
function loadNetscapeCookies(path: string): NetscapeCookie[] {
|
|
99
|
+
const cookies: NetscapeCookie[] = [];
|
|
100
|
+
|
|
101
|
+
if (!existsSync(path)) {
|
|
102
|
+
return cookies;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const lines = readFileSync(path, "utf-8").split("\n");
|
|
106
|
+
for (const line of lines) {
|
|
107
|
+
if (!line || (line.startsWith("#") && !line.startsWith("#HttpOnly_"))) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let httpOnly = false;
|
|
112
|
+
let trimmed = line;
|
|
113
|
+
if (trimmed.startsWith("#HttpOnly_")) {
|
|
114
|
+
httpOnly = true;
|
|
115
|
+
trimmed = trimmed.slice("#HttpOnly_".length);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const parts = trimmed.split("\t");
|
|
119
|
+
if (parts.length < 7) continue;
|
|
120
|
+
|
|
121
|
+
const [domain, _flag, cookiePath, secure, expires, name, value] = parts;
|
|
122
|
+
cookies.push({
|
|
123
|
+
name,
|
|
124
|
+
value,
|
|
125
|
+
domain,
|
|
126
|
+
path: cookiePath || "/",
|
|
127
|
+
secure: secure.toUpperCase() === "TRUE",
|
|
128
|
+
httpOnly,
|
|
129
|
+
expires: parseInt(expires, 10) || undefined,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return cookies;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Format remaining seconds into human-readable string
|
|
138
|
+
*/
|
|
139
|
+
function formatRemainsSeconds(seconds: number): string {
|
|
140
|
+
if (seconds <= 0) return "soon";
|
|
141
|
+
|
|
142
|
+
const days = Math.floor(seconds / 86400);
|
|
143
|
+
const hr = Math.floor((seconds % 86400) / 3600);
|
|
144
|
+
const min = Math.floor((seconds % 3600) / 60);
|
|
145
|
+
|
|
146
|
+
const parts: string[] = [];
|
|
147
|
+
if (days > 0) parts.push(`${days} days`);
|
|
148
|
+
if (hr > 0) parts.push(`${hr} hr`);
|
|
149
|
+
if (min > 0 && days === 0) parts.push(`${min} min`);
|
|
150
|
+
|
|
151
|
+
return parts.join(" ") || "0 min";
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* OpenAI Quota Scraper
|
|
156
|
+
*
|
|
157
|
+
* Uses Playwright to fetch quota data from ChatGPT Codex analytics.
|
|
158
|
+
*/
|
|
159
|
+
export class OpenAIQuotaScraper {
|
|
160
|
+
private readonly config: Required<OpenAIScraperConfig>;
|
|
161
|
+
|
|
162
|
+
constructor(config: OpenAIScraperConfig = {}) {
|
|
163
|
+
this.config = {
|
|
164
|
+
cookieFile: config.cookieFile ?? DEFAULT_COOKIE_FILE,
|
|
165
|
+
headless: config.headless ?? true,
|
|
166
|
+
timeout: config.timeout ?? 60000,
|
|
167
|
+
chromePath: config.chromePath ?? "",
|
|
168
|
+
quiet: config.quiet ?? false,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Set cookie file path
|
|
174
|
+
*/
|
|
175
|
+
setCookieFile(path: string): this {
|
|
176
|
+
this.config.cookieFile = path;
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Enable/disable quiet mode
|
|
182
|
+
*/
|
|
183
|
+
setQuiet(quiet: boolean): this {
|
|
184
|
+
this.config.quiet = quiet;
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Check if any cookie source exists
|
|
190
|
+
*/
|
|
191
|
+
hasCookieFile(): boolean {
|
|
192
|
+
return existsSync(this.config.cookieFile);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Scrape quota data using Playwright
|
|
197
|
+
*/
|
|
198
|
+
async scrape(): Promise<OpenAIQuotaData> {
|
|
199
|
+
const { chromium } = await import("playwright");
|
|
200
|
+
|
|
201
|
+
const cookies = loadNetscapeCookies(this.config.cookieFile);
|
|
202
|
+
if (cookies.length === 0) {
|
|
203
|
+
const dropHint = join(homedir(), ".pi-harness-runtime", "cookies");
|
|
204
|
+
const msg = `No OpenAI cookies found.
|
|
205
|
+
Drop your chatgpt.com cookies (Netscape or EditThisCookie JSON) into:
|
|
206
|
+
${dropHint}
|
|
207
|
+
Then run: bun run packages/cookie-sanitizer/src/sync.ts`;
|
|
208
|
+
if (!this.config.quiet) console.error(`[OpenAIQuotaScraper] ${msg}`);
|
|
209
|
+
throw new Error(msg);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Launch browser
|
|
213
|
+
if (!this.config.quiet) {
|
|
214
|
+
console.log("[OpenAIQuotaScraper] Launching browser...");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const browser = await chromium.launch({
|
|
218
|
+
executablePath: this.config.chromePath || undefined,
|
|
219
|
+
headless: this.config.headless,
|
|
220
|
+
args: ["--no-sandbox", "--disable-dev-shm-usage"],
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const context = await browser.newContext({
|
|
224
|
+
locale: "en-US",
|
|
225
|
+
viewport: { width: 1440, height: 900 },
|
|
226
|
+
userAgent:
|
|
227
|
+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// Inject cookies
|
|
231
|
+
await context.addCookies(
|
|
232
|
+
cookies.map((c) => ({
|
|
233
|
+
name: c.name,
|
|
234
|
+
value: c.value,
|
|
235
|
+
domain: c.domain,
|
|
236
|
+
path: c.path,
|
|
237
|
+
secure: c.secure,
|
|
238
|
+
httpOnly: c.httpOnly,
|
|
239
|
+
expires: c.expires,
|
|
240
|
+
})),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const page = await context.newPage();
|
|
244
|
+
|
|
245
|
+
// Capture API responses
|
|
246
|
+
let usageData: any = null;
|
|
247
|
+
|
|
248
|
+
page.on("response", async (response) => {
|
|
249
|
+
const url = response.url();
|
|
250
|
+
if (url.includes("/wham/usage")) {
|
|
251
|
+
try {
|
|
252
|
+
usageData = await response.json().catch(() => null);
|
|
253
|
+
} catch {
|
|
254
|
+
// Ignore parse errors
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
try {
|
|
260
|
+
// Navigate to analytics page
|
|
261
|
+
if (!this.config.quiet) {
|
|
262
|
+
console.log("[OpenAIQuotaScraper] Navigating to analytics page...");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
await page.goto(ANALYTICS_URL, {
|
|
266
|
+
waitUntil: "domcontentloaded",
|
|
267
|
+
timeout: this.config.timeout,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Wait for JS to render
|
|
271
|
+
await page.waitForTimeout(5000);
|
|
272
|
+
|
|
273
|
+
// Wait for network to settle
|
|
274
|
+
try {
|
|
275
|
+
await page.waitForLoadState("networkidle", { timeout: 10000 });
|
|
276
|
+
} catch {
|
|
277
|
+
// Network idle might not be achievable
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Check if redirected to login
|
|
281
|
+
const currentUrl = page.url();
|
|
282
|
+
if (currentUrl.includes("login") || currentUrl.includes("auth")) {
|
|
283
|
+
const msg =
|
|
284
|
+
"OpenAI cookies are expired or insufficient. Please re-export cookies from chatgpt.com.";
|
|
285
|
+
if (!this.config.quiet) console.error("[OpenAIQuotaScraper] " + msg);
|
|
286
|
+
throw new Error(msg);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Parse the usage data from the captured API response
|
|
290
|
+
if (!usageData) {
|
|
291
|
+
const msg = "Failed to capture usage data from API";
|
|
292
|
+
if (!this.config.quiet) console.error("[OpenAIQuotaScraper] " + msg);
|
|
293
|
+
throw new Error(msg);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Extract weekly usage from primary_window
|
|
297
|
+
const primaryWindow = usageData?.rate_limit?.primary_window;
|
|
298
|
+
const weeklyUsedPct = primaryWindow?.used_percent ?? 0;
|
|
299
|
+
const resetAfterSeconds = primaryWindow?.reset_after_seconds ?? 0;
|
|
300
|
+
const resetAtEpoch = primaryWindow?.reset_at;
|
|
301
|
+
const weeklyResetsAt = formatRemainsSeconds(resetAfterSeconds);
|
|
302
|
+
|
|
303
|
+
// Extract credits if available
|
|
304
|
+
const credits = usageData?.credits;
|
|
305
|
+
const creditBalance = credits?.has_credits ? credits.balance : undefined;
|
|
306
|
+
|
|
307
|
+
if (!this.config.quiet) {
|
|
308
|
+
console.log(
|
|
309
|
+
`[OpenAIQuotaScraper] Weekly usage: ${weeklyUsedPct}%, resets in ${weeklyResetsAt}`,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
provider: "openai",
|
|
315
|
+
weeklyUsedPct,
|
|
316
|
+
weeklyResetsAt,
|
|
317
|
+
weeklyResetsAtEpoch: resetAtEpoch ? resetAtEpoch * 1000 : undefined,
|
|
318
|
+
resetAfterSeconds,
|
|
319
|
+
creditBalance,
|
|
320
|
+
apiEndpoint: USAGE_API,
|
|
321
|
+
scrapedAt: new Date().toISOString(),
|
|
322
|
+
};
|
|
323
|
+
} finally {
|
|
324
|
+
await browser.close();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Quick scrape using direct API fetch (faster, but may fail without browser context)
|
|
330
|
+
*/
|
|
331
|
+
async scrapeDirect(): Promise<OpenAIQuotaData | null> {
|
|
332
|
+
const cookies = loadNetscapeCookies(this.config.cookieFile);
|
|
333
|
+
if (cookies.length === 0) return null;
|
|
334
|
+
|
|
335
|
+
const cookieHeader = cookies
|
|
336
|
+
.map((c) => `${c.name}=${encodeURIComponent(c.value)}`)
|
|
337
|
+
.join("; ");
|
|
338
|
+
|
|
339
|
+
try {
|
|
340
|
+
const response = await fetch(USAGE_API, {
|
|
341
|
+
headers: {
|
|
342
|
+
Cookie: cookieHeader,
|
|
343
|
+
Accept: "application/json",
|
|
344
|
+
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
|
|
345
|
+
Referer: "https://chatgpt.com/codex/cloud/settings/analytics",
|
|
346
|
+
},
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
if (!response.ok) {
|
|
350
|
+
if (!this.config.quiet) {
|
|
351
|
+
console.log(
|
|
352
|
+
`[OpenAIQuotaScraper] Direct API failed: ${response.status}`,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const data = (await response.json()) as OpenAIUsageResponse;
|
|
359
|
+
const primaryWindow = data?.rate_limit?.primary_window;
|
|
360
|
+
const weeklyUsedPct = primaryWindow?.used_percent ?? 0;
|
|
361
|
+
const resetAfterSeconds = primaryWindow?.reset_after_seconds ?? 0;
|
|
362
|
+
const resetAtEpoch = primaryWindow?.reset_at;
|
|
363
|
+
const weeklyResetsAt = formatRemainsSeconds(resetAfterSeconds);
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
provider: "openai",
|
|
367
|
+
weeklyUsedPct,
|
|
368
|
+
weeklyResetsAt,
|
|
369
|
+
weeklyResetsAtEpoch: resetAtEpoch ? resetAtEpoch * 1000 : undefined,
|
|
370
|
+
resetAfterSeconds,
|
|
371
|
+
apiEndpoint: USAGE_API,
|
|
372
|
+
scrapedAt: new Date().toISOString(),
|
|
373
|
+
};
|
|
374
|
+
} catch (error) {
|
|
375
|
+
if (!this.config.quiet) {
|
|
376
|
+
console.warn(
|
|
377
|
+
"[OpenAIQuotaScraper] Direct API error:",
|
|
378
|
+
error instanceof Error ? error.message : String(error),
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Integration helper for index.ts
|
|
388
|
+
*/
|
|
389
|
+
export class OpenAIQuotaManager {
|
|
390
|
+
private scraper: OpenAIQuotaScraper;
|
|
391
|
+
private lastQuota?: OpenAIQuotaData;
|
|
392
|
+
private lastFetchTime = 0;
|
|
393
|
+
private readonly cacheDurationMs: number;
|
|
394
|
+
|
|
395
|
+
constructor(config: OpenAIScraperConfig & { cacheDurationMs?: number } = {}) {
|
|
396
|
+
this.scraper = new OpenAIQuotaScraper(config);
|
|
397
|
+
this.cacheDurationMs = config.cacheDurationMs ?? 5 * 60 * 1000; // 5 min default
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Get current quota (uses cache)
|
|
402
|
+
*/
|
|
403
|
+
async getQuota(forceRefresh = false): Promise<OpenAIQuotaData> {
|
|
404
|
+
const now = Date.now();
|
|
405
|
+
|
|
406
|
+
if (
|
|
407
|
+
!forceRefresh &&
|
|
408
|
+
this.lastQuota &&
|
|
409
|
+
now - this.lastFetchTime < this.cacheDurationMs
|
|
410
|
+
) {
|
|
411
|
+
return this.lastQuota;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Try direct API first (faster)
|
|
415
|
+
const directResult = await this.scraper.scrapeDirect();
|
|
416
|
+
if (directResult) {
|
|
417
|
+
this.lastQuota = directResult;
|
|
418
|
+
this.lastFetchTime = now;
|
|
419
|
+
return this.lastQuota;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Fall back to browser scrape
|
|
423
|
+
try {
|
|
424
|
+
this.lastQuota = await this.scraper.scrape();
|
|
425
|
+
this.lastFetchTime = now;
|
|
426
|
+
return this.lastQuota;
|
|
427
|
+
} catch (error) {
|
|
428
|
+
// Return cached value if available
|
|
429
|
+
if (this.lastQuota) {
|
|
430
|
+
return this.lastQuota;
|
|
431
|
+
}
|
|
432
|
+
throw error;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Check if quota is available
|
|
438
|
+
*/
|
|
439
|
+
isAvailable(): boolean {
|
|
440
|
+
return this.scraper.hasCookieFile();
|
|
441
|
+
}
|
|
442
|
+
}
|
package/harness/loop-runtime.ts
CHANGED
|
@@ -244,7 +244,11 @@ export class LoopRuntime {
|
|
|
244
244
|
await new Promise((r) => setTimeout(r, 5 * 60_000)); // 5 min
|
|
245
245
|
// Re-check mirror — quota may have reset early
|
|
246
246
|
const fresh = await this.callbacks.onCheckMirror?.("minimax");
|
|
247
|
-
if (
|
|
247
|
+
if (
|
|
248
|
+
fresh &&
|
|
249
|
+
fresh.h5_used_pct !== undefined &&
|
|
250
|
+
fresh.h5_used_pct < 100
|
|
251
|
+
) {
|
|
248
252
|
break; // quota reset early, resume now
|
|
249
253
|
}
|
|
250
254
|
}
|
|
@@ -700,13 +704,13 @@ export class LoopRuntime {
|
|
|
700
704
|
if (epoch) {
|
|
701
705
|
const resumeAt = new Date(epoch).toISOString();
|
|
702
706
|
await this.jobState.setResumeTime(resumeAt);
|
|
703
|
-
console.log(
|
|
704
|
-
|
|
705
|
-
);
|
|
707
|
+
// console.log(
|
|
708
|
+
// `[LoopRuntime] Paused for 5h quota. Auto-resume at ${resumeAt}`,
|
|
709
|
+
// );
|
|
706
710
|
} else {
|
|
707
|
-
console.log(
|
|
708
|
-
|
|
709
|
-
);
|
|
711
|
+
// console.log(
|
|
712
|
+
// "[LoopRuntime] Paused for quota but no reset time known yet.",
|
|
713
|
+
// );
|
|
710
714
|
}
|
|
711
715
|
|
|
712
716
|
await this.callbacks.onQuotaEvent?.("paused");
|