-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
50 lines (43 loc) · 1.23 KB
/
server.mjs
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
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import fs from "fs";
import {
CopilotRuntime,
OpenAIAdapter,
copilotRuntimeNodeHttpEndpoint,
} from "@copilotkit/runtime";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "your-api-key",
});
const serviceAdapter = new OpenAIAdapter({ openai });
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
app.use("/copilotkit", (req, res, next) => {
const runtime = new CopilotRuntime();
const handler = copilotRuntimeNodeHttpEndpoint({
endpoint: "/copilotkit",
runtime,
serviceAdapter,
});
return handler(req, res, next);
});
app.post("/create", (req, res, next) => {
console.log("req=", req.body);
const { path, name, ext, code } = req.body;
try {
const outputFile = `${path}/${name}.${ext}`;
if (!fs.existsSync(path)) fs.mkdirSync(path, { recursive: true });
fs.writeFileSync(outputFile, code);
res.status(200).send("file created successfully");
} catch (e) {
res.status(500).send(e.message);
console.log("error=", e.message);
}
});
app.listen(4000, () => {
console.log("Listening at http://localhost:4000");
});