2023-07-27 13:08:57 +08:00
|
|
|
import json
|
|
|
|
from enum import Enum
|
|
|
|
|
2024-02-06 13:21:13 +08:00
|
|
|
from extensions.ext_database import db
|
2024-08-13 14:44:10 +08:00
|
|
|
|
|
|
|
from .types import StringUUID
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2023-07-27 13:08:57 +08:00
|
|
|
|
|
|
|
class ToolProviderName(Enum):
|
2024-09-10 17:08:06 +08:00
|
|
|
SERPAPI = "serpapi"
|
2023-07-27 13:08:57 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def value_of(value):
|
|
|
|
for member in ToolProviderName:
|
|
|
|
if member.value == value:
|
|
|
|
return member
|
|
|
|
raise ValueError(f"No matching enum found for value '{value}'")
|
|
|
|
|
|
|
|
|
|
|
|
class ToolProvider(db.Model):
|
2024-09-10 17:08:06 +08:00
|
|
|
__tablename__ = "tool_providers"
|
2023-07-27 13:08:57 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tool_provider_pkey"),
|
|
|
|
db.UniqueConstraint("tenant_id", "tool_name", name="unique_tool_provider_tool_name"),
|
2023-07-27 13:08:57 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-04-29 11:58:17 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
2023-07-27 13:08:57 +08:00
|
|
|
tool_name = db.Column(db.String(40), nullable=False)
|
|
|
|
encrypted_credentials = db.Column(db.Text, nullable=True)
|
2024-09-10 17:08:06 +08:00
|
|
|
is_enabled = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2023-07-27 13:08:57 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def credentials_is_set(self):
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
Returns True if the encrypted_config is not None, indicating that the token is set.
|
|
|
|
"""
|
2023-07-27 13:08:57 +08:00
|
|
|
return self.encrypted_credentials is not None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def credentials(self):
|
|
|
|
"""
|
|
|
|
Returns the decrypted config.
|
|
|
|
"""
|
|
|
|
return json.loads(self.encrypted_credentials) if self.encrypted_credentials is not None else None
|