added basic error handling
This commit is contained in:
parent
e453a22361
commit
bb18f128ce
1 changed files with 73 additions and 58 deletions
|
|
@ -49,77 +49,92 @@ export default function ChatModal({
|
||||||
return newChat;
|
return newChat;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
function connectWS(){
|
||||||
const newWs = new WebSocket(`ws://127.0.0.1:5003/chat/${flow.id}`);
|
const newWs = new WebSocket(`ws://127.0.0.1:5003/chat/${flow.id}`);
|
||||||
newWs.onopen = () => {
|
newWs.onopen = () => {
|
||||||
console.log("WebSocket connection established!");
|
console.log("WebSocket connection established!");
|
||||||
};
|
};
|
||||||
newWs.onmessage = (event) => {
|
newWs.onmessage = (event) => {
|
||||||
const data = JSON.parse(event.data);
|
try {
|
||||||
console.log("Received data:", data);
|
const data = JSON.parse(event.data);
|
||||||
//get chat history
|
console.log("Received data:", data);
|
||||||
if (Array.isArray(data)) {
|
//get chat history
|
||||||
console.log(data);
|
if (Array.isArray(data)) {
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
setChatHistory((_) => {
|
setChatHistory((_) => {
|
||||||
let newChatHistory: ChatMessageType[] = [];
|
let newChatHistory: ChatMessageType[] = [];
|
||||||
data.forEach(
|
data.forEach(
|
||||||
(chatItem: {
|
(chatItem: {
|
||||||
intermediate_steps?: "string";
|
intermediate_steps?: "string";
|
||||||
is_bot: boolean;
|
is_bot: boolean;
|
||||||
message: string;
|
message: string;
|
||||||
type: string;
|
type: string;
|
||||||
files?: Array<any>;
|
files?: Array<any>;
|
||||||
}) => {
|
}) => {
|
||||||
if (chatItem.message) {
|
if (chatItem.message) {
|
||||||
newChatHistory.push(
|
newChatHistory.push(
|
||||||
chatItem.files
|
chatItem.files
|
||||||
? {
|
? {
|
||||||
isSend: !chatItem.is_bot,
|
isSend: !chatItem.is_bot,
|
||||||
message: chatItem.message,
|
message: chatItem.message,
|
||||||
thought: chatItem.intermediate_steps,
|
thought: chatItem.intermediate_steps,
|
||||||
files: chatItem.files,
|
files: chatItem.files,
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
isSend: !chatItem.is_bot,
|
isSend: !chatItem.is_bot,
|
||||||
message: chatItem.message,
|
message: chatItem.message,
|
||||||
thought: chatItem.intermediate_steps,
|
thought: chatItem.intermediate_steps,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
);
|
||||||
);
|
return newChatHistory;
|
||||||
return newChatHistory;
|
});
|
||||||
});
|
}
|
||||||
}
|
if (data.type === "end") {
|
||||||
if (data.type === "end") {
|
if (data.files) {
|
||||||
if (data.files) {
|
addChatHistory(
|
||||||
addChatHistory(
|
data.message,
|
||||||
data.message,
|
false,
|
||||||
false,
|
data.intermediate_steps,
|
||||||
data.intermediate_steps,
|
data.files
|
||||||
data.files
|
);
|
||||||
);
|
} else {
|
||||||
} else {
|
addChatHistory(data.message, false, data.intermediate_steps);
|
||||||
addChatHistory(data.message, false, data.intermediate_steps);
|
}
|
||||||
|
setLockChat(false);
|
||||||
|
}
|
||||||
|
if (data.type == "file") {
|
||||||
|
console.log(data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
setErrorData({title:event.data})
|
||||||
|
if(newWs.readyState===newWs.CLOSED){
|
||||||
|
window.alert(error)
|
||||||
|
|
||||||
}
|
}
|
||||||
setLockChat(false);
|
|
||||||
}
|
}
|
||||||
if (data.type == "file") {
|
|
||||||
console.log(data);
|
|
||||||
}
|
|
||||||
// Do something with the data received from the WebSocket
|
|
||||||
};
|
};
|
||||||
newWs.onclose = (e) => {
|
newWs.onclose = (_) => {
|
||||||
console.log("close");
|
if(open){
|
||||||
setLockChat(false);
|
setLockChat(false);
|
||||||
};
|
setTimeout(() => {
|
||||||
newWs.onerror = (e) => {
|
connectWS()
|
||||||
window.alert("error");
|
}, 100);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
setWs(newWs);
|
setWs(newWs);
|
||||||
|
|
||||||
|
return newWs
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let newWs = connectWS()
|
||||||
return () => {
|
return () => {
|
||||||
newWs.close();
|
newWs.close();
|
||||||
};
|
};
|
||||||
|
|
@ -136,7 +151,7 @@ export default function ChatModal({
|
||||||
}, [chatHistory]);
|
}, [chatHistory]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (ws &&ws.readyState===ws.CLOSED) {
|
if (ws && ws.readyState === ws.CLOSED) {
|
||||||
setLockChat(false);
|
setLockChat(false);
|
||||||
}
|
}
|
||||||
}, [lockChat]);
|
}, [lockChat]);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue