This commit is contained in:
parent
ab277f7628
commit
6b2e42e585
@ -11,25 +11,22 @@ from django.http import JsonResponse, HttpRequest
|
|||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.views.decorators.http import require_http_methods, require_POST
|
from django.views.decorators.http import require_http_methods, require_POST
|
||||||
|
|
||||||
# -------------------------------------------------
|
# ----- contracts version (best-effort) -----
|
||||||
# Contracts spec version (best-effort import)
|
|
||||||
# -------------------------------------------------
|
|
||||||
try:
|
try:
|
||||||
from pxy_contracts.version import SPEC_VERSION
|
from pxy_contracts.version import SPEC_VERSION
|
||||||
except Exception:
|
except Exception:
|
||||||
SPEC_VERSION = "0.1.0"
|
SPEC_VERSION = "0.1.0"
|
||||||
|
|
||||||
# Where to call your internal APIs for /api/agents/execute proxying.
|
# ----- INTERNAL CALL BASES -----
|
||||||
# In prod: set AGENTS_INTERNAL_BASE=http://127.0.0.1:8002
|
# For the generic /api/agents/execute proxy (kept for compatibility)
|
||||||
AGENTS_INTERNAL_BASE = getattr(settings, "AGENTS_INTERNAL_BASE", "")
|
AGENTS_INTERNAL_BASE = getattr(settings, "AGENTS_INTERNAL_BASE", "")
|
||||||
|
|
||||||
# -------------------------------------------------
|
# For the formatter endpoints we *force* an internal base and never guess from Host.
|
||||||
# Small helpers shared by formatter endpoints
|
# Set in .env: AGENTS_INTERNAL_BASE=http://127.0.0.1:8002
|
||||||
# -------------------------------------------------
|
# Fallback keeps you safe even if env is missing/misread.
|
||||||
def _base(request: HttpRequest) -> str:
|
FORMAT_INTERNAL_BASE = AGENTS_INTERNAL_BASE or "http://127.0.0.1:8002"
|
||||||
"""Absolute base URL without trailing slash."""
|
|
||||||
return request.build_absolute_uri("/")[:-1]
|
|
||||||
|
|
||||||
|
# ===== helpers =====
|
||||||
def _load_body(request: HttpRequest) -> Dict[str, Any]:
|
def _load_body(request: HttpRequest) -> Dict[str, Any]:
|
||||||
try:
|
try:
|
||||||
raw = (request.body or b"").decode("utf-8")
|
raw = (request.body or b"").decode("utf-8")
|
||||||
@ -46,7 +43,6 @@ def _extract_payload(body: Dict[str, Any]) -> Tuple[Dict[str, Any], str]:
|
|||||||
"""
|
"""
|
||||||
if isinstance(body.get("payload"), dict):
|
if isinstance(body.get("payload"), dict):
|
||||||
return body["payload"], "payload"
|
return body["payload"], "payload"
|
||||||
|
|
||||||
args_raw = (body.get("input", {}) or {}).get("args_raw") or ""
|
args_raw = (body.get("input", {}) or {}).get("args_raw") or ""
|
||||||
cleaned = re.sub(r"^/\w+\s*", "", args_raw).strip()
|
cleaned = re.sub(r"^/\w+\s*", "", args_raw).strip()
|
||||||
if cleaned:
|
if cleaned:
|
||||||
@ -56,14 +52,14 @@ def _extract_payload(body: Dict[str, Any]) -> Tuple[Dict[str, Any], str]:
|
|||||||
pass
|
pass
|
||||||
return {}, "empty"
|
return {}, "empty"
|
||||||
|
|
||||||
def _post_underlying(request: HttpRequest, agent: str, payload: Dict[str, Any], timeout: float = 60.0):
|
def _post_underlying(agent: str, payload: Dict[str, Any], timeout: float = 60.0):
|
||||||
"""
|
"""
|
||||||
Directly call the real internal APIs on THIS host:8002, bypassing the execute proxy.
|
Call the *real* internal APIs via a fixed base (no build_absolute_uri):
|
||||||
sami -> /api/sami/run
|
sami -> /api/sami/run
|
||||||
sites -> /api/sites/search
|
sites -> /api/sites/search
|
||||||
"""
|
"""
|
||||||
path = "/api/sami/run" if agent == "sami" else "/api/sites/search"
|
path = "/api/sami/run" if agent == "sami" else "/api/sites/search"
|
||||||
url = f"{_base(request)}{path}"
|
url = f"{FORMAT_INTERNAL_BASE.rstrip('/')}{path}"
|
||||||
try:
|
try:
|
||||||
r = requests.post(url, json=payload, timeout=timeout)
|
r = requests.post(url, json=payload, timeout=timeout)
|
||||||
try:
|
try:
|
||||||
@ -72,11 +68,11 @@ def _post_underlying(request: HttpRequest, agent: str, payload: Dict[str, Any],
|
|||||||
data = {"code": "NON_JSON", "message": r.text[:2000]}
|
data = {"code": "NON_JSON", "message": r.text[:2000]}
|
||||||
return r.status_code, data
|
return r.status_code, data
|
||||||
except requests.Timeout:
|
except requests.Timeout:
|
||||||
return 504, {"code": "UPSTREAM_TIMEOUT", "message": "agent upstream timed out"}
|
return 504, {"code": "UPSTREAM_TIMEOUT", "message": "agent upstream timed out", "_debug_url": url}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return 500, {"code": "EXEC_ERROR", "message": str(e)}
|
return 500, {"code": "EXEC_ERROR", "message": str(e), "_debug_url": url}
|
||||||
|
|
||||||
# Tiny text builders (handy for bots)
|
# Tiny text builders for bot replies
|
||||||
def _text_sami(data: Dict[str, Any]) -> str:
|
def _text_sami(data: Dict[str, Any]) -> str:
|
||||||
if "beta" in data and "r2" in data:
|
if "beta" in data and "r2" in data:
|
||||||
lines = [f"SAMI run: β={data['beta']:.3f}, R²={data['r2']:.3f}"]
|
lines = [f"SAMI run: β={data['beta']:.3f}, R²={data['r2']:.3f}"]
|
||||||
@ -95,20 +91,19 @@ def _text_sites(data: Dict[str, Any]) -> str:
|
|||||||
lines = [f"Top sites for {biz} in {city}:"]
|
lines = [f"Top sites for {biz} in {city}:"]
|
||||||
for i, c in enumerate(data["candidates"][:3], 1):
|
for i, c in enumerate(data["candidates"][:3], 1):
|
||||||
lines.append(f"{i}. score={c.get('score',0):.2f} @ ({c.get('lat',0):.5f},{c.get('lon',0):.5f})")
|
lines.append(f"{i}. score={c.get('score',0):.2f} @ ({c.get('lat',0):.5f},{c.get('lon',0):.5f})")
|
||||||
for k in ("share_url","isochrones_geojson_url","candidates_geojson_url"):
|
for k in ("share_url", "isochrones_geojson_url", "candidates_geojson_url"):
|
||||||
if data.get(k): lines.append(data[k])
|
if data.get(k): lines.append(data[k])
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
if data.get("code"):
|
if data.get("code"):
|
||||||
return f"⚠️ {data.get('code')}: {data.get('message','')}"
|
return f"⚠️ {data.get('code')}: {data.get('message','')}"
|
||||||
return "Site scoring ready."
|
return "Site scoring ready."
|
||||||
|
|
||||||
# -------------------------------------------------
|
# ===== public endpoints =====
|
||||||
# Public endpoints
|
|
||||||
# -------------------------------------------------
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
@require_http_methods(["GET", "POST"])
|
@require_http_methods(["GET", "POST"])
|
||||||
def agents_list(request: HttpRequest):
|
def agents_list(request: HttpRequest):
|
||||||
base = _base(request)
|
# use request host only for *outward* links (safe)
|
||||||
|
base = request.build_absolute_uri("/")[:-1]
|
||||||
agents = [
|
agents = [
|
||||||
{
|
{
|
||||||
"agent": "sami",
|
"agent": "sami",
|
||||||
@ -129,7 +124,6 @@ def agents_list(request: HttpRequest):
|
|||||||
"description": "Site scoring (access, demand, competition) with maps",
|
"description": "Site scoring (access, demand, competition) with maps",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
lines = ["Available agents:"]
|
lines = ["Available agents:"]
|
||||||
for a in agents:
|
for a in agents:
|
||||||
lines.append(f"- {a['agent']}: {a['description']}")
|
lines.append(f"- {a['agent']}: {a['description']}")
|
||||||
@ -145,10 +139,8 @@ def agents_list(request: HttpRequest):
|
|||||||
@require_POST
|
@require_POST
|
||||||
def agents_execute(request: HttpRequest):
|
def agents_execute(request: HttpRequest):
|
||||||
"""
|
"""
|
||||||
POST /api/agents/execute
|
|
||||||
Body: { "agent": "sami"|"sites", "payload": {...} }
|
Body: { "agent": "sami"|"sites", "payload": {...} }
|
||||||
Proxies to: /api/sami/run or /api/sites/search
|
Proxies to the *internal* API using AGENTS_INTERNAL_BASE (or same-host fallback).
|
||||||
(Uses AGENTS_INTERNAL_BASE if set; else same-host absolute URL.)
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
body = json.loads(request.body.decode("utf-8") or "{}")
|
body = json.loads(request.body.decode("utf-8") or "{}")
|
||||||
@ -156,43 +148,31 @@ def agents_execute(request: HttpRequest):
|
|||||||
payload = body.get("payload")
|
payload = body.get("payload")
|
||||||
|
|
||||||
if agent not in {"sami", "sites"}:
|
if agent not in {"sami", "sites"}:
|
||||||
return JsonResponse(
|
return JsonResponse({"code": "AGENT_NOT_FOUND", "message": f"unknown agent '{agent}'"}, status=404)
|
||||||
{"code": "AGENT_NOT_FOUND", "message": f"unknown agent '{agent}'"},
|
|
||||||
status=404,
|
|
||||||
)
|
|
||||||
if payload is None:
|
if payload is None:
|
||||||
return JsonResponse(
|
return JsonResponse({"code": "BAD_REQUEST", "message": "missing 'payload'"}, status=400)
|
||||||
{"code": "BAD_REQUEST", "message": "missing 'payload'"},
|
|
||||||
status=400,
|
|
||||||
)
|
|
||||||
|
|
||||||
path = "/api/sami/run" if agent == "sami" else "/api/sites/search"
|
path = "/api/sami/run" if agent == "sami" else "/api/sites/search"
|
||||||
url = (AGENTS_INTERNAL_BASE or "").rstrip("/") + path
|
base = (AGENTS_INTERNAL_BASE or "http://127.0.0.1:8002").rstrip("/")
|
||||||
if not url.startswith("http"): # fall back to same host:port (8002)
|
url = f"{base}{path}"
|
||||||
url = f"{_base(request)}{path}"
|
|
||||||
|
|
||||||
r = requests.post(url, json=payload, timeout=90)
|
r = requests.post(url, json=payload, timeout=90)
|
||||||
return JsonResponse(r.json(), status=r.status_code, safe=False)
|
return JsonResponse(r.json(), status=r.status_code, safe=False)
|
||||||
|
|
||||||
except requests.Timeout:
|
except requests.Timeout:
|
||||||
return JsonResponse(
|
return JsonResponse({"code": "UPSTREAM_TIMEOUT", "message": "agent upstream timed out"}, status=504)
|
||||||
{"code": "UPSTREAM_TIMEOUT", "message": "agent upstream timed out"},
|
|
||||||
status=504,
|
|
||||||
)
|
|
||||||
except ValueError as ve:
|
except ValueError as ve:
|
||||||
return JsonResponse({"code": "BAD_JSON", "message": str(ve)}, status=400)
|
return JsonResponse({"code": "BAD_JSON", "message": str(ve)}, status=400)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return JsonResponse({"code": "AGENT_EXEC_ERROR", "message": str(e)}, status=500)
|
return JsonResponse({"code": "AGENT_EXEC_ERROR", "message": str(e)}, status=500)
|
||||||
|
|
||||||
# -------------------------------------------------
|
# ----- formatters (call underlying APIs directly via fixed base) -----
|
||||||
# Formatter endpoints (call underlying APIs directly)
|
|
||||||
# -------------------------------------------------
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
@require_http_methods(["GET", "POST"])
|
@require_http_methods(["GET", "POST"])
|
||||||
def format_sami(request: HttpRequest):
|
def format_sami(request: HttpRequest):
|
||||||
body = _load_body(request)
|
body = _load_body(request)
|
||||||
payload, src = _extract_payload(body)
|
payload, src = _extract_payload(body)
|
||||||
status, data = _post_underlying(request, "sami", payload, timeout=60.0)
|
status, data = _post_underlying("sami", payload, timeout=60.0)
|
||||||
data = data if isinstance(data, dict) else {"result": data}
|
data = data if isinstance(data, dict) else {"result": data}
|
||||||
data.setdefault("_echo", {"src": src, "payload_keys": list(payload.keys())})
|
data.setdefault("_echo", {"src": src, "payload_keys": list(payload.keys())})
|
||||||
try:
|
try:
|
||||||
@ -206,7 +186,7 @@ def format_sami(request: HttpRequest):
|
|||||||
def format_sites(request: HttpRequest):
|
def format_sites(request: HttpRequest):
|
||||||
body = _load_body(request)
|
body = _load_body(request)
|
||||||
payload, src = _extract_payload(body)
|
payload, src = _extract_payload(body)
|
||||||
status, data = _post_underlying(request, "sites", payload, timeout=60.0)
|
status, data = _post_underlying("sites", payload, timeout=60.0)
|
||||||
data = data if isinstance(data, dict) else {"result": data}
|
data = data if isinstance(data, dict) else {"result": data}
|
||||||
data.setdefault("_echo", {"src": src, "payload_keys": list(payload.keys())})
|
data.setdefault("_echo", {"src": src, "payload_keys": list(payload.keys())})
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user