Skip to content

Commit c96023d

Browse files
authored
Clean up Anthropic conversation entity (home-assistant#146737)
1 parent 2f8ad4d commit c96023d

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

homeassistant/components/anthropic/conversation.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,26 @@ async def _async_handle_message(
375375
except conversation.ConverseError as err:
376376
return err.as_conversation_result()
377377

378+
await self._async_handle_chat_log(chat_log)
379+
380+
response_content = chat_log.content[-1]
381+
if not isinstance(response_content, conversation.AssistantContent):
382+
raise TypeError("Last message must be an assistant message")
383+
intent_response = intent.IntentResponse(language=user_input.language)
384+
intent_response.async_set_speech(response_content.content or "")
385+
return conversation.ConversationResult(
386+
response=intent_response,
387+
conversation_id=chat_log.conversation_id,
388+
continue_conversation=chat_log.continue_conversation,
389+
)
390+
391+
async def _async_handle_chat_log(
392+
self,
393+
chat_log: conversation.ChatLog,
394+
) -> None:
395+
"""Generate an answer for the chat log."""
396+
options = self.entry.options
397+
378398
tools: list[ToolParam] | None = None
379399
if chat_log.llm_api:
380400
tools = [
@@ -424,7 +444,7 @@ async def _async_handle_message(
424444
[
425445
content
426446
async for content in chat_log.async_add_delta_content_stream(
427-
user_input.agent_id,
447+
self.entity_id,
428448
_transform_stream(chat_log, stream, messages),
429449
)
430450
if not isinstance(content, conversation.AssistantContent)
@@ -435,17 +455,6 @@ async def _async_handle_message(
435455
if not chat_log.unresponded_tool_results:
436456
break
437457

438-
response_content = chat_log.content[-1]
439-
if not isinstance(response_content, conversation.AssistantContent):
440-
raise TypeError("Last message must be an assistant message")
441-
intent_response = intent.IntentResponse(language=user_input.language)
442-
intent_response.async_set_speech(response_content.content or "")
443-
return conversation.ConversationResult(
444-
response=intent_response,
445-
conversation_id=chat_log.conversation_id,
446-
continue_conversation=chat_log.continue_conversation,
447-
)
448-
449458
async def _async_entry_update_listener(
450459
self, hass: HomeAssistant, entry: ConfigEntry
451460
) -> None:

0 commit comments

Comments
 (0)