# Calls helpers

> mv.calls.waitUntilEnded() in @morevoice/sdk: wait for a call to end, and for its summary, without writing a polling loop.

`mv.calls.waitUntilEnded()` waits until a call reaches a final status and returns it. With `waitForAnalysisMs` it also waits for the summary and the outcome, which arrive a few seconds after the call. It follows the call's event stream when the API offers one and polls `GET /v1/calls/{id}` otherwise; it throws a `WaitTimeoutError` after `timeoutMs`.

In production, prefer the [`call.ended` and `call.analyzed` webhooks](https://docs.morevoice.ai/webhooks/events/): waiting suits scripts, tests and test mode.

```ts title="test-call.ts"
// Place a test call and wait for it to end, then for its summary. A test key (mv_test_sk_…) never dials:
// +972500000001 answers on the virtual carrier and talks to your assistant.
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // reads MOREVOICE_API_KEY

const call = await mv.calls.create({ to: "+972500000001", assistant_id: process.env.ASSISTANT_ID!, purpose: "service" });
const ended = await mv.calls.waitUntilEnded(call.id, {
	timeoutMs: 5 * 60_000,
	waitForAnalysisMs: 30_000, // the summary and outcome arrive a few seconds after the call
	onUpdate: (c) => console.log(c.status),
});
console.log(ended.end_reason, ended.duration_ms, ended.summary?.outcome);
```

## API

Exported from `@morevoice/sdk` (source: `packages/sdk-ts/src/lib/calls.ts`).

### `CallLike`

*interface*

The call fields the helper reads. The facade returns the full generated `Call` type.

```ts
interface CallLike {
	id: string;
	status?: string | null;
	ended_at?: string | null;
	summary?: unknown;
	[field: string]: unknown;
}
```

### `FINAL_CALL_STATUSES`

*constant*

Statuses after which a call never changes again (besides its post-call analysis).

```ts
const FINAL_CALL_STATUSES: readonly string[];
```

### `isCallEnded()`

*function*

True when the call is over: a final status, or an `ended_at`.

```ts
function isCallEnded(call: CallLike): boolean;
```

### `WaitUntilEndedOptions`

*interface*

```ts
interface WaitUntilEndedOptions {
	/** Give up after this long (default 10 minutes), with a WaitTimeoutError. */
	timeoutMs?: number;
	/** Stops waiting; the promise rejects with the signal's reason. */
	signal?: AbortSignal;
	/** After the call ends, wait up to this long for its summary (`call.analyzed`). Default 0: resolve at once. */
	waitForAnalysisMs?: number;
	/** First polling interval (default 1000 ms); it grows ×1.5 per poll up to `maxPollIntervalMs`. */
	pollIntervalMs?: number;
	/** Longest polling interval (default 10 000 ms). */
	maxPollIntervalMs?: number;
	/** Called with every state the helper sees (each poll, and the final one). */
	onUpdate?: (call: CallLike) => void;
}
```

### `CallStreamEventLike`

*interface*

A stream event the helper reacts to (the C9 envelope; only `type` is read).

```ts
interface CallStreamEventLike {
	type?: string;
}
```

### `WaitDeps`

*interface*

What waitUntilEnded needs from the client.

```ts
interface WaitDeps<C extends CallLike> {
	/** GET /v1/calls/{id}. */
	retrieve(callId: string, options: { signal: AbortSignal }): Promise<C>;
	/** The call's event stream, when the API offers one; it should end after `call.ended`. */
	stream?: (callId: string, options: { signal: AbortSignal; types: string[] }) => AsyncIterable<CallStreamEventLike>;
}
```

### `DEFAULT_WAIT_TIMEOUT_MS`

*constant*

```ts
const DEFAULT_WAIT_TIMEOUT_MS = 600_000;
```

### `waitUntilEnded()`

*function*

Wait for a call to end. See the file header.

```ts
function waitUntilEnded<C extends CallLike>(deps: WaitDeps<C>, callId: string, options: WaitUntilEndedOptions = {}): Promise<C>;
```
