Merge remote-tracking branch 'origin/websocket' into chat_and_cache
This commit is contained in:
commit
35b4b50b8f
2 changed files with 115 additions and 80 deletions
|
|
@ -14,7 +14,8 @@ export default function Chat({ flow }: ChatType) {
|
|||
const [open, setOpen] = useState(false);
|
||||
useEffect(() => {
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === "K" && event.shiftKey && (event.metaKey||event.ctrlKey)) {
|
||||
event.preventDefault()
|
||||
if ((event.key === "K"||event.key==="k") && (event.metaKey||event.ctrlKey)) {
|
||||
setOpen(oldState=>!oldState);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ export default function ChatModal({
|
|||
setOpen: Function;
|
||||
flow: FlowType;
|
||||
}) {
|
||||
const { updateFlow } = useContext(TabsContext);
|
||||
const [chatValue, setChatValue] = useState("");
|
||||
const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]);
|
||||
const { reactFlowInstance } = useContext(typesContext);
|
||||
|
|
@ -55,8 +54,8 @@ export default function ChatModal({
|
|||
};
|
||||
|
||||
function connectWS() {
|
||||
// Check if the app is running with npm start or npm run build
|
||||
// if npm start, use localhost, otherwise use the windows.location.host
|
||||
console.log("conectou");
|
||||
try {
|
||||
const urlWs =
|
||||
process.env.NODE_ENV === "development"
|
||||
? `ws://localhost:7860/chat/${flow.id}`
|
||||
|
|
@ -124,6 +123,8 @@ export default function ChatModal({
|
|||
} catch (error) {
|
||||
if (event.data !== "Error: 1005") {
|
||||
setErrorData({ title: event.data });
|
||||
newWs.close();
|
||||
connectWS();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -132,25 +133,58 @@ export default function ChatModal({
|
|||
setLockChat(false);
|
||||
setTimeout(() => {
|
||||
connectWS();
|
||||
}, 100);
|
||||
}, 1000);
|
||||
}
|
||||
};
|
||||
newWs.onerror = (ev) => {
|
||||
console.log(ev, "error");
|
||||
};
|
||||
setWs(newWs);
|
||||
|
||||
return newWs;
|
||||
} catch {
|
||||
setErrorData({
|
||||
title: "There was an error on web connection, please: ",
|
||||
list: [
|
||||
"refresh the page",
|
||||
"use a new flow tab",
|
||||
"check if the backend is up",
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (ws && (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING)) {
|
||||
let newWs = connectWS();
|
||||
return () => {
|
||||
console.log("trigger");
|
||||
newWs.close();
|
||||
};
|
||||
}
|
||||
}, [lockChat]);
|
||||
|
||||
useEffect(() => {
|
||||
let newWs = connectWS();
|
||||
return () => {
|
||||
console.log("trigger");
|
||||
newWs.close();
|
||||
};
|
||||
}, []);
|
||||
|
||||
async function sendAll(data: sendAllProps) {
|
||||
try {
|
||||
if (ws) {
|
||||
ws.send(JSON.stringify(data));
|
||||
}
|
||||
} catch (error) {
|
||||
setErrorData({
|
||||
title: "There was an erro sending the message",
|
||||
list: [error.message],
|
||||
});
|
||||
setChatValue(data.message);
|
||||
connectWS();
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue