| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- from __future__ import annotations
- from fastapi import FastAPI, HTTPException
- from content_agent.run_service import RunService
- from content_agent.schemas import (
- JsonFileResponse,
- RecordsResponse,
- RunStartRequest,
- RunStartResponse,
- RunSummaryResponse,
- ValidationResponse,
- )
- app = FastAPI(title="Content Agent V1")
- service = RunService()
- @app.post("/runs", response_model=RunStartResponse)
- def start_run(request: RunStartRequest) -> RunStartResponse:
- state = service.start_run(request)
- if state["status"] != "success":
- raise HTTPException(status_code=500, detail=state.get("errors", ["run failed"]))
- trace_id = state["trace_id"]
- return RunStartResponse(
- trace_id=trace_id,
- status=state["status"],
- policy_bundle_version=state["policy_bundle_version"],
- platform=state["platform"],
- platform_mode=state["platform_mode"],
- output_dir=str(service.runtime.run_dir(trace_id)),
- )
- @app.get("/runs/{trace_id}", response_model=RunSummaryResponse)
- def get_run(trace_id: str) -> RunSummaryResponse:
- _ensure_run_exists(trace_id)
- return RunSummaryResponse(**service.get_summary(trace_id))
- @app.get("/runs/{trace_id}/candidates", response_model=RecordsResponse)
- def get_candidates(trace_id: str) -> RecordsResponse:
- return _jsonl_response(trace_id, "candidate_pool.jsonl")
- @app.get("/runs/{trace_id}/rule-decisions", response_model=RecordsResponse)
- def get_rule_decisions(trace_id: str) -> RecordsResponse:
- return _jsonl_response(trace_id, "rule_decisions.jsonl")
- @app.get("/runs/{trace_id}/source-edges", response_model=RecordsResponse)
- def get_source_edges(trace_id: str) -> RecordsResponse:
- return _jsonl_response(trace_id, "source_edges.jsonl")
- @app.get("/runs/{trace_id}/final-output", response_model=JsonFileResponse)
- def get_final_output(trace_id: str) -> JsonFileResponse:
- return _json_response(trace_id, "final_output.json")
- @app.get("/runs/{trace_id}/strategy-review", response_model=JsonFileResponse)
- def get_strategy_review(trace_id: str) -> JsonFileResponse:
- _ensure_run_exists(trace_id)
- return JsonFileResponse(trace_id=trace_id, data=service.strategy_review(trace_id))
- @app.get("/runs/{trace_id}/validation", response_model=ValidationResponse)
- def get_validation(trace_id: str) -> ValidationResponse:
- _ensure_run_exists(trace_id)
- return ValidationResponse(**service.validate_run(trace_id))
- def _jsonl_response(trace_id: str, filename: str) -> RecordsResponse:
- _ensure_run_exists(trace_id)
- return RecordsResponse(trace_id=trace_id, records=service.read_jsonl(trace_id, filename))
- def _json_response(trace_id: str, filename: str) -> JsonFileResponse:
- _ensure_run_exists(trace_id)
- return JsonFileResponse(trace_id=trace_id, data=service.read_json(trace_id, filename))
- def _ensure_run_exists(trace_id: str) -> None:
- if not service.runtime.run_dir(trace_id).exists():
- raise HTTPException(status_code=404, detail=f"run not found: {trace_id}")
|