@@ -189,29 +189,29 @@ async def _add_pending_tool_call_with_context(self, session_id: str, tool_call_i
189
189
"""
190
190
logger .debug (f"Adding pending tool call { tool_call_id } for session { session_id } , app_name={ app_name } , user_id={ user_id } " )
191
191
try :
192
- session = await self ._session_manager ._session_service .get_session (
192
+ # Get current pending calls using SessionManager
193
+ pending_calls = await self ._session_manager .get_state_value (
193
194
session_id = session_id ,
194
195
app_name = app_name ,
195
- user_id = user_id
196
+ user_id = user_id ,
197
+ key = "pending_tool_calls" ,
198
+ default = []
196
199
)
197
- logger .debug (f"Retrieved session: { session } " )
198
- if session :
199
- # Get current state or initialize empty
200
- current_state = session .state or {}
201
- pending_calls = current_state .get ("pending_tool_calls" , [])
200
+
201
+ # Add new tool call if not already present
202
+ if tool_call_id not in pending_calls :
203
+ pending_calls .append (tool_call_id )
202
204
203
- # Add new tool call if not already present
204
- if tool_call_id not in pending_calls :
205
- pending_calls .append (tool_call_id )
206
-
207
- # Persist the state change using append_event with EventActions
208
- from google .adk .events import Event , EventActions
209
- event = Event (
210
- author = "adk_middleware" ,
211
- actions = EventActions (stateDelta = {"pending_tool_calls" : pending_calls })
212
- )
213
- await self ._session_manager ._session_service .append_event (session , event )
214
-
205
+ # Update the state using SessionManager
206
+ success = await self ._session_manager .set_state_value (
207
+ session_id = session_id ,
208
+ app_name = app_name ,
209
+ user_id = user_id ,
210
+ key = "pending_tool_calls" ,
211
+ value = pending_calls
212
+ )
213
+
214
+ if success :
215
215
logger .info (f"Added tool call { tool_call_id } to session { session_id } pending list" )
216
216
except Exception as e :
217
217
logger .error (f"Failed to add pending tool call { tool_call_id } to session { session_id } : { e } " )
@@ -242,28 +242,29 @@ async def _remove_pending_tool_call(self, session_id: str, tool_call_id: str):
242
242
break
243
243
244
244
if session_key and user_id and app_name :
245
- session = await self ._session_manager ._session_service .get_session (
245
+ # Get current pending calls using SessionManager
246
+ pending_calls = await self ._session_manager .get_state_value (
246
247
session_id = session_id ,
247
248
app_name = app_name ,
248
- user_id = user_id
249
+ user_id = user_id ,
250
+ key = "pending_tool_calls" ,
251
+ default = []
249
252
)
250
- if session :
251
- # Get current state
252
- current_state = session . state or {}
253
- pending_calls = current_state . get ( "pending_tool_calls" , [] )
253
+
254
+ # Remove tool call if present
255
+ if tool_call_id in pending_calls :
256
+ pending_calls . remove ( tool_call_id )
254
257
255
- # Remove tool call if present
256
- if tool_call_id in pending_calls :
257
- pending_calls .remove (tool_call_id )
258
-
259
- # Persist the state change using append_event with EventActions
260
- from google .adk .events import Event , EventActions
261
- event = Event (
262
- author = "adk_middleware" ,
263
- actions = EventActions (stateDelta = {"pending_tool_calls" : pending_calls })
264
- )
265
- await self ._session_manager ._session_service .append_event (session , event )
266
-
258
+ # Update the state using SessionManager
259
+ success = await self ._session_manager .set_state_value (
260
+ session_id = session_id ,
261
+ app_name = app_name ,
262
+ user_id = user_id ,
263
+ key = "pending_tool_calls" ,
264
+ value = pending_calls
265
+ )
266
+
267
+ if success :
267
268
logger .info (f"Removed tool call { tool_call_id } from session { session_id } pending list" )
268
269
except Exception as e :
269
270
logger .error (f"Failed to remove pending tool call { tool_call_id } from session { session_id } : { e } " )
@@ -283,15 +284,16 @@ async def _has_pending_tool_calls(self, session_id: str) -> bool:
283
284
for key in keys :
284
285
if key .endswith (f":{ session_id } " ):
285
286
app_name = key .split (':' , 1 )[0 ]
286
- session = await self ._session_manager ._session_service .get_session (
287
+
288
+ # Get pending calls using SessionManager
289
+ pending_calls = await self ._session_manager .get_state_value (
287
290
session_id = session_id ,
288
291
app_name = app_name ,
289
- user_id = uid
292
+ user_id = uid ,
293
+ key = "pending_tool_calls" ,
294
+ default = []
290
295
)
291
- if session :
292
- current_state = session .state or {}
293
- pending_calls = current_state .get ("pending_tool_calls" , [])
294
- return len (pending_calls ) > 0
296
+ return len (pending_calls ) > 0
295
297
except Exception as e :
296
298
logger .error (f"Failed to check pending tool calls for session { session_id } : { e } " )
297
299
0 commit comments