9
9
10
10
from osmosis_wrap .utils import send_to_hoover
11
11
from osmosis_wrap import utils
12
+ from osmosis_wrap .logger import logger
12
13
13
14
def wrap_anthropic () -> None :
14
15
"""
@@ -26,44 +27,44 @@ def wrap_anthropic() -> None:
26
27
try :
27
28
import anthropic
28
29
except ImportError :
29
- print ( "Error: anthropic package is not installed.", file = sys . stderr )
30
+ logger . error ( " anthropic package is not installed." )
30
31
return
31
32
32
- print (f"Wrapping Anthropic client, package version: { anthropic .__version__ } " , file = sys . stderr )
33
+ logger . info (f"Wrapping Anthropic client, package version: { anthropic .__version__ } " )
33
34
34
35
# Get the resources.messages module and class
35
36
messages_module = anthropic .resources .messages
36
37
messages_class = messages_module .Messages
37
38
38
- print (f"Found Anthropic messages class: { messages_class } " , file = sys . stderr )
39
+ logger . info (f"Found Anthropic messages class: { messages_class } " )
39
40
40
41
# Patch the Messages.create method
41
42
original_messages_create = messages_class .create
42
- print (f"Original create method: { original_messages_create } " , file = sys . stderr )
43
+ logger . info (f"Original create method: { original_messages_create } " )
43
44
44
45
if not hasattr (original_messages_create , "_osmosis_wrapped" ):
45
46
@functools .wraps (original_messages_create )
46
47
def wrapped_messages_create (self , * args , ** kwargs ):
47
- print (f"Wrapped create called with args: { args } , kwargs: { kwargs } " , file = sys . stderr )
48
+ logger . debug (f"Wrapped create called with args: { args } , kwargs: { kwargs } " )
48
49
try :
49
50
# Check if the call includes tool use parameters
50
51
has_tools = "tools" in kwargs
51
52
if has_tools :
52
- print (f"Tool use detected with { len (kwargs ['tools' ])} tools" , file = sys . stderr )
53
+ logger . debug (f"Tool use detected with { len (kwargs ['tools' ])} tools" )
53
54
54
55
# Check if this is a tool response message
55
56
if "messages" in kwargs :
56
57
for message in kwargs ["messages" ]:
57
58
if message .get ("role" ) == "user" and isinstance (message .get ("content" ), list ):
58
59
for content_item in message ["content" ]:
59
60
if isinstance (content_item , dict ) and content_item .get ("type" ) == "tool_response" :
60
- print ("Tool response detected in messages" , file = sys . stderr )
61
+ logger . debug ("Tool response detected in messages" )
61
62
break
62
63
63
64
response = original_messages_create (self , * args , ** kwargs )
64
65
65
66
if utils .enabled :
66
- print ("Sending success to Hoover (success)" , file = sys . stderr )
67
+ logger . debug ("Sending success to Hoover (success)" )
67
68
send_to_hoover (
68
69
query = kwargs ,
69
70
response = response .model_dump () if hasattr (response , 'model_dump' ) else response ,
@@ -72,26 +73,26 @@ def wrapped_messages_create(self, *args, **kwargs):
72
73
73
74
return response
74
75
except Exception as e :
75
- print (f"Error in wrapped create: { e } " , file = sys . stderr )
76
+ logger . error (f"Error in wrapped create: { e } " )
76
77
if utils .enabled :
77
78
error_response = {"error" : str (e )}
78
79
send_to_hoover (
79
80
query = kwargs ,
80
81
response = error_response ,
81
82
status = 400
82
83
)
83
- print ("Sending error to Hoover (success)" , file = sys . stderr )
84
+ logger . debug ("Sending error to Hoover (success)" )
84
85
raise # Re-raise the exception
85
86
86
87
wrapped_messages_create ._osmosis_wrapped = True
87
88
messages_class .create = wrapped_messages_create
88
- print ("Successfully wrapped Messages.create method" , file = sys . stderr )
89
+ logger . info ("Successfully wrapped Messages.create method" )
89
90
90
91
# Directly wrap the AsyncAnthropic client
91
92
try :
92
93
# Get the AsyncAnthropic class
93
94
AsyncAnthropicClass = anthropic .AsyncAnthropic
94
- print (f"Found AsyncAnthropic class: { AsyncAnthropicClass } " , file = sys . stderr )
95
+ logger . info (f"Found AsyncAnthropic class: { AsyncAnthropicClass } " )
95
96
96
97
# Store the original __init__ to keep track of created instances
97
98
original_async_init = AsyncAnthropicClass .__init__
@@ -102,7 +103,7 @@ def wrapped_async_init(self, *args, **kwargs):
102
103
# Call the original init
103
104
result = original_async_init (self , * args , ** kwargs )
104
105
105
- print ("Wrapping new AsyncAnthropic instance's messages.create method" , file = sys . stderr )
106
+ logger . info ("Wrapping new AsyncAnthropic instance's messages.create method" )
106
107
107
108
# Get the messages client from this instance
108
109
async_messages = self .messages
@@ -113,12 +114,12 @@ def wrapped_async_init(self, *args, **kwargs):
113
114
114
115
@functools .wraps (original_async_messages_create )
115
116
async def wrapped_async_messages_create (* args , ** kwargs ):
116
- print (f"AsyncAnthropic.messages.create called with args: { args } , kwargs: { kwargs } " , file = sys . stderr )
117
+ logger . debug (f"AsyncAnthropic.messages.create called with args: { args } , kwargs: { kwargs } " )
117
118
try :
118
119
response = await original_async_messages_create (* args , ** kwargs )
119
120
120
121
if utils .enabled :
121
- print ("Sending AsyncAnthropic response to Hoover (success)" , file = sys . stderr )
122
+ logger . debug ("Sending AsyncAnthropic response to Hoover (success)" )
122
123
send_to_hoover (
123
124
query = kwargs ,
124
125
response = response .model_dump () if hasattr (response , 'model_dump' ) else response ,
@@ -127,9 +128,9 @@ async def wrapped_async_messages_create(*args, **kwargs):
127
128
128
129
return response
129
130
except Exception as e :
130
- print (f"Error in wrapped AsyncAnthropic.messages.create: { e } " , file = sys . stderr )
131
+ logger . error (f"Error in wrapped AsyncAnthropic.messages.create: { e } " )
131
132
if utils .enabled :
132
- print ("Sending AsyncAnthropic error to Hoover" , file = sys . stderr )
133
+ logger . debug ("Sending AsyncAnthropic error to Hoover" )
133
134
error_response = {"error" : str (e )}
134
135
send_to_hoover (
135
136
query = kwargs ,
@@ -140,41 +141,41 @@ async def wrapped_async_messages_create(*args, **kwargs):
140
141
141
142
wrapped_async_messages_create ._osmosis_wrapped = True
142
143
async_messages .create = wrapped_async_messages_create
143
- print ("Successfully wrapped AsyncAnthropic.messages.create method" , file = sys . stderr )
144
+ logger . info ("Successfully wrapped AsyncAnthropic.messages.create method" )
144
145
145
146
return result
146
147
147
148
wrapped_async_init ._osmosis_wrapped = True
148
149
AsyncAnthropicClass .__init__ = wrapped_async_init
149
- print ("Successfully wrapped AsyncAnthropic.__init__ to patch message methods on new instances" , file = sys . stderr )
150
+ logger . info ("Successfully wrapped AsyncAnthropic.__init__ to patch message methods on new instances" )
150
151
except (ImportError , AttributeError ) as e :
151
- print (f"AsyncAnthropic class not found or has unexpected structure: { e } " , file = sys . stderr )
152
+ logger . warning (f"AsyncAnthropic class not found or has unexpected structure: { e } " )
152
153
153
154
# For compatibility, still try to patch the old-style acreate method if it exists
154
155
if hasattr (messages_class , "acreate" ):
155
156
original_acreate = messages_class .acreate
156
157
if not hasattr (original_acreate , "_osmosis_wrapped" ):
157
158
@functools .wraps (original_acreate )
158
159
async def wrapped_acreate (self , * args , ** kwargs ):
159
- print (f"Wrapped async create called with args: { args } , kwargs: { kwargs } " , file = sys . stderr )
160
+ logger . debug (f"Wrapped async create called with args: { args } , kwargs: { kwargs } " )
160
161
try :
161
162
# Check if the async call includes tool use parameters
162
163
has_tools = "tools" in kwargs
163
164
if has_tools :
164
- print (f"Async tool use detected with { len (kwargs ['tools' ])} tools" , file = sys . stderr )
165
+ logger . debug (f"Async tool use detected with { len (kwargs ['tools' ])} tools" )
165
166
166
167
if "messages" in kwargs :
167
168
for message in kwargs ["messages" ]:
168
169
if message .get ("role" ) == "user" and isinstance (message .get ("content" ), list ):
169
170
for content_item in message ["content" ]:
170
171
if isinstance (content_item , dict ) and content_item .get ("type" ) == "tool_response" :
171
- print ("Async tool response detected in messages" , file = sys . stderr )
172
+ logger . debug ("Async tool response detected in messages" )
172
173
break
173
174
174
175
response = await original_acreate (self , * args , ** kwargs )
175
176
176
177
if utils .enabled :
177
- print ("Sending async response to Hoover (success)" , file = sys . stderr )
178
+ logger . debug ("Sending async response to Hoover (success)" )
178
179
send_to_hoover (
179
180
query = kwargs ,
180
181
response = response .model_dump () if hasattr (response , 'model_dump' ) else response ,
@@ -183,9 +184,9 @@ async def wrapped_acreate(self, *args, **kwargs):
183
184
184
185
return response
185
186
except Exception as e :
186
- print (f"Error in wrapped async create: { e } " , file = sys . stderr )
187
+ logger . error (f"Error in wrapped async create: { e } " )
187
188
if utils .enabled :
188
- print ("Sending async error to Hoover" , file = sys . stderr )
189
+ logger . debug ("Sending async error to Hoover" )
189
190
error_response = {"error" : str (e )}
190
191
send_to_hoover (
191
192
query = kwargs ,
@@ -196,9 +197,9 @@ async def wrapped_acreate(self, *args, **kwargs):
196
197
197
198
wrapped_acreate ._osmosis_wrapped = True
198
199
messages_class .acreate = wrapped_acreate
199
- print ("Successfully wrapped Messages.acreate method" , file = sys . stderr )
200
+ logger . info ("Successfully wrapped Messages.acreate method" )
200
201
else :
201
- print ("No async acreate method found in Messages class" , file = sys . stderr )
202
+ logger . info ("No async acreate method found in Messages class" )
202
203
203
204
# Check if Completions exists and wrap it if it does
204
205
try :
@@ -229,12 +230,12 @@ def wrapped_completions_create(self, *args, **kwargs):
229
230
if not hasattr (original_completions_acreate , "_osmosis_wrapped" ):
230
231
@functools .wraps (original_completions_acreate )
231
232
async def wrapped_completions_acreate (self , * args , ** kwargs ):
232
- print (f"Wrapped Completions async create called with args: { args } , kwargs: { kwargs } " , file = sys . stderr )
233
+ logger . debug (f"Wrapped Completions async create called with args: { args } , kwargs: { kwargs } " )
233
234
try :
234
235
response = await original_completions_acreate (self , * args , ** kwargs )
235
236
236
237
if utils .enabled :
237
- print ("Sending Completions async response to Hoover (success)" , file = sys . stderr )
238
+ logger . debug ("Sending Completions async response to Hoover (success)" )
238
239
send_to_hoover (
239
240
query = kwargs ,
240
241
response = response .model_dump () if hasattr (response , 'model_dump' ) else response ,
@@ -243,9 +244,9 @@ async def wrapped_completions_acreate(self, *args, **kwargs):
243
244
244
245
return response
245
246
except Exception as e :
246
- print (f"Error in wrapped Completions async create: { e } " , file = sys . stderr )
247
+ logger . error (f"Error in wrapped Completions async create: { e } " )
247
248
if utils .enabled :
248
- print ("Sending Completions async error to Hoover" , file = sys . stderr )
249
+ logger . debug ("Sending Completions async error to Hoover" )
249
250
error_response = {"error" : str (e )}
250
251
send_to_hoover (
251
252
query = kwargs ,
@@ -256,15 +257,15 @@ async def wrapped_completions_acreate(self, *args, **kwargs):
256
257
257
258
wrapped_completions_acreate ._osmosis_wrapped = True
258
259
completions_class .acreate = wrapped_completions_acreate
259
- print ("Successfully wrapped Completions.acreate method" , file = sys . stderr )
260
+ logger . info ("Successfully wrapped Completions.acreate method" )
260
261
else :
261
- print ("Completions.acreate already wrapped" , file = sys . stderr )
262
+ logger . info ("Completions.acreate already wrapped" )
262
263
else :
263
- print ("No async acreate method found in Completions class" , file = sys . stderr )
264
+ logger . info ("No async acreate method found in Completions class" )
264
265
else :
265
- print ("Completions.create already wrapped" , file = sys . stderr )
266
+ logger . info ("Completions.create already wrapped" )
266
267
except (ImportError , AttributeError ) as e :
267
268
# Completions module may not exist in this version
268
- print (f"Completions module not found or has an unexpected structure: { e } " , file = sys . stderr )
269
+ logger . warning (f"Completions module not found or has an unexpected structure: { e } " )
269
270
270
- print ("Anthropic client has been wrapped by osmosis-wrap." , file = sys . stderr )
271
+ logger . info ("Anthropic client has been wrapped by osmosis-wrap." )
0 commit comments