claude-codex-proxy 1.0.5-beta.42 → 1.0.5-beta.44
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 +23 -0
- package/src/model-pricing.js +24 -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
|
|
@@ -355,6 +376,8 @@ function convertAnthropicToolsToOpenAI(tools) {
|
|
|
355
376
|
type: 'function',
|
|
356
377
|
name: tool.name,
|
|
357
378
|
description: tool.description || '',
|
|
379
|
+
// Preserve optional arguments instead of letting Responses normalize them as required.
|
|
380
|
+
strict: false,
|
|
358
381
|
parameters: sanitizeSchema(tool.input_schema || { type: 'object' })
|
|
359
382
|
};
|
|
360
383
|
});
|
package/src/model-pricing.js
CHANGED
|
@@ -9,6 +9,18 @@ export const LONG_CONTEXT_THRESHOLD_TOKENS = 272_000;
|
|
|
9
9
|
export const DEFAULT_PRICING_MODEL = 'gpt-5.6-sol';
|
|
10
10
|
|
|
11
11
|
const STANDARD_PRICING = {
|
|
12
|
+
'gpt-6.2-sol': {
|
|
13
|
+
input: 2,
|
|
14
|
+
cachedInput: 0.1,
|
|
15
|
+
cacheWrite: 2.5,
|
|
16
|
+
output: 10
|
|
17
|
+
},
|
|
18
|
+
'gpt-6.2-astra': {
|
|
19
|
+
input: 8,
|
|
20
|
+
cachedInput: 0.4,
|
|
21
|
+
cacheWrite: 10,
|
|
22
|
+
output: 40
|
|
23
|
+
},
|
|
12
24
|
'gpt-6.1-terra': {
|
|
13
25
|
input: 1,
|
|
14
26
|
cachedInput: 0.05,
|
|
@@ -66,6 +78,18 @@ const STANDARD_PRICING = {
|
|
|
66
78
|
};
|
|
67
79
|
|
|
68
80
|
const LONG_CONTEXT_PRICING = {
|
|
81
|
+
'gpt-6.2-sol': {
|
|
82
|
+
input: 4,
|
|
83
|
+
cachedInput: 0.2,
|
|
84
|
+
cacheWrite: 5,
|
|
85
|
+
output: 15
|
|
86
|
+
},
|
|
87
|
+
'gpt-6.2-astra': {
|
|
88
|
+
input: 16,
|
|
89
|
+
cachedInput: 0.8,
|
|
90
|
+
cacheWrite: 20,
|
|
91
|
+
output: 60
|
|
92
|
+
},
|
|
69
93
|
'gpt-6.1-terra': {
|
|
70
94
|
input: 2,
|
|
71
95
|
cachedInput: 0.1,
|
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 {
|