mirror of
https://github.com/langgenius/dify.git
synced 2026-03-17 04:47:50 +08:00
refactor(sandbox): update imports and remove unused bash tool files, adjust DIFY_CLI_CONFIG_PATH
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
from core.sandbox.bash_tool import SandboxBashTool
|
||||
from core.sandbox.constants import (
|
||||
DIFY_CLI_CONFIG_PATH,
|
||||
DIFY_CLI_PATH,
|
||||
@ -25,6 +26,7 @@ __all__ = [
|
||||
"DifyCliInitializer",
|
||||
"DifyCliLocator",
|
||||
"DifyCliToolConfig",
|
||||
"SandboxBashTool",
|
||||
"SandboxInitializer",
|
||||
"SandboxSession",
|
||||
]
|
||||
|
||||
@ -6,4 +6,4 @@ DIFY_CLI_PATH: Final[str] = "/work/.dify/bin/dify"
|
||||
|
||||
DIFY_CLI_PATH_PATTERN: Final[str] = "dify-cli-{os}-{arch}"
|
||||
|
||||
DIFY_CLI_CONFIG_PATH: Final[str] = "/work/config.json"
|
||||
DIFY_CLI_CONFIG_PATH: Final[str] = "/work/.dify_cli.json"
|
||||
|
||||
48
api/core/sandbox/encryption.py
Normal file
48
api/core/sandbox/encryption.py
Normal file
@ -0,0 +1,48 @@
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from core.entities.provider_entities import BasicProviderConfig
|
||||
from core.helper.provider_cache import ProviderCredentialsCache
|
||||
from core.helper.provider_encryption import ProviderConfigCache, ProviderConfigEncrypter, create_provider_encrypter
|
||||
|
||||
|
||||
class SandboxProviderConfigCache(ProviderCredentialsCache):
|
||||
def __init__(self, tenant_id: str, provider_type: str):
|
||||
super().__init__(tenant_id=tenant_id, provider_type=provider_type)
|
||||
|
||||
def _generate_cache_key(self, **kwargs) -> str:
|
||||
tenant_id = kwargs["tenant_id"]
|
||||
provider_type = kwargs["provider_type"]
|
||||
return f"sandbox_config:tenant_id:{tenant_id}:provider_type:{provider_type}"
|
||||
|
||||
|
||||
def create_sandbox_config_encrypter(
|
||||
tenant_id: str,
|
||||
config_schema: list[BasicProviderConfig],
|
||||
provider_type: str,
|
||||
) -> tuple[ProviderConfigEncrypter, ProviderConfigCache]:
|
||||
cache = SandboxProviderConfigCache(tenant_id=tenant_id, provider_type=provider_type)
|
||||
return create_provider_encrypter(tenant_id=tenant_id, config=config_schema, cache=cache)
|
||||
|
||||
|
||||
def masked_config(
|
||||
schemas: list[BasicProviderConfig],
|
||||
config: Mapping[str, Any],
|
||||
) -> Mapping[str, Any]:
|
||||
masked = dict(config)
|
||||
configs = {x.name: x for x in schemas}
|
||||
for key, value in config.items():
|
||||
schema = configs.get(key)
|
||||
if not schema:
|
||||
masked[key] = value
|
||||
continue
|
||||
if schema.type == BasicProviderConfig.Type.SECRET_INPUT:
|
||||
if not isinstance(value, str):
|
||||
continue
|
||||
if len(value) <= 4:
|
||||
masked[key] = "*" * len(value)
|
||||
else:
|
||||
masked[key] = value[:2] + "*" * (len(value) - 4) + value[-2:]
|
||||
else:
|
||||
masked[key] = value
|
||||
return masked
|
||||
@ -2,20 +2,17 @@ import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from core.sandbox.constants import DIFY_CLI_PATH
|
||||
from core.sandbox.dify_cli import DifyCliLocator
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.virtual_environment.__base.virtual_environment import VirtualEnvironment
|
||||
from core.virtual_environment.__base.virtual_environment import VirtualEnvironment
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SandboxInitializer(ABC):
|
||||
@abstractmethod
|
||||
def initialize(self, env: "VirtualEnvironment") -> None:
|
||||
def initialize(self, env: VirtualEnvironment) -> None:
|
||||
...
|
||||
|
||||
|
||||
@ -23,7 +20,7 @@ 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:
|
||||
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()))
|
||||
|
||||
|
||||
@ -5,12 +5,12 @@ import logging
|
||||
from io import BytesIO
|
||||
from types import TracebackType
|
||||
|
||||
from core.sandbox.bash_tool import SandboxBashTool
|
||||
from core.sandbox.constants import DIFY_CLI_CONFIG_PATH, DIFY_CLI_PATH
|
||||
from core.sandbox.dify_cli import DifyCliConfig
|
||||
from core.sandbox.manager import SandboxManager
|
||||
from core.session.inner_api import InnerApiSessionManager
|
||||
from core.tools.__base.tool import Tool
|
||||
from core.tools.builtin_tool.providers.sandbox.bash_tool import SandboxBashTool
|
||||
from core.virtual_environment.__base.virtual_environment import VirtualEnvironment
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
from core.tools.builtin_tool.providers.sandbox.bash_tool import SandboxBashTool
|
||||
|
||||
__all__ = ["SandboxBashTool"]
|
||||
@ -357,7 +357,7 @@ class LLMNodeData(BaseNodeData):
|
||||
|
||||
# Tool support
|
||||
tools: Sequence[ToolMetadata] = Field(default_factory=list)
|
||||
max_iterations: int | None = Field(default=None, description="Maximum number of iterations for the LLM node")
|
||||
max_iterations: int | None = Field(default=100, description="Maximum number of iterations for the LLM node")
|
||||
|
||||
@field_validator("prompt_config", mode="before")
|
||||
@classmethod
|
||||
|
||||
@ -1609,7 +1609,7 @@ class LLMNode(Node[LLMNodeData]):
|
||||
model_instance=model_instance,
|
||||
tools=[sandbox_session.bash_tool],
|
||||
files=prompt_files,
|
||||
max_iterations=self._node_data.max_iterations or 10,
|
||||
max_iterations=self._node_data.max_iterations or 100,
|
||||
context=ExecutionContext(user_id=self.user_id, app_id=self.app_id, tenant_id=self.tenant_id),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user