Class: Braintrust::Contrib::Anthropic::MessagesPatcher
- Defined in:
- lib/braintrust/contrib/anthropic/patcher.rb
Overview
Patcher for Anthropic messages. Instruments Anthropic::Messages#create and #stream methods.
Class Method Summary collapse
- .applicable? ⇒ Boolean
- .patched?(**options) ⇒ Boolean
-
.perform_patch(**options) ⇒ void
Perform the actual patching.
Methods inherited from Patcher
Class Method Details
.applicable? ⇒ Boolean
14 15 16 |
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 14 def applicable? defined?(::Anthropic::Client) end |
.patched?(**options) ⇒ Boolean
18 19 20 21 |
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 18 def patched?(**) target_class = get_singleton_class([:target]) || ::Anthropic::Resources::Messages Instrumentation::Messages.applied?(target_class) end |
.perform_patch(**options) ⇒ void
This method returns an undefined value.
Perform the actual patching.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 28 def perform_patch(**) return unless applicable? # MessageStream is shared across all clients, so patch at class level. # The instrumentation short-circuits when no context is present, # so uninstrumented clients' streams pass through unaffected. if [:target] # Instance-level (for only this client instance) raise ArgumentError, "target must be a kind of ::Anthropic::Client" unless [:target].is_a?(::Anthropic::Client) get_singleton_class([:target]).include(Instrumentation::Messages) else # Class-level (for all client instances) ::Anthropic::Resources::Messages.include(Instrumentation::Messages) end end |