From 5088d8471afce3b24f22bb43995c1caf042170d8 Mon Sep 17 00:00:00 2001 From: sherifButt <90522472+sherifButt@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:51:23 +0000 Subject: [PATCH] -added switch between streaming response and not, to stop streaming while using dynamic input, as it require the full response to allow json schema extraction - fixed auto page refresh after chat reset (quick line) - added temporary isDynamicInput as static variable for development, later to be added as dynamic setting to workspace settings menue --- .../PromptInput/SlashCommands/index.jsx | 1 + .../WorkspaceChat/ChatContainer/index.jsx | 22 +++++- .../src/components/WorkspaceChat/index.jsx | 22 ++++-- frontend/src/utils/chat/extractMetaData.js | 77 ++++++++++--------- frontend/src/utils/chat/index.js | 2 + 5 files changed, 77 insertions(+), 47 deletions(-) diff --git a/frontend/src/components/WorkspaceChat/ChatContainer/PromptInput/SlashCommands/index.jsx b/frontend/src/components/WorkspaceChat/ChatContainer/PromptInput/SlashCommands/index.jsx index 1e85d372..4bd738f9 100644 --- a/frontend/src/components/WorkspaceChat/ChatContainer/PromptInput/SlashCommands/index.jsx +++ b/frontend/src/components/WorkspaceChat/ChatContainer/PromptInput/SlashCommands/index.jsx @@ -56,6 +56,7 @@ export function SlashCommands({ showing, setShowing, sendCommand }) { onClick={() => { setShowing(false); sendCommand("/reset", true); + window.location.reload(); }} className="w-full hover:cursor-pointer hover:bg-zinc-700 px-2 py-2 rounded-xl flex flex-col justify-start" > diff --git a/frontend/src/components/WorkspaceChat/ChatContainer/index.jsx b/frontend/src/components/WorkspaceChat/ChatContainer/index.jsx index 293da491..1924971e 100644 --- a/frontend/src/components/WorkspaceChat/ChatContainer/index.jsx +++ b/frontend/src/components/WorkspaceChat/ChatContainer/index.jsx @@ -6,12 +6,18 @@ import handleChat from "@/utils/chat"; import { isMobile } from "react-device-detect"; import { SidebarMobileHeader } from "../../Sidebar"; import { useParams } from "react-router-dom"; +import { extractMetaData } from "@/utils/chat/extractMetaData"; -export default function ChatContainer({ workspace, knownHistory = [] }) { + + +export default function ChatContainer({ workspace, knownHistory = [], isDynamicInput}) { const { threadSlug = null } = useParams(); const [message, setMessage] = useState(""); const [loadingResponse, setLoadingResponse] = useState(false); const [chatHistory, setChatHistory] = useState(knownHistory); + const [finalizedChatHistory, setFinalizedChatHistory] = + useState(knownHistory); + const [currentInputMeta, setCurrentInputMeta] = useState(null); const handleMessageChange = (event) => { setMessage(event.target.value); }; @@ -33,6 +39,7 @@ export default function ChatContainer({ workspace, knownHistory = [] }) { ]; setChatHistory(prevChatHistory); + setFinalizedChatHistory(prevChatHistory); setMessage(""); setLoadingResponse(true); }; @@ -100,6 +107,17 @@ export default function ChatContainer({ workspace, knownHistory = [] }) { ) ); } + + if (isDynamicInput) { + const { remainingText, metaData } = extractMetaData( + _chatHistory[_chatHistory.length - 1].content + ); + _chatHistory[_chatHistory.length - 1].content = remainingText; + setFinalizedChatHistory(_chatHistory); + setCurrentInputMeta(metaData); + console.log("metaData", metaData); + } + return; } loadingResponse === true && fetchReply(); @@ -113,7 +131,7 @@ export default function ChatContainer({ workspace, knownHistory = [] }) { {isMobile && }
diff --git a/frontend/src/components/WorkspaceChat/index.jsx b/frontend/src/components/WorkspaceChat/index.jsx index 38c4dbde..964b6319 100644 --- a/frontend/src/components/WorkspaceChat/index.jsx +++ b/frontend/src/components/WorkspaceChat/index.jsx @@ -12,6 +12,8 @@ export default function WorkspaceChat({ loading, workspace }) { const [history, setHistory] = useState([]); const [loadingHistory, setLoadingHistory] = useState(true); + const isDynamicInput = false; + useEffect(() => { async function getHistory() { if (loading) return; @@ -26,13 +28,17 @@ export default function WorkspaceChat({ loading, workspace }) { // TODO: add conditional if dynamic input is enabled in the workspace by default is false // Append metadata to the chat history - chatHistory = chatHistory.map((message) => { - if (message.role === "assistant") { - const { remainingText, metaData } = extractMetaData(message.content); - return { ...message, content: remainingText, metaData }; - } - return message; - }); + if (isDynamicInput) { + chatHistory = chatHistory.map((message) => { + if (message.role === "assistant") { + const { remainingText, metaData } = extractMetaData( + message.content + ); + return { ...message, content: remainingText, metaData }; + } + return message; + }); + } setHistory(chatHistory); setLoadingHistory(false); @@ -73,7 +79,7 @@ export default function WorkspaceChat({ loading, workspace }) { } setEventDelegatorForCodeSnippets(); - return ; + return ; } // Enables us to safely markdown and sanitize all responses without risk of injection diff --git a/frontend/src/utils/chat/extractMetaData.js b/frontend/src/utils/chat/extractMetaData.js index 2b7849f5..90fa9ef7 100644 --- a/frontend/src/utils/chat/extractMetaData.js +++ b/frontend/src/utils/chat/extractMetaData.js @@ -5,49 +5,52 @@ */ export const extractMetaData = (textResponse) => { - let remainingText = textResponse; - let inString = false; - let char, prevChar; - let braceCount = 0; - let bracketCount = 0; - let startIndex = null; - let extractedObjects = {}; // Keep as an object as requested + console.log("textResponse", textResponse); + let remainingText = textResponse; + let inString = false; + let char, prevChar; + let braceCount = 0; + let bracketCount = 0; + let startIndex = null; + let extractedObjects = {}; // Keep as an object as requested - for (let i = 0; i < textResponse.length; i++) { - char = textResponse[i]; - if (char === '"' && prevChar !== '\\') inString = !inString; - if (inString) continue; + for (let i = 0; i < textResponse.length; i++) { + char = textResponse[i]; + if (char === '"' && prevChar !== "\\") inString = !inString; + if (inString) continue; - if (char === '{' || char === '[') { - if (braceCount === 0 && bracketCount === 0) startIndex = i; - if (char === '{') braceCount++; - if (char === '[') bracketCount++; - } else if (char === '}' || char === ']') { - if (char === '}') braceCount--; - if (char === ']') bracketCount--; + if (char === "{" || char === "[") { + if (braceCount === 0 && bracketCount === 0) startIndex = i; + if (char === "{") braceCount++; + if (char === "[") bracketCount++; + } else if (char === "}" || char === "]") { + if (char === "}") braceCount--; + if (char === "]") bracketCount--; - if (braceCount === 0 && bracketCount === 0 && startIndex !== null) { - let json = textResponse.substring(startIndex, i + 1); + if (braceCount === 0 && bracketCount === 0 && startIndex !== null) { + let json = textResponse.substring(startIndex, i + 1); - try { - let parsedJson = JSON.parse(json); - for (let key in parsedJson) { - if (parsedJson.hasOwnProperty(key)) { - extractedObjects[key] = parsedJson[key]; - } - } - } catch (error) { - console.error("Error parsing JSON:", error, 'in JSON:', json); - } - startIndex = null; + try { + let parsedJson = JSON.parse(json); + console.log("Parsed JSON:", parsedJson); + for (let key in parsedJson) { + if (parsedJson.hasOwnProperty(key)) { + extractedObjects[key] = parsedJson[key]; } + } + } catch (error) { + console.error("Error parsing JSON:", error, "in JSON:", json); } - prevChar = char; + startIndex = null; + } } + prevChar = char; + } - // Remove any json objects from the text tat starts with ```json nad ends with ``` - const jsonRegex = /```json[\s\S]*?```/g; - remainingText = remainingText.replace(jsonRegex, ''); - - return { remainingText, metaData: extractedObjects }; + // Remove any json objects from the text tat starts with ```json nad ends with ``` + const jsonRegex = /```json[\s\S]*?```/g; + remainingText = remainingText.replace(jsonRegex, ""); +console.log("remainingText", remainingText); +console.log("extractedObjects", extractedObjects); + return { remainingText, metaData: extractedObjects }; } \ No newline at end of file diff --git a/frontend/src/utils/chat/index.js b/frontend/src/utils/chat/index.js index f1df11fe..441d9fcf 100644 --- a/frontend/src/utils/chat/index.js +++ b/frontend/src/utils/chat/index.js @@ -97,6 +97,7 @@ export default function handleChat( }); } setChatHistory([..._chatHistory]); + console.log("chunk: ", [..._chatHistory]); } else if (type === "finalizeResponseStream") { const chatIdx = _chatHistory.findIndex((chat) => chat.uuid === uuid); if (chatIdx !== -1) { @@ -108,6 +109,7 @@ export default function handleChat( _chatHistory[chatIdx] = updatedHistory; } setChatHistory([..._chatHistory]); + console.log("final: ", [..._chatHistory]); } }