File size: 1,500 Bytes
19f5032
6905aa5
 
 
8c7ca34
 
19f5032
8c7ca34
19f5032
8c7ca34
6905aa5
 
 
19f5032
 
 
 
 
 
e12655f
 
19f5032
8c7ca34
6905aa5
 
 
19f5032
 
 
6905aa5
19f5032
8c7ca34
 
19f5032
f230570
19f5032
8c7ca34
 
 
19f5032
 
 
8c7ca34
 
 
bae207d
 
 
 
 
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
# blaxel_main.py (New Secure Version)
from fastapi import FastAPI
from pydantic import BaseModel
from sse_starlette.sse import EventSourceResponse
import os
import uvicorn
from typing import Dict, Optional

# 1. Import our "Brain"
from agent_logic import StrategicSelectorAgent

app = FastAPI()

# 2. Update Pydantic model to accept keys (some are optional)
class ApiKeys(BaseModel):
    google: str
    anthropic: Optional[str] = None
    sambanova: Optional[str] = None

class ProblemRequest(BaseModel):
    problem: str
    keys: ApiKeys

@app.post("/solve_problem")
async def solve_problem(request: ProblemRequest):
    """
    This endpoint now creates a *new* agent for every request,
    and passes the user's API keys to it.
    This is stateless and secure.
    """
    
    async def stream_solution():
        try:
            # 3. Pass keys to the agent's constructor
            mudabbir_ai = StrategicSelectorAgent(api_keys=request.keys.model_dump())
            
            async for status_update in mudabbir_ai.solve(request.problem):
                yield status_update
        except Exception as e:
            # This will catch errors (like invalid keys)
            yield f"AGENT ERROR: {e}"
            print(f"AGENT ERROR: {e}") # Also log to backend

    return EventSourceResponse(stream_solution())


if __name__ == "__main__":
    HOST = os.getenv("BL_SERVER_HOST", "0.0.0.0")
    PORT = int(os.getenv("BL_SERVER_PORT", "8080"))
    uvicorn.run(app, host=HOST, port=PORT)