Class: Legion::Extensions::Llm::Canonical::ContentBlock
- Inherits:
-
Data
- Object
- Data
- Legion::Extensions::Llm::Canonical::ContentBlock
- 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. # -- required for Data.define block scope
Constant Summary collapse
- CONTENT_BLOCK_TYPES =
%i[text thinking tool_use tool_result image audio video].freeze
- BUILD_SITE =
'Canonical::ContentBlock.build'- FROM_HASH_SITE =
'Canonical::ContentBlock.from_hash'- NEW_SITE =
'Canonical::ContentBlock.new'
Instance Attribute Summary collapse
-
#cache_control ⇒ Object
readonly
Returns the value of attribute cache_control.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#detail ⇒ Object
readonly
Returns the value of attribute detail.
-
#end_index ⇒ Object
readonly
Returns the value of attribute end_index.
-
#file_id ⇒ Object
readonly
Returns the value of attribute file_id.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#is_error ⇒ Object
readonly
Returns the value of attribute is_error.
-
#media_type ⇒ Object
readonly
Returns the value of attribute media_type.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#source_type ⇒ Object
readonly
Returns the value of attribute source_type.
-
#start_index ⇒ Object
readonly
Returns the value of attribute start_index.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#tool_use_id ⇒ Object
readonly
Returns the value of attribute tool_use_id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.build(type: nil, text: nil, 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, metadata: {}) ⇒ Object
Build from keyword args (primary constructor).
-
.from_hash(source) ⇒ Object
Build from a Hash (raw provider response or deserialized wire payload).
-
.image(data:, media_type:, source_type: :base64, detail: nil) ⇒ Object
Build an image content block with media_type (G20a).
-
.normalize_type!(type, site) ⇒ Object
L6: validate against the declared block types when present.
-
.text(content, cache_control: nil) ⇒ Object
Build a text content block.
-
.thinking(content) ⇒ Object
Build a thinking content block.
-
.tool_result(tool_use_id:, content:, is_error: false) ⇒ Object
Build a tool_result content block.
-
.tool_use(id:, name:, input:) ⇒ Object
Build a tool_use content block.
Instance Method Summary collapse
-
#as_json ⇒ Object
MultiJson/Oj/::JSON callback — prevents Data.define #inspect leak into JSON.
-
#inspect ⇒ Object
Concise inspect — prevents raw Data.define dump in Array#inspect output.
-
#text? ⇒ Boolean
Whether this block carries textual content.
-
#thinking? ⇒ Boolean
Whether this block carries thinking/reasoning content.
-
#to_h ⇒ Object
Serialize to a Hash for AMQP/fleet/wire transport.
- #to_json ⇒ Object
-
#to_s ⇒ Object
Human-readable string — prevents #inspect leaking into user-facing output.
-
#tool_result? ⇒ Boolean
Whether this block represents a tool result.
-
#tool_use? ⇒ Boolean
Whether this block represents a tool use request.
Instance Attribute Details
#cache_control ⇒ Object (readonly)
Returns the value of attribute cache_control
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def cache_control @cache_control end |
#code ⇒ Object (readonly)
Returns the value of attribute code
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def data @data end |
#detail ⇒ Object (readonly)
Returns the value of attribute detail
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def detail @detail end |
#end_index ⇒ Object (readonly)
Returns the value of attribute end_index
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def end_index @end_index end |
#file_id ⇒ Object (readonly)
Returns the value of attribute file_id
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def file_id @file_id end |
#id ⇒ Object (readonly)
Returns the value of attribute id
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def id @id end |
#input ⇒ Object (readonly)
Returns the value of attribute input
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def input @input end |
#is_error ⇒ Object (readonly)
Returns the value of attribute is_error
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def is_error @is_error end |
#media_type ⇒ Object (readonly)
Returns the value of attribute media_type
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def media_type @media_type end |
#message ⇒ Object (readonly)
Returns the value of attribute message
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def @message end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def @metadata end |
#name ⇒ Object (readonly)
Returns the value of attribute name
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def name @name end |
#source ⇒ Object (readonly)
Returns the value of attribute source
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def source @source end |
#source_type ⇒ Object (readonly)
Returns the value of attribute source_type
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def source_type @source_type end |
#start_index ⇒ Object (readonly)
Returns the value of attribute start_index
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def start_index @start_index end |
#text ⇒ Object (readonly)
Returns the value of attribute text
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def text @text end |
#tool_use_id ⇒ Object (readonly)
Returns the value of attribute 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 |
#type ⇒ Object (readonly)
Returns the value of attribute type
10 11 12 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 10 def type @type end |
Class Method Details
.build(type: nil, text: nil, 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, metadata: {}) ⇒ Object
Build from keyword args (primary constructor).
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 18 def self.build( type: nil, text: nil, 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, metadata: {} ) new( type: normalize_type!(type, self::BUILD_SITE), text:, data:, source_type:, media_type:, detail:, name:, file_id:, id:, input:, tool_use_id:, is_error:, source:, start_index:, end_index:, code:, message:, cache_control:, metadata: Strict.(, self::BUILD_SITE) ) end |
.from_hash(source) ⇒ Object
Build from a Hash (raw provider response or deserialized wire payload). Canonical keys only (O03a); unknown keys fold into metadata (L5).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 38 def self.from_hash(source) Strict.require_hash!(source, self::FROM_HASH_SITE) hash = Strict.symbolize_keys(source) = Strict.fold_unknowns!(self, self::FROM_HASH_SITE, hash) build( type: hash[:type], text: hash[:text], data: hash[:data], source_type: hash[:source_type], media_type: hash[:media_type], detail: hash[:detail], name: hash[:name], file_id: hash[:file_id], id: hash[:id], input: hash[:input], tool_use_id: hash[:tool_use_id], is_error: hash[:is_error], source: hash[:source], start_index: hash[:start_index], end_index: hash[:end_index], code: hash[:code], message: hash[:message], cache_control: hash[:cache_control], metadata: ) end |
.image(data:, media_type:, source_type: :base64, detail: nil) ⇒ Object
Build an image content block with media_type (G20a).
83 84 85 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 83 def self.image(data:, media_type:, source_type: :base64, detail: nil) build(type: :image, data:, media_type:, source_type:, detail:) end |
.normalize_type!(type, site) ⇒ Object
L6: validate against the declared block types when present.
55 56 57 58 59 60 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 55 def self.normalize_type!(type, site) return nil if type.nil? type_sym = type.is_a?(::String) ? type.to_sym : type Strict.enum!(type_sym, self::CONTENT_BLOCK_TYPES, site, :type) end |
.text(content, cache_control: nil) ⇒ Object
Build a text content block.
63 64 65 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 63 def self.text(content, cache_control: nil) build(type: :text, text: content, cache_control:) end |
.thinking(content) ⇒ Object
Build a thinking content block.
68 69 70 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 68 def self.thinking(content) build(type: :thinking, text: content) end |
.tool_result(tool_use_id:, content:, is_error: false) ⇒ Object
Build a tool_result content block.
78 79 80 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 78 def self.tool_result(tool_use_id:, content:, is_error: false) build(type: :tool_result, text: content, tool_use_id:, is_error:) end |
.tool_use(id:, name:, input:) ⇒ Object
Build a tool_use content block.
73 74 75 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 73 def self.tool_use(id:, name:, input:) build(type: :tool_use, id:, name:, input:) end |
Instance Method Details
#as_json ⇒ Object
MultiJson/Oj/::JSON callback — prevents Data.define #inspect leak into JSON.
93 94 95 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 93 def as_json(*) to_h end |
#inspect ⇒ Object
Concise inspect — prevents raw Data.define dump in Array#inspect output.
110 111 112 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 110 def inspect "#<ContentBlock:#{type} #{to_s.slice(0, 80).inspect}>" end |
#text? ⇒ Boolean
Whether this block carries textual content.
115 116 117 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 115 def text? type == :text end |
#thinking? ⇒ Boolean
Whether this block carries thinking/reasoning content.
120 121 122 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 120 def thinking? type == :thinking end |
#to_h ⇒ Object
Serialize to a Hash for AMQP/fleet/wire transport.
88 89 90 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 88 def to_h super.compact end |
#to_json ⇒ Object
97 98 99 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 97 def to_json(*) to_h.to_json(*) end |
#to_s ⇒ Object
Human-readable string — prevents #inspect leaking into user-facing output.
102 103 104 105 106 107 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 102 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.
130 131 132 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 130 def tool_result? type == :tool_result end |
#tool_use? ⇒ Boolean
Whether this block represents a tool use request.
125 126 127 |
# File 'lib/legion/extensions/llm/canonical/content_block.rb', line 125 def tool_use? type == :tool_use end |