@@ -218,9 +218,6 @@ async def _async_handle_message(
218218 """Call the API."""
219219 settings = {** self .entry .data , ** self .entry .options }
220220
221- client = self .hass .data [DOMAIN ][self .entry .entry_id ]
222- model = settings [CONF_MODEL ]
223-
224221 try :
225222 await chat_log .async_update_llm_data (
226223 DOMAIN ,
@@ -231,6 +228,31 @@ async def _async_handle_message(
231228 except conversation .ConverseError as err :
232229 return err .as_conversation_result ()
233230
231+ await self ._async_handle_chat_log (chat_log )
232+
233+ # Create intent response
234+ intent_response = intent .IntentResponse (language = user_input .language )
235+ if not isinstance (chat_log .content [- 1 ], conversation .AssistantContent ):
236+ raise TypeError (
237+ f"Unexpected last message type: { type (chat_log .content [- 1 ])} "
238+ )
239+ intent_response .async_set_speech (chat_log .content [- 1 ].content or "" )
240+ return conversation .ConversationResult (
241+ response = intent_response ,
242+ conversation_id = chat_log .conversation_id ,
243+ continue_conversation = chat_log .continue_conversation ,
244+ )
245+
246+ async def _async_handle_chat_log (
247+ self ,
248+ chat_log : conversation .ChatLog ,
249+ ) -> None :
250+ """Generate an answer for the chat log."""
251+ settings = {** self .entry .data , ** self .entry .options }
252+
253+ client = self .hass .data [DOMAIN ][self .entry .entry_id ]
254+ model = settings [CONF_MODEL ]
255+
234256 tools : list [dict [str , Any ]] | None = None
235257 if chat_log .llm_api :
236258 tools = [
@@ -269,27 +291,14 @@ async def _async_handle_message(
269291 [
270292 _convert_content (content )
271293 async for content in chat_log .async_add_delta_content_stream (
272- user_input . agent_id , _transform_stream (response_generator )
294+ self . entity_id , _transform_stream (response_generator )
273295 )
274296 ]
275297 )
276298
277299 if not chat_log .unresponded_tool_results :
278300 break
279301
280- # Create intent response
281- intent_response = intent .IntentResponse (language = user_input .language )
282- if not isinstance (chat_log .content [- 1 ], conversation .AssistantContent ):
283- raise TypeError (
284- f"Unexpected last message type: { type (chat_log .content [- 1 ])} "
285- )
286- intent_response .async_set_speech (chat_log .content [- 1 ].content or "" )
287- return conversation .ConversationResult (
288- response = intent_response ,
289- conversation_id = chat_log .conversation_id ,
290- continue_conversation = chat_log .continue_conversation ,
291- )
292-
293302 def _trim_history (self , message_history : MessageHistory , max_messages : int ) -> None :
294303 """Trims excess messages from a single history.
295304
0 commit comments