MCPcopy Index your code
hub / github.com/admineral/OpenAI-Assistant-API-Chat / fetchAssistantResponse

Function fetchAssistantResponse

app/modules/chatModules.ts:40–72  ·  view source on GitHub ↗
(runId: string, threadId: string, setStatusMessage: (message: string) => void, setProgress: (progress: number) => void, initialProgress: number)

Source from the content-addressed store, hash-verified

38* @returns {Promise<string>} - A promise that resolves to the messages from the assistant.
39*/
40export const fetchAssistantResponse = async (runId: string, threadId: string, setStatusMessage: (message: string) => void, setProgress: (progress: number) => void, initialProgress: number): Promise<string> => {
41 try {
42 const startTime = Date.now(); // Get the current time at the start
43 setStatusMessage('Fetching assistant response...');
44 let status: string;
45 let fetchCount = 0; // Number of fetches so far
46 const maxFetches = 10; // Maximum number of fetches
47 do {
48 const statusData: StatusData = await checkRunStatus(threadId, runId);
49 status = statusData.status;
50 fetchCount++; // Increment the fetch count
51 const progress = initialProgress + ((fetchCount / maxFetches) * (90 - initialProgress)); // Calculate progress as a percentage
52 setProgress(progress); // Update the progress bar
53 if (status === 'cancelled' || status === 'cancelling' || status === 'failed' || status === 'expired') {
54 throw new Error(status);
55 }
56 const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2); // Calculate elapsed time in seconds
57 setStatusMessage(`Waiting for assistant response... Current status: ${status}. Time elapsed: ${elapsedTime} seconds.`);
58 await new Promise(resolve => setTimeout(resolve, 1000)); // Polling delay
59 } while (status !== 'completed');
60 setStatusMessage('Assistant response fetched successfully.');
61 setProgress(100); // Set progress to 100% after completion
62 const response = await listMessages(threadId, runId);
63 return response.messages;
64 } catch (error) {
65 setProgress(0); // Reset progress in case of error
66 if (error instanceof Error) {
67 setStatusMessage(`Error: ${error.message}`);
68 throw error; // Re-throw the error after setting the status message
69 }
70 throw error; // Re-throw the error if it's not an instance of Error
71 }
72};
73
74
75/**

Callers 3

startAssistantMethod · 0.90
startAssistantWithIdMethod · 0.90
sendMessageMethod · 0.90

Calls 2

checkRunStatusFunction · 0.90
listMessagesFunction · 0.90

Tested by

no test coverage detected