Files
dify/api/models/tools.py
T

525 lines
21 KiB
Python
Raw Normal View History

2024-01-23 19:58:23 +08:00
import json
+109
2025-02-17 17:05:13 +08:00
from datetime import datetime
2025-10-13 11:16:12 +09:00
from decimal import Decimal
+9
2025-09-18 12:49:10 +08:00
from typing import TYPE_CHECKING, Any, cast
2024-01-23 19:58:23 +08:00
2024-11-01 15:51:22 +08:00
import sqlalchemy as sa
+109
2025-02-17 17:05:13 +08:00
from deprecated import deprecated
2025-07-31 19:43:04 +09:00
from sqlalchemy import ForeignKey, String, func
from sqlalchemy.orm import Mapped, mapped_column
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-10-13 11:16:12 +09:00
from models.base import TypeBase
from .engine import db
from .model import Account, App, Tenant
from .types import StringUUID
2024-01-23 19:58:23 +08:00
+9
2025-09-18 12:49:10 +08:00
if TYPE_CHECKING:
from core.entities.mcp_provider import MCPProviderEntity
+9
2025-09-18 12:49:10 +08:00
from core.tools.entities.common_entities import I18nObject
from core.tools.entities.tool_bundle import ApiToolBundle
from core.tools.entities.tool_entities import ApiProviderSchemaType, WorkflowToolParameterConfiguration
2024-01-23 19:58:23 +08:00
# system level tool oauth client params (client_id, client_secret, etc.)
2025-09-06 04:20:13 +09:00
class ToolOAuthSystemClient(TypeBase):
__tablename__ = "tool_oauth_system_clients"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_oauth_system_client_pkey"),
sa.UniqueConstraint("plugin_id", "provider", name="tool_oauth_system_client_plugin_id_provider_idx"),
)
2025-09-06 04:20:13 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
plugin_id: Mapped[str] = mapped_column(String(512), nullable=False)
2025-07-31 19:43:04 +09:00
provider: Mapped[str] = mapped_column(String(255), nullable=False)
# oauth params of the tool provider
encrypted_oauth_params: Mapped[str] = mapped_column(sa.Text, nullable=False)
# tenant level tool oauth client params (client_id, client_secret, etc.)
2025-10-13 11:16:12 +09:00
class ToolOAuthTenantClient(TypeBase):
__tablename__ = "tool_oauth_tenant_clients"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_oauth_tenant_client_pkey"),
sa.UniqueConstraint("tenant_id", "plugin_id", "provider", name="unique_tool_oauth_tenant_client"),
)
2025-10-13 11:16:12 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
# tenant id
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2025-07-31 19:43:04 +09:00
plugin_id: Mapped[str] = mapped_column(String(512), nullable=False)
provider: Mapped[str] = mapped_column(String(255), nullable=False)
2025-10-13 11:16:12 +09:00
enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true"), init=False)
# oauth params of the tool provider
2025-10-13 11:16:12 +09:00
encrypted_oauth_params: Mapped[str] = mapped_column(sa.Text, nullable=False, init=False)
@property
def oauth_params(self) -> dict[str, Any]:
return cast(dict[str, Any], json.loads(self.encrypted_oauth_params or "{}"))
2025-10-13 11:16:12 +09:00
class BuiltinToolProvider(TypeBase):
2024-01-23 19:58:23 +08:00
"""
This table stores the tool provider information for built-in tools for each tenant.
"""
__tablename__ = "tool_builtin_providers"
2024-01-23 19:58:23 +08:00
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_builtin_provider_pkey"),
sa.UniqueConstraint("tenant_id", "provider", "name", name="unique_builtin_tool_provider"),
2024-01-23 19:58:23 +08:00
)
# id of the tool provider
2025-10-13 11:16:12 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
name: Mapped[str] = mapped_column(
2025-10-13 11:16:12 +09:00
String(256),
nullable=False,
server_default=sa.text("'API KEY 1'::character varying"),
)
2024-01-23 19:58:23 +08:00
# id of the tenant
2025-10-13 11:16:12 +09:00
tenant_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True)
2024-01-23 19:58:23 +08:00
# who created this tool provider
+109
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-07-31 19:43:04 +09:00
provider: Mapped[str] = mapped_column(String(256), nullable=False)
2024-01-23 19:58:23 +08:00
# credential of the tool provider
2025-10-13 11:16:12 +09:00
encrypted_credentials: Mapped[str | None] = mapped_column(sa.Text, nullable=True, default=None)
+109
2025-02-17 17:05:13 +08:00
created_at: Mapped[datetime] = mapped_column(
2025-10-13 11:16:12 +09:00
sa.DateTime, nullable=False, server_default=sa.text("CURRENT_TIMESTAMP(0)"), init=False
+109
2025-02-17 17:05:13 +08:00
)
updated_at: Mapped[datetime] = mapped_column(
2025-10-13 11:16:12 +09:00
sa.DateTime,
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP(0)"),
onupdate=func.current_timestamp(),
init=False,
+109
2025-02-17 17:05:13 +08:00
)
2025-10-13 11:16:12 +09:00
is_default: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False)
# credential type, e.g., "api-key", "oauth2"
credential_type: Mapped[str] = mapped_column(
2025-10-13 11:16:12 +09:00
String(32), nullable=False, server_default=sa.text("'api-key'::character varying"), default="api-key"
)
2025-10-13 11:16:12 +09:00
expires_at: Mapped[int] = mapped_column(sa.BigInteger, nullable=False, server_default=sa.text("-1"), default=-1)
2024-01-23 19:58:23 +08:00
@property
def credentials(self) -> dict[str, Any]:
2025-10-13 11:16:12 +09:00
if not self.encrypted_credentials:
return {}
return cast(dict[str, Any], json.loads(self.encrypted_credentials))
2024-01-23 19:58:23 +08:00
2025-10-13 11:16:12 +09:00
class ApiToolProvider(TypeBase):
2024-01-23 19:58:23 +08:00
"""
The table stores the api providers.
"""
__tablename__ = "tool_api_providers"
2024-01-23 19:58:23 +08:00
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_api_provider_pkey"),
sa.UniqueConstraint("name", "tenant_id", name="unique_api_tool_provider"),
2024-01-23 19:58:23 +08:00
)
2025-10-13 11:16:12 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
2024-01-23 19:58:23 +08:00
# name of the api provider
2025-10-13 11:16:12 +09:00
name: Mapped[str] = mapped_column(
String(255),
nullable=False,
server_default=sa.text("'API KEY 1'::character varying"),
)
2024-01-23 19:58:23 +08:00
# icon
2025-07-31 19:43:04 +09:00
icon: Mapped[str] = mapped_column(String(255), nullable=False)
2024-01-23 19:58:23 +08:00
# original schema
2025-10-13 11:16:12 +09:00
schema: Mapped[str] = mapped_column(sa.Text, nullable=False)
2025-07-31 19:43:04 +09:00
schema_type_str: Mapped[str] = mapped_column(String(40), nullable=False)
2024-01-23 19:58:23 +08:00
# who created this tool
2025-10-13 11:16:12 +09:00
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2024-01-31 11:58:07 +08:00
# tenant id
2025-10-13 11:16:12 +09:00
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2024-01-23 19:58:23 +08:00
# description of the provider
2025-10-13 11:16:12 +09:00
description: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-01-23 19:58:23 +08:00
# json format tools
2025-10-13 11:16:12 +09:00
tools_str: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-01-23 19:58:23 +08:00
# json format credentials
2025-10-13 11:16:12 +09:00
credentials_str: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-01-23 19:58:23 +08:00
# privacy policy
2025-10-13 11:16:12 +09:00
privacy_policy: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None)
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-10-13 11:16:12 +09:00
created_at: Mapped[datetime] = mapped_column(
sa.DateTime, nullable=False, server_default=func.current_timestamp(), init=False
)
updated_at: Mapped[datetime] = mapped_column(
sa.DateTime,
nullable=False,
server_default=func.current_timestamp(),
onupdate=func.current_timestamp(),
init=False,
)
2024-01-23 19:58:23 +08:00
@property
+9
2025-09-18 12:49:10 +08:00
def schema_type(self) -> "ApiProviderSchemaType":
from core.tools.entities.tool_entities import ApiProviderSchemaType
2024-01-23 19:58:23 +08:00
return ApiProviderSchemaType.value_of(self.schema_type_str)
2024-01-23 19:58:23 +08:00
@property
+9
2025-09-18 12:49:10 +08:00
def tools(self) -> list["ApiToolBundle"]:
from core.tools.entities.tool_bundle import ApiToolBundle
2025-10-10 17:30:13 +09:00
return [ApiToolBundle.model_validate(tool) for tool in json.loads(self.tools_str)]
2024-01-23 19:58:23 +08:00
@property
def credentials(self) -> dict[str, Any]:
return dict[str, Any](json.loads(self.credentials_str))
2024-01-23 19:58:23 +08:00
@property
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
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
2025-09-02 20:13:43 +09:00
class ToolLabelBinding(TypeBase):
2024-05-27 22:01:11 +08:00
"""
The table stores the labels for tools.
"""
__tablename__ = "tool_label_bindings"
2024-05-27 22:01:11 +08:00
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_label_bind_pkey"),
sa.UniqueConstraint("tool_id", "label_name", name="unique_tool_label_bind"),
2024-05-27 22:01:11 +08:00
)
2025-09-02 20:13:43 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
2024-05-27 22:01:11 +08:00
# tool id
2025-07-31 19:43:04 +09:00
tool_id: Mapped[str] = mapped_column(String(64), nullable=False)
2024-05-27 22:01:11 +08:00
# tool type
2025-07-31 19:43:04 +09:00
tool_type: Mapped[str] = mapped_column(String(40), nullable=False)
2024-05-27 22:01:11 +08:00
# label name
2025-07-31 19:43:04 +09:00
label_name: Mapped[str] = mapped_column(String(40), nullable=False)
2024-05-27 22:01:11 +08:00
2025-10-13 11:16:12 +09:00
class WorkflowToolProvider(TypeBase):
2024-05-27 22:01:11 +08:00
"""
The table stores the workflow providers.
"""
__tablename__ = "tool_workflow_providers"
2024-05-27 22:01:11 +08:00
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_workflow_provider_pkey"),
sa.UniqueConstraint("name", "tenant_id", name="unique_workflow_tool_provider"),
sa.UniqueConstraint("tenant_id", "app_id", name="unique_workflow_tool_provider_app_id"),
2024-05-27 22:01:11 +08:00
)
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
2024-05-27 22:01:11 +08:00
# name of the workflow provider
2025-07-31 19:43:04 +09:00
name: Mapped[str] = mapped_column(String(255), nullable=False)
2024-05-27 22:01:11 +08:00
# label of the workflow provider
2025-07-31 19:43:04 +09:00
label: Mapped[str] = mapped_column(String(255), nullable=False, server_default="")
2024-05-27 22:01:11 +08:00
# icon
2025-07-31 19:43:04 +09:00
icon: Mapped[str] = mapped_column(String(255), nullable=False)
2024-05-27 22:01:11 +08:00
# app id of the workflow provider
+109
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-07-31 19:43:04 +09:00
version: Mapped[str] = mapped_column(String(255), nullable=False, server_default="")
2024-05-27 22:01:11 +08:00
# who created this tool
+109
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
+109
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
description: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-05-27 22:01:11 +08:00
# parameter configuration
2025-10-13 11:16:12 +09:00
parameter_configuration: Mapped[str] = mapped_column(sa.Text, nullable=False, server_default="[]", default="[]")
2024-05-27 22:01:11 +08:00
# privacy policy
2025-10-13 11:16:12 +09:00
privacy_policy: Mapped[str | None] = mapped_column(String(255), nullable=True, server_default="", default=None)
2024-05-27 22:01:11 +08:00
+109
2025-02-17 17:05:13 +08:00
created_at: Mapped[datetime] = mapped_column(
2025-10-13 11:16:12 +09:00
sa.DateTime, nullable=False, server_default=sa.text("CURRENT_TIMESTAMP(0)"), init=False
+109
2025-02-17 17:05:13 +08:00
)
updated_at: Mapped[datetime] = mapped_column(
2025-10-13 11:16:12 +09:00
sa.DateTime,
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP(0)"),
onupdate=func.current_timestamp(),
init=False,
+109
2025-02-17 17:05:13 +08:00
)
2024-05-27 22:01:11 +08:00
@property
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
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
@property
+9
2025-09-18 12:49:10 +08:00
def parameter_configurations(self) -> list["WorkflowToolParameterConfiguration"]:
from core.tools.entities.tool_entities import WorkflowToolParameterConfiguration
2025-10-10 17:30:13 +09:00
return [
WorkflowToolParameterConfiguration.model_validate(config)
for config in json.loads(self.parameter_configuration)
]
2024-05-27 22:01:11 +08:00
@property
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
2025-10-13 11:16:12 +09:00
class MCPToolProvider(TypeBase):
2025-07-10 14:01:34 +08:00
"""
The table stores the mcp providers.
"""
__tablename__ = "tool_mcp_providers"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_mcp_provider_pkey"),
sa.UniqueConstraint("tenant_id", "server_url_hash", name="unique_mcp_provider_server_url"),
sa.UniqueConstraint("tenant_id", "name", name="unique_mcp_provider_name"),
sa.UniqueConstraint("tenant_id", "server_identifier", name="unique_mcp_provider_server_identifier"),
2025-07-10 14:01:34 +08:00
)
2025-10-13 11:16:12 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
2025-07-10 14:01:34 +08:00
# name of the mcp provider
2025-07-31 19:43:04 +09:00
name: Mapped[str] = mapped_column(String(40), nullable=False)
2025-07-10 14:01:34 +08:00
# server identifier of the mcp provider
2025-07-31 19:43:04 +09:00
server_identifier: Mapped[str] = mapped_column(String(64), nullable=False)
2025-07-10 14:01:34 +08:00
# encrypted url of the mcp provider
server_url: Mapped[str] = mapped_column(sa.Text, nullable=False)
2025-07-10 14:01:34 +08:00
# hash of server_url for uniqueness check
2025-07-31 19:43:04 +09:00
server_url_hash: Mapped[str] = mapped_column(String(64), nullable=False)
2025-07-10 14:01:34 +08:00
# icon of the mcp provider
2025-10-13 11:16:12 +09:00
icon: Mapped[str | None] = mapped_column(String(255), nullable=True)
2025-07-10 14:01:34 +08:00
# 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
2025-10-13 11:16:12 +09:00
encrypted_credentials: Mapped[str | None] = mapped_column(sa.Text, nullable=True, default=None)
2025-07-10 14:01:34 +08:00
# authed
authed: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, default=False)
2025-07-10 14:01:34 +08:00
# tools
tools: Mapped[str] = mapped_column(sa.Text, nullable=False, default="[]")
2025-07-10 14:01:34 +08:00
created_at: Mapped[datetime] = mapped_column(
2025-10-13 11:16:12 +09:00
sa.DateTime, nullable=False, server_default=sa.text("CURRENT_TIMESTAMP(0)"), init=False
2025-07-10 14:01:34 +08:00
)
updated_at: Mapped[datetime] = mapped_column(
2025-10-13 11:16:12 +09:00
sa.DateTime,
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP(0)"),
onupdate=func.current_timestamp(),
init=False,
)
timeout: Mapped[float] = mapped_column(sa.Float, nullable=False, server_default=sa.text("30"), default=30.0)
sse_read_timeout: Mapped[float] = mapped_column(
sa.Float, nullable=False, server_default=sa.text("300"), default=300.0
2025-07-10 14:01:34 +08:00
)
# encrypted headers for MCP server requests
2025-10-13 11:16:12 +09:00
encrypted_headers: Mapped[str | None] = mapped_column(sa.Text, nullable=True, default=None)
2025-07-10 14:01:34 +08:00
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 credentials(self) -> dict[str, Any]:
2025-10-13 11:16:12 +09:00
if not self.encrypted_credentials:
return {}
2025-07-10 14:01:34 +08:00
try:
return json.loads(self.encrypted_credentials)
except Exception:
return {}
@property
def headers(self) -> dict[str, Any]:
if self.encrypted_headers is None:
return {}
try:
return json.loads(self.encrypted_headers)
except Exception:
return {}
2025-07-10 14:01:34 +08:00
@property
def tool_dict(self) -> list[dict[str, Any]]:
try:
return json.loads(self.tools) if self.tools else []
except (json.JSONDecodeError, TypeError):
return []
2025-07-10 14:01:34 +08:00
def to_entity(self) -> "MCPProviderEntity":
"""Convert to domain entity"""
from core.entities.mcp_provider import MCPProviderEntity
2025-07-10 14:01:34 +08:00
return MCPProviderEntity.from_db_model(self)
2025-07-10 14:01:34 +08:00
2025-10-13 11:16:12 +09:00
class ToolModelInvoke(TypeBase):
2024-01-23 19:58:23 +08:00
"""
store the invoke logs from tool invoke
"""
__tablename__ = "tool_model_invokes"
__table_args__ = (sa.PrimaryKeyConstraint("id", name="tool_model_invoke_pkey"),)
2025-10-13 11:16:12 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
2024-01-23 19:58:23 +08:00
# who invoke this tool
2025-10-13 11:16:12 +09:00
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2024-01-31 11:58:07 +08:00
# tenant id
2025-10-13 11:16:12 +09:00
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2024-01-23 19:58:23 +08:00
# provider
2025-07-31 19:43:04 +09:00
provider: Mapped[str] = mapped_column(String(255), nullable=False)
2024-01-23 19:58:23 +08:00
# type
2025-10-13 11:16:12 +09:00
tool_type: Mapped[str] = mapped_column(String(40), nullable=False)
2024-01-23 19:58:23 +08:00
# tool name
2025-10-13 11:16:12 +09:00
tool_name: Mapped[str] = mapped_column(String(128), nullable=False)
2024-01-23 19:58:23 +08:00
# invoke parameters
2025-10-13 11:16:12 +09:00
model_parameters: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-01-23 19:58:23 +08:00
# prompt messages
2025-10-13 11:16:12 +09:00
prompt_messages: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-01-23 19:58:23 +08:00
# invoke response
2025-10-13 11:16:12 +09:00
model_response: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-01-23 19:58:23 +08:00
prompt_tokens: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0"))
answer_tokens: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0"))
2025-10-13 11:16:12 +09:00
answer_unit_price: Mapped[Decimal] = mapped_column(sa.Numeric(10, 4), nullable=False)
answer_price_unit: Mapped[Decimal] = mapped_column(
sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001")
)
provider_response_latency: Mapped[float] = mapped_column(sa.Float, nullable=False, server_default=sa.text("0"))
total_price: Mapped[Decimal | None] = mapped_column(sa.Numeric(10, 7))
2025-07-31 19:43:04 +09:00
currency: Mapped[str] = mapped_column(String(255), nullable=False)
2025-10-13 11:16:12 +09:00
created_at: Mapped[datetime] = mapped_column(
sa.DateTime, nullable=False, server_default=func.current_timestamp(), init=False
)
updated_at: Mapped[datetime] = mapped_column(
sa.DateTime,
nullable=False,
server_default=func.current_timestamp(),
onupdate=func.current_timestamp(),
init=False,
)
2024-01-23 19:58:23 +08:00
+109
2025-02-17 17:05:13 +08:00
@deprecated
2025-10-13 11:16:12 +09:00
class ToolConversationVariables(TypeBase):
2024-01-23 19:58:23 +08:00
"""
store the conversation variables from tool invoke
"""
2024-01-23 19:58:23 +08:00
__tablename__ = "tool_conversation_variables"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_conversation_variables_pkey"),
2024-01-23 19:58:23 +08:00
# add index for user_id and conversation_id
sa.Index("user_id_idx", "user_id"),
sa.Index("conversation_id_idx", "conversation_id"),
2024-01-23 19:58:23 +08:00
)
2025-10-13 11:16:12 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
2024-01-23 19:58:23 +08:00
# conversation user id
2025-10-13 11:16:12 +09:00
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2024-01-31 11:58:07 +08:00
# tenant id
2025-10-13 11:16:12 +09:00
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2024-01-23 19:58:23 +08:00
# conversation id
2025-10-13 11:16:12 +09:00
conversation_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
2024-01-23 19:58:23 +08:00
# variables pool
2025-10-13 11:16:12 +09:00
variables_str: Mapped[str] = mapped_column(sa.Text, nullable=False)
2024-01-23 19:58:23 +08:00
2025-10-13 11:16:12 +09:00
created_at: Mapped[datetime] = mapped_column(
sa.DateTime, nullable=False, server_default=func.current_timestamp(), init=False
)
updated_at: Mapped[datetime] = mapped_column(
sa.DateTime,
nullable=False,
server_default=func.current_timestamp(),
onupdate=func.current_timestamp(),
init=False,
)
2024-01-23 19:58:23 +08:00
@property
def variables(self):
2024-12-24 23:14:32 +08:00
return json.loads(self.variables_str)
2025-09-06 04:20:13 +09:00
class ToolFile(TypeBase):
"""This table stores file metadata generated in workflows,
not only files created by agent.
+109
2025-02-17 17:05:13 +08:00
"""
2024-01-23 19:58:23 +08:00
__tablename__ = "tool_files"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="tool_file_pkey"),
sa.Index("tool_file_conversation_id_idx", "conversation_id"),
2024-01-23 19:58:23 +08:00
)
2025-09-06 04:20:13 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
+109
2025-02-17 17:05:13 +08:00
# 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 | None] = mapped_column(StringUUID, nullable=True)
+109
2025-02-17 17:05:13 +08:00
# file key
2025-07-31 19:43:04 +09:00
file_key: Mapped[str] = mapped_column(String(255), nullable=False)
+109
2025-02-17 17:05:13 +08:00
# mime type
2025-07-31 19:43:04 +09:00
mimetype: Mapped[str] = mapped_column(String(255), nullable=False)
+109
2025-02-17 17:05:13 +08:00
# original url
original_url: Mapped[str | None] = mapped_column(String(2048), nullable=True, default=None)
+109
2025-02-17 17:05:13 +08:00
# name
name: Mapped[str] = mapped_column(default="")
# size
size: Mapped[int] = mapped_column(default=-1)
@deprecated
2025-10-13 11:16:12 +09:00
class DeprecatedPublishedAppTool(TypeBase):
+109
2025-02-17 17:05:13 +08:00
"""
The table stores the apps published as a tool for each person.
"""
__tablename__ = "tool_published_apps"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="published_app_tool_pkey"),
sa.UniqueConstraint("app_id", "user_id", name="unique_published_app_tool"),
+109
2025-02-17 17:05:13 +08:00
)
2025-10-13 11:16:12 +09:00
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
+109
2025-02-17 17:05:13 +08:00
# id of the app
2025-10-13 11:16:12 +09:00
app_id: Mapped[str] = 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)
+109
2025-02-17 17:05:13 +08:00
# who published this tool
2025-10-13 11:16:12 +09:00
description: Mapped[str] = mapped_column(sa.Text, nullable=False)
+109
2025-02-17 17:05:13 +08:00
# llm_description of the tool, for LLM
2025-10-13 11:16:12 +09:00
llm_description: Mapped[str] = mapped_column(sa.Text, nullable=False)
+109
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-10-13 11:16:12 +09:00
query_description: Mapped[str] = mapped_column(sa.Text, nullable=False)
+109
2025-02-17 17:05:13 +08:00
# query name, the name of the query parameter
2025-10-13 11:16:12 +09:00
query_name: Mapped[str] = mapped_column(String(40), nullable=False)
+109
2025-02-17 17:05:13 +08:00
# name of the tool provider
2025-10-13 11:16:12 +09:00
tool_name: Mapped[str] = mapped_column(String(40), nullable=False)
+109
2025-02-17 17:05:13 +08:00
# author
2025-10-13 11:16:12 +09:00
author: Mapped[str] = mapped_column(String(40), nullable=False)
created_at: Mapped[datetime] = mapped_column(
sa.DateTime, nullable=False, server_default=sa.text("CURRENT_TIMESTAMP(0)"), init=False
)
updated_at: Mapped[datetime] = mapped_column(
sa.DateTime,
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP(0)"),
onupdate=func.current_timestamp(),
init=False,
)
+109
2025-02-17 17:05:13 +08:00
@property
+9
2025-09-18 12:49:10 +08:00
def description_i18n(self) -> "I18nObject":
from core.tools.entities.common_entities import I18nObject
2025-10-10 17:30:13 +09:00
return I18nObject.model_validate(json.loads(self.description))