@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 296 lines 10.7 KB
Raw
1 import type { Toolkit, ToolkitDetailed } from 'src/models/toolkits';
2 import type { SessionToolkitsResponse } from '@composio/client/resources/tool-router';
3 import { bold, gray, green, dim } from 'src/ui/colors';
4 import { truncate } from 'src/ui/truncate';
5
6 type SessionToolkitItem = SessionToolkitsResponse['items'][number];
7
8 /** The next-step hint the toolkit commands print when a toolkit cannot be found or fetched. */
9 export const BROWSE_TOOLKITS_HINT = 'Browse available toolkits:\n> composio dev toolkits list';
10
11 const formatAuthConfigField = (field: {
12 name: string;
13 displayName: string;
14 description: string;
15 type: string;
16 required: boolean;
17 default: string | null;
18 }): string =>
19 [
20 ` - ${field.name} (${field.type})`,
21 ` display: ${field.displayName}`,
22 ` required: ${field.required ? 'yes' : 'no'}`,
23 ` default: ${field.default ?? '-'}`,
24 ` description: ${field.description || '-'}`,
25 ].join('\n');
26
27 // ---------- Unified toolkit list format ----------
28
29 const TOOLKITS_TABLE = {
30 name: 20,
31 slug: 20,
32 version: 12,
33 tools: 7,
34 triggers: 10,
35 connected: 16,
36 description: 50,
37 } as const;
38
39 /**
40 * Unified representation of a toolkit for list/search output.
41 * Catalog data is always present; connection data is optional (requires a user session).
42 */
43 export interface UnifiedToolkit {
44 name: string;
45 slug: string;
46 description: string;
47 latest_version: string | null;
48 tools_count: number;
49 triggers_count: number;
50 is_no_auth: boolean;
51 enabled: boolean | null;
52 connected: {
53 status: string;
54 id: string;
55 auth_scheme: string | null;
56 is_composio_managed: boolean | null;
57 } | null;
58 }
59
60 /**
61 * Build unified toolkit items from catalog data, optionally enriched with
62 * session connection data (keyed by slug).
63 */
64 export function mergeToolkitData(
65 catalogItems: ReadonlyArray<Toolkit>,
66 sessionItems?: ReadonlyArray<SessionToolkitItem>
67 ): UnifiedToolkit[] {
68 const sessionBySlug = new Map<string, SessionToolkitItem>();
69 if (sessionItems) {
70 for (const s of sessionItems) {
71 sessionBySlug.set(s.slug, s);
72 }
73 }
74
75 return catalogItems.map(t => {
76 const session = sessionBySlug.get(t.slug);
77 return {
78 name: t.name,
79 slug: t.slug,
80 description: t.meta.description,
81 latest_version: t.meta.available_versions.at(-1) ?? null,
82 tools_count: t.meta.tools_count,
83 triggers_count: t.meta.triggers_count,
84 is_no_auth: t.no_auth,
85 enabled: session?.enabled ?? null,
86 connected: session?.connected_account
87 ? {
88 status: session.connected_account.status,
89 id: session.connected_account.id,
90 auth_scheme: session.connected_account.auth_config?.auth_scheme ?? null,
91 is_composio_managed: session.connected_account.auth_config?.is_composio_managed ?? null,
92 }
93 : null,
94 };
95 });
96 }
97
98 /**
99 * Derive connection status text and color from a unified toolkit.
100 */
101 function connectionStatusParts(item: UnifiedToolkit): {
102 text: string;
103 color: (s: string) => string;
104 } {
105 if (item.is_no_auth) return { text: 'no auth', color: dim };
106 if (item.connected?.status === 'ACTIVE') return { text: 'active', color: green };
107 if (item.connected === null && item.enabled === null) return { text: '-', color: dim };
108 return { text: 'not connected', color: dim };
109 }
110
111 /**
112 * Format a list of unified toolkits as a human-readable table.
113 */
114 export function formatToolkitsTable(toolkits: ReadonlyArray<UnifiedToolkit>): string {
115 const header = `${bold('Name'.padEnd(TOOLKITS_TABLE.name))} ${bold('Slug'.padEnd(TOOLKITS_TABLE.slug))} ${bold('Version'.padEnd(TOOLKITS_TABLE.version))} ${bold('Tools'.padEnd(TOOLKITS_TABLE.tools))} ${bold('Triggers'.padEnd(TOOLKITS_TABLE.triggers))} ${bold('Connected'.padEnd(TOOLKITS_TABLE.connected))} ${bold('Description')}`;
116
117 const rows = toolkits.map(t => {
118 const name = truncate(t.name, TOOLKITS_TABLE.name).padEnd(TOOLKITS_TABLE.name);
119 const slug = truncate(t.slug, TOOLKITS_TABLE.slug).padEnd(TOOLKITS_TABLE.slug);
120 const version = truncate(t.latest_version ?? '-', TOOLKITS_TABLE.version).padEnd(
121 TOOLKITS_TABLE.version
122 );
123 const tools = String(t.tools_count).padEnd(TOOLKITS_TABLE.tools);
124 const triggers = String(t.triggers_count).padEnd(TOOLKITS_TABLE.triggers);
125 const { text: statusText, color: statusColor } = connectionStatusParts(t);
126 const status = statusColor(statusText.padEnd(TOOLKITS_TABLE.connected));
127 const desc = gray(truncate(t.description, TOOLKITS_TABLE.description));
128 return `${name} ${slug} ${version} ${tools} ${triggers} ${status} ${desc}`;
129 });
130
131 return [header, ...rows].join('\n');
132 }
133
134 /**
135 * Format unified toolkits as JSON for piped output.
136 */
137 export function formatToolkitsJson(toolkits: ReadonlyArray<UnifiedToolkit>): string {
138 return JSON.stringify(toolkits, null, 2);
139 }
140
141 /**
142 * Format a single session toolkit for detailed interactive display.
143 */
144 export function formatToolkitInfo(
145 toolkit: SessionToolkitItem,
146 detailed?: ToolkitDetailed,
147 showAllDetails = false
148 ): string {
149 const lines: string[] = [];
150 const detailedMeta = detailed?.meta;
151 const availableVersions = detailedMeta?.available_versions ?? [];
152 const latestVersion = availableVersions.at(-1);
153 const authModes = detailed?.auth_config_details.map(detail => detail.mode) ?? [];
154
155 lines.push(`${bold('Name:')} ${toolkit.name}`);
156 lines.push(`${bold('Slug:')} ${toolkit.slug}`);
157 lines.push(
158 `${bold('Description:')} ${detailedMeta?.description || toolkit.meta.description || '(none)'}`
159 );
160 lines.push(`${bold('Latest Version:')} ${latestVersion ?? '-'}`);
161 lines.push(`${bold('Tools Count:')} ${detailedMeta?.tools_count ?? '-'}`);
162 lines.push(`${bold('Triggers Count:')} ${detailedMeta?.triggers_count ?? '-'}`);
163 if (authModes.length > 0) {
164 lines.push(`${bold('Auth Modes:')} ${authModes.join(', ')}`);
165 }
166 if (showAllDetails && detailed) {
167 lines.push(`${bold('No Auth:')} ${detailed.no_auth ? 'Yes' : 'No'}`);
168 lines.push(`${bold('Is Local Toolkit:')} ${detailed.is_local_toolkit ? 'Yes' : 'No'}`);
169 lines.push(`${bold('Created At:')} ${String(detailed.meta.created_at)}`);
170 lines.push(`${bold('Updated At:')} ${String(detailed.meta.updated_at)}`);
171 lines.push(
172 `${bold('Composio Managed Auth Schemes:')} ${
173 detailed.composio_managed_auth_schemes.length > 0
174 ? detailed.composio_managed_auth_schemes.join(', ')
175 : '-'
176 }`
177 );
178 lines.push('');
179 lines.push(bold('Auth Config Details:'));
180 if (detailed.auth_config_details.length === 0) {
181 lines.push(' - none');
182 } else {
183 for (const detail of detailed.auth_config_details) {
184 lines.push(` - ${detail.mode} (${detail.name})`);
185 const creationRequired = detail.fields.auth_config_creation.required;
186 const creationOptional = detail.fields.auth_config_creation.optional;
187 const initiationRequired = detail.fields.connected_account_initiation.required;
188 const initiationOptional = detail.fields.connected_account_initiation.optional;
189
190 lines.push(' auth_config_creation.required:');
191 lines.push(
192 creationRequired.length > 0
193 ? creationRequired.map(formatAuthConfigField).join('\n')
194 : ' - none'
195 );
196 lines.push(' auth_config_creation.optional:');
197 lines.push(
198 creationOptional.length > 0
199 ? creationOptional.map(formatAuthConfigField).join('\n')
200 : ' - none'
201 );
202 lines.push(' connected_account_initiation.required:');
203 lines.push(
204 initiationRequired.length > 0
205 ? initiationRequired.map(formatAuthConfigField).join('\n')
206 : ' - none'
207 );
208 lines.push(' connected_account_initiation.optional:');
209 lines.push(
210 initiationOptional.length > 0
211 ? initiationOptional.map(formatAuthConfigField).join('\n')
212 : ' - none'
213 );
214 }
215 }
216 }
217
218 if (toolkit.is_no_auth) {
219 lines.push(`${bold('Auth:')} No authentication required`);
220 } else if (toolkit.composio_managed_auth_schemes.length > 0) {
221 lines.push(
222 `${bold('Composio Managed Auth Schemes:')} ${toolkit.composio_managed_auth_schemes.join(', ')}`
223 );
224 }
225
226 // Connection status — render the actual status, not a hardcoded label
227 lines.push('');
228 if (toolkit.connected_account) {
229 const ca = toolkit.connected_account;
230 const statusDisplay = ca.status === 'ACTIVE' ? green(ca.status) : dim(ca.status);
231 lines.push(`${bold('Connection Status:')} ${statusDisplay}`);
232 lines.push(`${bold('Connected Account ID:')} ${ca.id}`);
233 lines.push(`${bold('Auth Scheme:')} ${ca.auth_config?.auth_scheme ?? '-'}`);
234 lines.push(
235 `${bold('Composio Managed:')} ${ca.auth_config?.is_composio_managed ? 'Yes' : 'No'}`
236 );
237 } else if (!toolkit.is_no_auth) {
238 lines.push(`${bold('Connection Status:')} ${dim('Not connected')}`);
239 lines.push(
240 `${bold('Tip:')} Link this toolkit:\n> composio dev connected-accounts link "${toolkit.slug}"`
241 );
242 }
243
244 return lines.join('\n');
245 }
246
247 /**
248 * Format a single session toolkit as JSON for piped output.
249 * Produces a stable, curated schema — does not leak the raw API response.
250 */
251 export function formatToolkitInfoJson(
252 toolkit: SessionToolkitItem,
253 detailed?: ToolkitDetailed,
254 showAllDetails = false
255 ): string {
256 const availableVersions = detailed?.meta.available_versions ?? [];
257 const latestVersion = availableVersions.at(-1) ?? null;
258 return JSON.stringify(
259 {
260 name: toolkit.name,
261 slug: toolkit.slug,
262 meta: {
263 description: detailed?.meta.description ?? toolkit.meta.description,
264 logo: toolkit.meta.logo,
265 latest_version: latestVersion,
266 tools_count: detailed?.meta.tools_count ?? null,
267 triggers_count: detailed?.meta.triggers_count ?? null,
268 },
269 is_no_auth: toolkit.is_no_auth,
270 enabled: toolkit.enabled,
271 connected_account: toolkit.connected_account
272 ? {
273 status: toolkit.connected_account.status,
274 id: toolkit.connected_account.id,
275 auth_scheme: toolkit.connected_account.auth_config?.auth_scheme ?? null,
276 is_composio_managed: toolkit.connected_account.auth_config?.is_composio_managed ?? null,
277 }
278 : null,
279 composio_managed_auth_schemes: toolkit.composio_managed_auth_schemes,
280 auth_modes: detailed?.auth_config_details.map(detail => detail.mode) ?? [],
281 ...(showAllDetails && detailed
282 ? {
283 detailed: {
284 is_local_toolkit: detailed.is_local_toolkit,
285 no_auth: detailed.no_auth,
286 created_at: String(detailed.meta.created_at),
287 updated_at: String(detailed.meta.updated_at),
288 auth_config_details: detailed.auth_config_details,
289 },
290 }
291 : {}),
292 },
293 null,
294 2
295 );
296 }