188
tool_router.create(user_id="user_123", instant=policy)
189
kwargs = mock_client.tool_router.session.create.call_args.kwargs
190
assert kwargs["instant"] == policy
191
- assert "premium_usage" not in kwargs
191
192
mock_client.tool_router.session.create.reset_mock()
193
tool_router.create(user_id="user_123")
1410
mock_execute_response = MagicMock()
1411
mock_execute_response.data = {"ok": True}
1412
mock_execute_response.error = None
1413
+ mock_execute_response.result_type = "completed"
1414
mock_execute_response.log_id = "log_123"
1415
mock_client.tool_router.session.execute.return_value = mock_execute_response
1416
2020
mock_execute_response = MagicMock()
2021
mock_execute_response.data = {"result": "success"}
2022
mock_execute_response.error = None
2023
+ mock_execute_response.result_type = "completed"
2024
mock_client.tool_router.session.execute.return_value = mock_execute_response
2025
2026
# Create a real Tools instance to test the execute function
2067
mock_client.tool_router.session.execute.return_value = (
2068
SessionExecuteResponse.model_validate(
2069
{
2070
+ "result_type": "completed",
2071
"data": {"result": "success"},
2072
"error": None,
2073
"log_id": "log_123",
2095
mock_execute_response = MagicMock()
2096
mock_execute_response.data = {"result": "success"}
2097
mock_execute_response.error = None
2098
+ mock_execute_response.result_type = "completed"
2099
mock_client.tool_router.session.execute.return_value = mock_execute_response
2100
2101
from composio.core.models.tools import Tools as RealTools
2137
mock_execute_response = MagicMock()
2138
mock_execute_response.data = {"result": "success"}
2139
mock_execute_response.error = None
2140
+ mock_execute_response.result_type = "completed"
2141
mock_client.tool_router.session.execute.return_value = mock_execute_response
2142
2143
# Create modifier functions
2191
mock_execute_response = MagicMock()
2192
mock_execute_response.data = {}
2193
mock_execute_response.error = "Authentication failed"
2194
+ mock_execute_response.result_type = "failed"
2195
mock_client.tool_router.session.execute.return_value = mock_execute_response
2196
2197
# Create a real execute function
2333
router = ToolRouter(client=client, provider=MagicMock())
2334
session = router.create(user_id="user_123", instant=policy)
2335
body = json.loads(requests[0].content)
2331
- assert "premium_usage" not in body
2336
if policy is None:
2337
assert "instant" not in body
2338
else:
2339
assert body["instant"] == policy
2340
assert not isinstance(session.config.instant, bool)
2341
assert session.config.instant.return_instant_charge is True
2338
- assert "premium_usage" not in session.config.model_dump()
2342
2343
if policy is None:
2344
session.update(expected_config_version=3)
2346
session.update(instant=policy, expected_config_version=3)
2347
body = json.loads(requests[-1].content)
2348
assert body["expected_config_version"] == 3
2346
- assert "premium_usage" not in body
2349
if policy is None:
2350
assert "instant" not in body
2351
else:
2352
assert body["instant"] == policy
2353
assert not isinstance(session.config.instant, bool)
2354
assert session.config.instant.return_instant_charge is True
2353
- assert "premium_usage" not in session.config.model_dump()
2355
2356
attached = router.use(session_id="session_123")
2357
assert not isinstance(attached.config.instant, bool)
2367
return httpx.Response(
2368
200,
2369
json={
2370
+ "result_type": "completed",
2371
"data": {},
2372
"error": None,
2373
"log_id": "log_123",
2392
assert result.instant_charge is not None
2393
assert result.instant_charge.amount == "0.012"
2394
assert result.instant_charge.model_dump() == charge
2393
- assert "premium_charge" not in result.model_dump()
2395
status = session.search(query="search").toolkit_connection_statuses[0]
2396
assert status.instant_account is not None
2397
assert status.instant_account.allowed_tool_slugs == ["EXA_SEARCH"]
2711
assert result.result_type == "failed"
2712
2713
2714
+class TestMixedMultiExecuteRequiresUserInput:
2715
+ """A ``COMPOSIO_MULTI_EXECUTE_TOOL`` batch that mixes local and remote
2716
+ tools turns a raised backend error into per-tool failures. An input request
2717
+ is not one: the caller needs the questions and the request state.
2718
+ """
2719
+
2720
+ def test_remote_half_input_required_raises(self):
2721
+ from composio.core.models.tools import Tools
2722
+
2723
+ client, requests = _answering_client(_INPUT_REQUIRED_JSON)
2724
+ local_calls: t.List[bool] = []
2725
+ session = _session_with_custom_tools(
2726
+ client, _custom_tool_calling(lambda ctx: local_calls.append(True))
2727
+ )
2728
+ tools = Tools(
2729
+ client=client,
2730
+ provider=MagicMock(),
2731
+ dangerously_allow_auto_upload_download_files=False,
2732
+ )
2733
+ # Keep the (read) tool-schema lookup off the transport.
2734
+ tools._tool_schemas["COMPOSIO_MULTI_EXECUTE_TOOL"] = MagicMock()
2735
+
2736
+ with pytest.raises(ToolInputRequiredError) as raised:
2737
+ session._route_multi_execute(
2738
+ {
2739
+ "tools": [
2740
+ {"tool_slug": "SEND_WELCOME_EMAIL", "arguments": {}},
2741
+ {"tool_slug": "GMAIL_SEND_EMAIL", "arguments": {}},
2742
+ ]
2743
+ },
2744
+ tools,
2745
+ )
2746
+
2747
+ error = raised.value
2748
+ assert "Tool COMPOSIO_MULTI_EXECUTE_TOOL requires user input" in error.message
2749
+ assert list(error.input_requests) == ["approval_1"]
2750
+ assert error.request_state == "opaque-state-token"
2751
+ # Only the remote half reached the API, once.
2752
+ assert len(requests) == 1
2753
+ assert [
2754
+ tool["tool_slug"]
2755
+ for tool in json.loads(requests[0].content)["arguments"]["tools"]
2756
+ ] == ["GMAIL_SEND_EMAIL"]
2757
+ # The local tool had already run; its result is discarded by the raise.
2758
+ assert local_calls == [True]
2759
+
2760
+
2761
_EXECUTED_ANSWERS = [
2762
pytest.param("failed", None, False, id="failed with a null error"),
2763
pytest.param("failed", "", False, id="failed with an empty error"),
2764
pytest.param("failed", "Boom", False, id="failed with a message"),
2765
pytest.param("completed", None, True, id="completed"),
2718
- pytest.param(None, None, True, id="no result_type and no error"),
2719
- pytest.param(None, "Boom", False, id="no result_type and an error"),
2766
]
2767
2768
2723
-def _executed_json(
2724
- result_type: t.Optional[str], error: t.Optional[str]
2725
-) -> t.Dict[str, t.Any]:
2726
- body: t.Dict[str, t.Any] = {"data": {}, "error": error, "log_id": "log"}
2727
- if result_type is not None:
2728
- body["result_type"] = result_type
2729
- return body
2769
+def _executed_json(result_type: str, error: t.Optional[str]) -> t.Dict[str, t.Any]:
2770
+ return {"result_type": result_type, "data": {}, "error": error, "log_id": "log"}
2771
2772
2773
class TestSessionExecutionSuccess:
2774
"""``result_type`` says whether a tool that ran succeeded. A failed
2775
execution can carry a ``None`` or empty ``error``, so success is read from
2735
- ``result_type`` and only falls back to the error text when the API sent no
2736
- ``result_type``.
2776
+ ``result_type`` alone.
2777
"""
2778
2779
@pytest.mark.parametrize(("result_type", "error", "successful"), _EXECUTED_ANSWERS)
2822
assert result.error == error
2823
2824
2825
+_INVALID_ANSWERS = [
2826
+ pytest.param({"data": {}, "error": None, "log_id": "log"}, id="no result_type"),
2827
+ pytest.param(
2828
+ {"result_type": "deferred", "data": {}, "error": None, "log_id": "log"},
2829
+ id="an unknown result_type",
2830
+ ),
2831
+]
2832
+
2833
+
2834
+@pytest.mark.parametrize("body", _INVALID_ANSWERS)
2835
+class TestSessionExecuteAnswerWithoutAKnownResultType:
2836
+ """Every session execute answer carries a ``result_type``. One without it,
2837
+ or with a value this SDK does not know, is not a result: reading it as a
2838
+ success or as a failure would be a guess, so each path raises instead.
2839
+ """
2840
+
2841
+ _MESSAGE = (
2842
+ "Tool GMAIL_SEND_EMAIL returned an execute response without a known result_type"
2843
+ )
2844
+
2845
+ def test_provider_wrapped_session_tool_raises(self, body):
2846
+ client, requests = _answering_client(body)
2847
+
2848
+ with pytest.raises(ValidationError, match=self._MESSAGE):
2849
+ _session_tool_execute_fn(client)("GMAIL_SEND_EMAIL", {})
2850
+
2851
+ assert len(requests) == 1
2852
+
2853
+ def test_provider_tool_call_bound_to_a_session_raises(self, body):
2854
+ from composio.core.provider._openai import OpenAIProvider
2855
+
2856
+ client, _ = _answering_client(body)
2857
+
2858
+ with pytest.raises(ValidationError, match=self._MESSAGE):
2859
+ OpenAIProvider().execute_tool_for_target(
2860
+ target=_created_session(client), slug="GMAIL_SEND_EMAIL", arguments={}
2861
+ )
2862
+
2863
+ def test_session_execute_raises(self, body):
2864
+ client, _ = _answering_client(body)
2865
+
2866
+ with pytest.raises(ValidationError, match=self._MESSAGE):
2867
+ _created_session(client).execute("GMAIL_SEND_EMAIL")
2868
+
2869
+ def test_custom_tool_context_execute_raises(self, body):
2870
+ client, _ = _answering_client(body)
2871
+
2872
+ with pytest.raises(ValidationError, match=self._MESSAGE):
2873
+ SessionContextImpl(client, "user_123", "session_123").execute(
2874
+ "GMAIL_SEND_EMAIL", {}
2875
+ )
2876
+
2877
+
2878
def _session_tool_execute_fn(client: HttpClient) -> t.Callable[..., t.Any]:
2879
"""The execute function providers wrap session tools with."""
2880
from composio.core.models.tools import Tools
3197
"toolkits": {"enable": ["exa"]},
3198
"return_instant_charge": True,
3199
}
3107
- assert "premium_usage" not in kwargs
3200
3201
def test_instant_can_be_disabled(self, session, mock_client):
3202
session.update(instant=False)