claude-codex-proxy 1.0.5-beta.20 → 1.0.5-beta.22

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.20",
3
+ "version": "1.0.5-beta.22",
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",
@@ -1,27 +1,43 @@
1
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
2
- import { join } from 'path';
1
+ import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'fs';
2
+ import { randomUUID } from 'crypto';
3
+ import { dirname, join } from 'path';
3
4
  import { CONFIG_DIR } from './account-manager.js';
4
5
 
5
6
  const USAGE_FILE = join(CONFIG_DIR, 'client-key-usage.json');
6
7
  export const MICRO_USD_PER_USD = 1_000_000;
7
8
 
8
- function loadUsage() {
9
- if (!existsSync(USAGE_FILE)) return {};
9
+ function loadUsage(usageFile = USAGE_FILE) {
10
+ if (!existsSync(usageFile)) return {};
10
11
 
11
12
  try {
12
- const data = JSON.parse(readFileSync(USAGE_FILE, 'utf8'));
13
- return data && typeof data === 'object' && !Array.isArray(data) ? data : {};
13
+ const data = JSON.parse(readFileSync(usageFile, 'utf8'));
14
+ if (!data || typeof data !== 'object' || Array.isArray(data)) {
15
+ throw new Error('usage file must contain a JSON object');
16
+ }
17
+ return data;
14
18
  } catch (error) {
15
- console.error('[ClientKeyUsage] Failed to read usage:', error.message);
16
- return {};
19
+ throw new Error(`[ClientKeyUsage] Failed to read ${usageFile}: ${error.message}`, { cause: error });
17
20
  }
18
21
  }
19
22
 
20
- function saveUsage(usage) {
21
- if (!existsSync(CONFIG_DIR)) {
22
- mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
23
+ function saveUsage(usage, usageFile = USAGE_FILE) {
24
+ const usageDirectory = dirname(usageFile);
25
+ if (!existsSync(usageDirectory)) {
26
+ mkdirSync(usageDirectory, { recursive: true, mode: 0o700 });
27
+ }
28
+
29
+ const tempFile = `${usageFile}.${process.pid}.${randomUUID()}.tmp`;
30
+ try {
31
+ writeFileSync(tempFile, JSON.stringify(usage, null, 2), { mode: 0o600 });
32
+ renameSync(tempFile, usageFile);
33
+ } catch (error) {
34
+ try {
35
+ rmSync(tempFile, { force: true });
36
+ } catch {
37
+ // Preserve the original write error.
38
+ }
39
+ throw error;
23
40
  }
24
- writeFileSync(USAGE_FILE, JSON.stringify(usage, null, 2), { mode: 0o600 });
25
41
  }
26
42
 
27
43
  /**
@@ -122,4 +138,4 @@ export function getClientKeyQuota(keyId, weeklyLimitUsd, now = new Date()) {
122
138
  };
123
139
  }
124
140
 
125
- export { USAGE_FILE };
141
+ export { USAGE_FILE, loadUsage, saveUsage };
package/src/direct-api.js CHANGED
@@ -14,7 +14,6 @@ const require = createRequire(import.meta.url);
14
14
  const packageJson = require('../package.json');
15
15
  const API_URL = 'https://chatgpt.com/backend-api/codex/responses';
16
16
  const APP_VERSION = packageJson.version;
17
- const UPSTREAM_REQUEST_TIMEOUT_MS = 4 * 60 * 1000;
18
17
 
19
18
  function createAnthropicUsage(upstreamUsage = {}) {
20
19
  const usage = {
@@ -129,8 +128,7 @@ async function createResponsesAPIStreamResponse(anthropicRequest, accessToken, a
129
128
  const response = await fetch(API_URL, {
130
129
  method: 'POST',
131
130
  headers: buildOpencodeHeaders(accessToken, accountId, context),
132
- body: JSON.stringify(request),
133
- signal: AbortSignal.timeout(UPSTREAM_REQUEST_TIMEOUT_MS)
131
+ body: JSON.stringify(request)
134
132
  });
135
133
 
136
134
  if (response.ok) return response;
@@ -197,8 +195,7 @@ export async function sendMessage(anthropicRequest, accessToken, accountId, cont
197
195
  const response = await fetch(API_URL, {
198
196
  method: 'POST',
199
197
  headers: buildOpencodeHeaders(accessToken, accountId, context),
200
- body: JSON.stringify(request),
201
- signal: AbortSignal.timeout(UPSTREAM_REQUEST_TIMEOUT_MS)
198
+ body: JSON.stringify(request)
202
199
  });
203
200
 
204
201
  if (!response.ok) {
@@ -26,7 +26,7 @@ function getRequestApiKey(req) {
26
26
  return String(headerKey).trim();
27
27
  }
28
28
 
29
- const queryKey = req.query?.api_key || req.query?.access_token;
29
+ const queryKey = req.query?.api_key;
30
30
  if (queryKey) {
31
31
  return String(queryKey).trim();
32
32
  }
@@ -16,10 +16,10 @@ const STANDARD_PRICING = {
16
16
  output: 30
17
17
  },
18
18
  'gpt-5.6-terra': {
19
- input: 2.5,
20
- cachedInput: 0.25,
21
- cacheWrite: 3.125,
22
- output: 15
19
+ input: 1,
20
+ cachedInput: 0.1,
21
+ cacheWrite: 1.25,
22
+ output: 6
23
23
  },
24
24
  'gpt-5.4-mini': {
25
25
  input: 0.75,
@@ -37,10 +37,10 @@ const LONG_CONTEXT_PRICING = {
37
37
  output: 45
38
38
  },
39
39
  'gpt-5.6-terra': {
40
- input: 5,
41
- cachedInput: 0.5,
42
- cacheWrite: 6.25,
43
- output: 22.5
40
+ input: 2,
41
+ cachedInput: 0.2,
42
+ cacheWrite: 2.5,
43
+ output: 9
44
44
  }
45
45
  };
46
46
 
@@ -72,6 +72,7 @@ export async function* streamResponsesAPI(response, model) {
72
72
  let stopReason = 'end_turn';
73
73
  let pendingArguments = '';
74
74
  let usage = { input_tokens: 0, output_tokens: 0 };
75
+ let hasCompletedResponse = false;
75
76
 
76
77
  const reader = response.body.getReader();
77
78
  const decoder = new TextDecoder();
@@ -98,6 +99,7 @@ export async function* streamResponsesAPI(response, model) {
98
99
 
99
100
  // Extract usage from completed response
100
101
  if (eventType === 'response.completed' && event.response?.usage) {
102
+ hasCompletedResponse = true;
101
103
  if (shouldLogRawProxyPayloads()) {
102
104
  logger.info('[OpenAI Response Completed]', event.response);
103
105
  }
@@ -341,6 +343,10 @@ export async function* streamResponsesAPI(response, model) {
341
343
  }
342
344
  }
343
345
 
346
+ if (!hasCompletedResponse) {
347
+ throw createResponsesAPIError('Upstream stream ended before response.completed');
348
+ }
349
+
344
350
  // Handle no content received
345
351
  if (!hasEmittedStart) {
346
352
  hasEmittedStart = true;