claude-codex-proxy 1.0.5-beta.15 → 1.0.5-beta.16
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/format-converter.js +24 -7
- package/src/model-pricing.js +96 -96
- package/src/response-streamer.js +12 -2
- 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/format-converter.js
CHANGED
|
@@ -322,19 +322,36 @@ function convertAssistantContentToOpenAI(content) {
|
|
|
322
322
|
}
|
|
323
323
|
|
|
324
324
|
/**
|
|
325
|
-
* Convert Anthropic tools to OpenAI
|
|
325
|
+
* Convert Anthropic tools to OpenAI Responses API tool format.
|
|
326
326
|
*/
|
|
327
327
|
function convertAnthropicToolsToOpenAI(tools) {
|
|
328
328
|
if (!Array.isArray(tools)) {
|
|
329
329
|
return [];
|
|
330
330
|
}
|
|
331
331
|
|
|
332
|
-
return tools.map(tool =>
|
|
333
|
-
type
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
332
|
+
return tools.map(tool => {
|
|
333
|
+
if (tool.type === 'web_search_20250305' && tool.name === 'web_search') {
|
|
334
|
+
const webSearchTool = {
|
|
335
|
+
type: 'web_search',
|
|
336
|
+
external_web_access: true
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
if (Array.isArray(tool.allowed_domains)) {
|
|
340
|
+
webSearchTool.filters = {
|
|
341
|
+
allowed_domains: tool.allowed_domains
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return webSearchTool;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
type: 'function',
|
|
350
|
+
name: tool.name,
|
|
351
|
+
description: tool.description || '',
|
|
352
|
+
parameters: sanitizeSchema(tool.input_schema || { type: 'object' })
|
|
353
|
+
};
|
|
354
|
+
});
|
|
338
355
|
}
|
|
339
356
|
|
|
340
357
|
function sanitizeAnthropicToolInput(name, input) {
|
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 };
|
package/src/response-streamer.js
CHANGED
|
@@ -122,7 +122,13 @@ export async function* streamResponsesAPI(response, model) {
|
|
|
122
122
|
// Handle output item added
|
|
123
123
|
if (eventType === 'response.output_item.added') {
|
|
124
124
|
const item = event.item;
|
|
125
|
-
|
|
125
|
+
|
|
126
|
+
// Codex executes hosted web searches upstream. Claude Code only
|
|
127
|
+
// needs the final output_text, not a client-side tool_use block.
|
|
128
|
+
if (item?.type === 'web_search_call') {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
126
132
|
if (!hasEmittedStart) {
|
|
127
133
|
hasEmittedStart = true;
|
|
128
134
|
yield {
|
|
@@ -162,9 +168,13 @@ export async function* streamResponsesAPI(response, model) {
|
|
|
162
168
|
data: { type: 'content_block_stop', index: blockIndex }
|
|
163
169
|
};
|
|
164
170
|
blockIndex++;
|
|
171
|
+
currentBlockType = null;
|
|
172
|
+
currentBlockId = null;
|
|
173
|
+
currentToolName = null;
|
|
165
174
|
}
|
|
166
175
|
|
|
167
|
-
// Start new block based on item type
|
|
176
|
+
// Start new block based on item type. Hosted web_search_call
|
|
177
|
+
// items are executed upstream and do not become Claude tool_use blocks.
|
|
168
178
|
if (item.type === 'message') {
|
|
169
179
|
currentBlockType = 'text';
|
|
170
180
|
currentBlockId = item.id;
|
|
@@ -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
|
+
}
|