Class: Legion::LLM::API::DebugFormats::CanonicalEvents
- Inherits:
-
Object
- Object
- Legion::LLM::API::DebugFormats::CanonicalEvents
- Defined in:
- lib/legion/llm/api/debug_formats.rb
Overview
Canonical streaming emitter — implements the StreamAssembler emitter contract but writes canonical chunk hashes (not client-format events). Same envelope across /v1/messages, /v1/responses, and /v1/chat/completions.
Instance Method Summary collapse
-
#initialize(out) ⇒ CanonicalEvents
constructor
A new instance of CanonicalEvents.
- #on_done(stop_reason:, usage:, model:) ⇒ Object
- #on_error(message:, type:, status_code:) ⇒ Object
- #on_keep_alive ⇒ Object
- #on_message_delta(stop_reason:, output_tokens:) ⇒ Object
- #on_server_tool_result(block_index:, tool_call_id:, result_text:) ⇒ Object
- #on_start(model:, request_id:, input_tokens:) ⇒ Object
- #on_text_close(block_index:) ⇒ Object
- #on_text_delta(block_index:, text:) ⇒ Object
- #on_text_open(block_index:) ⇒ Object
- #on_thinking_close(block_index:, signature:) ⇒ Object
- #on_thinking_delta(block_index:, text:, signature:) ⇒ Object
- #on_thinking_open(block_index:) ⇒ Object
- #on_tool_call_abort(block_index:, reason:) ⇒ Object
- #on_tool_call_close(block_index:) ⇒ Object
- #on_tool_call_delta(block_index:, partial_arguments_json:) ⇒ Object
- #on_tool_call_open(block_index:, tool_call:, server_tool:) ⇒ Object
Constructor Details
#initialize(out) ⇒ CanonicalEvents
Returns a new instance of CanonicalEvents.
148 149 150 151 |
# File 'lib/legion/llm/api/debug_formats.rb', line 148 def initialize(out) @out = out @done_emitted = false end |
Instance Method Details
#on_done(stop_reason:, usage:, model:) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/legion/llm/api/debug_formats.rb', line 237 def on_done(stop_reason:, usage:, model:) return if @done_emitted emit_event({ type: 'done', stop_reason: stop_reason, usage: usage, model: model.to_s }) @out << "data: [DONE]\n\n" @done_emitted = true end |
#on_error(message:, type:, status_code:) ⇒ Object
250 251 252 253 254 |
# File 'lib/legion/llm/api/debug_formats.rb', line 250 def on_error(message:, type:, status_code:) emit_event({ type: 'error', error: { message: , kind: type, status: status_code } }) @out << "data: [DONE]\n\n" unless @done_emitted @done_emitted = true end |
#on_keep_alive ⇒ Object
229 230 231 |
# File 'lib/legion/llm/api/debug_formats.rb', line 229 def on_keep_alive @out << ": keep-alive\n\n" end |
#on_message_delta(stop_reason:, output_tokens:) ⇒ Object
233 234 235 |
# File 'lib/legion/llm/api/debug_formats.rb', line 233 def (stop_reason:, output_tokens:) emit_event({ type: 'message_delta', stop_reason: stop_reason, output_tokens: output_tokens.to_i }) end |
#on_server_tool_result(block_index:, tool_call_id:, result_text:) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/legion/llm/api/debug_formats.rb', line 220 def on_server_tool_result(block_index:, tool_call_id:, result_text:) emit_event({ type: 'server_tool_result', block_index: block_index, tool_call_id: tool_call_id, result: result_text }) end |
#on_start(model:, request_id:, input_tokens:) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/legion/llm/api/debug_formats.rb', line 153 def on_start(model:, request_id:, input_tokens:) emit_event({ type: 'start', contract_version: DebugFormats::Canonical::CONTRACT_VERSION, request_id: request_id, model: model.to_s, input_tokens: input_tokens.to_i }) end |
#on_text_close(block_index:) ⇒ Object
171 172 173 |
# File 'lib/legion/llm/api/debug_formats.rb', line 171 def on_text_close(block_index:) emit_event({ type: 'text_close', block_index: block_index }) end |
#on_text_delta(block_index:, text:) ⇒ Object
167 168 169 |
# File 'lib/legion/llm/api/debug_formats.rb', line 167 def on_text_delta(block_index:, text:) emit_event({ type: 'text_delta', block_index: block_index, delta: text }) end |
#on_text_open(block_index:) ⇒ Object
163 164 165 |
# File 'lib/legion/llm/api/debug_formats.rb', line 163 def on_text_open(block_index:) emit_event({ type: 'text_open', block_index: block_index }) end |
#on_thinking_close(block_index:, signature:) ⇒ Object
185 186 187 188 189 |
# File 'lib/legion/llm/api/debug_formats.rb', line 185 def on_thinking_close(block_index:, signature:) payload = { type: 'thinking_close', block_index: block_index } payload[:signature] = signature unless signature.to_s.empty? emit_event(payload) end |
#on_thinking_delta(block_index:, text:, signature:) ⇒ Object
179 180 181 182 183 |
# File 'lib/legion/llm/api/debug_formats.rb', line 179 def on_thinking_delta(block_index:, text:, signature:) payload = { type: 'thinking_delta', block_index: block_index, delta: text } payload[:signature] = signature unless signature.to_s.empty? emit_event(payload) end |
#on_thinking_open(block_index:) ⇒ Object
175 176 177 |
# File 'lib/legion/llm/api/debug_formats.rb', line 175 def on_thinking_open(block_index:) emit_event({ type: 'thinking_open', block_index: block_index }) end |
#on_tool_call_abort(block_index:, reason:) ⇒ Object
216 217 218 |
# File 'lib/legion/llm/api/debug_formats.rb', line 216 def on_tool_call_abort(block_index:, reason:) emit_event({ type: 'tool_call_abort', block_index: block_index, reason: reason }) end |
#on_tool_call_close(block_index:) ⇒ Object
212 213 214 |
# File 'lib/legion/llm/api/debug_formats.rb', line 212 def on_tool_call_close(block_index:) emit_event({ type: 'tool_call_close', block_index: block_index }) end |
#on_tool_call_delta(block_index:, partial_arguments_json:) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/legion/llm/api/debug_formats.rb', line 204 def on_tool_call_delta(block_index:, partial_arguments_json:) emit_event({ type: 'tool_call_delta', block_index: block_index, partial_arguments_json: partial_arguments_json }) end |
#on_tool_call_open(block_index:, tool_call:, server_tool:) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/legion/llm/api/debug_formats.rb', line 191 def on_tool_call_open(block_index:, tool_call:, server_tool:) emit_event({ type: 'tool_call_open', block_index: block_index, tool_call: { id: tool_call[:id], name: tool_call[:name], arguments: tool_call[:arguments] || {} }, server_tool: server_tool }) end |