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

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.
@@ -10,13 +10,30 @@
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
+ } from '../server-settings.js';
19
+ import { deleteClientKeyUsage, getClientKeyQuota } from '../client-key-usage.js';
14
20
 
15
21
  const VALID_STRATEGIES = ['sticky', 'round-robin'];
16
22
 
17
23
  function getSafeClientApiKey(clientApiKey) {
18
24
  const { key, ...safeClientApiKey } = clientApiKey;
19
- return safeClientApiKey;
25
+ return {
26
+ ...safeClientApiKey,
27
+ quota: getClientKeyQuota(clientApiKey.id, clientApiKey.weeklyLimitUsd)
28
+ };
29
+ }
30
+
31
+ function validateWeeklyLimitUsd(weeklyLimitUsd) {
32
+ if (typeof weeklyLimitUsd !== 'number' || !Number.isFinite(weeklyLimitUsd) || weeklyLimitUsd < 0) {
33
+ return 'weeklyLimitUsd must be a non-negative number';
34
+ }
35
+
36
+ return null;
20
37
  }
21
38
 
22
39
  function createClientApiKeyValue() {
@@ -168,17 +185,23 @@ export function handleListClientApiKeys(req, res) {
168
185
  * Creates a Claude Code client API key.
169
186
  */
170
187
  export function handleCreateClientApiKey(req, res) {
171
- const { name = '', remark = '' } = req.body || {};
188
+ const { name = '', remark = '', weeklyLimitUsd = DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD } = req.body || {};
172
189
 
173
190
  if (typeof name !== 'string' || typeof remark !== 'string') {
174
191
  return res.status(400).json({ success: false, error: 'name and remark must be strings' });
175
192
  }
176
193
 
194
+ const weeklyLimitError = validateWeeklyLimitUsd(weeklyLimitUsd);
195
+ if (weeklyLimitError) {
196
+ return res.status(400).json({ success: false, error: weeklyLimitError });
197
+ }
198
+
177
199
  const clientApiKey = {
178
200
  id: crypto.randomUUID(),
179
201
  key: createClientApiKeyValue(),
180
202
  name: name.trim(),
181
203
  remark: remark.trim(),
204
+ weeklyLimitUsd,
182
205
  createdAt: new Date().toISOString()
183
206
  };
184
207
  const settings = getServerSettings();
@@ -193,7 +216,7 @@ export function handleCreateClientApiKey(req, res) {
193
216
  */
194
217
  export function handleUpdateClientApiKey(req, res) {
195
218
  const { id } = req.params;
196
- const { name, remark } = req.body || {};
219
+ const { name, remark, weeklyLimitUsd } = req.body || {};
197
220
 
198
221
  if (name !== undefined && typeof name !== 'string') {
199
222
  return res.status(400).json({ success: false, error: 'name must be a string' });
@@ -201,6 +224,12 @@ export function handleUpdateClientApiKey(req, res) {
201
224
  if (remark !== undefined && typeof remark !== 'string') {
202
225
  return res.status(400).json({ success: false, error: 'remark must be a string' });
203
226
  }
227
+ if (weeklyLimitUsd !== undefined) {
228
+ const weeklyLimitError = validateWeeklyLimitUsd(weeklyLimitUsd);
229
+ if (weeklyLimitError) {
230
+ return res.status(400).json({ success: false, error: weeklyLimitError });
231
+ }
232
+ }
204
233
 
205
234
  const settings = getServerSettings();
206
235
  const index = settings.clientApiKeys.findIndex((clientApiKey) => clientApiKey.id === id);
@@ -212,7 +241,8 @@ export function handleUpdateClientApiKey(req, res) {
212
241
  const clientApiKey = {
213
242
  ...current,
214
243
  ...(name === undefined ? {} : { name: name.trim() }),
215
- ...(remark === undefined ? {} : { remark: remark.trim() })
244
+ ...(remark === undefined ? {} : { remark: remark.trim() }),
245
+ ...(weeklyLimitUsd === undefined ? {} : { weeklyLimitUsd })
216
246
  };
217
247
  const clientApiKeys = [...settings.clientApiKeys];
218
248
  clientApiKeys[index] = clientApiKey;
@@ -235,6 +265,7 @@ export function handleDeleteClientApiKey(req, res) {
235
265
  }
236
266
 
237
267
  setServerSettings({ clientApiKeys });
268
+ deleteClientKeyUsage(id);
238
269
  res.json({ success: true });
239
270
  }
240
271
 
@@ -10,6 +10,8 @@ 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
+
13
15
  export const DEFAULT_SETTINGS = {
14
16
  accountStrategy: 'sticky',
15
17
  defaultCodexModel: 'gpt-5.5',
@@ -24,6 +26,16 @@ function ensureConfigDir() {
24
26
  }
25
27
  }
26
28
 
29
+ function normalizeClientApiKey(clientApiKey) {
30
+ const weeklyLimitUsd = Number(clientApiKey?.weeklyLimitUsd);
31
+ return {
32
+ ...clientApiKey,
33
+ weeklyLimitUsd: Number.isFinite(weeklyLimitUsd) && weeklyLimitUsd >= 0
34
+ ? weeklyLimitUsd
35
+ : DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD
36
+ };
37
+ }
38
+
27
39
  function mergeSettings(data = {}) {
28
40
  return {
29
41
  ...DEFAULT_SETTINGS,
@@ -31,7 +43,10 @@ function mergeSettings(data = {}) {
31
43
  modelMap: {
32
44
  ...DEFAULT_MODEL_MAP,
33
45
  ...(data.modelMap || {})
34
- }
46
+ },
47
+ clientApiKeys: Array.isArray(data.clientApiKeys)
48
+ ? data.clientApiKeys.map(normalizeClientApiKey)
49
+ : []
35
50
  };
36
51
  }
37
52
 
@@ -65,5 +80,6 @@ export default {
65
80
  setServerSettings,
66
81
  SETTINGS_FILE,
67
82
  DEFAULT_SETTINGS,
68
- DEFAULT_MODEL_MAP
83
+ DEFAULT_MODEL_MAP,
84
+ DEFAULT_CLIENT_KEY_WEEKLY_LIMIT_USD
69
85
  };
@@ -0,0 +1,5 @@
1
+ import { getServerSettings } from '../server-settings.js';
2
+
3
+ export function shouldLogRawProxyPayloads() {
4
+ return getServerSettings().logRawProxyPayloads === true;
5
+ }