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 101 lines 3.2 KB
Raw
1 /**
2 * @fileoverview The `composio.logs` namespace: search and inspect
3 * tool-execution logs for the project.
4 *
5 * @module Logs
6 */
7 import ComposioClient from '@composio/client';
8 import {
9 LogSearchParams,
10 LogSearchParamsSchema,
11 LogSearchResponse,
12 ToolExecutionLogDetail,
13 } from '../types/logs.types';
14 import { ComposioRequestOptions } from '../types/requestOptions.types';
15 import { ValidationError } from '../errors/ValidationErrors';
16 import { telemetry } from '../telemetry/Telemetry';
17 import { withCancellation } from '../utils/cancellation';
18 import {
19 transformLogSearchParams,
20 transformLogSearchResponse,
21 transformToolExecutionLogDetail,
22 } from '../utils/transformers/logs';
23
24 /**
25 * `composio.logs` — tool-execution logs.
26 *
27 * @example
28 * ```typescript
29 * const { logs, nextCursor } = await composio.logs.search({
30 * filters: [{ field: 'toolkit_slug', operator: '==', value: 'github' }],
31 * limit: 20,
32 * });
33 * const detail = await composio.logs.get(logs[0].id);
34 * ```
35 */
36 export class Logs {
37 private client: ComposioClient;
38
39 constructor(client: ComposioClient) {
40 this.client = client;
41 telemetry.instrument(this, 'Logs');
42 }
43
44 /**
45 * Search tool-execution logs with filters, a time range and cursor
46 * pagination.
47 *
48 * @param {LogSearchParams} [params] - Filters, `timeRange`, `limit` and `cursor`
49 * @returns {Promise<LogSearchResponse>} Matching logs and the next cursor. Charged calls include `metadata.instant_charge` as an exact USD string.
50 * @throws {ValidationError} If the params fail validation
51 *
52 * @example
53 * ```typescript
54 * const { logs } = await composio.logs.search({
55 * filters: [
56 * { field: 'status', operator: '==', value: 'failed' },
57 * { field: 'user_id', operator: '==', value: 'user_123' },
58 * ],
59 * timeRange: { from: Date.now() - 24 * 60 * 60 * 1000, to: Date.now() },
60 * });
61 * ```
62 */
63 async search(
64 params?: LogSearchParams,
65 requestOptions?: ComposioRequestOptions
66 ): Promise<LogSearchResponse> {
67 const parsedParams = LogSearchParamsSchema.safeParse(params ?? {});
68 if (!parsedParams.success) {
69 throw new ValidationError('Failed to parse log search params', {
70 cause: parsedParams.error,
71 });
72 }
73 const body = transformLogSearchParams(parsedParams.data);
74 const result = await withCancellation(
75 () => this.client.logs.createToolExecution(body, requestOptions),
76 requestOptions?.signal
77 );
78 return transformLogSearchResponse(result);
79 }
80
81 /**
82 * Retrieve one tool-execution log with its full detail (timings, context,
83 * source and raw `data`).
84 *
85 * @param {string} id - The log id
86 * @returns {Promise<ToolExecutionLogDetail>} The log detail, with an optional exact USD charge in `metadata.instant_charge`
87 *
88 * @example
89 * ```typescript
90 * const log = await composio.logs.get('log_abc123');
91 * console.log(log.status, log.timings?.endTime);
92 * ```
93 */
94 async get(id: string, requestOptions?: ComposioRequestOptions): Promise<ToolExecutionLogDetail> {
95 const result = await withCancellation(
96 () => this.client.logs.retrieveToolExecution(id, requestOptions),
97 requestOptions?.signal
98 );
99 return transformToolExecutionLogDetail(result);
100 }
101 }