Skip to content

Commit 8ad78c5

Browse files
authored
(fix): AgentOps is only instantiated if an api key is passed (#22)
1 parent 1fd8d3c commit 8ad78c5

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/multion/client.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# this is used as the default value for optional parameters
1616
OMIT = typing.cast(typing.Any, ...)
1717

18+
record_function = lambda *args, **kwargs: lambda fn: fn
19+
1820

1921
class MultiOn(BaseMultiOn):
2022
"""
@@ -52,25 +54,37 @@ def __init__(
5254
agentops_api_key: typing.Optional[str] = os.getenv("AGENTOPS_API_KEY"),
5355
**kwargs
5456
):
57+
5558
super().__init__(*args, **kwargs)
56-
self.sessions = WrappedSessionsClient(client_wrapper=self._client_wrapper)
57-
if agentops_api_key is not None:
59+
self._agentops_api_key = agentops_api_key
60+
61+
self.sessions = WrappedSessionsClient(
62+
client_wrapper=self._client_wrapper, use_agentops=self._agentops_api_key is not None)
63+
64+
if self._agentops_api_key is not None:
5865
agentops.init(
5966
api_key=agentops_api_key,
6067
parent_key=os.getenv("AGENTOPS_PARENT_KEY"),
6168
auto_start_session=False,
6269
)
70+
# Redefine the record_function globally
71+
global record_function
72+
record_function = agentops.record_function
73+
# Apply the decorator at runtime
74+
self.browse = record_function(event_name="browse")(self.browse)
75+
self.retrieve = record_function(
76+
event_name="retrieve")(self.retrieve)
6377

64-
@agentops.record_function(event_name="browse") # type: ignore
6578
@wraps_function(BaseMultiOn.browse)
6679
def browse(self, *args, **kwargs) -> BrowseOutput:
67-
agentops.start_session(tags=["multion-sdk"])
80+
if self._agentops_api_key is not None:
81+
agentops.start_session(tags=["multion-sdk"])
6882
return super().browse(*args, **kwargs)
6983

70-
@agentops.record_function(event_name="retrieve") # type: ignore
7184
@wraps_function(BaseMultiOn.retrieve)
7285
def retrieve(self, *args, **kwargs) -> RetrieveOutput:
73-
agentops.start_session(tags=["multion-sdk"])
86+
if self._agentops_api_key is not None:
87+
agentops.start_session(tags=["multion-sdk"])
7488
return super().retrieve(*args, **kwargs)
7589

7690

@@ -111,22 +125,31 @@ def __init__(
111125
**kwargs
112126
):
113127
super().__init__(*args, **kwargs)
114-
self.sessions = WrappedAsyncSessionsClient(client_wrapper=self._client_wrapper)
128+
self._agentops_api_key = agentops_api_key
129+
self.sessions = WrappedAsyncSessionsClient(client_wrapper=self._client_wrapper,
130+
use_agentops=self._agentops_api_key is not None)
115131
if agentops_api_key is not None:
116132
agentops.init(
117133
api_key=agentops_api_key,
118134
parent_key=os.getenv("AGENTOPS_PARENT_KEY"),
119135
auto_start_session=False,
120136
)
137+
# If agentops is set, we need to wrap the record function
138+
global record_function
139+
record_function = agentops.record_function
140+
141+
record_function = agentops.record_function
121142

122-
@agentops.record_function(event_name="browse") # type: ignore
143+
@record_function(event_name="browse") # type: ignore
123144
@wraps_function(AsyncBaseMultiOn.browse)
124145
async def browse(self, *args, **kwargs) -> BrowseOutput:
125-
agentops.start_session(tags=["multion-sdk"])
146+
if self._agentops_api_key is not None:
147+
agentops.start_session(tags=["multion-sdk"])
126148
return await super().browse(*args, **kwargs)
127149

128-
@agentops.record_function(event_name="retrieve") # type: ignore
150+
@record_function(event_name="retrieve") # type: ignore
129151
@wraps_function(BaseMultiOn.retrieve)
130152
async def retrieve(self, *args, **kwargs) -> RetrieveOutput:
131-
agentops.start_session(tags=["multion-sdk"])
153+
if self._agentops_api_key is not None:
154+
agentops.start_session(tags=["multion-sdk"])
132155
return await super().retrieve(*args, **kwargs)

src/multion/sessions/wrapped_client.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ..wrappers import wraps_function
12
from multion.sessions.client import AsyncSessionsClient, SessionsClient
23
import agentops # type: ignore
34
from agentops import ActionEvent, LLMEvent, ErrorEvent # type: ignore
@@ -11,13 +12,16 @@
1112
# this is used as the default value for optional parameters
1213
OMIT = typing.cast(typing.Any, ...)
1314

14-
from ..wrappers import wraps_function
15-
1615

1716
class WrappedSessionsClient(SessionsClient):
17+
def __init__(self, use_agentops: bool = False, *args, **kwargs):
18+
super().__init__(*args, **kwargs)
19+
self.use_agentops = use_agentops
20+
1821
@wraps_function(SessionsClient.create) # type: ignore
1922
def create(self, *args, **kwargs) -> SessionCreated:
20-
agentops.start_session(tags=["multion-sdk"])
23+
if self.use_agentops:
24+
agentops.start_session(tags=["multion-sdk"])
2125
try:
2226
return super().create(*args, **kwargs)
2327
except Exception as e:
@@ -68,9 +72,14 @@ def close(self, *args, **kwargs) -> SessionsCloseResponse:
6872

6973

7074
class WrappedAsyncSessionsClient(AsyncSessionsClient):
75+
def __init__(self, use_agentops: bool = False, *args, **kwargs):
76+
super().__init__(*args, **kwargs)
77+
self.use_agentops = use_agentops
78+
7179
@wraps_function(AsyncSessionsClient.create) # type: ignore
7280
async def create(self, *args, **kwargs) -> SessionCreated:
73-
agentops.start_session(tags=["multion-sdk"])
81+
if self.use_agentops:
82+
agentops.start_session(tags=["multion-sdk"])
7483
try:
7584
return await super().create(*args, **kwargs)
7685
except Exception as e:

0 commit comments

Comments
 (0)