Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import type { SessionToolkitsResponse } from '@composio/client/resources/tool-router'; |
| 2 | import { Array as Arr, Effect, Option } from 'effect'; |
| 3 | import { |
| 4 | fallbackNotice, |
| 5 | resolveSurfaceProjectAndUserOption, |
| 6 | warnOnStderr, |
| 7 | } from 'src/commands/surface'; |
| 8 | import { resolveToolRouterSession } from 'src/effects/create-tool-router-session'; |
| 9 | import { ComposioClientSingleton, ComposioToolkitsRepository } from 'src/services/composio-clients'; |
| 10 | import { idempotent } from 'src/services/composio-request'; |
| 11 | import { printFailure, ReportedFailure } from 'src/effects/report-failure'; |
| 12 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 13 | import { clampLimit } from 'src/ui/clamp-limit'; |
| 14 | import { extractMessage } from 'src/utils/api-error-extraction'; |
| 15 | import { |
| 16 | formatToolkitsJson, |
| 17 | formatToolkitsTable, |
| 18 | mergeToolkitData, |
| 19 | type UnifiedToolkit, |
| 20 | } from '../format'; |
| 21 | |
| 22 | export type ToolkitsListParams = { |
| 23 | readonly query: Option.Option<string>; |
| 24 | readonly limit: number; |
| 25 | readonly connected: Option.Option<boolean>; |
| 26 | }; |
| 27 | |
| 28 | type SessionToolkitItem = SessionToolkitsResponse['items'][number]; |
| 29 | |
| 30 | /** |
| 31 | * Applies `--connected` to the merged rows. The filter needs the session's connection data; when a |
| 32 | * user was resolved but the session returned nothing, the rows are kept and the filter is reported |
| 33 | * as skipped. |
| 34 | */ |
| 35 | export const applyConnectedFilter = ( |
| 36 | rows: ReadonlyArray<UnifiedToolkit>, |
| 37 | params: { |
| 38 | readonly connectedOnly: boolean; |
| 39 | readonly sessionLoaded: boolean; |
| 40 | readonly userResolved: boolean; |
| 41 | } |
| 42 | ): { readonly rows: ReadonlyArray<UnifiedToolkit>; readonly filterSkipped: boolean } => { |
| 43 | if (params.connectedOnly && params.sessionLoaded) { |
| 44 | return { rows: rows.filter(row => row.connected?.status === 'ACTIVE'), filterSkipped: false }; |
| 45 | } |
| 46 | return { rows, filterSkipped: params.connectedOnly && params.userResolved }; |
| 47 | }; |
| 48 | |
| 49 | /** Why the listing carries no connection status when no developer project was resolved. */ |
| 50 | const NO_PROJECT_STATUS_MESSAGE = |
| 51 | 'No developer project to read connection status from; showing toolkits without it. Run `composio dev init`, or pass --user-id or --project-name.'; |
| 52 | |
| 53 | /** |
| 54 | * Lists the toolkit catalog. When the developer project and a user are known |
| 55 | * (`resolveSurfaceProjectAndUserOption`: `--project-name`'s project, the directory's, or for |
| 56 | * `--user-id` or the global test user the backend's default one), the rows also carry that |
| 57 | * user's connection status, read from a tool router session in that project; a failed session |
| 58 | * read only drops the status. Without a developer project the catalog is listed without it. |
| 59 | */ |
| 60 | export const runToolkitsList = ({ query, limit, connected }: ToolkitsListParams) => |
| 61 | Effect.gen(function* () { |
| 62 | const ui = yield* TerminalUI; |
| 63 | const repo = yield* ComposioToolkitsRepository; |
| 64 | const clientSingleton = yield* ComposioClientSingleton; |
| 65 | |
| 66 | const clampedLimit = clampLimit(limit); |
| 67 | // Resolved outside the session lookup, whose failures only drop the connection status. |
| 68 | const { project, user } = yield* resolveSurfaceProjectAndUserOption; |
| 69 | |
| 70 | if (Option.isNone(project)) { |
| 71 | yield* ui.log.info(NO_PROJECT_STATUS_MESSAGE); |
| 72 | } |
| 73 | |
| 74 | // `devActingUserOption` named the test user standing in; say how to pick another. |
| 75 | if (Option.isSome(Option.flatMap(user, fallbackNotice))) { |
| 76 | yield* ui.log.message('To show status for a specific user, use `--user-id`.'); |
| 77 | } |
| 78 | |
| 79 | if (Option.isSome(connected) && Option.isNone(user)) { |
| 80 | yield* warnOnStderr( |
| 81 | Option.isNone(project) |
| 82 | ? '`--connected` filter could not be applied — developer project unavailable. Showing the unfiltered catalog.' |
| 83 | : '`--connected` requires a user id. Use `--user-id` or run `composio dev init`.' |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | const catalogEffect = repo.searchToolkits({ |
| 88 | search: Option.getOrUndefined(query), |
| 89 | limit: clampedLimit, |
| 90 | }); |
| 91 | |
| 92 | // The session is created alongside the catalog fetch (saves one round trip); its toolkits |
| 93 | // call needs the session id, so it runs after. |
| 94 | const sessionContextEffect = Option.match(user, { |
| 95 | onNone: () => Effect.succeedNone, |
| 96 | onSome: resolved => |
| 97 | Effect.gen(function* () { |
| 98 | const client = yield* clientSingleton.getFor({ |
| 99 | orgId: resolved.project.orgId, |
| 100 | projectId: resolved.project.projectId, |
| 101 | }); |
| 102 | const { sessionId } = yield* resolveToolRouterSession(client, resolved.userId); |
| 103 | return Option.some({ client, sessionId }); |
| 104 | }).pipe( |
| 105 | Effect.catch(error => |
| 106 | Effect.logDebug('Failed to create session:', error).pipe(Effect.as(Option.none())) |
| 107 | ) |
| 108 | ), |
| 109 | }); |
| 110 | |
| 111 | const [catalogResult, sessionContext] = yield* ui.withSpinner( |
| 112 | 'Fetching toolkits...', |
| 113 | Effect.all([catalogEffect, sessionContextEffect], { concurrency: 'unbounded' }) |
| 114 | ); |
| 115 | |
| 116 | if (catalogResult.items.length === 0) { |
| 117 | yield* ui.log.warn('No toolkits found. Try broadening your search.'); |
| 118 | yield* ui.output('[]'); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // The session items only add connection status to the catalog rows, and a failed or empty |
| 123 | // read is a silent fallback. |
| 124 | const sessionItems = yield* Option.match(sessionContext, { |
| 125 | onNone: () => Effect.succeedNone, |
| 126 | onSome: ({ client, sessionId }) => |
| 127 | client.toolRouter.session |
| 128 | .toolkits( |
| 129 | sessionId, |
| 130 | { |
| 131 | search: Option.getOrUndefined(query), |
| 132 | limit: clampedLimit, |
| 133 | is_connected: Option.getOrUndefined(connected), |
| 134 | }, |
| 135 | idempotent |
| 136 | ) |
| 137 | .pipe( |
| 138 | Effect.map(response => response.items), |
| 139 | Effect.catch(error => |
| 140 | Effect.logDebug('Failed to fetch session toolkits:', error).pipe( |
| 141 | Effect.as<ReadonlyArray<SessionToolkitItem>>([]) |
| 142 | ) |
| 143 | ), |
| 144 | Effect.map(Option.liftPredicate(Arr.isReadonlyArrayNonEmpty)) |
| 145 | ), |
| 146 | }); |
| 147 | |
| 148 | const { rows, filterSkipped } = applyConnectedFilter( |
| 149 | mergeToolkitData(catalogResult.items, Option.getOrUndefined(sessionItems)), |
| 150 | { |
| 151 | connectedOnly: Option.getOrElse(connected, () => false), |
| 152 | sessionLoaded: Option.isSome(sessionItems), |
| 153 | userResolved: Option.isSome(user), |
| 154 | } |
| 155 | ); |
| 156 | if (filterSkipped) { |
| 157 | yield* warnOnStderr('`--connected` filter could not be applied — session data unavailable.'); |
| 158 | } |
| 159 | |
| 160 | if (rows.length === 0) { |
| 161 | yield* ui.log.warn('No connected toolkits found. Try without --connected.'); |
| 162 | yield* ui.output('[]'); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | yield* ui.log.info( |
| 167 | `Listing ${rows.length} of ${catalogResult.total_items} toolkits\n\n${formatToolkitsTable(rows)}` |
| 168 | ); |
| 169 | |
| 170 | const firstSlug = rows[0]?.slug; |
| 171 | if (firstSlug) { |
| 172 | yield* ui.log.step( |
| 173 | `To view details of a toolkit:\n> composio dev toolkits info "${firstSlug}"` |
| 174 | ); |
| 175 | } |
| 176 | yield* ui.output(formatToolkitsJson(rows)); |
| 177 | }).pipe( |
| 178 | Effect.catch(error => |
| 179 | Effect.gen(function* () { |
| 180 | const ui = yield* TerminalUI; |
| 181 | const message = extractMessage(error) ?? 'An error occurred while fetching toolkits.'; |
| 182 | yield* printFailure(ui, message); |
| 183 | yield* ui.output('[]'); |
| 184 | return yield* new ReportedFailure({ message }); |
| 185 | }) |
| 186 | ) |
| 187 | ); |