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/js/app.js
CHANGED
|
@@ -1,126 +1,762 @@
|
|
|
1
|
-
const adminPath = window.__PROXY_ADMIN_PATH__ || '/admin';
|
|
2
|
-
const adminUrl = (path) => `${adminPath}${path}`;
|
|
3
|
-
|
|
4
1
|
document.addEventListener('alpine:init', () => {
|
|
5
2
|
Alpine.data('app', () => ({
|
|
6
|
-
version: '',
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
3
|
+
version: '',
|
|
4
|
+
connectionStatus: 'connecting',
|
|
5
|
+
activeTab: 'dashboard',
|
|
6
|
+
sidebarOpen: window.innerWidth >= 1024,
|
|
7
|
+
loading: false,
|
|
8
|
+
refreshingTokenEmails: [],
|
|
9
|
+
toast: null,
|
|
10
|
+
currentTime: '',
|
|
11
|
+
authRequired: false,
|
|
12
|
+
authChecked: false,
|
|
13
|
+
apiKey: localStorage.getItem('codexProxyApiKey') || '',
|
|
14
|
+
loginApiKey: '',
|
|
15
|
+
|
|
16
|
+
accounts: [],
|
|
17
|
+
searchQuery: '',
|
|
18
|
+
stats: { total: 0, active: 0, expired: 0, planType: '-' },
|
|
19
|
+
|
|
20
|
+
accountStrategy: 'sticky',
|
|
21
|
+
modelMappings: {
|
|
22
|
+
sonnet: '',
|
|
23
|
+
opus: '',
|
|
24
|
+
haiku: ''
|
|
25
|
+
},
|
|
26
|
+
modelMapLoading: false,
|
|
27
|
+
modelMapSaving: false,
|
|
28
|
+
strategySaving: false,
|
|
29
|
+
logRawProxyPayloads: false,
|
|
30
|
+
rawProxyLogsSaving: false,
|
|
31
|
+
clientApiKeys: [],
|
|
32
|
+
clientApiKeyName: '',
|
|
33
|
+
clientApiKeyRemark: '',
|
|
34
|
+
clientApiKeysLoading: false,
|
|
35
|
+
clientApiKeySaving: false,
|
|
36
|
+
revealedClientApiKey: '',
|
|
37
|
+
showCreateClientApiKeyModal: false,
|
|
38
|
+
|
|
39
|
+
showAddModal: false,
|
|
40
|
+
showDeleteModal: false,
|
|
41
|
+
deleteTarget: '',
|
|
42
|
+
showQuotaModalView: false,
|
|
43
|
+
selectedAccount: null,
|
|
44
|
+
|
|
45
|
+
oauthManualMode: false,
|
|
46
|
+
oauthManualUrl: '',
|
|
47
|
+
oauthManualVerifier: '',
|
|
48
|
+
oauthManualCode: '',
|
|
49
|
+
|
|
50
|
+
testPrompt: 'Say hello',
|
|
51
|
+
testResponse: '',
|
|
52
|
+
testing: false,
|
|
53
|
+
|
|
54
|
+
configPath: '~/.claude-codex-proxy/accounts.json',
|
|
55
|
+
serverUrl: window.location.origin,
|
|
56
|
+
|
|
57
|
+
logs: [],
|
|
58
|
+
logSearchQuery: '',
|
|
59
|
+
logFilters: { INFO: true, SUCCESS: true, WARN: true, ERROR: true, DEBUG: false },
|
|
60
|
+
logEventSource: null,
|
|
61
|
+
|
|
62
|
+
get isAuthenticated() {
|
|
63
|
+
return !this.authRequired || Boolean(this.apiKey);
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
get filteredLogs() {
|
|
67
|
+
const query = this.logSearchQuery.trim().toLowerCase();
|
|
68
|
+
return this.logs.filter(log => {
|
|
69
|
+
if (!this.logFilters[log.level]) return false;
|
|
70
|
+
if (query && !log.message.toLowerCase().includes(query)) return false;
|
|
71
|
+
return true;
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
get filteredAccounts() {
|
|
76
|
+
if (!this.searchQuery) return this.accounts;
|
|
77
|
+
const q = this.searchQuery.toLowerCase();
|
|
78
|
+
return this.accounts.filter(a => a.email.toLowerCase().includes(q));
|
|
79
|
+
},
|
|
24
80
|
|
|
25
81
|
async init() {
|
|
26
|
-
await this.checkHealth();
|
|
27
|
-
this.authChecked = true;
|
|
28
|
-
if (this.isLoginPage()) {
|
|
29
|
-
if (this.isAuthenticated) window.location.replace(adminUrl('/dashboard'));
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
if (!this.isAuthenticated) { window.location.replace(adminUrl('/login')); return; }
|
|
33
|
-
await this.loadCurrentView();
|
|
34
82
|
this.updateTime();
|
|
35
83
|
setInterval(() => this.updateTime(), 1000);
|
|
84
|
+
await this.checkHealth();
|
|
85
|
+
if (this.isAuthenticated) {
|
|
86
|
+
this.loadProtectedData();
|
|
87
|
+
}
|
|
36
88
|
setInterval(() => this.checkHealth(), 30000);
|
|
37
|
-
|
|
38
|
-
window.addEventListener('
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
89
|
+
|
|
90
|
+
window.addEventListener('resize', () => {
|
|
91
|
+
this.sidebarOpen = window.innerWidth >= 1024;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
window.addEventListener('message', (event) => {
|
|
95
|
+
if (event.data && event.data.type === 'oauth-success') {
|
|
96
|
+
this.showToast(`Account ${event.data.email} added!`, 'success');
|
|
97
|
+
this.showAddModal = false;
|
|
98
|
+
this.refreshAccounts();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
loadProtectedData() {
|
|
104
|
+
this.refreshAccounts();
|
|
105
|
+
this.startLogStream();
|
|
106
|
+
this.loadAccountStrategySetting();
|
|
107
|
+
this.loadModelMapSetting();
|
|
108
|
+
this.loadRawProxyLogsSetting();
|
|
109
|
+
this.loadClientApiKeys();
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
updateTime() {
|
|
113
|
+
this.currentTime = new Date().toLocaleTimeString([], {hour: '2-digit', minute: '2-digit', second: '2-digit'});
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
setActiveTab(tab) {
|
|
117
|
+
this.activeTab = tab;
|
|
118
|
+
if (window.innerWidth < 1024) {
|
|
119
|
+
this.sidebarOpen = false;
|
|
120
|
+
}
|
|
67
121
|
},
|
|
68
|
-
|
|
122
|
+
|
|
69
123
|
async api(endpoint, options = {}) {
|
|
70
124
|
try {
|
|
71
|
-
const headers = {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
125
|
+
const headers = {
|
|
126
|
+
'Content-Type': 'application/json',
|
|
127
|
+
...(options.headers || {})
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
if (this.apiKey) {
|
|
131
|
+
headers.Authorization = `Bearer ${this.apiKey}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const response = await fetch(endpoint, {
|
|
135
|
+
...options,
|
|
136
|
+
headers
|
|
137
|
+
});
|
|
75
138
|
const data = await response.json();
|
|
76
|
-
|
|
139
|
+
|
|
140
|
+
if (response.status === 401 && endpoint !== '/health') {
|
|
141
|
+
this.logout(false);
|
|
142
|
+
this.showToast('Access key is invalid or expired', 'error');
|
|
143
|
+
}
|
|
144
|
+
|
|
77
145
|
return { ok: response.ok, data };
|
|
78
|
-
} catch (error) {
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return { ok: false, error: error.message };
|
|
148
|
+
}
|
|
79
149
|
},
|
|
80
|
-
|
|
150
|
+
|
|
151
|
+
login() {
|
|
81
152
|
const key = this.loginApiKey.trim();
|
|
82
|
-
if (!key) {
|
|
153
|
+
if (!key) {
|
|
154
|
+
this.showToast('Access key is required', 'error');
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
83
158
|
this.apiKey = key;
|
|
84
|
-
const { ok } = await this.api('/settings/account-strategy');
|
|
85
|
-
if (!ok) { this.apiKey = ''; this.loginError = 'Access key is invalid or expired'; return; }
|
|
86
159
|
localStorage.setItem('codexProxyApiKey', key);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
async
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
async
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
160
|
+
this.loginApiKey = '';
|
|
161
|
+
this.showToast('Access key saved', 'success');
|
|
162
|
+
this.loadProtectedData();
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
logout(showMessage = true) {
|
|
166
|
+
this.apiKey = '';
|
|
167
|
+
this.loginApiKey = '';
|
|
168
|
+
localStorage.removeItem('codexProxyApiKey');
|
|
169
|
+
if (this.logEventSource) {
|
|
170
|
+
this.logEventSource.close();
|
|
171
|
+
this.logEventSource = null;
|
|
172
|
+
}
|
|
173
|
+
if (showMessage) this.showToast('Logged out', 'info');
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
async checkHealth() {
|
|
177
|
+
const { ok, data } = await this.api('/health');
|
|
178
|
+
this.connectionStatus = ok ? 'connected' : 'disconnected';
|
|
179
|
+
if (ok && data?.version) this.version = data.version;
|
|
180
|
+
if (ok && typeof data?.publicMode === 'boolean') {
|
|
181
|
+
this.authRequired = data.publicMode;
|
|
182
|
+
this.authChecked = true;
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
async refreshAccounts() {
|
|
187
|
+
this.loading = true;
|
|
188
|
+
const { ok, data } = await this.api('/accounts');
|
|
189
|
+
|
|
190
|
+
if (ok && data.accounts) {
|
|
191
|
+
this.accounts = data.accounts;
|
|
192
|
+
this.stats = {
|
|
193
|
+
total: data.total || data.accounts.length,
|
|
194
|
+
active: data.accounts.filter(a => a.isActive).length,
|
|
195
|
+
expired: data.accounts.filter(a => a.tokenExpired).length,
|
|
196
|
+
planType: data.accounts.find(a => a.isActive)?.planType || '-'
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
await this.refreshAllQuotaData();
|
|
200
|
+
}
|
|
201
|
+
this.loading = false;
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
async refreshAllQuotaData() {
|
|
205
|
+
if (!this.accounts.length) return;
|
|
206
|
+
const { ok, data } = await this.api('/accounts/quota/all');
|
|
207
|
+
if (!ok || !data?.accounts) return;
|
|
208
|
+
|
|
209
|
+
const quotaMap = new Map(
|
|
210
|
+
data.accounts.map((entry) => [entry.email, entry.quota || null])
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
this.accounts = this.accounts.map((account) => ({
|
|
214
|
+
...account,
|
|
215
|
+
quota: quotaMap.has(account.email) ? quotaMap.get(account.email) : account.quota
|
|
216
|
+
}));
|
|
217
|
+
|
|
218
|
+
if (this.selectedAccount?.email) {
|
|
219
|
+
const refreshed = this.accounts.find((account) => account.email === this.selectedAccount.email);
|
|
220
|
+
if (refreshed) this.selectedAccount = refreshed;
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
getRemainingPercentage(account) {
|
|
225
|
+
const usage = account?.quota?.usage;
|
|
226
|
+
if (!usage) return null;
|
|
227
|
+
|
|
228
|
+
const percentage = Number(usage.percentage);
|
|
229
|
+
const usedFromTotal = Number(usage.totalTokenUsage);
|
|
230
|
+
const remainingFromApi = Number(usage.remaining);
|
|
231
|
+
|
|
232
|
+
let used = null;
|
|
233
|
+
if (Number.isFinite(percentage)) {
|
|
234
|
+
used = percentage;
|
|
235
|
+
} else if (Number.isFinite(usedFromTotal)) {
|
|
236
|
+
used = usedFromTotal;
|
|
237
|
+
} else if (Number.isFinite(remainingFromApi)) {
|
|
238
|
+
used = 100 - remainingFromApi;
|
|
239
|
+
} else if (usage.limitReached === true || usage.allowed === false) {
|
|
240
|
+
used = 100;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!Number.isFinite(used)) return null;
|
|
244
|
+
const clampedUsed = Math.max(0, Math.min(100, used));
|
|
245
|
+
return Math.max(0, Math.round(100 - clampedUsed));
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
isQuotaExhausted(account) {
|
|
249
|
+
const remaining = this.getRemainingPercentage(account);
|
|
250
|
+
if (remaining === null) return false;
|
|
251
|
+
const usage = account?.quota?.usage;
|
|
252
|
+
return remaining <= 0 || usage?.limitReached === true || usage?.allowed === false;
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
quotaBarClass(account) {
|
|
256
|
+
const remaining = this.getRemainingPercentage(account);
|
|
257
|
+
if (remaining === null) return 'bg-gray-500';
|
|
258
|
+
if (remaining > 50) return 'bg-neon-green';
|
|
259
|
+
if (remaining > 20) return 'bg-yellow-500';
|
|
260
|
+
return 'bg-red-500';
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
quotaTextClass(account) {
|
|
264
|
+
const remaining = this.getRemainingPercentage(account);
|
|
265
|
+
if (remaining === null) return 'text-gray-500';
|
|
266
|
+
return remaining <= 20 ? 'text-red-400' : 'text-gray-400';
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
quotaLabel(account) {
|
|
270
|
+
const remaining = this.getRemainingPercentage(account);
|
|
271
|
+
if (remaining === null) return '-';
|
|
272
|
+
return `${remaining}%`;
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
getQuotaResetAt(account) {
|
|
276
|
+
const usage = account?.quota?.usage;
|
|
277
|
+
if (!usage) return null;
|
|
278
|
+
|
|
279
|
+
if (usage.resetAt) return usage.resetAt;
|
|
280
|
+
|
|
281
|
+
const epoch = Number(usage?.raw?.rate_limit?.primary_window?.reset_at);
|
|
282
|
+
if (Number.isFinite(epoch)) {
|
|
283
|
+
return new Date(epoch * 1000).toISOString();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const resetAfter = Number(
|
|
287
|
+
usage.resetAfterSeconds ?? usage?.raw?.rate_limit?.primary_window?.reset_after_seconds
|
|
288
|
+
);
|
|
289
|
+
if (Number.isFinite(resetAfter) && resetAfter > 0) {
|
|
290
|
+
return new Date(Date.now() + resetAfter * 1000).toISOString();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return null;
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
quotaResetAtLabel(account) {
|
|
297
|
+
const resetAt = this.getQuotaResetAt(account);
|
|
298
|
+
if (!resetAt) return null;
|
|
299
|
+
const date = new Date(resetAt);
|
|
300
|
+
if (Number.isNaN(date.getTime())) return null;
|
|
301
|
+
return date.toLocaleString();
|
|
302
|
+
},
|
|
303
|
+
|
|
304
|
+
quotaResetSummary(account) {
|
|
305
|
+
const resetAt = this.getQuotaResetAt(account);
|
|
306
|
+
if (!resetAt) return null;
|
|
307
|
+
|
|
308
|
+
const resetMs = new Date(resetAt).getTime();
|
|
309
|
+
if (!Number.isFinite(resetMs)) return null;
|
|
310
|
+
|
|
311
|
+
const deltaSec = Math.max(0, Math.floor((resetMs - Date.now()) / 1000));
|
|
312
|
+
if (deltaSec === 0) return 'Reset due now';
|
|
313
|
+
|
|
314
|
+
const days = Math.floor(deltaSec / 86400);
|
|
315
|
+
const hours = Math.floor((deltaSec % 86400) / 3600);
|
|
316
|
+
const minutes = Math.floor((deltaSec % 3600) / 60);
|
|
317
|
+
|
|
318
|
+
if (days > 0) return `Resets in ${days}d ${hours}h`;
|
|
319
|
+
if (hours > 0) return `Resets in ${hours}h ${minutes}m`;
|
|
320
|
+
return `Resets in ${minutes}m`;
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
async startOAuth() {
|
|
324
|
+
await this.api('/accounts/oauth/cleanup', { method: 'POST' });
|
|
325
|
+
const { ok, data } = await this.api('/accounts/add', { method: 'POST' });
|
|
326
|
+
|
|
327
|
+
if (ok && data.oauth_url) {
|
|
328
|
+
const width = 500, height = 700;
|
|
329
|
+
const left = (screen.width - width) / 2;
|
|
330
|
+
const top = (screen.height - height) / 2;
|
|
331
|
+
window.open(data.oauth_url, 'ChatGPT Login', `width=${width},height=${height},left=${left},top=${top}`);
|
|
332
|
+
|
|
333
|
+
const checkAdded = setInterval(async () => {
|
|
334
|
+
const { ok, data } = await this.api('/accounts');
|
|
335
|
+
if (ok && data.accounts?.length > this.accounts.length) {
|
|
336
|
+
clearInterval(checkAdded);
|
|
337
|
+
this.showAddModal = false;
|
|
338
|
+
this.refreshAccounts();
|
|
339
|
+
}
|
|
340
|
+
}, 2000);
|
|
341
|
+
|
|
342
|
+
setTimeout(() => clearInterval(checkAdded), 120000);
|
|
343
|
+
} else {
|
|
344
|
+
this.showToast(data?.message || 'Failed to start OAuth', 'error');
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
|
|
348
|
+
async startManualOAuth() {
|
|
349
|
+
await this.api('/accounts/oauth/cleanup', { method: 'POST' });
|
|
350
|
+
const { ok, data } = await this.api('/accounts/add', { method: 'POST' });
|
|
351
|
+
|
|
352
|
+
if (ok && data.oauth_url) {
|
|
353
|
+
this.oauthManualUrl = data.oauth_url;
|
|
354
|
+
this.oauthManualVerifier = data.verifier;
|
|
355
|
+
this.oauthManualCode = '';
|
|
356
|
+
this.oauthManualMode = true;
|
|
357
|
+
} else {
|
|
358
|
+
this.showToast(data?.message || 'Failed to start OAuth', 'error');
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
|
|
362
|
+
async submitManualOAuth() {
|
|
363
|
+
if (!this.oauthManualCode) return;
|
|
364
|
+
|
|
365
|
+
const { ok, data } = await this.api('/accounts/add/manual', {
|
|
366
|
+
method: 'POST',
|
|
367
|
+
body: JSON.stringify({
|
|
368
|
+
code: this.oauthManualCode,
|
|
369
|
+
verifier: this.oauthManualVerifier
|
|
370
|
+
})
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
if (ok && data.success) {
|
|
374
|
+
this.showToast(data.message, 'success');
|
|
375
|
+
this.showAddModal = false;
|
|
376
|
+
this.oauthManualMode = false;
|
|
377
|
+
this.refreshAccounts();
|
|
378
|
+
} else {
|
|
379
|
+
this.showToast(data?.error || 'Failed to add account', 'error');
|
|
380
|
+
}
|
|
381
|
+
},
|
|
382
|
+
|
|
383
|
+
async copyToClipboard(text) {
|
|
384
|
+
try {
|
|
385
|
+
await navigator.clipboard.writeText(text);
|
|
386
|
+
this.showToast('Copied to clipboard', 'success');
|
|
387
|
+
} catch (e) {
|
|
388
|
+
this.showToast('Failed to copy', 'error');
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
|
|
392
|
+
async importFromCodex() {
|
|
393
|
+
const { ok, data } = await this.api('/accounts/import', { method: 'POST' });
|
|
394
|
+
if (ok && data.success) {
|
|
395
|
+
this.showToast(data.message, 'success');
|
|
396
|
+
this.showAddModal = false;
|
|
397
|
+
this.refreshAccounts();
|
|
398
|
+
} else {
|
|
399
|
+
this.showToast(data?.message || 'Import failed', 'error');
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
|
|
403
|
+
async switchAccount(email) {
|
|
404
|
+
const { ok, data } = await this.api('/accounts/switch', {
|
|
405
|
+
method: 'POST',
|
|
406
|
+
body: JSON.stringify({ email })
|
|
407
|
+
});
|
|
408
|
+
if (ok && data.success) {
|
|
409
|
+
this.showToast(data.message, 'success');
|
|
410
|
+
this.refreshAccounts();
|
|
411
|
+
} else {
|
|
412
|
+
this.showToast(data?.message || 'Failed to switch', 'error');
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
|
|
416
|
+
async refreshToken(email) {
|
|
417
|
+
if (this.refreshingTokenEmails.includes(email)) return;
|
|
418
|
+
|
|
419
|
+
this.refreshingTokenEmails.push(email);
|
|
420
|
+
try {
|
|
421
|
+
const { ok, data } = await this.api(`/accounts/${encodeURIComponent(email)}/refresh`, { method: 'POST' });
|
|
422
|
+
if (ok && data.success) {
|
|
423
|
+
this.showToast(data.message, 'success');
|
|
424
|
+
await this.refreshAccounts();
|
|
425
|
+
} else {
|
|
426
|
+
this.showToast(data?.message || 'Refresh failed', 'error');
|
|
427
|
+
}
|
|
428
|
+
} finally {
|
|
429
|
+
this.refreshingTokenEmails = this.refreshingTokenEmails.filter(item => item !== email);
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
|
|
433
|
+
async refreshAllTokens() {
|
|
434
|
+
this.showToast('Refreshing all tokens...', 'info');
|
|
435
|
+
const { ok, data } = await this.api('/accounts/refresh/all', { method: 'POST' });
|
|
436
|
+
if (ok) {
|
|
437
|
+
this.showToast(data.message, 'success');
|
|
438
|
+
this.refreshAccounts();
|
|
439
|
+
} else {
|
|
440
|
+
this.showToast(data?.message || 'Failed', 'error');
|
|
441
|
+
}
|
|
442
|
+
},
|
|
443
|
+
|
|
444
|
+
confirmDelete(email) {
|
|
445
|
+
this.deleteTarget = email;
|
|
446
|
+
this.showDeleteModal = true;
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
async executeDelete() {
|
|
450
|
+
const { ok, data } = await this.api(`/accounts/${encodeURIComponent(this.deleteTarget)}`, { method: 'DELETE' });
|
|
451
|
+
this.showDeleteModal = false;
|
|
452
|
+
if (ok && data.success) {
|
|
453
|
+
this.showToast(data.message, 'success');
|
|
454
|
+
this.refreshAccounts();
|
|
455
|
+
} else {
|
|
456
|
+
this.showToast(data?.message || 'Delete failed', 'error');
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
|
|
460
|
+
showQuotaModal(acc) {
|
|
461
|
+
this.selectedAccount = acc;
|
|
462
|
+
this.showQuotaModalView = true;
|
|
463
|
+
},
|
|
464
|
+
|
|
465
|
+
async testChat() {
|
|
466
|
+
if (!this.testPrompt.trim()) return;
|
|
467
|
+
|
|
468
|
+
this.testing = true;
|
|
469
|
+
this.testResponse = '';
|
|
470
|
+
|
|
471
|
+
const testModel = this.modelMappings?.sonnet || 'gpt-5.5';
|
|
472
|
+
|
|
473
|
+
const { ok, data } = await this.api('/v1/chat/completions', {
|
|
474
|
+
method: 'POST',
|
|
475
|
+
body: JSON.stringify({
|
|
476
|
+
model: testModel,
|
|
477
|
+
messages: [{ role: 'user', content: this.testPrompt }]
|
|
478
|
+
})
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
this.testing = false;
|
|
482
|
+
|
|
483
|
+
if (ok && data.choices) {
|
|
484
|
+
const content = data.choices[0]?.message?.content;
|
|
485
|
+
|
|
486
|
+
this.testResponse = content && content.trim()
|
|
487
|
+
? content
|
|
488
|
+
: `[Empty response]\n\n${JSON.stringify(data, null, 2)}`;
|
|
489
|
+
} else {
|
|
490
|
+
this.testResponse = data?.error?.message || data?.error || 'Request failed';
|
|
491
|
+
}
|
|
492
|
+
},
|
|
493
|
+
|
|
494
|
+
async loadAccountStrategySetting() {
|
|
495
|
+
const { ok, data } = await this.api('/settings/account-strategy');
|
|
496
|
+
if (ok && data?.accountStrategy) {
|
|
497
|
+
this.accountStrategy = data.accountStrategy;
|
|
498
|
+
}
|
|
499
|
+
},
|
|
500
|
+
|
|
501
|
+
async setAccountStrategy(strategy) {
|
|
502
|
+
if (this.strategySaving || this.accountStrategy === strategy) return;
|
|
503
|
+
this.strategySaving = true;
|
|
504
|
+
const { ok, data } = await this.api('/settings/account-strategy', {
|
|
505
|
+
method: 'POST',
|
|
506
|
+
body: JSON.stringify({ accountStrategy: strategy })
|
|
507
|
+
});
|
|
508
|
+
this.strategySaving = false;
|
|
509
|
+
if (ok && data?.accountStrategy) {
|
|
510
|
+
this.accountStrategy = data.accountStrategy;
|
|
511
|
+
this.showToast(`Account strategy set to ${data.accountStrategy === 'sticky' ? 'Sticky' : 'Round-Robin'}`, 'success');
|
|
512
|
+
} else {
|
|
513
|
+
this.showToast(data?.error || 'Failed to update strategy', 'error');
|
|
514
|
+
}
|
|
515
|
+
},
|
|
516
|
+
|
|
517
|
+
async loadRawProxyLogsSetting() {
|
|
518
|
+
const { ok, data } = await this.api('/settings/raw-proxy-logs');
|
|
519
|
+
if (ok && typeof data?.logRawProxyPayloads === 'boolean') {
|
|
520
|
+
this.logRawProxyPayloads = data.logRawProxyPayloads;
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
|
|
524
|
+
async setRawProxyLogs(enabled) {
|
|
525
|
+
if (this.rawProxyLogsSaving || this.logRawProxyPayloads === enabled) return;
|
|
526
|
+
this.rawProxyLogsSaving = true;
|
|
527
|
+
const { ok, data } = await this.api('/settings/raw-proxy-logs', {
|
|
528
|
+
method: 'POST',
|
|
529
|
+
body: JSON.stringify({ enabled })
|
|
530
|
+
});
|
|
531
|
+
this.rawProxyLogsSaving = false;
|
|
532
|
+
|
|
533
|
+
if (ok && typeof data?.logRawProxyPayloads === 'boolean') {
|
|
534
|
+
this.logRawProxyPayloads = data.logRawProxyPayloads;
|
|
535
|
+
this.showToast(data.logRawProxyPayloads ? 'Raw proxy payload logging enabled' : 'Raw proxy payload logging disabled', 'success');
|
|
536
|
+
} else {
|
|
537
|
+
this.showToast(data?.error || 'Failed to update raw proxy logging', 'error');
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
|
|
541
|
+
async loadModelMapSetting() {
|
|
542
|
+
this.modelMapLoading = true;
|
|
543
|
+
const { ok, data } = await this.api('/settings/model-map');
|
|
544
|
+
this.modelMapLoading = false;
|
|
545
|
+
|
|
546
|
+
if (ok && data?.modelMap) {
|
|
547
|
+
this.modelMappings = {
|
|
548
|
+
sonnet: data.modelMap.sonnet || this.modelMappings.sonnet,
|
|
549
|
+
opus: data.modelMap.opus || this.modelMappings.opus,
|
|
550
|
+
haiku: data.modelMap.haiku || this.modelMappings.haiku
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
|
|
555
|
+
async saveModelMapSetting() {
|
|
556
|
+
if (this.modelMapSaving) return;
|
|
557
|
+
|
|
558
|
+
const modelMap = {
|
|
559
|
+
sonnet: this.modelMappings.sonnet.trim(),
|
|
560
|
+
opus: this.modelMappings.opus.trim(),
|
|
561
|
+
haiku: this.modelMappings.haiku.trim()
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
if (!modelMap.sonnet || !modelMap.opus || !modelMap.haiku) {
|
|
565
|
+
this.showToast('All Claude model mappings are required.', 'error');
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
this.modelMapSaving = true;
|
|
570
|
+
const { ok, data } = await this.api('/settings/model-map', {
|
|
571
|
+
method: 'POST',
|
|
572
|
+
body: JSON.stringify({
|
|
573
|
+
modelMap,
|
|
574
|
+
reset: true
|
|
575
|
+
})
|
|
576
|
+
});
|
|
577
|
+
this.modelMapSaving = false;
|
|
578
|
+
|
|
579
|
+
if (ok && data?.modelMap) {
|
|
580
|
+
this.modelMappings = {
|
|
581
|
+
sonnet: data.modelMap.sonnet || modelMap.sonnet,
|
|
582
|
+
opus: data.modelMap.opus || modelMap.opus,
|
|
583
|
+
haiku: data.modelMap.haiku || modelMap.haiku
|
|
584
|
+
};
|
|
585
|
+
this.showToast('Model mapping updated', 'success');
|
|
586
|
+
} else {
|
|
587
|
+
this.showToast(data?.error || 'Failed to update model mapping', 'error');
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
|
|
591
|
+
async resetModelMapSetting() {
|
|
592
|
+
if (this.modelMapSaving) return;
|
|
593
|
+
this.modelMapSaving = true;
|
|
594
|
+
const { ok, data } = await this.api('/settings/model-map', {
|
|
595
|
+
method: 'POST',
|
|
596
|
+
body: JSON.stringify({ reset: true })
|
|
597
|
+
});
|
|
598
|
+
this.modelMapSaving = false;
|
|
599
|
+
|
|
600
|
+
if (ok && data?.modelMap) {
|
|
601
|
+
this.modelMappings = {
|
|
602
|
+
sonnet: data.modelMap.sonnet || 'gpt-5.5',
|
|
603
|
+
opus: data.modelMap.opus || 'gpt-5.5',
|
|
604
|
+
haiku: data.modelMap.haiku || 'gpt-5.4-mini'
|
|
605
|
+
};
|
|
606
|
+
this.showToast('Model mapping reset', 'success');
|
|
607
|
+
} else {
|
|
608
|
+
this.showToast(data?.error || 'Failed to reset model mapping', 'error');
|
|
609
|
+
}
|
|
610
|
+
},
|
|
611
|
+
|
|
612
|
+
async loadClientApiKeys() {
|
|
613
|
+
this.clientApiKeysLoading = true;
|
|
614
|
+
const { ok, data } = await this.api('/settings/client-api-keys');
|
|
615
|
+
this.clientApiKeysLoading = false;
|
|
616
|
+
if (ok && Array.isArray(data?.clientApiKeys)) {
|
|
617
|
+
this.clientApiKeys = data.clientApiKeys;
|
|
618
|
+
} else {
|
|
619
|
+
this.showToast(data?.error || 'Failed to load client API keys', 'error');
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
|
|
623
|
+
async createClientApiKey() {
|
|
624
|
+
if (this.clientApiKeySaving) return;
|
|
625
|
+
this.clientApiKeySaving = true;
|
|
626
|
+
const { ok, data } = await this.api('/settings/client-api-keys', {
|
|
627
|
+
method: 'POST',
|
|
628
|
+
body: JSON.stringify({
|
|
629
|
+
name: this.clientApiKeyName,
|
|
630
|
+
remark: this.clientApiKeyRemark
|
|
631
|
+
})
|
|
632
|
+
});
|
|
633
|
+
this.clientApiKeySaving = false;
|
|
634
|
+
|
|
635
|
+
if (ok && data?.clientApiKey?.key) {
|
|
636
|
+
this.revealedClientApiKey = data.clientApiKey.key;
|
|
637
|
+
this.clientApiKeyName = '';
|
|
638
|
+
this.clientApiKeyRemark = '';
|
|
639
|
+
this.showCreateClientApiKeyModal = false;
|
|
640
|
+
await this.loadClientApiKeys();
|
|
641
|
+
this.showToast('Client API key created. Copy it now; it will not be shown again.', 'success');
|
|
642
|
+
} else {
|
|
643
|
+
this.showToast(data?.error || 'Failed to create client API key', 'error');
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
|
|
647
|
+
async updateClientApiKey(clientApiKey) {
|
|
648
|
+
if (this.clientApiKeySaving) return;
|
|
649
|
+
this.clientApiKeySaving = true;
|
|
650
|
+
const { ok, data } = await this.api(`/settings/client-api-keys/${encodeURIComponent(clientApiKey.id)}`, {
|
|
651
|
+
method: 'PUT',
|
|
652
|
+
body: JSON.stringify({ name: clientApiKey.name, remark: clientApiKey.remark })
|
|
653
|
+
});
|
|
654
|
+
this.clientApiKeySaving = false;
|
|
655
|
+
if (ok) {
|
|
656
|
+
this.showToast('Client API key updated', 'success');
|
|
657
|
+
await this.loadClientApiKeys();
|
|
658
|
+
} else {
|
|
659
|
+
this.showToast(data?.error || 'Failed to update client API key', 'error');
|
|
660
|
+
}
|
|
661
|
+
},
|
|
662
|
+
|
|
663
|
+
async deleteClientApiKey(clientApiKey) {
|
|
664
|
+
if (!confirm(`Delete the key for "${clientApiKey.name || clientApiKey.remark || 'this client'}"? Connected clients will lose access immediately.`)) return;
|
|
665
|
+
this.clientApiKeySaving = true;
|
|
666
|
+
const { ok, data } = await this.api(`/settings/client-api-keys/${encodeURIComponent(clientApiKey.id)}`, { method: 'DELETE' });
|
|
667
|
+
this.clientApiKeySaving = false;
|
|
668
|
+
if (ok) {
|
|
669
|
+
await this.loadClientApiKeys();
|
|
670
|
+
this.showToast('Client API key deleted', 'success');
|
|
671
|
+
} else {
|
|
672
|
+
this.showToast(data?.error || 'Failed to delete client API key', 'error');
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
|
|
676
|
+
async setClaudeCodeProxyTestConfig() {
|
|
677
|
+
const { ok, data, error } = await this.api('/claude/config/set', {
|
|
678
|
+
method: 'POST',
|
|
679
|
+
body: JSON.stringify({
|
|
680
|
+
apiUrl: this.serverUrl,
|
|
681
|
+
apiKey: 'test'
|
|
682
|
+
})
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
if (ok && data?.success) {
|
|
686
|
+
this.showToast('Updated Claude Code settings.json (API URL + API key).', 'success');
|
|
687
|
+
} else {
|
|
688
|
+
this.showToast(data?.error || error || 'Failed to update Claude Code settings.json', 'error');
|
|
689
|
+
}
|
|
690
|
+
},
|
|
691
|
+
|
|
692
|
+
showToast(message, type = 'success') {
|
|
693
|
+
this.toast = { message, type };
|
|
694
|
+
setTimeout(() => { this.toast = null; }, 3000);
|
|
695
|
+
},
|
|
696
|
+
|
|
697
|
+
startLogStream() {
|
|
698
|
+
if (!this.isAuthenticated) return;
|
|
699
|
+
if (this.logEventSource) this.logEventSource.close();
|
|
700
|
+
|
|
701
|
+
const params = new URLSearchParams({ history: 'true' });
|
|
702
|
+
if (this.apiKey) params.set('api_key', this.apiKey);
|
|
703
|
+
this.logEventSource = new EventSource(`/api/logs/stream?${params.toString()}`);
|
|
704
|
+
this.logEventSource.onmessage = (event) => {
|
|
705
|
+
try {
|
|
706
|
+
const log = JSON.parse(event.data);
|
|
707
|
+
this.logs.unshift(log);
|
|
708
|
+
|
|
709
|
+
if (this.logs.length > 500) {
|
|
710
|
+
this.logs = this.logs.slice(0, 500);
|
|
711
|
+
}
|
|
712
|
+
} catch (e) {}
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
this.logEventSource.onerror = () => {
|
|
716
|
+
setTimeout(() => this.startLogStream(), 3000);
|
|
717
|
+
};
|
|
718
|
+
},
|
|
719
|
+
|
|
720
|
+
clearLogs() {
|
|
721
|
+
this.logs = [];
|
|
722
|
+
},
|
|
723
|
+
|
|
724
|
+
formatLogMessage(message) {
|
|
725
|
+
if (!message) return '';
|
|
726
|
+
const match = message.match(/^\[(\w+)\]\s*/);
|
|
727
|
+
if (match) {
|
|
728
|
+
return message.replace(match[0], '');
|
|
729
|
+
}
|
|
730
|
+
return message;
|
|
731
|
+
},
|
|
732
|
+
|
|
733
|
+
getLogDetails(message) {
|
|
734
|
+
if (!message) return null;
|
|
735
|
+
const details = {};
|
|
736
|
+
|
|
737
|
+
const patterns = [
|
|
738
|
+
['model', /model=([^\s|,]+)/],
|
|
739
|
+
['account', /account=([^\s|,]+)/],
|
|
740
|
+
['stream', /stream=(true|false)/],
|
|
741
|
+
['messages', /messages=(\d+)/],
|
|
742
|
+
['tools', /tools=(\d+)/],
|
|
743
|
+
['tokens', /tokens=(\d+)/],
|
|
744
|
+
['cache_read_input_tokens', /cache_read_input_tokens=(\d+)/],
|
|
745
|
+
['input_tokens', /input_tokens=(\d+)/],
|
|
746
|
+
['output_tokens', /output_tokens=(\d+)/],
|
|
747
|
+
['duration', /(\d+)ms/],
|
|
748
|
+
['status', /status=(\d+)/],
|
|
749
|
+
['error', /error=([^\s|]+)/]
|
|
750
|
+
];
|
|
751
|
+
|
|
752
|
+
for (const [key, pattern] of patterns) {
|
|
753
|
+
const match = message.match(pattern);
|
|
754
|
+
if (match) {
|
|
755
|
+
details[key] = match[1];
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
return Object.keys(details).length > 0 ? details : null;
|
|
760
|
+
}
|
|
125
761
|
}));
|
|
126
762
|
});
|