suna/backend/composio_integration/auth_config_service.py

96 lines
3.7 KiB
Python
Raw Normal View History

2025-08-03 04:10:11 +08:00
from typing import Optional, List
from composio_client import Composio
2025-08-02 16:22:17 +08:00
from utils.logger import logger
2025-08-03 04:10:11 +08:00
from pydantic import BaseModel
2025-08-02 16:22:17 +08:00
from .client import ComposioClient
class AuthConfig(BaseModel):
id: str
auth_scheme: str
2025-08-03 04:10:11 +08:00
is_composio_managed: bool = True
2025-08-02 16:22:17 +08:00
restrict_to_following_tools: List[str] = []
toolkit_slug: str
class AuthConfigService:
def __init__(self, api_key: Optional[str] = None):
self.client = ComposioClient.get_client(api_key)
async def create_auth_config(self, toolkit_slug: str) -> AuthConfig:
try:
logger.info(f"Creating auth config for toolkit: {toolkit_slug}")
response = self.client.auth_configs.create(
toolkit={"slug": toolkit_slug}
)
2025-08-03 04:10:11 +08:00
# Access Pydantic model attributes directly
auth_config_obj = response.auth_config
2025-08-02 16:22:17 +08:00
auth_config = AuthConfig(
2025-08-03 04:10:11 +08:00
id=auth_config_obj.id,
auth_scheme=auth_config_obj.auth_scheme,
is_composio_managed=getattr(auth_config_obj, 'is_composio_managed', True),
restrict_to_following_tools=getattr(auth_config_obj, 'restrict_to_following_tools', []),
2025-08-02 16:22:17 +08:00
toolkit_slug=toolkit_slug
)
logger.info(f"Successfully created auth config: {auth_config.id}")
return auth_config
except Exception as e:
logger.error(f"Failed to create auth config for {toolkit_slug}: {e}", exc_info=True)
raise
async def get_auth_config(self, auth_config_id: str) -> Optional[AuthConfig]:
try:
logger.info(f"Fetching auth config: {auth_config_id}")
2025-08-03 04:10:11 +08:00
response = self.client.auth_configs.get(auth_config_id)
if not response:
return None
# Access Pydantic model attributes directly
return AuthConfig(
id=response.id,
auth_scheme=response.auth_scheme,
is_composio_managed=getattr(response, 'is_composio_managed', True),
restrict_to_following_tools=getattr(response, 'restrict_to_following_tools', []),
toolkit_slug=getattr(response, 'toolkit_slug', '')
)
2025-08-02 16:22:17 +08:00
except Exception as e:
logger.error(f"Failed to get auth config {auth_config_id}: {e}", exc_info=True)
raise
2025-08-03 04:10:11 +08:00
async def list_auth_configs(self, toolkit_slug: Optional[str] = None) -> List[AuthConfig]:
2025-08-02 16:22:17 +08:00
try:
2025-08-03 04:10:11 +08:00
logger.info(f"Listing auth configs for toolkit: {toolkit_slug}")
2025-08-02 16:22:17 +08:00
2025-08-03 04:10:11 +08:00
if toolkit_slug:
response = self.client.auth_configs.list(toolkit=toolkit_slug)
else:
response = self.client.auth_configs.list()
2025-08-02 16:22:17 +08:00
2025-08-03 04:10:11 +08:00
auth_configs = []
items = getattr(response, 'items', [])
for item in items:
auth_config = AuthConfig(
id=item.id,
auth_scheme=item.auth_scheme,
is_composio_managed=getattr(item, 'is_composio_managed', True),
restrict_to_following_tools=getattr(item, 'restrict_to_following_tools', []),
toolkit_slug=getattr(item, 'toolkit_slug', toolkit_slug or '')
)
auth_configs.append(auth_config)
2025-08-02 16:22:17 +08:00
2025-08-03 04:10:11 +08:00
logger.info(f"Successfully listed {len(auth_configs)} auth configs")
return auth_configs
2025-08-02 16:22:17 +08:00
except Exception as e:
2025-08-03 04:10:11 +08:00
logger.error(f"Failed to list auth configs: {e}", exc_info=True)
2025-08-02 16:22:17 +08:00
raise