Coverage for app / routers / simulate.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1from fastapi import APIRouter, Depends, HTTPException 

2from sqlalchemy.orm import Session 

3 

4from app.dependency.base import get_db 

5from app.models.simulation import SimulationRunRequest, SimulationRunResult 

6from app.services.conversation import trace_invocation 

7from app.services.simulator import run_simulation 

8 

9router = APIRouter(prefix="/simulate", tags=["Simulation"]) 

10 

11#: Trace purpose of a simulation run's model calls. 

12SIMULATION_PURPOSE = "simulation" 

13 

14 

15@router.post( 

16 "/run", 

17 response_model=SimulationRunResult, 

18 summary="Run a simulation", 

19) 

20async def run_sim( 

21 request: SimulationRunRequest, db: Session = Depends(get_db) 

22) -> SimulationRunResult: 

23 """Run a simulation, traced as one invocation: its agents may each run 

24 on a different model, and every call names its own.""" 

25 if not request.agents: 

26 raise HTTPException(status_code=422, detail="At least one agent is required.") 

27 with trace_invocation( 

28 db, 

29 purpose=SIMULATION_PURPOSE, 

30 user_id=request.user_id, 

31 request_config={"reference": {"simulation_id": str(request.simulation_id)}}, 

32 ): 

33 return await run_simulation(request)