Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import type { LogCreateToolExecutionParams } from '@composio/client/resources/logs'; |
| 2 | import { Array as Arr, Clock, Effect, Option, String as Str } from 'effect'; |
| 3 | import { commandInputError } from 'src/commands/errors'; |
| 4 | import { CommandSurface, developerProjectClient } from 'src/commands/surface'; |
| 5 | import { parseCsv } from 'src/commands/triggers/parse-csv'; |
| 6 | import { failedWith } from 'src/effects/request-error'; |
| 7 | import { commandHintStep } from 'src/services/command-hints'; |
| 8 | import { idempotent } from 'src/services/composio-request'; |
| 9 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 10 | import { nonEmptyOrUndefined, optionalParams } from 'src/utils/optional-params'; |
| 11 | import { formatToolLogInfo, formatToolLogsTable } from '../format'; |
| 12 | import { ToolLogsRequestError } from './logs.tools.errors'; |
| 13 | |
| 14 | export type LogsToolsParams = { |
| 15 | readonly logId: Option.Option<string>; |
| 16 | readonly cursor: Option.Option<string>; |
| 17 | readonly from: Option.Option<number>; |
| 18 | readonly to: Option.Option<number>; |
| 19 | readonly limit: number; |
| 20 | readonly caseSensitive: boolean; |
| 21 | readonly toolkit: Option.Option<string>; |
| 22 | readonly tool: Option.Option<string>; |
| 23 | readonly connectedAccountId: Option.Option<string>; |
| 24 | readonly authConfigId: Option.Option<string>; |
| 25 | readonly status: Option.Option<string>; |
| 26 | /** Read only when the `dev` group's shared `--user-id` is absent. */ |
| 27 | readonly userId: Option.Option<string>; |
| 28 | readonly logIdFilter: Option.Option<string>; |
| 29 | readonly toolRouterSessionId: Option.Option<string>; |
| 30 | readonly sessionId: Option.Option<string>; |
| 31 | }; |
| 32 | |
| 33 | type ToolLogFilterInput = { |
| 34 | readonly tool?: string; |
| 35 | readonly toolkit?: string; |
| 36 | readonly connectedAccountId?: string; |
| 37 | readonly authConfigId?: string; |
| 38 | readonly status?: string; |
| 39 | readonly userId?: string; |
| 40 | readonly logId?: string; |
| 41 | readonly toolRouterSessionId?: string; |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * The search field each filter is sent as and the flag that sets it, in the |
| 46 | * order the filters are sent. |
| 47 | */ |
| 48 | const TOOL_LOG_FILTER_FIELDS = [ |
| 49 | ['tool', 'tool_slug', '--tool'], |
| 50 | ['toolkit', 'toolkit_slug', '--toolkit'], |
| 51 | ['connectedAccountId', 'connected_account_id', '--connected-account-id'], |
| 52 | ['authConfigId', 'auth_config_id', '--auth-config-id'], |
| 53 | ['status', 'status', '--status'], |
| 54 | ['userId', 'user_id', '--user-id'], |
| 55 | ['logId', 'log_id', '--log-id'], |
| 56 | ['toolRouterSessionId', 'session_id', '--tool-router-session-id'], |
| 57 | ] as const satisfies ReadonlyArray<readonly [keyof ToolLogFilterInput, string, string]>; |
| 58 | |
| 59 | /** |
| 60 | * The flag of the first filter given more than one comma-separated value. The |
| 61 | * public API combines filters with AND, so several values for one field would |
| 62 | * match nothing. |
| 63 | */ |
| 64 | const findMultiValuedFilter = (filters: ToolLogFilterInput): Option.Option<string> => |
| 65 | Arr.findFirst(TOOL_LOG_FILTER_FIELDS, ([key]) => parseCsv(filters[key] ?? '').length > 1).pipe( |
| 66 | Option.map(([, , flag]) => flag) |
| 67 | ); |
| 68 | |
| 69 | /** One equality filter for each present flag, in the order of `TOOL_LOG_FILTER_FIELDS`. */ |
| 70 | const buildToolLogFilters = ( |
| 71 | filters: ToolLogFilterInput |
| 72 | ): NonNullable<LogCreateToolExecutionParams['filters']> => |
| 73 | Arr.flatMap(TOOL_LOG_FILTER_FIELDS, ([key, field]) => |
| 74 | parseCsv(filters[key] ?? '').map(value => ({ field, operator: '==' as const, value })) |
| 75 | ); |
| 76 | |
| 77 | const showToolLog = (toolLogId: string) => |
| 78 | Effect.gen(function* () { |
| 79 | const ui = yield* TerminalUI; |
| 80 | const client = yield* developerProjectClient; |
| 81 | const toolLog = yield* ui.withSpinner( |
| 82 | `Fetching tool log "${toolLogId}"...`, |
| 83 | client.logs |
| 84 | .retrieveToolExecution(toolLogId, idempotent) |
| 85 | .pipe(failedWith(ToolLogsRequestError, `Failed to fetch tool log "${toolLogId}".`)) |
| 86 | ); |
| 87 | |
| 88 | yield* ui.log.info(formatToolLogInfo(toolLog)); |
| 89 | yield* ui.output(JSON.stringify(toolLog, null, 2)); |
| 90 | }); |
| 91 | |
| 92 | const listToolLogs = ({ |
| 93 | cursor, |
| 94 | from, |
| 95 | to, |
| 96 | limit, |
| 97 | caseSensitive, |
| 98 | toolkit, |
| 99 | tool, |
| 100 | connectedAccountId, |
| 101 | authConfigId, |
| 102 | status, |
| 103 | userId, |
| 104 | logIdFilter, |
| 105 | toolRouterSessionId, |
| 106 | sessionId, |
| 107 | }: LogsToolsParams) => |
| 108 | Effect.gen(function* () { |
| 109 | const userIdFilter = Option.orElse((yield* CommandSurface).userId, () => userId); |
| 110 | const ui = yield* TerminalUI; |
| 111 | const now = yield* Clock.currentTimeMillis; |
| 112 | const invalidInput = commandInputError('dev logs tools'); |
| 113 | if (caseSensitive) { |
| 114 | return yield* invalidInput( |
| 115 | 'The public tool logs API does not support --case-sensitive. Remove this flag.', |
| 116 | '--case-sensitive' |
| 117 | ); |
| 118 | } |
| 119 | if (Option.isSome(sessionId)) { |
| 120 | return yield* invalidInput( |
| 121 | 'The public tool logs API does not support the legacy --session-id filter. Use --tool-router-session-id for a Tool Router session.', |
| 122 | '--session-id' |
| 123 | ); |
| 124 | } |
| 125 | const filterInput = optionalParams({ |
| 126 | tool, |
| 127 | toolkit, |
| 128 | connectedAccountId, |
| 129 | authConfigId, |
| 130 | status, |
| 131 | userId: userIdFilter, |
| 132 | logId: logIdFilter, |
| 133 | toolRouterSessionId, |
| 134 | }); |
| 135 | const multiValuedFlag = findMultiValuedFilter(filterInput); |
| 136 | if (Option.isSome(multiValuedFlag)) { |
| 137 | return yield* invalidInput( |
| 138 | `The public tool logs API matches one value per filter. Pass a single value to ${multiValuedFlag.value}.`, |
| 139 | multiValuedFlag.value |
| 140 | ); |
| 141 | } |
| 142 | if ( |
| 143 | Option.isSome(status) && |
| 144 | parseCsv(status.value).some(value => value !== 'success' && value !== 'failed') |
| 145 | ) { |
| 146 | return yield* invalidInput('Use success or failed for --status.', '--status'); |
| 147 | } |
| 148 | const timeRange = |
| 149 | Option.isSome(from) || Option.isSome(to) |
| 150 | ? { from: Option.getOrElse(from, () => 0), to: Option.getOrElse(to, () => now) } |
| 151 | : undefined; |
| 152 | const filters = buildToolLogFilters(filterInput); |
| 153 | |
| 154 | const client = yield* developerProjectClient; |
| 155 | // The client names this read-only search POST createToolExecution. |
| 156 | const response = yield* ui.withSpinner( |
| 157 | 'Fetching tool logs...', |
| 158 | client.logs |
| 159 | .createToolExecution( |
| 160 | { |
| 161 | time_range: timeRange, |
| 162 | cursor: Option.getOrNull(cursor), |
| 163 | limit: Math.max(1, Math.min(100, limit)), |
| 164 | filters: nonEmptyOrUndefined(filters), |
| 165 | }, |
| 166 | idempotent |
| 167 | ) |
| 168 | .pipe(failedWith(ToolLogsRequestError, 'Failed to fetch tool logs.')) |
| 169 | ); |
| 170 | |
| 171 | yield* Arr.match(response.logs ?? [], { |
| 172 | onEmpty: () => ui.log.warn('No tool logs found for the given filters.'), |
| 173 | onNonEmpty: logs => |
| 174 | Effect.gen(function* () { |
| 175 | yield* ui.log.info( |
| 176 | `Listing ${logs.length} tool log${logs.length === 1 ? '' : 's'}\n\n${formatToolLogsTable(logs)}` |
| 177 | ); |
| 178 | |
| 179 | const firstLogId = Arr.headNonEmpty(logs).id; |
| 180 | if (firstLogId) { |
| 181 | yield* ui.log.step( |
| 182 | commandHintStep('To view full details for a log', 'dev.logs.tools', { |
| 183 | logId: firstLogId, |
| 184 | }) |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | if (response.next_cursor !== null && response.next_cursor !== undefined) { |
| 189 | yield* ui.log.step(`Next cursor: ${response.next_cursor}`); |
| 190 | } |
| 191 | }), |
| 192 | }); |
| 193 | |
| 194 | yield* ui.output(JSON.stringify(response, null, 2)); |
| 195 | }); |
| 196 | |
| 197 | /** |
| 198 | * Show one tool log when a log id is given, otherwise list tool execution logs |
| 199 | * with optional filters. |
| 200 | * |
| 201 | * @example |
| 202 | * ```bash |
| 203 | * composio dev logs tools --limit 50 |
| 204 | * composio dev logs tools <log_id> |
| 205 | * composio dev logs tools --toolkit gmail --tool GMAIL_SEND_EMAIL |
| 206 | * composio dev logs tools --connected-account-id con_123 --user-id user_123 |
| 207 | * composio dev logs tools --status success --auth-config-id ac_123 |
| 208 | * composio dev logs tools --from 1735689600000 --to 1735776000000 |
| 209 | * ``` |
| 210 | */ |
| 211 | export const runLogsTools = (params: LogsToolsParams) => |
| 212 | Option.match(Option.filter(params.logId, Str.isNonEmpty), { |
| 213 | onSome: showToolLog, |
| 214 | onNone: () => listToolLogs(params), |
| 215 | }); |