data/api/streaming library

Classes

CacheConfig
Configuration for Anthropic prompt caching.
ContentBlockAccumulator
Content block accumulator — assembles deltas into a complete block.
ImageAccumulator
Holds image data received in a content block.
MessageComplete
The message is complete.
MessageStartUpdate
Message started — carries model and message id metadata.
RateLimiter
Token-bucket rate limiter for API calls.
SseEvent
A single parsed SSE field set (one event).
SseParser
Server-Sent Events parser for Anthropic streaming API.
StreamAssembler
Stream assembler — processes raw SSE events into StreamUpdates and maintains a StreamingState that accumulates the full message.
StreamError
A stream-level error.
StreamingState
Streaming state for a single API call — tracks all content blocks and emits StreamUpdates as deltas arrive.
StreamUpdate
Stream update events emitted to UI.
TextAccumulator
Accumulates text deltas into a complete TextBlock.
TextDelta
A text delta for an assistant text block.
ThinkingAccumulator
Accumulates thinking/reasoning text.
ThinkingDelta
A thinking/reasoning delta.
ToolUseAccumulator
Accumulates tool_use JSON input from partial_json deltas.
ToolUseComplete
Signals that a tool_use block is complete with fully parsed input.
ToolUseInputDelta
Partial JSON input delta for a tool_use block.
ToolUseStart
Signals the start of a tool_use block.
UsageUpdate
Token usage update.

Enums

StreamEventType
SSE event types from Anthropic API.

Functions

applyCacheBreakpoints(List<Map<String, dynamic>> messages, CacheConfig config) List<Map<String, dynamic>>
Apply cache breakpoints to a list of message maps destined for the API.
applyCacheToSystem(List<Map<String, dynamic>> systemBlocks) List<Map<String, dynamic>>
Apply cache control to a system prompt (list of system content blocks).
collectStreamToMessage(Stream<StreamUpdate> stream, {void onUpdate(StreamUpdate update)?}) Future<Message>
Consume a Stream<StreamUpdate> and assemble the final Message.
convertProviderStream(Stream<StreamEvent> providerStream) Stream<StreamUpdate>
Convert the existing StreamEvent (from api_provider.dart) sequence into the higher-level StreamUpdate sequence used by this module.
encodeImageContent(List<int> bytes, String mediaType) Map<String, dynamic>
Encode raw image bytes for Anthropic API submission.
encodePdfContent(List<int> bytes) Map<String, dynamic>
Encode raw PDF bytes for Anthropic API submission.
isNonRetryableStatus(int statusCode) bool
Determine if an HTTP status code should NOT be retried.
isRetryableStatus(int statusCode) bool
Determine if an HTTP status code is retryable.
mediaTypeFromBytes(List<int> bytes) String?
Detect image media type from magic bytes.
mediaTypeFromExtension(String extension) String?
Detect image media type from file extension.
parseStreamEventType(String raw) StreamEventType?
Map raw SSE event type strings to StreamEventType.
processStream(Stream<List<int>> byteStream) Stream<StreamUpdate>
Convenience: process a byte stream and yield StreamUpdates directly.
processStreamWithAssembler(Stream<List<int>> byteStream) → (Stream<StreamUpdate>, StreamAssembler)
Process an HTTP streaming response byte stream into StreamUpdates.
withStreamRetry<T>(Future<T> fn(), {int maxRetries = 3, Duration initialDelay = const Duration(seconds: 1), Duration maxDelay = const Duration(seconds: 30), bool shouldRetry(Object error)?}) Future<T>
Execute an async operation with exponential backoff and jitter.