Clean up Anthropic conversation entity (#146737)

This commit is contained in:
Paulus Schoutsen 2025-06-13 10:29:26 -04:00 committed by GitHub
parent 2f8ad4d5bf
commit c96023dcae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -375,6 +375,26 @@ class AnthropicConversationEntity(
except conversation.ConverseError as err: except conversation.ConverseError as err:
return err.as_conversation_result() return err.as_conversation_result()
await self._async_handle_chat_log(chat_log)
response_content = chat_log.content[-1]
if not isinstance(response_content, conversation.AssistantContent):
raise TypeError("Last message must be an assistant message")
intent_response = intent.IntentResponse(language=user_input.language)
intent_response.async_set_speech(response_content.content or "")
return conversation.ConversationResult(
response=intent_response,
conversation_id=chat_log.conversation_id,
continue_conversation=chat_log.continue_conversation,
)
async def _async_handle_chat_log(
self,
chat_log: conversation.ChatLog,
) -> None:
"""Generate an answer for the chat log."""
options = self.entry.options
tools: list[ToolParam] | None = None tools: list[ToolParam] | None = None
if chat_log.llm_api: if chat_log.llm_api:
tools = [ tools = [
@ -424,7 +444,7 @@ class AnthropicConversationEntity(
[ [
content content
async for content in chat_log.async_add_delta_content_stream( async for content in chat_log.async_add_delta_content_stream(
user_input.agent_id, self.entity_id,
_transform_stream(chat_log, stream, messages), _transform_stream(chat_log, stream, messages),
) )
if not isinstance(content, conversation.AssistantContent) if not isinstance(content, conversation.AssistantContent)
@ -435,17 +455,6 @@ class AnthropicConversationEntity(
if not chat_log.unresponded_tool_results: if not chat_log.unresponded_tool_results:
break break
response_content = chat_log.content[-1]
if not isinstance(response_content, conversation.AssistantContent):
raise TypeError("Last message must be an assistant message")
intent_response = intent.IntentResponse(language=user_input.language)
intent_response.async_set_speech(response_content.content or "")
return conversation.ConversationResult(
response=intent_response,
conversation_id=chat_log.conversation_id,
continue_conversation=chat_log.continue_conversation,
)
async def _async_entry_update_listener( async def _async_entry_update_listener(
self, hass: HomeAssistant, entry: ConfigEntry self, hass: HomeAssistant, entry: ConfigEntry
) -> None: ) -> None: