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 { |
| 2 | LogCreateToolExecutionResponse, |
| 3 | LogRetrieveToolExecutionResponse, |
| 4 | } from '@composio/client/resources/logs'; |
| 5 | import { Option, Schema } from 'effect'; |
| 6 | import type { TriggerLogRecord } from 'src/commands/logs/trigger-log-record'; |
| 7 | import { bold, gray, green, red } from 'src/ui/colors'; |
| 8 | import { truncate } from 'src/ui/truncate'; |
| 9 | |
| 10 | type ToolLog = LogCreateToolExecutionResponse['logs'][number]; |
| 11 | type ToolLogDetailed = LogRetrieveToolExecutionResponse; |
| 12 | |
| 13 | const readRecord = (value: unknown): Record<string, unknown> => |
| 14 | Option.getOrElse( |
| 15 | Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.Unknown))(value), |
| 16 | () => ({}) |
| 17 | ); |
| 18 | const readString = (value: unknown): string | undefined => |
| 19 | Option.getOrUndefined(Schema.decodeUnknownOption(Schema.String)(value)); |
| 20 | const readNumber = (value: unknown): number | undefined => |
| 21 | Option.getOrUndefined(Schema.decodeUnknownOption(Schema.Number)(value)); |
| 22 | |
| 23 | const TRIGGER_LOG_TABLE = { |
| 24 | createdAt: 24, |
| 25 | id: 18, |
| 26 | triggerId: 18, |
| 27 | app: 12, |
| 28 | triggerName: 28, |
| 29 | triggerUserId: 16, |
| 30 | connectedAccountId: 20, |
| 31 | } as const; |
| 32 | |
| 33 | const TOOL_LOG_TABLE = { |
| 34 | createdAt: 24, |
| 35 | id: 18, |
| 36 | status: 8, |
| 37 | app: 12, |
| 38 | actionKey: 28, |
| 39 | executionMs: 12, |
| 40 | connectedAccountId: 20, |
| 41 | } as const; |
| 42 | |
| 43 | export const formatTriggerLogsTable = (logs: ReadonlyArray<TriggerLogRecord>): string => { |
| 44 | const header = [ |
| 45 | bold('Created At'.padEnd(TRIGGER_LOG_TABLE.createdAt)), |
| 46 | bold('Log Id'.padEnd(TRIGGER_LOG_TABLE.id)), |
| 47 | bold('Trigger Id'.padEnd(TRIGGER_LOG_TABLE.triggerId)), |
| 48 | bold('Toolkit'.padEnd(TRIGGER_LOG_TABLE.app)), |
| 49 | bold('Trigger'.padEnd(TRIGGER_LOG_TABLE.triggerName)), |
| 50 | bold('User Id'.padEnd(TRIGGER_LOG_TABLE.triggerUserId)), |
| 51 | bold('Connected Account Id'), |
| 52 | ].join(' '); |
| 53 | |
| 54 | const rows = logs.map(log => { |
| 55 | const createdAt = gray( |
| 56 | truncate(formatCreatedAt(log.createdAt), TRIGGER_LOG_TABLE.createdAt).padEnd( |
| 57 | TRIGGER_LOG_TABLE.createdAt |
| 58 | ) |
| 59 | ); |
| 60 | const id = (log.id ?? '-').padEnd(TRIGGER_LOG_TABLE.id); |
| 61 | const triggerId = truncate( |
| 62 | valueOrDash(log.table.triggerId), |
| 63 | TRIGGER_LOG_TABLE.triggerId |
| 64 | ).padEnd(TRIGGER_LOG_TABLE.triggerId); |
| 65 | const app = truncate(log.appName ?? '-', TRIGGER_LOG_TABLE.app).padEnd(TRIGGER_LOG_TABLE.app); |
| 66 | const triggerName = truncate( |
| 67 | valueOrDash(log.table.triggerName), |
| 68 | TRIGGER_LOG_TABLE.triggerName |
| 69 | ).padEnd(TRIGGER_LOG_TABLE.triggerName); |
| 70 | const triggerUserId = truncate(log.entityId ?? '-', TRIGGER_LOG_TABLE.triggerUserId).padEnd( |
| 71 | TRIGGER_LOG_TABLE.triggerUserId |
| 72 | ); |
| 73 | const connectedAccountId = truncate( |
| 74 | log.connectionId ?? '-', |
| 75 | TRIGGER_LOG_TABLE.connectedAccountId |
| 76 | ); |
| 77 | |
| 78 | return [createdAt, id, triggerId, app, triggerName, triggerUserId, connectedAccountId].join( |
| 79 | ' ' |
| 80 | ); |
| 81 | }); |
| 82 | |
| 83 | return [header, ...rows].join('\n'); |
| 84 | }; |
| 85 | |
| 86 | export const formatToolLogsTable = (logs: ReadonlyArray<ToolLog>): string => { |
| 87 | const header = [ |
| 88 | bold('Created At'.padEnd(TOOL_LOG_TABLE.createdAt)), |
| 89 | bold('Log Id'.padEnd(TOOL_LOG_TABLE.id)), |
| 90 | bold('Status'.padEnd(TOOL_LOG_TABLE.status)), |
| 91 | bold('Toolkit'.padEnd(TOOL_LOG_TABLE.app)), |
| 92 | bold('Tool'.padEnd(TOOL_LOG_TABLE.actionKey)), |
| 93 | bold('Exec Time'.padEnd(TOOL_LOG_TABLE.executionMs)), |
| 94 | bold('Connected Account'), |
| 95 | ].join(' '); |
| 96 | |
| 97 | const rows = logs.map(log => { |
| 98 | const metadata = readRecord(log.metadata); |
| 99 | const metrics = readRecord(log.metrics); |
| 100 | const createdAt = gray( |
| 101 | truncate(formatCreatedAt(log.timestamp), TOOL_LOG_TABLE.createdAt).padEnd( |
| 102 | TOOL_LOG_TABLE.createdAt |
| 103 | ) |
| 104 | ); |
| 105 | // Keep log ids untruncated for copy/paste workflows. |
| 106 | const id = (log.id ?? '-').padEnd(TOOL_LOG_TABLE.id); |
| 107 | const status = formatLogStatus(log.status, TOOL_LOG_TABLE.status); |
| 108 | const app = truncate( |
| 109 | readString(readRecord(metadata.toolkit).slug) ?? '-', |
| 110 | TOOL_LOG_TABLE.app |
| 111 | ).padEnd(TOOL_LOG_TABLE.app); |
| 112 | const actionKey = truncate( |
| 113 | readString(readRecord(metadata.tool).slug) ?? '-', |
| 114 | TOOL_LOG_TABLE.actionKey |
| 115 | ).padEnd(TOOL_LOG_TABLE.actionKey); |
| 116 | const executionMs = formatDuration(metrics.duration_ms).padEnd(TOOL_LOG_TABLE.executionMs); |
| 117 | const connectedAccountId = truncate( |
| 118 | valueOrDash(readString(metadata.connected_account_id)), |
| 119 | TOOL_LOG_TABLE.connectedAccountId |
| 120 | ); |
| 121 | |
| 122 | return [createdAt, id, status, app, actionKey, executionMs, connectedAccountId].join(' '); |
| 123 | }); |
| 124 | |
| 125 | return [header, ...rows].join('\n'); |
| 126 | }; |
| 127 | |
| 128 | const formatLogStatus = (status: string | null | undefined, width: number): string => { |
| 129 | const label = truncate(valueOrDash(status), width).padEnd(width); |
| 130 | const normalizedStatus = status?.trim().toLowerCase(); |
| 131 | |
| 132 | if (normalizedStatus === 'success') return green(label); |
| 133 | if (normalizedStatus === 'failure' || normalizedStatus === 'failed') return red(label); |
| 134 | return label; |
| 135 | }; |
| 136 | |
| 137 | const valueOrDash = (value: string | null | undefined): string => { |
| 138 | if (value == null) return '-'; |
| 139 | const normalizedValue = value.trim(); |
| 140 | return normalizedValue.length > 0 ? normalizedValue : '-'; |
| 141 | }; |
| 142 | |
| 143 | const formatDuration = (value: unknown): string => { |
| 144 | const durationMs = readNumber(value); |
| 145 | return durationMs === undefined ? '-' : `${durationMs}ms`; |
| 146 | }; |
| 147 | |
| 148 | const formatEpoch = (value: number): string => { |
| 149 | const date = new Date(value); |
| 150 | if (Number.isNaN(date.getTime())) return '-'; |
| 151 | return date.toISOString(); |
| 152 | }; |
| 153 | |
| 154 | const formatCreatedAt = (value: number | string | null | undefined): string => { |
| 155 | if (typeof value === 'number') return formatEpoch(value); |
| 156 | if (typeof value === 'string') return value; |
| 157 | return '-'; |
| 158 | }; |
| 159 | |
| 160 | export const formatToolLogInfo = (toolLog: ToolLogDetailed): string => { |
| 161 | const metadata = readRecord(toolLog.metadata); |
| 162 | const tool = readRecord(metadata.tool); |
| 163 | const data = readRecord(toolLog.data); |
| 164 | return [ |
| 165 | `${bold('logId:')} ${toolLog.id}`, |
| 166 | `${bold('toolSlug:')} ${valueOrDash(readString(tool.slug))}`, |
| 167 | `${bold('Status:')} ${valueOrDash(toolLog.status)}`, |
| 168 | `${bold('toolkit:')} ${valueOrDash(readString(readRecord(metadata.toolkit).slug))}`, |
| 169 | `${bold('Connection ID:')} ${valueOrDash(readString(metadata.connected_account_id))}`, |
| 170 | `${bold('Entity:')} ${valueOrDash(readString(metadata.user_id))}`, |
| 171 | `${bold('Start Time:')} ${formatCreatedAt(toolLog.timings?.start_time ?? toolLog.timestamp)}`, |
| 172 | `${bold('End Time:')} ${formatCreatedAt(toolLog.timings?.end_time)}`, |
| 173 | `${bold('Total Duration:')} ${formatDuration(readRecord(toolLog.metrics).duration_ms)}`, |
| 174 | `${bold('Version:')} ${valueOrDash(readString(tool.version))}`, |
| 175 | `\nPayload:\n${JSON.stringify(readRecord(data.request).payload ?? {}, null, 2)}`, |
| 176 | `\nResponse:\n${JSON.stringify(readRecord(data.response).body ?? {}, null, 2)}`, |
| 177 | ].join('\n'); |
| 178 | }; |
| 179 | |
| 180 | export const formatTriggerLogInfo = (triggerLog: TriggerLogRecord): string => { |
| 181 | const { detail } = triggerLog; |
| 182 | const lines: string[] = []; |
| 183 | |
| 184 | lines.push(`${bold('logId:')} ${valueOrDash(detail.logId)}`); |
| 185 | lines.push(`${bold('triggerId:')} ${valueOrDash(detail.triggerId)}`); |
| 186 | lines.push(`${bold('triggerNanoId:')} ${valueOrDash(detail.triggerNanoId)}`); |
| 187 | lines.push(`${bold('trigger:')} ${valueOrDash(detail.triggerName)}`); |
| 188 | lines.push(`${bold('Status:')} ${valueOrDash(detail.status)}`); |
| 189 | lines.push(`${bold('toolkit:')} ${valueOrDash(detail.toolkit)}`); |
| 190 | lines.push(`${bold('Connection ID:')} ${valueOrDash(detail.connectionId)}`); |
| 191 | lines.push(`${bold('Entity:')} ${valueOrDash(detail.entityId)}`); |
| 192 | lines.push(`${bold('Created At:')} ${formatCreatedAt(detail.createdAt)}`); |
| 193 | |
| 194 | return lines.join('\n'); |
| 195 | }; |