Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Harness-owned httpx tracer, loaded via PYTHONPATH (sitecustomize hook). |
| 2 | |
| 3 | Examples must never reference COMPOSIO_TRACE_FILE themselves (lint-enforced). |
| 4 | """ |
| 5 | |
| 6 | import json |
| 7 | import os |
| 8 | import re |
| 9 | import sys |
| 10 | from urllib.parse import urlsplit |
| 11 | |
| 12 | _TRACE = os.environ.get("COMPOSIO_TRACE_FILE") |
| 13 | _STAGING_BASE_URL = "https://staging-backend.composio.dev" |
| 14 | |
| 15 | if _TRACE: |
| 16 | # Mirrors harness/backend-url.mjs: COMPOSIO_BASE_URL selects the backend and |
| 17 | # defaults to staging. The structural checks stay — a base URL carrying a |
| 18 | # path, query, fragment, or embedded credentials means the caller meant |
| 19 | # something else, and the host pin below needs a bare root to be meaningful. |
| 20 | _base_url = os.environ.get("COMPOSIO_BASE_URL", _STAGING_BASE_URL) |
| 21 | _parsed_base_url = urlsplit(_base_url) |
| 22 | if not ( |
| 23 | _parsed_base_url.scheme == "https" |
| 24 | and _parsed_base_url.hostname |
| 25 | and _parsed_base_url.path in {"", "/"} |
| 26 | and not _parsed_base_url.query |
| 27 | and not _parsed_base_url.fragment |
| 28 | and not _parsed_base_url.username |
| 29 | and not _parsed_base_url.password |
| 30 | ): |
| 31 | print( |
| 32 | f"sitecustomize: refusing malformed COMPOSIO_BASE_URL: {_base_url}", |
| 33 | file=sys.stderr, |
| 34 | ) |
| 35 | os._exit(78) |
| 36 | |
| 37 | try: |
| 38 | import httpx |
| 39 | |
| 40 | _LLM_HOSTS = { |
| 41 | "api.openai.com", |
| 42 | "api.anthropic.com", |
| 43 | "generativelanguage.googleapis.com", |
| 44 | } |
| 45 | _BACKEND_HOST = _parsed_base_url.hostname |
| 46 | _ID_SEG = re.compile( |
| 47 | r"^(ca_|ac_|ti_|tr_|trs_|sess_|auth_|req_|proj_|org_)[\w-]+$" |
| 48 | r"|^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" |
| 49 | r"|^\d+$", |
| 50 | re.IGNORECASE, |
| 51 | ) |
| 52 | |
| 53 | def _template(path: str) -> str: |
| 54 | return "/".join("{id}" if _ID_SEG.match(seg) else seg for seg in path.split("/")) |
| 55 | |
| 56 | def _record(obj) -> None: |
| 57 | try: |
| 58 | with open(_TRACE, "a") as f: |
| 59 | f.write(json.dumps(obj) + "\n") |
| 60 | except OSError: |
| 61 | pass |
| 62 | |
| 63 | def _log(request, response) -> None: |
| 64 | host = request.url.host |
| 65 | if host == _BACKEND_HOST: |
| 66 | status = f"{response.status_code // 100}xx" if response is not None else "ERR" |
| 67 | _record({"m": request.method.upper(), "p": _template(request.url.path), "s": status}) |
| 68 | elif host in _LLM_HOSTS: |
| 69 | _record({"llm": host}) |
| 70 | |
| 71 | # Outbound-email guard: tool executions matching the denylist are |
| 72 | # refused at the transport, never forwarded to the backend. |
| 73 | _DENY = re.compile( |
| 74 | os.environ.get( |
| 75 | "COMPOSIO_TOOL_DENYLIST", |
| 76 | r"GMAIL_SEND|GMAIL_REPLY|SEND_EMAIL|SEND_DRAFT|OUTLOOK[A-Z_]*SEND", |
| 77 | ), |
| 78 | re.IGNORECASE, |
| 79 | ) |
| 80 | |
| 81 | def _guard(request) -> None: |
| 82 | if request.url.host == _BACKEND_HOST and _DENY.search(request.url.path): |
| 83 | _record({"m": request.method.upper(), "p": _template(request.url.path), "s": "BLOCKED"}) |
| 84 | raise RuntimeError(f"harness: outbound-email tool execution blocked ({request.url.path})") |
| 85 | |
| 86 | _orig_send = httpx.Client.send |
| 87 | |
| 88 | def _send(self, request, *args, **kwargs): |
| 89 | _guard(request) |
| 90 | response = None |
| 91 | try: |
| 92 | response = _orig_send(self, request, *args, **kwargs) |
| 93 | return response |
| 94 | finally: |
| 95 | _log(request, response) |
| 96 | |
| 97 | httpx.Client.send = _send |
| 98 | |
| 99 | _orig_send_async = httpx.AsyncClient.send |
| 100 | |
| 101 | async def _send_async(self, request, *args, **kwargs): |
| 102 | _guard(request) |
| 103 | response = None |
| 104 | try: |
| 105 | response = await _orig_send_async(self, request, *args, **kwargs) |
| 106 | return response |
| 107 | finally: |
| 108 | _log(request, response) |
| 109 | |
| 110 | httpx.AsyncClient.send = _send_async |
| 111 | except Exception: # noqa: BLE001 - tracing must never break the example |
| 112 | pass |