77 lines
2.7 KiB
JavaScript
77 lines
2.7 KiB
JavaScript
import dotenv from 'dotenv';
|
||
import { createClients } from './src/clients.js';
|
||
import { getConfig, validateConfig } from './src/config/env.js';
|
||
import { createSearchService } from './src/services/searchService.js';
|
||
import { renderHTML } from './src/utils/htmlConsoleRenderer.js';
|
||
|
||
// Load environment variables from .env file
|
||
dotenv.config();
|
||
|
||
function printUsage() {
|
||
console.log('Usage: node searchCLI.js <question>');
|
||
console.log('');
|
||
console.log('Example:');
|
||
console.log(' node searchCLI.js "What are the latest developments in AI?"');
|
||
}
|
||
|
||
function createSimpleBroadcast() {
|
||
return (message, type = 'info', data = null) => {
|
||
const timestamp = new Date().toLocaleTimeString();
|
||
const prefix = type === 'error' ? '❌' : type === 'warning' ? '⚠️' : type === 'success' ? '✅' : 'ℹ️';
|
||
console.log(`[${timestamp}] ${prefix} ${message}`);
|
||
};
|
||
}
|
||
|
||
async function runCLI() {
|
||
const args = process.argv.slice(2);
|
||
|
||
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
|
||
printUsage();
|
||
process.exit(args.includes('--help') || args.includes('-h') ? 0 : 1);
|
||
}
|
||
|
||
const question = args.join(' ');
|
||
|
||
try {
|
||
const config = getConfig();
|
||
validateConfig(config);
|
||
|
||
const broadcast = createSimpleBroadcast();
|
||
const clients = createClients(config);
|
||
const searchService = createSearchService({
|
||
...clients,
|
||
broadcast,
|
||
});
|
||
|
||
broadcast(`Starting search for: "${question}"`, 'info');
|
||
|
||
const result = await searchService.search(question);
|
||
|
||
console.log('');
|
||
console.log('═══════════════════════════════════════════════════════════');
|
||
console.log('FINAL ANSWER:');
|
||
console.log('═══════════════════════════════════════════════════════════');
|
||
console.log(renderHTML(result.fullAnswerHTMLSnippet) || 'No answer generated');
|
||
console.log('');
|
||
|
||
if (result.mostRelevantSources && result.mostRelevantSources.length > 0) {
|
||
console.log('SOURCES:');
|
||
result.mostRelevantSources.forEach((source, index) => {
|
||
console.log(` ${index + 1}. ${source}`);
|
||
});
|
||
}
|
||
console.log('═══════════════════════════════════════════════════════════');
|
||
|
||
process.exit(0);
|
||
} catch (error) {
|
||
console.error('');
|
||
console.error('❌ Error:', error.message);
|
||
if (error.details) {
|
||
console.error(' Details:', error.details);
|
||
}
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
runCLI();
|