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.



181
182
183
184
185
186
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 181

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.



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

def request_id
  @request_id
end

Instance Method Details

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



317
318
319
320
321
322
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 317

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



324
325
326
327
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 324

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

#on_keep_aliveObject



305
306
307
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 305

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

#on_message_delta(stop_reason:, output_tokens:) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 309

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



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

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



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

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



221
222
223
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 221

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



213
214
215
216
217
218
219
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 213

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



204
205
206
207
208
209
210
211
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 204

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



250
251
252
253
254
255
256
257
258
259
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 250

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



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

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



225
226
227
228
229
230
231
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 225

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



286
287
288
289
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 286

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



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

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



274
275
276
277
278
279
280
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 274

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



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/legion/llm/api/client_translators/anthropic_messages.rb', line 261

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