Skip to content

feat: isDelta property in Message for streaming #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/a2a/server/events/event_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async def consume_all(self) -> AsyncGenerator[Event]:

is_final_event = (
(isinstance(event, TaskStatusUpdateEvent) and event.final)
or isinstance(event, Message)
or (isinstance(event, Message) and not event.isDelta)
or (
isinstance(event, Task)
and event.status.state
Expand Down
24 changes: 20 additions & 4 deletions src/a2a/server/tasks/task_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ async def get_task(self) -> Task | None:
logger.debug('Task %s not found.', self.task_id)
return self._current_task

async def add_history_message(self, task: Task) -> None:
"""Adds a message to the task's history.

If the message is not a delta, it appends the message to the history.
If the message is a delta, but with the same messageId, it extends the last message in the history.
If the message is a delta, but with a different messageId, it appends the message to the history.

Args:
task: The task with the message to add to the history.
"""
if not task.status.message:
return
if not task.history:
task.history = [task.status.message]
elif task.history[-1].messageId != task.status.message.messageId:
task.history.append(task.status.message)
elif task.status.message.isDelta:
task.history[-1].parts.extend(task.status.message.parts)

async def save_task_event(
self, event: Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent
) -> Task | None:
Expand Down Expand Up @@ -132,10 +151,7 @@ async def save_task_event(
'Updating task %s status to: %s', task.id, event.status.state
)
if task.status.message:
if not task.history:
task.history = [task.status.message]
else:
task.history.append(task.status.message)
await self.add_history_message(task)
if event.metadata:
if not task.metadata:
task.metadata = {}
Expand Down
1 change: 1 addition & 0 deletions src/a2a/server/tasks/task_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,5 @@ def new_agent_message(
messageId=str(uuid.uuid4()),
metadata=metadata,
parts=parts,
isDelta=False
)
4 changes: 4 additions & 0 deletions src/a2a/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,10 @@ class Message(BaseModel):
"""
Identifier of task the message is related to
"""
isDelta: bool | None = None
"""
Indicates if this is a delta message
"""


class MessageSendParams(BaseModel):
Expand Down
Loading