diff --git a/homeassistant/components/conversation/chat_log.py b/homeassistant/components/conversation/chat_log.py index 60c7c7d42e20..c0f867e95daf 100644 --- a/homeassistant/components/conversation/chat_log.py +++ b/homeassistant/components/conversation/chat_log.py @@ -113,6 +113,16 @@ def async_get_chat_log( chat_log = ChatLog(hass, session.conversation_id) is_new_log = True all_chat_logs[session.conversation_id] = chat_log + + chat_log.turn_start = len(chat_log.content) + if user_input is not None: + user_content = UserContent(content=user_input.text) + if is_new_log: + chat_log.content.append(user_content) + else: + chat_log.async_add_user_content(user_content) + + if is_new_log: _async_notify_subscribers( hass, session.conversation_id, @@ -120,10 +130,6 @@ def async_get_chat_log( {"chat_log": chat_log.as_dict()}, ) - chat_log.turn_start = len(chat_log.content) - if user_input is not None: - chat_log.async_add_user_content(UserContent(content=user_input.text)) - if chat_log_delta_listener: chat_log.delta_listener = chat_log_delta_listener diff --git a/tests/components/conversation/test_chat_log.py b/tests/components/conversation/test_chat_log.py index 9602b59eb9ca..a9a9a72b26d2 100644 --- a/tests/components/conversation/test_chat_log.py +++ b/tests/components/conversation/test_chat_log.py @@ -1552,16 +1552,13 @@ async def test_chat_log_subscription( assert external_tool_event["tool_calls"][0].tool_name == "external_api_call" # Verify we received the expected events - # Should have: 1 CREATED event + 7 CONTENT_ADDED events - assert len(received_events) == 8 + # Should have: 1 CREATED event + 6 CONTENT_ADDED events + assert len(received_events) == 7 - # Check the first event is CREATED + # Check the first event is CREATED, with the message of the first turn assert received_events[0][1] == ChatLogEventType.CREATED assert received_events[0][2]["chat_log"]["conversation_id"] == conversation_id - - # Check the second event is CONTENT_ADDED (from mock_conversation_input) - assert received_events[1][1] == ChatLogEventType.CONTENT_ADDED - assert received_events[1][0] == conversation_id + assert received_events[0][2]["chat_log"]["content"][-1]["content"] == "Hello" # Test cleanup functionality assert conversation_id in hass.data[chat_session.DATA_CHAT_SESSION] diff --git a/tests/components/conversation/test_http.py b/tests/components/conversation/test_http.py index 0b22189f5569..a525aba8c373 100644 --- a/tests/components/conversation/test_http.py +++ b/tests/components/conversation/test_http.py @@ -882,7 +882,7 @@ async def test_ws_chat_log_index_subscription( conversation_id = session.conversation_id # We should receive 2 events for this newly created chat: - # 1. The CREATED event (fired before content is added) + # 1. The CREATED event, with the message of the first turn msg = await client.receive_json() assert msg == { "type": "event", @@ -895,7 +895,10 @@ async def test_ws_chat_log_index_subscription( "conversation_id": conversation_id, "continue_conversation": False, "created": now, - "content": [{"role": "system", "content": "", "created": now}], + "content": [ + {"role": "system", "content": "", "created": now}, + {"role": "user", "content": "Hello", "created": now}, + ], } }, },