claude-codex-proxy 1.0.5-beta.12 → 1.0.5-beta.2
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 +272 -0
- package/bin/cli.js +2 -37
- package/package.json +1 -1
- package/public/css/style.css +9 -210
- package/public/index.html +926 -32
- package/public/js/app.js +744 -108
- package/src/account-rotation/rate-limits.js +26 -0
- package/src/claude-config.js +18 -0
- package/src/deployment.js +49 -49
- package/src/direct-api.js +13 -12
- package/src/index.js +4 -13
- package/src/middleware/auth.js +11 -46
- package/src/model-api.js +0 -60
- package/src/response-streamer.js +5 -5
- package/src/routes/accounts-route.js +1 -78
- package/src/routes/api-routes.js +35 -59
- package/src/routes/chat-route.js +4 -25
- package/src/routes/claude-config-route.js +15 -22
- package/src/routes/logs-route.js +2 -21
- package/src/routes/messages-route.js +6 -30
- package/src/routes/settings-route.js +5 -36
- package/src/server-settings.js +2 -18
- package/src/utils/logger.js +0 -18
- package/public/login.html +0 -32
- package/public/views/accounts-modals.html +0 -196
- 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/admin-path.js +0 -33
- package/src/client-key-usage.js +0 -125
- package/src/model-pricing.js +0 -96
- package/src/routes/client-quota-route.js +0 -25
- package/src/utils/proxy-logging.js +0 -5
package/public/views/empty.html
DELETED
|
File without changes
|
package/public/views/logs.html
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
<div x-show="activeTab === 'logs'" x-transition class="view-container h-full flex flex-col">
|
|
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">Server Logs</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">Real-time log stream</span>
|
|
7
|
-
</div>
|
|
8
|
-
</div>
|
|
9
|
-
<div class="flex items-center gap-3">
|
|
10
|
-
<div class="text-[10px] font-mono text-gray-600">
|
|
11
|
-
<span x-text="logTotal"></span> entries
|
|
12
|
-
</div>
|
|
13
|
-
<button class="btn btn-xs btn-ghost text-gray-400 hover:text-white" @click="clearLogs()" title="Clear logs">
|
|
14
|
-
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
15
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
|
|
16
|
-
</svg>
|
|
17
|
-
</button>
|
|
18
|
-
</div>
|
|
19
|
-
</div>
|
|
20
|
-
|
|
21
|
-
<div class="view-card !p-0 flex flex-col flex-1 min-h-0">
|
|
22
|
-
<div class="bg-space-900 flex items-center p-2 px-4 border-b border-space-border gap-4">
|
|
23
|
-
<div class="flex gap-2">
|
|
24
|
-
<div class="w-3 h-3 rounded-full bg-red-500/20 border border-red-500/50"></div>
|
|
25
|
-
<div class="w-3 h-3 rounded-full bg-yellow-500/20 border border-yellow-500/50"></div>
|
|
26
|
-
<div class="w-3 h-3 rounded-full bg-green-500/20 border border-green-500/50"></div>
|
|
27
|
-
</div>
|
|
28
|
-
<span class="text-xs font-mono text-gray-500 hidden sm:inline-block">~/logs</span>
|
|
29
|
-
|
|
30
|
-
<div class="flex-1 flex items-center justify-center gap-4">
|
|
31
|
-
<div class="relative w-full max-w-xs">
|
|
32
|
-
<input type="text" x-model="logSearchQuery" placeholder="Search logs..."
|
|
33
|
-
class="w-full h-7 bg-space-950 border border-space-border rounded text-xs font-mono pl-7 pr-2 focus:border-neon-purple focus:outline-none transition-colors placeholder-gray-700 text-gray-300">
|
|
34
|
-
<svg class="w-3 h-3 absolute left-2.5 top-2 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
35
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
|
|
36
|
-
</svg>
|
|
37
|
-
</div>
|
|
38
|
-
|
|
39
|
-
<div class="hidden md:flex gap-3 text-[10px] font-mono font-bold uppercase select-none">
|
|
40
|
-
<label class="flex items-center gap-1.5 cursor-pointer text-blue-400 opacity-50 hover:opacity-100 transition-opacity" :class="{'opacity-100': logFilters.INFO}">
|
|
41
|
-
<input type="checkbox" class="checkbox checkbox-xs rounded-[2px] w-3 h-3" x-model="logFilters.INFO"> INFO
|
|
42
|
-
</label>
|
|
43
|
-
<label class="flex items-center gap-1.5 cursor-pointer text-green-400 opacity-50 hover:opacity-100 transition-opacity" :class="{'opacity-100': logFilters.SUCCESS}">
|
|
44
|
-
<input type="checkbox" class="checkbox checkbox-xs rounded-[2px] w-3 h-3" x-model="logFilters.SUCCESS"> SUCCESS
|
|
45
|
-
</label>
|
|
46
|
-
<label class="flex items-center gap-1.5 cursor-pointer text-yellow-400 opacity-50 hover:opacity-100 transition-opacity" :class="{'opacity-100': logFilters.WARN}">
|
|
47
|
-
<input type="checkbox" class="checkbox checkbox-xs rounded-[2px] w-3 h-3" x-model="logFilters.WARN"> WARN
|
|
48
|
-
</label>
|
|
49
|
-
<label class="flex items-center gap-1.5 cursor-pointer text-red-500 opacity-50 hover:opacity-100 transition-opacity" :class="{'opacity-100': logFilters.ERROR}">
|
|
50
|
-
<input type="checkbox" class="checkbox checkbox-xs rounded-[2px] w-3 h-3" x-model="logFilters.ERROR"> ERROR
|
|
51
|
-
</label>
|
|
52
|
-
</div>
|
|
53
|
-
</div>
|
|
54
|
-
</div>
|
|
55
|
-
|
|
56
|
-
<div id="logs-container" class="flex-1 overflow-auto p-4 font-mono text-[11px] leading-relaxed bg-space-950 custom-scrollbar">
|
|
57
|
-
<template x-for="(log, idx) in filteredLogs" :key="`${log.timestamp}-${idx}`">
|
|
58
|
-
<div class="flex gap-3 px-2 py-1 -mx-2 hover:bg-white/[0.03] transition-colors group border-b border-space-border/10">
|
|
59
|
-
<span class="text-zinc-600 w-16 shrink-0 select-none group-hover:text-zinc-500 transition-colors"
|
|
60
|
-
x-text="new Date(log.timestamp).toLocaleTimeString([], {hour12:false})"></span>
|
|
61
|
-
<div class="w-16 shrink-0 flex items-center">
|
|
62
|
-
<span class="px-1.5 py-0.5 rounded-[2px] text-[10px] font-bold uppercase tracking-wider leading-none border"
|
|
63
|
-
:class="{
|
|
64
|
-
'bg-blue-500/10 text-blue-400 border-blue-500/20': log.level === 'INFO',
|
|
65
|
-
'bg-yellow-500/10 text-yellow-400 border-yellow-500/20': log.level === 'WARN',
|
|
66
|
-
'bg-red-500/10 text-red-500 border-red-500/20': log.level === 'ERROR',
|
|
67
|
-
'bg-emerald-500/10 text-emerald-400 border-emerald-500/20': log.level === 'SUCCESS',
|
|
68
|
-
'bg-purple-500/10 text-purple-400 border-purple-500/20': log.level === 'DEBUG'
|
|
69
|
-
}" x-text="log.level"></span>
|
|
70
|
-
</div>
|
|
71
|
-
<div class="flex-1 flex flex-col gap-0.5 min-w-0">
|
|
72
|
-
<span class="text-zinc-200 break-all group-hover:text-white transition-colors"
|
|
73
|
-
x-text="formatLogMessage(log.message)"></span>
|
|
74
|
-
<template x-if="getLogDetails(log.message)">
|
|
75
|
-
<div class="flex flex-wrap gap-2 text-[10px] text-zinc-500 mt-0.5">
|
|
76
|
-
<template x-for="(detail, key) in getLogDetails(log.message)" :key="key">
|
|
77
|
-
<span class="px-1.5 py-0.5 bg-space-800/50 rounded border border-space-border/30">
|
|
78
|
-
<span class="text-zinc-600" x-text="key"></span>=<span class="text-zinc-400" x-text="detail"></span>
|
|
79
|
-
</span>
|
|
80
|
-
</template>
|
|
81
|
-
</div>
|
|
82
|
-
</template>
|
|
83
|
-
</div>
|
|
84
|
-
</div>
|
|
85
|
-
</template>
|
|
86
|
-
<div class="h-3 w-1.5 bg-zinc-600 animate-pulse mt-1 inline-block" x-show="logPage === 1 && filteredLogs.length === logs.length && !logSearchQuery"></div>
|
|
87
|
-
<div x-show="filteredLogs.length === 0 && logs.length > 0" class="text-zinc-700 italic mt-8 text-center">
|
|
88
|
-
No logs match filter
|
|
89
|
-
</div>
|
|
90
|
-
</div>
|
|
91
|
-
<div class="flex items-center justify-between gap-3 px-4 py-3 border-t border-space-border bg-space-900/60 text-xs font-mono">
|
|
92
|
-
<span class="text-gray-500" x-text="logTotalPages ? `Page ${logPage} of ${logTotalPages} · 30 per page` : 'No logs' "></span>
|
|
93
|
-
<div class="flex items-center gap-2">
|
|
94
|
-
<button class="btn btn-xs btn-ghost" :disabled="logPage <= 1 || logLoading" @click="changeLogPage(logPage - 1)">Previous</button>
|
|
95
|
-
<button class="btn btn-xs btn-ghost" :disabled="logPage >= logTotalPages || logLoading" @click="changeLogPage(logPage + 1)">Next</button>
|
|
96
|
-
</div>
|
|
97
|
-
</div>
|
|
98
|
-
</div>
|
|
99
|
-
</div>
|
|
@@ -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/admin-path.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
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
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/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
|
-
}
|