ruby_llm 2.2.2 → 2.2.3

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/ruby_llm/accounting/usage.rb +1 -0
  4. data/lib/ruby_llm/active_record/chat_methods.rb +28 -1
  5. data/lib/ruby_llm/active_record/model.rb +1 -1
  6. data/lib/ruby_llm/active_record/run_claims.rb +73 -18
  7. data/lib/ruby_llm/agent.rb +2 -2
  8. data/lib/ruby_llm/aliases.json +14 -0
  9. data/lib/ruby_llm/chat/tool_call_state.rb +1 -1
  10. data/lib/ruby_llm/chat/tool_concurrency.rb +58 -18
  11. data/lib/ruby_llm/chat.rb +20 -13
  12. data/lib/ruby_llm/code_execution/input_names.rb +80 -0
  13. data/lib/ruby_llm/code_execution/workspace.rb +4 -37
  14. data/lib/ruby_llm/code_execution.rb +44 -12
  15. data/lib/ruby_llm/configuration.rb +55 -0
  16. data/lib/ruby_llm/file_store/directory.rb +10 -7
  17. data/lib/ruby_llm/mcp/content.rb +2 -0
  18. data/lib/ruby_llm/model/pricing.rb +46 -0
  19. data/lib/ruby_llm/model.rb +55 -4
  20. data/lib/ruby_llm/models/lookup.rb +2 -1
  21. data/lib/ruby_llm/models.json +3281 -1791
  22. data/lib/ruby_llm/models.rb +1 -1
  23. data/lib/ruby_llm/protocol/stream_accumulator.rb +9 -4
  24. data/lib/ruby_llm/protocol/streaming.rb +22 -8
  25. data/lib/ruby_llm/protocols/bedrock/event_stream.rb +3 -7
  26. data/lib/ruby_llm/protocols/converse/streaming.rb +2 -2
  27. data/lib/ruby_llm/protocols/gemini/decisions.rb +106 -0
  28. data/lib/ruby_llm/protocols/gemini/streaming.rb +1 -1
  29. data/lib/ruby_llm/providers/gemini.rb +2 -0
  30. data/lib/ruby_llm/providers/minimax.rb +0 -6
  31. data/lib/ruby_llm/providers/openai/rerank.rb +13 -0
  32. data/lib/ruby_llm/providers/openai.rb +14 -0
  33. data/lib/ruby_llm/question.rb +60 -5
  34. data/lib/ruby_llm/tool.rb +24 -5
  35. data/lib/ruby_llm/transport/connection.rb +8 -5
  36. data/lib/ruby_llm/transport/log_formatter.rb +39 -0
  37. data/lib/ruby_llm/transport/release_request.rb +6 -2
  38. data/lib/ruby_llm/version.rb +1 -1
  39. data/lib/ruby_llm.rb +0 -1
  40. data/skills/rubyllm/references/cost-and-tracing.md +1 -1
  41. data/skills/rubyllm/references/tools-and-agents.md +1 -1
  42. metadata +5 -3
  43. data/lib/ruby_llm/providers/minimax/chat_completions.rb +0 -48
@@ -60,44 +60,11 @@ module RubyLLM
60
60
  "#{name.scrub} (name is not valid UTF-8)"
61
61
  end
62
62
 
63
- # Returns each attachment's name under +attachments/+, newest first:
64
- # the newest keeps its name, as it would on a disk, and an older one
65
- # stays readable beside it. Names compare without case, for the
66
- # filesystems that do, and files with equal content share one.
63
+ # Returns each attachment's name under +attachments/+, as InputNames
64
+ # gives them.
67
65
  def self.input_names(attachments)
