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 { resolveCommandProject, type ResolvedCommandProject } from 'src/services/command-project'; |
| 4 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 5 | import { ComposioUserContext } from 'src/services/user-context'; |
| 6 | import { |
| 7 | ABORTED_MESSAGE, |
| 8 | CONFIRM_PRINT_CREDENTIALS_MESSAGE, |
| 9 | NO_API_KEY_MESSAGE, |
| 10 | formatDebugApiInfo, |
| 11 | } from './debug.api-info.format'; |
| 12 | |
| 13 | const debugError = commandInputError('debug'); |
| 14 | |
| 15 | /** Only a consumer project carries the playground user the API key acts for. */ |
| 16 | export const consumerUserIdOf = (project: Option.Option<ResolvedCommandProject>): string | null => |
| 17 | project.pipe( |
| 18 | Option.filter(resolved => resolved.projectType === 'CONSUMER'), |
| 19 | Option.flatMapNullishOr(resolved => resolved.consumerUserId), |
| 20 | Option.getOrNull |
| 21 | ); |
| 22 | |
| 23 | /** |
| 24 | * The consumer project, when there is one. A project the user has not set up yet is a legitimate |
| 25 | * "none" here; a failed lookup is not, and keeps its typed error so the invocation prints it and exits |
| 26 | * non-zero. |
| 27 | */ |
| 28 | const resolveConsumerProject = resolveCommandProject({ mode: 'consumer' }).pipe( |
| 29 | Effect.map(Option.some), |
| 30 | Effect.catchTag('services/ProjectResolutionError', error => |
| 31 | error.reason === 'missing_org' || error.reason === 'missing_developer_project' |
| 32 | ? Effect.succeed(Option.none<ResolvedCommandProject>()) |
| 33 | : Effect.fail(error) |
| 34 | ) |
| 35 | ); |
| 36 | |
| 37 | /** |
| 38 | * Prints the CLI's API key, current org, and consumer user id as JSON, after the user confirms. |
| 39 | * Nothing gates the command on login, so a missing key is reported here. |
| 40 | */ |
| 41 | export const runDebugApiInfo = () => |
| 42 | Effect.gen(function* () { |
| 43 | const ui = yield* TerminalUI; |
| 44 | const confirmed = yield* ui.confirm(CONFIRM_PRINT_CREDENTIALS_MESSAGE, { defaultValue: false }); |
| 45 | if (!confirmed) { |
| 46 | return yield* debugError(ABORTED_MESSAGE); |
| 47 | } |
| 48 | const ctx = yield* ComposioUserContext; |
| 49 | const apiKey = yield* Effect.fromOption( |
| 50 | Option.filter(ctx.data.apiKey, key => key.length > 0), |
| 51 | () => debugError(NO_API_KEY_MESSAGE) |
| 52 | ); |
| 53 | const consumerProject = yield* resolveConsumerProject; |
| 54 | yield* ui.output( |
| 55 | formatDebugApiInfo({ |
| 56 | apiKey, |
| 57 | orgId: Option.getOrNull(ctx.data.orgId), |
| 58 | consumerUserId: consumerUserIdOf(consumerProject), |
| 59 | }), |
| 60 | { force: true } |
| 61 | ); |
| 62 | }); |