riffer 0.53.0 → 0.55.0
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/.claude/rules/rbs-inline.md +8 -8
- data/.release-please-manifest.json +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +43 -0
- data/README.md +24 -0
- data/Steepfile +19 -0
- data/docs/AGENTS.md +101 -14
- data/docs/CATALOG.md +8 -8
- data/docs/CONFIGURATION.md +47 -16
- data/docs/MCP.md +3 -4
- data/docs/OVERVIEW.md +1 -1
- data/docs/STREAM_EVENTS.md +1 -1
- data/docs/TOOLS.md +15 -12
- data/docs/TRACING.md +1 -1
- data/docs/providers/AMAZON_BEDROCK.md +2 -0
- data/docs/providers/ANTHROPIC.md +32 -36
- data/docs/providers/CUSTOM_PROVIDERS.md +35 -0
- data/docs/providers/GEMINI.md +76 -25
- data/docs/providers/GOOGLE_CLOUD.md +363 -0
- data/docs/providers/PROVIDERS.md +30 -18
- data/docs-site/manifest.yml +3 -0
- data/lib/riffer/agent/structured_output.rb +28 -3
- data/lib/riffer/config/anthropic.rb +9 -0
- data/lib/riffer/config/gemini.rb +9 -0
- data/lib/riffer/config/google_cloud.rb +38 -0
- data/lib/riffer/config.rb +3 -0
- data/lib/riffer/http/client.rb +141 -0
- data/lib/riffer/http.rb +5 -0
- data/lib/riffer/mcp/client.rb +0 -1
- data/lib/riffer/params/any_of.rb +50 -0
- data/lib/riffer/params/format.rb +73 -0
- data/lib/riffer/params/param.rb +143 -19
- data/lib/riffer/params.rb +156 -30
- data/lib/riffer/providers/amazon_bedrock.rb +38 -5
- data/lib/riffer/providers/anthropic/client.rb +49 -0
- data/lib/riffer/providers/anthropic.rb +19 -452
- data/lib/riffer/providers/anthropic_compatible/messages_api/stream.rb +195 -0
- data/lib/riffer/providers/anthropic_compatible/messages_api.rb +293 -0
- data/lib/riffer/providers/anthropic_compatible.rb +5 -0
- data/lib/riffer/providers/anthropic_vertex.rb +66 -0
- data/lib/riffer/providers/base.rb +13 -16
- data/lib/riffer/providers/gemini/client.rb +17 -85
- data/lib/riffer/providers/gemini.rb +213 -80
- data/lib/riffer/providers/google_cloud/client.rb +106 -0
- data/lib/riffer/providers/google_cloud.rb +99 -0
- data/lib/riffer/providers/repository.rb +2 -0
- data/lib/riffer/version.rb +1 -1
- data/lib/riffer/wire/sse.rb +51 -0
- data/lib/riffer/wire.rb +31 -0
- data/lib/riffer.rb +18 -0
- data/rbs_collection.lock.yaml +32 -4
- data/rbs_collection.yaml +2 -0
- data/sig/_private/faraday.rbs +8 -0
- data/sig/_private/googleauth.rbs +7 -0
- data/sig/generated/riffer/agent/structured_output.rbs +6 -0
- data/sig/generated/riffer/config/anthropic.rbs +6 -0
- data/sig/generated/riffer/config/gemini.rbs +6 -0
- data/sig/generated/riffer/config/google_cloud.rbs +28 -0
- data/sig/generated/riffer/config.rbs +2 -0
- data/sig/generated/riffer/http/client.rbs +59 -0
- data/sig/generated/riffer/http.rbs +4 -0
- data/sig/generated/riffer/params/any_of.rbs +21 -0
- data/sig/generated/riffer/params/format.rbs +39 -0
- data/sig/generated/riffer/params/param.rbs +27 -4
- data/sig/generated/riffer/params.rbs +38 -8
- data/sig/generated/riffer/providers/amazon_bedrock.rbs +14 -2
- data/sig/generated/riffer/providers/anthropic/client.rbs +34 -0
- data/sig/generated/riffer/providers/anthropic.rbs +5 -124
- data/sig/generated/riffer/providers/anthropic_compatible/messages_api/stream.rbs +71 -0
- data/sig/generated/riffer/providers/anthropic_compatible/messages_api.rbs +99 -0
- data/sig/generated/riffer/providers/anthropic_compatible.rbs +4 -0
- data/sig/generated/riffer/providers/anthropic_vertex.rbs +45 -0
- data/sig/generated/riffer/providers/gemini/client.rbs +11 -34
- data/sig/generated/riffer/providers/gemini.rbs +66 -4
- data/sig/generated/riffer/providers/google_cloud/client.rbs +71 -0
- data/sig/generated/riffer/providers/google_cloud.rbs +64 -0
- data/sig/generated/riffer/wire/sse.rbs +23 -0
- data/sig/generated/riffer/wire.rbs +17 -0
- data/sig/generated/riffer.rbs +12 -0
- data/sig/manifest.yaml +2 -1
- data/sig/manual/riffer/params/format.rbs +5 -0
- data/sig/manual/riffer/providers/anthropic_compatible/messages_api.rbs +8 -0
- data/sig/manual/riffer/providers/gemini.rbs +8 -0
- data/sig/manual/riffer/providers/google_cloud.rbs +8 -0
- data/sig/manual/riffer/wire.rbs +5 -0
- metadata +66 -5
- data/sig/_private/anthropic.rbs +0 -17
- data/sig/_private/riffer/providers/anthropic.rbs +0 -6
- data/sig/_private/riffer/providers/gemini.rbs +0 -7
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
class Riffer::Providers::AnthropicCompatible::MessagesAPI < Riffer::Providers::Base
|
|
5
|
+
WEB_SEARCH_TOOL_TYPE = "web_search_20250305" #: String
|
|
6
|
+
|
|
7
|
+
FINISH_REASONS = {
|
|
8
|
+
"end_turn" => :stop,
|
|
9
|
+
"stop_sequence" => :stop,
|
|
10
|
+
"max_tokens" => :length,
|
|
11
|
+
"tool_use" => :tool_calls,
|
|
12
|
+
"refusal" => :content_filter,
|
|
13
|
+
"model_context_window_exceeded" => :context_window,
|
|
14
|
+
# A paused server-tool turn resumes only by re-sending the response; the
|
|
15
|
+
# agent loop does not do that, so it has no normalized equivalent.
|
|
16
|
+
"pause_turn" => :other,
|
|
17
|
+
}.freeze #: Hash[String, Symbol]
|
|
18
|
+
|
|
19
|
+
# Only parts carrying this tag are replayed, so reasoning captured by another
|
|
20
|
+
# adapter, whose signatures the Messages API may not accept, never reaches the
|
|
21
|
+
# request.
|
|
22
|
+
REASONING_FORMAT = "anthropic-messages-v1" #: String
|
|
23
|
+
|
|
24
|
+
#--
|
|
25
|
+
#: (?String?) -> singleton(Riffer::Skills::Adapter)
|
|
26
|
+
def self.skills_adapter(_model = nil)
|
|
27
|
+
Riffer::Skills::XmlAdapter
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
#--
|
|
33
|
+
#: (Symbol) -> Hash[Symbol, untyped]
|
|
34
|
+
def default_reasoning_options(level)
|
|
35
|
+
return { thinking: { type: "disabled" } } if level == :off
|
|
36
|
+
|
|
37
|
+
{ thinking: { type: "adaptive" }, output_config: { effort: level.to_s } }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Relative to the client's base URL.
|
|
41
|
+
#--
|
|
42
|
+
#: (String?, stream: bool) -> String
|
|
43
|
+
def messages_path(_model, stream:)
|
|
44
|
+
"v1/messages"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
#--
|
|
48
|
+
#: (Hash[Symbol, untyped], stream: bool) -> Hash[Symbol, untyped]
|
|
49
|
+
def request_body(params, stream:)
|
|
50
|
+
stream ? params.merge(stream: true) : params
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
#--
|
|
54
|
+
#: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
|
|
55
|
+
def execute_generate(params)
|
|
56
|
+
client.post(messages_path(params[:model], stream: false), request_body(params, stream: false))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
#--
|
|
60
|
+
#: (Hash[Symbol, untyped], Riffer::Providers::_EventSink) -> void
|
|
61
|
+
def execute_stream(params, yielder)
|
|
62
|
+
stream = Stream.new(tools: @current_tools)
|
|
63
|
+
decoder = Riffer::Wire::SSE.new
|
|
64
|
+
|
|
65
|
+
client.post_stream(messages_path(params[:model], stream: true), request_body(params, stream: true)) do |chunk|
|
|
66
|
+
decoder.feed(chunk) { |data, _event| stream.handle(JSON.parse(data, symbolize_names: true), yielder) }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
message = stream.finish!
|
|
70
|
+
yield_finish_reason(yielder, extract_finish_reason(message))
|
|
71
|
+
usage = extract_token_usage(message)
|
|
72
|
+
yielder << Riffer::StreamEvents::TokenUsageDone.new(token_usage: usage) if usage
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
#--
|
|
76
|
+
#: (Hash[Symbol, untyped]) -> String
|
|
77
|
+
def extract_content(response)
|
|
78
|
+
content_blocks(response).filter_map { |block| block[:text] if block[:type] == "text" }.join
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
#--
|
|
82
|
+
#: (Hash[Symbol, untyped]) -> Array[Riffer::Messages::Assistant::ToolCall]
|
|
83
|
+
def extract_tool_calls(response)
|
|
84
|
+
content_blocks(response).filter_map do |block|
|
|
85
|
+
next unless block[:type] == "tool_use"
|
|
86
|
+
|
|
87
|
+
Riffer::Messages::Assistant::ToolCall.new(
|
|
88
|
+
call_id: block[:id],
|
|
89
|
+
name: decode_tool_name(block[:name], tools: @current_tools),
|
|
90
|
+
arguments: (block[:input] || {}).to_json,
|
|
91
|
+
)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
#--
|
|
96
|
+
#: (Hash[Symbol, untyped]) -> Array[Riffer::Messages::Assistant::ReasoningPart]
|
|
97
|
+
def extract_reasoning(response)
|
|
98
|
+
content_blocks(response).filter_map { |block| Stream.reasoning_part(block) }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
#--
|
|
102
|
+
#: (Hash[Symbol, untyped]) -> Riffer::Providers::TokenUsage?
|
|
103
|
+
def extract_token_usage(response)
|
|
104
|
+
usage = response[:usage]
|
|
105
|
+
return nil unless usage
|
|
106
|
+
|
|
107
|
+
cache_write = usage[:cache_creation_input_tokens]
|
|
108
|
+
cache_read = usage[:cache_read_input_tokens]
|
|
109
|
+
|
|
110
|
+
apply_pricing(
|
|
111
|
+
Riffer::Providers::TokenUsage.new(
|
|
112
|
+
# Anthropic's input_tokens excludes the cache buckets; TokenUsage's includes them.
|
|
113
|
+
input_tokens: (usage[:input_tokens] || 0) + (cache_write || 0) + (cache_read || 0),
|
|
114
|
+
output_tokens: usage[:output_tokens] || 0,
|
|
115
|
+
cache_write_tokens: cache_write,
|
|
116
|
+
cache_read_tokens: cache_read,
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
#--
|
|
122
|
+
#: (Hash[Symbol, untyped]) -> Riffer::Providers::FinishReason?
|
|
123
|
+
def extract_finish_reason(response)
|
|
124
|
+
raw = response[:stop_reason]
|
|
125
|
+
return nil unless raw
|
|
126
|
+
|
|
127
|
+
Riffer::Providers::FinishReason.new(reason: FINISH_REASONS.fetch(raw, :other), raw: raw)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
#--
|
|
131
|
+
#: (Hash[Symbol, untyped]) -> Array[Hash[Symbol, untyped]]
|
|
132
|
+
def content_blocks(response)
|
|
133
|
+
response[:content] || []
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
#--
|
|
137
|
+
#: (Array[Riffer::Messages::Base], String?, Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
|
|
138
|
+
def build_request_params(messages, model, options)
|
|
139
|
+
partitioned_messages = partition_messages(messages)
|
|
140
|
+
tools = options[:tools]
|
|
141
|
+
structured_output = options[:structured_output]
|
|
142
|
+
web_search = options[:web_search]
|
|
143
|
+
tags = options[:tags] || {}
|
|
144
|
+
|
|
145
|
+
max_tokens = options.fetch(:max_tokens, 4096)
|
|
146
|
+
|
|
147
|
+
params = {
|
|
148
|
+
model: model,
|
|
149
|
+
messages: partitioned_messages[:conversation],
|
|
150
|
+
max_tokens: max_tokens,
|
|
151
|
+
**options.except(:tools, :max_tokens, :structured_output, :web_search, :tags),
|
|
152
|
+
} #: Hash[Symbol, untyped]
|
|
153
|
+
|
|
154
|
+
params[:system] = partitioned_messages[:system] if partitioned_messages[:system]
|
|
155
|
+
|
|
156
|
+
metadata = request_metadata(tags)
|
|
157
|
+
params[:metadata] = metadata if metadata
|
|
158
|
+
|
|
159
|
+
api_tools = [] #: Array[Hash[Symbol, untyped]]
|
|
160
|
+
api_tools.concat(tools.map { |t| convert_tool(t) }) if tools && !tools.empty?
|
|
161
|
+
api_tools << web_search_tool(web_search) if web_search
|
|
162
|
+
|
|
163
|
+
if structured_output
|
|
164
|
+
# The format wins over caller output_config keys because the run loop
|
|
165
|
+
# validates against it.
|
|
166
|
+
params[:output_config] = { **(params[:output_config] || {}), **structured_output_config(structured_output) }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
params[:tools] = api_tools unless api_tools.empty?
|
|
170
|
+
|
|
171
|
+
params
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
#--
|
|
175
|
+
#: (Hash[String, untyped]) -> Hash[Symbol, untyped]?
|
|
176
|
+
def request_metadata(tags)
|
|
177
|
+
# The Messages API's only request-metadata field is metadata.user_id (opaque, no
|
|
178
|
+
# PII); all other tags survive only on spans.
|
|
179
|
+
user_id = tags["user_id"]
|
|
180
|
+
{ user_id: user_id } if user_id
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
#--
|
|
184
|
+
#: (untyped) -> Hash[Symbol, untyped]
|
|
185
|
+
def web_search_tool(web_search)
|
|
186
|
+
tool = { type: WEB_SEARCH_TOOL_TYPE, name: "web_search" } #: Hash[Symbol, untyped]
|
|
187
|
+
tool.merge!(web_search) if web_search.is_a?(Hash)
|
|
188
|
+
tool
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
#--
|
|
192
|
+
#: (Riffer::Agent::StructuredOutput) -> Hash[Symbol, untyped]
|
|
193
|
+
def structured_output_config(structured_output)
|
|
194
|
+
# Strict schema makes optional fields nullable; otherwise Anthropic may
|
|
195
|
+
# return empty strings or whitespace instead of null.
|
|
196
|
+
{
|
|
197
|
+
format: {
|
|
198
|
+
type: "json_schema",
|
|
199
|
+
schema: structured_output.json_schema(strict: true),
|
|
200
|
+
},
|
|
201
|
+
}
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
#--
|
|
205
|
+
#: (Array[Riffer::Messages::Base]) -> Hash[Symbol, untyped]
|
|
206
|
+
def partition_messages(messages)
|
|
207
|
+
system_prompts = [] #: Array[Hash[Symbol, untyped]]
|
|
208
|
+
conversation_messages = [] #: Array[Hash[Symbol, untyped]]
|
|
209
|
+
|
|
210
|
+
messages.each do |message|
|
|
211
|
+
case message
|
|
212
|
+
when Riffer::Messages::System
|
|
213
|
+
system_prompts << { type: "text", text: message.content }
|
|
214
|
+
when Riffer::Messages::User
|
|
215
|
+
if message.files.empty?
|
|
216
|
+
conversation_messages << { role: "user", content: message.content }
|
|
217
|
+
else
|
|
218
|
+
content = [{ type: "text", text: message.content }]
|
|
219
|
+
message.files.each { |file| content << convert_file_part(file) }
|
|
220
|
+
conversation_messages << { role: "user", content: content }
|
|
221
|
+
end
|
|
222
|
+
when Riffer::Messages::Assistant
|
|
223
|
+
conversation_messages << convert_assistant(message)
|
|
224
|
+
when Riffer::Messages::Tool
|
|
225
|
+
conversation_messages << {
|
|
226
|
+
role: "user",
|
|
227
|
+
content: [{
|
|
228
|
+
type: "tool_result",
|
|
229
|
+
tool_use_id: message.tool_call_id,
|
|
230
|
+
content: message.content,
|
|
231
|
+
}],
|
|
232
|
+
}
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
{
|
|
237
|
+
system: system_prompts.empty? ? nil : system_prompts,
|
|
238
|
+
conversation: conversation_messages,
|
|
239
|
+
}
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
#--
|
|
243
|
+
#: (Riffer::Messages::Assistant) -> Hash[Symbol, untyped]
|
|
244
|
+
def convert_assistant(message)
|
|
245
|
+
content = message.reasoning.filter_map do |part|
|
|
246
|
+
convert_reasoning_part(part) if part.format == REASONING_FORMAT
|
|
247
|
+
end
|
|
248
|
+
content << { type: "text", text: message.content } if message.content && !message.content.empty?
|
|
249
|
+
|
|
250
|
+
message.tool_calls.each do |tc|
|
|
251
|
+
content << {
|
|
252
|
+
type: "tool_use",
|
|
253
|
+
id: tc.call_id,
|
|
254
|
+
name: encode_tool_name(tc.name),
|
|
255
|
+
input: parse_tool_arguments(tc.arguments),
|
|
256
|
+
}
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
{ role: "assistant", content: content }
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
#--
|
|
263
|
+
#: (Riffer::Messages::Assistant::ReasoningPart) -> Hash[Symbol, untyped]
|
|
264
|
+
def convert_reasoning_part(part)
|
|
265
|
+
return { type: "redacted_thinking", data: part.data } if part.type == :encrypted
|
|
266
|
+
|
|
267
|
+
{ type: "thinking", thinking: part.text.to_s, signature: part.signature }
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
#--
|
|
271
|
+
#: (Riffer::Messages::User::FilePart) -> Hash[Symbol, untyped]
|
|
272
|
+
def convert_file_part(file)
|
|
273
|
+
type = file.image? ? "image" : "document"
|
|
274
|
+
|
|
275
|
+
source = if file.url?
|
|
276
|
+
{ type: "url", url: file.url }
|
|
277
|
+
else
|
|
278
|
+
{ type: "base64", media_type: file.media_type, data: file.data }
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
{ type: type, source: source }
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
#--
|
|
285
|
+
#: (singleton(Riffer::Tool)) -> Hash[Symbol, untyped]
|
|
286
|
+
def convert_tool(tool)
|
|
287
|
+
{
|
|
288
|
+
name: encode_tool_name(tool.name),
|
|
289
|
+
description: tool.description,
|
|
290
|
+
input_schema: tool.parameters_schema(strict: true),
|
|
291
|
+
}
|
|
292
|
+
end
|
|
293
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
# Claude models on Gemini Enterprise Agent Platform (formerly Vertex AI), through
|
|
5
|
+
# its rawPredict endpoints. Shares config.google_cloud and GoogleCloud::Client
|
|
6
|
+
# with the google_cloud provider.
|
|
7
|
+
class Riffer::Providers::AnthropicVertex < Riffer::Providers::AnthropicCompatible::MessagesAPI
|
|
8
|
+
# Matches the Anthropic provider. Agent Platform also accepts "vertex-2023-10-16".
|
|
9
|
+
DEFAULT_ANTHROPIC_VERSION = "2026-10-21" #: String
|
|
10
|
+
|
|
11
|
+
# Agent Platform model ids may carry an +@version+ suffix.
|
|
12
|
+
VALID_MODEL_PATTERN = /\A[a-zA-Z0-9._@-]+\z/ #: Regexp
|
|
13
|
+
|
|
14
|
+
#--
|
|
15
|
+
#: () -> String
|
|
16
|
+
def self.semconv_provider_name
|
|
17
|
+
"gcp.vertex_ai"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
#--
|
|
23
|
+
#: () -> untyped
|
|
24
|
+
def global_client
|
|
25
|
+
Riffer.config.google_cloud.client
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
#--
|
|
29
|
+
#: () -> Riffer::Providers::GoogleCloud::Client
|
|
30
|
+
def build_client
|
|
31
|
+
Riffer::Providers::GoogleCloud::Client.from_config(Riffer.config.google_cloud)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Agent Platform takes the API version in the body, in place of the
|
|
35
|
+
# anthropic-version header. Override in a subclass to send another version.
|
|
36
|
+
#--
|
|
37
|
+
#: () -> String
|
|
38
|
+
def anthropic_version
|
|
39
|
+
DEFAULT_ANTHROPIC_VERSION
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The model goes in the URL rather than the body.
|
|
43
|
+
#--
|
|
44
|
+
#: (String?, stream: bool) -> String
|
|
45
|
+
def messages_path(model, stream:)
|
|
46
|
+
validate_model!(model)
|
|
47
|
+
"publishers/anthropic/models/#{model}:#{stream ? 'streamRawPredict' : 'rawPredict'}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
#--
|
|
51
|
+
#: (Hash[Symbol, untyped], stream: bool) -> Hash[Symbol, untyped]
|
|
52
|
+
def request_body(params, stream:)
|
|
53
|
+
body = { anthropic_version: anthropic_version, **params.except(:model) }
|
|
54
|
+
stream ? body.merge(stream: true) : body
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
#--
|
|
58
|
+
#: (String?) -> void
|
|
59
|
+
def validate_model!(model)
|
|
60
|
+
return if model.is_a?(String) && model.match?(VALID_MODEL_PATTERN)
|
|
61
|
+
|
|
62
|
+
raise Riffer::ArgumentError,
|
|
63
|
+
"Invalid model name: #{model.inspect}. Model must contain only alphanumeric characters, " \
|
|
64
|
+
"hyphens, dots, underscores, and @."
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -9,7 +9,7 @@ class Riffer::Providers::Base
|
|
|
9
9
|
# @rbs @client: untyped
|
|
10
10
|
# @rbs self.@semconv_provider_name: String?
|
|
11
11
|
|
|
12
|
-
WIRE_SEPARATOR =
|
|
12
|
+
WIRE_SEPARATOR = Riffer::Wire::TOOL_NAME_SEPARATOR #: String
|
|
13
13
|
|
|
14
14
|
#--
|
|
15
15
|
#: (?String?) -> singleton(Riffer::Skills::Adapter)
|
|
@@ -38,7 +38,7 @@ class Riffer::Providers::Base
|
|
|
38
38
|
messages = normalize_messages(prompt: prompt, system: system, messages: messages, files: files)
|
|
39
39
|
validate_normalized_messages!(messages)
|
|
40
40
|
Riffer::Files::Resolver.new(provider: self).resolve!(messages)
|
|
41
|
-
messages = merge_consecutive_messages(
|
|
41
|
+
messages = with_structured_output_prompt(merge_consecutive_messages(messages), options[:structured_output])
|
|
42
42
|
options = apply_reasoning(model, options)
|
|
43
43
|
params = build_request_params(messages, model, request_options(options))
|
|
44
44
|
|
|
@@ -77,7 +77,7 @@ class Riffer::Providers::Base
|
|
|
77
77
|
messages = normalize_messages(prompt: prompt, system: system, messages: messages, files: files)
|
|
78
78
|
validate_normalized_messages!(messages)
|
|
79
79
|
Riffer::Files::Resolver.new(provider: self).resolve!(messages)
|
|
80
|
-
messages = merge_consecutive_messages(
|
|
80
|
+
messages = with_structured_output_prompt(merge_consecutive_messages(messages), options[:structured_output])
|
|
81
81
|
options = apply_reasoning(model, options)
|
|
82
82
|
params = build_request_params(messages, model, request_options(options))
|
|
83
83
|
|
|
@@ -135,14 +135,13 @@ class Riffer::Providers::Base
|
|
|
135
135
|
#--
|
|
136
136
|
#: (String) -> String
|
|
137
137
|
def encode_tool_name(name)
|
|
138
|
-
|
|
138
|
+
Riffer::Wire.encode_tool_name(name)
|
|
139
139
|
end
|
|
140
140
|
|
|
141
141
|
#--
|
|
142
142
|
#: (String, tools: Array[singleton(Riffer::Tool)]) -> String
|
|
143
143
|
def decode_tool_name(wire_name, tools:)
|
|
144
|
-
|
|
145
|
-
tool ? tool.name : wire_name
|
|
144
|
+
Riffer::Wire.decode_tool_name(wire_name, tools: tools)
|
|
146
145
|
end
|
|
147
146
|
|
|
148
147
|
#--
|
|
@@ -417,9 +416,7 @@ class Riffer::Providers::Base
|
|
|
417
416
|
#--
|
|
418
417
|
#: (String) -> Hash[String, untyped]
|
|
419
418
|
def parse_tool_arguments(arguments)
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
JSON.parse(arguments)
|
|
419
|
+
Riffer::Wire.parse_tool_arguments(arguments)
|
|
423
420
|
end
|
|
424
421
|
|
|
425
422
|
#--
|
|
@@ -427,13 +424,13 @@ class Riffer::Providers::Base
|
|
|
427
424
|
def with_structured_output_prompt(messages, structured_output)
|
|
428
425
|
return messages unless structured_output&.prompted?
|
|
429
426
|
|
|
430
|
-
#
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
427
|
+
# Models follow the schema more reliably at the end of each user turn than in the system prompt.
|
|
428
|
+
instructions = structured_output.prompt_instructions
|
|
429
|
+
messages.map do |message|
|
|
430
|
+
next message unless message.is_a?(Riffer::Messages::User)
|
|
431
|
+
|
|
432
|
+
Riffer::Messages::User.new("#{message.content}\n\n#{instructions}", id: message.id, files: message.files)
|
|
433
|
+
end
|
|
437
434
|
end
|
|
438
435
|
|
|
439
436
|
#--
|
|
@@ -1,107 +1,39 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
# rbs_inline: enabled
|
|
3
3
|
|
|
4
|
-
require "json"
|
|
5
|
-
require "net/http"
|
|
6
|
-
require "uri"
|
|
7
|
-
|
|
8
4
|
# <tt>Riffer.config.gemini.client</tt> accepts any object implementing +post+ and +post_stream+
|
|
9
|
-
#
|
|
10
|
-
class Riffer::Providers::Gemini::Client
|
|
11
|
-
# @rbs @api_key: String
|
|
5
|
+
# (see Riffer::HTTP::Client); this class is the default, not a required base.
|
|
6
|
+
class Riffer::Providers::Gemini::Client < Riffer::HTTP::Client
|
|
7
|
+
# @rbs @api_key: String
|
|
12
8
|
# @rbs @base_url: String
|
|
13
|
-
# @rbs @open_timeout: Integer
|
|
14
|
-
# @rbs @read_timeout: Integer
|
|
15
|
-
# @rbs @write_timeout: Integer?
|
|
16
|
-
# @rbs @proxy_address: String?
|
|
17
|
-
# @rbs @proxy_port: Integer?
|
|
18
9
|
|
|
19
10
|
DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com" #: String
|
|
20
|
-
DEFAULT_OPEN_TIMEOUT = 10 #: Integer
|
|
21
|
-
DEFAULT_READ_TIMEOUT = 60 #: Integer
|
|
22
|
-
|
|
23
|
-
#: (?api_key: String?, ?base_url: String, ?open_timeout: Integer, ?read_timeout: Integer, ?write_timeout: Integer?, ?proxy_address: String?, ?proxy_port: Integer?) -> void
|
|
24
|
-
def initialize(api_key: nil, base_url: DEFAULT_BASE_URL, open_timeout: DEFAULT_OPEN_TIMEOUT,
|
|
25
|
-
read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: nil,
|
|
26
|
-
proxy_address: nil, proxy_port: nil)
|
|
27
|
-
@api_key = api_key
|
|
28
|
-
@base_url = base_url
|
|
29
|
-
@open_timeout = open_timeout
|
|
30
|
-
@read_timeout = read_timeout
|
|
31
|
-
@write_timeout = write_timeout
|
|
32
|
-
@proxy_address = proxy_address
|
|
33
|
-
@proxy_port = proxy_port
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# Raises Riffer::Error on a non-success status.
|
|
37
|
-
#--
|
|
38
|
-
#: (String, Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
|
|
39
|
-
def post(path, body)
|
|
40
|
-
uri = URI("#{@base_url}/#{path}")
|
|
41
|
-
response = start_http(uri) { |http| http.request(build_request(uri, body)) }
|
|
42
|
-
handle_api_error!(response) unless response.is_a?(Net::HTTPSuccess)
|
|
43
|
-
JSON.parse(response.body, symbolize_names: true)
|
|
44
|
-
end
|
|
45
11
|
|
|
46
|
-
# Raises Riffer::Error on a non-success status.
|
|
47
12
|
#--
|
|
48
|
-
#: (String, Hash[
|
|
49
|
-
def
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
handle_api_error!(response) unless response.is_a?(Net::HTTPSuccess)
|
|
54
|
-
|
|
55
|
-
begin
|
|
56
|
-
response.read_body(&block)
|
|
57
|
-
rescue IOError
|
|
58
|
-
# A pre-buffered body (VCR/WebMock playback) raises IOError on a
|
|
59
|
-
# streaming read; hand over the full body instead.
|
|
60
|
-
yield(response.body)
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
13
|
+
#: (api_key: String, ?base_url: String, ?headers: Hash[String, String], ?connection: Faraday::Connection?) -> void
|
|
14
|
+
def initialize(api_key:, base_url: DEFAULT_BASE_URL, headers: {}, connection: nil)
|
|
15
|
+
super(headers: headers, connection: connection)
|
|
16
|
+
@api_key = api_key
|
|
17
|
+
@base_url = base_url.chomp("/")
|
|
64
18
|
end
|
|
65
19
|
|
|
66
20
|
private
|
|
67
21
|
|
|
68
22
|
#--
|
|
69
|
-
#: (
|
|
70
|
-
def
|
|
71
|
-
|
|
72
|
-
request["Content-Type"] = "application/json"
|
|
73
|
-
request["x-goog-api-key"] = @api_key
|
|
74
|
-
request.body = body.to_json
|
|
75
|
-
request
|
|
23
|
+
#: (String) -> String
|
|
24
|
+
def url_for(path)
|
|
25
|
+
"#{@base_url}/#{path}"
|
|
76
26
|
end
|
|
77
27
|
|
|
78
28
|
#--
|
|
79
|
-
#:
|
|
80
|
-
def
|
|
81
|
-
|
|
82
|
-
options = {
|
|
83
|
-
use_ssl: uri.scheme == "https",
|
|
84
|
-
open_timeout: @open_timeout,
|
|
85
|
-
read_timeout: @read_timeout,
|
|
86
|
-
} #: Hash[Symbol, untyped]
|
|
87
|
-
options[:write_timeout] = @write_timeout if @write_timeout
|
|
88
|
-
|
|
89
|
-
if @proxy_address
|
|
90
|
-
Net::HTTP.start(host, uri.port, @proxy_address, @proxy_port, nil, nil, **options, &)
|
|
91
|
-
else
|
|
92
|
-
Net::HTTP.start(host, uri.port, **options, &)
|
|
93
|
-
end
|
|
29
|
+
#: () -> Hash[String, String]
|
|
30
|
+
def headers
|
|
31
|
+
{ "x-goog-api-key" => @api_key }
|
|
94
32
|
end
|
|
95
33
|
|
|
96
34
|
#--
|
|
97
|
-
#: (
|
|
98
|
-
def
|
|
99
|
-
|
|
100
|
-
JSON.parse(response.body, symbolize_names: true)
|
|
101
|
-
rescue JSON::ParserError
|
|
102
|
-
{ message: response.body }
|
|
103
|
-
end
|
|
104
|
-
error_message = parsed.dig(:error, :message) || parsed[:message] || response.body
|
|
105
|
-
raise Riffer::Error, "Gemini API error (#{response.code}): #{error_message}"
|
|
35
|
+
#: () -> String
|
|
36
|
+
def error_label
|
|
37
|
+
"Gemini"
|
|
106
38
|
end
|
|
107
39
|
end
|