From 3a01eebc040f84ee28310f5129210bffb2cee5ee Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:04:50 +0530 Subject: [PATCH] feat(chat): prevent ws not open errors --- package.json | 1 + src/agents/academicSearchAgent.ts | 1 - src/agents/webSearchAgent.ts | 22 ++++++++++++++++++++- src/websocket/connectionManager.ts | 12 ++++++++++++ ui/components/ChatWindow.tsx | 31 +++++++++++++++++------------- yarn.lock | 7 +++++++ 6 files changed, 59 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 17cbe41..48efd23 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@types/html-to-text": "^9.0.4", "@types/pdf-parse": "^1.1.4", "@types/readable-stream": "^4.0.11", + "@types/ws": "^8.5.12", "drizzle-kit": "^0.22.7", "nodemon": "^3.1.0", "prettier": "^3.2.5", diff --git a/src/agents/academicSearchAgent.ts b/src/agents/academicSearchAgent.ts index fc95d3c..bad4065 100644 --- a/src/agents/academicSearchAgent.ts +++ b/src/agents/academicSearchAgent.ts @@ -167,7 +167,6 @@ const createBasicAcademicSearchAnsweringChain = ( if (optimizationMode === 'speed') { return docsWithContent.slice(0, 15); } else if (optimizationMode === 'balanced') { - console.log('Balanced mode'); const [docEmbeddings, queryEmbedding] = await Promise.all([ embeddings.embedDocuments( docsWithContent.map((doc) => doc.pageContent), diff --git a/src/agents/webSearchAgent.ts b/src/agents/webSearchAgent.ts index 51653a0..1ff3354 100644 --- a/src/agents/webSearchAgent.ts +++ b/src/agents/webSearchAgent.ts @@ -227,7 +227,7 @@ const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => { The text will be shared inside the \`text\` XML tag, and the query inside the \`query\` XML tag. - + 1. \` Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. It was first released in 2013 and is developed by Docker, Inc. Docker is designed to make it easier to create, deploy, and run applications by using containers. @@ -241,6 +241,26 @@ const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => { Docker is a revolutionary platform-as-a-service product developed by Docker, Inc., that uses container technology to make application deployment more efficient. It allows developers to package their software with all necessary dependencies, making it easier to run in any environment. Released in 2013, Docker has transformed the way applications are built, deployed, and managed. + \` + 2. \` + The theory of relativity, or simply relativity, encompasses two interrelated theories of Albert Einstein: special relativity and general + relativity. However, the word "relativity" is sometimes used in reference to Galilean invariance. The term "theory of relativity" was based + on the expression "relative theory" used by Max Planck in 1906. The theory of relativity usually encompasses two interrelated theories by + Albert Einstein: special relativity and general relativity. Special relativity applies to all physical phenomena in the absence of gravity. + General relativity explains the law of gravitation and its relation to other forces of nature. It applies to the cosmological and astrophysical + realm, including astronomy. + + + + summarize + + + Response: + The theory of relativity, developed by Albert Einstein, encompasses two main theories: special relativity and general relativity. Special + relativity applies to all physical phenomena in the absence of gravity, while general relativity explains the law of gravitation and its + relation to other forces of nature. The theory of relativity is based on the concept of "relative theory," as introduced by Max Planck in + 1906. It is a fundamental theory in physics that has revolutionized our understanding of the universe. + \` Everything below is the actual data you will be working with. Good luck! diff --git a/src/websocket/connectionManager.ts b/src/websocket/connectionManager.ts index 04797c5..d980500 100644 --- a/src/websocket/connectionManager.ts +++ b/src/websocket/connectionManager.ts @@ -78,6 +78,18 @@ export const handleConnection = async ( ws.close(); } + const interval = setInterval(() => { + if (ws.readyState === ws.OPEN) { + ws.send( + JSON.stringify({ + type: 'signal', + data: 'open', + }), + ); + clearInterval(interval); + } + }, 5); + ws.on( 'message', async (message) => diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx index 6a56f7e..a80a5c9 100644 --- a/ui/components/ChatWindow.tsx +++ b/ui/components/ChatWindow.tsx @@ -171,11 +171,22 @@ const useSocket = ( } }, 10000); - ws.onopen = () => { - console.log('[DEBUG] open'); - clearTimeout(timeoutId); - setIsWSReady(true); - }; + ws.addEventListener('message', (e) => { + const data = JSON.parse(e.data); + if (data.type === 'signal' && data.data === 'open') { + const interval = setInterval(() => { + if (ws.readyState === 1) { + setIsWSReady(true); + clearInterval(interval); + } + }, 5); + clearTimeout(timeoutId); + console.log('[DEBUG] opened'); + } + if (data.type === 'error') { + toast.error(data.data); + } + }); ws.onerror = () => { clearTimeout(timeoutId); @@ -189,13 +200,6 @@ const useSocket = ( console.log('[DEBUG] closed'); }; - ws.addEventListener('message', (e) => { - const data = JSON.parse(e.data); - if (data.type === 'error') { - toast.error(data.data); - } - }); - setWs(ws); }; @@ -325,6 +329,7 @@ const ChatWindow = ({ id }: { id?: string }) => { useEffect(() => { if (isMessagesLoaded && isWSReady) { setIsReady(true); + console.log('[DEBUG] ready'); } }, [isMessagesLoaded, isWSReady]); @@ -477,7 +482,7 @@ const ChatWindow = ({ id }: { id?: string }) => { sendMessage(initialMessage); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [isReady, initialMessage, ws?.readyState]); + }, [ws?.readyState, isReady, initialMessage, isWSReady]); if (hasError) { return ( diff --git a/yarn.lock b/yarn.lock index 1c34e1d..deb35ea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -690,6 +690,13 @@ resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.8.tgz#7545ba4fc3c003d6c756f651f3bf163d8f0f29ba" integrity sha512-jg+97EGIcY9AGHJJRaaPVgetKDsrTgbRjQ5Msgjh/DQKEFl0DtyRr/VCOyD1T2R1MNeWPK/u7JoGhlDZnKBAfA== +"@types/ws@^8.5.12": + version "8.5.12" + resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.12.tgz#619475fe98f35ccca2a2f6c137702d85ec247b7e" + integrity sha512-3tPRkv1EtkDpzlgyKyI8pGsGZAGPEaXeu0DOj5DI25Ja91bdAYddYHbADRYVrZMRbfW+1l5YwXVDKohDJNQxkQ== + dependencies: + "@types/node" "*" + "@xenova/transformers@^2.17.1": version "2.17.1" resolved "https://registry.yarnpkg.com/@xenova/transformers/-/transformers-2.17.1.tgz#712f7a72c76c8aa2075749382f83dc7dd4e5a9a5"