From f8f6dd1d903f04e39b12770b14d720f69cfaca4d Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Thu, 29 Feb 2024 14:22:35 +0200 Subject: [PATCH 1/2] Manage chat files: fragment search a.k.a. swoop --- public/script.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index 6b5006582..021d4a8b0 100644 --- a/public/script.js +++ b/public/script.js @@ -6199,7 +6199,29 @@ export async function displayPastChats() { const fileName = chat['file_name']; const chatContent = rawChats[fileName]; - return chatContent && Object.values(chatContent).some(message => message?.mes?.toLowerCase()?.includes(searchQuery.toLowerCase())); + // // Uncomment this to return to old behavior (classical full-substring search). + // return chatContent && Object.values(chatContent).some(message => message?.mes?.toLowerCase()?.includes(searchQuery.toLowerCase())); + + // Fragment search a.k.a. swoop (as in `helm-swoop` in the Helm package of Emacs). + // Split a `query` {string} into its fragments {string[]}. + function makeQueryFragments(query) { + let fragments = query.trim().split(/\s+/).map( function (str) { return str.trim(); } ); + fragments = [...new Set(fragments)]; // uniques only + // fragments = fragments.filter( function(str) { return str.length >= 3; } ); // Helm does this, but perhaps better if we don't. + fragments = fragments.map( function (str) { return str.toLowerCase(); } ); + return fragments; + } + // Check whether `text` {string} includes all of the `fragments` {string[]}. + function matchFragments(fragments, text) { + if (!text) { + return false; + } + return fragments.every(function (item, idx, arr) { return text.includes(item); }); + } + const fragments = makeQueryFragments(searchQuery); + // At least one chat message must match *all* the fragments. + // Currently, this doesn't match if the fragment matches are distributed across several chat messages. + return chatContent && Object.values(chatContent).some(message => matchFragments(fragments, message?.mes?.toLowerCase())); }); console.debug(filteredData); From e3084dda17efa76921225275b4c47af21205d8a1 Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Thu, 29 Feb 2024 14:22:51 +0200 Subject: [PATCH 2/2] Manage chat files: UX: focus the search field when the view opens --- public/script.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/public/script.js b/public/script.js index 021d4a8b0..ea3ea3f57 100644 --- a/public/script.js +++ b/public/script.js @@ -6270,6 +6270,14 @@ export async function displayPastChats() { const searchQuery = $(this).val(); debouncedDisplay(searchQuery); }); + + // UX convenience: Focus the search field when the Manage Chat Files view opens. + setTimeout(function () { + const textSearchElement = $('#select_chat_search'); + textSearchElement.click(); + textSearchElement.focus(); + textSearchElement.select(); // select content (if any) for easy erasing + }, 200); } function selectRightMenuWithAnimation(selectedMenuId) {