XYLEX Group

API Reference

Complete API reference for Worlds Engine

API Reference

World Class

The main entry point for the engine.

new World(config?: WorldConfig)

Methods

  • register(...items: (Workflow | Activity)[]): void - Register workflows and activities
  • start(): Promise<void> - Start the world and workers
  • shutdown(): Promise<void> - Gracefully shut down
  • execute<T, R>(name: string, input: T, options?: WorkflowOptions): Promise<WorkflowHandle<R>> - Execute a workflow
  • query(workflowId: string): Promise<WorkflowState> - Get workflow state
  • queryWorkflows(filters: WorkflowQueryFilter): Promise<WorkflowState[]> - Query multiple workflows
  • schedule(id: string, name: string, input: any, cron: string): Promise<void> - Schedule a recurring workflow
  • getMetrics(): WorldMetrics - Get current metrics
  • getWorkers(): WorkerInfo[] - Get worker status

Workflow Functions

workflow()

Creates a workflow definition.

workflow<T, R>(
  name: string,
  handler: (ctx: WorkflowContext, input: T) => Promise<R>,
  options?: WorkflowOptions
): Workflow<T, R>

activity()

Creates an activity definition.

activity<T, R>(
  name: string,
  handler: (ctx: ActivityContext, input: T) => Promise<R>,
  options?: ActivityOptions
): Activity<T, R>

Workflow Context

Available inside workflow handlers.

interface WorkflowContext {
  // Execution
  run<T, R>(activity: Activity<T, R>, input: T): Promise<R>;
  executeChild<T, R>(name: string, input: T, options?: WorkflowOptions): Promise<WorkflowHandle<R>>;
  
  // Saga Pattern
  addCompensation(fn: () => Promise<void>): void;
  
  // Control Flow
  sleep(ms: number): Promise<void>;
  isCancelled(): boolean;
  
  // External Integration
  fetch(url: string, init?: RequestInit): Promise<Response>;
  createHook<T>(): Promise<Hook<T>>;
  createWebhook(): Promise<Webhook>;
  getWritable(): WritableStream<any>;
  
  // Metadata
  getMetadata(): WorkflowMetadata;
  workflowId: string;
  runId: string;
  parentId?: string;
}

Workflow Dev Kit API

Alternative functional API.

import {
  getWorkflowMetadata,
  getStepMetadata,
  sleep,
  fetch,
  createHook,
  defineHook,
  createWebhook,
  getWritable
} from 'worlds-engine';
  • getWorkflowMetadata() - Returns context about current workflow execution
  • getStepMetadata() - Returns context about current step execution
  • sleep(ms) - Suspends workflow deterministically
  • fetch(url) - Makes HTTP requests with automatic retry
  • createHook<T>() - Creates low-level hook
  • defineHook<T>() - Creates type-safe hook factory
  • createWebhook() - Suspends workflow until HTTP request received
  • getWritable() - Accesses current workflow run default stream

Runtime API

Functions used outside workflow handlers.

import {
  start,
  resumeHook,
  resumeWebhook,
  getRun,
  queryRuns,
  initializeRuntime
} from 'worlds-engine';
  • start(name, input, options) - Enqueues new workflow run
  • resumeHook(token, payload) - Resumes workflow via hook
  • resumeWebhook(token, request) - Resumes workflow via webhook
  • getRun(workflowId) - Gets status without waiting
  • queryRuns(filters) - Filters multiple workflow runs
  • initializeRuntime(world) - Connects runtime API to world instance

Type Definitions

Configuration Types

interface WorldConfig {
  minWorkers?: number;
  maxWorkers?: number;
  scaleThreshold?: number;
  scaleDownThreshold?: number;
  persistence?: 'memory' | 'file' | 'hybrid';
  persistencePath?: string;
  failureStrategy?: FailureStrategy;
  heartbeatInterval?: number;
  heartbeatTimeout?: number;
}

interface WorkflowOptions {
  retry?: RetryConfig;
  timeout?: string | number;
  failureStrategy?: FailureStrategy;
  taskQueue?: string;
  priority?: number;
}

Metric Types

interface WorldMetrics {
  uptime: number;
  workers: {
    total: number;
    idle: number;
    busy: number;
  };
  workflows: {
    queued: number;
    running: number;
    completed: number;
    failed: number;
  };
  throughput: {
    perMinute: number;
    perHour: number;
  };
  workload: number;
}