68
- attachments = attachments.reject(&:provider_file?)
69
- folders = input_folders(attachments)
70
- taken = {}
71
- attachments.reverse.each_with_object({}.compare_by_identity) do |attachment, names|
72
- name = filename(attachment.filename)
73
- choice = numbered_names(name).find do |candidate|
74
- (candidate == name || !folders.include?(candidate.downcase)) &&
75
- !different?(taken[candidate.downcase], attachment)
76
- end
77
- taken[choice.downcase] ||= [choice, attachment]
78
- names[attachment] = taken[choice.downcase].first
79
- end
80
- end
81
-
82
- def self.different?(holder, attachment)
83
- holder && !holder.last.equal?(attachment) && holder.last.content.b != attachment.content.b
84
- end
85
- private_class_method :different?
86
-
87
- def self.input_folders(attachments)
88
- attachments.flat_map do |attachment|
89
- parts = filename(attachment.filename).downcase.split('/')
90
- (1...parts.size).map { |size| parts.first(size).join('/') }
91
- end
92
- end
93
- private_class_method :input_folders
94
-
95
- def self.numbered_names(name)
96
- extension = File.extname(name)
97
- stem = name.delete_suffix(extension)
98
- [name].chain((2..).lazy.map { |number| "#{stem}-#{number}#{extension}" })
66
+ attachments.each_with_object(InputNames.new) { |attachment, names| names.add(attachment) }.names
99
67
  end
100
- private_class_method :numbered_names
101
68
 
102
69
  def initialize(root, attachments: true)
103
70
  @directory = File.join(root, 'work')
@@ -160,7 +127,7 @@ module RubyLLM
160
127
  end
161
128
 
162
129
  def copy_in(attachments)
163
- self.class.input_names(attachments).to_a.reverse_each do |attachment, name|
130
+ self.class.input_names(attachments).each do |attachment, name|
164
131
  path = File.join(inputs, name)
165
132
  make_room(path, inputs)
166
133
  File.binwrite(path, attachment.content.b)
@@ -107,26 +107,29 @@ module RubyLLM
107
107
  ids = messages.flat_map { |message| run_code_call_ids(message) }
108
108
  return messages if ids.empty?
109
109
 
110
- names = Workspace.input_names(messages.flat_map(&:attachments))
110
+ arrivals = arrival_names(messages)
111
111
  messages.map do |message|
112
- returned_files?(message, ids) ? message.with_attachments(model_view(message.attachments, names)) : message
112
+ returned_files?(message, ids) ? message.with_attachments(model_view(message.attachments, arrivals)) : message
113
113
  end
114
114
  end
115
115
 
116
- # Returns a callable that describes one of +attachments+, the
117
- # conversation's files, under the name its run finds.
118
- def describer(attachments) # :nodoc:
119
- names = Workspace.input_names(attachments)
120
- ->(attachment) { describe(attachment, name: names[attachment]) }
116
+ # Returns a callable that describes a file in +messages+ under the
117
+ # name its run found when the file arrived. A newer file of the same
118
+ # name takes that name over, and its description says where the
119
+ # earlier files moved, so what the model has already read never
120
+ # changes and its cached prompt and signed reasoning stay valid.
121
+ def describer(messages) # :nodoc:
122
+ arrivals = arrival_names(messages)
123
+ ->(attachment) { describe(attachment, **arrivals.fetch(attachment, {})) }
121
124
  end
122
125
 
123
126
  # Returns a text Attachment telling the model where its code finds
124
- # +attachment+.
125
- def describe(attachment, name: nil) # :nodoc:
127
+ # +attachment+, and where the earlier files it displaced went.
128
+ def describe(attachment, name: nil, moved: []) # :nodoc:
126
129
  name ||= Workspace.filename(attachment.filename)
127
130
  text = "#{name} (#{attachment.mime_type}, #{attachment.byte_size} bytes) is not shown here. " \
128
131
  "Code you run with #{tool_name} can read it at #{Workspace::ATTACHMENTS}/#{name}."
129
- Attachment.new(StringIO.new(text), filename: "#{name}.txt")
132
+ Attachment.new(StringIO.new([text, *relocations(moved)].join(' ')), filename: "#{name}.txt")
130
133
  end
