Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 95 lines 2.92 KB
Raw
1 import type { Tool } from 'src/models/tools';
2 import type { ToolRetrieveResponse } from '@composio/client/resources/tools';
3 import { bold, gray } from 'src/ui/colors';
4 import { truncate } from 'src/ui/truncate';
5 import { extractSchemaProperties } from 'src/ui/extract-schema-properties';
6
7 /**
8 * Format a list of tools as a human-readable table.
9 */
10 export function formatToolsTable(tools: ReadonlyArray<Tool>): string {
11 const header = `${bold('Slug'.padEnd(35))} ${bold('Name'.padEnd(20))} ${bold('Description')}`;
12
13 const rows = tools.map(t => {
14 const slug = truncate(t.slug, 35).padEnd(35);
15 const name = truncate(t.name, 20).padEnd(20);
16 const desc = gray(truncate(t.description, 50));
17 return `${slug} ${name} ${desc}`;
18 });
19
20 return [header, ...rows].join('\n');
21 }
22
23 /**
24 * Format tools as JSON for piped output.
25 */
26 export function formatToolsJson(tools: ReadonlyArray<Tool>): string {
27 return JSON.stringify(
28 tools.map(t => ({
29 slug: t.slug,
30 name: t.name,
31 description: t.description,
32 tags: t.tags,
33 })),
34 null,
35 2
36 );
37 }
38
39 /**
40 * Format JSON Schema properties as a human-readable parameter table.
41 * Extracts `properties` entries, cross-references `required` array.
42 */
43 function formatSchemaProperties(schema: Record<string, unknown>): string {
44 const entries = extractSchemaProperties(schema);
45 if (entries.length === 0) {
46 return ' (none)';
47 }
48
49 const nameWidth = Math.max(...entries.map(e => e.name.length));
50 const typeWidth = Math.max(...entries.map(e => e.type.length));
51 const labelWidth = Math.max(...entries.map(e => e.label.length));
52
53 return entries
54 .map(e => {
55 const desc = e.description ? ` ${gray(`"${truncate(e.description, 50)}"`)}` : '';
56 return ` ${e.name.padEnd(nameWidth)} ${e.type.padEnd(typeWidth)} ${e.label.padEnd(labelWidth)}${desc}`;
57 })
58 .join('\n');
59 }
60
61 /**
62 * Format a detailed tool for interactive display.
63 */
64 export function formatToolInfo(tool: ToolRetrieveResponse): string {
65 const lines: string[] = [];
66
67 lines.push(`${bold('Name:')} ${tool.name}`);
68 lines.push(`${bold('Slug:')} ${tool.slug}`);
69 lines.push(`${bold('Description:')} ${tool.description}`);
70 lines.push(`${bold('Tags:')} ${tool.tags.length > 0 ? tool.tags.join(', ') : '(none)'}`);
71
72 if (tool.toolkit.slug) {
73 lines.push(`${bold('Toolkit:')} ${tool.toolkit.name} (${tool.toolkit.slug})`);
74 }
75
76 if (tool.no_auth) {
77 lines.push(`${bold('Auth:')} No authentication required`);
78 }
79
80 if (tool.available_versions.length > 0) {
81 lines.push(`${bold('Versions:')} ${tool.available_versions.join(', ')}`);
82 }
83
84 // Input parameters
85 lines.push('');
86 lines.push(bold('Input Parameters:'));
87 lines.push(formatSchemaProperties(tool.input_parameters as Record<string, unknown>));
88
89 // Output parameters
90 lines.push('');
91 lines.push(bold('Output Parameters:'));
92 lines.push(formatSchemaProperties(tool.output_parameters as Record<string, unknown>));
93
94 return lines.join('\n');
95 }