-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
163 lines (143 loc) · 5.35 KB
/
server.ts
File metadata and controls
163 lines (143 loc) · 5.35 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
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import { DeepResearchEngine } from "./server/deepResearch";
import dotenv from "dotenv";
dotenv.config({ path: '.env.local' });
dotenv.config();
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json({ limit: '50mb' }));
// API routes FIRST
app.get("/api/health", (req, res) => {
res.json({ status: "ok" });
});
app.post("/api/deep-research", async (req, res) => {
try {
const { query, model } = req.body;
let apiKey = req.headers.authorization || "";
apiKey = apiKey.replace(/^(bearer\s*)+/ig, '').trim();
if (!apiKey || apiKey === "undefined" || apiKey === "") {
apiKey = (process.env.OLLAMA_CLOUD_API_KEY || process.env.OLLAMA_API_KEY || "").trim();
apiKey = apiKey.replace(/^(bearer\s*)+/ig, '').trim();
}
if (!query || !model || !apiKey) {
console.error("Missing required fields:", { hasQuery: !!query, hasModel: !!model, hasApiKey: !!apiKey });
return res.status(400).json({ error: `Missing required fields: query=${!!query}, model=${!!model}, apiKey=${!!apiKey}` });
}
res.setHeader("Content-Type", "application/json");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.setHeader("X-Accel-Buffering", "no");
res.flushHeaders();
const engine = new DeepResearchEngine(model, apiKey.trim());
for await (const chunk of engine.runDeepResearch(query)) {
res.write(chunk);
if (typeof (res as any).flush === 'function') {
(res as any).flush();
}
}
res.end();
} catch (error: any) {
console.error("Deep Research error:", error);
if (!res.headersSent) {
res.status(500).json({ error: error.message });
} else {
res.end();
}
}
});
app.post("/api/chat", async (req, res) => {
try {
let authHeader = req.headers.authorization || "";
let key = authHeader.replace(/^(bearer\s*)+/ig, '').trim();
if (!key || key === "undefined" || key === "") {
key = (process.env.OLLAMA_CLOUD_API_KEY || process.env.OLLAMA_API_KEY || "").trim();
key = key.replace(/^(bearer\s*)+/ig, '').trim();
}
authHeader = `Bearer ${key}`;
let endpoint = process.env.OLLAMA_ENDPOINT || "https://ollama.com/api/chat";
// If the user accidentally put the API key in the endpoint field, it will have an underscore and no slashes
if (endpoint.includes('_') && !endpoint.includes('/')) {
endpoint = "https://ollama.com/api/chat";
}
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
endpoint = 'http://' + endpoint;
}
// Validate URL to prevent Invalid URL crash
try {
new URL(endpoint);
} catch (e) {
endpoint = "https://ollama.com/api/chat";
}
let response;
try {
response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": authHeader,
},
body: JSON.stringify(req.body),
});
} catch (error: any) {
if (error.message && error.message.includes('Invalid URL')) {
return res.status(400).json({ error: `Invalid Ollama Endpoint URL: ${endpoint}` });
}
if (error.message && error.message.includes('ECONNREFUSED') && endpoint.includes('localhost')) {
return res.status(503).json({
error: "Connection refused. It looks like you are trying to connect to a local Ollama instance from inside the cloud environment. To use local models, you must download this project and run it on your own computer."
});
}
throw error;
}
if (!response.ok) {
return res.status(response.status).send(await response.text());
}
if (!response.body) {
return res.status(500).send("No response body");
}
// Stream the response back to the client
res.setHeader("Content-Type", response.headers.get("Content-Type") || "application/json");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders();
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
res.write(value);
if (typeof (res as any).flush === 'function') {
(res as any).flush();
}
}
res.end();
} catch (error: any) {
console.error("Proxy error:", error);
if (!res.headersSent) {
res.status(500).json({ error: error.message });
} else {
res.end();
}
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*all', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();