-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathagent_dispatch_service.py
More file actions
119 lines (100 loc) · 3.87 KB
/
Copy pathagent_dispatch_service.py
File metadata and controls
119 lines (100 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import aiohttp
from typing import Optional
from livekit.protocol.agent_dispatch import (
CreateAgentDispatchRequest,
AgentDispatch,
DeleteAgentDispatchRequest,
ListAgentDispatchRequest,
ListAgentDispatchResponse,
)
from ._service import Service
from ._failover import FailoverConfig
from .access_token import VideoGrants
SVC = "AgentDispatchService"
"""@private"""
class AgentDispatchService(Service):
"""Manage agent dispatches. Service APIs require roomAdmin permissions.
Recommended way to use this service is via `livekit.api.LiveKitAPI`:
```python
from livekit import api
lkapi = api.LiveKitAPI()
agent_dispatch = lkapi.agent_dispatch
```
"""
def __init__(
self,
session: aiohttp.ClientSession,
url: str,
api_key: str,
api_secret: str,
failover: Optional[FailoverConfig] = None,
):
super().__init__(session, url, api_key, api_secret, failover=failover)
async def create_dispatch(self, req: CreateAgentDispatchRequest) -> AgentDispatch:
"""Create an explicit dispatch for an agent to join a room.
To use explicit dispatch, your agent must be registered with an `agentName`.
Args:
req (CreateAgentDispatchRequest): Request containing dispatch creation parameters.
The request can include an optional `deployment` field to target a specific
deployment. Leave empty to target the production deployment.
Returns:
AgentDispatch: The created agent dispatch object
"""
return await self._client.request(
SVC,
"CreateDispatch",
req,
self._auth_header(VideoGrants(room_admin=True, room=req.room)),
AgentDispatch,
)
async def delete_dispatch(self, dispatch_id: str, room_name: str) -> AgentDispatch:
"""Delete an explicit dispatch for an agent in a room.
Args:
dispatch_id (str): ID of the dispatch to delete
room_name (str): Name of the room containing the dispatch
Returns:
AgentDispatch: The deleted agent dispatch object
"""
return await self._client.request(
SVC,
"DeleteDispatch",
DeleteAgentDispatchRequest(
dispatch_id=dispatch_id,
room=room_name,
),
self._auth_header(VideoGrants(room_admin=True, room=room_name)),
AgentDispatch,
)
async def list_dispatch(self, room_name: str) -> list[AgentDispatch]:
"""List all agent dispatches in a room.
Args:
room_name (str): Name of the room to list dispatches from
Returns:
list[AgentDispatch]: List of agent dispatch objects in the room
"""
res = await self._client.request(
SVC,
"ListDispatch",
ListAgentDispatchRequest(room=room_name),
self._auth_header(VideoGrants(room_admin=True, room=room_name)),
ListAgentDispatchResponse,
)
return list(res.agent_dispatches)
async def get_dispatch(self, dispatch_id: str, room_name: str) -> Optional[AgentDispatch]:
"""Get an Agent dispatch by ID
Args:
dispatch_id (str): ID of the dispatch to retrieve
room_name (str): Name of the room containing the dispatch
Returns:
Optional[AgentDispatch]: The requested agent dispatch object if found, None otherwise
"""
res = await self._client.request(
SVC,
"ListDispatch",
ListAgentDispatchRequest(dispatch_id=dispatch_id, room=room_name),
self._auth_header(VideoGrants(room_admin=True, room=room_name)),
ListAgentDispatchResponse,
)
if len(res.agent_dispatches) > 0:
return res.agent_dispatches[0]
return None