2121from google .adk .sessions import BaseSessionService
2222from google .adk .sessions .base_session_service import ListSessionsResponse
2323
24- from ._client_tool_call import ClientToolCallState
25- from ._constants import CHATKIT_THREAD_METADTA_KEY , CHATKIT_WIDGET_STATE_KEY , CLIENT_TOOL_KEY_IN_TOOL_RESPONSE
24+ from ._client_tool_call import serialize_client_tool_call_item
25+ from ._constants import CHATKIT_CLIENT_TOOL_CALLS_KEY , CHATKIT_THREAD_METADTA_KEY , CHATKIT_WIDGET_STATE_KEY
2626from ._context import ADKContext
2727from ._thread_utils import (
28- add_client_tool_status ,
29- get_client_tool_status ,
3028 get_thread_metadata_from_state ,
3129 serialize_thread_metadata ,
3230)
@@ -167,36 +165,24 @@ async def load_thread_items(
167165 an_item = WidgetItem .model_validate (widget_data )
168166
169167 # let's check for adk-client-tool in the response
170- adk_client_tool = fn_response .response .get (CLIENT_TOOL_KEY_IN_TOOL_RESPONSE , None )
171- if adk_client_tool :
172- adk_client_tool = ClientToolCallState .model_validate (adk_client_tool )
173- status = get_client_tool_status (
174- session .state ,
175- adk_client_tool .id ,
176- )
177- if status :
178- an_item = ClientToolCallItem (
179- id = event .id ,
180- thread_id = thread_id ,
181- name = adk_client_tool .name ,
182- arguments = adk_client_tool .arguments ,
183- status = status , # type: ignore
184- created_at = datetime .fromtimestamp (event .timestamp ),
185- call_id = adk_client_tool .id ,
186- )
168+ adk_client_tool = session .state .get (CHATKIT_CLIENT_TOOL_CALLS_KEY , {})
169+ if fn_response .id in adk_client_tool :
170+ client_tool_data = adk_client_tool [fn_response .id ]
171+ an_item = ClientToolCallItem .model_validate (client_tool_data )
187172
188173 if an_item :
189174 thread_items .append (an_item )
190175
191176 return Page (data = thread_items )
192177
193178 async def add_thread_item (self , thread_id : str , item : ThreadItem , context : ADKContext ) -> None :
194- # items are added to the session by runner except for WidgetItem
195- if not isinstance (item , WidgetItem ):
179+ if not isinstance (item , (ClientToolCallItem , WidgetItem )):
196180 return
197181
198182 _LOGGER .info (f"Adding thread item to thread { thread_id } for user { context .user_id } in app { context .app_name } " )
199183
184+ print (item )
185+
200186 # the widget item is added in a function call so it's ID has the function call id
201187 # we issue a system event to add the widget item in the State keeping the info about which function call added it
202188 # so that it is able to be retrieved later and sequenced
@@ -212,9 +198,14 @@ async def add_thread_item(self, thread_id: str, item: ThreadItem, context: ADKCo
212198 f"Session with id { thread_id } not found for user { context .user_id } in app { context .app_name } "
213199 )
214200
215- state_delta = {
216- CHATKIT_WIDGET_STATE_KEY : {item .id : serialize_widget_item (item )},
217- }
201+ if isinstance (item , ClientToolCallItem ):
202+ state_delta = {
203+ CHATKIT_CLIENT_TOOL_CALLS_KEY : {item .id : serialize_client_tool_call_item (item )},
204+ }
205+ elif isinstance (item , WidgetItem ):
206+ state_delta = {
207+ CHATKIT_WIDGET_STATE_KEY : {item .id : serialize_widget_item (item )},
208+ }
218209
219210 actions_with_update = EventActions (state_delta = state_delta )
220211 system_event = Event (
@@ -265,38 +256,24 @@ async def save_item(self, thread_id: str, item: ThreadItem, context: ADKContext)
265256 f"Session with id { thread_id } not found for user { context .user_id } in app { context .app_name } "
266257 )
267258
268- # we will only handle specify types of items here
269- # as quite many are automatically handled by runner
270259 if isinstance (item , ClientToolCallItem ):
271- thread_metadata = add_client_tool_status (session .state , item .call_id , item .status )
272-
273260 state_delta = {
274- CHATKIT_THREAD_METADTA_KEY : serialize_thread_metadata ( thread_metadata ) ,
261+ CHATKIT_CLIENT_TOOL_CALLS_KEY : { item . id : serialize_client_tool_call_item ( item )} ,
275262 }
276-
277- actions_with_update = EventActions (state_delta = state_delta )
278- system_event = Event (
279- invocation_id = uuid4 ().hex ,
280- author = "system" ,
281- actions = actions_with_update ,
282- timestamp = datetime .now ().timestamp (),
283- )
284- await self ._session_service .append_event (session , system_event )
285-
286263 elif isinstance (item , WidgetItem ):
287- # we should update the widget stored state
288264 state_delta = {
289265 CHATKIT_WIDGET_STATE_KEY : {item .id : serialize_widget_item (item )},
290266 }
291267
292- actions_with_update = EventActions (state_delta = state_delta )
293- system_event = Event (
294- invocation_id = uuid4 ().hex ,
295- author = "system" ,
296- actions = actions_with_update ,
297- timestamp = datetime .now ().timestamp (),
298- )
299- await self ._session_service .append_event (session , system_event )
268+ actions_with_update = EventActions (state_delta = state_delta )
269+ system_event = Event (
270+ invocation_id = uuid4 ().hex ,
271+ author = "system" ,
272+ actions = actions_with_update ,
273+ timestamp = datetime .now ().timestamp (),
274+ )
275+
276+ await self ._session_service .append_event (session , system_event )
300277
301278 async def load_item (self , thread_id : str , item_id : str , context : ADKContext ) -> ThreadItem :
302279 _LOGGER .info (
0 commit comments