Skip to content

Commit

Permalink
fix: create local cache if running App.all() on a fresh install (#1085
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tushar-composio authored Dec 24, 2024
1 parent 7c89483 commit 0717ec8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
10 changes: 9 additions & 1 deletion python/composio/client/enums/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ def iter(cls) -> t.Iterator[str]:
# If we try to fetch Actions.iter() with local caching disabled
# for example, we'd get here.
if not path.exists():
return
# pylint: disable=import-outside-toplevel
from composio.client import Composio

# pylint: disable=import-outside-toplevel
from composio.client.utils import check_cache_refresh

check_cache_refresh(Composio.get_latest())
if not path.exists():
return

yield from os.listdir(path)

Expand Down
6 changes: 6 additions & 0 deletions python/composio/client/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import typing as t

from composio.client import Composio, enums
Expand All @@ -14,6 +15,8 @@

logger = get_logger(__name__)

NO_CACHE_REFRESH = os.getenv("COMPOSIO_NO_CACHE_REFRESH", "false") == "true"


def filter_non_beta_items(items: t.Sequence[EnumModels]) -> t.List:
filtered_items: t.List[EnumModels] = []
Expand Down Expand Up @@ -215,6 +218,9 @@ def check_cache_refresh(client: Composio) -> None:
SDK version, and didn't come from the API. We need to start storing the data
from the API and invalidate the cache if the data is not already stored.
"""
if NO_CACHE_REFRESH:
return

if enums.base.ACTIONS_CACHE.exists():
first_file = next(enums.base.ACTIONS_CACHE.iterdir(), None)
if first_file is not None:
Expand Down
11 changes: 7 additions & 4 deletions python/composio/tools/local/sqltool/actions/sql_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sqlite3
import sqlalchemy
from pathlib import Path
from typing import Dict

Expand Down Expand Up @@ -42,11 +41,13 @@ def _is_sqlite_connection(self, connection_string: str) -> bool:

def execute(self, request: SqlQueryRequest, metadata: Dict) -> SqlQueryResponse:
"""Execute SQL query for either SQLite or remote databases"""
import sqlalchemy.exc # pylint: disable=import-outside-toplevel

try:
if self._is_sqlite_connection(request.connection_string):
return self._execute_sqlite(request)
else:
return self._execute_remote(request)

return self._execute_remote(request)
except sqlite3.Error as e:
raise ValueError(f"SQLite database error: {str(e)}") from e
except sqlalchemy.exc.SQLAlchemyError as e:
Expand All @@ -61,7 +62,7 @@ def _execute_sqlite(self, request: SqlQueryRequest) -> SqlQueryResponse:
raise ValueError(f"Error: Database file '{db_path}' does not exist.")
with sqlite3.connect(db_path) as connection:
cursor = connection.cursor()
cursor.execute(request.query, {})
cursor.execute(request.query)
response_data = [list(row) for row in cursor.fetchall()]
connection.commit()
return SqlQueryResponse(
Expand All @@ -71,6 +72,8 @@ def _execute_sqlite(self, request: SqlQueryRequest) -> SqlQueryResponse:

def _execute_remote(self, request: SqlQueryRequest) -> SqlQueryResponse:
"""Execute query for remote databases"""
import sqlalchemy # pylint: disable=import-outside-toplevel

engine = sqlalchemy.create_engine(
request.connection_string,
pool_size=5,
Expand Down
2 changes: 2 additions & 0 deletions python/composio/tools/local/sqltool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Sqltool(LocalTool, autoload=True):

logo = "https://raw.githubusercontent.com/ComposioHQ/composio/master/python/docs/imgs/logos/sqltool.png"

requires = ["sqlalchemy>=2.0"]

@classmethod
def actions(cls) -> list[Type[LocalAction]]:
return [SqlQuery]
6 changes: 1 addition & 5 deletions python/composio/tools/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@
ProcessorType = te.Literal["pre", "post", "schema"]


NO_CACHE_REFRESH = os.environ.get("COMPOSIO_NO_CACHE_REFRESH", "false") == "true"


class IntegrationParams(te.TypedDict):

integration_id: str
Expand Down Expand Up @@ -374,8 +371,7 @@ def client(self) -> Composio:
base_url=self._base_url,
runtime=self._runtime,
)
if not NO_CACHE_REFRESH:
check_cache_refresh(self._remote_client)
check_cache_refresh(self._remote_client)

self._remote_client.local = self._local_client
return self._remote_client
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_tools/test_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_find_actions_by_tags() -> None:
App.SLACK, App.GITHUB, tags=["important"]
):
assert "important" in action.tags
assert action.app in ("github", "slack", "slackbot")
assert action.app in ("GITHUB", "SLACK", "SLACKBOT")


def test_uninitialize_app() -> None:
Expand Down

0 comments on commit 0717ec8

Please sign in to comment.