coolhand 0.2.0 → 0.3.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.
data/lib/coolhand/ruby.rb DELETED
@@ -1,121 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "net/http"
4
- require "uri"
5
- require "faraday"
6
- require "securerandom"
7
-
8
- require_relative "ruby/version"
9
- require_relative "ruby/configuration"
10
- require_relative "ruby/collector"
11
- require_relative "ruby/faraday_interceptor"
12
- require_relative "ruby/anthropic_interceptor"
13
- require_relative "ruby/api_service"
14
- require_relative "ruby/logger_service"
15
- require_relative "ruby/feedback_service"
16
-
17
- # The main module for the Coolhand gem.
18
- # It provides the configuration interface and initializes the patching.
19
- module Coolhand
20
- class Error < StandardError; end
21
-
22
- # Class-level instance variables to hold the configuration
23
- @configuration = Configuration.new
24
-
25
- class << self
26
- attr_reader :configuration
27
-
28
- # Reset configuration to defaults (mainly for testing)
29
- def reset_configuration!
30
- @configuration = Configuration.new
31
- end
32
-
33
- # Provides a block to configure the gem.
34
- #
35
- # Example:
36
- # Coolhand.configure do |config|
37
- # config.environment = 'development'
38
- # config.silent = false
39
- # config.api_key = "xxx-yyy-zzz"
40
- # config.intercept_addresses = ["openai.com"]
41
- # end
42
- def configure
43
- yield(configuration)
44
-
45
- configuration.validate!
46
-
47
- # Apply the Faraday patch (needed for ruby-anthropic and other Faraday-based gems)
48
- Ruby::FaradayInterceptor.patch!
49
-
50
- # Conditionally patch the official Anthropic gem if it's loaded
51
- if anthropic_gem_loaded?
52
- if defined?(Anthropic::Internal)
53
- # Official anthropic gem - patch the AnthropicInterceptor for Net::HTTP requests
54
- Ruby::AnthropicInterceptor.patch!
55
- log "✅ Coolhand ready - will log OpenAI and Anthropic (official gem) calls"
56
- else
57
- # ruby-anthropic gem uses Faraday, so FaradayInterceptor is sufficient
58
- log "✅ Coolhand ready - will log OpenAI and Anthropic (ruby-anthropic via Faraday) calls"
59
- end
60
- else
61
- log "✅ Coolhand ready - will log OpenAI calls"
62
- end
63
- end
64
-
65
- def capture
66
- unless block_given?
67
- log "❌ Coolhand Error: Method .capture requires block."
68
- return
69
- end
70
-
71
- Ruby::FaradayInterceptor.patch!
72
-
73
- # Only patch AnthropicInterceptor for official anthropic gem
74
- anthropic_patched = false
75
- if anthropic_gem_loaded? && defined?(Anthropic::Internal)
76
- Ruby::AnthropicInterceptor.patch!
77
- anthropic_patched = true
78
- end
79
-
80
- yield
81
- ensure
82
- Ruby::FaradayInterceptor.unpatch!
83
- Ruby::AnthropicInterceptor.unpatch! if anthropic_patched
84
- end
85
-
86
- # A simple logger that respects the 'silent' configuration option.
87
- def log(message)
88
- return if configuration.silent
89
-
90
- puts "COOLHAND: #{message}"
91
- end
92
-
93
- # Creates a new FeedbackService instance
94
- def feedback_service
95
- Ruby::FeedbackService.new
96
- end
97
-
98
- # Creates a new LoggerService instance
99
- def logger_service
100
- Ruby::LoggerService.new
101
- end
102
-
103
- def required_field?(value)
104
- return false if value.nil?
105
- return false if value.respond_to?(:empty?) && value.empty?
106
- return false if value.to_s.strip.empty?
107
-
108
- true
109
- end
110
-
111
- def current_request_id
112
- Thread.current[:coolhand_current_request_id]
113
- end
114
-
115
- private
116
-
117
- def anthropic_gem_loaded?
118
- defined?(Anthropic::Client)
119
- end
120
- end
121
- end