Skip to content

Commit 7c3d5e3

Browse files
fix: don't require API key for local tools (#1122)
1 parent 27df7e5 commit 7c3d5e3

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

python/composio/client/collections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,4 +1538,8 @@ class Logs(Collection[LogRecord]):
15381538

15391539
def push(self, record: t.Dict) -> None:
15401540
"""Push logs to composio."""
1541+
# TODO: handle this better
1542+
if self.client._api_key is None: # pylint: disable=protected-access
1543+
return
1544+
15411545
self.client.http.post(url=str(self.endpoint), json=record)

python/composio/tools/env/base.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,11 @@ def __init__(self, config: WorkspaceConfigType):
159159
super().__init__()
160160
self.id = generate_id()
161161
self.access_token = uuid4().hex.replace("-", "")
162-
self.composio_api_key = _read_env_var(
163-
name=ENV_COMPOSIO_API_KEY,
164-
default=config.composio_api_key,
165-
)
166-
self.composio_base_url = _read_env_var(
167-
name=ENV_COMPOSIO_BASE_URL,
168-
default=config.composio_base_url,
169-
)
170-
self.github_access_token = config.github_access_token or os.environ.get(
171-
ENV_GITHUB_ACCESS_TOKEN, "NO_VALUE"
172-
)
162+
self.persistent = config.persistent
173163
self.environment = {
174164
**(config.environment or {}),
175-
ENV_COMPOSIO_API_KEY: self.composio_api_key,
176-
ENV_COMPOSIO_BASE_URL: self.composio_base_url,
177-
ENV_GITHUB_ACCESS_TOKEN: self.github_access_token,
178-
f"_COMPOSIO_{ENV_GITHUB_ACCESS_TOKEN}": self.github_access_token,
179165
ENV_ACCESS_TOKEN: self.access_token,
180166
}
181-
self.persistent = config.persistent
182167

183168
def __str__(self) -> str:
184169
"""String representation."""
@@ -220,6 +205,31 @@ def teardown(self) -> None:
220205
class RemoteWorkspace(Workspace):
221206
"""Remote workspace client."""
222207

208+
def __init__(self, config: WorkspaceConfigType):
209+
super().__init__(config)
210+
self.composio_api_key = _read_env_var(
211+
name=ENV_COMPOSIO_API_KEY,
212+
default=config.composio_api_key,
213+
)
214+
self.composio_base_url = _read_env_var(
215+
name=ENV_COMPOSIO_BASE_URL,
216+
default=config.composio_base_url,
217+
)
218+
self.github_access_token = (
219+
config.github_access_token
220+
if config.github_access_token is not None
221+
else os.environ.get(ENV_GITHUB_ACCESS_TOKEN, "NO_VALUE")
222+
)
223+
self.environment.update(
224+
{
225+
ENV_COMPOSIO_API_KEY: self.composio_api_key,
226+
ENV_COMPOSIO_BASE_URL: self.composio_base_url,
227+
ENV_GITHUB_ACCESS_TOKEN: self.github_access_token,
228+
f"_COMPOSIO_{ENV_GITHUB_ACCESS_TOKEN}": self.github_access_token,
229+
ENV_ACCESS_TOKEN: self.access_token,
230+
}
231+
)
232+
223233
def _request(
224234
self,
225235
endpoint: str,

python/composio/tools/toolset.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,20 @@ def workspace(self) -> Workspace:
418418

419419
workspace_config = self._workspace_config or HostWorkspaceConfig()
420420
if workspace_config.composio_api_key is None:
421-
workspace_config.composio_api_key = self.api_key
421+
try:
422+
workspace_config.composio_api_key = self.api_key
423+
except ApiKeyNotProvidedError:
424+
warnings.warn(
425+
"Running without a Composio API key", UserWarning, stacklevel=2
426+
)
422427

423428
if workspace_config.composio_base_url is None:
424429
workspace_config.composio_base_url = self._base_url
425430

426-
if workspace_config.github_access_token is None:
431+
if (
432+
workspace_config.github_access_token is None
433+
and workspace_config.composio_api_key is not None
434+
):
427435
workspace_config.github_access_token = (
428436
self._try_get_github_access_token_for_current_entity()
429437
)

python/tests/test_tools/test_env/test_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
from composio.client.enums import Action, ActionType, AppType, TagType
1010
from composio.exceptions import ComposioSDKError
1111
from composio.tools.env.base import (
12+
RemoteWorkspace,
1213
SessionFactory,
1314
Sessionable,
14-
Workspace,
1515
WorkspaceConfigType,
1616
)
1717
from composio.tools.env.id import generate_id
1818

1919

20-
def test_workspace() -> None:
21-
class TestWorkspace(Workspace):
20+
def test_remote_workspace() -> None:
21+
class TestRemoteWorkspace(RemoteWorkspace):
2222
def check_for_missing_dependencies(
2323
self,
2424
apps: t.Optional[t.Sequence[AppType]] = None,
@@ -46,7 +46,7 @@ def teardown(self) -> None:
4646
ComposioSDKError,
4747
match="Please provide value for `COMPOSIO_API_KEY`",
4848
):
49-
_ = TestWorkspace(config=WorkspaceConfigType())
49+
_ = TestRemoteWorkspace(config=WorkspaceConfigType())
5050

5151

5252
def test_sessionable() -> None:

python/tests/test_tools/test_toolset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ def _patch(*_, **kwargs):
154154
)
155155

156156

157-
def test_api_key_missing() -> None:
157+
def test_api_key_missing(monkeypatch: pytest.MonkeyPatch) -> None:
158+
monkeypatch.setenv("COMPOSIO_API_KEY", "")
158159
toolset = ComposioToolSet()
159-
toolset._api_key = None # pylint: disable=protected-access
160160
with pytest.raises(
161161
ApiKeyNotProvidedError,
162162
match=(
163163
"API Key not provided, either provide API key or export it as "
164164
"`COMPOSIO_API_KEY` or run `composio login`"
165165
),
166166
):
167-
_ = toolset.workspace
167+
_ = toolset.execute_action(Action.HACKERNEWS_GET_FRONTPAGE, {})
168168

169169

170170
def test_processors(monkeypatch: pytest.MonkeyPatch) -> None:

0 commit comments

Comments
 (0)