claude-codex-proxy 1.0.5-beta.3 → 1.0.5-beta.30

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.
Files changed (39) hide show
  1. package/README.md +0 -272
  2. package/bin/cli.js +37 -2
  3. package/package.json +1 -1
  4. package/public/css/style.css +132 -0
  5. package/public/index.html +60 -60
  6. package/public/js/app.js +194 -116
  7. package/public/login.html +32 -32
  8. package/public/views/accounts-modals.html +61 -2
  9. package/public/views/accounts.html +29 -10
  10. package/public/views/client-api-keys-modals.html +15 -1
  11. package/public/views/client-api-keys.html +68 -31
  12. package/public/views/logs.html +10 -3
  13. package/src/account-manager.js +34 -3
  14. package/src/account-rotation/rate-limits.js +4 -0
  15. package/src/account-rotation/strategies/base-strategy.js +4 -0
  16. package/src/account-rotation/strategies/sticky-strategy.js +7 -0
  17. package/src/admin-path.js +33 -0
  18. package/src/client-key-usage.js +193 -0
  19. package/src/deployment.js +49 -49
  20. package/src/direct-api.js +11 -11
  21. package/src/format-converter.js +83 -48
  22. package/src/index.js +13 -4
  23. package/src/middleware/auth.js +54 -16
  24. package/src/middleware/credentials.js +17 -7
  25. package/src/middleware/sse.js +69 -29
  26. package/src/model-api.js +60 -0
  27. package/src/model-pricing.js +127 -0
  28. package/src/response-streamer.js +23 -7
  29. package/src/routes/accounts-route.js +106 -2
  30. package/src/routes/api-routes.js +59 -42
  31. package/src/routes/chat-route.js +25 -4
  32. package/src/routes/claude-config-route.js +19 -10
  33. package/src/routes/client-quota-route.js +31 -0
  34. package/src/routes/logs-route.js +21 -2
  35. package/src/routes/messages-route.js +100 -51
  36. package/src/routes/settings-route.js +66 -5
  37. package/src/server-settings.js +25 -2
  38. package/src/utils/logger.js +30 -0
  39. package/src/utils/proxy-logging.js +5 -0
@@ -10,13 +10,34 @@
10
10
  */
11
11
 
12
12
  import crypto from 'crypto';
13
- import { getServerSettings, setServerSettings, DEFAULT_MODEL_MAP } from '../server-settings.js';
13
+ import {
14
+ getServerSettings,
15
+ setServerSettings,
16
+ DEFAULT_MODEL_MAP,
17
+ DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD,
18
+ DEFAULT_CLIENT_KEY_FIVE_HOUR_LIMIT_USD
19
+ } from '../server-settings.js';
20
+ import { deleteClientKeyUsage, getClientKeyQuota } from '../client-key-usage.js';
14
21
 
15
22
  const VALID_STRATEGIES = ['sticky', 'round-robin'];
16
23
 
17
24
  function getSafeClientApiKey(clientApiKey) {
18
25
  const { key, ...safeClientApiKey } = clientApiKey;
19
- return safeClientApiKey;
26
+ return {
27
+ ...safeClientApiKey,
28
+ quota: getClientKeyQuota(clientApiKey.id, clientApiKey.weeklyLimitUsd, {
29
+ enabled: clientApiKey.fiveHourLimitEnabled,
30
+ usd: clientApiKey.fiveHourLimitUsd
31
+ })
32
+ };
33
+ }
34
+
35
+ function validateLimitUsd(value, fieldName) {
36
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
37
+ return `${fieldName} must be a non-negative number`;
38
+ }
39
+
40
+ return null;
20
41
  }
21
42
 