131
134
 
132
135
  # Returns +true+ when +sandbox+ runs +language+, or why it cannot.
@@ -215,8 +218,37 @@ module RubyLLM
215
218
  message.tool_result? && ids.include?(message.tool_call_id) && message.attachments.any?
216
219
  end
217
220
 
218
- def model_view(files, names)
219
- files.map { |file| readable_by_model?(file) ? file : describe(file, name: names[file]) }
221
+ def model_view(files, arrivals)
222
+ files.flat_map do |file|
223
+ arrival = arrivals.fetch(file, {})
224
+ next describe(file, **arrival) unless readable_by_model?(file)
225
+
226
+ [file, relocation_note(**arrival)].compact
227
+ end
228
+ end
229
+
230
+ def relocation_note(name: nil, moved: [])
231
+ Attachment.new(StringIO.new(relocations(moved).join(' ')), filename: "#{name}.txt") if moved.any?
232
+ end
233
+
234
+ def relocations(moved)
235
+ moved.map { |from, to| "The earlier #{from} is now at #{Workspace::ATTACHMENTS}/#{to}." }
236
+ end
237
+
238
+ def arrival_names(messages)
239
+ names = InputNames.new
240
+ messages.each_with_object({}.compare_by_identity) do |message, arrivals|
241
+ fresh = message.attachments.reject { |file| names.names.key?(file) }
242
+ moves = message.attachments.map { |file| names.add(file) }
243
+ message.attachments.zip(moves) do |file, made|
244
+ arrivals[file] ||= { name: names.names[file], moved: earlier(made, fresh) }
245
+ end
246
+ end
247
+ end
248
+
249
+ def earlier(moves, fresh)
250
+ moves.reject { |move| move.files.all? { |file| fresh.any? { |other| other.equal?(file) } } }
251
+ .map { |move| [move.from, move.to] }
220
252
  end
221
253
 
222
254
  def run_code_call_ids(message)
@@ -24,6 +24,8 @@ module RubyLLM
24
24
  # +nil+, so unset environment variables behave as if the option was
25
25
  # never set.
26
26
  class Configuration
27
+ SECRET_OPTION = /_(key|secret|token)\z/ # :nodoc:
28
+
27
29
  class << self
28
30
  def option(key, default = nil, &normalize) # :nodoc:
29
31
  key = key.to_sym
@@ -35,6 +37,7 @@ module RubyLLM
35
37
  value = nil if value.is_a?(String) && value.strip.empty?
36
38
  value = normalize.call(value) if normalize
37
39
  instance_variable_set(:"@#{key}", value)
40
+ @revision = @revision.to_i + 1
38
41
  end
39
42
 
40
43
  option_keys << key
@@ -455,6 +458,14 @@ module RubyLLM
455
458
  # otherwise.
456
459
  option :log_stream_debug, -> { env_says_yes?('RUBYLLM_STREAM_DEBUG') }
457
460
 
461
+ ##
462
+ # :attr_accessor: log_bodies
463
+ #
464
+ # Whether request and response bodies are logged, including the bodies
465
+ # of failed requests. Defaults to +nil+, which logs them when the logger
466
+ # is at debug level. Set it to +false+ to keep bodies out of a debug log.
467
+ option :log_bodies, nil
468
+
458
469
  ##
459
470
  # :attr_accessor: log_regexp_timeout
460
471
  #
@@ -463,22 +474,66 @@ module RubyLLM
463
474
  # when none is set.
464
475
  option :log_regexp_timeout, -> { Regexp.timeout || 1.0 }
465
476
 
477
+ attr_reader :prices # :nodoc:
478
+
479
+ # Counts the assignments to this configuration, so that what was
480
+ # derived from it can tell when to derive it again.
481
+ attr_reader :revision # :nodoc:
482
+
466
483
  def initialize # :nodoc:
484
+ @prices = {}.freeze
467
485
  self.class.send(:defaults).each do |key, default|
