From 679087e7b36118f8996efb757cd01a2a8689ef47 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 21 Nov 2026 18:55:53 +0400 Subject: [PATCH] refac --- backend/open_webui/models/chats.py | 15 ++-- backend/open_webui/routers/chats.py | 6 +- src/lib/apis/chats/index.ts | 17 ++++- src/lib/components/layout/Sidebar.svelte | 71 ++++++++++++++++++- .../components/layout/Sidebar/ChatItem.svelte | 14 ++-- src/lib/i18n/locales/ar-BH/translation.json | 2 + src/lib/i18n/locales/ar/translation.json | 2 + src/lib/i18n/locales/az-AZ/translation.json | 2 + src/lib/i18n/locales/bg-BG/translation.json | 2 + src/lib/i18n/locales/bn-BD/translation.json | 2 + src/lib/i18n/locales/bo-TB/translation.json | 2 + src/lib/i18n/locales/bs-BA/translation.json | 2 + src/lib/i18n/locales/ca-ES/translation.json | 2 + src/lib/i18n/locales/ceb-PH/translation.json | 2 + src/lib/i18n/locales/cs-CZ/translation.json | 2 + src/lib/i18n/locales/da-DK/translation.json | 2 + src/lib/i18n/locales/de-DE/translation.json | 2 + src/lib/i18n/locales/dg-DG/translation.json | 2 + src/lib/i18n/locales/el-GR/translation.json | 2 + src/lib/i18n/locales/en-GB/translation.json | 2 + src/lib/i18n/locales/en-US/translation.json | 2 + src/lib/i18n/locales/es-ES/translation.json | 2 + src/lib/i18n/locales/et-EE/translation.json | 2 + src/lib/i18n/locales/eu-ES/translation.json | 2 + src/lib/i18n/locales/fa-IR/translation.json | 2 + src/lib/i18n/locales/fi-FI/translation.json | 2 + src/lib/i18n/locales/fil-PH/translation.json | 2 + src/lib/i18n/locales/fo-FO/translation.json | 2 + src/lib/i18n/locales/fr-CA/translation.json | 2 + src/lib/i18n/locales/fr-FR/translation.json | 2 + src/lib/i18n/locales/gl-ES/translation.json | 2 + src/lib/i18n/locales/he-IL/translation.json | 2 + src/lib/i18n/locales/hi-IN/translation.json | 2 + src/lib/i18n/locales/hr-HR/translation.json | 2 + src/lib/i18n/locales/hu-HU/translation.json | 2 + src/lib/i18n/locales/id-ID/translation.json | 2 + src/lib/i18n/locales/ie-GA/translation.json | 2 + src/lib/i18n/locales/it-IT/translation.json | 2 + src/lib/i18n/locales/ja-JP/translation.json | 2 + src/lib/i18n/locales/ka-GE/translation.json | 2 + src/lib/i18n/locales/kab-DZ/translation.json | 2 + src/lib/i18n/locales/ko-KR/translation.json | 2 + src/lib/i18n/locales/lt-LT/translation.json | 2 + src/lib/i18n/locales/lv-LV/translation.json | 2 + src/lib/i18n/locales/ms-MY/translation.json | 2 + src/lib/i18n/locales/nb-NO/translation.json | 2 + src/lib/i18n/locales/nl-NL/translation.json | 2 + src/lib/i18n/locales/pa-IN/translation.json | 2 + src/lib/i18n/locales/pl-PL/translation.json | 2 + src/lib/i18n/locales/pt-BR/translation.json | 2 + src/lib/i18n/locales/pt-PT/translation.json | 2 + src/lib/i18n/locales/ro-RO/translation.json | 2 + src/lib/i18n/locales/ru-RU/translation.json | 2 + src/lib/i18n/locales/sk-SK/translation.json | 2 + src/lib/i18n/locales/sl-SI/translation.json | 2 + src/lib/i18n/locales/sr-RS/translation.json | 2 + src/lib/i18n/locales/sv-SE/translation.json | 2 + src/lib/i18n/locales/ta-IN/translation.json | 2 + src/lib/i18n/locales/th-TH/translation.json | 2 + src/lib/i18n/locales/tk-TM/translation.json | 2 + src/lib/i18n/locales/tr-TR/translation.json | 2 + src/lib/i18n/locales/ug-CN/translation.json | 2 + src/lib/i18n/locales/uk-UA/translation.json | 2 + src/lib/i18n/locales/ur-PK/translation.json | 2 + .../i18n/locales/uz-Cyrl-UZ/translation.json | 2 + .../i18n/locales/uz-Latn-UZ/translation.json | 2 + src/lib/i18n/locales/vi-VN/translation.json | 2 + src/lib/i18n/locales/zh-CN/translation.json | 2 + src/lib/i18n/locales/zh-TW/translation.json | 2 + src/lib/stores/chatList.ts | 39 +++++++++- 70 files changed, 274 insertions(+), 16 deletions(-) diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 1731c2ea8f..baef84a710 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -184,7 +184,7 @@ def chat_search_message_content_match_sql(dialect_name: str, key: str) -> str: def chat_list_order(sort_by: str = 'updated_at', sort_dir: str = 'desc', user_id: str | None = None): if sort_by != 'unread_updated_at': - sort_column = Chat.title if sort_by == 'title' else Chat.updated_at + sort_column = {'title': Chat.title, 'created_at': Chat.created_at}.get(sort_by, Chat.updated_at) order_clause = sort_column.asc() if sort_dir == 'asc' else sort_column.desc() return order_clause, Chat.id @@ -1817,7 +1817,7 @@ class ChatTable: async def get_chat_title_id_list_by_user_id( self, user_id: str, - include_archived: bool = False, + status: str = 'active', # 'active', 'archived' or 'all' include_folders: bool = False, include_pinned: bool = False, sort_by: str = 'updated_at', @@ -1827,9 +1827,9 @@ class ChatTable: db: AsyncSession | None = None, ) -> list[ChatTitleIdResponse]: async with get_async_db_context(db) as session: - stmt = select(Chat.id, Chat.title, Chat.updated_at, Chat.created_at, Chat.last_read_at).filter_by( - user_id=user_id - ) + stmt = select( + Chat.id, Chat.title, Chat.updated_at, Chat.created_at, Chat.last_read_at, Chat.archived + ).filter_by(user_id=user_id) stmt = stmt.where(Chat.meta['internal'].as_boolean().is_not(True)) if not include_folders: @@ -1838,7 +1838,9 @@ class ChatTable: if not include_pinned: stmt = stmt.filter(or_(Chat.pinned == False, Chat.pinned == None)) - if not include_archived: + if status == 'archived': + stmt = stmt.filter_by(archived=True) + elif status != 'all': stmt = stmt.filter_by(archived=False) stmt = stmt.order_by(*chat_list_order(sort_by, sort_dir)) @@ -1859,6 +1861,7 @@ class ChatTable: 'updated_at': chat[2], 'created_at': chat[3], 'last_read_at': chat[4], + 'archived': bool(chat[5]), } ) for chat in all_chats diff --git a/backend/open_webui/routers/chats.py b/backend/open_webui/routers/chats.py index 5397da5511..2586fb4ee1 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -344,6 +344,7 @@ async def get_session_user_chat_list( include_folders: bool | None = False, sort_by: str = 'updated_at', sort_dir: str = 'desc', + status: Literal['active', 'archived', 'all'] = 'active', db: AsyncSession = Depends(get_async_session), ): try: @@ -359,6 +360,7 @@ async def get_session_user_chat_list( sort_dir=sort_dir, skip=skip, limit=limit, + status=status, db=db, ) else: @@ -368,12 +370,14 @@ async def get_session_user_chat_list( include_pinned=include_pinned, sort_by=sort_by, sort_dir=sort_dir, + status=status, db=db, ) return await add_active_state_to_chat_list(request, chats, db=db) except Exception as e: log.exception(e) - raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()) + # `status` here is the query parameter, so the code is written out. + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT()) @router.post('/read') diff --git a/src/lib/apis/chats/index.ts b/src/lib/apis/chats/index.ts index 04ec6ddce5..1a15245070 100644 --- a/src/lib/apis/chats/index.ts +++ b/src/lib/apis/chats/index.ts @@ -203,7 +203,11 @@ export const getChatList = async ( token: string = '', page: number | null = null, include_pinned: boolean = false, - include_folders: boolean = false + include_folders: boolean = false, + view: { + sortBy?: 'updated_at' | 'created_at'; + status?: 'active' | 'archived' | 'all'; + } = {} ) => { let error = null; const searchParams = new URLSearchParams(); @@ -212,6 +216,14 @@ export const getChatList = async ( searchParams.append('page', `${page}`); } + if (view.sortBy) { + searchParams.append('sort_by', view.sortBy); + } + + if (view.status) { + searchParams.append('status', view.status); + } + if (include_folders) { searchParams.append('include_folders', 'true'); } @@ -249,9 +261,10 @@ export const getChatList = async ( return []; } + // Date headings follow the date the list is sorted by. return res.map((chat) => ({ ...chat, - time_range: getTimeRange(chat.updated_at) + time_range: getTimeRange(view.sortBy === 'created_at' ? chat.created_at : chat.updated_at) })); }; diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 47894c3645..6e5dccd29f 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -39,7 +39,9 @@ registerFolderRefreshHandler, setAllChatsRead, setChatActive, - setChatReadAt + setChatReadAt, + chatListView, + setChatListView } from '$lib/stores/chatList'; import { folderStates, @@ -120,6 +122,8 @@ import Dropdown from '../common/Dropdown.svelte'; import DropdownMenu from '../common/DropdownMenu.svelte'; import CheckIcon from '../icons/Check.svelte'; + import ChevronRight from '../icons/ChevronRight.svelte'; + import DropdownSub from '../common/DropdownSub.svelte'; import ArchiveBoxIcon from '../icons/ArchiveBox.svelte'; import GarbageBinIcon from '../icons/GarbageBin.svelte'; import XMarkIcon from '../icons/XMark.svelte'; @@ -267,6 +271,25 @@ let showFolders = false; let showSharedFolders = false; let showChatsMenu = false; + $: chatListMenus = [ + { + label: $i18n.t('Status'), + key: 'status', + choices: [ + { value: 'active', label: $i18n.t('Active') }, + { value: 'archived', label: $i18n.t('Archived') }, + { value: 'all', label: $i18n.t('All') } + ] + }, + { + label: $i18n.t('Sort by'), + key: 'sortBy', + choices: [ + { value: 'updated_at', label: $i18n.t('Last activity') }, + { value: 'created_at', label: $i18n.t('Created') } + ] + } + ] as const; let folders: Record = {}; type SelectedSidebarFolder = { id: string } | null; @@ -1804,6 +1827,51 @@
+ {#each chatListMenus as menu (menu.key)} + {@const current = menu.choices.find( + (choice) => choice.value === $chatListView[menu.key] + )} + + + {#each menu.choices as choice (choice.value)} + + {/each} + + {/each} +