Class: Legion::Extensions::Llm::Canonical::ContentBlock

Inherits:
Data
  • Object
show all
Defined in:
lib/legion/extensions/llm/canonical/content_block.rb

Overview

Typed content block with media_type support per G20a. Ports field vocabulary from Legion::LLM::Types::ContentBlock. rubocop:disable Lint/ConstantDefinitionInBlock – required for Data.define block scope

Constant Summary collapse

TEXT_TYPE_ALIASES =
%i[text output_text input_text].freeze
CONTENT_BLOCK_TYPES =
%i[text thinking tool_use tool_result image audio video].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_controlObject (readonly)

Returns the value of attribute cache_control

Returns:

  • (Object)

    the current value of cache_control



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def cache_control
  @cache_control
end

#codeObject (readonly)

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def data
  @data
end

#detailObject (readonly)

Returns the value of attribute detail

Returns:

  • (Object)

    the current value of detail



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def detail
  @detail
end

#end_indexObject (readonly)

Returns the value of attribute end_index

Returns:

  • (Object)

    the current value of end_index



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def end_index
  @end_index
end

#file_idObject (readonly)

Returns the value of attribute file_id

Returns:

  • (Object)

    the current value of file_id



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def file_id
  @file_id
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def id
  @id
end

#inputObject (readonly)

Returns the value of attribute input

Returns:

  • (Object)

    the current value of input



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def input
  @input
end

#is_errorObject (readonly)

Returns the value of attribute is_error

Returns:

  • (Object)

    the current value of is_error



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def is_error
  @is_error
end

#media_typeObject (readonly)

Returns the value of attribute media_type

Returns:

  • (Object)

    the current value of media_type



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def media_type
  @media_type
end

#messageObject (readonly)

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def source
  @source
end

#source_typeObject (readonly)

Returns the value of attribute source_type

Returns:

  • (Object)

    the current value of source_type



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def source_type
  @source_type
end

#start_indexObject (readonly)

Returns the value of attribute start_index

Returns:

  • (Object)

    the current value of start_index



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def start_index
  @start_index
end

#textObject (readonly)

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def text
  @text
end

#tool_use_idObject (readonly)

Returns the value of attribute tool_use_id

Returns:

  • (Object)

    the current value of tool_use_id



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def tool_use_id
  @tool_use_id
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



10
11
12
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10

def type
  @type
end

Class Method Details

.from_hash(source) ⇒ Object

Build from a Hash (raw provider response or deserialized wire payload). Rescues NoMethodError from corrupted inputs (e.g. String elements from prior serialization bugs where ContentBlock#inspect leaked into storage).



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 72

def self.from_hash(source)
  return nil if source.nil?

  h = source.transform_keys(&:to_sym)
  type_raw = h.delete(:type)
  if type_raw
    type_sym = type_raw.to_sym
    h[:type] = TEXT_TYPE_ALIASES.include?(type_sym) ? :text : type_sym
  end

  new(
    type: h[:type],
    text: h[:text],
    data: h[:data],
    source_type: h[:source_type],
    media_type: h[:media_type],
    detail: h[:detail],
    name: h[:name],
    file_id: h[:file_id],
    id: h[:id],
    input: h[:input],
    tool_use_id: h[:tool_use_id],
    is_error: h[:is_error],
    source: h[:source],
    start_index: h[:start_index],
    end_index: h[:end_index],
    code: h[:code],
    message: h[:message],
    cache_control: h[:cache_control]
  )
rescue NoMethodError => e
  Legion::Logging.log.warn('[canonical][content_block] from_hash received non-Hash input ' \
                           "(#{source.class}): #{e.message}")
  text(source.to_s)
end

.image(data:, media_type:, source_type: :base64, detail: nil) ⇒ Object

Build an image content block with media_type (G20a).



60
61
62
63
64
65
66
67
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 60

def self.image(data:, media_type:, source_type: :base64, detail: nil)
  new(
    type: :image, text: nil, data: data, source_type: source_type, media_type: media_type,
    detail: detail, name: nil, file_id: nil, id: nil, input: nil,
    tool_use_id: nil, is_error: nil, source: nil, start_index: nil,
    end_index: nil, code: nil, message: nil, cache_control: nil
  )
end

.text(content, cache_control: nil) ⇒ Object

Build a text content block.



20
21
22
23
24
25
26
27
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 20

def self.text(content, cache_control: nil)
  new(
    type: :text, text: content, data: nil, source_type: nil, media_type: nil,
    detail: nil, name: nil, file_id: nil, id: nil, input: nil,
    tool_use_id: nil, is_error: nil, source: nil, start_index: nil,
    end_index: nil, code: nil, message: nil, cache_control: cache_control
  )
end

.thinking(content) ⇒ Object

Build a thinking content block.



30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 30

def self.thinking(content)
  new(
    type: :thinking, text: content, data: nil, source_type: nil, media_type: nil,
    detail: nil, name: nil, file_id: nil, id: nil, input: nil,
    tool_use_id: nil, is_error: nil, source: nil, start_index: nil,
    end_index: nil, code: nil, message: nil, cache_control: nil
  )
end

.tool_result(tool_use_id:, content:, is_error: false) ⇒ Object

Build a tool_result content block.



50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 50

def self.tool_result(tool_use_id:, content:, is_error: false)
  new(
    type: :tool_result, text: content, data: nil, source_type: nil, media_type: nil,
    detail: nil, name: nil, file_id: nil, id: nil, input: nil,
    tool_use_id: tool_use_id, is_error: is_error, source: nil, start_index: nil,
    end_index: nil, code: nil, message: nil, cache_control: nil
  )
end

.tool_use(id:, name:, input:) ⇒ Object

Build a tool_use content block.



40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 40

def self.tool_use(id:, name:, input:)
  new(
    type: :tool_use, text: nil, data: nil, source_type: nil, media_type: nil,
    detail: nil, name: name, file_id: nil, id: id, input: input,
    tool_use_id: nil, is_error: nil, source: nil, start_index: nil,
    end_index: nil, code: nil, message: nil, cache_control: nil
  )
end

Instance Method Details

#inspectObject

Concise inspect — prevents raw Data.define dump in Array#inspect output.



122
123
124
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 122

def inspect
  "#<ContentBlock:#{type} #{to_s.slice(0, 80).inspect}>"
end

#text?Boolean

Whether this block carries textual content.

Returns:

  • (Boolean)


127
128
129
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 127

def text?
  TEXT_TYPE_ALIASES.include?(type)
end

#thinking?Boolean

Whether this block carries thinking/reasoning content.

Returns:

  • (Boolean)


132
133
134
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 132

def thinking?
  type == :thinking
end

#to_hObject

Serialize to a Hash for AMQP/fleet/wire transport.



109
110
111
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 109

def to_h
  super.compact
end

#to_sObject

Human-readable string — prevents #inspect leaking into user-facing output.



114
115
116
117
118
119
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 114

def to_s
  return "[tool_use:#{name}]" if type == :tool_use
  return '[image]' if type == :image

  text.to_s
end

#tool_result?Boolean

Whether this block represents a tool result.

Returns:

  • (Boolean)


142
143
144
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 142

def tool_result?
  type == :tool_result
end

#tool_use?Boolean

Whether this block represents a tool use request.

Returns:

  • (Boolean)


137
138
139
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 137

def tool_use?
  type == :tool_use
end