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, Option } from 'effect'; |
| 2 | import { commandInputError } from 'src/commands/errors'; |
| 3 | import { actingUser, CommandSurface } from 'src/commands/surface'; |
| 4 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 5 | import { failedWith } from 'src/effects/request-error'; |
| 6 | import { setupCacheDir } from 'src/effects/setup-cache-dir'; |
| 7 | import { clampLimit } from 'src/ui/clamp-limit'; |
| 8 | import { resolveToolRouterSession } from 'src/effects/create-tool-router-session'; |
| 9 | import { buildMinimalPayloadFromSchema } from 'src/ui/build-minimal-payload'; |
| 10 | import { ComposioClientSingleton } from 'src/services/composio-clients'; |
| 11 | import { decode, idempotent } from 'src/services/composio-request'; |
| 12 | import { SearchToolSchemas, ToolSearchResponse } from 'src/models/tool-search'; |
| 13 | import { isRemoteCustomToolSlug } from 'src/utils/remote-custom-toolkit'; |
| 14 | import { toolInputRequiredError } from 'src/utils/tool-input-required'; |
| 15 | import { |
| 16 | primeConsumerConnectedToolkitsCacheInBackground, |
| 17 | writeConsumerConnectedToolkitsCache, |
| 18 | } from 'src/services/consumer-short-term-cache'; |
| 19 | import { appendCliSessionHistory } from 'src/services/cli-session-artifacts'; |
| 20 | import { |
| 21 | cacheToolInputDefinition, |
| 22 | getOrFetchToolInputDefinition, |
| 23 | } from 'src/services/tool-input-validation'; |
| 24 | import { ToolsSearchRequestError } from './tools.search.errors'; |
| 25 | import { |
| 26 | buildSearchNextSteps, |
| 27 | collectToolsForResult, |
| 28 | emitHumanSearchOutput, |
| 29 | stripSearchResultMetadata, |
| 30 | toHomeRelativePath, |
| 31 | TOOL_SCHEMA_PATH_FORMAT, |
| 32 | } from './tools.search.format'; |
| 33 | |
| 34 | export type ToolsSearchParams = { |
| 35 | readonly query: ReadonlyArray<string>; |
| 36 | readonly toolkits: Option.Option<string>; |
| 37 | readonly limit: number; |
| 38 | readonly json: boolean; |
| 39 | readonly human: boolean; |
| 40 | }; |
| 41 | |
| 42 | const invalidSearchInput = commandInputError('search'); |
| 43 | |
| 44 | const buildSearchJsonPayload = (params: { |
| 45 | searchResponse: ToolSearchResponse; |
| 46 | cacheScope: { orgId?: string; consumerUserId?: string }; |
| 47 | projectScope: { orgId: string; projectId: string }; |
| 48 | nextSteps: Array<{ action: string; command: string }>; |
| 49 | }) => |
| 50 | Effect.gen(function* () { |
| 51 | const cacheDir = yield* setupCacheDir; |
| 52 | const primaryToolSlugs = Array.from( |
| 53 | new Set(params.searchResponse.results.flatMap(result => result.primary_tool_slugs)) |
| 54 | ); |
| 55 | const primaryToolSchemaPaths = Object.fromEntries( |
| 56 | yield* Effect.forEach(primaryToolSlugs, slug => |
| 57 | Effect.gen(function* () { |
| 58 | const searchSchema = params.searchResponse.tool_schemas[slug]; |
| 59 | const definition = |
| 60 | isRemoteCustomToolSlug(slug) && searchSchema?.input_schema |
| 61 | ? yield* cacheToolInputDefinition({ |
| 62 | slug, |
| 63 | schema: searchSchema.input_schema, |
| 64 | }) |
| 65 | : yield* getOrFetchToolInputDefinition(slug, params.projectScope); |
| 66 | return [slug, toHomeRelativePath(cacheDir, definition.schemaPath)] satisfies readonly [ |
| 67 | string, |
| 68 | string, |
| 69 | ]; |
| 70 | }) |
| 71 | ) |
| 72 | ); |
| 73 | const connectedToolkits = Array.from( |
| 74 | new Set( |
| 75 | params.searchResponse.toolkit_connection_statuses |
| 76 | .filter(status => status.has_active_connection) |
| 77 | .map(status => status.toolkit.toLowerCase()) |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | if (params.cacheScope.orgId && params.cacheScope.consumerUserId) { |
| 82 | yield* writeConsumerConnectedToolkitsCache({ |
| 83 | orgId: params.cacheScope.orgId, |
| 84 | consumerUserId: params.cacheScope.consumerUserId, |
| 85 | toolkits: connectedToolkits, |
| 86 | }).pipe(Effect.catch(() => Effect.void)); |
| 87 | } |
| 88 | |
| 89 | return { |
| 90 | results: params.searchResponse.results.map(stripSearchResultMetadata), |
| 91 | tool_schemas: { |
| 92 | primary: primaryToolSchemaPaths, |
| 93 | related_tools_path_format: TOOL_SCHEMA_PATH_FORMAT, |
| 94 | }, |
| 95 | connected_toolkits: connectedToolkits, |
| 96 | ...(params.searchResponse.error ? { error: params.searchResponse.error } : {}), |
| 97 | next_steps: { |
| 98 | guidance: |
| 99 | 'You can directly proceed with these steps without waiting for the user to ask. Link accounts first if needed, then execute tools.', |
| 100 | steps: params.nextSteps, |
| 101 | }, |
| 102 | }; |
| 103 | }); |
| 104 | |
| 105 | export const runToolsSearch = (params: ToolsSearchParams) => |
| 106 | Effect.gen(function* () { |
| 107 | const ui = yield* TerminalUI; |
| 108 | const { tree } = yield* CommandSurface; |
| 109 | |
| 110 | const clampedLimit = clampLimit(params.limit); |
| 111 | const emitHuman = params.human; |
| 112 | const emitJson = params.json || !emitHuman; |
| 113 | const queries = params.query.map(q => q.trim()).filter(Boolean); |
| 114 | if (queries.length === 0) { |
| 115 | return yield* invalidSearchInput('At least one query is required.', '<query>'); |
| 116 | } |
| 117 | const toolkitFilter = Option.getOrUndefined(params.toolkits); |
| 118 | const toolkitList = |
| 119 | toolkitFilter && toolkitFilter.trim().length > 0 |
| 120 | ? toolkitFilter |
| 121 | .split(',') |
| 122 | .map(s => s.trim().toLowerCase()) |
| 123 | .filter(Boolean) |
| 124 | : undefined; |
| 125 | |
| 126 | const runSearchRequest = Effect.gen(function* () { |
| 127 | const { userId: resolvedUserId, project: resolvedProject } = yield* actingUser(); |
| 128 | const clientSingleton = yield* ComposioClientSingleton; |
| 129 | const client = yield* clientSingleton.getFor({ |
| 130 | orgId: resolvedProject.orgId, |
| 131 | projectId: resolvedProject.projectId, |
| 132 | }); |
| 133 | if (resolvedProject.projectType === 'CONSUMER') { |
| 134 | yield* primeConsumerConnectedToolkitsCacheInBackground({ |
| 135 | orgId: resolvedProject.orgId, |
| 136 | consumerUserId: resolvedUserId, |
| 137 | }); |
| 138 | } |
| 139 | const { sessionId } = yield* resolveToolRouterSession(client, resolvedUserId, { |
| 140 | toolkits: toolkitList, |
| 141 | cacheScope: |
| 142 | resolvedProject.projectType === 'CONSUMER' && resolvedProject.consumerUserId |
| 143 | ? { |
| 144 | orgId: resolvedProject.orgId, |
| 145 | projectId: resolvedProject.projectId, |
| 146 | consumerUserId: resolvedProject.consumerUserId, |
| 147 | } |
| 148 | : undefined, |
| 149 | }); |
| 150 | const searchPayload = { |
| 151 | queries: queries.map(query => ({ use_case: query })), |
| 152 | }; |
| 153 | // Decoded with the CLI's schema for the fields it reads; the rest, enums |
| 154 | // among them, may change without this version of the CLI noticing. |
| 155 | const searchResponse = yield* client.toolRouter.session |
| 156 | .search(sessionId, searchPayload, idempotent) |
| 157 | .pipe( |
| 158 | Effect.flatMap(decode(ToolSearchResponse, { operation: 'toolRouter.session.search' })), |
| 159 | failedWith(ToolsSearchRequestError, 'Failed to search tools.') |
| 160 | ); |
| 161 | const customToolSlugsMissingSchemas = Object.entries(searchResponse.tool_schemas) |
| 162 | .filter(([slug, schema]) => isRemoteCustomToolSlug(slug) && !schema.input_schema) |
| 163 | .map(([slug]) => slug); |
| 164 | const hydratedSearchResponse = |
| 165 | customToolSlugsMissingSchemas.length === 0 |
| 166 | ? searchResponse |
| 167 | : yield* Effect.gen(function* () { |
| 168 | const schemasFailed = (cause: unknown) => |
| 169 | new ToolsSearchRequestError({ |
| 170 | message: 'Failed to fetch custom tool schemas.', |
| 171 | cause, |
| 172 | }); |
| 173 | // Fetching tool schemas is a read, so it is retried like one. |
| 174 | const schemaResponse = yield* client.toolRouter.session |
| 175 | .executeMeta( |
| 176 | sessionId, |
| 177 | { |
| 178 | slug: 'COMPOSIO_GET_TOOL_SCHEMAS', |
| 179 | arguments: { tool_slugs: customToolSlugsMissingSchemas }, |
| 180 | }, |
| 181 | idempotent |
| 182 | ) |
| 183 | .pipe(Effect.mapError(schemasFailed)); |
| 184 | if (schemaResponse.result_type === 'input_required') { |
| 185 | return yield* schemasFailed( |
| 186 | toolInputRequiredError('COMPOSIO_GET_TOOL_SCHEMAS', schemaResponse) |
| 187 | ); |
| 188 | } |
| 189 | if (schemaResponse.result_type === 'blocked') { |
| 190 | return yield* schemasFailed(new Error(schemaResponse.message)); |
| 191 | } |
| 192 | if (schemaResponse.error) { |
| 193 | return yield* schemasFailed(new Error(schemaResponse.error)); |
| 194 | } |
| 195 | |
| 196 | const resolvedSchemas = yield* decode(SearchToolSchemas, { |
| 197 | operation: 'toolRouter.session.executeMeta', |
| 198 | })(schemaResponse.data.tool_schemas).pipe(Effect.mapError(schemasFailed)); |
| 199 | |
| 200 | return { |
| 201 | ...searchResponse, |
| 202 | tool_schemas: { ...searchResponse.tool_schemas, ...resolvedSchemas }, |
| 203 | }; |
| 204 | }); |
| 205 | return { |
| 206 | searchResponse: hydratedSearchResponse, |
| 207 | projectScope: { |
| 208 | orgId: resolvedProject.orgId, |
| 209 | projectId: resolvedProject.projectId, |
| 210 | }, |
| 211 | historyScope: |
| 212 | resolvedProject.projectType === 'CONSUMER' |
| 213 | ? { |
| 214 | orgId: resolvedProject.orgId, |
| 215 | consumerUserId: resolvedUserId, |
| 216 | toolRouterSessionId: sessionId, |
| 217 | } |
| 218 | : undefined, |
| 219 | }; |
| 220 | }); |
| 221 | |
| 222 | const searchResult = emitHuman |
| 223 | ? yield* ui.withSpinner( |
| 224 | queries.length === 1 |
| 225 | ? `Searching tools for "${queries[0]}"...` |
| 226 | : `Searching tools for ${queries.length} queries...`, |
| 227 | runSearchRequest |
| 228 | ) |
| 229 | : yield* runSearchRequest; |
| 230 | const searchResponse = searchResult.searchResponse; |
| 231 | |
| 232 | const toolkitSet = toolkitList && toolkitList.length > 0 ? new Set(toolkitList) : undefined; |
| 233 | |
| 234 | const resultsWithTools = searchResponse.results.map(result => ({ |
| 235 | result, |
| 236 | tools: collectToolsForResult({ |
| 237 | result, |
| 238 | toolSchemas: searchResponse.tool_schemas, |
| 239 | toolkitSet, |
| 240 | limit: clampedLimit, |
| 241 | }), |
| 242 | })); |
| 243 | |
| 244 | const totalToolCount = resultsWithTools.reduce((sum, item) => sum + item.tools.length, 0); |
| 245 | if (totalToolCount === 0) { |
| 246 | const description = |
| 247 | queries.length === 1 ? `"${queries[0]}"` : `the provided ${queries.length} queries`; |
| 248 | if (emitHuman) { |
| 249 | yield* ui.log.warn(`No tools found matching ${description}. Try broadening your search.`); |
| 250 | } else if (emitJson) { |
| 251 | yield* ui.log.message('[]'); |
| 252 | yield* ui.output('[]'); |
| 253 | } |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | const firstToolsList = resultsWithTools.find(item => item.tools.length > 0)?.tools ?? []; |
| 258 | const firstSlug = firstToolsList[0]?.slug; |
| 259 | const firstSchema = |
| 260 | firstSlug && searchResponse.tool_schemas[firstSlug] |
| 261 | ? searchResponse.tool_schemas[firstSlug] |
| 262 | : undefined; |
| 263 | const firstToolkit = firstSchema?.toolkit; |
| 264 | const firstPayload = buildMinimalPayloadFromSchema(firstSchema?.input_schema ?? {}); |
| 265 | const firstPayloadJson = JSON.stringify(firstPayload); |
| 266 | const firstDataArg = |
| 267 | Object.keys(firstPayload).length === 0 ? '-d "{}"' : `-d '${firstPayloadJson}'`; |
| 268 | |
| 269 | const nextSteps = buildSearchNextSteps({ |
| 270 | firstSlug, |
| 271 | firstToolkit, |
| 272 | firstDataArg, |
| 273 | tree, |
| 274 | }); |
| 275 | |
| 276 | const outputForJq = yield* buildSearchJsonPayload({ |
| 277 | searchResponse, |
| 278 | cacheScope: { |
| 279 | orgId: searchResult.historyScope?.orgId, |
| 280 | consumerUserId: searchResult.historyScope?.consumerUserId, |
| 281 | }, |
| 282 | projectScope: searchResult.projectScope, |
| 283 | nextSteps, |
| 284 | }); |
| 285 | |
| 286 | yield* appendCliSessionHistory({ |
| 287 | orgId: searchResult.historyScope?.orgId, |
| 288 | consumerUserId: searchResult.historyScope?.consumerUserId, |
| 289 | entry: { |
| 290 | command: 'search', |
| 291 | query: queries.join(' | '), |
| 292 | queries, |
| 293 | toolkitFilter: toolkitList ?? [], |
| 294 | limit: clampedLimit, |
| 295 | resultCount: totalToolCount, |
| 296 | toolRouterSessionId: searchResult.historyScope?.toolRouterSessionId, |
| 297 | nextSteps: searchResponse.next_steps_guidance ?? [], |
| 298 | }, |
| 299 | }).pipe(Effect.catch(() => Effect.void)); |
| 300 | |
| 301 | if (emitHuman) { |
| 302 | yield* emitHumanSearchOutput({ |
| 303 | ui, |
| 304 | queries, |
| 305 | resultsWithTools, |
| 306 | nextStepsGuidance: searchResponse.next_steps_guidance ?? [], |
| 307 | firstSlug, |
| 308 | firstDataArg, |
| 309 | tree, |
| 310 | error: searchResponse.error ?? null, |
| 311 | }); |
| 312 | } else if (emitJson) { |
| 313 | const outputJson = JSON.stringify(outputForJq, null, 2); |
| 314 | yield* ui.log.message(outputJson); |
| 315 | yield* ui.output(outputJson); |
| 316 | } |
| 317 | }); |