mirror of
https://github.com/langgenius/dify.git
synced 2026-01-23 12:12:02 +08:00
feat: extend construct_environment method to accept environments parameter in virtual environment classes
This commit is contained in:
parent
0a97e87a8e
commit
f679065d2c
@ -12,16 +12,16 @@ class VirtualEnvironment(ABC):
|
||||
Base class for virtual environment implementations.
|
||||
"""
|
||||
|
||||
def __init__(self, options: Mapping[str, Any]) -> None:
|
||||
def __init__(self, options: Mapping[str, Any], environments: Mapping[str, Any] | None = None) -> None:
|
||||
"""
|
||||
Initialize the virtual environment with metadata.
|
||||
"""
|
||||
|
||||
self.options = options
|
||||
self.metadata = self.construct_environment(options)
|
||||
self.metadata = self.construct_environment(options, environments or {})
|
||||
|
||||
@abstractmethod
|
||||
def construct_environment(self, options: Mapping[str, Any]) -> Metadata:
|
||||
def construct_environment(self, options: Mapping[str, Any], environments: Mapping[str, Any]) -> Metadata:
|
||||
"""
|
||||
Construct the unique identifier for the virtual environment.
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ environment.release_environment()
|
||||
class DockerDaemonEnvironment(VirtualEnvironment):
|
||||
_WORKING_DIR = "/workspace"
|
||||
|
||||
def construct_environment(self, options: Mapping[str, Any]) -> Metadata:
|
||||
def construct_environment(self, options: Mapping[str, Any], environments: Mapping[str, Any]) -> Metadata:
|
||||
"""
|
||||
Construct the Docker daemon virtual environment.
|
||||
"""
|
||||
@ -68,6 +68,7 @@ class DockerDaemonEnvironment(VirtualEnvironment):
|
||||
remove=True,
|
||||
stdin_open=True,
|
||||
working_dir=self._WORKING_DIR,
|
||||
environment=dict(environments),
|
||||
)
|
||||
|
||||
# wait for the container to be fully started
|
||||
@ -223,7 +224,7 @@ class DockerDaemonEnvironment(VirtualEnvironment):
|
||||
return
|
||||
|
||||
def execute_command(
|
||||
self, connection_handle: ConnectionHandle, command: list[str]
|
||||
self, connection_handle: ConnectionHandle, command: list[str], environments: Mapping[str, str] | None = None
|
||||
) -> tuple[str, Transport, Transport, Transport]:
|
||||
container = self._get_container()
|
||||
container_id = container.id
|
||||
@ -240,6 +241,7 @@ class DockerDaemonEnvironment(VirtualEnvironment):
|
||||
stderr=True,
|
||||
tty=False,
|
||||
workdir=self._working_dir,
|
||||
environment=environments,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@ class LocalVirtualEnvironment(VirtualEnvironment):
|
||||
NEVER USE IT IN PRODUCTION ENVIRONMENTS.
|
||||
"""
|
||||
|
||||
def construct_environment(self, options: Mapping[str, Any]) -> Metadata:
|
||||
def construct_environment(self, options: Mapping[str, Any], environments: Mapping[str, Any]) -> Metadata:
|
||||
"""
|
||||
Construct the local virtual environment.
|
||||
|
||||
@ -117,7 +117,7 @@ class LocalVirtualEnvironment(VirtualEnvironment):
|
||||
pass
|
||||
|
||||
def execute_command(
|
||||
self, connection_handle: ConnectionHandle, command: list[str]
|
||||
self, connection_handle: ConnectionHandle, command: list[str], environments: Mapping[str, str] | None = None
|
||||
) -> tuple[str, Transport, Transport, Transport]:
|
||||
"""
|
||||
Execute a command in the local virtual environment.
|
||||
@ -138,6 +138,7 @@ class LocalVirtualEnvironment(VirtualEnvironment):
|
||||
stderr=stderr_write_fd,
|
||||
cwd=working_path,
|
||||
close_fds=True,
|
||||
env=environments,
|
||||
)
|
||||
except Exception:
|
||||
# Clean up file descriptors if process creation fails
|
||||
|
||||
Loading…
Reference in New Issue
Block a user