diff --git a/main.wasp b/main.wasp index db74897..e076da7 100644 --- a/main.wasp +++ b/main.wasp @@ -68,6 +68,9 @@ app chatApp { ("stripe", "11.15.0"), ("markdown-to-jsx", "7.3.2"), ], + webSocket: { + fn: import { webSocketFn } from "@server/webSocket.js" + }, } /* 💽 Wasp defines DB entities via Prisma Database Models: diff --git a/src/client/components/ConversationWrapper.tsx b/src/client/components/ConversationWrapper.tsx index d60ecde..a640ae6 100644 --- a/src/client/components/ConversationWrapper.tsx +++ b/src/client/components/ConversationWrapper.tsx @@ -7,6 +7,7 @@ import { useQuery } from "@wasp/queries"; import updateConversation from "@wasp/actions/updateConversation"; import getAgentResponse from "@wasp/actions/getAgentResponse"; import getConversations from "@wasp/queries/getConversations"; +import { useSocket, useSocketListener } from "@wasp/webSocket"; import ConversationsList from "./ConversationList"; import Loader from "./Loader"; @@ -41,8 +42,16 @@ export default function ConversationWrapper() { // @ts-ignore const { id } = useParams(); const [isLoading, setIsLoading] = useState(false); + // const { socket, isConnected } = useSocket(); + // const { refreshChatUI, setRefreshChatUI } = useState(false); const chatContainerRef = useRef(null); + // useSocketListener("updateChatUI", updateChatUI); + + // function updateChatUI() { + // setRefreshChatUI(true); + // } + const loginMsgQuery: any = getQueryParam("msg"); const formInputRef = useCallback( (node: any) => { diff --git a/src/server/webSocket.js b/src/server/webSocket.js new file mode 100644 index 0000000..eef1350 --- /dev/null +++ b/src/server/webSocket.js @@ -0,0 +1,70 @@ +import HttpError from "@wasp/core/HttpError.js"; + +const ADS_SERVER_URL = process.env.ADS_SERVER_URL || "http://127.0.0.1:9000"; + +export const webSocketFn = (io, context) => { + io.on("connection", async (socket) => { + if (socket.data.user) { + const userEmail = socket.data.user.email; + console.log("========"); + console.log("a user connected: ", userEmail); + + // Check for updates every 5 seconds + const updateInterval = setInterval(async () => { + console.log("Check for inprogress tasks update"); + const conversations = await context.entities.Conversation.findMany({ + where: { userId: socket.data.user.id, status: "inprogress" }, + }); + + conversations.length > 0 && + conversations.forEach(async function (conversation) { + try { + const payload = { + conversation_id: conversation.id, + }; + const response = await fetch( + `${ADS_SERVER_URL}/openai/get-team-status`, + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(payload), + } + ); + + const json = await response.json(); + + // Todo: check how throwing the below error affects the user experience + if (!response.ok) { + const errorMsg = + json.detail || + `HTTP error with status code ${response.status}`; + console.error("Server Error:", errorMsg); + throw new Error(errorMsg); + } + + const conversation_status = json["status"]; + if (conversation_status === "ready") { + const updated_conversation = conversation.conversation.concat([ + { role: "assistant", content: json["msg"] }, + ]); + await context.entities.Conversation.update({ + where: { + // userId: socket.data.user.id, + id: conversation.id, + }, + data: { + conversation: updated_conversation, + status: conversation_status, + }, + }); + + // io.emit("updateChatUI"); + } + } catch (error) { + throw new HttpError(500, error); + } + }); + }, 5000); + } + }); +};