UI: add a "Search chats" input field

This commit is contained in:
oobabooga 2025-01-02 18:46:40 -08:00
parent b8fc9010fa
commit 4b3e1b3757
4 changed files with 37 additions and 16 deletions

View File

@ -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;

View File

@ -593,10 +593,15 @@ 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):
file_content = ""
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
file_content = f.read()
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|>':
@ -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)

View File

@ -230,6 +230,7 @@ def list_interface_input_elements():
'start_with',
'character_menu',
'history',
'search_chat',
'unique_id',
'name1',
'user_bio',

View File

@ -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(