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 types |
| 2 | import typing as t |
| 3 | from inspect import Signature |
| 4 | |
| 5 | import pydantic |
| 6 | from google.adk.tools import FunctionTool |
| 7 | |
| 8 | from composio.client.types import Tool |
| 9 | from composio.core.provider import AgenticProvider |
| 10 | from composio.core.provider.agentic import AgenticProviderExecuteFn |
| 11 | from composio.utils.openapi import function_signature_from_jsonschema |
| 12 | from composio.utils.pydantic import parse_pydantic_error |
| 13 | from composio.utils.shared import ( |
| 14 | alias_tool_input_schema, |
| 15 | json_schema_to_model, |
| 16 | normalize_tool_arguments, |
| 17 | validate_and_serialize_tool_arguments, |
| 18 | ) |
| 19 | |
| 20 | if t.TYPE_CHECKING: |
| 21 | from google.adk.tools.tool_context import ToolContext |
| 22 | |
| 23 | |
| 24 | class _ComposioFunctionTool(FunctionTool): |
| 25 | """`FunctionTool` that hands the wrapped function every root argument. |
| 26 | |
| 27 | ADK keeps only the arguments named in the function signature, so root keys |
| 28 | the tool schema admits through `additionalProperties` or |
| 29 | `patternProperties` never reach the function. The function body validates |
| 30 | the full mapping against the tool schema instead. |
| 31 | |
| 32 | ADK reserves its context parameter and `input_stream` for objects it |
| 33 | injects. Those names are never forwarded: neither a value the model sent |
| 34 | under them nor ADK's own objects reach the tool backend. |
| 35 | """ |
| 36 | |
| 37 | def _prepare_invocation_args( |
| 38 | self, |
| 39 | args: t.Dict[str, t.Any], |
| 40 | tool_context: "ToolContext", |
| 41 | ) -> t.Dict[str, t.Any]: |
| 42 | prepared = super()._prepare_invocation_args(args, tool_context) |
| 43 | reserved = getattr(self, "_ignore_params", ("tool_context", "input_stream")) |
| 44 | return { |
| 45 | key: value |
| 46 | for key, value in {**args, **prepared}.items() |
| 47 | if key not in reserved |
| 48 | } |
| 49 | |
| 50 | |
| 51 | class GoogleAdkProvider( |
| 52 | AgenticProvider[FunctionTool, list[FunctionTool]], name="google_adk" |
| 53 | ): |
| 54 | """ |
| 55 | Composio toolset for Google ADK framework. |
| 56 | """ |
| 57 | |
| 58 | __schema_skip_defaults__ = True |
| 59 | |
| 60 | def wrap_tool( |
| 61 | self, |
| 62 | tool: Tool, |
| 63 | execute_tool: AgenticProviderExecuteFn, |
| 64 | ) -> FunctionTool: |
| 65 | """Wraps composio tool as Google Genai SDK compatible function calling object.""" |
| 66 | |
| 67 | input_parameters = t.cast( |
| 68 | t.Dict[str, t.Any], |
| 69 | tool.input_parameters |
| 70 | or { |
| 71 | "type": "object", |
| 72 | "properties": {}, |
| 73 | "required": [], |
| 74 | }, |
| 75 | ) |
| 76 | aliases = alias_tool_input_schema(schema=input_parameters) |
| 77 | properties = t.cast( |
| 78 | t.Dict[str, t.Dict[str, t.Any]], |
| 79 | aliases.schema.get("properties", {}), |
| 80 | ) |
| 81 | docstring = tool.description or f"Execute {tool.slug}" |
| 82 | docstring += "\nArgs:" |
| 83 | for _param, _schema in properties.items(): |
| 84 | docstring += "\n " |
| 85 | docstring += _param + ": " + _schema.get("description", _param.title()) |
| 86 | |
| 87 | docstring += "\nReturns:" |
| 88 | docstring += "\n A dictionary containing response from the action" |
| 89 | |
| 90 | # ADK treats every signature parameter as mandatory, so the model |
| 91 | # requires every declared field too. |
| 92 | args_schema = json_schema_to_model( |
| 93 | aliases.schema, skip_default=self.skip_default |
| 94 | ) |
| 95 | |
| 96 | def _execute(**kwargs: t.Any) -> t.Dict: |
| 97 | try: |
| 98 | arguments = validate_and_serialize_tool_arguments( |
| 99 | args_schema, normalize_tool_arguments(kwargs) |
| 100 | ) |
| 101 | except pydantic.ValidationError as e: |
| 102 | return { |
| 103 | "successful": False, |
| 104 | "error": parse_pydantic_error(e), |
| 105 | "data": None, |
| 106 | } |
| 107 | return execute_tool( |
| 108 | slug=tool.slug, arguments=aliases.restore_arguments(arguments) |
| 109 | ) |
| 110 | |
| 111 | function = types.FunctionType( |
| 112 | code=_execute.__code__, |
| 113 | name=tool.slug, |
| 114 | globals=globals(), |
| 115 | closure=_execute.__closure__, |
| 116 | ) |
| 117 | parameters = function_signature_from_jsonschema( |
| 118 | schema=aliases.schema, |
| 119 | skip_default=self.skip_default, |
| 120 | ) |
| 121 | setattr(function, "__signature__", Signature(parameters=parameters)) |
| 122 | setattr( |
| 123 | function, |
| 124 | "__annotations__", |
| 125 | {p.name: p.annotation for p in parameters} | {"return": dict}, |
| 126 | ) |
| 127 | function.__doc__ = docstring |
| 128 | return _ComposioFunctionTool(function) |
| 129 | |
| 130 | def wrap_tools( |
| 131 | self, |
| 132 | tools: t.Sequence[Tool], |
| 133 | execute_tool: AgenticProviderExecuteFn, |
| 134 | ) -> list[FunctionTool]: |
| 135 | """Get composio tools wrapped as Google Genai SDK compatible function calling object.""" |
| 136 | return [self.wrap_tool(tool, execute_tool) for tool in tools] |