crewai-ts
Version:
TypeScript port of crewAI for agent-based workflows
116 lines (115 loc) • 3.94 kB
JavaScript
/**
* StorageAdapter Interface
*
* Defines the contract for all memory storage implementations
* Optimized for efficient data persistence across different backends
*/
/**
* Base class for storage adapters with common functionality
* Provides optimized serialization, compression, and error handling
*/
export class BaseStorageAdapter {
options;
constructor(options = {}) {
// Set defaults optimized for performance and memory usage
this.options = {
namespace: options.namespace || 'crewai',
compression: options.compression || false,
compressionLevel: options.compressionLevel || 6, // Balanced compression level
serializer: options.serializer || JSON.stringify,
deserializer: options.deserializer || JSON.parse,
ttlMs: options.ttlMs || 0, // 0 means no expiration
debug: options.debug || false
};
}
/**
* Get the full storage key with namespace
* @param key The base key
* @returns Namespaced key
*/
getNamespacedKey(key) {
return `${this.options.namespace}:${key}`;
}
/**
* Serialize a value for storage with optimized handling
* @param value The value to serialize
* @returns Serialized value
*/
serialize(value) {
try {
// Add metadata for improved reconstruction
const valueWithMeta = {
_data: value,
_type: typeof value,
_timestamp: Date.now()
};
// Serialize the data
const serialized = this.options.serializer(valueWithMeta);
// Apply compression if enabled
if (this.options.compression) {
return this.compress(serialized);
}
return serialized;
}
catch (error) {
if (this.options.debug) {
console.error('Error serializing value:', error);
}
// Fallback to simple string conversion
return String(value);
}
}
/**
* Deserialize a value from storage with optimized handling
* @param serialized The serialized value
* @returns Deserialized value
*/
deserialize(serialized) {
try {
// Decompress if needed
const decompressed = this.options.compression ?
this.decompress(serialized) : serialized;
// Deserialize the data
const valueWithMeta = this.options.deserializer(decompressed);
// Extract the actual data from metadata wrapper
return valueWithMeta._data;
}
catch (error) {
if (this.options.debug) {
console.error('Error deserializing value:', error);
}
// Return the original string if deserialization fails
return serialized;
}
}
/**
* Compress a string
* @param data The string to compress
* @returns Compressed string
*/
compress(data) {
// In a real implementation, use a proper compression algorithm
// like zlib or lz-string. For this example, using simple base64 encoding
return Buffer.from(data).toString('base64');
}
/**
* Decompress a compressed string
* @param compressed The compressed string
* @returns Decompressed string
*/
decompress(compressed) {
// Matching the simple compression implementation above
return Buffer.from(compressed, 'base64').toString('utf8');
}
/**
* Check if a value has expired
* @param timestamp The timestamp when the value was stored
* @returns Whether the value has expired
*/
isExpired(timestamp) {
if (this.options.ttlMs === 0)
return false; // No expiration
const now = Date.now();
return now - timestamp > this.options.ttlMs;
}
}