forked from guy-hartstein/company-research-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
273 lines (231 loc) · 9.77 KB
/
Copy pathapplication.py
File metadata and controls
273 lines (231 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import asyncio
import json
import logging
import os
import uuid
from datetime import datetime
from pathlib import Path
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from pydantic import BaseModel
from backend.graph import Graph
from backend.services.mongodb import MongoDBService
from backend.services.pdf_service import PDFService
from backend.classes.state import job_status
# Load environment variables from .env file at startup
env_path = Path(__file__).parent / '.env'
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=True)
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
logger.addHandler(console_handler)
app = FastAPI(title="Tavily Company Research API")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
)
pdf_service = PDFService({"pdf_output_dir": "pdfs"})
mongodb = None
if mongo_uri := os.getenv("MONGODB_URI"):
try:
mongodb = MongoDBService(mongo_uri)
logger.info("MongoDB integration enabled")
except Exception as e:
logger.warning(f"Failed to initialize MongoDB: {e}. Continuing without persistence.")
class ResearchRequest(BaseModel):
company: str
company_url: str | None = None
industry: str | None = None
hq_location: str | None = None
class PDFGenerationRequest(BaseModel):
report_content: str
company_name: str | None = None
@app.options("/research")
async def preflight():
response = JSONResponse(content=None, status_code=200)
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST, OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
return response
@app.post("/research")
async def research(data: ResearchRequest):
try:
logger.info(f"Received research request for {data.company}")
job_id = str(uuid.uuid4())
asyncio.create_task(process_research(job_id, data))
response = JSONResponse(content={
"status": "accepted",
"job_id": job_id,
"message": "Research started. Connect to /research/{job_id}/stream for updates."
})
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST, OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
return response
except Exception as e:
logger.error(f"Error initiating research: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
async def process_research(job_id: str, data: ResearchRequest):
"""Process research request asynchronously and store results"""
try:
if mongodb:
mongodb.create_job(job_id, data.dict())
await asyncio.sleep(0.5) # Brief delay
logger.info(f"Starting research for {data.company}")
graph = Graph(
company=data.company,
url=data.company_url,
industry=data.industry,
hq_location=data.hq_location,
job_id=job_id
)
final_state = {}
# Stream through the graph and update progress
async for state in graph.run(thread={}):
final_state.update(state)
node_name = list(state.keys())[0] if state else 'unknown'
logger.debug(f"Node completed: {node_name}")
# Update job status with current step
job_status[job_id].update({
"status": "processing",
"current_step": node_name,
"last_update": datetime.now().isoformat()
})
# Extract final report
report_content = final_state.get('report') or (final_state.get('editor') or {}).get('report')
if report_content:
logger.info(f"Research completed. Report length: {len(report_content)}")
job_status[job_id].update({
"status": "completed",
"report": report_content,
"company": data.company,
"last_update": datetime.now().isoformat()
})
if mongodb:
mongodb.update_job(job_id=job_id, status="completed")
mongodb.store_report(job_id=job_id, report_data={"report": report_content})
logger.info(f"Research completed successfully for {data.company}")
else:
logger.error(f"Research completed without report. State keys: {list(final_state.keys())}")
job_status[job_id].update({
"status": "failed",
"error": "No report generated",
"last_update": datetime.now().isoformat()
})
except Exception as e:
logger.error(f"Research failed: {str(e)}", exc_info=True)
job_status[job_id].update({
"status": "failed",
"error": str(e),
"last_update": datetime.now().isoformat()
})
if mongodb:
mongodb.update_job(job_id=job_id, status="failed", error=str(e))
@app.get("/")
async def ping():
return {"message": "Alive"}
@app.get("/research/pdf/{filename}")
async def get_pdf(filename: str):
pdf_path = os.path.join("pdfs", filename)
if not os.path.exists(pdf_path):
raise HTTPException(status_code=404, detail="PDF not found")
return FileResponse(pdf_path, media_type='application/pdf', filename=filename)
@app.get("/research/{job_id}")
async def get_research(job_id: str):
if not mongodb:
raise HTTPException(status_code=501, detail="Database persistence not configured")
job = mongodb.get_job(job_id)
if not job:
raise HTTPException(status_code=404, detail="Research job not found")
return job
@app.get("/research/{job_id}/stream")
async def stream_research(job_id: str):
"""Stream research progress via SSE"""
async def event_generator():
try:
# Wait for job to exist
for _ in range(50):
if job_id in job_status:
break
await asyncio.sleep(0.1)
last_step = None
# Stream status updates
while job_id in job_status:
result = job_status[job_id]
status = result.get("status")
current_step = result.get("current_step")
events = result.get("events", [])
# Send node progress updates when step changes
if status == "processing" and current_step and current_step != last_step:
data = json.dumps({"type": "progress", "step": current_step})
yield f"data: {data}\n\n"
last_step = current_step
# Send all queued events (FIFO - pop from start)
while events:
event = events.pop(0)
data = json.dumps(event)
yield f"data: {data}\n\n"
if status == "completed" and (report := result.get("report")):
data = json.dumps({"type": "complete", "report": report})
yield f"data: {data}\n\n"
break
elif status == "failed":
data = json.dumps({"type": "error", "error": result.get("error", "Unknown error")})
yield f"data: {data}\n\n"
break
await asyncio.sleep(0.1) # Faster polling for responsive updates
except Exception as e:
data = json.dumps({"type": "error", "error": str(e)})
yield f"data: {data}\n\n"
return StreamingResponse(event_generator(), media_type="text/event-stream")
@app.get("/research/{job_id}/report")
async def get_research_report(job_id: str):
if not mongodb:
if job_id in job_status:
result = job_status[job_id]
if report := result.get("report"):
return {"report": report}
# Job exists but report not ready yet
return JSONResponse(
status_code=202,
content={"status": result.get("status", "pending"), "message": "Report not ready yet"}
)
raise HTTPException(status_code=404, detail="Job not found")
report = mongodb.get_report(job_id)
if not report:
# Check if job exists
if job := mongodb.get_job(job_id):
return JSONResponse(
status_code=202,
content={"status": job.get("status", "pending"), "message": "Report not ready yet"}
)
raise HTTPException(status_code=404, detail="Job not found")
return report
@app.post("/generate-pdf")
async def generate_pdf(data: PDFGenerationRequest):
"""Generate a PDF from markdown content and stream it to the client."""
try:
success, result = pdf_service.generate_pdf_stream(data.report_content, data.company_name)
if success:
pdf_buffer, filename = result
return StreamingResponse(
pdf_buffer,
media_type='application/pdf',
headers={
'Content-Disposition': f'attachment; filename="{filename}"'
}
)
else:
raise HTTPException(status_code=500, detail=result)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)