Skip to content

Commit a8ae779

Browse files
committed
fix: adapt to Fishjam 0.29 peer API
- Use the new PeerConfigWebRTC/PeerConfigAgent/PeerConfigVAPI models (PeerConfig was split per peer type in the regenerated OpenAPI client). - Connect agents via the peer_websocket_url returned by add_peer: on deployments with a separate media node the agent socket no longer lives on the Fishjam URL. Falls back to the old URL when the field is absent.
1 parent 4127bca commit a8ae779

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

fishjam/agent/agent.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def __init__(self, id: str, room_id: str, token: str, fishjam_url: str):
203203
self.id = id
204204
self.room_id = room_id
205205

206+
self._fishjam_url = fishjam_url
206207
self._socket_url = f"{fishjam_url}/socket/agent/websocket".replace("http", "ws")
207208
self._token = token
208209

fishjam/api/_fishjam_client.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@
3232
MoqAccess,
3333
MoqAccessConfig,
3434
Peer,
35-
PeerConfig,
35+
PeerConfigAgent,
36+
PeerConfigAgentType,
37+
PeerConfigVAPI,
38+
PeerConfigVAPIType,
39+
PeerConfigWebRTC,
40+
PeerConfigWebRTCType,
3641
PeerDetailsResponse,
3742
PeerOptionsAgent,
3843
PeerOptionsVapi,
3944
PeerOptionsWebRTC,
4045
PeerRefreshTokenResponse,
41-
PeerType,
4246
RoomConfig,
4347
RoomCreateDetailsResponse,
4448
RoomDetailsResponse,
@@ -51,7 +55,7 @@
5155
ViewerToken,
5256
WebRTCMetadata,
5357
)
54-
from fishjam._openapi_client.types import UNSET
58+
from fishjam._openapi_client.types import UNSET, Unset
5559
from fishjam.agent import Agent
5660
from fishjam.api._client import Client
5761
from fishjam.errors import (
@@ -230,7 +234,7 @@ def create_peer(
230234
metadata=peer_metadata,
231235
subscribe_mode=SubscribeMode(options.subscribe_mode),
232236
)
233-
body = PeerConfig(type_=PeerType.WEBRTC, options=peer_options)
237+
body = PeerConfigWebRTC(type_=PeerConfigWebRTCType.WEBRTC, options=peer_options)
234238

235239
resp = cast(
236240
PeerDetailsResponse,
@@ -251,8 +255,8 @@ def create_agent(self, room_id: str, options: AgentOptions | None = None):
251255
and Fishjam URL.
252256
"""
253257
options = options or AgentOptions()
254-
body = PeerConfig(
255-
type_=PeerType.AGENT,
258+
body = PeerConfigAgent(
259+
type_=PeerConfigAgentType.AGENT,
256260
options=PeerOptionsAgent(
257261
output=AgentOutput(
258262
audio_format=AudioFormat(options.output.audio_format),
@@ -267,7 +271,22 @@ def create_agent(self, room_id: str, options: AgentOptions | None = None):
267271
self._request(room_add_peer, room_id=room_id, body=body),
268272
)
269273

270-
return Agent(resp.data.peer.id, room_id, resp.data.token, self._fishjam_url)
274+
socket_base_url = self._peer_socket_base_url(resp.data.peer_websocket_url)
275+
return Agent(resp.data.peer.id, room_id, resp.data.token, socket_base_url)
276+
277+
def _peer_socket_base_url(self, peer_websocket_url: str | Unset) -> str:
278+
# Fishjam deployments with a separate media node return the websocket
279+
# address the peer must connect to; older deployments omit it, in which
280+
# case the socket lives on the Fishjam URL itself.
281+
if isinstance(peer_websocket_url, Unset) or not peer_websocket_url:
282+
return self._fishjam_url
283+
284+
url = peer_websocket_url
285+
if "://" not in url:
286+
url = f"https://{url}"
287+
for suffix in ("/socket/peer/websocket", "/socket/agent/websocket"):
288+
url = url.removesuffix(suffix)
289+
return url
271290

272291
def create_vapi_agent(
273292
self,
@@ -283,7 +302,7 @@ def create_vapi_agent(
283302
Returns:
284303
- Peer: The created peer object.
285304
"""
286-
body = PeerConfig(type_=PeerType.VAPI, options=options)
305+
body = PeerConfigVAPI(type_=PeerConfigVAPIType.VAPI, options=options)
287306

288307
resp = cast(
289308
PeerDetailsResponse,

tests/agent/test_agent.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ async def wait_event(event: asyncio.Event, timeout: float = 5):
105105

106106
class TestAgentConnection:
107107
@pytest.mark.asyncio
108-
async def test_invalid_auth(self, room_api: FishjamClient):
109-
agent = Agent("fake-id", "room-id", "fake-token", room_api._fishjam_url)
108+
async def test_invalid_auth(self, room: Room, agent: Agent):
109+
# Reuse a real agent's socket address so the connection reaches
110+
# authentication instead of failing on routing.
111+
bogus_agent = Agent("fake-id", room.id, "fake-token", agent._fishjam_url)
110112

111113
with pytest.raises(AgentAuthError):
112-
async with agent.connect():
114+
async with bogus_agent.connect():
113115
raise RuntimeError("Connect should have raised AgentAuthError.")
114116

115117
@pytest.mark.asyncio

0 commit comments

Comments
 (0)