forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
34 lines (31 loc) · 1.08 KB
/
Copy pathindex.ts
File metadata and controls
34 lines (31 loc) · 1.08 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
import http from "http";
import { CopilotBackend, OpenAIAdapter } from "@copilotkit/backend";
const HEADERS = {
// make sure to modify CORS headers to match your frontend's origin
"Access-Control-Allow-Origin": "http://localhost:3000",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
const server = http.createServer((request, response) => {
try {
const headers =
request.method === "POST" ? { ...HEADERS, "Content-Type": "application/json" } : HEADERS;
response.writeHead(200, headers);
if (request.method == "POST") {
const copilotKit = new CopilotBackend();
console.log("streaming response");
copilotKit.streamHttpServerResponse(request, response, new OpenAIAdapter({})).then(() => {
console.log("streaming response done");
});
} else {
response.end("openai server");
}
} catch (err) {
console.error(err);
response.end("error");
}
});
const port = 4201;
const host = "localhost";
server.listen(port, host);
console.log(`Listening at http://${host}:${port}`);