4747
4848router = APIRouter ()
4949
50+ SEARCH_FILTER_PREFIXES = ('tag:' , 'folder:' , 'pinned:' , 'archived:' , 'shared:' )
51+
5052CHAT_CONFIG_KEYS = {
5153 'ENABLE_CONTEXT_COMPACTION' : 'chat.context_compaction.enable' ,
5254 'CONTEXT_COMPACTION_TOKEN_THRESHOLD' : 'chat.context_compaction.token_threshold' ,
@@ -64,6 +66,43 @@ class CompactChatForm(BaseModel):
6466 model : str | None = None
6567
6668
69+ def chat_search_content_text (text : str ) -> str :
70+ words = text .lower ().strip ().split (' ' )
71+ return ' ' .join (word for word in words if not word .startswith (SEARCH_FILTER_PREFIXES )).strip ()
72+
73+
74+ def chat_search_snippet (chat : dict , search_text : str , max_length : int = 200 ) -> str | None :
75+ if not search_text :
76+ return None
77+
78+ messages = chat .get ('messages' , [])
79+ if isinstance (messages , dict ):
80+ messages = messages .values ()
81+
82+ for message in messages :
83+ if not isinstance (message , dict ):
84+ continue
85+
86+ content = message .get ('content' )
87+ if not isinstance (content , str ):
88+ continue
89+
90+ index = content .lower ().find (search_text )
91+ if index == - 1 :
92+ continue
93+
94+ start = max (index - max_length // 2 , 0 )
95+ end = min (start + max_length , len (content ))
96+ if index + len (search_text ) > end :
97+ end = min (index + len (search_text ), len (content ))
98+ start = max (end - max_length , 0 )
99+
100+ snippet = ' ' .join (content [start :end ].split ())
101+ return f'{ "..." if start else "" } { snippet } { "..." if end < len (content ) else "" } '
102+
103+ return None
104+
105+
67106async def get_chat_config_values () -> dict :
68107 values = await Config .get_many (* CHAT_CONFIG_KEYS .values ())
69108 return {field : values [storage_key ] for field , storage_key in CHAT_CONFIG_KEYS .items () if storage_key in values }
@@ -704,10 +743,10 @@ async def search_user_chats(
704743 limit = 60
705744 skip = (page - 1 ) * limit
706745
707- chat_list = [
708- ChatTitleIdResponse ( ** chat . model_dump ())
709- for chat in await Chats .get_chats_by_user_id_and_search_text (user .id , text , skip = skip , limit = limit , db = db )
710- ]
746+ search_text = chat_search_content_text ( text )
747+ chat_list = []
748+ for chat in await Chats .get_chats_by_user_id_and_search_text (user .id , text , skip = skip , limit = limit , db = db ):
749+ chat_list . append ( ChatTitleIdResponse ( ** chat . model_dump (), snippet = chat_search_snippet ( chat . chat , search_text )))
711750
712751 # Delete tag if no chat is found
713752 words = text .strip ().split (' ' )
0 commit comments