@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.
@@ -0,0 +1,364 @@
1
+ import * as _simulacra_ai_core from '@simulacra-ai/core';
2
+ import { CheckpointState, Message, LifecycleErrorEvent, Conversation } from '@simulacra-ai/core';
3
+
4
+ /**
5
+ * Metadata that describes a conversation session.
6
+ *
7
+ * Sessions can form a tree structure through forking, where a child session
8
+ * can inherit messages from its parent session up to a specific fork point.
9
+ */
10
+ interface SessionMetadata {
11
+ /** The unique identifier for the session. */
12
+ id: string;
13
+ /** ISO 8601 timestamp when the session was created. */
14
+ created_at: string;
15
+ /** ISO 8601 timestamp when the session was last updated. */
16
+ updated_at: string;
17
+ /** The AI provider used in this session (e.g., "anthropic", "openai"). */
18
+ provider?: string;
19
+ /** The model identifier used in this session (e.g., "claude-3-5-sonnet-20241022"). */
20
+ model?: string;
21
+ /** A human-readable label or title for the session. */
22
+ label?: string;
23
+ /** The number of messages stored in this session. */
24
+ message_count: number;
25
+ /** The ID of the parent session if this is a fork. */
26
+ parent_id?: string;
27
+ /** The ID of the last message from the parent session included in this fork. */
28
+ fork_message_id?: string;
29
+ /** Whether this fork is detached and does not inherit parent messages. */
30
+ detached?: boolean;
31
+ /** Whether this session is a checkpoint summarization session. */
32
+ is_checkpoint?: boolean;
33
+ /** The latest checkpoint state for this conversation. */
34
+ checkpoint_state?: CheckpointState;
35
+ }
36
+ /**
37
+ * A storage backend for conversation sessions.
38
+ *
39
+ * Implementations are responsible for persisting sessions and their messages,
40
+ * whether in memory, on disk, or in a database.
41
+ */
42
+ interface SessionStore {
43
+ /**
44
+ * Lists all stored sessions.
45
+ *
46
+ * @returns A promise that resolves to an array of session metadata, typically sorted by last update time.
47
+ */
48
+ list(): Promise<SessionMetadata[]>;
49
+ /**
50
+ * Loads a session by its ID.
51
+ *
52
+ * @param id - The unique identifier of the session to load.
53
+ * @returns A promise that resolves to the session metadata and messages, or undefined if not found.
54
+ */
55
+ load(id: string): Promise<{
56
+ metadata: SessionMetadata;
57
+ messages: Message[];
58
+ } | undefined>;
59
+ /**
60
+ * Saves a session with the given messages and metadata.
61
+ *
62
+ * If the session already exists, it updates the existing entry. If it does not exist, it creates a new one.
63
+ *
64
+ * @param id - The unique identifier of the session.
65
+ * @param messages - The messages to store for this session.
66
+ * @param metadata - Optional partial metadata to merge with existing or create new metadata.
67
+ * @returns A promise that resolves when the save operation is complete.
68
+ */
69
+ save(id: string, messages: Message[], metadata?: Partial<SessionMetadata>): Promise<void>;
70
+ /**
71
+ * Deletes a session by its ID.
72
+ *
73
+ * @param id - The unique identifier of the session to delete.
74
+ * @returns A promise that resolves to true if the session was deleted, false if it was not found.
75
+ */
76
+ delete(id: string): Promise<boolean>;
77
+ }
78
+ /**
79
+ * Configuration options for SessionManager behavior.
80
+ */
81
+ interface SessionManagerOptions {
82
+ /**
83
+ * Whether to automatically save the session after each completed message.
84
+ *
85
+ * @defaultValue true
86
+ */
87
+ auto_save?: boolean;
88
+ /**
89
+ * Whether to automatically generate a label from the first user message.
90
+ *
91
+ * @defaultValue true
92
+ */
93
+ auto_slug?: boolean;
94
+ }
95
+ /**
96
+ * Events emitted by SessionManager instances.
97
+ *
98
+ * Each event key maps to a tuple representing the arguments passed to event listeners.
99
+ */
100
+ interface SessionManagerEvents {
101
+ /** Emitted when a session is loaded. */
102
+ load: [{
103
+ id: string;
104
+ messages: Readonly<Message[]>;
105
+ }];
106
+ /** Emitted when a session is saved. */
107
+ save: [{
108
+ id: string;
109
+ messages: Readonly<Message[]>;
110
+ }];
111
+ /** Emitted when an infrastructure or lifecycle operation fails. */
112
+ lifecycle_error: [LifecycleErrorEvent];
113
+ /** Emitted when the SessionManager is disposed. */
114
+ dispose: [];
115
+ }
116
+
117
+ /**
118
+ * Manages conversation sessions including creation, loading, saving, and forking.
119
+ *
120
+ * The SessionManager acts as a bridge between a Conversation instance and a SessionStore,
121
+ * handling session lifecycle, automatic saving, and session tree management through forking.
122
+ * It supports disposable resource management and event emission for session operations.
123
+ */
124
+ declare class SessionManager {
125
+ #private;
126
+ /**
127
+ * Creates a new SessionManager instance.
128
+ *
129
+ * @param store - The storage backend to use for persisting sessions.
130
+ * @param conversation - The conversation instance to manage.
131
+ * @param options - Optional configuration for session management behavior.
132
+ */
133
+ constructor(store: SessionStore, conversation: Conversation, options?: SessionManagerOptions);
134
+ /**
135
+ * The ID of the currently active session.
136
+ *
137
+ * @returns The session ID if a session is active, otherwise undefined.
138
+ */
139
+ get current_session_id(): string | undefined;
140
+ /**
141
+ * Whether a session is currently loaded.
142
+ *
143
+ * @returns True if a session is active, false otherwise.
144
+ */
145
+ get is_loaded(): boolean;
146
+ /**
147
+ * Disposes of the SessionManager and cleans up resources.
148
+ *
149
+ * This method removes event listeners, disposes child sessions, and emits the dispose event.
150
+ * It is called automatically when the associated conversation is disposed or when using
151
+ * explicit resource management.
152
+ */
153
+ [Symbol.dispose](): void;
154
+ /**
155
+ * Starts a new session with a freshly generated ID.
156
+ *
157
+ * This method clears the conversation history and creates a new session. If auto_save
158
+ * is enabled or a label is provided, the session is immediately saved to the store.
159
+ *
160
+ * @param label - Optional label to assign to the new session.
161
+ * @returns The generated session ID.
162
+ */
163
+ start_new(label?: string): string;
164
+ /**
165
+ * Creates a new session as a fork of an existing parent session.
166
+ *
167
+ * A fork creates a new session that inherits messages from the parent session up to
168
+ * the fork point. If detached, the fork does not inherit any messages from the parent
169
+ * but still maintains a parent reference for organizational purposes.
170
+ *
171
+ * @param parent_session_id - The ID of the session to fork from.
172
+ * @param options - Optional configuration for the fork operation.
173
+ * @param options.detached - Whether to create a detached fork that does not inherit parent messages.
174
+ * @returns A promise that resolves to the generated session ID for the fork.
175
+ */
176
+ fork(parent_session_id: string, options?: {
177
+ detached?: boolean;
178
+ }): Promise<string>;
179
+ /**
180
+ * Loads a session by ID or loads the most recent session if no ID is provided.
181
+ *
182
+ * If no session ID is provided and no sessions exist in the store, this method
183
+ * starts a new session automatically. Loading a session resolves its full message
184
+ * history by recursively following parent references.
185
+ *
186
+ * @param id - The ID of the session to load, or undefined to load the most recent session.
187
+ * @returns A promise that resolves when the session is loaded.
188
+ * @throws {Error} If the specified session ID is not found in the store.
189
+ */
190
+ load(id?: string): Promise<void>;
191
+ /**
192
+ * Saves the current session to the store.
193
+ *
194
+ * This method persists only the messages owned by this session, excluding any messages
195
+ * inherited from parent sessions. If auto_slug is enabled and no label has been set,
196
+ * a label is automatically generated from the first user message.
197
+ *
198
+ * @param metadata - Optional partial metadata to update on the session.
199
+ * @returns A promise that resolves when the save operation is complete.
200
+ * @throws {Error} If no session is currently active.
201
+ */
202
+ save(metadata?: Partial<Pick<SessionMetadata, "label" | "provider" | "model" | "parent_id" | "fork_message_id" | "detached">>): Promise<void>;
203
+ /**
204
+ * Lists all sessions stored in the store.
205
+ *
206
+ * @returns A promise that resolves to an array of session metadata, typically sorted by last update time.
207
+ */
208
+ list(): Promise<SessionMetadata[]>;
209
+ /**
210
+ * Deletes a session from the store.
211
+ *
212
+ * If the deleted session is currently active, the conversation is cleared and the
213
+ * current session is unset.
214
+ *
215
+ * @param id - The ID of the session to delete.
216
+ * @returns A promise that resolves to true if the session was deleted, false if it was not found.
217
+ */
218
+ delete(id: string): Promise<boolean>;
219
+ /**
220
+ * Renames a session by updating its label.
221
+ *
222
+ * @param id - The ID of the session to rename.
223
+ * @param label - The new label to assign to the session.
224
+ * @returns A promise that resolves when the rename operation is complete.
225
+ * @throws {Error} If the specified session ID is not found in the store.
226
+ */
227
+ rename(id: string, label: string): Promise<void>;
228
+ /**
229
+ * Registers an event listener for the specified event.
230
+ *
231
+ * @param event - The name of the event to listen for.
232
+ * @param listener - The callback function to invoke when the event is emitted.
233
+ * @returns This SessionManager instance for method chaining.
234
+ */
235
+ on<E extends keyof SessionManagerEvents>(event: E, listener: (...args: SessionManagerEvents[E]) => void): this;
236
+ /**
237
+ * Removes an event listener for the specified event.
238
+ *
239
+ * @param event - The name of the event to stop listening for.
240
+ * @param listener - The callback function to remove.
241
+ * @returns This SessionManager instance for method chaining.
242
+ */
243
+ off<E extends keyof SessionManagerEvents>(event: E, listener: (...args: SessionManagerEvents[E]) => void): this;
244
+ }
245
+
246
+ /**
247
+ * A file-based implementation of SessionStore.
248
+ *
249
+ * This store persists sessions as JSON files in a specified directory. Each session
250
+ * is stored in a separate file named with its session ID. The store also maintains
251
+ * hard links for fork relationships to enable efficient querying of session trees.
252
+ */
253
+ declare class FileSessionStore implements SessionStore {
254
+ #private;
255
+ /**
256
+ * Creates a new FileSessionStore instance.
257
+ *
258
+ * @param root - The absolute path to the directory where session files will be stored.
259
+ */
260
+ constructor(root: string);
261
+ /**
262
+ * Lists all sessions stored in the file system.
263
+ *
264
+ * Reads all JSON files from the root directory and parses their metadata.
265
+ *
266
+ * @returns A promise that resolves to an array of session metadata, sorted by most recently updated first.
267
+ */
268
+ list(): Promise<SessionMetadata[]>;
269
+ /**
270
+ * Loads a session from the file system.
271
+ *
272
+ * @param id - The unique identifier of the session to load.
273
+ * @returns A promise that resolves to the session metadata and messages, or undefined if not found.
274
+ */
275
+ load(id: string): Promise<{
276
+ metadata: SessionMetadata;
277
+ messages: Message[];
278
+ } | undefined>;
279
+ /**
280
+ * Saves a session to the file system.
281
+ *
282
+ * Creates a new session file if the ID does not exist, or updates an existing file.
283
+ * Automatically updates the updated_at timestamp and message_count. If the session
284
+ * has a parent, creates a hard link in the parent's fork directory for efficient querying.
285
+ *
286
+ * @param id - The unique identifier of the session.
287
+ * @param messages - The messages to store for this session.
288
+ * @param metadata - Optional partial metadata to merge with existing metadata.
289
+ * @returns A promise that resolves when the save operation is complete.
290
+ */
291
+ save(id: string, messages: Message[], metadata?: Partial<SessionMetadata>): Promise<void>;
292
+ /**
293
+ * Deletes a session from the file system.
294
+ *
295
+ * Removes the session file, any hard links in parent fork directories, and the session's
296
+ * own fork directory if it exists.
297
+ *
298
+ * @param id - The unique identifier of the session to delete.
299
+ * @returns A promise that resolves to true if the session was deleted, false if it was not found.
300
+ */
301
+ delete(id: string): Promise<boolean>;
302
+ }
303
+
304
+ /**
305
+ * An in-memory implementation of SessionStore.
306
+ *
307
+ * This store keeps all session data in memory using a Map. Data is not persisted
308
+ * across process restarts. Useful for testing or scenarios where persistence is not required.
309
+ */
310
+ declare class InMemorySessionStore implements SessionStore {
311
+ #private;
312
+ /**
313
+ * Lists all sessions stored in memory.
314
+ *
315
+ * @returns A promise that resolves to an array of session metadata, sorted by most recently updated first.
316
+ */
317
+ list(): Promise<SessionMetadata[]>;
318
+ /**
319
+ * Loads a session from memory.
320
+ *
321
+ * Returns a deep clone of the stored data to prevent external mutations.
322
+ *
323
+ * @param id - The unique identifier of the session to load.
324
+ * @returns A promise that resolves to the session metadata and messages, or undefined if not found.
325
+ */
326
+ load(id: string): Promise<{
327
+ metadata: {
328
+ id: string;
329
+ created_at: string;
330
+ updated_at: string;
331
+ provider?: string;
332
+ model?: string;
333
+ label?: string;
334
+ message_count: number;
335
+ parent_id?: string;
336
+ fork_message_id?: string;
337
+ detached?: boolean;
338
+ is_checkpoint?: boolean;
339
+ checkpoint_state?: _simulacra_ai_core.CheckpointState;
340
+ };
341
+ messages: Message[];
342
+ } | undefined>;
343
+ /**
344
+ * Saves a session to memory.
345
+ *
346
+ * Creates a new session if the ID does not exist, or updates an existing session.
347
+ * Automatically updates the updated_at timestamp and message_count.
348
+ *
349
+ * @param id - The unique identifier of the session.
350
+ * @param messages - The messages to store for this session.
351
+ * @param metadata - Optional partial metadata to merge with existing metadata.
352
+ * @returns A promise that resolves when the save operation is complete.
353
+ */
354
+ save(id: string, messages: Message[], metadata?: Partial<SessionMetadata>): Promise<void>;
355
+ /**
356
+ * Deletes a session from memory.
357
+ *
358
+ * @param id - The unique identifier of the session to delete.
359
+ * @returns A promise that resolves to true if the session was deleted, false if it was not found.
360
+ */
361
+ delete(id: string): Promise<boolean>;
362
+ }
363
+
364
+ export { FileSessionStore, InMemorySessionStore, SessionManager, type SessionManagerEvents, type SessionManagerOptions, type SessionMetadata, type SessionStore };