@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 236 lines 8.03 KB
Raw
1 import type { TriggerType } from 'src/models/trigger-types';
2 import type { TriggerListenEvent } from './types';
3 import type { TriggerInstanceItem } from 'src/models/triggers';
4 import { bold, gray } from 'src/ui/colors';
5 import { escapeTerminalText } from 'src/ui/escape-terminal-text';
6 import { truncate } from 'src/ui/truncate';
7 import { extractSchemaProperties } from 'src/ui/extract-schema-properties';
8
9 /** One line per field; event values are escaped so each stays on its own line. */
10 export const formatTriggerListenSummary = (event: TriggerListenEvent): string => {
11 const userId = event.userId || '-';
12 const connectedAccountId = event.metadata.connectedAccount.id || '-';
13 return [
14 `Trigger: ${escapeTerminalText(event.triggerSlug)}`,
15 `Toolkit: ${escapeTerminalText(event.toolkitSlug)}`,
16 `Trigger Id: ${escapeTerminalText(event.id)}`,
17 `Connected Account: ${escapeTerminalText(connectedAccountId)}`,
18 `User Id: ${escapeTerminalText(userId)}`,
19 ].join('\n');
20 };
21
22 const TRIGGER_TABLE = {
23 timestamp: 24,
24 triggerId: 20,
25 triggerSlug: 22,
26 toolkit: 8,
27 userId: 16,
28 connectedAccountId: 20,
29 } as const;
30
31 export const formatTriggerListenTableHeader = (): string =>
32 [
33 bold('Timestamp'.padEnd(TRIGGER_TABLE.timestamp)),
34 bold('Trigger Id'.padEnd(TRIGGER_TABLE.triggerId)),
35 bold('Trigger Slug'.padEnd(TRIGGER_TABLE.triggerSlug)),
36 bold('Toolkit'.padEnd(TRIGGER_TABLE.toolkit)),
37 bold('User Id'.padEnd(TRIGGER_TABLE.userId)),
38 bold('Connected Account Id'),
39 ].join(' ');
40
41 /**
42 * One table row. Each cell is escaped before it is truncated, padded and styled, so the row
43 * stays one line and its widths count what is shown.
44 */
45 export const formatTriggerListenTableRow = ({
46 timestamp,
47 event,
48 }: {
49 timestamp: string;
50 event: TriggerListenEvent;
51 }): string => {
52 const connectedAccountId = escapeTerminalText(event.metadata.connectedAccount.id || '-');
53 const userId = escapeTerminalText(event.userId || '-');
54 const timestampCell = truncate(escapeTerminalText(timestamp), TRIGGER_TABLE.timestamp).padEnd(
55 TRIGGER_TABLE.timestamp
56 );
57 const triggerIdCell = truncate(escapeTerminalText(event.id), TRIGGER_TABLE.triggerId).padEnd(
58 TRIGGER_TABLE.triggerId
59 );
60
61 return [
62 gray(timestampCell),
63 triggerIdCell,
64 truncate(escapeTerminalText(event.triggerSlug), TRIGGER_TABLE.triggerSlug).padEnd(
65 TRIGGER_TABLE.triggerSlug
66 ),
67 truncate(escapeTerminalText(event.toolkitSlug), TRIGGER_TABLE.toolkit).padEnd(
68 TRIGGER_TABLE.toolkit
69 ),
70 truncate(userId, TRIGGER_TABLE.userId).padEnd(TRIGGER_TABLE.userId),
71 truncate(connectedAccountId, TRIGGER_TABLE.connectedAccountId),
72 ].join(' ');
73 };
74
75 const TRIGGER_TYPES_TABLE = {
76 slug: 35,
77 name: 26,
78 type: 8,
79 } as const;
80
81 export const formatTriggerTypesTable = (triggerTypes: ReadonlyArray<TriggerType>): string => {
82 const header = [
83 bold('Slug'.padEnd(TRIGGER_TYPES_TABLE.slug)),
84 bold('Name'.padEnd(TRIGGER_TYPES_TABLE.name)),
85 bold('Type'.padEnd(TRIGGER_TYPES_TABLE.type)),
86 bold('Description'),
87 ].join(' ');
88
89 const rows = triggerTypes.map(triggerType => {
90 const slug = truncate(triggerType.slug, TRIGGER_TYPES_TABLE.slug).padEnd(
91 TRIGGER_TYPES_TABLE.slug
92 );
93 const name = truncate(triggerType.name, TRIGGER_TYPES_TABLE.name).padEnd(
94 TRIGGER_TYPES_TABLE.name
95 );
96 const type = truncate(triggerType.type, TRIGGER_TYPES_TABLE.type).padEnd(
97 TRIGGER_TYPES_TABLE.type
98 );
99 const description = gray(truncate(triggerType.description, 50));
100 return `${slug} ${name} ${type} ${description}`;
101 });
102
103 return [header, ...rows].join('\n');
104 };
105
106 export const formatTriggerTypesJson = (triggerTypes: ReadonlyArray<TriggerType>): string =>
107 JSON.stringify(
108 triggerTypes.map(triggerType => ({
109 slug: triggerType.slug,
110 name: triggerType.name,
111 type: triggerType.type,
112 description: triggerType.description,
113 })),
114 null,
115 2
116 );
117
118 function formatSchemaProperties(schema: Record<string, unknown>): string {
119 const entries = extractSchemaProperties(schema);
120 if (entries.length === 0) {
121 return ' (none)';
122 }
123
124 const typeWidth = Math.max(...entries.map(e => e.type.length));
125 const labelWidth = Math.max(...entries.map(e => e.label.length));
126 const metadataLabels = ['description:', 'type:', 'required:', 'default:'] as const;
127 const metadataLabelWidth = Math.max(...metadataLabels.map(label => label.length));
128
129 return entries
130 .map(e => {
131 const lines: string[] = [];
132 lines.push(` ${bold(e.name)}`);
133 lines.push(
134 ` ${'description:'.padEnd(metadataLabelWidth)} ${e.description ? gray(truncate(e.description, 70)) : '-'}`
135 );
136 lines.push(` ${'type:'.padEnd(metadataLabelWidth)} ${e.type.padEnd(typeWidth)}`);
137 lines.push(` ${'required:'.padEnd(metadataLabelWidth)} ${e.label.padEnd(labelWidth)}`);
138 if (e.hasDefault) {
139 lines.push(
140 ` ${'default:'.padEnd(metadataLabelWidth)} ${gray(truncate(JSON.stringify(e.defaultValue), 40))}`
141 );
142 }
143 return lines.join('\n');
144 })
145 .join('\n\n');
146 }
147
148 export const formatTriggerTypeInfo = (triggerType: TriggerType): string => {
149 const lines: string[] = [];
150
151 lines.push(`${bold('Name:')} ${triggerType.name}`);
152 lines.push(`${bold('Slug:')} ${triggerType.slug}`);
153 lines.push(`${bold('Type:')} ${triggerType.type}`);
154 lines.push(`${bold('Description:')} ${triggerType.description}`);
155 lines.push(`${bold('Instructions:')} ${triggerType.instructions}`);
156
157 lines.push('');
158 lines.push(gray('------------------------------'));
159 lines.push(bold('Config Fields:'));
160 lines.push(formatSchemaProperties(triggerType.config));
161
162 lines.push('');
163 lines.push(gray('------------------------------'));
164 lines.push(bold('Payload Fields:'));
165 lines.push(formatSchemaProperties(triggerType.payload));
166
167 return lines.join('\n');
168 };
169
170 const TRIGGER_STATUS_TABLE = {
171 id: 20,
172 triggerName: 26,
173 toolkit: 10,
174 userId: 16,
175 connectedAccountId: 22,
176 status: 10,
177 } as const;
178
179 const toStatus = (item: TriggerInstanceItem): string =>
180 item.disabled_at === null ? 'ACTIVE' : 'DISABLED';
181
182 export const toolkitSlugFromTriggerName = (triggerName: string): string => {
183 const prefix = triggerName.split('_')[0];
184 return prefix ? prefix.toLowerCase() : '-';
185 };
186
187 export const formatTriggersStatusTable = (items: ReadonlyArray<TriggerInstanceItem>): string => {
188 const header = [
189 bold('Trigger Id'.padEnd(TRIGGER_STATUS_TABLE.id)),
190 bold('Trigger Name'.padEnd(TRIGGER_STATUS_TABLE.triggerName)),
191 bold('Toolkit'.padEnd(TRIGGER_STATUS_TABLE.toolkit)),
192 bold('User Id'.padEnd(TRIGGER_STATUS_TABLE.userId)),
193 bold('Connected Account'.padEnd(TRIGGER_STATUS_TABLE.connectedAccountId)),
194 bold('Status'),
195 ].join(' ');
196
197 const rows = items.map(item => {
198 const status = toStatus(item);
199 return [
200 truncate(item.id, TRIGGER_STATUS_TABLE.id).padEnd(TRIGGER_STATUS_TABLE.id),
201 truncate(item.trigger_name, TRIGGER_STATUS_TABLE.triggerName).padEnd(
202 TRIGGER_STATUS_TABLE.triggerName
203 ),
204 truncate(toolkitSlugFromTriggerName(item.trigger_name), TRIGGER_STATUS_TABLE.toolkit).padEnd(
205 TRIGGER_STATUS_TABLE.toolkit
206 ),
207 truncate(item.user_id || '-', TRIGGER_STATUS_TABLE.userId).padEnd(
208 TRIGGER_STATUS_TABLE.userId
209 ),
210 truncate(item.connected_account_id || '-', TRIGGER_STATUS_TABLE.connectedAccountId).padEnd(
211 TRIGGER_STATUS_TABLE.connectedAccountId
212 ),
213 status === 'ACTIVE' ? status : gray(status),
214 ].join(' ');
215 });
216
217 return [header, ...rows].join('\n');
218 };
219
220 export const formatTriggersStatusJson = (items: ReadonlyArray<TriggerInstanceItem>): string =>
221 JSON.stringify(
222 items.map(item => ({
223 id: item.id,
224 uuid: item.uuid || undefined,
225 trigger_name: item.trigger_name,
226 toolkit_slug: toolkitSlugFromTriggerName(item.trigger_name),
227 user_id: item.user_id || undefined,
228 connected_account_id: item.connected_account_id || undefined,
229 status: toStatus(item),
230 disabled_at: item.disabled_at,
231 updated_at: item.updated_at || undefined,
232 trigger_data: item.trigger_data || undefined,
233 })),
234 null,
235 2
236 );