| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | import uuid | 
					
						
							| 
									
										
										
										
											2024-11-04 15:55:34 +08:00
										 |  |  | from collections.abc import Mapping | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2025-04-23 16:48:00 +08:00
										 |  |  | from enum import Enum, StrEnum | 
					
						
							|  |  |  | from typing import TYPE_CHECKING, Any, Literal, Optional, cast | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | from core.plugin.entities.plugin import GenericProviderID | 
					
						
							|  |  |  | from core.tools.entities.tool_entities import ToolProviderType | 
					
						
							| 
									
										
										
										
											2025-04-30 17:28:02 +08:00
										 |  |  | from core.tools.signature import sign_tool_file | 
					
						
							| 
									
										
										
										
											2025-05-30 04:34:13 +08:00
										 |  |  | from core.workflow.entities.workflow_execution import WorkflowExecutionStatus | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from services.plugin.plugin_service import PluginService | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if TYPE_CHECKING: | 
					
						
							|  |  |  |     from models.workflow import Workflow | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  | import sqlalchemy as sa | 
					
						
							| 
									
										
										
										
											2024-07-17 22:26:18 +08:00
										 |  |  | from flask import request | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | from flask_login import UserMixin | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from sqlalchemy import Float, Index, PrimaryKeyConstraint, func, text | 
					
						
							|  |  |  | from sqlalchemy.orm import Mapped, Session, mapped_column | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-17 22:26:18 +08:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2025-04-11 15:26:26 +08:00
										 |  |  | from constants import DEFAULT_FILE_NUMBER_LIMITS | 
					
						
							| 
									
										
										
										
											2024-11-08 18:13:24 +08:00
										 |  |  | from core.file import FILE_MODEL_IDENTITY, File, FileTransferMethod, FileType | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from core.file import helpers as file_helpers | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from libs.helper import generate_string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from .account import Account, Tenant | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | from .base import Base | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:29 +08:00
										 |  |  | from .engine import db | 
					
						
							| 
									
										
										
										
											2025-05-17 00:56:16 +08:00
										 |  |  | from .enums import CreatorUserRole | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | from .types import StringUUID | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 14:32:11 +08:00
										 |  |  | if TYPE_CHECKING: | 
					
						
							|  |  |  |     from .workflow import Workflow | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class DifySetup(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "dify_setups" | 
					
						
							|  |  |  |     __table_args__ = (db.PrimaryKeyConstraint("version", name="dify_setup_pkey"),) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     version = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     setup_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 13:28:46 +08:00
										 |  |  | class AppMode(StrEnum): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     COMPLETION = "completion" | 
					
						
							|  |  |  |     WORKFLOW = "workflow" | 
					
						
							|  |  |  |     CHAT = "chat" | 
					
						
							|  |  |  |     ADVANCED_CHAT = "advanced-chat" | 
					
						
							|  |  |  |     AGENT_CHAT = "agent-chat" | 
					
						
							|  |  |  |     CHANNEL = "channel" | 
					
						
							| 
									
										
										
										
											2025-05-23 00:05:57 +08:00
										 |  |  |     RAG_PIPELINE = "rag-pipeline" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     def value_of(cls, value: str) -> "AppMode": | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Get value of given mode. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         :param value: mode value | 
					
						
							|  |  |  |         :return: mode | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         for mode in cls: | 
					
						
							|  |  |  |             if mode.value == value: | 
					
						
							|  |  |  |                 return mode | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         raise ValueError(f"invalid mode value {value}") | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  | class IconType(Enum): | 
					
						
							|  |  |  |     IMAGE = "image" | 
					
						
							|  |  |  |     EMOJI = "emoji" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class App(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "apps" | 
					
						
							|  |  |  |     __table_args__ = (db.PrimaryKeyConstraint("id", name="app_pkey"), db.Index("app_tenant_id_idx", "tenant_id")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     tenant_id: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     name = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     description = db.Column(db.Text, nullable=False, server_default=db.text("''::character varying")) | 
					
						
							| 
									
										
										
										
											2025-03-18 11:12:25 +08:00
										 |  |  |     mode: Mapped[str] = mapped_column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |     icon_type = db.Column(db.String(255), nullable=True)  # image, emoji | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     icon = db.Column(db.String(255)) | 
					
						
							|  |  |  |     icon_background = db.Column(db.String(255)) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_model_config_id = db.Column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     workflow_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     status = db.Column(db.String(255), nullable=False, server_default=db.text("'normal'::character varying")) | 
					
						
							|  |  |  |     enable_site = db.Column(db.Boolean, nullable=False) | 
					
						
							|  |  |  |     enable_api = db.Column(db.Boolean, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     api_rpm = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							|  |  |  |     api_rph = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							|  |  |  |     is_demo = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							|  |  |  |     is_public = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							|  |  |  |     is_universal = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |     tracing = db.Column(db.Text, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |     max_active_requests: Mapped[Optional[int]] = mapped_column(nullable=True) | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  |     created_by = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  |     updated_by = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-09-02 21:00:41 +09:00
										 |  |  |     use_icon_as_answer_icon = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def desc_or_prompt(self): | 
					
						
							|  |  |  |         if self.description: | 
					
						
							|  |  |  |             return self.description | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             app_model_config = self.app_model_config | 
					
						
							|  |  |  |             if app_model_config: | 
					
						
							|  |  |  |                 return app_model_config.pre_prompt | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 return "" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def site(self): | 
					
						
							|  |  |  |         site = db.session.query(Site).filter(Site.app_id == self.id).first() | 
					
						
							|  |  |  |         return site | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							| 
									
										
										
										
											2024-11-04 15:55:34 +08:00
										 |  |  |     def app_model_config(self): | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         if self.app_model_config_id: | 
					
						
							|  |  |  |             return db.session.query(AppModelConfig).filter(AppModelConfig.id == self.app_model_config_id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     def workflow(self) -> Optional["Workflow"]: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         if self.workflow_id: | 
					
						
							|  |  |  |             from .workflow import Workflow | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             return db.session.query(Workflow).filter(Workflow.id == self.workflow_id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def api_base_url(self): | 
					
						
							| 
									
										
										
										
											2024-09-12 15:50:49 +08:00
										 |  |  |         return (dify_config.SERVICE_API_URL or request.host_url.rstrip("/")) + "/v1" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def tenant(self): | 
					
						
							|  |  |  |         tenant = db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first() | 
					
						
							|  |  |  |         return tenant | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def is_agent(self) -> bool: | 
					
						
							|  |  |  |         app_model_config = self.app_model_config | 
					
						
							|  |  |  |         if not app_model_config: | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  |         if not app_model_config.agent_mode: | 
					
						
							|  |  |  |             return False | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if app_model_config.agent_mode_dict.get("enabled", False) and app_model_config.agent_mode_dict.get( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             "strategy", "" | 
					
						
							| 
									
										
										
										
											2024-09-13 22:42:08 +08:00
										 |  |  |         ) in {"function_call", "react"}: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             self.mode = AppMode.AGENT_CHAT.value | 
					
						
							|  |  |  |             db.session.commit() | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             return True | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def mode_compatible_with_agent(self) -> str: | 
					
						
							|  |  |  |         if self.mode == AppMode.CHAT.value and self.is_agent: | 
					
						
							|  |  |  |             return AppMode.AGENT_CHAT.value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         return str(self.mode) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def deleted_tools(self) -> list: | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         from core.tools.tool_manager import ToolManager | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         # get agent mode tools | 
					
						
							|  |  |  |         app_model_config = self.app_model_config | 
					
						
							|  |  |  |         if not app_model_config: | 
					
						
							|  |  |  |             return [] | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         if not app_model_config.agent_mode: | 
					
						
							|  |  |  |             return [] | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         agent_mode = app_model_config.agent_mode_dict | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         tools = agent_mode.get("tools", []) | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         api_provider_ids: list[str] = [] | 
					
						
							|  |  |  |         builtin_provider_ids: list[GenericProviderID] = [] | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for tool in tools: | 
					
						
							|  |  |  |             keys = list(tool.keys()) | 
					
						
							|  |  |  |             if len(keys) >= 4: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 provider_type = tool.get("provider_type", "") | 
					
						
							|  |  |  |                 provider_id = tool.get("provider_id", "") | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 if provider_type == ToolProviderType.API.value: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |                     try: | 
					
						
							|  |  |  |                         uuid.UUID(provider_id) | 
					
						
							|  |  |  |                     except Exception: | 
					
						
							|  |  |  |                         continue | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                     api_provider_ids.append(provider_id) | 
					
						
							|  |  |  |                 if provider_type == ToolProviderType.BUILT_IN.value: | 
					
						
							|  |  |  |                     try: | 
					
						
							|  |  |  |                         # check if it's hardcoded | 
					
						
							|  |  |  |                         try: | 
					
						
							|  |  |  |                             ToolManager.get_hardcoded_provider(provider_id) | 
					
						
							|  |  |  |                             is_hardcoded = True | 
					
						
							|  |  |  |                         except Exception: | 
					
						
							|  |  |  |                             is_hardcoded = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         provider_id = GenericProviderID(provider_id, is_hardcoded) | 
					
						
							|  |  |  |                     except Exception: | 
					
						
							|  |  |  |                         continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     builtin_provider_ids.append(provider_id) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         if not api_provider_ids and not builtin_provider_ids: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             return [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         with Session(db.engine) as session: | 
					
						
							|  |  |  |             if api_provider_ids: | 
					
						
							|  |  |  |                 existing_api_providers = [ | 
					
						
							|  |  |  |                     api_provider.id | 
					
						
							|  |  |  |                     for api_provider in session.execute( | 
					
						
							|  |  |  |                         text("SELECT id FROM tool_api_providers WHERE id IN :provider_ids"), | 
					
						
							|  |  |  |                         {"provider_ids": tuple(api_provider_ids)}, | 
					
						
							|  |  |  |                     ).fetchall() | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 existing_api_providers = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if builtin_provider_ids: | 
					
						
							|  |  |  |             # get the non-hardcoded builtin providers | 
					
						
							|  |  |  |             non_hardcoded_builtin_providers = [ | 
					
						
							|  |  |  |                 provider_id for provider_id in builtin_provider_ids if not provider_id.is_hardcoded | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |             if non_hardcoded_builtin_providers: | 
					
						
							|  |  |  |                 existence = list(PluginService.check_tools_existence(self.tenant_id, non_hardcoded_builtin_providers)) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 existence = [] | 
					
						
							|  |  |  |             # add the hardcoded builtin providers | 
					
						
							|  |  |  |             existence.extend([True] * (len(builtin_provider_ids) - len(non_hardcoded_builtin_providers))) | 
					
						
							|  |  |  |             builtin_provider_ids = non_hardcoded_builtin_providers + [ | 
					
						
							|  |  |  |                 provider_id for provider_id in builtin_provider_ids if provider_id.is_hardcoded | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             existence = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         existing_builtin_providers = { | 
					
						
							|  |  |  |             provider_id.provider_name: existence[i] for i, provider_id in enumerate(builtin_provider_ids) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         deleted_tools = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for tool in tools: | 
					
						
							|  |  |  |             keys = list(tool.keys()) | 
					
						
							|  |  |  |             if len(keys) >= 4: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 provider_type = tool.get("provider_type", "") | 
					
						
							|  |  |  |                 provider_id = tool.get("provider_id", "") | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if provider_type == ToolProviderType.API.value: | 
					
						
							| 
									
										
										
										
											2025-03-11 16:11:24 +08:00
										 |  |  |                     if uuid.UUID(provider_id) not in existing_api_providers: | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                         deleted_tools.append( | 
					
						
							|  |  |  |                             { | 
					
						
							|  |  |  |                                 "type": ToolProviderType.API.value, | 
					
						
							|  |  |  |                                 "tool_name": tool["tool_name"], | 
					
						
							|  |  |  |                                 "provider_id": provider_id, | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if provider_type == ToolProviderType.BUILT_IN.value: | 
					
						
							|  |  |  |                     generic_provider_id = GenericProviderID(provider_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if not existing_builtin_providers[generic_provider_id.provider_name]: | 
					
						
							|  |  |  |                         deleted_tools.append( | 
					
						
							|  |  |  |                             { | 
					
						
							|  |  |  |                                 "type": ToolProviderType.BUILT_IN.value, | 
					
						
							|  |  |  |                                 "tool_name": tool["tool_name"], | 
					
						
							|  |  |  |                                 "provider_id": provider_id,  # use the original one | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         ) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return deleted_tools | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def tags(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         tags = ( | 
					
						
							|  |  |  |             db.session.query(Tag) | 
					
						
							|  |  |  |             .join(TagBinding, Tag.id == TagBinding.tag_id) | 
					
						
							|  |  |  |             .filter( | 
					
						
							|  |  |  |                 TagBinding.target_id == self.id, | 
					
						
							|  |  |  |                 TagBinding.tenant_id == self.tenant_id, | 
					
						
							|  |  |  |                 Tag.tenant_id == self.tenant_id, | 
					
						
							|  |  |  |                 Tag.type == "app", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .all() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 15:50:49 +08:00
										 |  |  |         return tags or [] | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-26 18:20:53 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def author_name(self): | 
					
						
							|  |  |  |         if self.created_by: | 
					
						
							|  |  |  |             account = db.session.query(Account).filter(Account.id == self.created_by).first() | 
					
						
							|  |  |  |             if account: | 
					
						
							|  |  |  |                 return account.name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class AppModelConfig(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "app_model_configs" | 
					
						
							|  |  |  |     __table_args__ = (db.PrimaryKeyConstraint("id", name="app_model_config_pkey"), db.Index("app_app_id_idx", "app_id")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     provider = db.Column(db.String(255), nullable=True) | 
					
						
							|  |  |  |     model_id = db.Column(db.String(255), nullable=True) | 
					
						
							|  |  |  |     configs = db.Column(db.JSON, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  |     created_by = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  |     updated_by = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     opening_statement = db.Column(db.Text) | 
					
						
							|  |  |  |     suggested_questions = db.Column(db.Text) | 
					
						
							|  |  |  |     suggested_questions_after_answer = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  |     speech_to_text = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |     text_to_speech = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     more_like_this = db.Column(db.Text) | 
					
						
							|  |  |  |     model = db.Column(db.Text) | 
					
						
							|  |  |  |     user_input_form = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  |     dataset_query_variable = db.Column(db.String(255)) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     pre_prompt = db.Column(db.Text) | 
					
						
							|  |  |  |     agent_mode = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |     sensitive_word_avoidance = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     retriever_resource = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     prompt_type = db.Column(db.String(255), nullable=False, server_default=db.text("'simple'::character varying")) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:13:10 +08:00
										 |  |  |     chat_prompt_config = db.Column(db.Text) | 
					
						
							|  |  |  |     completion_prompt_config = db.Column(db.Text) | 
					
						
							|  |  |  |     dataset_configs = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:16 +08:00
										 |  |  |     external_data_tools = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |     file_upload = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def app(self): | 
					
						
							|  |  |  |         app = db.session.query(App).filter(App.id == self.app_id).first() | 
					
						
							|  |  |  |         return app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def model_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-11-22 15:48:50 +08:00
										 |  |  |         return json.loads(self.model) if self.model else {} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def suggested_questions_list(self) -> list: | 
					
						
							|  |  |  |         return json.loads(self.suggested_questions) if self.suggested_questions else [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def suggested_questions_after_answer_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             json.loads(self.suggested_questions_after_answer) | 
					
						
							|  |  |  |             if self.suggested_questions_after_answer | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             else {"enabled": False} | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-08-16 20:48:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def speech_to_text_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return json.loads(self.speech_to_text) if self.speech_to_text else {"enabled": False} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def text_to_speech_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return json.loads(self.text_to_speech) if self.text_to_speech else {"enabled": False} | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def retriever_resource_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return json.loads(self.retriever_resource) if self.retriever_resource else {"enabled": True} | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def annotation_reply_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         annotation_setting = ( | 
					
						
							|  |  |  |             db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == self.app_id).first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |         if annotation_setting: | 
					
						
							|  |  |  |             collection_binding_detail = annotation_setting.collection_binding_detail | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             if not collection_binding_detail: | 
					
						
							|  |  |  |                 raise ValueError("Collection binding detail not found") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |             return { | 
					
						
							|  |  |  |                 "id": annotation_setting.id, | 
					
						
							|  |  |  |                 "enabled": True, | 
					
						
							|  |  |  |                 "score_threshold": annotation_setting.score_threshold, | 
					
						
							|  |  |  |                 "embedding_model": { | 
					
						
							|  |  |  |                     "embedding_provider_name": collection_binding_detail.provider_name, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                     "embedding_model_name": collection_binding_detail.model_name, | 
					
						
							|  |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return {"enabled": False} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def more_like_this_dict(self) -> dict: | 
					
						
							|  |  |  |         return json.loads(self.more_like_this) if self.more_like_this else {"enabled": False} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def sensitive_word_avoidance_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             json.loads(self.sensitive_word_avoidance) | 
					
						
							|  |  |  |             if self.sensitive_word_avoidance | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:16 +08:00
										 |  |  |             else {"enabled": False, "type": "", "configs": []} | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def external_data_tools_list(self) -> list[dict]: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return json.loads(self.external_data_tools) if self.external_data_tools else [] | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @property | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     def user_input_form_list(self): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         return json.loads(self.user_input_form) if self.user_input_form else [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def agent_mode_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             json.loads(self.agent_mode) | 
					
						
							|  |  |  |             if self.agent_mode | 
					
						
							|  |  |  |             else {"enabled": False, "strategy": None, "tools": [], "prompt": None} | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 23:13:10 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def chat_prompt_config_dict(self) -> dict: | 
					
						
							|  |  |  |         return json.loads(self.chat_prompt_config) if self.chat_prompt_config else {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def completion_prompt_config_dict(self) -> dict: | 
					
						
							|  |  |  |         return json.loads(self.completion_prompt_config) if self.completion_prompt_config else {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def dataset_configs_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2023-11-17 22:13:37 +08:00
										 |  |  |         if self.dataset_configs: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             dataset_configs: dict = json.loads(self.dataset_configs) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             if "retrieval_model" not in dataset_configs: | 
					
						
							|  |  |  |                 return {"retrieval_model": "single"} | 
					
						
							| 
									
										
										
										
											2023-11-17 22:13:37 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 return dataset_configs | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:11 +08:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             "retrieval_model": "multiple", | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-12 23:13:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def file_upload_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             json.loads(self.file_upload) | 
					
						
							|  |  |  |             if self.file_upload | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 "image": { | 
					
						
							|  |  |  |                     "enabled": False, | 
					
						
							| 
									
										
										
										
											2025-04-11 15:26:26 +08:00
										 |  |  |                     "number_limits": DEFAULT_FILE_NUMBER_LIMITS, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                     "detail": "high", | 
					
						
							|  |  |  |                     "transfer_methods": ["remote_url", "local_file"], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |     def to_dict(self) -> dict: | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             "opening_statement": self.opening_statement, | 
					
						
							|  |  |  |             "suggested_questions": self.suggested_questions_list, | 
					
						
							|  |  |  |             "suggested_questions_after_answer": self.suggested_questions_after_answer_dict, | 
					
						
							|  |  |  |             "speech_to_text": self.speech_to_text_dict, | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |             "text_to_speech": self.text_to_speech_dict, | 
					
						
							| 
									
										
										
										
											2023-09-13 18:53:35 +08:00
										 |  |  |             "retriever_resource": self.retriever_resource_dict, | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |             "annotation_reply": self.annotation_reply_dict, | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |             "more_like_this": self.more_like_this_dict, | 
					
						
							|  |  |  |             "sensitive_word_avoidance": self.sensitive_word_avoidance_dict, | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:16 +08:00
										 |  |  |             "external_data_tools": self.external_data_tools_list, | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |             "model": self.model_dict, | 
					
						
							|  |  |  |             "user_input_form": self.user_input_form_list, | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  |             "dataset_query_variable": self.dataset_query_variable, | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |             "pre_prompt": self.pre_prompt, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:13:10 +08:00
										 |  |  |             "agent_mode": self.agent_mode_dict, | 
					
						
							|  |  |  |             "prompt_type": self.prompt_type, | 
					
						
							|  |  |  |             "chat_prompt_config": self.chat_prompt_config_dict, | 
					
						
							|  |  |  |             "completion_prompt_config": self.completion_prompt_config_dict, | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |             "dataset_configs": self.dataset_configs_dict, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             "file_upload": self.file_upload_dict, | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-31 15:16:34 +08:00
										 |  |  |     def from_model_config_dict(self, model_config: Mapping[str, Any]): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         self.opening_statement = model_config.get("opening_statement") | 
					
						
							|  |  |  |         self.suggested_questions = ( | 
					
						
							|  |  |  |             json.dumps(model_config["suggested_questions"]) if model_config.get("suggested_questions") else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.suggested_questions_after_answer = ( | 
					
						
							|  |  |  |             json.dumps(model_config["suggested_questions_after_answer"]) | 
					
						
							|  |  |  |             if model_config.get("suggested_questions_after_answer") | 
					
						
							|  |  |  |             else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.speech_to_text = json.dumps(model_config["speech_to_text"]) if model_config.get("speech_to_text") else None | 
					
						
							|  |  |  |         self.text_to_speech = json.dumps(model_config["text_to_speech"]) if model_config.get("text_to_speech") else None | 
					
						
							|  |  |  |         self.more_like_this = json.dumps(model_config["more_like_this"]) if model_config.get("more_like_this") else None | 
					
						
							|  |  |  |         self.sensitive_word_avoidance = ( | 
					
						
							|  |  |  |             json.dumps(model_config["sensitive_word_avoidance"]) | 
					
						
							|  |  |  |             if model_config.get("sensitive_word_avoidance") | 
					
						
							|  |  |  |             else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.external_data_tools = ( | 
					
						
							|  |  |  |             json.dumps(model_config["external_data_tools"]) if model_config.get("external_data_tools") else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.model = json.dumps(model_config["model"]) if model_config.get("model") else None | 
					
						
							|  |  |  |         self.user_input_form = ( | 
					
						
							|  |  |  |             json.dumps(model_config["user_input_form"]) if model_config.get("user_input_form") else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.dataset_query_variable = model_config.get("dataset_query_variable") | 
					
						
							|  |  |  |         self.pre_prompt = model_config["pre_prompt"] | 
					
						
							|  |  |  |         self.agent_mode = json.dumps(model_config["agent_mode"]) if model_config.get("agent_mode") else None | 
					
						
							|  |  |  |         self.retriever_resource = ( | 
					
						
							|  |  |  |             json.dumps(model_config["retriever_resource"]) if model_config.get("retriever_resource") else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.prompt_type = model_config.get("prompt_type", "simple") | 
					
						
							|  |  |  |         self.chat_prompt_config = ( | 
					
						
							|  |  |  |             json.dumps(model_config.get("chat_prompt_config")) if model_config.get("chat_prompt_config") else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.completion_prompt_config = ( | 
					
						
							|  |  |  |             json.dumps(model_config.get("completion_prompt_config")) | 
					
						
							|  |  |  |             if model_config.get("completion_prompt_config") | 
					
						
							|  |  |  |             else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.dataset_configs = ( | 
					
						
							|  |  |  |             json.dumps(model_config.get("dataset_configs")) if model_config.get("dataset_configs") else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.file_upload = json.dumps(model_config.get("file_upload")) if model_config.get("file_upload") else None | 
					
						
							| 
									
										
										
										
											2023-08-16 20:48:42 +08:00
										 |  |  |         return self | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def copy(self): | 
					
						
							|  |  |  |         new_app_model_config = AppModelConfig( | 
					
						
							|  |  |  |             id=self.id, | 
					
						
							|  |  |  |             app_id=self.app_id, | 
					
						
							|  |  |  |             opening_statement=self.opening_statement, | 
					
						
							|  |  |  |             suggested_questions=self.suggested_questions, | 
					
						
							|  |  |  |             suggested_questions_after_answer=self.suggested_questions_after_answer, | 
					
						
							|  |  |  |             speech_to_text=self.speech_to_text, | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |             text_to_speech=self.text_to_speech, | 
					
						
							| 
									
										
										
										
											2023-08-16 20:48:42 +08:00
										 |  |  |             more_like_this=self.more_like_this, | 
					
						
							|  |  |  |             sensitive_word_avoidance=self.sensitive_word_avoidance, | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:16 +08:00
										 |  |  |             external_data_tools=self.external_data_tools, | 
					
						
							| 
									
										
										
										
											2023-08-16 20:48:42 +08:00
										 |  |  |             model=self.model, | 
					
						
							|  |  |  |             user_input_form=self.user_input_form, | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  |             dataset_query_variable=self.dataset_query_variable, | 
					
						
							| 
									
										
										
										
											2023-08-16 20:48:42 +08:00
										 |  |  |             pre_prompt=self.pre_prompt, | 
					
						
							| 
									
										
										
										
											2023-10-12 03:37:11 +08:00
										 |  |  |             agent_mode=self.agent_mode, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:13:10 +08:00
										 |  |  |             retriever_resource=self.retriever_resource, | 
					
						
							|  |  |  |             prompt_type=self.prompt_type, | 
					
						
							|  |  |  |             chat_prompt_config=self.chat_prompt_config, | 
					
						
							|  |  |  |             completion_prompt_config=self.completion_prompt_config, | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |             dataset_configs=self.dataset_configs, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             file_upload=self.file_upload, | 
					
						
							| 
									
										
										
										
											2023-08-16 20:48:42 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new_app_model_config | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class RecommendedApp(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "recommended_apps" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="recommended_app_pkey"), | 
					
						
							|  |  |  |         db.Index("recommended_app_app_id_idx", "app_id"), | 
					
						
							|  |  |  |         db.Index("recommended_app_is_listed_idx", "is_listed", "language"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, primary_key=True, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     description = db.Column(db.JSON, nullable=False) | 
					
						
							|  |  |  |     copyright = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     privacy_policy = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  |     custom_disclaimer: Mapped[str] = mapped_column(sa.TEXT, default="") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     category = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     position = db.Column(db.Integer, nullable=False, default=0) | 
					
						
							|  |  |  |     is_listed = db.Column(db.Boolean, nullable=False, default=True) | 
					
						
							|  |  |  |     install_count = db.Column(db.Integer, nullable=False, default=0) | 
					
						
							| 
									
										
										
										
											2023-05-25 18:53:28 +08:00
										 |  |  |     language = db.Column(db.String(255), nullable=False, server_default=db.text("'en-US'::character varying")) | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def app(self): | 
					
						
							|  |  |  |         app = db.session.query(App).filter(App.id == self.app_id).first() | 
					
						
							|  |  |  |         return app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class InstalledApp(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "installed_apps" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="installed_app_pkey"), | 
					
						
							|  |  |  |         db.Index("installed_app_tenant_id_idx", "tenant_id"), | 
					
						
							|  |  |  |         db.Index("installed_app_app_id_idx", "app_id"), | 
					
						
							|  |  |  |         db.UniqueConstraint("tenant_id", "app_id", name="unique_tenant_app"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     app_owner_tenant_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     position = db.Column(db.Integer, nullable=False, default=0) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     is_pinned = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     last_used_at = db.Column(db.DateTime, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def app(self): | 
					
						
							|  |  |  |         app = db.session.query(App).filter(App.id == self.app_id).first() | 
					
						
							|  |  |  |         return app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def tenant(self): | 
					
						
							|  |  |  |         tenant = db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first() | 
					
						
							|  |  |  |         return tenant | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | class Conversation(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "conversations" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="conversation_pkey"), | 
					
						
							|  |  |  |         db.Index("conversation_app_from_user_idx", "app_id", "from_source", "from_end_user_id"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-25 16:24:52 +08:00
										 |  |  |     id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     app_model_config_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     model_provider = db.Column(db.String(255), nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     override_model_configs = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     model_id = db.Column(db.String(255), nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-25 16:24:52 +08:00
										 |  |  |     mode: Mapped[str] = mapped_column(db.String(255)) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     name = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     summary = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     _inputs: Mapped[dict] = mapped_column("inputs", db.JSON) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     introduction = db.Column(db.Text) | 
					
						
							|  |  |  |     system_instruction = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     system_instruction_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     status = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     invoke_from = db.Column(db.String(255), nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     from_source = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     from_end_user_id = db.Column(StringUUID) | 
					
						
							|  |  |  |     from_account_id = db.Column(StringUUID) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     read_at = db.Column(db.DateTime) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     read_account_id = db.Column(StringUUID) | 
					
						
							| 
									
										
										
										
											2024-08-15 10:53:05 +08:00
										 |  |  |     dialogue_count: Mapped[int] = mapped_column(default=0) | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     messages = db.relationship("Message", backref="conversation", lazy="select", passive_deletes="all") | 
					
						
							|  |  |  |     message_annotations = db.relationship( | 
					
						
							|  |  |  |         "MessageAnnotation", backref="conversation", lazy="select", passive_deletes="all" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     is_deleted = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2023-06-28 13:31:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def inputs(self): | 
					
						
							|  |  |  |         inputs = self._inputs.copy() | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Convert file mapping to File object | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         for key, value in inputs.items(): | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:50 +08:00
										 |  |  |             # NOTE: It's not the best way to implement this, but it's the only way to avoid circular import for now. | 
					
						
							|  |  |  |             from factories import file_factory | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             if isinstance(value, dict) and value.get("dify_model_identity") == FILE_MODEL_IDENTITY: | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:50 +08:00
										 |  |  |                 if value["transfer_method"] == FileTransferMethod.TOOL_FILE: | 
					
						
							|  |  |  |                     value["tool_file_id"] = value["related_id"] | 
					
						
							|  |  |  |                 elif value["transfer_method"] == FileTransferMethod.LOCAL_FILE: | 
					
						
							|  |  |  |                     value["upload_file_id"] = value["related_id"] | 
					
						
							|  |  |  |                 inputs[key] = file_factory.build_from_mapping(mapping=value, tenant_id=value["tenant_id"]) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             elif isinstance(value, list) and all( | 
					
						
							|  |  |  |                 isinstance(item, dict) and item.get("dify_model_identity") == FILE_MODEL_IDENTITY for item in value | 
					
						
							|  |  |  |             ): | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:50 +08:00
										 |  |  |                 inputs[key] = [] | 
					
						
							|  |  |  |                 for item in value: | 
					
						
							|  |  |  |                     if item["transfer_method"] == FileTransferMethod.TOOL_FILE: | 
					
						
							|  |  |  |                         item["tool_file_id"] = item["related_id"] | 
					
						
							|  |  |  |                     elif item["transfer_method"] == FileTransferMethod.LOCAL_FILE: | 
					
						
							|  |  |  |                         item["upload_file_id"] = item["related_id"] | 
					
						
							|  |  |  |                     inputs[key].append(file_factory.build_from_mapping(mapping=item, tenant_id=item["tenant_id"])) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         return inputs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @inputs.setter | 
					
						
							|  |  |  |     def inputs(self, value: Mapping[str, Any]): | 
					
						
							|  |  |  |         inputs = dict(value) | 
					
						
							|  |  |  |         for k, v in inputs.items(): | 
					
						
							|  |  |  |             if isinstance(v, File): | 
					
						
							|  |  |  |                 inputs[k] = v.model_dump() | 
					
						
							|  |  |  |             elif isinstance(v, list) and all(isinstance(item, File) for item in v): | 
					
						
							|  |  |  |                 inputs[k] = [item.model_dump() for item in v] | 
					
						
							|  |  |  |         self._inputs = inputs | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def model_config(self): | 
					
						
							|  |  |  |         model_config = {} | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         app_model_config: Optional[AppModelConfig] = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         if self.mode == AppMode.ADVANCED_CHAT.value: | 
					
						
							|  |  |  |             if self.override_model_configs: | 
					
						
							|  |  |  |                 override_model_configs = json.loads(self.override_model_configs) | 
					
						
							|  |  |  |                 model_config = override_model_configs | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             if self.override_model_configs: | 
					
						
							|  |  |  |                 override_model_configs = json.loads(self.override_model_configs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 if "model" in override_model_configs: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     app_model_config = AppModelConfig() | 
					
						
							|  |  |  |                     app_model_config = app_model_config.from_model_config_dict(override_model_configs) | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |                     assert app_model_config is not None, "app model config not found" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     model_config = app_model_config.to_dict() | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                     model_config["configs"] = override_model_configs | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 app_model_config = ( | 
					
						
							|  |  |  |                     db.session.query(AppModelConfig).filter(AppModelConfig.id == self.app_model_config_id).first() | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2024-11-22 15:48:50 +08:00
										 |  |  |                 if app_model_config: | 
					
						
							|  |  |  |                     model_config = app_model_config.to_dict() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         model_config["model_id"] = self.model_id | 
					
						
							|  |  |  |         model_config["provider"] = self.model_provider | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return model_config | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def summary_or_query(self): | 
					
						
							|  |  |  |         if self.summary: | 
					
						
							|  |  |  |             return self.summary | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             first_message = self.first_message | 
					
						
							|  |  |  |             if first_message: | 
					
						
							|  |  |  |                 return first_message.query | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 return "" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def annotated(self): | 
					
						
							|  |  |  |         return db.session.query(MessageAnnotation).filter(MessageAnnotation.conversation_id == self.id).count() > 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def annotation(self): | 
					
						
							|  |  |  |         return db.session.query(MessageAnnotation).filter(MessageAnnotation.conversation_id == self.id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def message_count(self): | 
					
						
							|  |  |  |         return db.session.query(Message).filter(Message.conversation_id == self.id).count() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def user_feedback_stats(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         like = ( | 
					
						
							|  |  |  |             db.session.query(MessageFeedback) | 
					
						
							|  |  |  |             .filter( | 
					
						
							|  |  |  |                 MessageFeedback.conversation_id == self.id, | 
					
						
							|  |  |  |                 MessageFeedback.from_source == "user", | 
					
						
							|  |  |  |                 MessageFeedback.rating == "like", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         dislike = ( | 
					
						
							|  |  |  |             db.session.query(MessageFeedback) | 
					
						
							|  |  |  |             .filter( | 
					
						
							|  |  |  |                 MessageFeedback.conversation_id == self.id, | 
					
						
							|  |  |  |                 MessageFeedback.from_source == "user", | 
					
						
							|  |  |  |                 MessageFeedback.rating == "dislike", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return {"like": like, "dislike": dislike} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def admin_feedback_stats(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         like = ( | 
					
						
							|  |  |  |             db.session.query(MessageFeedback) | 
					
						
							|  |  |  |             .filter( | 
					
						
							|  |  |  |                 MessageFeedback.conversation_id == self.id, | 
					
						
							|  |  |  |                 MessageFeedback.from_source == "admin", | 
					
						
							|  |  |  |                 MessageFeedback.rating == "like", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         dislike = ( | 
					
						
							|  |  |  |             db.session.query(MessageFeedback) | 
					
						
							|  |  |  |             .filter( | 
					
						
							|  |  |  |                 MessageFeedback.conversation_id == self.id, | 
					
						
							|  |  |  |                 MessageFeedback.from_source == "admin", | 
					
						
							|  |  |  |                 MessageFeedback.rating == "dislike", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return {"like": like, "dislike": dislike} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-20 14:13:44 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def status_count(self): | 
					
						
							|  |  |  |         messages = db.session.query(Message).filter(Message.conversation_id == self.id).all() | 
					
						
							|  |  |  |         status_counts = { | 
					
						
							| 
									
										
										
										
											2025-05-30 04:34:13 +08:00
										 |  |  |             WorkflowExecutionStatus.RUNNING: 0, | 
					
						
							|  |  |  |             WorkflowExecutionStatus.SUCCEEDED: 0, | 
					
						
							|  |  |  |             WorkflowExecutionStatus.FAILED: 0, | 
					
						
							|  |  |  |             WorkflowExecutionStatus.STOPPED: 0, | 
					
						
							|  |  |  |             WorkflowExecutionStatus.PARTIAL_SUCCEEDED: 0, | 
					
						
							| 
									
										
										
										
											2024-12-20 14:13:44 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for message in messages: | 
					
						
							|  |  |  |             if message.workflow_run: | 
					
						
							| 
									
										
										
										
											2025-05-30 04:34:13 +08:00
										 |  |  |                 status_counts[WorkflowExecutionStatus(message.workflow_run.status)] += 1 | 
					
						
							| 
									
										
										
										
											2024-12-20 14:13:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return ( | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-05-30 04:34:13 +08:00
										 |  |  |                 "success": status_counts[WorkflowExecutionStatus.SUCCEEDED], | 
					
						
							|  |  |  |                 "failed": status_counts[WorkflowExecutionStatus.FAILED], | 
					
						
							|  |  |  |                 "partial_success": status_counts[WorkflowExecutionStatus.PARTIAL_SUCCEEDED], | 
					
						
							| 
									
										
										
										
											2024-12-20 14:13:44 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             if messages | 
					
						
							|  |  |  |             else None | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def first_message(self): | 
					
						
							|  |  |  |         return db.session.query(Message).filter(Message.conversation_id == self.id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def app(self): | 
					
						
							|  |  |  |         return db.session.query(App).filter(App.id == self.app_id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-31 11:50:35 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def from_end_user_session_id(self): | 
					
						
							|  |  |  |         if self.from_end_user_id: | 
					
						
							|  |  |  |             end_user = db.session.query(EndUser).filter(EndUser.id == self.from_end_user_id).first() | 
					
						
							|  |  |  |             if end_user: | 
					
						
							|  |  |  |                 return end_user.session_id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 13:40:44 +09:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def from_account_name(self): | 
					
						
							|  |  |  |         if self.from_account_id: | 
					
						
							|  |  |  |             account = db.session.query(Account).filter(Account.id == self.from_account_id).first() | 
					
						
							|  |  |  |             if account: | 
					
						
							|  |  |  |                 return account.name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def in_debug_mode(self): | 
					
						
							|  |  |  |         return self.override_model_configs is not None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 21:30:35 +08:00
										 |  |  |     def to_dict(self): | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             "id": self.id, | 
					
						
							|  |  |  |             "app_id": self.app_id, | 
					
						
							|  |  |  |             "app_model_config_id": self.app_model_config_id, | 
					
						
							|  |  |  |             "model_provider": self.model_provider, | 
					
						
							|  |  |  |             "override_model_configs": self.override_model_configs, | 
					
						
							|  |  |  |             "model_id": self.model_id, | 
					
						
							|  |  |  |             "mode": self.mode, | 
					
						
							|  |  |  |             "name": self.name, | 
					
						
							|  |  |  |             "summary": self.summary, | 
					
						
							|  |  |  |             "inputs": self.inputs, | 
					
						
							|  |  |  |             "introduction": self.introduction, | 
					
						
							|  |  |  |             "system_instruction": self.system_instruction, | 
					
						
							|  |  |  |             "system_instruction_tokens": self.system_instruction_tokens, | 
					
						
							|  |  |  |             "status": self.status, | 
					
						
							|  |  |  |             "invoke_from": self.invoke_from, | 
					
						
							|  |  |  |             "from_source": self.from_source, | 
					
						
							|  |  |  |             "from_end_user_id": self.from_end_user_id, | 
					
						
							|  |  |  |             "from_account_id": self.from_account_id, | 
					
						
							|  |  |  |             "read_at": self.read_at, | 
					
						
							|  |  |  |             "read_account_id": self.read_account_id, | 
					
						
							|  |  |  |             "dialogue_count": self.dialogue_count, | 
					
						
							|  |  |  |             "created_at": self.created_at, | 
					
						
							|  |  |  |             "updated_at": self.updated_at, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | class Message(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "messages" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         PrimaryKeyConstraint("id", name="message_pkey"), | 
					
						
							|  |  |  |         Index("message_app_id_idx", "app_id", "created_at"), | 
					
						
							|  |  |  |         Index("message_conversation_id_idx", "conversation_id"), | 
					
						
							|  |  |  |         Index("message_end_user_idx", "app_id", "from_source", "from_end_user_id"), | 
					
						
							|  |  |  |         Index("message_account_idx", "app_id", "from_source", "from_account_id"), | 
					
						
							|  |  |  |         Index("message_workflow_run_id_idx", "conversation_id", "workflow_run_id"), | 
					
						
							|  |  |  |         Index("message_created_at_idx", "created_at"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-25 16:24:52 +08:00
										 |  |  |     id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     model_provider = db.Column(db.String(255), nullable=True) | 
					
						
							|  |  |  |     model_id = db.Column(db.String(255), nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     override_model_configs = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     conversation_id = db.Column(StringUUID, db.ForeignKey("conversations.id"), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     _inputs: Mapped[dict] = mapped_column("inputs", db.JSON) | 
					
						
							|  |  |  |     query: Mapped[str] = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     message = db.Column(db.JSON, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     message_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     message_unit_price = db.Column(db.Numeric(10, 4), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     message_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text("0.001")) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     answer: Mapped[str] = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +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")) | 
					
						
							| 
									
										
										
										
											2024-09-22 03:15:11 +08:00
										 |  |  |     parent_message_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     total_price = db.Column(db.Numeric(10, 7)) | 
					
						
							|  |  |  |     currency = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     status = db.Column(db.String(255), nullable=False, server_default=db.text("'normal'::character varying")) | 
					
						
							|  |  |  |     error = db.Column(db.Text) | 
					
						
							|  |  |  |     message_metadata = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     invoke_from: Mapped[Optional[str]] = db.Column(db.String(255), nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     from_source = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     from_end_user_id: Mapped[Optional[str]] = db.Column(StringUUID) | 
					
						
							|  |  |  |     from_account_id: Mapped[Optional[str]] = db.Column(StringUUID) | 
					
						
							| 
									
										
										
										
											2024-12-25 16:24:52 +08:00
										 |  |  |     created_at: Mapped[datetime] = mapped_column(db.DateTime, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     agent_based = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     workflow_run_id = db.Column(StringUUID) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def inputs(self): | 
					
						
							|  |  |  |         inputs = self._inputs.copy() | 
					
						
							|  |  |  |         for key, value in inputs.items(): | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:50 +08:00
										 |  |  |             # NOTE: It's not the best way to implement this, but it's the only way to avoid circular import for now. | 
					
						
							|  |  |  |             from factories import file_factory | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             if isinstance(value, dict) and value.get("dify_model_identity") == FILE_MODEL_IDENTITY: | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:50 +08:00
										 |  |  |                 if value["transfer_method"] == FileTransferMethod.TOOL_FILE: | 
					
						
							|  |  |  |                     value["tool_file_id"] = value["related_id"] | 
					
						
							|  |  |  |                 elif value["transfer_method"] == FileTransferMethod.LOCAL_FILE: | 
					
						
							|  |  |  |                     value["upload_file_id"] = value["related_id"] | 
					
						
							|  |  |  |                 inputs[key] = file_factory.build_from_mapping(mapping=value, tenant_id=value["tenant_id"]) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             elif isinstance(value, list) and all( | 
					
						
							|  |  |  |                 isinstance(item, dict) and item.get("dify_model_identity") == FILE_MODEL_IDENTITY for item in value | 
					
						
							|  |  |  |             ): | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:50 +08:00
										 |  |  |                 inputs[key] = [] | 
					
						
							|  |  |  |                 for item in value: | 
					
						
							|  |  |  |                     if item["transfer_method"] == FileTransferMethod.TOOL_FILE: | 
					
						
							|  |  |  |                         item["tool_file_id"] = item["related_id"] | 
					
						
							|  |  |  |                     elif item["transfer_method"] == FileTransferMethod.LOCAL_FILE: | 
					
						
							|  |  |  |                         item["upload_file_id"] = item["related_id"] | 
					
						
							|  |  |  |                     inputs[key].append(file_factory.build_from_mapping(mapping=item, tenant_id=item["tenant_id"])) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         return inputs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @inputs.setter | 
					
						
							|  |  |  |     def inputs(self, value: Mapping[str, Any]): | 
					
						
							|  |  |  |         inputs = dict(value) | 
					
						
							|  |  |  |         for k, v in inputs.items(): | 
					
						
							|  |  |  |             if isinstance(v, File): | 
					
						
							|  |  |  |                 inputs[k] = v.model_dump() | 
					
						
							|  |  |  |             elif isinstance(v, list) and all(isinstance(item, File) for item in v): | 
					
						
							|  |  |  |                 inputs[k] = [item.model_dump() for item in v] | 
					
						
							|  |  |  |         self._inputs = inputs | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def re_sign_file_url_answer(self) -> str: | 
					
						
							|  |  |  |         if not self.answer: | 
					
						
							|  |  |  |             return self.answer | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         pattern = r"\[!?.*?\]\((((http|https):\/\/.+)?\/files\/(tools\/)?[\w-]+.*?timestamp=.*&nonce=.*&sign=.*)\)" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         matches = re.findall(pattern, self.answer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not matches: | 
					
						
							|  |  |  |             return self.answer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         urls = [match[0] for match in matches] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # remove duplicate urls | 
					
						
							|  |  |  |         urls = list(set(urls)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not urls: | 
					
						
							|  |  |  |             return self.answer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         re_sign_file_url_answer = self.answer | 
					
						
							|  |  |  |         for url in urls: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             if "files/tools" in url: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                 # get tool file id | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 tool_file_id_pattern = r"\/files\/tools\/([\.\w-]+)?\?timestamp=" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                 result = re.search(tool_file_id_pattern, url) | 
					
						
							|  |  |  |                 if not result: | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 tool_file_id = result.group(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # get extension | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 if "." in tool_file_id: | 
					
						
							|  |  |  |                     split_result = tool_file_id.split(".") | 
					
						
							|  |  |  |                     extension = f".{split_result[-1]}" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     if len(extension) > 10: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                         extension = ".bin" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     tool_file_id = split_result[0] | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                     extension = ".bin" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if not tool_file_id: | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-30 17:28:02 +08:00
										 |  |  |                 sign_url = sign_tool_file(tool_file_id=tool_file_id, extension=extension) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             elif "file-preview" in url: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                 # get upload file id | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                 upload_file_id_pattern = r"\/files\/([\w-]+)\/file-preview?\?timestamp=" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                 result = re.search(upload_file_id_pattern, url) | 
					
						
							|  |  |  |                 if not result: | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 upload_file_id = result.group(1) | 
					
						
							|  |  |  |                 if not upload_file_id: | 
					
						
							|  |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                 sign_url = file_helpers.get_signed_file_url(upload_file_id) | 
					
						
							|  |  |  |             elif "image-preview" in url: | 
					
						
							|  |  |  |                 # image-preview is deprecated, use file-preview instead | 
					
						
							|  |  |  |                 upload_file_id_pattern = r"\/files\/([\w-]+)\/image-preview?\?timestamp=" | 
					
						
							|  |  |  |                 result = re.search(upload_file_id_pattern, url) | 
					
						
							|  |  |  |                 if not result: | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 upload_file_id = result.group(1) | 
					
						
							|  |  |  |                 if not upload_file_id: | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 sign_url = file_helpers.get_signed_file_url(upload_file_id) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2025-04-28 14:25:59 +08:00
										 |  |  |             # if as_attachment is in the url, add it to the sign_url. | 
					
						
							|  |  |  |             if "as_attachment" in url: | 
					
						
							|  |  |  |                 sign_url += "&as_attachment=true" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             re_sign_file_url_answer = re_sign_file_url_answer.replace(url, sign_url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return re_sign_file_url_answer | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def user_feedback(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         feedback = ( | 
					
						
							|  |  |  |             db.session.query(MessageFeedback) | 
					
						
							|  |  |  |             .filter(MessageFeedback.message_id == self.id, MessageFeedback.from_source == "user") | 
					
						
							|  |  |  |             .first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         return feedback | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def admin_feedback(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         feedback = ( | 
					
						
							|  |  |  |             db.session.query(MessageFeedback) | 
					
						
							|  |  |  |             .filter(MessageFeedback.message_id == self.id, MessageFeedback.from_source == "admin") | 
					
						
							|  |  |  |             .first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         return feedback | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def feedbacks(self): | 
					
						
							|  |  |  |         feedbacks = db.session.query(MessageFeedback).filter(MessageFeedback.message_id == self.id).all() | 
					
						
							|  |  |  |         return feedbacks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def annotation(self): | 
					
						
							|  |  |  |         annotation = db.session.query(MessageAnnotation).filter(MessageAnnotation.message_id == self.id).first() | 
					
						
							|  |  |  |         return annotation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def annotation_hit_history(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         annotation_history = ( | 
					
						
							|  |  |  |             db.session.query(AppAnnotationHitHistory).filter(AppAnnotationHitHistory.message_id == self.id).first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-12-19 17:47:25 +08:00
										 |  |  |         if annotation_history: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             annotation = ( | 
					
						
							|  |  |  |                 db.session.query(MessageAnnotation) | 
					
						
							|  |  |  |                 .filter(MessageAnnotation.id == annotation_history.annotation_id) | 
					
						
							|  |  |  |                 .first() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-12-19 17:47:25 +08:00
										 |  |  |             return annotation | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def app_model_config(self): | 
					
						
							|  |  |  |         conversation = db.session.query(Conversation).filter(Conversation.id == self.conversation_id).first() | 
					
						
							|  |  |  |         if conversation: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             return ( | 
					
						
							|  |  |  |                 db.session.query(AppModelConfig).filter(AppModelConfig.id == conversation.app_model_config_id).first() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def in_debug_mode(self): | 
					
						
							|  |  |  |         return self.override_model_configs is not None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def message_metadata_dict(self) -> dict: | 
					
						
							|  |  |  |         return json.loads(self.message_metadata) if self.message_metadata else {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def agent_thoughts(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             db.session.query(MessageAgentThought) | 
					
						
							|  |  |  |             .filter(MessageAgentThought.message_id == self.id) | 
					
						
							|  |  |  |             .order_by(MessageAgentThought.position.asc()) | 
					
						
							|  |  |  |             .all() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-07-27 13:08:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def retriever_resources(self): | 
					
						
							| 
									
										
										
										
											2025-04-17 18:05:15 +08:00
										 |  |  |         return self.message_metadata_dict.get("retriever_resources") if self.message_metadata else [] | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def message_files(self): | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         from factories import file_factory | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         message_files = db.session.query(MessageFile).filter(MessageFile.message_id == self.id).all() | 
					
						
							|  |  |  |         current_app = db.session.query(App).filter(App.id == self.app_id).first() | 
					
						
							|  |  |  |         if not current_app: | 
					
						
							|  |  |  |             raise ValueError(f"App {self.app_id} not found") | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         files = [] | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |         for message_file in message_files: | 
					
						
							| 
									
										
										
										
											2025-03-07 20:32:29 +08:00
										 |  |  |             if message_file.transfer_method == FileTransferMethod.LOCAL_FILE.value: | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                 if message_file.upload_file_id is None: | 
					
						
							|  |  |  |                     raise ValueError(f"MessageFile {message_file.id} is a local file but has no upload_file_id") | 
					
						
							|  |  |  |                 file = file_factory.build_from_mapping( | 
					
						
							|  |  |  |                     mapping={ | 
					
						
							|  |  |  |                         "id": message_file.id, | 
					
						
							|  |  |  |                         "type": message_file.type, | 
					
						
							| 
									
										
										
										
											2025-03-07 20:32:29 +08:00
										 |  |  |                         "transfer_method": message_file.transfer_method, | 
					
						
							|  |  |  |                         "upload_file_id": message_file.upload_file_id, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                     }, | 
					
						
							|  |  |  |                     tenant_id=current_app.tenant_id, | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2025-03-07 20:32:29 +08:00
										 |  |  |             elif message_file.transfer_method == FileTransferMethod.REMOTE_URL.value: | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                 if message_file.url is None: | 
					
						
							|  |  |  |                     raise ValueError(f"MessageFile {message_file.id} is a remote url but has no url") | 
					
						
							|  |  |  |                 file = file_factory.build_from_mapping( | 
					
						
							|  |  |  |                     mapping={ | 
					
						
							|  |  |  |                         "id": message_file.id, | 
					
						
							|  |  |  |                         "type": message_file.type, | 
					
						
							|  |  |  |                         "transfer_method": message_file.transfer_method, | 
					
						
							| 
									
										
										
										
											2025-03-07 20:32:29 +08:00
										 |  |  |                         "upload_file_id": message_file.upload_file_id, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                         "url": message_file.url, | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     tenant_id=current_app.tenant_id, | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2025-03-07 20:32:29 +08:00
										 |  |  |             elif message_file.transfer_method == FileTransferMethod.TOOL_FILE.value: | 
					
						
							| 
									
										
										
										
											2024-10-24 11:43:57 +08:00
										 |  |  |                 if message_file.upload_file_id is None: | 
					
						
							|  |  |  |                     assert message_file.url is not None | 
					
						
							|  |  |  |                     message_file.upload_file_id = message_file.url.split("/")[-1].split(".")[0] | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                 mapping = { | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                     "id": message_file.id, | 
					
						
							|  |  |  |                     "type": message_file.type, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                     "transfer_method": message_file.transfer_method, | 
					
						
							|  |  |  |                     "tool_file_id": message_file.upload_file_id, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                 file = file_factory.build_from_mapping( | 
					
						
							|  |  |  |                     mapping=mapping, | 
					
						
							|  |  |  |                     tenant_id=current_app.tenant_id, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise ValueError( | 
					
						
							|  |  |  |                     f"MessageFile {message_file.id} has an invalid transfer_method {message_file.transfer_method}" | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             files.append(file) | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         result = [ | 
					
						
							| 
									
										
										
										
											2025-04-16 09:56:46 +08:00
										 |  |  |             {"belongs_to": message_file.belongs_to, "upload_file_id": message_file.upload_file_id, **file.to_dict()} | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             for (file, message_file) in zip(files, message_files) | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-24 11:43:57 +08:00
										 |  |  |         db.session.commit() | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         return result | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def workflow_run(self): | 
					
						
							|  |  |  |         if self.workflow_run_id: | 
					
						
							| 
									
										
										
										
											2024-04-09 15:20:45 +08:00
										 |  |  |             from .workflow import WorkflowRun | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             return db.session.query(WorkflowRun).filter(WorkflowRun.id == self.workflow_run_id).first() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-28 00:24:37 +08:00
										 |  |  |     def to_dict(self) -> dict: | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             "id": self.id, | 
					
						
							|  |  |  |             "app_id": self.app_id, | 
					
						
							|  |  |  |             "conversation_id": self.conversation_id, | 
					
						
							| 
									
										
										
										
											2025-02-02 09:27:27 +02:00
										 |  |  |             "model_id": self.model_id, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             "inputs": self.inputs, | 
					
						
							|  |  |  |             "query": self.query, | 
					
						
							| 
									
										
										
										
											2025-02-02 09:27:27 +02:00
										 |  |  |             "total_price": self.total_price, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             "message": self.message, | 
					
						
							|  |  |  |             "answer": self.answer, | 
					
						
							|  |  |  |             "status": self.status, | 
					
						
							|  |  |  |             "error": self.error, | 
					
						
							|  |  |  |             "message_metadata": self.message_metadata_dict, | 
					
						
							|  |  |  |             "from_source": self.from_source, | 
					
						
							|  |  |  |             "from_end_user_id": self.from_end_user_id, | 
					
						
							|  |  |  |             "from_account_id": self.from_account_id, | 
					
						
							|  |  |  |             "created_at": self.created_at.isoformat(), | 
					
						
							|  |  |  |             "updated_at": self.updated_at.isoformat(), | 
					
						
							|  |  |  |             "agent_based": self.agent_based, | 
					
						
							|  |  |  |             "workflow_run_id": self.workflow_run_id, | 
					
						
							| 
									
										
										
										
											2024-06-28 00:24:37 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def from_dict(cls, data: dict): | 
					
						
							|  |  |  |         return cls( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             id=data["id"], | 
					
						
							|  |  |  |             app_id=data["app_id"], | 
					
						
							|  |  |  |             conversation_id=data["conversation_id"], | 
					
						
							| 
									
										
										
										
											2025-02-02 09:27:27 +02:00
										 |  |  |             model_id=data["model_id"], | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             inputs=data["inputs"], | 
					
						
							| 
									
										
										
										
											2025-02-02 09:27:27 +02:00
										 |  |  |             total_price=data["total_price"], | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             query=data["query"], | 
					
						
							|  |  |  |             message=data["message"], | 
					
						
							|  |  |  |             answer=data["answer"], | 
					
						
							|  |  |  |             status=data["status"], | 
					
						
							|  |  |  |             error=data["error"], | 
					
						
							|  |  |  |             message_metadata=json.dumps(data["message_metadata"]), | 
					
						
							|  |  |  |             from_source=data["from_source"], | 
					
						
							|  |  |  |             from_end_user_id=data["from_end_user_id"], | 
					
						
							|  |  |  |             from_account_id=data["from_account_id"], | 
					
						
							|  |  |  |             created_at=data["created_at"], | 
					
						
							|  |  |  |             updated_at=data["updated_at"], | 
					
						
							|  |  |  |             agent_based=data["agent_based"], | 
					
						
							|  |  |  |             workflow_run_id=data["workflow_run_id"], | 
					
						
							| 
									
										
										
										
											2024-06-28 00:24:37 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | class MessageFeedback(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "message_feedbacks" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="message_feedback_pkey"), | 
					
						
							|  |  |  |         db.Index("message_feedback_app_idx", "app_id"), | 
					
						
							|  |  |  |         db.Index("message_feedback_message_idx", "message_id", "from_source"), | 
					
						
							|  |  |  |         db.Index("message_feedback_conversation_idx", "conversation_id", "from_source", "rating"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     conversation_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     message_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     rating = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     content = db.Column(db.Text) | 
					
						
							|  |  |  |     from_source = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     from_end_user_id = db.Column(StringUUID) | 
					
						
							|  |  |  |     from_account_id = db.Column(StringUUID) | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def from_account(self): | 
					
						
							|  |  |  |         account = db.session.query(Account).filter(Account.id == self.from_account_id).first() | 
					
						
							|  |  |  |         return account | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-13 10:35:14 +08:00
										 |  |  |     def to_dict(self): | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             "id": str(self.id), | 
					
						
							|  |  |  |             "app_id": str(self.app_id), | 
					
						
							|  |  |  |             "conversation_id": str(self.conversation_id), | 
					
						
							|  |  |  |             "message_id": str(self.message_id), | 
					
						
							|  |  |  |             "rating": self.rating, | 
					
						
							|  |  |  |             "content": self.content, | 
					
						
							|  |  |  |             "from_source": self.from_source, | 
					
						
							|  |  |  |             "from_end_user_id": str(self.from_end_user_id) if self.from_end_user_id else None, | 
					
						
							|  |  |  |             "from_account_id": str(self.from_account_id) if self.from_account_id else None, | 
					
						
							|  |  |  |             "created_at": self.created_at.isoformat(), | 
					
						
							|  |  |  |             "updated_at": self.updated_at.isoformat(), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | class MessageFile(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "message_files" | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="message_file_pkey"), | 
					
						
							|  |  |  |         db.Index("message_file_message_idx", "message_id"), | 
					
						
							|  |  |  |         db.Index("message_file_created_by_idx", "created_by"), | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     def __init__( | 
					
						
							|  |  |  |         self, | 
					
						
							|  |  |  |         *, | 
					
						
							|  |  |  |         message_id: str, | 
					
						
							|  |  |  |         type: FileType, | 
					
						
							|  |  |  |         transfer_method: FileTransferMethod, | 
					
						
							|  |  |  |         url: str | None = None, | 
					
						
							|  |  |  |         belongs_to: Literal["user", "assistant"] | None = None, | 
					
						
							|  |  |  |         upload_file_id: str | None = None, | 
					
						
							| 
									
										
										
										
											2025-05-17 00:56:16 +08:00
										 |  |  |         created_by_role: CreatorUserRole, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         created_by: str, | 
					
						
							|  |  |  |     ): | 
					
						
							|  |  |  |         self.message_id = message_id | 
					
						
							|  |  |  |         self.type = type | 
					
						
							|  |  |  |         self.transfer_method = transfer_method | 
					
						
							|  |  |  |         self.url = url | 
					
						
							|  |  |  |         self.belongs_to = belongs_to | 
					
						
							|  |  |  |         self.upload_file_id = upload_file_id | 
					
						
							| 
									
										
										
										
											2024-10-22 21:56:26 +08:00
										 |  |  |         self.created_by_role = created_by_role.value | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         self.created_by = created_by | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     id: Mapped[str] = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							|  |  |  |     message_id: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     type: Mapped[str] = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     transfer_method: Mapped[str] = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     url: Mapped[Optional[str]] = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     belongs_to: Mapped[Optional[str]] = db.Column(db.String(255), nullable=True) | 
					
						
							|  |  |  |     upload_file_id: Mapped[Optional[str]] = db.Column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     created_by_role: Mapped[str] = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     created_by: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at: Mapped[datetime] = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-13 15:38:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | class MessageAnnotation(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "message_annotations" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="message_annotation_pkey"), | 
					
						
							|  |  |  |         db.Index("message_annotation_app_idx", "app_id"), | 
					
						
							|  |  |  |         db.Index("message_annotation_conversation_idx", "conversation_id"), | 
					
						
							|  |  |  |         db.Index("message_annotation_message_idx", "message_id"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     conversation_id = db.Column(StringUUID, db.ForeignKey("conversations.id"), nullable=True) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     message_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     question = db.Column(db.Text, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     content = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     hit_count = db.Column(db.Integer, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     account_id = db.Column(StringUUID, 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()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def account(self): | 
					
						
							|  |  |  |         account = db.session.query(Account).filter(Account.id == self.account_id).first() | 
					
						
							|  |  |  |         return account | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 17:47:25 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def annotation_create_account(self): | 
					
						
							|  |  |  |         account = db.session.query(Account).filter(Account.id == self.account_id).first() | 
					
						
							|  |  |  |         return account | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | class AppAnnotationHitHistory(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "app_annotation_hit_histories" | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="app_annotation_hit_histories_pkey"), | 
					
						
							|  |  |  |         db.Index("app_annotation_hit_histories_app_idx", "app_id"), | 
					
						
							|  |  |  |         db.Index("app_annotation_hit_histories_account_idx", "account_id"), | 
					
						
							|  |  |  |         db.Index("app_annotation_hit_histories_annotation_idx", "annotation_id"), | 
					
						
							|  |  |  |         db.Index("app_annotation_hit_histories_message_idx", "message_id"), | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  |     annotation_id: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     source = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     question = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     account_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     score = db.Column(Float, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     message_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     annotation_question = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     annotation_content = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def account(self): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         account = ( | 
					
						
							|  |  |  |             db.session.query(Account) | 
					
						
							|  |  |  |             .join(MessageAnnotation, MessageAnnotation.account_id == Account.id) | 
					
						
							|  |  |  |             .filter(MessageAnnotation.id == self.annotation_id) | 
					
						
							|  |  |  |             .first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |         return account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def annotation_create_account(self): | 
					
						
							|  |  |  |         account = db.session.query(Account).filter(Account.id == self.account_id).first() | 
					
						
							|  |  |  |         return account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-09 13:52:05 +08:00
										 |  |  | class AppAnnotationSetting(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "app_annotation_settings" | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="app_annotation_settings_pkey"), | 
					
						
							|  |  |  |         db.Index("app_annotation_settings_app_idx", "app_id"), | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     score_threshold = db.Column(Float, nullable=False, server_default=db.text("0")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     collection_binding_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     created_user_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     updated_user_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def collection_binding_detail(self): | 
					
						
							|  |  |  |         from .dataset import DatasetCollectionBinding | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         collection_binding_detail = ( | 
					
						
							|  |  |  |             db.session.query(DatasetCollectionBinding) | 
					
						
							|  |  |  |             .filter(DatasetCollectionBinding.id == self.collection_binding_id) | 
					
						
							|  |  |  |             .first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |         return collection_binding_detail | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class OperationLog(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "operation_logs" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="operation_log_pkey"), | 
					
						
							|  |  |  |         db.Index("operation_log_account_action_idx", "tenant_id", "account_id", "action"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     account_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     action = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     content = db.Column(db.JSON) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     created_ip = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class EndUser(Base, UserMixin): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "end_users" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="end_user_pkey"), | 
					
						
							|  |  |  |         db.Index("end_user_session_id_idx", "session_id", "type"), | 
					
						
							|  |  |  |         db.Index("end_user_tenant_session_id_idx", "tenant_id", "session_id", "type"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2025-05-17 00:56:16 +08:00
										 |  |  |     tenant_id: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     type = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     external_user_id = db.Column(db.String(255), nullable=True) | 
					
						
							|  |  |  |     name = db.Column(db.String(255)) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     is_anonymous = db.Column(db.Boolean, nullable=False, server_default=db.text("true")) | 
					
						
							| 
									
										
										
										
											2024-12-25 16:24:52 +08:00
										 |  |  |     session_id: Mapped[str] = mapped_column() | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class Site(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "sites" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="site_pkey"), | 
					
						
							|  |  |  |         db.Index("site_app_id_idx", "app_id"), | 
					
						
							|  |  |  |         db.Index("site_code_idx", "code", "status"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     title = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  |     icon_type = db.Column(db.String(255), nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     icon = db.Column(db.String(255)) | 
					
						
							|  |  |  |     icon_background = db.Column(db.String(255)) | 
					
						
							| 
									
										
										
										
											2024-01-21 12:30:16 +08:00
										 |  |  |     description = db.Column(db.Text) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     default_language = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  |     chat_color_theme = db.Column(db.String(255)) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     chat_color_theme_inverted = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     copyright = db.Column(db.String(255)) | 
					
						
							|  |  |  |     privacy_policy = db.Column(db.String(255)) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     show_workflow_steps = db.Column(db.Boolean, nullable=False, server_default=db.text("true")) | 
					
						
							| 
									
										
										
										
											2024-09-02 21:00:41 +09:00
										 |  |  |     use_icon_as_answer_icon = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2024-11-05 14:40:57 +08:00
										 |  |  |     _custom_disclaimer: Mapped[str] = mapped_column("custom_disclaimer", sa.TEXT, default="") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     customize_domain = db.Column(db.String(255)) | 
					
						
							|  |  |  |     customize_token_strategy = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     prompt_public = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     status = db.Column(db.String(255), nullable=False, server_default=db.text("'normal'::character varying")) | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  |     created_by = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-08-28 09:47:30 +09:00
										 |  |  |     updated_by = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     code = db.Column(db.String(255)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-05 14:40:57 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def custom_disclaimer(self): | 
					
						
							|  |  |  |         return self._custom_disclaimer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @custom_disclaimer.setter | 
					
						
							|  |  |  |     def custom_disclaimer(self, value: str): | 
					
						
							|  |  |  |         if len(value) > 512: | 
					
						
							|  |  |  |             raise ValueError("Custom disclaimer cannot exceed 512 characters.") | 
					
						
							|  |  |  |         self._custom_disclaimer = value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def generate_code(n): | 
					
						
							|  |  |  |         while True: | 
					
						
							|  |  |  |             result = generate_string(n) | 
					
						
							|  |  |  |             while db.session.query(Site).filter(Site.code == result).count() > 0: | 
					
						
							|  |  |  |                 result = generate_string(n) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def app_base_url(self): | 
					
						
							| 
									
										
										
										
											2024-09-12 15:50:49 +08:00
										 |  |  |         return dify_config.APP_WEB_URL or request.url_root.rstrip("/") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ApiToken(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "api_tokens" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="api_token_pkey"), | 
					
						
							|  |  |  |         db.Index("api_token_app_id_type_idx", "app_id", "type"), | 
					
						
							|  |  |  |         db.Index("api_token_token_idx", "token", "type"), | 
					
						
							|  |  |  |         db.Index("api_token_tenant_idx", "tenant_id", "type"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     tenant_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     type = db.Column(db.String(16), nullable=False) | 
					
						
							|  |  |  |     token = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     last_used_at = db.Column(db.DateTime, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							|  |  |  |     def generate_api_key(prefix, n): | 
					
						
							|  |  |  |         while True: | 
					
						
							|  |  |  |             result = prefix + generate_string(n) | 
					
						
							| 
									
										
										
										
											2025-01-09 21:30:17 +08:00
										 |  |  |             if db.session.query(ApiToken).filter(ApiToken.token == result).count() > 0: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class UploadFile(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "upload_files" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="upload_file_pkey"), | 
					
						
							|  |  |  |         db.Index("upload_file_tenant_idx", "tenant_id"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     id: Mapped[str] = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							|  |  |  |     tenant_id: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     storage_type: Mapped[str] = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     key: Mapped[str] = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     name: Mapped[str] = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     size: Mapped[int] = db.Column(db.Integer, nullable=False) | 
					
						
							|  |  |  |     extension: Mapped[str] = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     mime_type: Mapped[str] = db.Column(db.String(255), nullable=True) | 
					
						
							|  |  |  |     created_by_role: Mapped[str] = db.Column( | 
					
						
							|  |  |  |         db.String(255), nullable=False, server_default=db.text("'account'::character varying") | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     created_by: Mapped[str] = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at: Mapped[datetime] = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     used: Mapped[bool] = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) | 
					
						
							|  |  |  |     used_by: Mapped[str | None] = db.Column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     used_at: Mapped[datetime | None] = db.Column(db.DateTime, nullable=True) | 
					
						
							|  |  |  |     hash: Mapped[str | None] = db.Column(db.String(255), nullable=True) | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  |     source_url: Mapped[str] = mapped_column(sa.TEXT, default="") | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __init__( | 
					
						
							|  |  |  |         self, | 
					
						
							|  |  |  |         *, | 
					
						
							|  |  |  |         tenant_id: str, | 
					
						
							|  |  |  |         storage_type: str, | 
					
						
							|  |  |  |         key: str, | 
					
						
							|  |  |  |         name: str, | 
					
						
							|  |  |  |         size: int, | 
					
						
							|  |  |  |         extension: str, | 
					
						
							|  |  |  |         mime_type: str, | 
					
						
							| 
									
										
										
										
											2025-05-17 00:56:16 +08:00
										 |  |  |         created_by_role: CreatorUserRole, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         created_by: str, | 
					
						
							|  |  |  |         created_at: datetime, | 
					
						
							|  |  |  |         used: bool, | 
					
						
							|  |  |  |         used_by: str | None = None, | 
					
						
							|  |  |  |         used_at: datetime | None = None, | 
					
						
							|  |  |  |         hash: str | None = None, | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  |         source_url: str = "", | 
					
						
							|  |  |  |     ): | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         self.tenant_id = tenant_id | 
					
						
							|  |  |  |         self.storage_type = storage_type | 
					
						
							|  |  |  |         self.key = key | 
					
						
							|  |  |  |         self.name = name | 
					
						
							|  |  |  |         self.size = size | 
					
						
							|  |  |  |         self.extension = extension | 
					
						
							|  |  |  |         self.mime_type = mime_type | 
					
						
							| 
									
										
										
										
											2024-10-22 13:03:50 +08:00
										 |  |  |         self.created_by_role = created_by_role.value | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         self.created_by = created_by | 
					
						
							|  |  |  |         self.created_at = created_at | 
					
						
							|  |  |  |         self.used = used | 
					
						
							|  |  |  |         self.used_by = used_by | 
					
						
							|  |  |  |         self.used_at = used_at | 
					
						
							|  |  |  |         self.hash = hash | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  |         self.source_url = source_url | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ApiRequest(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "api_requests" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="api_request_pkey"), | 
					
						
							|  |  |  |         db.Index("api_request_token_idx", "tenant_id", "api_token_id"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, nullable=False, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     api_token_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     path = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     request = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     response = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     ip = 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()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class MessageChain(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "message_chains" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="message_chain_pkey"), | 
					
						
							|  |  |  |         db.Index("message_chain_message_id_idx", "message_id"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, nullable=False, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     message_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     type = db.Column(db.String(255), nullable=False) | 
					
						
							|  |  |  |     input = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     output = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=db.func.current_timestamp()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class MessageAgentThought(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "message_agent_thoughts" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="message_agent_thought_pkey"), | 
					
						
							|  |  |  |         db.Index("message_agent_thought_message_id_idx", "message_id"), | 
					
						
							|  |  |  |         db.Index("message_agent_thought_message_chain_id_idx", "message_chain_id"), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, nullable=False, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     message_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     message_chain_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     position = db.Column(db.Integer, nullable=False) | 
					
						
							|  |  |  |     thought = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     tool = db.Column(db.Text, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  |     tool_labels_str = db.Column(db.Text, nullable=False, server_default=db.text("'{}'::text")) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     tool_meta_str = db.Column(db.Text, nullable=False, server_default=db.text("'{}'::text")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     tool_input = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     observation = db.Column(db.Text, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     # plugin_id = db.Column(StringUUID, nullable=True)  ## for future design | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     tool_process_data = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     message = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     message_token = db.Column(db.Integer, nullable=True) | 
					
						
							|  |  |  |     message_unit_price = db.Column(db.Numeric, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     message_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text("0.001")) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     message_files = db.Column(db.Text, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     answer = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     answer_token = db.Column(db.Integer, nullable=True) | 
					
						
							|  |  |  |     answer_unit_price = db.Column(db.Numeric, nullable=True) | 
					
						
							| 
									
										
										
										
											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")) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     tokens = db.Column(db.Integer, nullable=True) | 
					
						
							|  |  |  |     total_price = db.Column(db.Numeric, nullable=True) | 
					
						
							|  |  |  |     currency = db.Column(db.String, nullable=True) | 
					
						
							|  |  |  |     latency = db.Column(db.Float, nullable=True) | 
					
						
							|  |  |  |     created_by_role = db.Column(db.String, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     created_by = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=db.func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def files(self) -> list: | 
					
						
							|  |  |  |         if self.message_files: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             return cast(list[Any], json.loads(self.message_files)) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             return [] | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def tools(self) -> list[str]: | 
					
						
							|  |  |  |         return self.tool.split(";") if self.tool else [] | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def tool_labels(self) -> dict: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             if self.tool_labels_str: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |                 return cast(dict, json.loads(self.tool_labels_str)) | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 return {} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  |             return {} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def tool_meta(self) -> dict: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             if self.tool_meta_str: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |                 return cast(dict, json.loads(self.tool_meta_str)) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 return {} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             return {} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def tool_inputs_dict(self) -> dict: | 
					
						
							|  |  |  |         tools = self.tools | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             if self.tool_input: | 
					
						
							|  |  |  |                 data = json.loads(self.tool_input) | 
					
						
							|  |  |  |                 result = {} | 
					
						
							|  |  |  |                 for tool in tools: | 
					
						
							|  |  |  |                     if tool in data: | 
					
						
							|  |  |  |                         result[tool] = data[tool] | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         if len(tools) == 1: | 
					
						
							|  |  |  |                             result[tool] = data | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             result[tool] = {} | 
					
						
							|  |  |  |                 return result | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 return {tool: {} for tool in tools} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             return {} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @property | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     def tool_outputs_dict(self): | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         tools = self.tools | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             if self.observation: | 
					
						
							|  |  |  |                 data = json.loads(self.observation) | 
					
						
							|  |  |  |                 result = {} | 
					
						
							|  |  |  |                 for tool in tools: | 
					
						
							|  |  |  |                     if tool in data: | 
					
						
							|  |  |  |                         result[tool] = data[tool] | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         if len(tools) == 1: | 
					
						
							|  |  |  |                             result[tool] = data | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             result[tool] = {} | 
					
						
							|  |  |  |                 return result | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |                 return {tool: {} for tool in tools} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             if self.observation: | 
					
						
							| 
									
										
										
										
											2024-08-19 09:21:11 +08:00
										 |  |  |                 return dict.fromkeys(tools, self.observation) | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 return {} | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class DatasetRetrieverResource(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "dataset_retriever_resources" | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="dataset_retriever_resource_pkey"), | 
					
						
							|  |  |  |         db.Index("dataset_retriever_resource_message_id_idx", "message_id"), | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, nullable=False, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     message_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     position = db.Column(db.Integer, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     dataset_id = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     dataset_name = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |     document_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     document_name = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |     data_source_type = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     segment_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     score = db.Column(db.Float, nullable=True) | 
					
						
							|  |  |  |     content = db.Column(db.Text, nullable=False) | 
					
						
							|  |  |  |     hit_count = db.Column(db.Integer, nullable=True) | 
					
						
							|  |  |  |     word_count = db.Column(db.Integer, nullable=True) | 
					
						
							|  |  |  |     segment_position = db.Column(db.Integer, nullable=True) | 
					
						
							|  |  |  |     index_node_hash = db.Column(db.Text, nullable=True) | 
					
						
							|  |  |  |     retriever_from = db.Column(db.Text, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     created_by = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=db.func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class Tag(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "tags" | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="tag_pkey"), | 
					
						
							|  |  |  |         db.Index("tag_type_idx", "type"), | 
					
						
							|  |  |  |         db.Index("tag_name_idx", "name"), | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     TAG_TYPE_LIST = ["knowledge", "app"] | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=True) | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     type = db.Column(db.String(16), nullable=False) | 
					
						
							|  |  |  |     name = db.Column(db.String(255), nullable=False) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     created_by = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class TagBinding(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "tag_bindings" | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="tag_binding_pkey"), | 
					
						
							|  |  |  |         db.Index("tag_bind_target_id_idx", "target_id"), | 
					
						
							|  |  |  |         db.Index("tag_bind_tag_id_idx", "tag_id"), | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-04-29 11:58:17 +08:00
										 |  |  |     tenant_id = db.Column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     tag_id = db.Column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     target_id = db.Column(StringUUID, nullable=True) | 
					
						
							|  |  |  |     created_by = db.Column(StringUUID, nullable=False) | 
					
						
							| 
									
										
										
										
											2024-12-21 23:13:58 +08:00
										 |  |  |     created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class TraceAppConfig(Base): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     __tablename__ = "trace_app_config" | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |     __table_args__ = ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |         db.PrimaryKeyConstraint("id", name="tracing_app_config_pkey"), | 
					
						
							|  |  |  |         db.Index("trace_app_config_app_id_idx", "app_id"), | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()")) | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |     app_id = db.Column(StringUUID, nullable=False) | 
					
						
							|  |  |  |     tracing_provider = db.Column(db.String(255), nullable=True) | 
					
						
							|  |  |  |     tracing_config = db.Column(db.JSON, nullable=True) | 
					
						
							| 
									
										
										
										
											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(), onupdate=func.current_timestamp() | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |     is_active = db.Column(db.Boolean, nullable=False, server_default=db.text("true")) | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def tracing_config_dict(self): | 
					
						
							| 
									
										
										
										
											2024-09-12 15:50:49 +08:00
										 |  |  |         return self.tracing_config or {} | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def tracing_config_str(self): | 
					
						
							|  |  |  |         return json.dumps(self.tracing_config_dict) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def to_dict(self): | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-09-10 17:08:06 +08:00
										 |  |  |             "id": self.id, | 
					
						
							|  |  |  |             "app_id": self.app_id, | 
					
						
							|  |  |  |             "tracing_provider": self.tracing_provider, | 
					
						
							|  |  |  |             "tracing_config": self.tracing_config_dict, | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |             "is_active": self.is_active, | 
					
						
							| 
									
										
										
										
											2024-09-13 22:42:08 +08:00
										 |  |  |             "created_at": str(self.created_at) if self.created_at else None, | 
					
						
							|  |  |  |             "updated_at": str(self.updated_at) if self.updated_at else None, | 
					
						
							| 
									
										
										
										
											2024-06-26 17:33:29 +08:00
										 |  |  |         } |