mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-03-02 10:57:45 +01:00
Manage chat files: fragment search a.k.a. swoop
This commit is contained in:
parent
3d84ae026d
commit
f8f6dd1d90
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user