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.

1.1 KB
Raw Blame
from collections.abc import Generator
from contextlib import AbstractContextManager, contextmanager
from typing import override

from context import capture_current_context
from graphon.engine.layer import Layer
from graphon.file.runtime import peek_workflow_file_runtime, use_workflow_file_runtime
from graphon.nodes.base.node import Node


class ExecutionContextLayer(Layer):
    """Restore the host's Flask and contextvars state for each worker activation."""

    def __init__(self) -> None:
        super().__init__()
        self._context = capture_current_context()

    @contextmanager
    def enter_context(self) -> Generator[None]:
        """Create a fresh manager for a node worker or child-frame construction."""
        file_runtime = peek_workflow_file_runtime()
        # Flask installs its app adapter; node execution uses the engine's adapter.
        with self._context, use_workflow_file_runtime(file_runtime):
            yield

    @override
    def node_run_context(self, node: Node, *, parent_execution_id: str | None = None) -> AbstractContextManager[None]:
        return self.enter_context()