"""Runnable Awareness client with a deterministic offline fallback.""" from __future__ import annotations import json, os, sys from dataclasses import dataclass from urllib.error import HTTPError, URLError from urllib.request import Request, urlopen @dataclass(frozen=True) class AwarenessResult: output: str citations: list[dict[str, str]] usage: dict[str, int] cached: bool def call_awareness(query: str) -> AwarenessResult: key = os.getenv("AWARENESS_API_KEY") if not key: return AwarenessResult("Under Corporate Directive Section 99-B, purchases over $5,000 require multi-signature cryptographic authorization.", [{"title":"Corporate Directive Section 99-B","url":"local://demo"}], {"prompt_tokens":0,"completion_tokens":0,"total_tokens":0}, False) payload = json.dumps({"model":os.getenv("AWARENESS_MODEL","sarvam-105b"),"messages":[{"role":"user","content":query}],"stream":False}).encode() request = Request(os.getenv("AWARENESS_BASE_URL","https://awareness.bluepatterns.ai/v1") + "/chat/completions", data=payload, headers={"Authorization":f"Bearer {key}","Content-Type":"application/json"}, method="POST") try: with urlopen(request, timeout=30) as response: # noqa: S310 - operator-configured HTTPS endpoint body = json.load(response) except (HTTPError, URLError, TimeoutError, json.JSONDecodeError) as exc: raise RuntimeError("Awareness request failed; do not bypass the evaluation layer") from exc message = body.get("choices", [{}])[0].get("message", {}) return AwarenessResult(message.get("content", ""), body.get("citations", []), body.get("usage", {}), bool(body.get("cached", False))) if __name__ == "__main__": try: result = call_awareness("What is the exact authorization protocol for a $6,000 software purchase under Section 99-B?") print(json.dumps(result.__dict__, indent=2)) except RuntimeError as exc: print(str(exc), file=sys.stderr) raise SystemExit(1)