import { a as PersistedValue, c as RunStatus, d as StorageAdapter, f as WorkflowRun, i as PersistedPrimitive, l as StepResult, n as CreateRunResult, o as RetryConfig, r as PersistedObject, s as RunInfo, t as ClaimedRun, u as StepStatus } from "./types-nm947ViN.mjs"; import { a as InferInput, c as StepConfig, d as Workflow, f as WorkflowInputMap, i as InferBranchOutput, l as StepContext, m as createWorkflow, n as ExecutionUnit, o as InferSteps, p as WorkflowStepsMap, r as FailureContext, s as ParallelBranch, t as AnyWorkflow, u as StepDefinition } from "./workflow-Cic2yHNB.mjs"; //#region src/core/engine.d.ts /** * A lifecycle event emitted during workflow execution. * * Consumed both by the {@link EngineHooks} callbacks and by {@link Engine.stream}. * Every event carries the owning `workflow` name so a single stream can fan out * across multiple workflows. */ type EngineEvent = { readonly type: 'runStart'; readonly runId: string; readonly workflow: string; } | { readonly type: 'stepStart'; readonly runId: string; readonly workflow: string; readonly stepName: string; } | { readonly type: 'stepComplete'; readonly runId: string; readonly workflow: string; readonly stepName: string; readonly output: PersistedValue; readonly attempts: number; } | { readonly type: 'runComplete'; readonly runId: string; readonly workflow: string; readonly output: PersistedValue; } | { readonly type: 'runFailed'; readonly runId: string; readonly workflow: string; readonly stepName: string; readonly error: Error; }; /** Narrow {@link EngineEvent} to a single `type` (or union of types). */ type EngineEventOf = Extract; /** * Lifecycle hooks fired during workflow execution. * * Hooks may be synchronous or `async` — an async hook is awaited before the * engine proceeds, so it can apply backpressure or guarantee ordering. A hook * that throws (or rejects) never affects engine state; the error is swallowed. */ interface EngineHooks { onRunStart?: (event: EngineEventOf<'runStart'>) => void; onStepStart?: (event: EngineEventOf<'stepStart'>) => void; onStepComplete?: (event: EngineEventOf<'stepComplete'>) => void; onRunComplete?: (event: EngineEventOf<'runComplete'>) => void; onRunFailed?: (event: EngineEventOf<'runFailed'>) => void; /** Called when a background operation fails (scheduled enqueue, poll cycle). Without this hook, these errors are silently swallowed. */ onError?: (error: Error) => void; } /** Options for {@link Engine.stream}. */ interface StreamOptions { /** * Maximum number of events buffered before the engine pauses (backpressure). * Defaults to `Infinity` — events buffer without bound and the engine never * waits on the consumer. Set a finite value (e.g. `1`) to pace the engine * against a slow consumer; the engine will not start the next unit of work * until the consumer drains the buffer below this size. Set `0` for strict * rendezvous delivery, where every event waits for a pending pull. */ bufferSize?: number; } /** * A pull-based, backpressure-aware stream of {@link EngineEvent}s. * * Implements `AsyncIterableIterator`, so it works directly with `for await`, * and `AsyncDisposable`, so it works with `await using`. Breaking out of a * `for await` loop (or disposing) unsubscribes from the engine automatically. */ interface ResultStream extends AsyncIterableIterator { [Symbol.asyncDispose](): Promise; } /** Configuration for {@link createEngine}. */ interface EngineConfig { /** Storage backend for persisting runs and step results. */ storage: StorageAdapter; /** Workflows the engine can execute. */ workflows: TWorkflows; /** Optional lifecycle hooks. */ hooks?: EngineHooks; /** Maximum runs to process in parallel per tick (default: 1). */ concurrency?: number; /** How long a run's lease is valid before another engine can reclaim it (default: 30000). */ runLeaseDurationMs?: number; /** How often to renew the lease while a run is executing (default: leaseDuration / 3). */ heartbeatIntervalMs?: number; } /** Options for `engine.enqueue()`. */ interface EnqueueOptions { /** Prevents duplicate runs. Same key + same input returns the existing run. Same key + different input throws. */ idempotencyKey?: string; } /** The workflow engine. Connects workflows to storage and handles execution, polling, and scheduling. */ interface Engine = Record> { /** Submit a workflow run. Type-safe: only accepts registered workflow names with matching input. */ enqueue(workflowName: TName, input: TWorkflowMap[TName], options?: EnqueueOptions): Promise; /** Get a run and its step results, or null if not found. */ getRunStatus(runId: string): Promise; /** Cancel a pending or running workflow. Returns true if cancelled. */ cancel(runId: string): Promise; /** Enqueue a workflow on a recurring interval. Returns a schedule ID. */ schedule(workflowName: TName, input: TWorkflowMap[TName], intervalMs: number): string; /** Cancel a recurring schedule by ID. */ unschedule(scheduleId: string): boolean; /** * Subscribe to a live, pull-based stream of execution events * ({@link EngineEvent}). Use it to consume step/run results as they happen, * with optional backpressure: * * ```ts * for await (const event of engine.stream()) { * if (event.type === 'runComplete') process(event.output) * } * ``` * * Each call returns an independent stream. Breaking out of the loop, or * disposing the stream, unsubscribes automatically. */ stream(options?: StreamOptions): ResultStream; /** Process one batch of pending runs. Useful for tests and CLI tools. */ tick(): Promise; /** Initialize storage and start the polling loop. Call once at startup. */ start(pollIntervalMs?: number): Promise; /** Stop the polling loop, clear all schedules, and wait for in-flight ticks to finish. */ stop(): Promise; } /** * Create a workflow engine that connects workflows to storage and handles execution. * * @example * ```ts * const engine = createEngine({ * storage: new SQLiteStorage('./reflow.db'), * workflows: [myWorkflow], * }) * await engine.start() * ``` */ declare function createEngine(config: EngineConfig): Engine>; //#endregion //#region src/core/errors.d.ts /** * Typed error hierarchy for Reflow. * * Every error Reflow throws extends `ReflowError`, so a single * `instanceof ReflowError` catch-all is always available. More specific * subclasses carry structured context (workflow name, step name, run ID, etc.) * so callers never need to parse error messages. */ /** Base class for all Reflow errors. */ declare class ReflowError extends Error { constructor(message: string); } /** Thrown when engine, retry, or schedule configuration is invalid. */ declare class ConfigError extends ReflowError { constructor(message: string); } /** Thrown when `enqueue()` or `schedule()` references an unregistered workflow name. */ declare class WorkflowNotFoundError extends ReflowError { readonly workflowName: string; constructor(workflowName: string); } /** Thrown when `createEngine()` receives the same workflow name twice. */ declare class DuplicateWorkflowError extends ReflowError { readonly workflowName: string; constructor(workflowName: string); } /** Thrown when `.step()` reuses a name that already exists in the workflow. */ declare class DuplicateStepError extends ReflowError { readonly workflowName: string; readonly stepName: string; constructor(workflowName: string, stepName: string); } /** Thrown when `complete()` is called inside a parallel step handler. */ declare class ParallelCompleteError extends ReflowError { readonly stepName: string; constructor(stepName: string); } /** A single validation issue from the input schema. */ interface ValidationIssue { readonly message: string; readonly path?: ReadonlyArray; } /** Thrown when workflow input fails schema validation. */ declare class ValidationError extends ReflowError { readonly issues: readonly ValidationIssue[]; constructor(message: string, issues: readonly ValidationIssue[]); } /** Thrown when `enqueue()` reuses an idempotency key with different input. */ declare class IdempotencyConflictError extends ReflowError { readonly workflowName: string; readonly idempotencyKey: string; constructor(workflowName: string, idempotencyKey: string); } /** Thrown when a step output or workflow input contains non-JSON-compatible data. */ declare class SerializationError extends ReflowError { readonly path: string; constructor(message: string, path: string); } /** Thrown when a step exceeds its `timeoutMs`. Reaches `onRunFailed`. */ declare class StepTimeoutError extends ReflowError { readonly timeoutMs: number; constructor(timeoutMs: number); } /** * Internal base class for errors that represent control-flow signals * (cancellation, lease loss) rather than real failures. These do NOT * reach `onRunFailed`. * * @internal Not exported from the public API. */ declare class RunControlError extends ReflowError { constructor(message: string); } /** Thrown when a run is cancelled via `engine.cancel()`. */ declare class RunCancelledError extends RunControlError { readonly runId: string; constructor(runId: string); } /** Thrown when the engine loses its lease on a run (another worker reclaimed it). */ declare class LeaseExpiredError extends RunControlError { readonly runId: string; constructor(runId: string); } //#endregion export { type AnyWorkflow, type ClaimedRun, ConfigError, type CreateRunResult, DuplicateStepError, DuplicateWorkflowError, type Engine, type EngineConfig, type EngineEvent, type EngineEventOf, type EngineHooks, type EnqueueOptions, type ExecutionUnit, type FailureContext, IdempotencyConflictError, type InferBranchOutput, type InferInput, type InferSteps, LeaseExpiredError, type ParallelBranch, ParallelCompleteError, type PersistedObject, type PersistedPrimitive, type PersistedValue, ReflowError, type ResultStream, type RetryConfig, RunCancelledError, type RunInfo, type RunStatus, SerializationError, type StepConfig, type StepContext, type StepDefinition, type StepResult, type StepStatus, StepTimeoutError, type StorageAdapter, type StreamOptions, ValidationError, type ValidationIssue, type Workflow, type WorkflowInputMap, WorkflowNotFoundError, type WorkflowRun, type WorkflowStepsMap, createEngine, createWorkflow }; //# sourceMappingURL=index.d.mts.map