mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-03 18:17:56 -05:00
refac
This commit is contained in:
1 parent
880f9aaab3
commit
209ffacbf2
3 files changed
+19
-31
No files matched your search
@@ -1137,28 +1137,33 @@ async def generate_chat_export_ndjson(user_id: str):
|
||||
"""
|
||||
Async generator that streams all user chats as NDJSON (one JSON object per line).
|
||||
|
||||
Uses short-lived DB sessions per batch to avoid holding locks for the
|
||||
entire duration, which is critical for SQLite environments.
|
||||
Pages through chat ids and loads one full chat at a time, so memory never
|
||||
holds more than the largest chat. Uses short-lived DB sessions to avoid
|
||||
holding locks for the entire duration, which is critical for SQLite.
|
||||
"""
|
||||
skip = 0
|
||||
|
||||
while True:
|
||||
result = await Chats.get_chats_by_user_id(
|
||||
chat_list = await Chats.get_chat_list_by_user_id(
|
||||
user_id,
|
||||
include_archived=True,
|
||||
skip=skip,
|
||||
limit=CHAT_EXPORT_BATCH_SIZE,
|
||||
db=None,
|
||||
)
|
||||
if not result.items:
|
||||
if not chat_list:
|
||||
break
|
||||
|
||||
for chat in await chat_responses(result.items):
|
||||
for item in chat_list:
|
||||
chat = await Chats.get_chat_by_id(item.id)
|
||||
if chat is None:
|
||||
log.warning(f'Skipping chat {item.id} in export: it could not be loaded')
|
||||
continue
|
||||
try:
|
||||
yield chat.model_dump_json() + '\n'
|
||||
yield (await chat_response(chat)).model_dump_json() + '\n'
|
||||
except Exception as e:
|
||||
log.exception(f'Error serializing chat {chat.id}: {e}')
|
||||
|
||||
if len(result.items) < CHAT_EXPORT_BATCH_SIZE:
|
||||
if len(chat_list) < CHAT_EXPORT_BATCH_SIZE:
|
||||
break
|
||||
|
||||
skip += CHAT_EXPORT_BATCH_SIZE
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import logging
|
||||
import random
|
||||
import sys
|
||||
@@ -1625,11 +1624,7 @@ async def get_event_emitter(request_info, update_db=True):
|
||||
if (request_info.get('chat_id') or '').startswith('channel:'):
|
||||
return await _make_channel_emitter(request_info)
|
||||
|
||||
last_shared_emit = 0.0
|
||||
output = None
|
||||
|
||||
async def __event_emitter__(event_data):
|
||||
nonlocal last_shared_emit, output
|
||||
user_id = request_info['user_id']
|
||||
chat_id = request_info['chat_id']
|
||||
message_id = request_info['message_id']
|
||||
@@ -1683,27 +1678,13 @@ async def get_event_emitter(request_info, update_db=True):
|
||||
'chat:message:error',
|
||||
'chat:tasks:cancel',
|
||||
'chat:message:follow_ups',
|
||||
'response:completion',
|
||||
}:
|
||||
shared_event = event_data
|
||||
elif event_type in {'chat:completion', 'response:completion'}:
|
||||
elif event_type == 'chat:completion':
|
||||
data = event_data.get('data') or {}
|
||||
if isinstance(data.get('output'), list):
|
||||
output = copy.deepcopy(data['output'])
|
||||
elif event_type == 'response:completion':
|
||||
from open_webui.utils.response import handle_responses_streaming_event
|
||||
|
||||
output, _ = handle_responses_streaming_event(data, output or [])
|
||||
now = time.monotonic()
|
||||
if not (data.get('type') or '').endswith('.delta') or now - last_shared_emit >= 0.15:
|
||||
last_shared_emit = now
|
||||
payload = {
|
||||
key: value
|
||||
for key, value in data.items()
|
||||
if key in {'done', 'error', 'usage', 'finish_reason', 'content', 'selected_model_id', 'sources'}
|
||||
}
|
||||
if output is not None:
|
||||
payload['output'] = output
|
||||
shared_event = {'type': 'chat:completion', 'data': payload}
|
||||
keys = {'done', 'error', 'usage', 'finish_reason', 'content', 'selected_model_id', 'sources', 'output'}
|
||||
shared_event = {'type': event_type, 'data': {key: value for key, value in data.items() if key in keys}}
|
||||
if shared_event:
|
||||
await sio.emit(
|
||||
'events',
|
||||
|
||||
@@ -5567,6 +5567,8 @@
|
||||
if (Object.keys(params).length > 0) {
|
||||
await updateChatParams(localStorage.token, res.chat_id, params);
|
||||
}
|
||||
// Load the chat the backend created so later Controls changes save to it.
|
||||
void refreshChat();
|
||||
}
|
||||
await processNextInQueue(res.chat_id);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user