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 } from 'effect'; |
| 2 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 3 | import { listOrganizations, type OrganizationSummary } from 'src/services/composio-account'; |
| 4 | import { clampLimit } from 'src/ui/clamp-limit'; |
| 5 | |
| 6 | const DEFAULT_LIMIT = 50; |
| 7 | |
| 8 | /** Prompts user to select an org, or auto-selects if only one. */ |
| 9 | const selectOrganization = (params: { |
| 10 | organizations: ReadonlyArray<OrganizationSummary>; |
| 11 | currentOrgId?: string; |
| 12 | }) => |
| 13 | Effect.gen(function* () { |
| 14 | const ui = yield* TerminalUI; |
| 15 | const { organizations, currentOrgId } = params; |
| 16 | if (organizations.length === 0) return undefined; |
| 17 | if (organizations.length === 1) return organizations[0]; |
| 18 | |
| 19 | const currentOrganization = currentOrgId |
| 20 | ? organizations.find(org => org.id === currentOrgId) |
| 21 | : undefined; |
| 22 | const orderedOrganizations = currentOrganization |
| 23 | ? [currentOrganization, ...organizations.filter(org => org.id !== currentOrganization.id)] |
| 24 | : organizations; |
| 25 | |
| 26 | if (currentOrganization) { |
| 27 | yield* ui.log.info(`Current org: "${currentOrganization.name}" (${currentOrganization.id})`); |
| 28 | } |
| 29 | |
| 30 | return yield* ui.select('Select current organization:', [ |
| 31 | ...orderedOrganizations.map(org => ({ |
| 32 | value: org, |
| 33 | label: org.name, |
| 34 | hint: org.id === currentOrgId ? `${org.id} (current)` : org.id, |
| 35 | })), |
| 36 | ]); |
| 37 | }); |
| 38 | |
| 39 | export const runOrgSelection = (params: { |
| 40 | apiKey: string; |
| 41 | explicitOrgId?: string; |
| 42 | currentOrgId?: string; |
| 43 | limit?: number; |
| 44 | }) => |
| 45 | Effect.gen(function* () { |
| 46 | const ui = yield* TerminalUI; |
| 47 | const { apiKey, explicitOrgId, currentOrgId, limit = DEFAULT_LIMIT } = params; |
| 48 | const clampedLimit = clampLimit(limit); |
| 49 | |
| 50 | const selectedOrganization = |
| 51 | explicitOrgId !== undefined |
| 52 | ? ({ id: explicitOrgId, name: explicitOrgId } satisfies OrganizationSummary) |
| 53 | : yield* Effect.gen(function* () { |
| 54 | const organizations = yield* listOrganizations({ |
| 55 | apiKey, |
| 56 | limit: clampedLimit, |
| 57 | }); |
| 58 | yield* ui.log.info(`Loaded ${organizations.data.length} orgs`); |
| 59 | if (organizations.data.length === 0) return undefined; |
| 60 | return yield* selectOrganization({ |
| 61 | organizations: organizations.data, |
| 62 | currentOrgId, |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | if (!selectedOrganization) { |
| 67 | yield* ui.log.warn('No organizations found for this API key.'); |
| 68 | return undefined; |
| 69 | } |
| 70 | |
| 71 | yield* ui.log.info( |
| 72 | `Selected organization: "${selectedOrganization.name}" (${selectedOrganization.id})` |
| 73 | ); |
| 74 | |
| 75 | return selectedOrganization; |
| 76 | }); |