feat(chat): prevent ws not open errors

This commit is contained in:
ItzCrazyKns 2024-10-15 18:04:50 +05:30
parent 7532c436db
commit 3a01eebc04
6 changed files with 59 additions and 15 deletions

View File

@ -18,6 +18,7 @@
"@types/html-to-text": "^9.0.4", "@types/html-to-text": "^9.0.4",
"@types/pdf-parse": "^1.1.4", "@types/pdf-parse": "^1.1.4",
"@types/readable-stream": "^4.0.11", "@types/readable-stream": "^4.0.11",
"@types/ws": "^8.5.12",
"drizzle-kit": "^0.22.7", "drizzle-kit": "^0.22.7",
"nodemon": "^3.1.0", "nodemon": "^3.1.0",
"prettier": "^3.2.5", "prettier": "^3.2.5",

View File

@ -167,7 +167,6 @@ const createBasicAcademicSearchAnsweringChain = (
if (optimizationMode === 'speed') { if (optimizationMode === 'speed') {
return docsWithContent.slice(0, 15); return docsWithContent.slice(0, 15);
} else if (optimizationMode === 'balanced') { } else if (optimizationMode === 'balanced') {
console.log('Balanced mode');
const [docEmbeddings, queryEmbedding] = await Promise.all([ const [docEmbeddings, queryEmbedding] = await Promise.all([
embeddings.embedDocuments( embeddings.embedDocuments(
docsWithContent.map((doc) => doc.pageContent), docsWithContent.map((doc) => doc.pageContent),

View File

@ -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. The text will be shared inside the \`text\` XML tag, and the query inside the \`query\` XML tag.
<example> <example>
<text> 1. \`<text>
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. 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 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. 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 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 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. any environment. Released in 2013, Docker has transformed the way applications are built, deployed, and managed.
\`
2. \`<text>
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.
</text>
<query>
summarize
</query>
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.
\`
</example> </example>
Everything below is the actual data you will be working with. Good luck! Everything below is the actual data you will be working with. Good luck!

View File

@ -78,6 +78,18 @@ export const handleConnection = async (
ws.close(); ws.close();
} }
const interval = setInterval(() => {
if (ws.readyState === ws.OPEN) {
ws.send(
JSON.stringify({
type: 'signal',
data: 'open',
}),
);
clearInterval(interval);
}
}, 5);
ws.on( ws.on(
'message', 'message',
async (message) => async (message) =>

View File

@ -171,11 +171,22 @@ const useSocket = (
} }
}, 10000); }, 10000);
ws.onopen = () => { ws.addEventListener('message', (e) => {
console.log('[DEBUG] open'); const data = JSON.parse(e.data);
clearTimeout(timeoutId); if (data.type === 'signal' && data.data === 'open') {
setIsWSReady(true); 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 = () => { ws.onerror = () => {
clearTimeout(timeoutId); clearTimeout(timeoutId);
@ -189,13 +200,6 @@ const useSocket = (
console.log('[DEBUG] closed'); console.log('[DEBUG] closed');
}; };
ws.addEventListener('message', (e) => {
const data = JSON.parse(e.data);
if (data.type === 'error') {
toast.error(data.data);
}
});
setWs(ws); setWs(ws);
}; };
@ -325,6 +329,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
useEffect(() => { useEffect(() => {
if (isMessagesLoaded && isWSReady) { if (isMessagesLoaded && isWSReady) {
setIsReady(true); setIsReady(true);
console.log('[DEBUG] ready');
} }
}, [isMessagesLoaded, isWSReady]); }, [isMessagesLoaded, isWSReady]);
@ -477,7 +482,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
sendMessage(initialMessage); sendMessage(initialMessage);
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [isReady, initialMessage, ws?.readyState]); }, [ws?.readyState, isReady, initialMessage, isWSReady]);
if (hasError) { if (hasError) {
return ( return (

View File

@ -690,6 +690,13 @@
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.8.tgz#7545ba4fc3c003d6c756f651f3bf163d8f0f29ba" resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.8.tgz#7545ba4fc3c003d6c756f651f3bf163d8f0f29ba"
integrity sha512-jg+97EGIcY9AGHJJRaaPVgetKDsrTgbRjQ5Msgjh/DQKEFl0DtyRr/VCOyD1T2R1MNeWPK/u7JoGhlDZnKBAfA== 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": "@xenova/transformers@^2.17.1":
version "2.17.1" version "2.17.1"
resolved "https://registry.yarnpkg.com/@xenova/transformers/-/transformers-2.17.1.tgz#712f7a72c76c8aa2075749382f83dc7dd4e5a9a5" resolved "https://registry.yarnpkg.com/@xenova/transformers/-/transformers-2.17.1.tgz#712f7a72c76c8aa2075749382f83dc7dd4e5a9a5"