-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
219 lines (174 loc) · 6.19 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
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
// src/index.ts
import * as readline from 'readline';
import slug from "./src/helpers.ts";
import initializeAgent from './src/agent';
import ora from 'ora';
import chalk from 'chalk';
import fs from 'fs/promises';
import path from 'path';
import initializeAgentLangChain from './src/agent/tools/initializeAgentLangChain.ts';
const history: string[] = [];
let selectedProvider: "openai" | "openrouter" | null = null; // Lưu AI provider được chọn
async function saveToFile(content: string, directory: string, filename: string) {
try {
await fs.mkdir(directory, { recursive: true });
const sanitizedFilename = slug(filename);
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const fullPath = path.join(directory, `${sanitizedFilename}_${timestamp}.md`);
await fs.writeFile(fullPath, content, 'utf-8');
return fullPath;
} catch (error) {
console.error(chalk.red(`Error saving to ${directory}:`), error);
throw error;
}
}
async function saveLog(query: string, process: any) {
const content = `# Query Log
Query: ${query}
Timestamp: ${new Date().toISOString()}
## Process Details
\`\`\`json
${JSON.stringify(process, null, 2)}
\`\`\`
`;
return await saveToFile(content, './logs', query);
}
async function saveData(query: string, result: any) {
const content = `# Query Result
Query: ${query}
Timestamp: ${new Date().toISOString()}
${result}
`;
return await saveToFile(content, './data', query);
}
async function promptUser(rl: readline.Interface, message: string): Promise<string> {
return new Promise((resolve) => {
rl.question(chalk.blue(message), (answer) => {
resolve(answer.trim());
});
});
}
async function selectAIProvider(rl: readline.Interface): Promise<string> {
console.log(chalk.yellow('\nAvailable AI Providers:\n'));
const providers: { name: string; id: string }[] = [
{ name: 'OpenAI(LangChain)', id: 'openai' },
{ name: 'OpenRouter', id: 'openrouter' },
];
providers.forEach((provider, index) => {
console.log(chalk.green(`${index + 1}. ${provider.name}`));
});
let selectedIndex: number | null = null;
while (selectedIndex === null || selectedIndex < 0 || selectedIndex >= providers.length) {
const choice = await promptUser(rl, 'Select an AI provider (1, 2) [Default: 1]:\n> ');
// Nếu người dùng không nhập gì, chọn mặc định là `1` (OpenAI)
const parsedChoice = choice.trim() === '' ? 1 : parseInt(choice, 10);
if (!isNaN(parsedChoice) && parsedChoice > 0 && parsedChoice <= providers.length) {
selectedIndex = parsedChoice - 1;
} else {
console.log(chalk.red('Invalid selection. Please try again.'));
selectedIndex = null; // Reset để tiếp tục vòng lặp
}
}
// Chắc chắn `selectedIndex` hợp lệ trước khi truy cập phần tử mảng
const selectedProvider = providers[selectedIndex];
if (!selectedProvider) {
throw new Error('Unexpected error: Selected provider is undefined.');
}
console.log(chalk.green(`\nYou selected: ${selectedProvider.name}`));
return selectedProvider.id;
}
async function main() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
try {
// Bước 1: Chọn AI Provider
selectedProvider = await selectAIProvider(rl) as "openai" | "openrouter"
console.log(chalk.yellow(`Initializing AI agent using ${selectedProvider}...`));
let executor = null;
switch (selectedProvider) {
case 'openai':
console.log(chalk.yellow('Initializing OpenAI agent...'));
executor = await initializeAgentLangChain();
break;
case 'openrouter':
console.log(chalk.yellow('Initializing OpenRouter agent...'));
executor = await initializeAgent();
break;
default:
throw new Error('Invalid AI provider selected');
}
while (true) {
const query = await promptUser(rl, 'Enter your search query (or type "exit" to quit, "history" to see past queries):\n> ');
if (query.toLowerCase() === 'exit') {
console.log(chalk.yellow('Shutting down...'));
break;
}
if (query.toLowerCase() === 'history') {
console.log(chalk.cyan('\nSearch History:'));
if (history.length === 0) {
console.log(chalk.gray('No searches yet'));
} else {
history.forEach((h, i) => console.log(chalk.white(`${i + 1}. ${h}`)));
}
console.log('\n-------------------------------------------\n');
continue;
}
const spinner = ora('Searching...').start();
try {
const processStart = {
timestamp: new Date().toISOString(),
query,
status: 'started',
provider: selectedProvider // Ghi lại provider đã sử dụng
};
const result = await executor.run(query);
const processComplete = {
...processStart,
status: 'completed',
completedAt: new Date().toISOString(),
executionTime: Date.now() - new Date(processStart.timestamp).getTime()
};
const logPath = await saveLog(query, processComplete);
const dataPath = await saveData(query, result);
spinner.succeed('Search completed');
console.log(chalk.green('\nResult:'), result);
console.log(chalk.gray(`\nLog saved to: ${logPath}`));
console.log(chalk.gray(`Data saved to: ${dataPath}`));
history.push(query);
} catch (error) {
const processError = {
timestamp: new Date().toISOString(),
query,
status: 'failed',
provider: selectedProvider,
error: error instanceof Error ? error.message : String(error)
};
await saveLog(query, processError);
spinner.fail('Search failed');
console.error(chalk.red('\nError during search:'), error);
}
console.log('\n-------------------------------------------\n');
}
} catch (error) {
console.error(chalk.red('Fatal error:'), error);
}
}
// Error handlers remain the same
process.on('SIGINT', async () => {
console.log(chalk.yellow('\nReceived SIGINT. Cleaning up...'));
process.exit(0);
});
process.on('uncaughtException', (error) => {
console.error(chalk.red('\nUncaught Exception:'), error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error(chalk.red('\nUnhandled Rejection at:'), promise, chalk.red('\nReason:'), reason);
process.exit(1);
});
main().catch((error) => {
console.error(chalk.red('Application Error:'), error);
process.exit(1);
});