The Workflow System manages the complete lifecycle of AI-powered workflows from creation through execution. Workflows are directed acyclic graphs (DAGs) where nodes (blocks) represent operations and edges define execution dependencies.
This page covers the workflow lifecycle:
workflow.tsx, real-time collaboration via useCollaborativeWorkflow, and state persistence through normalized tablesuseWorkflowStore for graph structure, useSubBlockStore for user inputs, and useOperationQueue for collaborative syncSerializer.serializeWorkflow()DAGBuilder, orchestration by ExecutionEngine, and block-level handlersworkflowDeploymentVersion table with version controlChild Pages (detailed documentation):
Sources: apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx, apps/sim/stores/workflows/workflow/store.ts apps/sim/lib/workflows/executor/execution-core.ts apps/sim/executor/execution/engine.ts
Workflows progress through five distinct phases, each with specific storage patterns and execution modes:
| Phase | Storage | Execution Mode | Key APIs |
|---|---|---|---|
| Draft | workflowBlocks, workflowEdges, workflowSubflows tables | Uses current draft state | PUT /api/workflows/:id/state |
| Testing | Same normalized tables | Manual execution from editor | POST /api/workflows/:id/execute?useDraftState=true |
| Deployed | workflowDeploymentVersion.stateSnapshot (immutable) | Production triggers use snapshot | POST /api/workflows/:id/deploy |
| Executing | Execution logs to workflowExecutionLogs | Real-time via SSE or async via Trigger.dev | POST /api/workflows/:id/execute |
| Paused | Execution state in workflowPauseTokens | Resume from snapshot | POST /api/workflows/:id/execute with resumeFromSnapshot |
Sources: apps/sim/app/api/workflows/[id]/execute/route.ts:178-536, apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts:287-406, apps/sim/lib/workflows/executor/execution-core.ts114-320
Sources: apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx:1-2145, apps/sim/app/api/workflows/[id]/deploy/route.ts, apps/sim/background/webhook-execution.ts95-380
The workflow system spans three major components: the visual canvas, state management layer, and execution engine.
Sources: apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx:226-1042, apps/sim/stores/workflows/workflow/store.ts108-567 apps/sim/hooks/use-collaborative-workflow.ts33-858 apps/sim/stores/operation-queue/store.ts50-406 apps/sim/lib/workflows/executor/execution-core.ts114-320 apps/sim/executor/execution/engine.ts20-179
Workflow creation and editing occurs in the ReactFlow-based canvas with real-time collaborative features.
| Component | File | Responsibility |
|---|---|---|
| WorkflowContent | workflow.tsx226 | Main canvas, ReactFlow provider, node/edge management |
| WorkflowBlock | workflow-block.tsx658 | Individual block rendering, SubBlockRow display |
| WorkflowEdge | workflow-edge.tsx | Edge rendering with conditional styling |
| SubflowNodeComponent | subflow-node.tsx | Loop/parallel container rendering |
Sources: apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx:226-1042, apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/workflow-block.tsx:658-1084
The useWorkflowStore provides atomic operations for graph manipulation:
Sources: apps/sim/stores/workflows/workflow/store.ts117-551
Multi-user editing synchronizes through a queue-based operation system:
Sources: apps/sim/hooks/use-collaborative-workflow.ts660-717 apps/sim/stores/operation-queue/store.ts55-154
The operation queue ensures FIFO ordering and handles network failures:
Retry policies:
Sources: apps/sim/stores/operation-queue/store.ts7-257
A workflow is a directed acyclic graph (DAG) composed of four structural elements that define the execution topology:
| Element | Purpose | TypeScript Type |
|---|---|---|
| Blocks | Individual operations (nodes in the graph) | BlockState |
| Edges | Directed connections defining execution order | Edge |
| Loops | Containers for iterative execution of child blocks | Loop |
| Parallels | Containers for concurrent execution of child blocks | Parallel |
Sources: apps/sim/stores/workflows/workflow/types.ts73-142
Sources: apps/sim/stores/workflows/workflow/types.ts apps/sim/executor/dag/builder.ts
Blocks are the fundamental execution units:
For detailed block configuration including subblocks and outputs, see Workflows & Blocks.
Sources: apps/sim/stores/workflows/workflow/types.ts73-87
Edges define execution flow and handle-to-handle connections:
The sourceHandle determines conditional routing:
source/success: Normal execution patherror: Error handling pathcondition: Conditional branching (condition block)Sources: apps/sim/executor/constants.ts181-194 apps/sim/executor/execution/edge-manager.ts14-48
Containers group child blocks for special execution patterns. See Loop & Parallel Execution for detailed semantics.
Sources: apps/sim/stores/workflows/workflow/types.ts125-142
Sim includes 150+ block types organized into three categories:
| Category | Purpose | Count | Example Blocks |
|---|---|---|---|
blocks | Core workflow operations | ~20 | agent, function, api, condition, router, evaluator |
tools | External service integrations | ~130 | slack, github, gmail, notion, airtable, hubspot |
triggers | Workflow invocation entry points | ~10 | start_trigger, webhook, schedule, rss, email_polling |
Sources: apps/sim/blocks/types.ts15 apps/sim/blocks/registry.ts
| Block Type | Purpose | Handler |
|---|---|---|
agent | LLM-based AI agent with tool calling | AgentHandler |
function | JavaScript/Python code execution | FunctionHandler |
api | HTTP API requests | ApiHandler |
condition | Conditional branching | ConditionHandler |
router | Multi-branch routing with descriptions | RouterHandler |
Sources: apps/sim/executor/constants.ts3-30 apps/sim/executor/handlers/
| Block Type | Purpose | Orchestrator |
|---|---|---|
loop | Iterative execution (for, forEach, while, doWhile) | LoopOrchestrator |
parallel | Concurrent execution of branches | ParallelOrchestrator |
workflow | Invoke another workflow | WorkflowBlockHandler |
Sources: apps/sim/executor/orchestrators/loop.ts apps/sim/executor/orchestrators/parallel.ts apps/sim/executor/handlers/workflow/workflow-handler.ts
| Block Type | Purpose | See |
|---|---|---|
start_trigger | Manual execution entry point | Workflow Triggers |
webhook | HTTP webhook endpoint | Workflow Triggers |
schedule | Cron-based scheduled execution | Workflow Triggers |
Sources: apps/sim/lib/workflows/triggers/triggers.ts
Each block type is defined by a BlockConfig object that specifies its UI, inputs, outputs, and execution behavior. See Workflows & Blocks for the complete schema.
Sources: apps/sim/blocks/types.ts312-345
The workflow execution engine transforms the visual graph into an executable DAG and orchestrates block execution based on dependencies.
Sources: apps/sim/serializer/index.ts apps/sim/executor/dag/builder.ts apps/sim/executor/execution/engine.ts apps/sim/executor/execution/executor.ts
Before execution, the UI graph undergoes transformation into an executable format.
The Serializer class converts BlockState objects into SerializedBlock format:
Key transformations:
subBlocks → inputs: User-entered values become execution inputstype → config.tool: Block type maps to handler registrydata.parentId → Container relationships preserved for loop/parallel orchestration<blockName.field> expressions validated and mappedSources: apps/sim/serializer/index.ts37-218 apps/sim/stores/workflows/workflow/types.ts76-90
Two loading paths exist based on execution context:
Sources: apps/sim/lib/workflows/persistence/utils.ts apps/sim/lib/workflows/executor/execution-core.ts160-183
The DAGBuilder constructs the directed acyclic graph:
The DAG includes:
DAGNode has incoming and outgoing edgesSources: apps/sim/executor/dag/builder.ts51-190
Sources: apps/sim/executor/dag/types.ts apps/sim/executor/dag/builder.ts
The ExecutionEngine implements a queue-based traversal of the DAG with dependency tracking.
Sources: apps/sim/executor/execution/engine.ts20-179
Sources: apps/sim/executor/execution/engine.ts103-247
The EdgeManager determines which downstream nodes become ready:
Sources: apps/sim/executor/execution/edge-manager.ts9-160
Independent nodes execute concurrently:
Concurrency benefits:
Sources: apps/sim/executor/execution/engine.ts249-343
Block execution is delegated to type-specific handlers:
| Handler | Block Types | File | Key Logic |
|---|---|---|---|
| AgentHandler | agent, router, evaluator | agent-handler.ts | LLM orchestration, tool calling, max 20 iterations |
| FunctionHandler | function | function-handler.ts | JavaScript/Python sandbox execution via isolated-vm |
| ApiHandler | api | api-handler.ts | HTTP requests with retry logic |
| ConditionHandler | condition | condition-handler.ts | JavaScript expression evaluation for branching |
| LoopOrchestrator | loop | loop.ts | Iterative execution: for, forEach, while, doWhile |
| ParallelOrchestrator | parallel | parallel.ts | Concurrent branch execution |
Handler selection:
Sources: apps/sim/executor/orchestrators/node.ts1-234 apps/sim/executor/handlers/
For detailed execution mechanics, see Workflow Execution Engine.
A workflow progresses through distinct phases from creation to production execution:
Sources: apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx, apps/sim/app/api/workflows/[id]/deploy/route.ts
Workflow state persists across sessions using a normalized database schema.
| Table | Purpose | Key Columns |
|---|---|---|
workflows | Workflow metadata | id, name, userId, workspaceId, isDeployed |
workflowBlocks | Block definitions | id, workflowId, type, name, position, subBlocks, outputs |
workflowEdges | Edge connections | id, workflowId, source, target, sourceHandle, targetHandle |
workflowSubflows | Loop/parallel configs | id, workflowId, type, config (JSONB: loopType, iterations, etc.) |
Persistence flow:
collaborativeBatchAddBlocks() → useOperationQueueoperationIdPUT /api/workflows/:id/stateemit('operation-confirmed', {operationId})Sources: apps/sim/app/api/workflows/[id]/state/route.ts, apps/sim/stores/operation-queue/store.ts274-339
User-entered values are stored separately from block structure:
Merge pattern for execution:
Sources: apps/sim/stores/workflows/subblock/store.ts apps/sim/lib/workflows/subblocks.ts
Manual execution from the editor uses draft state and streams results in real-time.
Sources: apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts:287-654, apps/sim/app/api/workflows/[id]/execute/route.ts:178-697, apps/sim/lib/workflows/executor/execution-core.ts114-403
Real-time execution updates stream to the client:
Client handling:
Sources: apps/sim/lib/workflows/executor/execution-events.ts apps/sim/app/api/workflows/[id]/execute/route.ts:541-697
Production workflows execute from immutable snapshots stored in the workflowDeploymentVersion table.
Snapshot deduplication benefits:
Sources: apps/sim/app/api/workflows/[id]/deploy/route.ts, apps/sim/lib/workflows/persistence/utils.ts
Deployment activates configured triggers:
| Trigger Type | Activation | Invocation Path |
|---|---|---|
| Webhook | Create/update record in webhook table with path | POST /api/webhooks/trigger/:path → Trigger.dev → webhook-execution task |
| Schedule | Create/update record in workflowSchedule table | Cron job polls table → Trigger.dev → schedule-execution task |
| API | Deployment marks workflow as isDeployed=true | Direct API call → POST /api/workflows/:id/execute |
Background execution via Trigger.dev:
Sources: apps/sim/background/webhook-execution.ts95-380 apps/sim/background/schedule-execution.ts apps/sim/app/api/webhooks/trigger/[path]/route.ts:37-176
Core endpoints for workflow lifecycle management:
| Endpoint | Method | Purpose | Key Parameters |
|---|---|---|---|
POST /api/workflows | POST | Create new workflow | name, workspaceId |
GET /api/workflows/:id | GET | Load workflow metadata and state from normalized tables | - |
PUT /api/workflows/:id/state | PUT | Save draft state (upserts to workflowBlocks, workflowEdges, workflowSubflows) | blocks, edges, loops, parallels |
POST /api/workflows/:id/execute | POST | Execute workflow | useDraftState, input, stream, triggerType |
POST /api/workflows/:id/deploy | POST | Create immutable deployment snapshot | - |
GET /api/workflows/:id/deployments | GET | List deployment versions | - |
POST /api/workflows/:id/revert | POST | Revert to previous deployment | deploymentVersionId |
DELETE /api/workflows/:id | DELETE | Delete workflow and cleanup triggers | - |
Execution modes:
Sources: apps/sim/app/api/workflows/[id]/route.ts, apps/sim/app/api/workflows/[id]/state/route.ts, apps/sim/app/api/workflows/[id]/execute/route.ts:178-536, apps/sim/app/api/workflows/[id]/deploy/route.ts
The Core Workflow System provides:
This foundation enables all higher-level workflow operations including execution, debugging, deployment, and collaboration.
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.