@chat-adapter/state-memory 4.13.1 → 4.13.3
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/README.md +7 -18
- package/dist/index.d.ts +3 -4
- package/dist/index.js +0 -12
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# @chat-adapter/state-memory
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@chat-adapter/state-memory)
|
|
4
|
+
[](https://www.npmjs.com/package/@chat-adapter/state-memory)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
In-memory state adapter for [Chat SDK](https://chat-sdk.dev/docs). For development and testing only — state is lost on restart.
|
|
6
7
|
|
|
7
8
|
## Installation
|
|
8
9
|
|
|
@@ -16,30 +17,18 @@ npm install chat @chat-adapter/state-memory
|
|
|
16
17
|
import { Chat } from "chat";
|
|
17
18
|
import { createMemoryState } from "@chat-adapter/state-memory";
|
|
18
19
|
|
|
19
|
-
const
|
|
20
|
+
const bot = new Chat({
|
|
20
21
|
userName: "mybot",
|
|
21
22
|
adapters: { /* ... */ },
|
|
22
23
|
state: createMemoryState(),
|
|
23
24
|
});
|
|
24
25
|
```
|
|
25
26
|
|
|
26
|
-
##
|
|
27
|
+
## Documentation
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
- Distributed locking (single-process only)
|
|
30
|
-
- Zero configuration required
|
|
29
|
+
Full documentation at [chat-sdk.dev/docs/state/memory](https://chat-sdk.dev/docs/state/memory).
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- **Not suitable for production**: State is lost on restart
|
|
35
|
-
- **Single process only**: Locks don't work across multiple instances
|
|
36
|
-
- **No persistence**: Subscriptions reset when process restarts
|
|
37
|
-
|
|
38
|
-
## When to Use
|
|
39
|
-
|
|
40
|
-
- Local development
|
|
41
|
-
- Unit testing
|
|
42
|
-
- Single-instance deployments (not recommended for production)
|
|
31
|
+
For production, use [Redis](https://chat-sdk.dev/docs/state/redis) or [ioredis](https://chat-sdk.dev/docs/state/ioredis).
|
|
43
32
|
|
|
44
33
|
## License
|
|
45
34
|
|
package/dist/index.d.ts
CHANGED
|
@@ -7,9 +7,9 @@ import { StateAdapter, Lock } from 'chat';
|
|
|
7
7
|
* Use RedisStateAdapter for production.
|
|
8
8
|
*/
|
|
9
9
|
declare class MemoryStateAdapter implements StateAdapter {
|
|
10
|
-
private subscriptions;
|
|
11
|
-
private locks;
|
|
12
|
-
private cache;
|
|
10
|
+
private readonly subscriptions;
|
|
11
|
+
private readonly locks;
|
|
12
|
+
private readonly cache;
|
|
13
13
|
private connected;
|
|
14
14
|
private connectPromise;
|
|
15
15
|
connect(): Promise<void>;
|
|
@@ -17,7 +17,6 @@ declare class MemoryStateAdapter implements StateAdapter {
|
|
|
17
17
|
subscribe(threadId: string): Promise<void>;
|
|
18
18
|
unsubscribe(threadId: string): Promise<void>;
|
|
19
19
|
isSubscribed(threadId: string): Promise<boolean>;
|
|
20
|
-
listSubscriptions(adapterName?: string): AsyncIterable<string>;
|
|
21
20
|
acquireLock(threadId: string, ttlMs: number): Promise<Lock | null>;
|
|
22
21
|
releaseLock(lock: Lock): Promise<void>;
|
|
23
22
|
extendLock(lock: Lock, ttlMs: number): Promise<boolean>;
|
package/dist/index.js
CHANGED
|
@@ -39,18 +39,6 @@ var MemoryStateAdapter = class {
|
|
|
39
39
|
this.ensureConnected();
|
|
40
40
|
return this.subscriptions.has(threadId);
|
|
41
41
|
}
|
|
42
|
-
async *listSubscriptions(adapterName) {
|
|
43
|
-
this.ensureConnected();
|
|
44
|
-
for (const threadId of this.subscriptions) {
|
|
45
|
-
if (adapterName) {
|
|
46
|
-
if (threadId.startsWith(`${adapterName}:`)) {
|
|
47
|
-
yield threadId;
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
yield threadId;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
42
|
async acquireLock(threadId, ttlMs) {
|
|
55
43
|
this.ensureConnected();
|
|
56
44
|
this.cleanExpiredLocks();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Lock, StateAdapter } from \"chat\";\n\ninterface MemoryLock extends Lock {\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Lock, StateAdapter } from \"chat\";\n\ninterface MemoryLock extends Lock {\n expiresAt: number;\n threadId: string;\n token: string;\n}\n\ninterface CachedValue<T = unknown> {\n expiresAt: number | null; // null = no expiry\n value: T;\n}\n\n/**\n * In-memory state adapter for development and testing.\n *\n * WARNING: State is not persisted across restarts.\n * Use RedisStateAdapter for production.\n */\nexport class MemoryStateAdapter implements StateAdapter {\n private readonly subscriptions = new Set<string>();\n private readonly locks = new Map<string, MemoryLock>();\n private readonly cache = new Map<string, CachedValue>();\n private connected = false;\n private connectPromise: Promise<void> | null = null;\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 = Promise.resolve().then(() => {\n if (process.env.NODE_ENV === \"production\") {\n console.warn(\n \"[chat] MemoryStateAdapter is not recommended for production. \" +\n \"Consider using @chat-adapter/state-redis instead.\"\n );\n }\n this.connected = true;\n });\n }\n\n await this.connectPromise;\n }\n\n async disconnect(): Promise<void> {\n this.connected = false;\n this.connectPromise = null;\n this.subscriptions.clear();\n this.locks.clear();\n }\n\n async subscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n this.subscriptions.add(threadId);\n }\n\n async unsubscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n this.subscriptions.delete(threadId);\n }\n\n async isSubscribed(threadId: string): Promise<boolean> {\n this.ensureConnected();\n return this.subscriptions.has(threadId);\n }\n\n async acquireLock(threadId: string, ttlMs: number): Promise<Lock | null> {\n this.ensureConnected();\n this.cleanExpiredLocks();\n\n // Check if already locked\n const existingLock = this.locks.get(threadId);\n if (existingLock && existingLock.expiresAt > Date.now()) {\n return null;\n }\n\n // Create new lock\n const lock: MemoryLock = {\n threadId,\n token: generateToken(),\n expiresAt: Date.now() + ttlMs,\n };\n\n this.locks.set(threadId, lock);\n return lock;\n }\n\n async releaseLock(lock: Lock): Promise<void> {\n this.ensureConnected();\n\n const existingLock = this.locks.get(lock.threadId);\n if (existingLock && existingLock.token === lock.token) {\n this.locks.delete(lock.threadId);\n }\n }\n\n async extendLock(lock: Lock, ttlMs: number): Promise<boolean> {\n this.ensureConnected();\n\n const existingLock = this.locks.get(lock.threadId);\n if (!existingLock || existingLock.token !== lock.token) {\n return false;\n }\n\n if (existingLock.expiresAt < Date.now()) {\n // Lock has already expired\n this.locks.delete(lock.threadId);\n return false;\n }\n\n // Extend the lock\n existingLock.expiresAt = Date.now() + ttlMs;\n return true;\n }\n\n async get<T = unknown>(key: string): Promise<T | null> {\n this.ensureConnected();\n\n const cached = this.cache.get(key);\n if (!cached) {\n return null;\n }\n\n // Check if expired\n if (cached.expiresAt !== null && cached.expiresAt <= Date.now()) {\n this.cache.delete(key);\n return null;\n }\n\n return cached.value as T;\n }\n\n async set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void> {\n this.ensureConnected();\n\n this.cache.set(key, {\n value,\n expiresAt: ttlMs ? Date.now() + ttlMs : null,\n });\n }\n\n async delete(key: string): Promise<void> {\n this.ensureConnected();\n this.cache.delete(key);\n }\n\n private ensureConnected(): void {\n if (!this.connected) {\n throw new Error(\n \"MemoryStateAdapter is not connected. Call connect() first.\"\n );\n }\n }\n\n private cleanExpiredLocks(): void {\n const now = Date.now();\n for (const [threadId, lock] of this.locks) {\n if (lock.expiresAt <= now) {\n this.locks.delete(threadId);\n }\n }\n }\n\n // For testing purposes\n _getSubscriptionCount(): number {\n return this.subscriptions.size;\n }\n\n _getLockCount(): number {\n this.cleanExpiredLocks();\n return this.locks.size;\n }\n}\n\nfunction generateToken(): string {\n return `mem_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;\n}\n\nexport function createMemoryState(): MemoryStateAdapter {\n return new MemoryStateAdapter();\n}\n"],"mappings":";AAmBO,IAAM,qBAAN,MAAiD;AAAA,EACrC,gBAAgB,oBAAI,IAAY;AAAA,EAChC,QAAQ,oBAAI,IAAwB;AAAA,EACpC,QAAQ,oBAAI,IAAyB;AAAA,EAC9C,YAAY;AAAA,EACZ,iBAAuC;AAAA,EAE/C,MAAM,UAAyB;AAC7B,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAGA,QAAI,CAAC,KAAK,gBAAgB;AACxB,WAAK,iBAAiB,QAAQ,QAAQ,EAAE,KAAK,MAAM;AACjD,YAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,kBAAQ;AAAA,YACN;AAAA,UAEF;AAAA,QACF;AACA,aAAK,YAAY;AAAA,MACnB,CAAC;AAAA,IACH;AAEA,UAAM,KAAK;AAAA,EACb;AAAA,EAEA,MAAM,aAA4B;AAChC,SAAK,YAAY;AACjB,SAAK,iBAAiB;AACtB,SAAK,cAAc,MAAM;AACzB,SAAK,MAAM,MAAM;AAAA,EACnB;AAAA,EAEA,MAAM,UAAU,UAAiC;AAC/C,SAAK,gBAAgB;AACrB,SAAK,cAAc,IAAI,QAAQ;AAAA,EACjC;AAAA,EAEA,MAAM,YAAY,UAAiC;AACjD,SAAK,gBAAgB;AACrB,SAAK,cAAc,OAAO,QAAQ;AAAA,EACpC;AAAA,EAEA,MAAM,aAAa,UAAoC;AACrD,SAAK,gBAAgB;AACrB,WAAO,KAAK,cAAc,IAAI,QAAQ;AAAA,EACxC;AAAA,EAEA,MAAM,YAAY,UAAkB,OAAqC;AACvE,SAAK,gBAAgB;AACrB,SAAK,kBAAkB;AAGvB,UAAM,eAAe,KAAK,MAAM,IAAI,QAAQ;AAC5C,QAAI,gBAAgB,aAAa,YAAY,KAAK,IAAI,GAAG;AACvD,aAAO;AAAA,IACT;AAG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gBAAgB;AACrB,SAAK,MAAM,OAAO,GAAG;AAAA,EACvB;AAAA,EAEQ,kBAAwB;AAC9B,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,oBAA0B;AAChC,UAAM,MAAM,KAAK,IAAI;AACrB,eAAW,CAAC,UAAU,IAAI,KAAK,KAAK,OAAO;AACzC,UAAI,KAAK,aAAa,KAAK;AACzB,aAAK,MAAM,OAAO,QAAQ;AAAA,MAC5B;AAAA,IACF;AAAA,EACF;AAAA;AAAA,EAGA,wBAAgC;AAC9B,WAAO,KAAK,cAAc;AAAA,EAC5B;AAAA,EAEA,gBAAwB;AACtB,SAAK,kBAAkB;AACvB,WAAO,KAAK,MAAM;AAAA,EACpB;AACF;AAEA,SAAS,gBAAwB;AAC/B,SAAO,OAAO,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,GAAG,EAAE,CAAC;AACzE;AAEO,SAAS,oBAAwC;AACtD,SAAO,IAAI,mBAAmB;AAChC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chat-adapter/state-memory",
|
|
3
|
-
"version": "4.13.
|
|
3
|
+
"version": "4.13.3",
|
|
4
4
|
"description": "In-memory state adapter for chat (development/testing)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -16,16 +16,16 @@
|
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"chat": "4.13.
|
|
19
|
+
"chat": "4.13.3"
|
|
20
20
|
},
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
23
|
-
"url": "git+https://github.com/vercel
|
|
23
|
+
"url": "git+https://github.com/vercel/chat.git",
|
|
24
24
|
"directory": "packages/state-memory"
|
|
25
25
|
},
|
|
26
|
-
"homepage": "https://github.com/vercel
|
|
26
|
+
"homepage": "https://github.com/vercel/chat#readme",
|
|
27
27
|
"bugs": {
|
|
28
|
-
"url": "https://github.com/vercel
|
|
28
|
+
"url": "https://github.com/vercel/chat/issues"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
@@ -46,10 +46,9 @@
|
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup",
|
|
48
48
|
"dev": "tsup --watch",
|
|
49
|
-
"test": "vitest run",
|
|
49
|
+
"test": "vitest run --coverage",
|
|
50
50
|
"test:watch": "vitest",
|
|
51
51
|
"typecheck": "tsc --noEmit",
|
|
52
|
-
"lint": "biome check src",
|
|
53
52
|
"clean": "rm -rf dist"
|
|
54
53
|
}
|
|
55
54
|
}
|