claude-codex-proxy 1.0.5-beta.43 → 1.0.5-beta.45
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/direct-api.js +1 -19
- package/src/format-converter.js +34 -0
- package/src/response-streamer.js +2 -19
- package/src/routes/chat-route.js +6 -3
- package/src/routes/messages-route.js +2 -1
package/package.json
CHANGED
package/src/direct-api.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import os from 'os';
|
|
7
7
|
import { createRequire } from 'module';
|
|
8
|
-
import { convertAnthropicToResponsesAPI, convertOutputToAnthropic, generateMessageId } from './format-converter.js';
|
|
8
|
+
import { convertAnthropicToResponsesAPI, convertOutputToAnthropic, generateMessageId, createAnthropicUsage } from './format-converter.js';
|
|
9
9
|
import { streamResponsesAPI, parseResponsesAPIResponse, createResponsesAPIError } from './response-streamer.js';
|
|
10
10
|
import { logger } from './utils/logger.js';
|
|
11
11
|
import { shouldLogRawProxyPayloads } from './utils/proxy-logging.js';
|
|
@@ -15,24 +15,6 @@ 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
17
|
|
|
18
|
-
function createAnthropicUsage(upstreamUsage = {}) {
|
|
19
|
-
const usage = {
|
|
20
|
-
input_tokens: upstreamUsage.input_tokens || 0,
|
|
21
|
-
output_tokens: upstreamUsage.output_tokens || 0,
|
|
22
|
-
// Responses API cache metrics are already included in input_tokens.
|
|
23
|
-
cache_read_input_tokens: 0
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(usage, 'upstreamCacheReadInputTokens', {
|
|
26
|
-
value: upstreamUsage.cache_read_input_tokens || upstreamUsage.input_tokens_details?.cached_tokens || 0,
|
|
27
|
-
enumerable: false
|
|
28
|
-
});
|
|
29
|
-
Object.defineProperty(usage, 'upstreamCacheWriteInputTokens', {
|
|
30
|
-
value: upstreamUsage.input_tokens_details?.cache_write_tokens || 0,
|
|
31
|
-
enumerable: false
|
|
32
|
-
});
|
|
33
|
-
return usage;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
18
|
function buildOpencodeHeaders(accessToken, accountId, context = {}) {
|
|
37
19
|
const headers = {
|
|
38
20
|
'Authorization': `Bearer ${accessToken}`,
|
package/src/format-converter.js
CHANGED
|
@@ -10,6 +10,27 @@ import { getCachedSignature, cacheSignature, cacheThinkingSignature, SIGNATURE_C
|
|
|
10
10
|
const { MIN_SIGNATURE_LENGTH } = SIGNATURE_CONSTANTS;
|
|
11
11
|
const SUPPORTED_REASONING_EFFORTS = new Set(['low', 'medium', 'high', 'xhigh', 'max']);
|
|
12
12
|
|
|
13
|
+
/** Convert inclusive Responses input usage to Anthropic's disjoint input categories. */
|
|
14
|
+
export function createAnthropicUsage(upstreamUsage = {}) {
|
|
15
|
+
const inputTokens = upstreamUsage.input_tokens || 0;
|
|
16
|
+
const cacheReadTokens = upstreamUsage.cache_read_input_tokens ?? upstreamUsage.input_tokens_details?.cached_tokens ?? 0;
|
|
17
|
+
const cacheWriteTokens = upstreamUsage.cache_creation_input_tokens ?? upstreamUsage.input_tokens_details?.cache_write_tokens ?? 0;
|
|
18
|
+
const usage = {
|
|
19
|
+
input_tokens: Math.max(0, inputTokens - cacheReadTokens - cacheWriteTokens),
|
|
20
|
+
output_tokens: upstreamUsage.output_tokens || 0,
|
|
21
|
+
cache_read_input_tokens: cacheReadTokens,
|
|
22
|
+
cache_creation_input_tokens: cacheWriteTokens
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Billing and logs still use the inclusive upstream total; never serialize it to clients.
|
|
26
|
+
Object.defineProperties(usage, {
|
|
27
|
+
upstreamInputTokens: { value: inputTokens, enumerable: false },
|
|
28
|
+
upstreamCacheReadInputTokens: { value: cacheReadTokens, enumerable: false },
|
|
29
|
+
upstreamCacheWriteInputTokens: { value: cacheWriteTokens, enumerable: false }
|
|
30
|
+
});
|
|
31
|
+
return usage;
|
|
32
|
+
}
|
|
33
|
+
|
|
13
34
|
/**
|
|
14
35
|
* Convert Anthropic tool ID to OpenAI fc_ format
|
|
15
36
|
* Deterministic: strips toolu_/call_ prefix, adds fc_ prefix
|
|
@@ -163,6 +184,19 @@ export function convertAnthropicToResponsesAPI(anthropicRequest) {
|
|
|
163
184
|
request.instructions = '';
|
|
164
185
|
}
|
|
165
186
|
|
|
187
|
+
// Preserve the output constraint independently of reasoning effort and transport.
|
|
188
|
+
const format = anthropicRequest.output_config?.format;
|
|
189
|
+
if (format?.type === 'json_schema') {
|
|
190
|
+
request.text = {
|
|
191
|
+
format: {
|
|
192
|
+
type: 'json_schema',
|
|
193
|
+
name: 'claude_output_schema',
|
|
194
|
+
strict: true,
|
|
195
|
+
schema: format.schema
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
166
200
|
const effort = anthropicRequest.output_config?.effort;
|
|
167
201
|
if (SUPPORTED_REASONING_EFFORTS.has(effort)) {
|
|
168
202
|
request.reasoning = { effort };
|
package/src/response-streamer.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Streams SSE events from OpenAI Responses API and converts to Anthropic format
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { generateMessageId, sanitizeAnthropicToolInputJson, toAnthropicToolId } from './format-converter.js';
|
|
6
|
+
import { generateMessageId, sanitizeAnthropicToolInputJson, toAnthropicToolId, createAnthropicUsage } from './format-converter.js';
|
|
7
7
|
import { cacheSignature, cacheThinkingSignature, SIGNATURE_CONSTANTS } from './signature-cache.js';
|
|
8
8
|
import { logger } from './utils/logger.js';
|
|
9
9
|
import { shouldLogRawProxyPayloads } from './utils/proxy-logging.js';
|
|
@@ -67,8 +67,6 @@ export async function* streamResponsesAPI(response, model) {
|
|
|
67
67
|
let currentBlockId = null;
|
|
68
68
|
let currentToolName = null;
|
|
69
69
|
let currentThinkingSignature = '';
|
|
70
|
-
let inputTokens = 0;
|
|
71
|
-
let outputTokens = 0;
|
|
72
70
|
let stopReason = 'end_turn';
|
|
73
71
|
let pendingArguments = '';
|
|
74
72
|
let usage = { input_tokens: 0, output_tokens: 0 };
|
|
@@ -103,22 +101,7 @@ export async function* streamResponsesAPI(response, model) {
|
|
|
103
101
|
if (shouldLogRawProxyPayloads()) {
|
|
104
102
|
logger.info('[OpenAI Response Completed]', event.response);
|
|
105
103
|
}
|
|
106
|
-
|
|
107
|
-
outputTokens = event.response.usage.output_tokens || 0;
|
|
108
|
-
usage = {
|
|
109
|
-
input_tokens: inputTokens,
|
|
110
|
-
output_tokens: outputTokens,
|
|
111
|
-
// Responses API cache metrics are already included in input_tokens.
|
|
112
|
-
cache_read_input_tokens: 0
|
|
113
|
-
};
|
|
114
|
-
Object.defineProperty(usage, 'upstreamCacheReadInputTokens', {
|
|
115
|
-
value: event.response.usage.cache_read_input_tokens || event.response.usage.input_tokens_details?.cached_tokens || 0,
|
|
116
|
-
enumerable: false
|
|
117
|
-
});
|
|
118
|
-
Object.defineProperty(usage, 'upstreamCacheWriteInputTokens', {
|
|
119
|
-
value: event.response.usage.input_tokens_details?.cache_write_tokens || 0,
|
|
120
|
-
enumerable: false
|
|
121
|
-
});
|
|
104
|
+
usage = createAnthropicUsage(event.response.usage);
|
|
122
105
|
}
|
|
123
106
|
|
|
124
107
|
// Handle output item added
|
package/src/routes/chat-route.js
CHANGED
|
@@ -117,6 +117,7 @@ function recordRequestUsage(clientKeyId, model, usage) {
|
|
|
117
117
|
|
|
118
118
|
const costMicrousd = calculateUsageCostMicrousd(model, {
|
|
119
119
|
...usage,
|
|
120
|
+
input_tokens: usage?.upstreamInputTokens ?? usage?.input_tokens,
|
|
120
121
|
cache_read_input_tokens: usage?.upstreamCacheReadInputTokens ?? usage?.cache_read_input_tokens,
|
|
121
122
|
input_tokens_details: {
|
|
122
123
|
...(usage?.input_tokens_details || {}),
|
|
@@ -228,6 +229,8 @@ function _buildOpenAIResponse(response, responseModel) {
|
|
|
228
229
|
}));
|
|
229
230
|
}
|
|
230
231
|
|
|
232
|
+
// Chat Completions counts cached tokens inside prompt_tokens, unlike Anthropic.
|
|
233
|
+
const inputTokens = response.usage?.upstreamInputTokens ?? response.usage?.input_tokens ?? 0;
|
|
231
234
|
return {
|
|
232
235
|
id: response.id,
|
|
233
236
|
object: 'chat.completion',
|
|
@@ -239,15 +242,15 @@ function _buildOpenAIResponse(response, responseModel) {
|
|
|
239
242
|
finish_reason: toolUses.length > 0 ? 'tool_calls' : 'stop'
|
|
240
243
|
}],
|
|
241
244
|
usage: {
|
|
242
|
-
prompt_tokens:
|
|
245
|
+
prompt_tokens: inputTokens,
|
|
243
246
|
completion_tokens: response.usage?.output_tokens || 0,
|
|
244
|
-
total_tokens:
|
|
247
|
+
total_tokens: inputTokens + (response.usage?.output_tokens || 0)
|
|
245
248
|
}
|
|
246
249
|
};
|
|
247
250
|
}
|
|
248
251
|
|
|
249
252
|
function getTokenDetails(usage = {}) {
|
|
250
|
-
const inputTokens = usage?.input_tokens
|
|
253
|
+
const inputTokens = usage?.upstreamInputTokens ?? usage?.input_tokens ?? 0;
|
|
251
254
|
const outputTokens = usage?.output_tokens || 0;
|
|
252
255
|
const cacheReadInputTokens = usage?.upstreamCacheReadInputTokens ?? usage?.cache_read_input_tokens ?? 0;
|
|
253
256
|
return {
|
|
@@ -209,6 +209,7 @@ function recordRequestUsage(clientKeyId, model, usage) {
|
|
|
209
209
|
|
|
210
210
|
const costMicrousd = calculateUsageCostMicrousd(model, {
|
|
211
211
|
...usage,
|
|
212
|
+
input_tokens: usage?.upstreamInputTokens ?? usage?.input_tokens,
|
|
212
213
|
cache_read_input_tokens: usage?.upstreamCacheReadInputTokens ?? usage?.cache_read_input_tokens,
|
|
213
214
|
input_tokens_details: {
|
|
214
215
|
...(usage?.input_tokens_details || {}),
|
|
@@ -267,7 +268,7 @@ function syncActiveAccount(email) {
|
|
|
267
268
|
}
|
|
268
269
|
|
|
269
270
|
function getTokenDetails(usage = {}) {
|
|
270
|
-
const inputTokens = usage?.input_tokens
|
|
271
|
+
const inputTokens = usage?.upstreamInputTokens ?? usage?.input_tokens ?? 0;
|
|
271
272
|
const outputTokens = usage?.output_tokens || 0;
|
|
272
273
|
const cacheReadInputTokens = usage?.upstreamCacheReadInputTokens ?? usage?.cache_read_input_tokens ?? 0;
|
|
273
274
|
return {
|