ai-patterns
Version:
Production-ready TypeScript patterns to build solid and robust AI applications. Retry logic, circuit breakers, rate limiting, human-in-the-loop escalation, prompt versioning, response validation, context window management, and more—all with complete type
150 lines • 4.3 kB
TypeScript
/**
* Global unified storage for all ai-patterns.
* Single source of truth for all in-memory state management.
*/
/**
* Configuration options for GlobalStorage
*/
export interface GlobalStorageOptions {
/**
* Enable automatic cleanup of expired entries
*/
autoCleanup?: boolean;
/**
* Cleanup interval in milliseconds (default: 60000ms = 1 minute)
*/
cleanupIntervalMs?: number;
}
/**
* Namespace prefixes for different patterns
*/
export declare enum StorageNamespace {
IDEMPOTENCY = "idempotency:",
COST_TRACKING = "cost:",
AB_TEST = "abtest:",
PROMPT_VERSION = "prompt:",
REFLECTION = "reflection:",
CUSTOM = "custom:"
}
/**
* Entry wrapper with optional expiration timestamp
*/
export interface StorageEntry<T> {
value: T;
expiresAt?: number;
namespace?: string;
}
/**
* Global unified storage singleton
* Single Map for all patterns with namespace isolation
*/
export declare class GlobalStorage {
private static instance;
private store;
private cleanupTimer;
private options;
private constructor();
/**
* Get the global storage instance (singleton)
*/
static getInstance(options?: GlobalStorageOptions): GlobalStorage;
/**
* Reset the global storage instance (useful for testing)
*/
static resetInstance(): void;
/**
* Clear all data but keep the instance (useful for tests)
*/
static clearAll(): Promise<void>;
/**
* Create a namespaced key
*/
private namespaceKey;
/**
* Get a value from storage
*/
get<T = any>(namespace: string, key: string): Promise<T | undefined>;
/**
* Set a value in storage with optional TTL
*/
set<T = any>(namespace: string, key: string, value: T, ttlMs?: number): Promise<void>;
/**
* Delete a specific entry
*/
delete(namespace: string, key: string): Promise<boolean>;
/**
* Clear all entries for a namespace (or all if no namespace)
*/
clear(namespace?: string): Promise<void>;
/**
* Check if a key exists and is not expired
*/
has(namespace: string, key: string): Promise<boolean>;
/**
* Get all keys for a namespace (excluding expired entries)
*/
keys(namespace: string): Promise<string[]>;
/**
* Get number of entries for a namespace (excluding expired)
*/
size(namespace?: string): Promise<number>;
/**
* Get statistics about storage usage
*/
getStats(): {
totalEntries: number;
byNamespace: Record<string, number>;
expiredEntries: number;
};
/**
* Start automatic cleanup of expired entries
*/
private startCleanup;
/**
* Stop automatic cleanup
*/
stopCleanup(): void;
/**
* Manually cleanup expired entries
*/
cleanupExpired(): number;
/**
* Cleanup on instance destruction
*/
private destroy;
}
/**
* Legacy adapter class for backward compatibility
* @deprecated Use GlobalStorage.getInstance() directly
*/
export declare class InMemoryStorage<K = string, V = any> {
protected store: Map<K, StorageEntry<V>>;
private cleanupTimer;
private readonly options;
constructor(options?: GlobalStorageOptions);
get(key: K): Promise<V | undefined>;
set(key: K, value: V, ttlMs?: number): Promise<void>;
delete(key: K): Promise<boolean>;
protected clear(): Promise<void>;
protected has(key: K): Promise<boolean>;
keys(): Promise<K[]>;
protected size(): Promise<number>;
protected startCleanup(intervalMs?: number): void;
protected stopCleanup(): void;
protected cleanupExpired(): void;
protected getRawEntry(key: K): StorageEntry<V> | undefined;
protected destroy(): void;
}
/**
* Simple key-value in-memory storage (string keys)
*/
export declare class InMemoryKeyValueStorage<V = any> extends InMemoryStorage<string, V> {
getValue(key: string): Promise<V | undefined>;
setValue(key: string, value: V, ttlMs?: number): Promise<void>;
deleteValue(key: string): Promise<boolean>;
clearAll(): Promise<void>;
hasValue(key: string): Promise<boolean>;
getAllKeys(): Promise<string[]>;
getSize(): Promise<number>;
}
//# sourceMappingURL=storage.d.ts.map