@sandbox-agent/persist-rivet 0.3.1 → 0.4.0-rc.1
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 +5 -0
- package/dist/index.d.ts +1 -35
- package/dist/index.js +3 -125
- package/dist/index.js.map +1 -1
- package/package.json +4 -18
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @sandbox-agent/persist-rivet
|
|
2
|
+
|
|
3
|
+
> **Deprecated:** This package has been deprecated and removed.
|
|
4
|
+
|
|
5
|
+
Copy the driver source into your project. See the [multiplayer docs](https://github.com/rivet-dev/sandbox-agent/tree/main/docs/multiplayer.mdx) and the [session persistence docs](https://sandboxagent.dev/session-persistence) for guidance.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,36 +1,2 @@
|
|
|
1
|
-
import { SessionRecord, SessionEvent, SessionPersistDriver, ListPageRequest, ListPage, ListEventsRequest } from 'sandbox-agent';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
interface ActorContextLike {
|
|
5
|
-
state: Record<string, unknown>;
|
|
6
|
-
}
|
|
7
|
-
interface RivetPersistData {
|
|
8
|
-
sessions: Record<string, SessionRecord>;
|
|
9
|
-
events: Record<string, SessionEvent[]>;
|
|
10
|
-
}
|
|
11
|
-
type RivetPersistState = {
|
|
12
|
-
_sandboxAgentPersist: RivetPersistData;
|
|
13
|
-
};
|
|
14
|
-
interface RivetSessionPersistDriverOptions {
|
|
15
|
-
/** Maximum number of sessions to retain. Oldest are evicted first. Default: 1024. */
|
|
16
|
-
maxSessions?: number;
|
|
17
|
-
/** Maximum events per session. Oldest are trimmed first. Default: 500. */
|
|
18
|
-
maxEventsPerSession?: number;
|
|
19
|
-
/** Key on `c.state` where persist data is stored. Default: `"_sandboxAgentPersist"`. */
|
|
20
|
-
stateKey?: string;
|
|
21
|
-
}
|
|
22
|
-
declare class RivetSessionPersistDriver implements SessionPersistDriver {
|
|
23
|
-
private readonly maxSessions;
|
|
24
|
-
private readonly maxEventsPerSession;
|
|
25
|
-
private readonly stateKey;
|
|
26
|
-
private readonly ctx;
|
|
27
|
-
constructor(ctx: ActorContextLike, options?: RivetSessionPersistDriverOptions);
|
|
28
|
-
private get data();
|
|
29
|
-
getSession(id: string): Promise<SessionRecord | null>;
|
|
30
|
-
listSessions(request?: ListPageRequest): Promise<ListPage<SessionRecord>>;
|
|
31
|
-
updateSession(session: SessionRecord): Promise<void>;
|
|
32
|
-
listEvents(request: ListEventsRequest): Promise<ListPage<SessionEvent>>;
|
|
33
|
-
insertEvent(event: SessionEvent): Promise<void>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export { type ActorContextLike, type RivetPersistData, type RivetPersistState, RivetSessionPersistDriver, type RivetSessionPersistDriverOptions };
|
|
2
|
+
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,127 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var DEFAULT_STATE_KEY = "_sandboxAgentPersist";
|
|
6
|
-
var RivetSessionPersistDriver = class {
|
|
7
|
-
maxSessions;
|
|
8
|
-
maxEventsPerSession;
|
|
9
|
-
stateKey;
|
|
10
|
-
ctx;
|
|
11
|
-
constructor(ctx, options = {}) {
|
|
12
|
-
this.ctx = ctx;
|
|
13
|
-
this.maxSessions = normalizeCap(options.maxSessions, DEFAULT_MAX_SESSIONS);
|
|
14
|
-
this.maxEventsPerSession = normalizeCap(
|
|
15
|
-
options.maxEventsPerSession,
|
|
16
|
-
DEFAULT_MAX_EVENTS_PER_SESSION
|
|
17
|
-
);
|
|
18
|
-
this.stateKey = options.stateKey ?? DEFAULT_STATE_KEY;
|
|
19
|
-
if (!this.ctx.state[this.stateKey]) {
|
|
20
|
-
this.ctx.state[this.stateKey] = { sessions: {}, events: {} };
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
get data() {
|
|
24
|
-
return this.ctx.state[this.stateKey];
|
|
25
|
-
}
|
|
26
|
-
async getSession(id) {
|
|
27
|
-
const session = this.data.sessions[id];
|
|
28
|
-
return session ? cloneSessionRecord(session) : null;
|
|
29
|
-
}
|
|
30
|
-
async listSessions(request = {}) {
|
|
31
|
-
const sorted = Object.values(this.data.sessions).sort((a, b) => {
|
|
32
|
-
if (a.createdAt !== b.createdAt) {
|
|
33
|
-
return a.createdAt - b.createdAt;
|
|
34
|
-
}
|
|
35
|
-
return a.id.localeCompare(b.id);
|
|
36
|
-
});
|
|
37
|
-
const page = paginate(sorted, request);
|
|
38
|
-
return {
|
|
39
|
-
items: page.items.map(cloneSessionRecord),
|
|
40
|
-
nextCursor: page.nextCursor
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
async updateSession(session) {
|
|
44
|
-
this.data.sessions[session.id] = { ...session };
|
|
45
|
-
if (!this.data.events[session.id]) {
|
|
46
|
-
this.data.events[session.id] = [];
|
|
47
|
-
}
|
|
48
|
-
const ids = Object.keys(this.data.sessions);
|
|
49
|
-
if (ids.length <= this.maxSessions) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const overflow = ids.length - this.maxSessions;
|
|
53
|
-
const removable = Object.values(this.data.sessions).sort((a, b) => {
|
|
54
|
-
if (a.createdAt !== b.createdAt) {
|
|
55
|
-
return a.createdAt - b.createdAt;
|
|
56
|
-
}
|
|
57
|
-
return a.id.localeCompare(b.id);
|
|
58
|
-
}).slice(0, overflow).map((s) => s.id);
|
|
59
|
-
for (const sessionId of removable) {
|
|
60
|
-
delete this.data.sessions[sessionId];
|
|
61
|
-
delete this.data.events[sessionId];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
async listEvents(request) {
|
|
65
|
-
const all = [...this.data.events[request.sessionId] ?? []].sort((a, b) => {
|
|
66
|
-
if (a.eventIndex !== b.eventIndex) {
|
|
67
|
-
return a.eventIndex - b.eventIndex;
|
|
68
|
-
}
|
|
69
|
-
return a.id.localeCompare(b.id);
|
|
70
|
-
});
|
|
71
|
-
const page = paginate(all, request);
|
|
72
|
-
return {
|
|
73
|
-
items: page.items.map(cloneSessionEvent),
|
|
74
|
-
nextCursor: page.nextCursor
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
async insertEvent(event) {
|
|
78
|
-
const events = this.data.events[event.sessionId] ?? [];
|
|
79
|
-
events.push(cloneSessionEvent(event));
|
|
80
|
-
if (events.length > this.maxEventsPerSession) {
|
|
81
|
-
events.splice(0, events.length - this.maxEventsPerSession);
|
|
82
|
-
}
|
|
83
|
-
this.data.events[event.sessionId] = events;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
function cloneSessionRecord(session) {
|
|
87
|
-
return {
|
|
88
|
-
...session,
|
|
89
|
-
sessionInit: session.sessionInit ? JSON.parse(JSON.stringify(session.sessionInit)) : void 0
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
function cloneSessionEvent(event) {
|
|
93
|
-
return {
|
|
94
|
-
...event,
|
|
95
|
-
payload: JSON.parse(JSON.stringify(event.payload))
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
function normalizeCap(value, fallback) {
|
|
99
|
-
if (!Number.isFinite(value) || (value ?? 0) < 1) {
|
|
100
|
-
return fallback;
|
|
101
|
-
}
|
|
102
|
-
return Math.floor(value);
|
|
103
|
-
}
|
|
104
|
-
function paginate(items, request) {
|
|
105
|
-
const offset = parseCursor(request.cursor);
|
|
106
|
-
const limit = normalizeCap(request.limit, DEFAULT_LIST_LIMIT);
|
|
107
|
-
const slice = items.slice(offset, offset + limit);
|
|
108
|
-
const nextOffset = offset + slice.length;
|
|
109
|
-
return {
|
|
110
|
-
items: slice,
|
|
111
|
-
nextCursor: nextOffset < items.length ? String(nextOffset) : void 0
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
function parseCursor(cursor) {
|
|
115
|
-
if (!cursor) {
|
|
116
|
-
return 0;
|
|
117
|
-
}
|
|
118
|
-
const parsed = Number.parseInt(cursor, 10);
|
|
119
|
-
if (!Number.isFinite(parsed) || parsed < 0) {
|
|
120
|
-
return 0;
|
|
121
|
-
}
|
|
122
|
-
return parsed;
|
|
123
|
-
}
|
|
124
|
-
export {
|
|
125
|
-
RivetSessionPersistDriver
|
|
126
|
-
};
|
|
2
|
+
throw new Error(
|
|
3
|
+
"@sandbox-agent/persist-rivet has been deprecated and removed. Copy the reference implementation from docs/multiplayer.mdx into your project instead. See https://github.com/rivet-dev/sandbox-agent/tree/main/docs/multiplayer.mdx"
|
|
4
|
+
);
|
|
127
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["throw new Error(\n \"@sandbox-agent/persist-rivet has been deprecated and removed. \" +\n \"Copy the reference implementation from docs/multiplayer.mdx into your project instead. \" +\n \"See https://github.com/rivet-dev/sandbox-agent/tree/main/docs/multiplayer.mdx\",\n);\n"],"mappings":";AAAA,MAAM,IAAI;AAAA,EACR;AAGF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sandbox-agent/persist-rivet",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Rivet Actor persistence driver for the Sandbox Agent TypeScript SDK",
|
|
3
|
+
"version": "0.4.0-rc.1",
|
|
4
|
+
"description": "Rivet Actor persistence driver for the Sandbox Agent TypeScript SDK (DEPRECATED)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -16,30 +16,16 @@
|
|
|
16
16
|
"import": "./dist/index.js"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"sandbox-agent": "0.3.1"
|
|
21
|
-
},
|
|
22
|
-
"peerDependencies": {
|
|
23
|
-
"rivetkit": ">=0.5.0"
|
|
24
|
-
},
|
|
25
|
-
"peerDependenciesMeta": {
|
|
26
|
-
"rivetkit": {
|
|
27
|
-
"optional": true
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
19
|
"files": [
|
|
31
20
|
"dist"
|
|
32
21
|
],
|
|
33
22
|
"devDependencies": {
|
|
34
23
|
"@types/node": "^22.0.0",
|
|
35
24
|
"tsup": "^8.0.0",
|
|
36
|
-
"typescript": "^5.7.0"
|
|
37
|
-
"vitest": "^3.0.0"
|
|
25
|
+
"typescript": "^5.7.0"
|
|
38
26
|
},
|
|
39
27
|
"scripts": {
|
|
40
28
|
"build": "tsup",
|
|
41
|
-
"typecheck": "tsc --noEmit"
|
|
42
|
-
"test": "vitest run",
|
|
43
|
-
"test:watch": "vitest"
|
|
29
|
+
"typecheck": "tsc --noEmit"
|
|
44
30
|
}
|
|
45
31
|
}
|