/** * Workflow Service * * Main service class for workflow operations. * Provides CRUD operations and execution for Ductape Workflows. * * Based on Ductape Workflows Code-First API documentation. */ import { IProductWorkflow } from '../types/productsBuilder.types'; import { IWorkflowServiceConfig, IWorkflowConfig, IDefineWorkflowOptions, IExecuteWorkflowOptions, IWorkflowExecutionResult, IWorkflowDispatchInput, IWorkflowDispatchResult, ISendSignalOptions, IQueryWorkflowOptions, IWorkflowReplayInput, IWorkflowReplayResult, IWorkflowRestartInput, IWorkflowRestartResult, IWorkflowResumeInput, IWorkflowResumeResult, IWorkflowReplayFromStepInput, IWorkflowStatusInput, IWorkflowStatus, IWorkflowCancelInput, IWorkflowCancelResult, IWorkflowHistoryInput, IWorkflowHistory, IStepDetailInput, IStepDetail, IRelatedExecutionsInput, IRelatedExecutions, ICompareExecutionsInput, IExecutionComparison } from './types'; /** * Error class for workflow-related errors */ export declare class WorkflowError extends Error { readonly code: string; readonly details?: Record; constructor(message: string, code: string, details?: Record); static configurationError(message: string): WorkflowError; static validationError(message: string, details?: Record): WorkflowError; static notFoundError(message: string): WorkflowError; static executionError(message: string, details?: Record): WorkflowError; } /** * Main Workflow Service class * Provides unified interface for workflow management and execution */ export declare class WorkflowService { /** Service configuration */ private config; /** ProductBuilder instances cache (keyed by product tag) */ private productBuilders; /** Local workflow configurations */ private localConfigs; /** Workflow API service for backend communication */ private workflowApiService; /** LogService instance for logging operations */ private logService; /** Current product ID for logging */ private productId; private _privateKey; /** Cache manager for 3-tier caching */ private cacheManager; /** Local cache for cache configurations to avoid repeated API calls */ private cacheConfigCache; /** * Create a new WorkflowService instance * @param config - Optional configuration for authentication and workspace context */ constructor(config?: IWorkflowServiceConfig & { private_key: string; access_key: string; }); /** * Update service configuration */ updateConfig(config: IWorkflowServiceConfig & { access_key: string; }): void; /** * Get auth payload for API calls */ private getAuthPayload; /** * Ensure WorkflowApiService is initialized */ private ensureApiService; /** * Get service configuration */ getConfig(): IWorkflowServiceConfig | null; /** * Create a new ProductBuilder instance */ private createNewProductBuilder; /** * Get or create a ProductBuilder instance for the given product tag */ private getProductBuilder; /** * Initialize logging service */ private initializeLogService; /** * Validate cache tag exists in product and return cache configuration */ private validateCache; /** * Create a new ProcessorService instance for job scheduling */ private createNewProcessor; /** * Create a new workflow from JSON schema * * @example * ```ts * await ductape.workflows.create('my-product', { * tag: 'order-fulfillment', * name: 'Order Fulfillment', * steps: [ * { tag: 'validate', type: 'action', app: 'orders', event: 'validate', input: {} }, * { tag: 'process', type: 'database_action', database: 'orders-db', event: 'create', input: {} }, * ], * envs: [{ slug: 'prd' }], * }); * ``` */ create(productTag: string, workflowData: Partial): Promise; /** * Fetch all workflows for a product * * @example * ```ts * const workflows = await ductape.workflows.fetchAll('my-product'); * workflows.forEach(wf => console.log(wf.name, wf.tag)); * ``` */ fetchAll(productTag?: string): Promise; /** * Fetch a specific workflow by tag * * @example * ```ts * const workflow = await ductape.workflows.fetch('order-fulfillment', 'my-product'); * console.log(workflow?.name, workflow?.steps.length); * ``` */ fetch(workflowTag: string, productTag?: string): Promise; /** * Update a workflow * * @example * ```ts * await ductape.workflows.update('order-fulfillment', 'my-product', { * description: 'Updated description', * options: { timeout: 3600000 }, * }); * ``` */ update(workflowTag: string, productTag: string, updates: Partial): Promise; /** * Delete a workflow * * @example * ```ts * await ductape.workflows.delete('order-fulfillment', 'my-product'); * ``` */ delete(workflowTag: string, productTag: string): Promise; /** * Define a workflow using the code-first API * Compiles the handler to JSON schema and creates the workflow in one step * * @example * ```ts * const orderWorkflow = await ductape.workflows.define({ * product: 'my-product', * tag: 'order-fulfillment', * name: 'Order Fulfillment', * handler: async (ctx) => { * const validation = await ctx.step('validate', async () => { * return ctx.action.run({ * app: 'inventory-service', * event: 'validate-order', * input: { body: ctx.input }, * }); * }); * * if (!validation.valid) { * return { success: false, error: validation.reason }; * } * * return { success: true }; * }, * }); * ``` */ define, TOutput = unknown>(options: IDefineWorkflowOptions): Promise>; /** * Execute a workflow * * @example * ```ts * const result = await ductape.workflows.execute({ * product: 'my-product', * env: 'production', * tag: 'order-fulfillment', * input: { * order_id: 'ORD-12345', * items: [{ product_id: 'PROD-1', quantity: 2 }], * }, * }); * * console.log(result.status); // 'completed' | 'failed' | 'rolled_back' * console.log(result.output); * ``` */ execute(options: IExecuteWorkflowOptions): Promise>; /** * Dispatch a workflow to run as a scheduled job * * @example * ```ts * // Schedule a workflow to run in 1 hour * const result = await ductape.workflows.dispatch({ * product: 'my-product', * env: 'production', * workflow: 'order-fulfillment', * input: { order_id: 'ORD-123' }, * schedule: { start_at: Date.now() + 3600000 }, * }); * * // Run on a cron schedule * const result = await ductape.workflows.dispatch({ * product: 'my-product', * env: 'production', * workflow: 'daily-report', * input: {}, * schedule: { cron: '0 0 * * *' }, // Daily at midnight * }); * ``` */ dispatch(data: IWorkflowDispatchInput): Promise; /** * Send a signal to a running workflow * * @example * ```ts * await ductape.workflows.signal({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * signal: 'approve', * payload: { approver_id: 'user-456', comments: 'Approved' }, * }); * ``` */ signal(options: ISendSignalOptions): Promise; /** * Query a running workflow * * @example * ```ts * const status = await ductape.workflows.query({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * query: 'getStatus', * }); * * console.log(status.current_step, status.progress); * ``` */ query(options: IQueryWorkflowOptions): Promise; /** * Replay a workflow with the same input * * @example * ```ts * const result = await ductape.workflows.replay({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * reason: 'Debugging payment failure', * }); * ``` */ replay(options: IWorkflowReplayInput): Promise; /** * Restart a workflow with new or modified input * * @example * ```ts * const result = await ductape.workflows.restart({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * input: { email: 'corrected@email.com' }, * reason: 'Customer email was incorrect', * }); * ``` */ restart(options: IWorkflowRestartInput): Promise; /** * Resume a paused or failed workflow from where it stopped * * @example * ```ts * const result = await ductape.workflows.resume({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * from_checkpoint: 'payment-complete', * }); * ``` */ resume(options: IWorkflowResumeInput): Promise; /** * Replay a workflow starting from a specific step * * @example * ```ts * const result = await ductape.workflows.replayFromStep({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * from_step: 'process-payment', * }); * ``` */ replayFromStep(options: IWorkflowReplayFromStepInput): Promise; /** * Get the status of a workflow execution * * @example * ```ts * const status = await ductape.workflows.status({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * }); * ``` */ status(options: IWorkflowStatusInput): Promise; /** * Cancel a running workflow * * @example * ```ts * const result = await ductape.workflows.cancel({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * reason: 'User requested cancellation', * }); * ``` */ cancel(options: IWorkflowCancelInput): Promise; /** * Get the execution history of a workflow * * @example * ```ts * const history = await ductape.workflows.history({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * }); * ``` */ history(options: IWorkflowHistoryInput): Promise; /** * Get detailed information about a specific step * * @example * ```ts * const detail = await ductape.workflows.stepDetail({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * step_tag: 'process-payment', * }); * ``` */ stepDetail(options: IStepDetailInput): Promise; /** * List all related executions (replays, restarts, resumes) * * @example * ```ts * const related = await ductape.workflows.relatedExecutions({ * product: 'my-product', * env: 'production', * workflow_id: 'wf-123', * }); * ``` */ relatedExecutions(options: IRelatedExecutionsInput): Promise; /** * Compare two workflow executions * * @example * ```ts * const comparison = await ductape.workflows.compare({ * product: 'my-product', * env: 'production', * workflow_ids: ['wf-123', 'wf-456'], * }); * ``` */ compare(options: ICompareExecutionsInput): Promise; /** * Convert IProductWorkflow to IWorkflowConfig */ private productWorkflowToConfig; } /** * Result of workflow.define() - contains the compiled schema and handler */ export interface IDefinedWorkflow, TOutput = unknown> { /** Workflow tag */ tag: string; /** Workflow name */ name: string; /** Original handler function */ handler: (ctx: any) => Promise; /** Compile to JSON schema */ compile: () => IProductWorkflow; /** The compiled JSON schema */ schema: IProductWorkflow; } export declare const workflowService: WorkflowService; export default WorkflowService;