diff --git a/app/models/ai/instrumentation/usage_subscriber.rb b/app/models/ai/instrumentation/usage_subscriber.rb index fde75cdc1a..0f715c0fa4 100644 --- a/app/models/ai/instrumentation/usage_subscriber.rb +++ b/app/models/ai/instrumentation/usage_subscriber.rb @@ -18,9 +18,21 @@ class Ai::Instrumentation::UsageSubscriber end private - def record_request(payload) - family = payload[:owner] + # RubyLLM 2.1.1 keeps the usage owner and workflow in Thread.current, so a request from a thread + # started inside a call has neither (2.2 moves them to fiber storage, RubyLLM #1327). Sure's + # own call context is in fiber storage, which threads inherit, so it fills the gap. + def owner_and_call(payload) call = (payload[:workflow_metadata] || {}).to_h.stringify_keys + return [ payload[:owner], call ] if call["call_id"].present? + + context = Ai::CallContext.current + return [ payload[:owner], call ] if context.nil? + + [ payload[:owner] || (Family.find_by(id: context.family_id) if context.family_id), context.metadata.stringify_keys ] + end + + def record_request(payload) + family, call = owner_and_call(payload) return unless family.is_a?(Family) && call["call_id"].present? status = STATUSES.fetch(payload[:status], nil) diff --git a/app/models/ai/transport/compatible.rb b/app/models/ai/transport/compatible.rb index 6880f7a4a5..89814b435c 100644 --- a/app/models/ai/transport/compatible.rb +++ b/app/models/ai/transport/compatible.rb @@ -28,14 +28,16 @@ class Ai::Transport::Compatible < Ai::Transport::Base # One vector per text, with the model's dimensions. RubyLLM 2.1.1 doesn't check the row # count itself (2.2 does, RubyLLM #1297). + # RubyLLM 2.1.1's embed takes no headers (crmne/ruby_llm#1556), so an endpoint that needs + # them is refused rather than probed without them. def probe_embedding(model, timeout:) + if Ai::Transport::Http.headers(endpoint).any? + raise Ai::Error::Refused, "embedding requests can't carry the endpoint's static headers yet" + end + embedding = mapping_errors do ruby_llm_context(timeout:).embed(Ai::Transport::Base::PROBE_EMBEDDING_INPUT, model: model.name, provider: ruby_llm_provider, assume_model_exists: true) - rescue NoMethodError, TypeError => error - raise unless raised_in_ruby_llm?(error) - - raise Ai::Error::InvalidOutput, "the embedding response isn't the expected shape" end vectors = embedding.vectors vectors = [ vectors ] if vectors.first.is_a?(Numeric) diff --git a/app/models/ai/transport/http.rb b/app/models/ai/transport/http.rb index 34d0f9cecf..cb0e59ac2f 100644 --- a/app/models/ai/transport/http.rb +++ b/app/models/ai/transport/http.rb @@ -26,11 +26,24 @@ module Ai::Transport::Http return if proxy.nil? no_proxy = env["NO_PROXY"].presence || env["no_proxy"].presence - return if no_proxy && (no_proxy.strip == "*" || !URI::Generic.use_proxy?(uri.host, uri.host, uri.port, no_proxy)) + return if no_proxy && (no_proxy.strip == "*" || !URI::Generic.use_proxy?(uri.host, address(uri.host, no_proxy), uri.port, no_proxy)) proxy end + # The host's address, for NO_PROXY's IP and CIDR entries, as Ruby's own lookup resolves it + # (URI#find_proxy). Names are resolved only when NO_PROXY has such an entry. + def address(host, no_proxy) + return host if ip?(host.delete("[]")) + return host unless no_proxy.split(",").any? { ip?(_1.strip) } + + IPSocket.getaddress(host) + rescue SocketError + host + end + + def ip?(value) = IPAddr.new(value).present? rescue false + # The proxy for Net::HTTP.new, which takes host, port, user and password. def net_http_proxy(url, env: ENV) proxy = proxy_for(url, env:) @@ -69,7 +82,9 @@ module Ai::Transport::Http ::OpenAI::Client.new(**options, &faraday_options(base || OPENAI_BASE)) end - # The anthropic gem's client, for the Anthropic PDF probe, as PDF import uses it. It takes no proxy. + # The anthropic gem's client, for the Anthropic PDF probe, as PDF import uses it. It takes no + # proxy or TLS options, so it ignores HTTPS_PROXY and SSL_VERIFY=false, as PDF import does + # until it moves onto RubyLLM. def anthropic_client(endpoint, timeout: endpoint.timeout_seconds) options = { api_key: endpoint.api_key.to_s, max_retries: 0, timeout: } options[:base_url] = endpoint.base_url if endpoint.base_url.present? diff --git a/app/models/ai/transport/instrumenter.rb b/app/models/ai/transport/instrumenter.rb index 8c89c71af4..73ae39fd60 100644 --- a/app/models/ai/transport/instrumenter.rb +++ b/app/models/ai/transport/instrumenter.rb @@ -3,10 +3,12 @@ # after the call's deadline isn't sent, and a failed request's mapped, scrubbed error goes on the # payload as `:ai_error`, which UsageSubscriber adds to that request's usage row. class Ai::Transport::Instrumenter - # map_error: turns a raised error into the Ai::Error the transport raises, scrubbed. - def initialize(delegate = ActiveSupport::Notifications, map_error: ->(error) { error }) + # map_error: turns a raised error into the Ai::Error the transport raises, scrubbed, or nil. + # scrub: removes the endpoint's secrets from an error that has no Ai::Error. + def initialize(delegate = ActiveSupport::Notifications, map_error: ->(error) { error }, scrub: ->(text) { text }) @delegate = delegate @map_error = map_error + @scrub = scrub end def instrument(name, payload = {}, &block) @@ -33,7 +35,9 @@ class Ai::Transport::Instrumenter end def failure_details(error) - mapped = @map_error.call(error) || error + mapped = @map_error.call(error) + return { error: @scrub.call(error.message), error_class: error.class.name } if mapped.nil? + { error: mapped.message, error_class: mapped.class.name, http_status_code: mapped.try(:status) }.compact rescue StandardError nil diff --git a/app/models/ai/transport/ruby_llm_adapter.rb b/app/models/ai/transport/ruby_llm_adapter.rb index 59796a7d03..7dc65ce613 100644 --- a/app/models/ai/transport/ruby_llm_adapter.rb +++ b/app/models/ai/transport/ruby_llm_adapter.rb @@ -22,7 +22,7 @@ module Ai::Transport::RubyLlmAdapter config.http_proxy = Ai::Transport::Http.proxy_for(ruby_llm_api_base) config.faraday_adapter = Ai::Transport::Http.ruby_llm_adapter config.auto_upload_large_files = false - config.instrumenter = Ai::Transport::Instrumenter.new(config.instrumenter, map_error: method(:mapped_error)) + config.instrumenter = Ai::Transport::Instrumenter.new(config.instrumenter, map_error: method(:mapped_error), scrub: scrubber) configure_provider(config) end end @@ -68,6 +68,11 @@ module Ai::Transport::RubyLlmAdapter raise unless refused_by_ruby_llm?(error) raise scrubbed(Ai::Error::Refused.new(error.message)) + rescue NoMethodError, TypeError => error + # RubyLLM parsing a 200 that isn't the API's shape (`{}`, an HTML page); in Sure's own code it's a bug. + raise unless raised_in_ruby_llm?(error) + + raise Ai::Error::InvalidOutput, "the response isn't the expected shape" rescue StandardError => error mapped = ruby_llm_error(error) raise mapped ? scrubbed(mapped) : error diff --git a/app/views/settings/llm_usages/show.html.erb b/app/views/settings/llm_usages/show.html.erb index 679c745c10..5416561785 100644 --- a/app/views/settings/llm_usages/show.html.erb +++ b/app/views/settings/llm_usages/show.html.erb @@ -105,7 +105,8 @@
<%= t(".recent_usage_scope") %>