removed chat history from flow save

This commit is contained in:
anovazzi1 2023-04-25 17:34:14 -03:00
commit b526c436cd
4 changed files with 60 additions and 68 deletions

View file

@ -4,14 +4,13 @@ import {
useState,
} from "react";
import { ChatType } from "../../types/chat";
import { ChatMessageType, ChatType } from "../../types/chat";
import ChatTrigger from "./chatTrigger";
import ChatModal from "../../modals/chatModal";
const _ = require("lodash");
export default function Chat({ flow }: ChatType) {
const [open, setOpen] = useState(false);
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {

View file

@ -1,4 +1,11 @@
import { createContext, useEffect, useState, useRef, ReactNode, useContext } from "react";
import {
createContext,
useEffect,
useState,
useRef,
ReactNode,
useContext,
} from "react";
import { FlowType } from "../types/flow";
import { TabsContextType } from "../types/tabs";
import { normalCaseToSnakeCase } from "../utils";
@ -25,7 +32,7 @@ export const TabsContext = createContext<TabsContextType>(
);
export function TabsProvider({ children }: { children: ReactNode }) {
const {setNoticeData} = useContext(alertContext)
const { setNoticeData } = useContext(alertContext);
const [tabIndex, setTabIndex] = useState(0);
const [flows, setFlows] = useState<Array<FlowType>>([]);
const [id, setId] = useState(0);
@ -45,11 +52,9 @@ export function TabsProvider({ children }: { children: ReactNode }) {
}
useEffect(() => {
//save tabs locally
save()
save();
}, [flows, id, tabIndex, newNodeId]);
useEffect(() => {
//get tabs locally saved
let cookie = window.localStorage.getItem("tabsData");
@ -63,7 +68,9 @@ export function TabsProvider({ children }: { children: ReactNode }) {
}, []);
function hardReset() {
newNodeId.current = 0;
setTabIndex(0);setFlows([]);setId(0);
setTabIndex(0);
setFlows([]);
setId(0);
}
/**
@ -82,7 +89,9 @@ export function TabsProvider({ children }: { children: ReactNode }) {
// simulate a click on the link element to trigger the download
link.click();
setNoticeData({title:"Warning: Critical data,JSON file may including API keys."})
setNoticeData({
title: "Warning: Critical data,JSON file may including API keys.",
});
}
/**
@ -139,7 +148,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
function addFlow(flow?: FlowType) {
// Get data from the flow or set it to null if there's no flow provided.
const data = flow?.data ? flow.data : null;
const description = flow?.description?flow.description:""
const description = flow?.description ? flow.description : "";
// Create a new flow with a default name if no flow is provided.
let newFlow: FlowType = {
@ -147,7 +156,6 @@ export function TabsProvider({ children }: { children: ReactNode }) {
name: flow?.name ?? "New Flow",
id: id.toString(),
data,
chat: flow ? flow.chat : [],
};
// Increment the ID counter.
@ -171,10 +179,9 @@ export function TabsProvider({ children }: { children: ReactNode }) {
const newFlows = [...prevState];
const index = newFlows.findIndex((flow) => flow.id === newFlow.id);
if (index !== -1) {
newFlows[index].description = newFlow.description??""
newFlows[index].description = newFlow.description ?? "";
newFlows[index].data = newFlow.data;
newFlows[index].name = newFlow.name;
newFlows[index].chat = newFlow.chat;
}
return newFlows;
});

View file

@ -10,15 +10,23 @@ import { typesContext } from "../../contexts/typesContext";
import ChatMessage from "./chatMessage";
import { FaEraser } from "react-icons/fa";
import { sendAllProps } from "../../types/api";
import { ChatMessageType } from "../../types/chat";
const _ = require("lodash");
export default function ChatModal({ flow, open, setOpen }:{open:boolean,setOpen:Function,flow:FlowType}) {
const { updateFlow, lockChat, setLockChat, flows, tabIndex } =
export default function ChatModal({
flow,
open,
setOpen,
}: {
open: boolean;
setOpen: Function;
flow: FlowType;
}) {
const { updateFlow, lockChat, setLockChat} =
useContext(TabsContext);
const [saveChat, setSaveChat] = useState(false);
const [chatValue, setChatValue] = useState("");
const [chatHistory, setChatHistory] = useState(flow.chat);
const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]);
const { reactFlowInstance } = useContext(typesContext);
const { setErrorData, setNoticeData } = useContext(alertContext);
const [ws, setWs] = useState<WebSocket | null>(null);
@ -27,13 +35,9 @@ export default function ChatModal({ flow, open, setOpen }:{open:boolean,setOpen:
isSend: boolean,
thought?: string
) => {
let tabsChange = false;
setChatHistory((old) => {
let newChat = _.cloneDeep(old);
if (JSON.stringify(flow.chat) !== JSON.stringify(old)) {
tabsChange = true;
return old;
}
if (thought) {
newChat.push({ message, isSend, thought });
} else {
@ -41,30 +45,20 @@ export default function ChatModal({ flow, open, setOpen }:{open:boolean,setOpen:
}
return newChat;
});
if (tabsChange) {
if (thought) {
updateFlow({
..._.cloneDeep(flow),
chat: [...flow.chat, { isSend, message, thought }],
});
} else {
updateFlow({
..._.cloneDeep(flow),
chat: [...flow.chat, { isSend, message }],
});
}
}
setSaveChat((chat) => !chat);
};
useEffect(() => {
const newWs = new WebSocket(`ws://localhost:7860/chat/${flow.id}`);
newWs.onopen = () => {
console.log('WebSocket connection established!');
console.log("WebSocket connection established!");
};
newWs.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received data:', data);
console.log("Received data:", data);
if(Array.isArray(data)){
console.log("entrou")
setChatHistory([{isSend:true,message:"sdsdsad"}])
}
// Do something with the data received from the WebSocket
};
setWs(newWs);
@ -78,16 +72,9 @@ export default function ChatModal({ flow, open, setOpen }:{open:boolean,setOpen:
if (ws) {
ws.send(JSON.stringify(data));
}
return {data:{result:"sdsdsad",thought:"dsdsad"}}
return { data: { result: "sdsdsad", thought: "dsdsad" } };
}
useEffect(() => {
updateFlow({ ..._.cloneDeep(flow), chat: chatHistory });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [saveChat]);
useEffect(() => {
setChatHistory(flow.chat);
}, [flow]);
useEffect(() => {
if (ref.current) ref.current.scrollIntoView({ behavior: "smooth" });
}, [chatHistory]);

View file

@ -6,7 +6,6 @@ export type FlowType = {
name: string;
id: string;
data: ReactFlowJsonObject;
chat: Array<ChatMessageType>;
description:string;
};
export type NodeType = {id:string,type:string,position:XYPosition,data:NodeDataType}