-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio-utils.js
34 lines (28 loc) · 1015 Bytes
/
io-utils.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
import chalk from 'chalk';
import fs from 'fs/promises';
import path from 'path';
const CONVERSATION_DIR = 'conversations';
export const saveConversationToFile = async (messages) => {
try {
await ensureDirectoryExists(CONVERSATION_DIR);
const timestamp = new Date().toISOString().replace(/:/g, '');
const fileName = `conversation_${timestamp}.txt`;
const filePath = path.join(CONVERSATION_DIR, fileName);
const content = messages.map(message => `${message.role}: ${message.content}`).join('\n');
await fs.writeFile(filePath, content, 'utf-8');
console.log(chalk.green(`Chatbot: Conversation saved to ${filePath}`));
} catch (error) {
console.error(chalk.red('Error saving conversation to file:', error.message));
}
};
const ensureDirectoryExists = async (directory) => {
try {
await fs.access(directory);
} catch (error) {
if (error.code === 'ENOENT') {
await fs.mkdir(directory);
} else {
throw error;
}
}
};