Claudex.Stream.Accumulator (Claudex v0.6.1)

Copy Markdown View Source

Folds the Claudex.Stream.Event structs a stream yields back into the Claudex.Message they describe — the same struct Claudex.Messages.create/2 would have returned.

client
|> Claudex.Messages.stream!(params)
|> Enum.reduce(Accumulator.new(), &Accumulator.add(&2, &1))
|> Accumulator.message()

message/1 works at any point, so you can take a snapshot mid-stream. A tool call's input is only filled in once its content_block_stop arrives, because the API sends the arguments as JSON fragments that are only parseable together.

Summary

Functions

Folds one event in.

Returns the message built so far, or nil if the stream hasn't started one yet.

Builds an empty accumulator.

Types

t()

@type t() :: %Claudex.Stream.Accumulator{
  blocks: %{required(non_neg_integer()) => Claudex.ContentBlock.t()},
  message: Claudex.Message.t() | nil,
  tool_input: %{required(non_neg_integer()) => String.t()}
}

Functions

add(accumulator, event)

@spec add(t(), Claudex.Stream.Event.t()) :: t()

Folds one event in.

Events that arrive before message_start, or that refer to a block index that never started, are ignored — a stream that never starts a message is reported by Claudex.Stream.final_message/1 instead.

message(accumulator)

@spec message(t()) :: Claudex.Message.t() | nil

Returns the message built so far, or nil if the stream hasn't started one yet.

new()

@spec new() :: t()

Builds an empty accumulator.