mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-01 01:12:43 -05:00
The admin user list, feedbacks, groups, the workspace models, prompts and skills lists, prompt history, notes, shared chats, automations, calendar search, analytics messages and SCIM user listings are loaded page by page, and when many rows share the sorted value (a role, a bulk-import timestamp, a rating) the database is free to return those ties in a different order for each page. On PostgreSQL 16, paging through 300 users sorted by role showed 16 users twice and never showed 16 others, and with an id tiebreaker every user showed up exactly once. These lists now sort by each entry's id as a last step, as the chats and knowledge lists already do. Fixes #35280
243 lines
8.3 KiB
Python
243 lines
8.3 KiB
Python
import logging
|
|
import time
|
|
import uuid
|
|
from typing import Literal, Optional
|
|
|
|
from open_webui.internal.db import Base, JSONField, get_async_db_context
|
|
from pydantic import BaseModel, ConfigDict
|
|
from sqlalchemy import JSON, BigInteger, Column, ForeignKey, Text, delete, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
ChatShareMode = Literal['continue'] | None
|
|
|
|
|
|
class ShareChatForm(BaseModel):
|
|
share_mode: ChatShareMode = None
|
|
|
|
|
|
# SharedChat DB Schema
|
|
|
|
|
|
class SharedChat(Base):
|
|
__tablename__ = 'shared_chat'
|
|
|
|
id = Column(Text, primary_key=True) # The share token (UUID) — used in /s/{id} URL
|
|
chat_id = Column(Text, ForeignKey('chat.id', ondelete='CASCADE'), nullable=False)
|
|
user_id = Column(Text, nullable=False) # Who created this share
|
|
|
|
title = Column(Text)
|
|
chat = Column(JSON) # Snapshot of chat JSON at share time
|
|
|
|
created_at = Column(BigInteger)
|
|
updated_at = Column(BigInteger)
|
|
|
|
|
|
class SharedChatModel(BaseModel):
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: str
|
|
chat_id: str
|
|
user_id: str
|
|
|
|
title: str
|
|
chat: dict
|
|
|
|
created_at: int
|
|
updated_at: int
|
|
|
|
|
|
class SharedChatResponse(BaseModel):
|
|
id: str
|
|
chat_id: str
|
|
title: str
|
|
share_id: Optional[str] = None # Alias for id, for backward compat
|
|
updated_at: int
|
|
created_at: int
|
|
|
|
|
|
# Table Operations
|
|
|
|
|
|
class SharedChatsTable:
|
|
async def create(
|
|
self, chat_id: str, user_id: str, db: Optional[AsyncSession] = None, *, share_mode: ChatShareMode = None
|
|
) -> Optional[SharedChatModel]:
|
|
"""
|
|
Create a snapshot of the chat for link sharing.
|
|
Returns the SharedChatModel with the share token as its id.
|
|
"""
|
|
async with get_async_db_context(db) as db:
|
|
from open_webui.models.chats import Chats
|
|
|
|
chat = await Chats.get_chat_by_id(chat_id, db=db)
|
|
if not chat:
|
|
return None
|
|
|
|
share_id = str(uuid.uuid4())
|
|
now = int(time.time())
|
|
|
|
shared_chat = SharedChat(
|
|
id=share_id,
|
|
chat_id=chat_id,
|
|
user_id=user_id,
|
|
title=chat.title,
|
|
chat={**chat.chat, 'share_mode': share_mode},
|
|
created_at=now,
|
|
updated_at=now,
|
|
)
|
|
db.add(shared_chat)
|
|
await db.commit()
|
|
await db.refresh(shared_chat)
|
|
|
|
return SharedChatModel.model_validate(shared_chat)
|
|
|
|
async def update(
|
|
self, share_id: str, form_data: ShareChatForm | None = None, db: Optional[AsyncSession] = None
|
|
) -> Optional[SharedChatModel]:
|
|
"""
|
|
Re-snapshot: update the shared chat with the current state of the original chat.
|
|
"""
|
|
async with get_async_db_context(db) as db:
|
|
from open_webui.models.chats import Chats
|
|
|
|
shared_chat = await db.get(SharedChat, share_id)
|
|
if not shared_chat:
|
|
return None
|
|
|
|
chat = await Chats.get_chat_by_id(shared_chat.chat_id, db=db)
|
|
if not chat:
|
|
return None
|
|
|
|
shared_chat.title = chat.title
|
|
shared_chat.chat = {
|
|
**chat.chat,
|
|
'share_mode': shared_chat.chat.get('share_mode'),
|
|
**(form_data.model_dump(exclude_unset=True) if form_data else {}),
|
|
}
|
|
shared_chat.updated_at = int(time.time())
|
|
|
|
await db.commit()
|
|
await db.refresh(shared_chat)
|
|
return SharedChatModel.model_validate(shared_chat)
|
|
|
|
async def set_share_mode(self, share_id: str, share_mode: ChatShareMode, db: Optional[AsyncSession] = None):
|
|
async with get_async_db_context(db) as db:
|
|
shared = await db.get(SharedChat, share_id)
|
|
if shared:
|
|
if share_mode is None and shared.chat.get('share_mode') == 'continue':
|
|
from open_webui.models.chats import Chats
|
|
|
|
chat = await Chats.get_chat_by_id(shared.chat_id, db=db)
|
|
if chat:
|
|
shared.chat = chat.chat
|
|
shared.title = chat.title
|
|
shared.chat = {**shared.chat, 'share_mode': share_mode}
|
|
await db.commit()
|
|
|
|
async def get_by_id(self, share_id: str, db: Optional[AsyncSession] = None) -> Optional[SharedChatModel]:
|
|
"""Get a shared chat by its share token."""
|
|
async with get_async_db_context(db) as db:
|
|
shared_chat = await db.get(SharedChat, share_id)
|
|
if shared_chat:
|
|
return SharedChatModel.model_validate(shared_chat)
|
|
return None
|
|
|
|
async def get_by_chat_id(self, chat_id: str, db: Optional[AsyncSession] = None) -> Optional[SharedChatModel]:
|
|
"""Get the shared chat for a given original chat. Returns the most recent one."""
|
|
async with get_async_db_context(db) as db:
|
|
result = await db.execute(
|
|
select(SharedChat).filter_by(chat_id=chat_id).order_by(SharedChat.updated_at.desc()).limit(1)
|
|
)
|
|
shared_chat = result.scalars().first()
|
|
if shared_chat:
|
|
return SharedChatModel.model_validate(shared_chat)
|
|
return None
|
|
|
|
async def get_by_user_id(
|
|
self,
|
|
user_id: str,
|
|
filter: Optional[dict] = None,
|
|
skip: int = 0,
|
|
limit: int = 50,
|
|
db: Optional[AsyncSession] = None,
|
|
) -> list[SharedChatResponse]:
|
|
"""List all shared chats created by a user."""
|
|
async with get_async_db_context(db) as db:
|
|
stmt = select(SharedChat).filter_by(user_id=user_id)
|
|
|
|
if filter:
|
|
query_key = filter.get('query')
|
|
if query_key:
|
|
stmt = stmt.filter(SharedChat.title.ilike(f'%{query_key}%'))
|
|
|
|
order_by = filter.get('order_by')
|
|
direction = filter.get('direction')
|
|
|
|
if order_by and direction:
|
|
col = getattr(SharedChat, order_by, None)
|
|
if not col:
|
|
raise ValueError('Invalid order_by field')
|
|
if direction.lower() == 'asc':
|
|
stmt = stmt.order_by(col.asc())
|
|
elif direction.lower() == 'desc':
|
|
stmt = stmt.order_by(col.desc())
|
|
else:
|
|
raise ValueError('Invalid direction for ordering')
|
|
else:
|
|
stmt = stmt.order_by(SharedChat.updated_at.desc())
|
|
|
|
stmt = stmt.order_by(SharedChat.id)
|
|
|
|
if skip:
|
|
stmt = stmt.offset(skip)
|
|
if limit:
|
|
stmt = stmt.limit(limit)
|
|
|
|
result = await db.execute(stmt)
|
|
return [
|
|
SharedChatResponse(
|
|
id=sc.chat_id,
|
|
chat_id=sc.chat_id,
|
|
title=sc.title,
|
|
share_id=sc.id,
|
|
updated_at=sc.updated_at,
|
|
created_at=sc.created_at,
|
|
)
|
|
for sc in result.scalars().all()
|
|
]
|
|
|
|
async def delete_by_id(self, share_id: str, db: Optional[AsyncSession] = None) -> bool:
|
|
"""Delete a shared chat by its share token."""
|
|
try:
|
|
async with get_async_db_context(db) as db:
|
|
await db.execute(delete(SharedChat).filter_by(id=share_id))
|
|
await db.commit()
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
async def delete_by_chat_id(self, chat_id: str, db: Optional[AsyncSession] = None) -> bool:
|
|
"""Delete all shared chats for a given original chat."""
|
|
try:
|
|
async with get_async_db_context(db) as db:
|
|
await db.execute(delete(SharedChat).filter_by(chat_id=chat_id))
|
|
await db.commit()
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
async def delete_all_by_user_id(self, user_id: str, db: Optional[AsyncSession] = None) -> bool:
|
|
"""Delete all shared chats created by a user."""
|
|
try:
|
|
async with get_async_db_context(db) as db:
|
|
await db.execute(delete(SharedChat).filter_by(user_id=user_id))
|
|
await db.commit()
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
SharedChats = SharedChatsTable()
|