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 +1 -1
- package/src/client-key-usage.js +29 -13
- package/src/middleware/auth.js +1 -1
- package/src/response-streamer.js +6 -0
package/package.json
CHANGED
package/src/client-key-usage.js
CHANGED
|
@@ -1,27 +1,43 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
2
|
-
import {
|
|
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(
|
|
9
|
+
function loadUsage(usageFile = USAGE_FILE) {
|
|
10
|
+
if (!existsSync(usageFile)) return {};
|
|
10
11
|
|
|
11
12
|
try {
|
|
12
|
-
const data = JSON.parse(readFileSync(
|
|
13
|
-
|
|
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
|
-
|
|
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
|
-
|
|
22
|
-
|
|
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/middleware/auth.js
CHANGED
package/src/response-streamer.js
CHANGED
|
@@ -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;
|