API Reference
Step API

Step API Reference

This reference documents the core APIs for defining, executing, and managing steps in ZoopFlow.

Step Definition

defineStep<TInput, TOutput>(definition: StepDefinition<TInput, TOutput>)

Creates a new step definition with specified input and output types.

import { defineStep } from '@zoopflow/core';
 
const stringTransformer = defineStep({
  id: 'examples.string.transform',
  version: '1.0.0',
  description: 'Transforms a string based on the specified operation',
  
  inputSchema: { /* JSON Schema for input validation */ },
  outputSchema: { /* JSON Schema for output validation */ },
  
  execute: async (input, context) => {
    // Step implementation
    return output;
  }
});

Parameters

NameTypeDescription
definitionStepDefinition<TInput, TOutput>The step definition configuration

Returns

Returns a Step<TInput, TOutput> instance that can be registered and executed.

StepDefinition<TInput, TOutput>

Interface for defining a step.

interface StepDefinition<TInput, TOutput> {
  // Required properties
  id: string;
  version: string;
  description?: string;
  displayName?: string;
  inputSchema: JSONSchema7;
  outputSchema: JSONSchema7;
  execute: (input: TInput, context: StepContext) => Promise<TOutput>;
  
  // Optional properties
  retryConfig?: RetryConfig;
  timeoutSeconds?: number;
  tags?: string[];
  metadata?: Record<string, unknown>;
}

Properties

NameTypeRequiredDescription
idstringYesUnique identifier for the step in the format domain.resource.action
versionstringYesSemantic version of the step
descriptionstringNoHuman-readable description of what the step does
displayNamestringNoFriendly name for display in UIs
inputSchemaJSONSchema7YesJSON Schema for validating step input
outputSchemaJSONSchema7YesJSON Schema for validating step output
executeFunctionYesThe implementation function for the step
retryConfigRetryConfigNoConfiguration for retry behavior
timeoutSecondsnumberNoTimeout for step execution in seconds
tagsstring[]NoTags for categorization and filtering
metadataRecord<string, unknown>NoAdditional metadata for the step

Step Context

The StepContext provides utilities, services, and state management for step execution.

StepContext Interface

interface StepContext {
  // Execution metadata
  info: ExecutionInfo;
  stepMetadata: StepMetadata;
  readonly executionMetadata: StepExecutionMetadata;
  readonly history: StepHistoryEntry[];
  
  // State management
  getState<T>(key: string): Promise<T | undefined>;
  setState<T>(key: string, value: T): Promise<void>;
  getStateObject(): Promise<Record<string, unknown>>;
  updateState(stateUpdates: Record<string, unknown>): Promise<void>;
  
  // Checkpointing and recovery
  createCheckpoint(metadata?: Record<string, unknown>): Promise<string>;
  restoreFromCheckpoint(checkpointId: string): Promise<void>;
  getCheckpointInfo(checkpointId: string): Promise<CheckpointInfo | undefined>;
  getAllCheckpoints(): Promise<CheckpointInfo[]>;
  
  // Logging and history
  log(message: string, metadata?: Record<string, unknown>): void;
  addHistoryEntry(type: 'custom', details: Record<string, unknown>): void;
  
  // Lifecycle management
  complete(details?: Record<string, unknown>): Promise<void>;
  fail(reason: string, details?: Record<string, unknown>): Promise<void>;
  
  // Service access
  getService<T>(name: string, options?: Record<string, unknown>): T;
}

ExecutionInfo

Information about the current execution context.

interface ExecutionInfo {
  stepId: string;
  flowId: string;
  executionId: string;
  attemptNumber: number;
}

StepExecutionMetadata

Metadata about the step execution.

interface StepExecutionMetadata {
  executionId: string;
  status: StepExecutionStatus;
  startedAt: Date;
  completedAt?: Date;
  succeeded?: boolean;
  failureReason?: string;
  attemptNumber: number;
  [key: string]: unknown;
}

StepExecutionStatus

Possible status values for step execution.

type StepExecutionStatus =
  | 'pending'
  | 'executing'
  | 'completed'
  | 'failed'
  | 'skipped';

StepHistoryEntry

History entry for tracking step execution events.

interface StepHistoryEntry {
  timestamp: Date;
  type: 'started' | 'checkpoint' | 'completed' | 'failed' | 'state_change' | 'custom';
  details?: Record<string, unknown>;
}

CheckpointInfo

Information about a step execution checkpoint.

interface CheckpointInfo {
  id: string;
  createdAt: Date;
  state: Record<string, unknown>;
  metadata?: Record<string, unknown>;
}

Step Execution

executeStep<TInput, TOutput>(step: Step<TInput, TOutput>, input: TInput, options?: ExecuteStepOptions)

Executes a step with the given input and options.

import { executeStep } from '@zoopflow/core';
 
const result = await executeStep(stringTransformer, {
  text: 'Hello, World!',
  operation: 'uppercase'
});

Parameters

NameTypeRequiredDescription
stepStep<TInput, TOutput>YesThe step to execute
inputTInputYesInput data for the step
optionsExecuteStepOptionsNoOptions for execution

Returns

Returns a Promise<TOutput> with the result of the step execution.

ExecuteStepOptions

Options for step execution.

interface ExecuteStepOptions {
  context?: StepContext;
  validateInput?: boolean;
  validateOutput?: boolean;
  retryConfig?: RetryConfig;
  timeoutSeconds?: number;
}

Step Registry

registerStep<TInput, TOutput>(step: Step<TInput, TOutput>)

Registers a step with the step registry.

import { registerStep } from '@zoopflow/core';
 
registerStep(stringTransformer);

Parameters

NameTypeDescription
stepStep<TInput, TOutput>The step to register

getStep(id: string, version?: string)

Gets a registered step by ID and optionally version.

import { getStep } from '@zoopflow/core';
 
const step = getStep('examples.string.transform');
// or with version
const specificVersion = getStep('examples.string.transform', '1.0.0');

Parameters

NameTypeRequiredDescription
idstringYesThe ID of the step to retrieve
versionstringNoThe version of the step (defaults to latest)

Returns

Returns the Step instance if found, otherwise undefined.

listSteps(options?: ListStepsOptions)

Lists registered steps based on optional filter criteria.

import { listSteps } from '@zoopflow/core';
 
// List all steps
const allSteps = listSteps();
 
// List steps with specific tag
const dataSteps = listSteps({ tags: ['data-processing'] });

Parameters

NameTypeRequiredDescription
optionsListStepsOptionsNoFilter options

Returns

Returns an array of StepMetadata objects.

ListStepsOptions

Options for listing steps.

interface ListStepsOptions {
  domain?: string;
  tags?: string[];
  includeDeprecated?: boolean;
}

Retry Configuration

RetryConfig

Configuration for step retry behavior.

interface RetryConfig {
  maxAttempts: number;
  backoffCoefficient?: number;
  initialIntervalSeconds?: number;
  maxIntervalSeconds?: number;
  retryableErrors?: string[];
}

Properties

NameTypeRequiredDescription
maxAttemptsnumberYesMaximum number of retry attempts
backoffCoefficientnumberNoMultiplier for retry interval (default: 2.0)
initialIntervalSecondsnumberNoInitial retry interval in seconds (default: 1)
maxIntervalSecondsnumberNoMaximum retry interval in seconds (default: 60)
retryableErrorsstring[]NoList of error types that should trigger retry