Shakeel401 commited on
Commit
01c3ff5
·
verified ·
1 Parent(s): 07ab924

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -17
main.py CHANGED
@@ -10,60 +10,68 @@ from agent import run_agent_query
10
 
11
  app = FastAPI(title="CFO Bot - Agent API")
12
 
13
- # Allow local dev; modify in production
 
 
14
  app.add_middleware(
15
  CORSMiddleware,
16
- allow_origins=["*"], # replace with exact domain in production
17
  allow_credentials=True,
18
  allow_methods=["*"],
19
  allow_headers=["*"],
20
  )
21
 
22
- # -------------------------------
23
- # Models
24
- # -------------------------------
25
-
26
  class QueryRequest(BaseModel):
27
  query: str
28
- thread_id: str | None = None
29
 
30
 
31
  class QueryResponse(BaseModel):
32
  output: str
33
 
34
 
35
- # -------------------------------
36
- # Routes
37
- # -------------------------------
38
-
39
  @app.get("/")
40
  def root():
41
  return {"status": "CFO Bot API running", "version": "1.0.0"}
42
 
43
 
 
 
 
44
  @app.post("/query", response_model=QueryResponse)
45
  async def query_endpoint(req: QueryRequest):
46
 
47
- if not req.query or req.query.strip() == "":
 
48
  raise HTTPException(status_code=400, detail="Query cannot be empty.")
49
 
 
 
 
 
50
  try:
51
- # Call the agent runner
52
  result = await run_agent_query(
53
  req.query,
54
- thread_id=req.thread_id or "default"
55
  )
56
 
57
  except Exception as exc:
58
- raise HTTPException(status_code=500, detail=f"Agent failed: {exc}")
59
 
60
- # Extract final output from agent SDK result
61
  final_output = (
62
  getattr(result, "final_output", None)
63
  or (result.get("final_output") if isinstance(result, dict) else None)
64
  )
65
 
66
- if final_output is None:
67
  final_output = "No response generated."
68
 
69
  return QueryResponse(output=final_output)
 
10
 
11
  app = FastAPI(title="CFO Bot - Agent API")
12
 
13
+ # ---------------------------------------------------------
14
+ # CORS (adjust for production)
15
+ # ---------------------------------------------------------
16
  app.add_middleware(
17
  CORSMiddleware,
18
+ allow_origins=["*"], # replace with your frontend domain
19
  allow_credentials=True,
20
  allow_methods=["*"],
21
  allow_headers=["*"],
22
  )
23
 
24
+ # ---------------------------------------------------------
25
+ # Request / Response Models
26
+ # ---------------------------------------------------------
 
27
  class QueryRequest(BaseModel):
28
  query: str
29
+ thread_id: str # REQUIRED not optional
30
 
31
 
32
  class QueryResponse(BaseModel):
33
  output: str
34
 
35
 
36
+ # ---------------------------------------------------------
37
+ # Health check
38
+ # ---------------------------------------------------------
 
39
  @app.get("/")
40
  def root():
41
  return {"status": "CFO Bot API running", "version": "1.0.0"}
42
 
43
 
44
+ # ---------------------------------------------------------
45
+ # Query Route
46
+ # ---------------------------------------------------------
47
  @app.post("/query", response_model=QueryResponse)
48
  async def query_endpoint(req: QueryRequest):
49
 
50
+ # Validate query
51
+ if not req.query or not req.query.strip():
52
  raise HTTPException(status_code=400, detail="Query cannot be empty.")
53
 
54
+ # Validate thread_id
55
+ if not req.thread_id or not req.thread_id.strip():
56
+ raise HTTPException(status_code=400, detail="thread_id is required.")
57
+
58
  try:
59
+ # Run agent — NO DEFAULT thread_id
60
  result = await run_agent_query(
61
  req.query,
62
+ thread_id=req.thread_id
63
  )
64
 
65
  except Exception as exc:
66
+ raise HTTPException(status_code=500, detail=f"Agent failed: {str(exc)}")
67
 
68
+ # Extract output properly
69
  final_output = (
70
  getattr(result, "final_output", None)
71
  or (result.get("final_output") if isinstance(result, dict) else None)
72
  )
73
 
74
+ if not final_output:
75
  final_output = "No response generated."
76
 
77
  return QueryResponse(output=final_output)