Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.0 KB
Raw Blame
"""Client-safe DTOs for the Dify shell layer.

Server-only Agent Stub and redaction settings are injected by the runtime
runtime services. The Sandbox dependency supplies the active shellctl data
plane. Public config carries product-level Agent Soul settings that affect the
workspace itself: CLI tool bootstrap commands, normal environment variables,
secret environment variable names. Sandbox selection is a deployment concern.
"""

import re
from typing import ClassVar

from pydantic import BaseModel, ConfigDict, Field, field_validator


_ENV_NAME_PATTERN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")


class DifyShellEnvVarConfig(BaseModel):
    """One shell environment variable exported for every sandbox command."""

    model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")

    name: str = Field(min_length=1, max_length=255)
    value: str = ""

    @field_validator("name")
    @classmethod
    def _validate_name(cls, value: str) -> str:
        if not _ENV_NAME_PATTERN.fullmatch(value):
            raise ValueError("env var name must be a valid shell identifier")
        return value


class DifyShellSecretRefConfig(BaseModel):
    """Name of a secret env var expected to be supplied by the sandbox host."""

    model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")

    name: str = Field(min_length=1, max_length=255)
    ref: str | None = Field(default=None, max_length=255)

    @field_validator("name")
    @classmethod
    def _validate_name(cls, value: str) -> str:
        if not _ENV_NAME_PATTERN.fullmatch(value):
            raise ValueError("secret env var name must be a valid shell identifier")
        return value


class DifyShellCliToolConfig(BaseModel):
    """One CLI tool declaration that can bootstrap itself in the sandbox."""

    model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")

    name: str | None = Field(default=None, max_length=255)
    install_commands: list[str] = Field(default_factory=list)
    env: list[DifyShellEnvVarConfig] = Field(default_factory=list)
    secret_refs: list[DifyShellSecretRefConfig] = Field(default_factory=list)

    @field_validator("install_commands")
    @classmethod
    def _reject_blank_install_commands(cls, value: list[str]) -> list[str]:
        return [command for command in (item.strip() for item in value) if command]


class DifyShellLayerConfig(BaseModel):
    """Public product behavior for the Sandbox-backed Dify Shell layer."""

    runtime: str = "runtime"
    execution_context: str | None = "execution_context"

    model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")

    cli_tools: list[DifyShellCliToolConfig] = Field(default_factory=list)
    env: list[DifyShellEnvVarConfig] = Field(default_factory=list)
    secret_refs: list[DifyShellSecretRefConfig] = Field(default_factory=list)
    redact_patterns: list[str] = Field(default_factory=list)


__all__ = [
    "DifyShellCliToolConfig",
    "DifyShellEnvVarConfig",
    "DifyShellLayerConfig",
    "DifyShellSecretRefConfig",
]