mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	#386 Match whole word option for world info
This commit is contained in:
		| @@ -1768,7 +1768,7 @@ | ||||
|                             </div> | ||||
|                         </div> | ||||
|  | ||||
|                         <div class="flex1 range-block flex-container flexFlowColumn"> | ||||
|                         <div class="flex1 range-block flex-container"> | ||||
|                             <label title="Entries can activate other entries by mentioning their keywords" class="checkbox_label"> | ||||
|                                 <input id="world_info_recursive" type="checkbox" /> | ||||
|                                 <span> | ||||
| @@ -1787,6 +1787,14 @@ | ||||
|                                     </a> | ||||
|                                 </span> | ||||
|                             </label> | ||||
|                             <label title="If the entry key consists of only one word, it would not be matched as part of other words" class="checkbox_label"> | ||||
|                                 <input id="world_info_match_whole_words" type="checkbox" /> | ||||
|                                 <span> | ||||
|                                     Match whole words | ||||
|                                     <a href="https://docs.sillytavern.app/usage/guidebook/#match-whole-words" class="notes-link" target="_blank"> | ||||
|                                         <span class="note-link-span">?</span> | ||||
|                                     </a> | ||||
|                             </label> | ||||
|                         </div> | ||||
|                     </div> | ||||
|  | ||||
|   | ||||
| @@ -27,6 +27,7 @@ import { | ||||
|     deleteWorldInfo, | ||||
|     world_info_recursive, | ||||
|     world_info_case_sensitive, | ||||
|     world_info_match_whole_words, | ||||
| } from "./scripts/world-info.js"; | ||||
|  | ||||
| import { | ||||
| @@ -4080,6 +4081,7 @@ async function saveSettings(type) { | ||||
|             world_info_budget: world_info_budget, | ||||
|             world_info_recursive: world_info_recursive, | ||||
|             world_info_case_sensitive: world_info_case_sensitive, | ||||
|             world_info_match_whole_words: world_info_match_whole_words, | ||||
|             textgenerationwebui_settings: textgenerationwebui_settings, | ||||
|             swipes: swipes, | ||||
|             horde_settings: horde_settings, | ||||
|   | ||||
| @@ -8,6 +8,7 @@ export { | ||||
|     world_info_depth, | ||||
|     world_info_recursive, | ||||
|     world_info_case_sensitive, | ||||
|     world_info_match_whole_words, | ||||
|     world_names, | ||||
|     imported_world_name, | ||||
|     checkWorldInfo, | ||||
| @@ -25,6 +26,7 @@ let world_info_budget = 128; | ||||
| let is_world_edit_open = false; | ||||
| let world_info_recursive = false; | ||||
| let world_info_case_sensitive = false; | ||||
| let world_info_match_whole_words = false; | ||||
| let imported_world_name = ""; | ||||
| const saveWorldDebounced = debounce(async () => await _save(), 500); | ||||
| const saveSettingsDebounced = debounce(() => saveSettings(), 500); | ||||
| @@ -55,6 +57,8 @@ function setWorldInfoSettings(settings, data) { | ||||
|         world_info_recursive = Boolean(settings.world_info_recursive); | ||||
|     if (settings.world_info_case_sensitive !== undefined) | ||||
|         world_info_case_sensitive = Boolean(settings.world_info_case_sensitive); | ||||
|     if (settings.world_info_match_whole_words !== undefined) | ||||
|         world_info_match_whole_words = Boolean(settings.world_info_match_whole_words); | ||||
|  | ||||
|     $("#world_info_depth_counter").text(world_info_depth); | ||||
|     $("#world_info_depth").val(world_info_depth); | ||||
| @@ -64,6 +68,7 @@ function setWorldInfoSettings(settings, data) { | ||||
|  | ||||
|     $("#world_info_recursive").prop('checked', world_info_recursive); | ||||
|     $("#world_info_case_sensitive").prop('checked', world_info_case_sensitive); | ||||
|     $("#world_info_match_whole_words").prop('checked', world_info_match_whole_words); | ||||
|  | ||||
|     world_names = data.world_names?.length ? data.world_names : []; | ||||
|  | ||||
| @@ -517,7 +522,7 @@ function checkWorldInfo(chat) { | ||||
|             if (Array.isArray(entry.key) && entry.key.length) { | ||||
|                 primary: for (let key of entry.key) { | ||||
|                     const substituted = substituteParams(key); | ||||
|                     if (substituted && textToScan.includes(transformString(substituted.trim()))) { | ||||
|                     if (substituted && matchKeys(textToScan, substituted.trim())) { | ||||
|                         if ( | ||||
|                             entry.selective && | ||||
|                             Array.isArray(entry.keysecondary) && | ||||
| @@ -525,10 +530,7 @@ function checkWorldInfo(chat) { | ||||
|                         ) { | ||||
|                             secondary: for (let keysecondary of entry.keysecondary) { | ||||
|                                 const secondarySubstituted = substituteParams(keysecondary); | ||||
|                                 if ( | ||||
|                                     secondarySubstituted && | ||||
|                                     textToScan.includes(transformString(secondarySubstituted.trim())) | ||||
|                                 ) { | ||||
|                                 if (secondarySubstituted && matchKeys(textToScan, secondarySubstituted.trim())) { | ||||
|                                     activatedNow.add(entry.uid); | ||||
|                                     break secondary; | ||||
|                                 } | ||||
| @@ -576,6 +578,29 @@ function checkWorldInfo(chat) { | ||||
|     return { worldInfoBefore, worldInfoAfter }; | ||||
| } | ||||
|  | ||||
| function matchKeys(haystack, needle) { | ||||
|     const transformedString = transformString(needle); | ||||
|  | ||||
|     if (world_info_match_whole_words) { | ||||
|         const keyWords = transformedString.split(/\s+/); | ||||
|  | ||||
|         if (keyWords.length > 1) { | ||||
|             return haystack.includes(transformedString); | ||||
|         } | ||||
|         else { | ||||
|             const regex = new RegExp(`\\b${transformedString}\\b`); | ||||
|             if (regex.test(haystack)) { | ||||
|                 return true; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|     } else { | ||||
|         return haystack.includes(transformedString); | ||||
|     } | ||||
|  | ||||
|     return false; | ||||
| } | ||||
|  | ||||
| function selectImportedWorldInfo() { | ||||
|     if (!imported_world_name) { | ||||
|         return; | ||||
| @@ -699,4 +724,9 @@ jQuery(() => { | ||||
|         world_info_case_sensitive = !!$(this).prop('checked'); | ||||
|         saveSettingsDebounced(); | ||||
|     }) | ||||
|  | ||||
|     $('#world_info_match_whole_words').on('input', function () { | ||||
|         world_info_match_whole_words = !!$(this).prop('checked'); | ||||
|         saveSettingsDebounced(); | ||||
|     }); | ||||
| }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user