@@ -217,6 +217,26 @@ def _restore_user_instances(
217217 except Exception as e :
218218 logger .error (f"Error during user instance restoration: { e } " )
219219
220+ def _initialize_cube_from_default_config (
221+ self , cube_id : str , user_id : str , default_config : GeneralMemCubeConfig
222+ ) -> GeneralMemCube | None :
223+ """
224+ Initialize a cube from default configuration when cube path doesn't exist.
225+
226+ Args:
227+ cube_id (str): The cube ID to initialize.
228+ user_id (str): The user ID for the cube.
229+ default_config (GeneralMemCubeConfig): The default configuration to use.
230+ """
231+ cube_config = default_config .model_copy (deep = True )
232+ # Safely modify the graph_db user_name if it exists
233+ if cube_config .text_mem .config .graph_db .config :
234+ cube_config .text_mem .config .graph_db .config .user_name = (
235+ f"memos{ user_id .replace ('-' , '' )} "
236+ )
237+ mem_cube = GeneralMemCube (config = cube_config )
238+ return mem_cube
239+
220240 def _preload_user_cubes (
221241 self , user_id : str , default_cube_config : GeneralMemCubeConfig | None = None
222242 ) -> None :
@@ -286,8 +306,24 @@ def _load_user_cubes(
286306 )
287307 else :
288308 logger .warning (
289- f"Cube path { cube .cube_path } does not exist for cube { cube .cube_id } "
309+ f"Cube path { cube .cube_path } does not exist for cube { cube .cube_id } , now init by default config "
290310 )
311+ cube_obj = self ._initialize_cube_from_default_config (
312+ cube_id = cube .cube_id ,
313+ user_id = user_id ,
314+ default_config = default_cube_config ,
315+ )
316+ if cube_obj :
317+ self .register_mem_cube (
318+ cube_obj ,
319+ cube .cube_id ,
320+ user_id ,
321+ memory_types = [],
322+ )
323+ else :
324+ raise ValueError (
325+ f"Failed to initialize default cube { cube .cube_id } for user { user_id } "
326+ )
291327 except Exception as e :
292328 logger .error (f"Failed to load cube { cube .cube_id } for user { user_id } : { e } " )
293329 logger .info (f"load user { user_id } cubes successfully" )
0 commit comments