MCPcopy
hub / github.com/MiniMax-AI/cli / poll

Function poll

src/polling/poll.ts:16–60  ·  view source on GitHub ↗
(config: Config, opts: PollOptions)

Source from the content-addressed store, hash-verified

14}
15
16export 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}

Callers 1

poll.test.tsFile · 0.85

Calls 4

createSpinnerFunction · 0.90
startMethod · 0.65
updateMethod · 0.65
stopMethod · 0.65

Tested by

no test coverage detected