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

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.21",
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 };
@@ -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
  }
@@ -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;