char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

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