crewai-ts
Version:
TypeScript port of crewAI for agent-based workflows
102 lines • 2.86 kB
TypeScript
/**
* FlowStateManager
*
* Efficiently manages flow state with optimized memory usage, change tracking,
* and performance monitoring.
*/
import { FlowState } from '../FlowState.js';
import { FlowPersistence } from '../persistence/FlowPersistence.js';
/**
* Options for configuring the FlowStateManager
*/
export interface FlowStateManagerOptions {
persistence?: FlowPersistence;
persistOnChange?: boolean;
persistenceDebounceMs?: number;
useStructuralSharing?: boolean;
maxStateHistorySize?: number;
enableGarbageCollection?: boolean;
trackPerformance?: boolean;
warningThresholdMs?: number;
}
/**
* Performance metrics for state operations
*/
interface StatePerformanceMetrics {
updateCount: number;
totalUpdateTimeMs: number;
averageUpdateTimeMs: number;
maxUpdateTimeMs: number;
lastUpdateTimeMs: number;
totalMemoryChangeMb: number;
}
/**
* FlowStateManager handles efficient state management for flows
* with optimized memory usage and performance tracking.
*/
export declare class FlowStateManager<T extends FlowState = FlowState> {
private currentState;
private stateHistory;
private stateHistoryTimestamps;
private readonly options;
private readonly persistence?;
private performanceMetrics;
private changePending;
private persistenceTimeout?;
constructor(initialState: T, options?: FlowStateManagerOptions);
/**
* Get the current state
*/
getState(): T;
/**
* Update the state using an updater function
* Uses optimized structural sharing when enabled
*/
updateState(updater: (state: T) => void): T;
/**
* Get performance metrics for state operations
*/
getPerformanceMetrics(): StatePerformanceMetrics;
/**
* Get the state history
*/
getStateHistory(): T[];
/**
* Restore state to a specific point in history
*/
restoreState(timestamp: number): boolean;
/**
* Clear all state history to free memory
*/
clearHistory(): void;
/**
* Save the current state to persistence layer immediately
*/
persistStateNow(): Promise<void>;
/**
* Handle state change with debounced persistence
*/
private handleStateChange;
/**
* Add a state to the history with efficient memory management
*/
private addStateToHistory;
/**
* Remove oldest states from history to stay within size limit
*/
private pruneStateHistory;
/**
* Update performance metrics with new data
*/
private updatePerformanceMetrics;
/**
* Create a shallow clone of a state object for structural sharing
*/
private shallowCloneState;
/**
* Create a deep clone of a state object
*/
private cloneState;
}
export {};
//# sourceMappingURL=FlowStateManager.d.ts.map