468
486
  value = default.respond_to?(:call) ? instance_exec(&default) : default
469
487
  public_send("#{key}=", value)
470
488
  end
471
489
  end
472
490
 
491
+ # Sets what +model+ costs on +provider+, in USD per million, in place
492
+ # of the registry's prices. Use it when you pay another price than the
493
+ # list price, or when the registry has none:
494
+ #
495
+ # RubyLLM.configure do |config|
496
+ # config.price 'gpt-5.6-luna', provider: :azure, input: 0.24, output: 1.44
497
+ # config.price 'Cohere-rerank-v4.0-fast', provider: :azure, input: 0.024
498
+ # end
499
+ #
500
+ # The prices are +input+, +output+, +cache_read+, and +cache_write+ per
501
+ # million tokens, and +characters+, +audio_seconds+, and +search_units+
502
+ # per million of those units. Prices you leave out stay as the registry
503
+ # has them, and so do the batch and long-context rates. A model takes
504
+ # its prices under the id you call it by, including an id the registry
505
+ # does not list.
506
+ #
507
+ # Model#pricing, Model#price, and every Cost use the prices from then
508
+ # on, and a registry refresh keeps them. They belong to the global
509
+ # configuration: a context raises ConfigurationError.
510
+ def price(model, provider:, **prices)
511
+ raise ConfigurationError, 'Set prices in RubyLLM.configure: a context cannot price a model differently' if @copy
512
+
513
+ key = [provider.to_s, model.to_s]
514
+ @prices = @prices.merge(key => @prices.fetch(key, {}).merge(Model::Pricing.override(prices)).freeze).freeze
515
+ @revision += 1
516
+ end
517
+
473
518
  # Copies file_converters too, so registering one on a context's copy,
474
519
  # <tt>config.file_converters["image/*"] = ...</tt>, leaves the
475
520
  # original alone.
476
521
  def initialize_copy(source) # :nodoc:
477
522
  super
523
+ @copy = true
478
524
  @file_converters = @file_converters.dup
479
525
  @ssl = @ssl.to_h.transform_values { |value| value.is_a?(String) ? value.dup : value } if @ssl
480
526
  end
481
527
 
528
+ # The values of the credential options set on this configuration, which
529
+ # logging replaces before writing a line.
530
+ def secret_values # :nodoc:
531
+ self.class.options.grep(SECRET_OPTION).filter_map do |key|
532
+ value = public_send(key)
533
+ value if value.is_a?(String)
534
+ end
535
+ end
536
+
482
537
  def instance_variables # :nodoc:
483
538
  super.reject { |ivar| ivar.to_s.match?(/(_id|_key|_secret|_token|_credential_provider|_proxy|\A@ssl)$/) }
484
539
  end
@@ -132,26 +132,29 @@ module RubyLLM
132
132
  def full_path(path)
133
133
  parent = root
134
134
  path.split('/').each do |part|
135
- raise ArgumentError, "#{path} is not inside the chat's files" if part.start_with?('.')
135
+ if part.start_with?('.') || linked_folder?(parent)
136
+ raise ArgumentError, "#{path} is not inside the chat's files"
137
+ end
136
138
 
137
139
  parent = File.join(parent, part)
138
140
  end
139
- folder = File.dirname(parent)
140
- if File.directory?(folder) && !"#{File.realpath(folder)}/".start_with?("#{root}/")
141
- raise ArgumentError, "#{path} is not inside the chat's files"
142
- end
143
-
144
141
  parent
145
142
  end
146
143
 
144
+ def linked_folder?(path)
145
+ File.symlink?(path) && File.directory?(path)
146
+ end
147
+
147
148
  def walk(start)
148
149
  files = []
149
150
  Find.find(start) do |path|
150
151
  next if path == root
151
152
 
152
- stat = File.lstat(path)
153
153
  Find.prune if File.basename(path).start_with?('.')
154
+ stat = File.lstat(path)
154
155
  files << stored_file(path, stat) if stat.file?
