2525 get_agent_id_by_name
2626)
2727from services .conversation_management_service import (
28+ save_conversation_user ,
2829 get_conversation_list_service ,
2930 create_new_conversation ,
3031 update_conversation_title as update_conversation_title_service ,
@@ -165,7 +166,7 @@ async def start_streaming_chat(
165166 add_mapping_id (internal_id = internal_conversation_id , external_id = external_conversation_id , tenant_id = ctx .tenant_id , user_id = ctx .user_id )
166167
167168 # Get history according to internal_conversation_id
168- history = await get_conversation_history (ctx , external_conversation_id )
169+ history_resp = await get_conversation_history (ctx , external_conversation_id )
169170 agent_id = await get_agent_id_by_name (agent_name = agent_name , tenant_id = ctx .tenant_id )
170171 # Idempotency: only prevent concurrent duplicate starts
171172 composed_key = idempotency_key or _build_idempotency_key (ctx .tenant_id , external_conversation_id , agent_id , query )
@@ -174,11 +175,18 @@ async def start_streaming_chat(
174175 conversation_id = internal_conversation_id ,
175176 agent_id = agent_id ,
176177 query = query ,
177- history = history .get ("history" ),
178+ history = ( history_resp .get ("data" , {})). get ( " history", [] ),
178179 minio_files = None ,
179180 is_debug = False ,
180181 )
181182
183+ # Synchronously persist the user message before starting the stream to avoid race conditions
184+ try :
185+ save_conversation_user (
186+ agent_request , user_id = ctx .user_id , tenant_id = ctx .tenant_id )
187+ except Exception as e :
188+ raise Exception (f"Failed to persist user message: { str (e )} " )
189+
182190 except LimitExceededError as _ :
183191 raise LimitExceededError ("Query rate exceeded limit. Please try again later." )
184192 except UnauthorizedError as _ :
@@ -193,6 +201,7 @@ async def start_streaming_chat(
193201 authorization = ctx .authorization ,
194202 user_id = ctx .user_id ,
195203 tenant_id = ctx .tenant_id ,
204+ skip_user_save = True ,
196205 )
197206 finally :
198207 if composed_key :
0 commit comments