Skip to content
providerkit
GitHub

collectTurn

function collectTurn(stream): Promise<Turn>;

Defined in: core/src/turn.ts:57

Drain one turn into the assistant message it produced, plus its usage, finish reason and source.

const turn = await collectTurn(provider.createStream(history, tools));
history.push(turn.message);
for (const call of turn.message.toolCalls ?? []) history.push(await run(call));

Hosted calls (a vendor’s search, fetch or code run) land in hostedCalls, never in toolCalls: there is nothing for the loop to run.

Parameter Type
stream AsyncIterable<ProviderChunk>

Promise<Turn>

overload when a tool call ends the stream with no name, or a hosted call with no type. Whatever the stream itself throws passes through unchanged.