@@ -94,18 +94,10 @@ async def run_third_party_agent(
9494
9595@dataclass
9696class _ThirdPartyRunnerOutput :
97- chain : MessageChain | None
97+ chain : MessageChain
9898 is_error : bool = False
9999
100100
101- async def _iter_runner_output_chain (
102- output_stream : AsyncGenerator [_ThirdPartyRunnerOutput , None ],
103- ) -> AsyncGenerator [_ThirdPartyRunnerOutput , None ]:
104- async for output in output_stream :
105- if output .chain :
106- yield output
107-
108-
109101async def _close_runner_if_supported (runner : "BaseAgentRunner" ) -> None :
110102 close_callable = getattr (runner , "close" , None )
111103 if not callable (close_callable ):
@@ -239,18 +231,15 @@ async def process(
239231
240232 async def _stream_runner_chain () -> AsyncGenerator [MessageChain , None ]:
241233 nonlocal stream_has_runner_error
242- async for runner_output in _iter_runner_output_chain (
243- run_third_party_agent (
244- runner ,
245- stream_to_general = False ,
246- custom_error_message = custom_error_message ,
247- ),
234+ async for runner_output in run_third_party_agent (
235+ runner ,
236+ stream_to_general = False ,
237+ custom_error_message = custom_error_message ,
248238 ):
249239 if runner_output .is_error :
250240 stream_has_runner_error = True
251241 event .set_extra (THIRD_PARTY_RUNNER_ERROR_EXTRA_KEY , True )
252- if runner_output .chain :
253- yield runner_output .chain
242+ yield runner_output .chain
254243
255244 event .set_result (
256245 MessageEventResult ()
@@ -276,29 +265,25 @@ async def _stream_runner_chain() -> AsyncGenerator[MessageChain, None]:
276265 )
277266 else :
278267 # 非流式响应或转换为普通响应
279- fallback_chains : list [ MessageChain ] = []
268+ merged_chain : list = []
280269 fallback_is_error = False
281270 async for output in run_third_party_agent (
282271 runner ,
283272 stream_to_general = stream_to_general ,
284273 custom_error_message = custom_error_message ,
285274 ):
286- if output .chain :
287- fallback_chains .append (output .chain )
275+ merged_chain .extend (output .chain .chain or [])
288276 if output .is_error :
289277 fallback_is_error = True
290278 yield
291279
292280 final_resp = runner .get_final_llm_resp ()
293281
294282 if not final_resp or not final_resp .result_chain :
295- if fallback_chains :
283+ if merged_chain :
296284 logger .warning (
297285 "Agent Runner returned no final response, fallback to streamed error/result chain."
298286 )
299- merged_chain : list = []
300- for chain in fallback_chains :
301- merged_chain .extend (chain .chain or [])
302287 content_type = (
303288 ResultContentType .AGENT_RUNNER_ERROR
304289 if fallback_is_error
0 commit comments