feat: support get tool runtime parameters

This commit is contained in:
Yeuoly 2024-09-29 18:18:45 +08:00
parent 8563155d1b
commit d4e007f9db
No known key found for this signature in database
GPG Key ID: A66E7E320FB19F61
4 changed files with 65 additions and 3 deletions

View File

@ -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 []

View File

@ -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=())

View File

@ -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,
)

View File

@ -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