|
| 1 | +from abc import abstractmethod |
| 2 | +from enum import Enum |
| 3 | +from typing import Dict, List, Optional |
| 4 | + |
| 5 | +from pydantic import BaseModel, Field, validator |
| 6 | + |
| 7 | +from models.config import ConfigModel |
| 8 | +from typings.agent import AgentOutput, ConfigsOutput |
| 9 | +from typings.config import ConfigQueryParams |
| 10 | + |
| 11 | + |
| 12 | +class IntegrationEnvKeyType(Enum): |
| 13 | + STRING = "string" |
| 14 | + FILE = "file" |
| 15 | + INT = "int" |
| 16 | + |
| 17 | + def __str__(self): |
| 18 | + return self.value |
| 19 | + |
| 20 | + |
| 21 | +class IntegrationEnvKey(BaseModel): |
| 22 | + label: str = Field() |
| 23 | + key: str = Field() |
| 24 | + key_type: IntegrationEnvKeyType = Field(default=IntegrationEnvKeyType.STRING) |
| 25 | + is_required: bool = Field(default=False) |
| 26 | + is_secret: bool = Field(default=False) |
| 27 | + default_value: str = Field(default=None) |
| 28 | + |
| 29 | + @validator("is_secret", "is_required", pre=True, always=True) |
| 30 | + def check_bool(cls, v): |
| 31 | + """Check if the value is a boolean.""" |
| 32 | + if v is None: |
| 33 | + return False |
| 34 | + elif isinstance(v, bool): |
| 35 | + return v |
| 36 | + else: |
| 37 | + raise ValueError("Value should be a boolean") |
| 38 | + |
| 39 | + @validator("key_type", pre=True, always=True) |
| 40 | + def check_key_type(cls, v): |
| 41 | + """Check if the value is a boolean.""" |
| 42 | + if v is None: |
| 43 | + return IntegrationEnvKeyType.STRING |
| 44 | + elif isinstance(v, IntegrationEnvKeyType): |
| 45 | + return v |
| 46 | + else: |
| 47 | + raise ValueError("key_type should be string/file/integer") |
| 48 | + |
| 49 | + |
| 50 | +class BaseIntegrationTools: |
| 51 | + id: str |
| 52 | + configs: Dict[str, str] = {} |
| 53 | + settings: Optional[ConfigsOutput] = None |
| 54 | + voice_slug: Optional[str] = None |
| 55 | + agent: Optional[AgentOutput] = None |
| 56 | + |
| 57 | + def get_env_key(self, key: str): |
| 58 | + return self.configs.get(key) |
| 59 | + |
| 60 | + |
| 61 | +class BaseIntegration(BaseModel): |
| 62 | + id: str |
| 63 | + name: str |
| 64 | + description: str |
| 65 | + slug: str |
| 66 | + is_active: bool = Field(default=True) |
| 67 | + |
| 68 | + def get_tools_with_configs( |
| 69 | + self, db, account, settings, agent_with_configs |
| 70 | + ) -> List[BaseIntegrationTools]: |
| 71 | + configs = ConfigModel.get_configs( |
| 72 | + db=db, query=ConfigQueryParams(toolkit_id=self.id), account=account |
| 73 | + ) |
| 74 | + config_dict = {config.key: config.value for config in configs} |
| 75 | + tools = self.get_tools() |
| 76 | + |
| 77 | + for tool in tools: |
| 78 | + tool.configs = config_dict |
| 79 | + tool.voice_slug = self.slug |
| 80 | + tool.settings = settings |
| 81 | + tool.account = account |
| 82 | + |
| 83 | + return tools |
| 84 | + |
| 85 | + @abstractmethod |
| 86 | + def get_env_keys(self) -> List[IntegrationEnvKey]: |
| 87 | + # Add file related config keys here |
| 88 | + pass |
0 commit comments