-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dde0206
commit 18ed6d1
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
}; |