"""Transparent, loopback-only OpenAI-compatible evidence proxy for DataAgent.""" from __future__ import annotations import json import time from pathlib import Path import httpx from fastapi import FastAPI, HTTPException, Request from fastapi.responses import StreamingResponse from scripts.dataagent_trial import OUT, load_private app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) @app.get("/healthz") async def healthz(): return {"status": "ok", "bind": "127.0.0.1"} def _redact(text: str, p: dict) -> str: for value in (p.get("upstream_key"), p.get("embedding_key"), p.get("upstream_base"), p.get("embedding_base")): if value: text = text.replace(value, "[REDACTED]") return text @app.api_route("/v1/{path:path}", methods=["GET", "POST", "PUT", "DELETE"]) async def forward(path: str, request: Request): p = load_private() incoming_auth = request.headers.get("authorization", "") model_group = "embedding" if path.rstrip("/").endswith("embeddings") else "chat" expected = p[model_group + "_local_token"] if incoming_auth != "Bearer " + expected: raise HTTPException(status_code=401, detail="invalid local trial token") base = p["embedding_base"] if model_group == "embedding" else p["upstream_base"] key = p["embedding_key"] if model_group == "embedding" else p["upstream_key"] body = await request.body() # The Java client may use chunked transfer encoding. Rebuild the outbound # content-length from the buffered body instead of forwarding TE alongside it. headers = {k: v for k, v in request.headers.items() if k.lower() not in {"authorization", "host", "content-length", "transfer-encoding", "connection", "accept-encoding"}} headers["authorization"] = "Bearer " + key headers["accept-encoding"] = "identity" upstream_url = base.rstrip("/") + "/" + path if request.url.query: upstream_url += "?" + request.url.query started = time.time() evidence = { "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S%z"), "model_group": model_group, "method": request.method, "path": "/v1/" + path, "model": p["embedding_model"] if model_group == "embedding" else p["chat_model"], "request_headers": {k: v for k, v in headers.items() if k.lower() not in {"authorization", "host"}}, "request_body_redacted": _redact(body.decode("utf-8", errors="replace"), p), "tls_verify": True, } client = httpx.AsyncClient(timeout=None) try: req = client.build_request(request.method, upstream_url, headers=headers, content=body) response = await client.send(req, stream=True) response_headers = {k: v for k, v in response.headers.items() if k.lower() in {"content-type", "cache-control", "x-request-id"}} async def chunks(): collected = bytearray() try: async for chunk in response.aiter_raw(): collected.extend(chunk) yield chunk finally: raw = _redact(collected.decode("utf-8", errors="replace"), p) evidence.update({"http_status": response.status_code, "duration_ms": round((time.time() - started) * 1000), "response_body_redacted": raw}) _append_evidence(evidence) await response.aclose() await client.aclose() return StreamingResponse(chunks(), status_code=response.status_code, headers=response_headers) except Exception as exc: evidence.update({"error_type": type(exc).__name__, "duration_ms": round((time.time() - started) * 1000)}) _append_evidence(evidence) await client.aclose() raise HTTPException(status_code=502, detail="upstream model request failed") from None def _append_evidence(record: dict): p = load_private() name = "embedding-api.jsonl" if record.get("model_group") == "embedding" else "chat-api.jsonl" path = OUT / name line = json.dumps(record, ensure_ascii=False, separators=(",", ":")) line = _redact(line, p) with path.open("a", encoding="utf-8") as f: f.write(line + "\n") path.chmod(0o600)