const createRun = async (threadId: string, assistantId: string) => {
try {
const run = await openai.beta.threads.runs.create(threadId, {
assistant_id: assistantId,
});
console.log(run);
if (run.id) {
return startPolling(run.id, threadId);
}
} catch (error) {
console.error(error);
return "Error while creating run.";
}
};
const startPolling = async (runId: string, threadId: string) => {
if (!threadId) return;
console.log("Start polling");
const poll = async () => {
try {
const response = await openai.beta.threads.runs.retrieve(threadId, runId);
console.log(response);
if (
["cancelled", "failed", "completed", "expired"].includes(
response.status
)
) {
const completion = await openai.beta.threads.messages.list(threadId);
clearInterval(intervalId);
console.log(completion.data);
return completion.data;
}
} catch (error) {
console.error("Error polling run status:", error);
clearInterval(intervalId);
return "Error polling run status";
}
};
const intervalId = setInterval(poll, 1000);
return poll;
};
I am creating a run, and then calling the startPolling function once I have the run.id. In the startPolling func, I want to retrieve the run and check for status after every1s till status is completed or failed.
But when I am implementing the above approach, the polling func isn't working perfectly, I feel like it's only running once or not running at all and I am also getting Promise errors.
Can you help me with this? I want to create a func that retrieves the run every 1s till the sttaus of run is completed or failed.