claude-codex-proxy 1.0.5-beta.23 → 1.0.5-beta.24

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-codex-proxy",
3
- "version": "1.0.5-beta.23",
3
+ "version": "1.0.5-beta.24",
4
4
  "description": "Multi-account proxy server for OpenAI Codex CLI with Claude API compatibility",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -8,6 +8,7 @@ import { cleanCacheControl, processAssistantContent, hasUnsignedThinkingBlocks }
8
8
  import { getCachedSignature, cacheSignature, cacheThinkingSignature, SIGNATURE_CONSTANTS } from './signature-cache.js';
9
9
 
10
10
  const { MIN_SIGNATURE_LENGTH } = SIGNATURE_CONSTANTS;
11
+ const SUPPORTED_REASONING_EFFORTS = new Set(['low', 'medium', 'high', 'xhigh', 'max']);
11
12
 
12
13
  /**
13
14
  * Convert Anthropic tool ID to OpenAI fc_ format
@@ -162,6 +163,11 @@ export function convertAnthropicToResponsesAPI(anthropicRequest) {
162
163
  request.instructions = '';
163
164
  }
164
165
 
166
+ const effort = anthropicRequest.output_config?.effort;
167
+ if (SUPPORTED_REASONING_EFFORTS.has(effort)) {
168
+ request.reasoning = { effort };
169
+ }
170
+
165
171
  return request;
166
172
  }
167
173
 
@@ -4,16 +4,12 @@ import { sendAuthError, getCredentialsForAccount } from '../middleware/credentia
4
4
  import { initSSEResponse, pipeSSEStream, handleStreamError } from '../middleware/sse.js';
5
5
  import { logger } from '../utils/logger.js';
6
6
  import { AccountRotator } from '../account-rotation/index.js';
7
- import { listAccounts, loadAccounts, getActiveAccount, setActiveAccount, save, isAccountExpired } from '../account-manager.js';
7
+ import { listAccounts, loadAccounts, getActiveAccount, setActiveAccount, save } from '../account-manager.js';
8
8
  import { getServerSettings } from '../server-settings.js';
9
9
  import { recordClientKeyUsage } from '../client-key-usage.js';
10
10
  import { calculateUsageCostMicrousd } from '../model-pricing.js';
11
11
  import { shouldLogRawProxyPayloads } from '../utils/proxy-logging.js';
12
12
 
13
- const MAX_RETRIES = 5;
14
- const MAX_WAIT_BEFORE_ERROR_MS = 120000;
15
- const SHORT_RATE_LIMIT_THRESHOLD_MS = 5000;
16
-
17
13
  let accountRotator = null;
18
14
  let currentStrategy = null;
19
15
 
@@ -52,7 +48,6 @@ function getAccountRotator() {
52
48
  export async function handleMessages(req, res) {
53
49
  const startTime = Date.now();
54
50
  const body = req.body;
55
- const settings = getServerSettings();
56
51
  if (shouldLogRawProxyPayloads()) {
57
52
  logger.info('[Claude Request]', body);
58
53
  }
@@ -65,38 +60,38 @@ export async function handleMessages(req, res) {
65
60
 
66
61
  rotator.clearExpiredLimits();
67
62
 
68
- const maxAttempts = Math.max(MAX_RETRIES, listAccounts().total);
69
-
70
- for (let attempt = 0; attempt < maxAttempts; attempt++) {
71
- const { account, waitMs } = rotator.selectAccount(upstreamModel);
72
-
73
- if (!account && rotator.isAllRateLimited(upstreamModel)) {
74
- const minWait = rotator.getMinWaitTimeMs(upstreamModel);
75
- if (minWait > 0) {
76
- const waitSeconds = Math.round(minWait / 1000);
77
- logger.info(`[Messages] All accounts temporarily unavailable. Next possible reset: ${waitSeconds}s`);
78
- return handleStreamError(res, new Error(`RESOURCE_EXHAUSTED: All accounts rate-limited. Wait ${waitSeconds}s`), upstreamModel, startTime);
79
- }
80
- }
81
-
82
- if (!account) {
83
- if (waitMs > 0) {
84
- await sleep(waitMs);
85
- attempt--;
86
- continue;
87
- }
88
- return sendAuthError(res, 'No available accounts');
63
+ const maxAttempts = listAccounts().total;
64
+ let { account } = rotator.selectAccount(upstreamModel);
65
+
66
+ if (!account && rotator.isAllRateLimited(upstreamModel)) {
67
+ const minWait = rotator.getMinWaitTimeMs(upstreamModel);
68
+ if (minWait > 0) {
69
+ const waitSeconds = Math.round(minWait / 1000);
70
+ logger.info(`[Messages] All accounts temporarily unavailable. Next possible reset: ${waitSeconds}s`);
71
+ return handleStreamError(res, new Error(`RESOURCE_EXHAUSTED: All accounts rate-limited. Wait ${waitSeconds}s`), upstreamModel, startTime);
89
72
  }
90
-
91
- syncActiveAccount(account.email);
73
+ }
74
+
75
+ if (!account) {
76
+ return sendAuthError(res, 'No available accounts');
77
+ }
78
+
79
+ syncActiveAccount(account.email);
80
+
81
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
92
82
  logger.info(`[Messages] Using account ${account.email} for model ${upstreamModel}`);
93
83
 
94
84
  const creds = await getCredentialsForAccount(account.email);
95
85
  if (!creds) {
96
86
  rotator.markInvalid(account.email, 'Failed to get credentials');
87
+ const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel);
88
+ if (!replacement) {
89
+ return sendAuthError(res, 'No other available account');
90
+ }
91
+ account = replacement;
97
92
  continue;
98
93
  }
99
-
94
+
100
95
  const anthropicRequest = { ...body, model: upstreamModel };
101
96
  const requestContext = getRequestContext(req);
102
97
 
@@ -109,26 +104,33 @@ export async function handleMessages(req, res) {
109
104
  rotator.notifySuccess(account, upstreamModel);
110
105
  return;
111
106
  } catch (error) {
107
+ if (res.headersSent) {
108
+ return handleStreamError(res, error, upstreamModel, startTime);
109
+ }
110
+
112
111
  if (error.message.startsWith('RATE_LIMITED:')) {
113
- const parts = error.message.split(':');
114
- const resetMs = parseInt(parts[1], 10);
115
- const errorText = parts.slice(2).join(':');
116
-
117
112
  rotator.notifyRateLimit(account, upstreamModel);
118
-
119
- if (resetMs <= SHORT_RATE_LIMIT_THRESHOLD_MS) {
120
- logger.info(`[Messages] Short rate limit on ${account.email}, waiting ${resetMs}ms...`);
121
- await sleep(resetMs);
122
- attempt--;
123
- continue;
113
+ const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel);
114
+ if (!replacement) {
115
+ logger.info(`[Messages] Rate limit on ${account.email}; no other account available, stopping retry`);
116
+ return handleStreamError(res, error, upstreamModel, startTime);
124
117
  }
125
-
126
- logger.info(`[Messages] Rate limit on ${account.email}, switching account...`);
118
+
119
+ logger.info(`[Messages] Rate limit on ${account.email}; switched to ${replacement.email}`);
120
+ account = replacement;
127
121
  continue;
128
122
  }
129
-
123
+
130
124
  if (error.message.includes('AUTH_EXPIRED')) {
131
125
  rotator.markInvalid(account.email, 'Auth expired');
126
+ const replacement = switchToReplacementAccount(rotator, account.email, upstreamModel);
127
+ if (!replacement) {
128
+ logger.info(`[Messages] Auth expired on ${account.email}; no other account available, stopping retry`);
129
+ return handleStreamError(res, error, upstreamModel, startTime);
130
+ }
131
+
132
+ logger.info(`[Messages] Auth expired on ${account.email}; switched to ${replacement.email}`);
133
+ account = replacement;
132
134
  continue;
133
135
  }
134
136
 
@@ -179,14 +181,34 @@ function recordRequestUsage(clientKeyId, model, usage) {
179
181
  recordClientKeyUsage(clientKeyId, costMicrousd);
180
182
  }
181
183
 
184
+ /**
185
+ * Selects and activates a different usable account before allowing a retry.
186
+ * @returns {object|null} The activated replacement account, or null when switching failed.
187
+ */
188
+ export function switchToReplacementAccount(rotator, failedEmail, modelId, activateAccount = setActiveAccount) {
189
+ const { account } = rotator.selectAccount(modelId);
190
+ if (!account || account.email === failedEmail) {
191
+ return null;
192
+ }
193
+
194
+ const result = activateAccount(account.email);
195
+ if (!result?.success) {
196
+ logger.warn(`[Messages] Failed to switch account from ${failedEmail} to ${account.email}`);
197
+ return null;
198
+ }
199
+
200
+ return account;
201
+ }
202
+
182
203
  function syncActiveAccount(email) {
183
204
  const active = getActiveAccount();
184
- if (active?.email === email) return;
205
+ if (active?.email === email) return true;
185
206
 
186
207
  const result = setActiveAccount(email);
187
208
  if (result.success) {
188
209
  logger.info(`[Messages] Synced active account to actual account: ${email}`);
189
210
  }
211
+ return result.success;
190
212
  }
191
213
 
192
214
  function getTokenDetails(usage = {}) {
@@ -201,8 +223,4 @@ function getTokenDetails(usage = {}) {
201
223
  };
202
224
  }
203
225
 
204
- function sleep(ms) {
205
- return new Promise(resolve => setTimeout(resolve, ms));
206
- }
207
-
208
226
  export default { handleMessages };