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
|
@@ -1,477 +1,44 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
# rbs_inline: enabled
|
|
3
3
|
|
|
4
|
-
class Riffer::Providers::Anthropic < Riffer::Providers::
|
|
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
|
-
|
|
4
|
+
class Riffer::Providers::Anthropic < Riffer::Providers::AnthropicCompatible::MessagesAPI
|
|
30
5
|
#--
|
|
31
6
|
#: () -> String
|
|
32
7
|
def self.semconv_provider_name
|
|
33
8
|
"anthropic"
|
|
34
9
|
end
|
|
35
10
|
|
|
36
|
-
#--
|
|
37
|
-
#: () -> void
|
|
38
|
-
def initialize
|
|
39
|
-
super
|
|
40
|
-
depends_on "anthropic"
|
|
41
|
-
end
|
|
42
|
-
|
|
43
11
|
private
|
|
44
12
|
|
|
45
13
|
#--
|
|
46
14
|
#: () -> untyped
|
|
47
|
-
def
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def build_client
|
|
54
|
-
# Never pass an explicit nil credential, so the SDK's own
|
|
55
|
-
# ANTHROPIC_API_KEY resolution stays reachable.
|
|
56
|
-
::Anthropic::Client.new(**{ api_key: Riffer.config.anthropic.api_key }.compact)
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
#--
|
|
60
|
-
#: (Symbol) -> Hash[Symbol, untyped]
|
|
61
|
-
def default_reasoning_options(level)
|
|
62
|
-
return { thinking: { type: "disabled" } } if level == :off
|
|
63
|
-
|
|
64
|
-
{ thinking: { type: "adaptive" }, output_config: { effort: level.to_s } }
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
#--
|
|
68
|
-
#: (Array[Riffer::Messages::Base], String?, Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
|
|
69
|
-
def build_request_params(messages, model, options)
|
|
70
|
-
partitioned_messages = partition_messages(messages)
|
|
71
|
-
tools = options[:tools]
|
|
72
|
-
structured_output = options[:structured_output]
|
|
73
|
-
web_search = options[:web_search]
|
|
74
|
-
tags = options[:tags] || {}
|
|
75
|
-
|
|
76
|
-
max_tokens = options.fetch(:max_tokens, 4096)
|
|
77
|
-
|
|
78
|
-
params = {
|
|
79
|
-
model: model,
|
|
80
|
-
messages: partitioned_messages[:conversation],
|
|
81
|
-
max_tokens: max_tokens,
|
|
82
|
-
**options.except(:tools, :max_tokens, :structured_output, :web_search, :tags),
|
|
83
|
-
} #: Hash[Symbol, untyped]
|
|
84
|
-
|
|
85
|
-
params[:system] = partitioned_messages[:system] if partitioned_messages[:system]
|
|
86
|
-
|
|
87
|
-
# Anthropic's only request-metadata field is metadata.user_id (opaque, no
|
|
88
|
-
# PII); all other tags survive only on spans.
|
|
89
|
-
user_id = tags["user_id"]
|
|
90
|
-
params[:metadata] = { user_id: user_id } if user_id
|
|
91
|
-
|
|
92
|
-
anthropic_tools = [] #: Array[Hash[Symbol, untyped]]
|
|
93
|
-
anthropic_tools.concat(tools.map { |t| convert_tool_to_anthropic_format(t) }) if tools && !tools.empty?
|
|
94
|
-
|
|
95
|
-
if web_search
|
|
96
|
-
web_search_tool = { type: WEB_SEARCH_TOOL_TYPE, name: "web_search" }
|
|
97
|
-
web_search_tool.merge!(web_search) if web_search.is_a?(Hash)
|
|
98
|
-
anthropic_tools << web_search_tool
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
if structured_output
|
|
102
|
-
# Strict schema makes optional fields nullable; otherwise Anthropic may
|
|
103
|
-
# return empty strings or whitespace instead of null. The format wins
|
|
104
|
-
# over caller output_config keys because the run loop validates against it.
|
|
105
|
-
params[:output_config] = {
|
|
106
|
-
**(params[:output_config] || {}),
|
|
107
|
-
format: {
|
|
108
|
-
type: "json_schema",
|
|
109
|
-
schema: structured_output.json_schema(strict: true),
|
|
110
|
-
},
|
|
111
|
-
}
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
params[:tools] = anthropic_tools unless anthropic_tools.empty?
|
|
115
|
-
|
|
116
|
-
params
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
#--
|
|
120
|
-
#: (Hash[Symbol, untyped]) -> untyped
|
|
121
|
-
def execute_generate(params)
|
|
122
|
-
client.messages.create(**params)
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
#--
|
|
126
|
-
#: (untyped) -> Riffer::Providers::TokenUsage?
|
|
127
|
-
def extract_token_usage(response)
|
|
128
|
-
message = response #: Anthropic::Models::Message
|
|
129
|
-
build_token_usage(message.usage)
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
#--
|
|
133
|
-
#: (untyped) -> Riffer::Providers::FinishReason?
|
|
134
|
-
def extract_finish_reason(response)
|
|
135
|
-
message = response #: Anthropic::Models::Message
|
|
136
|
-
build_finish_reason(message.stop_reason)
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
#--
|
|
140
|
-
#: (untyped) -> Riffer::Providers::FinishReason?
|
|
141
|
-
def build_finish_reason(stop_reason)
|
|
142
|
-
return nil unless stop_reason
|
|
143
|
-
|
|
144
|
-
raw = stop_reason.to_s
|
|
145
|
-
Riffer::Providers::FinishReason.new(reason: FINISH_REASONS.fetch(raw, :other), raw: raw)
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
#--
|
|
149
|
-
#: (untyped) -> Riffer::Providers::TokenUsage
|
|
150
|
-
def build_token_usage(usage)
|
|
151
|
-
cache_write = usage.cache_creation_input_tokens
|
|
152
|
-
cache_read = usage.cache_read_input_tokens
|
|
153
|
-
|
|
154
|
-
apply_pricing(
|
|
155
|
-
Riffer::Providers::TokenUsage.new(
|
|
156
|
-
# Anthropic's input_tokens excludes the cache buckets; TokenUsage's includes them.
|
|
157
|
-
input_tokens: usage.input_tokens + (cache_write || 0) + (cache_read || 0),
|
|
158
|
-
output_tokens: usage.output_tokens,
|
|
159
|
-
cache_write_tokens: cache_write,
|
|
160
|
-
cache_read_tokens: cache_read,
|
|
161
|
-
),
|
|
162
|
-
)
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
#--
|
|
166
|
-
#: (untyped) -> String
|
|
167
|
-
def extract_content(response)
|
|
168
|
-
message = response #: Anthropic::Models::Message
|
|
169
|
-
(message.content || []).filter_map { |block| block.text if block.is_a?(::Anthropic::Models::TextBlock) }.join
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
#--
|
|
173
|
-
#: (untyped) -> Array[Riffer::Messages::Assistant::ToolCall]
|
|
174
|
-
def extract_tool_calls(response)
|
|
175
|
-
message = response #: Anthropic::Models::Message
|
|
176
|
-
content_blocks = message.content
|
|
177
|
-
return [] if content_blocks.nil? || content_blocks.empty?
|
|
178
|
-
|
|
179
|
-
tool_calls = [] #: Array[Riffer::Messages::Assistant::ToolCall]
|
|
180
|
-
|
|
181
|
-
content_blocks.each do |block|
|
|
182
|
-
next unless block.is_a?(::Anthropic::Models::ToolUseBlock)
|
|
183
|
-
|
|
184
|
-
tool_calls << Riffer::Messages::Assistant::ToolCall.new(
|
|
185
|
-
call_id: block.id,
|
|
186
|
-
name: decode_tool_name(block.name, tools: @current_tools),
|
|
187
|
-
arguments: block.input.to_json,
|
|
188
|
-
)
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
tool_calls
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
#--
|
|
195
|
-
#: (untyped) -> Array[Riffer::Messages::Assistant::ReasoningPart]
|
|
196
|
-
def extract_reasoning(response)
|
|
197
|
-
message = response #: Anthropic::Models::Message
|
|
198
|
-
(message.content || []).filter_map { |block| build_reasoning_part(block) }
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
#--
|
|
202
|
-
#: (untyped) -> Riffer::Messages::Assistant::ReasoningPart?
|
|
203
|
-
def build_reasoning_part(block)
|
|
204
|
-
case block
|
|
205
|
-
when ::Anthropic::Models::ThinkingBlock
|
|
206
|
-
Riffer::Messages::Assistant::ReasoningPart.new(
|
|
207
|
-
type: :text,
|
|
208
|
-
text: block.thinking,
|
|
209
|
-
signature: block.signature,
|
|
210
|
-
format: REASONING_FORMAT,
|
|
211
|
-
)
|
|
212
|
-
when ::Anthropic::Models::RedactedThinkingBlock
|
|
213
|
-
Riffer::Messages::Assistant::ReasoningPart.new(type: :encrypted, data: block.data, format: REASONING_FORMAT)
|
|
15
|
+
def client
|
|
16
|
+
resolved = super
|
|
17
|
+
if defined?(::Anthropic::Client) && resolved.is_a?(::Anthropic::Client) # steep:ignore UnknownConstant
|
|
18
|
+
raise Riffer::ArgumentError,
|
|
19
|
+
"config.anthropic.client now takes a Riffer::Providers::Anthropic::Client; pass timeouts, retries " \
|
|
20
|
+
"and proxies through its connection: (see the Anthropic provider docs)"
|
|
214
21
|
end
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
#--
|
|
218
|
-
#: (Hash[Symbol, untyped], Riffer::Providers::_EventSink) -> void
|
|
219
|
-
def execute_stream(params, yielder)
|
|
220
|
-
current_state = {
|
|
221
|
-
text: nil,
|
|
222
|
-
tool_call: nil,
|
|
223
|
-
web_search_index: nil,
|
|
224
|
-
web_search_json: nil,
|
|
225
|
-
web_search_query: nil,
|
|
226
|
-
} #: Hash[Symbol, untyped]
|
|
227
|
-
|
|
228
|
-
stream_completed = false
|
|
229
|
-
|
|
230
|
-
# Workaround for anthropics/anthropic-sdk-ruby#182: force identity
|
|
231
|
-
# encoding so Net::HTTP/Zlib doesn't buffer SSE chunks until EOF.
|
|
232
|
-
stream = client.messages.stream(
|
|
233
|
-
**params,
|
|
234
|
-
request_options: { extra_headers: { "accept-encoding" => "identity" } },
|
|
235
|
-
)
|
|
236
|
-
|
|
237
|
-
begin
|
|
238
|
-
stream.each do |event|
|
|
239
|
-
case event
|
|
240
|
-
when ::Anthropic::Models::RawContentBlockStartEvent
|
|
241
|
-
handle_raw_content_block_start(event, state: current_state)
|
|
242
|
-
when ::Anthropic::Models::RawContentBlockDeltaEvent
|
|
243
|
-
handle_raw_content_block_delta(event, state: current_state)
|
|
244
|
-
when ::Anthropic::Helpers::Streaming::TextEvent
|
|
245
|
-
handle_text_event(event, state: current_state, yielder: yielder)
|
|
246
|
-
when ::Anthropic::Helpers::Streaming::ThinkingEvent
|
|
247
|
-
yielder << Riffer::StreamEvents::ReasoningDelta.new(event.thinking)
|
|
248
|
-
when ::Anthropic::Helpers::Streaming::InputJsonEvent
|
|
249
|
-
handle_input_json_event(event, state: current_state, yielder: yielder)
|
|
250
|
-
when ::Anthropic::Helpers::Streaming::ContentBlockStopEvent
|
|
251
|
-
case event.content_block
|
|
252
|
-
when ::Anthropic::Models::ToolUseBlock
|
|
253
|
-
handle_content_block_stop_tool_use(event, state: current_state, yielder: yielder)
|
|
254
|
-
when ::Anthropic::Models::ThinkingBlock, ::Anthropic::Models::RedactedThinkingBlock
|
|
255
|
-
handle_content_block_stop_reasoning(event, yielder: yielder)
|
|
256
|
-
when ::Anthropic::Models::ServerToolUseBlock
|
|
257
|
-
handle_content_block_stop_server_tool_use(event, state: current_state, yielder: yielder)
|
|
258
|
-
when ::Anthropic::Models::WebSearchToolResultBlock
|
|
259
|
-
handle_content_block_stop_web_search_result(event, state: current_state, yielder: yielder)
|
|
260
|
-
end
|
|
261
|
-
when ::Anthropic::Helpers::Streaming::MessageStopEvent
|
|
262
|
-
stream_completed = true
|
|
263
|
-
handle_message_stop(
|
|
264
|
-
event, state: current_state, accumulated_message: stream.accumulated_message, yielder: yielder,
|
|
265
|
-
)
|
|
266
|
-
end
|
|
267
|
-
end
|
|
268
|
-
ensure
|
|
269
|
-
# Anthropic SDK does not auto-close the underlying HTTP stream when
|
|
270
|
-
# iteration is interrupted (raise / fiber cancellation), so the SSE
|
|
271
|
-
# socket leaks until GC. close is idempotent and a no-op after EOF.
|
|
272
|
-
stream.close
|
|
273
|
-
end
|
|
274
|
-
|
|
275
|
-
return if stream_completed
|
|
276
|
-
|
|
277
|
-
raise Riffer::IncompleteStreamError, "Anthropic stream ended without a message_stop event"
|
|
278
|
-
end
|
|
279
|
-
|
|
280
|
-
#--
|
|
281
|
-
#: (untyped, state: Hash[Symbol, untyped]) -> void
|
|
282
|
-
def handle_raw_content_block_start(event, state:)
|
|
283
|
-
content_block = event.content_block
|
|
284
|
-
return unless content_block.type.to_s == "server_tool_use" && content_block.name.to_s == "web_search"
|
|
285
|
-
|
|
286
|
-
state[:web_search_index] = event.index
|
|
287
|
-
state[:web_search_json] = +""
|
|
288
|
-
end
|
|
289
|
-
|
|
290
|
-
#--
|
|
291
|
-
#: (untyped, state: Hash[Symbol, untyped]) -> void
|
|
292
|
-
def handle_raw_content_block_delta(event, state:)
|
|
293
|
-
return unless state[:web_search_index] == event.index
|
|
294
|
-
|
|
295
|
-
delta = event.delta
|
|
296
|
-
state[:web_search_json] << delta.partial_json if delta.respond_to?(:partial_json)
|
|
297
|
-
end
|
|
298
|
-
|
|
299
|
-
#--
|
|
300
|
-
#: (untyped, state: Hash[Symbol, untyped], yielder: Riffer::Providers::_EventSink) -> void
|
|
301
|
-
def handle_text_event(event, state:, yielder:)
|
|
302
|
-
# Mutating append avoids O(n^2) copying across the response; the buffer
|
|
303
|
-
# reaches TextDone only at message stop, so no reader sees it mid-append.
|
|
304
|
-
state[:text] ||= +""
|
|
305
|
-
state[:text] << event.text
|
|
306
|
-
yielder << Riffer::StreamEvents::TextDelta.new(event.text)
|
|
307
|
-
end
|
|
308
|
-
|
|
309
|
-
#--
|
|
310
|
-
#: (untyped, state: Hash[Symbol, untyped], yielder: Riffer::Providers::_EventSink) -> void
|
|
311
|
-
def handle_input_json_event(event, state:, yielder:)
|
|
312
|
-
# server_tool_use (web_search) input streams through the raw-event path
|
|
313
|
-
# (handle_raw_content_block_delta) and must not surface as tool-call deltas.
|
|
314
|
-
return if state[:web_search_json]
|
|
315
|
-
|
|
316
|
-
state[:tool_call] = { id: nil, name: nil, arguments: +"" } if state[:tool_call].nil?
|
|
317
|
-
state[:tool_call][:arguments] << event.partial_json
|
|
318
|
-
yielder << Riffer::StreamEvents::ToolCallDelta.new(
|
|
319
|
-
item_id: state[:tool_call][:id] || "pending",
|
|
320
|
-
name: state[:tool_call][:name],
|
|
321
|
-
arguments_delta: event.partial_json,
|
|
322
|
-
)
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
#--
|
|
326
|
-
#: (untyped, state: Hash[Symbol, untyped], yielder: Riffer::Providers::_EventSink) -> void
|
|
327
|
-
def handle_content_block_stop_tool_use(event, state:, yielder:)
|
|
328
|
-
content_block = event.content_block
|
|
329
|
-
arguments = content_block.input.is_a?(String) ? content_block.input : content_block.input.to_json
|
|
330
|
-
yielder << Riffer::StreamEvents::ToolCallDone.new(
|
|
331
|
-
item_id: content_block.id,
|
|
332
|
-
call_id: content_block.id,
|
|
333
|
-
name: decode_tool_name(content_block.name, tools: @current_tools),
|
|
334
|
-
arguments: arguments,
|
|
335
|
-
)
|
|
336
|
-
state[:tool_call] = nil
|
|
337
|
-
end
|
|
338
|
-
|
|
339
|
-
# The SDK's accumulated block already carries the full thinking text and the
|
|
340
|
-
# signature from its trailing signature_delta.
|
|
341
|
-
#--
|
|
342
|
-
#: (untyped, yielder: Riffer::Providers::_EventSink) -> void
|
|
343
|
-
def handle_content_block_stop_reasoning(event, yielder:)
|
|
344
|
-
part = build_reasoning_part(event.content_block) #: Riffer::Messages::Assistant::ReasoningPart
|
|
345
|
-
yielder << Riffer::StreamEvents::ReasoningDone.new(part)
|
|
346
|
-
end
|
|
347
|
-
|
|
348
|
-
#--
|
|
349
|
-
#: (untyped, state: Hash[Symbol, untyped], yielder: Riffer::Providers::_EventSink) -> void
|
|
350
|
-
def handle_content_block_stop_server_tool_use(_event, state:, yielder:)
|
|
351
|
-
return unless state[:web_search_json]
|
|
352
22
|
|
|
353
|
-
|
|
354
|
-
state[:web_search_query] = input[:query]
|
|
355
|
-
state[:web_search_index] = nil
|
|
356
|
-
state[:web_search_json] = nil
|
|
357
|
-
yielder << Riffer::StreamEvents::WebSearchStatus.new("searching", query: input[:query])
|
|
23
|
+
resolved
|
|
358
24
|
end
|
|
359
25
|
|
|
360
26
|
#--
|
|
361
|
-
#: (
|
|
362
|
-
def
|
|
363
|
-
|
|
364
|
-
sources = (content_block.content || []).filter_map do |item|
|
|
365
|
-
next unless item.type.to_s == "web_search_result"
|
|
366
|
-
|
|
367
|
-
{ title: item.title, url: item.url }
|
|
368
|
-
end
|
|
369
|
-
|
|
370
|
-
yielder << Riffer::StreamEvents::WebSearchDone.new(state[:web_search_query] || "", sources: sources)
|
|
371
|
-
state[:web_search_query] = nil
|
|
372
|
-
end
|
|
373
|
-
|
|
374
|
-
#--
|
|
375
|
-
#: (untyped, state: Hash[Symbol, untyped], accumulated_message: untyped, yielder: Riffer::Providers::_EventSink) -> void
|
|
376
|
-
def handle_message_stop(_event, state:, accumulated_message:, yielder:)
|
|
377
|
-
yielder << Riffer::StreamEvents::TextDone.new(state[:text]) if state[:text]
|
|
378
|
-
|
|
379
|
-
message = accumulated_message #: Anthropic::Models::Message?
|
|
380
|
-
yield_finish_reason(yielder, build_finish_reason(message&.stop_reason))
|
|
381
|
-
|
|
382
|
-
usage = message&.usage
|
|
383
|
-
return unless usage
|
|
384
|
-
|
|
385
|
-
yielder << Riffer::StreamEvents::TokenUsageDone.new(token_usage: build_token_usage(usage))
|
|
386
|
-
end
|
|
387
|
-
|
|
388
|
-
#--
|
|
389
|
-
#: (Array[Riffer::Messages::Base]) -> Hash[Symbol, untyped]
|
|
390
|
-
def partition_messages(messages)
|
|
391
|
-
system_prompts = [] #: Array[Hash[Symbol, untyped]]
|
|
392
|
-
conversation_messages = [] #: Array[Hash[Symbol, untyped]]
|
|
393
|
-
|
|
394
|
-
messages.each do |message|
|
|
395
|
-
case message
|
|
396
|
-
when Riffer::Messages::System
|
|
397
|
-
system_prompts << { type: "text", text: message.content }
|
|
398
|
-
when Riffer::Messages::User
|
|
399
|
-
if message.files.empty?
|
|
400
|
-
conversation_messages << { role: "user", content: message.content }
|
|
401
|
-
else
|
|
402
|
-
content = [{ type: "text", text: message.content }]
|
|
403
|
-
message.files.each { |file| content << convert_file_part_to_anthropic_format(file) }
|
|
404
|
-
conversation_messages << { role: "user", content: content }
|
|
405
|
-
end
|
|
406
|
-
when Riffer::Messages::Assistant
|
|
407
|
-
conversation_messages << convert_assistant_to_anthropic_format(message)
|
|
408
|
-
when Riffer::Messages::Tool
|
|
409
|
-
conversation_messages << {
|
|
410
|
-
role: "user",
|
|
411
|
-
content: [{
|
|
412
|
-
type: "tool_result",
|
|
413
|
-
tool_use_id: message.tool_call_id,
|
|
414
|
-
content: message.content,
|
|
415
|
-
}],
|
|
416
|
-
}
|
|
417
|
-
end
|
|
418
|
-
end
|
|
419
|
-
|
|
420
|
-
{
|
|
421
|
-
system: system_prompts.empty? ? nil : system_prompts,
|
|
422
|
-
conversation: conversation_messages,
|
|
423
|
-
}
|
|
27
|
+
#: () -> untyped
|
|
28
|
+
def global_client
|
|
29
|
+
Riffer.config.anthropic.client
|
|
424
30
|
end
|
|
425
31
|
|
|
426
32
|
#--
|
|
427
|
-
#: (
|
|
428
|
-
def
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
message.tool_calls.each do |tc|
|
|
435
|
-
content << {
|
|
436
|
-
type: "tool_use",
|
|
437
|
-
id: tc.call_id,
|
|
438
|
-
name: encode_tool_name(tc.name),
|
|
439
|
-
input: parse_tool_arguments(tc.arguments),
|
|
440
|
-
}
|
|
33
|
+
#: () -> Riffer::Providers::Anthropic::Client
|
|
34
|
+
def build_client
|
|
35
|
+
config = Riffer.config.anthropic
|
|
36
|
+
api_key = config.api_key || ENV.fetch("ANTHROPIC_API_KEY", nil)
|
|
37
|
+
unless api_key
|
|
38
|
+
raise Riffer::ArgumentError, "Anthropic requires an API key: set config.anthropic.api_key or ANTHROPIC_API_KEY"
|
|
441
39
|
end
|
|
442
40
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
#--
|
|
447
|
-
#: (Riffer::Messages::Assistant::ReasoningPart) -> Hash[Symbol, untyped]
|
|
448
|
-
def convert_reasoning_part_to_anthropic_format(part)
|
|
449
|
-
return { type: "redacted_thinking", data: part.data } if part.type == :encrypted
|
|
450
|
-
|
|
451
|
-
{ type: "thinking", thinking: part.text.to_s, signature: part.signature }
|
|
452
|
-
end
|
|
453
|
-
|
|
454
|
-
#--
|
|
455
|
-
#: (Riffer::Messages::User::FilePart) -> Hash[Symbol, untyped]
|
|
456
|
-
def convert_file_part_to_anthropic_format(file)
|
|
457
|
-
type = file.image? ? "image" : "document"
|
|
458
|
-
|
|
459
|
-
source = if file.url?
|
|
460
|
-
{ type: "url", url: file.url }
|
|
461
|
-
else
|
|
462
|
-
{ type: "base64", media_type: file.media_type, data: file.data }
|
|
463
|
-
end
|
|
464
|
-
|
|
465
|
-
{ type: type, source: source }
|
|
466
|
-
end
|
|
467
|
-
|
|
468
|
-
#--
|
|
469
|
-
#: (singleton(Riffer::Tool)) -> Hash[Symbol, untyped]
|
|
470
|
-
def convert_tool_to_anthropic_format(tool)
|
|
471
|
-
{
|
|
472
|
-
name: encode_tool_name(tool.name),
|
|
473
|
-
description: tool.description,
|
|
474
|
-
input_schema: tool.parameters_schema(strict: true),
|
|
475
|
-
}
|
|
41
|
+
base_url = config.base_url || ENV.fetch("ANTHROPIC_BASE_URL", nil) || Client::DEFAULT_BASE_URL
|
|
42
|
+
Client.new(api_key: api_key, base_url: base_url)
|
|
476
43
|
end
|
|
477
44
|
end
|
|
@@ -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
|