Update app.js
Browse files
app.js
CHANGED
@@ -13,9 +13,17 @@ const NOTDIAMOND_HEADERS = {
|
|
13 |
const SUPABASE_URL = 'https://spuckhogycrxcbomznwo.supabase.co';
|
14 |
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNwdWNraG9neWNyeGNib216bndvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDcyNDYwMzksImV4cCI6MjAyMjgyMjAzOX0.tvlGT7NZY8bijMjNIu1WhAtPnSKuDeYhtveo4DRt6xg';
|
15 |
|
16 |
-
const DEFAULT_MODEL = 'gpt-
|
17 |
|
18 |
const MODEL_MAPPING = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
"gpt-4-turbo": {
|
20 |
"provider": "openai",
|
21 |
"mapping": "gpt-4-turbo-2024-04-09"
|
@@ -48,20 +56,15 @@ const MODEL_MAPPING = {
|
|
48 |
"provider": "anthropic",
|
49 |
"mapping": "anthropic.claude-3-haiku-20240307-v1:0"
|
50 |
},
|
51 |
-
"gpt-4o-mini": {
|
52 |
-
"provider": "openai",
|
53 |
-
"mapping": "gpt-4o-mini"
|
54 |
-
},
|
55 |
-
"gpt-4o": {
|
56 |
-
"provider": "openai",
|
57 |
-
"mapping": "gpt-4o"
|
58 |
-
},
|
59 |
"mistral-large-2407": {
|
60 |
"provider": "mistral",
|
61 |
"mapping": "mistral.mistral-large-2407-v1:0"
|
62 |
}
|
63 |
};
|
64 |
|
|
|
|
|
|
|
65 |
async function getAuthCookie(req) {
|
66 |
const authHeader = req.headers['authorization'];
|
67 |
if (authHeader && authHeader.startsWith('Bearer ')) {
|
@@ -70,6 +73,13 @@ async function getAuthCookie(req) {
|
|
70 |
throw new Error('Invalid authentication format');
|
71 |
}
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
const response = await fetch(`${SUPABASE_URL}/auth/v1/token?grant_type=password`, {
|
74 |
method: 'POST',
|
75 |
headers: {
|
@@ -95,9 +105,17 @@ async function getAuthCookie(req) {
|
|
95 |
throw new Error('Authentication failed');
|
96 |
}
|
97 |
|
98 |
-
const responseData = await response.
|
99 |
-
const baseCookie = Buffer.from(responseData).toString('base64');
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
}
|
102 |
return null;
|
103 |
}
|
|
|
13 |
const SUPABASE_URL = 'https://spuckhogycrxcbomznwo.supabase.co';
|
14 |
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNwdWNraG9neWNyeGNib216bndvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDcyNDYwMzksImV4cCI6MjAyMjgyMjAzOX0.tvlGT7NZY8bijMjNIu1WhAtPnSKuDeYhtveo4DRt6xg';
|
15 |
|
16 |
+
const DEFAULT_MODEL = 'gpt-4o';
|
17 |
|
18 |
const MODEL_MAPPING = {
|
19 |
+
"gpt-4o-mini": {
|
20 |
+
"provider": "openai",
|
21 |
+
"mapping": "gpt-4o-mini"
|
22 |
+
},
|
23 |
+
"gpt-4o": {
|
24 |
+
"provider": "openai",
|
25 |
+
"mapping": "gpt-4o"
|
26 |
+
},
|
27 |
"gpt-4-turbo": {
|
28 |
"provider": "openai",
|
29 |
"mapping": "gpt-4-turbo-2024-04-09"
|
|
|
56 |
"provider": "anthropic",
|
57 |
"mapping": "anthropic.claude-3-haiku-20240307-v1:0"
|
58 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
"mistral-large-2407": {
|
60 |
"provider": "mistral",
|
61 |
"mapping": "mistral.mistral-large-2407-v1:0"
|
62 |
}
|
63 |
};
|
64 |
|
65 |
+
// 简单的内存缓存
|
66 |
+
const authCache = new Map();
|
67 |
+
|
68 |
async function getAuthCookie(req) {
|
69 |
const authHeader = req.headers['authorization'];
|
70 |
if (authHeader && authHeader.startsWith('Bearer ')) {
|
|
|
73 |
throw new Error('Invalid authentication format');
|
74 |
}
|
75 |
|
76 |
+
const cacheKey = `${email}:${password}`;
|
77 |
+
const cachedAuth = authCache.get(cacheKey);
|
78 |
+
|
79 |
+
if (cachedAuth && cachedAuth.expiresAt > Date.now()) {
|
80 |
+
return cachedAuth.cookie;
|
81 |
+
}
|
82 |
+
|
83 |
const response = await fetch(`${SUPABASE_URL}/auth/v1/token?grant_type=password`, {
|
84 |
method: 'POST',
|
85 |
headers: {
|
|
|
105 |
throw new Error('Authentication failed');
|
106 |
}
|
107 |
|
108 |
+
const responseData = await response.json();
|
109 |
+
const baseCookie = Buffer.from(JSON.stringify(responseData)).toString('base64');
|
110 |
+
const cookie = `sb-spuckhogycrxcbomznwo-auth-token=base64-${baseCookie}`;
|
111 |
+
|
112 |
+
// 缓存认证信息
|
113 |
+
authCache.set(cacheKey, {
|
114 |
+
cookie: cookie,
|
115 |
+
expiresAt: Date.now() + (responseData.expires_in * 1000) // 转换为毫秒
|
116 |
+
});
|
117 |
+
|
118 |
+
return cookie;
|
119 |
}
|
120 |
return null;
|
121 |
}
|