-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
126 lines (104 loc) · 4.16 KB
/
index.ts
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
/**
* This file contains the main entry point of the application.
* It initializes the necessary dependencies, defines the command line interface,
* and starts the backend server.
*/
import puppeteer, { Browser } from 'puppeteer';
import { Command } from 'commander';
import { Elysia } from 'elysia';
import { sendMessageHandler } from './routes';
import chatgpt from './api/chatgpt';
var store: Store;
// Create a new command using the Command class
const program = new Command();
async function defaultInit() {
// return a promise object for the broser
// const browser = await puppeteer.launch({
// headless: 'new',
// // `headless: true` (default) enables old Headless;
// // `headless: 'new'` enables new Headless;
// // `headless: false` enables "headful" mode.
// });
const { browserManager, pageManager } = await chatgpt.createSession();
store = {
puppeteer: {
browserManager,
pageManager,
auth: {
username: process.env['CHATGPT_LOGIN'] || '',
password: process.env['CHATGPT_PASSWORD'] || ''
}
}
};
await chatgpt.loadBaseURL(store);
// login to chatgpt (might get asked to enter the 2FA code)
// await chatgpt.login(store);
// logged in now - session should be active and maintained in the server store
}
const isChatResponse = (response: any): response is NewChatResponse => typeof response.user === "string" && response.assistant === "string";
/**
* Makes a post request to the server using http and retrieves the answer to a given question.
* The server should be running on localhost:5234 and the endpoint should be /chat.
* @param _options - The options object.
* @param cmd - The command object.
*/
async function clientAsk(_options: any, cmd: Command) {
// Make a post request to the server using http
// The server should be running on localhost:5234
// The endpoint should be /chat
// The body should be the question
// The response should be the answer
const question = cmd.args.join("\n");
console.debug("Command:", question);
const response = await fetch('http://localhost:5234/chat', {
method: 'PUT',
body: JSON.stringify({ question }),
headers: { 'Content-Type': 'application/json' }
});
console.debug(response)
if (response && response.ok) {
const result = await response.json();
console.debug(result);
console.log(result.assistant);
return;
}
console.error(`!!!<HTTP ${response.status}>!!!\n`);
}
program
.command('ask')
.description('Ask ChatGPT')
.action(clientAsk);
program
.command('serve')
.description('Start the backend server')
.option('-l, --login [login]', 'Your email login for ChatGPT. Note: It is prefereable to pass that as an environment variable CHATGPT_LOGIN')
.option('-p, --password [password]', 'Your login password for ChatGPT. Note: It is prefereable to pass that as an environment variable CHATGPT_PASSWORD')
.action(startServer);
program
.command('help', { isDefault: true })
.action((options: any[], cmd: Command) => { program.outputHelp() });
async function startServer(options: object) {
// create a new browser object and store it in browser
defaultInit();
const banner = "🐈 GPT Server | Server is running on port 5234";
const app = new Elysia();
app.store = store;
app
// .get('/chat', () => 'Get Chat by ID')
.post('/chat', () => '{"user":"", "assistant":""}')//sendMessageHandler)
.put('/chat', () => () => sendMessageHandler) //'{"user":"", "assistant":""}'
// .delete('/chat', () => 'Delete Chat by ID')
.onStart(() => console.info(banner))
.listen(5234);
// Handle the signals to stop the server
for (const signal of ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGKILL']) {
process.on(signal, async () => {
app.stop();
await store?.puppeteer?.browserManager?.close();
console.error(`\nServer is being stopped by signal: ${signal}`);
});
}
}
// Parse the command line arguments
program.parse(process.argv);
// program.arguments