@@ -383,6 +383,10 @@ def generate_chat_response() -> Generator[str, None, None]:
383383 readable_cube_ids = chat_req .readable_cube_ids or (
384384 [chat_req .mem_cube_id ] if chat_req .mem_cube_id else [chat_req .user_id ]
385385 )
386+ # Resolve writable cube IDs (for add)
387+ writable_cube_ids = chat_req .writable_cube_ids or (
388+ [chat_req .mem_cube_id ] if chat_req .mem_cube_id else [chat_req .user_id ]
389+ )
386390
387391 search_req = APISearchRequest (
388392 query = chat_req .query ,
@@ -399,6 +403,15 @@ def generate_chat_response() -> Generator[str, None, None]:
399403 )
400404
401405 search_response = self .search_handler .handle_search_memories (search_req )
406+ # for playground, add the query to memory without response
407+ self ._start_add_to_memory (
408+ user_id = chat_req .user_id ,
409+ writable_cube_ids = writable_cube_ids ,
410+ session_id = chat_req .session_id or "default_session" ,
411+ query = chat_req .query ,
412+ full_response = None ,
413+ async_mode = "sync" ,
414+ )
402415
403416 yield f"data: { json .dumps ({'type' : 'status' , 'data' : '1' })} \n \n "
404417 # Use first readable cube ID for scheduler (backward compatibility)
@@ -541,11 +554,6 @@ def generate_chat_response() -> Generator[str, None, None]:
541554 speed_improvement = speed_improvement ,
542555 current_messages = current_messages ,
543556 )
544-
545- # Resolve writable cube IDs (for add)
546- writable_cube_ids = chat_req .writable_cube_ids or (
547- [chat_req .mem_cube_id ] if chat_req .mem_cube_id else [chat_req .user_id ]
548- )
549557 self ._start_add_to_memory (
550558 user_id = chat_req .user_id ,
551559 writable_cube_ids = writable_cube_ids ,
@@ -907,25 +915,29 @@ async def _add_conversation_to_memory(
907915 writable_cube_ids : list [str ],
908916 session_id : str ,
909917 query : str ,
910- clean_response : str ,
918+ clean_response : str | None = None ,
911919 async_mode : Literal ["async" , "sync" ] = "sync" ,
912920 ) -> None :
913- add_req = APIADDRequest (
914- user_id = user_id ,
915- writable_cube_ids = writable_cube_ids ,
916- session_id = session_id ,
917- messages = [
918- {
919- "role" : "user" ,
920- "content" : query ,
921- "chat_time" : str (datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )),
922- },
921+ messages = [
922+ {
923+ "role" : "user" ,
924+ "content" : query ,
925+ "chat_time" : str (datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )),
926+ }
927+ ]
928+ if clean_response :
929+ messages .append (
923930 {
924931 "role" : "assistant" ,
925932 "content" : clean_response ,
926933 "chat_time" : str (datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )),
927- },
928- ],
934+ }
935+ )
936+ add_req = APIADDRequest (
937+ user_id = user_id ,
938+ writable_cube_ids = writable_cube_ids ,
939+ session_id = session_id ,
940+ messages = messages ,
929941 async_mode = async_mode ,
930942 )
931943
@@ -1130,15 +1142,17 @@ def _start_add_to_memory(
11301142 writable_cube_ids : list [str ],
11311143 session_id : str ,
11321144 query : str ,
1133- full_response : str ,
1145+ full_response : str | None = None ,
11341146 async_mode : Literal ["async" , "sync" ] = "sync" ,
11351147 ) -> None :
11361148 def run_async_in_thread ():
11371149 try :
11381150 loop = asyncio .new_event_loop ()
11391151 asyncio .set_event_loop (loop )
11401152 try :
1141- clean_response , _ = self ._extract_references_from_response (full_response )
1153+ clean_response = full_response
1154+ if full_response :
1155+ clean_response , _ = self ._extract_references_from_response (full_response )
11421156 loop .run_until_complete (
11431157 self ._add_conversation_to_memory (
11441158 user_id = user_id ,
@@ -1159,7 +1173,9 @@ def run_async_in_thread():
11591173
11601174 try :
11611175 asyncio .get_running_loop ()
1162- clean_response , _ = self ._extract_references_from_response (full_response )
1176+ clean_response = full_response
1177+ if full_response :
1178+ clean_response , _ = self ._extract_references_from_response (full_response )
11631179 task = asyncio .create_task (
11641180 self ._add_conversation_to_memory (
11651181 user_id = user_id ,
0 commit comments