From a0e11522585cb7ebd4fb8e910a47fa50f9047c89 Mon Sep 17 00:00:00 2001 From: shatfield4 Date: Thu, 25 Jan 2024 17:26:36 -0800 Subject: [PATCH] WIP got response from backend in embedded app --- embedded-app/src/App.jsx | 120 ++++++++++++++++++++++++++----- frontend/src/models/workspace.js | 66 +++++++++++++++++ server/endpoints/chat.js | 3 +- server/endpoints/embedded.js | 16 ++++- 4 files changed, 183 insertions(+), 22 deletions(-) diff --git a/embedded-app/src/App.jsx b/embedded-app/src/App.jsx index 23215d67..105fdd30 100644 --- a/embedded-app/src/App.jsx +++ b/embedded-app/src/App.jsx @@ -15,25 +15,105 @@ export default function App() { id = v4(); localStorage.setItem("userId", id); } - setUserId(id); }, []); - const toggleOpen = () => setIsOpen(!isOpen); - - const streamMessages = () => { - const eventSource = fetchEventSource( - `http://localhost:3001/api/workspace/${userId}/embedded-chat` - ); - - eventSource.addEventListener("message", (event) => { - const data = JSON.parse(event.data); - console.log(data); - }); + const toggleOpen = () => { + setIsOpen(!isOpen); + // if (!isOpen) { + // streamMessages(); + // } }; - const sendMessage = () => { - console.log(message); + const streamMessages = async () => { + const ctrl = new AbortController(); + await fetchEventSource( + `http://localhost:3001/api/workspace/hello/stream-chat`, + { + method: "POST", + body: JSON.stringify({ message, mode: "query" }), + // headers: baseHeaders(), + signal: ctrl.signal, + openWhenHidden: true, + async onopen(response) { + if (response.ok) { + return; // everything's good + } else if ( + response.status >= 400 && + response.status < 500 && + response.status !== 429 + ) { + handleChat({ + id: v4(), + type: "abort", + textResponse: null, + sources: [], + close: true, + error: `An error occurred while streaming response. Code ${response.status}`, + }); + ctrl.abort(); + throw new Error("Invalid Status code response."); + } else { + handleChat({ + id: v4(), + type: "abort", + textResponse: null, + sources: [], + close: true, + error: `An error occurred while streaming response. Unknown Error.`, + }); + ctrl.abort(); + throw new Error("Unknown error"); + } + }, + async onmessage(msg) { + try { + const chatResult = JSON.parse(msg.data); + console.log(chatResult); + handleChat(chatResult); + } catch {} + }, + onerror(err) { + handleChat({ + id: v4(), + type: "abort", + textResponse: null, + sources: [], + close: true, + error: `An error occurred while streaming response. ${err.message}`, + }); + ctrl.abort(); + throw new Error(); + }, + } + ); + // fetchEventSource( + // `http://localhost:3001/api/workspace/${userId}/embedded-chat`, + // { + // onmessage(event) { + // try { + // // Assuming the data is in the format "data: " + // // Extracting the JSON part from the data and parsing it. + // const jsonPart = event.data.replace(/^data: /, ""); + // const parsedData = JSON.parse(jsonPart); + // console.log("Parsed data:", parsedData); + // } catch (error) { + // console.error("Error parsing event data:", error); + // } + // }, + // onopen() { + // console.log("Connection opened"); + // }, + // onerror(err) { + // console.error("EventSource failed:", err); + // }, + // } + // ); + }; + + const sendMessage = async () => { + console.log("EMBEDDED MESSAGE: ", message); + await streamMessages(); fetch(`http://localhost:3001/api/workspace/${userId}/embedded-chat`, { method: "POST", headers: { @@ -43,7 +123,12 @@ export default function App() { message: message, }), }) - .then((response) => response.json()) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + return response.json(); + }) .then((data) => { console.log("Success:", data); }) @@ -88,10 +173,7 @@ export default function App() { )}