22
43
  function createClientApiKeyValue() {
@@ -168,17 +189,38 @@ export function handleListClientApiKeys(req, res) {
168
189
  * Creates a Claude Code client API key.
169
190
  */
170
191
  export function handleCreateClientApiKey(req, res) {
171
- const { name = '', remark = '' } = req.body || {};
192
+ const {
193
+ name = '',
194
+ remark = '',
195
+ weeklyLimitUsd = DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD,
196
+ fiveHourLimitEnabled = false,
197
+ fiveHourLimitUsd = DEFAULT_CLIENT_KEY_FIVE_HOUR_LIMIT_USD
198
+ } = req.body || {};
172
199
 
173
200
  if (typeof name !== 'string' || typeof remark !== 'string') {
174
201
  return res.status(400).json({ success: false, error: 'name and remark must be strings' });
175
202
  }
203
+ if (typeof fiveHourLimitEnabled !== 'boolean') {
204
+ return res.status(400).json({ success: false, error: 'fiveHourLimitEnabled must be a boolean' });
205
+ }
206
+
207
+ const weeklyLimitError = validateLimitUsd(weeklyLimitUsd, 'weeklyLimitUsd');
208
+ if (weeklyLimitError) {
209
+ return res.status(400).json({ success: false, error: weeklyLimitError });
210
+ }
211
+ const fiveHourLimitError = validateLimitUsd(fiveHourLimitUsd, 'fiveHourLimitUsd');
212
+ if (fiveHourLimitError) {
213
+ return res.status(400).json({ success: false, error: fiveHourLimitError });
214
+ }
176
215
 
177
216
  const clientApiKey = {
178
217
  id: crypto.randomUUID(),
179
218
  key: createClientApiKeyValue(),
180
219
  name: name.trim(),
181
220
  remark: remark.trim(),
221
+ weeklyLimitUsd,
222
+ fiveHourLimitEnabled,
223
+ fiveHourLimitUsd,
182
224
  createdAt: new Date().toISOString()
183
225
  };
184
226
  const settings = getServerSettings();
@@ -193,7 +235,7 @@ export function handleCreateClientApiKey(req, res) {
193
235
  */
194
236
  export function handleUpdateClientApiKey(req, res) {
195
237
  const { id } = req.params;
196
- const { name, remark } = req.body || {};
238
+ const { name, remark, weeklyLimitUsd, fiveHourLimitEnabled, fiveHourLimitUsd } = req.body || {};
197
239
 
198
240
  if (name !== undefined && typeof name !== 'string') {
199
241
  return res.status(400).json({ success: false, error: 'name must be a string' });
@@ -201,6 +243,21 @@ export function handleUpdateClientApiKey(req, res) {
201
243
  if (remark !== undefined && typeof remark !== 'string') {
202
244
  return res.status(400).json({ success: false, error: 'remark must be a string' });
203
245
  }
246
+ if (weeklyLimitUsd !== undefined) {
247
+ const weeklyLimitError = validateLimitUsd(weeklyLimitUsd, 'weeklyLimitUsd');
248
+ if (weeklyLimitError) {
249
+ return res.status(400).json({ success: false, error: weeklyLimitError });
250
+ }
251
+ }
252
+ if (fiveHourLimitEnabled !== undefined && typeof fiveHourLimitEnabled !== 'boolean') {
253
+ return res.status(400).json({ success: false, error: 'fiveHourLimitEnabled must be a boolean' });
254
+ }
255
+ if (fiveHourLimitUsd !== undefined) {
256
+ const fiveHourLimitError = validateLimitUsd(fiveHourLimitUsd, 'fiveHourLimitUsd');
257
+ if (fiveHourLimitError) {
258
+ return res.status(400).json({ success: false, error: fiveHourLimitError });
259
+ }
260
+ }
204
261
 
205
262
  const settings = getServerSettings();
206
263
  const index = settings.clientApiKeys.findIndex((clientApiKey) => clientApiKey.id === id);
@@ -212,7 +269,10 @@ export function handleUpdateClientApiKey(req, res) {
212
269
  const clientApiKey = {
213
270
  ...current,
214
271
  ...(name === undefined ? {} : { name: name.trim() }),
215
- ...(remark === undefined ? {} : { remark: remark.trim() })
272
+ ...(remark === undefined ? {} : { remark: remark.trim() }),
273
+ ...(weeklyLimitUsd === undefined ? {} : { weeklyLimitUsd }),
274
+ ...(fiveHourLimitEnabled === undefined ? {} : { fiveHourLimitEnabled }),
275
+ ...(fiveHourLimitUsd === undefined ? {} : { fiveHourLimitUsd })
216
276
  };
217
277
  const clientApiKeys = [...settings.clientApiKeys];
218
278
  clientApiKeys[index] = clientApiKey;
@@ -235,6 +295,7 @@ export function handleDeleteClientApiKey(req, res) {
235
295
  }
236
296
 
237
297
  setServerSettings({ clientApiKeys });
298
+ deleteClientKeyUsage(id);
238
299
  res.json({ success: true });
239
300
  }
240
301
 
@@ -10,6 +10,9 @@ export const DEFAULT_MODEL_MAP = {
10
10
  haiku: 'gpt-5.4-mini'
11
11
  };
12
12
 
13
+ export const DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD = 50;
14
+ export const DEFAULT_CLIENT_KEY_FIVE_HOUR_LIMIT_USD = 0;
15
+
13
16
  export const DEFAULT_SETTINGS = {
14
17
  accountStrategy: 'sticky',
15
18
  defaultCodexModel: 'gpt-5.5',
@@ -24,6 +27,21 @@ function ensureConfigDir() {
24
27
  }
25
28
  }
26
29
 
30
+ function normalizeClientApiKey(clientApiKey) {
31
+ const weeklyLimitUsd = Number(clientApiKey?.weeklyLimitUsd);
32
+ const fiveHourLimitUsd = Number(clientApiKey?.fiveHourLimitUsd);
33
+ return {
34
+ ...clientApiKey,
35
+ weeklyLimitUsd: Number.isFinite(weeklyLimitUsd) && weeklyLimitUsd >= 0
36
+ ? weeklyLimitUsd
37
+ : DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD,
38
+ fiveHourLimitEnabled: clientApiKey?.fiveHourLimitEnabled === true,
39
+ fiveHourLimitUsd: Number.isFinite(fiveHourLimitUsd) && fiveHourLimitUsd >= 0
40
+ ? fiveHourLimitUsd
41
+ : DEFAULT_CLIENT_KEY_FIVE_HOUR_LIMIT_USD
42
+ };
43
+ }
44
+
27
45
  function mergeSettings(data = {}) {
28
46
  return {
29
47
  ...DEFAULT_SETTINGS,
@@ -31,7 +49,10 @@ function mergeSettings(data = {}) {
31
49
  modelMap: {
32
50
  ...DEFAULT_MODEL_MAP,
33
51
  ...(data.modelMap || {})
34
- }
52
+ },
53
+ clientApiKeys: Array.isArray(data.clientApiKeys)
54
+ ? data.clientApiKeys.map(normalizeClientApiKey)
55
+ : []
35
56
  };
36
57
  }
37
58
 
@@ -65,5 +86,7 @@ export default {
65
86
  setServerSettings,
66
87
  SETTINGS_FILE,
67
88
  DEFAULT_SETTINGS,
68
- DEFAULT_MODEL_MAP
89
+ DEFAULT_MODEL_MAP,
90
+ DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD,
91
+ DEFAULT_CLIENT_KEY_FIVE_HOUR_LIMIT_USD
69
92
  };
@@ -40,6 +40,24 @@ class Logger extends EventEmitter {
40
40
  return [...this.history];
41
41
  }
42
42
 
43
+ /**
44
+ * Returns a newest-first page of in-memory log entries.
45
+ *
46
+ * @param {number} page - One-based page number.
47
+ * @param {number} pageSize - Maximum number of entries per page.
48
+ * @returns {{ logs: object[], total: number }} Requested page and total entry count.
49
+ */
50
+ getHistoryPage(page, pageSize) {
51
+ const total = this.history.length;
52
+ const end = Math.max(0, total - (page - 1) * pageSize);
53
+ const start = Math.max(0, end - pageSize);
54
+
55
+ return {
56
+ logs: this.history.slice(start, end).reverse(),
57
+ total
58
+ };
59
+ }
60
+
43
61
  clear() {
44
62
  this.history = [];
45
63
  }
@@ -156,6 +174,18 @@ class Logger extends EventEmitter {
156
174
  if (details.inputTokens !== undefined) parts.push(`input_tokens=${details.inputTokens}`);
157
175
  if (details.outputTokens !== undefined) parts.push(`output_tokens=${details.outputTokens}`);
158
176
  if (details.cacheReadInputTokens !== undefined) parts.push(`cache_read_input_tokens=${details.cacheReadInputTokens}`);
177
+ if (details.transport) parts.push(`transport=${details.transport}`);
178
+ if (details.clientHttpStatus !== undefined) parts.push(`client_http_status=${details.clientHttpStatus}`);
179
+ if (details.errorType) parts.push(`error_type=${details.errorType}`);
180
+ if (details.retrySignal !== undefined) parts.push(`retry_signal=${details.retrySignal}`);
181
+ if (details.errorName) parts.push(`error_name=${details.errorName}`);
182
+ if (details.errorCode) parts.push(`error_code=${details.errorCode}`);
183
+ if (details.upstreamStatus !== undefined) parts.push(`upstream_status=${details.upstreamStatus}`);
184
+ if (details.isUpstreamError !== undefined) parts.push(`is_upstream_error=${details.isUpstreamError}`);
185
+ if (details.sseWriteAttempted !== undefined) parts.push(`sse_write_attempted=${details.sseWriteAttempted}`);
186
+ if (details.sseWriteBackpressure !== undefined) parts.push(`sse_write_backpressure=${details.sseWriteBackpressure}`);
187
+ if (details.socketDestroyed !== undefined) parts.push(`socket_destroyed=${details.socketDestroyed}`);
188
+ if (details.writableEnded !== undefined) parts.push(`writable_ended=${details.writableEnded}`);
159
189
  if (details.duration) parts.push(`${details.duration}ms`);
160
190
  if (details.error) parts.push(`error=${details.error}`);
161
191
 
@@ -0,0 +1,5 @@
1
+ import { getServerSettings } from '../server-settings.js';
2
+
3
+ export function shouldLogRawProxyPayloads() {
4
+ return getServerSettings().logRawProxyPayloads === true;
5
+ }