main.py 23 KB

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