claude-codex-proxy 1.0.5-beta.35 → 1.0.5-beta.37
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/package.json +1 -1
- package/public/views/settings.html +11 -1
- package/src/account-rotation/index.js +6 -0
- package/src/account-rotation/strategies/round-robin-strategy.js +78 -3
- package/src/model-pricing.js +23 -0
- package/src/routes/messages-route.js +37 -9
- package/src/routes/models-route.js +2 -0
package/package.json
CHANGED
|
@@ -110,7 +110,7 @@
|
|
|
110
110
|
<div class="flex flex-wrap items-center justify-between gap-4">
|
|
111
111
|
<div>
|
|
112
112
|
<div class="text-sm text-gray-300">Strategy</div>
|
|
113
|
-
<div class="text-xs text-gray-500 font-mono">How
|
|
113
|
+
<div class="text-xs text-gray-500 font-mono">How requests are distributed across available accounts</div>
|
|
114
114
|
</div>
|
|
115
115
|
<div class="inline-flex items-center gap-2">
|
|
116
116
|
<button class="btn btn-xs" :class="accountStrategy === 'sticky' ? 'bg-neon-purple text-white' : 'btn-outline text-gray-400'"
|
|
@@ -119,5 +119,15 @@
|
|
|
119
119
|
@click="setAccountStrategy('round-robin')" :disabled="strategySaving">Round-Robin</button>
|
|
120
120
|
</div>
|
|
121
121
|
</div>
|
|
122
|
+
<div class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-4">
|
|
123
|
+
<div class="rounded-lg border p-3" :class="accountStrategy === 'sticky' ? 'border-neon-purple/50 bg-neon-purple/5' : 'border-space-border bg-space-800/40'">
|
|
124
|
+
<div class="text-xs font-mono text-gray-300 mb-1">Sticky · Cache-Optimized</div>
|
|
125
|
+
<div class="text-xs text-gray-500">Keeps using the active account. If it becomes unavailable, switches to another usable account.</div>
|
|
126
|
+
</div>
|
|
127
|
+
<div class="rounded-lg border p-3" :class="accountStrategy === 'round-robin' ? 'border-neon-purple/50 bg-neon-purple/5' : 'border-space-border bg-space-800/40'">
|
|
128
|
+
<div class="text-xs font-mono text-gray-300 mb-1">Round-Robin · Session-Balanced</div>
|
|
129
|
+
<div class="text-xs text-gray-500">Distributes new sessions across accounts, then keeps each session on the same account for cache reuse.</div>
|
|
130
|
+
</div>
|
|
131
|
+
</div>
|
|
122
132
|
</div>
|
|
123
133
|
</div>
|
|
@@ -75,6 +75,12 @@ export class AccountRotator {
|
|
|
75
75
|
return getMinWaitTimeMs(accounts, modelId);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
commitSessionBinding(sessionId, email) {
|
|
79
|
+
if (this.strategy.commitSessionBinding) {
|
|
80
|
+
this.strategy.commitSessionBinding(sessionId, email);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
78
84
|
notifySuccess(account, modelId) {
|
|
79
85
|
if (this.strategy.notifySuccess) {
|
|
80
86
|
this.strategy.notifySuccess(account, modelId);
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { BaseStrategy } from './base-strategy.js';
|
|
2
2
|
import { logger } from '../../utils/logger.js';
|
|
3
3
|
|
|
4
|
+
const DEFAULT_SESSION_BINDING_TTL_MS = 24 * 60 * 60 * 1000;
|
|
5
|
+
const DEFAULT_SESSION_CLEANUP_INTERVAL_MS = 60 * 1000;
|
|
6
|
+
const DEFAULT_MAX_SESSION_BINDINGS = 10000;
|
|
7
|
+
|
|
4
8
|
export class RoundRobinStrategy extends BaseStrategy {
|
|
5
|
-
constructor(config) {
|
|
9
|
+
constructor(config = {}) {
|
|
6
10
|
super(config, 'round-robin');
|
|
7
11
|
this.cursor = 0;
|
|
12
|
+
this.sessionBindings = new Map();
|
|
13
|
+
this.sessionBindingTtlMs = config.sessionBindingTtlMs ?? DEFAULT_SESSION_BINDING_TTL_MS;
|
|
14
|
+
this.sessionCleanupIntervalMs = config.sessionCleanupIntervalMs ?? DEFAULT_SESSION_CLEANUP_INTERVAL_MS;
|
|
15
|
+
this.maxSessionBindings = config.maxSessionBindings ?? DEFAULT_MAX_SESSION_BINDINGS;
|
|
16
|
+
this.lastSessionCleanupAt = 0;
|
|
17
|
+
this.now = config.now ?? Date.now;
|
|
8
18
|
}
|
|
9
19
|
|
|
10
20
|
selectAccount(accounts, modelId, options = {}) {
|
|
@@ -12,6 +22,32 @@ export class RoundRobinStrategy extends BaseStrategy {
|
|
|
12
22
|
return { account: null, index: 0, waitMs: 0 };
|
|
13
23
|
}
|
|
14
24
|
|
|
25
|
+
const sessionId = this.#normalizeSessionId(options.sessionId);
|
|
26
|
+
const excludedEmails = new Set(options.excludeEmails || []);
|
|
27
|
+
const now = this.now();
|
|
28
|
+
|
|
29
|
+
if (sessionId) {
|
|
30
|
+
this.#cleanupSessionBindingsIfDue(now);
|
|
31
|
+
const binding = this.sessionBindings.get(sessionId);
|
|
32
|
+
if (binding && now - binding.lastUsedAt >= this.sessionBindingTtlMs) {
|
|
33
|
+
this.sessionBindings.delete(sessionId);
|
|
34
|
+
}
|
|
35
|
+
const currentBinding = this.sessionBindings.get(sessionId);
|
|
36
|
+
const boundIndex = currentBinding
|
|
37
|
+
? accounts.findIndex(account => account.email === currentBinding.email)
|
|
38
|
+
: -1;
|
|
39
|
+
const boundAccount = boundIndex >= 0 ? accounts[boundIndex] : null;
|
|
40
|
+
|
|
41
|
+
if (boundAccount
|
|
42
|
+
&& !excludedEmails.has(boundAccount.email)
|
|
43
|
+
&& this.isAccountUsable(boundAccount, modelId)) {
|
|
44
|
+
boundAccount.lastUsed = now;
|
|
45
|
+
this.#storeSessionBinding(sessionId, boundAccount.email, now);
|
|
46
|
+
logger.debug(`RoundRobinStrategy: Reusing session account at index ${boundIndex}`);
|
|
47
|
+
return { account: boundAccount, index: boundIndex, waitMs: 0 };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
15
51
|
// Clamp cursor
|
|
16
52
|
if (this.cursor >= accounts.length) {
|
|
17
53
|
this.cursor = 0;
|
|
@@ -24,9 +60,12 @@ export class RoundRobinStrategy extends BaseStrategy {
|
|
|
24
60
|
const checkIndex = (startIndex + i) % accounts.length;
|
|
25
61
|
const account = accounts[checkIndex];
|
|
26
62
|
|
|
27
|
-
if (this.isAccountUsable(account, modelId)) {
|
|
28
|
-
account.lastUsed =
|
|
63
|
+
if (!excludedEmails.has(account.email) && this.isAccountUsable(account, modelId)) {
|
|
64
|
+
account.lastUsed = now;
|
|
29
65
|
this.cursor = checkIndex;
|
|
66
|
+
if (sessionId && !options.deferSessionBinding) {
|
|
67
|
+
this.#storeSessionBinding(sessionId, account.email, now);
|
|
68
|
+
}
|
|
30
69
|
logger.debug(`RoundRobinStrategy: Using account at index ${checkIndex}`);
|
|
31
70
|
return { account, index: checkIndex, waitMs: 0 };
|
|
32
71
|
}
|
|
@@ -36,7 +75,43 @@ export class RoundRobinStrategy extends BaseStrategy {
|
|
|
36
75
|
return { account: null, index: this.cursor, waitMs: 0 };
|
|
37
76
|
}
|
|
38
77
|
|
|
78
|
+
commitSessionBinding(sessionId, email) {
|
|
79
|
+
const normalizedSessionId = this.#normalizeSessionId(sessionId);
|
|
80
|
+
if (!normalizedSessionId || !email) return;
|
|
81
|
+
|
|
82
|
+
const now = this.now();
|
|
83
|
+
this.#cleanupSessionBindingsIfDue(now);
|
|
84
|
+
this.#storeSessionBinding(normalizedSessionId, email, now);
|
|
85
|
+
}
|
|
86
|
+
|
|
39
87
|
resetCursor() {
|
|
40
88
|
this.cursor = 0;
|
|
41
89
|
}
|
|
90
|
+
|
|
91
|
+
#normalizeSessionId(sessionId) {
|
|
92
|
+
if (typeof sessionId !== 'string' && typeof sessionId !== 'number') return null;
|
|
93
|
+
const normalized = String(sessionId).trim();
|
|
94
|
+
return normalized || null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#storeSessionBinding(sessionId, email, now) {
|
|
98
|
+
this.sessionBindings.delete(sessionId);
|
|
99
|
+
this.sessionBindings.set(sessionId, { email, lastUsedAt: now });
|
|
100
|
+
|
|
101
|
+
while (this.sessionBindings.size > this.maxSessionBindings) {
|
|
102
|
+
const oldestSessionId = this.sessionBindings.keys().next().value;
|
|
103
|
+
this.sessionBindings.delete(oldestSessionId);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
#cleanupSessionBindingsIfDue(now) {
|
|
108
|
+
if (now - this.lastSessionCleanupAt < this.sessionCleanupIntervalMs) return;
|
|
109
|
+
|
|
110
|
+
for (const [sessionId, binding] of this.sessionBindings) {
|
|
111
|
+
if (now - binding.lastUsedAt >= this.sessionBindingTtlMs) {
|
|
112
|
+
this.sessionBindings.delete(sessionId);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
this.lastSessionCleanupAt = now;
|
|
116
|
+
}
|
|
42
117
|
}
|
package/src/model-pricing.js
CHANGED
|
@@ -9,6 +9,12 @@ export const LONG_CONTEXT_THRESHOLD_TOKENS = 272_000;
|
|
|
9
9
|
export const DEFAULT_PRICING_MODEL = 'gpt-5.6-sol';
|
|
10
10
|
|
|
11
11
|
const STANDARD_PRICING = {
|
|
12
|
+
'gpt-6-astra': {
|
|
13
|
+
input: 10,
|
|
14
|
+
cachedInput: 1,
|
|
15
|
+
cacheWrite: 12.5,
|
|
16
|
+
output: 50
|
|
17
|
+
},
|
|
12
18
|
'gpt-5.6-sol': {
|
|
13
19
|
input: 5,
|
|
14
20
|
cachedInput: 0.5,
|
|
@@ -21,6 +27,12 @@ const STANDARD_PRICING = {
|
|
|
21
27
|
cacheWrite: 2.5,
|
|
22
28
|
output: 12
|
|
23
29
|
},
|
|
30
|
+
'gpt-5.6-luna': {
|
|
31
|
+
input: 0.2,
|
|
32
|
+
cachedInput: 0.02,
|
|
33
|
+
cacheWrite: 0.25,
|
|
34
|
+
output: 1.2
|
|
35
|
+
},
|
|
24
36
|
'gpt-5.4-mini': {
|
|
25
37
|
input: 0.75,
|
|
26
38
|
cachedInput: 0.075,
|
|
@@ -30,6 +42,12 @@ const STANDARD_PRICING = {
|
|
|
30
42
|
};
|
|
31
43
|
|
|
32
44
|
const LONG_CONTEXT_PRICING = {
|
|
45
|
+
'gpt-6-astra': {
|
|
46
|
+
input: 20,
|
|
47
|
+
cachedInput: 2,
|
|
48
|
+
cacheWrite: 25,
|
|
49
|
+
output: 75
|
|
50
|
+
},
|
|
33
51
|
'gpt-5.6-sol': {
|
|
34
52
|
input: 10,
|
|
35
53
|
cachedInput: 1,
|
|
@@ -41,6 +59,11 @@ const LONG_CONTEXT_PRICING = {
|
|
|
41
59
|
cachedInput: 0.4,
|
|
42
60
|
cacheWrite: 5,
|
|
43
61
|
output: 18
|
|
62
|
+
},
|
|
63
|
+
'gpt-5.6-luna': {
|
|
64
|
+
input: 0.4,
|
|
65
|
+
cacheWrite: 0.5,
|
|
66
|
+
output: 1.8
|
|
44
67
|
}
|
|
45
68
|
};
|
|
46
69
|
|
|
@@ -84,11 +84,14 @@ export async function handleMessages(req, res) {
|
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
const rotator = getAccountRotator();
|
|
87
|
-
|
|
87
|
+
const requestContext = getRequestContext(req);
|
|
88
|
+
|
|
88
89
|
rotator.clearExpiredLimits();
|
|
89
|
-
|
|
90
|
+
|
|
90
91
|
const maxAttempts = listAccounts().total;
|
|
91
|
-
let { account } = rotator.selectAccount(upstreamModel
|
|
92
|
+
let { account } = rotator.selectAccount(upstreamModel, {
|
|
93
|
+
sessionId: requestContext.sessionId
|
|
94
|
+
});
|
|
92
95
|
|
|
93
96
|
if (!account && rotator.isAllRateLimited(upstreamModel)) {
|
|
94
97
|
const minWait = rotator.getMinWaitTimeMs(upstreamModel);
|
|
@@ -104,14 +107,19 @@ export async function handleMessages(req, res) {
|
|
|
104
107
|
}
|
|
105
108
|
|
|
106
109
|
syncActiveAccount(account.email);
|
|
110
|
+
const attemptedEmails = new Set();
|
|
107
111
|
|
|
108
112
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
109
113
|
logger.info(`[Messages] Using account ${account.email} for model ${upstreamModel}`);
|
|
110
114
|
|
|
111
115
|
const creds = await getCredentialsForAccount(account.email);
|
|
112
116
|
if (!creds) {
|
|
117
|
+
attemptedEmails.add(account.email);
|
|
113
118
|
rotator.markInvalid(account.email, 'Failed to get credentials');
|
|
114
|
-
const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel
|
|
119
|
+
const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel, {
|
|
120
|
+
sessionId: requestContext.sessionId,
|
|
121
|
+
excludeEmails: attemptedEmails
|
|
122
|
+
});
|
|
115
123
|
if (!replacement) {
|
|
116
124
|
return sendAuthError(res, 'No other available account');
|
|
117
125
|
}
|
|
@@ -120,7 +128,6 @@ export async function handleMessages(req, res) {
|
|
|
120
128
|
}
|
|
121
129
|
|
|
122
130
|
const anthropicRequest = { ...body, model: upstreamModel };
|
|
123
|
-
const requestContext = getRequestContext(req);
|
|
124
131
|
|
|
125
132
|
try {
|
|
126
133
|
if (isStreaming) {
|
|
@@ -136,8 +143,12 @@ export async function handleMessages(req, res) {
|
|
|
136
143
|
}
|
|
137
144
|
|
|
138
145
|
if (error.message.startsWith('RATE_LIMITED:')) {
|
|
146
|
+
attemptedEmails.add(account.email);
|
|
139
147
|
rotator.notifyRateLimit(account, upstreamModel);
|
|
140
|
-
const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel
|
|
148
|
+
const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel, {
|
|
149
|
+
sessionId: requestContext.sessionId,
|
|
150
|
+
excludeEmails: attemptedEmails
|
|
151
|
+
});
|
|
141
152
|
if (!replacement) {
|
|
142
153
|
logger.info(`[Messages] Rate limit on ${account.email}; no other account available, stopping retry`);
|
|
143
154
|
return handleStreamError(res, error, upstreamModel, startTime, requestedModel);
|
|
@@ -149,8 +160,12 @@ export async function handleMessages(req, res) {
|
|
|
149
160
|
}
|
|
150
161
|
|
|
151
162
|
if (error.message.includes('AUTH_EXPIRED')) {
|
|
163
|
+
attemptedEmails.add(account.email);
|
|
152
164
|
rotator.markInvalid(account.email, 'Auth expired');
|
|
153
|
-
const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel
|
|
165
|
+
const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel, {
|
|
166
|
+
sessionId: requestContext.sessionId,
|
|
167
|
+
excludeEmails: attemptedEmails
|
|
168
|
+
});
|
|
154
169
|
if (!replacement) {
|
|
155
170
|
logger.info(`[Messages] Auth expired on ${account.email}; no other account available, stopping retry`);
|
|
156
171
|
return handleStreamError(res, error, upstreamModel, startTime, requestedModel);
|
|
@@ -212,8 +227,20 @@ function recordRequestUsage(clientKeyId, model, usage) {
|
|
|
212
227
|
* Selects and activates a different usable account before allowing a retry.
|
|
213
228
|
* @returns {object|null} The activated replacement account, or null when switching failed.
|
|
214
229
|
*/
|
|
215
|
-
export function switchToReplacementAccount(
|
|
216
|
-
|
|
230
|
+
export function switchToReplacementAccount(
|
|
231
|
+
rotator,
|
|
232
|
+
failedEmail,
|
|
233
|
+
modelId,
|
|
234
|
+
selectionOptions = {},
|
|
235
|
+
activateAccount = setActiveAccount
|
|
236
|
+
) {
|
|
237
|
+
const excludeEmails = new Set(selectionOptions.excludeEmails || []);
|
|
238
|
+
excludeEmails.add(failedEmail);
|
|
239
|
+
const { account } = rotator.selectAccount(modelId, {
|
|
240
|
+
...selectionOptions,
|
|
241
|
+
excludeEmails,
|
|
242
|
+
deferSessionBinding: true
|
|
243
|
+
});
|
|
217
244
|
if (!account || account.email === failedEmail) {
|
|
218
245
|
return null;
|
|
219
246
|
}
|
|
@@ -224,6 +251,7 @@ export function switchToReplacementAccount(rotator, failedEmail, modelId, activa
|
|
|
224
251
|
return null;
|
|
225
252
|
}
|
|
226
253
|
|
|
254
|
+
rotator.commitSessionBinding?.(selectionOptions.sessionId, account.email);
|
|
227
255
|
return account;
|
|
228
256
|
}
|
|
229
257
|
|
|
@@ -13,6 +13,8 @@ import { getCredentialsOrError } from '../middleware/credentials.js';
|
|
|
13
13
|
|
|
14
14
|
export const FALLBACK_MODELS = [
|
|
15
15
|
// OpenAI upstream models
|
|
16
|
+
{ id: 'gpt-6-astra', object: 'model', owned_by: 'openai' },
|
|
17
|
+
{ id: 'gpt-5.6-luna', object: 'model', owned_by: 'openai' },
|
|
16
18
|
{ id: 'gpt-5.3-codex', object: 'model', owned_by: 'openai' },
|
|
17
19
|
{ id: 'gpt-5.2-codex', object: 'model', owned_by: 'openai' },
|
|
18
20
|
{ id: 'gpt-5.1-codex', object: 'model', owned_by: 'openai' },
|