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 | from __future__ import annotations |
| 2 | |
| 3 | import typing as t |
| 4 | |
| 5 | from composio.client.types import Tool |
| 6 | from composio.core.provider.base import ( |
| 7 | BaseProvider, |
| 8 | ToolCallExecutionTarget, |
| 9 | TTool, |
| 10 | TToolCollection, |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | class AgenticProviderExecuteFn(t.Protocol): |
| 15 | def __call__( |
| 16 | self, |
| 17 | slug: str, |
| 18 | arguments: t.Dict, |
| 19 | ) -> t.Dict: |
| 20 | """ |
| 21 | Execute a wrapped tool by slug, passing an arbitrary input dict. |
| 22 | Returns a dict with the following keys: |
| 23 | - data: The data returned by the tool. |
| 24 | - error: The error returned by the tool. |
| 25 | - successful: Whether the tool was successful. |
| 26 | """ |
| 27 | ... |
| 28 | |
| 29 | |
| 30 | _EXECUTION_TARGET_ATTR = "__composio_execution_target__" |
| 31 | |
| 32 | TExecuteFn = t.TypeVar("TExecuteFn", bound=t.Callable[..., t.Any]) |
| 33 | |
| 34 | |
| 35 | def bind_execution_target( |
| 36 | execute_tool: TExecuteFn, |
| 37 | target: ToolCallExecutionTarget, |
| 38 | ) -> TExecuteFn: |
| 39 | """Record the user ID or Tool Router session ``execute_tool`` runs as. |
| 40 | |
| 41 | A provider that executes calls outside the wrapped tools, such as Gemini's |
| 42 | ``handle_response``, reads it back with :func:`get_execution_target` to |
| 43 | pick the executor that belongs to a caller's ``user_id`` or ``session``. |
| 44 | """ |
| 45 | setattr(execute_tool, _EXECUTION_TARGET_ATTR, target) |
| 46 | return execute_tool |
| 47 | |
| 48 | |
| 49 | def get_execution_target( |
| 50 | execute_tool: t.Callable[..., t.Any], |
| 51 | ) -> t.Optional[ToolCallExecutionTarget]: |
| 52 | """Return the target recorded by :func:`bind_execution_target`, if any.""" |
| 53 | return getattr(execute_tool, _EXECUTION_TARGET_ATTR, None) |
| 54 | |
| 55 | |
| 56 | class AgenticProvider(BaseProvider[TTool, TToolCollection]): |
| 57 | """ |
| 58 | Base class for all agentic providers. This class is not meant to be used |
| 59 | directly but rather to be extended by concrete provider implementations. |
| 60 | """ |
| 61 | |
| 62 | def __init_subclass__(cls, name: str) -> None: |
| 63 | cls.name = name |
| 64 | |
| 65 | def wrap_tool( |
| 66 | self, |
| 67 | tool: Tool, |
| 68 | execute_tool: AgenticProviderExecuteFn, |
| 69 | ) -> TTool: |
| 70 | """Wrap a tool in the provider-specific format""" |
| 71 | raise NotImplementedError |
| 72 | |
| 73 | def wrap_tools( |
| 74 | self, |
| 75 | tools: t.Sequence[Tool], |
| 76 | execute_tool: AgenticProviderExecuteFn, |
| 77 | ) -> TToolCollection: |
| 78 | """Wrap a list of tools in the provider-specific format""" |
| 79 | raise NotImplementedError |