|
5 | 5 | from typing import Any, Optional
|
6 | 6 |
|
7 | 7 | from pydantic import RootModel
|
| 8 | +from yarl import URL |
8 | 9 |
|
9 | 10 | from dify_plugin.config.config import DifyPluginEnv, InstallMethod
|
10 | 11 | from dify_plugin.config.logger_format import plugin_logger_handler
|
@@ -81,9 +82,12 @@ def _launch_remote_stream(self, config: DifyPluginEnv) -> tuple[RequestReader, O
|
81 | 82 | if not config.REMOTE_INSTALL_KEY:
|
82 | 83 | raise ValueError("Missing remote install key")
|
83 | 84 |
|
| 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 | + |
84 | 88 | tcp_stream = TCPReaderWriter(
|
85 |
| - config.REMOTE_INSTALL_HOST, |
86 |
| - config.REMOTE_INSTALL_PORT, |
| 89 | + install_host, |
| 90 | + install_port, |
87 | 91 | config.REMOTE_INSTALL_KEY,
|
88 | 92 | on_connected=lambda: self._initialize_tcp_stream(tcp_stream),
|
89 | 93 | )
|
@@ -385,3 +389,33 @@ def _execute_request(
|
385 | 389 | session_id=session_id,
|
386 | 390 | data=writer.stream_object(data=response),
|
387 | 391 | )
|
| 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