@chat-adapter/state-redis 4.20.2 → 4.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Logger, StateAdapter, Lock } from 'chat';
1
+ import { Logger, StateAdapter, Lock, QueueEntry } from 'chat';
2
2
  import { RedisClientType } from 'redis';
3
3
 
4
4
  interface RedisStateAdapterOptions {
@@ -41,6 +41,9 @@ declare class RedisStateAdapter implements StateAdapter {
41
41
  maxLength?: number;
42
42
  ttlMs?: number;
43
43
  }): Promise<void>;
44
+ enqueue(threadId: string, entry: QueueEntry, maxSize: number): Promise<number>;
45
+ dequeue(threadId: string): Promise<QueueEntry | null>;
46
+ queueDepth(threadId: string): Promise<number>;
44
47
  getList<T = unknown>(key: string): Promise<T[]>;
45
48
  private ensureConnected;
46
49
  /**
package/dist/index.js CHANGED
@@ -164,6 +164,39 @@ var RedisStateAdapter = class {
164
164
  arguments: [serialized, maxLength.toString(), ttlMs.toString()]
165
165
  });
166
166
  }
167
+ async enqueue(threadId, entry, maxSize) {
168
+ this.ensureConnected();
169
+ const queueKey = this.key("queue", threadId);
170
+ const serialized = JSON.stringify(entry);
171
+ const script = `
172
+ redis.call("rpush", KEYS[1], ARGV[1])
173
+ if tonumber(ARGV[2]) > 0 then
174
+ redis.call("ltrim", KEYS[1], -tonumber(ARGV[2]), -1)
175
+ end
176
+ redis.call("pexpire", KEYS[1], ARGV[3])
177
+ return redis.call("llen", KEYS[1])
178
+ `;
179
+ const ttlMs = Math.max(entry.expiresAt - Date.now(), 6e4).toString();
180
+ const result = await this.client.eval(script, {
181
+ keys: [queueKey],
182
+ arguments: [serialized, maxSize.toString(), ttlMs]
183
+ });
184
+ return result;
185
+ }
186
+ async dequeue(threadId) {
187
+ this.ensureConnected();
188
+ const queueKey = this.key("queue", threadId);
189
+ const value = await this.client.lPop(queueKey);
190
+ if (value === null) {
191
+ return null;
192
+ }
193
+ return JSON.parse(value);
194
+ }
195
+ async queueDepth(threadId) {
196
+ this.ensureConnected();
197
+ const queueKey = this.key("queue", threadId);
198
+ return await this.client.lLen(queueKey);
199
+ }
167
200
  async getList(key) {
168
201
  this.ensureConnected();
169
202
  const listKey = `${this.keyPrefix}:list:${key}`;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Lock, Logger, StateAdapter } from \"chat\";\nimport { ConsoleLogger } from \"chat\";\nimport { createClient, type RedisClientType } from \"redis\";\n\nexport interface RedisStateAdapterOptions {\n /** Key prefix for all Redis keys (default: \"chat-sdk\") */\n keyPrefix?: string;\n /** Logger instance for error reporting */\n logger: Logger;\n /** Redis connection URL (e.g., redis://localhost:6379) */\n url: string;\n}\n\n/**\n * Redis state adapter for production use.\n *\n * Provides persistent subscriptions and distributed locking\n * across multiple server instances.\n */\nexport class RedisStateAdapter implements StateAdapter {\n private readonly client: RedisClientType;\n private readonly keyPrefix: string;\n private readonly logger: Logger;\n private connected = false;\n private connectPromise: Promise<void> | null = null;\n\n constructor(options: RedisStateAdapterOptions) {\n this.client = createClient({ url: options.url });\n this.keyPrefix = options.keyPrefix || \"chat-sdk\";\n this.logger = options.logger;\n\n // Handle connection errors\n this.client.on(\"error\", (err) => {\n this.logger.error(\"Redis client error\", { error: err });\n });\n }\n\n private key(type: \"sub\" | \"lock\" | \"cache\", id: string): string {\n return `${this.keyPrefix}:${type}:${id}`;\n }\n\n private subscriptionsSetKey(): string {\n return `${this.keyPrefix}:subscriptions`;\n }\n\n async connect(): Promise<void> {\n if (this.connected) {\n return;\n }\n\n // Reuse existing connection attempt to avoid race conditions\n if (!this.connectPromise) {\n this.connectPromise = this.client.connect().then(() => {\n this.connected = true;\n });\n }\n\n await this.connectPromise;\n }\n\n async disconnect(): Promise<void> {\n if (this.connected) {\n await this.client.close();\n this.connected = false;\n this.connectPromise = null;\n }\n }\n\n async subscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.sAdd(this.subscriptionsSetKey(), threadId);\n }\n\n async unsubscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.sRem(this.subscriptionsSetKey(), threadId);\n }\n\n async isSubscribed(threadId: string): Promise<boolean> {\n this.ensureConnected();\n const result = await this.client.sIsMember(\n this.subscriptionsSetKey(),\n threadId\n );\n return result === 1;\n }\n\n async acquireLock(threadId: string, ttlMs: number): Promise<Lock | null> {\n this.ensureConnected();\n\n const token = generateToken();\n const lockKey = this.key(\"lock\", threadId);\n\n // Use SET NX EX for atomic lock acquisition\n const acquired = await this.client.set(lockKey, token, {\n NX: true,\n PX: ttlMs,\n });\n\n if (acquired) {\n return {\n threadId,\n token,\n expiresAt: Date.now() + ttlMs,\n };\n }\n\n return null;\n }\n\n async forceReleaseLock(threadId: string): Promise<void> {\n this.ensureConnected();\n const lockKey = this.key(\"lock\", threadId);\n await this.client.del(lockKey);\n }\n\n async releaseLock(lock: Lock): Promise<void> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-delete\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"del\", KEYS[1])\n else\n return 0\n end\n `;\n\n await this.client.eval(script, {\n keys: [lockKey],\n arguments: [lock.token],\n });\n }\n\n async extendLock(lock: Lock, ttlMs: number): Promise<boolean> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-extend\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"pexpire\", KEYS[1], ARGV[2])\n else\n return 0\n end\n `;\n\n const result = await this.client.eval(script, {\n keys: [lockKey],\n arguments: [lock.token, ttlMs.toString()],\n });\n\n return result === 1;\n }\n\n async get<T = unknown>(key: string): Promise<T | null> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const value = await this.client.get(cacheKey);\n\n if (value === null) {\n return null;\n }\n\n try {\n return JSON.parse(value) as T;\n } catch {\n // If parsing fails, return as string\n return value as unknown as T;\n }\n }\n\n async set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n if (ttlMs) {\n await this.client.set(cacheKey, serialized, { PX: ttlMs });\n } else {\n await this.client.set(cacheKey, serialized);\n }\n }\n\n async setIfNotExists(\n key: string,\n value: unknown,\n ttlMs?: number\n ): Promise<boolean> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n const result = ttlMs\n ? await this.client.set(cacheKey, serialized, { NX: true, PX: ttlMs })\n : await this.client.set(cacheKey, serialized, { NX: true });\n\n return result !== null;\n }\n\n async delete(key: string): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n await this.client.del(cacheKey);\n }\n\n async appendToList(\n key: string,\n value: unknown,\n options?: { maxLength?: number; ttlMs?: number }\n ): Promise<void> {\n this.ensureConnected();\n\n const listKey = `${this.keyPrefix}:list:${key}`;\n const serialized = JSON.stringify(value);\n const maxLength = options?.maxLength ?? 0;\n const ttlMs = options?.ttlMs ?? 0;\n\n // Atomic RPUSH + LTRIM + PEXPIRE via Lua\n const script = `\n redis.call(\"rpush\", KEYS[1], ARGV[1])\n if tonumber(ARGV[2]) > 0 then\n redis.call(\"ltrim\", KEYS[1], -tonumber(ARGV[2]), -1)\n end\n if tonumber(ARGV[3]) > 0 then\n redis.call(\"pexpire\", KEYS[1], tonumber(ARGV[3]))\n end\n return 1\n `;\n\n await this.client.eval(script, {\n keys: [listKey],\n arguments: [serialized, maxLength.toString(), ttlMs.toString()],\n });\n }\n\n async getList<T = unknown>(key: string): Promise<T[]> {\n this.ensureConnected();\n\n const listKey = `${this.keyPrefix}:list:${key}`;\n const values = await this.client.lRange(listKey, 0, -1);\n\n return values.map((v) => JSON.parse(v) as T);\n }\n\n private ensureConnected(): void {\n if (!this.connected) {\n throw new Error(\n \"RedisStateAdapter is not connected. Call connect() first.\"\n );\n }\n }\n\n /**\n * Get the underlying Redis client for advanced usage.\n */\n getClient(): RedisClientType {\n return this.client;\n }\n}\n\nfunction generateToken(): string {\n return `redis_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;\n}\n\nexport function createRedisState(\n options?: Partial<RedisStateAdapterOptions>\n): RedisStateAdapter {\n const url = options?.url ?? process.env.REDIS_URL;\n if (!url) {\n throw new Error(\n \"Redis url is required. Set REDIS_URL or provide it in options.\"\n );\n }\n const resolved: RedisStateAdapterOptions = {\n url,\n keyPrefix: options?.keyPrefix,\n logger: options?.logger ?? new ConsoleLogger(\"info\").child(\"redis\"),\n };\n return new RedisStateAdapter(resolved);\n}\n"],"mappings":";AACA,SAAS,qBAAqB;AAC9B,SAAS,oBAA0C;AAiB5C,IAAM,oBAAN,MAAgD;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACT,YAAY;AAAA,EACZ,iBAAuC;AAAA,EAE/C,YAAY,SAAmC;AAC7C,SAAK,SAAS,aAAa,EAAE,KAAK,QAAQ,IAAI,CAAC;AAC/C,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,SAAS,QAAQ;AAGtB,SAAK,OAAO,GAAG,SAAS,CAAC,QAAQ;AAC/B,WAAK,OAAO,MAAM,sBAAsB,EAAE,OAAO,IAAI,CAAC;AAAA,IACxD,CAAC;AAAA,EACH;AAAA,EAEQ,IAAI,MAAgC,IAAoB;AAC9D,WAAO,GAAG,KAAK,SAAS,IAAI,IAAI,IAAI,EAAE;AAAA,EACxC;AAAA,EAEQ,sBAA8B;AACpC,WAAO,GAAG,KAAK,SAAS;AAAA,EAC1B;AAAA,EAEA,MAAM,UAAyB;AAC7B,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAGA,QAAI,CAAC,KAAK,gBAAgB;AACxB,WAAK,iBAAiB,KAAK,OAAO,QAAQ,EAAE,KAAK,MAAM;AACrD,aAAK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AK;AAAA,EACd;AACF;AAEA,SAAS,gBAAwB;AAC/B,SAAO,SAAS,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,GAAG,EAAE,CAAC;AAC3E;AAEO,SAAS,iBACd,SACmB;AACnB,QAAM,MAAM,SAAS,OAAO,QAAQ,IAAI;AACxC,MAAI,CAAC,KAAK;AACR,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,QAAM,WAAqC;AAAA,IACzC;AAAA,IACA,WAAW,SAAS;AAAA,IACpB,QAAQ,SAAS,UAAU,IAAI,cAAc,MAAM,EAAE,MAAM,OAAO;AAAA,EACpE;AACA,SAAO,IAAI,kBAAkB,QAAQ;AACvC;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Lock, Logger, QueueEntry, StateAdapter } from \"chat\";\nimport { ConsoleLogger } from \"chat\";\nimport { createClient, type RedisClientType } from \"redis\";\n\nexport interface RedisStateAdapterOptions {\n /** Key prefix for all Redis keys (default: \"chat-sdk\") */\n keyPrefix?: string;\n /** Logger instance for error reporting */\n logger: Logger;\n /** Redis connection URL (e.g., redis://localhost:6379) */\n url: string;\n}\n\n/**\n * Redis state adapter for production use.\n *\n * Provides persistent subscriptions and distributed locking\n * across multiple server instances.\n */\nexport class RedisStateAdapter implements StateAdapter {\n private readonly client: RedisClientType;\n private readonly keyPrefix: string;\n private readonly logger: Logger;\n private connected = false;\n private connectPromise: Promise<void> | null = null;\n\n constructor(options: RedisStateAdapterOptions) {\n this.client = createClient({ url: options.url });\n this.keyPrefix = options.keyPrefix || \"chat-sdk\";\n this.logger = options.logger;\n\n // Handle connection errors\n this.client.on(\"error\", (err) => {\n this.logger.error(\"Redis client error\", { error: err });\n });\n }\n\n private key(type: \"sub\" | \"lock\" | \"cache\" | \"queue\", id: string): string {\n return `${this.keyPrefix}:${type}:${id}`;\n }\n\n private subscriptionsSetKey(): string {\n return `${this.keyPrefix}:subscriptions`;\n }\n\n async connect(): Promise<void> {\n if (this.connected) {\n return;\n }\n\n // Reuse existing connection attempt to avoid race conditions\n if (!this.connectPromise) {\n this.connectPromise = this.client.connect().then(() => {\n this.connected = true;\n });\n }\n\n await this.connectPromise;\n }\n\n async disconnect(): Promise<void> {\n if (this.connected) {\n await this.client.close();\n this.connected = false;\n this.connectPromise = null;\n }\n }\n\n async subscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.sAdd(this.subscriptionsSetKey(), threadId);\n }\n\n async unsubscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.sRem(this.subscriptionsSetKey(), threadId);\n }\n\n async isSubscribed(threadId: string): Promise<boolean> {\n this.ensureConnected();\n const result = await this.client.sIsMember(\n this.subscriptionsSetKey(),\n threadId\n );\n return result === 1;\n }\n\n async acquireLock(threadId: string, ttlMs: number): Promise<Lock | null> {\n this.ensureConnected();\n\n const token = generateToken();\n const lockKey = this.key(\"lock\", threadId);\n\n // Use SET NX EX for atomic lock acquisition\n const acquired = await this.client.set(lockKey, token, {\n NX: true,\n PX: ttlMs,\n });\n\n if (acquired) {\n return {\n threadId,\n token,\n expiresAt: Date.now() + ttlMs,\n };\n }\n\n return null;\n }\n\n async forceReleaseLock(threadId: string): Promise<void> {\n this.ensureConnected();\n const lockKey = this.key(\"lock\", threadId);\n await this.client.del(lockKey);\n }\n\n async releaseLock(lock: Lock): Promise<void> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-delete\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"del\", KEYS[1])\n else\n return 0\n end\n `;\n\n await this.client.eval(script, {\n keys: [lockKey],\n arguments: [lock.token],\n });\n }\n\n async extendLock(lock: Lock, ttlMs: number): Promise<boolean> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-extend\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"pexpire\", KEYS[1], ARGV[2])\n else\n return 0\n end\n `;\n\n const result = await this.client.eval(script, {\n keys: [lockKey],\n arguments: [lock.token, ttlMs.toString()],\n });\n\n return result === 1;\n }\n\n async get<T = unknown>(key: string): Promise<T | null> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const value = await this.client.get(cacheKey);\n\n if (value === null) {\n return null;\n }\n\n try {\n return JSON.parse(value) as T;\n } catch {\n // If parsing fails, return as string\n return value as unknown as T;\n }\n }\n\n async set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n if (ttlMs) {\n await this.client.set(cacheKey, serialized, { PX: ttlMs });\n } else {\n await this.client.set(cacheKey, serialized);\n }\n }\n\n async setIfNotExists(\n key: string,\n value: unknown,\n ttlMs?: number\n ): Promise<boolean> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n const result = ttlMs\n ? await this.client.set(cacheKey, serialized, { NX: true, PX: ttlMs })\n : await this.client.set(cacheKey, serialized, { NX: true });\n\n return result !== null;\n }\n\n async delete(key: string): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n await this.client.del(cacheKey);\n }\n\n async appendToList(\n key: string,\n value: unknown,\n options?: { maxLength?: number; ttlMs?: number }\n ): Promise<void> {\n this.ensureConnected();\n\n const listKey = `${this.keyPrefix}:list:${key}`;\n const serialized = JSON.stringify(value);\n const maxLength = options?.maxLength ?? 0;\n const ttlMs = options?.ttlMs ?? 0;\n\n // Atomic RPUSH + LTRIM + PEXPIRE via Lua\n const script = `\n redis.call(\"rpush\", KEYS[1], ARGV[1])\n if tonumber(ARGV[2]) > 0 then\n redis.call(\"ltrim\", KEYS[1], -tonumber(ARGV[2]), -1)\n end\n if tonumber(ARGV[3]) > 0 then\n redis.call(\"pexpire\", KEYS[1], tonumber(ARGV[3]))\n end\n return 1\n `;\n\n await this.client.eval(script, {\n keys: [listKey],\n arguments: [serialized, maxLength.toString(), ttlMs.toString()],\n });\n }\n\n async enqueue(\n threadId: string,\n entry: QueueEntry,\n maxSize: number\n ): Promise<number> {\n this.ensureConnected();\n\n const queueKey = this.key(\"queue\", threadId);\n const serialized = JSON.stringify(entry);\n\n // Atomic RPUSH + LTRIM + PEXPIRE via Lua\n const script = `\n redis.call(\"rpush\", KEYS[1], ARGV[1])\n if tonumber(ARGV[2]) > 0 then\n redis.call(\"ltrim\", KEYS[1], -tonumber(ARGV[2]), -1)\n end\n redis.call(\"pexpire\", KEYS[1], ARGV[3])\n return redis.call(\"llen\", KEYS[1])\n `;\n\n const ttlMs = Math.max(entry.expiresAt - Date.now(), 60_000).toString();\n\n const result = await this.client.eval(script, {\n keys: [queueKey],\n arguments: [serialized, maxSize.toString(), ttlMs],\n });\n\n return result as number;\n }\n\n async dequeue(threadId: string): Promise<QueueEntry | null> {\n this.ensureConnected();\n\n const queueKey = this.key(\"queue\", threadId);\n const value = await this.client.lPop(queueKey);\n\n if (value === null) {\n return null;\n }\n\n return JSON.parse(value) as QueueEntry;\n }\n\n async queueDepth(threadId: string): Promise<number> {\n this.ensureConnected();\n\n const queueKey = this.key(\"queue\", threadId);\n return await this.client.lLen(queueKey);\n }\n\n async getList<T = unknown>(key: string): Promise<T[]> {\n this.ensureConnected();\n\n const listKey = `${this.keyPrefix}:list:${key}`;\n const values = await this.client.lRange(listKey, 0, -1);\n\n return values.map((v) => JSON.parse(v) as T);\n }\n\n private ensureConnected(): void {\n if (!this.connected) {\n throw new Error(\n \"RedisStateAdapter is not connected. Call connect() first.\"\n );\n }\n }\n\n /**\n * Get the underlying Redis client for advanced usage.\n */\n getClient(): RedisClientType {\n return this.client;\n }\n}\n\nfunction generateToken(): string {\n return `redis_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;\n}\n\nexport function createRedisState(\n options?: Partial<RedisStateAdapterOptions>\n): RedisStateAdapter {\n const url = options?.url ?? process.env.REDIS_URL;\n if (!url) {\n throw new Error(\n \"Redis url is required. Set REDIS_URL or provide it in options.\"\n );\n }\n const resolved: RedisStateAdapterOptions = {\n url,\n keyPrefix: options?.keyPrefix,\n logger: options?.logger ?? new ConsoleLogger(\"info\").child(\"redis\"),\n };\n return new RedisStateAdapter(resolved);\n}\n"],"mappings":";AACA,SAAS,qBAAqB;AAC9B,SAAS,oBAA0C;AAiB5C,IAAM,oBAAN,MAAgD;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACT,YAAY;AAAA,EACZ,iBAAuC;AAAA,EAE/C,YAAY,SAAmC;AAC7C,SAAK,SAAS,aAAa,EAAE,KAAK,QAAQ,IAAI,CAAC;AAC/C,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,SAAS,QAAQ;AAGtB,SAAK,OAAO,GAAG,SAAS,CAAC,QAAQ;AAC/B,WAAK,OAAO,MAAM,sBAAsB,EAAE,OAAO,IAAI,CAAC;AAAA,IACxD,CAAC;AAAA,EACH;AAAA,EAEQ,IAAI,MAA0C,IAAoB;AACxE,WAAO,GAAG,KAAK,SAAS,IAAI,IAAI,IAAI,EAAE;AAAA,EACxC;AAAA,EAEQ,sBAA8B;AACpC,WAAO,GAAG,KAAK,SAAS;AAAA,EAC1B;AAAA,EAEA,MAAM,UAAyB;AAC7B,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAGA,QAAI,CAAC,KAAK,gBAAgB;AACxB,WAAK,iBAAiB,KAAK,OAAO,QAAQ,EAAE,KAAK,MAAM;AACrD,aAAK,YAAY;AAAA,MACnB,CAAC;AAAA,IACH;AAEA,UAAM,KAAK;AAAA,EACb;AAAA,EAEA,MAAM,aAA4B;AAChC,QAAI,KAAK,WAAW;AAClB,YAAM,KAAK,OAAO,MAAM;AACxB,WAAK,YAAY;AACjB,WAAK,iBAAiB;AAAA,IACxB;AAAA,EACF;AAAA,EAEA,MAAM,UAAU,UAAiC;AAC/C,S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IACT;AAEA,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEA,MAAM,WAAW,UAAmC;AAClD,SAAK,gBAAgB;AAErB,UAAM,WAAW,KAAK,IAAI,SAAS,QAAQ;AAC3C,WAAO,MAAM,KAAK,OAAO,KAAK,QAAQ;AAAA,EACxC;AAAA,EAEA,MAAM,QAAqB,KAA2B;AACpD,SAAK,gBAAgB;AAErB,UAAM,UAAU,GAAG,KAAK,SAAS,SAAS,GAAG;AAC7C,UAAM,SAAS,MAAM,KAAK,OAAO,OAAO,SAAS,GAAG,EAAE;AAEtD,WAAO,OAAO,IAAI,CAAC,MAAM,KAAK,MAAM,CAAC,CAAM;AAAA,EAC7C;AAAA,EAEQ,kBAAwB;AAC9B,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,YAA6B;AAC3B,WAAO,KAAK;AAAA,EACd;AACF;AAEA,SAAS,gBAAwB;AAC/B,SAAO,SAAS,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,GAAG,EAAE,CAAC;AAC3E;AAEO,SAAS,iBACd,SACmB;AACnB,QAAM,MAAM,SAAS,OAAO,QAAQ,IAAI;AACxC,MAAI,CAAC,KAAK;AACR,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,QAAM,WAAqC;AAAA,IACzC;AAAA,IACA,WAAW,SAAS;AAAA,IACpB,QAAQ,SAAS,UAAU,IAAI,cAAc,MAAM,EAAE,MAAM,OAAO;AAAA,EACpE;AACA,SAAO,IAAI,kBAAkB,QAAQ;AACvC;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chat-adapter/state-redis",
3
- "version": "4.20.2",
3
+ "version": "4.22.0",
4
4
  "description": "Redis state adapter for chat (production)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "dependencies": {
19
19
  "redis": "^5.11.0",
20
- "chat": "4.20.2"
20
+ "chat": "4.22.0"
21
21
  },
22
22
  "repository": {
23
23
  "type": "git",