| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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"), | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         # one tenant can only have one tool provider with the same name | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.UniqueConstraint("tenant_id", "provider", 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()")) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2025-05-15 15:14:52 +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)") | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def credentials(self) -> dict: | 
					
						
							|  |  |  |         return cast(dict, json.loads(self.encrypted_credentials)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BuiltinDatasourceProvider(Base): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     This table stores the datasource provider information for built-in datasources for each tenant. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     __tablename__ = "tool_builtin_datasource_providers" | 
					
						
							|  |  |  |     __table_args__ = ( | 
					
						
							|  |  |  |         db.PrimaryKeyConstraint("id", name="tool_builtin_datasource_provider_pkey"), | 
					
						
							|  |  |  |         # one tenant can only have one tool provider with the same name | 
					
						
							|  |  |  |         db.UniqueConstraint("tenant_id", "provider", name="unique_builtin_datasource_provider"), | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # id of the tool provider | 
					
						
							|  |  |  |     id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							|  |  |  |     # id of the tenant | 
					
						
							|  |  |  |     tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     # who created this tool provider | 
					
						
							|  |  |  |     user_id: Mapped[str] = mapped_column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     # name of the tool provider | 
					
						
							|  |  |  |     provider: Mapped[str] = mapped_column(db.String(256), nullable=False) | 
					
						
							|  |  |  |     # credential of the tool provider | 
					
						
							|  |  |  |     encrypted_credentials: Mapped[str] = mapped_column(db.Text, nullable=True) | 
					
						
							| 
									
										
										
										
											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-01-23 19:58:23 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # name of the api provider | 
					
						
							| 
									
										
										
										
											2025-03-07 12:15:52 +08:00
										 |  |  |     name = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # icon | 
					
						
							|  |  |  |     icon = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     # original schema | 
					
						
							|  |  |  |     schema = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     schema_type_str: Mapped[str] = db.Column(db.String(40), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # who created this tool | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     user_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-31 11:58:07 +08:00
										 |  |  |     # tenant id | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # description of the provider | 
					
						
							|  |  |  |     description = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # json format tools | 
					
						
							|  |  |  |     tools_str = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # json format credentials | 
					
						
							|  |  |  |     credentials_str = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # privacy policy | 
					
						
							|  |  |  |     privacy_policy = db.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 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         return db.session.query(Account).filter(Account.id == self.user_id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     def tenant(self) -> Tenant | None: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         return db.session.query(Tenant).filter(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: | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |         return db.session.query(Account).filter(Account.id == self.user_id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     def tenant(self) -> Tenant | None: | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |         return db.session.query(Tenant).filter(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: | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |         return db.session.query(App).filter(App.id == self.app_id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # who invoke this tool | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     user_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-31 11:58:07 +08:00
										 |  |  |     # tenant id | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # provider | 
					
						
							| 
									
										
										
										
											2025-03-07 12:15:52 +08:00
										 |  |  |     provider = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # type | 
					
						
							|  |  |  |     tool_type = db.Column(db.String(40), nullable=False) | 
					
						
							|  |  |  |     # tool name | 
					
						
							|  |  |  |     tool_name = db.Column(db.String(40), nullable=False) | 
					
						
							|  |  |  |     # invoke parameters | 
					
						
							|  |  |  |     model_parameters = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # prompt messages | 
					
						
							|  |  |  |     prompt_messages = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # invoke response | 
					
						
							|  |  |  |     model_response = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     prompt_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							|  |  |  |     answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     answer_unit_price = db.Column(db.Numeric(10, 4), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     answer_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text("0.001")) | 
					
						
							|  |  |  |     provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     total_price = db.Column(db.Numeric(10, 7)) | 
					
						
							|  |  |  |     currency = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							|  |  |  |     updated_at = db.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
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # conversation user id | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     user_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-31 11:58:07 +08:00
										 |  |  |     # tenant id | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # conversation id | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     conversation_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     # variables pool | 
					
						
							|  |  |  |     variables_str = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							|  |  |  |     updated_at = db.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-05-06 18:05:19 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     # id of the app | 
					
						
							|  |  |  |     app_id = db.Column(StringUUID, ForeignKey("apps.id"), nullable=False) | 
					
						
							| 
									
										
										
										
											2025-05-06 18:05:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     user_id: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     # who published this tool | 
					
						
							|  |  |  |     description = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # llm_description of the tool, for LLM | 
					
						
							|  |  |  |     llm_description = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # query description, query will be seem as a parameter of the tool, | 
					
						
							|  |  |  |     # to describe this parameter to llm, we need this field | 
					
						
							|  |  |  |     query_description = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     # query name, the name of the query parameter | 
					
						
							|  |  |  |     query_name = db.Column(db.String(40), nullable=False) | 
					
						
							|  |  |  |     # name of the tool provider | 
					
						
							|  |  |  |     tool_name = db.Column(db.String(40), nullable=False) | 
					
						
							|  |  |  |     # author | 
					
						
							|  |  |  |     author = db.Column(db.String(40), nullable=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)")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def description_i18n(self) -> I18nObject: | 
					
						
							|  |  |  |         return I18nObject(**json.loads(self.description)) |