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 { ComposioClientSingleton, ComposioToolkitsRepository } from 'src/services/composio-clients'; |
| 3 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 4 | import { idempotent } from 'src/services/composio-request'; |
| 5 | import { failedWith } from 'src/effects/request-error'; |
| 6 | import { resolveToolRouterSession } from 'src/effects/create-tool-router-session'; |
| 7 | import { extractMessage } from 'src/utils/api-error-extraction'; |
| 8 | import { reportFailure } from 'src/effects/report-failure'; |
| 9 | import { commandInputError } from 'src/commands/errors'; |
| 10 | import { resolveSurfaceProjectAndUserOption } from 'src/commands/surface'; |
| 11 | import { BROWSE_TOOLKITS_HINT, formatToolkitInfo, formatToolkitInfoJson } from '../format'; |
| 12 | import { ToolkitsInfoRequestError } from './toolkits.info.errors'; |
| 13 | |
| 14 | export type ToolkitsInfoParams = { |
| 15 | readonly slug: Option.Option<string>; |
| 16 | readonly allDetails: boolean; |
| 17 | }; |
| 18 | |
| 19 | const invalidInput = commandInputError('dev toolkits info'); |
| 20 | |
| 21 | /** |
| 22 | * Shows a toolkit with its connection status in the developer project |
| 23 | * (`resolveSurfaceProjectAndUserOption`: `--project-name`'s, the directory's, or for `--user-id` |
| 24 | * or the global test user the backend's default one) for the surface's user: the `dev` group's |
| 25 | * `--user-id`, then the project test user id, then the global one. Without a developer project the toolkit is shown without |
| 26 | * connection status. A missing `<slug>` fails with a `CommandInputError` before any request; a |
| 27 | * failed lookup or an unknown toolkit is reported on stderr and exits 1. |
| 28 | */ |
| 29 | export const runToolkitsInfo = ({ slug, allDetails }: ToolkitsInfoParams) => |
| 30 | Effect.gen(function* () { |
| 31 | const ui = yield* TerminalUI; |
| 32 | const clientSingleton = yield* ComposioClientSingleton; |
| 33 | |
| 34 | if (Option.isNone(slug)) { |
| 35 | return yield* invalidInput( |
| 36 | 'Missing required argument: <slug>\nTry specifying a toolkit slug, e.g.:\n> composio dev toolkits info "gmail"', |
| 37 | '<slug>' |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | const slugValue = slug.value; |
| 42 | // Resolved outside the session lookup, whose failures only drop the connection status. |
| 43 | const { project, user: acting } = yield* resolveSurfaceProjectAndUserOption; |
| 44 | const repo = yield* ComposioToolkitsRepository; |
| 45 | |
| 46 | if (Option.isNone(project)) { |
| 47 | yield* ui.log.info( |
| 48 | 'No developer project to read connection status from; showing toolkit details without it. Run `composio dev init`, or pass --user-id or --project-name.' |
| 49 | ); |
| 50 | } else if (Option.isNone(acting)) { |
| 51 | yield* ui.log.info( |
| 52 | 'No test user id found; showing toolkit details without connection status.' |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | const result = yield* ui |
| 57 | .withSpinner( |
| 58 | `Fetching toolkit "${slugValue}"...`, |
| 59 | Effect.gen(function* () { |
| 60 | const detailedToolkitOpt = yield* repo.getToolkitDetailed(slugValue).pipe(Effect.option); |
| 61 | |
| 62 | if (Option.isSome(acting)) { |
| 63 | const client = yield* clientSingleton.getFor({ |
| 64 | orgId: acting.value.project.orgId, |
| 65 | projectId: acting.value.project.projectId, |
| 66 | }); |
| 67 | const { sessionId } = yield* resolveToolRouterSession(client, acting.value.userId); |
| 68 | const sessionToolkits = yield* client.toolRouter.session |
| 69 | // Only the first item is read, and only a few of its fields. |
| 70 | .toolkits(sessionId, { toolkits: [slugValue] }, idempotent) |
| 71 | .pipe( |
| 72 | failedWith(ToolkitsInfoRequestError, `Failed to fetch toolkit "${slugValue}".`) |
| 73 | ); |
| 74 | return { toolkit: sessionToolkits.items[0], detailedToolkitOpt }; |
| 75 | } |
| 76 | |
| 77 | const toolkit = Option.match(detailedToolkitOpt, { |
| 78 | onNone: () => undefined, |
| 79 | onSome: detailed => ({ |
| 80 | slug: detailed.slug, |
| 81 | name: detailed.name, |
| 82 | meta: { |
| 83 | description: detailed.meta.description, |
| 84 | logo: '', |
| 85 | }, |
| 86 | is_no_auth: detailed.no_auth, |
| 87 | enabled: true, |
| 88 | connected_account: null, |
| 89 | composio_managed_auth_schemes: [...detailed.composio_managed_auth_schemes], |
| 90 | }), |
| 91 | }); |
| 92 | return { toolkit, detailedToolkitOpt }; |
| 93 | }) |
| 94 | ) |
| 95 | .pipe( |
| 96 | Effect.catch(error => |
| 97 | Effect.gen(function* () { |
| 98 | const message = extractMessage(error) ?? `Failed to fetch toolkit "${slugValue}".`; |
| 99 | yield* Effect.logDebug('Toolkit info error:', error); |
| 100 | return yield* reportFailure(ui, message, BROWSE_TOOLKITS_HINT); |
| 101 | }) |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | const toolkit = result.toolkit; |
| 106 | const detailedToolkit = Option.getOrUndefined(result.detailedToolkitOpt); |
| 107 | |
| 108 | if (!toolkit) { |
| 109 | // "Did you mean?" suggestions via legacy search |
| 110 | const suggestions = yield* repo.searchToolkits({ search: slugValue, limit: 3 }).pipe( |
| 111 | Effect.map(r => |
| 112 | r.items.map(s => ({ |
| 113 | label: `${s.slug} — ${s.meta.description}`, |
| 114 | command: `> composio dev toolkits info "${s.slug}"`, |
| 115 | })) |
| 116 | ), |
| 117 | Effect.catch(() => Effect.succeed([] as { label: string; command: string }[])) |
| 118 | ); |
| 119 | |
| 120 | const [first] = suggestions; |
| 121 | const hint = first |
| 122 | ? `Did you mean?\n${suggestions.map(s => ` ${s.label}`).join('\n')}\n\n${first.command}` |
| 123 | : BROWSE_TOOLKITS_HINT; |
| 124 | return yield* reportFailure(ui, `Toolkit "${slugValue}" not found.`, hint); |
| 125 | } |
| 126 | |
| 127 | yield* ui.log.message( |
| 128 | `Toolkit: ${toolkit.name}\n\n${formatToolkitInfo(toolkit, detailedToolkit, allDetails)}` |
| 129 | ); |
| 130 | |
| 131 | // Next step hint |
| 132 | yield* ui.log.step(`To list tools in this toolkit:\n> composio tools list "${toolkit.slug}"`); |
| 133 | |
| 134 | yield* ui.output(formatToolkitInfoJson(toolkit, detailedToolkit, allDetails)); |
| 135 | }); |