-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (73 loc) · 2.04 KB
/
index.js
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
const { Server } = require("node:http");
const express = require("express");
const socketIO = require("socket.io");
const Agent = require("./agent");
const PORT = 3001;
const app = express();
const http = Server(app);
const io = socketIO(http, {
cors: {
origin: "http://localhost:3000",
},
});
io.on("connection", (socket) => {
console.log(`⚡: ${socket.id} user just connected!`);
const agent = new Agent();
socket.on("developPhaseInitialMessage", async () => {
const response = await agent.getUserStory();
console.log(response);
const { name, role, text } = JSON.parse(response);
socket.emit("GPTResponse", [
{
type: "AI",
name,
text,
role,
time: new Date().toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}),
id: `${socket.id}${Math.random()}`,
socketID: socket.id,
},
]);
});
socket.on("userMessage", async (data) => {
console.log("🚀 ~ file: index.js:20 ~ socket.on ~ data:", data);
let response;
if (data.phase && data.phase === "develop") {
response = await agent.chatWithUserInDevelopPhase(data.text, data.code);
} else if (data.phase && data.phase === "design") {
response = await agent.chatWithUserInDesignPhase(data.text);
}
console.log(response);
const { name, role, text } = JSON.parse(response);
socket.emit("GPTResponse", [
data,
{
type: "AI",
name,
text,
role,
time: new Date().toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}),
id: `${socket.id}${Math.random()}`,
socketID: socket.id,
},
]);
});
socket.on("disconnect", () => {
console.log(`🔥: ${socket.id} user disconnected`);
socket.disconnect();
});
});
app.get("/help", (req, res) => {
res.json({ message: "Reach out to [email protected]" });
});
http.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});