diff --git a/astrbot/core/agent/runners/tool_loop_agent_runner.py b/astrbot/core/agent/runners/tool_loop_agent_runner.py index 6e3ba40a98..16199ab55c 100644 --- a/astrbot/core/agent/runners/tool_loop_agent_runner.py +++ b/astrbot/core/agent/runners/tool_loop_agent_runner.py @@ -193,6 +193,32 @@ async def _complete_with_assistant_response(self, llm_resp: LLMResponse) -> None logger.error(f"Error in on_agent_done hook: {e}", exc_info=True) self._resolve_unconsumed_follow_ups() + def _yield_llm_result_response( + self, llm_resp: LLMResponse + ) -> T.Generator[AgentResponse, None, None]: + """Yield an AgentResponse for llm_result based on llm_resp content.""" + if llm_resp.result_chain: + yield AgentResponse( + type="llm_result", + data=AgentResponseData(chain=llm_resp.result_chain), + ) + elif llm_resp.completion_text: + yield AgentResponse( + type="llm_result", + data=AgentResponseData( + chain=MessageChain().message(llm_resp.completion_text), + ), + ) + elif llm_resp.reasoning_content: + yield AgentResponse( + type="llm_result", + data=AgentResponseData( + chain=MessageChain(type="reasoning").message( + llm_resp.reasoning_content, + ), + ), + ) + @override async def reset( self, @@ -718,18 +744,8 @@ async def step(self): await self._complete_with_assistant_response(llm_resp) # 返回 LLM 结果 - if llm_resp.result_chain: - yield AgentResponse( - type="llm_result", - data=AgentResponseData(chain=llm_resp.result_chain), - ) - elif llm_resp.completion_text: - yield AgentResponse( - type="llm_result", - data=AgentResponseData( - chain=MessageChain().message(llm_resp.completion_text), - ), - ) + for response in self._yield_llm_result_response(llm_resp): + yield response # 如果有工具调用,还需处理工具调用 if llm_resp.tools_call_name: @@ -739,18 +755,8 @@ async def step(self): logger.warning( "skills_like tool re-query returned no tool calls; fallback to assistant response." ) - if llm_resp.result_chain: - yield AgentResponse( - type="llm_result", - data=AgentResponseData(chain=llm_resp.result_chain), - ) - elif llm_resp.completion_text: - yield AgentResponse( - type="llm_result", - data=AgentResponseData( - chain=MessageChain().message(llm_resp.completion_text), - ), - ) + for response in self._yield_llm_result_response(llm_resp): + yield response await self._complete_with_assistant_response(llm_resp) return