lex-llm 0.6.2 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/legion/extensions/llm/canonical/content_block.rb +28 -2
- data/lib/legion/extensions/llm/canonical/message.rb +5 -0
- data/lib/legion/extensions/llm/version.rb +1 -1
- data/spec/legion/extensions/llm/canonical/content_block_spec.rb +46 -0
- data/spec/legion/extensions/llm/canonical/message_spec.rb +23 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b1d6f2214c5ae415eab2e51068984dfdfb3cc7a35223439573d550d820bf864d
|
|
4
|
+
data.tar.gz: 23ba380421192e5320c2060fd89e357d26d2cb7958836697970eaa4bba5746fc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e1fb762ee7588f593a75e81b3a6ebc15f131ba74d0d5a54a6cd030d0367c4e41a1a2240c4287c10de4b7bbf25838f27c7a4ae557f75c67aa0b1e481378f752d8
|
|
7
|
+
data.tar.gz: 9d29438b9d78732887e081b41b7ee792b2459284d654e80e7c70f12be62fcaa585c3e166213530d7bfa57fd09bc5b9de43e12530daf8a38574172452de30a727
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.6.3 - 2026-06-25
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- `ContentBlock.from_hash` rescues `NoMethodError` when content arrays contain corrupted String elements (serialized `#inspect` output from prior storage bugs) — returns a text block instead of crashing with `undefined method 'transform_keys' for an instance of String`.
|
|
7
|
+
- `ContentBlock.from_hash` normalizes `output_text`/`input_text` types to `:text` via `TEXT_TYPE_ALIASES` so Responses API content blocks are recognized by `text?` and extracted by `Message#text`.
|
|
8
|
+
- `ContentBlock#to_s` returns clean text for all text-type blocks; `#inspect` returns a concise debug representation instead of the full 18-field Data.define dump.
|
|
9
|
+
- `Canonical::Message#to_s` delegates to `#text` to prevent Array#inspect leaking struct internals into string contexts.
|
|
10
|
+
|
|
3
11
|
## 0.6.2 - 2026-06-20
|
|
4
12
|
|
|
5
13
|
### Fixed
|
|
@@ -6,6 +6,7 @@ module Legion
|
|
|
6
6
|
module Canonical
|
|
7
7
|
# Typed content block with media_type support per G20a.
|
|
8
8
|
# Ports field vocabulary from Legion::LLM::Types::ContentBlock.
|
|
9
|
+
# rubocop:disable Lint/ConstantDefinitionInBlock -- required for Data.define block scope
|
|
9
10
|
ContentBlock = ::Data.define(
|
|
10
11
|
:type, :text, :data, :source_type, :media_type,
|
|
11
12
|
:detail, :name, :file_id,
|
|
@@ -13,6 +14,8 @@ module Legion
|
|
|
13
14
|
:source, :start_index, :end_index,
|
|
14
15
|
:code, :message, :cache_control
|
|
15
16
|
) do
|
|
17
|
+
TEXT_TYPE_ALIASES = %i[text output_text input_text].freeze
|
|
18
|
+
|
|
16
19
|
# Build a text content block.
|
|
17
20
|
def self.text(content, cache_control: nil)
|
|
18
21
|
new(
|
|
@@ -64,12 +67,17 @@ module Legion
|
|
|
64
67
|
end
|
|
65
68
|
|
|
66
69
|
# Build from a Hash (raw provider response or deserialized wire payload).
|
|
70
|
+
# Rescues NoMethodError from corrupted inputs (e.g. String elements from
|
|
71
|
+
# prior serialization bugs where ContentBlock#inspect leaked into storage).
|
|
67
72
|
def self.from_hash(source)
|
|
68
73
|
return nil if source.nil?
|
|
69
74
|
|
|
70
75
|
h = source.transform_keys(&:to_sym)
|
|
71
76
|
type_raw = h.delete(:type)
|
|
72
|
-
|
|
77
|
+
if type_raw
|
|
78
|
+
type_sym = type_raw.to_sym
|
|
79
|
+
h[:type] = TEXT_TYPE_ALIASES.include?(type_sym) ? :text : type_sym
|
|
80
|
+
end
|
|
73
81
|
|
|
74
82
|
new(
|
|
75
83
|
type: h[:type],
|
|
@@ -91,6 +99,10 @@ module Legion
|
|
|
91
99
|
message: h[:message],
|
|
92
100
|
cache_control: h[:cache_control]
|
|
93
101
|
)
|
|
102
|
+
rescue NoMethodError => e
|
|
103
|
+
Legion::Logging.log.warn('[canonical][content_block] from_hash received non-Hash input ' \
|
|
104
|
+
"(#{source.class}): #{e.message}")
|
|
105
|
+
text(source.to_s)
|
|
94
106
|
end
|
|
95
107
|
|
|
96
108
|
# Serialize to a Hash for AMQP/fleet/wire transport.
|
|
@@ -98,9 +110,22 @@ module Legion
|
|
|
98
110
|
super.compact
|
|
99
111
|
end
|
|
100
112
|
|
|
113
|
+
# Human-readable string — prevents #inspect leaking into user-facing output.
|
|
114
|
+
def to_s
|
|
115
|
+
return "[tool_use:#{name}]" if type == :tool_use
|
|
116
|
+
return '[image]' if type == :image
|
|
117
|
+
|
|
118
|
+
text.to_s
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Concise inspect — prevents raw Data.define dump in Array#inspect output.
|
|
122
|
+
def inspect
|
|
123
|
+
"#<ContentBlock:#{type} #{to_s.slice(0, 80).inspect}>"
|
|
124
|
+
end
|
|
125
|
+
|
|
101
126
|
# Whether this block carries textual content.
|
|
102
127
|
def text?
|
|
103
|
-
type
|
|
128
|
+
TEXT_TYPE_ALIASES.include?(type)
|
|
104
129
|
end
|
|
105
130
|
|
|
106
131
|
# Whether this block carries thinking/reasoning content.
|
|
@@ -120,6 +145,7 @@ module Legion
|
|
|
120
145
|
end
|
|
121
146
|
|
|
122
147
|
ContentBlock::CONTENT_BLOCK_TYPES = %i[text thinking tool_use tool_result image audio video].freeze
|
|
148
|
+
# rubocop:enable Lint/ConstantDefinitionInBlock
|
|
123
149
|
end
|
|
124
150
|
end
|
|
125
151
|
end
|
|
@@ -123,6 +123,11 @@ module Legion
|
|
|
123
123
|
super.compact
|
|
124
124
|
end
|
|
125
125
|
|
|
126
|
+
# Human-readable string — prevents #inspect leaking into user-facing output.
|
|
127
|
+
def to_s
|
|
128
|
+
text
|
|
129
|
+
end
|
|
130
|
+
|
|
126
131
|
# Minimal provider-facing hash (role + text content).
|
|
127
132
|
def to_provider_hash
|
|
128
133
|
{ role: role.to_s, content: text }.compact
|
|
@@ -166,6 +166,52 @@ RSpec.describe Legion::Extensions::Llm::Canonical::ContentBlock do
|
|
|
166
166
|
end
|
|
167
167
|
end
|
|
168
168
|
|
|
169
|
+
describe 'Responses API type normalization (output_text/input_text)' do
|
|
170
|
+
it 'normalizes output_text to :text via from_hash' do
|
|
171
|
+
block = described_class.from_hash(type: 'output_text', text: 'The seat templates')
|
|
172
|
+
|
|
173
|
+
expect(block.type).to eq(:text)
|
|
174
|
+
expect(block.text).to eq('The seat templates')
|
|
175
|
+
expect(block.text?).to be true
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
it 'normalizes input_text to :text via from_hash' do
|
|
179
|
+
block = described_class.from_hash(type: 'input_text', text: 'user message')
|
|
180
|
+
|
|
181
|
+
expect(block.type).to eq(:text)
|
|
182
|
+
expect(block.text?).to be true
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
it 'returns text content from to_s for output_text blocks' do
|
|
186
|
+
block = described_class.from_hash(type: 'output_text', text: "The seat templates don't")
|
|
187
|
+
|
|
188
|
+
expect(block.to_s).to eq("The seat templates don't")
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it 'does not leak #inspect struct into Array#to_s' do
|
|
192
|
+
blocks = [described_class.from_hash(type: 'output_text', text: 'hello')]
|
|
193
|
+
|
|
194
|
+
expect(blocks.inspect).not_to include('data Legion::Extensions')
|
|
195
|
+
expect(blocks.inspect).not_to include('source_type=nil')
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
describe '#to_s' do
|
|
200
|
+
it 'returns text for text blocks' do
|
|
201
|
+
expect(described_class.text('hello').to_s).to eq('hello')
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it 'returns placeholder for tool_use blocks' do
|
|
205
|
+
block = described_class.tool_use(id: '1', name: 'bash', input: {})
|
|
206
|
+
expect(block.to_s).to eq('[tool_use:bash]')
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
it 'returns placeholder for image blocks' do
|
|
210
|
+
block = described_class.image(data: 'x', media_type: 'image/png')
|
|
211
|
+
expect(block.to_s).to eq('[image]')
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
169
215
|
describe 'round-trip' do
|
|
170
216
|
it 'preserves text block through from_hash/to_h' do
|
|
171
217
|
original = { type: 'text', text: 'hello world' }
|
|
@@ -163,6 +163,29 @@ RSpec.describe Legion::Extensions::Llm::Canonical::Message do
|
|
|
163
163
|
|
|
164
164
|
expect(msg.text).to eq('')
|
|
165
165
|
end
|
|
166
|
+
|
|
167
|
+
it 'extracts text from output_text ContentBlock array (Responses API / Codex)' do
|
|
168
|
+
msg = described_class.from_hash(
|
|
169
|
+
role: :assistant,
|
|
170
|
+
content: [{ type: 'output_text', text: "The seat templates don't" }]
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
expect(msg.text).to eq("The seat templates don't")
|
|
174
|
+
expect(msg.text).not_to include('#<data')
|
|
175
|
+
expect(msg.text).not_to include('ContentBlock')
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
it 'extracts text from mixed output_text and text blocks' do
|
|
179
|
+
msg = described_class.from_hash(
|
|
180
|
+
role: :assistant,
|
|
181
|
+
content: [
|
|
182
|
+
{ type: 'output_text', text: 'first ' },
|
|
183
|
+
{ type: 'text', text: 'second' }
|
|
184
|
+
]
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
expect(msg.text).to eq('first second')
|
|
188
|
+
end
|
|
166
189
|
end
|
|
167
190
|
|
|
168
191
|
describe '#to_h' do
|