11from __future__ import annotations
22
3+ from google .protobuf .empty_pb2 import Empty
34from livekit .protocol .agent_pb .agent_thread import AgentTask , AgentThread
45
56from ._generated .public_api .models import (
910 ThreadServiceUpdateThreadBody ,
1011)
1112from ._service import PublicApiService
12- from .public_api_client import from_json , next_cursor , opt , page_size , to_json
13+ from .public_api_client import to_json
1314
1415_SORT_ORDERS = {
1516 AgentThread .SORT_ORDER_ASC : LivekitPublicapiCommonV1SortOrder .SORT_ORDER_ASC ,
@@ -31,67 +32,73 @@ class ThreadService(PublicApiService):
3132
3233 async def create_thread (self , req : AgentThread .CreateRequest ) -> AgentThread .Thread :
3334 """Creates a thread and its database."""
34- res = await self ._request (
35+ return await self ._client . request (
3536 self ._client .threads .thread_service_create_thread_without_preload_content ,
37+ self ._auth_header (),
38+ AgentThread .Thread ,
39+ field = "thread" ,
3640 thread_service_create_thread_body = ThreadServiceCreateThreadBody .from_dict (to_json (req )),
3741 )
38- return from_json (res .get ("thread" ), AgentThread .Thread ())
3942
4043 async def get_thread (self , thread_id : str ) -> AgentThread .Thread :
41- res = await self ._request (
44+ return await self ._client . request (
4245 self ._client .threads .thread_service_get_thread_without_preload_content ,
46+ self ._auth_header (),
47+ AgentThread .Thread ,
48+ field = "thread" ,
4349 thread_id = thread_id ,
4450 )
45- return from_json (res .get ("thread" ), AgentThread .Thread ())
4651
4752 async def list_threads (self , req : AgentThread .ListRequest ) -> AgentThread .ListResponse :
4853 """The threads of one scope (every scope when empty), newest first."""
49- res = await self ._request (
54+ return await self ._client . list (
5055 self ._client .threads .thread_service_list_threads_without_preload_content ,
51- scope = opt (req .scope ),
52- page_cursor = opt (req .page_token ),
53- page_page_size = page_size (req .limit ),
54- )
55- return from_json (
56- {"threads" : res .get ("items" ), "nextPageToken" : next_cursor (res )},
57- AgentThread .ListResponse (),
56+ self ._auth_header (),
57+ AgentThread .ListResponse ,
58+ items = "threads" ,
59+ page_token = req .page_token ,
60+ limit = req .limit ,
61+ scope = req .scope ,
5862 )
5963
6064 async def update_thread (self , req : AgentThread .UpdateRequest ) -> AgentThread .Thread :
6165 """Merges the thread's attributes (an empty value deletes the entry) and sets its idle
6266 TTL. An update counts as activity."""
6367 body = to_json (req )
6468 body .pop ("threadId" , None )
65- res = await self ._request (
69+ return await self ._client . request (
6670 self ._client .threads .thread_service_update_thread_without_preload_content ,
71+ self ._auth_header (),
72+ AgentThread .Thread ,
73+ field = "thread" ,
6774 thread_id = req .thread_id ,
6875 thread_service_update_thread_body = ThreadServiceUpdateThreadBody .from_dict (body ),
6976 )
70- return from_json (res .get ("thread" ), AgentThread .Thread ())
7177
7278 async def delete_thread (self , thread_id : str ) -> None :
7379 """Deletes the thread and its database."""
74- await self ._request (
80+ await self ._client . request (
7581 self ._client .threads .thread_service_delete_thread_without_preload_content ,
82+ self ._auth_header (),
83+ Empty ,
7684 thread_id = thread_id ,
7785 )
7886
7987 async def get_thread_items (
8088 self , req : AgentThread .GetItemsRequest
8189 ) -> AgentThread .GetItemsResponse :
8290 """The thread's chat items, oldest first unless `sort_order` says otherwise."""
83- res = await self ._request (
91+ return await self ._client . list (
8492 self ._client .threads .thread_service_get_thread_items_without_preload_content ,
93+ self ._auth_header (),
94+ AgentThread .GetItemsResponse ,
95+ items = "items" ,
96+ page_token = req .page_token ,
97+ limit = req .limit ,
8598 thread_id = req .thread_id ,
86- agent = opt (req .agent ),
87- page_cursor = opt (req .page_token ),
88- page_page_size = page_size (req .limit ),
99+ agent = req .agent ,
89100 sort_order = _SORT_ORDERS .get (req .sort_order ),
90101 )
91- return from_json (
92- {"items" : res .get ("items" ), "nextPageToken" : next_cursor (res )},
93- AgentThread .GetItemsResponse (),
94- )
95102
96103 async def get_thread_tasks (self , req : AgentTask .GetTasksRequest ) -> AgentTask .GetTasksResponse :
97104 """The thread's tasks, oldest first."""
@@ -100,14 +107,13 @@ async def get_thread_tasks(self, req: AgentTask.GetTasksRequest) -> AgentTask.Ge
100107 if req .HasField ("status" )
101108 else None
102109 )
103- res = await self ._request (
110+ return await self ._client . list (
104111 self ._client .threads .thread_service_get_thread_tasks_without_preload_content ,
112+ self ._auth_header (),
113+ AgentTask .GetTasksResponse ,
114+ items = "tasks" ,
115+ page_token = req .page_token ,
116+ limit = req .limit ,
105117 thread_id = req .thread_id ,
106118 status = status ,
107- page_cursor = opt (req .page_token ),
108- page_page_size = page_size (req .limit ),
109- )
110- return from_json (
111- {"tasks" : res .get ("items" ), "nextPageToken" : next_cursor (res )},
112- AgentTask .GetTasksResponse (),
113119 )
0 commit comments