2626)
2727from memos .configs .mem_os import MOSConfig
2828from memos .mem_os .product import MOSProduct
29+ from memos .memos_tools .notification_service import get_error_bot_function , get_online_bot_function
2930
3031
3132logger = logging .getLogger (__name__ )
@@ -49,8 +50,17 @@ def get_mos_product_instance():
4950 # Get default cube config from APIConfig (may be None if disabled)
5051 default_cube_config = APIConfig .get_default_cube_config ()
5152 logger .info (f"*********initdefault_cube_config******** { default_cube_config } " )
53+
54+ # Get DingDing bot functions
55+ dingding_enabled = APIConfig .is_dingding_bot_enabled ()
56+ online_bot = get_online_bot_function () if dingding_enabled else None
57+ error_bot = get_error_bot_function () if dingding_enabled else None
58+
5259 MOS_PRODUCT_INSTANCE = MOSProduct (
53- default_config = mos_config , default_cube_config = default_cube_config
60+ default_config = mos_config ,
61+ default_cube_config = default_cube_config ,
62+ online_bot = online_bot ,
63+ error_bot = error_bot ,
5464 )
5565 logger .info ("MOSProduct instance created successfully with inheritance architecture" )
5666 return MOS_PRODUCT_INSTANCE
@@ -60,15 +70,15 @@ def get_mos_product_instance():
6070
6171
6272@router .post ("/configure" , summary = "Configure MOSProduct" , response_model = SimpleResponse )
63- async def set_config (config ):
73+ def set_config (config ):
6474 """Set MOSProduct configuration."""
6575 global MOS_PRODUCT_INSTANCE
6676 MOS_PRODUCT_INSTANCE = MOSProduct (default_config = config )
6777 return SimpleResponse (message = "Configuration set successfully" )
6878
6979
7080@router .post ("/users/register" , summary = "Register a new user" , response_model = UserRegisterResponse )
71- async def register_user (user_req : UserRegisterRequest , g : Annotated [G , Depends (get_g_object )]):
81+ def register_user (user_req : UserRegisterRequest , g : Annotated [G , Depends (get_g_object )]):
7282 """Register a new user with configuration and default cube."""
7383 try :
7484 # Set request-related information in g object
@@ -113,7 +123,7 @@ async def register_user(user_req: UserRegisterRequest, g: Annotated[G, Depends(g
113123@router .get (
114124 "/suggestions/{user_id}" , summary = "Get suggestion queries" , response_model = SuggestionResponse
115125)
116- async def get_suggestion_queries (user_id : str ):
126+ def get_suggestion_queries (user_id : str ):
117127 """Get suggestion queries for a specific user."""
118128 try :
119129 mos_product = get_mos_product_instance ()
@@ -133,7 +143,7 @@ async def get_suggestion_queries(user_id: str):
133143 summary = "Get suggestion queries with language" ,
134144 response_model = SuggestionResponse ,
135145)
136- async def get_suggestion_queries_post (suggestion_req : SuggestionRequest ):
146+ def get_suggestion_queries_post (suggestion_req : SuggestionRequest ):
137147 """Get suggestion queries for a specific user with language preference."""
138148 try :
139149 mos_product = get_mos_product_instance ()
@@ -151,7 +161,7 @@ async def get_suggestion_queries_post(suggestion_req: SuggestionRequest):
151161
152162
153163@router .post ("/get_all" , summary = "Get all memories for user" , response_model = MemoryResponse )
154- async def get_all_memories (memory_req : GetMemoryRequest ):
164+ def get_all_memories (memory_req : GetMemoryRequest ):
155165 """Get all memories for a specific user."""
156166 try :
157167 mos_product = get_mos_product_instance ()
@@ -178,7 +188,7 @@ async def get_all_memories(memory_req: GetMemoryRequest):
178188
179189
180190@router .post ("/add" , summary = "add a new memory" , response_model = SimpleResponse )
181- async def create_memory (memory_req : MemoryCreateRequest ):
191+ def create_memory (memory_req : MemoryCreateRequest ):
182192 """Create a new memory for a specific user."""
183193 try :
184194 mos_product = get_mos_product_instance ()
@@ -199,7 +209,7 @@ async def create_memory(memory_req: MemoryCreateRequest):
199209
200210
201211@router .post ("/search" , summary = "Search memories" , response_model = SearchResponse )
202- async def search_memories (search_req : SearchRequest ):
212+ def search_memories (search_req : SearchRequest ):
203213 """Search memories for a specific user."""
204214 try :
205215 mos_product = get_mos_product_instance ()
@@ -219,37 +229,38 @@ async def search_memories(search_req: SearchRequest):
219229
220230
221231@router .post ("/chat" , summary = "Chat with MemOS" )
222- async def chat (chat_req : ChatRequest ):
232+ def chat (chat_req : ChatRequest ):
223233 """Chat with MemOS for a specific user. Returns SSE stream."""
224234 try :
225235 mos_product = get_mos_product_instance ()
226236
227- async def generate_chat_response ():
237+ def generate_chat_response ():
228238 """Generate chat response as SSE stream."""
229239 try :
230- import asyncio
231-
232- for chunk in mos_product .chat_with_references (
240+ # Directly yield from the generator without async wrapper
241+ yield from mos_product .chat_with_references (
233242 query = chat_req .query ,
234243 user_id = chat_req .user_id ,
235244 cube_id = chat_req .mem_cube_id ,
236245 history = chat_req .history ,
237246 internet_search = chat_req .internet_search ,
238- ):
239- yield chunk
240- await asyncio .sleep (0.00001 ) # 50ms delay between chunks
247+ )
248+
241249 except Exception as e :
242250 logger .error (f"Error in chat stream: { e } " )
243251 error_data = f"data: { json .dumps ({'type' : 'error' , 'content' : str (traceback .format_exc ())})} \n \n "
244252 yield error_data
245253
246254 return StreamingResponse (
247255 generate_chat_response (),
248- media_type = "text/plain " ,
256+ media_type = "text/event-stream " ,
249257 headers = {
250258 "Cache-Control" : "no-cache" ,
251259 "Connection" : "keep-alive" ,
252260 "Content-Type" : "text/event-stream" ,
261+ "Access-Control-Allow-Origin" : "*" ,
262+ "Access-Control-Allow-Headers" : "*" ,
263+ "Access-Control-Allow-Methods" : "*" ,
253264 },
254265 )
255266
@@ -261,7 +272,7 @@ async def generate_chat_response():
261272
262273
263274@router .get ("/users" , summary = "List all users" , response_model = BaseResponse [list ])
264- async def list_users ():
275+ def list_users ():
265276 """List all registered users."""
266277 try :
267278 mos_product = get_mos_product_instance ()
@@ -289,7 +300,7 @@ async def get_user_info(user_id: str):
289300@router .get (
290301 "/configure/{user_id}" , summary = "Get MOSProduct configuration" , response_model = SimpleResponse
291302)
292- async def get_config (user_id : str ):
303+ def get_config (user_id : str ):
293304 """Get MOSProduct configuration."""
294305 global MOS_PRODUCT_INSTANCE
295306 config = MOS_PRODUCT_INSTANCE .default_config
@@ -299,7 +310,7 @@ async def get_config(user_id: str):
299310@router .get (
300311 "/users/{user_id}/config" , summary = "Get user configuration" , response_model = BaseResponse [dict ]
301312)
302- async def get_user_config (user_id : str ):
313+ def get_user_config (user_id : str ):
303314 """Get user-specific configuration."""
304315 try :
305316 mos_product = get_mos_product_instance ()
@@ -323,7 +334,7 @@ async def get_user_config(user_id: str):
323334@router .put (
324335 "/users/{user_id}/config" , summary = "Update user configuration" , response_model = SimpleResponse
325336)
326- async def update_user_config (user_id : str , config_data : dict ):
337+ def update_user_config (user_id : str , config_data : dict ):
327338 """Update user-specific configuration."""
328339 try :
329340 mos_product = get_mos_product_instance ()
@@ -348,7 +359,7 @@ async def update_user_config(user_id: str, config_data: dict):
348359@router .get (
349360 "/instances/status" , summary = "Get user configuration status" , response_model = BaseResponse [dict ]
350361)
351- async def get_instance_status ():
362+ def get_instance_status ():
352363 """Get information about active user configurations in memory."""
353364 try :
354365 mos_product = get_mos_product_instance ()
@@ -362,7 +373,7 @@ async def get_instance_status():
362373
363374
364375@router .get ("/instances/count" , summary = "Get active user count" , response_model = BaseResponse [int ])
365- async def get_active_user_count ():
376+ def get_active_user_count ():
366377 """Get the number of active user configurations in memory."""
367378 try :
368379 mos_product = get_mos_product_instance ()
0 commit comments