cli
This commit is contained in:
80
searchCLI.js
Normal file
80
searchCLI.js
Normal file
@@ -0,0 +1,80 @@
|
||||
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';
|
||||
|
||||
// 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}`);
|
||||
if (data) {
|
||||
console.log(` Data: ${JSON.stringify(data, null, 2)}`);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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(result);
|
||||
console.log('');
|
||||
console.log('═══════════════════════════════════════════════════════════');
|
||||
console.log('FINAL ANSWER:');
|
||||
console.log('═══════════════════════════════════════════════════════════');
|
||||
console.log(result.fullAnswerHTMLSnippet || result.answer || '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();
|
||||
Reference in New Issue
Block a user