FlowGram Runtime Source Code Guide
This document aims to help developers gain a deep understanding of FlowGram Runtime's source code structure and implementation details, providing guidance for customization and extension. Since FlowGram Runtime is positioned as a reference implementation rather than an SDK for direct use, understanding its internal implementation is particularly important for developers.
Project Structure Overview
Directory Structure
The FlowGram Runtime JS project has the following directory structure:
Module Organization
FlowGram Runtime JS employs a modular design, primarily divided into three core modules:
- interface: Defines the system's interfaces and data structures, serving as the foundation for other modules
- js-core: Implements the core functionality of the workflow engine, including workflow parsing, node execution, state management, etc.
- nodejs: Provides an HTTP API service based on NodeJS, allowing the workflow engine to be called via HTTP interfaces
Dependencies
The dependencies between modules are as follows:
- interface is the foundational module with no dependencies on other modules
- js-core depends on interfaces defined in the interface module
- nodejs depends on functionality provided by the js-core module, while also using interface definitions from the interface module
Key external dependencies include:
- TypeScript: Provides type safety and object-oriented programming support
- LangChain: Used for integrating large language models
- OpenAI API: Provides the default implementation for LLM nodes
- fastify: Used to implement HTTP API services
- tRPC: Used for type-safe API definitions and calls
Core Module Analysis
js-core Module
The js-core module is the core of FlowGram Runtime, implementing the main functionality of the workflow engine. This module adopts a Domain-Driven Design (DDD) architecture, divided into application, domain, and infrastructure layers.
Application Layer
The application layer is responsible for coordinating domain objects and implementing system use cases. Key files:
application/workflow.ts: Workflow application service, implementing workflow validation, execution, cancellation, querying, etc.application/api.ts: API implementation, including TaskValidate, TaskRun, TaskResult, TaskReport, TaskCancel, etc.
Domain Layer
The domain layer contains core business logic and domain models. Key directories and files:
domain/engine/: Workflow execution engine, responsible for workflow parsing and executionengine.ts: Workflow engine implementation, containing core logic for node execution, state management, etc.validator.ts: Workflow validator, checking the validity of workflow definitions
domain/document/: Workflow document model, representing the structure of workflowsworkflow.ts: Workflow definition modelnode.ts: Node definition modeledge.ts: Edge definition model
domain/executor/: Node executors, responsible for executing specific node logicexecutor.ts: Node executor base class and factory
domain/variable/: Variable management, handling variable storage and references in workflowsmanager.ts: Variable manager, responsible for variable storage, retrieval, and parsingstore.ts: Variable storage, providing variable persistence
domain/status/: Status management, tracking the execution status of workflows and nodescenter.ts: Status center, managing workflow and node statuses
domain/snapshot/: Snapshot management, recording intermediate states of workflow executioncenter.ts: Snapshot center, managing node execution snapshots
domain/report/: Report generation, collecting detailed information on workflow executioncenter.ts: Report center, generating workflow execution reports
Infrastructure Layer
The infrastructure layer provides technical support, including logging, events, containers, etc. Key files:
infrastructure/logger.ts: Logging service, providing logging functionalityinfrastructure/event.ts: Event service, providing event publishing and subscription functionalityinfrastructure/container.ts: Dependency injection container, managing object creation and lifecycleinfrastructure/error.ts: Error handling, defining error types and handling methods in the system
Node Executors
The nodes directory contains executor implementations for various node types. Key files:
nodes/start.ts: Start node executornodes/end.ts: End node executornodes/llm.ts: LLM node executor, integrating large language modelsnodes/condition.ts: Condition node executor, implementing conditional branchingnodes/loop.ts: Loop node executor, implementing loop logic
interface Module
The interface module defines the system's interfaces and data structures, serving as the foundation for other modules. Key directories and files:
api/: API interface definitionsapi.ts: Defines the API interfaces provided by the systemtypes.ts: API-related data type definitions
domain/: Domain model interface definitionsdocument.ts: Workflow document-related interfacesengine.ts: Workflow engine-related interfacesexecutor.ts: Node executor-related interfacesvariable.ts: Variable management-related interfacesstatus.ts: Status management-related interfacessnapshot.ts: Snapshot management-related interfacesreport.ts: Report generation-related interfaces
engine/: Engine interface definitionstypes.ts: Engine-related data type definitions
node/: Node interface definitionstypes.ts: Node-related data type definitions
nodejs Module
The nodejs module provides an HTTP API service based on NodeJS, allowing the workflow engine to be called via HTTP interfaces. Key directories and files:
api/: HTTP API implementationrouter.ts: API route definitionshandlers.ts: API handler functions
server/: Server implementationserver.ts: HTTP server implementationconfig.ts: Server configuration
Key Implementation Details
Workflow Engine
The workflow engine is the core of FlowGram Runtime, responsible for workflow parsing and execution. Its main implementation is located in js-core/src/domain/engine/engine.ts.
The main functions of the workflow engine include:
- Workflow Parsing: Converting workflow definitions into internal models
- Node Scheduling: Determining the execution order of nodes based on edges defined in the workflow
- Node Execution: Calling node executors to execute node logic
- State Management: Tracking the execution status of workflows and nodes
- Variable Management: Handling data transfer between nodes
- Error Handling: Managing exceptions during execution
Key code snippet:
Node Executors
Node executors are responsible for executing the specific logic of nodes. Each node type has a corresponding executor implementation located in the js-core/src/nodes/ directory.
The basic interface for node executors is defined in interface/src/domain/executor.ts:
Taking the LLM node executor as an example, its implementation is in js-core/src/nodes/llm.ts:
Variable Management
Variable management is an important part of workflow execution, responsible for handling data transfer between nodes. Its main implementation is in the js-core/src/domain/variable/ directory.
The core of variable management is the variable manager and variable storage:
- Variable Manager: Responsible for parsing, getting, and setting variables
- Variable Storage: Provides persistent storage for variables
Key code snippet:
State Storage
State storage is responsible for managing the execution state of workflows and nodes. Its main implementation is in the js-core/src/domain/status/ and js-core/src/domain/snapshot/ directories.
The core components of state management include:
- Status Center: Manages the status of workflows and nodes
- Snapshot Center: Records snapshots of node execution
- Report Center: Generates workflow execution reports
Key code snippet:
Design Patterns and Architectural Decisions
Domain-Driven Design
FlowGram Runtime adopts a Domain-Driven Design (DDD) architecture, dividing the system into application, domain, and infrastructure layers. This architecture helps separate concerns, making the code more modular and maintainable.
Key domain concepts include:
- Workflow: Represents a complete workflow definition
- Node: Basic execution unit in a workflow
- Edge: Line connecting nodes, representing execution flow
- Execution Context: Environment for workflow execution
- Variable: Data in the workflow execution process
Factory Pattern
FlowGram Runtime uses the Factory pattern to create node executors, enabling the system to dynamically create corresponding executors based on node types.
Strategy Pattern
FlowGram Runtime uses the Strategy pattern to handle execution logic for different types of nodes, with each node type having a corresponding execution strategy.
Observer Pattern
FlowGram Runtime uses the Observer pattern to implement the event system, allowing components to publish and subscribe to events.
Dependency Injection
FlowGram Runtime uses Dependency Injection to manage dependencies between components, making components more loosely coupled and testable.