1
0

main.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. import asyncio
  2. import random
  3. import socketio
  4. import logging
  5. import sys
  6. import time
  7. from typing import Dict, Set
  8. from redis import asyncio as aioredis
  9. import pycrdt as Y
  10. from open_webui.models.users import Users, UserNameResponse
  11. from open_webui.models.channels import Channels
  12. from open_webui.models.chats import Chats
  13. from open_webui.models.notes import Notes, NoteUpdateForm
  14. from open_webui.utils.redis import (
  15. get_sentinels_from_env,
  16. get_sentinel_url_from_env,
  17. )
  18. from open_webui.env import (
  19. ENABLE_WEBSOCKET_SUPPORT,
  20. WEBSOCKET_MANAGER,
  21. WEBSOCKET_REDIS_URL,
  22. WEBSOCKET_REDIS_CLUSTER,
  23. WEBSOCKET_REDIS_LOCK_TIMEOUT,
  24. WEBSOCKET_SENTINEL_PORT,
  25. WEBSOCKET_SENTINEL_HOSTS,
  26. REDIS_KEY_PREFIX,
  27. )
  28. from open_webui.utils.auth import decode_token
  29. from open_webui.socket.utils import RedisDict, RedisLock, YdocManager
  30. from open_webui.tasks import create_task, stop_item_tasks
  31. from open_webui.utils.redis import get_redis_connection
  32. from open_webui.utils.access_control import has_access, get_users_with_access
  33. from open_webui.env import (
  34. GLOBAL_LOG_LEVEL,
  35. SRC_LOG_LEVELS,
  36. )
  37. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  38. log = logging.getLogger(__name__)
  39. log.setLevel(SRC_LOG_LEVELS["SOCKET"])
  40. REDIS = None
  41. if WEBSOCKET_MANAGER == "redis":
  42. if WEBSOCKET_SENTINEL_HOSTS:
  43. mgr = socketio.AsyncRedisManager(
  44. get_sentinel_url_from_env(
  45. WEBSOCKET_REDIS_URL, WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT
  46. )
  47. )
  48. else:
  49. mgr = socketio.AsyncRedisManager(WEBSOCKET_REDIS_URL)
  50. sio = socketio.AsyncServer(
  51. cors_allowed_origins=[],
  52. async_mode="asgi",
  53. transports=(["websocket"] if ENABLE_WEBSOCKET_SUPPORT else ["polling"]),
  54. allow_upgrades=ENABLE_WEBSOCKET_SUPPORT,
  55. always_connect=True,
  56. client_manager=mgr,
  57. )
  58. else:
  59. sio = socketio.AsyncServer(
  60. cors_allowed_origins=[],
  61. async_mode="asgi",
  62. transports=(["websocket"] if ENABLE_WEBSOCKET_SUPPORT else ["polling"]),
  63. allow_upgrades=ENABLE_WEBSOCKET_SUPPORT,
  64. always_connect=True,
  65. )
  66. # Timeout duration in seconds
  67. TIMEOUT_DURATION = 3
  68. # Dictionary to maintain the user pool
  69. if WEBSOCKET_MANAGER == "redis":
  70. log.debug("Using Redis to manage websockets.")
  71. REDIS = get_redis_connection(
  72. redis_url=WEBSOCKET_REDIS_URL,
  73. redis_sentinels=get_sentinels_from_env(
  74. WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT
  75. ),
  76. redis_cluster=WEBSOCKET_REDIS_CLUSTER,
  77. async_mode=True,
  78. )
  79. redis_sentinels = get_sentinels_from_env(
  80. WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT
  81. )
  82. SESSION_POOL = RedisDict(
  83. f"{REDIS_KEY_PREFIX}:session_pool",
  84. redis_url=WEBSOCKET_REDIS_URL,
  85. redis_sentinels=redis_sentinels,
  86. redis_cluster=WEBSOCKET_REDIS_CLUSTER,
  87. )
  88. USER_POOL = RedisDict(
  89. f"{REDIS_KEY_PREFIX}:user_pool",
  90. redis_url=WEBSOCKET_REDIS_URL,
  91. redis_sentinels=redis_sentinels,
  92. redis_cluster=WEBSOCKET_REDIS_CLUSTER,
  93. )
  94. USAGE_POOL = RedisDict(
  95. f"{REDIS_KEY_PREFIX}:usage_pool",
  96. redis_url=WEBSOCKET_REDIS_URL,
  97. redis_sentinels=redis_sentinels,
  98. redis_cluster=WEBSOCKET_REDIS_CLUSTER,
  99. )
  100. clean_up_lock = RedisLock(
  101. redis_url=WEBSOCKET_REDIS_URL,
  102. lock_name="usage_cleanup_lock",
  103. timeout_secs=WEBSOCKET_REDIS_LOCK_TIMEOUT,
  104. redis_sentinels=redis_sentinels,
  105. redis_cluster=WEBSOCKET_REDIS_CLUSTER,
  106. )
  107. aquire_func = clean_up_lock.aquire_lock
  108. renew_func = clean_up_lock.renew_lock
  109. release_func = clean_up_lock.release_lock
  110. else:
  111. SESSION_POOL = {}
  112. USER_POOL = {}
  113. USAGE_POOL = {}
  114. aquire_func = release_func = renew_func = lambda: True
  115. YDOC_MANAGER = YdocManager(
  116. redis=REDIS,
  117. redis_key_prefix=f"{REDIS_KEY_PREFIX}:ydoc:documents",
  118. )
  119. async def periodic_usage_pool_cleanup():
  120. max_retries = 2
  121. retry_delay = random.uniform(
  122. WEBSOCKET_REDIS_LOCK_TIMEOUT / 2, WEBSOCKET_REDIS_LOCK_TIMEOUT
  123. )
  124. for attempt in range(max_retries + 1):
  125. if aquire_func():
  126. break
  127. else:
  128. if attempt < max_retries:
  129. log.debug(
  130. f"Cleanup lock already exists. Retry {attempt + 1} after {retry_delay}s..."
  131. )
  132. await asyncio.sleep(retry_delay)
  133. else:
  134. log.warning(
  135. "Failed to acquire cleanup lock after retries. Skipping cleanup."
  136. )
  137. return
  138. log.debug("Running periodic_cleanup")
  139. try:
  140. while True:
  141. if not renew_func():
  142. log.error(f"Unable to renew cleanup lock. Exiting usage pool cleanup.")
  143. raise Exception("Unable to renew usage pool cleanup lock.")
  144. now = int(time.time())
  145. send_usage = False
  146. for model_id, connections in list(USAGE_POOL.items()):
  147. # Creating a list of sids to remove if they have timed out
  148. expired_sids = [
  149. sid
  150. for sid, details in connections.items()
  151. if now - details["updated_at"] > TIMEOUT_DURATION
  152. ]
  153. for sid in expired_sids:
  154. del connections[sid]
  155. if not connections:
  156. log.debug(f"Cleaning up model {model_id} from usage pool")
  157. del USAGE_POOL[model_id]
  158. else:
  159. USAGE_POOL[model_id] = connections
  160. send_usage = True
  161. await asyncio.sleep(TIMEOUT_DURATION)
  162. finally:
  163. release_func()
  164. app = socketio.ASGIApp(
  165. sio,
  166. socketio_path="/ws/socket.io",
  167. )
  168. def get_models_in_use():
  169. # List models that are currently in use
  170. models_in_use = list(USAGE_POOL.keys())
  171. return models_in_use
  172. def get_active_user_ids():
  173. """Get the list of active user IDs."""
  174. return list(USER_POOL.keys())
  175. def get_user_active_status(user_id):
  176. """Check if a user is currently active."""
  177. return user_id in USER_POOL
  178. def get_user_id_from_session_pool(sid):
  179. user = SESSION_POOL.get(sid)
  180. if user:
  181. return user["id"]
  182. return None
  183. def get_session_ids_from_room(room):
  184. """Get all session IDs from a specific room."""
  185. active_session_ids = sio.manager.get_participants(
  186. namespace="/",
  187. room=room,
  188. )
  189. return [session_id[0] for session_id in active_session_ids]
  190. def get_user_ids_from_room(room):
  191. active_session_ids = get_session_ids_from_room(room)
  192. active_user_ids = list(
  193. set([SESSION_POOL.get(session_id)["id"] for session_id in active_session_ids])
  194. )
  195. return active_user_ids
  196. def get_active_status_by_user_id(user_id):
  197. if user_id in USER_POOL:
  198. return True
  199. return False
  200. @sio.on("usage")
  201. async def usage(sid, data):
  202. if sid in SESSION_POOL:
  203. model_id = data["model"]
  204. # Record the timestamp for the last update
  205. current_time = int(time.time())
  206. # Store the new usage data and task
  207. USAGE_POOL[model_id] = {
  208. **(USAGE_POOL[model_id] if model_id in USAGE_POOL else {}),
  209. sid: {"updated_at": current_time},
  210. }
  211. @sio.event
  212. async def connect(sid, environ, auth):
  213. user = None
  214. if auth and "token" in auth:
  215. data = decode_token(auth["token"])
  216. if data is not None and "id" in data:
  217. user = Users.get_user_by_id(data["id"])
  218. if user:
  219. SESSION_POOL[sid] = user.model_dump()
  220. if user.id in USER_POOL:
  221. USER_POOL[user.id] = USER_POOL[user.id] + [sid]
  222. else:
  223. USER_POOL[user.id] = [sid]
  224. @sio.on("user-join")
  225. async def user_join(sid, data):
  226. auth = data["auth"] if "auth" in data else None
  227. if not auth or "token" not in auth:
  228. return
  229. data = decode_token(auth["token"])
  230. if data is None or "id" not in data:
  231. return
  232. user = Users.get_user_by_id(data["id"])
  233. if not user:
  234. return
  235. SESSION_POOL[sid] = user.model_dump()
  236. if user.id in USER_POOL:
  237. USER_POOL[user.id] = USER_POOL[user.id] + [sid]
  238. else:
  239. USER_POOL[user.id] = [sid]
  240. # Join all the channels
  241. channels = Channels.get_channels_by_user_id(user.id)
  242. log.debug(f"{channels=}")
  243. for channel in channels:
  244. await sio.enter_room(sid, f"channel:{channel.id}")
  245. return {"id": user.id, "name": user.name}
  246. @sio.on("join-channels")
  247. async def join_channel(sid, data):
  248. auth = data["auth"] if "auth" in data else None
  249. if not auth or "token" not in auth:
  250. return
  251. data = decode_token(auth["token"])
  252. if data is None or "id" not in data:
  253. return
  254. user = Users.get_user_by_id(data["id"])
  255. if not user:
  256. return
  257. # Join all the channels
  258. channels = Channels.get_channels_by_user_id(user.id)
  259. log.debug(f"{channels=}")
  260. for channel in channels:
  261. await sio.enter_room(sid, f"channel:{channel.id}")
  262. @sio.on("join-note")
  263. async def join_note(sid, data):
  264. auth = data["auth"] if "auth" in data else None
  265. if not auth or "token" not in auth:
  266. return
  267. token_data = decode_token(auth["token"])
  268. if token_data is None or "id" not in token_data:
  269. return
  270. user = Users.get_user_by_id(token_data["id"])
  271. if not user:
  272. return
  273. note = Notes.get_note_by_id(data["note_id"])
  274. if not note:
  275. log.error(f"Note {data['note_id']} not found for user {user.id}")
  276. return
  277. if (
  278. user.role != "admin"
  279. and user.id != note.user_id
  280. and not has_access(user.id, type="read", access_control=note.access_control)
  281. ):
  282. log.error(f"User {user.id} does not have access to note {data['note_id']}")
  283. return
  284. log.debug(f"Joining note {note.id} for user {user.id}")
  285. await sio.enter_room(sid, f"note:{note.id}")
  286. @sio.on("channel-events")
  287. async def channel_events(sid, data):
  288. room = f"channel:{data['channel_id']}"
  289. participants = sio.manager.get_participants(
  290. namespace="/",
  291. room=room,
  292. )
  293. sids = [sid for sid, _ in participants]
  294. if sid not in sids:
  295. return
  296. event_data = data["data"]
  297. event_type = event_data["type"]
  298. if event_type == "typing":
  299. await sio.emit(
  300. "channel-events",
  301. {
  302. "channel_id": data["channel_id"],
  303. "message_id": data.get("message_id", None),
  304. "data": event_data,
  305. "user": UserNameResponse(**SESSION_POOL[sid]).model_dump(),
  306. },
  307. room=room,
  308. )
  309. @sio.on("ydoc:document:join")
  310. async def ydoc_document_join(sid, data):
  311. """Handle user joining a document"""
  312. user = SESSION_POOL.get(sid)
  313. try:
  314. document_id = data["document_id"]
  315. if document_id.startswith("note:"):
  316. note_id = document_id.split(":")[1]
  317. note = Notes.get_note_by_id(note_id)
  318. if not note:
  319. log.error(f"Note {note_id} not found")
  320. return
  321. if (
  322. user.get("role") != "admin"
  323. and user.get("id") != note.user_id
  324. and not has_access(
  325. user.get("id"), type="read", access_control=note.access_control
  326. )
  327. ):
  328. log.error(
  329. f"User {user.get('id')} does not have access to note {note_id}"
  330. )
  331. return
  332. user_id = data.get("user_id", sid)
  333. user_name = data.get("user_name", "Anonymous")
  334. user_color = data.get("user_color", "#000000")
  335. log.info(f"User {user_id} joining document {document_id}")
  336. await YDOC_MANAGER.add_user(document_id=document_id, user_id=sid)
  337. # Join Socket.IO room
  338. await sio.enter_room(sid, f"doc_{document_id}")
  339. active_session_ids = get_session_ids_from_room(f"doc_{document_id}")
  340. # Get the Yjs document state
  341. ydoc = Y.Doc()
  342. updates = await YDOC_MANAGER.get_updates(document_id)
  343. for update in updates:
  344. ydoc.apply_update(bytes(update))
  345. # Encode the entire document state as an update
  346. state_update = ydoc.get_update()
  347. await sio.emit(
  348. "ydoc:document:state",
  349. {
  350. "document_id": document_id,
  351. "state": list(state_update), # Convert bytes to list for JSON
  352. "sessions": active_session_ids,
  353. },
  354. room=sid,
  355. )
  356. # Notify other users about the new user
  357. await sio.emit(
  358. "ydoc:user:joined",
  359. {
  360. "document_id": document_id,
  361. "user_id": user_id,
  362. "user_name": user_name,
  363. "user_color": user_color,
  364. },
  365. room=f"doc_{document_id}",
  366. skip_sid=sid,
  367. )
  368. log.info(f"User {user_id} successfully joined document {document_id}")
  369. except Exception as e:
  370. log.error(f"Error in yjs_document_join: {e}")
  371. await sio.emit("error", {"message": "Failed to join document"}, room=sid)
  372. async def document_save_handler(document_id, data, user):
  373. if document_id.startswith("note:"):
  374. note_id = document_id.split(":")[1]
  375. note = Notes.get_note_by_id(note_id)
  376. if not note:
  377. log.error(f"Note {note_id} not found")
  378. return
  379. if (
  380. user.get("role") != "admin"
  381. and user.get("id") != note.user_id
  382. and not has_access(
  383. user.get("id"), type="read", access_control=note.access_control
  384. )
  385. ):
  386. log.error(f"User {user.get('id')} does not have access to note {note_id}")
  387. return
  388. Notes.update_note_by_id(note_id, NoteUpdateForm(data=data))
  389. @sio.on("ydoc:document:state")
  390. async def yjs_document_state(sid, data):
  391. """Send the current state of the Yjs document to the user"""
  392. try:
  393. document_id = data["document_id"]
  394. room = f"doc_{document_id}"
  395. active_session_ids = get_session_ids_from_room(room)
  396. if sid not in active_session_ids:
  397. log.warning(f"Session {sid} not in room {room}. Cannot send state.")
  398. return
  399. if not await YDOC_MANAGER.document_exists(document_id):
  400. log.warning(f"Document {document_id} not found")
  401. return
  402. # Get the Yjs document state
  403. ydoc = Y.Doc()
  404. updates = await YDOC_MANAGER.get_updates(document_id)
  405. for update in updates:
  406. ydoc.apply_update(bytes(update))
  407. # Encode the entire document state as an update
  408. state_update = ydoc.get_update()
  409. await sio.emit(
  410. "ydoc:document:state",
  411. {
  412. "document_id": document_id,
  413. "state": list(state_update), # Convert bytes to list for JSON
  414. "sessions": active_session_ids,
  415. },
  416. room=sid,
  417. )
  418. except Exception as e:
  419. log.error(f"Error in yjs_document_state: {e}")
  420. @sio.on("ydoc:document:update")
  421. async def yjs_document_update(sid, data):
  422. """Handle Yjs document updates"""
  423. try:
  424. document_id = data["document_id"]
  425. try:
  426. await stop_item_tasks(REDIS, document_id)
  427. except:
  428. pass
  429. user_id = data.get("user_id", sid)
  430. update = data["update"] # List of bytes from frontend
  431. await YDOC_MANAGER.append_to_updates(
  432. document_id=document_id,
  433. update=update, # Convert list of bytes to bytes
  434. )
  435. # Broadcast update to all other users in the document
  436. await sio.emit(
  437. "ydoc:document:update",
  438. {
  439. "document_id": document_id,
  440. "user_id": user_id,
  441. "update": update,
  442. "socket_id": sid, # Add socket_id to match frontend filtering
  443. },
  444. room=f"doc_{document_id}",
  445. skip_sid=sid,
  446. )
  447. async def debounced_save():
  448. await asyncio.sleep(0.5)
  449. await document_save_handler(
  450. document_id, data.get("data", {}), SESSION_POOL.get(sid)
  451. )
  452. if data.get("data"):
  453. await create_task(REDIS, debounced_save(), document_id)
  454. except Exception as e:
  455. log.error(f"Error in yjs_document_update: {e}")
  456. @sio.on("ydoc:document:leave")
  457. async def yjs_document_leave(sid, data):
  458. """Handle user leaving a document"""
  459. try:
  460. document_id = data["document_id"]
  461. user_id = data.get("user_id", sid)
  462. log.info(f"User {user_id} leaving document {document_id}")
  463. # Remove user from the document
  464. await YDOC_MANAGER.remove_user(document_id=document_id, user_id=sid)
  465. # Leave Socket.IO room
  466. await sio.leave_room(sid, f"doc_{document_id}")
  467. # Notify other users
  468. await sio.emit(
  469. "ydoc:user:left",
  470. {"document_id": document_id, "user_id": user_id},
  471. room=f"doc_{document_id}",
  472. )
  473. if (
  474. await YDOC_MANAGER.document_exists(document_id)
  475. and len(await YDOC_MANAGER.get_users(document_id)) == 0
  476. ):
  477. log.info(f"Cleaning up document {document_id} as no users are left")
  478. await YDOC_MANAGER.clear_document(document_id)
  479. except Exception as e:
  480. log.error(f"Error in yjs_document_leave: {e}")
  481. @sio.on("ydoc:awareness:update")
  482. async def yjs_awareness_update(sid, data):
  483. """Handle awareness updates (cursors, selections, etc.)"""
  484. try:
  485. document_id = data["document_id"]
  486. user_id = data.get("user_id", sid)
  487. update = data["update"]
  488. # Broadcast awareness update to all other users in the document
  489. await sio.emit(
  490. "ydoc:awareness:update",
  491. {"document_id": document_id, "user_id": user_id, "update": update},
  492. room=f"doc_{document_id}",
  493. skip_sid=sid,
  494. )
  495. except Exception as e:
  496. log.error(f"Error in yjs_awareness_update: {e}")
  497. @sio.event
  498. async def disconnect(sid):
  499. if sid in SESSION_POOL:
  500. user = SESSION_POOL[sid]
  501. del SESSION_POOL[sid]
  502. user_id = user["id"]
  503. USER_POOL[user_id] = [_sid for _sid in USER_POOL[user_id] if _sid != sid]
  504. if len(USER_POOL[user_id]) == 0:
  505. del USER_POOL[user_id]
  506. await YDOC_MANAGER.remove_user_from_all_documents(sid)
  507. else:
  508. pass
  509. # print(f"Unknown session ID {sid} disconnected")
  510. def get_event_emitter(request_info, update_db=True):
  511. async def __event_emitter__(event_data):
  512. user_id = request_info["user_id"]
  513. session_ids = list(
  514. set(
  515. USER_POOL.get(user_id, [])
  516. + (
  517. [request_info.get("session_id")]
  518. if request_info.get("session_id")
  519. else []
  520. )
  521. )
  522. )
  523. emit_tasks = [
  524. sio.emit(
  525. "chat-events",
  526. {
  527. "chat_id": request_info.get("chat_id", None),
  528. "message_id": request_info.get("message_id", None),
  529. "data": event_data,
  530. },
  531. to=session_id,
  532. )
  533. for session_id in session_ids
  534. ]
  535. await asyncio.gather(*emit_tasks)
  536. if update_db:
  537. if "type" in event_data and event_data["type"] == "status":
  538. Chats.add_message_status_to_chat_by_id_and_message_id(
  539. request_info["chat_id"],
  540. request_info["message_id"],
  541. event_data.get("data", {}),
  542. )
  543. if "type" in event_data and event_data["type"] == "message":
  544. message = Chats.get_message_by_id_and_message_id(
  545. request_info["chat_id"],
  546. request_info["message_id"],
  547. )
  548. if message:
  549. content = message.get("content", "")
  550. content += event_data.get("data", {}).get("content", "")
  551. Chats.upsert_message_to_chat_by_id_and_message_id(
  552. request_info["chat_id"],
  553. request_info["message_id"],
  554. {
  555. "content": content,
  556. },
  557. )
  558. if "type" in event_data and event_data["type"] == "replace":
  559. content = event_data.get("data", {}).get("content", "")
  560. Chats.upsert_message_to_chat_by_id_and_message_id(
  561. request_info["chat_id"],
  562. request_info["message_id"],
  563. {
  564. "content": content,
  565. },
  566. )
  567. return __event_emitter__
  568. def get_event_call(request_info):
  569. async def __event_caller__(event_data):
  570. response = await sio.call(
  571. "chat-events",
  572. {
  573. "chat_id": request_info.get("chat_id", None),
  574. "message_id": request_info.get("message_id", None),
  575. "data": event_data,
  576. },
  577. to=request_info["session_id"],
  578. )
  579. return response
  580. return __event_caller__
  581. get_event_caller = get_event_call