-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathmain.ts
More file actions
573 lines (494 loc) · 16.9 KB
/
main.ts
File metadata and controls
573 lines (494 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Import required modules
import { Application, Router, Context, Request } from "https://deno.land/x/oak@v12.6.1/mod.ts";
import { config } from "https://deno.land/x/dotenv@v3.2.2/mod.ts";
// Load environment variables
const env = config();
// Configure logging
const logger = {
info: (message: string) => console.log(`INFO: ${new Date().toISOString()} - ${message}`),
warning: (message: string) => console.warn(`WARNING: ${new Date().toISOString()} - ${message}`),
error: (message: string) => console.error(`ERROR: ${new Date().toISOString()} - ${message}`),
exception: (message: string) => console.error(`EXCEPTION: ${new Date().toISOString()} - ${message}`)
};
// Create Oak application
const app = new Application();
// CORS middleware
app.use(async (ctx, next) => {
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
ctx.response.headers.set("Access-Control-Allow-Methods", "*");
ctx.response.headers.set("Access-Control-Allow-Headers", "*");
ctx.response.headers.set("Access-Control-Allow-Credentials", "true");
if (ctx.request.method === "OPTIONS") {
ctx.response.status = 204;
return;
}
await next();
});
// Configuration
const DEEPSIDER_API_BASE = "https://api.chargpt.ai/api/v2";
let TOKEN_INDEX = 0;
// Model mapping table
const MODEL_MAPPING: Record<string, string> = {
"gpt-4o-mini": "openai/gpt-4o-mini",
"gpt-4o": "openai/gpt-4o",
"gpt-4o-image": "openai/gpt-4o-image",
"o1": "openai/o1",
"o3-mini": "oopenai/o3-mini",
"claude-3.5-sonnet": "anthropic/claude-3.5-sonnet",
"claude-3.7-sonnet": "anthropic/claude-3.7-sonnet",
"grok-3": "x-ai/grok-3",
"grok-3-reasoner":"x-ai/grok-3-reasoner",
"deepseek-v3":"deepseek/deepseek-chat",
"deepseek-r1":"deepseek/deepseek-r1",
"gemini-2.0-flash":"google/gemini-2.0-flash",
"gemini-2.0-pro-exp":"google/gemini-2.0-pro-exp-02-05",
"gemini-2.0-flash-thinking-exp":"google/gemini-2.0-flash-thinking-exp-1219",
"qwq-32b":"qwen/qwq-32b",
"qwen-max":"qwen/qwen-max"
};
// TypeScript interfaces
interface ChatMessage {
role: string;
content: string;
name?: string;
}
interface ChatCompletionRequest {
model: string;
messages: ChatMessage[];
temperature?: number;
top_p?: number;
n?: number;
stream?: boolean;
stop?: string[] | string;
max_tokens?: number;
presence_penalty?: number;
frequency_penalty?: number;
user?: string;
}
// Helper functions
function getHeaders(apiKey: string): Record<string, string> {
// Check if multiple tokens are provided (comma-separated)
const tokens = apiKey.split(',');
let currentToken: string;
if (tokens.length > 0) {
// Rotate tokens
currentToken = tokens[TOKEN_INDEX % tokens.length];
TOKEN_INDEX = (TOKEN_INDEX + 1) % tokens.length;
} else {
currentToken = apiKey;
}
return {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
"content-type": "application/json",
"origin": "chrome-extension://client",
"i-lang": "zh-CN",
"i-version": "1.1.64",
"sec-ch-ua": '"Chromium";v="134", "Not:A-Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "Windows",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
"authorization": `Bearer ${currentToken.trim()}`
};
}
function verifyApiKey(ctx: Context): string | null {
const authHeader = ctx.request.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Bearer ")) {
ctx.response.status = 401;
ctx.response.body = { detail: "Invalid API key format" };
return null;
}
return authHeader.replace("Bearer ", "");
}
function mapOpenaiToDeepsiderModel(model: string): string {
return MODEL_MAPPING[model] || "anthropic/claude-3.7-sonnet";
}
function formatMessagesForDeepsider(messages: ChatMessage[]): string {
let prompt = "";
for (const msg of messages) {
const role = msg.role;
// Map OpenAI roles to DeepSider format
if (role === "system") {
// System messages at the beginning as guidance
prompt = `${msg.content}\n\n` + prompt;
} else if (role === "user") {
prompt += `Human: ${msg.content}\n\n`;
} else if (role === "assistant") {
prompt += `Assistant: ${msg.content}\n\n`;
} else {
// Other roles treated as user
prompt += `Human (${role}): ${msg.content}\n\n`;
}
}
// If the last message is not from the user, add a Human prefix to prompt a response
if (messages.length > 0 && messages[messages.length - 1].role !== "user") {
prompt += "Human: ";
}
return prompt.trim();
}
async function generateOpenaiResponse(fullResponse: string, requestId: string, model: string): Promise<Record<string, any>> {
const timestamp = Math.floor(Date.now() / 1000);
return {
"id": `chatcmpl-${requestId}`,
"object": "chat.completion",
"created": timestamp,
"model": model,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": fullResponse
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0, // Cannot calculate accurately
"completion_tokens": 0, // Cannot calculate accurately
"total_tokens": 0 // Cannot calculate accurately
}
};
}
async function* streamOpenaiResponse(response: Response, requestId: string, model: string, apiKey: string, tokenIndex: number): AsyncGenerator<string> {
const timestamp = Math.floor(Date.now() / 1000);
let fullResponse = "";
try {
// Get the reader from the response body
const reader = response.body?.getReader();
if (!reader) {
throw new Error("Response body cannot be read");
}
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
// Decode the chunk and add it to the buffer
buffer += decoder.decode(value, { stream: true });
// Process complete lines
const lines = buffer.split("\n");
buffer = lines.pop() || ""; // Keep the last incomplete line in the buffer
for (const line of lines) {
if (!line.trim()) continue;
if (line.startsWith("data: ")) {
try {
const data = JSON.parse(line.substring(6));
if (data.code === 202 && data.data?.type === "chat") {
// Get the content
const content = data.data?.content || '';
if (content) {
fullResponse += content;
// Generate OpenAI format streaming response
const chunk = {
"id": `chatcmpl-${requestId}`,
"object": "chat.completion.chunk",
"created": timestamp,
"model": model,
"choices": [
{
"index": 0,
"delta": {
"content": content
},
"finish_reason": null
}
]
};
yield `data: ${JSON.stringify(chunk)}\n\n`;
}
} else if (data.code === 203) {
// Send completion signal
const chunk = {
"id": `chatcmpl-${requestId}`,
"object": "chat.completion.chunk",
"created": timestamp,
"model": model,
"choices": [
{
"index": 0,
"delta": {},
"finish_reason": "stop"
}
]
};
yield `data: ${JSON.stringify(chunk)}\n\n`;
yield "data: [DONE]\n\n";
}
} catch (e) {
logger.warning(`Cannot parse response: ${line}`);
}
}
}
}
} catch (e) {
logger.error(`Error processing streaming response: ${e instanceof Error ? e.message : String(e)}`);
// Try using the next token
const tokens = apiKey.split(',');
if (tokens.length > 1) {
logger.info(`Trying to retry with the next token`);
// We don't implement auto-retry here, just log the error
}
// Return error message
const errorChunk = {
"id": `chatcmpl-${requestId}`,
"object": "chat.completion.chunk",
"created": timestamp,
"model": model,
"choices": [
{
"index": 0,
"delta": {
"content": `\n\n[Error processing response: ${e instanceof Error ? e.message : String(e)}]`
},
"finish_reason": "stop"
}
]
};
yield `data: ${JSON.stringify(errorChunk)}\n\n`;
yield "data: [DONE]\n\n";
}
}
// Check account balance function
async function checkAccountBalance(apiKey: string, tokenIndex: number | null = null): Promise<[boolean, Record<string, any>]> {
const tokens = apiKey.split(',');
// If token_index is provided and valid, use the specified token
let currentToken: string;
if (tokenIndex !== null && tokens.length > tokenIndex) {
currentToken = tokens[tokenIndex].trim();
} else {
// Otherwise use the first token
currentToken = tokens.length ? tokens[0].trim() : apiKey;
}
const headers = {
"accept": "*/*",
"content-type": "application/json",
"authorization": `Bearer ${currentToken}`
};
try {
// Get account balance info
const response = await fetch(
`${DEEPSIDER_API_BASE.replace('/v2', '')}/quota/retrieve`,
{ headers }
);
if (response.status === 200) {
const data = await response.json();
if (data.code === 0) {
const quotaList = data.data?.list || [];
// Parse balance info
const quotaInfo: Record<string, any> = {};
for (const item of quotaList) {
const itemType = item.type || '';
const available = item.available || 0;
quotaInfo[itemType] = {
"total": item.total || 0,
"available": available,
"title": item.title || ''
};
}
return [true, quotaInfo];
}
}
return [false, {}];
} catch (e) {
logger.warning(`Error checking account balance: ${e instanceof Error ? e.message : String(e)}`);
return [false, {}];
}
}
// Create router
const router = new Router();
// Routes
router.get("/", (ctx) => {
ctx.response.body = { message: "OpenAI API Proxy service is running, connected to DeepSider API" };
});
router.get("/v1/models", async (ctx) => {
const apiKey = verifyApiKey(ctx);
if (!apiKey) return;
const models = [];
for (const openaiModel in MODEL_MAPPING) {
models.push({
"id": openaiModel,
"object": "model",
"created": Math.floor(Date.now() / 1000),
"owned_by": "openai-proxy"
});
}
ctx.response.body = {
"object": "list",
"data": models
};
});
router.post("/v1/chat/completions", async (ctx) => {
const apiKey = verifyApiKey(ctx);
if (!apiKey) return;
// Parse request body
const body = await ctx.request.body().value;
const chatRequest: ChatCompletionRequest = body;
// Generate unique request ID
const now = new Date();
const timestamp = now.getTime().toString();
const requestId = now.toISOString().replace(/[-:T.Z]/g, '').substring(0, 14) +
timestamp.substring(timestamp.length - 6);
// Map model
const deepsiderModel = mapOpenaiToDeepsiderModel(chatRequest.model);
// Prepare prompt for DeepSider API
const prompt = formatMessagesForDeepsider(chatRequest.messages);
// Prepare request payload
const payload = {
"model": deepsiderModel,
"prompt": prompt,
"webAccess": "close", // Default: web access off
"timezone": "Asia/Shanghai"
};
// Get request headers (with selected token)
const headers = getHeaders(apiKey);
// Get current token index
const tokens = apiKey.split(',');
const currentTokenIndex = tokens.length > 0 ? (TOKEN_INDEX - 1) % tokens.length : 0;
try {
// Send request to DeepSider API
const response = await fetch(
`${DEEPSIDER_API_BASE}/chat/conversation`,
{
method: "POST",
headers,
body: JSON.stringify(payload)
}
);
// Check response status
if (response.status !== 200) {
let errorMsg = `DeepSider API request failed: ${response.status}`;
try {
const errorData = await response.json();
errorMsg += ` - ${errorData.message || ''}`;
} catch {
errorMsg += ` - ${await response.text()}`;
}
logger.error(errorMsg);
ctx.response.status = response.status;
ctx.response.body = { detail: "API request failed" };
return;
}
// Handle streaming or non-streaming response
if (chatRequest.stream) {
// Set up streaming response
ctx.response.type = "text/event-stream";
const streamGenerator = streamOpenaiResponse(response, requestId, chatRequest.model, apiKey, currentTokenIndex);
const body = new ReadableStream({
async start(controller) {
try {
for await (const chunk of streamGenerator) {
controller.enqueue(new TextEncoder().encode(chunk));
}
controller.close();
} catch (e) {
controller.error(e);
}
}
});
ctx.response.body = body;
} else {
// Collect full response
const reader = response.body?.getReader();
if (!reader) {
throw new Error("Response body cannot be read");
}
const decoder = new TextDecoder();
let buffer = "";
let fullResponse = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (!line.trim()) continue;
if (line.startsWith("data: ")) {
try {
const data = JSON.parse(line.substring(6));
if (data.code === 202 && data.data?.type === "chat") {
const content = data.data?.content || '';
if (content) {
fullResponse += content;
}
}
} catch (e) {
// Ignore JSON parse errors
}
}
}
}
// Return OpenAI format full response
ctx.response.body = await generateOpenaiResponse(fullResponse, requestId, chatRequest.model);
}
} catch (e) {
logger.exception(`Error processing request: ${e instanceof Error ? e.message : String(e)}`);
ctx.response.status = 500;
ctx.response.body = { detail: `Internal server error: ${e instanceof Error ? e.message : String(e)}` };
}
});
router.get("/admin/balance", async (ctx) => {
// Simple admin key check
const adminKey = ctx.request.headers.get("X-Admin-Key");
const expectedAdminKey = env.ADMIN_KEY || "admin";
if (!adminKey || adminKey !== expectedAdminKey) {
ctx.response.status = 403;
ctx.response.body = { detail: "Unauthorized" };
return;
}
// Get API key from headers
const authHeader = ctx.request.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Bearer ")) {
ctx.response.status = 401;
ctx.response.body = { detail: "Missing or invalid Authorization header" };
return;
}
const apiKey = authHeader.replace("Bearer ", "");
const tokens = apiKey.split(',');
const result: Record<string, any> = {};
// Get balance info for all tokens
for (let i = 0; i < tokens.length; i++) {
const tokenDisplay = `token_${i+1}`;
const [success, quotaInfo] = await checkAccountBalance(apiKey, i);
if (success) {
result[tokenDisplay] = {
"status": "success",
"quota": quotaInfo
};
} else {
result[tokenDisplay] = {
"status": "error",
"message": "Could not get account balance information"
};
}
}
ctx.response.body = result;
});
// Error handler for 404
router.all("(.*)", (ctx) => {
ctx.response.status = 404;
ctx.response.body = {
"error": {
"message": `Resource not found: ${ctx.request.url.pathname}`,
"type": "not_found_error",
"code": "not_found"
}
};
});
// Apply router
app.use(router.routes());
app.use(router.allowedMethods());
// Startup event
app.addEventListener("listen", () => {
logger.info(`OpenAI API proxy service started, ready to accept requests`);
logger.info(`Multiple token rotation supported, use comma-separated tokens in Authorization header`);
});
// Start server
const port = parseInt(env.PORT || "7860");
logger.info(`Starting OpenAI API proxy service on port: ${port}`);
await app.listen({ port });