UNPKG

crewai-ts

Version:

TypeScript port of crewAI for agent-based workflows

211 lines 5.92 kB
/** * FlowRouter * * Provides optimized conditional routing for flows with * efficient condition evaluation and minimal memory overhead. */ import { Flow, FlowState } from '../index.js'; /** * Types of routing conditions that can be evaluated */ export declare enum ConditionType { EQUALS = "equals", NOT_EQUALS = "not_equals", GREATER_THAN = "greater_than", LESS_THAN = "less_than", CONTAINS = "contains", NOT_CONTAINS = "not_contains", REGEX = "regex", FUNCTION = "function" } /** * Basic condition interface */ export interface BaseCondition { type: ConditionType; path: string; } /** * Value comparison condition */ export interface ValueCondition extends BaseCondition { type: ConditionType.EQUALS | ConditionType.NOT_EQUALS | ConditionType.GREATER_THAN | ConditionType.LESS_THAN; value: any; } /** * String or array contains condition */ export interface ContainsCondition extends BaseCondition { type: ConditionType.CONTAINS | ConditionType.NOT_CONTAINS; value: any; } /** * Regex matching condition */ export interface RegexCondition extends BaseCondition { type: ConditionType.REGEX; pattern: string; flags?: string; } /** * Function condition with custom evaluation logic */ export interface FunctionCondition extends BaseCondition { type: ConditionType.FUNCTION; predicate: (value: any, state: any) => boolean; } /** * Combined conditions with logical operators */ export interface LogicalGroup { operator: 'AND' | 'OR'; conditions: RouteCondition[]; } /** * Complete route condition type */ export type RouteCondition = ValueCondition | ContainsCondition | RegexCondition | FunctionCondition | LogicalGroup; /** * Route definition with condition and target */ export interface Route<T extends FlowState = FlowState> { id: string; name: string; condition: RouteCondition; target: Flow<T> | string; priority?: number; metadata?: Record<string, any>; } /** * Options for route evaluation */ export interface RouteEvaluationOptions { enableConditionCache?: boolean; cacheTTLMs?: number; traceEvaluation?: boolean; shortCircuitEvaluation?: boolean; maxTraceResults?: number; } /** * Result from evaluating routes */ export interface RouteEvaluationResult<T extends FlowState = FlowState> { matched: Route<T>[]; matchedIds: string[]; trace?: { route: string; condition: RouteCondition; result: boolean; timestamp: number; evaluationTimeMs: number; }[]; } /** * FlowRouter provides efficient conditional routing for flows */ export declare class FlowRouter<T extends FlowState = FlowState> { private defaultOptions; private routes; private routePriorities; private conditionCache; private compiledFunctions; private fastPathLookup; private evaluationCount; private cacheHitCount; /** * Create a new FlowRouter with optimized routing capabilities * @param options Default options for route evaluation */ constructor(defaultOptions?: RouteEvaluationOptions); /** * Register a new route * @param route Route definition to register */ registerRoute(route: Route<T>): void; /** * Update an existing route * @param routeId ID of the route to update * @param updates Updates to apply to the route */ updateRoute(routeId: string, updates: Partial<Route<T>>): void; /** * Remove a route * @param routeId ID of the route to remove */ removeRoute(routeId: string): boolean; /** * Find matching routes for a given state * @param state State to evaluate routes against * @param options Route evaluation options */ findMatchingRoutes(state: T, options?: RouteEvaluationOptions): RouteEvaluationResult<T>; /** * Get a specific route by ID * @param routeId ID of the route to get */ getRoute(routeId: string): Route<T> | undefined; /** * Get all registered routes */ getAllRoutes(): Route<T>[]; /** * Clear the condition evaluation cache */ clearConditionCache(): void; /** * Get evaluation performance metrics */ getPerformanceMetrics(): { evaluations: number; cacheHitRate: number; }; /** * Get an optimized evaluation order for routes * This prioritizes routes based on: * 1. Explicit priority (higher first) * 2. Matching properties in state (routes that check properties present in state) * 3. Complexity (simpler conditions evaluated first) */ private getOptimizedRouteEvaluationOrder; /** * Evaluate a route condition against a state */ private evaluateCondition; /** * Actual condition evaluation logic */ private doEvaluateCondition; /** * Get a value from a dotted path in an object */ private getValueFromPath; /** * Calculate how relevant a condition is to the current state * Returns a number representing relevance (higher is more relevant) */ private calculateStateRelevance; /** * Extract all property paths from an object * Used to find which properties are available in the state */ private extractPropertyPaths; /** * Calculate the complexity of a condition * Used for sorting conditions by complexity (simpler first) */ private getConditionComplexity; /** * Build fast path lookup for direct property access * This optimizes route finding by indexing routes by the properties they check */ private buildFastPathLookup; /** * Extract all condition paths from a condition */ private extractConditionPaths; /** * Type guard for function conditions */ private isFunctionCondition; } //# sourceMappingURL=FlowRouter.d.ts.map