Update app.js
Browse files
app.js
CHANGED
@@ -10,6 +10,8 @@ const NOTDIAMOND_HEADERS = {
|
|
10 |
'Content-Type': 'application/json',
|
11 |
'next-action': '8189eb37107121e024940f588629a394a594e6a4'
|
12 |
};
|
|
|
|
|
13 |
|
14 |
const DEFAULT_MODEL = 'gpt-4-turbo';
|
15 |
|
@@ -60,10 +62,42 @@ const MODEL_MAPPING = {
|
|
60 |
}
|
61 |
};
|
62 |
|
63 |
-
function
|
64 |
const authHeader = req.headers['authorization'];
|
65 |
if (authHeader && authHeader.startsWith('Bearer ')) {
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
}
|
68 |
return null;
|
69 |
}
|
@@ -188,26 +222,30 @@ app.get('/', (req, res) => {
|
|
188 |
});
|
189 |
});
|
190 |
|
191 |
-
app.get('/ai/v1/models', (req, res) => {
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
196 |
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
|
210 |
-
|
|
|
|
|
|
|
211 |
});
|
212 |
|
213 |
app.post('/ai/v1/chat/completions', async (req, res) => {
|
@@ -217,8 +255,8 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
|
|
217 |
let status = 200;
|
218 |
|
219 |
try {
|
220 |
-
const
|
221 |
-
if (!
|
222 |
status = 401;
|
223 |
throw new Error('Unauthorized');
|
224 |
}
|
@@ -251,7 +289,7 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
|
|
251 |
const headers = {
|
252 |
'Content-Type': 'application/json',
|
253 |
'next-action': '4e63dabc37fef18cae74cbfd41d1bace49acf47e',
|
254 |
-
'
|
255 |
};
|
256 |
|
257 |
const response = await fetch(NOTDIAMOND_URL, {
|
|
|
10 |
'Content-Type': 'application/json',
|
11 |
'next-action': '8189eb37107121e024940f588629a394a594e6a4'
|
12 |
};
|
13 |
+
const SUPABASE_URL = 'https://spuckhogycrxcbomznwo.supabase.co';
|
14 |
+
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNwdWNraG9neWNyeGNib216bndvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDcyNDYwMzksImV4cCI6MjAyMjgyMjAzOX0.tvlGT7NZY8bijMjNIu1WhAtPnSKuDeYhtveo4DRt6xg';
|
15 |
|
16 |
const DEFAULT_MODEL = 'gpt-4-turbo';
|
17 |
|
|
|
62 |
}
|
63 |
};
|
64 |
|
65 |
+
async function getAuthCookie(req) {
|
66 |
const authHeader = req.headers['authorization'];
|
67 |
if (authHeader && authHeader.startsWith('Bearer ')) {
|
68 |
+
const [email, password] = authHeader.slice(7).split('|');
|
69 |
+
if (!email || !password) {
|
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: {
|
76 |
+
'accept': '*/*',
|
77 |
+
'accept-language': 'zh-CN,zh;q=0.9',
|
78 |
+
'apikey': SUPABASE_KEY,
|
79 |
+
'authorization': `Bearer ${SUPABASE_KEY}`,
|
80 |
+
'content-type': 'application/json;charset=UTF-8',
|
81 |
+
'origin': 'https://chat.notdiamond.ai',
|
82 |
+
'referer': 'https://chat.notdiamond.ai/',
|
83 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
|
84 |
+
'x-client-info': 'supabase-ssr/0.4.0',
|
85 |
+
'x-supabase-api-version': '2024-01-01'
|
86 |
+
},
|
87 |
+
body: JSON.stringify({
|
88 |
+
email: email,
|
89 |
+
password: password,
|
90 |
+
gotrue_meta_security: {}
|
91 |
+
})
|
92 |
+
});
|
93 |
+
|
94 |
+
if (!response.ok) {
|
95 |
+
throw new Error('Authentication failed');
|
96 |
+
}
|
97 |
+
|
98 |
+
const responseData = await response.text();
|
99 |
+
const baseCookie = Buffer.from(responseData).toString('base64');
|
100 |
+
return `sb-spuckhogycrxcbomznwo-auth-token=base64-${baseCookie}`;
|
101 |
}
|
102 |
return null;
|
103 |
}
|
|
|
222 |
});
|
223 |
});
|
224 |
|
225 |
+
app.get('/ai/v1/models', async (req, res) => {
|
226 |
+
try {
|
227 |
+
const authCookie = await getAuthCookie(req);
|
228 |
+
if (!authCookie) {
|
229 |
+
return res.status(401).json({ error: 'Unauthorized' });
|
230 |
+
}
|
231 |
|
232 |
+
res.json({
|
233 |
+
object: "list",
|
234 |
+
data: Object.entries(MODEL_MAPPING).map(([id, info]) => ({
|
235 |
+
id: id,
|
236 |
+
object: "model",
|
237 |
+
created: Math.floor(Date.now() / 1000),
|
238 |
+
owned_by: info.provider,
|
239 |
+
permission: [],
|
240 |
+
root: id,
|
241 |
+
parent: null,
|
242 |
+
}))
|
243 |
+
});
|
244 |
|
245 |
+
console.log(`${new Date().toISOString()} - ${req.ip || req.connection.remoteAddress} - GET /ai/v1/models - 200`);
|
246 |
+
} catch (error) {
|
247 |
+
res.status(401).json({ error: 'Authentication failed', details: error.message });
|
248 |
+
}
|
249 |
});
|
250 |
|
251 |
app.post('/ai/v1/chat/completions', async (req, res) => {
|
|
|
255 |
let status = 200;
|
256 |
|
257 |
try {
|
258 |
+
const authCookie = await getAuthCookie(req);
|
259 |
+
if (!authCookie) {
|
260 |
status = 401;
|
261 |
throw new Error('Unauthorized');
|
262 |
}
|
|
|
289 |
const headers = {
|
290 |
'Content-Type': 'application/json',
|
291 |
'next-action': '4e63dabc37fef18cae74cbfd41d1bace49acf47e',
|
292 |
+
'Cookie': authCookie
|
293 |
};
|
294 |
|
295 |
const response = await fetch(NOTDIAMOND_URL, {
|