---
description: Connecting nodes virtually and creating a smooth flow in Workbench
globs: steps/**/*.step.ts,steps/**/*.step.js,steps/**/*_step.py
alwaysApply: false
---

# Virtual Steps Guide

Virtual Steps are useful for creating a smooth flow in Workbench.

They offer two ways to connect nodes virtually:
- NOOP Steps: Used mostly when we want to override the workflow to add buttons or show UI elements.
- Virtual Connections between steps: Used when we want to connect virtually two steps, with 
or without NOOP Steps.

## Creating NOOP Steps

Steps need to be created in the `steps` folder, it can be in subfolders.

- Steps in TS and JS should end with `.step.ts` and `.step.js` respectively.
- Steps in Python should end with `_step.py`.

### Configuration

#### TypeScript Example

```typescript
import { NoopConfig } from 'motia'

export const config: NoopConfig = {
  /**
   * Should always be noop
   */
  type: 'noop',

  /**
   * A unique name for this noop step, used internally and for linking handlers.
   */
  name: 'manual-trigger',

  /**
   * A description for this noop step, used for documentation and UI.
   */
  description: 'Manual trigger point for workflow',

  /**
   * An array of topics this step can emit events to.
   */
  virtualEmits: ['workflow.start'],

  /**
   * An array of topics this step can subscribe to.
   */
  virtualSubscribes: ['manual.trigger'],

  /**
   * An array of flow names this step belongs to.
   */
  flows: ['my-workflow']
}

// NOOP steps don't need handlers - they're purely for Workbench workflow connections
```

#### Python Example

```python
config = {
    "type": "noop",
    "name": "manual-trigger",
    "description": "Manual trigger point for workflow",
    "virtualEmits": ["workflow.start"],
    "virtualSubscribes": ["manual.trigger"],
    "flows": ["my-workflow"]
}

# NOOP steps don't need handlers - they're purely for Workbench workflow connections
```

### Common Use Cases

#### Workflow Starter

This NOOP step will create a flow node in Workbench, then as a UI Step, we will override it
to show a button.

**TypeScript:**
```typescript
export const config: NoopConfig = {
  type: 'noop',
  name: 'flow-starter',
  description: 'Start point for the workflow',
  virtualEmits: ['process.begin'],
  virtualSubscribes: [],
  flows: ['main-flow']
}
```

**Python:**
```python
config = {
    "type": "noop",
    "name": "flow-starter",
    "description": "Start point for the workflow",
    "virtualEmits": ["process.begin"],
    "virtualSubscribes": [],
    "flows": ["main-flow"]
}
```

### Manual Approval Point

This NOOP step will create a flow node in Workbench, it's important just to connect
a previous Step to the step where it will have a manual approval button.

Example:

```mermaid
graph LR
    A[Submit Article]
    C[Approve Article]
    D[Reject Article]
```

Without this NOOP Step, the steps A->C and A->D would all appear disconnected in Workbench.

**TypeScript:**
```typescript
export const config: NoopConfig = {
  type: 'noop',
  name: 'approval-gate',
  description: 'Manual approval required',
  virtualEmits: ['approved'],
  virtualSubscribes: ['pending.approval'],
  flows: ['approval-flow']
}
```

**Python:**
```python
config = {
    "type": "noop",
    "name": "approval-gate",
    "description": "Manual approval required",
    "virtualEmits": ["approved"],
    "virtualSubscribes": ["pending.approval"],
    "flows": ["approval-flow"]
}
```

## How it works

It uses the `virtualEmits` and `virtualSubscribes` to connect to the previous and next steps.

In `Submit Article` Step, it must have a `virtualEmits: ['approved']` to connect to the `Manual Approval` Step.

In `Approve Article` Step, it must have a `virtualSubscribes: ['pending.approval']` to connect to the `Submit Article` Step.

It's also possible to connect two Steps without NOOP just by connecting them directly.
It's also possible to use labels in connections.

**TypeScript Examples:**
```typescript
export const config: ApiRouteConfig = {
  type: 'api',
  name: 'CreateArticle',
  path: '/articles',
  method: 'POST',
  description: 'Creates an article',
  virtualEmits: [{ topic: 'approval.required', label: 'Requires approval' }],
  emits: [],
  flows: ['article']
}

export const config: ApiRouteConfig = {
  type: 'api',
  name: 'ApproveArticle',
  path: '/articles/:id/approve',
  method: 'POST',
  description: 'Approves an article',
  virtualSubscribes: ['approval.required'],
  emits: [],
  flows: ['article']
}

export const config: ApiRouteConfig = {
  type: 'api',
  name: 'RejectArticle',
  path: '/articles/:id/reject',
  method: 'POST',
  description: 'Rejects an article',
  virtualSubscribes: ['approval.required'],
  emits: [],
  flows: ['article']
}
```

**Python Examples:**
```python
# create_article_step.py
config = {
    "type": "api",
    "name": "CreateArticle",
    "path": "/articles",
    "method": "POST",
    "description": "Creates an article",
    "virtualEmits": [{"topic": "approval.required", "label": "Requires approval"}],
    "emits": [],
    "flows": ["article"]
}

# approve_article_step.py
config = {
    "type": "api",
    "name": "ApproveArticle",
    "path": "/articles/:id/approve",
    "method": "POST",
    "description": "Approves an article",
    "virtualSubscribes": ["approval.required"],
    "emits": [],
    "flows": ["article"]
}

# reject_article_step.py
config = {
    "type": "api",
    "name": "RejectArticle",
    "path": "/articles/:id/reject",
    "method": "POST",
    "description": "Rejects an article",
    "virtualSubscribes": ["approval.required"],
    "emits": [],
    "flows": ["article"]
}
```

This will create connection to two API Steps.


```mermaid
graph LR
    A[Create Article] --> B[Requires Approval]
    B --> C[Approve Article]
    B --> D[Reject Article]
```

## When to Use NOOP Steps
- Testing workflow connections
- Manual trigger points
- Workflow visualization
- Placeholder for future steps
