UNPKG

agent-workflow

Version:

A powerful workflow engine supporting DAG (Directed Acyclic Graph) task scheduling, dynamic task generation, and intelligent strategy systems.

110 lines (109 loc) 3.93 kB
import { TaskInput } from './Task'; export declare abstract class DAGTask { abstract name: string; dependsOn: DAGTask[]; constructor(dependencies?: DAGTask[]); abstract execute(input: TaskInput): Promise<Record<string, any>>; } export declare abstract class StreamingDAGTask extends DAGTask { executeStream?(input: TaskInput): AsyncGenerator<StreamChunk, Record<string, any>, unknown>; isStreaming?: boolean; } export interface StreamChunk { type: 'progress' | 'data' | 'error' | 'complete'; taskName: string; content?: any; progress?: number; timestamp: number; metadata?: Record<string, any>; } export interface StreamingWorkflowResult { stream: AsyncGenerator<StreamChunk, WorkflowResult, unknown>; getResult(): Promise<WorkflowResult>; } export interface WorkflowConfig { enableStreaming?: boolean; retryAttempts?: number; timeoutMs?: number; maxDynamicSteps?: number; } export interface WorkflowContext { readonly data: Record<string, unknown>; get<T>(key: string): T | undefined; set<T>(key: string, value: T): void; getAll(): Record<string, unknown>; clear(): void; getExecutionHistory(): TaskExecutionResult[]; getLastResult(): any; } export interface TaskExecutionResult { taskName: string; status: 'completed' | 'failed' | 'skipped'; output?: any; error?: string; duration: number; timestamp: number; } export interface WorkflowResult<T = any> { success: boolean; data?: T; error?: Error; executionTime: number; taskResults: Map<string, TaskExecutionResult>; dynamicTasksGenerated?: number; totalSteps?: number; } export interface DynamicStrategy { name: string; condition: (context: WorkflowContext, result?: any) => boolean; generator: (context: WorkflowContext) => Promise<DAGTask[]>; priority?: number; once?: boolean; } export declare abstract class AISDKStreamingTask extends DAGTask { executeStreamAI?(input: TaskInput): Promise<{ textStream?: AsyncIterable<string>; fullStream?: AsyncIterable<any>; toDataStreamResponse?: () => Response; toReadableStream?: () => ReadableStream; }>; isAISDKStreaming?: boolean; } export interface AISDKStreamingWorkflowResult { textStream: AsyncIterable<string>; fullStream: AsyncIterable<any>; toDataStreamResponse(): Response; toReadableStream(): ReadableStream; getResult(): Promise<WorkflowResult>; } export declare class WorkflowBuilder { private config; private staticTasks; private dynamicStrategies; private constructor(); static create(): WorkflowBuilder; withConfig(config: Partial<WorkflowConfig>): this; withRetry(attempts: number): this; withTimeout(timeoutMs: number): this; addTask(task: DAGTask): this; addTasks(tasks: DAGTask[]): this; addDynamicStrategy(strategy: DynamicStrategy): this; whenCondition(condition: (context: WorkflowContext) => boolean, taskGenerator: (context: WorkflowContext) => Promise<DAGTask[]>): this; onTaskComplete(taskName: string, resultProcessor: (result: any, context: WorkflowContext) => Promise<DAGTask[]>): this; onContextChange(contextKey: string, taskGenerator: (value: any, context: WorkflowContext) => Promise<DAGTask[]>): this; build(): Workflow; buildStreaming(): StreamingWorkflow; buildAISDKStreaming(): AISDKStreamingWorkflow; } export interface Workflow { execute(input?: TaskInput): Promise<WorkflowResult>; executeStream?(input?: TaskInput): StreamingWorkflowResult; getContext(): WorkflowContext; getResults(): Map<string, TaskExecutionResult>; } export interface StreamingWorkflow extends Workflow { executeStream(input?: TaskInput): StreamingWorkflowResult; } export interface AISDKStreamingWorkflow extends Workflow { executeStreamAISDK(input?: TaskInput): AISDKStreamingWorkflowResult; }