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,20 @@ 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_resp shape: {"message": "success", "data": {"conversation_id": str, "history": [...]}, ...}
179+ history = (history_resp .get ("data" , {})).get ("history" , []),
178180 minio_files = None ,
179181 is_debug = False ,
180182 )
181183
184+ # Synchronously persist the user message before starting the stream to avoid race conditions
185+ try :
186+ save_conversation_user (
187+ agent_request , user_id = ctx .user_id , tenant_id = ctx .tenant_id )
188+ except Exception as persist_err :
189+ # Normalize error and continue to surface via outer exception path
190+ raise Exception (f"Failed to persist user message: { persist_err } " )
191+
182192 except LimitExceededError as _ :
183193 raise LimitExceededError ("Query rate exceeded limit. Please try again later." )
184194 except UnauthorizedError as _ :
@@ -193,6 +203,7 @@ async def start_streaming_chat(
193203 authorization = ctx .authorization ,
194204 user_id = ctx .user_id ,
195205 tenant_id = ctx .tenant_id ,
206+ skip_user_save = True ,
196207 )
197208 finally :
198209 if composed_key :
0 commit comments