Skip to content

Commit d880ce6

Browse files
authored
Clean up Google conversation entity (home-assistant#146736)
1 parent c96023d commit d880ce6

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

homeassistant/components/google_generative_ai_conversation/conversation.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,11 @@ def _create_google_tool_response_content(
199199

200200

201201
def _convert_content(
202-
content: conversation.UserContent
203-
| conversation.AssistantContent
204-
| conversation.SystemContent,
202+
content: (
203+
conversation.UserContent
204+
| conversation.AssistantContent
205+
| conversation.SystemContent
206+
),
205207
) -> Content:
206208
"""Convert HA content to Google content."""
207209
if content.role != "assistant" or not content.tool_calls:
@@ -381,6 +383,29 @@ async def _async_handle_message(
381383
except conversation.ConverseError as err:
382384
return err.as_conversation_result()
383385

386+
await self._async_handle_chat_log(chat_log)
387+
388+
response = intent.IntentResponse(language=user_input.language)
389+
if not isinstance(chat_log.content[-1], conversation.AssistantContent):
390+
LOGGER.error(
391+
"Last content in chat log is not an AssistantContent: %s. This could be due to the model not returning a valid response",
392+
chat_log.content[-1],
393+
)
394+
raise HomeAssistantError(f"{ERROR_GETTING_RESPONSE}")
395+
response.async_set_speech(chat_log.content[-1].content or "")
396+
return conversation.ConversationResult(
397+
response=response,
398+
conversation_id=chat_log.conversation_id,
399+
continue_conversation=chat_log.continue_conversation,
400+
)
401+
402+
async def _async_handle_chat_log(
403+
self,
404+
chat_log: conversation.ChatLog,
405+
) -> None:
406+
"""Generate an answer for the chat log."""
407+
options = self.entry.options
408+
384409
tools: list[Tool | Callable[..., Any]] | None = None
385410
if chat_log.llm_api:
386411
tools = [
@@ -499,7 +524,9 @@ async def _async_handle_message(
499524
chat = self._genai_client.aio.chats.create(
500525
model=model_name, history=messages, config=generateContentConfig
501526
)
502-
chat_request: str | list[Part] = user_input.text
527+
user_message = chat_log.content[-1]
528+
assert isinstance(user_message, conversation.UserContent)
529+
chat_request: str | list[Part] = user_message.content
503530
# To prevent infinite loops, we limit the number of iterations
504531
for _iteration in range(MAX_TOOL_ITERATIONS):
505532
try:
@@ -519,7 +546,7 @@ async def _async_handle_message(
519546
[
520547
content
521548
async for content in chat_log.async_add_delta_content_stream(
522-
user_input.agent_id,
549+
self.entity_id,
523550
_transform_stream(chat_response_generator),
524551
)
525552
if isinstance(content, conversation.ToolResultContent)
@@ -529,20 +556,6 @@ async def _async_handle_message(
529556
if not chat_log.unresponded_tool_results:
530557
break
531558

532-
response = intent.IntentResponse(language=user_input.language)
533-
if not isinstance(chat_log.content[-1], conversation.AssistantContent):
534-
LOGGER.error(
535-
"Last content in chat log is not an AssistantContent: %s. This could be due to the model not returning a valid response",
536-
chat_log.content[-1],
537-
)
538-
raise HomeAssistantError(f"{ERROR_GETTING_RESPONSE}")
539-
response.async_set_speech(chat_log.content[-1].content or "")
540-
return conversation.ConversationResult(
541-
response=response,
542-
conversation_id=chat_log.conversation_id,
543-
continue_conversation=chat_log.continue_conversation,
544-
)
545-
546559
async def _async_entry_update_listener(
547560
self, hass: HomeAssistant, entry: ConfigEntry
548561
) -> None:

0 commit comments

Comments
 (0)