Skip to content

Commit 6d7d558

Browse files
Merge pull request #34 from Contextable/issue-25-fix
Issue 25 fix
2 parents 1ff5971 + edd8897 commit 6d7d558

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

typescript-sdk/integrations/adk-middleware/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- **FIXED**: Race condition in tool result processing causing "No pending tool calls found" warnings
1616
- **FIXED**: Tool call removal now happens after pending check to prevent race conditions
1717
- **IMPROVED**: Better handling of empty tool result content with graceful JSON parsing fallback
18+
- **FIXED**: Pending tool call state management now uses SessionManager methods (issue #25)
1819

1920
### Enhanced
2021
- **LOGGING**: Added debug logging for tool result processing to aid in troubleshooting
2122
- **ARCHITECTURE**: Consolidated agent copying logic to avoid creating multiple unnecessary copies
2223
- **CLEANUP**: Removed unused toolset parameter from `_run_adk_in_background` method
24+
- **REFACTOR**: Replaced direct session service access with SessionManager state management methods for pending tool calls
2325

2426
## [0.4.1] - 2025-07-13
2527

typescript-sdk/integrations/adk-middleware/src/adk_middleware/adk_agent.py

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -189,29 +189,29 @@ async def _add_pending_tool_call_with_context(self, session_id: str, tool_call_i
189189
"""
190190
logger.debug(f"Adding pending tool call {tool_call_id} for session {session_id}, app_name={app_name}, user_id={user_id}")
191191
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(
193194
session_id=session_id,
194195
app_name=app_name,
195-
user_id=user_id
196+
user_id=user_id,
197+
key="pending_tool_calls",
198+
default=[]
196199
)
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)
202204

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:
215215
logger.info(f"Added tool call {tool_call_id} to session {session_id} pending list")
216216
except Exception as e:
217217
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):
242242
break
243243

244244
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(
246247
session_id=session_id,
247248
app_name=app_name,
248-
user_id=user_id
249+
user_id=user_id,
250+
key="pending_tool_calls",
251+
default=[]
249252
)
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)
254257

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:
267268
logger.info(f"Removed tool call {tool_call_id} from session {session_id} pending list")
268269
except Exception as e:
269270
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:
283284
for key in keys:
284285
if key.endswith(f":{session_id}"):
285286
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(
287290
session_id=session_id,
288291
app_name=app_name,
289-
user_id=uid
292+
user_id=uid,
293+
key="pending_tool_calls",
294+
default=[]
290295
)
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
295297
except Exception as e:
296298
logger.error(f"Failed to check pending tool calls for session {session_id}: {e}")
297299

0 commit comments

Comments
 (0)