UNPKG

kaibanjs

Version:

AI Multi-Agent library for Javascript Developers.

185 lines (184 loc) 4.88 kB
/** * Workflow Driven Agent Implementation. * * This file implements the WorkflowDrivenAgent, a specialized agent that executes workflows * instead of using traditional LLM-based reasoning. The agent maintains workflow state and * can handle suspend/resume operations for long-running workflows. * * @packageDocumentation */ import { Task } from '..'; import { TeamStore } from '../stores'; import { AgentLoopResult } from '../utils/llm.types'; import { BaseAgent, Env } from './baseAgent'; import type { Workflow, WorkflowResult } from '@kaibanjs/workflow'; /** * Interface for WorkflowDrivenAgent parameters */ export interface WorkflowDrivenAgentParams { /** The workflow to be executed by this agent */ name: string; /** The workflow to be executed by this agent */ workflow: Workflow<any, any, any>; type?: 'WorkflowDrivenAgent'; } /** * Store state for workflow-driven agent */ export interface WorkflowAgentState { /** Current run ID */ currentRunId: string | null; /** Current workflow status */ workflowStatus: 'idle' | 'running' | 'suspended' | 'completed' | 'failed'; /** Last workflow result */ lastResult: WorkflowResult<any> | null; /** Last error */ lastError: Error | null; /** Execution metadata */ metadata: { iterations: number; maxIterations: number; startTime: number | null; endTime: number | null; }; } /** * WorkflowDrivenAgent class that extends BaseAgent to execute workflows */ export declare class WorkflowDrivenAgent extends BaseAgent { /** The workflow to be executed */ readonly workflow: Workflow<any, any, any>; /** Internal state for workflow management */ private workflowState; constructor(config: WorkflowDrivenAgentParams); /** * Initializes the agent with store and environment configuration */ initialize(store: TeamStore, env: Env): void; /** * Process a task by executing the assigned workflow */ workOnTask(task: Task, inputs: Record<string, unknown>, context: string): Promise<AgentLoopResult>; /** * Process feedback for a task (not applicable for workflow-driven agent) */ workOnFeedback(_task: Task, _feedbackList: Array<{ content: string; }>, _context: string): Promise<AgentLoopResult>; /** * Resume work on a suspended workflow */ workOnTaskResume(task: Task): Promise<void>; /** * Reset the agent and workflow state */ reset(): void; /** * Get cleaned agent data (without sensitive information) */ getCleanedAgent(): Partial<BaseAgent>; /** * Create initial workflow state */ private createInitialState; /** * Initialize workflow execution */ private initializeWorkflowExecution; /** * Create runtime context with task data */ private createRuntimeContext; /** * Setup workflow run */ private setupWorkflowRun; /** * Subscribe to workflow events */ private subscribeToWorkflowEvents; /** * Execute workflow */ private executeWorkflow; /** * Handle workflow result based on status */ private handleWorkflowResult; /** * Validate resume conditions */ private validateResumeConditions; /** * Get current workflow run */ private getCurrentRun; /** * Handle workflow events for monitoring and logging */ private handleWorkflowEvent; /** * Handle workflow status updates */ private handleWorkflowStatusUpdate; /** * Handle step status updates */ private handleStepStatusUpdate; /** * Handle completed workflow */ private handleWorkflowCompleted; /** * Handle failed workflow */ private handleWorkflowFailed; /** * Handle suspended workflow */ private handleWorkflowSuspended; /** * Handle workflow errors - FIXED: Now properly sets ERRORED status */ private handleWorkflowError; /** * Update task store for completion */ private updateTaskStoreForCompletion; /** * Update task store for failure */ private updateTaskStoreForFailure; /** * Calculate execution time */ private calculateExecutionTime; /** * Get metadata for agent loop result */ private getMetadata; /** * Reset workflow state */ private resetWorkflowState; /** * Log workflow start */ private logWorkflowStart; /** * Log workflow completion */ private logWorkflowCompleted; /** * Log workflow failure */ private logWorkflowFailed; /** * Log workflow suspension */ private logWorkflowSuspended; /** * Log workflow error */ private logWorkflowError; }