@@ -78,6 +78,25 @@ async def get_task(self) -> Task | None:
7878 logger .debug ('Task %s not found.' , self .task_id )
7979 return self ._current_task
8080
81+ async def add_history_message (self , task : Task ) -> None :
82+ """Adds a message to the task's history.
83+
84+ If the message is not a delta, it appends the message to the history.
85+ If the message is a delta, but with the same messageId, it extends the last message in the history.
86+ If the message is a delta, but with a different messageId, it appends the message to the history.
87+
88+ Args:
89+ task: The task with the message to add to the history.
90+ """
91+ if not task .status .message :
92+ return
93+ if not task .history :
94+ task .history = [task .status .message ]
95+ elif task .history [- 1 ].messageId != task .status .message .messageId :
96+ task .history .append (task .status .message )
97+ elif task .status .message .isDelta :
98+ task .history [- 1 ].parts .extend (task .status .message .parts )
99+
81100 async def save_task_event (
82101 self , event : Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent
83102 ) -> Task | None :
@@ -132,10 +151,7 @@ async def save_task_event(
132151 'Updating task %s status to: %s' , task .id , event .status .state
133152 )
134153 if task .status .message :
135- if not task .history :
136- task .history = [task .status .message ]
137- else :
138- task .history .append (task .status .message )
154+ await self .add_history_message (task )
139155 if event .metadata :
140156 if not task .metadata :
141157 task .metadata = {}
0 commit comments