Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
feat(cli): add API metrics to monitor HTTP requests and data downloaded
jkomyno committed
Jan 14, 2026 at 14:22 UTC
178defdfa06171b095d7c4e9d9c407e934ebdfe5
6 files changed
+269
-74
ts/packages/cli/src/commands/py/commands/py.generate.cmd.ts
+5
@@ -2,6 +2,7 @@ import { Command, Options } from '@effect/cli';
2
import { pipe, Console, Effect, Option, Array } from 'effect';
3
import { FileSystem } from '@effect/platform';
4
import { ComposioToolkitsRepository } from 'src/services/composio-clients';
5
+import { logMetrics } from 'src/effects/log-metrics';
6
import type { GetCmdParams } from 'src/type-utils';
7
import { NodeProcess } from 'src/services/node-process';
8
import { createToolkitIndex } from 'src/generation/create-toolkit-index';
@@ -136,6 +137,10 @@ export function generatePythonTypeStubs({
137
`Generated files are available at: ${outputDir}`
138
);
139
140
+ // Log API metrics
141
+ const metrics = yield* client.getMetrics();
142
+ yield* logMetrics(metrics);
143
+
144
return outputDir;
145
});
146
}
ts/packages/cli/src/commands/ts/commands/ts.generate.cmd.ts
+5
@@ -21,6 +21,7 @@ import { Console, Effect, Option, pipe, Array } from 'effect';
21
import { Match } from 'effect';
22
import { FileSystem } from '@effect/platform';
23
import { ComposioToolkitsRepository } from 'src/services/composio-clients';
24
+import { logMetrics } from 'src/effects/log-metrics';
25
import { NodeProcess } from 'src/services/node-process';
26
import { createToolkitIndex } from 'src/generation/create-toolkit-index';
27
import type { GetCmdParams } from 'src/type-utils';
@@ -247,6 +248,10 @@ export function generateTypescriptTypeStubs({
248
`Generated files are available at: ${outputDir}`
249
);
250
251
+ // Log API metrics
252
+ const metrics = yield* client.getMetrics();
253
+ yield* logMetrics(metrics);
254
+
255
return outputDir;
256
});
257
}
ts/packages/cli/src/effects/log-metrics.ts
new
+36
@@ -0,0 +1,36 @@
1
+import { Effect } from 'effect';
2
+
3
+/**
4
+ * Metrics from API calls.
5
+ */
6
+export type Metrics = {
7
+ readonly byteSize: number;
8
+ readonly requests: number;
9
+};
10
+
11
+/**
12
+ * Formats byte size into a human-readable string.
13
+ * @param bytes - Number of bytes to format
14
+ * @returns A string like "1.23 KB" or "456 B"
15
+ */
16
+const formatBytes = (bytes: number): string => {
17
+ if (bytes < 1024) {
18
+ return `${bytes} B`;
19
+ }
20
+ const kb = bytes / 1024;
21
+ if (kb < 1024) {
22
+ return `${kb.toFixed(2)} KB`;
23
+ }
24
+ const mb = kb / 1024;
25
+ return `${mb.toFixed(2)} MB`;
26
+};
27
+
28
+/**
29
+ * Logs API metrics (request count and total bytes downloaded) at debug level.
30
+ * @param metrics - The metrics to log
31
+ * @returns An Effect that logs the metrics
32
+ */
33
+export const logMetrics = (metrics: Metrics): Effect.Effect<void> =>
34
+ Effect.logDebug(
35
+ `API metrics: ${metrics.requests} request(s), ${formatBytes(metrics.byteSize)} downloaded`
36
+ );
ts/packages/cli/src/services/composio-clients-cached.ts
+1
@@ -164,6 +164,7 @@ export const ComposioToolkitsRepositoryCached = Layer.effect(
164
165
// These methods don't need caching as they operate on already fetched data
166
// or perform validation that should always be fresh
167
+ getMetrics: () => underlyingRepository.getMetrics(),
168
validateToolkits: toolkitSlugs => underlyingRepository.validateToolkits(toolkitSlugs),
169
filterToolkitsBySlugs: (toolkits, toolkitSlugs) =>
170
underlyingRepository.filterToolkitsBySlugs(toolkits, toolkitSlugs),
ts/packages/cli/src/services/composio-clients.ts
+221
-73
@@ -1,6 +1,19 @@
1
-import { pipe, Data, Effect, Option, Schema, Array, Order, ParseResult, String } from 'effect';
2
-import { Composio as _RawComposioClient } from '@composio/client';
3
-import { Toolkit, Toolkits, ToolkitName } from 'src/models/toolkits';
1
+import {
2
+ pipe,
3
+ Data,
4
+ Effect,
5
+ Option,
6
+ Schema,
7
+ Array,
8
+ Order,
9
+ ParseResult,
10
+ String,
11
+ Stream,
12
+ Sink,
13
+ SynchronizedRef,
14
+} from 'effect';
15
+import { Composio as _RawComposioClient, APIPromise } from '@composio/client';
16
+import { Toolkit, Toolkits } from 'src/models/toolkits';
17
import { ToolsAsEnums, Tools, Tool } from 'src/models/tools';
18
import { Session, RetrievedSession } from 'src/models/session';
19
import { TriggerType, TriggerTypes, TriggerTypesAsEnums } from 'src/models/trigger-types';
@@ -88,20 +101,96 @@ export const HttpErrorResponse = Schema.Struct({
101
export type HttpErrorResponse = Schema.Schema.Type<typeof HttpErrorResponse>;
102
103
/**
91
- * Utilities
104
+ * Result of streaming a response with byte counting.
105
*/
106
+interface StreamedResponse {
107
+ /** The parsed JSON data from the response body */
108
+ readonly json: unknown;
109
+ /** The exact byte size of the response body */
110
+ readonly byteSize: number;
111
+}
112
+
113
+type Metrics = {
114
+ readonly byteSize: number;
115
+ readonly requests: number;
116
+};
117
+
118
+/**
119
+ * Streams a Fetch Response body, counting bytes precisely and parsing JSON in a single pass.
120
+ * Uses streaming to avoid loading the entire response into memory at once.
121
+ *
122
+ * @param response - The Fetch API Response object
123
+ * @returns An Effect that yields the parsed JSON data and byte count
124
+ */
125
+const streamResponseWithByteCount = (
126
+ response: Response
127
+): Effect.Effect<StreamedResponse, HttpServerError> =>
128
+ Effect.gen(function* () {
129
+ const body = response.body;
130
+ if (!body) {
131
+ return yield* Effect.fail(
132
+ new HttpServerError({
133
+ cause: 'Response body is null',
134
+ })
135
+ );
136
+ }
137
+
138
+ // Convert the ReadableStream to an Effect Stream
139
+ const byteStream = Stream.fromReadableStream(
140
+ () => body,
141
+ (error: unknown) =>
142
+ new HttpServerError({
143
+ cause: error,
144
+ })
145
+ );
146
+
147
+ // Collect all chunks while counting bytes
148
+ const [chunks, byteSize] = yield* pipe(
149
+ byteStream,
150
+ Stream.run(
151
+ Sink.fold<[Uint8Array[], number], Uint8Array>(
152
+ [[], 0],
153
+ () => true,
154
+ ([chunks, size], chunk) => [[...chunks, chunk], size + chunk.byteLength]
155
+ )
156
+ )
157
+ );
158
+
159
+ // Merge chunks into a single Uint8Array
160
+ const merged = new Uint8Array(byteSize);
161
+ let offset = 0;
162
+ for (const chunk of chunks) {
163
+ merged.set(chunk, offset);
164
+ offset += chunk.byteLength;
165
+ }
166
+
167
+ // Decode and parse JSON
168
+ const text = new TextDecoder().decode(merged);
169
+ const json = yield* Effect.try({
170
+ try: () => JSON.parse(text) as unknown,
171
+ catch: error =>
172
+ new HttpServerError({
173
+ cause: `Failed to parse JSON response: ${error}`,
174
+ }),
175
+ });
176
+
177
+ return { json, byteSize };
178
+ });
179
180
// Utility function for calling the Composio API and decoding its response.
181
// eslint-disable-next-line @typescript-eslint/no-explicit-any
182
const callClient = <T, S extends Schema.Schema<any, any>>(
183
clientSingleton: ComposioClientSingleton,
98
- apiCall: (client: _RawComposioClient) => Promise<T>,
184
+ apiCall: (client: _RawComposioClient) => APIPromise<T>,
185
responseSchema: S
100
-): Effect.Effect<Schema.Schema.Type<S>, HttpError | NoSuchElementException> =>
186
+): Effect.Effect<
187
+ { data: Schema.Schema.Type<S>; metrics: Metrics },
188
+ HttpError | NoSuchElementException
189
+> =>
190
Effect.gen(function* () {
191
const client = yield* clientSingleton.get();
103
- const json = yield* Effect.tryPromise({
104
- try: () => apiCall(client),
192
+ const response = yield* Effect.tryPromise({
193
+ try: () => apiCall(client).asResponse(),
194
catch: e => {
195
const decodedOpt = Schema.decodeUnknownOption(HttpErrorResponse)(e);
196
@@ -127,7 +216,11 @@ const callClient = <T, S extends Schema.Schema<any, any>>(
216
},
217
});
218
130
- return yield* pipe(
219
+ // Stream the response body with byte counting
220
+ const { json, byteSize } = yield* streamResponseWithByteCount(response);
221
+ const metrics = { byteSize, requests: 1 };
222
+
223
+ const typedJson = yield* pipe(
224
Schema.decodeUnknown(responseSchema)(json),
225
Effect.catchTag('ParseError', e => {
226
const message = ParseResult.TreeFormatter.formatErrorSync(e);
@@ -137,6 +230,8 @@ const callClient = <T, S extends Schema.Schema<any, any>>(
230
});
231
})
232
);
233
+
234
+ return { metrics, data: typedJson };
235
});
236
237
// Schema constraint for paginated responses
@@ -155,39 +250,49 @@ type PaginatedSchema = Schema.Schema<
250
// Automatically fetches all pages until the specified limit is reached.
251
const callClientWithPagination = <T, S extends PaginatedSchema>(
252
clientSingleton: ComposioClientSingleton,
158
- apiCall: (client: _RawComposioClient, cursor?: string) => Promise<T>,
253
+ apiCall: (client: _RawComposioClient, cursor?: string) => APIPromise<T>,
254
responseSchema: S,
255
limit: number
161
-): Effect.Effect<Schema.Schema.Type<S>, HttpError | NoSuchElementException> =>
256
+): Effect.Effect<
257
+ { data: Schema.Schema.Type<S>; metrics: Metrics },
258
+ HttpError | NoSuchElementException
259
+> =>
260
Effect.gen(function* () {
261
const client = yield* clientSingleton.get();
262
+ let totalByteSize = 0;
263
+ let totalRequests = 0;
264
+
265
+ const fetchPage = (cursor?: string): Effect.Effect<StreamedResponse, HttpServerError> =>
266
+ Effect.gen(function* () {
267
+ const response = yield* Effect.tryPromise({
268
+ try: () => apiCall(client, cursor).asResponse(),
269
+ catch: e => {
270
+ const decodedOpt = Schema.decodeUnknownOption(HttpErrorResponse)(e);
271
+
272
+ if (Option.isNone(decodedOpt)) {
273
+ return new HttpServerError({
274
+ cause: e,
275
+ });
276
+ }
277
165
- const fetchPage = (cursor?: string): Effect.Effect<T, HttpServerError> =>
166
- Effect.tryPromise({
167
- try: () => apiCall(client, cursor),
168
- catch: e => {
169
- const decodedOpt = Schema.decodeUnknownOption(HttpErrorResponse)(e);
278
+ const {
279
+ status,
280
+ error: { error },
281
+ } = decodedOpt.value;
282
+ const pretty = renderPrettyError([
283
+ ['code', error.code],
284
+ ['message', error.message],
285
+ ['suggested fix', error.suggested_fix],
286
+ ]);
287
171
- if (Option.isNone(decodedOpt)) {
288
return new HttpServerError({
173
- cause: e,
289
+ cause: `HTTP ${status}\n${pretty}`,
290
});
175
- }
176
-
177
- const {
178
- status,
179
- error: { error },
180
- } = decodedOpt.value;
181
- const pretty = renderPrettyError([
182
- ['code', error.code],
183
- ['message', error.message],
184
- ['suggested fix', error.suggested_fix],
185
- ]);
291
+ },
292
+ });
293
187
- return new HttpServerError({
188
- cause: `HTTP ${status}\n${pretty}`,
189
- });
190
- },
294
+ // Stream the response body with byte counting
295
+ return yield* streamResponseWithByteCount(response);
296
});
297
298
type DecodedPage = Schema.Schema.Type<S>;
@@ -209,26 +314,32 @@ const callClientWithPagination = <T, S extends PaginatedSchema>(
314
let totalPages = 0;
315
316
while (allItems.length < limit) {
212
- const json: T = yield* fetchPage(currentCursor ?? undefined);
317
+ const { json, byteSize } = yield* fetchPage(currentCursor ?? undefined);
318
+ totalByteSize += byteSize;
319
+ totalRequests += 1;
320
+
321
const decoded: DecodedPage = yield* decodeResponse(json);
322
323
allItems = allItems.concat(decoded.items);
324
totalPages = decoded.total_pages;
325
currentCursor = decoded.next_cursor;
326
219
- yield* Effect.logDebug(` Total items fetched: ${allItems.length}/${limit}`);
220
-
327
// Stop if no more pages
328
if (currentCursor === null) {
329
break;
330
}
331
}
332
333
+ const metrics = { byteSize: totalByteSize, requests: totalRequests };
334
+
335
return {
228
- items: allItems.slice(0, limit),
229
- total_pages: totalPages,
230
- next_cursor: currentCursor,
231
- } as DecodedPage;
336
+ data: {
337
+ items: allItems.slice(0, limit),
338
+ total_pages: totalPages,
339
+ next_cursor: currentCursor,
340
+ } as DecodedPage,
341
+ metrics,
342
+ };
343
});
344
345
/**
@@ -276,7 +387,27 @@ export class ComposioClientLive extends Effect.Service<ComposioClientLive>()(
387
effect: Effect.gen(function* () {
388
const clientSingleton = yield* ComposioClientSingleton;
389
390
+ // Initialize metrics tracking via SynchronizedRef
391
+ const metricsRef = yield* SynchronizedRef.make<Metrics>({ byteSize: 0, requests: 0 });
392
+
393
+ // Helper to update metrics and return just the data
394
+ const withMetrics = <A, E, R>(
395
+ effect: Effect.Effect<{ data: A; metrics: Metrics }, E, R>
396
+ ): Effect.Effect<A, E, R> =>
397
+ Effect.gen(function* () {
398
+ const { data, metrics } = yield* effect;
399
+ yield* SynchronizedRef.update(metricsRef, current => ({
400
+ byteSize: current.byteSize + metrics.byteSize,
401
+ requests: current.requests + metrics.requests,
402
+ }));
403
+ return data;
404
+ });
405
+
406
return {
407
+ /**
408
+ * Returns a snapshot of the current accumulated metrics (total bytes received and request count).
409
+ */
410
+ getMetrics: () => SynchronizedRef.get(metricsRef),
411
toolkits: {
412
/**
413
* Retrieves a comprehensive list of toolkits that are available to the authenticated project.
@@ -284,34 +415,42 @@ export class ComposioClientLive extends Effect.Service<ComposioClientLive>()(
415
*/
416
list: (params?: { limit?: number }) =>
417
params?.limit
287
- ? callClientWithPagination(
288
- clientSingleton,
289
- (client, cursor) => client.toolkits.list({ cursor }),
290
- ToolkitsResponse,
291
- params.limit
418
+ ? withMetrics(
419
+ callClientWithPagination(
420
+ clientSingleton,
421
+ (client, cursor) => client.toolkits.list({ cursor }),
422
+ ToolkitsResponse,
423
+ params.limit
424
+ )
425
)
293
- : callClient(clientSingleton, client => client.toolkits.list(), ToolkitsResponse),
426
+ : withMetrics(
427
+ callClient(clientSingleton, client => client.toolkits.list(), ToolkitsResponse)
428
+ ),
429
},
430
tools: {
431
/**
432
* Retrieve a list of all available tool enumeration values (tool slugs) for the project.
433
*/
434
retrieveEnum: () =>
300
- callClient(
301
- clientSingleton,
302
- client => client.tools.retrieveEnum(),
303
- ToolsAsEnumsResponse
435
+ withMetrics(
436
+ callClient(
437
+ clientSingleton,
438
+ client => client.tools.retrieveEnum(),
439
+ ToolsAsEnumsResponse
440
+ )
441
),
442
/**
443
* Retrieve a list of tools, automatically handling pagination to fetch up to $limit items.
444
*/
445
list: (params: { limit: number }) =>
309
- callClientWithPagination(
310
- clientSingleton,
311
- (client, cursor) =>
312
- client.tools.list({ cursor, limit: params.limit, toolkit_versions: 'latest' }),
313
- ToolsResponse,
314
- params.limit
446
+ withMetrics(
447
+ callClientWithPagination(
448
+ clientSingleton,
449
+ (client, cursor) =>
450
+ client.tools.list({ cursor, limit: params.limit, toolkit_versions: 'latest' }),
451
+ ToolsResponse,
452
+ params.limit
453
+ )
454
),
455
},
456
triggersTypes: {
@@ -319,20 +458,24 @@ export class ComposioClientLive extends Effect.Service<ComposioClientLive>()(
458
* Retrieves a list of all available trigger type enum values that can be used across the API.
459
*/
460
retrieveEnum: () =>
322
- callClient(
323
- clientSingleton,
324
- client => client.triggersTypes.retrieveEnum(),
325
- TriggerTypesAsEnumsResponse
461
+ withMetrics(
462
+ callClient(
463
+ clientSingleton,
464
+ client => client.triggersTypes.retrieveEnum(),
465
+ TriggerTypesAsEnumsResponse
466
+ )
467
),
468
/**
469
* Retrieve a list of trigger types, automatically handling pagination to fetch up to $limit items.
470
*/
471
list: (params: { limit: number }) =>
331
- callClientWithPagination(
332
- clientSingleton,
333
- (client, cursor) => client.triggersTypes.list({ cursor, limit: params.limit }),
334
- TriggerTypesResponse,
335
- params.limit
472
+ withMetrics(
473
+ callClientWithPagination(
474
+ clientSingleton,
475
+ (client, cursor) => client.triggersTypes.list({ cursor, limit: params.limit }),
476
+ TriggerTypesResponse,
477
+ params.limit
478
+ )
479
),
480
},
481
cli: {
@@ -340,20 +483,24 @@ export class ComposioClientLive extends Effect.Service<ComposioClientLive>()(
483
* Generates a new CLI session with a random 6-character code.
484
*/
485
createSession: () =>
343
- callClient(
344
- clientSingleton,
345
- client => client.cli.createSession(),
346
- CliCreateSessionResponse
486
+ withMetrics(
487
+ callClient(
488
+ clientSingleton,
489
+ client => client.cli.createSession(),
490
+ CliCreateSessionResponse
491
+ )
492
),
493
494
/**
495
* Retrieves the current state of a CLI session using either the session ID (UUID) or the 6-character code.
496
*/
497
getSession: (session: { id: string }) =>
353
- callClient(
354
- clientSingleton,
355
- client => client.cli.getSession(session),
356
- CliGetSessionResponse
498
+ withMetrics(
499
+ callClient(
500
+ clientSingleton,
501
+ client => client.cli.getSession(session),
502
+ CliGetSessionResponse
503
+ )
504
),
505
},
506
};
@@ -383,6 +530,7 @@ export class ComposioToolkitsRepository extends Effect.Service<ComposioToolkitsR
530
531
return {
532
getToolkits,
533
+ getMetrics: () => client.getMetrics(),
534
getToolsAsEnums: () => client.tools.retrieveEnum(),
535
getTools: (limit: number) =>
536
client.tools.list({ limit }).pipe(
ts/packages/cli/test/__utils__/services/test-layer.ts
+1
-1
@@ -92,6 +92,7 @@ export const TestLayer = (input?: TestLiveInput) =>
92
ComposioToolkitsRepository,
93
new ComposioToolkitsRepository({
94
getToolkits: () => Effect.succeed(toolkitsData.toolkits),
95
+ getMetrics: () => Effect.succeed({ byteSize: 0, requests: 0 }),
96
getToolsAsEnums: () => Effect.succeed(toolkitsData.tools.map(tool => tool.slug)),
97
getTriggerTypesAsEnums: () => Effect.succeed(toolkitsData.triggerTypesAsEnums),
98
getTriggerTypes: limit => Effect.succeed(toolkitsData.triggerTypes.slice(0, limit)),
@@ -120,7 +121,6 @@ export const TestLayer = (input?: TestLiveInput) =>
121
},
122
})
123
);
123
-
124
const ComposioSessionRepositoryTest = yield* setupComposioSessionRepository();
125
126
// Mock `node:os`