Class: Legion::LLM::API::ClientTranslators::AnthropicMessages::Events

Inherits:
Object
  • Object
show all
Includes:
Legion::Logging::Helper
Defined in:
lib/legion/llm/api/client_translators/anthropic_messages.rb

Overview

SSE emitter for Anthropic Messages API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out:, request_id:, model:, conv_id: nil) ⇒ Events

Returns a new instance of Events.



178
179
180
181
182
183
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 178

def initialize(out:, request_id:, model:, conv_id: nil)
  @out = out
  @request_id = request_id
  @model = model
  @conv_id = conv_id
end

Instance Attribute Details

#request_idObject (readonly)

Returns the value of attribute request_id.



176
177
178
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 176

def request_id
  @request_id
end

Instance Method Details

#on_done(stop_reason:, usage:, model:) ⇒ Object



314
315
316
317
318
319
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 314

def on_done(stop_reason:, usage:, model:)
  _ = stop_reason
  _ = usage
  _ = model
  emit('message_stop', { type: 'message_stop' })
end

#on_error(message:, type:, status_code:) ⇒ Object



321
322
323
324
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 321

def on_error(message:, type:, status_code:)
  _ = status_code
  emit('error', { type: 'error', error: { type: type, message: message } })
end

#on_keep_aliveObject



302
303
304
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 302

def on_keep_alive
  emit('ping', { type: 'ping' })
end

#on_message_delta(stop_reason:, output_tokens:) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 306

def on_message_delta(stop_reason:, output_tokens:)
  emit('message_delta', {
         type:  'message_delta',
         delta: { stop_reason: STOP_REASON_MAP[stop_reason] || 'end_turn', stop_sequence: nil },
         usage: { output_tokens: output_tokens.to_i }
       })
end

#on_server_tool_result(block_index:, tool_call_id:, result_text:) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 288

def on_server_tool_result(block_index:, tool_call_id:, result_text:)
  emit('content_block_start', {
         type:          'content_block_start',
         index:         block_index,
         content_block: { type: 'server_tool_result', id: tool_call_id, content: [] }
       })
  emit('content_block_delta', {
         type:  'content_block_delta',
         index: block_index,
         delta: { type: 'content_block_delta', content: [{ type: 'text', text: result_text }] }
       })
  emit('content_block_stop', { type: 'content_block_stop', index: block_index })
end

#on_start(model:, request_id:, input_tokens:) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 185

def on_start(model:, request_id:, input_tokens:)
  emit('message_start', {
         type:    'message_start',
         message: {
           id:            request_id,
           type:          'message',
           role:          'assistant',
           content:       [],
           model:         model.to_s,
           stop_reason:   nil,
           stop_sequence: nil,
           usage:         { input_tokens: input_tokens.to_i, output_tokens: 0 }
         }
       })
end

#on_text_close(block_index:) ⇒ Object



218
219
220
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 218

def on_text_close(block_index:)
  emit('content_block_stop', { type: 'content_block_stop', index: block_index })
end

#on_text_delta(block_index:, text:) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 210

def on_text_delta(block_index:, text:)
  emit('content_block_delta', {
         type:  'content_block_delta',
         index: block_index,
         delta: { type: 'text_delta', text: text }
       })
end

#on_text_open(block_index:) ⇒ Object



201
202
203
204
205
206
207
208
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 201

def on_text_open(block_index:)
  emit('content_block_start', {
         type:          'content_block_start',
         index:         block_index,
         content_block: { type: 'text', text: '' }
       })
  emit('ping', { type: 'ping' })
end

#on_thinking_close(block_index:, signature:) ⇒ Object



247
248
249
250
251
252
253
254
255
256
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 247

def on_thinking_close(block_index:, signature:)
  unless signature.to_s.empty?
    emit('content_block_delta', {
           type:  'content_block_delta',
           index: block_index,
           delta: { type: 'signature_delta', signature: signature }
         })
  end
  emit('content_block_stop', { type: 'content_block_stop', index: block_index })
end

#on_thinking_delta(block_index:, text:, signature:) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 230

def on_thinking_delta(block_index:, text:, signature:)
  unless text.to_s.empty?
    emit('content_block_delta', {
           type:  'content_block_delta',
           index: block_index,
           delta: { type: 'thinking_delta', thinking: text }
         })
  end
  return if signature.to_s.empty?

  emit('content_block_delta', {
         type:  'content_block_delta',
         index: block_index,
         delta: { type: 'signature_delta', signature: signature }
       })
end

#on_thinking_open(block_index:) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 222

def on_thinking_open(block_index:)
  emit('content_block_start', {
         type:          'content_block_start',
         index:         block_index,
         content_block: { type: 'thinking', thinking: '', signature: '' }
       })
end

#on_tool_call_abort(block_index:, reason:) ⇒ Object



283
284
285
286
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 283

def on_tool_call_abort(block_index:, reason:)
  # No client-visible abort primitive in the Anthropic protocol.
  # Partial tool call was buffered and never emitted, or discarded.
end

#on_tool_call_close(block_index:) ⇒ Object



279
280
281
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 279

def on_tool_call_close(block_index:)
  emit('content_block_stop', { type: 'content_block_stop', index: block_index })
end

#on_tool_call_delta(block_index:, partial_arguments_json:) ⇒ Object



271
272
273
274
275
276
277
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 271

def on_tool_call_delta(block_index:, partial_arguments_json:)
  emit('content_block_delta', {
         type:  'content_block_delta',
         index: block_index,
         delta: { type: 'input_json_delta', partial_json: partial_arguments_json }
       })
end

#on_tool_call_open(block_index:, tool_call:, server_tool:) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 258

def on_tool_call_open(block_index:, tool_call:, server_tool:)
  emit('content_block_start', {
         type:          'content_block_start',
         index:         block_index,
         content_block: {
           type:  server_tool ? 'server_tool_use' : 'tool_use',
           id:    tool_call[:id] || (server_tool ? "srvtoolu_#{SecureRandom.hex(12)}" : "toolu_#{SecureRandom.hex(12)}"),
           name:  tool_call[:name],
           input: tool_call[:arguments] || {}
         }
       })
end