|
1 | 1 | import cors from 'cors';
|
2 | 2 | import express, { Request, Response } from 'express';
|
3 | 3 |
|
4 |
| -import { deepResearch, writeFinalAnswer } from './deep-research'; |
| 4 | +import { deepResearch, writeFinalAnswer,writeFinalReport } from './deep-research'; |
5 | 5 |
|
6 | 6 | const app = express();
|
7 | 7 | const port = process.env.PORT || 3051;
|
@@ -58,6 +58,42 @@ app.post('/api/research', async (req: Request, res: Response) => {
|
58 | 58 | }
|
59 | 59 | });
|
60 | 60 |
|
| 61 | +// generate report API |
| 62 | +app.post('/api/generate-report',async(req:Request,res:Response)=>{ |
| 63 | + try{ |
| 64 | + const {query,depth = 3,breadth=3 } = req.body; |
| 65 | + if(!query){ |
| 66 | + return res.status(400).json({error:'Query is required'}); |
| 67 | + } |
| 68 | + log('\n Starting research...\n') |
| 69 | + const {learnings,visitedUrls} = await deepResearch({ |
| 70 | + query, |
| 71 | + breadth, |
| 72 | + depth |
| 73 | + }); |
| 74 | + log(`\n\nLearnings:\n\n${learnings.join('\n')}`); |
| 75 | + log( |
| 76 | + `\n\nVisited URLs (${visitedUrls.length}):\n\n${visitedUrls.join('\n')}`, |
| 77 | + ); |
| 78 | + const report = await writeFinalReport({ |
| 79 | + prompt:query, |
| 80 | + learnings, |
| 81 | + visitedUrls |
| 82 | + }); |
| 83 | + |
| 84 | + return report |
| 85 | + |
| 86 | + }catch(error:unknown){ |
| 87 | + console.error("Error in generate report API:",error) |
| 88 | + return res.status(500).json({ |
| 89 | + error:'An error occurred during research', |
| 90 | + message:error instanceof Error? error.message: String(error), |
| 91 | + }) |
| 92 | + } |
| 93 | +}) |
| 94 | + |
| 95 | + |
| 96 | + |
61 | 97 | // Start the server
|
62 | 98 | app.listen(port, () => {
|
63 | 99 | console.log(`Deep Research API running on port ${port}`);
|
|
0 commit comments