-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulation.py
More file actions
497 lines (449 loc) · 16.2 KB
/
Copy pathsimulation.py
File metadata and controls
497 lines (449 loc) · 16.2 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""Simulation run API routes."""
import asyncio
import logging
from fastapi import APIRouter, Depends, Query, Request
from fastapi.responses import JSONResponse, Response
from lib.decorators import timed
from lib.rate_limiting import RATE_LIMIT_AGENTS_CREATE, limiter
from lib.request_logging import RunIdSource, log_route_completion_decorator
from simulation.api.constants import (
DEFAULT_AGENT_LIST_LIMIT,
DEFAULT_AGENT_LIST_OFFSET,
DEFAULT_SIMULATION_CONFIG,
MAX_AGENT_LIST_LIMIT,
)
from simulation.api.dependencies.auth import require_auth
from simulation.api.errors import (
ApiHandleAlreadyExistsError,
ApiRunCreationFailedError,
ApiRunNotFoundError,
)
from simulation.api.schemas.simulation import (
AgentSchema,
CreateAgentRequest,
DefaultConfigSchema,
FeedAlgorithmSchema,
MetricSchema,
PostSchema,
RunDetailsResponse,
RunListItem,
RunRequest,
RunResponse,
TurnSchema,
)
from simulation.api.services.agent_command_service import create_agent
from simulation.api.services.agent_query_service import list_agents
from simulation.api.services.metadata_service import list_feed_algorithms, list_metrics
from simulation.api.services.run_execution_service import execute
from simulation.api.services.run_query_service import (
get_posts_by_uris,
get_run_details,
get_turns_for_run,
list_runs,
)
logger = logging.getLogger(__name__)
router = APIRouter(tags=["simulation"], dependencies=[Depends(require_auth)])
SIMULATION_RUN_ROUTE: str = "POST /v1/simulations/run"
SIMULATION_RUNS_ROUTE: str = "GET /v1/simulations/runs"
SIMULATION_RUN_DETAILS_ROUTE: str = "GET /v1/simulations/runs/{run_id}"
SIMULATION_RUN_TURNS_ROUTE: str = "GET /v1/simulations/runs/{run_id}/turns"
SIMULATION_AGENTS_ROUTE: str = "GET /v1/simulations/agents"
SIMULATION_AGENTS_CREATE_ROUTE: str = "POST /v1/simulations/agents"
SIMULATION_POSTS_ROUTE: str = "GET /v1/simulations/posts"
SIMULATION_FEED_ALGORITHMS_ROUTE: str = "GET /v1/simulations/feed-algorithms"
SIMULATION_METRICS_ROUTE: str = "GET /v1/simulations/metrics"
SIMULATION_CONFIG_DEFAULT_ROUTE: str = "GET /v1/simulations/config/default"
@router.get(
"/simulations/feed-algorithms",
response_model=list[FeedAlgorithmSchema],
status_code=200,
summary="List feed algorithms",
description="Return available feed algorithms with metadata for the UI.",
)
@log_route_completion_decorator(
route=SIMULATION_FEED_ALGORITHMS_ROUTE, success_type=list
)
async def get_simulation_feed_algorithms(
request: Request,
) -> list[FeedAlgorithmSchema] | Response:
"""Return registered feed algorithms with metadata."""
return await _execute_get_feed_algorithms(request)
@router.get(
"/simulations/metrics",
response_model=list[MetricSchema],
status_code=200,
summary="List metrics",
description="Return available metrics with metadata for the UI.",
)
@log_route_completion_decorator(route=SIMULATION_METRICS_ROUTE, success_type=list)
async def get_simulation_metrics(
request: Request,
) -> list[MetricSchema] | Response:
"""Return registered metrics with metadata."""
return await _execute_get_metrics(request)
@router.get(
"/simulations/config/default",
response_model=DefaultConfigSchema,
status_code=200,
summary="Get default simulation config",
description="Return default config for simulation start form (num_agents, num_turns, metric_keys).",
)
@log_route_completion_decorator(
route=SIMULATION_CONFIG_DEFAULT_ROUTE, success_type=DefaultConfigSchema
)
async def get_simulation_config_default(
request: Request,
) -> DefaultConfigSchema | Response:
"""Return default config for simulation start form."""
return await _execute_get_default_config(request)
@router.post(
"/simulations/agents",
response_model=AgentSchema,
status_code=201,
summary="Create simulation agent",
description="Create a user-generated agent.",
)
@limiter.limit(RATE_LIMIT_AGENTS_CREATE)
@log_route_completion_decorator(
route=SIMULATION_AGENTS_CREATE_ROUTE, success_type=AgentSchema
)
async def post_simulation_agents(
request: Request, body: CreateAgentRequest
) -> AgentSchema | Response:
"""Create an agent and return it."""
return await _execute_post_simulation_agents(request, body=body)
@router.get(
"/simulations/agents",
response_model=list[AgentSchema],
status_code=200,
summary="List simulation agents",
description="Return simulation agent profiles from DB for View agents and Create form.",
)
@log_route_completion_decorator(route=SIMULATION_AGENTS_ROUTE, success_type=list)
async def get_simulation_agents(
request: Request,
limit: int = Query(
default=DEFAULT_AGENT_LIST_LIMIT,
ge=1,
le=MAX_AGENT_LIST_LIMIT,
description="Maximum number of agents to return (ordered by handle).",
),
offset: int = Query(
default=DEFAULT_AGENT_LIST_OFFSET,
ge=0,
description="Number of agents to skip before returning results (ordered by handle).",
),
) -> list[AgentSchema] | Response:
"""Return all simulation agents from the database."""
return await _execute_get_simulation_agents(request, limit=limit, offset=offset)
@router.get(
"/simulations/runs",
response_model=list[RunListItem],
status_code=200,
summary="List simulation runs",
description="Return simulation run summaries for the UI.",
)
@log_route_completion_decorator(route=SIMULATION_RUNS_ROUTE, success_type=list)
async def get_simulation_runs(request: Request) -> list[RunListItem] | Response:
"""Return all simulation runs from the database (app engine backed by SqliteTransactionProvider; DB path from SIM_DB_PATH or local dev DB in LOCAL mode)."""
return await _execute_get_simulation_runs(request)
@router.post(
"/simulations/run",
response_model=RunResponse,
status_code=200,
summary="Run a simulation",
description="Execute a synchronous simulation run.",
)
@limiter.limit("5/minute")
@log_route_completion_decorator(
route=SIMULATION_RUN_ROUTE,
success_type=RunResponse,
run_id_from=RunIdSource.RESPONSE,
)
async def post_simulations_run(
request: Request, body: RunRequest
) -> RunResponse | Response:
"""Execute a simulation run and return completed or partial results."""
return await _execute_simulation_run(request=request, body=body)
@router.get(
"/simulations/runs/{run_id}",
response_model=RunDetailsResponse,
status_code=200,
summary="Get simulation run details",
description="Fetch run config and turn-by-turn action summary by run ID.",
)
@log_route_completion_decorator(
route=SIMULATION_RUN_DETAILS_ROUTE,
success_type=RunDetailsResponse,
run_id_from=RunIdSource.RESPONSE,
)
async def get_simulation_run(
request: Request, run_id: str
) -> RunDetailsResponse | Response:
"""Return run details and turn history for a persisted run."""
return await _execute_get_simulation_run(request=request, run_id=run_id)
@router.get(
"/simulations/posts",
response_model=list[PostSchema],
status_code=200,
summary="List simulation posts",
description="Return posts, optionally filtered by URIs. Batch lookup for feed resolution.",
)
@log_route_completion_decorator(route=SIMULATION_POSTS_ROUTE, success_type=list)
async def get_simulation_posts(
request: Request,
uris: list[str] | None = Query(default=None, description="Filter by post URIs"),
) -> list[PostSchema] | Response:
"""Return posts from the database (via SqliteTransactionProvider; DB path from SIM_DB_PATH or local dev DB in LOCAL mode)."""
return await _execute_get_simulation_posts(request, uris=uris)
@router.get(
"/simulations/runs/{run_id}/turns",
response_model=dict[str, TurnSchema],
status_code=200,
summary="Get simulation run turns",
description="Return full per-turn payload for a run ID.",
)
@log_route_completion_decorator(
route=SIMULATION_RUN_TURNS_ROUTE, success_type=dict, run_id_from=RunIdSource.PATH
)
async def get_simulation_run_turns(
request: Request, run_id: str
) -> dict[str, TurnSchema] | Response:
"""Return turn payload for a run from the database (via SqliteTransactionProvider; DB path from SIM_DB_PATH or local dev DB in LOCAL mode)."""
return await _execute_get_simulation_run_turns(request, run_id=run_id)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_metrics(
request: Request,
) -> list[MetricSchema] | Response:
"""Fetch metrics and convert unexpected failures to HTTP responses."""
try:
return await asyncio.to_thread(list_metrics)
except Exception:
logger.exception("Unexpected error while listing metrics")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_feed_algorithms(
request: Request,
) -> list[FeedAlgorithmSchema] | Response:
"""Fetch feed algorithms and convert unexpected failures to HTTP responses."""
try:
return await asyncio.to_thread(list_feed_algorithms)
except Exception:
logger.exception("Unexpected error while listing feed algorithms")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_default_config(
request: Request,
) -> DefaultConfigSchema | Response:
"""Fetch default config and convert unexpected failures to HTTP responses."""
try:
return DEFAULT_SIMULATION_CONFIG
except Exception:
logger.exception("Unexpected error while fetching default config")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_simulation_posts(
request: Request,
*,
uris: list[str] | None = None,
) -> list[PostSchema] | Response:
"""Fetch posts and convert unexpected failures to HTTP responses."""
try:
engine = request.app.state.engine
return await asyncio.to_thread(get_posts_by_uris, uris=uris, engine=engine)
except Exception:
logger.exception("Unexpected error while listing simulation posts")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_simulation_runs(
request: Request,
) -> list[RunListItem] | Response:
"""Fetch run summaries from the database and convert failures to HTTP responses."""
try:
engine = request.app.state.engine
# Use to_thread for consistency with other async routes and to prepare for real I/O later.
return await asyncio.to_thread(list_runs, engine=engine)
except Exception:
logger.exception("Unexpected error while listing simulation runs")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_post_simulation_agents(
request: Request, *, body: CreateAgentRequest
) -> AgentSchema | Response:
"""Create agent and convert known failures to HTTP responses."""
try:
return await asyncio.to_thread(create_agent, body)
except ApiHandleAlreadyExistsError as e:
return _error_response(
status_code=409,
code="HANDLE_ALREADY_EXISTS",
message="Agent with this handle already exists",
detail=e.handle,
)
except ValueError as e:
return _error_response(
status_code=422,
code="VALIDATION_ERROR",
message=str(e),
detail=None,
)
except Exception:
logger.exception("Unexpected error while creating agent")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_simulation_agents(
request: Request,
*,
limit: int,
offset: int,
) -> list[AgentSchema] | Response:
"""Fetch agent list from DB and convert unexpected failures to HTTP responses."""
try:
return await asyncio.to_thread(list_agents, limit=limit, offset=offset)
except Exception:
logger.exception("Unexpected error while listing simulation agents")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_simulation_run(
request: Request, body: RunRequest
) -> RunResponse | Response:
"""Run the simulation and return response; used for timing and logging."""
engine = request.app.state.engine
try:
return await asyncio.to_thread(
execute,
request=body,
engine=engine,
)
except ApiRunCreationFailedError as e:
logger.exception("Simulation run failed before run creation")
return _error_response(
status_code=500,
code="RUN_CREATION_FAILED",
message=e.message,
detail=None,
)
except Exception:
logger.exception("Unexpected error during simulation run")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_simulation_run_turns(
request: Request,
run_id: str,
) -> dict[str, TurnSchema] | Response:
"""Fetch run turns and convert known failures to HTTP responses."""
try:
engine = request.app.state.engine
return await asyncio.to_thread(get_turns_for_run, run_id=run_id, engine=engine)
except ApiRunNotFoundError as e:
return _error_response(
status_code=404,
code="RUN_NOT_FOUND",
message="Run not found",
detail=e.run_id,
)
except ValueError as e:
return _error_response(
status_code=400,
code="INVALID_RUN_ID",
message="Invalid run_id",
detail=str(e),
)
except Exception:
logger.exception("Unexpected error while fetching run turns")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
@timed(attach_attr="duration_ms", log_level=None)
async def _execute_get_simulation_run(
request: Request, run_id: str
) -> RunDetailsResponse | Response:
"""Fetch persisted run details and convert known failures to HTTP responses."""
engine = request.app.state.engine
try:
return await asyncio.to_thread(
get_run_details,
run_id=run_id,
engine=engine,
)
except ApiRunNotFoundError as e:
return _error_response(
status_code=404,
code="RUN_NOT_FOUND",
message="Run not found",
detail=e.run_id,
)
except ValueError as e:
return _error_response(
status_code=400,
code="INVALID_RUN_ID",
message="Invalid run_id",
detail=str(e),
)
except Exception:
logger.exception("Unexpected error while fetching run details")
return _error_response(
status_code=500,
code="INTERNAL_ERROR",
message="Internal server error",
detail=None,
)
def _error_response(
status_code: int,
code: str,
message: str,
detail: str | None = None,
) -> JSONResponse:
return JSONResponse(
status_code=status_code,
content={
"error": {
"code": code,
"message": message,
"detail": detail,
}
},
)