Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion python/dify_plugin/core/entities/plugin/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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,
)
4 changes: 2 additions & 2 deletions python/dify_plugin/core/plugin_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions python/dify_plugin/core/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions python/dify_plugin/core/server/io_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -101,6 +104,7 @@ def _execute_request_in_thread(
app_id,
endpoint_id,
context,
passthrough,
)
except Exception as e:
args = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
1 change: 1 addition & 0 deletions python/dify_plugin/core/server/stdio/request_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
1 change: 1 addition & 0 deletions python/dify_plugin/core/server/tcp/request_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
8 changes: 6 additions & 2 deletions python/dify_plugin/interfaces/tool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +366 to +367
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The broad except TypeError is a bit risky as it could catch any TypeError from within the _invoke implementation, not just the one for an unexpected keyword argument. This could hide bugs in the tool's code.

To make this safer, you can inspect the TypeError to ensure it's the specific one you want to handle for backward compatibility, and re-raise any others.

Suggested change
except TypeError:
return self._invoke(tool_parameters)
except TypeError as e:
if "unexpected keyword argument" in str(e):
return self._invoke(tool_parameters)
raise


@deprecated("This feature is deprecated, will soon be replaced by dynamic select parameter")
def get_runtime_parameters(self) -> list[ToolParameter]:
Expand Down
2 changes: 2 additions & 0 deletions python/dify_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down