@agorio/session-redis 0.1.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/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +88 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agorio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @agorio/session-redis
|
|
2
|
+
|
|
3
|
+
Redis-backed `SessionStorage` for the [Agorio SDK](https://www.npmjs.com/package/@agorio/sdk). Durable agent session persistence across process restarts.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @agorio/sdk @agorio/session-redis ioredis
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import Redis from 'ioredis';
|
|
11
|
+
import { ShoppingAgent } from '@agorio/sdk';
|
|
12
|
+
import { RedisSessionStorage } from '@agorio/session-redis';
|
|
13
|
+
|
|
14
|
+
const storage = new RedisSessionStorage({
|
|
15
|
+
redis: new Redis(process.env.REDIS_URL!),
|
|
16
|
+
keyPrefix: 'agorio:sessions:',
|
|
17
|
+
ttlSeconds: 60 * 60 * 24 * 30, // 30-day expiry
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const agent = new ShoppingAgent({
|
|
21
|
+
llm,
|
|
22
|
+
sessionStorage: storage,
|
|
23
|
+
sessionId: 'po-1234', // resumes from this session if it exists
|
|
24
|
+
sessionCustomerId: 'cust-acme',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await agent.run('Order 100 ergonomic chairs from preferred vendors');
|
|
28
|
+
// If the process crashes mid-run, construct the same agent again — it picks
|
|
29
|
+
// up from the last persisted iteration.
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Why durable sessions?
|
|
33
|
+
|
|
34
|
+
Procurement agents pause for human approval. A `submit_payment` over $1k might wait hours for a reviewer to click "approve" — the agent must survive a process restart in that window. `RedisSessionStorage` is the production answer; `MemorySessionStorage` and `FileSessionStorage` (shipped in-tree with the SDK) are the dev / single-process answers.
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @agorio/session-redis — Redis-backed SessionStorage.
|
|
3
|
+
*
|
|
4
|
+
* import Redis from 'ioredis';
|
|
5
|
+
* import { RedisSessionStorage } from '@agorio/session-redis';
|
|
6
|
+
*
|
|
7
|
+
* const storage = new RedisSessionStorage({
|
|
8
|
+
* redis: new Redis(process.env.REDIS_URL!),
|
|
9
|
+
* keyPrefix: 'agorio:sessions:',
|
|
10
|
+
* ttlSeconds: 60 * 60 * 24 * 30, // 30 days
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
* const agent = new ShoppingAgent({ llm, sessionStorage: storage, sessionId: 'po-1234' });
|
|
14
|
+
*
|
|
15
|
+
* The storage layout is one HSET-style JSON document per session, plus
|
|
16
|
+
* a secondary index `<prefix>by_customer:<customerId>` (a SET of session IDs)
|
|
17
|
+
* for filtered list() queries.
|
|
18
|
+
*/
|
|
19
|
+
import type { SessionState, SessionStorage } from '@agorio/sdk';
|
|
20
|
+
/** Minimal Redis client surface — ioredis and node-redis v4 both satisfy this. */
|
|
21
|
+
export interface RedisClientLike {
|
|
22
|
+
get(key: string): Promise<string | null>;
|
|
23
|
+
set(key: string, value: string, ...args: unknown[]): Promise<unknown>;
|
|
24
|
+
del(...keys: string[]): Promise<number>;
|
|
25
|
+
keys(pattern: string): Promise<string[]>;
|
|
26
|
+
mget(...keys: string[]): Promise<Array<string | null>>;
|
|
27
|
+
sadd(key: string, ...members: string[]): Promise<number>;
|
|
28
|
+
srem(key: string, ...members: string[]): Promise<number>;
|
|
29
|
+
smembers(key: string): Promise<string[]>;
|
|
30
|
+
expire?(key: string, seconds: number): Promise<number>;
|
|
31
|
+
}
|
|
32
|
+
export interface RedisSessionStorageOptions {
|
|
33
|
+
redis: RedisClientLike;
|
|
34
|
+
/** Key prefix for session documents. Default: `agorio:sessions:`. */
|
|
35
|
+
keyPrefix?: string;
|
|
36
|
+
/** Optional per-session TTL in seconds. Omit for no expiry. */
|
|
37
|
+
ttlSeconds?: number;
|
|
38
|
+
}
|
|
39
|
+
export declare class RedisSessionStorage implements SessionStorage {
|
|
40
|
+
private readonly redis;
|
|
41
|
+
private readonly keyPrefix;
|
|
42
|
+
private readonly ttlSeconds?;
|
|
43
|
+
constructor(options: RedisSessionStorageOptions);
|
|
44
|
+
save(state: SessionState): Promise<void>;
|
|
45
|
+
load(sessionId: string): Promise<SessionState | null>;
|
|
46
|
+
list(filter?: {
|
|
47
|
+
customerId?: string;
|
|
48
|
+
before?: Date;
|
|
49
|
+
}): Promise<SessionState[]>;
|
|
50
|
+
delete(sessionId: string): Promise<void>;
|
|
51
|
+
private sessionKey;
|
|
52
|
+
private customerIndexKey;
|
|
53
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @agorio/session-redis — Redis-backed SessionStorage.
|
|
3
|
+
*
|
|
4
|
+
* import Redis from 'ioredis';
|
|
5
|
+
* import { RedisSessionStorage } from '@agorio/session-redis';
|
|
6
|
+
*
|
|
7
|
+
* const storage = new RedisSessionStorage({
|
|
8
|
+
* redis: new Redis(process.env.REDIS_URL!),
|
|
9
|
+
* keyPrefix: 'agorio:sessions:',
|
|
10
|
+
* ttlSeconds: 60 * 60 * 24 * 30, // 30 days
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
* const agent = new ShoppingAgent({ llm, sessionStorage: storage, sessionId: 'po-1234' });
|
|
14
|
+
*
|
|
15
|
+
* The storage layout is one HSET-style JSON document per session, plus
|
|
16
|
+
* a secondary index `<prefix>by_customer:<customerId>` (a SET of session IDs)
|
|
17
|
+
* for filtered list() queries.
|
|
18
|
+
*/
|
|
19
|
+
export class RedisSessionStorage {
|
|
20
|
+
redis;
|
|
21
|
+
keyPrefix;
|
|
22
|
+
ttlSeconds;
|
|
23
|
+
constructor(options) {
|
|
24
|
+
if (!options.redis)
|
|
25
|
+
throw new Error('RedisSessionStorage: `redis` client is required');
|
|
26
|
+
this.redis = options.redis;
|
|
27
|
+
this.keyPrefix = options.keyPrefix ?? 'agorio:sessions:';
|
|
28
|
+
this.ttlSeconds = options.ttlSeconds;
|
|
29
|
+
}
|
|
30
|
+
async save(state) {
|
|
31
|
+
const key = this.sessionKey(state.sessionId);
|
|
32
|
+
const json = JSON.stringify(state);
|
|
33
|
+
if (this.ttlSeconds && this.ttlSeconds > 0) {
|
|
34
|
+
await this.redis.set(key, json, 'EX', this.ttlSeconds);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
await this.redis.set(key, json);
|
|
38
|
+
}
|
|
39
|
+
if (state.customerId) {
|
|
40
|
+
await this.redis.sadd(this.customerIndexKey(state.customerId), state.sessionId);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async load(sessionId) {
|
|
44
|
+
const raw = await this.redis.get(this.sessionKey(sessionId));
|
|
45
|
+
if (!raw)
|
|
46
|
+
return null;
|
|
47
|
+
return JSON.parse(raw);
|
|
48
|
+
}
|
|
49
|
+
async list(filter) {
|
|
50
|
+
let sessionIds;
|
|
51
|
+
if (filter?.customerId) {
|
|
52
|
+
sessionIds = await this.redis.smembers(this.customerIndexKey(filter.customerId));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const keys = await this.redis.keys(`${this.keyPrefix}*`);
|
|
56
|
+
const indexPrefix = `${this.keyPrefix}by_customer:`;
|
|
57
|
+
sessionIds = keys
|
|
58
|
+
.filter(k => !k.startsWith(indexPrefix))
|
|
59
|
+
.map(k => k.slice(this.keyPrefix.length));
|
|
60
|
+
}
|
|
61
|
+
if (sessionIds.length === 0)
|
|
62
|
+
return [];
|
|
63
|
+
const docs = await this.redis.mget(...sessionIds.map(id => this.sessionKey(id)));
|
|
64
|
+
const states = [];
|
|
65
|
+
for (const doc of docs) {
|
|
66
|
+
if (!doc)
|
|
67
|
+
continue;
|
|
68
|
+
const state = JSON.parse(doc);
|
|
69
|
+
if (filter?.before && new Date(state.savedAt) >= filter.before)
|
|
70
|
+
continue;
|
|
71
|
+
states.push(state);
|
|
72
|
+
}
|
|
73
|
+
return states;
|
|
74
|
+
}
|
|
75
|
+
async delete(sessionId) {
|
|
76
|
+
const existing = await this.load(sessionId);
|
|
77
|
+
await this.redis.del(this.sessionKey(sessionId));
|
|
78
|
+
if (existing?.customerId) {
|
|
79
|
+
await this.redis.srem(this.customerIndexKey(existing.customerId), sessionId);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
sessionKey(id) {
|
|
83
|
+
return `${this.keyPrefix}${id}`;
|
|
84
|
+
}
|
|
85
|
+
customerIndexKey(customerId) {
|
|
86
|
+
return `${this.keyPrefix}by_customer:${customerId}`;
|
|
87
|
+
}
|
|
88
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agorio/session-redis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Redis-backed SessionStorage for Agorio SDK — durable agent session persistence across process restarts",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"typecheck": "tsc --noEmit",
|
|
14
|
+
"test": "vitest run",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@agorio/sdk": "^0.7.0",
|
|
19
|
+
"ioredis": "^5.4.0"
|
|
20
|
+
},
|
|
21
|
+
"peerDependenciesMeta": {
|
|
22
|
+
"ioredis": {
|
|
23
|
+
"optional": false
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@agorio/sdk": "file:../..",
|
|
28
|
+
"@types/node": "^22.9.0",
|
|
29
|
+
"typescript": "^5.6.3",
|
|
30
|
+
"vitest": "^4.0.17"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"agorio",
|
|
34
|
+
"session",
|
|
35
|
+
"redis",
|
|
36
|
+
"ioredis",
|
|
37
|
+
"shopping-agent",
|
|
38
|
+
"ai-agent",
|
|
39
|
+
"persistence"
|
|
40
|
+
],
|
|
41
|
+
"author": "Agorio",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/Nolpak14/agorio",
|
|
46
|
+
"directory": "packages/session-redis"
|
|
47
|
+
}
|
|
48
|
+
}
|