Overview
Motia uses an auto-discovery system to find and load your application components at runtime. This eliminates the need for manual registration and keeps your codebase clean and focused.What Gets Discovered
Motia automatically discovers:- Steps - Files matching
**/*.step.{ts,js,py} - Streams - Files matching
**/*.stream.{ts,js} - Functions - Registered via WebSocket protocol
- Triggers - Defined in step configurations
Step Discovery
File Naming Convention
Steps must follow the naming pattern*.step.{ts,js,py}:
Discovery Process
Glob Pattern Matching
Motia searches for files using glob patterns:Reference:
motia-js/packages/motia/src/new/build/loader.ts:35-38File Compilation
Each discovered file is bundled with esbuild:This allows importing dependencies and TypeScript/JSX without separate build steps.Reference:
motia-js/packages/motia/src/new/build/loader.ts:55-61Module Loading
The compiled code is written to a temporary file and dynamically imported:Reference:
motia-js/packages/motia/src/new/build/loader.ts:64-68ID Generation
Each step receives a deterministic UUID v5 based on its file path:This ensures consistent IDs across deployments and enables reliable trigger registration.Reference:
motia-js/packages/motia/src/new/build/loader.ts:29-32Excluded Directories
The following directories are excluded from discovery:node_modules/dist/__pycache__/.git/
Stream Discovery
File Naming Convention
Streams must follow the naming pattern*.stream.{ts,js}:
Stream Discovery Process
Streams follow the same discovery process as steps:motia-js/packages/motia/src/new/build/loader.ts:40-45
Stream Registration
Discovered streams are registered by name:motia-js/packages/motia/src/new/build/loader.ts:48-50
Function Registration
Functions are registered dynamically via the WebSocket protocol between workers and the engine.Registration Message
engine/src/protocol.rs:57-67
Registration Flow
Worker Connects
A worker establishes a WebSocket connection to the engine:Reference:
engine/src/engine/mod.rs:645-650Worker ID Assignment
The engine assigns a unique worker ID and sends it back:Reference:
engine/src/engine/mod.rs:680-686Trigger Discovery
Triggers are discovered from step configurations and registered with the engine.Trigger Registration
engine/src/protocol.rs:39-44
Trigger Types Registry
The engine maintains a registry of trigger types:engine/src/engine/mod.rs:234-251
Module System
The engine uses a modular architecture where each module is auto-initialized:engine/src/lib.rs:20-38
Module Trait
Each module implements theModule trait:
Configuration Discovery
Motia discovers configuration from multiple sources:1. motia.config.ts
2. Environment Variables
3. .env Files
Motia automatically loads.env files:
motia-js/packages/motia/src/index.ts:2
Type Generation
Motia generates TypeScript types for discovered streams and enqueues:Hot Reloading
In development mode, Motia watches for file changes and automatically:- Re-discovers modified steps
- Unregisters old functions
- Registers new functions
- Updates trigger registrations
Best Practices
Follow naming conventions
Follow naming conventions
Always use
.step.{ts,js,py} and .stream.{ts,js} suffixes for discoverable files.Keep helper code separate
Keep helper code separate
Place utility functions, types, and constants in separate files without
.step or .stream suffixes to avoid unnecessary processing.Use consistent file paths
Use consistent file paths
Step IDs are generated from file paths. Moving a step file creates a new ID and requires re-registering triggers.
Export config and handler
Export config and handler
Ensure every step file exports both
config and handler. Missing exports will cause discovery to fail silently.Leverage type generation
Leverage type generation
Use the auto-generated types from
.motia/types.ts for type-safe stream and enqueue operations.Debugging Discovery
To troubleshoot discovery issues:1. Check File Naming
Ensure files match the expected patterns:2. Verify Exports
Confirm thatconfig and handler are properly exported:
3. Check Logs
Look for discovery-related log messages:4. Inspect the Registry
Use the Motia Console to view registered functions and triggers.Next Steps
Your First Step
Learn recommended project organization
Installation
Explore CLI installation
Workflows
Organize steps into workflows
Examples
View example projects