(config: Config, opts: PollOptions)
| 14 | } |
| 15 | |
| 16 | export async function poll<T>(config: Config, opts: PollOptions): Promise<T> { |
| 17 | const deadline = Date.now() + opts.timeoutSec * 1000; |
| 18 | const spinner = createSpinner('Polling...'); |
| 19 | |
| 20 | if (!config.quiet) spinner.start(); |
| 21 | |
| 22 | try { |
| 23 | while (Date.now() < deadline) { |
| 24 | const data = await requestJson<T>(config, { url: opts.url }); |
| 25 | |
| 26 | if (opts.getStatus && !config.quiet) { |
| 27 | spinner.update(`Status: ${opts.getStatus(data)}`); |
| 28 | } |
| 29 | |
| 30 | if (opts.isComplete(data)) { |
| 31 | spinner.stop('Done.'); |
| 32 | return data; |
| 33 | } |
| 34 | |
| 35 | if (opts.isFailed(data)) { |
| 36 | spinner.stop('Failed.'); |
| 37 | // Include API status context to help users diagnose failures |
| 38 | const status = opts.getStatus ? opts.getStatus(data) : 'failed'; |
| 39 | const extra = (data as Record<string, unknown>)?.base_resp |
| 40 | ? ` (${(data as { base_resp: { status_code?: number; status_msg?: string } }).base_resp.status_msg})` |
| 41 | : ''; |
| 42 | throw new CLIError( |
| 43 | `Task ${status}.${extra}`, |
| 44 | ExitCode.GENERAL, |
| 45 | 'Check the MiniMax dashboard or --verbose output for details.', |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | await new Promise(r => setTimeout(r, opts.intervalSec * 1000)); |
| 50 | } |
| 51 | } finally { |
| 52 | spinner.stop(); |
| 53 | } |
| 54 | |
| 55 | throw new CLIError( |
| 56 | 'Polling timed out.', |
| 57 | ExitCode.TIMEOUT, |
| 58 | 'Try increasing --timeout or check task status manually.', |
| 59 | ); |
| 60 | } |
no test coverage detected