|
| 1 | +import copy |
1 | 2 | from typing import Any
|
2 | 3 |
|
3 | 4 | from google.adk.tools import BaseTool, ToolContext
|
4 | 5 | from google.genai import types
|
5 | 6 |
|
6 | 7 | from thirdweb_ai.tools.tool import Tool, ToolSchema
|
7 | 8 |
|
8 |
| -from pydantic import BaseModel |
9 | 9 |
|
10 | 10 | class GoogleAdkTool(BaseTool):
|
11 | 11 | """Adapter for Thirdweb Tool to Google ADK BaseTool.
|
@@ -40,38 +40,34 @@ def _get_declaration(self) -> types.FunctionDeclaration:
|
40 | 40 | Returns:
|
41 | 41 | A FunctionDeclaration for Google ADK
|
42 | 42 | """
|
43 |
| - # Deep copy the parameters to avoid modifying the original |
44 |
| - import copy |
45 |
| - parameters = copy.deepcopy(self.tool.schema["parameters"]) |
46 |
| - |
47 |
| - if "additionalProperties" in parameters: |
48 |
| - del parameters["additionalProperties"] |
49 |
| - |
50 |
| - def remove_additional_properties(obj: dict[str, Any]): |
51 |
| - if "additionalProperties" in obj: |
52 |
| - del obj["additionalProperties"] |
53 |
| - |
54 |
| - if "items" in obj and isinstance(obj["items"], dict): |
55 |
| - remove_additional_properties(obj["items"]) |
56 |
| - |
57 |
| - if "properties" in obj and isinstance(obj["properties"], dict): |
58 |
| - for prop in obj["properties"].values(): |
59 |
| - if isinstance(prop, dict): |
60 |
| - remove_additional_properties(prop) |
61 |
| - |
62 |
| - if "properties" in parameters: |
63 |
| - for prop in parameters["properties"].values(): |
64 |
| - remove_additional_properties(prop) |
65 |
| - |
| 43 | + |
| 44 | + if "parameters" not in self.tool.schema: |
| 45 | + raise ValueError("Tool schema must contain 'parameters'.") |
| 46 | + |
| 47 | + # Create a clean parameters dict without additionalProperties |
| 48 | + parameters = copy.deepcopy(dict(self.tool.schema["parameters"])) |
| 49 | + |
| 50 | + # Remove additionalProperties recursively from the entire schema |
| 51 | + def clean_schema(obj: dict[str, Any]) -> dict[str, Any]: |
| 52 | + cleaned = {k: v for k, v in obj.items() if k != "additionalProperties"} |
| 53 | + |
| 54 | + for key, value in cleaned.items(): |
| 55 | + if isinstance(value, dict): |
| 56 | + cleaned[key] = clean_schema(value) |
| 57 | + elif isinstance(value, list): |
| 58 | + cleaned[key] = [clean_schema(item) if isinstance(item, dict) else item for item in value] |
| 59 | + |
| 60 | + return cleaned |
| 61 | + |
| 62 | + clean_parameters = clean_schema(parameters) |
| 63 | + |
66 | 64 | return types.FunctionDeclaration(
|
67 | 65 | name=self.name,
|
68 | 66 | description=self.description,
|
69 |
| - parameters=parameters, |
| 67 | + parameters=types.Schema(**clean_parameters), |
70 | 68 | )
|
71 | 69 |
|
72 |
| - # Override the method with the expected signature based on the error message |
73 |
| - # and adapting from the reference implementation |
74 |
| - async def run_async(self, args: dict[str, Any], tool_context: ToolContext) -> Any: |
| 70 | + async def run_async(self, *, args: dict[str, Any], tool_context: ToolContext) -> Any: |
75 | 71 | """Execute the tool asynchronously.
|
76 | 72 |
|
77 | 73 | This method adapts the Thirdweb tool to work with Google ADK's async execution.
|
|
0 commit comments