Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """ |
| 2 | BaseProvider module |
| 3 | |
| 4 | Defines the barebones provider metaclass that needs to be subclassed for every provider. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import typing as t |
| 10 | |
| 11 | import typing_extensions as te |
| 12 | |
| 13 | if t.TYPE_CHECKING: |
| 14 | from composio.core.models.tools import Modifiers, ToolExecutionResponse |
| 15 | |
| 16 | TTool = t.TypeVar("TTool") |
| 17 | TToolCollection = t.TypeVar("TToolCollection") |
| 18 | |
| 19 | |
| 20 | class ExecuteToolFn(t.Protocol): |
| 21 | def __call__( |
| 22 | self, |
| 23 | slug: str, |
| 24 | arguments: t.Dict, |
| 25 | *, |
| 26 | modifiers: t.Optional[Modifiers] = None, |
| 27 | user_id: t.Optional[str] = None, |
| 28 | ) -> ToolExecutionResponse: |
| 29 | """ |
| 30 | Execute a wrapped tool by slug, passing an arbitrary input dict. |
| 31 | This function is used by the providers to execute tools for the helper methods. |
| 32 | Returns a dict with the following keys: |
| 33 | - data: The data returned by the tool. |
| 34 | - error: The error returned by the tool. |
| 35 | - successful: Whether the tool was successful. |
| 36 | """ |
| 37 | ... |
| 38 | |
| 39 | |
| 40 | class ToolCallSession(t.Protocol): |
| 41 | """Execution contract implemented by ToolRouterSession.""" |
| 42 | |
| 43 | def execute( |
| 44 | self, |
| 45 | tool_slug: str, |
| 46 | *, |
| 47 | arguments: t.Optional[t.Dict[str, t.Any]] = None, |
| 48 | ) -> t.Any: ... |
| 49 | |
| 50 | |
| 51 | ToolCallExecutionTarget: t.TypeAlias = t.Union[str, ToolCallSession] |
| 52 | |
| 53 | |
| 54 | class SchemaConfig(te.TypedDict): |
| 55 | skip_defaults: te.NotRequired[bool] |
| 56 | |
| 57 | |
| 58 | class BaseProviderConfig(te.TypedDict): |
| 59 | schema_config: te.NotRequired[SchemaConfig] |
| 60 | |
| 61 | |
| 62 | class BaseProvider(t.Generic[TTool, TToolCollection]): |
| 63 | """ |
| 64 | BaseProvider class |
| 65 | |
| 66 | All providers should inherit from this class and implement `wrap_tools` so that |
| 67 | they can be used with the core Composio class. |
| 68 | """ |
| 69 | |
| 70 | name: str |
| 71 | """Name of the provider""" |
| 72 | |
| 73 | __schema_skip_defaults__ = False |
| 74 | |
| 75 | execute_tool: ExecuteToolFn |
| 76 | """ |
| 77 | The function to execute a tool for the provider's helper methods. |
| 78 | This is automatically injected by the core SDK. |
| 79 | """ |
| 80 | |
| 81 | def __init__(self, **kwargs: te.Unpack[BaseProviderConfig]) -> None: |
| 82 | self.skip_default = kwargs.get("schema_config", {}).get( |
| 83 | "skip_defaults", self.__schema_skip_defaults__ |
| 84 | ) |
| 85 | |
| 86 | def set_execute_tool_fn(self, execute_tool_fn: ExecuteToolFn) -> None: |
| 87 | self.execute_tool = execute_tool_fn |
| 88 | |
| 89 | def resolve_tool_call_execution_target( |
| 90 | self, |
| 91 | *, |
| 92 | user_id: t.Optional[str], |
| 93 | session: t.Optional[ToolCallSession], |
| 94 | ) -> ToolCallExecutionTarget: |
| 95 | """Resolve exactly one direct user or Tool Router session target.""" |
| 96 | if (user_id is None) == (session is None): |
| 97 | raise ValueError("Provide exactly one of user_id or session") |
| 98 | if session is not None: |
| 99 | return session |
| 100 | return t.cast(str, user_id) |
| 101 | |
| 102 | def execute_tool_for_target( |
| 103 | self, |
| 104 | *, |
| 105 | target: ToolCallExecutionTarget, |
| 106 | slug: str, |
| 107 | arguments: t.Dict[str, t.Any], |
| 108 | modifiers: t.Optional[Modifiers] = None, |
| 109 | ) -> ToolExecutionResponse: |
| 110 | """Execute normalized arguments through the matching SDK boundary.""" |
| 111 | if isinstance(target, str): |
| 112 | return self.execute_tool( |
| 113 | slug=slug, |
| 114 | arguments=arguments, |
| 115 | modifiers=modifiers, |
| 116 | user_id=target, |
| 117 | ) |
| 118 | |
| 119 | if modifiers is not None: |
| 120 | raise ValueError( |
| 121 | "Direct execution modifiers cannot be used with a Tool Router session" |
| 122 | ) |
| 123 | |
| 124 | # Imported here: the tools module imports this one. |
| 125 | from composio.core.models.tools import tool_execution_response_from_session |
| 126 | |
| 127 | result = target.execute(tool_slug=slug, arguments=arguments) |
| 128 | return tool_execution_response_from_session(result) |