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, Match, Option, Result } from 'effect'; |
| 2 | import { commandInputError } from 'src/commands/errors'; |
| 3 | import { devActingUserOption, resolveDeveloperResourceProject } from 'src/commands/surface'; |
| 4 | import { handleClientApiError } from 'src/effects/handle-http-error'; |
| 5 | import { ComposioToolkitsRepository } from 'src/services/composio-clients'; |
| 6 | import { CLIENT_API_ERROR_TAGS } from 'src/services/composio-errors'; |
| 7 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 8 | import { parseJsonRecord, type JsonParsingError } from 'src/utils/parse-json'; |
| 9 | |
| 10 | export type TriggersCreateParams = { |
| 11 | readonly triggerName: Option.Option<string>; |
| 12 | readonly connectedAccountId: Option.Option<string>; |
| 13 | readonly triggerConfig: Option.Option<string>; |
| 14 | }; |
| 15 | |
| 16 | /** `--trigger-config` as a record, `undefined` when the flag is absent. */ |
| 17 | const parseTriggerConfig = (triggerConfig: Option.Option<string>) => |
| 18 | Option.match(triggerConfig, { |
| 19 | onNone: () => Result.succeed(undefined), |
| 20 | onSome: parseJsonRecord, |
| 21 | }); |
| 22 | |
| 23 | const invalidInput = commandInputError('dev triggers create'); |
| 24 | |
| 25 | /** The usage error for a `--trigger-config` that is not valid JSON or not a JSON object. */ |
| 26 | const invalidTriggerConfig = (error: JsonParsingError) => |
| 27 | invalidInput( |
| 28 | Match.value(error.reason).pipe( |
| 29 | Match.when( |
| 30 | 'not-a-record', |
| 31 | () => '--trigger-config must be a JSON object (e.g. \'{"key":"value"}\').' |
| 32 | ), |
| 33 | Match.when( |
| 34 | 'syntax', |
| 35 | () => |
| 36 | 'Invalid JSON in --trigger-config. Please provide valid JSON.\nExample:\n> composio dev triggers create "GMAIL_NEW_GMAIL_MESSAGE" --trigger-config \'{"label":"inbox"}\'' |
| 37 | ), |
| 38 | Match.exhaustive |
| 39 | ), |
| 40 | '--trigger-config' |
| 41 | ); |
| 42 | |
| 43 | /** |
| 44 | * Create a trigger instance in the developer project (`resolveDeveloperResourceProject`) as |
| 45 | * the `dev` group's `--user-id`, else the directory project's test user (for its own project), |
| 46 | * else the global test user; without any, the request omits `user_id`. A missing |
| 47 | * `<trigger-name>` or an invalid `--trigger-config` fails with a `CommandInputError` before any |
| 48 | * request, and a developer project that cannot be found with `ProjectResolutionError`; an API failure is |
| 49 | * reported with a hint and exits 1. |
| 50 | * |
| 51 | * @example |
| 52 | * ```bash |
| 53 | * composio dev triggers create "GMAIL_NEW_GMAIL_MESSAGE" --connected-account-id "con_123" |
| 54 | * ``` |
| 55 | */ |
| 56 | export const runTriggersCreate = ({ |
| 57 | triggerName, |
| 58 | connectedAccountId, |
| 59 | triggerConfig, |
| 60 | }: TriggersCreateParams) => |
| 61 | Effect.gen(function* () { |
| 62 | const ui = yield* TerminalUI; |
| 63 | const repo = yield* ComposioToolkitsRepository; |
| 64 | |
| 65 | if (Option.isNone(triggerName)) { |
| 66 | return yield* invalidInput( |
| 67 | 'Missing required argument: <trigger-name>\nTry specifying a trigger slug, e.g.:\n> composio dev triggers create "GMAIL_NEW_GMAIL_MESSAGE" --connected-account-id "con_123"', |
| 68 | '<trigger-name>' |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | const parsedTriggerConfig = parseTriggerConfig(triggerConfig); |
| 73 | if (Result.isFailure(parsedTriggerConfig)) { |
| 74 | return yield* invalidTriggerConfig(parsedTriggerConfig.failure); |
| 75 | } |
| 76 | |
| 77 | const project = yield* resolveDeveloperResourceProject; |
| 78 | // A defaulted user id must own the connected account on a 2FA project; `devActingUserOption` |
| 79 | // names it on stderr. |
| 80 | const acting = yield* devActingUserOption(project); |
| 81 | const resolvedUserId = Option.map(acting, found => found.userId); |
| 82 | |
| 83 | const created = yield* ui |
| 84 | .withSpinner( |
| 85 | `Creating trigger "${triggerName.value}"...`, |
| 86 | repo.createTrigger(project, triggerName.value, { |
| 87 | connected_account_id: Option.getOrUndefined(connectedAccountId), |
| 88 | trigger_config: parsedTriggerConfig.success, |
| 89 | user_id: Option.getOrUndefined(resolvedUserId), |
| 90 | }) |
| 91 | ) |
| 92 | .pipe( |
| 93 | Effect.catchTag( |
| 94 | CLIENT_API_ERROR_TAGS, |
| 95 | handleClientApiError(ui, { |
| 96 | fallbackMessage: `Failed to create trigger "${triggerName.value}".`, |
| 97 | hint: 'List available trigger types with:\n> composio dev triggers list', |
| 98 | }) |
| 99 | ) |
| 100 | ); |
| 101 | |
| 102 | yield* ui.log.success(`Trigger created: ${created.trigger_id}`); |
| 103 | yield* ui.log.step( |
| 104 | `To check status:\n> composio dev triggers status --trigger-ids "${created.trigger_id}"` |
| 105 | ); |
| 106 | yield* ui.output(JSON.stringify(created, null, 2)); |
| 107 | }); |