/**
 * Workflows Module - Multi-step workflow execution engine
 *
 * Provides:
 * - Workflow definition and registration
 * - Step-by-step execution with state management
 * - Event-driven workflow lifecycle
 * - Conditional branching and error handling
 * - Workflow persistence and resume capability
 */
import { EventEmitter } from 'events';
export type WorkflowStatus = 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'cancelled';
export type StepStatus = 'pending' | 'running' | 'completed' | 'failed' | 'skipped';
export interface WorkflowStep {
    id: string;
    name: string;
    description?: string;
    action: string | ((context: WorkflowContext) => Promise<StepResult>);
    condition?: string | ((context: WorkflowContext) => boolean);
    timeout?: number;
    retryOnFailure?: boolean;
    maxRetries?: number;
    onSuccess?: string;
    onFailure?: string;
    metadata?: Record<string, unknown>;
}
export interface WorkflowDefinition {
    id: string;
    name: string;
    description: string;
    version: string;
    steps: WorkflowStep[];
    initialContext?: Record<string, unknown>;
    timeout?: number;
    onComplete?: string;
    onError?: string;
    tags?: string[];
}
export interface StepResult {
    success: boolean;
    output?: unknown;
    error?: string;
    duration?: number;
    metadata?: Record<string, unknown>;
}
export interface StepExecution {
    stepId: string;
    stepName: string;
    status: StepStatus;
    startedAt?: Date;
    completedAt?: Date;
    result?: StepResult;
    retries: number;
}
export interface WorkflowContext {
    workflowId: string;
    instanceId: string;
    variables: Record<string, unknown>;
    stepResults: Map<string, StepResult>;
    currentStep?: string;
    metadata: Record<string, unknown>;
}
export interface WorkflowState {
    instanceId: string;
    workflowId: string;
    status: WorkflowStatus;
    context: WorkflowContext;
    stepExecutions: Map<string, StepExecution>;
    currentStepIndex: number;
    createdAt: Date;
    startedAt?: Date;
    completedAt?: Date;
    pausedAt?: Date;
    error?: string;
    totalDuration?: number;
}
export interface WorkflowResult {
    success: boolean;
    instanceId: string;
    workflowId: string;
    status: WorkflowStatus;
    stepResults: Map<string, StepResult>;
    finalContext: Record<string, unknown>;
    duration: number;
    error?: string;
    completedSteps: number;
    totalSteps: number;
}
export interface WorkflowExecutionOptions {
    initialContext?: Record<string, unknown>;
    startFromStep?: string;
    timeout?: number;
    onStepComplete?: (step: StepExecution) => void;
    onStepStart?: (stepId: string) => void;
}
export declare class StepManager extends EventEmitter {
    private actionHandlers;
    constructor();
    /**
     * Register built-in action handlers
     */
    private registerBuiltInActions;
    /**
     * Register a custom action handler
     */
    registerAction(name: string, handler: (context: WorkflowContext) => Promise<StepResult>): void;
    /**
     * Check if an action is registered
     */
    hasAction(name: string): boolean;
    /**
     * Get all registered action names
     */
    getRegisteredActions(): string[];
    /**
     * Execute a step
     */
    executeStep(step: WorkflowStep, context: WorkflowContext, options?: {
        timeout?: number;
    }): Promise<StepResult>;
    /**
     * Execute with timeout
     */
    private executeWithTimeout;
    /**
     * Evaluate a condition
     */
    evaluateCondition(condition: string | ((context: WorkflowContext) => boolean), context: WorkflowContext): boolean;
}
export declare class WorkflowStateManager {
    private statesDir;
    private states;
    constructor(statesDir?: string);
    /**
     * Ensure the states directory exists
     */
    private ensureDir;
    /**
     * Load existing states from disk
     */
    private loadStates;
    /**
     * Serialize state for storage
     */
    private serializeState;
    /**
     * Deserialize state from storage
     */
    private deserializeState;
    /**
     * Generate unique instance ID
     */
    generateInstanceId(): string;
    /**
     * Create a new workflow state
     */
    createState(workflowId: string, initialContext?: Record<string, unknown>): WorkflowState;
    /**
     * Get a workflow state by instance ID
     */
    getState(instanceId: string): WorkflowState | undefined;
    /**
     * Update a workflow state
     */
    updateState(instanceId: string, updates: Partial<WorkflowState>): WorkflowState | undefined;
    /**
     * Save state to disk
     */
    saveState(state: WorkflowState): void;
    /**
     * Delete a workflow state
     */
    deleteState(instanceId: string): boolean;
    /**
     * Get all states
     */
    getAllStates(): WorkflowState[];
    /**
     * Get states by workflow ID
     */
    getStatesByWorkflow(workflowId: string): WorkflowState[];
    /**
     * Get states by status
     */
    getStatesByStatus(status: WorkflowStatus): WorkflowState[];
    /**
     * Clear completed states
     */
    clearCompleted(): number;
    /**
     * Get statistics
     */
    getStats(): {
        total: number;
        pending: number;
        running: number;
        paused: number;
        completed: number;
        failed: number;
        cancelled: number;
    };
}
export declare class WorkflowEngine extends EventEmitter {
    private workflows;
    private stateManager;
    private stepManager;
    private runningWorkflows;
    constructor(statesDir?: string);
    /**
     * Register built-in workflows
     */
    private registerBuiltInWorkflows;
    /**
     * Register a workflow definition
     */
    registerWorkflow(workflow: WorkflowDefinition): void;
    /**
     * Unregister a workflow
     */
    unregisterWorkflow(workflowId: string): boolean;
    /**
     * Get a workflow definition
     */
    getWorkflow(workflowId: string): WorkflowDefinition | undefined;
    /**
     * Get all registered workflows
     */
    getWorkflows(): WorkflowDefinition[];
    /**
     * Register a custom action
     */
    registerAction(name: string, handler: (context: WorkflowContext) => Promise<StepResult>): void;
    /**
     * Start a new workflow instance
     */
    startWorkflow(workflowId: string, options?: WorkflowExecutionOptions): Promise<WorkflowResult>;
    /**
     * Resume a paused workflow
     */
    resumeWorkflow(instanceId: string): Promise<WorkflowResult>;
    /**
     * Pause a running workflow
     */
    pauseWorkflow(instanceId: string): boolean;
    /**
     * Cancel a workflow
     */
    cancelWorkflow(instanceId: string): boolean;
    /**
     * Execute a workflow
     */
    private executeWorkflow;
    /**
     * Build workflow result
     */
    private buildResult;
    /**
     * Get workflow state
     */
    getWorkflowState(instanceId: string): WorkflowState | undefined;
    /**
     * Get all workflow instances
     */
    getWorkflowInstances(): WorkflowState[];
    /**
     * Get running workflows
     */
    getRunningWorkflows(): string[];
    /**
     * Get statistics
     */
    getStats(): ReturnType<WorkflowStateManager['getStats']>;
    /**
     * Format workflow result for display
     */
    formatResult(result: WorkflowResult): string;
    /**
     * Format available workflows for display
     */
    formatWorkflows(): string;
    /**
     * Clean up resources
     */
    dispose(): void;
}
export declare function getWorkflowEngine(statesDir?: string): WorkflowEngine;
export declare function resetWorkflowEngine(): void;
export type { PipelineStep, PipelineOperator, PipelineToken, StepResult as PipelineStepResult, PipelineResult, ToolExecutor, PipelineConfig, } from './pipeline.js';
export { PipelineCompositor, getPipelineCompositor, resetPipelineCompositor, } from './pipeline.js';
export type { PipelineFileDefinition, PipelineValidationResult, } from '../commands/pipeline.js';
export { loadPipelineFile, validatePipelineDefinition, createPipelineCommand, } from '../commands/pipeline.js';
