| 16 | ]; |
| 17 | |
| 18 | export async function sendFunLoadingMessages<PromiseResponse>( |
| 19 | promise: Promise<PromiseResponse>, |
| 20 | streamInfo: (message: StreamingStepInput) => void, |
| 21 | ): Promise<PromiseResponse> { |
| 22 | /** Sends fun loading messages while waiting for the promise to resolve */ |
| 23 | let continueMessages = true; |
| 24 | const output: PromiseResponse | "FAILED" = await Promise.race([ |
| 25 | promise, |
| 26 | (async (): Promise<"FAILED"> => { |
| 27 | const startTime = Date.now(); |
| 28 | // Until 120s have passed, or the promise has resolved |
| 29 | while (continueMessages && Date.now() - startTime < 120000) { |
| 30 | streamInfo({ |
| 31 | role: "loading", |
| 32 | content: |
| 33 | funLoadingMessages[ |
| 34 | Math.floor(Math.random() * funLoadingMessages.length) |
| 35 | ], |
| 36 | }); |
| 37 | await new Promise((resolve) => |
| 38 | // 3 seconds +- 1 seconds of random delay |
| 39 | setTimeout(resolve, 3000 + (Math.random() - 0.5) * 2000), |
| 40 | ); |
| 41 | } |
| 42 | return "FAILED"; |
| 43 | })(), |
| 44 | ]); |
| 45 | if (output === "FAILED") { |
| 46 | throw new Error("sendFunLoadingMessages promise hung for 2 mins"); |
| 47 | } |
| 48 | continueMessages = false; |
| 49 | return output; |
| 50 | } |