diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 56526c121..b04e619a8 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -470,6 +470,10 @@ class ProviderGoogleGenAI(Provider): raise continue + # Accumulate the complete response text for the final response + accumulated_text = "" + final_response = None + async for chunk in result: llm_response = LLMResponse("assistant", is_chunk=True) @@ -481,23 +485,37 @@ class ProviderGoogleGenAI(Provider): chunk, llm_response ) yield llm_response - break + return if chunk.text: + accumulated_text += chunk.text llm_response.result_chain = MessageChain(chain=[Comp.Plain(chunk.text)]) yield llm_response if chunk.candidates[0].finish_reason: - llm_response = LLMResponse("assistant", is_chunk=False) - if not chunk.candidates[0].content.parts: - llm_response.result_chain = MessageChain(chain=[Comp.Plain(" ")]) - else: - llm_response.result_chain = self._process_content_parts( - chunk, llm_response + # Process the final chunk for potential tool calls or other content + if chunk.candidates[0].content.parts: + final_response = LLMResponse("assistant", is_chunk=False) + final_response.result_chain = self._process_content_parts( + chunk, final_response ) - yield llm_response break + # Yield final complete response with accumulated text + if not final_response: + final_response = LLMResponse("assistant", is_chunk=False) + + # Set the complete accumulated text in the final response + if accumulated_text: + final_response.result_chain = MessageChain( + chain=[Comp.Plain(accumulated_text)] + ) + elif not final_response.result_chain: + # If no text was accumulated and no final response was set, provide empty space + final_response.result_chain = MessageChain(chain=[Comp.Plain(" ")]) + + yield final_response + async def text_chat( self, prompt: str,