This commit is contained in:
Timothy J. Baek 2024-10-11 00:00:13 -07:00
parent 9658c2559a
commit ba2df1c33a
3 changed files with 55 additions and 23 deletions

View File

@ -393,28 +393,56 @@ class ChatTable:
if not include_archived: if not include_archived:
query = query.filter(Chat.archived == False) query = query.filter(Chat.archived == False)
# Fetch all potentially relevant chats query = query.order_by(Chat.updated_at.desc())
all_chats = query.all()
# Filter chats using Python # Check if the database dialect is either 'sqlite' or 'postgresql'
filtered_chats = [] dialect_name = db.bind.dialect.name
for chat in all_chats: if dialect_name == "sqlite":
# Check chat title # SQLite case: using JSON1 extension for JSON searching
title_matches = search_text in chat.title.lower() query = query.filter(
(
Chat.title.ilike(
f"%{search_text}%"
) # Case-insensitive search in title
| text(
"""
EXISTS (
SELECT 1
FROM json_each(Chat.chat, '$.messages') AS message
WHERE LOWER(message.value->>'content') LIKE '%' || :search_text || '%'
)
"""
)
).params(search_text=search_text)
)
elif dialect_name == "postgresql":
# PostgreSQL relies on proper JSON query for search
query = query.filter(
(
Chat.title.ilike(
f"%{search_text}%"
) # Case-insensitive search in title
| text(
"""
EXISTS (
SELECT 1
FROM json_array_elements(Chat.chat->'messages') AS message
WHERE LOWER(message->>'content') LIKE '%' || :search_text || '%'
)
"""
)
).params(search_text=search_text)
)
else:
raise NotImplementedError(
f"Unsupported dialect: {db.bind.dialect.name}"
)
# Check chat content in chat JSON # Perform pagination at the SQL level
content_matches = any( all_chats = query.offset(skip).limit(limit).all()
search_text in message.get("content", "").lower()
for message in chat.chat.get("messages", [])
if "content" in message
)
if title_matches or content_matches: # Validate and return chats
filtered_chats.append(chat) return [ChatModel.model_validate(chat) for chat in all_chats]
# Implementing pagination manually
paginated_chats = filtered_chats[skip : skip + limit]
return [ChatModel.model_validate(chat) for chat in paginated_chats]
def get_chat_tags_by_id_and_user_id(self, id: str, user_id: str) -> list[TagModel]: def get_chat_tags_by_id_and_user_id(self, id: str, user_id: str) -> list[TagModel]:
with get_db() as db: with get_db() as db:

View File

@ -199,7 +199,10 @@ export const getChatListBySearchText = async (token: string, text: string, page:
throw error; throw error;
} }
return res; return res.map((chat) => ({
...chat,
time_range: getTimeRange(chat.updated_at)
}));
}; };
export const getAllArchivedChats = async (token: string) => { export const getAllArchivedChats = async (token: string) => {

View File

@ -111,6 +111,7 @@
if (search === '') { if (search === '') {
await initChatList(); await initChatList();
allChatsLoaded = false;
return; return;
} else { } else {
searchDebounceTimeout = setTimeout(async () => { searchDebounceTimeout = setTimeout(async () => {
@ -512,7 +513,7 @@
</div> </div>
{#if $tags.length > 0} {#if $tags.length > 0}
<div class="px-3.5 mb-1 flex gap-0.5 flex-wrap"> <div class="px-3.5 mb-2.5 flex gap-0.5 flex-wrap">
<button <button
class="px-2.5 py-[1px] text-xs transition {selectedTagName === null class="px-2.5 py-[1px] text-xs transition {selectedTagName === null
? 'bg-gray-100 dark:bg-gray-900' ? 'bg-gray-100 dark:bg-gray-900'
@ -551,7 +552,7 @@
{/if} {/if}
{#if !search && $pinnedChats.length > 0} {#if !search && $pinnedChats.length > 0}
<div class="pl-2 py-2 flex flex-col space-y-1"> <div class="pl-2 pb-2 flex flex-col space-y-1">
<div class=""> <div class="">
<div class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium pb-1.5"> <div class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium pb-1.5">
{$i18n.t('Pinned')} {$i18n.t('Pinned')}
@ -586,7 +587,7 @@
</div> </div>
{/if} {/if}
<div class="pl-2 my-2 flex-1 flex flex-col space-y-1 overflow-y-auto scrollbar-hidden"> <div class="pl-2 flex-1 flex flex-col space-y-1 overflow-y-auto scrollbar-hidden">
{#if $chats} {#if $chats}
{#each $chats as chat, idx} {#each $chats as chat, idx}
{#if idx === 0 || (idx > 0 && chat.time_range !== $chats[idx - 1].time_range)} {#if idx === 0 || (idx > 0 && chat.time_range !== $chats[idx - 1].time_range)}