diff --git a/dbgpt/agent/resource/app.py b/dbgpt/agent/resource/app.py index 595c5fdb9..70810ade6 100644 --- a/dbgpt/agent/resource/app.py +++ b/dbgpt/agent/resource/app.py @@ -5,13 +5,16 @@ import uuid from typing import Any, Dict, List, Optional, Tuple, Type, cast from dbgpt.agent import AgentMessage, ConversableAgent -from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager from dbgpt.util import ParameterDescription from .base import Resource, ResourceParameters, ResourceType def _get_app_list(): + # TODO: Don't import dbgpt.serve in dbgpt.agent module + from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager + + # Only call this function when the system app is initialized apps = get_app_manager().get_dbgpts() results = [ { @@ -24,53 +27,63 @@ def _get_app_list(): return results -@dataclasses.dataclass -class AppResourceParameters(ResourceParameters): - """Application resource class.""" +def _create_app_resource_parameters() -> Type[ResourceParameters]: + """Create AppResourceParameters.""" - app_code: str = dataclasses.field( - metadata={ - "help": "app code", - "valid_values": _get_app_list(), - }, - ) + @dataclasses.dataclass + class _DynAppResourceParameters(ResourceParameters): + """Application resource class.""" - @classmethod - def to_configurations( - cls, - parameters: Type["AppResourceParameters"], - version: Optional[str] = None, - **kwargs, - ) -> Any: - """Convert the parameters to configurations.""" - conf: List[ParameterDescription] = cast( - List[ParameterDescription], super().to_configurations(parameters) + app_code: str = dataclasses.field( + metadata={ + "help": "app code", + "valid_values": _get_app_list(), + }, ) - version = version or cls._resource_version() - if version != "v1": - return conf - # Compatible with old version - for param in conf: - if param.param_name == "app_code": - return param.valid_values or [] - return [] - @classmethod - def from_dict( - cls, data: dict, ignore_extra_fields: bool = True - ) -> ResourceParameters: - """Create a new instance from a dictionary.""" - copied_data = data.copy() - if "app_code" not in copied_data and "value" in copied_data: - copied_data["app_code"] = copied_data.pop("value") - return super().from_dict(copied_data, ignore_extra_fields=ignore_extra_fields) + @classmethod + def to_configurations( + cls, + parameters: Type["ResourceParameters"], + version: Optional[str] = None, + **kwargs, + ) -> Any: + """Convert the parameters to configurations.""" + conf: List[ParameterDescription] = cast( + List[ParameterDescription], super().to_configurations(parameters) + ) + version = version or cls._resource_version() + if version != "v1": + return conf + # Compatible with old version + for param in conf: + if param.param_name == "app_code": + return param.valid_values or [] + return [] + + @classmethod + def from_dict( + cls, data: dict, ignore_extra_fields: bool = True + ) -> ResourceParameters: + """Create a new instance from a dictionary.""" + copied_data = data.copy() + if "app_code" not in copied_data and "value" in copied_data: + copied_data["app_code"] = copied_data.pop("value") + return super().from_dict( + copied_data, ignore_extra_fields=ignore_extra_fields + ) + + return _DynAppResourceParameters -class AppResource(Resource[AppResourceParameters]): +class AppResource(Resource[ResourceParameters]): """AppResource resource class.""" def __init__(self, name: str, app_code: str, **kwargs): """Initialize AppResource resource.""" + # TODO: Don't import dbgpt.serve in dbgpt.agent module + from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager + self._resource_name = name self._app_code = app_code @@ -101,7 +114,7 @@ class AppResource(Resource[AppResourceParameters]): @classmethod def resource_parameters_class(cls, **kwargs) -> Type[ResourceParameters]: """Return the resource parameters class.""" - return AppResourceParameters + return _create_app_resource_parameters() async def get_prompt( self, @@ -172,6 +185,9 @@ async def _start_app( conv_uid: Optional[str] = None, ) -> AgentMessage: """Start App By AppResource.""" + # TODO: Don't import dbgpt.serve in dbgpt.agent module + from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager + conv_uid = str(uuid.uuid4()) if conv_uid is None else conv_uid gpts_app = get_app_manager().get_app(app_code) app_agent = await get_app_manager().create_agent_by_app_code( diff --git a/dbgpt/serve/agent/agents/controller.py b/dbgpt/serve/agent/agents/controller.py index 8f857f907..8f0a9c598 100644 --- a/dbgpt/serve/agent/agents/controller.py +++ b/dbgpt/serve/agent/agents/controller.py @@ -107,6 +107,18 @@ class MultiAgents(BaseComponent, ABC): super().__init__(system_app) self.system_app = system_app + def on_init(self): + """Called when init the application. + + Import your own module here to ensure the module is loaded before the application starts + """ + from ..db.gpts_app import ( + GptsAppCollectionEntity, + GptsAppDetailEntity, + GptsAppEntity, + UserRecentAppsEntity, + ) + def get_dbgpts(self, user_code: str = None, sys_code: str = None): apps = self.gpts_app.app_list( GptsAppQuery(user_code=user_code, sys_code=sys_code)