diff --git a/src/smallestai/atoms/realtime/client.py b/src/smallestai/atoms/realtime/client.py index 91b00724..ba972efa 100644 --- a/src/smallestai/atoms/realtime/client.py +++ b/src/smallestai/atoms/realtime/client.py @@ -49,7 +49,8 @@ def register_call( 1. Call this endpoint with your API key and the `agent_id` (plus optional `mode` and per-call `variables`). All session configuration is fixed here — it is baked into the returned token. - 2. Open a WebSocket to `wss://api.smallest.ai/atoms/v1/agent/connect?token=`. + 2. Open the returned `websocket_url`, appending `?token=`. + If an older server omits it, use the WebSocket endpoint for the same API region. No `agent_id`, `mode`, or `variables` query params are needed on the WebSocket — they come from the token. @@ -136,7 +137,8 @@ async def register_call( 1. Call this endpoint with your API key and the `agent_id` (plus optional `mode` and per-call `variables`). All session configuration is fixed here — it is baked into the returned token. - 2. Open a WebSocket to `wss://api.smallest.ai/atoms/v1/agent/connect?token=`. + 2. Open the returned `websocket_url`, appending `?token=`. + If an older server omits it, use the WebSocket endpoint for the same API region. No `agent_id`, `mode`, or `variables` query params are needed on the WebSocket — they come from the token. diff --git a/src/smallestai/atoms/realtime/raw_client.py b/src/smallestai/atoms/realtime/raw_client.py index 61f9b9b7..4929eebf 100644 --- a/src/smallestai/atoms/realtime/raw_client.py +++ b/src/smallestai/atoms/realtime/raw_client.py @@ -48,7 +48,8 @@ def register_call( 1. Call this endpoint with your API key and the `agent_id` (plus optional `mode` and per-call `variables`). All session configuration is fixed here — it is baked into the returned token. - 2. Open a WebSocket to `wss://api.smallest.ai/atoms/v1/agent/connect?token=`. + 2. Open the returned `websocket_url`, appending `?token=`. + If an older server omits it, use the WebSocket endpoint for the same API region. No `agent_id`, `mode`, or `variables` query params are needed on the WebSocket — they come from the token. @@ -190,7 +191,8 @@ async def register_call( 1. Call this endpoint with your API key and the `agent_id` (plus optional `mode` and per-call `variables`). All session configuration is fixed here — it is baked into the returned token. - 2. Open a WebSocket to `wss://api.smallest.ai/atoms/v1/agent/connect?token=`. + 2. Open the returned `websocket_url`, appending `?token=`. + If an older server omits it, use the WebSocket endpoint for the same API region. No `agent_id`, `mode`, or `variables` query params are needed on the WebSocket — they come from the token. diff --git a/src/smallestai/atoms/realtime/types/register_call_realtime_response_data.py b/src/smallestai/atoms/realtime/types/register_call_realtime_response_data.py index 2f1bfc0c..e32f1415 100644 --- a/src/smallestai/atoms/realtime/types/register_call_realtime_response_data.py +++ b/src/smallestai/atoms/realtime/types/register_call_realtime_response_data.py @@ -15,6 +15,12 @@ class RegisterCallRealtimeResponseData(UncheckedBaseModel): WebSocket. """ + websocket_url: typing.Optional[str] = pydantic.Field(default=None) + """ + Region-correct WebSocket endpoint for this token, without query parameters. + Append the access_token as the token query parameter. Older servers may omit it. + """ + expires_in: typing.Optional[int] = pydantic.Field(default=None) """ Token lifetime in seconds. diff --git a/tests/custom/test_register_call_websocket_url.py b/tests/custom/test_register_call_websocket_url.py new file mode 100644 index 00000000..d68743a1 --- /dev/null +++ b/tests/custom/test_register_call_websocket_url.py @@ -0,0 +1,39 @@ +import httpx +import pytest + +from smallestai import AsyncSmallestAI, SmallestAI +from smallestai.atoms.realtime.types.register_call_realtime_response_data import RegisterCallRealtimeResponseData + + +@pytest.mark.parametrize("websocket_url", [None, "wss://us.example.com/atoms/v1/agent/connect"]) +def test_register_call_parses_optional_regional_websocket_url(websocket_url): + payload = {"access_token": "wct_test", "expires_in": 30} + if websocket_url is not None: + payload["websocket_url"] = websocket_url + with httpx.Client( + transport=httpx.MockTransport(lambda _: httpx.Response(201, json={"status": True, "data": payload})) + ) as http: + client = SmallestAI(api_key="test", httpx_client=http) + result = client.atoms.realtime.register_call(agent_id="agent") + assert result.data.websocket_url == websocket_url + assert result.data.access_token == "wct_test" + + +@pytest.mark.asyncio +async def test_async_register_call_preserves_regional_websocket_url(): + target = "wss://us.example.com/atoms/v1/agent/connect" + async with httpx.AsyncClient( + transport=httpx.MockTransport(lambda _: httpx.Response(201, json={"data": {"websocket_url": target}})) + ) as http: + client = AsyncSmallestAI(api_key="test", httpx_client=http) + result = await client.atoms.realtime.register_call(agent_id="agent") + assert result.data.websocket_url == target + + +def test_websocket_url_is_a_declared_optional_response_field(): + # Extra-field parsing alone would hide a missing generated contract. + fields = getattr(RegisterCallRealtimeResponseData, "model_fields", None) + if fields is None: + fields = RegisterCallRealtimeResponseData.__fields__ + assert "websocket_url" in fields + assert RegisterCallRealtimeResponseData().websocket_url is None