diff --git a/src/App/libs/provider/provider.js b/src/App/libs/provider/provider.js index c5f48b9..376a819 100644 --- a/src/App/libs/provider/provider.js +++ b/src/App/libs/provider/provider.js @@ -9,11 +9,6 @@ import { parseUrlParams, createUrlParams } from 'App/libs/provider/url-params'; export const useSearchContext = create(createStore); const endpoint = import.meta.env.VITE_ENDPOINT; -const dummyEventSource = Object.freeze({ - close: () => {}, - addEventListener: () => {}, - removeEventListener: () => {}, -}); export function SearchContext() { const location = useLocation(); @@ -56,36 +51,52 @@ export function SearchContext() { if (query.length === 0) return; const filters = namespaces.map((n) => `+namespace:${n}`).join(' '); + let cancelled = false; + + // If the user has not consented to the abstract yet, use regular search + if (!abstractConsent) { + const searchUrl = new UrlBuilder(endpoint) + .add('search') + .queryParam('query', query) + .queryParam('filters', filters) + .queryParam('queryProfile', 'llmsearch') + .toString(true); + Get(searchUrl) + .then( + (result) => + !cancelled && setHits({ hits: result.root.children ?? [] }), + ) + .catch((error) => !cancelled && setHits({ error })); + return () => { + cancelled = true; + }; + } + + // However, if the user has consented, use RAG search const streamUrl = new UrlBuilder(endpoint) - .add('stream') + .add('sse') .queryParam('query', query) .queryParam('filters', filters) - .queryParam('queryProfile', 'llmsearch') + .queryParam('queryProfile', 'ragsearch') + .queryParam('llm.includeHits', 'true') .toString(true); - const source = abstractConsent - ? new EventSource(streamUrl) - : dummyEventSource; - const onMessage = (e) => summaryAppend(e.data); + const source = new EventSource(streamUrl); + const onToken = (e) => summaryAppend(JSON.parse(e.data).token); + const onHits = (e) => { + if (!cancelled) { + const result = JSON.parse(e.data); + setHits({ hits: result.root.children ?? [] }); + } + }; const onError = () => summaryComplete() || source.close(); - source.addEventListener('message', onMessage); + source.addEventListener('token', onToken); + source.addEventListener('hits', onHits); source.addEventListener('error', onError); - - let cancelled = false; - const searchUrl = new UrlBuilder(endpoint) - .add('search') - .queryParam('query', query) - .queryParam('filters', filters) - .queryParam('queryProfile', 'llmsearch') - .toString(true); - Get(searchUrl) - .then( - (result) => !cancelled && setHits({ hits: result.root.children ?? [] }), - ) - .catch((error) => !cancelled && setHits({ error })); return () => { cancelled = true; source.close(); - source.removeEventListener('message', onMessage); + source.removeEventListener('token', onToken); + source.removeEventListener('hits', onHits); source.removeEventListener('error', onError); }; }, [