main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import asyncio
  2. import random
  3. import socketio
  4. import logging
  5. import sys
  6. import time
  7. from redis import asyncio as aioredis
  8. from open_webui.models.users import Users, UserNameResponse
  9. from open_webui.models.channels import Channels
  10. from open_webui.models.chats import Chats
  11. from open_webui.utils.redis import (
  12. get_sentinels_from_env,
  13. get_sentinel_url_from_env,
  14. )
  15. from open_webui.env import (
  16. ENABLE_WEBSOCKET_SUPPORT,
  17. WEBSOCKET_MANAGER,
  18. WEBSOCKET_REDIS_URL,
  19. WEBSOCKET_REDIS_LOCK_TIMEOUT,
  20. WEBSOCKET_SENTINEL_PORT,
  21. WEBSOCKET_SENTINEL_HOSTS,
  22. )
  23. from open_webui.utils.auth import decode_token
  24. from open_webui.socket.utils import RedisDict, RedisLock
  25. from open_webui.env import (
  26. GLOBAL_LOG_LEVEL,
  27. SRC_LOG_LEVELS,
  28. )
  29. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  30. log = logging.getLogger(__name__)
  31. log.setLevel(SRC_LOG_LEVELS["SOCKET"])
  32. if WEBSOCKET_MANAGER == "redis":
  33. if WEBSOCKET_SENTINEL_HOSTS:
  34. mgr = socketio.AsyncRedisManager(
  35. get_sentinel_url_from_env(
  36. WEBSOCKET_REDIS_URL, WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT
  37. )
  38. )
  39. else:
  40. mgr = socketio.AsyncRedisManager(WEBSOCKET_REDIS_URL)
  41. sio = socketio.AsyncServer(
  42. cors_allowed_origins=[],
  43. async_mode="asgi",
  44. transports=(["websocket"] if ENABLE_WEBSOCKET_SUPPORT else ["polling"]),
  45. allow_upgrades=ENABLE_WEBSOCKET_SUPPORT,
  46. always_connect=True,
  47. client_manager=mgr,
  48. )
  49. else:
  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. )
  57. # Timeout duration in seconds
  58. TIMEOUT_DURATION = 3
  59. # Dictionary to maintain the user pool
  60. if WEBSOCKET_MANAGER == "redis":
  61. log.debug("Using Redis to manage websockets.")
  62. redis_sentinels = get_sentinels_from_env(
  63. WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT
  64. )
  65. SESSION_POOL = RedisDict(
  66. "open-webui:session_pool",
  67. redis_url=WEBSOCKET_REDIS_URL,
  68. redis_sentinels=redis_sentinels,
  69. )
  70. USER_POOL = RedisDict(
  71. "open-webui:user_pool",
  72. redis_url=WEBSOCKET_REDIS_URL,
  73. redis_sentinels=redis_sentinels,
  74. )
  75. USAGE_POOL = RedisDict(
  76. "open-webui:usage_pool",
  77. redis_url=WEBSOCKET_REDIS_URL,
  78. redis_sentinels=redis_sentinels,
  79. )
  80. clean_up_lock = RedisLock(
  81. redis_url=WEBSOCKET_REDIS_URL,
  82. lock_name="usage_cleanup_lock",
  83. timeout_secs=WEBSOCKET_REDIS_LOCK_TIMEOUT,
  84. redis_sentinels=redis_sentinels,
  85. )
  86. aquire_func = clean_up_lock.aquire_lock
  87. renew_func = clean_up_lock.renew_lock
  88. release_func = clean_up_lock.release_lock
  89. else:
  90. SESSION_POOL = {}
  91. USER_POOL = {}
  92. USAGE_POOL = {}
  93. aquire_func = release_func = renew_func = lambda: True
  94. async def periodic_usage_pool_cleanup():
  95. max_retries = 2
  96. retry_delay = random.uniform(
  97. WEBSOCKET_REDIS_LOCK_TIMEOUT / 2, WEBSOCKET_REDIS_LOCK_TIMEOUT
  98. )
  99. for attempt in range(max_retries + 1):
  100. if aquire_func():
  101. break
  102. else:
  103. if attempt < max_retries:
  104. log.debug(
  105. f"Cleanup lock already exists. Retry {attempt + 1} after {retry_delay}s..."
  106. )
  107. await asyncio.sleep(retry_delay)
  108. else:
  109. log.warning(
  110. "Failed to acquire cleanup lock after retries. Skipping cleanup."
  111. )
  112. return
  113. log.debug("Running periodic_cleanup")
  114. try:
  115. while True:
  116. if not renew_func():
  117. log.error(f"Unable to renew cleanup lock. Exiting usage pool cleanup.")
  118. raise Exception("Unable to renew usage pool cleanup lock.")
  119. now = int(time.time())
  120. send_usage = False
  121. for model_id, connections in list(USAGE_POOL.items()):
  122. # Creating a list of sids to remove if they have timed out
  123. expired_sids = [
  124. sid
  125. for sid, details in connections.items()
  126. if now - details["updated_at"] > TIMEOUT_DURATION
  127. ]
  128. for sid in expired_sids:
  129. del connections[sid]
  130. if not connections:
  131. log.debug(f"Cleaning up model {model_id} from usage pool")
  132. del USAGE_POOL[model_id]
  133. else:
  134. USAGE_POOL[model_id] = connections
  135. send_usage = True
  136. await asyncio.sleep(TIMEOUT_DURATION)
  137. finally:
  138. release_func()
  139. app = socketio.ASGIApp(
  140. sio,
  141. socketio_path="/ws/socket.io",
  142. )
  143. def get_models_in_use():
  144. # List models that are currently in use
  145. models_in_use = list(USAGE_POOL.keys())
  146. return models_in_use
  147. def get_active_user_ids():
  148. """Get the list of active user IDs."""
  149. return list(USER_POOL.keys())
  150. def get_user_active_status(user_id):
  151. """Check if a user is currently active."""
  152. return user_id in USER_POOL
  153. def get_user_id_from_session_pool(sid):
  154. user = SESSION_POOL.get(sid)
  155. if user:
  156. return user["id"]
  157. return None
  158. def get_user_ids_from_room(room):
  159. active_session_ids = sio.manager.get_participants(
  160. namespace="/",
  161. room=room,
  162. )
  163. active_user_ids = list(
  164. set(
  165. [SESSION_POOL.get(session_id[0])["id"] for session_id in active_session_ids]
  166. )
  167. )
  168. return active_user_ids
  169. def get_active_status_by_user_id(user_id):
  170. if user_id in USER_POOL:
  171. return True
  172. return False
  173. @sio.on("usage")
  174. async def usage(sid, data):
  175. if sid in SESSION_POOL:
  176. model_id = data["model"]
  177. # Record the timestamp for the last update
  178. current_time = int(time.time())
  179. # Store the new usage data and task
  180. USAGE_POOL[model_id] = {
  181. **(USAGE_POOL[model_id] if model_id in USAGE_POOL else {}),
  182. sid: {"updated_at": current_time},
  183. }
  184. @sio.event
  185. async def connect(sid, environ, auth):
  186. user = None
  187. if auth and "token" in auth:
  188. data = decode_token(auth["token"])
  189. if data is not None and "id" in data:
  190. user = Users.get_user_by_id(data["id"])
  191. if user:
  192. SESSION_POOL[sid] = user.model_dump()
  193. if user.id in USER_POOL:
  194. USER_POOL[user.id] = USER_POOL[user.id] + [sid]
  195. else:
  196. USER_POOL[user.id] = [sid]
  197. @sio.on("user-join")
  198. async def user_join(sid, data):
  199. auth = data["auth"] if "auth" in data else None
  200. if not auth or "token" not in auth:
  201. return
  202. data = decode_token(auth["token"])
  203. if data is None or "id" not in data:
  204. return
  205. user = Users.get_user_by_id(data["id"])
  206. if not user:
  207. return
  208. SESSION_POOL[sid] = user.model_dump()
  209. if user.id in USER_POOL:
  210. USER_POOL[user.id] = USER_POOL[user.id] + [sid]
  211. else:
  212. USER_POOL[user.id] = [sid]
  213. # Join all the channels
  214. channels = Channels.get_channels_by_user_id(user.id)
  215. log.debug(f"{channels=}")
  216. for channel in channels:
  217. await sio.enter_room(sid, f"channel:{channel.id}")
  218. return {"id": user.id, "name": user.name}
  219. @sio.on("join-channels")
  220. async def join_channel(sid, data):
  221. auth = data["auth"] if "auth" in data else None
  222. if not auth or "token" not in auth:
  223. return
  224. data = decode_token(auth["token"])
  225. if data is None or "id" not in data:
  226. return
  227. user = Users.get_user_by_id(data["id"])
  228. if not user:
  229. return
  230. # Join all the channels
  231. channels = Channels.get_channels_by_user_id(user.id)
  232. log.debug(f"{channels=}")
  233. for channel in channels:
  234. await sio.enter_room(sid, f"channel:{channel.id}")
  235. @sio.on("channel-events")
  236. async def channel_events(sid, data):
  237. room = f"channel:{data['channel_id']}"
  238. participants = sio.manager.get_participants(
  239. namespace="/",
  240. room=room,
  241. )
  242. sids = [sid for sid, _ in participants]
  243. if sid not in sids:
  244. return
  245. event_data = data["data"]
  246. event_type = event_data["type"]
  247. if event_type == "typing":
  248. await sio.emit(
  249. "channel-events",
  250. {
  251. "channel_id": data["channel_id"],
  252. "message_id": data.get("message_id", None),
  253. "data": event_data,
  254. "user": UserNameResponse(**SESSION_POOL[sid]).model_dump(),
  255. },
  256. room=room,
  257. )
  258. @sio.event
  259. async def disconnect(sid):
  260. if sid in SESSION_POOL:
  261. user = SESSION_POOL[sid]
  262. del SESSION_POOL[sid]
  263. user_id = user["id"]
  264. USER_POOL[user_id] = [_sid for _sid in USER_POOL[user_id] if _sid != sid]
  265. if len(USER_POOL[user_id]) == 0:
  266. del USER_POOL[user_id]
  267. else:
  268. pass
  269. # print(f"Unknown session ID {sid} disconnected")
  270. def get_event_emitter(request_info, update_db=True):
  271. async def __event_emitter__(event_data):
  272. user_id = request_info["user_id"]
  273. session_ids = list(
  274. set(
  275. USER_POOL.get(user_id, [])
  276. + (
  277. [request_info.get("session_id")]
  278. if request_info.get("session_id")
  279. else []
  280. )
  281. )
  282. )
  283. emit_tasks = [
  284. sio.emit(
  285. "chat-events",
  286. {
  287. "chat_id": request_info.get("chat_id", None),
  288. "message_id": request_info.get("message_id", None),
  289. "data": event_data,
  290. },
  291. to=session_id,
  292. )
  293. for session_id in session_ids
  294. ]
  295. await asyncio.gather(*emit_tasks)
  296. if update_db:
  297. if "type" in event_data and event_data["type"] == "status":
  298. Chats.add_message_status_to_chat_by_id_and_message_id(
  299. request_info["chat_id"],
  300. request_info["message_id"],
  301. event_data.get("data", {}),
  302. )
  303. if "type" in event_data and event_data["type"] == "message":
  304. message = Chats.get_message_by_id_and_message_id(
  305. request_info["chat_id"],
  306. request_info["message_id"],
  307. )
  308. if message:
  309. content = message.get("content", "")
  310. content += event_data.get("data", {}).get("content", "")
  311. Chats.upsert_message_to_chat_by_id_and_message_id(
  312. request_info["chat_id"],
  313. request_info["message_id"],
  314. {
  315. "content": content,
  316. },
  317. )
  318. if "type" in event_data and event_data["type"] == "replace":
  319. content = event_data.get("data", {}).get("content", "")
  320. Chats.upsert_message_to_chat_by_id_and_message_id(
  321. request_info["chat_id"],
  322. request_info["message_id"],
  323. {
  324. "content": content,
  325. },
  326. )
  327. return __event_emitter__
  328. def get_event_call(request_info):
  329. async def __event_caller__(event_data):
  330. response = await sio.call(
  331. "chat-events",
  332. {
  333. "chat_id": request_info.get("chat_id", None),
  334. "message_id": request_info.get("message_id", None),
  335. "data": event_data,
  336. },
  337. to=request_info["session_id"],
  338. )
  339. return response
  340. return __event_caller__
  341. get_event_caller = get_event_call