156
+ rescue Errno::ENOENT, Errno::ENOTDIR
157
+ next
155
158
  end
156
159
  files
157
160
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'marcel'
4
+
3
5
  module RubyLLM
4
6
  class MCP
5
7
  # Reads MCP content blocks into text and attachments. Tool results and
@@ -16,6 +16,52 @@ module RubyLLM
16
16
  # The pricing categories a model may define.
17
17
  CATEGORIES = %i[text_tokens images audio_tokens embeddings characters audio_seconds search_units].freeze
18
18
 
19
+ # The prices Configuration#price sets, each as the category and the
20
+ # standard-tier price it replaces.
21
+ OVERRIDES = {
22
+ input: %i[text_tokens input_per_million],
23
+ output: %i[text_tokens output_per_million],
24
+ cache_read: %i[text_tokens cache_read_input_per_million],
25
+ cache_write: %i[text_tokens cache_write_input_per_million],
26
+ characters: %i[characters input_per_million],
27
+ audio_seconds: %i[audio_seconds input_per_million],
28
+ search_units: %i[search_units input_per_million]
29
+ }.freeze
30
+ private_constant :OVERRIDES
31
+
32
+ class << self
33
+ # Returns +prices+ checked against the prices a configuration can
34
+ # set.
35
+ def override(prices) # :nodoc:
36
+ unknown = prices.keys - OVERRIDES.keys
37
+ if unknown.any? || prices.empty?
38
+ raise ArgumentError, "#{unknown.any? ? "Unknown price: #{unknown.join(', ')}" : 'No price given'}. " \
39
+ "Valid prices: #{OVERRIDES.keys.join(', ')}"
40
+ end
41
+
42
+ prices.each do |kind, price|
43
+ raise ArgumentError, "The #{kind} price must be a number, got #{price.inspect}" unless price.is_a?(Numeric)
44
+ end
45
+ prices
46
+ end
47
+
48
+ # Returns pricing +data+ with the standard-tier +prices+ replaced.
49
+ # A thinking price that equals the output price is the same price,
50
+ # so it follows a new output price.
51
+ def overridden(data, prices) # :nodoc:
52
+ data = RubyLLM::Support::Utils.deep_dup(data)
53
+ standard = data.dig(:text_tokens, :standard) || {}
54
+ thinking = standard[:reasoning_output_per_million]
55
+ follows = prices.key?(:output) && thinking && thinking == standard[:output_per_million]
56
+ prices.each do |kind, price|
57
+ category, attribute = OVERRIDES.fetch(kind)
58
+ ((data[category] ||= {})[:standard] ||= {})[attribute] = price
59
+ end
60
+ data[:text_tokens][:standard][:reasoning_output_per_million] = prices[:output] if follows
61
+ data
62
+ end
63
+ end
64
+
19
65
  def initialize(data) # :nodoc:
20
66
  @data = {}
21
67
 
@@ -49,8 +49,34 @@ module RubyLLM
49
49
  # e.g. <tt>["function_calling", "streaming"]</tt>.
50
50
  attr_reader :capabilities
51
51
 
52
- # The model's pricing as a Model::Pricing object, in USD per million tokens.
53
- attr_reader :pricing
52
+ # The model's pricing as a Model::Pricing object, in USD per million
53
+ # tokens: the registry's prices, with the ones set through
54
+ # Configuration#price in their place.
55
+ def pricing
56
+ config = RubyLLM.config
57
+ revision = config.revision
58
+ prices = config.prices
59
+ return @pricing if prices.empty?
60
+
61
+ priced = @priced
62
+ return priced.last if priced && priced[0].equal?(config) && priced[1] == revision
63
+
64
+ override = configured_prices(prices)
65
+ pricing = override.empty? ? @pricing : Pricing.new(Pricing.overridden(@pricing_data, override))
66
+ @priced = [config, revision, pricing].freeze
67
+ pricing
68
+ end
69
+
70
+ # The registry's own pricing, which is what a registry store keeps.
71
+ def registry_pricing # :nodoc:
72
+ @pricing
73
+ end
74
+
75
+ # Whether the configuration prices this model, so that its costs use
76
+ # those prices whatever model id the provider answers with.
77
+ def configured_prices? # :nodoc:
78
+ !configured_prices(RubyLLM.config.prices).empty?
79
+ end
54
80
 
