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 { Effect, Match, Option } from 'effect'; |
| 2 | import { |
| 3 | CONNECTED_ACCOUNTS_SHAPE_MESSAGE, |
| 4 | decodeAllConnectedAccountPages, |
| 5 | } from 'src/effects/decode-connected-account-list'; |
| 6 | import { failedWith } from 'src/effects/request-error'; |
| 7 | import { ComposioClientSingleton } from 'src/services/composio-clients'; |
| 8 | import { actingUser } from 'src/commands/surface'; |
| 9 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 10 | import { |
| 11 | getConnectedAccountPermissionGroup, |
| 12 | getConsumerPermissionSnapshot, |
| 13 | } from 'src/services/tool-permissions'; |
| 14 | import { listAllConnectedAccounts } from 'src/services/list-all-connected-accounts'; |
| 15 | import { ConnectionsListRequestError } from './connections.list.errors'; |
| 16 | import { formatConnectionsJson } from './connections.list.format'; |
| 17 | |
| 18 | /** The flags of `composio connections list`. */ |
| 19 | export type ConnectionsListParams = { |
| 20 | readonly toolkit: Option.Option<string>; |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * Prints the acting user's connections, optionally for one toolkit, as JSON grouped by toolkit |
| 25 | * slug with each account's permission group. |
| 26 | */ |
| 27 | export const runConnectionsList = ({ toolkit }: ConnectionsListParams) => |
| 28 | Effect.gen(function* () { |
| 29 | const ui = yield* TerminalUI; |
| 30 | const clientSingleton = yield* ComposioClientSingleton; |
| 31 | const toolkitSlug = Option.getOrUndefined(toolkit); |
| 32 | const { userId: consumerUserId, project: resolvedProject } = yield* actingUser(); |
| 33 | |
| 34 | const client = yield* clientSingleton.getFor({ |
| 35 | orgId: resolvedProject.orgId, |
| 36 | projectId: resolvedProject.projectId, |
| 37 | }); |
| 38 | const rawItems = yield* listAllConnectedAccounts(client, { |
| 39 | toolkit_slugs: toolkitSlug ? [toolkitSlug] : undefined, |
| 40 | user_ids: [consumerUserId], |
| 41 | }).pipe( |
| 42 | Effect.mapError( |
| 43 | cause => |
| 44 | new ConnectionsListRequestError({ |
| 45 | message: Match.value(cause).pipe( |
| 46 | Match.tag( |
| 47 | 'effects/ConnectedAccountListShapeError', |
| 48 | () => CONNECTED_ACCOUNTS_SHAPE_MESSAGE |
| 49 | ), |
| 50 | Match.orElse(() => 'Failed to list connections.') |
| 51 | ), |
| 52 | cause, |
| 53 | }) |
| 54 | ) |
| 55 | ); |
| 56 | const items = yield* decodeAllConnectedAccountPages(rawItems).pipe( |
| 57 | failedWith(ConnectionsListRequestError, CONNECTED_ACCOUNTS_SHAPE_MESSAGE) |
| 58 | ); |
| 59 | const permissionSnapshot = yield* getConsumerPermissionSnapshot({ |
| 60 | orgId: resolvedProject.orgId, |
| 61 | projectId: resolvedProject.projectId, |
| 62 | consumerUserId, |
| 63 | connectedAccountIds: items.map(item => item.id), |
| 64 | }); |
| 65 | const permissionGroups = new Map( |
| 66 | items.map(item => [ |
| 67 | item.id, |
| 68 | getConnectedAccountPermissionGroup({ |
| 69 | snapshot: permissionSnapshot, |
| 70 | connectedAccountId: item.id, |
| 71 | }), |
| 72 | ]) |
| 73 | ); |
| 74 | |
| 75 | yield* ui.output(formatConnectionsJson(items, permissionGroups), { force: true }); |
| 76 | }); |