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 | """Tests for the Vertex AI Google provider.""" |
| 2 | |
| 3 | from types import SimpleNamespace |
| 4 | from typing import Any |
| 5 | from unittest.mock import Mock |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from composio.client.types import Tool |
| 10 | |
| 11 | GoogleProvider = pytest.importorskip("composio_google").GoogleProvider |
| 12 | GenerationResponse = pytest.importorskip( |
| 13 | "vertexai.generative_models" |
| 14 | ).GenerationResponse |
| 15 | |
| 16 | |
| 17 | def test_wrap_tool_dereferences_internal_refs() -> None: |
| 18 | """Referenced input properties must be expanded before Vertex translation.""" |
| 19 | tool = Tool.model_construct( |
| 20 | slug="TEST_REF", |
| 21 | description="test", |
| 22 | input_parameters={ |
| 23 | "type": "object", |
| 24 | "properties": {"message": {"$ref": "#/$defs/Message"}}, |
| 25 | "required": ["message"], |
| 26 | "$defs": { |
| 27 | "Message": { |
| 28 | "type": "object", |
| 29 | "properties": {"subject": {"type": "string"}}, |
| 30 | "required": ["subject"], |
| 31 | } |
| 32 | }, |
| 33 | }, |
| 34 | ) |
| 35 | |
| 36 | wrapped = GoogleProvider().wrap_tool(tool) |
| 37 | message_schema = wrapped.to_dict()["parameters"]["properties"]["message"] |
| 38 | |
| 39 | assert "ref" not in message_schema |
| 40 | assert message_schema["properties"]["subject"]["type"] == "STRING" |
| 41 | |
| 42 | |
| 43 | def test_wrap_tool_drops_keywords_vertex_rejects() -> None: |
| 44 | """Composio schema keywords outside the Vertex ``Schema`` must not raise.""" |
| 45 | tool = Tool.model_construct( |
| 46 | slug="TEST_KEYWORDS", |
| 47 | description="test", |
| 48 | input_parameters={ |
| 49 | "type": "object", |
| 50 | "properties": { |
| 51 | "query": { |
| 52 | "type": "string", |
| 53 | "examples": ["is:unread"], |
| 54 | "human_parameter_name": "Search query", |
| 55 | "human_parameter_description": "What to search for", |
| 56 | }, |
| 57 | "kind": {"type": "string", "const": "message"}, |
| 58 | "status": { |
| 59 | "type": ["string", "null"], |
| 60 | "enum": ["open", "closed", None], |
| 61 | }, |
| 62 | "page_token": { |
| 63 | "description": "Next page", |
| 64 | "anyOf": [ |
| 65 | {"type": "string", "examples": ["abc"]}, |
| 66 | {"type": "null"}, |
| 67 | ], |
| 68 | }, |
| 69 | "amount": { |
| 70 | "description": "Amount", |
| 71 | "anyOf": [ |
| 72 | {"type": "string"}, |
| 73 | {"anyOf": [{"type": "integer"}, {"type": "number"}]}, |
| 74 | ], |
| 75 | }, |
| 76 | "flag": { |
| 77 | "type": ["string", "null"], |
| 78 | "anyOf": [{"type": "string"}, {"type": "boolean"}], |
| 79 | }, |
| 80 | "shape": { |
| 81 | "type": "object", |
| 82 | "properties": {"kind": {"type": "string"}}, |
| 83 | "required": ["kind"], |
| 84 | "oneOf": [ |
| 85 | {"properties": {"r": {"type": "number"}}, "required": ["r"]}, |
| 86 | { |
| 87 | "properties": {"side": {"type": "number"}}, |
| 88 | "required": ["side"], |
| 89 | }, |
| 90 | ], |
| 91 | }, |
| 92 | "direction": {"type": "null"}, |
| 93 | "label": {"type": ["string", "null"]}, |
| 94 | "value": {"type": ["string", "integer"]}, |
| 95 | "id": { |
| 96 | "type": ["string", "integer"], |
| 97 | "anyOf": [ |
| 98 | {"type": ["string"], "enum": ["yes"]}, |
| 99 | {"type": "boolean"}, |
| 100 | ], |
| 101 | }, |
| 102 | "format": {"type": "integer", "enum": [0, 1], "exclusiveMinimum": -1}, |
| 103 | "target": { |
| 104 | "oneOf": [ |
| 105 | {"type": "object", "properties": {"id": {"type": "string"}}}, |
| 106 | {"type": "string"}, |
| 107 | ] |
| 108 | }, |
| 109 | # A property may be named like a schema keyword. |
| 110 | "description": {"type": "string", "file_uploadable": True}, |
| 111 | }, |
| 112 | "required": ["query"], |
| 113 | }, |
| 114 | ) |
| 115 | |
| 116 | parameters = GoogleProvider().wrap_tool(tool).to_dict()["parameters"] |
| 117 | properties = parameters["properties"] |
| 118 | |
| 119 | assert properties["query"] == {"type": "STRING"} |
| 120 | assert properties["kind"] == {"type": "STRING", "enum": ["message"]} |
| 121 | assert properties["status"] == { |
| 122 | "type": "STRING", |
| 123 | "nullable": True, |
| 124 | "enum": ["open", "closed"], |
| 125 | } |
| 126 | assert properties["page_token"] == { |
| 127 | "type": "STRING", |
| 128 | "description": "Next page", |
| 129 | "nullable": True, |
| 130 | } |
| 131 | assert properties["amount"] == { |
| 132 | "any_of": [ |
| 133 | {"type": "STRING", "description": "Amount"}, |
| 134 | {"type": "INTEGER", "description": "Amount"}, |
| 135 | {"type": "NUMBER", "description": "Amount"}, |
| 136 | ] |
| 137 | } |
| 138 | assert properties["flag"] == {"type": "STRING", "nullable": True} |
| 139 | shapes = properties["shape"]["any_of"] |
| 140 | assert [sorted(s["properties"]) for s in shapes] == [ |
| 141 | ["kind", "r"], |
| 142 | ["kind", "side"], |
| 143 | ] |
| 144 | assert [s["required"] for s in shapes] == [["kind", "r"], ["kind", "side"]] |
| 145 | assert properties["direction"] == {"nullable": True} |
| 146 | assert properties["label"] == {"type": "STRING", "nullable": True} |
| 147 | assert properties["value"]["any_of"] == [{"type": "STRING"}, {"type": "INTEGER"}] |
| 148 | assert properties["id"] == {"type": "STRING", "enum": ["yes"]} |
| 149 | assert properties["format"] == { |
| 150 | "type": "INTEGER", |
| 151 | "description": "Allowed values: 0, 1.", |
| 152 | } |
| 153 | assert len(properties["target"]["any_of"]) == 2 |
| 154 | assert properties["description"] == {"type": "STRING"} |
| 155 | |
| 156 | # Vertex rejects a schema that sets any other field next to any_of. |
| 157 | def any_of_stands_alone(node: Any) -> bool: |
| 158 | if isinstance(node, list): |
| 159 | return all(any_of_stands_alone(item) for item in node) |
| 160 | if not isinstance(node, dict): |
| 161 | return True |
| 162 | if "any_of" in node and len(node) > 1: |
| 163 | return False |
| 164 | return all(any_of_stands_alone(value) for value in node.values()) |
| 165 | |
| 166 | assert any_of_stands_alone(parameters) |
| 167 | |
| 168 | |
| 169 | def test_wrap_tool_accepts_boolean_schema_nodes() -> None: |
| 170 | """MCP toolkits emit ``items: true``, which Vertex has no field for.""" |
| 171 | tool = Tool.model_construct( |
| 172 | slug="TEST_BOOLEAN_NODE", |
| 173 | description="test", |
| 174 | input_parameters={ |
| 175 | "type": "object", |
| 176 | "properties": { |
| 177 | "params": {"type": ["null", "array"], "items": True}, |
| 178 | "options": {"type": "object", "additionalProperties": True}, |
| 179 | }, |
| 180 | }, |
| 181 | ) |
| 182 | |
| 183 | properties = GoogleProvider().wrap_tool(tool).to_dict()["parameters"]["properties"] |
| 184 | |
| 185 | assert properties["params"] == {"type": "ARRAY", "nullable": True, "items": {}} |
| 186 | assert properties["options"]["type"] == "OBJECT" |
| 187 | |
| 188 | |
| 189 | def _function_call_response() -> Any: |
| 190 | return GenerationResponse.from_dict( |
| 191 | { |
| 192 | "candidates": [ |
| 193 | { |
| 194 | "content": { |
| 195 | "role": "model", |
| 196 | "parts": [ |
| 197 | {"text": "Searching"}, |
| 198 | { |
| 199 | "function_call": { |
| 200 | "name": "COMPOSIO_SEARCH_TOOLS", |
| 201 | "args": {"query": "send an email"}, |
| 202 | } |
| 203 | }, |
| 204 | ], |
| 205 | } |
| 206 | } |
| 207 | ] |
| 208 | } |
| 209 | ) |
| 210 | |
| 211 | |
| 212 | def _session() -> Mock: |
| 213 | session = Mock() |
| 214 | session.execute.return_value = SimpleNamespace( |
| 215 | data={"results": []}, |
| 216 | error=None, |
| 217 | log_id="log-session", |
| 218 | result_type="completed", |
| 219 | ) |
| 220 | return session |
| 221 | |
| 222 | |
| 223 | def test_handle_response_routes_function_calls_through_session() -> None: |
| 224 | """Session tools execute through their Tool Router session, not tools.execute.""" |
| 225 | provider = GoogleProvider() |
| 226 | provider.execute_tool = Mock() |
| 227 | session = _session() |
| 228 | |
| 229 | results = provider.handle_response( |
| 230 | response=_function_call_response(), session=session |
| 231 | ) |
| 232 | |
| 233 | session.execute.assert_called_once_with( |
| 234 | tool_slug="COMPOSIO_SEARCH_TOOLS", arguments={"query": "send an email"} |
| 235 | ) |
| 236 | provider.execute_tool.assert_not_called() |
| 237 | assert results == [ |
| 238 | { |
| 239 | "data": {"results": []}, |
| 240 | "error": None, |
| 241 | "successful": True, |
| 242 | "log_id": "log-session", |
| 243 | "result_type": "completed", |
| 244 | } |
| 245 | ] |
| 246 | |
| 247 | |
| 248 | def test_handle_response_keeps_direct_user_id_execution() -> None: |
| 249 | """Existing positional user-ID calls keep the direct execution path.""" |
| 250 | provider = GoogleProvider() |
| 251 | provider.execute_tool = Mock( |
| 252 | return_value={"data": {}, "error": None, "successful": True} |
| 253 | ) |
| 254 | modifiers = [Mock()] |
| 255 | |
| 256 | results = provider.handle_response("user_123", _function_call_response(), modifiers) |
| 257 | |
| 258 | provider.execute_tool.assert_called_once_with( |
| 259 | slug="COMPOSIO_SEARCH_TOOLS", |
| 260 | arguments={"query": "send an email"}, |
| 261 | modifiers=modifiers, |
| 262 | user_id="user_123", |
| 263 | ) |
| 264 | assert results == [{"data": {}, "error": None, "successful": True}] |
| 265 | |
| 266 | |
| 267 | def test_execute_tool_call_routes_through_session() -> None: |
| 268 | provider = GoogleProvider() |
| 269 | provider.execute_tool = Mock() |
| 270 | session = _session() |
| 271 | function_call = SimpleNamespace(name="COMPOSIO_SEARCH_TOOLS", args={"query": "x"}) |
| 272 | |
| 273 | result = provider.execute_tool_call(session=session, function_call=function_call) |
| 274 | |
| 275 | session.execute.assert_called_once_with( |
| 276 | tool_slug="COMPOSIO_SEARCH_TOOLS", arguments={"query": "x"} |
| 277 | ) |
| 278 | provider.execute_tool.assert_not_called() |
| 279 | assert result == { |
| 280 | "data": {"results": []}, |
| 281 | "error": None, |
| 282 | "successful": True, |
| 283 | "log_id": "log-session", |
| 284 | "result_type": "completed", |
| 285 | } |
| 286 | |
| 287 | |
| 288 | def test_handle_response_rejects_modifiers_with_session() -> None: |
| 289 | provider = GoogleProvider() |
| 290 | session = _session() |
| 291 | |
| 292 | with pytest.raises(ValueError, match="cannot be used with a Tool Router session"): |
| 293 | provider.handle_response( |
| 294 | response=_function_call_response(), |
| 295 | session=session, |
| 296 | modifiers=[Mock()], |
| 297 | ) |
| 298 | session.execute.assert_not_called() |
| 299 | |
| 300 | |
| 301 | def test_execute_tool_call_requires_exactly_one_target() -> None: |
| 302 | provider = GoogleProvider() |
| 303 | function_call = SimpleNamespace(name="COMPOSIO_SEARCH_TOOLS", args={}) |
| 304 | |
| 305 | with pytest.raises(ValueError, match="exactly one of user_id or session"): |
| 306 | provider.execute_tool_call( |
| 307 | user_id="user_123", session=_session(), function_call=function_call |
| 308 | ) |