diff --git a/api/core/sandbox/constants.py b/api/core/sandbox/constants.py index 9c0e2dfe51..fb5634e333 100644 --- a/api/core/sandbox/constants.py +++ b/api/core/sandbox/constants.py @@ -1,9 +1,9 @@ from typing import Final -SANDBOX_WORK_DIR: Final[str] = "/work" +SANDBOX_WORK_DIR: Final[str] = "work" -DIFY_CLI_PATH: Final[str] = "/work/.dify/bin/dify" +DIFY_CLI_PATH: Final[str] = f"{SANDBOX_WORK_DIR}/.dify/bin/dify" DIFY_CLI_PATH_PATTERN: Final[str] = "dify-cli-{os}-{arch}" -DIFY_CLI_CONFIG_PATH: Final[str] = "/work/.dify_cli.json" +DIFY_CLI_CONFIG_PATH: Final[str] = f"{SANDBOX_WORK_DIR}/.dify_cli.json" diff --git a/api/core/sandbox/session.py b/api/core/sandbox/session.py index 53d0f0d376..f91b425724 100644 --- a/api/core/sandbox/session.py +++ b/api/core/sandbox/session.py @@ -52,9 +52,8 @@ class SandboxSession: try: future = sandbox.run_command(connection_handle, [DIFY_CLI_PATH, "init"]) result = future.result(timeout=30) - if result.exit_code not in (0, None): - stderr = result.stderr.decode("utf-8", errors="replace") if result.stderr else "" - raise RuntimeError(f"Failed to initialize Dify CLI in sandbox: {stderr}") + if result.is_error: + raise RuntimeError(f"Failed to initialize Dify CLI in sandbox: {result.error_message}") finally: sandbox.release_connection(connection_handle) diff --git a/api/core/virtual_environment/__base/entities.py b/api/core/virtual_environment/__base/entities.py index 6ba812e8d2..7d8617ec6e 100644 --- a/api/core/virtual_environment/__base/entities.py +++ b/api/core/virtual_environment/__base/entities.py @@ -77,3 +77,15 @@ class CommandResult(BaseModel): stderr: bytes = Field(description="Standard error content.") exit_code: int | None = Field(description="Exit code of the command. None if unavailable.") pid: str = Field(description="Process ID of the executed command.") + + @property + def is_error(self) -> bool: + return self.exit_code not in (None, 0) or bool(self.stderr.decode("utf-8", errors="replace")) + + @property + def error_message(self) -> str: + return self.stderr.decode("utf-8", errors="replace") if self.stderr else "" + + @property + def info_message(self) -> str: + return self.stdout.decode("utf-8", errors="replace") if self.stdout else ""