claude-codex-proxy 1.0.5-beta.9 → 1.0.5
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/README.md +6 -55
- package/bin/cli.js +0 -71
- package/docs/API.md +0 -22
- package/package.json +2 -3
- package/public/css/style.css +9 -210
- package/public/index.html +811 -32
- package/public/js/app.js +617 -104
- package/src/account-rotation/rate-limits.js +26 -0
- package/src/claude-config.js +18 -0
- package/src/direct-api.js +13 -12
- package/src/index.js +8 -26
- package/src/response-streamer.js +5 -5
- package/src/routes/api-routes.js +9 -44
- package/src/routes/chat-route.js +4 -25
- package/src/routes/claude-config-route.js +10 -54
- package/src/routes/logs-route.js +2 -21
- package/src/routes/messages-route.js +6 -30
- package/src/routes/settings-route.js +2 -131
- package/src/server-settings.js +3 -20
- package/src/server.js +6 -10
- package/src/utils/logger.js +0 -18
- package/public/login.html +0 -32
- package/public/views/accounts-modals.html +0 -159
- package/public/views/accounts.html +0 -183
- package/public/views/client-api-keys-modals.html +0 -55
- package/public/views/client-api-keys.html +0 -69
- package/public/views/dashboard.html +0 -90
- package/public/views/empty.html +0 -0
- package/public/views/logs.html +0 -99
- package/public/views/settings.html +0 -123
- package/src/client-key-usage.js +0 -125
- package/src/deployment.js +0 -49
- package/src/middleware/auth.js +0 -115
- package/src/model-pricing.js +0 -96
- package/src/routes/client-quota-route.js +0 -25
- package/src/utils/proxy-logging.js +0 -5
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
<div x-show="activeTab === 'settings'" x-transition class="view-container">
|
|
2
|
-
<div class="flex items-center justify-between gap-4 mb-6">
|
|
3
|
-
<div class="flex flex-wrap items-center gap-4">
|
|
4
|
-
<h1 class="text-2xl font-bold text-white tracking-tight">Settings</h1>
|
|
5
|
-
<div class="flex items-center h-6 px-3 rounded-full bg-space-800/80 border border-space-border/50 shadow-sm backdrop-blur-sm">
|
|
6
|
-
<span class="text-[10px] font-mono text-gray-400 uppercase tracking-wider">Server configuration</span>
|
|
7
|
-
</div>
|
|
8
|
-
</div>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div class="view-card">
|
|
12
|
-
<div class="flex items-center gap-2.5 mb-4">
|
|
13
|
-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-4 h-4 text-neon-cyan">
|
|
14
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
|
|
15
|
-
</svg>
|
|
16
|
-
<h3 class="text-xs font-mono text-gray-400 uppercase tracking-widest">Server Info</h3>
|
|
17
|
-
</div>
|
|
18
|
-
<div class="space-y-3">
|
|
19
|
-
<div class="flex items-center justify-between py-2 border-b border-space-border/30">
|
|
20
|
-
<span class="text-sm text-gray-400">Server URL</span>
|
|
21
|
-
<span class="font-mono text-sm text-white" x-text="serverUrl"></span>
|
|
22
|
-
</div>
|
|
23
|
-
<div class="flex items-center justify-between py-2 border-b border-space-border/30">
|
|
24
|
-
<span class="text-sm text-gray-400">Config Path</span>
|
|
25
|
-
<span class="font-mono text-sm text-white" x-text="configPath"></span>
|
|
26
|
-
</div>
|
|
27
|
-
<div class="flex items-center justify-between py-2">
|
|
28
|
-
<span class="text-sm text-gray-400">Auto Token Refresh</span>
|
|
29
|
-
<span class="text-xs font-mono text-neon-green">Every 55 minutes</span>
|
|
30
|
-
</div>
|
|
31
|
-
</div>
|
|
32
|
-
</div>
|
|
33
|
-
|
|
34
|
-
<div class="view-card mt-6">
|
|
35
|
-
<div class="flex items-center gap-2.5 mb-4">
|
|
36
|
-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-4 h-4 text-yellow-400">
|
|
37
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
|
|
38
|
-
</svg>
|
|
39
|
-
<h3 class="text-xs font-mono text-gray-400 uppercase tracking-widest">Raw Proxy Payload Logs</h3>
|
|
40
|
-
</div>
|
|
41
|
-
<div class="flex flex-wrap items-center justify-between gap-4">
|
|
42
|
-
<div>
|
|
43
|
-
<div class="text-sm text-gray-300">Capture raw request/response payloads</div>
|
|
44
|
-
<div class="text-xs text-gray-500 font-mono">Logs Claude Code requests, OpenAI/Codex upstream requests, completed responses, and upstream errors. May include prompts and tool results.</div>
|
|
45
|
-
</div>
|
|
46
|
-
<label class="flex items-center gap-3 cursor-pointer select-none">
|
|
47
|
-
<span class="text-xs font-mono" :class="logRawProxyPayloads ? 'text-yellow-400' : 'text-gray-500'" x-text="logRawProxyPayloads ? 'ENABLED' : 'DISABLED'"></span>
|
|
48
|
-
<input type="checkbox" class="toggle toggle-warning" :checked="logRawProxyPayloads" @change="setRawProxyLogs($event.target.checked)" :disabled="rawProxyLogsSaving">
|
|
49
|
-
</label>
|
|
50
|
-
</div>
|
|
51
|
-
</div>
|
|
52
|
-
|
|
53
|
-
<div class="view-card mt-6">
|
|
54
|
-
<div class="flex flex-wrap items-center justify-between gap-4 mb-4">
|
|
55
|
-
<div class="flex items-center gap-2.5">
|
|
56
|
-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-4 h-4 text-neon-cyan">
|
|
57
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7h5l2 3h11M3 17h5l2-3h11M14 4l3 3-3 3M14 20l3-3-3-3"></path>
|
|
58
|
-
</svg>
|
|
59
|
-
<h3 class="text-xs font-mono text-gray-400 uppercase tracking-widest">Model Mapping</h3>
|
|
60
|
-
</div>
|
|
61
|
-
<div class="flex flex-wrap items-center gap-2">
|
|
62
|
-
<button class="btn btn-xs btn-outline text-gray-400" @click="resetModelMapSetting()" :disabled="modelMapSaving">Reset Defaults</button>
|
|
63
|
-
<button class="btn btn-xs bg-neon-purple hover:bg-purple-600 border-none text-white" @click="saveModelMapSetting()" :disabled="modelMapSaving">Save Mapping</button>
|
|
64
|
-
</div>
|
|
65
|
-
</div>
|
|
66
|
-
|
|
67
|
-
<template x-if="modelMapLoading">
|
|
68
|
-
<div class="text-xs text-gray-500 font-mono">Loading model mapping...</div>
|
|
69
|
-
</template>
|
|
70
|
-
|
|
71
|
-
<div class="grid gap-3">
|
|
72
|
-
<div class="grid grid-cols-1 md:grid-cols-[160px_1fr] gap-2 md:items-center">
|
|
73
|
-
<div>
|
|
74
|
-
<div class="text-sm text-gray-300">Claude Opus</div>
|
|
75
|
-
<div class="text-xs text-gray-500 font-mono">opus</div>
|
|
76
|
-
</div>
|
|
77
|
-
<input type="text" x-model="modelMappings.opus" placeholder="gpt-5.5"
|
|
78
|
-
class="input bg-space-800 border-space-border text-white font-mono text-sm">
|
|
79
|
-
</div>
|
|
80
|
-
|
|
81
|
-
<div class="grid grid-cols-1 md:grid-cols-[160px_1fr] gap-2 md:items-center">
|
|
82
|
-
<div>
|
|
83
|
-
<div class="text-sm text-gray-300">Claude Sonnet</div>
|
|
84
|
-
<div class="text-xs text-gray-500 font-mono">sonnet</div>
|
|
85
|
-
</div>
|
|
86
|
-
<input type="text" x-model="modelMappings.sonnet" placeholder="gpt-5.5"
|
|
87
|
-
class="input bg-space-800 border-space-border text-white font-mono text-sm">
|
|
88
|
-
</div>
|
|
89
|
-
|
|
90
|
-
<div class="grid grid-cols-1 md:grid-cols-[160px_1fr] gap-2 md:items-center">
|
|
91
|
-
<div>
|
|
92
|
-
<div class="text-sm text-gray-300">Claude Haiku</div>
|
|
93
|
-
<div class="text-xs text-gray-500 font-mono">haiku</div>
|
|
94
|
-
</div>
|
|
95
|
-
<input type="text" x-model="modelMappings.haiku" placeholder="gpt-5.4-mini"
|
|
96
|
-
class="input bg-space-800 border-space-border text-white font-mono text-sm">
|
|
97
|
-
</div>
|
|
98
|
-
|
|
99
|
-
<div class="text-xs text-gray-500 font-mono">Map each Claude family to a GPT/Codex model available to your ChatGPT account.</div>
|
|
100
|
-
</div>
|
|
101
|
-
</div>
|
|
102
|
-
|
|
103
|
-
<div class="view-card mt-6">
|
|
104
|
-
<div class="flex items-center gap-2.5 mb-4">
|
|
105
|
-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-4 h-4 text-neon-purple">
|
|
106
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path>
|
|
107
|
-
</svg>
|
|
108
|
-
<h3 class="text-xs font-mono text-gray-400 uppercase tracking-widest">Account Selection Strategy</h3>
|
|
109
|
-
</div>
|
|
110
|
-
<div class="flex flex-wrap items-center justify-between gap-4">
|
|
111
|
-
<div>
|
|
112
|
-
<div class="text-sm text-gray-300">Strategy</div>
|
|
113
|
-
<div class="text-xs text-gray-500 font-mono">How accounts are selected when rate-limited</div>
|
|
114
|
-
</div>
|
|
115
|
-
<div class="inline-flex items-center gap-2">
|
|
116
|
-
<button class="btn btn-xs" :class="accountStrategy === 'sticky' ? 'bg-neon-purple text-white' : 'btn-outline text-gray-400'"
|
|
117
|
-
@click="setAccountStrategy('sticky')" :disabled="strategySaving">Sticky</button>
|
|
118
|
-
<button class="btn btn-xs" :class="accountStrategy === 'round-robin' ? 'bg-neon-purple text-white' : 'btn-outline text-gray-400'"
|
|
119
|
-
@click="setAccountStrategy('round-robin')" :disabled="strategySaving">Round-Robin</button>
|
|
120
|
-
</div>
|
|
121
|
-
</div>
|
|
122
|
-
</div>
|
|
123
|
-
</div>
|
package/src/client-key-usage.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
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/deployment.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Deployment policy helpers shared by server, routes and auth middleware.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const VALID_DEPLOYMENT_MODES = new Set(['local', 'public']);
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Returns the validated deployment mode, defaulting to local when unset.
|
|
9
|
-
* @returns {'local'|'public'}
|
|
10
|
-
*/
|
|
11
|
-
export function getDeploymentMode() {
|
|
12
|
-
const mode = process.env.PROXY_DEPLOYMENT_MODE || 'local';
|
|
13
|
-
if (!VALID_DEPLOYMENT_MODES.has(mode)) {
|
|
14
|
-
throw new Error(`Invalid PROXY_DEPLOYMENT_MODE "${mode}". Expected "local" or "public".`);
|
|
15
|
-
}
|
|
16
|
-
return mode;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Indicates whether the proxy is running in public deployment mode.
|
|
21
|
-
* @returns {boolean}
|
|
22
|
-
*/
|
|
23
|
-
export function isPublicDeployment() {
|
|
24
|
-
return getDeploymentMode() === 'public';
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Returns the host address the main HTTP server should bind to.
|
|
29
|
-
* @returns {string}
|
|
30
|
-
*/
|
|
31
|
-
export function getServerHost() {
|
|
32
|
-
if (process.env.HOST) {
|
|
33
|
-
return process.env.HOST;
|
|
34
|
-
}
|
|
35
|
-
return isPublicDeployment() ? '0.0.0.0' : '127.0.0.1';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns a display URL for logs and local helper responses.
|
|
40
|
-
* @param {number|string} port
|
|
41
|
-
* @returns {string}
|
|
42
|
-
*/
|
|
43
|
-
export function getLocalBaseUrl(port) {
|
|
44
|
-
const host = getServerHost();
|
|
45
|
-
const displayHost = host === '0.0.0.0' || host === '::' ? 'localhost' : host;
|
|
46
|
-
return `http://${displayHost}:${port}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export default { getDeploymentMode, isPublicDeployment, getServerHost, getLocalBaseUrl };
|
package/src/middleware/auth.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import crypto from 'crypto';
|
|
2
|
-
import { isPublicDeployment } from '../deployment.js';
|
|
3
|
-
import { getServerSettings, DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD } from '../server-settings.js';
|
|
4
|
-
import { getClientKeyQuota } from '../client-key-usage.js';
|
|
5
|
-
|
|
6
|
-
function timingSafeEqualString(a, b) {
|
|
7
|
-
const left = Buffer.from(String(a));
|
|
8
|
-
const right = Buffer.from(String(b));
|
|
9
|
-
|
|
10
|
-
if (left.length !== right.length) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return crypto.timingSafeEqual(left, right);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getRequestApiKey(req) {
|
|
18
|
-
const authorization = req.get?.('authorization') || req.headers?.authorization || '';
|
|
19
|
-
const bearerMatch = authorization.match(/^Bearer\s+(.+)$/i);
|
|
20
|
-
if (bearerMatch) {
|
|
21
|
-
return bearerMatch[1].trim();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const headerKey = req.get?.('x-api-key') || req.headers?.['x-api-key'];
|
|
25
|
-
if (headerKey) {
|
|
26
|
-
return String(headerKey).trim();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const queryKey = req.query?.api_key || req.query?.access_token;
|
|
30
|
-
if (queryKey) {
|
|
31
|
-
return String(queryKey).trim();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return '';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function sendUnauthorized(req, res) {
|
|
38
|
-
if (req.path?.startsWith('/v1/')) {
|
|
39
|
-
return res.status(401).json({
|
|
40
|
-
type: 'error',
|
|
41
|
-
error: { type: 'authentication_error', message: 'Invalid API key' }
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return res.status(401).json({ success: false, error: 'Unauthorized' });
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function matchesKey(requestKey, expectedKey) {
|
|
49
|
-
return expectedKey && requestKey && timingSafeEqualString(requestKey, expectedKey);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function requireKeys(getExpectedKeys) {
|
|
53
|
-
return (req, res, next) => {
|
|
54
|
-
if (!isPublicDeployment()) {
|
|
55
|
-
return next();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const expectedKeys = getExpectedKeys();
|
|
59
|
-
const requestKey = getRequestApiKey(req);
|
|
60
|
-
if (!expectedKeys.some((expectedKey) => matchesKey(requestKey, expectedKey))) {
|
|
61
|
-
return sendUnauthorized(req, res);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return next();
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function sendQuotaExceeded(res, quota) {
|
|
69
|
-
return res.status(429).json({
|
|
70
|
-
type: 'error',
|
|
71
|
-
error: {
|
|
72
|
-
type: 'rate_limit_error',
|
|
73
|
-
message: `Weekly client key quota exhausted. Resets at ${quota.nextPeriodStart}`
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function findClientKey(requestKey) {
|
|
79
|
-
return getServerSettings().clientApiKeys.find(({ key }) => matchesKey(requestKey, key));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const getAdminKeys = () => [process.env.PROXY_ADMIN_KEY].filter(Boolean);
|
|
83
|
-
|
|
84
|
-
export const requireAdminAuth = requireKeys(getAdminKeys);
|
|
85
|
-
|
|
86
|
-
export function requireClientAuth(req, res, next) {
|
|
87
|
-
const requestKey = getRequestApiKey(req);
|
|
88
|
-
const clientKey = findClientKey(requestKey);
|
|
89
|
-
if (isPublicDeployment() && !clientKey) {
|
|
90
|
-
return sendUnauthorized(req, res);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (!clientKey) {
|
|
94
|
-
return next();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const quota = getClientKeyQuota(
|
|
98
|
-
clientKey.id,
|
|
99
|
-
clientKey.weeklyLimitUsd ?? DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD
|
|
100
|
-
);
|
|
101
|
-
const isModelRequest = req.method === 'POST' && (
|
|
102
|
-
req.path === '/v1/messages' || req.path === '/v1/chat/completions'
|
|
103
|
-
);
|
|
104
|
-
if (isModelRequest && quota.remainingMicrousd <= 0) {
|
|
105
|
-
return sendQuotaExceeded(res, quota);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
req.clientApiKey = { id: clientKey.id, name: clientKey.name };
|
|
109
|
-
req.clientApiKeyQuota = quota;
|
|
110
|
-
return next();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export { isPublicDeployment };
|
|
114
|
-
|
|
115
|
-
export default { isPublicDeployment, requireAdminAuth, requireClientAuth };
|
package/src/model-pricing.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
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,25 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD } from '../server-settings.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Returns the authenticated client key's weekly quota using Claude Code's
|
|
5
|
-
* utilization response shape, so the terminal can consume it directly.
|
|
6
|
-
*/
|
|
7
|
-
export function handleGetClientQuota(req, res) {
|
|
8
|
-
const quota = req.clientApiKeyQuota;
|
|
9
|
-
const weeklyLimitUsd = quota?.weeklyLimitUsd ?? DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD;
|
|
10
|
-
const utilization = quota && weeklyLimitUsd > 0
|
|
11
|
-
? Math.min(100, Math.max(0, (quota.consumedUsd / weeklyLimitUsd) * 100))
|
|
12
|
-
: null;
|
|
13
|
-
|
|
14
|
-
return res.json({
|
|
15
|
-
five_hour: null,
|
|
16
|
-
seven_day: quota ? {
|
|
17
|
-
utilization,
|
|
18
|
-
resets_at: quota.nextPeriodStart
|
|
19
|
-
} : null,
|
|
20
|
-
seven_day_oauth_apps: null,
|
|
21
|
-
seven_day_opus: null,
|
|
22
|
-
seven_day_sonnet: null,
|
|
23
|
-
extra_usage: null
|
|
24
|
-
});
|
|
25
|
-
}
|