Skip to content

Commit

Permalink
feat(chat): prevent ws not open errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzCrazyKns committed Oct 15, 2024
1 parent 7532c43 commit 3a01eeb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/agents/academicSearchAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
22 changes: 21 additions & 1 deletion src/agents/webSearchAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<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.
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.
Expand All @@ -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. \`<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>
Everything below is the actual data you will be working with. Good luck!
Expand Down
12 changes: 12 additions & 0 deletions src/websocket/connectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
31 changes: 18 additions & 13 deletions ui/components/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};

Expand Down Expand Up @@ -325,6 +329,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
useEffect(() => {
if (isMessagesLoaded && isWSReady) {
setIsReady(true);
console.log('[DEBUG] ready');
}
}, [isMessagesLoaded, isWSReady]);

Expand Down Expand Up @@ -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 (
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3a01eeb

Please sign in to comment.