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
|
@@ -428,7 +428,8 @@ class Riffer::Providers::AmazonBedrock < Riffer::Providers::Base
|
|
|
428
428
|
item_id: tool_call[:id],
|
|
429
429
|
call_id: tool_call[:id],
|
|
430
430
|
name: tool_call[:name],
|
|
431
|
-
|
|
431
|
+
# Converse streams no input delta for a call without arguments.
|
|
432
|
+
arguments: tool_call[:arguments].empty? ? "{}" : tool_call[:arguments],
|
|
432
433
|
)
|
|
433
434
|
state[:tool_call] = nil
|
|
434
435
|
end
|
|
@@ -445,6 +446,7 @@ class Riffer::Providers::AmazonBedrock < Riffer::Providers::Base
|
|
|
445
446
|
def partition_messages(messages)
|
|
446
447
|
system_prompts = [] #: Array[Hash[Symbol, untyped]]
|
|
447
448
|
conversation_messages = [] #: Array[Hash[Symbol, untyped]]
|
|
449
|
+
document_names = [] #: Array[String]
|
|
448
450
|
|
|
449
451
|
messages.each do |message|
|
|
450
452
|
case message
|
|
@@ -452,7 +454,7 @@ class Riffer::Providers::AmazonBedrock < Riffer::Providers::Base
|
|
|
452
454
|
system_prompts << { text: message.content }
|
|
453
455
|
when Riffer::Messages::User
|
|
454
456
|
content = [{ text: message.content }]
|
|
455
|
-
message.files.each { |file| content << convert_file_part_to_bedrock_format(file) }
|
|
457
|
+
message.files.each { |file| content << convert_file_part_to_bedrock_format(file, document_names) }
|
|
456
458
|
conversation_messages << { role: "user", content: content }
|
|
457
459
|
when Riffer::Messages::Assistant
|
|
458
460
|
conversation_messages << convert_assistant_to_bedrock_format(message)
|
|
@@ -515,9 +517,11 @@ class Riffer::Providers::AmazonBedrock < Riffer::Providers::Base
|
|
|
515
517
|
end
|
|
516
518
|
end
|
|
517
519
|
|
|
520
|
+
# +document_names+ collects the names already used in the request, which
|
|
521
|
+
# Converse requires to be unique across every message.
|
|
518
522
|
#--
|
|
519
|
-
#: (Riffer::Messages::User::FilePart) -> Hash[Symbol, untyped]
|
|
520
|
-
def convert_file_part_to_bedrock_format(file)
|
|
523
|
+
#: (Riffer::Messages::User::FilePart, Array[String]) -> Hash[Symbol, untyped]
|
|
524
|
+
def convert_file_part_to_bedrock_format(file, document_names)
|
|
521
525
|
format = bedrock_format(file.media_type)
|
|
522
526
|
|
|
523
527
|
source = file.data_bytes ? { bytes: file.data_bytes } : { s3_location: { uri: file.url } }
|
|
@@ -525,10 +529,39 @@ class Riffer::Providers::AmazonBedrock < Riffer::Providers::Base
|
|
|
525
529
|
if file.image?
|
|
526
530
|
{ image: { format: format, source: source } }
|
|
527
531
|
else
|
|
528
|
-
{ document: { format: format, name: file.filename, source: source } }
|
|
532
|
+
{ document: { format: format, name: document_name(file.filename, document_names), source: source } }
|
|
529
533
|
end
|
|
530
534
|
end
|
|
531
535
|
|
|
536
|
+
DOCUMENT_NAME_LIMIT = 200 #: Integer
|
|
537
|
+
private_constant :DOCUMENT_NAME_LIMIT
|
|
538
|
+
|
|
539
|
+
# Converse allows only alphanumerics, single whitespace, hyphens, parentheses
|
|
540
|
+
# and square brackets in a document name, up to 200 characters, so
|
|
541
|
+
# +invoice.pdf+ is sent as +invoice+ and a repeated name gets a counter:
|
|
542
|
+
# +invoice (2)+.
|
|
543
|
+
#--
|
|
544
|
+
#: (String?, Array[String]) -> String
|
|
545
|
+
def document_name(filename, document_names)
|
|
546
|
+
filename = filename.to_s
|
|
547
|
+
base = File.basename(filename, File.extname(filename)).
|
|
548
|
+
gsub(/[^A-Za-z0-9\s\-()\[\]]+/, "-").
|
|
549
|
+
gsub(/\s+/, " ").
|
|
550
|
+
gsub(/\A[\s-]+|[\s-]+\z/, "")
|
|
551
|
+
base = "document" if base.empty?
|
|
552
|
+
|
|
553
|
+
head = base[0, DOCUMENT_NAME_LIMIT] #: String
|
|
554
|
+
name = head.rstrip
|
|
555
|
+
count = 1
|
|
556
|
+
while document_names.include?(name)
|
|
557
|
+
suffix = " (#{count += 1})"
|
|
558
|
+
head = base[0, DOCUMENT_NAME_LIMIT - suffix.length] #: String
|
|
559
|
+
name = "#{head.rstrip}#{suffix}"
|
|
560
|
+
end
|
|
561
|
+
document_names << name
|
|
562
|
+
name
|
|
563
|
+
end
|
|
564
|
+
|
|
532
565
|
BEDROCK_FORMAT_MAP = {
|
|
533
566
|
"image/jpeg" => "jpeg",
|
|
534
567
|
"image/png" => "png",
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
class Riffer::Providers::Anthropic::Client < Riffer::HTTP::Client
|
|
5
|
+
# @rbs @api_key: String
|
|
6
|
+
# @rbs @base_url: String
|
|
7
|
+
|
|
8
|
+
DEFAULT_BASE_URL = "https://api.anthropic.com" #: String
|
|
9
|
+
|
|
10
|
+
# Override with an anthropic-version entry in +headers+.
|
|
11
|
+
DEFAULT_ANTHROPIC_VERSION = "2026-10-21" #: String
|
|
12
|
+
|
|
13
|
+
#--
|
|
14
|
+
#: (api_key: String, ?base_url: String, ?headers: Hash[String, String], ?connection: Faraday::Connection?) -> void
|
|
15
|
+
def initialize(api_key:, base_url: DEFAULT_BASE_URL, headers: {}, connection: nil)
|
|
16
|
+
super(headers: headers, connection: connection)
|
|
17
|
+
@api_key = api_key
|
|
18
|
+
@base_url = base_url.chomp("/")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
#--
|
|
24
|
+
#: (String) -> String
|
|
25
|
+
def url_for(path)
|
|
26
|
+
"#{@base_url}/#{path}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
#--
|
|
30
|
+
#: () -> Hash[String, String]
|
|
31
|
+
def headers
|
|
32
|
+
{ "authorization" => "Bearer #{@api_key}", "anthropic-version" => DEFAULT_ANTHROPIC_VERSION }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
#--
|
|
36
|
+
#: () -> String
|
|
37
|
+
def error_label
|
|
38
|
+
"Anthropic"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
#--
|
|
42
|
+
#: (Hash[Symbol, untyped], String) -> String
|
|
43
|
+
def error_message(parsed, raw)
|
|
44
|
+
error = parsed[:error]
|
|
45
|
+
return super unless error.is_a?(Hash) && error[:type] && error[:message]
|
|
46
|
+
|
|
47
|
+
"#{error[:type]}: #{error[:message]}"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -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
|