'use client'; import { v4 } from 'uuid'; import classNames from 'classnames'; import { useCallback, useEffect, useState } from 'react'; import { CTAButton, Stack, Text, DropdownSelect, TextArea, useBoolean, Input } from 'ohmy-ui'; import { fetch } from '@/utils'; import styles from './SearchView.module.css'; import getHistory from '@/modules/chat/getHistory'; interface Message { id: string; user: 'user' | 'system'; text: any; } interface SelectOption { value: string; label: string; } export default function SearchView() { const [messages, setMessages] = useState([]); const [inputValue, setInputValue] = useState(""); const handleInputChange = useCallback((event: React.ChangeEvent) => { setInputValue(event.target.value); }, []); const searchOptions = [{ value: 'GRAPH_COMPLETION', label: 'Completion using Cognee\'s graph based memory', }, { value: 'RAG_COMPLETION', label: 'Completion using RAG', }, { value: 'GRAPH_COMPLETION_COT', label: 'Cognee\'s Chain of Thought search', }, { value: 'GRAPH_COMPLETION_CONTEXT_EXTENSION', label: 'Cognee\'s Multi-Hop search', }]; const [searchType, setSearchType] = useState(searchOptions[0]); const [rangeValue, setRangeValue] = useState(10); const scrollToBottom = useCallback(() => { setTimeout(() => { const messagesContainerElement = document.getElementById('messages'); if (messagesContainerElement) { const messagesElements = messagesContainerElement.children[0]; if (messagesElements) { messagesContainerElement.scrollTo({ top: messagesElements.scrollHeight, behavior: 'smooth', }); } } }, 300); }, []); useEffect(() => { getHistory() .then((history) => { setMessages(history); scrollToBottom(); }); }, [scrollToBottom]); const handleSearchSubmit = useCallback((event: React.FormEvent) => { event.preventDefault(); if (inputValue.trim() === '') { return; } setMessages((currentMessages) => [ ...currentMessages, { id: v4(), user: 'user', text: inputValue, }, ]); scrollToBottom(); setInputValue(''); const searchTypeValue = searchType.value; fetch('/v1/search', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: inputValue.trim(), searchType: searchTypeValue, topK: rangeValue, }), }) .then((response) => response.json()) .then((systemMessage) => { setMessages((currentMessages) => [ ...currentMessages, { id: v4(), user: 'system', text: convertToSearchTypeOutput(systemMessage, searchTypeValue), }, ]); scrollToBottom(); }) .catch(() => { setInputValue(inputValue); }); }, [inputValue, rangeValue, scrollToBottom, searchType.value]); const { value: isInputExpanded, setTrue: expandInput, setFalse: contractInput, } = useBoolean(false); const handleSubmitOnEnter = (event: React.KeyboardEvent) => { if (event.key === 'Enter' && !event.shiftKey) { handleSearchSubmit(event as unknown as React.FormEvent); } }; const handleRangeValueChange = (event: React.ChangeEvent) => { setRangeValue(parseInt(event.target.value)); }; return ( value={searchType} options={searchOptions} onChange={setSearchType} />
{messages.map((message) => ( {message?.text && ( typeof(message.text) == "string" ? message.text : JSON.stringify(message.text) )} ))}