mirror of
https://github.com/open-webui/open-webui.git
synced 2026-10-29 06:32:52 +00:00
refac: record chat messages under the signed-in user on save (#32872)
Chats that are created, imported or saved now record their incoming messages under the signed-in user.
This commit is contained in:
parent
91ce3b3358
commit
d1776b18a3
1 changed files with 15 additions and 0 deletions
|
|
@ -37,6 +37,7 @@ from open_webui.models.config import Config
|
|||
from open_webui.models.folders import Folders
|
||||
from open_webui.models.shared_chats import ChatShareMode, ShareChatForm, SharedChatResponse, SharedChats
|
||||
from open_webui.models.tags import TagModel, Tags
|
||||
from open_webui.models.users import UserModel
|
||||
from open_webui.socket.main import get_event_emitter
|
||||
from open_webui.tasks import get_response_streams_by_chat_id, has_active_tasks, stop_item_tasks
|
||||
from open_webui.utils.access_control import filter_allowed_access_grants, has_permission
|
||||
|
|
@ -167,6 +168,16 @@ async def shared_chat_response(chat, user=None, db=None):
|
|||
return data
|
||||
|
||||
|
||||
def set_message_authors(chat: dict, user: UserModel) -> None:
|
||||
"""Attribute client-supplied messages to the user."""
|
||||
messages = list((chat.get('history', {}).get('messages') or {}).values())
|
||||
messages.extend(chat.get('messages') or [])
|
||||
for message in messages:
|
||||
message['user_id'] = user.id
|
||||
if message.get('role') == 'user':
|
||||
message['user'] = {'id': user.id, 'name': user.name}
|
||||
|
||||
|
||||
async def add_active_state_to_chat_list(
|
||||
request: Request, chat_list: list[ChatTitleIdResponse]
|
||||
) -> list[ChatTitleIdResponse]:
|
||||
|
|
@ -832,6 +843,7 @@ async def create_new_chat(
|
|||
)
|
||||
|
||||
try:
|
||||
set_message_authors(form_data.chat, user)
|
||||
chat = await Chats.insert_new_chat(str(uuid4()), user.id, form_data, db=db)
|
||||
await publish_event(
|
||||
request,
|
||||
|
|
@ -863,6 +875,8 @@ async def import_chats(
|
|||
await require_chat_import_permission(request, user, db)
|
||||
|
||||
try:
|
||||
for chat_form in form_data.chats:
|
||||
set_message_authors(chat_form.chat, user)
|
||||
chats = await Chats.import_chats(user.id, form_data.chats, db=db)
|
||||
await Tags.ensure_tags_exist(
|
||||
list({tag_id for chat in chats for tag_id in (chat.meta or {}).get('tags', [])}), user.id, db=db
|
||||
|
|
@ -1467,6 +1481,7 @@ async def update_chat_by_id(
|
|||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT('Invalid title'))
|
||||
form_data.chat['title'] = title.strip()
|
||||
if chat:
|
||||
set_message_authors(form_data.chat, user)
|
||||
touch = 'history' in form_data.chat or 'messages' in form_data.chat
|
||||
chat = await Chats.update_chat_by_id(id, form_data.chat, db=db, touch=touch)
|
||||
if form_data.variables is not None:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue