Skip to content

Commit 9353164

Browse files
feat: support REMOTE_INSTALL_URL config for remote installing and debugging (#136)
* support REMOTE_INSTALL_URL * nit * nit * passing remote_install_host and remote_install_port to TCPReaderWriter * fix the host and port parsing approach without yarl * extract method for parsing the host and port * nit * nit * nit * nit * improve _get_remote_install_host_and_port method * fix style * fix style
1 parent 08c596d commit 9353164

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

python/dify_plugin/config/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class DifyPluginEnv(BaseSettings):
2424
description="Installation method, local or network",
2525
)
2626

27+
REMOTE_INSTALL_URL: Optional[str] = Field(description="Remote installation URL")
2728
REMOTE_INSTALL_HOST: str = Field(default="localhost", description="Remote installation host")
2829
REMOTE_INSTALL_PORT: int = Field(default=5003, description="Remote installation port")
2930
REMOTE_INSTALL_KEY: Optional[str] = Field(default=None, description="Remote installation key")

python/dify_plugin/plugin.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, Optional
66

77
from pydantic import RootModel
8+
from yarl import URL
89

910
from dify_plugin.config.config import DifyPluginEnv, InstallMethod
1011
from dify_plugin.config.logger_format import plugin_logger_handler
@@ -81,9 +82,12 @@ def _launch_remote_stream(self, config: DifyPluginEnv) -> tuple[RequestReader, O
8182
if not config.REMOTE_INSTALL_KEY:
8283
raise ValueError("Missing remote install key")
8384

85+
install_host, install_port = self._get_remote_install_host_and_port(config)
86+
logging.debug(f"Remote installing to {install_host}:{install_port}")
87+
8488
tcp_stream = TCPReaderWriter(
85-
config.REMOTE_INSTALL_HOST,
86-
config.REMOTE_INSTALL_PORT,
89+
install_host,
90+
install_port,
8791
config.REMOTE_INSTALL_KEY,
8892
on_connected=lambda: self._initialize_tcp_stream(tcp_stream),
8993
)
@@ -385,3 +389,33 @@ def _execute_request(
385389
session_id=session_id,
386390
data=writer.stream_object(data=response),
387391
)
392+
393+
@staticmethod
394+
def _get_remote_install_host_and_port(config: DifyPluginEnv) -> tuple[str, int]:
395+
"""
396+
Get host and port for remote installation
397+
:param config: Dify plugin env config
398+
:return: host and port
399+
"""
400+
install_url = config.REMOTE_INSTALL_URL
401+
if install_url:
402+
if ":" in install_url:
403+
url = URL(install_url)
404+
if url.host and url.port:
405+
# for the url with protocol prefix
406+
host = url.host
407+
port = url.port
408+
else:
409+
# for "host:port" format
410+
split = install_url.split(":")
411+
host = split[0]
412+
port = int(split[1])
413+
else:
414+
raise ValueError(
415+
f'Invalid remote install URL {install_url}, which should be in the format of "host:port"'
416+
)
417+
else:
418+
host = config.REMOTE_INSTALL_HOST
419+
port = config.REMOTE_INSTALL_PORT
420+
421+
return host, port

0 commit comments

Comments
 (0)