#!/usr/bin/env python3 import json import os import sys from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import urlparse MODE = sys.argv[1] if len(sys.argv) > 1 else "alert" PORT = int(os.environ.get("CASAN_MOCK_PORT", "8080")) EVENTS = [] USAGE = [ { "provider": "local-prod-mock", "model": "billing-api-emulator", "run_id": "infra-lab-run", "step": "speckit.implement", "input_tokens": 1842, "output_tokens": 936, "total_tokens": 2778, "cost_usd": 0.08334, "latency_ms": 4210, "status": "success", } ] class Handler(BaseHTTPRequestHandler): def send_json(self, code, payload): body = json.dumps(payload).encode() self.send_response(code) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def do_GET(self): # noqa: N802 path = urlparse(self.path).path if path == "/healthz": self.send_json(200, {"status": "ok", "mode": MODE, "events": len(EVENTS)}) elif MODE == "alert" and path == "/events": self.send_json(200, EVENTS) elif MODE == "billing" and path in ("/usage", "/v1/usage"): self.send_json(200, USAGE) else: self.send_json(404, {"error": "not_found"}) def do_POST(self): # noqa: N802 if MODE != "alert": self.send_json(404, {"error": "not_found"}) return size = int(self.headers.get("Content-Length", "0")) raw = self.rfile.read(size) try: payload = json.loads(raw or b"{}") except json.JSONDecodeError: payload = {"raw": raw.decode(errors="replace")} EVENTS.append(payload) self.send_json(200, {"ok": True, "events": len(EVENTS)}) def log_message(self, *args): pass if __name__ == "__main__": HTTPServer(("0.0.0.0", PORT), Handler).serve_forever()