tool.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from collections.abc import Generator
  2. from typing import Any
  3. from core.plugin.manager.tool import PluginToolManager
  4. from core.tools.__base.tool import Tool
  5. from core.tools.__base.tool_runtime import ToolRuntime
  6. from core.tools.entities.tool_entities import ToolEntity, ToolInvokeMessage, ToolProviderType
  7. class PluginTool(Tool):
  8. tenant_id: str
  9. plugin_id: str
  10. def __init__(self, entity: ToolEntity, runtime: ToolRuntime, tenant_id: str, plugin_id: str) -> None:
  11. super().__init__(entity, runtime)
  12. self.tenant_id = tenant_id
  13. self.plugin_id = plugin_id
  14. @property
  15. def tool_provider_type(self) -> ToolProviderType:
  16. return ToolProviderType.PLUGIN
  17. def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]:
  18. manager = PluginToolManager()
  19. return manager.invoke(
  20. tenant_id=self.tenant_id,
  21. user_id=user_id,
  22. plugin_id=self.plugin_id,
  23. tool_provider=self.entity.identity.provider,
  24. tool_name=self.entity.identity.name,
  25. credentials=self.runtime.credentials,
  26. tool_parameters=tool_parameters,
  27. )
  28. def fork_tool_runtime(self, runtime: ToolRuntime) -> "PluginTool":
  29. return PluginTool(
  30. entity=self.entity,
  31. runtime=runtime,
  32. tenant_id=self.tenant_id,
  33. plugin_id=self.plugin_id,
  34. )