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 { Array as Arr, Effect, Option, String as Str } from 'effect'; |
| 2 | import { commandInputError, notLoggedIn } from 'src/commands/errors'; |
| 3 | import { listOrganizationProjects } from 'src/services/composio-account'; |
| 4 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 5 | import { ComposioUserContext } from 'src/services/user-context'; |
| 6 | import { clampLimit } from 'src/ui/clamp-limit'; |
| 7 | import { |
| 8 | FETCH_PROJECTS_FAILED_MESSAGE, |
| 9 | LOADING_PROJECTS_MESSAGE, |
| 10 | NO_CURRENT_ORG_MESSAGE, |
| 11 | NO_CURRENT_ORG_OUTRO, |
| 12 | NO_PROJECTS_MESSAGE, |
| 13 | NO_PROJECTS_OUTRO, |
| 14 | PROJECTS_LIST_INTRO, |
| 15 | PROJECTS_LIST_OUTRO, |
| 16 | formatProjectLines, |
| 17 | formatProjectsJson, |
| 18 | loadedProjectsMessage, |
| 19 | } from './projects.list.format'; |
| 20 | |
| 21 | /** The flags of `composio dev projects list`. */ |
| 22 | export type ProjectsListParams = { |
| 23 | readonly orgId: Option.Option<string>; |
| 24 | readonly limit: number; |
| 25 | }; |
| 26 | |
| 27 | const invalidInput = commandInputError('dev projects list'); |
| 28 | |
| 29 | /** |
| 30 | * Lists the developer projects of `--org-id`, or of the current global org. With neither, it |
| 31 | * fails with a `CommandInputError` before any request. |
| 32 | */ |
| 33 | export const runProjectsList = ({ orgId, limit }: ProjectsListParams) => |
| 34 | Effect.gen(function* () { |
| 35 | const ui = yield* TerminalUI; |
| 36 | const ctx = yield* ComposioUserContext; |
| 37 | // `requiresLogin` on the command has already proven the key is there. |
| 38 | const apiKey = yield* Effect.fromOption(ctx.data.apiKey, notLoggedIn); |
| 39 | |
| 40 | yield* ui.intro(PROJECTS_LIST_INTRO); |
| 41 | |
| 42 | // An explicit `--org-id ""` does not fall back to the current org. |
| 43 | const resolvedOrgId = Option.orElse(orgId, () => ctx.data.orgId).pipe( |
| 44 | Option.filter(Str.isNonEmpty) |
| 45 | ); |
| 46 | |
| 47 | yield* Option.match(resolvedOrgId, { |
| 48 | onNone: () => |
| 49 | Effect.fail(invalidInput(`${NO_CURRENT_ORG_MESSAGE}\n${NO_CURRENT_ORG_OUTRO}`, '--org-id')), |
| 50 | onSome: resolvedOrgId => |
| 51 | Effect.gen(function* () { |
| 52 | const projects = yield* ui.withSpinner( |
| 53 | LOADING_PROJECTS_MESSAGE, |
| 54 | listOrganizationProjects({ apiKey, orgId: resolvedOrgId, limit: clampLimit(limit) }), |
| 55 | { |
| 56 | successMessage: result => loadedProjectsMessage(result.data.length), |
| 57 | errorMessage: FETCH_PROJECTS_FAILED_MESSAGE, |
| 58 | } |
| 59 | ); |
| 60 | |
| 61 | yield* Arr.match(projects.data, { |
| 62 | onEmpty: () => |
| 63 | Effect.gen(function* () { |
| 64 | yield* ui.log.warn(NO_PROJECTS_MESSAGE); |
| 65 | yield* ui.outro(NO_PROJECTS_OUTRO); |
| 66 | // Piped callers need the parseable empty result, not silence. |
| 67 | yield* ui.output(formatProjectsJson(projects.data)); |
| 68 | }), |
| 69 | onNonEmpty: data => |
| 70 | Effect.gen(function* () { |
| 71 | yield* ui.log.step(formatProjectLines(data)); |
| 72 | yield* ui.outro(PROJECTS_LIST_OUTRO); |
| 73 | yield* ui.output(formatProjectsJson(data)); |
| 74 | }), |
| 75 | }); |
| 76 | }), |
| 77 | }); |
| 78 | }); |