Claudex.Stream.Event (Claudex v0.6.1)

Copy Markdown View Source

The events the Messages API sends while it streams a reply.

A stream always runs message_start, then one content_block_start / content_block_delta... / content_block_stop group per block, then message_delta and message_stop. Match on the struct to handle the one you care about:

%Event.ContentBlockDelta{delta: {:text, chunk}} -> IO.write(chunk)

from_sse/1 turns a raw Claudex.Stream.SSE.Event into one of these. Keep-alive ping events are dropped, and an error event the API sends mid-stream comes back as a Claudex.Error — the reply ends there.

Summary

Functions

Builds the event struct for one decoded event payload.

Turns one raw SSE event into a typed event.

Types

Functions

decode(json)

@spec decode(map()) :: t()

Builds the event struct for one decoded event payload.

Falls back to Claudex.Stream.Event.Unknown for a type not modeled yet.

from_sse(event)

@spec from_sse(Claudex.Stream.SSE.Event.t()) ::
  {:ok, t()} | :ignore | {:error, Claudex.Error.t()}

Turns one raw SSE event into a typed event.

Returns :ignore for a ping, and {:error, %Claudex.Error{}} both for an error event from the API and for data that isn't a JSON object.