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
36 lines (32 loc) · 1.05 KB
/
Copy pathindex.ts
File metadata and controls
36 lines (32 loc) · 1.05 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
import http from "http";
import { CopilotRuntime, 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",
};
process.env.OPENAI_API_KEY = "";
const server = http.createServer((request, response) => {
try {
const headers = {
...HEADERS,
...(request.method === "POST" && { "Content-Type": "application/json" }),
};
response.writeHead(200, headers);
if (request.method == "POST") {
const copilotKit = new CopilotRuntime();
const openaiAdapter = new OpenAIAdapter();
copilotKit.streamHttpServerResponse(request, response, openaiAdapter);
} 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}`);