@simulacra-ai/session 0.0.2 → 0.0.4
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 +31 -34
- package/dist/index.cjs +614 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +364 -0
- package/dist/index.d.ts +364 -5
- package/dist/index.js +574 -3
- package/dist/index.js.map +1 -1
- package/package.json +11 -5
- package/dist/file-session-store.d.ts +0 -60
- package/dist/file-session-store.d.ts.map +0 -1
- package/dist/file-session-store.js +0 -166
- package/dist/file-session-store.js.map +0 -1
- package/dist/in-memory-session-store.d.ts +0 -62
- package/dist/in-memory-session-store.d.ts.map +0 -1
- package/dist/in-memory-session-store.js +0 -73
- package/dist/in-memory-session-store.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/session-manager.d.ts +0 -131
- package/dist/session-manager.d.ts.map +0 -1
- package/dist/session-manager.js +0 -366
- package/dist/session-manager.js.map +0 -1
- package/dist/types.d.ts +0 -114
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Simulacra Session
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The session package makes Simulacra conversations durable. Conversations are stateful, but that state lives in memory and disappears when the process exits. The session manager handles saving, loading, and labeling sessions with pluggable storage backends. Sessions can be resumed across process restarts, forked into branches, and automatically saved as the conversation progresses.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -11,26 +11,37 @@ npm install @simulacra-ai/core @simulacra-ai/session
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import {
|
|
14
|
+
import { Conversation } from "@simulacra-ai/core";
|
|
15
|
+
import { FileSessionStore, SessionManager } from "@simulacra-ai/session";
|
|
15
16
|
|
|
17
|
+
// assuming provider is already configured
|
|
18
|
+
|
|
19
|
+
// create a conversation
|
|
20
|
+
using conversation = new Conversation(provider);
|
|
21
|
+
|
|
22
|
+
// create a session manager backed by the filesystem
|
|
16
23
|
const store = new FileSessionStore("./sessions");
|
|
17
|
-
|
|
24
|
+
using session = new SessionManager(store, conversation);
|
|
18
25
|
|
|
26
|
+
// start a new session
|
|
19
27
|
session.start_new("my first session");
|
|
20
|
-
|
|
21
|
-
|
|
28
|
+
|
|
29
|
+
// conversation happens, messages are saved automatically
|
|
30
|
+
await conversation.prompt("Hello!");
|
|
22
31
|
```
|
|
23
32
|
|
|
24
|
-
With `auto_save` (default `true`), messages are
|
|
33
|
+
With `auto_save` (default `true`), messages are persisted after each model response without any manual save calls.
|
|
25
34
|
|
|
26
35
|
## SessionManager
|
|
27
36
|
|
|
28
|
-
`SessionManager` coordinates a conversation with a storage backend
|
|
37
|
+
The `SessionManager` coordinates a conversation with a storage backend. It handles the full lifecycle of sessions, from creation through saving to disposal.
|
|
29
38
|
|
|
30
39
|
```typescript
|
|
31
40
|
new SessionManager(store, conversation, options?)
|
|
32
41
|
```
|
|
33
42
|
|
|
43
|
+
The constructor accepts the following options.
|
|
44
|
+
|
|
34
45
|
Option|Type|Default|Description
|
|
35
46
|
-|-|-|-
|
|
36
47
|
`auto_save`|`boolean`|`true`|Save after every `message_complete` event
|
|
@@ -38,70 +49,56 @@ Option|Type|Default|Description
|
|
|
38
49
|
|
|
39
50
|
### Methods
|
|
40
51
|
|
|
52
|
+
The session manager exposes the following methods.
|
|
53
|
+
|
|
41
54
|
Method|Description
|
|
42
55
|
-|-
|
|
43
56
|
`start_new(label?)`|Begin a new session, returns the session ID
|
|
44
57
|
`load(id?)`|Load a session by ID, or the most recent if omitted
|
|
45
58
|
`save(metadata?)`|Persist current messages and metadata
|
|
46
|
-
`fork(parent_id, options?)`|Create a child session branching from a parent
|
|
59
|
+
`fork(parent_id, options?)`|Create a child session branching from a parent, returns the new session ID
|
|
47
60
|
`list()`|List all sessions from the store
|
|
48
61
|
`delete(id)`|Remove a session
|
|
49
62
|
`rename(id, label)`|Change a session's label
|
|
50
63
|
|
|
51
64
|
### Events
|
|
52
65
|
|
|
66
|
+
The session manager emits events as sessions are loaded and saved.
|
|
67
|
+
|
|
53
68
|
Event|Payload|When
|
|
54
69
|
-|-|-
|
|
55
70
|
`load`|`{ id, messages }`|Session loaded from store
|
|
56
71
|
`save`|`{ id, messages }`|Session written to store
|
|
72
|
+
`lifecycle_error`|`{ error, operation, context? }`|Infrastructure or lifecycle failure
|
|
57
73
|
`dispose`|(none)|Manager disposed
|
|
58
74
|
|
|
59
75
|
### Auto-Slug
|
|
60
76
|
|
|
61
|
-
When `auto_slug` is enabled,
|
|
77
|
+
When `auto_slug` is enabled, the session manager derives a label from the first ~50 characters of the first user message, trimmed to a word boundary. This runs once on the first save. Explicitly setting a label via `start_new(label)` or `rename()` takes precedence.
|
|
62
78
|
|
|
63
79
|
### Child Sessions
|
|
64
80
|
|
|
65
|
-
|
|
81
|
+
When a child conversation is spawned (via orchestration, checkpoints, or `spawn_child`), the session manager automatically creates a child session backed by a detached fork. Child sessions auto-save independently and are disposed when the child conversation ends. This means orchestration subagents, checkpoint summaries, and other child conversations all get their own persistent session history without any manual setup.
|
|
66
82
|
|
|
67
83
|
Checkpoint children have `auto_slug` disabled since their sessions are internal.
|
|
68
84
|
|
|
69
|
-
##
|
|
85
|
+
## Session Storage
|
|
70
86
|
|
|
71
|
-
|
|
87
|
+
A `SessionStore` is the storage backend that a `SessionManager` reads from and writes to. The store handles listing, loading, saving, and deleting sessions. Two stores are included out of the box.
|
|
72
88
|
|
|
73
|
-
|
|
89
|
+
**FileSessionStore** persists sessions as JSON files on disk. Each session is a single `{id}.json` file. Child session relationships are indexed using hard links under `{parent-id}-forks/` directories.
|
|
74
90
|
|
|
75
91
|
```typescript
|
|
76
|
-
import { FileSessionStore } from "@simulacra-ai/session";
|
|
77
|
-
|
|
78
92
|
const store = new FileSessionStore("./data/sessions");
|
|
79
93
|
```
|
|
80
94
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
### InMemorySessionStore
|
|
84
|
-
|
|
85
|
-
Non-persistent store for testing. Sessions live in a `Map` and are lost on process exit.
|
|
95
|
+
**InMemorySessionStore** is a non-persistent store for testing and development. Sessions live in a `Map` and are lost on process exit.
|
|
86
96
|
|
|
87
97
|
```typescript
|
|
88
|
-
import { InMemorySessionStore } from "@simulacra-ai/session";
|
|
89
|
-
|
|
90
98
|
const store = new InMemorySessionStore();
|
|
91
99
|
```
|
|
92
100
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
Custom storage backends implement the `SessionStore` interface. See the [extensibility guide](EXTENSIBILITY.md) for implementation details and examples.
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
interface SessionStore {
|
|
99
|
-
list(): Promise<SessionMetadata[]>;
|
|
100
|
-
load(id: string): Promise<{ metadata: SessionMetadata; messages: Message[] } | undefined>;
|
|
101
|
-
save(id: string, messages: Message[], metadata?: Partial<SessionMetadata>): Promise<void>;
|
|
102
|
-
delete(id: string): Promise<boolean>;
|
|
103
|
-
}
|
|
104
|
-
```
|
|
101
|
+
Custom storage backends (databases, cloud storage, key-value stores) can be built by implementing the `SessionStore` interface. The [extensibility guide](EXTENSIBILITY.md) covers the interface, implementation notes, and includes a full example.
|
|
105
102
|
|
|
106
103
|
## License
|
|
107
104
|
|