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
78 lines • 2.47 kB
TypeScript
/**
* Idempotency Pattern - Ensure operations can be safely retried
*/
import { AsyncFunction, Logger } from "../types/common";
import { ConcurrentBehavior, IdempotencyRecord, IdempotencyStore, IdempotencyOptions } from "../types/idempotency";
/**
* Internal options (without execute, key, keyGenerator)
*/
interface IdempotencyInternalOptions<T = any> {
keyGenerator?: (...args: any[]) => string;
ttl?: number;
store?: IdempotencyStore<T>;
logger?: Logger;
concurrentBehavior?: ConcurrentBehavior;
waitTimeout?: number;
onCacheHit?: (key: string) => void;
onCacheMiss?: (key: string) => void;
}
/**
* Idempotency - Guarantees operation idempotence
*/
export declare class Idempotency<TResult = any, TArgs extends any[] = any[]> {
private readonly fn;
private store;
private keyGenerator;
private ttl;
private logger;
private concurrentBehavior;
private waitTimeout;
private pendingRequests;
constructor(fn: AsyncFunction<TResult, TArgs>, options?: IdempotencyInternalOptions<TResult>);
/**
* Execute function with idempotency
*/
execute(...args: TArgs): Promise<TResult>;
/**
* Handle concurrent requests with same key
*/
private handleInProgress;
/**
* Execute function and cache result
*/
private executeAndCache;
/**
* Invalidate cache for key
*/
invalidate(...args: TArgs): Promise<void>;
/**
* Clear all cache
*/
clear(): Promise<void>;
/**
* Get cached record
*/
getRecord(...args: TArgs): Promise<IdempotencyRecord<TResult> | null>;
}
/**
* Reset the global idempotency store (useful for testing)
*/
export declare function resetGlobalIdempotencyStore(): void;
/**
* Stop the global idempotency store cleanup (useful for testing or when shutting down)
*/
export declare function stopGlobalIdempotencyCleanup(): void;
/**
* Manually start the global idempotency store cleanup
*/
export declare function startGlobalIdempotencyCleanup(): void;
/**
* Create idempotent function with single parameter API
*/
export declare function idempotent<TResult = any>(options: IdempotencyOptions<TResult>): Promise<TResult>;
/**
* Decorator to make a method idempotent
*/
export declare function Idempotent<T>(options?: IdempotencyOptions<T>): (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
export {};
//# sourceMappingURL=idempotency.d.ts.map