|
| 1 | +"""SilkWeb Agent Proxy Router. |
| 2 | +
|
| 3 | +Proxies requests from api.silkweb.io/agents/{agent_name}/{path} |
| 4 | +to the correct localhost port where each agent runs. |
| 5 | +
|
| 6 | +Agent port mapping: |
| 7 | + aegis -> 3003 (Cybersecurity) |
| 8 | + navigator -> 3004 (Logistics) |
| 9 | + sentinel -> 3005 (IT Ops) |
| 10 | + oracle -> 3006 (Finance) |
| 11 | + atlas -> 3007 (Geospatial) |
| 12 | + justice -> 3008 (Legal) |
| 13 | + shield -> 3009 (Personal Injury) |
| 14 | + fortress -> 3010 (Criminal Defense) |
| 15 | + design -> 3002 (Design) |
| 16 | +""" |
| 17 | + |
| 18 | +import logging |
| 19 | +from typing import Any |
| 20 | + |
| 21 | +import httpx |
| 22 | +from fastapi import APIRouter, Request |
| 23 | +from fastapi.responses import JSONResponse |
| 24 | + |
| 25 | +logger = logging.getLogger("silkweb.agents_proxy") |
| 26 | + |
| 27 | +router = APIRouter(prefix="/agents", tags=["agents-proxy"]) |
| 28 | + |
| 29 | +# ── Agent name -> localhost port mapping ────────────────────────── |
| 30 | + |
| 31 | +AGENT_PORTS: dict[str, int] = { |
| 32 | + "aegis": 3003, |
| 33 | + "navigator": 3004, |
| 34 | + "sentinel": 3005, |
| 35 | + "oracle": 3006, |
| 36 | + "atlas": 3007, |
| 37 | + "justice": 3008, |
| 38 | + "shield": 3009, |
| 39 | + "fortress": 3010, |
| 40 | + "design": 3002, |
| 41 | +} |
| 42 | + |
| 43 | +AGENT_METADATA: dict[str, dict[str, str]] = { |
| 44 | + "aegis": { |
| 45 | + "id": "aegis-security", |
| 46 | + "name": "AEGIS", |
| 47 | + "description": "Cybersecurity Threat Intelligence — URL scanning, SSL inspection, domain reputation, email security", |
| 48 | + }, |
| 49 | + "navigator": { |
| 50 | + "id": "navigator-logistics", |
| 51 | + "name": "NAVIGATOR", |
| 52 | + "description": "Global Logistics Intelligence — route calculation, multimodal optimization, customs compliance, carbon footprint", |
| 53 | + }, |
| 54 | + "sentinel": { |
| 55 | + "id": "sentinel-ops", |
| 56 | + "name": "SENTINEL", |
| 57 | + "description": "IT Infrastructure Monitoring — health checks, DNS resolution, SSL expiry, log analysis, incident classification", |
| 58 | + }, |
| 59 | + "oracle": { |
| 60 | + "id": "oracle-finance", |
| 61 | + "name": "ORACLE", |
| 62 | + "description": "Financial Intelligence — company analysis, risk scoring, Benford's Law fraud detection, regulatory compliance", |
| 63 | + }, |
| 64 | + "atlas": { |
| 65 | + "id": "atlas-geospatial", |
| 66 | + "name": "ATLAS", |
| 67 | + "description": "Geospatial Intelligence — distance calculations, geofencing, sun position, route analysis", |
| 68 | + }, |
| 69 | + "justice": { |
| 70 | + "id": "justice-legal", |
| 71 | + "name": "JUSTICE", |
| 72 | + "description": "General Legal & Contract Law — contract analysis, NDA review, statute research, clause drafting, compliance", |
| 73 | + }, |
| 74 | + "shield": { |
| 75 | + "id": "shield-injury", |
| 76 | + "name": "SHIELD", |
| 77 | + "description": "Personal Injury / Accident Law — case evaluation, damage calculation, statute of limitations, insurance analysis", |
| 78 | + }, |
| 79 | + "fortress": { |
| 80 | + "id": "fortress-defense", |
| 81 | + "name": "FORTRESS", |
| 82 | + "description": "Criminal Defense Intelligence — charge analysis, constitutional rights, evidence suppression, sentencing guidelines", |
| 83 | + }, |
| 84 | + "design": { |
| 85 | + "id": "sphinx-design", |
| 86 | + "name": "SPHINX", |
| 87 | + "description": "Design Intelligence Agent", |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | +# Proxy timeout in seconds |
| 92 | +PROXY_TIMEOUT = 10.0 |
| 93 | + |
| 94 | +# Shared httpx async client (created lazily) |
| 95 | +_client: httpx.AsyncClient | None = None |
| 96 | + |
| 97 | + |
| 98 | +async def _get_client() -> httpx.AsyncClient: |
| 99 | + """Get or create the shared httpx async client.""" |
| 100 | + global _client |
| 101 | + if _client is None or _client.is_closed: |
| 102 | + _client = httpx.AsyncClient( |
| 103 | + timeout=httpx.Timeout(PROXY_TIMEOUT, connect=5.0), |
| 104 | + follow_redirects=False, |
| 105 | + limits=httpx.Limits(max_connections=50, max_keepalive_connections=10), |
| 106 | + ) |
| 107 | + return _client |
| 108 | + |
| 109 | + |
| 110 | +# ── /agents/list — Agent directory ─────────────────────────────── |
| 111 | + |
| 112 | +@router.get("/list") |
| 113 | +async def list_agents() -> JSONResponse: |
| 114 | + """Return a directory of all available agents with metadata.""" |
| 115 | + agents = [] |
| 116 | + client = await _get_client() |
| 117 | + |
| 118 | + for name, port in AGENT_PORTS.items(): |
| 119 | + meta = AGENT_METADATA.get(name, {}) |
| 120 | + entry = { |
| 121 | + "id": meta.get("id", name), |
| 122 | + "name": meta.get("name", name.upper()), |
| 123 | + "description": meta.get("description", ""), |
| 124 | + "proxy_prefix": f"/agents/{name}", |
| 125 | + "status": "unknown", |
| 126 | + } |
| 127 | + |
| 128 | + # Quick health check (non-blocking, with short timeout) |
| 129 | + try: |
| 130 | + resp = await client.get( |
| 131 | + f"http://127.0.0.1:{port}/health", |
| 132 | + timeout=httpx.Timeout(2.0), |
| 133 | + ) |
| 134 | + if resp.status_code == 200: |
| 135 | + entry["status"] = "operational" |
| 136 | + else: |
| 137 | + entry["status"] = "degraded" |
| 138 | + except Exception: |
| 139 | + entry["status"] = "offline" |
| 140 | + |
| 141 | + agents.append(entry) |
| 142 | + |
| 143 | + return JSONResponse( |
| 144 | + content={"agents": agents, "count": len(agents)}, |
| 145 | + headers=_cors_headers(), |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +# ── Catch-all proxy: /agents/{agent_name}/{path} ──────────────── |
| 150 | + |
| 151 | +@router.api_route( |
| 152 | + "/{agent_name}/{path:path}", |
| 153 | + methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"], |
| 154 | +) |
| 155 | +async def proxy_to_agent(agent_name: str, path: str, request: Request) -> JSONResponse: |
| 156 | + """Proxy a request to the correct agent's localhost port. |
| 157 | +
|
| 158 | + Maps: |
| 159 | + POST /agents/aegis/scan/url -> POST http://127.0.0.1:3003/scan/url |
| 160 | + POST /agents/oracle/analyze/company -> POST http://127.0.0.1:3006/analyze/company |
| 161 | + """ |
| 162 | + # Handle CORS preflight |
| 163 | + if request.method == "OPTIONS": |
| 164 | + return JSONResponse(content={}, status_code=204, headers=_cors_headers()) |
| 165 | + |
| 166 | + # Resolve agent |
| 167 | + agent_key = agent_name.lower() |
| 168 | + port = AGENT_PORTS.get(agent_key) |
| 169 | + |
| 170 | + if port is None: |
| 171 | + return JSONResponse( |
| 172 | + status_code=404, |
| 173 | + content={ |
| 174 | + "error": f"Unknown agent: {agent_name}", |
| 175 | + "available_agents": list(AGENT_PORTS.keys()), |
| 176 | + }, |
| 177 | + headers=_cors_headers(), |
| 178 | + ) |
| 179 | + |
| 180 | + # Build target URL |
| 181 | + target_url = f"http://127.0.0.1:{port}/{path}" |
| 182 | + |
| 183 | + # Read request body (if any) |
| 184 | + body: bytes | None = None |
| 185 | + if request.method in ("POST", "PUT", "PATCH"): |
| 186 | + body = await request.body() |
| 187 | + |
| 188 | + # Forward headers (only safe ones) |
| 189 | + forward_headers: dict[str, str] = {} |
| 190 | + content_type = request.headers.get("content-type") |
| 191 | + if content_type: |
| 192 | + forward_headers["Content-Type"] = content_type |
| 193 | + authorization = request.headers.get("authorization") |
| 194 | + if authorization: |
| 195 | + forward_headers["Authorization"] = authorization |
| 196 | + |
| 197 | + # Proxy the request |
| 198 | + client = await _get_client() |
| 199 | + try: |
| 200 | + resp = await client.request( |
| 201 | + method=request.method, |
| 202 | + url=target_url, |
| 203 | + content=body, |
| 204 | + headers=forward_headers, |
| 205 | + ) |
| 206 | + |
| 207 | + # Try to return as JSON, fall back to plain text |
| 208 | + try: |
| 209 | + response_json = resp.json() |
| 210 | + return JSONResponse( |
| 211 | + content=response_json, |
| 212 | + status_code=resp.status_code, |
| 213 | + headers=_cors_headers(), |
| 214 | + ) |
| 215 | + except Exception: |
| 216 | + return JSONResponse( |
| 217 | + content={"raw": resp.text}, |
| 218 | + status_code=resp.status_code, |
| 219 | + headers=_cors_headers(), |
| 220 | + ) |
| 221 | + |
| 222 | + except httpx.TimeoutException: |
| 223 | + logger.warning(f"Timeout proxying to {agent_key} at {target_url}") |
| 224 | + return JSONResponse( |
| 225 | + status_code=504, |
| 226 | + content={ |
| 227 | + "error": "Agent request timed out", |
| 228 | + "agent": agent_key, |
| 229 | + "timeout_seconds": PROXY_TIMEOUT, |
| 230 | + }, |
| 231 | + headers=_cors_headers(), |
| 232 | + ) |
| 233 | + except httpx.ConnectError: |
| 234 | + logger.warning(f"Connection refused for {agent_key} at {target_url}") |
| 235 | + return JSONResponse( |
| 236 | + status_code=502, |
| 237 | + content={ |
| 238 | + "error": f"Agent '{agent_key}' is not running or unreachable", |
| 239 | + "agent": agent_key, |
| 240 | + "port": port, |
| 241 | + }, |
| 242 | + headers=_cors_headers(), |
| 243 | + ) |
| 244 | + except Exception as exc: |
| 245 | + logger.error(f"Proxy error for {agent_key}: {exc}") |
| 246 | + return JSONResponse( |
| 247 | + status_code=502, |
| 248 | + content={ |
| 249 | + "error": "Proxy error", |
| 250 | + "agent": agent_key, |
| 251 | + "detail": str(exc), |
| 252 | + }, |
| 253 | + headers=_cors_headers(), |
| 254 | + ) |
| 255 | + |
| 256 | + |
| 257 | +# ── CORS helper ────────────────────────────────────────────────── |
| 258 | + |
| 259 | +def _cors_headers() -> dict[str, str]: |
| 260 | + """Return CORS headers that allow ChatGPT Custom GPT Actions.""" |
| 261 | + return { |
| 262 | + "Access-Control-Allow-Origin": "*", |
| 263 | + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", |
| 264 | + "Access-Control-Allow-Headers": "Authorization, Content-Type, openai-conversation-id, openai-ephemeral-user-id", |
| 265 | + "Access-Control-Max-Age": "3600", |
| 266 | + } |
0 commit comments