Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix agent dispatch example imports #322

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 21 additions & 30 deletions examples/agent_dispatch.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import asyncio
import os
import aiohttp
from livekit.protocol.room import RoomConfiguration
from livekit.protocol.agent_dispatch import (
RoomAgentDispatch,
CreateAgentDispatchRequest,
)
from livekit.api import AccessToken, VideoGrants
from livekit.api.agent_dispatch_service import AgentDispatchService

from livekit import api

room_name = "my-room"
agent_name = "test-agent"
Expand All @@ -22,20 +13,19 @@
"""


async def create_explicit_dispatch(http_session: aiohttp.ClientSession):
agent_dispatch_service = AgentDispatchService(
session=http_session,
url=os.getenv("LIVEKIT_URL"),
api_key=os.getenv("LIVEKIT_API_KEY"),
api_secret=os.getenv("LIVEKIT_API_SECRET"),
)
dispatch_request = CreateAgentDispatchRequest(
agent_name=agent_name, room=room_name, metadata="my_metadata"
async def create_explicit_dispatch():
lkapi = api.LiveKitAPI()

dispatch = await lkapi.agent_dispatch.create_dispatch(
api.CreateAgentDispatchRequest(
agent_name=agent_name, room=room_name, metadata="my_metadata"
)
)
dispatch = await agent_dispatch_service.create_dispatch(dispatch_request)
print("created dispatch", dispatch)
dispatches = await agent_dispatch_service.list_dispatch(room_name=room_name)

dispatches = await lkapi.agent_dispatch.list_dispatch(room_name=room_name)
print(f"there are {len(dispatches)} dispatches in {room_name}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg, btw we need a await lkapi.aclose() at the end to close out the session

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just added, thanks for the catch!

await lkapi.aclose()


"""
Expand All @@ -48,13 +38,15 @@ async def create_explicit_dispatch(http_session: aiohttp.ClientSession):

async def create_token_with_agent_dispatch() -> str:
token = (
AccessToken()
api.AccessToken()
.with_identity("my_participant")
.with_grants(VideoGrants(room_join=True, room=room_name))
.with_grants(api.VideoGrants(room_join=True, room=room_name))
.with_room_config(
RoomConfiguration(
api.RoomConfiguration(
agents=[
RoomAgentDispatch(agent_name="test-agent", metadata="my_metadata")
api.RoomAgentDispatch(
agent_name="test-agent", metadata="my_metadata"
)
],
),
)
Expand All @@ -64,11 +56,10 @@ async def create_token_with_agent_dispatch() -> str:


async def main():
async with aiohttp.ClientSession() as http_session:
token = await create_token_with_agent_dispatch()
print("created participant token", token)
print("creating explicit dispatch")
await create_explicit_dispatch(http_session)
token = await create_token_with_agent_dispatch()
print("created participant token", token)
print("creating explicit dispatch")
await create_explicit_dispatch()


asyncio.run(main())
Loading