Files
searchAgent/searchCLI.js
2026-04-04 21:51:52 +02:00

77 lines
2.7 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();