Skip to content

Commit 17b66d7

Browse files
committed
feat: add API endpoint to cancel in-progress agent tasks
Adds a POST /apps/{app_name}/users/{user_id}/sessions/{session_id}:cancel endpoint that sets a 'temp:cancelled' flag in the session state. The agent execution loop checks this flag at two key checkpoints: 1. Before LLM calls (base_llm_flow.py:_call_llm_async) — yields a cancellation response and stops the turn immediately. 2. Before tool execution (functions.py:handle_function_call_list_async) — skips all pending tool calls and returns None. Uses the 'temp:' prefix convention so the flag bypasses state schema validation and is automatically cleaned up when the session ends. Fixes google#2425
1 parent 9670ce2 commit 17b66d7

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎src/google/adk/cli/api_server.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,61 @@ async def update_session(
12181218

12191219
return session
12201220

1221+
@app.post(
1222+
"/apps/{app_name}/users/{user_id}/sessions/{session_id}:cancel",
1223+
response_model_exclude_none=True,
1224+
)
1225+
async def cancel_session(
1226+
app_name: str,
1227+
user_id: str,
1228+
session_id: str,
1229+
) -> dict[str, str]:
1230+
"""Cancel an in-progress agent session.
1231+
1232+
Sets a ``temp:cancelled`` flag in the session state. The agent
1233+
checks this flag at key execution points (before LLM calls, before
1234+
tool execution) and will gracefully halt when it detects cancellation.
1235+
1236+
Returns:
1237+
Dict with status and session_id.
1238+
1239+
Raises:
1240+
HTTPException: If the session is not found.
1241+
"""
1242+
session = await self.session_service.get_session(
1243+
app_name=app_name,
1244+
user_id=user_id,
1245+
session_id=session_id,
1246+
)
1247+
if not session:
1248+
raise HTTPException(
1249+
status_code=404,
1250+
detail=f"Session not found: {session_id}",
1251+
)
1252+
1253+
import uuid
1254+
1255+
from ..events.event import Event
1256+
from ..events.event import EventActions
1257+
1258+
cancel_event = Event(
1259+
invocation_id="c-" + str(uuid.uuid4()),
1260+
author="user",
1261+
actions=EventActions(state_delta={"temp:cancelled": True}),
1262+
)
1263+
1264+
await self.session_service.append_event(
1265+
session=session, event=cancel_event
1266+
)
1267+
1268+
logger.info(
1269+
"Session cancelled: app=%s user=%s session=%s",
1270+
app_name,
1271+
user_id,
1272+
session_id,
1273+
)
1274+
return {"status": "cancelled", "session_id": session_id}
1275+
12211276
@app.get(
12221277
"/apps/{app_name}/users/{user_id}/sessions/{session_id}/artifacts/{artifact_name}",
12231278
response_model_exclude_none=True,

‎src/google/adk/flows/llm_flows/base_llm_flow.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,12 +1245,39 @@ def _get_agent_to_run(
12451245
raise ValueError(f'Agent {agent_name} not found in the agent tree.')
12461246
return agent_to_run
12471247

1248+
@classmethod
1249+
def _is_session_cancelled(cls, invocation_context: InvocationContext) -> bool:
1250+
"""Check if the current session has been cancelled via the cancel API."""
1251+
session = getattr(invocation_context, "session", None)
1252+
if session is not None and hasattr(session, "state"):
1253+
return bool(session.state.get("temp:cancelled", False))
1254+
return False
1255+
12481256
async def _call_llm_async(
12491257
self,
12501258
invocation_context: InvocationContext,
12511259
llm_request: LlmRequest,
12521260
model_response_event: Event,
12531261
) -> AsyncGenerator[LlmResponse, None]:
1262+
# Check for cancellation before making any LLM call.
1263+
if self._is_session_cancelled(invocation_context):
1264+
from .. import events as flow_events
1265+
1266+
yield LlmResponse(
1267+
event=flow_events.Event.new_id(),
1268+
llm_response=model_response_event,
1269+
model_response=types.GenerateContentResponse(
1270+
candidates=[{
1271+
"content": {
1272+
"role": "model",
1273+
"parts": [{"text": "Task cancelled by user."}],
1274+
},
1275+
"finish_reason": "STOP",
1276+
}],
1277+
),
1278+
turn_complete=True,
1279+
)
1280+
return
12541281

12551282
async def _call_llm_with_tracing() -> AsyncGenerator[LlmResponse, None]:
12561283
with tracer.start_as_current_span('call_llm') as span:

‎src/google/adk/flows/llm_flows/functions.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,13 @@ async def handle_function_call_list_async(
418418

419419
agent = invocation_context.agent
420420

421+
# Check if session has been cancelled before executing tools.
422+
session = getattr(invocation_context, "session", None)
423+
if session is not None and hasattr(session, "state"):
424+
if session.state.get("temp:cancelled", False):
425+
logger.info("Session cancelled, skipping tool execution.")
426+
return None
427+
421428
# Filter function calls
422429
filtered_calls = [
423430
fc for fc in function_calls if not filters or fc.id in filters

0 commit comments

Comments
 (0)