research-cli
Version:
AI-powered research assistant with web search capabilities and beautiful terminal UI
78 lines • 3.08 kB
JavaScript
/**
* Main query command handler
*/
import { OpenAIProvider } from "../providers/openai.js";
import { MarkdownRenderer } from "../render/markdown.js";
export async function runQuery(query, options) {
// Validate query
if (!query) {
// eslint-disable-next-line no-console
console.error("Error: Query is required. Use --help for usage information.");
process.exit(1);
}
if (options.verbose) {
// eslint-disable-next-line no-console
console.error(`Running query with provider: ${options.provider}, format: ${options.format}`);
}
try {
// For M1, we only implement OpenAI provider
if (options.provider !== "openai") {
throw new Error(`Provider '${options.provider}' not implemented yet. Only 'openai' is supported in M1.`);
}
// Create provider instance
const provider = new OpenAIProvider();
// Create renderer with progress options
const rendererOptions = {
useMultiProgress: options.web, // Use multi-progress when web search is enabled
silent: options.format !== "md", // Only show progress for markdown output
theme: "research",
};
const renderer = new MarkdownRenderer(rendererOptions);
// Dry run mode
if (options.dryRun) {
const request = {
query,
model: options.model,
webSearch: options.web,
webSearchContextSize: options.webSearchContextSize,
maxTokens: options.maxTokens,
temperature: options.temperature,
};
// eslint-disable-next-line no-console
console.log("Dry run - Request payload:");
// eslint-disable-next-line no-console
console.log(JSON.stringify({
...request,
// Redact any sensitive data for dry run
apiKey: "[REDACTED]",
}, null, 2));
return;
}
// Execute query with streaming
const eventStream = provider.streamQuery({
query,
model: options.model,
webSearch: options.web,
webSearchContextSize: options.webSearchContextSize,
maxTokens: options.maxTokens,
temperature: options.temperature,
});
// Timeout handling (for future use)
// const timeout = options.timeout || 120000; // 2 minutes default
// Process events based on format
if (options.format === "md") {
await renderer.renderStream(eventStream);
}
else {
throw new Error(`Format '${options.format}' not implemented yet. Only 'md' is supported in M1.`);
}
// Ensure the process exits cleanly
process.exit(0);
}
catch (error) {
// eslint-disable-next-line no-console
console.error("Error executing query:", error instanceof Error ? error.message : "Unknown error");
process.exit(1);
}
}
//# sourceMappingURL=query.js.map