char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { debounce } from '$lib/utils/debounce' ; 2 3/** 4* Shared debounced async-search machinery for the chat-form pickers: 5* AbortController + sequence counter to discard stale responses, a 6* debounce, and a live `isSearching` flag. 7*/ 8 9export interface UseDebouncedSearchOptions { 10debounceMs :number ; 11/** Fire-time guard: a scheduled call that outlives a reset is dropped. */ 12canRun :() => boolean ; 13/** Live query, used to drop a scheduled call whose query changed. */ 14getQuery :() => string ; 15/** Perform the search and commit results; bail out when `isCurrent()` is false. */ 16run :( query :string , signal :AbortSignal , isCurrent :() => boolean ) => void | Promise < void >; 17} 18 19export function useDebouncedSearch ( opts :UseDebouncedSearchOptions ) { 20let controller :AbortController | null = null ; 21let searchSeq = 0 ; 22let isSearching = $state ( false ); 23 24function isCurrent ( seq :number ) { 25return seq === searchSeq ; 26} 27 28function cancel () { 29controller ?. abort (); 30searchSeq ++ ; 31isSearching = false ; 32} 33 34const schedule = debounce (( query :string ) => { 35if ( ! opts . canRun () || query !== opts . getQuery (). trim ()) return ; 36 37void start ( query ); 38}, opts . debounceMs ); 39 40async function start ( query :string ) { 41cancel (); 42const fresh = new AbortController (); 43 44controller = fresh ; 45const mySeq = ++ searchSeq ; 46 47isSearching = true ; 48try { 49await opts . run ( query , fresh . signal , () => isCurrent ( mySeq )); 50} finally { 51if ( isCurrent ( mySeq )) isSearching = false ; 52} 53} 54 55return { 56 cancel, 57get isSearching () { 58return isSearching ; 59}, 60run ( query :string ) { 61schedule ( query ); 62}, 63/** Bump the loading flag synchronously (e.g. before the debounce fires). */ 64setLoading ( value :boolean ) { 65isSearching = value ; 66} 67}; 68} 69 70export type UseDebouncedSearchReturn = ReturnType < typeof useDebouncedSearch >;