2024-01-23 19:58:23 +08:00
|
|
|
import json
|
2025-02-17 17:05:13 +08:00
|
|
|
from datetime import datetime
|
2025-05-06 18:05:19 +08:00
|
|
|
from typing import Any, cast
|
2025-07-10 14:01:34 +08:00
|
|
|
from urllib.parse import urlparse
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-11-01 15:51:22 +08:00
|
|
|
import sqlalchemy as sa
|
2025-02-17 17:05:13 +08:00
|
|
|
from deprecated import deprecated
|
2024-12-21 23:13:58 +08:00
|
|
|
from sqlalchemy import ForeignKey, func
|
2024-10-21 10:43:49 +08:00
|
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2025-07-10 14:01:34 +08:00
|
|
|
from core.file import helpers as file_helpers
|
|
|
|
from core.helper import encrypter
|
|
|
|
from core.mcp.types import Tool
|
2024-01-23 19:58:23 +08:00
|
|
|
from core.tools.entities.common_entities import I18nObject
|
2024-05-27 22:01:11 +08:00
|
|
|
from core.tools.entities.tool_bundle import ApiToolBundle
|
|
|
|
from core.tools.entities.tool_entities import ApiProviderSchemaType, WorkflowToolParameterConfiguration
|
2025-02-17 17:05:13 +08:00
|
|
|
from models.base import Base
|
2024-08-13 14:44:10 +08:00
|
|
|
|
2024-12-20 14:12:29 +08:00
|
|
|
from .engine import db
|
2024-08-13 14:44:10 +08:00
|
|
|
from .model import Account, App, Tenant
|
|
|
|
from .types import StringUUID
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
2025-07-17 17:18:44 +08:00
|
|
|
# system level tool oauth client params (client_id, client_secret, etc.)
|
|
|
|
class ToolOAuthSystemClient(Base):
|
|
|
|
__tablename__ = "tool_oauth_system_clients"
|
|
|
|
__table_args__ = (
|
|
|
|
db.PrimaryKeyConstraint("id", name="tool_oauth_system_client_pkey"),
|
|
|
|
db.UniqueConstraint("plugin_id", "provider", name="tool_oauth_system_client_plugin_id_provider_idx"),
|
|
|
|
)
|
|
|
|
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
|
|
|
plugin_id: Mapped[str] = mapped_column(db.String(512), nullable=False)
|
|
|
|
provider: Mapped[str] = mapped_column(db.String(255), nullable=False)
|
|
|
|
# oauth params of the tool provider
|
|
|
|
encrypted_oauth_params: Mapped[str] = mapped_column(db.Text, nullable=False)
|
|
|
|
|
|
|
|
|
|
|
|
# tenant level tool oauth client params (client_id, client_secret, etc.)
|
|
|
|
class ToolOAuthTenantClient(Base):
|
|
|
|
__tablename__ = "tool_oauth_tenant_clients"
|
|
|
|
__table_args__ = (
|
|
|
|
db.PrimaryKeyConstraint("id", name="tool_oauth_tenant_client_pkey"),
|
|
|
|
db.UniqueConstraint("tenant_id", "plugin_id", "provider", name="unique_tool_oauth_tenant_client"),
|
|
|
|
)
|
|
|
|
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
|
|
|
# tenant id
|
|
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
|
|
plugin_id: Mapped[str] = mapped_column(db.String(512), nullable=False)
|
|
|
|
provider: Mapped[str] = mapped_column(db.String(255), nullable=False)
|
|
|
|
enabled: Mapped[bool] = mapped_column(db.Boolean, nullable=False, server_default=db.text("true"))
|
|
|
|
# oauth params of the tool provider
|
|
|
|
encrypted_oauth_params: Mapped[str] = mapped_column(db.Text, nullable=False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def oauth_params(self) -> dict:
|
|
|
|
return cast(dict, json.loads(self.encrypted_oauth_params or "{}"))
|
|
|
|
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class BuiltinToolProvider(Base):
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
This table stores the tool provider information for built-in tools for each tenant.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "tool_builtin_providers"
|
2024-01-23 19:58:23 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tool_builtin_provider_pkey"),
|
2025-07-17 17:18:44 +08:00
|
|
|
db.UniqueConstraint("tenant_id", "provider", "name", name="unique_builtin_tool_provider"),
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# id of the tool provider
|
2025-02-17 17:05:13 +08:00
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2025-07-17 17:18:44 +08:00
|
|
|
name: Mapped[str] = mapped_column(
|
|
|
|
db.String(256), nullable=False, server_default=db.text("'API KEY 1'::character varying")
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
# id of the tenant
|
2025-02-17 17:05:13 +08:00
|
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
# who created this tool provider
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# name of the tool provider
|
2025-02-17 17:05:13 +08:00
|
|
|
provider: Mapped[str] = mapped_column(db.String(256), nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# credential of the tool provider
|
2025-02-17 17:05:13 +08:00
|
|
|
encrypted_credentials: Mapped[str] = mapped_column(db.Text, nullable=True)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
|
|
db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)")
|
|
|
|
)
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
|
|
db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)")
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
2025-07-17 17:18:44 +08:00
|
|
|
is_default: Mapped[bool] = mapped_column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
# credential type, e.g., "api-key", "oauth2"
|
|
|
|
credential_type: Mapped[str] = mapped_column(
|
|
|
|
db.String(32), nullable=False, server_default=db.text("'api-key'::character varying")
|
|
|
|
)
|
2025-07-23 13:12:39 +08:00
|
|
|
expires_at: Mapped[int] = mapped_column(db.BigInteger, nullable=False, server_default=db.text("-1"))
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
@property
|
2025-02-17 17:05:13 +08:00
|
|
|
def credentials(self) -> dict:
|
|
|
|
return cast(dict, json.loads(self.encrypted_credentials))
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class ApiToolProvider(Base):
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
The table stores the api providers.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "tool_api_providers"
|
2024-01-23 19:58:23 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tool_api_provider_pkey"),
|
|
|
|
db.UniqueConstraint("name", "tenant_id", name="unique_api_tool_provider"),
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
2025-07-23 01:39:59 +09:00
|
|
|
id = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-01-23 19:58:23 +08:00
|
|
|
# name of the api provider
|
2025-07-23 01:39:59 +09:00
|
|
|
name = mapped_column(db.String(255), nullable=False, server_default=db.text("'API KEY 1'::character varying"))
|
2024-01-23 19:58:23 +08:00
|
|
|
# icon
|
2025-07-23 01:39:59 +09:00
|
|
|
icon = mapped_column(db.String(255), nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# original schema
|
2025-07-23 01:39:59 +09:00
|
|
|
schema = mapped_column(db.Text, nullable=False)
|
|
|
|
schema_type_str: Mapped[str] = mapped_column(db.String(40), nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# who created this tool
|
2025-07-23 01:39:59 +09:00
|
|
|
user_id = mapped_column(StringUUID, nullable=False)
|
2024-01-31 11:58:07 +08:00
|
|
|
# tenant id
|
2025-07-23 01:39:59 +09:00
|
|
|
tenant_id = mapped_column(StringUUID, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# description of the provider
|
2025-07-23 01:39:59 +09:00
|
|
|
description = mapped_column(db.Text, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# json format tools
|
2025-07-23 01:39:59 +09:00
|
|
|
tools_str = mapped_column(db.Text, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# json format credentials
|
2025-07-23 01:39:59 +09:00
|
|
|
credentials_str = mapped_column(db.Text, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# privacy policy
|
2025-07-23 01:39:59 +09:00
|
|
|
privacy_policy = mapped_column(db.String(255), nullable=True)
|
2024-05-18 04:52:48 +02:00
|
|
|
# custom_disclaimer
|
2024-11-01 15:51:22 +08:00
|
|
|
custom_disclaimer: Mapped[str] = mapped_column(sa.TEXT, default="")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
created_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def schema_type(self) -> ApiProviderSchemaType:
|
|
|
|
return ApiProviderSchemaType.value_of(self.schema_type_str)
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
@property
|
2024-05-27 22:01:11 +08:00
|
|
|
def tools(self) -> list[ApiToolBundle]:
|
|
|
|
return [ApiToolBundle(**tool) for tool in json.loads(self.tools_str)]
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
@property
|
|
|
|
def credentials(self) -> dict:
|
2024-12-24 18:38:51 +08:00
|
|
|
return dict(json.loads(self.credentials_str))
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
@property
|
2024-10-21 10:43:49 +08:00
|
|
|
def user(self) -> Account | None:
|
2025-03-12 16:34:56 +08:00
|
|
|
if not self.user_id:
|
|
|
|
return None
|
2025-07-24 01:57:45 +09:00
|
|
|
return db.session.query(Account).where(Account.id == self.user_id).first()
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
@property
|
2024-10-21 10:43:49 +08:00
|
|
|
def tenant(self) -> Tenant | None:
|
2025-07-24 01:57:45 +09:00
|
|
|
return db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class ToolLabelBinding(Base):
|
2024-05-27 22:01:11 +08:00
|
|
|
"""
|
|
|
|
The table stores the labels for tools.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "tool_label_bindings"
|
2024-05-27 22:01:11 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tool_label_bind_pkey"),
|
|
|
|
db.UniqueConstraint("tool_id", "label_name", name="unique_tool_label_bind"),
|
2024-05-27 22:01:11 +08:00
|
|
|
)
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-05-27 22:01:11 +08:00
|
|
|
# tool id
|
2025-02-17 17:05:13 +08:00
|
|
|
tool_id: Mapped[str] = mapped_column(db.String(64), nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# tool type
|
2025-02-17 17:05:13 +08:00
|
|
|
tool_type: Mapped[str] = mapped_column(db.String(40), nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# label name
|
2025-02-17 17:05:13 +08:00
|
|
|
label_name: Mapped[str] = mapped_column(db.String(40), nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class WorkflowToolProvider(Base):
|
2024-05-27 22:01:11 +08:00
|
|
|
"""
|
|
|
|
The table stores the workflow providers.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "tool_workflow_providers"
|
2024-05-27 22:01:11 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tool_workflow_provider_pkey"),
|
|
|
|
db.UniqueConstraint("name", "tenant_id", name="unique_workflow_tool_provider"),
|
|
|
|
db.UniqueConstraint("tenant_id", "app_id", name="unique_workflow_tool_provider_app_id"),
|
2024-05-27 22:01:11 +08:00
|
|
|
)
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-05-27 22:01:11 +08:00
|
|
|
# name of the workflow provider
|
2025-03-07 12:15:52 +08:00
|
|
|
name: Mapped[str] = mapped_column(db.String(255), nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# label of the workflow provider
|
2025-02-17 17:05:13 +08:00
|
|
|
label: Mapped[str] = mapped_column(db.String(255), nullable=False, server_default="")
|
2024-05-27 22:01:11 +08:00
|
|
|
# icon
|
2025-02-17 17:05:13 +08:00
|
|
|
icon: Mapped[str] = mapped_column(db.String(255), nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# app id of the workflow provider
|
2025-02-17 17:05:13 +08:00
|
|
|
app_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# version of the workflow provider
|
2025-02-17 17:05:13 +08:00
|
|
|
version: Mapped[str] = mapped_column(db.String(255), nullable=False, server_default="")
|
2024-05-27 22:01:11 +08:00
|
|
|
# who created this tool
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# tenant id
|
2025-02-17 17:05:13 +08:00
|
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# description of the provider
|
2025-02-17 17:05:13 +08:00
|
|
|
description: Mapped[str] = mapped_column(db.Text, nullable=False)
|
2024-05-27 22:01:11 +08:00
|
|
|
# parameter configuration
|
2025-02-17 17:05:13 +08:00
|
|
|
parameter_configuration: Mapped[str] = mapped_column(db.Text, nullable=False, server_default="[]")
|
2024-05-27 22:01:11 +08:00
|
|
|
# privacy policy
|
2025-02-17 17:05:13 +08:00
|
|
|
privacy_policy: Mapped[str] = mapped_column(db.String(255), nullable=True, server_default="")
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
|
|
db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)")
|
|
|
|
)
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
|
|
db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)")
|
|
|
|
)
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
@property
|
2024-10-21 10:43:49 +08:00
|
|
|
def user(self) -> Account | None:
|
2025-07-24 01:57:45 +09:00
|
|
|
return db.session.query(Account).where(Account.id == self.user_id).first()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
|
|
|
@property
|
2024-10-21 10:43:49 +08:00
|
|
|
def tenant(self) -> Tenant | None:
|
2025-07-24 01:57:45 +09:00
|
|
|
return db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
@property
|
|
|
|
def parameter_configurations(self) -> list[WorkflowToolParameterConfiguration]:
|
2024-09-10 17:08:06 +08:00
|
|
|
return [WorkflowToolParameterConfiguration(**config) for config in json.loads(self.parameter_configuration)]
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
@property
|
2024-10-21 10:43:49 +08:00
|
|
|
def app(self) -> App | None:
|
2025-07-24 01:57:45 +09:00
|
|
|
return db.session.query(App).where(App.id == self.app_id).first()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2025-07-10 14:01:34 +08:00
|
|
|
class MCPToolProvider(Base):
|
|
|
|
"""
|
|
|
|
The table stores the mcp providers.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__tablename__ = "tool_mcp_providers"
|
|
|
|
__table_args__ = (
|
|
|
|
db.PrimaryKeyConstraint("id", name="tool_mcp_provider_pkey"),
|
|
|
|
db.UniqueConstraint("tenant_id", "server_url_hash", name="unique_mcp_provider_server_url"),
|
|
|
|
db.UniqueConstraint("tenant_id", "name", name="unique_mcp_provider_name"),
|
|
|
|
db.UniqueConstraint("tenant_id", "server_identifier", name="unique_mcp_provider_server_identifier"),
|
|
|
|
)
|
|
|
|
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
|
|
|
# name of the mcp provider
|
|
|
|
name: Mapped[str] = mapped_column(db.String(40), nullable=False)
|
|
|
|
# server identifier of the mcp provider
|
2025-07-21 07:28:10 +05:30
|
|
|
server_identifier: Mapped[str] = mapped_column(db.String(64), nullable=False)
|
2025-07-10 14:01:34 +08:00
|
|
|
# encrypted url of the mcp provider
|
|
|
|
server_url: Mapped[str] = mapped_column(db.Text, nullable=False)
|
|
|
|
# hash of server_url for uniqueness check
|
|
|
|
server_url_hash: Mapped[str] = mapped_column(db.String(64), nullable=False)
|
|
|
|
# icon of the mcp provider
|
|
|
|
icon: Mapped[str] = mapped_column(db.String(255), nullable=True)
|
|
|
|
# tenant id
|
|
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
|
|
# who created this tool
|
|
|
|
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
|
|
# encrypted credentials
|
|
|
|
encrypted_credentials: Mapped[str] = mapped_column(db.Text, nullable=True)
|
|
|
|
# authed
|
|
|
|
authed: Mapped[bool] = mapped_column(db.Boolean, nullable=False, default=False)
|
|
|
|
# tools
|
|
|
|
tools: Mapped[str] = mapped_column(db.Text, nullable=False, default="[]")
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
|
|
db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)")
|
|
|
|
)
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
|
|
db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)")
|
|
|
|
)
|
|
|
|
|
|
|
|
def load_user(self) -> Account | None:
|
2025-07-24 01:57:45 +09:00
|
|
|
return db.session.query(Account).where(Account.id == self.user_id).first()
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def tenant(self) -> Tenant | None:
|
2025-07-24 01:57:45 +09:00
|
|
|
return db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def credentials(self) -> dict:
|
|
|
|
try:
|
|
|
|
return cast(dict, json.loads(self.encrypted_credentials)) or {}
|
|
|
|
except Exception:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mcp_tools(self) -> list[Tool]:
|
|
|
|
return [Tool(**tool) for tool in json.loads(self.tools)]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def provider_icon(self) -> dict[str, str] | str:
|
|
|
|
try:
|
|
|
|
return cast(dict[str, str], json.loads(self.icon))
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return file_helpers.get_signed_file_url(self.icon)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def decrypted_server_url(self) -> str:
|
|
|
|
return cast(str, encrypter.decrypt_token(self.tenant_id, self.server_url))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def masked_server_url(self) -> str:
|
|
|
|
def mask_url(url: str, mask_char: str = "*") -> str:
|
|
|
|
"""
|
|
|
|
mask the url to a simple string
|
|
|
|
"""
|
|
|
|
parsed = urlparse(url)
|
|
|
|
base_url = f"{parsed.scheme}://{parsed.netloc}"
|
|
|
|
|
|
|
|
if parsed.path and parsed.path != "/":
|
|
|
|
return f"{base_url}/{mask_char * 6}"
|
|
|
|
else:
|
|
|
|
return base_url
|
|
|
|
|
|
|
|
return mask_url(self.decrypted_server_url)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def decrypted_credentials(self) -> dict:
|
2025-07-17 17:18:44 +08:00
|
|
|
from core.helper.provider_cache import NoOpProviderCredentialCache
|
2025-07-10 14:01:34 +08:00
|
|
|
from core.tools.mcp_tool.provider import MCPToolProviderController
|
2025-07-17 17:18:44 +08:00
|
|
|
from core.tools.utils.encryption import create_provider_encrypter
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
provider_controller = MCPToolProviderController._from_db(self)
|
|
|
|
|
2025-07-17 17:18:44 +08:00
|
|
|
encrypter, _ = create_provider_encrypter(
|
2025-07-10 14:01:34 +08:00
|
|
|
tenant_id=self.tenant_id,
|
2025-07-17 17:18:44 +08:00
|
|
|
config=[x.to_basic_provider_config() for x in provider_controller.get_credentials_schema()],
|
|
|
|
cache=NoOpProviderCredentialCache(),
|
2025-07-10 14:01:34 +08:00
|
|
|
)
|
2025-07-17 17:18:44 +08:00
|
|
|
|
|
|
|
return encrypter.decrypt(self.credentials) # type: ignore
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class ToolModelInvoke(Base):
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
store the invoke logs from tool invoke
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
__tablename__ = "tool_model_invokes"
|
2024-09-10 17:08:06 +08:00
|
|
|
__table_args__ = (db.PrimaryKeyConstraint("id", name="tool_model_invoke_pkey"),)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2025-07-23 01:39:59 +09:00
|
|
|
id = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-01-23 19:58:23 +08:00
|
|
|
# who invoke this tool
|
2025-07-23 01:39:59 +09:00
|
|
|
user_id = mapped_column(StringUUID, nullable=False)
|
2024-01-31 11:58:07 +08:00
|
|
|
# tenant id
|
2025-07-23 01:39:59 +09:00
|
|
|
tenant_id = mapped_column(StringUUID, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# provider
|
2025-07-23 01:39:59 +09:00
|
|
|
provider = mapped_column(db.String(255), nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# type
|
2025-07-23 01:39:59 +09:00
|
|
|
tool_type = mapped_column(db.String(40), nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# tool name
|
2025-07-23 01:39:59 +09:00
|
|
|
tool_name = mapped_column(db.String(128), nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# invoke parameters
|
2025-07-23 01:39:59 +09:00
|
|
|
model_parameters = mapped_column(db.Text, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# prompt messages
|
2025-07-23 01:39:59 +09:00
|
|
|
prompt_messages = mapped_column(db.Text, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# invoke response
|
2025-07-23 01:39:59 +09:00
|
|
|
model_response = mapped_column(db.Text, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2025-07-23 01:39:59 +09:00
|
|
|
prompt_tokens = mapped_column(db.Integer, nullable=False, server_default=db.text("0"))
|
|
|
|
answer_tokens = mapped_column(db.Integer, nullable=False, server_default=db.text("0"))
|
|
|
|
answer_unit_price = mapped_column(db.Numeric(10, 4), nullable=False)
|
|
|
|
answer_price_unit = mapped_column(db.Numeric(10, 7), nullable=False, server_default=db.text("0.001"))
|
|
|
|
provider_response_latency = mapped_column(db.Float, nullable=False, server_default=db.text("0"))
|
|
|
|
total_price = mapped_column(db.Numeric(10, 7))
|
|
|
|
currency = mapped_column(db.String(255), nullable=False)
|
|
|
|
created_at = mapped_column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
updated_at = mapped_column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
@deprecated
|
|
|
|
class ToolConversationVariables(Base):
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
store the conversation variables from tool invoke
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
__tablename__ = "tool_conversation_variables"
|
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tool_conversation_variables_pkey"),
|
2024-01-23 19:58:23 +08:00
|
|
|
# add index for user_id and conversation_id
|
2024-09-10 17:08:06 +08:00
|
|
|
db.Index("user_id_idx", "user_id"),
|
|
|
|
db.Index("conversation_id_idx", "conversation_id"),
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
2025-07-23 01:39:59 +09:00
|
|
|
id = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-01-23 19:58:23 +08:00
|
|
|
# conversation user id
|
2025-07-23 01:39:59 +09:00
|
|
|
user_id = mapped_column(StringUUID, nullable=False)
|
2024-01-31 11:58:07 +08:00
|
|
|
# tenant id
|
2025-07-23 01:39:59 +09:00
|
|
|
tenant_id = mapped_column(StringUUID, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# conversation id
|
2025-07-23 01:39:59 +09:00
|
|
|
conversation_id = mapped_column(StringUUID, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
# variables pool
|
2025-07-23 01:39:59 +09:00
|
|
|
variables_str = mapped_column(db.Text, nullable=False)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2025-07-23 01:39:59 +09:00
|
|
|
created_at = mapped_column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
updated_at = mapped_column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
@property
|
2024-12-24 23:14:32 +08:00
|
|
|
def variables(self) -> Any:
|
|
|
|
return json.loads(self.variables_str)
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class ToolFile(Base):
|
2025-04-30 17:28:02 +08:00
|
|
|
"""This table stores file metadata generated in workflows,
|
|
|
|
not only files created by agent.
|
2025-02-17 17:05:13 +08:00
|
|
|
"""
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
__tablename__ = "tool_files"
|
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tool_file_pkey"),
|
|
|
|
db.Index("tool_file_conversation_id_idx", "conversation_id"),
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
|
|
|
# conversation user id
|
|
|
|
user_id: Mapped[str] = mapped_column(StringUUID)
|
|
|
|
# tenant id
|
|
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID)
|
|
|
|
# conversation id
|
|
|
|
conversation_id: Mapped[str] = mapped_column(StringUUID, nullable=True)
|
|
|
|
# file key
|
|
|
|
file_key: Mapped[str] = mapped_column(db.String(255), nullable=False)
|
|
|
|
# mime type
|
|
|
|
mimetype: Mapped[str] = mapped_column(db.String(255), nullable=False)
|
|
|
|
# original url
|
|
|
|
original_url: Mapped[str] = mapped_column(db.String(2048), nullable=True)
|
|
|
|
# name
|
|
|
|
name: Mapped[str] = mapped_column(default="")
|
|
|
|
# size
|
|
|
|
size: Mapped[int] = mapped_column(default=-1)
|
|
|
|
|
|
|
|
|
|
|
|
@deprecated
|
|
|
|
class DeprecatedPublishedAppTool(Base):
|
|
|
|
"""
|
|
|
|
The table stores the apps published as a tool for each person.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__tablename__ = "tool_published_apps"
|
|
|
|
__table_args__ = (
|
|
|
|
db.PrimaryKeyConstraint("id", name="published_app_tool_pkey"),
|
|
|
|
db.UniqueConstraint("app_id", "user_id", name="unique_published_app_tool"),
|
|
|
|
)
|
|
|
|
|
2025-07-23 01:39:59 +09:00
|
|
|
id = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2025-02-17 17:05:13 +08:00
|
|
|
# id of the app
|
2025-07-23 01:39:59 +09:00
|
|
|
app_id = mapped_column(StringUUID, ForeignKey("apps.id"), nullable=False)
|
2025-05-06 18:05:19 +08:00
|
|
|
|
2025-07-23 01:39:59 +09:00
|
|
|
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
2025-02-17 17:05:13 +08:00
|
|
|
# who published this tool
|
2025-07-23 01:39:59 +09:00
|
|
|
description = mapped_column(db.Text, nullable=False)
|
2025-02-17 17:05:13 +08:00
|
|
|
# llm_description of the tool, for LLM
|
2025-07-23 01:39:59 +09:00
|
|
|
llm_description = mapped_column(db.Text, nullable=False)
|
2025-02-17 17:05:13 +08:00
|
|
|
# query description, query will be seem as a parameter of the tool,
|
|
|
|
# to describe this parameter to llm, we need this field
|
2025-07-23 01:39:59 +09:00
|
|
|
query_description = mapped_column(db.Text, nullable=False)
|
2025-02-17 17:05:13 +08:00
|
|
|
# query name, the name of the query parameter
|
2025-07-23 01:39:59 +09:00
|
|
|
query_name = mapped_column(db.String(40), nullable=False)
|
2025-02-17 17:05:13 +08:00
|
|
|
# name of the tool provider
|
2025-07-23 01:39:59 +09:00
|
|
|
tool_name = mapped_column(db.String(40), nullable=False)
|
2025-02-17 17:05:13 +08:00
|
|
|
# author
|
2025-07-23 01:39:59 +09:00
|
|
|
author = mapped_column(db.String(40), nullable=False)
|
|
|
|
created_at = mapped_column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = mapped_column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def description_i18n(self) -> I18nObject:
|
|
|
|
return I18nObject(**json.loads(self.description))
|