diff --git a/css/main.css b/css/main.css index 564a39a1..64da1602 100644 --- a/css/main.css +++ b/css/main.css @@ -876,6 +876,10 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* { flex-shrink: 1; } +#search_chat > :nth-child(2) > :first-child { + display: none; +} + /* ---------------------------------------------- Keep dropdown menus above errored components ---------------------------------------------- */ @@ -911,7 +915,7 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* { } #past-chats { - max-height: calc(100dvh - 90px); + max-height: calc(100dvh - 135px); overflow-y: scroll !important; border-radius: 0; scrollbar-width: auto; diff --git a/modules/chat.py b/modules/chat.py index 81328385..694c137b 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -593,21 +593,26 @@ def find_all_histories_with_first_prompts(state): result = [] for i, path in enumerate(histories): filename = path.stem - if re.match(r'^[0-9]{8}-[0-9]{2}-[0-9]{2}-[0-9]{2}$', filename): - with open(path, 'r', encoding='utf-8') as f: - data = json.load(f) + file_content = "" + with open(path, 'r', encoding='utf-8') as f: + file_content = f.read() - first_prompt = "" - if data and 'visible' in data and len(data['visible']) > 0: - if data['internal'][0][0] == '<|BEGIN-VISIBLE-CHAT|>': - if len(data['visible']) > 1: - first_prompt = html.unescape(data['visible'][1][0]) - elif i == 0: - first_prompt = "New chat" - else: - first_prompt = html.unescape(data['visible'][0][0]) - elif i == 0: - first_prompt = "New chat" + if state['search_chat'] and state['search_chat'] not in file_content: + continue + + data = json.loads(file_content) + if re.match(r'^[0-9]{8}-[0-9]{2}-[0-9]{2}-[0-9]{2}$', filename): + first_prompt = "" + if data and 'visible' in data and len(data['visible']) > 0: + if data['internal'][0][0] == '<|BEGIN-VISIBLE-CHAT|>': + if len(data['visible']) > 1: + first_prompt = html.unescape(data['visible'][1][0]) + elif i == 0: + first_prompt = "New chat" + else: + first_prompt = html.unescape(data['visible'][0][0]) + elif i == 0: + first_prompt = "New chat" else: first_prompt = filename @@ -615,7 +620,7 @@ def find_all_histories_with_first_prompts(state): # Truncate the first prompt if it's longer than 30 characters if len(first_prompt) > 30: - first_prompt = first_prompt[:30-3] + '...' + first_prompt = first_prompt[:30 - 3] + '...' result.append((first_prompt, filename)) @@ -1124,6 +1129,11 @@ def handle_rename_chat_confirm(rename_to, state): ] +def handle_search_chat_change(state): + histories = find_all_histories_with_first_prompts(state) + return gr.update(choices=histories) + + def handle_upload_chat_history(load_chat_history, state): history = start_new_chat(state) history = load_history_json(load_chat_history, history) diff --git a/modules/ui.py b/modules/ui.py index 4bfea9fa..a3bf520f 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -230,6 +230,7 @@ def list_interface_input_elements(): 'start_with', 'character_menu', 'history', + 'search_chat', 'unique_id', 'name1', 'user_bio', diff --git a/modules/ui_chat.py b/modules/ui_chat.py index ecebc28e..b92dd9ae 100644 --- a/modules/ui_chat.py +++ b/modules/ui_chat.py @@ -29,6 +29,8 @@ def create_ui(): shared.gradio['delete_chat'] = gr.Button('🗑️', elem_classes='refresh-button', interactive=not mu) shared.gradio['Start new chat'] = gr.Button('New chat', elem_classes=['refresh-button', 'focus-on-chat-input']) + shared.gradio['search_chat'] = gr.Textbox(placeholder='Search chats...', max_lines=1, elem_id='search_chat') + with gr.Row(elem_id='delete-chat-row', visible=False) as shared.gradio['delete-chat-row']: shared.gradio['delete_chat-cancel'] = gr.Button('Cancel', elem_classes=['refresh-button', 'focus-on-chat-input']) shared.gradio['delete_chat-confirm'] = gr.Button('Confirm', variant='stop', elem_classes=['refresh-button', 'focus-on-chat-input']) @@ -265,6 +267,10 @@ def create_event_handlers(): ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( chat.handle_rename_chat_confirm, gradio('rename_to', 'interface_state'), gradio('unique_id', 'rename-row'), show_progress=False) + shared.gradio['search_chat'].change( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.handle_search_chat_change, gradio('interface_state'), gradio('unique_id'), show_progress=False) + shared.gradio['load_chat_history'].upload( ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( chat.handle_upload_chat_history, gradio('load_chat_history', 'interface_state'), gradio('history', 'display', 'unique_id'), show_progress=False).then(