Browse Source

feat: agent management

Yeuoly 5 months ago
parent
commit
a175d6b2d7

+ 1 - 0
api/controllers/console/__init__.py

@@ -91,5 +91,6 @@ from .workspace import (
     models,
     plugin,
     tool_providers,
+    agent_providers,
     workspace,
 )

+ 35 - 0
api/controllers/console/workspace/agent_providers.py

@@ -0,0 +1,35 @@
+from flask_login import current_user
+from flask_restful import Resource
+from controllers.console import api
+from controllers.console.wraps import account_initialization_required, setup_required
+from core.model_runtime.utils.encoders import jsonable_encoder
+from libs.login import login_required
+from services.agent_service import AgentService
+
+
+class AgentProviderListApi(Resource):
+    @setup_required
+    @login_required
+    @account_initialization_required
+    def get(self):
+        user = current_user
+
+        user_id = user.id
+        tenant_id = user.current_tenant_id
+
+        return jsonable_encoder(AgentService.list_agent_providers(user_id, tenant_id))
+
+
+class AgentProviderApi(Resource):
+    @setup_required
+    @login_required
+    @account_initialization_required
+    def get(self, provider_name: str):
+        user = current_user
+        user_id = user.id
+        tenant_id = user.current_tenant_id
+        return jsonable_encoder(AgentService.get_agent_provider(user_id, tenant_id, provider_name))
+
+
+api.add_resource(AgentProviderListApi, "/workspaces/current/agent-providers")
+api.add_resource(AgentProviderApi, "/workspaces/current/agent-provider/<path:provider_name>")

+ 5 - 2
api/core/plugin/manager/agent.py

@@ -18,9 +18,12 @@ class PluginAgentManager(BasePluginManager):
         def transformer(json_response: dict[str, Any]) -> dict:
             for provider in json_response.get("data", []):
                 declaration = provider.get("declaration", {}) or {}
+                declaration["identity"]["name"] = (
+                    f"{provider.get('plugin_id')}/{declaration.get('identity', {}).get('name')}"
+                )
                 provider_name = declaration.get("identity", {}).get("name")
-                for tool in declaration.get("tools", []):
-                    tool["identity"]["provider"] = provider_name
+                for strategy in declaration.get("strategies", []):
+                    strategy["identity"]["provider"] = provider_name
 
             return json_response
 

+ 2 - 2
api/core/workflow/nodes/agent/agent_node.py

@@ -99,9 +99,9 @@ class AgentNode(ToolNode):
         Generate parameters based on the given tool parameters, variable pool, and node data.
 
         Args:
-            tool_parameters (Sequence[ToolParameter]): The list of tool parameters.
+            agent_parameters (Sequence[AgentParameter]): The list of agent parameters.
             variable_pool (VariablePool): The variable pool containing the variables.
-            node_data (ToolNodeData): The data associated with the tool node.
+            node_data (AgentNodeData): The data associated with the agent node.
 
         Returns:
             Mapping[str, Any]: A dictionary containing the generated parameters.

+ 28 - 4
api/services/agent_service.py

@@ -5,6 +5,7 @@ from flask_login import current_user
 
 import contexts
 from core.app.app_config.easy_ui_based_app.agent.manager import AgentConfigManager
+from core.plugin.manager.agent import PluginAgentManager
 from core.tools.tool_manager import ToolManager
 from extensions.ext_database import db
 from models.account import Account
@@ -63,6 +64,10 @@ class AgentService:
 
         timezone = pytz.timezone(current_user.timezone)
 
+        app_model_config = app_model.app_model_config
+        if not app_model_config:
+            raise ValueError("App model config not found")
+
         result = {
             "meta": {
                 "status": "success",
@@ -70,15 +75,18 @@ class AgentService:
                 "start_time": message.created_at.astimezone(timezone).isoformat(),
                 "elapsed_time": message.provider_response_latency,
                 "total_tokens": message.answer_tokens + message.message_tokens,
-                "agent_mode": app_model.app_model_config.agent_mode_dict.get("strategy", "react"),
+                "agent_mode": app_model_config.agent_mode_dict.get("strategy", "react"),
                 "iterations": len(agent_thoughts),
             },
             "iterations": [],
             "files": message.message_files,
         }
 
-        agent_config = AgentConfigManager.convert(app_model.app_model_config.to_dict())
-        agent_tools = agent_config.tools
+        agent_config = AgentConfigManager.convert(app_model_config.to_dict())
+        if not agent_config:
+            raise ValueError("Agent config not found")
+
+        agent_tools = agent_config.tools or []
 
         def find_agent_tool(tool_name: str):
             for agent_tool in agent_tools:
@@ -90,7 +98,7 @@ class AgentService:
             tool_labels = agent_thought.tool_labels
             tool_meta = agent_thought.tool_meta
             tool_inputs = agent_thought.tool_inputs_dict
-            tool_outputs = agent_thought.tool_outputs_dict
+            tool_outputs = agent_thought.tool_outputs_dict or {}
             tool_calls = []
             for tool in tools:
                 tool_name = tool
@@ -145,3 +153,19 @@ class AgentService:
             )
 
         return result
+
+    @classmethod
+    def list_agent_providers(cls, user_id: str, tenant_id: str):
+        """
+        List agent providers
+        """
+        manager = PluginAgentManager()
+        return manager.fetch_agent_providers(tenant_id)
+
+    @classmethod
+    def get_agent_provider(cls, user_id: str, tenant_id: str, provider_name: str):
+        """
+        Get agent provider
+        """
+        manager = PluginAgentManager()
+        return manager.fetch_agent_provider(tenant_id, provider_name)