55
81
  # Provider-specific metadata as a Hash.
56
82
  attr_reader :metadata
@@ -86,7 +112,8 @@ module RubyLLM
86
112
  @capabilities = data[:capabilities] || []
87
113
  @metadata = data[:metadata]&.dup || {}
88
114
  @unlisted_at = Support::Utils.to_time(data[:unlisted_at])&.utc
89
- @pricing = Pricing.new(pricing_data_with_long_context(data[:pricing] || {}))
115
+ @pricing_data = pricing_data_with_long_context(data[:pricing] || {})
116
+ @pricing = Pricing.new(@pricing_data)
90
117
  @reasoning_options = normalize_reasoning_options(reasoning_options_from(data))
91
118
  store_reasoning_options_metadata
92
119
  end
@@ -205,7 +232,7 @@ module RubyLLM
205
232
  knowledge_cutoff: knowledge_cutoff,
206
233
  modalities: modalities.to_h,
207
234
  capabilities: capabilities,
208
- pricing: pricing.to_h,
235
+ pricing: registry_pricing.to_h,
209
236
  metadata: metadata
210
237
  }
211
238
  # A JSON registry drops unlisted models, so the key stays out of it.
@@ -215,6 +242,30 @@ module RubyLLM
215
242
 
216
243
  private
217
244
 
245
+ # A deployment costs what its model costs, unless it has prices of its
246
+ # own. A deployment a context declared carries its model's id.
247
+ def configured_prices(prices)
248
+ deployed = metadata[:deployed_model_id] || provider_class&.deployed_model_id(id, RubyLLM.config)
249
+ [deployed, id].compact.flat_map { |model_id| prices_named(prices, model_id.to_s) }.reduce({}, :merge)
250
+ end
251
+
252
+ # The prices set under another name of +model_id+, then the ones set
253
+ # under the id itself.
254
+ def prices_named(prices, model_id)
255
+ renamed = prices.select do |(slug, name), _|
256
+ slug == provider.to_s && name != model_id && names?(name, model_id)
257
+ end
258
+ [*renamed.values, prices[[provider.to_s, model_id]]].compact
259
+ end
260
+
261
+ # Whether lookup resolves +name+ to +model_id+: an alias, or an id the
262
+ # provider's registry stores under another one.
263
+ def names?(name, model_id)
264
+ resolved = Models::Aliases.resolve(name, provider.to_s)
265
+ resolved == model_id ||
266
+ provider_class&.resolve_registry_id(resolved, RubyLLM.models, RubyLLM.config) == model_id
267
+ end
268
+
218
269
  def usage_entries_of(source)
219
270
  return source.usage_entries if source.respond_to?(:usage_entries)
220
271
  return source.ruby_llm_usage_entries if source.respond_to?(:ruby_llm_usage_entries)
@@ -18,7 +18,8 @@ module RubyLLM
18
18
  deployed_id = Provider.resolve(provider)&.deployed_model_id(model_id, config || RubyLLM.config)
19
19
  return find_registered(model_id, provider, config) unless deployed_id
20
20
 
21
- Model.new(find_deployed_model(model_id, deployed_id, provider, config).to_h.merge(id: model_id))
21
+ model = find_deployed_model(model_id, deployed_id, provider, config)
22
+ Model.new(model.to_h.merge(id: model_id, metadata: model.metadata.merge(deployed_model_id: model.id)))
22
23
  end
23
24
 
24
25
  def find_deployed_model(deployment, model_id, provider, config)