-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·335 lines (274 loc) · 11.1 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import "dotenv/config";
import dotenv from 'dotenv';
import {
intro,
outro,
select,
spinner,
isCancel,
cancel,
text,
} from "@clack/prompts";
import color from "picocolors";
import OpenAIChat from "./open-ai/chat.js";
import OpenAIStore from "./open-ai/store.js";
import minimist from 'minimist';
import terminalLink from 'terminal-link';
import { marked } from 'marked';
import { markedTerminal } from 'marked-terminal';
class Assistant {
textMsg = `${color.bold("Ask")} a question or hit enter to quit:`;
imageMsg = `Enter a ${color.bold("prompt")} to generate an image or hit enter to quit`;
lastChatLoaded = false;
constructor() {
this.currentModuleDir = path.dirname(new URL(import.meta.url).pathname);
this.historyDir = path.resolve(this.currentModuleDir, `history`);
this.historyTextPath = path.resolve(this.historyDir, 'text');
this.historyImagePath = path.resolve(this.historyDir, 'image');
this.lastTextFileSavedReferencePath = path.resolve(this.historyTextPath, 'last-file-saved-reference.json');
this.lastTextFileSavedPath = fs.existsSync(this.lastTextFileSavedReferencePath) ? JSON.parse(fs.readFileSync(this.lastTextFileSavedReferencePath))?.filePath : null;
const envPath = path.resolve(this.currentModuleDir, '.env');
dotenv.config({ path: envPath });
this.oaiStore = new OpenAIStore();
this.s = spinner();
this.args = minimist(process.argv.slice(2));
marked.use(markedTerminal());
}
async run() {
let lastChat = null;
intro(color.inverse(" Your Terminal AI Assistant 🤖 "));
if (this.args.delete || this.args.d) {
this.deleteHistory();
this.exit(false);
}
if (this.args.help || this.args.h) {
this.exit(false);
}
if (this.args.image || this.args.i) {
this.oaiStore.set('modelType', 'image');
}
if (this.args.config || this.args.c) {
await this.configureAssistant();
}
if (this.oaiStore.get('modelType') === 'image') { // we always ask user to configure image assistant when image is selected
await this.configureImageAssistant();
} else if (this.lastTextFileSavedPath && !this.args.new && !this.args.n && !this.args.config && !this.args.c) {
lastChat = await this.getLastChat();
}
if (lastChat) this.oaiStore.config = lastChat.config;
this.oaiChat = new OpenAIChat(this.oaiStore.config);
if (lastChat) {
this.oaiChat.messages = lastChat.history;
this.loadChatHistory(lastChat.history);
}
this.question();
}
async configureAssistant() {
intro(`${color.bold("Configure")} your assistant for next session:\n${color.dim(" Settings are usually loaded from your .env file.")}\n`);
this.oaiStore.set(
'modelType',
await select({
message: 'Pick a generation model type.',
options: [
{ value: 'text', label: 'Text' },
{ value: 'image', label: 'Image' }
],
})
);
if (this.oaiStore.get('modelType') === 'text') {
await this.configureTextAssistant();
}
}
async configureTextAssistant() {
this.oaiStore.set(
'textModel',
await select({
message: 'Pick a text model.',
options: [
{ value: 'gpt-3.5-turbo', label: 'gpt-3.5-turbo' },
{ value: 'gpt-4', label: 'gpt-4' },
{ value: 'gpt-4-1106-preview', label: 'gpt-4 turbo' },
],
})
);
this.oaiStore.set(
'systemMessage',
await text({
message: "Add your system message to sey the context and guide the behavior of the language model during the conversation",
placeholder: this.oaiStore.get('systemMessage')
})
);
}
async configureImageAssistant() {
const sizeModel = await select({
message: 'Choose your image size and generation model.',
options: [
{ value: '256x256_dall-e-2', label: '256×256px with dall-e-2' },
{ value: '512x512_dall-e-2', label: '512×512px with dall-e-2' },
{ value: '1024x1024_dall-e-2', label: '1024×1024px with dall-e-2' },
{ value: '1024x1024_dall-e-3', label: '1024×1024px with dall-e-3' },
{ value: '1792x1024_dall-e-3', label: '1792×1024px with dall-e-3' },
{ value: '1024x1792_dall-e-3', label: '1024×1792px with dall-e-3' },
],
});
this.oaiStore
.set('imageSize', sizeModel.split('_')[0])
.set('imageModel', sizeModel.split('_')[1]);
if (this.oaiStore.get('imageModel') === 'dall-e-3') {
this.oaiStore.set(
'imageStyle',
await select({
message: 'Select an image style (only available for dall-e-3)',
options: [
{ value: 'vivid', label: 'Vivid: hyper-real and dramatic images' },
{ value: 'natural', label: 'Natural: more natural, less hyper-real looking images' }
],
}
))
.set(
'imageQuality',
await select({
message: 'Select the image quality (only available for dall-e-3)',
options: [
{ value: 'standard', label: 'Standard: hyper-real and dramatic images' },
{ value: 'hd', label: 'HD: more natural, less hyper-real looking images' }
],
})
);
}
}
async question() {
let question = false;
do {
question = await this.ask();
switch (this.oaiStore.get('modelType')) {
case 'image':
await this.processImageQuestion(question);
break;
default:
await this.processTextQuestion(question);
break;
}
} while (question);
}
async ask() {
let prompt = await text({
message: this.oaiStore.get('modelType') === 'image' ? this.imageMsg : this.textMsg,
});
if (isCancel(prompt) || !prompt) {
this.exit();
}
return prompt;
}
async processTextQuestion(prompt) {
this.s.start("fetching answer");
let answer = await this.oaiChat.sendTextChat(prompt);
this.s.stop();
if (answer.error) {
outro(answer.error);
return this.exit();
}
intro(`${color.bold("Answer:")}\n${marked(answer)}`);
}
async processImageQuestion(prompt) {
this.s.start("fetching image");
const response = await this.oaiChat.createImage(prompt);
this.s.stop();
if (response.error) {
outro(response.error);
return this.exit();
}
let imgResponse = terminalLink(`${color.blue('Click here to view the generated image in your browser')}`, response.url)
imgResponse += `\n${color.dim(' command + click on a mac')}`;
if (response.revised_prompt) {
imgResponse += `\nRevised Prompt: ${response.revised_prompt}`;
}
intro(`${color.bold("Response:")}\n${imgResponse}`);
}
goodbyeMessage(saveChatedChat = false) {
let msg = `Thanks for using ${color.bold("Your Terminal AI Assistant!")}\n`;
if (saveChatedChat) {
msg += `\n${saveChatedChat}\n`;
}
msg += `\nAvailable flags:`;
msg += `\n${color.bold("--help or -h")} show this help message`;
msg += `\n${color.bold("--new or -n")} ask a new question, without loading the last chat history`;
msg += `\n${color.bold("--config or -c")} configure assistant for next session`;
msg += `\n${color.bold("--image or -i")} create an image`;
msg += `\n${color.bold("--delete or -d")} delete chat history`;
msg += `\n`;
msg += `\n${color.inverse(" Good Bye ")} 👋🏻`;
return msg;
}
exit(save = true) {
cancel();
outro(this.goodbyeMessage(save ? this.saveChat() : false));
process.exit(0);
}
saveChat() {
const modelType = this.oaiStore.get('modelType');
const imageHistory = this.oaiChat.imageHistory;
const textHistory = this.oaiChat.messages;
let responseMessage = '';
if (!imageHistory.length && textHistory.length <= 1) {
return;
}
const obj = {
config: this.oaiStore.config,
history: modelType === 'image' ? imageHistory : textHistory,
}
if (!fs.existsSync(this.historyDir)) {
fs.mkdirSync(this.historyDir);
}
const directoryPath = modelType === 'image' ? this.historyImagePath : this.historyTextPath;
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath);
}
const timestamp = new Date().toLocaleString().replace(/[ ,:/]/g, '-');
const filePath = `${directoryPath}/${timestamp}.json`;
const jsonString = JSON.stringify(obj, null, 2);
try {
fs.writeFileSync(filePath, jsonString);
if(this.lastChatLoaded) {
fs.unlinkSync(this.lastTextFileSavedPath);
}
if (modelType === 'text') {
fs.writeFileSync(this.lastTextFileSavedReferencePath, JSON.stringify({filePath: filePath}, null, 2));
}
responseMessage = `History saved to file: ${filePath}`;
} catch (error) {
responseMessage = `Error saving history to file: ${error.message}`
}
return responseMessage;
}
async getLastChat() {
try {
return JSON.parse(fs.readFileSync(this.lastTextFileSavedPath));
} catch (error) {
console.log(`Error reading last chat file: ${error.message}`);
return null;
}
}
loadChatHistory(history = null) {
if (!history) return;
console.log(`\n${color.underline(color.bold("Chat History"))}\n`);
history.forEach((message) => {
if (message.role !== 'system') {
console.log(`${color.bold("Role:")} ${message.role}`);
console.log(`${color.bold("Message:")} ${marked(message.content)}`);
}
});
console.log(`${color.dim('To ask a new question run the ask command with the -n flag, ask -n ')}`);
this.lastChatLoaded = true;
}
deleteHistory () {
if (fs.existsSync(this.historyDir)) {
fs.rmdirSync(this.historyDir, { recursive: true });
}
}
}
const assistant = new Assistant();
assistant.run().catch(console.error);