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
ts 80 lines 2.7 KB
Raw
1 import { Effect } from 'effect';
2
3 /**
4 * Request and byte totals for every Composio API call a client made.
5 */
6 export interface ClientMetrics {
7 readonly byteSize: number;
8 readonly requests: number;
9 }
10
11 export interface ClientMetricsCollector {
12 /**
13 * A `fetch` to hand to the Composio client. It resolves `globalThis.fetch`
14 * on every call, so a spy installed after the client was built still
15 * intercepts the request.
16 */
17 readonly fetch: typeof globalThis.fetch;
18 readonly getMetrics: () => Effect.Effect<ClientMetrics>;
19 }
20
21 // Statuses whose response carries no body by definition. `new Response(body,
22 // init)` rejects a body for them, and there is nothing to count anyway.
23 const BODYLESS_STATUSES: ReadonlySet<number> = new Set([101, 204, 205, 304]);
24
25 const isRedirect = (response: Response): boolean => response.status >= 300 && response.status < 400;
26
27 /**
28 * Builds a counting `fetch` wrapper plus a reader for its totals.
29 *
30 * The byte count comes from `Content-Length` when the server sends it, and
31 * otherwise from a pass-through `TransformStream` over the body, so the body
32 * is never buffered a second time.
33 */
34 export const makeClientMetricsCollector = (): ClientMetricsCollector => {
35 let requests = 0;
36 let byteSize = 0;
37
38 const countResponse = (response: Response): Response => {
39 const contentLength = response.headers.get('content-length');
40 if (contentLength !== null) {
41 const parsed = Number.parseInt(contentLength, 10);
42 if (parsed >= 0) {
43 byteSize += parsed;
44 return response;
45 }
46 }
47
48 // A redirect is handed back untouched: the client inspects `type` and
49 // `url` on it to decide whether following the hop is safe, and a rewrapped
50 // response reports neither. Its body is discarded before the next hop, so
51 // nothing is lost by not counting it.
52 if (response.body === null || BODYLESS_STATUSES.has(response.status) || isRedirect(response)) {
53 return response;
54 }
55
56 const counting = new TransformStream<Uint8Array, Uint8Array>({
57 transform(chunk, controller) {
58 byteSize += chunk.byteLength;
59 controller.enqueue(chunk);
60 },
61 });
62
63 return new Response(response.body.pipeThrough(counting), response);
64 };
65
66 const fetch: typeof globalThis.fetch = Object.assign(
67 (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
68 requests += 1;
69 return globalThis.fetch(input, init).then(countResponse);
70 },
71 // Bun's `fetch` carries a `preconnect` helper; forward it so the wrapper
72 // satisfies the same type.
73 { preconnect: (url: string | URL) => globalThis.fetch.preconnect(url) }
74 );
75
76 return {
77 fetch,
78 getMetrics: () => Effect.sync(() => ({ byteSize, requests })),
79 };
80 };