11# Copyright 2025 © BeeAI a Series of LF Projects, LLC
22# SPDX-License-Identifier: Apache-2.0
3- import uuid
43from collections .abc import AsyncGenerator
54from typing import Annotated , Any , Unpack , cast
65
@@ -87,17 +86,16 @@ async def run(
8786 embedding = extra_extensions .get ("embedding" ),
8887 extra_extensions = extra_extensions , # type: ignore[arg-type]
8988 ) as stack_context :
90- artifact_id = uuid .uuid4 ()
91- append = False
89+ stream = False
9290
9391 @cloned_agent .emitter .on ("partial_update" )
9492 async def on_partial_update (data : ReActAgentUpdateEvent , _ : EventMeta ) -> None :
95- nonlocal append
93+ nonlocal stream
9694 if data .update .key == "final_answer" :
9795 update = data .update .value
9896 update = update .get_text_content () if hasattr (update , "get_text_content" ) else str (update )
99- await _send_final_answer_update ( context , artifact_id , update , append = append )
100- append = True
97+ await context . yield_async ( update )
98+ stream = True
10199
102100 @cloned_agent .emitter .on ("update" )
103101 async def on_update (data : ReActAgentUpdateEvent , _ : EventMeta ) -> None :
@@ -117,9 +115,7 @@ async def on_update(data: ReActAgentUpdateEvent, _: EventMeta) -> None:
117115 await context .store (message )
118116 await context .store (agent_response )
119117
120- if append :
121- await _send_final_answer_update (context , artifact_id , last_chunk = True )
122- else :
118+ if not stream :
123119 yield agent_response
124120
125121 return agentstack_agent .agent (** agent_metadata )(run )
@@ -207,14 +203,13 @@ async def run(
207203 embedding = extra_extensions .get ("embedding" ),
208204 extra_extensions = extra_extensions , # type: ignore[arg-type]
209205 ) as stack_context :
210- artifact_id = uuid .uuid4 ()
211- append = False
206+ stream = False
212207
213208 @cloned_agent .emitter .on ("final_answer" )
214209 async def on_final_answer (data : RequirementAgentFinalAnswerEvent , _ : EventMeta ) -> None :
215- nonlocal append
216- await _send_final_answer_update ( context , artifact_id , data .delta , append = append )
217- append = True
210+ nonlocal stream
211+ await context . yield_async ( data .delta )
212+ stream = True
218213
219214 result = await cloned_agent .run ([convert_a2a_to_framework_message (message )]).middleware (
220215 create_tool_trajectory_middleware (stack_context )
@@ -225,37 +220,12 @@ async def on_final_answer(data: RequirementAgentFinalAnswerEvent, _: EventMeta)
225220 await context .store (message )
226221 await context .store (agent_response )
227222
228- if append :
229- await _send_final_answer_update (context , artifact_id , last_chunk = True )
230- else :
223+ if not stream :
231224 yield agent_response
232225
233226 return agentstack_agent .agent (** agent_metadata )(run )
234227
235228
236- async def _send_final_answer_update (
237- context : agentstack_context .RunContext ,
238- artifact_id : uuid .UUID ,
239- update : str = "" ,
240- * ,
241- append : bool = True ,
242- last_chunk : bool = False ,
243- ) -> None :
244- await context .yield_async (
245- a2a_types .TaskArtifactUpdateEvent (
246- append = append ,
247- context_id = context .context_id ,
248- task_id = context .task_id ,
249- last_chunk = last_chunk ,
250- artifact = a2a_types .Artifact (
251- name = "final_answer" ,
252- artifact_id = str (artifact_id ),
253- parts = [a2a_types .Part (root = a2a_types .TextPart (text = update ))],
254- ),
255- )
256- )
257-
258-
259229def _runnable_factory (
260230 runnable : Runnable [Any ], * , metadata : AgentStackServerMetadata | None = None , memory_manager : MemoryManager
261231) -> agentstack_agent .AgentFactory :
0 commit comments