oc-codex-multi-auth 6.0.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 +21 -0
- package/README.md +162 -0
- package/assets/opencode-logo-ornate-dark.svg +18 -0
- package/assets/readme-hero.svg +31 -0
- package/config/README.md +96 -0
- package/config/minimal-opencode.json +13 -0
- package/config/opencode-legacy.json +871 -0
- package/config/opencode-modern.json +358 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5008 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/accounts/rate-limits.d.ts +22 -0
- package/dist/lib/accounts/rate-limits.d.ts.map +1 -0
- package/dist/lib/accounts/rate-limits.js +63 -0
- package/dist/lib/accounts/rate-limits.js.map +1 -0
- package/dist/lib/accounts.d.ts +134 -0
- package/dist/lib/accounts.d.ts.map +1 -0
- package/dist/lib/accounts.js +795 -0
- package/dist/lib/accounts.js.map +1 -0
- package/dist/lib/audit.d.ts +45 -0
- package/dist/lib/audit.d.ts.map +1 -0
- package/dist/lib/audit.js +154 -0
- package/dist/lib/audit.js.map +1 -0
- package/dist/lib/auth/auth.d.ts +51 -0
- package/dist/lib/auth/auth.d.ts.map +1 -0
- package/dist/lib/auth/auth.js +191 -0
- package/dist/lib/auth/auth.js.map +1 -0
- package/dist/lib/auth/browser.d.ts +17 -0
- package/dist/lib/auth/browser.d.ts.map +1 -0
- package/dist/lib/auth/browser.js +90 -0
- package/dist/lib/auth/browser.js.map +1 -0
- package/dist/lib/auth/device-code.d.ts +28 -0
- package/dist/lib/auth/device-code.d.ts.map +1 -0
- package/dist/lib/auth/device-code.js +215 -0
- package/dist/lib/auth/device-code.js.map +1 -0
- package/dist/lib/auth/login-runner.d.ts +55 -0
- package/dist/lib/auth/login-runner.d.ts.map +1 -0
- package/dist/lib/auth/login-runner.js +631 -0
- package/dist/lib/auth/login-runner.js.map +1 -0
- package/dist/lib/auth/server.d.ts +10 -0
- package/dist/lib/auth/server.d.ts.map +1 -0
- package/dist/lib/auth/server.js +98 -0
- package/dist/lib/auth/server.js.map +1 -0
- package/dist/lib/auth/token-utils.d.ts +60 -0
- package/dist/lib/auth/token-utils.d.ts.map +1 -0
- package/dist/lib/auth/token-utils.js +416 -0
- package/dist/lib/auth/token-utils.js.map +1 -0
- package/dist/lib/auth-rate-limit.d.ts +20 -0
- package/dist/lib/auth-rate-limit.d.ts.map +1 -0
- package/dist/lib/auth-rate-limit.js +91 -0
- package/dist/lib/auth-rate-limit.js.map +1 -0
- package/dist/lib/auto-update-checker.d.ts +10 -0
- package/dist/lib/auto-update-checker.d.ts.map +1 -0
- package/dist/lib/auto-update-checker.js +129 -0
- package/dist/lib/auto-update-checker.js.map +1 -0
- package/dist/lib/circuit-breaker.d.ts +34 -0
- package/dist/lib/circuit-breaker.d.ts.map +1 -0
- package/dist/lib/circuit-breaker.js +124 -0
- package/dist/lib/circuit-breaker.js.map +1 -0
- package/dist/lib/cli.d.ts +46 -0
- package/dist/lib/cli.d.ts.map +1 -0
- package/dist/lib/cli.js +192 -0
- package/dist/lib/cli.js.map +1 -0
- package/dist/lib/config.d.ts +42 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +314 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/lib/constants.d.ts +90 -0
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/lib/constants.js +90 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/context-overflow.d.ts +27 -0
- package/dist/lib/context-overflow.d.ts.map +1 -0
- package/dist/lib/context-overflow.js +124 -0
- package/dist/lib/context-overflow.js.map +1 -0
- package/dist/lib/errors.d.ts +113 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +103 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/health.d.ts +33 -0
- package/dist/lib/health.d.ts.map +1 -0
- package/dist/lib/health.js +70 -0
- package/dist/lib/health.js.map +1 -0
- package/dist/lib/index.d.ts +20 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +20 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/logger.d.ts +40 -0
- package/dist/lib/logger.d.ts.map +1 -0
- package/dist/lib/logger.js +337 -0
- package/dist/lib/logger.js.map +1 -0
- package/dist/lib/oauth-success.html +712 -0
- package/dist/lib/parallel-probe.d.ts +28 -0
- package/dist/lib/parallel-probe.d.ts.map +1 -0
- package/dist/lib/parallel-probe.js +97 -0
- package/dist/lib/parallel-probe.js.map +1 -0
- package/dist/lib/proactive-refresh.d.ts +67 -0
- package/dist/lib/proactive-refresh.d.ts.map +1 -0
- package/dist/lib/proactive-refresh.js +161 -0
- package/dist/lib/proactive-refresh.js.map +1 -0
- package/dist/lib/prompts/codex-opencode-bridge.d.ts +20 -0
- package/dist/lib/prompts/codex-opencode-bridge.d.ts.map +1 -0
- package/dist/lib/prompts/codex-opencode-bridge.js +126 -0
- package/dist/lib/prompts/codex-opencode-bridge.js.map +1 -0
- package/dist/lib/prompts/codex.d.ts +41 -0
- package/dist/lib/prompts/codex.d.ts.map +1 -0
- package/dist/lib/prompts/codex.js +400 -0
- package/dist/lib/prompts/codex.js.map +1 -0
- package/dist/lib/prompts/opencode-codex.d.ts +25 -0
- package/dist/lib/prompts/opencode-codex.d.ts.map +1 -0
- package/dist/lib/prompts/opencode-codex.js +223 -0
- package/dist/lib/prompts/opencode-codex.js.map +1 -0
- package/dist/lib/recovery/constants.d.ts +12 -0
- package/dist/lib/recovery/constants.d.ts.map +1 -0
- package/dist/lib/recovery/constants.js +25 -0
- package/dist/lib/recovery/constants.js.map +1 -0
- package/dist/lib/recovery/index.d.ts +12 -0
- package/dist/lib/recovery/index.d.ts.map +1 -0
- package/dist/lib/recovery/index.js +12 -0
- package/dist/lib/recovery/index.js.map +1 -0
- package/dist/lib/recovery/storage.d.ts +24 -0
- package/dist/lib/recovery/storage.d.ts.map +1 -0
- package/dist/lib/recovery/storage.js +362 -0
- package/dist/lib/recovery/storage.js.map +1 -0
- package/dist/lib/recovery/types.d.ts +116 -0
- package/dist/lib/recovery/types.d.ts.map +1 -0
- package/dist/lib/recovery/types.js +7 -0
- package/dist/lib/recovery/types.js.map +1 -0
- package/dist/lib/recovery.d.ts +31 -0
- package/dist/lib/recovery.d.ts.map +1 -0
- package/dist/lib/recovery.js +313 -0
- package/dist/lib/recovery.js.map +1 -0
- package/dist/lib/refresh-queue.d.ts +131 -0
- package/dist/lib/refresh-queue.d.ts.map +1 -0
- package/dist/lib/refresh-queue.js +297 -0
- package/dist/lib/refresh-queue.js.map +1 -0
- package/dist/lib/request/fetch-helpers.d.ts +142 -0
- package/dist/lib/request/fetch-helpers.d.ts.map +1 -0
- package/dist/lib/request/fetch-helpers.js +749 -0
- package/dist/lib/request/fetch-helpers.js.map +1 -0
- package/dist/lib/request/helpers/input-utils.d.ts +7 -0
- package/dist/lib/request/helpers/input-utils.d.ts.map +1 -0
- package/dist/lib/request/helpers/input-utils.js +214 -0
- package/dist/lib/request/helpers/input-utils.js.map +1 -0
- package/dist/lib/request/helpers/model-map.d.ts +22 -0
- package/dist/lib/request/helpers/model-map.d.ts.map +1 -0
- package/dist/lib/request/helpers/model-map.js +183 -0
- package/dist/lib/request/helpers/model-map.js.map +1 -0
- package/dist/lib/request/helpers/tool-utils.d.ts +29 -0
- package/dist/lib/request/helpers/tool-utils.d.ts.map +1 -0
- package/dist/lib/request/helpers/tool-utils.js +117 -0
- package/dist/lib/request/helpers/tool-utils.js.map +1 -0
- package/dist/lib/request/rate-limit-backoff.d.ts +17 -0
- package/dist/lib/request/rate-limit-backoff.d.ts.map +1 -0
- package/dist/lib/request/rate-limit-backoff.js +83 -0
- package/dist/lib/request/rate-limit-backoff.js.map +1 -0
- package/dist/lib/request/request-transformer.d.ts +107 -0
- package/dist/lib/request/request-transformer.d.ts.map +1 -0
- package/dist/lib/request/request-transformer.js +930 -0
- package/dist/lib/request/request-transformer.js.map +1 -0
- package/dist/lib/request/response-handler.d.ts +23 -0
- package/dist/lib/request/response-handler.d.ts.map +1 -0
- package/dist/lib/request/response-handler.js +245 -0
- package/dist/lib/request/response-handler.js.map +1 -0
- package/dist/lib/request/retry-budget.d.ts +19 -0
- package/dist/lib/request/retry-budget.d.ts.map +1 -0
- package/dist/lib/request/retry-budget.js +99 -0
- package/dist/lib/request/retry-budget.js.map +1 -0
- package/dist/lib/rotation.d.ts +130 -0
- package/dist/lib/rotation.d.ts.map +1 -0
- package/dist/lib/rotation.js +301 -0
- package/dist/lib/rotation.js.map +1 -0
- package/dist/lib/runtime-contracts.d.ts +16 -0
- package/dist/lib/runtime-contracts.d.ts.map +1 -0
- package/dist/lib/runtime-contracts.js +24 -0
- package/dist/lib/runtime-contracts.js.map +1 -0
- package/dist/lib/schemas.d.ts +457 -0
- package/dist/lib/schemas.d.ts.map +1 -0
- package/dist/lib/schemas.js +283 -0
- package/dist/lib/schemas.js.map +1 -0
- package/dist/lib/shutdown.d.ts +7 -0
- package/dist/lib/shutdown.d.ts.map +1 -0
- package/dist/lib/shutdown.js +43 -0
- package/dist/lib/shutdown.js.map +1 -0
- package/dist/lib/storage/migrations.d.ts +65 -0
- package/dist/lib/storage/migrations.d.ts.map +1 -0
- package/dist/lib/storage/migrations.js +44 -0
- package/dist/lib/storage/migrations.js.map +1 -0
- package/dist/lib/storage/paths.d.ts +16 -0
- package/dist/lib/storage/paths.d.ts.map +1 -0
- package/dist/lib/storage/paths.js +92 -0
- package/dist/lib/storage/paths.js.map +1 -0
- package/dist/lib/storage.d.ts +157 -0
- package/dist/lib/storage.d.ts.map +1 -0
- package/dist/lib/storage.js +1169 -0
- package/dist/lib/storage.js.map +1 -0
- package/dist/lib/table-formatter.d.ts +32 -0
- package/dist/lib/table-formatter.d.ts.map +1 -0
- package/dist/lib/table-formatter.js +44 -0
- package/dist/lib/table-formatter.js.map +1 -0
- package/dist/lib/types.d.ts +131 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +2 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/ui/ansi.d.ts +22 -0
- package/dist/lib/ui/ansi.d.ts.map +1 -0
- package/dist/lib/ui/ansi.js +38 -0
- package/dist/lib/ui/ansi.js.map +1 -0
- package/dist/lib/ui/auth-menu.d.ts +39 -0
- package/dist/lib/ui/auth-menu.d.ts.map +1 -0
- package/dist/lib/ui/auth-menu.js +186 -0
- package/dist/lib/ui/auth-menu.js.map +1 -0
- package/dist/lib/ui/beginner.d.ts +62 -0
- package/dist/lib/ui/beginner.d.ts.map +1 -0
- package/dist/lib/ui/beginner.js +259 -0
- package/dist/lib/ui/beginner.js.map +1 -0
- package/dist/lib/ui/confirm.d.ts +2 -0
- package/dist/lib/ui/confirm.d.ts.map +1 -0
- package/dist/lib/ui/confirm.js +21 -0
- package/dist/lib/ui/confirm.js.map +1 -0
- package/dist/lib/ui/format.d.ts +9 -0
- package/dist/lib/ui/format.d.ts.map +1 -0
- package/dist/lib/ui/format.js +49 -0
- package/dist/lib/ui/format.js.map +1 -0
- package/dist/lib/ui/runtime.d.ts +11 -0
- package/dist/lib/ui/runtime.d.ts.map +1 -0
- package/dist/lib/ui/runtime.js +31 -0
- package/dist/lib/ui/runtime.js.map +1 -0
- package/dist/lib/ui/select.d.ts +20 -0
- package/dist/lib/ui/select.d.ts.map +1 -0
- package/dist/lib/ui/select.js +343 -0
- package/dist/lib/ui/select.js.map +1 -0
- package/dist/lib/ui/theme.d.ts +34 -0
- package/dist/lib/ui/theme.d.ts.map +1 -0
- package/dist/lib/ui/theme.js +84 -0
- package/dist/lib/ui/theme.js.map +1 -0
- package/dist/lib/utils.d.ts +29 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +54 -0
- package/dist/lib/utils.js.map +1 -0
- package/package.json +113 -0
- package/scripts/audit-dev-allowlist.js +114 -0
- package/scripts/copy-oauth-success.js +37 -0
- package/scripts/install-oc-codex-multi-auth-core.js +381 -0
- package/scripts/install-oc-codex-multi-auth.js +15 -0
- package/scripts/test-all-models.sh +260 -0
- package/scripts/validate-model-map.sh +97 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refresh Queue Module
|
|
3
|
+
*
|
|
4
|
+
* Prevents race conditions when multiple concurrent requests try to refresh
|
|
5
|
+
* the same account's token simultaneously. Instead of firing parallel refresh
|
|
6
|
+
* requests, subsequent callers await the existing in-flight refresh.
|
|
7
|
+
*
|
|
8
|
+
* Ported from antigravity-auth refresh-queue.ts pattern.
|
|
9
|
+
*/
|
|
10
|
+
import { refreshAccessToken } from "./auth/auth.js";
|
|
11
|
+
import { createLogger } from "./logger.js";
|
|
12
|
+
const log = createLogger("refresh-queue");
|
|
13
|
+
function createInitialMetrics() {
|
|
14
|
+
return {
|
|
15
|
+
started: 0,
|
|
16
|
+
deduplicated: 0,
|
|
17
|
+
rotationReused: 0,
|
|
18
|
+
succeeded: 0,
|
|
19
|
+
failed: 0,
|
|
20
|
+
exceptions: 0,
|
|
21
|
+
rotated: 0,
|
|
22
|
+
staleEvictions: 0,
|
|
23
|
+
lastDurationMs: 0,
|
|
24
|
+
lastFailureReason: null,
|
|
25
|
+
pending: 0,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Manages queued token refresh operations to prevent race conditions.
|
|
30
|
+
*
|
|
31
|
+
* When multiple concurrent requests need to refresh the same account's token,
|
|
32
|
+
* only the first request triggers the actual refresh. Subsequent requests
|
|
33
|
+
* await the same promise, ensuring:
|
|
34
|
+
* - No duplicate refresh API calls for the same refresh token
|
|
35
|
+
* - Consistent token state across all waiting callers
|
|
36
|
+
* - Reduced load on OpenAI's token endpoint
|
|
37
|
+
*
|
|
38
|
+
* Token Rotation Handling:
|
|
39
|
+
* When OpenAI rotates the refresh token during a refresh operation, we maintain
|
|
40
|
+
* a mapping from old token → new token. This ensures that requests arriving with
|
|
41
|
+
* either the old or new token will find the in-flight refresh and not trigger
|
|
42
|
+
* duplicate refreshes.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const queue = new RefreshQueue();
|
|
47
|
+
*
|
|
48
|
+
* // These three concurrent calls will only trigger ONE actual refresh
|
|
49
|
+
* const [result1, result2, result3] = await Promise.all([
|
|
50
|
+
* queue.refresh(refreshToken),
|
|
51
|
+
* queue.refresh(refreshToken),
|
|
52
|
+
* queue.refresh(refreshToken),
|
|
53
|
+
* ]);
|
|
54
|
+
*
|
|
55
|
+
* // All three get the same result
|
|
56
|
+
* console.log(result1 === result2); // true (same object reference)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export class RefreshQueue {
|
|
60
|
+
pending = new Map();
|
|
61
|
+
metrics = createInitialMetrics();
|
|
62
|
+
/**
|
|
63
|
+
* Maps old refresh tokens to new tokens after rotation.
|
|
64
|
+
* This allows lookups with either old or new token to find the same entry.
|
|
65
|
+
* Format: oldToken → newToken
|
|
66
|
+
*/
|
|
67
|
+
tokenRotationMap = new Map();
|
|
68
|
+
/**
|
|
69
|
+
* Maximum time to keep a refresh entry in the queue (prevents memory leaks
|
|
70
|
+
* from stuck requests). After this timeout, the entry is removed and new
|
|
71
|
+
* callers will trigger a fresh refresh.
|
|
72
|
+
*/
|
|
73
|
+
maxEntryAgeMs;
|
|
74
|
+
/**
|
|
75
|
+
* Create a new RefreshQueue instance.
|
|
76
|
+
* @param maxEntryAgeMs - Maximum age for pending entries before cleanup (default: 30s)
|
|
77
|
+
*/
|
|
78
|
+
constructor(maxEntryAgeMs = 30_000) {
|
|
79
|
+
this.maxEntryAgeMs = maxEntryAgeMs;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Refresh a token, deduplicating concurrent requests for the same refresh token.
|
|
83
|
+
*
|
|
84
|
+
* If a refresh is already in-flight for this token, returns the existing promise.
|
|
85
|
+
* Otherwise, initiates a new refresh and caches the promise for other callers.
|
|
86
|
+
*
|
|
87
|
+
* @param refreshToken - The refresh token to use
|
|
88
|
+
* @returns Token result (success with new tokens, or failure)
|
|
89
|
+
*/
|
|
90
|
+
async refresh(refreshToken) {
|
|
91
|
+
this.cleanup();
|
|
92
|
+
// Check for existing in-flight refresh (direct match)
|
|
93
|
+
const existing = this.pending.get(refreshToken);
|
|
94
|
+
if (existing) {
|
|
95
|
+
this.metrics.deduplicated += 1;
|
|
96
|
+
this.metrics.pending = this.pending.size;
|
|
97
|
+
log.info("Reusing in-flight refresh for token", {
|
|
98
|
+
tokenSuffix: refreshToken.slice(-6),
|
|
99
|
+
waitingMs: Date.now() - existing.startedAt,
|
|
100
|
+
});
|
|
101
|
+
return existing.promise;
|
|
102
|
+
}
|
|
103
|
+
// Check if this token was rotated FROM another token that's still refreshing
|
|
104
|
+
// This handles: Request A starts with oldToken, gets newToken, Request B arrives with newToken
|
|
105
|
+
const rotatedFrom = this.findOriginalToken(refreshToken);
|
|
106
|
+
if (rotatedFrom) {
|
|
107
|
+
const originalEntry = this.pending.get(rotatedFrom);
|
|
108
|
+
if (originalEntry) {
|
|
109
|
+
this.metrics.rotationReused += 1;
|
|
110
|
+
this.metrics.pending = this.pending.size;
|
|
111
|
+
log.info("Reusing in-flight refresh via rotation mapping", {
|
|
112
|
+
newTokenSuffix: refreshToken.slice(-6),
|
|
113
|
+
originalTokenSuffix: rotatedFrom.slice(-6),
|
|
114
|
+
waitingMs: Date.now() - originalEntry.startedAt,
|
|
115
|
+
});
|
|
116
|
+
return originalEntry.promise;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// Start a new refresh
|
|
120
|
+
const startedAt = Date.now();
|
|
121
|
+
this.metrics.started += 1;
|
|
122
|
+
const promise = this.executeRefreshWithRotationTracking(refreshToken);
|
|
123
|
+
this.pending.set(refreshToken, { promise, startedAt });
|
|
124
|
+
this.metrics.pending = this.pending.size;
|
|
125
|
+
try {
|
|
126
|
+
return await promise;
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
this.pending.delete(refreshToken);
|
|
130
|
+
this.cleanupRotationMapping(refreshToken);
|
|
131
|
+
this.metrics.pending = this.pending.size;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
findOriginalToken(newToken) {
|
|
135
|
+
for (const [oldToken, mappedNewToken] of this.tokenRotationMap.entries()) {
|
|
136
|
+
if (mappedNewToken === newToken) {
|
|
137
|
+
return oldToken;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
cleanupRotationMapping(token) {
|
|
143
|
+
this.tokenRotationMap.delete(token);
|
|
144
|
+
for (const [oldToken, newToken] of this.tokenRotationMap.entries()) {
|
|
145
|
+
if (newToken === token) {
|
|
146
|
+
this.tokenRotationMap.delete(oldToken);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async executeRefreshWithRotationTracking(refreshToken) {
|
|
151
|
+
const result = await this.executeRefresh(refreshToken);
|
|
152
|
+
if (result.type === "success" && result.refresh !== refreshToken) {
|
|
153
|
+
this.tokenRotationMap.set(refreshToken, result.refresh);
|
|
154
|
+
this.metrics.rotated += 1;
|
|
155
|
+
log.info("Token rotated during refresh", {
|
|
156
|
+
oldTokenSuffix: refreshToken.slice(-6),
|
|
157
|
+
newTokenSuffix: result.refresh.slice(-6),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Execute the actual refresh and log results.
|
|
164
|
+
*/
|
|
165
|
+
async executeRefresh(refreshToken) {
|
|
166
|
+
const startTime = Date.now();
|
|
167
|
+
log.info("Starting token refresh", { tokenSuffix: refreshToken.slice(-6) });
|
|
168
|
+
try {
|
|
169
|
+
const result = await refreshAccessToken(refreshToken);
|
|
170
|
+
const duration = Date.now() - startTime;
|
|
171
|
+
this.metrics.lastDurationMs = duration;
|
|
172
|
+
if (result.type === "success") {
|
|
173
|
+
this.metrics.succeeded += 1;
|
|
174
|
+
this.metrics.lastFailureReason = null;
|
|
175
|
+
log.info("Token refresh succeeded", {
|
|
176
|
+
tokenSuffix: refreshToken.slice(-6),
|
|
177
|
+
durationMs: duration,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
this.metrics.failed += 1;
|
|
182
|
+
this.metrics.lastFailureReason = result.reason ?? "unknown";
|
|
183
|
+
log.warn("Token refresh failed", {
|
|
184
|
+
tokenSuffix: refreshToken.slice(-6),
|
|
185
|
+
reason: result.reason,
|
|
186
|
+
durationMs: duration,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
const duration = Date.now() - startTime;
|
|
193
|
+
this.metrics.failed += 1;
|
|
194
|
+
this.metrics.exceptions += 1;
|
|
195
|
+
this.metrics.lastDurationMs = duration;
|
|
196
|
+
this.metrics.lastFailureReason =
|
|
197
|
+
error instanceof Error ? error.message : "unknown_exception";
|
|
198
|
+
log.error("Token refresh threw exception", {
|
|
199
|
+
tokenSuffix: refreshToken.slice(-6),
|
|
200
|
+
error: error?.message ?? String(error),
|
|
201
|
+
durationMs: duration,
|
|
202
|
+
});
|
|
203
|
+
return {
|
|
204
|
+
type: "failed",
|
|
205
|
+
reason: "network_error",
|
|
206
|
+
message: error?.message ?? "Unknown error during refresh",
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Remove stale entries that have been pending too long.
|
|
212
|
+
* This prevents memory leaks from stuck or abandoned refresh operations.
|
|
213
|
+
*/
|
|
214
|
+
cleanup() {
|
|
215
|
+
const now = Date.now();
|
|
216
|
+
const staleTokens = [];
|
|
217
|
+
for (const [token, entry] of this.pending.entries()) {
|
|
218
|
+
if (now - entry.startedAt > this.maxEntryAgeMs) {
|
|
219
|
+
staleTokens.push(token);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
for (const token of staleTokens) {
|
|
223
|
+
// istanbul ignore next -- defensive: token always exists in pending at this point (not yet deleted)
|
|
224
|
+
const ageMs = now - (this.pending.get(token)?.startedAt ?? now);
|
|
225
|
+
this.metrics.staleEvictions += 1;
|
|
226
|
+
log.warn("Removing stale refresh entry", {
|
|
227
|
+
tokenSuffix: token.slice(-6),
|
|
228
|
+
ageMs,
|
|
229
|
+
});
|
|
230
|
+
this.pending.delete(token);
|
|
231
|
+
}
|
|
232
|
+
this.metrics.pending = this.pending.size;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Check if there's an in-flight refresh for a given token.
|
|
236
|
+
* @param refreshToken - The refresh token to check
|
|
237
|
+
* @returns True if refresh is in progress
|
|
238
|
+
*/
|
|
239
|
+
isRefreshing(refreshToken) {
|
|
240
|
+
return this.pending.has(refreshToken);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Get the number of pending refresh operations.
|
|
244
|
+
* Useful for debugging and monitoring.
|
|
245
|
+
*/
|
|
246
|
+
get pendingCount() {
|
|
247
|
+
return this.pending.size;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Clear all pending entries (primarily for testing).
|
|
251
|
+
*/
|
|
252
|
+
clear() {
|
|
253
|
+
this.pending.clear();
|
|
254
|
+
this.tokenRotationMap.clear();
|
|
255
|
+
this.metrics = createInitialMetrics();
|
|
256
|
+
}
|
|
257
|
+
getMetricsSnapshot() {
|
|
258
|
+
return {
|
|
259
|
+
...this.metrics,
|
|
260
|
+
pending: this.pending.size,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// ============================================================================
|
|
265
|
+
// Singleton Instance
|
|
266
|
+
// ============================================================================
|
|
267
|
+
let refreshQueueInstance = null;
|
|
268
|
+
/**
|
|
269
|
+
* Get the singleton RefreshQueue instance.
|
|
270
|
+
* @param maxEntryAgeMs - Maximum age for pending entries (only used on first call)
|
|
271
|
+
* @returns The global RefreshQueue instance
|
|
272
|
+
*/
|
|
273
|
+
export function getRefreshQueue(maxEntryAgeMs) {
|
|
274
|
+
if (!refreshQueueInstance) {
|
|
275
|
+
refreshQueueInstance = new RefreshQueue(maxEntryAgeMs);
|
|
276
|
+
}
|
|
277
|
+
return refreshQueueInstance;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Reset the singleton instance (primarily for testing).
|
|
281
|
+
*/
|
|
282
|
+
export function resetRefreshQueue() {
|
|
283
|
+
refreshQueueInstance?.clear();
|
|
284
|
+
refreshQueueInstance = null;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Convenience function to refresh a token using the singleton queue.
|
|
288
|
+
* @param refreshToken - The refresh token to use
|
|
289
|
+
* @returns Token result
|
|
290
|
+
*/
|
|
291
|
+
export async function queuedRefresh(refreshToken) {
|
|
292
|
+
return getRefreshQueue().refresh(refreshToken);
|
|
293
|
+
}
|
|
294
|
+
export function getRefreshQueueMetrics() {
|
|
295
|
+
return getRefreshQueue().getMetricsSnapshot();
|
|
296
|
+
}
|
|
297
|
+
//# sourceMappingURL=refresh-queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refresh-queue.js","sourceRoot":"","sources":["../../lib/refresh-queue.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,GAAG,GAAG,YAAY,CAAC,eAAe,CAAC,CAAC;AAwB1C,SAAS,oBAAoB;IAC5B,OAAO;QACN,OAAO,EAAE,CAAC;QACV,YAAY,EAAE,CAAC;QACf,cAAc,EAAE,CAAC;QACjB,SAAS,EAAE,CAAC;QACZ,MAAM,EAAE,CAAC;QACT,UAAU,EAAE,CAAC;QACb,OAAO,EAAE,CAAC;QACV,cAAc,EAAE,CAAC;QACjB,cAAc,EAAE,CAAC;QACjB,iBAAiB,EAAE,IAAI;QACvB,OAAO,EAAE,CAAC;KAC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AU,sBAAsB;IACpC,OAAO,eAAe,EAAE,CAAC,kBAAkB,EAAE,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions for the custom fetch implementation
|
|
3
|
+
* These functions break down the complex fetch logic into manageable, testable units
|
|
4
|
+
*/
|
|
5
|
+
import type { Auth, OpencodeClient } from "@opencode-ai/sdk";
|
|
6
|
+
import type { UserConfig, RequestBody } from "../types.js";
|
|
7
|
+
export interface RateLimitInfo {
|
|
8
|
+
retryAfterMs: number;
|
|
9
|
+
code?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface EntitlementError {
|
|
12
|
+
isEntitlement: true;
|
|
13
|
+
code: string;
|
|
14
|
+
message: string;
|
|
15
|
+
}
|
|
16
|
+
export declare const DEFAULT_UNSUPPORTED_CODEX_FALLBACK_CHAIN: Record<string, string[]>;
|
|
17
|
+
export interface UnsupportedCodexModelInfo {
|
|
18
|
+
isUnsupported: boolean;
|
|
19
|
+
code?: string;
|
|
20
|
+
message?: string;
|
|
21
|
+
unsupportedModel?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ResolveUnsupportedCodexFallbackOptions {
|
|
24
|
+
requestedModel: string | undefined;
|
|
25
|
+
errorBody: unknown;
|
|
26
|
+
attemptedModels?: Iterable<string>;
|
|
27
|
+
fallbackOnUnsupportedCodexModel: boolean;
|
|
28
|
+
fallbackToGpt52OnUnsupportedGpt53: boolean;
|
|
29
|
+
customChain?: Record<string, string[]>;
|
|
30
|
+
}
|
|
31
|
+
export declare function extractUnsupportedCodexModelFromText(bodyText: string): string | undefined;
|
|
32
|
+
export declare function getUnsupportedCodexModelInfo(errorBody: unknown): UnsupportedCodexModelInfo;
|
|
33
|
+
export declare function resolveUnsupportedCodexFallbackModel(options: ResolveUnsupportedCodexFallbackOptions): string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Returns true when the legacy `gpt-5.3-codex -> gpt-5.2-codex` edge is available.
|
|
36
|
+
*/
|
|
37
|
+
export declare function shouldFallbackToGpt52OnUnsupportedGpt53(requestedModel: string | undefined, errorBody: unknown): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Checks if an error code indicates an entitlement/subscription issue
|
|
40
|
+
* These errors should NOT be treated as rate limits because:
|
|
41
|
+
* 1. They won't resolve by waiting
|
|
42
|
+
* 2. They won't resolve by switching accounts (all accounts likely have same issue)
|
|
43
|
+
* 3. User needs to upgrade their subscription
|
|
44
|
+
*/
|
|
45
|
+
export declare function isEntitlementError(code: string, bodyText: string): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a user-friendly entitlement error response
|
|
48
|
+
*/
|
|
49
|
+
export declare function createEntitlementErrorResponse(_bodyText: string): Response;
|
|
50
|
+
export interface ErrorHandlingResult {
|
|
51
|
+
response: Response;
|
|
52
|
+
rateLimit?: RateLimitInfo;
|
|
53
|
+
errorBody?: unknown;
|
|
54
|
+
}
|
|
55
|
+
export interface ErrorHandlingOptions {
|
|
56
|
+
requestCorrelationId?: string;
|
|
57
|
+
threadId?: string;
|
|
58
|
+
}
|
|
59
|
+
export interface ErrorDiagnostics {
|
|
60
|
+
requestId?: string;
|
|
61
|
+
cfRay?: string;
|
|
62
|
+
correlationId?: string;
|
|
63
|
+
threadId?: string;
|
|
64
|
+
httpStatus?: number;
|
|
65
|
+
}
|
|
66
|
+
export declare function isDeactivatedWorkspaceError(errorBody: unknown, status?: number): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Determines if the current auth token needs to be refreshed
|
|
69
|
+
* @param auth - Current authentication state
|
|
70
|
+
* @returns True if token is expired or invalid
|
|
71
|
+
*/
|
|
72
|
+
export declare function shouldRefreshToken(auth: Auth, skewMs?: number): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Refreshes the OAuth token and updates stored credentials
|
|
75
|
+
* @param currentAuth - Current auth state
|
|
76
|
+
* @param client - Opencode client for updating stored credentials
|
|
77
|
+
* @returns Updated auth (throws on failure)
|
|
78
|
+
*/
|
|
79
|
+
export declare function refreshAndUpdateToken(currentAuth: Auth, client: OpencodeClient): Promise<Auth>;
|
|
80
|
+
/**
|
|
81
|
+
* Extracts URL string from various request input types
|
|
82
|
+
* @param input - Request input (string, URL, or Request object)
|
|
83
|
+
* @returns URL string
|
|
84
|
+
*/
|
|
85
|
+
export declare function extractRequestUrl(input: Request | string | URL): string;
|
|
86
|
+
/**
|
|
87
|
+
* Rewrites OpenAI API URLs to Codex backend URLs
|
|
88
|
+
* @param url - Original URL
|
|
89
|
+
* @returns Rewritten URL for Codex backend
|
|
90
|
+
*/
|
|
91
|
+
export declare function rewriteUrlForCodex(url: string): string;
|
|
92
|
+
/**
|
|
93
|
+
* Transforms request body and logs the transformation
|
|
94
|
+
* Fetches model-specific Codex instructions based on the request model
|
|
95
|
+
*
|
|
96
|
+
* @param init - Request init options
|
|
97
|
+
* @param url - Request URL
|
|
98
|
+
* @param userConfig - User configuration
|
|
99
|
+
* @param codexMode - Enable CODEX_MODE (bridge prompt instead of tool remap)
|
|
100
|
+
* @param parsedBody - Pre-parsed body to avoid double JSON.parse (optional)
|
|
101
|
+
* @returns Transformed body and updated init, or undefined if no body
|
|
102
|
+
*/
|
|
103
|
+
export declare function transformRequestForCodex(init: RequestInit | undefined, url: string, userConfig: UserConfig, codexMode?: boolean, parsedBody?: Record<string, unknown>, options?: {
|
|
104
|
+
requestTransformMode?: "native" | "legacy";
|
|
105
|
+
fastSession?: boolean;
|
|
106
|
+
fastSessionStrategy?: "hybrid" | "always";
|
|
107
|
+
fastSessionMaxInputItems?: number;
|
|
108
|
+
}): Promise<{
|
|
109
|
+
body: RequestBody;
|
|
110
|
+
updatedInit: RequestInit;
|
|
111
|
+
} | undefined>;
|
|
112
|
+
/**
|
|
113
|
+
* Creates headers for Codex API requests
|
|
114
|
+
* @param init - Request init options
|
|
115
|
+
* @param accountId - ChatGPT account ID
|
|
116
|
+
* @param accessToken - OAuth access token
|
|
117
|
+
* @param opts - Optional parameters including model, promptCacheKey, and organizationId
|
|
118
|
+
* @returns Headers object with all required Codex headers
|
|
119
|
+
*/
|
|
120
|
+
export declare function createCodexHeaders(init: RequestInit | undefined, accountId: string, accessToken: string, opts?: {
|
|
121
|
+
model?: string;
|
|
122
|
+
promptCacheKey?: string;
|
|
123
|
+
organizationId?: string;
|
|
124
|
+
}): Headers;
|
|
125
|
+
/**
|
|
126
|
+
* Handles error responses from the Codex API
|
|
127
|
+
* @param response - Error response from API
|
|
128
|
+
* @returns Original response or mapped retryable response
|
|
129
|
+
*/
|
|
130
|
+
export declare function handleErrorResponse(response: Response, options?: ErrorHandlingOptions): Promise<ErrorHandlingResult>;
|
|
131
|
+
/**
|
|
132
|
+
* Handles successful responses from the Codex API
|
|
133
|
+
* Converts SSE to JSON for non-streaming requests (generateText)
|
|
134
|
+
* Passes through SSE for streaming requests (streamText)
|
|
135
|
+
* @param response - Success response from API
|
|
136
|
+
* @param isStreaming - Whether this is a streaming request (stream=true in body)
|
|
137
|
+
* @returns Processed response (SSE→JSON for non-streaming, stream for streaming)
|
|
138
|
+
*/
|
|
139
|
+
export declare function handleSuccessResponse(response: Response, isStreaming: boolean, options?: {
|
|
140
|
+
streamStallTimeoutMs?: number;
|
|
141
|
+
}): Promise<Response>;
|
|
142
|
+
//# sourceMappingURL=fetch-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-helpers.d.ts","sourceRoot":"","sources":["../../../lib/request/fetch-helpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAM7D,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAc3D,MAAM,WAAW,aAAa;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,gBAAgB;IACzB,aAAa,EAAE,IAAI,CAAC;IACpB,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