Skip to content

Commit de43b48

Browse files
committed
cleanup server api usage
1 parent 76f0b5c commit de43b48

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

agent/game_host.py

+12-23
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
from collections import OrderedDict
88
from typing import List
99

10-
import aiohttp
1110
import openai
12-
from livekit import agents, api, protocol, rtc
11+
from livekit import agents, api, rtc
1312

1413
from drawings import Line, PlayerDrawing
1514
from prompts import PROMPTS, DifficultyLevel
@@ -65,6 +64,7 @@ def __init__(self, ctx: agents.JobContext):
6564
self._ctx = ctx
6665
self._game_state = GameState()
6766
self._openai_client = openai.AsyncOpenAI()
67+
self._lkapi = api.LiveKitAPI()
6868
self._drawings = {}
6969
self._guess_cache = GuessCache()
7070
self._last_guesses = {}
@@ -74,6 +74,7 @@ def __init__(self, ctx: agents.JobContext):
7474
async def connect(self):
7575
print("Starting game host agent")
7676
await self._ctx.connect()
77+
7778
for participant in self._ctx.room.remote_participants.values():
7879
if self._register_player(participant):
7980
await self._load_player_drawing(participant)
@@ -175,18 +176,12 @@ async def _run_judge_loop(self, sleep_interval: int = 1):
175176
print("exiting judge loop")
176177

177178
async def _publish_game_state(self):
178-
async with aiohttp.ClientSession() as session:
179-
await api.room_service.RoomService(
180-
session,
181-
os.getenv("LIVEKIT_URL"),
182-
os.getenv("LIVEKIT_API_KEY"),
183-
os.getenv("LIVEKIT_API_SECRET"),
184-
).update_room_metadata(
185-
protocol.room.UpdateRoomMetadataRequest(
186-
room=self._ctx.room.name,
187-
metadata=self._game_state.to_json_string(),
188-
)
179+
await self._lkapi.room.update_room_metadata(
180+
api.UpdateRoomMetadataRequest(
181+
room=self._ctx.room.name,
182+
metadata=self._game_state.to_json_string(),
189183
)
184+
)
190185

191186
def _register_player(self, participant: rtc.Participant) -> bool:
192187
if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_AGENT:
@@ -238,17 +233,11 @@ async def _kick_player(self, participant: rtc.Participant, reason: str):
238233
payload=json.dumps({"reason": reason}),
239234
)
240235

241-
async with aiohttp.ClientSession() as session:
242-
await api.room_service.RoomService(
243-
session,
244-
os.getenv("LIVEKIT_URL"),
245-
os.getenv("LIVEKIT_API_KEY"),
246-
os.getenv("LIVEKIT_API_SECRET"),
247-
).remove_participant(
248-
protocol.room.RoomParticipantIdentity(
249-
room=self._ctx.room.name, identity=participant.identity
250-
)
236+
await self._lkapi.room.remove_participant(
237+
api.RemoveParticipantRequest(
238+
room=self._ctx.room.name, identity=participant.identity
251239
)
240+
)
252241

253242
def _unregister_player(self, participant: rtc.Participant):
254243
if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_AGENT:

0 commit comments

Comments
 (0)