Setup project correctly for development and release

This commit is contained in:
2025-06-28 00:45:24 -05:00
parent ba36877f03
commit 5b73f1bb47
14 changed files with 3352 additions and 1902 deletions

View File

@@ -1,9 +1,10 @@
import http from 'http';
import { sendChat, sendChatStream } from './chatwrapper';
import { mapRequest, mapResponse, mapStreamChunk } from './mapper';
import { config } from './config';
/* ── basic config ─────────────────────────────────────────────────── */
const PORT = Number(process.env.PORT ?? 11434);
const PORT = config.PORT;
/* ── CORS helper ──────────────────────────────────────────────────── */
function allowCors(res: http.ServerResponse) {
@@ -58,55 +59,52 @@ http
}
/* -------- /v1/models ---------- */
if (pathname === "/v1/models" || pathname === "/models") {
res.writeHead(200, { "Content-Type": "application/json" });
if (pathname === '/v1/models' || pathname === '/models') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
data: [
{
id: "gemini-2.5-pro",
object: "model",
owned_by: "google",
id: 'gemini-2.5-pro',
object: 'model',
owned_by: 'google',
},
],
})
}),
);
return;
}
/* ---- /v1/chat/completions ---- */
if (
(pathname === "/chat/completions" ||
(pathname === "/v1/chat/completions" ) && req.method === "POST")
(pathname === '/chat/completions' ||
(pathname === '/v1/chat/completions' ) && req.method === 'POST')
) {
const body = await readJSON(req, res);
console.log("Request body:", body);
if (!body) return;
try {
const { geminiReq, tools } = await mapRequest(body);
console.log("Mapped Gemini request:", geminiReq);
if (body.stream) {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
for await (const chunk of sendChatStream({ ...geminiReq, tools })) {
console.log("Stream chunk:", chunk);
res.write(`data: ${JSON.stringify(mapStreamChunk(chunk))}\n\n`);
}
res.end("data: [DONE]\n\n");
res.end('data: [DONE]\n\n');
} else {
const gResp = await sendChat({ ...geminiReq, tools });
res.writeHead(200, { "Content-Type": "application/json" });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(mapResponse(gResp)));
}
} catch (err: any) {
console.error("Proxy error ➜", err);
res.writeHead(500, { "Content-Type": "application/json" });
console.error('Proxy error ➜', err);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: { message: err.message } }));
}
return;