Class: Ollama::StreamEvent

Inherits:
Struct
  • Object
show all
Defined in:
lib/ollama/stream_event.rb

Overview

A typed event emitted during a streaming chat response. Provides clean separation between reasoning, answer tokens, tool calls, and terminal events — enabling UI rendering, JSONL tracing, and agent orchestration without mixing reasoning into the final answer.

Event types:

:thought_start   — reasoning block begins
:thought_delta   — incremental reasoning token
:thought_end     — reasoning block complete
:answer_delta    — incremental final-answer token
:tool_call_start — tool call ready (data = ToolCall hash)
:tool_call_delta — incremental tool call token (partial streaming)
:complete        — stream finished (data = Ollama::Response)
:error           — stream error (data = exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



18
19
20
# File 'lib/ollama/stream_event.rb', line 18

def data
  @data
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



18
19
20
# File 'lib/ollama/stream_event.rb', line 18

def model
  @model
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



18
19
20
# File 'lib/ollama/stream_event.rb', line 18

def type
  @type
end

Instance Method Details

#answer?Boolean

Returns:

  • (Boolean)


20
# File 'lib/ollama/stream_event.rb', line 20

def answer?    = type == :answer_delta

#terminal?Boolean

Returns:

  • (Boolean)


22
# File 'lib/ollama/stream_event.rb', line 22

def terminal?  = %i[complete error].include?(type)

#thought?Boolean

Returns:

  • (Boolean)


19
# File 'lib/ollama/stream_event.rb', line 19

def thought?   = %i[thought_start thought_delta thought_end].include?(type)

#to_jsonlObject

Serialize to a JSONL line for trace logging.



25
26
27
28
29
# File 'lib/ollama/stream_event.rb', line 25

def to_jsonl
  require "json"
  data_val = data.respond_to?(:to_h) ? data.to_h : data
  JSON.generate({ type: type, model: model, data: data_val })
end

#tool_call?Boolean

Returns:

  • (Boolean)


21
# File 'lib/ollama/stream_event.rb', line 21

def tool_call? = %i[tool_call_start tool_call_delta].include?(type)