mirror of
https://github.com/langgenius/dify.git
synced 2026-05-13 08:57:28 +08:00
- Add helpers.py with connection management utilities:
- with_connection: context manager for connection lifecycle
- submit_command: execute command and return CommandFuture
- execute: run command with auto connection, raise on failure
- try_execute: run command with auto connection, return result
- Add CommandExecutionError to exec.py for typed error handling
with access to exit_code, stderr, and full result
- Remove run_command method from VirtualEnvironment base class
(now available as submit_command helper)
- Update all call sites to use new helper functions:
- sandbox/session.py
- sandbox/storage/archive_storage.py
- sandbox/bash/bash_tool.py
- workflow/nodes/command/node.py
- Add comprehensive unit tests for helpers with connection reuse
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
import logging
|
|
from abc import ABC, abstractmethod
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
|
|
from core.sandbox.bash.dify_cli import DifyCliLocator
|
|
from core.sandbox.constants import DIFY_CLI_PATH
|
|
from core.virtual_environment.__base.helpers import execute
|
|
from core.virtual_environment.__base.virtual_environment import VirtualEnvironment
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SandboxInitializer(ABC):
|
|
@abstractmethod
|
|
def initialize(self, env: VirtualEnvironment) -> None: ...
|
|
|
|
|
|
class DifyCliInitializer(SandboxInitializer):
|
|
def __init__(self, cli_root: str | Path | None = None) -> None:
|
|
self._locator = DifyCliLocator(root=cli_root)
|
|
|
|
def initialize(self, env: VirtualEnvironment) -> None:
|
|
binary = self._locator.resolve(env.metadata.os, env.metadata.arch)
|
|
env.upload_file(DIFY_CLI_PATH, BytesIO(binary.path.read_bytes()))
|
|
|
|
execute(
|
|
env,
|
|
["chmod", "+x", DIFY_CLI_PATH],
|
|
timeout=10,
|
|
error_message="Failed to mark dify CLI as executable",
|
|
)
|
|
logger.info("Dify CLI uploaded to sandbox, path=%s", DIFY_CLI_PATH)
|