Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
mjs 73 lines 2.3 KB
Raw
1 // Harness-owned fetch tracer. Injected via NODE_OPTIONS=--import; examples
2 // must never reference COMPOSIO_TRACE_FILE themselves (lint-enforced).
3 import { resolveBackendBaseUrl } from '../backend-url.mjs';
4
5 const traceFile = process.env.COMPOSIO_TRACE_FILE;
6
7 if (traceFile) {
8 const { appendFileSync } = await import('node:fs');
9
10 const LLM_HOSTS = new Set([
11 'api.openai.com',
12 'api.anthropic.com',
13 'generativelanguage.googleapis.com',
14 ]);
15
16 const backendHost = new URL(resolveBackendBaseUrl()).hostname;
17
18 const ID_SEG =
19 /^(ca_|ac_|ti_|tr_|trs_|sess_|auth_|req_|proj_|org_)[\w-]+$|^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$|^\d+$/i;
20 const template = (path) =>
21 path
22 .split('/')
23 .map((seg) => (ID_SEG.test(seg) ? '{id}' : seg))
24 .join('/');
25
26 const record = (obj) => {
27 try {
28 appendFileSync(traceFile, `${JSON.stringify(obj)}\n`);
29 } catch {
30 // never break the example over tracing
31 }
32 };
33
34 // Outbound-email guard: tool executions matching the denylist are refused at
35 // the transport, never forwarded to the backend.
36 const DENY = new RegExp(process.env.COMPOSIO_TOOL_DENYLIST ?? 'GMAIL_SEND|GMAIL_REPLY|SEND_EMAIL|SEND_DRAFT|OUTLOOK[A-Z_]*SEND', 'i');
37
38 const origFetch = globalThis.fetch;
39 globalThis.fetch = async function tracedFetch(input, init) {
40 let url;
41 try {
42 url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url);
43 } catch {
44 return origFetch.call(this, input, init);
45 }
46 const method = (
47 init?.method ??
48 (typeof input === 'object' && input !== null && 'method' in input ? input.method : 'GET') ??
49 'GET'
50 ).toUpperCase();
51
52 if (url.hostname === backendHost && DENY.test(url.pathname)) {
53 record({ m: method, p: template(url.pathname), s: 'BLOCKED' });
54 throw new Error(`harness: outbound-email tool execution blocked (${url.pathname})`);
55 }
56
57 let res;
58 try {
59 res = await origFetch.call(this, input, init);
60 return res;
61 } finally {
62 if (url.hostname === backendHost) {
63 record({
64 m: method,
65 p: template(url.pathname),
66 s: res ? `${Math.floor(res.status / 100)}xx` : 'ERR',
67 });
68 } else if (LLM_HOSTS.has(url.hostname)) {
69 record({ llm: url.hostname });
70 }
71 }
72 };
73 }