"use client"; import { FormEvent, useEffect, useRef, useState } from 'react'; import { FileLoader } from '../components/FileLoader'; import styles from './page.module.css'; import { useChat } from 'ai/react'; const Home: React.FC = () => { // eslint-disable-next-line no-unused-vars const [isWorkerLoading, setIsWorkerLoading] = useState(false); const [searchInProgress, setSearchInProgress] = useState(false); const [searchIsLoading, setIsLoading] = useState(false); const [fileText, setFileText] = useState(''); const embeddingsWorkerRef = useRef(null); const { isLoading, messages, input, handleInputChange, handleSubmit } = useChat(); useEffect(() => { if (typeof window === 'undefined') return; const EmbeddingsWorker = require('worker-loader!../components/embeddingsWorker.js').default; embeddingsWorkerRef.current = new EmbeddingsWorker(); return () => { embeddingsWorkerRef.current?.terminate(); }; }, []); useEffect(() => { if (fileText && embeddingsWorkerRef.current) { embeddingsWorkerRef.current.postMessage({ action: 'addDocumentsToStore', documents: [fileText] }); } }, [fileText]); const handleSearch = () => { return new Promise((resolve) => { const handleMessage = (event: any) => { if (event.data.action === 'searchResults') { embeddingsWorkerRef.current?.removeEventListener('message', handleMessage); setSearchInProgress(false); resolve(event.data.results); } }; embeddingsWorkerRef.current?.addEventListener('message', handleMessage); embeddingsWorkerRef.current?.postMessage({ action: 'searchSimilarDocuments', query: input, topK: 5 }); }); }; const modifiedHandleSubmit = async (e: FormEvent) => { e.preventDefault(); if (input.trim() === '' || searchInProgress) { return; // Prevent empty submissions or when a search is already in progress } setSearchInProgress(true); try { const results = await handleSearch(); const serializedResults = JSON.stringify(results); const chatRequestOptions = { data: { vectorStoreResults: serializedResults }, }; handleSubmit(e, chatRequestOptions); } catch (error) { console.error('Error during search:', error); setSearchInProgress(false); } }; return (

Upload a PDF to start the analysis.

{fileText && (

Processed Text Complete

)} {isLoading || searchIsLoading && (
...
)}
{messages.map(m => (
{m.role === 'user' ? 'You: ' : 'AI: '} {m.content}
))}
{(searchInProgress || isLoading) ? : }
); } export default Home;