diff --git a/api/core/plugin/manager/tool.py b/api/core/plugin/manager/tool.py index 50970243a1..874b3ff596 100644 --- a/api/core/plugin/manager/tool.py +++ b/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 [] diff --git a/api/core/tools/entities/tool_entities.py b/api/core/tools/entities/tool_entities.py index 0808ff721d..d037b00d33 100644 --- a/api/core/tools/entities/tool_entities.py +++ b/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=()) diff --git a/api/core/tools/plugin_tool/tool.py b/api/core/tools/plugin_tool/tool.py index 7c6c4de3e0..afbd2e40bf 100644 --- a/api/core/tools/plugin_tool/tool.py +++ b/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, + ) diff --git a/api/core/tools/tool_manager.py b/api/core/tools/tool_manager.py index 2f747a317e..d780d2866f 100644 --- a/api/core/tools/tool_manager.py +++ b/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