diff --git a/backend/open_webui/apps/webui/models/chats.py b/backend/open_webui/apps/webui/models/chats.py index 213872371..76923b05a 100644 --- a/backend/open_webui/apps/webui/models/chats.py +++ b/backend/open_webui/apps/webui/models/chats.py @@ -389,9 +389,22 @@ class ChatTable: Filters chats based on a search query using Python, allowing pagination using skip and limit. """ search_text = search_text.lower().strip() + if not search_text: return self.get_chat_list_by_user_id(user_id, include_archived, skip, limit) + search_text_words = search_text.split(" ") + + # search_text might contain 'tag:tag_name' format so we need to extract the tag_name, split the search_text and remove the tags + tag_ids = [] + for word in search_text_words: + if word.startswith("tag:"): + tag_id = word[4:] + tag_ids.append(tag_id) + search_text_words.remove(word) + + search_text = " ".join(search_text_words) + with get_db() as db: query = db.query(Chat).filter(Chat.user_id == user_id) @@ -420,6 +433,26 @@ class ChatTable: ) ).params(search_text=search_text) ) + + # Check if there are any tags to filter, it should have all the tags + if tag_ids: + query = query.filter( + and_( + *[ + text( + f""" + EXISTS ( + SELECT 1 + FROM json_each(Chat.meta, '$.tags') AS tag + WHERE tag.value = :tag_id + ) + """ + ).params(tag_id=tag_id) + for tag_id in tag_ids + ] + ) + ) + elif dialect_name == "postgresql": # PostgreSQL relies on proper JSON query for search query = query.filter( @@ -438,6 +471,25 @@ class ChatTable: ) ).params(search_text=search_text) ) + + # Check if there are any tags to filter, it should have all the tags + if tag_ids: + query = query.filter( + and_( + *[ + text( + f""" + EXISTS ( + SELECT 1 + FROM json_array_elements_text(Chat.meta->'tags') AS tag + WHERE tag = :tag_id + ) + """ + ).params(tag_id=tag_id) + for tag_id in tag_ids + ] + ) + ) else: raise NotImplementedError( f"Unsupported dialect: {db.bind.dialect.name}" diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 4ac597bd2..9a78274bb 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -48,6 +48,7 @@ import FilesOverlay from '../chat/MessageInput/FilesOverlay.svelte'; import AddFilesPlaceholder from '../AddFilesPlaceholder.svelte'; import { select } from 'd3-selection'; + import SearchInput from './Sidebar/SearchInput.svelte'; const BREAKPOINT = 768; @@ -93,7 +94,7 @@ // once the bottom of the list has been reached (no results) there is no need to continue querying allChatsLoaded = newChatList.length === 0; - await chats.set([...$chats, ...newChatList]); + await chats.set([...($chats ? $chats : []), ...newChatList]); chatListLoading = false; }; @@ -484,35 +485,13 @@
{/if} -