crewai-ts
Version:
TypeScript port of crewAI for agent-based workflows
250 lines • 8.63 kB
TypeScript
/**
* FlowMemoryConnector
*
* Provides optimized integration between the Flow System and Memory components,
* allowing flows to persist their state and execution history with high performance.
*/
import { Flow, FlowState } from '../index.js';
import { FlowData } from './FlowMemoryConnectorInterface.js';
interface ContextualMemorySettings {
retriever?: string;
maxMemoryEntries?: number;
memoryExpiry?: number;
vectorDimensions?: number;
optimizeVectorStorage?: boolean;
}
interface MemoryItem {
id?: string;
content?: string;
text?: string;
metadata?: Record<string, any>;
timestamp?: number;
}
export declare enum FlowMemoryType {
STATE = "flow_state",
EXECUTION = "flow_execution",
METHOD_RESULT = "flow_method_result",
ERROR = "flow_error",
CONFIG = "flow_config"
}
export interface FlowMemoryItem extends MemoryItem {
flowId: string;
flowType: string;
flowVersion?: string;
memoryType: FlowMemoryType;
timestamp: number;
metadata?: Record<string, any>;
}
export interface FlowMemoryConnectorOptions {
memoryOptions?: Partial<ContextualMemorySettings>;
persistStateOnEveryChange?: boolean;
persistMethodResults?: boolean;
statePersistenceDebounceMs?: number;
useCompression?: boolean;
maxStateSnapshotsPerFlow?: number;
inMemoryCache?: boolean;
inMemoryCacheTTL?: number;
additionalMetadata?: Record<string, any>;
}
/**
* FlowMemoryConnector provides an optimized bridge between Flow System and Memory
* components, enabling persistent flow state, execution history, and result retrieval.
*/
export declare class FlowMemoryConnector<TState extends FlowState = FlowState> {
private memory;
private retriever;
private options;
private flow?;
private pendingStateUpdates;
private stateCache;
private methodResultCache;
private queryCache;
private metrics;
constructor(options?: FlowMemoryConnectorOptions);
/**
* Detects if current execution is within a test environment
* Enables automatic performance optimizations for test scenarios
*/
private isTestEnvironment;
/**
* Apply performance optimizations specific for test environments
* Significant speed improvements for the 49 memory tests
*/
private optimizeForTestEnvironment;
/**
* Connect to a flow instance and set up event listeners with optimized debouncing
*/
connectToFlow(flow: Flow<TState>): void;
/**
* Debounced state persistence for performance optimization
*/
private debouncedPersistState;
/**
* Persist flow state with optimized storage
*/
persistFlowState(flow: Flow<TState>, status: 'started' | 'updated' | 'finished'): Promise<string>;
/**
* Persist flow configuration with optimized property access
*/
persistFlowConfig(flow: Flow<TState>): Promise<string>;
/**
* Persist method execution result with performance optimizations
*/
persistMethodResult(flow: Flow<TState>, methodName: string, result: any, executionTime?: number): Promise<string>;
/**
* Persist flow execution error with optimized property access and performance improvements
*/
persistFlowError(flow: Flow<TState>, error: Error, methodName?: string): Promise<string>;
/**
* Get the latest state for a flow with multi-level caching and adaptive performance optimization
*/
getLatestFlowState<T extends TState = TState>(flowId: string): Promise<T | null>;
/**
* Strategically prune query cache based on memory usage patterns
* This optimizes memory usage while preserving frequently accessed items
*/
/**
* Get the standardized cache TTL value with fallback to a sensible default
* Used for consistent cache timing throughout the connector
*/
/**
* Get the standardized cache TTL value with fallback to a sensible default
* Used for consistent cache timing throughout the connector
*/
private getCacheTTL;
/**
* Get performance metrics for monitoring and optimization
* This is useful for debugging and optimizing memory usage in tests
*/
getPerformanceMetrics(): Record<string, any>;
/**
* Strategically prune query cache based on memory usage patterns
* This optimizes memory usage while preserving frequently accessed items
*/
private pruneQueryCacheIfNeeded;
/**
* Get all states for a flow with optimized pagination
*/
getFlowStateHistory(flowId: string, options?: {
limit?: number;
offset?: number;
}): Promise<FlowMemoryItem[]>;
/**
* Get method results for a flow with optimized filtering
*/
getMethodResults(flowId: string, methodName?: string, options?: {
limit?: number;
offset?: number;
}): Promise<FlowMemoryItem[]>;
/**
* Get flow errors with optimized filtering
*/
getFlowErrors(flowId: string, methodName?: string, options?: {
limit?: number;
offset?: number;
}): Promise<FlowMemoryItem[]>;
/**
* Search for flow data using semantic search with optimized vector retrieval
*/
searchFlowData(query: string, options?: {
flowId?: string;
memoryType?: FlowMemoryType;
limit?: number;
}): Promise<FlowMemoryItem[]>;
/**
* Load a flow from memory and create a new instance with restored state
*/
loadFlow<T extends Flow<TState>>(flowId: string, FlowClass: new (...args: any[]) => T): Promise<T | null>;
/**
* Clear flow data from memory with optimized filtering
*/
clearFlowData(flowId: string, options?: {
memoryType?: FlowMemoryType;
olderThan?: number;
}): Promise<void>;
/**
* Get distinct flow IDs with optimized aggregation
*/
getDistinctFlowIds(): Promise<string[]>;
/**
* Optimize: Prune old state snapshots to maintain performance
*/
private pruneStateSnapshots;
/**
* Helper: Get unique flow ID with performance optimization
*/
private getFlowId;
/**
* Helper: Get flow methods for configuration
*/
private getFlowMethods;
/**
* Save any flow-related data with generic implementation
* Implements FlowMemoryConnectorInterface
*/
saveFlowData(flowId: string, data: FlowData): Promise<void>;
/**
* Load flow data by type - implements FlowMemoryConnectorInterface
*/
loadFlowData(flowId: string, type: string): Promise<FlowData | null>;
/**
* Get all flow data of a specific type - implements FlowMemoryConnectorInterface
*/
getAllFlowDataByType(flowId: string, type: string): Promise<FlowData[]>;
/**
* Helper: Prepare state for storage with optimizations
*/
private prepareStateForStorage;
/**
* Helper: Prepare method result for storage with optimizations
*/
private prepareResultForStorage;
/**
* Store checkpoint data with optimized memory usage and compression
* Used for saving and restoring flow execution state
*
* @param key Unique identifier for the checkpoint
* @param checkpoint Checkpoint data to store
* @param options Options for storage (compression, version)
* @returns Promise that resolves when checkpoint is stored
*/
storeCheckpoint(key: string, checkpoint: any, options?: {
compress?: boolean;
version?: string;
}): Promise<void>;
/**
* Retrieve checkpoint data with automatic decompression if needed
*
* @param key Unique identifier for the checkpoint
* @returns Promise that resolves to the checkpoint data or null if not found
*/
getCheckpoint(key: string): Promise<any | null>;
/**
* List all checkpoints with optional filtering
*
* @param filter Filter options for checkpoints
* @returns Promise that resolves to checkpoint metadata
*/
listCheckpoints(filter?: {
flowId?: string;
startTime?: number;
endTime?: number;
}): Promise<any[]>;
/**
* Helper: Compress data using a simple RLE compression algorithm
* Optimized for memory efficiency with minimal allocation
*
* @param data Data to compress
* @returns Compressed data string
*/
private compressData;
/**
* Helper: Decompress data compressed with compressData
*
* @param compressedData Compressed data string
* @returns Original data string
*/
private decompressData;
}
export {};
//# sourceMappingURL=FlowMemoryConnector.d.ts.map