riffer 0.54.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 +6 -6
- data/.release-please-manifest.json +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +28 -0
- data/README.md +24 -0
- data/Steepfile +2 -2
- data/docs/AGENTS.md +37 -12
- data/docs/CATALOG.md +8 -8
- data/docs/CONFIGURATION.md +47 -16
- data/docs/MCP.md +3 -4
- data/docs/STREAM_EVENTS.md +1 -1
- data/docs/TOOLS.md +11 -11
- 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 -26
- 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 +6 -1
- 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 +37 -9
- data/lib/riffer/params/param.rb +35 -14
- data/lib/riffer/params.rb +34 -19
- 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 +4 -7
- data/lib/riffer/providers/gemini/client.rb +17 -85
- data/lib/riffer/providers/gemini.rb +213 -81
- 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 +24 -4
- data/sig/_private/faraday.rbs +8 -0
- data/sig/_private/googleauth.rbs +7 -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 +13 -5
- data/sig/generated/riffer/params/param.rbs +10 -5
- data/sig/generated/riffer/params.rbs +8 -4
- 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/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 +61 -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,195 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
class Riffer::Providers::AnthropicCompatible::MessagesAPI::Stream
|
|
7
|
+
# @rbs @tools: Array[singleton(Riffer::Tool)]
|
|
8
|
+
# @rbs @message: Hash[Symbol, untyped]
|
|
9
|
+
# @rbs @partial_json: Hash[Integer, String]
|
|
10
|
+
# @rbs @web_search_query: String?
|
|
11
|
+
# @rbs @completed: bool
|
|
12
|
+
|
|
13
|
+
#--
|
|
14
|
+
#: (Hash[Symbol, untyped]) -> Riffer::Messages::Assistant::ReasoningPart?
|
|
15
|
+
def self.reasoning_part(block)
|
|
16
|
+
format = Riffer::Providers::AnthropicCompatible::MessagesAPI::REASONING_FORMAT
|
|
17
|
+
|
|
18
|
+
case block[:type]
|
|
19
|
+
when "thinking"
|
|
20
|
+
Riffer::Messages::Assistant::ReasoningPart.new(
|
|
21
|
+
type: :text,
|
|
22
|
+
text: block[:thinking],
|
|
23
|
+
signature: block[:signature],
|
|
24
|
+
format: format,
|
|
25
|
+
)
|
|
26
|
+
when "redacted_thinking"
|
|
27
|
+
Riffer::Messages::Assistant::ReasoningPart.new(type: :encrypted, data: block[:data], format: format)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
#--
|
|
32
|
+
#: (tools: Array[singleton(Riffer::Tool)]) -> void
|
|
33
|
+
def initialize(tools:)
|
|
34
|
+
@tools = tools
|
|
35
|
+
@message = { content: [] }
|
|
36
|
+
@partial_json = {}
|
|
37
|
+
@web_search_query = nil
|
|
38
|
+
@completed = false
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Raises Riffer::APIError on a stream +error+ event.
|
|
42
|
+
#--
|
|
43
|
+
#: (Hash[Symbol, untyped], Riffer::Providers::_EventSink) -> void
|
|
44
|
+
def handle(event, yielder)
|
|
45
|
+
case event[:type]
|
|
46
|
+
when "message_start" then start_message(event)
|
|
47
|
+
when "content_block_start" then start_block(event)
|
|
48
|
+
when "content_block_delta" then apply_delta(event, yielder)
|
|
49
|
+
when "content_block_stop" then stop_block(event, yielder)
|
|
50
|
+
when "message_delta" then apply_message_delta(event)
|
|
51
|
+
when "message_stop" then stop_message(yielder)
|
|
52
|
+
when "error" then raise_stream_error(event)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The caller reads finish reason and usage from here so it can price the usage.
|
|
57
|
+
#--
|
|
58
|
+
#: () -> Hash[Symbol, untyped]
|
|
59
|
+
def finish!
|
|
60
|
+
raise Riffer::IncompleteStreamError, "Anthropic stream ended without a message_stop event" unless @completed
|
|
61
|
+
|
|
62
|
+
@message
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
#--
|
|
68
|
+
#: (Hash[Symbol, untyped]) -> void
|
|
69
|
+
def start_message(event)
|
|
70
|
+
content = [] #: Array[Hash[Symbol, untyped]]
|
|
71
|
+
@message = event[:message].merge(content: content)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
#--
|
|
75
|
+
#: (Hash[Symbol, untyped]) -> void
|
|
76
|
+
def start_block(event)
|
|
77
|
+
index = event[:index]
|
|
78
|
+
block = event[:content_block].dup
|
|
79
|
+
@message[:content][index] = block
|
|
80
|
+
|
|
81
|
+
case block[:type]
|
|
82
|
+
when "text" then block[:text] = +block[:text].to_s
|
|
83
|
+
when "thinking"
|
|
84
|
+
block[:thinking] = +block[:thinking].to_s
|
|
85
|
+
block[:signature] = block[:signature].to_s
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
#--
|
|
90
|
+
#: (Hash[Symbol, untyped], Riffer::Providers::_EventSink) -> void
|
|
91
|
+
def apply_delta(event, yielder)
|
|
92
|
+
index = event[:index]
|
|
93
|
+
block = @message[:content][index]
|
|
94
|
+
delta = event[:delta]
|
|
95
|
+
|
|
96
|
+
case delta[:type]
|
|
97
|
+
when "text_delta"
|
|
98
|
+
block[:text] << delta[:text]
|
|
99
|
+
yielder << Riffer::StreamEvents::TextDelta.new(delta[:text])
|
|
100
|
+
when "input_json_delta"
|
|
101
|
+
(@partial_json[index] ||= +"") << delta[:partial_json]
|
|
102
|
+
# server_tool_use input (web search) is not a riffer tool call.
|
|
103
|
+
return unless block[:type] == "tool_use"
|
|
104
|
+
|
|
105
|
+
yielder << Riffer::StreamEvents::ToolCallDelta.new(
|
|
106
|
+
item_id: block[:id],
|
|
107
|
+
name: Riffer::Wire.decode_tool_name(block[:name], tools: @tools),
|
|
108
|
+
arguments_delta: delta[:partial_json],
|
|
109
|
+
)
|
|
110
|
+
when "thinking_delta"
|
|
111
|
+
block[:thinking] << delta[:thinking]
|
|
112
|
+
yielder << Riffer::StreamEvents::ReasoningDelta.new(delta[:thinking])
|
|
113
|
+
when "signature_delta"
|
|
114
|
+
block[:signature] = delta[:signature]
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
#--
|
|
119
|
+
#: (Hash[Symbol, untyped], Riffer::Providers::_EventSink) -> void
|
|
120
|
+
def stop_block(event, yielder)
|
|
121
|
+
index = event[:index]
|
|
122
|
+
block = @message[:content][index]
|
|
123
|
+
|
|
124
|
+
case block[:type]
|
|
125
|
+
when "tool_use"
|
|
126
|
+
raw = @partial_json.delete(index).to_s
|
|
127
|
+
input = parse_input(raw)
|
|
128
|
+
block[:input] = input if input
|
|
129
|
+
yielder << Riffer::StreamEvents::ToolCallDone.new(
|
|
130
|
+
item_id: block[:id],
|
|
131
|
+
call_id: block[:id],
|
|
132
|
+
name: Riffer::Wire.decode_tool_name(block[:name], tools: @tools),
|
|
133
|
+
arguments: input ? input.to_json : raw,
|
|
134
|
+
)
|
|
135
|
+
when "thinking", "redacted_thinking"
|
|
136
|
+
part = self.class.reasoning_part(block) #: Riffer::Messages::Assistant::ReasoningPart
|
|
137
|
+
yielder << Riffer::StreamEvents::ReasoningDone.new(part)
|
|
138
|
+
when "server_tool_use"
|
|
139
|
+
block[:input] = parse_input(@partial_json.delete(index).to_s) || {}
|
|
140
|
+
return unless block[:name] == "web_search"
|
|
141
|
+
|
|
142
|
+
@web_search_query = block[:input]["query"]
|
|
143
|
+
yielder << Riffer::StreamEvents::WebSearchStatus.new("searching", query: @web_search_query)
|
|
144
|
+
when "web_search_tool_result"
|
|
145
|
+
yielder << Riffer::StreamEvents::WebSearchDone.new(@web_search_query || "", sources: web_search_sources(block))
|
|
146
|
+
@web_search_query = nil
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
#--
|
|
151
|
+
#: (Hash[Symbol, untyped]) -> void
|
|
152
|
+
def apply_message_delta(event)
|
|
153
|
+
@message.merge!(event[:delta] || {})
|
|
154
|
+
usage = event[:usage]
|
|
155
|
+
# Usage counts are cumulative; a field the delta omits keeps its start value.
|
|
156
|
+
@message[:usage] = (@message[:usage] || {}).merge(usage.compact) if usage
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
#--
|
|
160
|
+
#: (Riffer::Providers::_EventSink) -> void
|
|
161
|
+
def stop_message(yielder)
|
|
162
|
+
@completed = true
|
|
163
|
+
text = @message[:content].filter_map { |block| block[:text] if block[:type] == "text" }.join
|
|
164
|
+
yielder << Riffer::StreamEvents::TextDone.new(text) unless text.empty?
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
#--
|
|
168
|
+
#: (Hash[Symbol, untyped]) -> void
|
|
169
|
+
def raise_stream_error(event)
|
|
170
|
+
error = event[:error] || {}
|
|
171
|
+
raise Riffer::APIError.new("Anthropic stream error (#{error[:type]}): #{error[:message]}", body: event.to_json)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# A response cut off by max_tokens can end a block mid-input, leaving JSON
|
|
175
|
+
# that does not parse.
|
|
176
|
+
#--
|
|
177
|
+
#: (String) -> Hash[String, untyped]?
|
|
178
|
+
def parse_input(raw)
|
|
179
|
+
Riffer::Wire.parse_tool_arguments(raw)
|
|
180
|
+
rescue JSON::ParserError
|
|
181
|
+
nil
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
#--
|
|
185
|
+
#: (Hash[Symbol, untyped]) -> Array[Hash[Symbol, String?]]
|
|
186
|
+
def web_search_sources(block)
|
|
187
|
+
content = block[:content]
|
|
188
|
+
# An error result carries an object, not a list, as its content.
|
|
189
|
+
return [] unless content.is_a?(Array)
|
|
190
|
+
|
|
191
|
+
content.filter_map do |item|
|
|
192
|
+
{ title: item[:title], url: item[:url] } if item[:type] == "web_search_result"
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
@@ -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)
|
|
@@ -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
|
#--
|