| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 | from collections.abc import Generatorfrom typing import Anyfrom core.callback_handler.workflow_tool_callback_handler import DifyWorkflowCallbackHandlerfrom core.plugin.backwards_invocation.base import BaseBackwardsInvocationfrom core.tools.entities.tool_entities import ToolInvokeMessage, ToolProviderTypefrom core.tools.tool_engine import ToolEnginefrom core.tools.tool_manager import ToolManagerfrom core.tools.utils.message_transformer import ToolFileMessageTransformerclass PluginToolBackwardsInvocation(BaseBackwardsInvocation):    """    Backwards invocation for plugin tools.    """    @classmethod    def invoke_tool(        cls,        tenant_id: str,        user_id: str,        tool_type: ToolProviderType,        provider: str,        tool_name: str,        tool_parameters: dict[str, Any],    ) -> Generator[ToolInvokeMessage, None, None]:        """        invoke tool        """        # get tool runtime        try:            tool_runtime = ToolManager.get_tool_runtime_from_plugin(                tool_type, tenant_id, provider, tool_name, tool_parameters            )            response = ToolEngine.generic_invoke(                tool_runtime, tool_parameters, user_id, DifyWorkflowCallbackHandler(), workflow_call_depth=1            )            response = ToolFileMessageTransformer.transform_tool_invoke_messages(                response, user_id=user_id, tenant_id=tenant_id            )            return response        except Exception as e:            raise e
 |