import { useEffect, useState } from 'react'; import { motion } from 'framer-motion'; import './dist.css'; import { Input } from './components/ui/input'; import { Button } from './components/ui/button'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from './components/ui/tooltip'; function sendUrlToAPI() { // get the current URL const url = window.location.href; const blacklist = ['localhost:3000', 'anycontext.dhr.wtf']; // check if the URL is blacklisted if (blacklist.some((blacklisted) => url.includes(blacklisted))) { console.log('URL is blacklisted'); return; } else { // const content = Entire page content, but cleaned up for the LLM. No ads, no scripts, no styles, just the text. if article, just the importnat info abou tit. const content = document.documentElement.innerText; chrome.runtime.sendMessage({ type: 'urlChange', content, url }); } } function SideBar({ jwt }: { jwt: string }) { const [isOpen, setIsOpen] = useState(false); const [input, setInput] = useState(''); const [savedWebsites, setSavedWebsites] = useState([]); const [isSendingData, setIsSendingData] = useState(false); const [toBeParsed, setToBeParsed] = useState(''); const [aiResponse, setAIResponse] = useState(''); const [lastQuestion, setLastQuestion] = useState(''); const handleStreamData = (newChunk: string) => { // Append the new chunk to the existing data to be parsed setToBeParsed((prev) => prev + newChunk); }; const queryApi = async () => { const content = document.documentElement.innerText; setAIResponse(''); const query = `Answer this question based on this query: ${input}\n\n${content}`; chrome.runtime.sendMessage({ type: 'queryApi', input: query, jwt }); }; useEffect(() => { if (typeof window === 'undefined') return; chrome.runtime.onMessage.addListener((message) => { if (message.action === 'streamData') { console.log(message.data); handleStreamData(message.data); } else if (message.action === 'streamEnd') { setLastQuestion(input); setInput(''); setToBeParsed(''); } }); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { // Define a function to try parsing the accumulated data const tryParseAccumulatedData = () => { // Attempt to parse the "toBeParsed" state as JSON try { // Split the accumulated data by the known delimiter "\n\n" const parts = toBeParsed.split('\n\n'); let remainingData = ''; // Process each part to extract JSON objects parts.forEach((part, index) => { try { const parsedPart = JSON.parse(part.replace('data: ', '')); // Try to parse the part as JSON // If the part is the last one and couldn't be parsed, keep it to accumulate more data if (index === parts.length - 1 && !parsedPart) { remainingData = part; } else if (parsedPart && parsedPart.response) { // If the part is parsable and has the "response" field, update the AI response state setAIResponse((prev) => prev + parsedPart.response); } } catch (error) { // If parsing fails and it's not the last part, it's a malformed JSON if (index !== parts.length - 1) { console.error('Malformed JSON part: ', part); } else { // If it's the last part, it may be incomplete, so keep it remainingData = part; } } }); // Update the toBeParsed state to only contain the unparsed remainder if (remainingData !== toBeParsed) { setToBeParsed(remainingData); } } catch (error) { console.error('Error parsing accumulated data: ', error); } }; // Call the parsing function if there's data to be parsed if (toBeParsed) { tryParseAccumulatedData(); } }, [toBeParsed]); return ( <> {isOpen && (
setIsOpen(false)} className="anycontext-overlay" >
)} {isOpen ? (

Ask a question to this page

{/* Three buttons as options to choose from */} {!input && (
)} {lastQuestion && (

{lastQuestion}

)}
{aiResponse.replace('', '')}
{ e.preventDefault(); await queryApi(); setInput(''); }} className="anycontext-flex anycontext-absolute anycontext-bottom-0 anycontext-w-full anycontext-gap-4 anycontext-justify-between anycontext-px-4" > setInput(e.target.value)} placeholder="Ask anything about this website" className="anycontext-mb-4 anycontext-text-black anycontext-outline" />
) : (

Open Sidebar

{savedWebsites.includes(window.location.href) ? 'Added to memory' : 'Add to memory'}

)}
); } export default SideBar;