diff --git a/python/dify_plugin/core/entities/plugin/io.py b/python/dify_plugin/core/entities/plugin/io.py index 381f1fff..788b87e1 100644 --- a/python/dify_plugin/core/entities/plugin/io.py +++ b/python/dify_plugin/core/entities/plugin/io.py @@ -30,6 +30,7 @@ def __init__( app_id: str | None = None, endpoint_id: str | None = None, context: dict | None = None, + passthrough: str | None = None, ) -> None: self.session_id = session_id self.event = event @@ -39,6 +40,7 @@ def __init__( self.app_id = app_id self.endpoint_id = endpoint_id self.context = context + self.passthrough = passthrough class PluginInStream(PluginInStreamBase): @@ -54,7 +56,18 @@ def __init__( app_id: str | None = None, endpoint_id: str | None = None, context: dict | None = None, + passthrough: str | None = None, ): self.reader = reader self.writer = writer - super().__init__(session_id, event, data, conversation_id, message_id, app_id, endpoint_id, context) + super().__init__( + session_id, + event, + data, + conversation_id, + message_id, + app_id, + endpoint_id, + context, + passthrough, + ) diff --git a/python/dify_plugin/core/plugin_executor.py b/python/dify_plugin/core/plugin_executor.py index e3db9044..1a7e9ec5 100644 --- a/python/dify_plugin/core/plugin_executor.py +++ b/python/dify_plugin/core/plugin_executor.py @@ -89,8 +89,8 @@ def invoke_tool(self, session: Session, request: ToolInvokeRequest): session=session, ) - # invoke tool - yield from tool.invoke(request.tool_parameters) + # invoke tool with passthrough (if provided) + yield from tool.invoke(request.tool_parameters, passthrough=session.passthrough) def invoke_agent_strategy(self, session: Session, request: AgentInvokeRequest): agent_cls = self.registration.get_agent_strategy_cls(request.agent_strategy_provider, request.agent_strategy) diff --git a/python/dify_plugin/core/runtime.py b/python/dify_plugin/core/runtime.py index d2e558cc..c9248663 100644 --- a/python/dify_plugin/core/runtime.py +++ b/python/dify_plugin/core/runtime.py @@ -120,6 +120,7 @@ def __init__( app_id: str | None = None, endpoint_id: str | None = None, context: SessionContext | dict | None = None, + passthrough: str | None = None, max_invocation_timeout: int = 250, ) -> None: # current session id @@ -152,6 +153,9 @@ def __init__( SessionContext.model_validate(context) if isinstance(context, dict) else context or SessionContext() ) + # passthrough data from Dify + self.passthrough: str | None = passthrough + # dify plugin daemon url self.dify_plugin_daemon_url: str | None = dify_plugin_daemon_url diff --git a/python/dify_plugin/core/server/io_server.py b/python/dify_plugin/core/server/io_server.py index 55bd0c99..68060b4c 100644 --- a/python/dify_plugin/core/server/io_server.py +++ b/python/dify_plugin/core/server/io_server.py @@ -47,6 +47,7 @@ def _execute_request( app_id: str | None = None, endpoint_id: str | None = None, context: dict | None = None, + passthrough: str | None = None, ): """ accept requests and execute them, should be implemented outside @@ -72,6 +73,7 @@ def filter(data: PluginInStream) -> bool: # noqa: A001 data.app_id, data.endpoint_id, data.context, + data.passthrough, ) def _execute_request_in_thread( @@ -85,6 +87,7 @@ def _execute_request_in_thread( app_id: str | None = None, endpoint_id: str | None = None, context: dict | None = None, + passthrough: str | None = None, ): """ wrapper for _execute_request @@ -101,6 +104,7 @@ def _execute_request_in_thread( app_id, endpoint_id, context, + passthrough, ) except Exception as e: args = {} diff --git a/python/dify_plugin/core/server/serverless/request_reader.py b/python/dify_plugin/core/server/serverless/request_reader.py index 263ea026..09cc1fb1 100644 --- a/python/dify_plugin/core/server/serverless/request_reader.py +++ b/python/dify_plugin/core/server/serverless/request_reader.py @@ -59,6 +59,7 @@ def handler(self): endpoint_id=data.get("endpoint_id"), data=data["data"], context=data.get("context"), + passthrough=data.get("passthrough"), reader=self, writer=ServerlessResponseWriter(queue), ) diff --git a/python/dify_plugin/core/server/stdio/request_reader.py b/python/dify_plugin/core/server/stdio/request_reader.py index a915c0ab..5333fb17 100644 --- a/python/dify_plugin/core/server/stdio/request_reader.py +++ b/python/dify_plugin/core/server/stdio/request_reader.py @@ -68,6 +68,7 @@ def _read_stream(self) -> Generator[PluginInStream, None, None]: event=PluginInStreamEvent.value_of(data["event"]), data=data["data"], context=data.get("context"), + passthrough=data.get("passthrough"), reader=self, writer=StdioResponseWriter(), ) diff --git a/python/dify_plugin/core/server/tcp/request_reader.py b/python/dify_plugin/core/server/tcp/request_reader.py index 5f81df7a..5bedf773 100644 --- a/python/dify_plugin/core/server/tcp/request_reader.py +++ b/python/dify_plugin/core/server/tcp/request_reader.py @@ -199,6 +199,7 @@ def _read_stream(self) -> Generator[PluginInStream, None, None]: event=PluginInStreamEvent.value_of(data["event"]), data=data["data"], context=data.get("context"), + passthrough=data.get("passthrough"), reader=self, writer=self, ) diff --git a/python/dify_plugin/interfaces/tool/__init__.py b/python/dify_plugin/interfaces/tool/__init__.py index fac0fc22..01044595 100644 --- a/python/dify_plugin/interfaces/tool/__init__.py +++ b/python/dify_plugin/interfaces/tool/__init__.py @@ -357,10 +357,14 @@ def _fetch_parameter_options(self, parameter: str) -> list[ParameterOption]: # For executor use only # ############################################################ - def invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: + def invoke(self, tool_parameters: dict, passthrough: str | None = None) -> Generator[ToolInvokeMessage, None, None]: # convert parameters into correct types tool_parameters = self._convert_parameters(tool_parameters) - return self._invoke(tool_parameters) + # try to pass passthrough to implementations that support it, fallback otherwise + try: + return self._invoke(tool_parameters, passthrough=passthrough) # type: ignore[call-arg] + except TypeError: + return self._invoke(tool_parameters) @deprecated("This feature is deprecated, will soon be replaced by dynamic select parameter") def get_runtime_parameters(self) -> list[ToolParameter]: diff --git a/python/dify_plugin/plugin.py b/python/dify_plugin/plugin.py index d1590f4e..cc78ff89 100644 --- a/python/dify_plugin/plugin.py +++ b/python/dify_plugin/plugin.py @@ -385,6 +385,7 @@ def _execute_request( app_id: str | None = None, endpoint_id: str | None = None, context: dict | None = None, + passthrough: str | None = None, ): """ accept requests and execute @@ -404,6 +405,7 @@ def _execute_request( app_id=app_id, endpoint_id=endpoint_id, context=context, + passthrough=passthrough, max_invocation_timeout=self.config.MAX_INVOCATION_TIMEOUT, ) response = self.dispatch(session, data)