Claudex.Stream (Claudex v0.6.1)

Copy Markdown View Source

Helpers for a stream of events from Claudex.Messages.stream!/2 or Claudex.Messages.stream_to/3.

Use final_message/1 or text/1 when you want the finished reply and the events were only a means to get there:

client
|> Claudex.Messages.stream!(params)
|> Claudex.Stream.text()

For the event-by-event view, enumerate the stream yourself and match on the structs in Claudex.Stream.Event.

Summary

Functions

Stops a stream started with Claudex.Messages.stream_to/3.

Consumes a stream and returns the message it describes.

Consumes a stream and returns just the reply text.

Functions

cancel(handle)

@spec cancel(Claudex.Stream.Handle.t()) :: :ok

Stops a stream started with Claudex.Messages.stream_to/3.

The stream stops at its next event and sends {:claudex, ref, :cancelled}, so a stalled stream keeps waiting until the next byte arrives or the request times out.

final_message(events)

@spec final_message(Enumerable.t()) ::
  {:ok, Claudex.Message.t()} | {:error, Claudex.Error.t()}

Consumes a stream and returns the message it describes.

Returns {:error, %Claudex.Error{}} if the request fails, if the API sends an error part-way through, or if the stream ends without ever starting a message.

text(events)

@spec text(Enumerable.t()) :: {:ok, String.t()} | {:error, Claudex.Error.t()}

Consumes a stream and returns just the reply text.