Browse Source

feat: support get tool runtime parameters

Yeuoly 7 months ago
parent
commit
d4e007f9db

+ 41 - 1
api/core/plugin/manager/tool.py

@@ -1,9 +1,11 @@
 from collections.abc import Generator
 from typing import Any
 
+from pydantic import BaseModel
+
 from core.plugin.entities.plugin_daemon import PluginBasicBooleanResponse, PluginToolProviderEntity
 from core.plugin.manager.base import BasePluginManager
-from core.tools.entities.tool_entities import ToolInvokeMessage
+from core.tools.entities.tool_entities import ToolInvokeMessage, ToolParameter
 
 
 class PluginToolManager(BasePluginManager):
@@ -144,3 +146,41 @@ class PluginToolManager(BasePluginManager):
             return resp.result
 
         return False
+
+    def get_runtime_parameters(
+        self,
+        tenant_id: str,
+        user_id: str,
+        provider: str,
+        credentials: dict[str, Any],
+        tool: str,
+    ) -> list[ToolParameter]:
+        """
+        get the runtime parameters of the tool
+        """
+        plugin_id, provider_name = self._split_provider(provider)
+
+        class RuntimeParametersResponse(BaseModel):
+            parameters: list[ToolParameter]
+
+        response = self._request_with_plugin_daemon_response_stream(
+            "GET",
+            f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters",
+            RuntimeParametersResponse,
+            params={
+                "user_id": user_id,
+                "data": {
+                    "provider": provider_name,
+                    "tool": tool,
+                    "credentials": credentials,
+                },
+            },
+            headers={
+                "X-Plugin-ID": plugin_id,
+            },
+        )
+
+        for resp in response:
+            return resp.parameters
+
+        return []

+ 2 - 0
api/core/tools/entities/tool_entities.py

@@ -298,6 +298,8 @@ class ToolEntity(BaseModel):
     identity: ToolIdentity
     parameters: list[ToolParameter] = Field(default_factory=list)
     description: Optional[ToolDescription] = None
+    # TODO: output schema
+    has_runtime_parameters: bool = Field(default=False, description="Whether the tool has runtime parameters")
 
     # pydantic configs
     model_config = ConfigDict(protected_namespaces=())

+ 17 - 1
api/core/tools/plugin_tool/tool.py

@@ -4,7 +4,7 @@ from typing import Any
 from core.plugin.manager.tool import PluginToolManager
 from core.tools.__base.tool import Tool
 from core.tools.__base.tool_runtime import ToolRuntime
-from core.tools.entities.tool_entities import ToolEntity, ToolInvokeMessage, ToolProviderType
+from core.tools.entities.tool_entities import ToolEntity, ToolInvokeMessage, ToolParameter, ToolProviderType
 
 
 class PluginTool(Tool):
@@ -35,3 +35,19 @@ class PluginTool(Tool):
             runtime=runtime,
             tenant_id=self.tenant_id,
         )
+
+    def get_runtime_parameters(self) -> list[ToolParameter]:
+        """
+        get the runtime parameters
+        """
+        if not self.entity.has_runtime_parameters:
+            return self.entity.parameters
+
+        manager = PluginToolManager()
+        return manager.get_runtime_parameters(
+            tenant_id=self.tenant_id,
+            user_id="",
+            provider=self.entity.identity.provider,
+            tool=self.entity.identity.name,
+            credentials=self.runtime.credentials,
+        )

+ 5 - 1
api/core/tools/tool_manager.py

@@ -255,7 +255,11 @@ class ToolManager:
 
     @classmethod
     def get_agent_tool_runtime(
-        cls, tenant_id: str, app_id: str, agent_tool: AgentToolEntity, invoke_from: InvokeFrom = InvokeFrom.DEBUGGER
+        cls,
+        tenant_id: str,
+        app_id: str,
+        agent_tool: AgentToolEntity,
+        invoke_from: InvokeFrom = InvokeFrom.DEBUGGER,
     ) -> Tool:
         """
         get the agent tool runtime