Skip to content

Rulvar API reference


Rulvar API reference / @rulvar/anthropic / mapAnthropicStream

Function: mapAnthropicStream()

ts
function mapAnthropicStream(
   stream, 
   ids, 
options?): AsyncGenerator<ChatEvent, TurnMapping>;

Defined in: packages/anthropic/src/wire.ts:474

Maps one Messages API stream into ChatEvents, yielding each canonical event AS the corresponding provider event is consumed: the consumer's pull drives the provider read (natural backpressure, no buffering, no detached work). The generator's RETURN value carries the accumulated turn state the adapter needs for pause_turn continuation. Yields an early usage event from message_start (the input side is known immediately) and exactly one terminal finish when the stream reaches message_stop. A stream that pauses (pause_turn) or ends before message_stop yields NO terminal event of its own: the return value's pauseTurn and finished flags report which case happened, and the anthropic() adapter turns a truncated read (finished false without a pause) into the retryable transport error the contract requires, so a direct mapper consumer must check the flags rather than wait for an error event. carryRetained holds thinking blocks from earlier pause_turn continuations of the same turn so the terminal finish ships the whole turn's retention payload (M4-T02).

Parameters

ParameterTypeDescription
streamAsyncIterable&lt;AnthropicStreamEvent&gt;-
idsIdMap-
options?{ carryRetained?: Block[]; usagePrior?: Usage; wirePrior?: { responseIds: (string | undefined)[]; }; }-
options.carryRetained?Block[]-
options.usagePrior?UsageAccumulated usage of the PRIOR pause_turn segments of this turn (RV1003): the terminal finish must speak for the WHOLE logical turn, because core sums the per-segment mid-stream reports and verifies them against the finish total; a finish carrying only the last segment's counts turns a legitimate absorption into a usage-invariant kill and loses the paid segments from the money. Absent on the first segment, so an unsegmented finish stays byte-identical.
options.wirePrior?{ responseIds: (string | undefined)[]; }Response ids of the PRIOR pause_turn segments of this turn (RV905): when present, the finish metadata names the whole wire request set (wireRequests: { count, responseIds }) so the core can account the dispatch at its true wire count. Absent on the first segment, so an unsegmented finish stays byte-identical.
options.wirePrior.responseIds?(string | undefined)[]-

Returns

AsyncGenerator&lt;ChatEvent, TurnMapping&gt;