fix(formModal): fix issue with template not being updated correctly when receiving prompt data

feat(formModal): add support for clearing chat history and resetting template to initial value
This commit is contained in:
anovazzi1 2023-09-29 15:26:55 -03:00 • committed by anovazzi1
commit 2b1c77aed7

View file

@ -61,6 +61,7 @@ export default function FormModal({
}); });
const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]); const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]);
const template = useRef(tabsState[flow.id].formKeysData.template);
const { reactFlowInstance } = useContext(typesContext); const { reactFlowInstance } = useContext(typesContext);
const { accessToken } = useContext(AuthContext); const { accessToken } = useContext(AuthContext);
const { setErrorData } = useContext(alertContext); const { setErrorData } = useContext(alertContext);
@ -208,7 +209,17 @@ export default function FormModal({
if (Array.isArray(data) && data.length > 0) { if (Array.isArray(data) && data.length > 0) {
//set chat history //set chat history
setChatHistory((_) => { setChatHistory((_) => {
console.log(data);
let newChatHistory: ChatMessageType[] = []; let newChatHistory: ChatMessageType[] = [];
for (let i = 0; i < data.length; i++) {
if (data[i].type === "prompt" && data[i].prompt) {
if (data[i - 1] && !data[i - 1].is_bot) {
data[i - 1].prompt = data[i].prompt;
template.current = data[i].prompt;
}
}
}
data = data.filter((item: any) => item.type !== "prompt");
data.forEach( data.forEach(
(chatItem: { (chatItem: {
intermediate_steps?: string; intermediate_steps?: string;
@ -225,7 +236,7 @@ export default function FormModal({
? { ? {
isSend: !chatItem.is_bot, isSend: !chatItem.is_bot,
message: chatItem.message, message: chatItem.message,
template: tabsState[flow.id].formKeysData.template, template: chatItem.prompt,
thought: chatItem.intermediate_steps, thought: chatItem.intermediate_steps,
files: chatItem.files, files: chatItem.files,
chatKey: chatItem.chatKey, chatKey: chatItem.chatKey,
@ -233,7 +244,7 @@ export default function FormModal({
: { : {
isSend: !chatItem.is_bot, isSend: !chatItem.is_bot,
message: chatItem.message, message: chatItem.message,
template: tabsState[flow.id].formKeysData.template, template: chatItem.prompt,
thought: chatItem.intermediate_steps, thought: chatItem.intermediate_steps,
chatKey: chatItem.chatKey, chatKey: chatItem.chatKey,
} }
@ -265,6 +276,9 @@ export default function FormModal({
files: data.files, files: data.files,
}); });
} }
if (data.type === "prompt" && data.prompt) {
template.current = data.prompt;
}
setLockChat(false); setLockChat(false);
isStream = false; isStream = false;
@ -378,12 +392,7 @@ export default function FormModal({
let inputs = tabsState[id.current].formKeysData.input_keys; let inputs = tabsState[id.current].formKeysData.input_keys;
setChatValue(""); setChatValue("");
const message = inputs; const message = inputs;
addChatHistory( addChatHistory(message!, true, chatKey!, template.current);
message!,
true,
chatKey!,
tabsState[flow.id].formKeysData.template
);
sendAll({ sendAll({
...reactFlowInstance?.toObject()!, ...reactFlowInstance?.toObject()!,
inputs: inputs!, inputs: inputs!,
@ -408,6 +417,7 @@ export default function FormModal({
} }
function clearChat(): void { function clearChat(): void {
setChatHistory([]); setChatHistory([]);
template.current = tabsState[id.current].formKeysData.template;
ws.current?.send(JSON.stringify({ clear_history: true })); ws.current?.send(JSON.stringify({ clear_history: true }));
if (lockChat) setLockChat(false); if (lockChat) setLockChat(false);
} }