You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from collections.abc import Generator
  2. from typing import Any, Optional
  3. from core.callback_handler.workflow_tool_callback_handler import DifyWorkflowCallbackHandler
  4. from core.plugin.backwards_invocation.base import BaseBackwardsInvocation
  5. from core.tools.entities.tool_entities import ToolInvokeMessage, ToolProviderType
  6. from core.tools.tool_engine import ToolEngine
  7. from core.tools.tool_manager import ToolManager
  8. from core.tools.utils.message_transformer import ToolFileMessageTransformer
  9. class PluginToolBackwardsInvocation(BaseBackwardsInvocation):
  10. """
  11. Backwards invocation for plugin tools.
  12. """
  13. @classmethod
  14. def invoke_tool(
  15. cls,
  16. tenant_id: str,
  17. user_id: str,
  18. tool_type: ToolProviderType,
  19. provider: str,
  20. tool_name: str,
  21. tool_parameters: dict[str, Any],
  22. credential_id: Optional[str] = None,
  23. ) -> Generator[ToolInvokeMessage, None, None]:
  24. """
  25. invoke tool
  26. """
  27. # get tool runtime
  28. try:
  29. tool_runtime = ToolManager.get_tool_runtime_from_plugin(
  30. tool_type, tenant_id, provider, tool_name, tool_parameters, credential_id
  31. )
  32. response = ToolEngine.generic_invoke(
  33. tool_runtime, tool_parameters, user_id, DifyWorkflowCallbackHandler(), workflow_call_depth=1
  34. )
  35. response = ToolFileMessageTransformer.transform_tool_invoke_messages(
  36. response, user_id=user_id, tenant_id=tenant_id
  37. )
  38. return response
  39. except Exception as e:
  40. raise e