lex-llm-gemini 0.4.5 → 0.4.7
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/.rubocop.yml +5 -0
- data/CHANGELOG.md +27 -0
- data/Gemfile +8 -2
- data/README.md +9 -1
- data/RULES.md +97 -0
- data/lex-llm-gemini.gemspec +1 -1
- data/lib/legion/extensions/llm/gemini/actors/discovery.rb +24 -0
- data/lib/legion/extensions/llm/gemini/helpers/callable.rb +207 -0
- data/lib/legion/extensions/llm/gemini/provider.rb +169 -182
- data/lib/legion/extensions/llm/gemini/runners/discovery.rb +309 -0
- data/lib/legion/extensions/llm/gemini/runners/fleet_worker.rb +0 -2
- data/lib/legion/extensions/llm/gemini/version.rb +1 -1
- data/lib/legion/extensions/llm/gemini.rb +32 -9
- metadata +7 -4
- data/lib/legion/extensions/llm/gemini/actors/discovery_refresh.rb +0 -1168
|
@@ -6,36 +6,40 @@ module Legion
|
|
|
6
6
|
module Extensions
|
|
7
7
|
module Llm
|
|
8
8
|
module Gemini
|
|
9
|
-
# Message-kind
|
|
9
|
+
# Message-kind predicates over canonical messages. Canonical::Message
|
|
10
|
+
# exposes .tool_calls (Array<ToolCall> | nil) and .tool_call_id; the
|
|
11
|
+
# base funnel rejects anything that is not a Canonical::Message before
|
|
12
|
+
# rendering, so these see canonical values only.
|
|
10
13
|
module MessageKinds
|
|
11
14
|
private
|
|
12
15
|
|
|
13
16
|
def tool_call_message?(message)
|
|
14
|
-
return message.tool_call? if message.respond_to?(:tool_call?)
|
|
15
|
-
|
|
16
17
|
calls = message.tool_calls
|
|
17
18
|
!calls.nil? && !calls.empty?
|
|
18
19
|
end
|
|
19
20
|
|
|
20
21
|
def tool_result_message?(message)
|
|
21
|
-
return message.tool_result? if message.respond_to?(:tool_result?)
|
|
22
|
-
|
|
23
22
|
tool_call_id = message.tool_call_id
|
|
24
23
|
!tool_call_id.nil? && !tool_call_id.to_s.empty?
|
|
25
24
|
end
|
|
26
25
|
end
|
|
27
26
|
|
|
28
|
-
#
|
|
27
|
+
# Gemini request renderer (08 R1/R4): renders the provider wire FROM
|
|
28
|
+
# canonical values. Every Gemini wire-dialect decision lives here —
|
|
29
|
+
# the base funnel enforces canonical input centrally before this runs.
|
|
29
30
|
module MessageFormatter
|
|
30
31
|
private
|
|
31
32
|
|
|
32
33
|
def render_payload(messages, **opts)
|
|
33
|
-
model
|
|
34
|
-
tools
|
|
35
|
-
|
|
36
|
-
@model
|
|
37
|
-
build_request_payload(
|
|
38
|
-
|
|
34
|
+
model = opts.fetch(:model)
|
|
35
|
+
tools = opts.fetch(:tools)
|
|
36
|
+
params = opts.fetch(:params)
|
|
37
|
+
@model = model.respond_to?(:id) ? model.id : model.to_s
|
|
38
|
+
build_request_payload(
|
|
39
|
+
messages: messages, tools: tools,
|
|
40
|
+
temperature: maybe_normalize_temperature(params),
|
|
41
|
+
schema: opts[:schema], tool_prefs: opts[:tool_prefs]
|
|
42
|
+
)
|
|
39
43
|
end
|
|
40
44
|
|
|
41
45
|
def build_request_payload(messages:, tools:, temperature:, schema:, tool_prefs:)
|
|
@@ -88,39 +92,36 @@ module Legion
|
|
|
88
92
|
content_parts(message.content)
|
|
89
93
|
end
|
|
90
94
|
|
|
95
|
+
# Canonical content only: String | ContentBlock | Array<ContentBlock> | nil.
|
|
91
96
|
def content_parts(content)
|
|
92
|
-
return
|
|
93
|
-
return
|
|
94
|
-
return
|
|
97
|
+
return [] if content.nil?
|
|
98
|
+
return content.map { |block| content_block_part(block) } if content.is_a?(::Array)
|
|
99
|
+
return content_block_part(content) if content.is_a?(Legion::Extensions::Llm::Canonical::ContentBlock)
|
|
95
100
|
|
|
96
|
-
|
|
97
|
-
parts << { text: content.text } if content.text
|
|
98
|
-
content.attachments.each { |attachment| parts << attachment_part(attachment) }
|
|
99
|
-
parts
|
|
101
|
+
[{ text: content.to_s }]
|
|
100
102
|
end
|
|
101
103
|
|
|
102
|
-
def
|
|
103
|
-
if
|
|
104
|
-
{ text:
|
|
104
|
+
def content_block_part(block)
|
|
105
|
+
if block.text?
|
|
106
|
+
{ text: block.text }
|
|
107
|
+
elsif block.type == :image
|
|
108
|
+
{ inline_data: { mime_type: block.media_type, data: block.data } }
|
|
105
109
|
else
|
|
106
|
-
|
|
110
|
+
raise ArgumentError,
|
|
111
|
+
"gemini renderer cannot render a :#{block.type} content block to the Gemini wire"
|
|
107
112
|
end
|
|
108
113
|
end
|
|
109
114
|
|
|
110
115
|
def tool_call_parts(message)
|
|
111
|
-
|
|
112
|
-
calls.map do |tool_call|
|
|
116
|
+
message.tool_calls.map do |tool_call|
|
|
113
117
|
{ functionCall: { name: tool_call.name, args: tool_call.arguments } }
|
|
114
118
|
end
|
|
115
119
|
end
|
|
116
120
|
|
|
117
121
|
def tool_result_parts(message)
|
|
118
|
-
[{
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
response: { name: message.tool_call_id, content: content_parts(message.content) }
|
|
122
|
-
}
|
|
123
|
-
}]
|
|
122
|
+
[{ functionResponse: { name: message.tool_call_id,
|
|
123
|
+
response: { name: message.tool_call_id,
|
|
124
|
+
content: content_parts(message.content) } } }]
|
|
124
125
|
end
|
|
125
126
|
|
|
126
127
|
def format_tools(tools)
|
|
@@ -130,7 +131,7 @@ module Legion
|
|
|
130
131
|
{
|
|
131
132
|
name: Legion::Extensions::Llm::Canonical::ToolSchema.tool_name(tool),
|
|
132
133
|
description: Legion::Extensions::Llm::Canonical::ToolSchema.tool_description(tool),
|
|
133
|
-
|
|
134
|
+
parametersJsonSchema: schema
|
|
134
135
|
}
|
|
135
136
|
end
|
|
136
137
|
}]
|
|
@@ -149,197 +150,175 @@ module Legion
|
|
|
149
150
|
end
|
|
150
151
|
end
|
|
151
152
|
|
|
152
|
-
#
|
|
153
|
+
# Gemini response parser (08 R2/R4): parses the provider wire TO
|
|
154
|
+
# canonical types. Gemini wire-dialect translation (finishReason
|
|
155
|
+
# vocabulary, usageMetadata spellings, JSON-object vs JSON-string tool
|
|
156
|
+
# arguments, thought parts) lives here, at the edge.
|
|
153
157
|
module ResponseParser
|
|
154
158
|
private
|
|
155
159
|
|
|
160
|
+
# One response-parse boundary (08 R2): returns Canonical::Response.
|
|
156
161
|
def parse_completion_response(response)
|
|
157
162
|
body = response.body
|
|
158
163
|
parts = response_parts(body)
|
|
159
164
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
165
|
+
Canonical::Response.build(
|
|
166
|
+
text: text_content(parts),
|
|
167
|
+
thinking: thinking_from_parts(parts),
|
|
163
168
|
tool_calls: parse_tool_calls(parts),
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
thinking_tokens: body.dig('usageMetadata', 'thoughtsTokenCount'),
|
|
168
|
-
model_id: body['modelVersion'] || @model,
|
|
169
|
-
raw: body
|
|
169
|
+
usage: usage_from_metadata(body['usageMetadata']),
|
|
170
|
+
stop_reason: stop_reason_from_body(body),
|
|
171
|
+
model: body['modelVersion'] || @model
|
|
170
172
|
)
|
|
171
173
|
end
|
|
172
174
|
|
|
175
|
+
# One chunk-parse boundary (08 R2): returns a Canonical::Chunk or an
|
|
176
|
+
# Array of them (nil when the SSE body carried no content).
|
|
173
177
|
def build_chunk(data)
|
|
174
178
|
parts = response_parts(data)
|
|
179
|
+
stop_reason = stop_reason_from_body(data)
|
|
175
180
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
cached_tokens: data.dig('usageMetadata', 'cachedContentTokenCount'),
|
|
183
|
-
thinking_tokens: data.dig('usageMetadata', 'thoughtsTokenCount'),
|
|
184
|
-
model_id: data['modelVersion'] || @model,
|
|
185
|
-
raw: data
|
|
186
|
-
)
|
|
181
|
+
chunks = streaming_chunks_for(parts, stop_reason: stop_reason)
|
|
182
|
+
chunks.concat(chunk_for_usage(usage_from_metadata(data['usageMetadata']), stop_reason))
|
|
183
|
+
|
|
184
|
+
return nil if chunks.empty?
|
|
185
|
+
|
|
186
|
+
chunks.size == 1 ? chunks.first : chunks
|
|
187
187
|
end
|
|
188
188
|
|
|
189
|
-
def
|
|
190
|
-
|
|
189
|
+
def streaming_chunks_for(parts, stop_reason:)
|
|
190
|
+
chunks = chunk_for_thinking(thinking_from_parts(parts), stop_reason)
|
|
191
|
+
chunks.concat(chunk_for_text(text_content(parts), stop_reason))
|
|
192
|
+
function_calls = parts.filter_map { |part| part['functionCall'] }.compact
|
|
193
|
+
chunks.concat(streaming_tool_call_chunks(function_calls, stop_reason: stop_reason))
|
|
194
|
+
chunks
|
|
191
195
|
end
|
|
192
196
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
197
|
+
# Gemini's final frame usually carries finishReason and
|
|
198
|
+
# usageMetadata together (often with no text part), so the
|
|
199
|
+
# usage chunk carries the frame's stop_reason — the
|
|
200
|
+
# usage_chunk factory has no slot for it.
|
|
201
|
+
def chunk_for_usage(usage, stop_reason)
|
|
202
|
+
return [] if usage.nil?
|
|
203
|
+
|
|
204
|
+
[Canonical::Chunk.build(type: :usage, usage: usage, request_id: nil, stop_reason: stop_reason)]
|
|
196
205
|
end
|
|
197
206
|
|
|
198
|
-
def
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
207
|
+
def chunk_for_thinking(thinking, stop_reason)
|
|
208
|
+
return [] unless thinking
|
|
209
|
+
|
|
210
|
+
[Canonical::Chunk.thinking_delta(delta: thinking.content.to_s, request_id: nil,
|
|
211
|
+
signature: thinking.signature, stop_reason: stop_reason)]
|
|
203
212
|
end
|
|
204
213
|
|
|
205
|
-
def
|
|
206
|
-
|
|
207
|
-
function_call = part['functionCall']
|
|
208
|
-
next unless function_call
|
|
214
|
+
def chunk_for_text(text, stop_reason)
|
|
215
|
+
return [] if text.nil?
|
|
209
216
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
id: id,
|
|
213
|
-
name: function_call['name'],
|
|
214
|
-
arguments: function_call['args'] || {}
|
|
215
|
-
)
|
|
216
|
-
end
|
|
217
|
+
[Canonical::Chunk.text_delta(delta: text, request_id: nil, stop_reason: stop_reason)]
|
|
218
|
+
end
|
|
217
219
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
id: model_id,
|
|
229
|
-
name: model_data['displayName'] || model_id,
|
|
230
|
-
provider: provider,
|
|
231
|
-
context_length: model_data['inputTokenLimit'],
|
|
232
|
-
capabilities: capabilities.critical_capabilities_for(model_data),
|
|
233
|
-
modalities_input: input_mods,
|
|
234
|
-
modalities_output: output_mods,
|
|
235
|
-
metadata: {
|
|
236
|
-
max_output_tokens: model_data['outputTokenLimit'],
|
|
237
|
-
version: model_data['version'],
|
|
238
|
-
description: model_data['description'],
|
|
239
|
-
supported_generation_methods: methods
|
|
240
|
-
}
|
|
241
|
-
)
|
|
220
|
+
def streaming_tool_call_chunks(function_calls, stop_reason:)
|
|
221
|
+
function_calls.map do |function_call|
|
|
222
|
+
# Gemini streams a complete functionCall per chunk; the
|
|
223
|
+
# accumulator assembles arguments as JSON fragments, so the
|
|
224
|
+
# wire object is serialized to its JSON spelling here (10 U2).
|
|
225
|
+
fragment = { id: nil, name: function_call['name'],
|
|
226
|
+
arguments: Legion::JSON.generate(function_call['args'] || {}), index: nil,
|
|
227
|
+
signature: function_call['thoughtSignature'] }
|
|
228
|
+
Canonical::Chunk.build(type: :tool_call_delta, tool_call: fragment, request_id: nil,
|
|
229
|
+
stop_reason: stop_reason)
|
|
242
230
|
end
|
|
243
231
|
end
|
|
244
232
|
|
|
245
|
-
def
|
|
246
|
-
|
|
247
|
-
model: model_path(model),
|
|
248
|
-
content: { parts: [{ text: text.to_s }] },
|
|
249
|
-
outputDimensionality: dimensions
|
|
250
|
-
}.compact
|
|
233
|
+
def response_parts(body)
|
|
234
|
+
body.dig('candidates', 0, 'content', 'parts') || []
|
|
251
235
|
end
|
|
252
236
|
|
|
253
|
-
def
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
model: model,
|
|
257
|
-
input_tokens: response.body.dig('usageMetadata', 'promptTokenCount').to_i
|
|
258
|
-
)
|
|
237
|
+
def text_content(parts)
|
|
238
|
+
text = parts.reject { |part| part['thought'] }.filter_map { |part| part['text'] }.join
|
|
239
|
+
text.empty? ? nil : text
|
|
259
240
|
end
|
|
260
|
-
end
|
|
261
241
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
242
|
+
# Gemini marks reasoning with the per-part `thought` flag; the
|
|
243
|
+
# thought signature rides on the part (commonly a functionCall).
|
|
244
|
+
def thinking_from_parts(parts)
|
|
245
|
+
thinking_text = parts.select { |part| part['thought'] }.filter_map { |part| part['text'] }.join
|
|
246
|
+
signature = parts.filter_map { |part| part['thoughtSignature'] }.first
|
|
247
|
+
return nil if thinking_text.empty? && signature.nil?
|
|
265
248
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
provider_envelope: {},
|
|
272
|
-
provider_config: provider_capability_config,
|
|
273
|
-
instance_config: instance_capability_config,
|
|
274
|
-
model_config: model_capability_config(model.id)
|
|
275
|
-
)
|
|
249
|
+
Canonical::Thinking.build(content: thinking_text, signature: signature)
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def usage_from_metadata(usage_metadata)
|
|
253
|
+
return nil if usage_metadata.nil?
|
|
276
254
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
provider_family: slug.to_sym,
|
|
283
|
-
provider_instance: model.instance || provider_instance_id,
|
|
284
|
-
transport: offering_transport,
|
|
285
|
-
tier: offering_tier,
|
|
286
|
-
model: model.id,
|
|
287
|
-
canonical_model_alias: model.name,
|
|
288
|
-
model_family: model.family,
|
|
289
|
-
usage_type: offering_usage_type(model),
|
|
290
|
-
capabilities: policy[:capabilities],
|
|
291
|
-
capability_sources: policy[:sources],
|
|
292
|
-
limits: offering_limits(model),
|
|
293
|
-
health:,
|
|
294
|
-
metadata: offering_metadata(model)
|
|
255
|
+
Canonical::Usage.build(
|
|
256
|
+
input_tokens: usage_metadata['promptTokenCount'],
|
|
257
|
+
output_tokens: output_tokens_from_metadata(usage_metadata),
|
|
258
|
+
cache_read_tokens: usage_metadata['cachedContentTokenCount'],
|
|
259
|
+
thinking_tokens: usage_metadata['thoughtsTokenCount']
|
|
295
260
|
)
|
|
296
261
|
end
|
|
297
262
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
embedding: methods.include?('embedContent'),
|
|
304
|
-
vision: Legion::Extensions::Llm::Gemini::Provider::Capabilities.vision?(
|
|
305
|
-
meta.merge('name' => "models/#{model.id}")
|
|
306
|
-
)
|
|
307
|
-
}.compact
|
|
263
|
+
# Gemini reports output as candidatesTokenCount + thoughtsTokenCount
|
|
264
|
+
# (thoughts are billed output); nil when the metadata reports none.
|
|
265
|
+
def output_tokens_from_metadata(usage_metadata)
|
|
266
|
+
total = (usage_metadata['candidatesTokenCount'] || 0) + (usage_metadata['thoughtsTokenCount'] || 0)
|
|
267
|
+
total.positive? ? total : nil
|
|
308
268
|
end
|
|
309
269
|
|
|
310
|
-
def
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
rescue StandardError => e
|
|
314
|
-
handle_exception(e, level: :warn, handled: true, operation: 'gemini.provider_capability_config')
|
|
315
|
-
{}
|
|
316
|
-
end
|
|
270
|
+
def stop_reason_from_body(body)
|
|
271
|
+
finish_reason = body.dig('candidates', 0, 'finishReason')
|
|
272
|
+
return nil if finish_reason.nil?
|
|
317
273
|
|
|
318
|
-
|
|
319
|
-
config.to_h.slice(*Legion::Extensions::Llm::Provider::CAPABILITY_CONFIG_KEYS)
|
|
274
|
+
stop_reason_lookup(finish_reason)
|
|
320
275
|
end
|
|
321
276
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
277
|
+
# Sync tool calls → Array<ToolCall> (canonical; the legacy Hash-of-
|
|
278
|
+
# calls shape is gone). The ONE strict arguments parser (10 U2) runs
|
|
279
|
+
# on the JSON-string spelling; a wire object passes through as the
|
|
280
|
+
# canonical Hash.
|
|
281
|
+
def parse_tool_calls(parts)
|
|
282
|
+
parts.filter_map do |part|
|
|
283
|
+
function_call = part['functionCall']
|
|
284
|
+
next if function_call.nil?
|
|
325
285
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
286
|
+
Canonical::ToolCall.build(name: function_call['name'].to_s,
|
|
287
|
+
arguments: gemini_tool_arguments(function_call['args']),
|
|
288
|
+
metadata: { signature: function_call['thoughtSignature'] }.compact)
|
|
289
|
+
end
|
|
330
290
|
end
|
|
331
291
|
|
|
332
|
-
def
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
292
|
+
def gemini_tool_arguments(raw)
|
|
293
|
+
return {} if raw.nil?
|
|
294
|
+
return raw if raw.is_a?(::Hash)
|
|
295
|
+
|
|
296
|
+
Legion::Extensions::Llm::Responses::ToolArguments.parse!(raw)
|
|
337
297
|
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# Gemini operation parsers: the embed operation's documented artifact
|
|
301
|
+
# (05 §3, O07 — a Hash shape, not a canonical type).
|
|
302
|
+
module OperationParsers
|
|
303
|
+
private
|
|
338
304
|
|
|
339
|
-
def
|
|
340
|
-
|
|
305
|
+
def render_embedding_payload(text, model:, dimensions:)
|
|
306
|
+
{
|
|
307
|
+
model: model_path(model),
|
|
308
|
+
content: { parts: [{ text: text.to_s }] },
|
|
309
|
+
outputDimensionality: dimensions
|
|
310
|
+
}.compact
|
|
311
|
+
end
|
|
341
312
|
|
|
342
|
-
|
|
313
|
+
def parse_embedding_response(response, model:, text:)
|
|
314
|
+
{
|
|
315
|
+
text: text,
|
|
316
|
+
model: model,
|
|
317
|
+
embedding: response.body.dig('embedding', 'values'),
|
|
318
|
+
usage: Canonical::Usage.build(
|
|
319
|
+
input_tokens: response.body.dig('usageMetadata', 'promptTokenCount')
|
|
320
|
+
)
|
|
321
|
+
}
|
|
343
322
|
end
|
|
344
323
|
end
|
|
345
324
|
|
|
@@ -349,7 +328,7 @@ module Legion
|
|
|
349
328
|
include MessageKinds
|
|
350
329
|
include MessageFormatter
|
|
351
330
|
include ResponseParser
|
|
352
|
-
include
|
|
331
|
+
include OperationParsers
|
|
353
332
|
|
|
354
333
|
class << self
|
|
355
334
|
def slug = 'gemini'
|
|
@@ -436,11 +415,19 @@ module Legion
|
|
|
436
415
|
"#{model_path(model)}:embedContent"
|
|
437
416
|
end
|
|
438
417
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
418
|
+
# StopReasonMapping mixin override: the shared vocabulary covers the
|
|
419
|
+
# common spellings; these are the Gemini wire finishReason additions
|
|
420
|
+
# (R4: dialect at the parser edge).
|
|
421
|
+
def stop_reason_map_additions
|
|
422
|
+
{
|
|
423
|
+
'STOP' => :end_turn,
|
|
424
|
+
'MAX_TOKENS' => :max_tokens,
|
|
425
|
+
'SAFETY' => :content_filter,
|
|
426
|
+
'RECITATION' => :content_filter,
|
|
427
|
+
'PROHIBITED_CONTENT' => :content_filter,
|
|
428
|
+
'SPII' => :content_filter,
|
|
429
|
+
'IMAGE_SAFETY' => :content_filter
|
|
430
|
+
}
|
|
444
431
|
end
|
|
445
432
|
end
|
|
446
433
|
end
|