dify/api/dify_graph
Novice a28f22e59d
fix: resolve import errors and test failures after segment 4 merge
- Update BaseNodeData import path to dify_graph.entities.base_node_data
- Change NodeType.COMMAND/FILE_UPLOAD to BuiltinNodeTypes constants
- Fix system_oauth_encryption -> system_encryption rename in commands
- Remove tests for deleted agent runner modules
- Fix Avatar: named import + string size API in collaboration files
- Add missing skill feature deps: @monaco-editor/react, react-arborist,
  @tanstack/react-virtual
- Fix frontend test mocks: add useUserProfile, useLeaderRestoreListener,
  next/navigation mock, and nodeOutputVars to expected payload

Made-with: Cursor
2026-03-23 13:59:09 +08:00
..
context style: auto-format after segment 3 import migrations 2026-03-23 10:37:02 +08:00
entities Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
file Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
graph fix: resolve import errors and test failures after segment 4 merge 2026-03-23 13:59:09 +08:00
graph_engine Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
graph_events Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
model_runtime Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
node_events Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
nodes fix: resolve import errors and test failures after segment 4 merge 2026-03-23 13:59:09 +08:00
repositories refactor(api): move workflow knowledge nodes and trigger nodes (#33445) 2026-03-15 15:24:59 +08:00
runtime Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
utils
variables fix: resolve import migrations and test failures after segment 3 merge 2026-03-23 10:31:11 +08:00
README.md refactor(api): move workflow knowledge nodes and trigger nodes (#33445) 2026-03-15 15:24:59 +08:00
__init__.py
constants.py
conversation_variable_updater.py
enums.py Merge commit 'fb41b215' into sandboxed-agent-rebase 2026-03-23 10:52:06 +08:00
errors.py
system_variable.py
variable_loader.py
workflow_type_encoder.py

README.md

Workflow

Project Overview

This is the workflow graph engine module of Dify, implementing a queue-based distributed workflow execution system. The engine handles agentic AI workflows with support for parallel execution, node iteration, conditional logic, and external command control.

Architecture

Core Components

The graph engine follows a layered architecture with strict dependency rules:

  1. Graph Engine (graph_engine/) - Orchestrates workflow execution

    • Manager - External control interface for stop/pause/resume commands
    • Worker - Node execution runtime
    • Command Processing - Handles control commands (abort, pause, resume)
    • Event Management - Event propagation and layer notifications
    • Graph Traversal - Edge processing and skip propagation
    • Response Coordinator - Path tracking and session management
    • Layers - Pluggable middleware (debug logging, execution limits)
    • Command Channels - Communication channels (InMemory, Redis)
  2. Graph (graph/) - Graph structure and runtime state

    • Graph Template - Workflow definition
    • Edge - Node connections with conditions
    • Runtime State Protocol - State management interface
  3. Nodes (nodes/) - Node implementations

    • Base - Abstract node classes and variable parsing
    • Specific Nodes - LLM, Agent, Code, HTTP Request, Iteration, Loop, etc.
  4. Events (node_events/) - Event system

    • Base - Event protocols
    • Node Events - Node lifecycle events
  5. Entities (entities/) - Domain models

    • Variable Pool - Variable storage
    • Graph Init Params - Initialization configuration

Key Design Patterns

Command Channel Pattern

External workflow control via Redis or in-memory channels:

# Send stop command to running workflow
channel = RedisChannel(redis_client, f"workflow:{task_id}:commands")
channel.send_command(AbortCommand(reason="User requested"))

Layer System

Extensible middleware for cross-cutting concerns:

engine = GraphEngine(graph)
engine.layer(DebugLoggingLayer(level="INFO"))
engine.layer(ExecutionLimitsLayer(max_nodes=100))

engine.layer() binds the read-only runtime state before execution, so layer hooks can assume graph_runtime_state is available.

Event-Driven Architecture

All node executions emit events for monitoring and integration:

  • NodeRunStartedEvent - Node execution begins
  • NodeRunSucceededEvent - Node completes successfully
  • NodeRunFailedEvent - Node encounters error
  • GraphRunStartedEvent/GraphRunCompletedEvent - Workflow lifecycle

Variable Pool

Centralized variable storage with namespace isolation:

# Variables scoped by node_id
pool.add(["node1", "output"], value)
result = pool.get(["node1", "output"])

Import Architecture Rules

The codebase enforces strict layering via import-linter:

  1. Workflow Layers (top to bottom):

    • graph_engine → graph_events → graph → nodes → node_events → entities
  2. Graph Engine Internal Layers:

    • orchestration → command_processing → event_management → graph_traversal → domain
  3. Domain Isolation:

    • Domain models cannot import from infrastructure layers
  4. Command Channel Independence:

    • InMemory and Redis channels must remain independent

Common Tasks

Adding a New Node Type

  1. Create node class in nodes/<node_type>/
  2. Inherit from BaseNode or appropriate base class
  3. Implement _run() method
  4. Ensure the node module is importable under nodes/<node_type>/
  5. Add tests in tests/unit_tests/dify_graph/nodes/

Implementing a Custom Layer

  1. Create class inheriting from Layer base
  2. Override lifecycle methods: on_graph_start(), on_event(), on_graph_end()
  3. Add to engine via engine.layer()

Debugging Workflow Execution

Enable debug logging layer:

debug_layer = DebugLoggingLayer(
    level="DEBUG",
    include_inputs=True,
    include_outputs=True
)