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 { Array as Arr, Effect, Option } from 'effect'; |
| 2 | import { resolveDeveloperResourceProject } from 'src/commands/surface'; |
| 3 | import { handleClientApiError } from 'src/effects/handle-http-error'; |
| 4 | import { CLIENT_API_ERROR_TAGS } from 'src/services/composio-errors'; |
| 5 | import { |
| 6 | ComposioToolkitsRepository, |
| 7 | type ComposioToolkitsRepositoryShape, |
| 8 | } from 'src/services/composio-clients'; |
| 9 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 10 | import { clampLimit } from 'src/ui/clamp-limit'; |
| 11 | import { |
| 12 | formatTriggersStatusJson, |
| 13 | formatTriggersStatusTable, |
| 14 | toolkitSlugFromTriggerName, |
| 15 | } from '../format'; |
| 16 | import { parseCsv } from '../parse-csv'; |
| 17 | |
| 18 | export type TriggersStatusParams = { |
| 19 | readonly userIds: Option.Option<string>; |
| 20 | readonly connectedAccountIds: Option.Option<string>; |
| 21 | readonly toolkits: Option.Option<string>; |
| 22 | readonly triggerIds: Option.Option<string>; |
| 23 | readonly triggerNames: Option.Option<string>; |
| 24 | readonly showDisabled: boolean; |
| 25 | readonly limit: number; |
| 26 | }; |
| 27 | |
| 28 | type ActiveTriggersPage = Effect.Success< |
| 29 | ReturnType<ComposioToolkitsRepositoryShape['listActiveTriggers']> |
| 30 | >; |
| 31 | |
| 32 | /** The comma-separated values of a filter flag, or `undefined` when the flag is absent. */ |
| 33 | const csvOption = (opt: Option.Option<string>): string[] | undefined => |
| 34 | Option.getOrUndefined(Option.map(opt, parseCsv)); |
| 35 | |
| 36 | /** |
| 37 | * The page's triggers whose toolkit is in `toolkits`, and the total the listing reports: |
| 38 | * the API's count without a toolkit filter, the size of the page with one, since the API |
| 39 | * does not filter by toolkit. |
| 40 | */ |
| 41 | const filterByToolkits = ( |
| 42 | page: ActiveTriggersPage, |
| 43 | toolkits: Option.Option<Arr.NonEmptyReadonlyArray<string>> |
| 44 | ) => |
| 45 | Option.match(toolkits, { |
| 46 | onNone: () => ({ items: page.items, total: page.total_items }), |
| 47 | onSome: slugs => ({ |
| 48 | items: page.items.filter(item => |
| 49 | slugs.includes(toolkitSlugFromTriggerName(item.trigger_name)) |
| 50 | ), |
| 51 | total: page.items.length, |
| 52 | }), |
| 53 | }); |
| 54 | |
| 55 | /** |
| 56 | * Display the developer project's active trigger instances with optional filters. Fails with |
| 57 | * `ProjectResolutionError` when no developer project can be found |
| 58 | * (`resolveDeveloperResourceProject`). |
| 59 | * |
| 60 | * @example |
| 61 | * ```bash |
| 62 | * composio dev triggers status |
| 63 | * composio dev triggers status --toolkits gmail,slack --show-disabled |
| 64 | * ``` |
| 65 | */ |
| 66 | export const runTriggersStatus = ({ |
| 67 | userIds, |
| 68 | connectedAccountIds, |
| 69 | toolkits, |
| 70 | triggerIds, |
| 71 | triggerNames, |
| 72 | showDisabled, |
| 73 | limit, |
| 74 | }: TriggersStatusParams) => |
| 75 | Effect.gen(function* () { |
| 76 | const ui = yield* TerminalUI; |
| 77 | const repo = yield* ComposioToolkitsRepository; |
| 78 | const project = yield* resolveDeveloperResourceProject; |
| 79 | |
| 80 | // An empty list (`--toolkits ","`) filters nothing. |
| 81 | const toolkitFilter = Option.flatMap(toolkits, csv => |
| 82 | Arr.match(parseCsv(csv), { |
| 83 | onEmpty: () => Option.none<Arr.NonEmptyReadonlyArray<string>>(), |
| 84 | onNonEmpty: slugs => Option.some(Arr.map(slugs, slug => slug.toLowerCase())), |
| 85 | }) |
| 86 | ); |
| 87 | |
| 88 | const page = yield* ui |
| 89 | .withSpinner( |
| 90 | 'Fetching trigger status...', |
| 91 | repo.listActiveTriggers(project, { |
| 92 | user_ids: csvOption(userIds), |
| 93 | connected_account_ids: csvOption(connectedAccountIds), |
| 94 | trigger_ids: csvOption(triggerIds), |
| 95 | trigger_names: csvOption(triggerNames)?.map(name => name.toUpperCase()), |
| 96 | show_disabled: showDisabled, |
| 97 | limit: clampLimit(limit), |
| 98 | }) |
| 99 | ) |
| 100 | .pipe( |
| 101 | Effect.catchTag( |
| 102 | CLIENT_API_ERROR_TAGS, |
| 103 | handleClientApiError(ui, { |
| 104 | fallbackMessage: 'Failed to fetch trigger status.', |
| 105 | hint: 'Retry with:\n> composio dev triggers status --show-disabled', |
| 106 | }) |
| 107 | ) |
| 108 | ); |
| 109 | |
| 110 | const { items, total } = filterByToolkits(page, toolkitFilter); |
| 111 | yield* Arr.match(items, { |
| 112 | onEmpty: () => |
| 113 | ui.log.warn( |
| 114 | showDisabled |
| 115 | ? 'No triggers found for the provided filters.' |
| 116 | : 'No active triggers found for the provided filters.' |
| 117 | ), |
| 118 | onNonEmpty: shown => |
| 119 | Effect.gen(function* () { |
| 120 | yield* ui.log.info( |
| 121 | `Listing ${shown.length} of ${total} triggers\n\n${formatTriggersStatusTable(shown)}` |
| 122 | ); |
| 123 | yield* ui.output(formatTriggersStatusJson(shown)); |
| 124 | }), |
| 125 | }); |
| 126 | }); |