|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from a2a.server.events import Event |
| 18 | +from a2a.types import Message |
| 19 | +from a2a.types import TaskState |
| 20 | +from a2a.types import TaskStatusUpdateEvent |
| 21 | + |
| 22 | +from ...utils.feature_decorator import working_in_progress |
| 23 | + |
| 24 | + |
| 25 | +@working_in_progress |
| 26 | +class TaskResultAggregator: |
| 27 | + """Aggregates the task status updates and provides the final task state.""" |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + self._task_state = TaskState.working |
| 31 | + self._task_status_message = None |
| 32 | + |
| 33 | + def process_event(self, event: Event): |
| 34 | + """Process an event from the agent run and detect signals about the task status. |
| 35 | + Priority of task state: |
| 36 | + - failed |
| 37 | + - auth_required |
| 38 | + - input_required |
| 39 | + - working |
| 40 | + """ |
| 41 | + if isinstance(event, TaskStatusUpdateEvent): |
| 42 | + if event.status.state == TaskState.failed: |
| 43 | + self._task_state = TaskState.failed |
| 44 | + self._task_status_message = event.status.message |
| 45 | + elif ( |
| 46 | + event.status.state == TaskState.auth_required |
| 47 | + and self._task_state != TaskState.failed |
| 48 | + ): |
| 49 | + self._task_state = TaskState.auth_required |
| 50 | + self._task_status_message = event.status.message |
| 51 | + elif ( |
| 52 | + event.status.state == TaskState.input_required |
| 53 | + and self._task_state |
| 54 | + not in (TaskState.failed, TaskState.auth_required) |
| 55 | + ): |
| 56 | + self._task_state = TaskState.input_required |
| 57 | + self._task_status_message = event.status.message |
| 58 | + # final state is already recorded and make sure the intermediate state is |
| 59 | + # always working because other state may terminate the event aggregation |
| 60 | + # in a2a request handler |
| 61 | + elif self._task_state == TaskState.working: |
| 62 | + self._task_status_message = event.status.message |
| 63 | + event.status.state = TaskState.working |
| 64 | + |
| 65 | + @property |
| 66 | + def task_state(self) -> TaskState: |
| 67 | + return self._task_state |
| 68 | + |
| 69 | + @property |
| 70 | + def task_status_message(self) -> Message | None: |
| 71 | + return self._task_status_message |
0 commit comments