claude-codex-proxy 1.0.5-beta.13 → 1.0.5-beta.15
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/admin-path.js +33 -33
- package/src/client-key-usage.js +125 -125
- package/src/direct-api.js +4 -0
- package/src/format-converter.js +53 -41
- package/src/model-pricing.js +96 -96
- package/src/routes/messages-route.js +6 -1
- package/src/utils/proxy-logging.js +5 -5
package/package.json
CHANGED
package/src/admin-path.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolves the non-public prefix used for the management UI and its APIs.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const DEFAULT_ADMIN_PATH = '/admin';
|
|
6
|
-
const VALID_ADMIN_PATH = /^\/[A-Za-z0-9][A-Za-z0-9._-]*(?:\/[A-Za-z0-9][A-Za-z0-9._-]*)*$/;
|
|
7
|
-
const RESERVED_PATHS = ['/v1', '/health', '/claude'];
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Returns the validated management UI path prefix.
|
|
11
|
-
* @returns {string}
|
|
12
|
-
*/
|
|
13
|
-
export function getAdminPath() {
|
|
14
|
-
const adminPath = process.env.PROXY_ADMIN_PATH || DEFAULT_ADMIN_PATH;
|
|
15
|
-
if (!VALID_ADMIN_PATH.test(adminPath)) {
|
|
16
|
-
throw new Error(`Invalid PROXY_ADMIN_PATH "${adminPath}". Use an absolute path without trailing slashes, query strings, or ".." segments.`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const overlapsReservedPath = RESERVED_PATHS.some((reservedPath) => (
|
|
20
|
-
adminPath === reservedPath
|
|
21
|
-
|| adminPath.startsWith(`${reservedPath}/`)
|
|
22
|
-
|| reservedPath.startsWith(`${adminPath}/`)
|
|
23
|
-
));
|
|
24
|
-
if (overlapsReservedPath) {
|
|
25
|
-
throw new Error(`Invalid PROXY_ADMIN_PATH "${adminPath}". The path must not overlap reserved routes: ${RESERVED_PATHS.join(', ')}.`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return adminPath;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export { DEFAULT_ADMIN_PATH };
|
|
32
|
-
|
|
33
|
-
export default { getAdminPath, DEFAULT_ADMIN_PATH };
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the non-public prefix used for the management UI and its APIs.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const DEFAULT_ADMIN_PATH = '/admin';
|
|
6
|
+
const VALID_ADMIN_PATH = /^\/[A-Za-z0-9][A-Za-z0-9._-]*(?:\/[A-Za-z0-9][A-Za-z0-9._-]*)*$/;
|
|
7
|
+
const RESERVED_PATHS = ['/v1', '/health', '/claude'];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns the validated management UI path prefix.
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
export function getAdminPath() {
|
|
14
|
+
const adminPath = process.env.PROXY_ADMIN_PATH || DEFAULT_ADMIN_PATH;
|
|
15
|
+
if (!VALID_ADMIN_PATH.test(adminPath)) {
|
|
16
|
+
throw new Error(`Invalid PROXY_ADMIN_PATH "${adminPath}". Use an absolute path without trailing slashes, query strings, or ".." segments.`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const overlapsReservedPath = RESERVED_PATHS.some((reservedPath) => (
|
|
20
|
+
adminPath === reservedPath
|
|
21
|
+
|| adminPath.startsWith(`${reservedPath}/`)
|
|
22
|
+
|| reservedPath.startsWith(`${adminPath}/`)
|
|
23
|
+
));
|
|
24
|
+
if (overlapsReservedPath) {
|
|
25
|
+
throw new Error(`Invalid PROXY_ADMIN_PATH "${adminPath}". The path must not overlap reserved routes: ${RESERVED_PATHS.join(', ')}.`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return adminPath;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { DEFAULT_ADMIN_PATH };
|
|
32
|
+
|
|
33
|
+
export default { getAdminPath, DEFAULT_ADMIN_PATH };
|
package/src/client-key-usage.js
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
2
|
-
import { join } from 'path';
|
|
3
|
-
import { CONFIG_DIR } from './account-manager.js';
|
|
4
|
-
|
|
5
|
-
const USAGE_FILE = join(CONFIG_DIR, 'client-key-usage.json');
|
|
6
|
-
export const MICRO_USD_PER_USD = 1_000_000;
|
|
7
|
-
|
|
8
|
-
function loadUsage() {
|
|
9
|
-
if (!existsSync(USAGE_FILE)) return {};
|
|
10
|
-
|
|
11
|
-
try {
|
|
12
|
-
const data = JSON.parse(readFileSync(USAGE_FILE, 'utf8'));
|
|
13
|
-
return data && typeof data === 'object' && !Array.isArray(data) ? data : {};
|
|
14
|
-
} catch (error) {
|
|
15
|
-
console.error('[ClientKeyUsage] Failed to read usage:', error.message);
|
|
16
|
-
return {};
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function saveUsage(usage) {
|
|
21
|
-
if (!existsSync(CONFIG_DIR)) {
|
|
22
|
-
mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
23
|
-
}
|
|
24
|
-
writeFileSync(USAGE_FILE, JSON.stringify(usage, null, 2), { mode: 0o600 });
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Returns the current weekly period start in the server's local time zone.
|
|
29
|
-
* A period begins at Monday 08:00 and ends at the following Monday 08:00.
|
|
30
|
-
*/
|
|
31
|
-
export function getCurrentPeriodStart(now = new Date()) {
|
|
32
|
-
const periodStart = new Date(now);
|
|
33
|
-
const daysSinceMonday = (periodStart.getDay() + 6) % 7;
|
|
34
|
-
periodStart.setDate(periodStart.getDate() - daysSinceMonday);
|
|
35
|
-
periodStart.setHours(8, 0, 0, 0);
|
|
36
|
-
|
|
37
|
-
if (now < periodStart) {
|
|
38
|
-
periodStart.setDate(periodStart.getDate() - 7);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return periodStart.toISOString();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function getNextPeriodStart(now = new Date()) {
|
|
45
|
-
const nextPeriodStart = new Date(getCurrentPeriodStart(now));
|
|
46
|
-
nextPeriodStart.setDate(nextPeriodStart.getDate() + 7);
|
|
47
|
-
return nextPeriodStart.toISOString();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function usdToMicrousd(usd) {
|
|
51
|
-
return Math.round(Number(usd) * MICRO_USD_PER_USD);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function microusdToUsd(microusd) {
|
|
55
|
-
return Number(microusd) / MICRO_USD_PER_USD;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Returns a key's usage after lazily resetting it for a new weekly period.
|
|
60
|
-
*/
|
|
61
|
-
export function getClientKeyUsage(keyId, now = new Date()) {
|
|
62
|
-
const usage = loadUsage();
|
|
63
|
-
const periodStart = getCurrentPeriodStart(now);
|
|
64
|
-
const entry = usage[keyId];
|
|
65
|
-
const consumedMicrousd = entry?.periodStart === periodStart && Number.isSafeInteger(entry.consumedMicrousd)
|
|
66
|
-
? Math.max(0, entry.consumedMicrousd)
|
|
67
|
-
: 0;
|
|
68
|
-
|
|
69
|
-
if (!entry || entry.periodStart !== periodStart || entry.consumedMicrousd !== consumedMicrousd) {
|
|
70
|
-
usage[keyId] = { periodStart, consumedMicrousd };
|
|
71
|
-
saveUsage(usage);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
periodStart,
|
|
76
|
-
nextPeriodStart: getNextPeriodStart(now),
|
|
77
|
-
consumedMicrousd
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Records actual USD consumption for a client key. Call this only after an
|
|
83
|
-
* upstream response provides a verified per-request charge.
|
|
84
|
-
*/
|
|
85
|
-
export function recordClientKeyUsage(keyId, consumedMicrousd, now = new Date()) {
|
|
86
|
-
if (!Number.isSafeInteger(consumedMicrousd) || consumedMicrousd < 0) {
|
|
87
|
-
throw new TypeError('consumedMicrousd must be a non-negative safe integer');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const current = getClientKeyUsage(keyId, now);
|
|
91
|
-
const usage = loadUsage();
|
|
92
|
-
usage[keyId] = {
|
|
93
|
-
periodStart: current.periodStart,
|
|
94
|
-
consumedMicrousd: current.consumedMicrousd + consumedMicrousd
|
|
95
|
-
};
|
|
96
|
-
saveUsage(usage);
|
|
97
|
-
return usage[keyId];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function deleteClientKeyUsage(keyId) {
|
|
101
|
-
const usage = loadUsage();
|
|
102
|
-
if (!(keyId in usage)) return;
|
|
103
|
-
delete usage[keyId];
|
|
104
|
-
saveUsage(usage);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Returns a key's current quota summary without charging any request.
|
|
109
|
-
*/
|
|
110
|
-
export function getClientKeyQuota(keyId, weeklyLimitUsd, now = new Date()) {
|
|
111
|
-
const usage = getClientKeyUsage(keyId, now);
|
|
112
|
-
const weeklyLimitMicrousd = usdToMicrousd(weeklyLimitUsd);
|
|
113
|
-
const remainingMicrousd = Math.max(0, weeklyLimitMicrousd - usage.consumedMicrousd);
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
...usage,
|
|
117
|
-
weeklyLimitMicrousd,
|
|
118
|
-
remainingMicrousd,
|
|
119
|
-
weeklyLimitUsd: microusdToUsd(weeklyLimitMicrousd),
|
|
120
|
-
consumedUsd: microusdToUsd(usage.consumedMicrousd),
|
|
121
|
-
remainingUsd: microusdToUsd(remainingMicrousd)
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export { USAGE_FILE };
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { CONFIG_DIR } from './account-manager.js';
|
|
4
|
+
|
|
5
|
+
const USAGE_FILE = join(CONFIG_DIR, 'client-key-usage.json');
|
|
6
|
+
export const MICRO_USD_PER_USD = 1_000_000;
|
|
7
|
+
|
|
8
|
+
function loadUsage() {
|
|
9
|
+
if (!existsSync(USAGE_FILE)) return {};
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const data = JSON.parse(readFileSync(USAGE_FILE, 'utf8'));
|
|
13
|
+
return data && typeof data === 'object' && !Array.isArray(data) ? data : {};
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.error('[ClientKeyUsage] Failed to read usage:', error.message);
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function saveUsage(usage) {
|
|
21
|
+
if (!existsSync(CONFIG_DIR)) {
|
|
22
|
+
mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
23
|
+
}
|
|
24
|
+
writeFileSync(USAGE_FILE, JSON.stringify(usage, null, 2), { mode: 0o600 });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Returns the current weekly period start in the server's local time zone.
|
|
29
|
+
* A period begins at Monday 08:00 and ends at the following Monday 08:00.
|
|
30
|
+
*/
|
|
31
|
+
export function getCurrentPeriodStart(now = new Date()) {
|
|
32
|
+
const periodStart = new Date(now);
|
|
33
|
+
const daysSinceMonday = (periodStart.getDay() + 6) % 7;
|
|
34
|
+
periodStart.setDate(periodStart.getDate() - daysSinceMonday);
|
|
35
|
+
periodStart.setHours(8, 0, 0, 0);
|
|
36
|
+
|
|
37
|
+
if (now < periodStart) {
|
|
38
|
+
periodStart.setDate(periodStart.getDate() - 7);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return periodStart.toISOString();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getNextPeriodStart(now = new Date()) {
|
|
45
|
+
const nextPeriodStart = new Date(getCurrentPeriodStart(now));
|
|
46
|
+
nextPeriodStart.setDate(nextPeriodStart.getDate() + 7);
|
|
47
|
+
return nextPeriodStart.toISOString();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function usdToMicrousd(usd) {
|
|
51
|
+
return Math.round(Number(usd) * MICRO_USD_PER_USD);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function microusdToUsd(microusd) {
|
|
55
|
+
return Number(microusd) / MICRO_USD_PER_USD;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns a key's usage after lazily resetting it for a new weekly period.
|
|
60
|
+
*/
|
|
61
|
+
export function getClientKeyUsage(keyId, now = new Date()) {
|
|
62
|
+
const usage = loadUsage();
|
|
63
|
+
const periodStart = getCurrentPeriodStart(now);
|
|
64
|
+
const entry = usage[keyId];
|
|
65
|
+
const consumedMicrousd = entry?.periodStart === periodStart && Number.isSafeInteger(entry.consumedMicrousd)
|
|
66
|
+
? Math.max(0, entry.consumedMicrousd)
|
|
67
|
+
: 0;
|
|
68
|
+
|
|
69
|
+
if (!entry || entry.periodStart !== periodStart || entry.consumedMicrousd !== consumedMicrousd) {
|
|
70
|
+
usage[keyId] = { periodStart, consumedMicrousd };
|
|
71
|
+
saveUsage(usage);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
periodStart,
|
|
76
|
+
nextPeriodStart: getNextPeriodStart(now),
|
|
77
|
+
consumedMicrousd
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Records actual USD consumption for a client key. Call this only after an
|
|
83
|
+
* upstream response provides a verified per-request charge.
|
|
84
|
+
*/
|
|
85
|
+
export function recordClientKeyUsage(keyId, consumedMicrousd, now = new Date()) {
|
|
86
|
+
if (!Number.isSafeInteger(consumedMicrousd) || consumedMicrousd < 0) {
|
|
87
|
+
throw new TypeError('consumedMicrousd must be a non-negative safe integer');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const current = getClientKeyUsage(keyId, now);
|
|
91
|
+
const usage = loadUsage();
|
|
92
|
+
usage[keyId] = {
|
|
93
|
+
periodStart: current.periodStart,
|
|
94
|
+
consumedMicrousd: current.consumedMicrousd + consumedMicrousd
|
|
95
|
+
};
|
|
96
|
+
saveUsage(usage);
|
|
97
|
+
return usage[keyId];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function deleteClientKeyUsage(keyId) {
|
|
101
|
+
const usage = loadUsage();
|
|
102
|
+
if (!(keyId in usage)) return;
|
|
103
|
+
delete usage[keyId];
|
|
104
|
+
saveUsage(usage);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns a key's current quota summary without charging any request.
|
|
109
|
+
*/
|
|
110
|
+
export function getClientKeyQuota(keyId, weeklyLimitUsd, now = new Date()) {
|
|
111
|
+
const usage = getClientKeyUsage(keyId, now);
|
|
112
|
+
const weeklyLimitMicrousd = usdToMicrousd(weeklyLimitUsd);
|
|
113
|
+
const remainingMicrousd = Math.max(0, weeklyLimitMicrousd - usage.consumedMicrousd);
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
...usage,
|
|
117
|
+
weeklyLimitMicrousd,
|
|
118
|
+
remainingMicrousd,
|
|
119
|
+
weeklyLimitUsd: microusdToUsd(weeklyLimitMicrousd),
|
|
120
|
+
consumedUsd: microusdToUsd(usage.consumedMicrousd),
|
|
121
|
+
remainingUsd: microusdToUsd(remainingMicrousd)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { USAGE_FILE };
|
package/src/direct-api.js
CHANGED
|
@@ -219,6 +219,10 @@ export async function sendMessage(anthropicRequest, accessToken, accountId, cont
|
|
|
219
219
|
throw error;
|
|
220
220
|
}
|
|
221
221
|
|
|
222
|
+
if (response.status === 400) {
|
|
223
|
+
throw createHTTPError(`INVALID_REQUEST: ${errorText}`, 400, 'invalid_request_error', response.status);
|
|
224
|
+
}
|
|
225
|
+
|
|
222
226
|
throw createUpstreamHTTPError(response, errorText);
|
|
223
227
|
}
|
|
224
228
|
|
package/src/format-converter.js
CHANGED
|
@@ -146,11 +146,11 @@ export function convertAnthropicToResponsesAPI(anthropicRequest) {
|
|
|
146
146
|
const instructions = extractSystemPrompt(system);
|
|
147
147
|
|
|
148
148
|
const request = {
|
|
149
|
-
model: model
|
|
149
|
+
model: model,
|
|
150
150
|
input: convertMessagesToInput(cleanedMessages),
|
|
151
151
|
tools: tools ? convertAnthropicToolsToOpenAI(tools) : [],
|
|
152
|
-
tool_choice:
|
|
153
|
-
parallel_tool_calls: true,
|
|
152
|
+
tool_choice: 'auto',
|
|
153
|
+
parallel_tool_calls: tool_choice?.disable_parallel_tool_use !== true,
|
|
154
154
|
store: false,
|
|
155
155
|
stream: true,
|
|
156
156
|
include: []
|
|
@@ -177,23 +177,7 @@ function convertMessagesToInput(messages) {
|
|
|
177
177
|
|
|
178
178
|
for (const msg of messages) {
|
|
179
179
|
if (msg.role === 'user') {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (textParts.length > 0) {
|
|
183
|
-
// API accepts: string OR array of {type: 'input_text', text: '...'}
|
|
184
|
-
const content = textParts.length === 1
|
|
185
|
-
? textParts[0] // Use string for single text
|
|
186
|
-
: textParts.map(text => ({ type: 'input_text', text }));
|
|
187
|
-
input.push({
|
|
188
|
-
type: 'message',
|
|
189
|
-
role: 'user',
|
|
190
|
-
content
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
for (const result of toolResults) {
|
|
195
|
-
input.push(result);
|
|
196
|
-
}
|
|
180
|
+
input.push(...convertUserContent(msg.content));
|
|
197
181
|
} else if (msg.role === 'assistant') {
|
|
198
182
|
// Process assistant content: restore signatures, reorder, sanitize
|
|
199
183
|
let msgContent = msg.content;
|
|
@@ -225,34 +209,62 @@ function convertMessagesToInput(messages) {
|
|
|
225
209
|
}
|
|
226
210
|
|
|
227
211
|
/**
|
|
228
|
-
* Convert user content
|
|
212
|
+
* Convert Anthropic user content while preserving block order.
|
|
229
213
|
*/
|
|
230
214
|
function convertUserContent(content) {
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
215
|
+
const inputItems = [];
|
|
216
|
+
let messageParts = [];
|
|
217
|
+
|
|
218
|
+
const flushMessageParts = () => {
|
|
219
|
+
if (messageParts.length === 0) return;
|
|
220
|
+
|
|
221
|
+
const messageContent =
|
|
222
|
+
messageParts.length === 1 && messageParts[0].type === 'input_text'
|
|
223
|
+
? messageParts[0].text
|
|
224
|
+
: messageParts;
|
|
225
|
+
|
|
226
|
+
inputItems.push({
|
|
227
|
+
type: 'message',
|
|
228
|
+
role: 'user',
|
|
229
|
+
content: messageContent
|
|
230
|
+
});
|
|
231
|
+
messageParts = [];
|
|
232
|
+
};
|
|
233
|
+
|
|
234
234
|
if (typeof content === 'string') {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
textParts.push(block.text);
|
|
240
|
-
} else if (block.type === 'tool_result') {
|
|
241
|
-
const outputContent = convertToolResultContentToResponsesOutput(block);
|
|
235
|
+
messageParts.push({ type: 'input_text', text: content });
|
|
236
|
+
flushMessageParts();
|
|
237
|
+
return inputItems;
|
|
238
|
+
}
|
|
242
239
|
|
|
243
|
-
|
|
244
|
-
|
|
240
|
+
if (!Array.isArray(content)) {
|
|
241
|
+
return inputItems;
|
|
242
|
+
}
|
|
245
243
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
244
|
+
for (const block of content) {
|
|
245
|
+
if (!block || typeof block !== 'object') continue;
|
|
246
|
+
|
|
247
|
+
if (block.type === 'text') {
|
|
248
|
+
messageParts.push({ type: 'input_text', text: block.text || '' });
|
|
249
|
+
} else if (block.type === 'image') {
|
|
250
|
+
const image = convertAnthropicImageToResponsesInputImage(block);
|
|
251
|
+
if (image) messageParts.push(image);
|
|
252
|
+
} else if (block.type === 'tool_result') {
|
|
253
|
+
flushMessageParts();
|
|
254
|
+
|
|
255
|
+
const outputContent = convertToolResultContentToResponsesOutput(block);
|
|
256
|
+
const callId = toOpenAIToolId(block.tool_use_id);
|
|
257
|
+
|
|
258
|
+
inputItems.push({
|
|
259
|
+
type: 'function_call_output',
|
|
260
|
+
call_id: callId,
|
|
261
|
+
output: block.is_error ? prefixToolErrorOutput(outputContent) : outputContent
|
|
262
|
+
});
|
|
252
263
|
}
|
|
253
264
|
}
|
|
254
|
-
|
|
255
|
-
|
|
265
|
+
|
|
266
|
+
flushMessageParts();
|
|
267
|
+
return inputItems;
|
|
256
268
|
}
|
|
257
269
|
|
|
258
270
|
/**
|
package/src/model-pricing.js
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Upstream Codex model pricing in USD per one million tokens.
|
|
3
|
-
* Long-context pricing applies when input tokens exceed 272,000.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export const PRICE_UNIT_TOKENS = 1_000_000;
|
|
7
|
-
export const LONG_CONTEXT_THRESHOLD_TOKENS = 272_000;
|
|
8
|
-
|
|
9
|
-
const STANDARD_PRICING = {
|
|
10
|
-
'gpt-5.6-sol': {
|
|
11
|
-
input: 5,
|
|
12
|
-
cachedInput: 0.5,
|
|
13
|
-
cacheWrite: 6.25,
|
|
14
|
-
output: 30
|
|
15
|
-
},
|
|
16
|
-
'gpt-5.6-terra': {
|
|
17
|
-
input: 2.5,
|
|
18
|
-
cachedInput: 0.25,
|
|
19
|
-
cacheWrite: 3.125,
|
|
20
|
-
output: 15
|
|
21
|
-
},
|
|
22
|
-
'gpt-5.4-mini': {
|
|
23
|
-
input: 0.75,
|
|
24
|
-
cachedInput: 0.075,
|
|
25
|
-
cacheWrite: 0,
|
|
26
|
-
output: 4.5
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const LONG_CONTEXT_PRICING = {
|
|
31
|
-
'gpt-5.6-sol': {
|
|
32
|
-
input: 10,
|
|
33
|
-
cachedInput: 1,
|
|
34
|
-
cacheWrite: 12.5,
|
|
35
|
-
output: 45
|
|
36
|
-
},
|
|
37
|
-
'gpt-5.6-terra': {
|
|
38
|
-
input: 5,
|
|
39
|
-
cachedInput: 0.5,
|
|
40
|
-
cacheWrite: 6.25,
|
|
41
|
-
output: 22.5
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Returns the applicable USD-per-million-token price record for an upstream
|
|
47
|
-
* model. Models without a configured price return null.
|
|
48
|
-
*/
|
|
49
|
-
export function getModelPricing(model, inputTokens = 0) {
|
|
50
|
-
const normalizedModel = String(model || '').toLowerCase();
|
|
51
|
-
const standard = STANDARD_PRICING[normalizedModel];
|
|
52
|
-
if (!standard) return null;
|
|
53
|
-
|
|
54
|
-
const usesLongContext = Number(inputTokens) > LONG_CONTEXT_THRESHOLD_TOKENS;
|
|
55
|
-
const longContext = usesLongContext ? LONG_CONTEXT_PRICING[normalizedModel] : null;
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
...standard,
|
|
59
|
-
...(longContext || {}),
|
|
60
|
-
model: normalizedModel,
|
|
61
|
-
priceUnitTokens: PRICE_UNIT_TOKENS,
|
|
62
|
-
longContext: Boolean(longContext)
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Calculates a request charge from Responses API usage and returns it in
|
|
68
|
-
* integer microdollars. Cached input and cache-write tokens are included in
|
|
69
|
-
* input_tokens, so both are removed from standard input before applying their
|
|
70
|
-
* respective prices.
|
|
71
|
-
*/
|
|
72
|
-
export function calculateUsageCostMicrousd(model, usage = {}) {
|
|
73
|
-
const inputTokens = Math.max(0, Number(usage.input_tokens) || 0);
|
|
74
|
-
const outputTokens = Math.max(0, Number(usage.output_tokens) || 0);
|
|
75
|
-
const cachedInputTokens = Math.min(
|
|
76
|
-
inputTokens,
|
|
77
|
-
Math.max(0, Number(usage.cache_read_input_tokens ?? usage.input_tokens_details?.cached_tokens) || 0)
|
|
78
|
-
);
|
|
79
|
-
const cacheWriteTokens = Math.max(0, Number(usage.input_tokens_details?.cache_write_tokens) || 0);
|
|
80
|
-
const pricing = getModelPricing(model, inputTokens);
|
|
81
|
-
if (!pricing) return null;
|
|
82
|
-
|
|
83
|
-
const uncachedInputTokens = Math.max(0, inputTokens - cachedInputTokens - cacheWriteTokens);
|
|
84
|
-
const usd = (
|
|
85
|
-
(uncachedInputTokens * pricing.input)
|
|
86
|
-
+ (cachedInputTokens * pricing.cachedInput)
|
|
87
|
-
+ (cacheWriteTokens * pricing.cacheWrite)
|
|
88
|
-
+ (outputTokens * pricing.output)
|
|
89
|
-
) / PRICE_UNIT_TOKENS;
|
|
90
|
-
|
|
91
|
-
return Math.round(usd * 1_000_000);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export { STANDARD_PRICING, LONG_CONTEXT_PRICING };
|
|
95
|
-
|
|
96
|
-
export default { getModelPricing, calculateUsageCostMicrousd };
|
|
1
|
+
/**
|
|
2
|
+
* Upstream Codex model pricing in USD per one million tokens.
|
|
3
|
+
* Long-context pricing applies when input tokens exceed 272,000.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const PRICE_UNIT_TOKENS = 1_000_000;
|
|
7
|
+
export const LONG_CONTEXT_THRESHOLD_TOKENS = 272_000;
|
|
8
|
+
|
|
9
|
+
const STANDARD_PRICING = {
|
|
10
|
+
'gpt-5.6-sol': {
|
|
11
|
+
input: 5,
|
|
12
|
+
cachedInput: 0.5,
|
|
13
|
+
cacheWrite: 6.25,
|
|
14
|
+
output: 30
|
|
15
|
+
},
|
|
16
|
+
'gpt-5.6-terra': {
|
|
17
|
+
input: 2.5,
|
|
18
|
+
cachedInput: 0.25,
|
|
19
|
+
cacheWrite: 3.125,
|
|
20
|
+
output: 15
|
|
21
|
+
},
|
|
22
|
+
'gpt-5.4-mini': {
|
|
23
|
+
input: 0.75,
|
|
24
|
+
cachedInput: 0.075,
|
|
25
|
+
cacheWrite: 0,
|
|
26
|
+
output: 4.5
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const LONG_CONTEXT_PRICING = {
|
|
31
|
+
'gpt-5.6-sol': {
|
|
32
|
+
input: 10,
|
|
33
|
+
cachedInput: 1,
|
|
34
|
+
cacheWrite: 12.5,
|
|
35
|
+
output: 45
|
|
36
|
+
},
|
|
37
|
+
'gpt-5.6-terra': {
|
|
38
|
+
input: 5,
|
|
39
|
+
cachedInput: 0.5,
|
|
40
|
+
cacheWrite: 6.25,
|
|
41
|
+
output: 22.5
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns the applicable USD-per-million-token price record for an upstream
|
|
47
|
+
* model. Models without a configured price return null.
|
|
48
|
+
*/
|
|
49
|
+
export function getModelPricing(model, inputTokens = 0) {
|
|
50
|
+
const normalizedModel = String(model || '').toLowerCase();
|
|
51
|
+
const standard = STANDARD_PRICING[normalizedModel];
|
|
52
|
+
if (!standard) return null;
|
|
53
|
+
|
|
54
|
+
const usesLongContext = Number(inputTokens) > LONG_CONTEXT_THRESHOLD_TOKENS;
|
|
55
|
+
const longContext = usesLongContext ? LONG_CONTEXT_PRICING[normalizedModel] : null;
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
...standard,
|
|
59
|
+
...(longContext || {}),
|
|
60
|
+
model: normalizedModel,
|
|
61
|
+
priceUnitTokens: PRICE_UNIT_TOKENS,
|
|
62
|
+
longContext: Boolean(longContext)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Calculates a request charge from Responses API usage and returns it in
|
|
68
|
+
* integer microdollars. Cached input and cache-write tokens are included in
|
|
69
|
+
* input_tokens, so both are removed from standard input before applying their
|
|
70
|
+
* respective prices.
|
|
71
|
+
*/
|
|
72
|
+
export function calculateUsageCostMicrousd(model, usage = {}) {
|
|
73
|
+
const inputTokens = Math.max(0, Number(usage.input_tokens) || 0);
|
|
74
|
+
const outputTokens = Math.max(0, Number(usage.output_tokens) || 0);
|
|
75
|
+
const cachedInputTokens = Math.min(
|
|
76
|
+
inputTokens,
|
|
77
|
+
Math.max(0, Number(usage.cache_read_input_tokens ?? usage.input_tokens_details?.cached_tokens) || 0)
|
|
78
|
+
);
|
|
79
|
+
const cacheWriteTokens = Math.max(0, Number(usage.input_tokens_details?.cache_write_tokens) || 0);
|
|
80
|
+
const pricing = getModelPricing(model, inputTokens);
|
|
81
|
+
if (!pricing) return null;
|
|
82
|
+
|
|
83
|
+
const uncachedInputTokens = Math.max(0, inputTokens - cachedInputTokens - cacheWriteTokens);
|
|
84
|
+
const usd = (
|
|
85
|
+
(uncachedInputTokens * pricing.input)
|
|
86
|
+
+ (cachedInputTokens * pricing.cachedInput)
|
|
87
|
+
+ (cacheWriteTokens * pricing.cacheWrite)
|
|
88
|
+
+ (outputTokens * pricing.output)
|
|
89
|
+
) / PRICE_UNIT_TOKENS;
|
|
90
|
+
|
|
91
|
+
return Math.round(usd * 1_000_000);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { STANDARD_PRICING, LONG_CONTEXT_PRICING };
|
|
95
|
+
|
|
96
|
+
export default { getModelPricing, calculateUsageCostMicrousd };
|
|
@@ -131,7 +131,12 @@ export async function handleMessages(req, res) {
|
|
|
131
131
|
rotator.markInvalid(account.email, 'Auth expired');
|
|
132
132
|
continue;
|
|
133
133
|
}
|
|
134
|
-
|
|
134
|
+
|
|
135
|
+
if (error.status === 400 || error.anthropicErrorType === 'invalid_request_error') {
|
|
136
|
+
logger.info(`[Messages] Invalid request on ${account.email}; stopping account rotation`);
|
|
137
|
+
return handleStreamError(res, error, upstreamModel, startTime);
|
|
138
|
+
}
|
|
139
|
+
|
|
135
140
|
return handleStreamError(res, error, upstreamModel, startTime);
|
|
136
141
|
}
|
|
137
142
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getServerSettings } from '../server-settings.js';
|
|
2
|
-
|
|
3
|
-
export function shouldLogRawProxyPayloads() {
|
|
4
|
-
return getServerSettings().logRawProxyPayloads === true;
|
|
5
|
-
}
|
|
1
|
+
import { getServerSettings } from '../server-settings.js';
|
|
2
|
+
|
|
3
|
+
export function shouldLogRawProxyPayloads() {
|
|
4
|
+
return getServerSettings().logRawProxyPayloads === true;
|
|
5
|
+
}
|