2121from a2a .server .request_handlers .request_handler import RequestHandler
2222from a2a .server .tasks import (
2323 PushNotificationConfigStore ,
24+ PushNotificationEvent ,
2425 PushNotificationSender ,
2526 ResultAggregator ,
2627 TaskManager ,
5253 TaskNotFoundError ,
5354 UnsupportedOperationError ,
5455)
55- from a2a .utils .task import apply_history_length
56+ from a2a .utils .task import (
57+ apply_history_length ,
58+ validate_history_length ,
59+ validate_page_size ,
60+ )
5661from a2a .utils .telemetry import SpanKind , trace_class
5762
5863
@@ -122,6 +127,8 @@ async def on_get_task(
122127 context : ServerCallContext | None = None ,
123128 ) -> Task | None :
124129 """Default handler for 'tasks/get'."""
130+ validate_history_length (params )
131+
125132 task_id = params .id
126133 task : Task | None = await self .task_store .get (task_id , context )
127134 if not task :
@@ -135,6 +142,10 @@ async def on_list_tasks(
135142 context : ServerCallContext | None = None ,
136143 ) -> ListTasksResponse :
137144 """Default handler for 'tasks/list'."""
145+ validate_history_length (params )
146+ if params .HasField ('page_size' ):
147+ validate_page_size (params .page_size )
148+
138149 page = await self .task_store .list (params , context )
139150 for task in page .tasks :
140151 if not params .include_artifacts :
@@ -309,13 +320,15 @@ def _validate_task_id_match(self, task_id: str, event_task_id: str) -> None:
309320 )
310321
311322 async def _send_push_notification_if_needed (
312- self , task_id : str , result_aggregator : ResultAggregator
323+ self , task_id : str , event : Event
313324 ) -> None :
314- """Sends push notification if configured and task is available."""
315- if self ._push_sender and task_id :
316- latest_task = await result_aggregator .current_result
317- if isinstance (latest_task , Task ):
318- await self ._push_sender .send_notification (latest_task )
325+ """Sends push notification if configured."""
326+ if (
327+ self ._push_sender
328+ and task_id
329+ and isinstance (event , PushNotificationEvent )
330+ ):
331+ await self ._push_sender .send_notification (task_id , event )
319332
320333 async def on_message_send (
321334 self ,
@@ -327,6 +340,8 @@ async def on_message_send(
327340 Starts the agent execution for the message and waits for the final
328341 result (Task or Message).
329342 """
343+ validate_history_length (params .configuration )
344+
330345 (
331346 _task_manager ,
332347 task_id ,
@@ -345,10 +360,8 @@ async def on_message_send(
345360 interrupted_or_non_blocking = False
346361 try :
347362 # Create async callback for push notifications
348- async def push_notification_callback () -> None :
349- await self ._send_push_notification_if_needed (
350- task_id , result_aggregator
351- )
363+ async def push_notification_callback (event : Event ) -> None :
364+ await self ._send_push_notification_if_needed (task_id , event )
352365
353366 (
354367 result ,
@@ -381,8 +394,6 @@ async def push_notification_callback() -> None:
381394 if params .configuration :
382395 result = apply_history_length (result , params .configuration )
383396
384- await self ._send_push_notification_if_needed (task_id , result_aggregator )
385-
386397 return result
387398
388399 async def on_message_send_stream (
@@ -410,9 +421,7 @@ async def on_message_send_stream(
410421 if isinstance (event , Task ):
411422 self ._validate_task_id_match (task_id , event .id )
412423
413- await self ._send_push_notification_if_needed (
414- task_id , result_aggregator
415- )
424+ await self ._send_push_notification_if_needed (task_id , event )
416425 yield event
417426 except (asyncio .CancelledError , GeneratorExit ):
418427 # Client disconnected: continue consuming and persisting events in the background
0 commit comments