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 | """Tests for the OpenAI Agents provider.""" |
| 2 | |
| 3 | import asyncio |
| 4 | import copy |
| 5 | from unittest.mock import MagicMock |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from composio_openai_agents.provider import OpenAIAgentsProvider |
| 10 | |
| 11 | |
| 12 | def test_wrap_tool_does_not_mutate_input_parameters(): |
| 13 | """wrap_tool must not strip default/examples/pattern from the caller's schema. |
| 14 | |
| 15 | ``wrap_tool`` removes ``examples``/``pattern``/``default`` from the schema it |
| 16 | hands to the OpenAI Agents SDK. It must do so on a *copy*: a shallow |
| 17 | ``dict.copy()`` left the nested ``properties`` dicts shared with the original, |
| 18 | so those in-place deletions leaked back into the caller's |
| 19 | ``Tool.input_parameters``. |
| 20 | """ |
| 21 | tool = MagicMock( |
| 22 | slug="GITHUB_GET_REPO", |
| 23 | name="Github Get Repo", |
| 24 | description="Get a repository", |
| 25 | input_parameters={ |
| 26 | "type": "object", |
| 27 | "properties": { |
| 28 | "owner": {"type": "string", "default": "me", "examples": ["octocat"]}, |
| 29 | "repo": {"type": "string", "pattern": "^[a-z]+$"}, |
| 30 | }, |
| 31 | "required": ["owner"], |
| 32 | }, |
| 33 | ) |
| 34 | snapshot = copy.deepcopy(tool.input_parameters) |
| 35 | |
| 36 | wrapped_tool = OpenAIAgentsProvider().wrap_tool(tool, lambda **kwargs: {}) |
| 37 | |
| 38 | # The caller's schema must be untouched. |
| 39 | assert tool.input_parameters == snapshot |
| 40 | owner = tool.input_parameters["properties"]["owner"] |
| 41 | assert owner["default"] == "me" |
| 42 | assert owner["examples"] == ["octocat"] |
| 43 | assert tool.input_parameters["properties"]["repo"]["pattern"] == "^[a-z]+$" |
| 44 | |
| 45 | # The provider-local schema should still be normalized for OpenAI Agents. |
| 46 | assert wrapped_tool.params_json_schema == { |
| 47 | "type": "object", |
| 48 | "properties": { |
| 49 | "owner": {"type": "string"}, |
| 50 | "repo": {"type": "string"}, |
| 51 | }, |
| 52 | "required": ["owner"], |
| 53 | "additionalProperties": False, |
| 54 | } |
| 55 | |
| 56 | |
| 57 | @pytest.mark.parametrize( |
| 58 | "items_schema", |
| 59 | [ |
| 60 | { |
| 61 | "anyOf": [ |
| 62 | { |
| 63 | "type": "object", |
| 64 | "properties": {"id": {"type": "string"}}, |
| 65 | "required": ["id"], |
| 66 | }, |
| 67 | {"type": "null"}, |
| 68 | ] |
| 69 | }, |
| 70 | { |
| 71 | "oneOf": [ |
| 72 | {"type": "integer"}, |
| 73 | {"type": "object", "additionalProperties": True}, |
| 74 | ] |
| 75 | }, |
| 76 | {"$ref": "#/$defs/Entry"}, |
| 77 | { |
| 78 | "properties": {"name": {"type": "string"}}, |
| 79 | "required": ["name"], |
| 80 | }, |
| 81 | {}, |
| 82 | ], |
| 83 | ids=["nullable-any-of", "one-of", "ref", "object-keywords", "empty-schema"], |
| 84 | ) |
| 85 | def test_wrap_tool_preserves_array_item_schemas(items_schema): |
| 86 | """Valid item schemas must not be intersected with an invented string type.""" |
| 87 | tool = MagicMock( |
| 88 | slug="PROCESS_RECORDS", |
| 89 | description="Process records", |
| 90 | input_parameters={ |
| 91 | "type": "object", |
| 92 | "$defs": { |
| 93 | "Entry": { |
| 94 | "type": "object", |
| 95 | "properties": {"id": {"type": "string"}}, |
| 96 | } |
| 97 | }, |
| 98 | "properties": { |
| 99 | "records": { |
| 100 | "type": "array", |
| 101 | "items": copy.deepcopy(items_schema), |
| 102 | } |
| 103 | }, |
| 104 | "required": ["records"], |
| 105 | }, |
| 106 | ) |
| 107 | |
| 108 | wrapped_tool = OpenAIAgentsProvider().wrap_tool(tool, lambda **kwargs: {}) |
| 109 | |
| 110 | assert ( |
| 111 | wrapped_tool.params_json_schema["properties"]["records"]["items"] |
| 112 | == items_schema |
| 113 | ) |
| 114 | |
| 115 | |
| 116 | def test_strict_mode_registers_a_strict_schema_with_optional_params_nullable(): |
| 117 | """Optional properties become required-nullable under strict mode. |
| 118 | |
| 119 | Mirrors the TypeScript OpenAIAgentsProvider's own strict-mode test. |
| 120 | """ |
| 121 | tool = MagicMock( |
| 122 | slug="CONFIGURE", |
| 123 | description="Configure something", |
| 124 | input_parameters={ |
| 125 | "type": "object", |
| 126 | "properties": { |
| 127 | "input": {"type": "string"}, |
| 128 | "cfg": { |
| 129 | "type": "object", |
| 130 | "properties": { |
| 131 | "url": {"type": "string"}, |
| 132 | "note": {"type": "string"}, |
| 133 | }, |
| 134 | "required": ["url"], |
| 135 | }, |
| 136 | }, |
| 137 | "required": ["input"], |
| 138 | }, |
| 139 | ) |
| 140 | |
| 141 | wrapped_tool = OpenAIAgentsProvider(strict=True).wrap_tool( |
| 142 | tool, lambda **kwargs: {} |
| 143 | ) |
| 144 | |
| 145 | assert wrapped_tool.strict_json_schema is True |
| 146 | assert wrapped_tool.params_json_schema == { |
| 147 | "type": "object", |
| 148 | "properties": { |
| 149 | "input": {"type": "string"}, |
| 150 | "cfg": { |
| 151 | "type": ["object", "null"], |
| 152 | "properties": { |
| 153 | "url": {"type": "string"}, |
| 154 | "note": {"type": ["string", "null"]}, |
| 155 | }, |
| 156 | "required": ["url", "note"], |
| 157 | "additionalProperties": False, |
| 158 | }, |
| 159 | }, |
| 160 | "required": ["input", "cfg"], |
| 161 | "additionalProperties": False, |
| 162 | } |
| 163 | |
| 164 | |
| 165 | def test_strict_mode_optional_enum_param_can_still_be_omitted(): |
| 166 | """An optional enum is widened to accept null, so the model can leave it out.""" |
| 167 | tool = MagicMock( |
| 168 | slug="SORT", |
| 169 | description="Sort something", |
| 170 | input_parameters={ |
| 171 | "type": "object", |
| 172 | "properties": { |
| 173 | "query": {"type": "string"}, |
| 174 | "order": {"type": "string", "enum": ["asc", "desc"]}, |
| 175 | }, |
| 176 | "required": ["query"], |
| 177 | }, |
| 178 | ) |
| 179 | |
| 180 | wrapped_tool = OpenAIAgentsProvider(strict=True).wrap_tool( |
| 181 | tool, lambda **kwargs: {} |
| 182 | ) |
| 183 | |
| 184 | assert wrapped_tool.strict_json_schema is True |
| 185 | assert wrapped_tool.params_json_schema["properties"]["order"] == { |
| 186 | "anyOf": [{"type": "string", "enum": ["asc", "desc"]}, {"type": "null"}] |
| 187 | } |
| 188 | |
| 189 | |
| 190 | def test_strict_mode_falls_back_when_schema_cannot_be_expressed_strict(): |
| 191 | """A schema strict mode can't express (arbitrary-key object) is |
| 192 | registered without strict mode instead, using the existing non-strict |
| 193 | schema-building path. |
| 194 | """ |
| 195 | tool = MagicMock( |
| 196 | slug="SET_HEADERS", |
| 197 | description="Set request headers", |
| 198 | input_parameters={ |
| 199 | "type": "object", |
| 200 | "properties": { |
| 201 | "headers": { |
| 202 | "type": "object", |
| 203 | "additionalProperties": {"type": "string"}, |
| 204 | } |
| 205 | }, |
| 206 | "required": ["headers"], |
| 207 | }, |
| 208 | ) |
| 209 | |
| 210 | wrapped_tool = OpenAIAgentsProvider(strict=True).wrap_tool( |
| 211 | tool, lambda **kwargs: {} |
| 212 | ) |
| 213 | |
| 214 | assert wrapped_tool.strict_json_schema is False |
| 215 | assert wrapped_tool.params_json_schema == { |
| 216 | "type": "object", |
| 217 | "properties": { |
| 218 | "headers": {"type": "object", "additionalProperties": {"type": "string"}} |
| 219 | }, |
| 220 | "required": ["headers"], |
| 221 | "additionalProperties": False, |
| 222 | } |
| 223 | |
| 224 | |
| 225 | def test_strict_mode_omits_null_arguments_the_tool_schema_rejects(): |
| 226 | """Under strict mode, a null the tool's own schema doesn't accept is |
| 227 | treated as omitted before the tool actually executes. |
| 228 | """ |
| 229 | tool = MagicMock( |
| 230 | slug="CONFIGURE", |
| 231 | description="Configure something", |
| 232 | input_parameters={ |
| 233 | "type": "object", |
| 234 | "properties": { |
| 235 | "input": {"type": "string"}, |
| 236 | "label": {"type": "string"}, |
| 237 | "clearable": {"type": ["string", "null"]}, |
| 238 | }, |
| 239 | "required": ["input"], |
| 240 | }, |
| 241 | ) |
| 242 | execute_tool = MagicMock(return_value={}) |
| 243 | |
| 244 | wrapped_tool = OpenAIAgentsProvider(strict=True).wrap_tool(tool, execute_tool) |
| 245 | |
| 246 | asyncio.run( |
| 247 | wrapped_tool.on_invoke_tool( |
| 248 | MagicMock(), '{"input": "x", "label": null, "clearable": null}' |
| 249 | ) |
| 250 | ) |
| 251 | |
| 252 | execute_tool.assert_called_once_with( |
| 253 | slug="CONFIGURE", arguments={"input": "x", "clearable": None} |
| 254 | ) |
| 255 | |
| 256 | |
| 257 | def test_strict_defaults_to_false(): |
| 258 | """The default constructor keeps today's non-strict behavior.""" |
| 259 | tool = MagicMock( |
| 260 | slug="GITHUB_GET_REPO", |
| 261 | description="Get a repository", |
| 262 | input_parameters={"type": "object", "properties": {}, "required": []}, |
| 263 | ) |
| 264 | |
| 265 | wrapped_tool = OpenAIAgentsProvider().wrap_tool(tool, lambda **kwargs: {}) |
| 266 | |
| 267 | assert wrapped_tool.strict_json_schema is False |
| 268 | |
| 269 | |
| 270 | def _wrap_tool(input_parameters, strict=False): |
| 271 | tool = MagicMock( |
| 272 | slug="FREE_FORM", |
| 273 | description="Accept free-form arguments", |
| 274 | input_parameters=input_parameters, |
| 275 | ) |
| 276 | wrapped_tool = OpenAIAgentsProvider(strict=strict).wrap_tool( |
| 277 | tool, lambda **kwargs: {} |
| 278 | ) |
| 279 | return wrapped_tool |
| 280 | |
| 281 | |
| 282 | @pytest.mark.parametrize( |
| 283 | "additional_properties", |
| 284 | [True, {"type": "string"}, False], |
| 285 | ids=["true", "schema", "false"], |
| 286 | ) |
| 287 | def test_root_keeps_the_source_additional_properties(additional_properties): |
| 288 | """A root that sets ``additionalProperties`` is advertised with that value.""" |
| 289 | wrapped_tool = _wrap_tool( |
| 290 | { |
| 291 | "type": "object", |
| 292 | "properties": {"title": {"type": "string"}}, |
| 293 | "additionalProperties": additional_properties, |
| 294 | } |
| 295 | ) |
| 296 | |
| 297 | assert ( |
| 298 | wrapped_tool.params_json_schema["additionalProperties"] == additional_properties |
| 299 | ) |
| 300 | |
| 301 | |
| 302 | def test_root_without_properties_is_advertised_open(): |
| 303 | """A root that names no properties and omits ``additionalProperties`` is |
| 304 | open, so the model may send arbitrary keys. |
| 305 | """ |
| 306 | wrapped_tool = _wrap_tool({"type": "object"}) |
| 307 | |
| 308 | assert wrapped_tool.params_json_schema == { |
| 309 | "type": "object", |
| 310 | "additionalProperties": True, |
| 311 | } |
| 312 | |
| 313 | |
| 314 | def test_root_pattern_properties_survive(): |
| 315 | """Root ``patternProperties`` stay in the advertised schema alongside the |
| 316 | source's closed ``additionalProperties``. |
| 317 | """ |
| 318 | wrapped_tool = _wrap_tool( |
| 319 | { |
| 320 | "type": "object", |
| 321 | "patternProperties": {"^count_": {"type": "integer", "minimum": 1}}, |
| 322 | "additionalProperties": False, |
| 323 | } |
| 324 | ) |
| 325 | |
| 326 | assert wrapped_tool.params_json_schema == { |
| 327 | "type": "object", |
| 328 | "patternProperties": {"^count_": {"type": "integer", "minimum": 1}}, |
| 329 | "additionalProperties": False, |
| 330 | } |
| 331 | |
| 332 | |
| 333 | def test_strict_mode_free_form_root_falls_back_and_is_advertised_open(): |
| 334 | """Strict mode cannot express an open root, so it registers the tool |
| 335 | without strict mode and advertises the root open. |
| 336 | """ |
| 337 | wrapped_tool = _wrap_tool({"type": "object", "properties": {}}, strict=True) |
| 338 | |
| 339 | assert wrapped_tool.strict_json_schema is False |
| 340 | assert wrapped_tool.params_json_schema == { |
| 341 | "type": "object", |
| 342 | "properties": {}, |
| 343 | "additionalProperties": True, |
| 344 | } |