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 { ComposioGlobalExecuteToolFnNotSetError } from '../errors/ToolErrors'; |
| 2 | import { ExecuteToolModifiers } from '../types/modifiers.types'; |
| 3 | import type { Tool, ToolExecuteParams, ToolExecuteResponse } from '../types/tool.types'; |
| 4 | import { |
| 5 | ExecuteToolFn, |
| 6 | ExecuteToolFnOptions, |
| 7 | GlobalExecuteToolFn, |
| 8 | ToolCallExecutionTarget, |
| 9 | } from '../types/provider.types'; |
| 10 | import { McpUrlResponse, McpServerGetResponse } from '../types/mcp.types'; |
| 11 | import { isExecutionSuccessful } from '../utils/transformers/toolRouterResponseTransform'; |
| 12 | |
| 13 | /** |
| 14 | * @internal |
| 15 | * Base class for all providers. |
| 16 | * This class is not meant to be used directly, but rather to be extended by different provider implementations. |
| 17 | */ |
| 18 | abstract class BaseProvider<TMcpResponse> { |
| 19 | /** |
| 20 | * @public |
| 21 | * The name of the provider. |
| 22 | * Used to identify the provider in the telemetry. |
| 23 | */ |
| 24 | abstract readonly name: string; |
| 25 | /** |
| 26 | * @internal |
| 27 | * Whether the provider is agentic. |
| 28 | * This is set automatically set by the core SDK implementation for different provider types. |
| 29 | */ |
| 30 | abstract readonly _isAgentic: boolean; |
| 31 | /** |
| 32 | * @internal |
| 33 | * The function to execute a tool. |
| 34 | * This is set automatically injected by the core SDK. |
| 35 | */ |
| 36 | private _globalExecuteToolFn!: GlobalExecuteToolFn; |
| 37 | /** |
| 38 | * @internal |
| 39 | * Set the function to execute a tool. |
| 40 | * This is set automatically and injected by the core SDK. |
| 41 | */ |
| 42 | _setExecuteToolFn(executeToolFn: GlobalExecuteToolFn): void { |
| 43 | this._globalExecuteToolFn = executeToolFn; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @public |
| 48 | * Global function to execute a tool. |
| 49 | * This function is used by providers to implement helper functions to execute tools. |
| 50 | * This is a 1:1 mapping of the `execute` method in the `Tools` class. |
| 51 | * @param {string} toolSlug - The slug of the tool to execute. |
| 52 | * @param {ToolExecuteParams} body - The body of the tool execution. |
| 53 | * @param {ExecuteToolModifiers} modifers - The modifiers of the tool execution. |
| 54 | * @returns {Promise<string>} The result of the tool execution. |
| 55 | */ |
| 56 | executeTool( |
| 57 | toolSlug: string, |
| 58 | body: ToolExecuteParams, |
| 59 | modifers?: ExecuteToolModifiers |
| 60 | ): Promise<ToolExecuteResponse> { |
| 61 | if (!this._globalExecuteToolFn) { |
| 62 | throw new ComposioGlobalExecuteToolFnNotSetError('executeToolFn is not set'); |
| 63 | } |
| 64 | |
| 65 | // For provider controlled execution, always skip version check. |
| 66 | return this._globalExecuteToolFn(toolSlug, body, modifers); |
| 67 | } |
| 68 | |
| 69 | /** Reject direct-only configuration when execution is bound to a session. */ |
| 70 | protected assertToolCallExecutionOptions( |
| 71 | target: ToolCallExecutionTarget, |
| 72 | options?: ExecuteToolFnOptions, |
| 73 | modifiers?: ExecuteToolModifiers |
| 74 | ): void { |
| 75 | if (typeof target !== 'string' && (options !== undefined || modifiers !== undefined)) { |
| 76 | throw new TypeError( |
| 77 | 'Direct execution options and modifiers cannot be used with a Tool Router session' |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Execute normalized provider arguments against either the direct tools API or |
| 84 | * the Tool Router session that produced the model-visible tools. |
| 85 | */ |
| 86 | protected async executeToolForTarget( |
| 87 | target: ToolCallExecutionTarget, |
| 88 | toolSlug: string, |
| 89 | arguments_: Record<string, unknown>, |
| 90 | options?: ExecuteToolFnOptions, |
| 91 | modifiers?: ExecuteToolModifiers |
| 92 | ): Promise<ToolExecuteResponse> { |
| 93 | this.assertToolCallExecutionOptions(target, options, modifiers); |
| 94 | |
| 95 | if (typeof target === 'string') { |
| 96 | return this.executeTool( |
| 97 | toolSlug, |
| 98 | { |
| 99 | arguments: arguments_, |
| 100 | connectedAccountId: options?.connectedAccountId, |
| 101 | customAuthParams: options?.customAuthParams, |
| 102 | customConnectionData: options?.customConnectionData, |
| 103 | userId: target, |
| 104 | }, |
| 105 | modifiers |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | const result = await target.execute(toolSlug, arguments_); |
| 110 | return { |
| 111 | ...result, |
| 112 | successful: isExecutionSuccessful(result), |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @public |
| 118 | * @deprecated: Will be removed in a future version, once the `experimental.mcp` flag is stabilized. Use `wrapMcpServers` instead. |
| 119 | * Optional method to transform MCP URL response into provider-specific format. |
| 120 | * Providers can override this method to define custom transformation logic |
| 121 | * for MCP server responses. |
| 122 | * |
| 123 | * @param data - The MCP URL response data |
| 124 | |
| 125 | * @returns Transformed response in provider-specific format, or undefined to use default transformation |
| 126 | */ |
| 127 | wrapMcpServerResponse?(data: McpUrlResponse): TMcpResponse; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @public |
| 132 | * Base class for all non-agentic providers. |
| 133 | * This class is not meant to be used directly, but rather to be extended by concrete provider implementations. |
| 134 | */ |
| 135 | export abstract class BaseNonAgenticProvider< |
| 136 | TToolCollection, |
| 137 | TTool, |
| 138 | TMcpResponse = McpServerGetResponse, |
| 139 | > extends BaseProvider<TMcpResponse> { |
| 140 | override readonly _isAgentic = false; |
| 141 | |
| 142 | /** |
| 143 | * Wrap a tool in the provider specific format. |
| 144 | * @param tool - The tool to wrap. |
| 145 | * @returns The wrapped tool. |
| 146 | */ |
| 147 | abstract wrapTool(tool: Tool): TTool; |
| 148 | /** |
| 149 | * Wrap a list of tools in the provider specific format. |
| 150 | * @param tools - The tools to wrap. |
| 151 | * @returns The wrapped tools. |
| 152 | */ |
| 153 | abstract wrapTools(tools: Tool[]): TToolCollection; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @public |
| 158 | * Base class for all agentic providers. |
| 159 | * This class is not meant to be used directly, but rather to be extended by concrete provider implementations. |
| 160 | */ |
| 161 | export abstract class BaseAgenticProvider< |
| 162 | TToolCollection, |
| 163 | TTool, |
| 164 | TMcpResponse, |
| 165 | > extends BaseProvider<TMcpResponse> { |
| 166 | override readonly _isAgentic = true; |
| 167 | |
| 168 | /** |
| 169 | * Wrap a tool in the provider specific format. |
| 170 | * @param tool - The tool to wrap. |
| 171 | * @param executeTool - The function to execute the tool. |
| 172 | * @returns The wrapped tool. |
| 173 | */ |
| 174 | abstract wrapTool(tool: Tool, executeTool: ExecuteToolFn): TTool; |
| 175 | /** |
| 176 | * Wrap a list of tools in the provider specific format. |
| 177 | * @param tools - The tools to wrap. |
| 178 | * @param executeTool - The function to execute the tool. |
| 179 | * @returns The wrapped tools. |
| 180 | */ |
| 181 | abstract wrapTools(tools: Tool[], executeTool: ExecuteToolFn): TToolCollection; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @internal |
| 186 | * Base type for all providers. |
| 187 | * This type is used to infer the type of the provider from the provider implementation. |
| 188 | */ |
| 189 | export type BaseComposioProvider<TToolCollection, TTool, TMcpResponse = McpServerGetResponse> = |
| 190 | | BaseNonAgenticProvider<TToolCollection, TTool, TMcpResponse> |
| 191 | | BaseAgenticProvider<TToolCollection, TTool, TMcpResponse>; |