44
55from collections .abc import Callable
66
7- from fastapi import APIRouter , Request
7+ from fastapi import APIRouter , HTTPException , Request
8+ from pydantic import BaseModel
89from starlette .concurrency import run_in_threadpool
910
1011from core .middleware import require_admin
1314from src .ulysses_catalog import default_runtime_registry
1415from src .ulysses_discovery import HostDiscoverySnapshot , collect_host_discovery
1516from src .ulysses_hermes import collect_hermes_adoption
17+ from src .ulysses_hermes_control import HermesControl
18+ from src .ulysses_jobs import (
19+ RuntimeJobConfirmationError ,
20+ RuntimeJobConflict ,
21+ RuntimeJobError ,
22+ )
1623from src .ulysses_topology import build_topology_report
1724
1825
26+ class HermesAdoptionApplyRequest (BaseModel ):
27+ confirmation_phrase : str
28+ expected_source_root : str
29+ expected_gateway_unit : str
30+
31+
32+ class HermesLifecyclePlanRequest (BaseModel ):
33+ action : str
34+
35+
36+ class RuntimeJobExecuteRequest (BaseModel ):
37+ confirmation_token : str
38+ confirmation_phrase : str
39+
40+
41+ def _job_http_error (exc : RuntimeJobError ) -> HTTPException :
42+ if isinstance (exc , RuntimeJobConflict ):
43+ return HTTPException (409 , str (exc ))
44+ if isinstance (exc , RuntimeJobConfirmationError ):
45+ return HTTPException (400 , str (exc ))
46+ return HTTPException (400 , str (exc ))
47+
48+
1949def setup_ulysses_routes (
2050 * ,
2151 collector : Callable [[], HostDiscoverySnapshot ] = collect_host_discovery ,
2252 chroma_collector : Callable [[], dict ] = collect_chroma_persistence ,
2353 hermes_collector : Callable [[], dict ] = collect_hermes_adoption ,
54+ hermes_control_factory : Callable [[], HermesControl ] = HermesControl ,
2455) -> APIRouter :
2556 router = APIRouter (prefix = "/api/ulysses" , tags = ["ulysses" ])
2657
@@ -40,6 +71,95 @@ async def get_chroma_persistence(request: Request) -> dict:
4071 @router .get ("/hermes/adoption" )
4172 async def get_hermes_adoption (request : Request ) -> dict :
4273 require_admin (request )
43- return await run_in_threadpool (hermes_collector )
74+ report = await run_in_threadpool (hermes_collector )
75+ return hermes_control_factory ().decorate_report (report )
76+
77+ @router .post ("/hermes/adoption/apply" )
78+ async def apply_hermes_adoption (
79+ request : Request ,
80+ body : HermesAdoptionApplyRequest ,
81+ ) -> dict :
82+ require_admin (request )
83+ report = await run_in_threadpool (hermes_collector )
84+ try :
85+ return await run_in_threadpool (
86+ hermes_control_factory ().apply_adoption ,
87+ report ,
88+ confirmation_phrase = body .confirmation_phrase ,
89+ expected_source_root = body .expected_source_root ,
90+ expected_gateway_unit = body .expected_gateway_unit ,
91+ )
92+ except RuntimeJobError as exc :
93+ raise _job_http_error (exc ) from exc
94+
95+ @router .post ("/hermes/jobs/plan" )
96+ async def plan_hermes_lifecycle (
97+ request : Request ,
98+ body : HermesLifecyclePlanRequest ,
99+ ) -> dict :
100+ require_admin (request )
101+ report = await run_in_threadpool (hermes_collector )
102+ try :
103+ plan , token = await run_in_threadpool (
104+ hermes_control_factory ().create_lifecycle_plan ,
105+ report ,
106+ action = body .action ,
107+ )
108+ return {"job" : plan , "confirmation_token" : token }
109+ except RuntimeJobError as exc :
110+ raise _job_http_error (exc ) from exc
111+
112+ @router .get ("/jobs" )
113+ async def list_runtime_jobs (request : Request , limit : int = 50 ) -> dict :
114+ require_admin (request )
115+ jobs = await run_in_threadpool (
116+ hermes_control_factory ().jobs .list ,
117+ limit = limit ,
118+ )
119+ return {"schema_version" : "ulysses.runtime-jobs.v1" , "jobs" : jobs }
120+
121+ @router .get ("/jobs/{job_id}" )
122+ async def get_runtime_job (request : Request , job_id : str ) -> dict :
123+ require_admin (request )
124+ try :
125+ return await run_in_threadpool (
126+ hermes_control_factory ().jobs .get ,
127+ job_id ,
128+ )
129+ except RuntimeJobError as exc :
130+ raise HTTPException (404 , str (exc )) from exc
131+
132+ @router .get ("/jobs/{job_id}/log" )
133+ async def get_runtime_job_log (
134+ request : Request ,
135+ job_id : str ,
136+ max_chars : int = 16000 ,
137+ ) -> dict :
138+ require_admin (request )
139+ try :
140+ return await run_in_threadpool (
141+ hermes_control_factory ().jobs .read_log ,
142+ job_id ,
143+ max_chars = max_chars ,
144+ )
145+ except RuntimeJobError as exc :
146+ raise HTTPException (404 , str (exc )) from exc
147+
148+ @router .post ("/jobs/{job_id}/execute" )
149+ async def execute_runtime_job (
150+ request : Request ,
151+ job_id : str ,
152+ body : RuntimeJobExecuteRequest ,
153+ ) -> dict :
154+ require_admin (request )
155+ try :
156+ return await run_in_threadpool (
157+ hermes_control_factory ().jobs .execute ,
158+ job_id ,
159+ confirmation_token = body .confirmation_token ,
160+ confirmation_phrase = body .confirmation_phrase ,
161+ )
162+ except RuntimeJobError as exc :
163+ raise _job_http_error (exc ) from exc
44164
45165 return router
0 commit comments