Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import type { TriggerListParams } from '@composio/client/resources/_internal/logs'; |
| 2 | import { Array as Arr, Effect, Option, String as Str } from 'effect'; |
| 3 | import { CommandSurface, developerProjectClient } from 'src/commands/surface'; |
| 4 | import { parseCsv } from 'src/commands/triggers/parse-csv'; |
| 5 | import { failedWith } from 'src/effects/request-error'; |
| 6 | import { commandHintStep } from 'src/services/command-hints'; |
| 7 | import { idempotent } from 'src/services/composio-request'; |
| 8 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 9 | import { clampLimit } from 'src/ui/clamp-limit'; |
| 10 | import { nonEmptyOrUndefined, optionalParams } from 'src/utils/optional-params'; |
| 11 | import { formatTriggerLogInfo, formatTriggerLogsTable } from '../format'; |
| 12 | import { decodeTriggerLogRecord } from '../trigger-log-record'; |
| 13 | import { toSearchParam, type SearchParam } from '../utils'; |
| 14 | import { TriggerLogsRequestError } from './logs.triggers.errors'; |
| 15 | |
| 16 | export type LogsTriggersParams = { |
| 17 | readonly logId: Option.Option<string>; |
| 18 | readonly cursor: Option.Option<string>; |
| 19 | /** Read only when the `dev` group's shared `--user-id` is absent. */ |
| 20 | readonly userId: Option.Option<string>; |
| 21 | readonly connectedAccountId: Option.Option<string>; |
| 22 | readonly trigger: Option.Option<string>; |
| 23 | readonly triggerId: Option.Option<string>; |
| 24 | readonly logIdFilter: Option.Option<string>; |
| 25 | readonly from: Option.Option<number>; |
| 26 | readonly to: Option.Option<number>; |
| 27 | readonly limit: number; |
| 28 | readonly time: Option.Option<NonNullable<TriggerListParams['time']>>; |
| 29 | readonly search: Option.Option<string>; |
| 30 | readonly includePayload: boolean; |
| 31 | }; |
| 32 | |
| 33 | /** |
| 34 | * One `==` search param per comma-separated value of each present filter, in |
| 35 | * the order the filters are given. |
| 36 | */ |
| 37 | export const triggerLogSearchParams = ( |
| 38 | filters: ReadonlyArray<readonly [field: string, filter: Option.Option<string>]> |
| 39 | ): Array<SearchParam> => |
| 40 | Arr.flatMap(filters, ([field, filter]) => |
| 41 | Option.match(filter, { |
| 42 | onNone: () => [], |
| 43 | onSome: csv => parseCsv(csv).map(value => toSearchParam(field, value)), |
| 44 | }) |
| 45 | ); |
| 46 | |
| 47 | const showTriggerLog = (triggerLogId: string) => |
| 48 | Effect.gen(function* () { |
| 49 | const ui = yield* TerminalUI; |
| 50 | const client = yield* developerProjectClient; |
| 51 | const triggerLog = yield* ui.withSpinner( |
| 52 | `Fetching trigger log "${triggerLogId}"...`, |
| 53 | client._internal.logs.triggers |
| 54 | .retrieve(triggerLogId, idempotent) |
| 55 | .pipe(failedWith(TriggerLogsRequestError, `Failed to fetch trigger log "${triggerLogId}".`)) |
| 56 | ); |
| 57 | const record = decodeTriggerLogRecord(triggerLog); |
| 58 | |
| 59 | yield* ui.log.info( |
| 60 | `${formatTriggerLogInfo(record)}\n\nPayload:\n${JSON.stringify(record.payload, null, 2)}\n\nResponse:\n${JSON.stringify(record.response, null, 2)}` |
| 61 | ); |
| 62 | // Raw API payload for scripts; only human-readable formatting uses the decoded record. |
| 63 | yield* ui.output(JSON.stringify(triggerLog, null, 2)); |
| 64 | }); |
| 65 | |
| 66 | const listTriggerLogs = ({ |
| 67 | cursor, |
| 68 | userId, |
| 69 | connectedAccountId, |
| 70 | trigger, |
| 71 | triggerId, |
| 72 | logIdFilter, |
| 73 | from, |
| 74 | to, |
| 75 | limit, |
| 76 | time, |
| 77 | search, |
| 78 | includePayload, |
| 79 | }: LogsTriggersParams) => |
| 80 | Effect.gen(function* () { |
| 81 | const userIdFilter = Option.orElse((yield* CommandSurface).userId, () => userId); |
| 82 | const ui = yield* TerminalUI; |
| 83 | const client = yield* developerProjectClient; |
| 84 | const searchParams = triggerLogSearchParams([ |
| 85 | ['trigger_name', trigger], |
| 86 | ['trigger_id', triggerId], |
| 87 | ['user_id', userIdFilter], |
| 88 | ['connected_account_id', connectedAccountId], |
| 89 | ['log_id', logIdFilter], |
| 90 | ]); |
| 91 | |
| 92 | const response = yield* ui.withSpinner( |
| 93 | 'Fetching trigger logs...', |
| 94 | client._internal.logs.triggers |
| 95 | .list( |
| 96 | { |
| 97 | ...optionalParams({ cursor, from, to, time, search }), |
| 98 | limit: clampLimit(limit), |
| 99 | include_payload: includePayload, |
| 100 | search_params: nonEmptyOrUndefined(searchParams), |
| 101 | }, |
| 102 | idempotent |
| 103 | ) |
| 104 | .pipe(failedWith(TriggerLogsRequestError, 'Failed to fetch trigger logs.')) |
| 105 | ); |
| 106 | |
| 107 | yield* Arr.match(response.data ?? [], { |
| 108 | onEmpty: () => ui.log.warn('No trigger logs found for the given filters.'), |
| 109 | onNonEmpty: logs => |
| 110 | Effect.gen(function* () { |
| 111 | yield* ui.log.info( |
| 112 | `Listing ${logs.length} trigger log${logs.length === 1 ? '' : 's'}\n\n${formatTriggerLogsTable(logs.map(decodeTriggerLogRecord))}` |
| 113 | ); |
| 114 | |
| 115 | const firstLogId = Arr.headNonEmpty(logs).id; |
| 116 | if (firstLogId) { |
| 117 | yield* ui.log.step( |
| 118 | commandHintStep('To view full details for a log', 'dev.logs.triggers', { |
| 119 | logId: firstLogId, |
| 120 | }) |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | if (response.nextCursor) { |
| 125 | yield* ui.log.step(`Next cursor: ${response.nextCursor}`); |
| 126 | } |
| 127 | }), |
| 128 | }); |
| 129 | |
| 130 | yield* ui.output(JSON.stringify(response, null, 2)); |
| 131 | }); |
| 132 | |
| 133 | /** |
| 134 | * Show one trigger log when a log id is given, otherwise list trigger logs |
| 135 | * with optional filters. |
| 136 | * |
| 137 | * @example |
| 138 | * ```bash |
| 139 | * composio dev logs triggers <log_id> |
| 140 | * composio dev logs triggers --trigger GMAIL_NEW_GMAIL_MESSAGE |
| 141 | * composio dev logs triggers --trigger-id 77ac1dbf-6db0-4039-8dbe-e903b3f2057e |
| 142 | * composio dev logs triggers --connected-account-id ca_123 --user-id user_123 |
| 143 | * composio dev logs triggers --log-id log_123 |
| 144 | * ``` |
| 145 | */ |
| 146 | export const runLogsTriggers = (params: LogsTriggersParams) => |
| 147 | Effect.gen(function* () { |
| 148 | return yield* Option.match(Option.filter(params.logId, Str.isNonEmpty), { |
| 149 | onSome: showTriggerLog, |
| 150 | onNone: () => listTriggerLogs(params), |
| 151 | }); |
| 152 | }); |