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 { actingUser } from 'src/commands/surface'; |
| 3 | import { APP_CONFIG } from 'src/config'; |
| 4 | import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug'; |
| 5 | import { resolveCliSessionArtifacts } from 'src/services/cli-session-artifacts'; |
| 6 | import { resolveCommandProject } from 'src/services/command-project'; |
| 7 | import { ComposioClientSingleton } from 'src/services/composio-clients'; |
| 8 | import type { ToolkitProjectScope } from 'src/services/composio-clients'; |
| 9 | import { resolveConnectedAccountForToolkit } from 'src/services/connected-account-selection'; |
| 10 | import { resolveConsumerExecutionTransport } from 'src/services/dashboard-tool-execution'; |
| 11 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 12 | import { getOrFetchToolInputDefinition } from 'src/services/tool-input-validation'; |
| 13 | import { ToolsExecutor } from 'src/services/tools-executor'; |
| 14 | import type { ToolExecuteParams } from 'src/services/tools-executor'; |
| 15 | import { injectSingleFileArgument, parseArguments, resolveInput } from './tools.execute.arguments'; |
| 16 | |
| 17 | export type RunToolsExecuteParams = { |
| 18 | slug: string; |
| 19 | data: Option.Option<string>; |
| 20 | file: Option.Option<string>; |
| 21 | account: Option.Option<string>; |
| 22 | projectName: Option.Option<string>; |
| 23 | surface: 'root' | 'dev'; |
| 24 | projectMode: 'consumer' | 'developer'; |
| 25 | getSchema: boolean; |
| 26 | dryRun: boolean; |
| 27 | skipConnectionCheck: boolean; |
| 28 | skipToolParamsCheck: boolean; |
| 29 | skipChecks: boolean; |
| 30 | }; |
| 31 | |
| 32 | export type SharedRunToolsExecuteParams = Omit<RunToolsExecuteParams, 'slug' | 'data' | 'file'>; |
| 33 | |
| 34 | export type ResolvedExecuteContext = { |
| 35 | readonly ui: TerminalUI; |
| 36 | readonly executor: ToolsExecutor; |
| 37 | readonly resolvedProject: { |
| 38 | readonly orgId: string; |
| 39 | readonly projectId: string; |
| 40 | readonly projectType: 'CONSUMER' | 'DEVELOPER'; |
| 41 | readonly consumerUserId?: string; |
| 42 | }; |
| 43 | readonly args: Record<string, unknown>; |
| 44 | readonly resolvedUserId: string; |
| 45 | readonly selectedConnectedAccountId?: string; |
| 46 | readonly executeParams: ToolExecuteParams; |
| 47 | readonly executeOutputDir?: string; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * The resolved project as toolkit resolution needs it: custom toolkits are |
| 52 | * listed per project, and it has to be the project execute runs against. |
| 53 | */ |
| 54 | export const toolkitProjectScope = (resolvedProject: ToolkitProjectScope): ToolkitProjectScope => ({ |
| 55 | orgId: resolvedProject.orgId, |
| 56 | projectId: resolvedProject.projectId, |
| 57 | }); |
| 58 | |
| 59 | type ResolvedSchemaContext = { |
| 60 | readonly ui: TerminalUI; |
| 61 | readonly resolvedProject: { |
| 62 | readonly orgId: string; |
| 63 | readonly projectId: string; |
| 64 | readonly projectType: 'CONSUMER' | 'DEVELOPER'; |
| 65 | readonly consumerUserId?: string; |
| 66 | }; |
| 67 | }; |
| 68 | |
| 69 | export const resolveExecuteContext = ( |
| 70 | params: RunToolsExecuteParams, |
| 71 | /** The acting user; `--parallel` resolves it once for all its tools. */ |
| 72 | user: ReturnType<typeof actingUser> = actingUser() |
| 73 | ) => |
| 74 | Effect.gen(function* () { |
| 75 | const ui = yield* TerminalUI; |
| 76 | const executor = yield* ToolsExecutor; |
| 77 | const input = yield* resolveInput(params.data); |
| 78 | const parsedArgs = yield* parseArguments(input); |
| 79 | const runOutputDirectory = yield* APP_CONFIG.RUN_OUTPUT_DIR; |
| 80 | |
| 81 | const { userId: resolvedUserId, project: resolvedProject } = yield* user; |
| 82 | const clientSingleton = yield* ComposioClientSingleton; |
| 83 | const client = yield* clientSingleton.getFor({ |
| 84 | orgId: resolvedProject.orgId, |
| 85 | projectId: resolvedProject.projectId, |
| 86 | }); |
| 87 | const toolkitSlug = yield* toolkitFromToolSlug( |
| 88 | params.slug, |
| 89 | toolkitProjectScope(resolvedProject) |
| 90 | ); |
| 91 | const selectedConnectedAccountId = yield* resolveConnectedAccountForToolkit({ |
| 92 | client, |
| 93 | toolkitSlug, |
| 94 | userId: resolvedUserId, |
| 95 | selector: params.account, |
| 96 | }); |
| 97 | const args = Option.isSome(params.file) |
| 98 | ? yield* getOrFetchToolInputDefinition(params.slug, { |
| 99 | orgId: resolvedProject.orgId, |
| 100 | projectId: resolvedProject.projectId, |
| 101 | }).pipe( |
| 102 | Effect.flatMap(definition => |
| 103 | injectSingleFileArgument({ |
| 104 | slug: params.slug, |
| 105 | args: parsedArgs, |
| 106 | filePath: Option.getOrThrow(params.file), |
| 107 | schema: definition.schema, |
| 108 | }) |
| 109 | ) |
| 110 | ) |
| 111 | : parsedArgs; |
| 112 | const executeOutputDir = |
| 113 | runOutputDirectory || |
| 114 | Option.getOrUndefined( |
| 115 | yield* resolveCliSessionArtifacts({ |
| 116 | orgId: resolvedProject.projectType === 'CONSUMER' ? resolvedProject.orgId : undefined, |
| 117 | consumerUserId: |
| 118 | resolvedProject.projectType === 'CONSUMER' ? resolvedProject.consumerUserId : undefined, |
| 119 | }).pipe(Effect.map(Option.map(artifacts => artifacts.directoryPath))) |
| 120 | ); |
| 121 | |
| 122 | // Root `execute` (the consumer surface, which is also what `composio run` |
| 123 | // spawns) goes through the Dashboard; `dev playground-execute` stays on the |
| 124 | // backend. The one exception is a backend URL overridden without a |
| 125 | // matching `COMPOSIO_WEB_URL`: the key is never sent to another |
| 126 | // deployment's Dashboard. |
| 127 | const executesViaDashboard = |
| 128 | params.surface === 'root' && (yield* resolveConsumerExecutionTransport) === 'dashboard'; |
| 129 | |
| 130 | return { |
| 131 | ui, |
| 132 | executor, |
| 133 | resolvedProject, |
| 134 | args, |
| 135 | resolvedUserId, |
| 136 | selectedConnectedAccountId, |
| 137 | executeOutputDir, |
| 138 | executeParams: { |
| 139 | userId: resolvedUserId, |
| 140 | target: executesViaDashboard |
| 141 | ? { kind: 'dashboard', orgId: resolvedProject.orgId } |
| 142 | : { kind: 'backend' }, |
| 143 | arguments: args, |
| 144 | client, |
| 145 | projectScope: { |
| 146 | orgId: resolvedProject.orgId, |
| 147 | projectId: resolvedProject.projectId, |
| 148 | }, |
| 149 | connectedAccounts: |
| 150 | toolkitSlug && selectedConnectedAccountId |
| 151 | ? { |
| 152 | [toolkitSlug]: selectedConnectedAccountId, |
| 153 | } |
| 154 | : undefined, |
| 155 | cacheScope: |
| 156 | resolvedProject.projectType === 'CONSUMER' && resolvedProject.consumerUserId |
| 157 | ? { |
| 158 | orgId: resolvedProject.orgId, |
| 159 | projectId: resolvedProject.projectId, |
| 160 | consumerUserId: resolvedProject.consumerUserId, |
| 161 | } |
| 162 | : undefined, |
| 163 | }, |
| 164 | } satisfies ResolvedExecuteContext; |
| 165 | }); |
| 166 | |
| 167 | export const resolveSchemaContext = (params: SharedRunToolsExecuteParams) => |
| 168 | Effect.gen(function* () { |
| 169 | const resolvedProject = yield* resolveCommandProject({ |
| 170 | mode: params.projectMode, |
| 171 | projectName: |
| 172 | params.surface === 'root' ? undefined : Option.getOrUndefined(params.projectName), |
| 173 | }); |
| 174 | const ui = yield* TerminalUI; |
| 175 | |
| 176 | return { |
| 177 | ui, |
| 178 | resolvedProject, |
| 179 | } satisfies ResolvedSchemaContext; |
| 180 | }); |