Skip to content

Commit 3485c66

Browse files
committed
perf: eliminate pre-read load in socket event emitter
1 parent a55da02 commit 3485c66

1 file changed

Lines changed: 13 additions & 51 deletions

File tree

backend/open_webui/socket/main.py

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -808,25 +808,15 @@ async def __event_emitter__(event_data):
808808
)
809809

810810
elif event_type == 'message':
811-
message = await asyncio.to_thread(
812-
Chats.get_message_by_id_and_message_id,
811+
delta = event_data.get('data', {}).get('content', '')
812+
await asyncio.to_thread(
813+
Chats.upsert_message_to_chat_by_id_and_message_id,
813814
request_info['chat_id'],
814815
request_info['message_id'],
816+
{},
817+
merge_fn=lambda existing: {'content': existing.get('content', '') + delta},
815818
)
816819

817-
if message:
818-
content = message.get('content', '')
819-
content += event_data.get('data', {}).get('content', '')
820-
821-
await asyncio.to_thread(
822-
Chats.upsert_message_to_chat_by_id_and_message_id,
823-
request_info['chat_id'],
824-
request_info['message_id'],
825-
{
826-
'content': content,
827-
},
828-
)
829-
830820
elif event_type == 'replace':
831821
content = event_data.get('data', {}).get('content', '')
832822

@@ -840,62 +830,34 @@ async def __event_emitter__(event_data):
840830
)
841831

842832
elif event_type == 'embeds':
843-
message = await asyncio.to_thread(
844-
Chats.get_message_by_id_and_message_id,
845-
request_info['chat_id'],
846-
request_info['message_id'],
847-
)
848-
849-
embeds = event_data.get('data', {}).get('embeds', [])
850-
embeds.extend(message.get('embeds', []))
851-
833+
new_embeds = event_data.get('data', {}).get('embeds', [])
852834
await asyncio.to_thread(
853835
Chats.upsert_message_to_chat_by_id_and_message_id,
854836
request_info['chat_id'],
855837
request_info['message_id'],
856-
{
857-
'embeds': embeds,
858-
},
838+
{},
839+
merge_fn=lambda existing: {'embeds': new_embeds + existing.get('embeds', [])},
859840
)
860841

861842
elif event_type == 'files':
862-
message = await asyncio.to_thread(
863-
Chats.get_message_by_id_and_message_id,
864-
request_info['chat_id'],
865-
request_info['message_id'],
866-
)
867-
868-
files = event_data.get('data', {}).get('files', [])
869-
files.extend(message.get('files', []))
870-
843+
new_files = event_data.get('data', {}).get('files', [])
871844
await asyncio.to_thread(
872845
Chats.upsert_message_to_chat_by_id_and_message_id,
873846
request_info['chat_id'],
874847
request_info['message_id'],
875-
{
876-
'files': files,
877-
},
848+
{},
849+
merge_fn=lambda existing: {'files': new_files + existing.get('files', [])},
878850
)
879851

880852
elif event_type in ('source', 'citation'):
881853
data = event_data.get('data', {})
882854
if data.get('type') is None:
883-
message = await asyncio.to_thread(
884-
Chats.get_message_by_id_and_message_id,
885-
request_info['chat_id'],
886-
request_info['message_id'],
887-
)
888-
889-
sources = message.get('sources', [])
890-
sources.append(data)
891-
892855
await asyncio.to_thread(
893856
Chats.upsert_message_to_chat_by_id_and_message_id,
894857
request_info['chat_id'],
895858
request_info['message_id'],
896-
{
897-
'sources': sources,
898-
},
859+
{},
860+
merge_fn=lambda existing: {'sources': existing.get('sources', []) + [data]},
899861
)
900862

901863
if 'user_id' in request_info and 'chat_id' in request_info and 'message_id' in request_info:

0 commit comments

Comments
 (0)