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.

2.6 KB
Raw Blame
"""Stateless application facade for deployment-owned Home Snapshots."""

from dataclasses import dataclass

from dify_agent.protocol.home_snapshot import (
    CreateHomeSnapshotFromBindingRequest,
    DeleteHomeSnapshotRequest,
    HomeSnapshotResponse,
)
from dify_agent.runtime_backend import (
    BindingAcquireError,
    BindingLostError,
    ExecutionBindingBackend,
    HomeSnapshotBackend,
    HomeSnapshotCreateError,
    HomeSnapshotCreateSpec,
    HomeSnapshotNotFoundError,
    HomeSnapshotTooLargeError,
)
from dify_agent.runtime_backend.leases import open_runtime_lease


class HomeSnapshotServiceError(RuntimeError):
    code: str
    message: str
    status_code: int

    def __init__(self, code: str, message: str, *, status_code: int) -> None:
        super().__init__(message)
        self.code = code
        self.message = message
        self.status_code = status_code


@dataclass(slots=True)
class HomeSnapshotService:
    home_snapshots: HomeSnapshotBackend
    execution_bindings: ExecutionBindingBackend

    async def create_from_binding(
        self,
        request: CreateHomeSnapshotFromBindingRequest,
    ) -> HomeSnapshotResponse:
        try:
            async with open_runtime_lease(self.execution_bindings, request.backend_binding_ref) as lease:
                snapshot_ref = await self.home_snapshots.create_from_runtime(
                    spec=HomeSnapshotCreateSpec(
                        tenant_id=request.tenant_id,
                        agent_id=request.agent_id,
                        home_snapshot_id=request.home_snapshot_id,
                    ),
                    source=lease,
                )
        except BindingLostError as exc:
            raise HomeSnapshotServiceError("binding_lost", str(exc), status_code=404) from exc
        except HomeSnapshotTooLargeError as exc:
            raise HomeSnapshotServiceError("home_snapshot_too_large", str(exc), status_code=413) from exc
        except (BindingAcquireError, HomeSnapshotCreateError) as exc:
            raise HomeSnapshotServiceError("home_snapshot_create_failed", str(exc), status_code=502) from exc
        return HomeSnapshotResponse(snapshot_ref=snapshot_ref)

    async def delete(self, request: DeleteHomeSnapshotRequest) -> None:
        try:
            await self.home_snapshots.delete(request.snapshot_ref)
        except HomeSnapshotNotFoundError:
            return
        except RuntimeError as exc:
            raise HomeSnapshotServiceError("home_snapshot_delete_failed", str(exc), status_code=502) from exc


__all__ = ["HomeSnapshotService", "HomeSnapshotServiceError"]