Class: OmniAI::Anthropic::Chat::Stream::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/anthropic/chat/stream.rb

Overview

Process the stream into chunks by event.

Instance Method Summary collapse

Instance Method Details

#content_block_delta(data) ⇒ Object

Handler for Type::CONTENT_BLOCK_DELTA

Parameters:

  • data (Hash)


60
61
62
# File 'lib/omniai/anthropic/chat/stream.rb', line 60

def content_block_delta(data)
  @content = [{ 'type' => 'text', 'text' => data['delta']['text'] }]
end

#content_block_start(_data) ⇒ Object

Handler for Type::CONTENT_BLOCK_START

Parameters:

  • data (Hash)


46
47
48
# File 'lib/omniai/anthropic/chat/stream.rb', line 46

def content_block_start(_data)
  @content = nil
end

#content_block_stop(_data) ⇒ Object

Handler for Type::CONTENT_BLOCK_STOP

Parameters:

  • _data (Hash)


53
54
55
# File 'lib/omniai/anthropic/chat/stream.rb', line 53

def content_block_stop(_data)
  @content = nil
end

#message_start(data) ⇒ Object

Handler for Type::MESSAGE_START

Parameters:

  • data (Hash)


32
33
34
# File 'lib/omniai/anthropic/chat/stream.rb', line 32

def message_start(data)
  @message = data['message']
end

#message_stop(_data) ⇒ Object

Handler for Type::MESSAGE_STOP

Parameters:

  • _data (Hash)


39
40
41
# File 'lib/omniai/anthropic/chat/stream.rb', line 39

def message_stop(_data)
  @message = nil
end

#payload(context:) ⇒ OmniAI::Chat::Payload?

Returns:

  • (OmniAI::Chat::Payload, nil)


21
22
23
24
25
26
27
# File 'lib/omniai/anthropic/chat/stream.rb', line 21

def payload(context:)
  return unless @content

  OmniAI::Chat::Payload.deserialize(@message.merge({
    'content' => @content,
  }), context:)
end