-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
127 lines (110 loc) · 3.99 KB
/
server.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
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
const express = require('express');
const OpenAIChatController = require('./chatgpt'); // Import your class
const ngrok = require('ngrok'); // Import ngrok
const app = express();
const port = 3000; // You can use any port that is free on your system
app.use(express.json()); // Middleware to parse JSON bodies
// Initialize the chat controller
const chatController = new OpenAIChatController();
chatController.initialize();
// Endpoint to initialize the browser and page
// app.post('/initialize', async (req, res) => {
// try {
// await chatController.initialize();
// res.status(200).send('Initialized');
// } catch (error) {
// res.status(500).send(error.message);
// }
// });
// Store client responses to resolve when 'end_turn' is emitted
let pendingResponses = [];
// Add a listener for the 'end_turn' event within the OpenAIChatController
chatController.on('end_turn', (content) => {
// Send the response back to all pending clients
pendingResponses.forEach(res => res.send(content));
pendingResponses = []; // Clear the responses after sending
});
// Start the server
app.listen(port, async () => {
console.log(`Server running on port ${port}`);
// (Optional) Start ngrok tunnel to expose the port
// try {
// const url = await ngrok.connect({
// proto: 'http', // http|tcp|tls, defaults to http
// addr: port, // port or network address, defaults to 80
// authtoken: '2XmsdAs8reYjtFEMmPTb7Vgg7Qj_29gyC5cJU4TjqHgcVVjAQ', // your ngrok authtoken from ngrok.com
// region: 'us' // one of ngrok's regions (us, eu, au, ap), defaults to us
// });
// console.log(`ngrok tunnel set up: ${url}`);
// } catch (error) {
// console.error(`Error occurred while trying to connect ngrok tunnel: ${error.message}`);
// }
});
// Endpoint to initialize the page
// this will open a new browser window of the chat and following requests will be in the same new chat
app.post('/new_page', async (req, res) => {
try {
// read request
const { closeOtherTabs } = req.body;
await chatController.newPage(closeOtherTabs);
res.status(200).send('New page created');
} catch (error) {
res.status(500).send(error.message);
}
});
// Endpoint to click new chat button
app.post('/new_chat', async (req, res) => {
try {
await chatController.clickNewChatButton();
res.status(200).send('New chat clicked');
} catch (error) {
res.status(500).send(error.message);
}
});
// Endpoint to send a message
app.post('/text', async (req, res) => {
const { message } = req.body;
try {
console.log(message);
await chatController.typeIntoPrompt(message);
res.status(200).send('prompt typed');
} catch (error) {
res.status(500).send(error.message);
}
});
// Endpoint to upload an image
app.post('/upload', async (req, res) => {
const { filePath } = req.body;
try {
await chatController.uploadImage(filePath);
res.status(200).send('Image uploaded');
} catch (error) {
res.status(500).send(error.message);
}
});
// Endpoint to click the send button
app.post('/send', async (req, res) => {
try {
await chatController.clickSendButton();
// Store the response object to resolve later
pendingResponses.push(res);
// Optionally add a timeout to release the response
setTimeout(() => {
if (pendingResponses.includes(res)) {
res.send('Timeout: No response received.');
pendingResponses = pendingResponses.filter(r => r !== res);
}
}, 60000); // 30-second timeout
} catch (error) {
res.status(500).send(error.message);
}
});
// Endpoint to close the browser
app.post('/close', async (req, res) => {
try {
await chatController.close();
res.status(200).send('Closed');
} catch (error) {
res.status(500).send(error.message);
}
});