-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (48 loc) · 1.58 KB
/
Copy pathserver.js
File metadata and controls
58 lines (48 loc) · 1.58 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
import { warmIntrospection, getParsedFlags, getParsedModels } from './lib/introspection-cache.js';
import { handleRequest } from './lib/routes.js';
import { handleOpenAiRequest } from './lib/openai-routes.js';
const VERSION = '3.0.0';
const port = Number.parseInt(process.env.PORT || '3000', 10);
const host = process.env.HOST || '0.0.0.0';
warmIntrospection();
console.log(
`cursor-openai-server v${VERSION} — ${getParsedModels().length} models, ${getParsedFlags().length} flags`,
);
const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};
function addCors(res) {
for (const [k, v] of Object.entries(CORS_HEADERS)) {
res.headers.set(k, v);
}
return res;
}
Bun.serve({
port,
hostname: host,
async fetch(req) {
const method = req.method;
if (method === 'OPTIONS') {
return new Response(null, { status: 204, headers: { ...CORS_HEADERS } });
}
const url = new URL(req.url);
const path = url.pathname;
try {
if (path.startsWith('/v1/')) {
return addCors(await handleOpenAiRequest(req, { port, host }));
}
return addCors(await handleRequest(req, { port, host }));
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
return addCors(
new Response(JSON.stringify({ error: msg }), {
status: 500,
headers: { 'Content-Type': 'application/json; charset=utf-8' },
}),
);
}
},
});
console.log(`listening — http://${host}:${port}`);