/** * Redis Blackboard Backend * * Provides a `RedisBackend` implementation of `BlackboardBackend` suitable for * multi-process and multi-machine agent coordination. Data is shared across all * processes that connect to the same Redis server. * * Architecture — write-through cache: * Reads → served from a local in-memory cache (fast, sync) * Writes → written to local cache immediately, then flushed to Redis async * Hydrate → on startup, loads existing keys from Redis into the local cache * * This design keeps the synchronous `BlackboardBackend` interface intact while * still leveraging Redis for distributed coordination. * * Peer dependency: * Install `ioredis`, `node-redis`, or any Redis client that satisfies the * minimal `RedisClient` interface defined below. No production dependency is * added to network-ai — Redis is optional and user-supplied. * * npm install ioredis # recommended * # or * npm install redis # node-redis v4+ * * @example * ```typescript * import Redis from 'ioredis'; * import { RedisBackend } from 'network-ai/lib/blackboard-backend-redis'; * * const client = new Redis({ host: 'localhost', port: 6379 }); * const backend = new RedisBackend(client, { keyPrefix: 'myapp:bb:' }); * await backend.hydrate(); // load existing data from Redis * * const board = orchestrator.getBlackboard('prod', { backend }); * ``` * * @module BlackboardBackendRedis * @version 1.0.0 * @license MIT */ import type { BlackboardBackend, BlackboardEntry } from './blackboard-backend'; /** * Minimal pipeline interface — returned by `client.pipeline()` or * `client.multi()`. Only the methods used by `RedisBackend` are required. */ export interface RedisPipeline { set(key: string, value: string): this; set(key: string, value: string, exArg: 'EX', seconds: number): this; exec(): Promise; } /** * Minimal Redis client interface. * * Any client that satisfies this shape can be used — ioredis, node-redis v4+, * or a custom mock for testing. */ export interface RedisClient { get(key: string): Promise; set(key: string, value: string): Promise; set(key: string, value: string, exArg: 'EX', seconds: number): Promise; del(...keys: string[]): Promise; keys(pattern: string): Promise; pipeline(): RedisPipeline; } export interface RedisBackendOptions { /** * Prefix prepended to every Redis key. * Useful for namespacing multiple boards on the same Redis server. * @default 'network-ai:bb:' */ keyPrefix?: string; } /** * Redis-backed `BlackboardBackend` for multi-process / multi-machine * agent coordination. * * Uses a write-through local cache so reads remain synchronous and fast. * Call `hydrate()` after construction to load any pre-existing Redis data * into the local cache before your agents start reading. * * @example * ```typescript * import Redis from 'ioredis'; * import { RedisBackend } from 'network-ai/lib/blackboard-backend-redis'; * * const client = new Redis(); * const backend = new RedisBackend(client, { keyPrefix: 'project-x:bb:' }); * await backend.hydrate(); * * const board = orchestrator.getBlackboard('shared', { backend }); * ``` */ export declare class RedisBackend implements BlackboardBackend { private cache; private client; private keyPrefix; private _ready; constructor(client: RedisClient, options?: RedisBackendOptions); /** * Read a single entry from the local cache. * Returns `null` if not found or TTL has expired. */ read(key: string): BlackboardEntry | null; /** * Write a value to the local cache and push to Redis asynchronously. * The write is immediately visible to all local reads. */ write(key: string, value: unknown, sourceAgent: string, ttl?: number): BlackboardEntry; /** * Delete an entry from the local cache and Redis asynchronously. * Returns `true` if the key existed. */ delete(key: string): boolean; /** * Return all non-expired keys from the local cache. */ listKeys(): string[]; /** * Return a full snapshot of all non-expired entries from the local cache. */ getSnapshot(): Record; /** * Load all existing entries from Redis into the local cache. * * Call this once after construction before your agents start reading, so the * local cache reflects any state written by other processes. * * @example * ```typescript * const backend = new RedisBackend(client); * await backend.hydrate(); * ``` */ hydrate(): Promise; /** * Flush all local cache entries to Redis in a single pipeline. * * Useful for ensuring durability before a graceful shutdown, or for * synchronising a newly-started process with the latest in-memory state. */ flush(): Promise; /** * Clear the local cache. Does NOT delete keys in Redis. * Call `hydrate()` afterwards to reload from Redis. */ clearCache(): void; /** * `true` after `hydrate()` has completed at least once. */ get isReady(): boolean; /** * Number of entries currently in the local cache * (including expired entries not yet evicted). */ get cacheSize(): number; private _isExpired; private _pushToRedis; } //# sourceMappingURL=blackboard-backend-redis.d.ts.map