| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 | from collections.abc import Mapping, Sequencefrom enum import Enumfrom typing import Any, Optionalfrom pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validatorfrom constants import UUID_NILfrom core.app.app_config.entities import EasyUIBasedAppConfig, WorkflowUIBasedAppConfigfrom core.entities.provider_configuration import ProviderModelBundlefrom core.file import File, FileUploadConfigfrom core.model_runtime.entities.model_entities import AIModelEntityfrom core.ops.ops_trace_manager import TraceQueueManagerclass InvokeFrom(Enum):    """    Invoke From.    """    SERVICE_API = "service-api"    WEB_APP = "web-app"    EXPLORE = "explore"    DEBUGGER = "debugger"    @classmethod    def value_of(cls, value: str):        """        Get value of given mode.        :param value: mode value        :return: mode        """        for mode in cls:            if mode.value == value:                return mode        raise ValueError(f"invalid invoke from value {value}")    def to_source(self) -> str:        """        Get source of invoke from.        :return: source        """        if self == InvokeFrom.WEB_APP:            return "web_app"        elif self == InvokeFrom.DEBUGGER:            return "dev"        elif self == InvokeFrom.EXPLORE:            return "explore_app"        elif self == InvokeFrom.SERVICE_API:            return "api"        return "dev"class ModelConfigWithCredentialsEntity(BaseModel):    """    Model Config With Credentials Entity.    """    provider: str    model: str    model_schema: AIModelEntity    mode: str    provider_model_bundle: ProviderModelBundle    credentials: dict[str, Any] = Field(default_factory=dict)    parameters: dict[str, Any] = Field(default_factory=dict)    stop: list[str] = Field(default_factory=list)    # pydantic configs    model_config = ConfigDict(protected_namespaces=())class AppGenerateEntity(BaseModel):    """    App Generate Entity.    """    task_id: str    # app config    app_config: Any    file_upload_config: Optional[FileUploadConfig] = None    inputs: Mapping[str, Any]    files: Sequence[File]    user_id: str    # extras    stream: bool    invoke_from: InvokeFrom    # invoke call depth    call_depth: int = 0    # extra parameters, like: auto_generate_conversation_name    extras: dict[str, Any] = Field(default_factory=dict)    # tracing instance    trace_manager: Optional[TraceQueueManager] = None    class Config:        arbitrary_types_allowed = Trueclass EasyUIBasedAppGenerateEntity(AppGenerateEntity):    """    Chat Application Generate Entity.    """    # app config    app_config: EasyUIBasedAppConfig    model_conf: ModelConfigWithCredentialsEntity    query: Optional[str] = None    # pydantic configs    model_config = ConfigDict(protected_namespaces=())class ConversationAppGenerateEntity(AppGenerateEntity):    """    Base entity for conversation-based app generation.    """    conversation_id: Optional[str] = None    parent_message_id: Optional[str] = Field(        default=None,        description=(            "Starting from v0.9.0, parent_message_id is used to support message regeneration for internal chat API."            "For service API, we need to ensure its forward compatibility, "            "so passing in the parent_message_id as request arg is not supported for now. "            "It needs to be set to UUID_NIL so that the subsequent processing will treat it as legacy messages."        ),    )    @field_validator("parent_message_id")    @classmethod    def validate_parent_message_id(cls, v, info: ValidationInfo):        if info.data.get("invoke_from") == InvokeFrom.SERVICE_API and v != UUID_NIL:            raise ValueError("parent_message_id should be UUID_NIL for service API")        return vclass ChatAppGenerateEntity(ConversationAppGenerateEntity, EasyUIBasedAppGenerateEntity):    """    Chat Application Generate Entity.    """    passclass CompletionAppGenerateEntity(EasyUIBasedAppGenerateEntity):    """    Completion Application Generate Entity.    """    passclass AgentChatAppGenerateEntity(ConversationAppGenerateEntity, EasyUIBasedAppGenerateEntity):    """    Agent Chat Application Generate Entity.    """    passclass AdvancedChatAppGenerateEntity(ConversationAppGenerateEntity):    """    Advanced Chat Application Generate Entity.    """    # app config    app_config: WorkflowUIBasedAppConfig    workflow_run_id: Optional[str] = None    query: str    class SingleIterationRunEntity(BaseModel):        """        Single Iteration Run Entity.        """        node_id: str        inputs: Mapping    single_iteration_run: Optional[SingleIterationRunEntity] = None    class SingleLoopRunEntity(BaseModel):        """        Single Loop Run Entity.        """        node_id: str        inputs: Mapping    single_loop_run: Optional[SingleLoopRunEntity] = Noneclass WorkflowAppGenerateEntity(AppGenerateEntity):    """    Workflow Application Generate Entity.    """    # app config    app_config: WorkflowUIBasedAppConfig    workflow_run_id: str    class SingleIterationRunEntity(BaseModel):        """        Single Iteration Run Entity.        """        node_id: str        inputs: dict    single_iteration_run: Optional[SingleIterationRunEntity] = None    class SingleLoopRunEntity(BaseModel):        """        Single Loop Run Entity.        """        node_id: str        inputs: dict    single_loop_run: Optional[SingleLoopRunEntity] = None
 |