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
| 1 | import { Effect } from 'effect'; |
| 2 | import type { SearchResult, SearchToolSchemas } from 'src/models/tool-search'; |
| 3 | import type { Tool } from 'src/models/tools'; |
| 4 | import type { TerminalUI } from 'src/services/terminal-ui'; |
| 5 | import type { CommandTree } from 'src/commands/surface'; |
| 6 | import { commandHintExample, commandHintStep } from 'src/services/command-hints'; |
| 7 | import { formatToolsTable } from '../format'; |
| 8 | |
| 9 | export const stripSearchResultMetadata = <T extends Readonly<Record<string, unknown>>>( |
| 10 | result: T |
| 11 | ) => { |
| 12 | const { |
| 13 | reference_workbench_snippets: _referenceWorkbenchSnippets, |
| 14 | plan_id: _planId, |
| 15 | ...rest |
| 16 | } = result; |
| 17 | return rest; |
| 18 | }; |
| 19 | |
| 20 | export const TOOL_SCHEMA_PATH_FORMAT = '~/.composio/tool_definitions/<TOOL_SLUG>.json'; |
| 21 | |
| 22 | export const toHomeRelativePath = (cacheDir: string, absolutePath: string) => |
| 23 | absolutePath.startsWith(cacheDir) ? absolutePath.replace(cacheDir, '~/.composio') : absolutePath; |
| 24 | |
| 25 | export const collectToolsForResult = (params: { |
| 26 | result: { |
| 27 | primary_tool_slugs: ReadonlyArray<string>; |
| 28 | related_tool_slugs: ReadonlyArray<string>; |
| 29 | }; |
| 30 | toolSchemas: SearchToolSchemas; |
| 31 | toolkitSet?: Set<string>; |
| 32 | limit: number; |
| 33 | }): Tool[] => { |
| 34 | const mergedSlugs: string[] = []; |
| 35 | const seen = new Set<string>(); |
| 36 | for (const slug of [...params.result.primary_tool_slugs, ...params.result.related_tool_slugs]) { |
| 37 | if (!seen.has(slug)) { |
| 38 | seen.add(slug); |
| 39 | mergedSlugs.push(slug); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | const toolsList: Tool[] = []; |
| 44 | for (const slug of mergedSlugs) { |
| 45 | const schema = params.toolSchemas[slug]; |
| 46 | if (!schema) continue; |
| 47 | if (params.toolkitSet && !params.toolkitSet.has(schema.toolkit.toLowerCase())) continue; |
| 48 | |
| 49 | toolsList.push({ |
| 50 | slug: schema.tool_slug, |
| 51 | name: schema.tool_slug, |
| 52 | description: schema.description ?? '', |
| 53 | tags: [], |
| 54 | available_versions: [], |
| 55 | input_parameters: schema.input_schema ?? {}, |
| 56 | output_parameters: schema.output_schema ?? {}, |
| 57 | }); |
| 58 | |
| 59 | if (toolsList.length >= params.limit) break; |
| 60 | } |
| 61 | |
| 62 | return toolsList; |
| 63 | }; |
| 64 | |
| 65 | export const buildSearchNextSteps = (params: { |
| 66 | firstSlug?: string; |
| 67 | firstToolkit?: string; |
| 68 | firstDataArg: string; |
| 69 | tree: CommandTree; |
| 70 | }) => { |
| 71 | const steps: Array<{ action: string; command: string }> = []; |
| 72 | if (params.firstToolkit) { |
| 73 | steps.push({ |
| 74 | action: 'Link a user account', |
| 75 | command: |
| 76 | params.tree === 'root' |
| 77 | ? commandHintExample('root.link', { toolkit: String(params.firstToolkit).toLowerCase() }) |
| 78 | : commandHintExample('dev.connectedAccounts.link', { |
| 79 | toolkit: String(params.firstToolkit).toLowerCase(), |
| 80 | userId: '<user-id>', |
| 81 | }), |
| 82 | }); |
| 83 | } |
| 84 | if (params.firstSlug) { |
| 85 | steps.push({ |
| 86 | action: 'Execute a tool', |
| 87 | command: |
| 88 | params.tree === 'root' |
| 89 | ? commandHintExample('root.execute', { |
| 90 | slug: params.firstSlug, |
| 91 | data: params.firstDataArg, |
| 92 | }) |
| 93 | : commandHintExample('dev.playgroundExecute', { |
| 94 | slug: params.firstSlug, |
| 95 | userId: '<user-id>', |
| 96 | data: params.firstDataArg, |
| 97 | }), |
| 98 | }); |
| 99 | } |
| 100 | return steps; |
| 101 | }; |
| 102 | |
| 103 | export const emitHumanSearchOutput = (params: { |
| 104 | ui: TerminalUI; |
| 105 | queries: ReadonlyArray<string>; |
| 106 | resultsWithTools: Array<{ result: SearchResult; tools: Tool[] }>; |
| 107 | nextStepsGuidance: ReadonlyArray<string>; |
| 108 | firstSlug?: string; |
| 109 | firstDataArg: string; |
| 110 | tree: CommandTree; |
| 111 | error: string | null; |
| 112 | }) => |
| 113 | Effect.gen(function* () { |
| 114 | for (const { result, tools } of params.resultsWithTools) { |
| 115 | if (params.queries.length > 1) { |
| 116 | yield* params.ui.log.info(`Results for "${result.use_case}"`); |
| 117 | } |
| 118 | |
| 119 | if (tools.length === 0) { |
| 120 | yield* params.ui.log.warn(`No tools found for "${result.use_case}".`); |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | yield* params.ui.log.info(`Found ${tools.length} tools\n\n${formatToolsTable(tools)}`); |
| 125 | |
| 126 | const planSteps = Array.from(new Set(result.recommended_plan_steps ?? [])); |
| 127 | if (planSteps.length > 0) { |
| 128 | yield* params.ui.log.info( |
| 129 | `Plan:\n${planSteps.map((step, i) => `${i + 1}. ${step}`).join('\n')}` |
| 130 | ); |
| 131 | } else if (params.queries.length === 1 && params.nextStepsGuidance.length > 0) { |
| 132 | yield* params.ui.log.info( |
| 133 | `Plan:\n${params.nextStepsGuidance.map((step, i) => `${i + 1}. ${step}`).join('\n')}` |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (params.firstSlug) { |
| 139 | const linkHint = |
| 140 | params.tree === 'root' |
| 141 | ? commandHintStep('Link an account', 'root.link', { toolkit: '<toolkit>' }) |
| 142 | : commandHintStep('Link an account', 'dev.connectedAccounts.link', { |
| 143 | toolkit: '<toolkit>', |
| 144 | userId: '<user-id>', |
| 145 | }); |
| 146 | const executeHint = |
| 147 | params.tree === 'root' |
| 148 | ? commandHintStep('Execute a tool', 'root.execute', { |
| 149 | slug: params.firstSlug, |
| 150 | data: params.firstDataArg, |
| 151 | }) |
| 152 | : commandHintStep('Test a tool against a playground user', 'dev.playgroundExecute', { |
| 153 | slug: params.firstSlug, |
| 154 | userId: '<user-id>', |
| 155 | data: params.firstDataArg, |
| 156 | }); |
| 157 | const steps = params.firstSlug.startsWith('LOCAL_') ? [executeHint] : [linkHint, executeHint]; |
| 158 | yield* params.ui.log.step(steps.join('\n')); |
| 159 | } |
| 160 | |
| 161 | if (params.error) { |
| 162 | yield* params.ui.log.warn(params.error); |
| 163 | } |
| 164 | }); |