Avoid duplicate chat log content (#139679)

This commit is contained in:
Paulus Schoutsen 2025-03-03 05:46:40 -05:00 committed by Franck Nijhof
parent 03cb177e7c
commit dca77e8232
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 7 additions and 1 deletions

View File

@ -49,7 +49,11 @@ def async_get_chat_log(
raise RuntimeError(
"Cannot attach chat log delta listener unless initial caller"
)
if user_input is not None:
if user_input is not None and (
(content := chat_log.content[-1]).role != "user"
# MyPy doesn't understand that content is a UserContent here
or content.content != user_input.text # type: ignore[union-attr]
):
chat_log.async_add_user_content(UserContent(content=user_input.text))
yield chat_log

View File

@ -86,7 +86,9 @@ async def test_default_content(
with (
chat_session.async_get_chat_session(hass) as session,
async_get_chat_log(hass, session, mock_conversation_input) as chat_log,
async_get_chat_log(hass, session, mock_conversation_input) as chat_log2,
):
assert chat_log is chat_log2
assert len(chat_log.content) == 2
assert chat_log.content[0].role == "system"
assert chat_log.content[0].content == ""