simplified

This commit is contained in:
David Zhang
2025-03-07 14:03:52 -08:00
parent ad0c3d08d7
commit c04379610f
4 changed files with 4 additions and 94 deletions

View File

@@ -2,8 +2,6 @@ import cors from 'cors';
import express, { Request, Response } from 'express';
import { deepResearch, writeFinalAnswer } from './deep-research';
import { generateFeedback } from './feedback';
import { OutputManager } from './output-manager';
const app = express();
const port = process.env.PORT || 3051;
@@ -12,12 +10,9 @@ const port = process.env.PORT || 3051;
app.use(cors());
app.use(express.json());
// Initialize output manager
const output = new OutputManager();
// Helper function for consistent logging
function log(...args: any[]) {
output.log(...args);
console.log(...args);
}
// API endpoint to run research
@@ -29,46 +24,30 @@ app.post('/api/research', async (req: Request, res: Response) => {
return res.status(400).json({ error: 'Query is required' });
}
log('\nResearching your topic...');
log('\nStarting research with progress tracking...\n');
log('\nStarting research...\n');
const { learnings, visitedUrls } = await deepResearch({
query,
breadth,
depth,
onProgress: progress => {
output.updateProgress(progress);
},
});
log(`\n\nLearnings:\n\n${learnings.join('\n')}`);
log(
`\n\nVisited URLs (${visitedUrls.length}):\n\n${visitedUrls.join('\n')}`,
);
log('Writing final answer...');
// Save report to file with timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const reportFilename = `output-${timestamp}.md`;
// await fs.writeFile(reportFilename, report, 'utf-8');
const answer = await writeFinalAnswer({
prompt: query,
learnings,
});
// Save answer to file
const answerFilename = `answer-${timestamp}.md`;
// await fs.writeFile(answerFilename, answer, 'utf-8');
// Return the results
return res.json({
success: true,
answer,
learnings,
visitedUrls,
reportFilename,
answerFilename,
});
} catch (error: unknown) {
console.error('Error in research API:', error);

View File

@@ -5,15 +5,10 @@ import pLimit from 'p-limit';
import { z } from 'zod';
import { getModel, trimPrompt } from './ai/providers';
import { OutputManager } from './output-manager';
import { systemPrompt } from './prompt';
// Initialize output manager for coordinated console/progress output
const output = new OutputManager();
// Replace console.log with output.log
function log(...args: any[]) {
output.log(...args);
console.log(...args);
}
export type ResearchProgress = {

View File

@@ -1,58 +0,0 @@
import { ResearchProgress } from './deep-research';
export class OutputManager {
private progressLines: number = 4;
private progressArea: string[] = [];
private initialized: boolean = false;
constructor() {
// Initialize terminal
process.stdout.write('\n'.repeat(this.progressLines));
this.initialized = true;
}
log(...args: any[]) {
// Move cursor up to progress area
if (this.initialized) {
process.stdout.write(`\x1B[${this.progressLines}A`);
// Clear progress area
process.stdout.write('\x1B[0J');
}
// Print log message
console.log(...args);
// Redraw progress area if initialized
if (this.initialized) {
this.drawProgress();
}
}
updateProgress(progress: ResearchProgress) {
this.progressArea = [
`Depth: [${this.getProgressBar(progress.totalDepth - progress.currentDepth, progress.totalDepth)}] ${Math.round(((progress.totalDepth - progress.currentDepth) / progress.totalDepth) * 100)}%`,
`Breadth: [${this.getProgressBar(progress.totalBreadth - progress.currentBreadth, progress.totalBreadth)}] ${Math.round(((progress.totalBreadth - progress.currentBreadth) / progress.totalBreadth) * 100)}%`,
`Queries: [${this.getProgressBar(progress.completedQueries, progress.totalQueries)}] ${Math.round((progress.completedQueries / progress.totalQueries) * 100)}%`,
progress.currentQuery ? `Current: ${progress.currentQuery}` : '',
];
this.drawProgress();
}
private getProgressBar(value: number, total: number): string {
const width = process.stdout.columns
? Math.min(30, process.stdout.columns - 20)
: 30;
const filled = Math.round((width * value) / total);
return '█'.repeat(filled) + ' '.repeat(width - filled);
}
private drawProgress() {
if (!this.initialized || this.progressArea.length === 0) return;
// Move cursor to progress area
const terminalHeight = process.stdout.rows || 24;
process.stdout.write(`\x1B[${terminalHeight - this.progressLines};1H`);
// Draw progress bars
process.stdout.write(this.progressArea.join('\n'));
// Move cursor back to content area
process.stdout.write(`\x1B[${terminalHeight - this.progressLines - 1};1H`);
}
}

View File

@@ -7,13 +7,10 @@ import {
writeFinalReport,
} from './deep-research';
import { generateFeedback } from './feedback';
import { OutputManager } from './output-manager';
const output = new OutputManager();
// Helper function for consistent logging
function log(...args: any[]) {
output.log(...args);
console.log(...args);
}
const rl = readline.createInterface({
@@ -84,9 +81,6 @@ ${followUpQuestions.map((q: string, i: number) => `Q: ${q}\nA: ${answers[i]}`).j
query: combinedQuery,
breadth,
depth,
onProgress: progress => {
output.updateProgress(progress);
},
});
log(`\n\nLearnings:\n\n${learnings.join('\n')}`);