glean/stream

Types

pub type StreamEvent {
  TextStart(id: String)
  TextDelta(id: String, delta: String)
  TextEnd(id: String)
  ReasoningStart(id: String)
  ReasoningDelta(id: String, delta: String)
  ReasoningEnd(id: String)
  ToolCallStart(
    id: String,
    tool_call_id: String,
    tool_name: String,
  )
  ToolCallDelta(id: String, tool_call_id: String, delta: String)
  ToolCallEnd(id: String, tool_call_id: String)
  ToolResultEvent(
    tool_call_id: String,
    tool_name: String,
    output: String,
  )
  ToolErrorEvent(
    tool_call_id: String,
    tool_name: String,
    error: String,
  )
  StepStart(step: Int)
  StepEnd(step: Int)
  Finish(finish_reason: error.FinishReason, usage: usage.Usage)
  StreamError(message: String)
}

Constructors

  • TextStart(id: String)
  • TextDelta(id: String, delta: String)
  • TextEnd(id: String)
  • ReasoningStart(id: String)
  • ReasoningDelta(id: String, delta: String)
  • ReasoningEnd(id: String)
  • ToolCallStart(
      id: String,
      tool_call_id: String,
      tool_name: String,
    )
  • ToolCallDelta(id: String, tool_call_id: String, delta: String)
  • ToolCallEnd(id: String, tool_call_id: String)
  • ToolResultEvent(
      tool_call_id: String,
      tool_name: String,
      output: String,
    )
  • ToolErrorEvent(
      tool_call_id: String,
      tool_name: String,
      error: String,
    )
  • StepStart(step: Int)
  • StepEnd(step: Int)
  • Finish(finish_reason: error.FinishReason, usage: usage.Usage)

    The run finished. Sent once, after the last step.

  • StreamError(message: String)

    The run failed: the provider returned an error, or the final answer did not pass validation. Sent once, just before run.stream returns the error; message is error.to_string of it.

Token usage. Defined in glean/usage, which has its constructor.

pub type Usage =
  usage.Usage

Values

pub fn add_usage(a: usage.Usage, b: usage.Usage) -> usage.Usage

Add usage from two Usage values together.

pub fn collect_text(events: List(StreamEvent)) -> String

Accumulate all text deltas from a list of stream events into a single string.

pub fn collect_tool_calls(
  events: List(StreamEvent),
) -> List(#(String, String, String))

Extract all completed tool calls from stream events. Returns a list of (tool_call_id, tool_name, arguments_json) tuples, in the order the calls ended. Deltas are matched to their call by tool_call_id, so calls that stream at the same time don’t mix. A call that never got its ToolCallEnd is left out.

pub fn zero_usage() -> usage.Usage

Zero usage (useful as initial accumulator).

✨ Search Document