@deepseek-ai/dsh-session-data 0.0.0-stage → 0.2.3-alpha.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/LICENSE +21 -0
- package/README.i18n.yaml +52 -0
- package/README.md +127 -2
- package/README.zh.md +128 -0
- package/lib/index.js +217 -0
- package/lib/types/augment-session.d.ts +18 -0
- package/lib/types/augment-session.d.ts.map +1 -0
- package/lib/types/augment-session.js.map +1 -0
- package/lib/types/augment.d.ts +26 -0
- package/lib/types/augment.d.ts.map +1 -0
- package/lib/types/augment.js.map +1 -0
- package/lib/types/data-array.d.ts +63 -0
- package/lib/types/data-array.d.ts.map +1 -0
- package/lib/types/data-array.js.map +1 -0
- package/lib/types/data.d.ts +247 -0
- package/lib/types/data.d.ts.map +1 -0
- package/lib/types/data.js.map +1 -0
- package/lib/types/index.d.ts +16 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/package.json +44 -3
- package/src/augment-session.ts +19 -0
- package/src/augment.ts +28 -0
- package/src/data-array.ts +185 -0
- package/src/data.ts +312 -0
- package/src/index.ts +20 -0
package/src/data.ts
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session data: one Session's header, inherited cut, and event log, and the
|
|
3
|
+
* service that creates and opens it.
|
|
4
|
+
* @module @deepseek-ai/dsh-session-data
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Context, Service } from '@deepseek-ai/cordis'
|
|
8
|
+
import { buildForkSeed, buildSessionHeader, SessionForkError, SessionLogOffset } from '@deepseek-ai/dsh-session'
|
|
9
|
+
import type { SessionEvent, SessionHeader, SessionHeaderMeta, SessionId, SessionSeq } from '@deepseek-ai/dsh-session'
|
|
10
|
+
|
|
11
|
+
declare module '@deepseek-ai/cordis' {
|
|
12
|
+
interface Context {
|
|
13
|
+
sessionData: SessionDataService
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* One state derived from a Session log: a key naming the state, the state of
|
|
19
|
+
* the empty log, and a pure transition over committed events. Data evaluates a
|
|
20
|
+
* definition with {@link ReadonlySessionData.evaluateState}, which keeps one
|
|
21
|
+
* incremental fold per definition object.
|
|
22
|
+
*/
|
|
23
|
+
export interface SessionStateDefinition<K extends string, S> {
|
|
24
|
+
/** Name of the state. */
|
|
25
|
+
readonly key: K
|
|
26
|
+
/**
|
|
27
|
+
* State for the empty log and the Session's immutable metadata.
|
|
28
|
+
* @param header - immutable metadata of the Session.
|
|
29
|
+
* @param inheritedEventCount - exact fork-inherited prefix length.
|
|
30
|
+
* @returns the initial state.
|
|
31
|
+
*/
|
|
32
|
+
readonly init: (header: SessionHeader, inheritedEventCount: SessionLogOffset) => S
|
|
33
|
+
/**
|
|
34
|
+
* Pure transition from the state covering all prior events and one committed event to the next state.
|
|
35
|
+
* @param state - the state covering all prior events.
|
|
36
|
+
* @param event - the next committed event.
|
|
37
|
+
* @returns the next state; the same reference when the event does not change it.
|
|
38
|
+
*/
|
|
39
|
+
readonly apply: (state: S, event: SessionEvent) => S
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Child header fields a fork caller may set; the fork marks the child as
|
|
44
|
+
* seeded, stamps its creation time, and inherits the source cwd unless `cwd`
|
|
45
|
+
* is provided here. Other fields are never inherited from the source.
|
|
46
|
+
*/
|
|
47
|
+
export type SessionForkMeta = Pick<SessionHeaderMeta, 'cwd' | 'parentSession' | 'origin' | 'delegationDepth' | 'agentPreset'>
|
|
48
|
+
|
|
49
|
+
/** Inputs of {@link ReadonlySessionData.fork}. */
|
|
50
|
+
export interface SessionForkOptions {
|
|
51
|
+
/** Inclusive fork point: the child inherits events `[0..at]`. */
|
|
52
|
+
readonly at: SessionSeq
|
|
53
|
+
/** Child header fields; omitted `cwd` inherits the source header's cwd. */
|
|
54
|
+
readonly meta?: SessionForkMeta
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Read-only access to one Session's header, inherited cut, and events. */
|
|
58
|
+
export interface ReadonlySessionData {
|
|
59
|
+
/** Validated, deeply frozen header. */
|
|
60
|
+
readonly header: SessionHeader
|
|
61
|
+
/** Number of leading events inherited from the fork parent. */
|
|
62
|
+
readonly inheritedEventCount: SessionLogOffset
|
|
63
|
+
/** Sequence number of the next event, equal to the current event count. */
|
|
64
|
+
readonly seq: SessionLogOffset
|
|
65
|
+
/** Whether the data holds no event, that is, {@link seq} is 0. */
|
|
66
|
+
readonly isEmpty: boolean
|
|
67
|
+
/** Events read from storage, excluding in-memory interrupted-turn closers; only data opened for reading has it. */
|
|
68
|
+
readonly persistedEventCount?: SessionLogOffset
|
|
69
|
+
/**
|
|
70
|
+
* Read an immutable snapshot of a half-open event range.
|
|
71
|
+
* @param fromSeq - inclusive start; defaults to 0.
|
|
72
|
+
* @param toSeqExclusive - exclusive end; defaults to {@link seq}.
|
|
73
|
+
* @returns a frozen array of deeply frozen events.
|
|
74
|
+
*/
|
|
75
|
+
readEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): Promise<readonly SessionEvent[]>
|
|
76
|
+
/**
|
|
77
|
+
* Transitional synchronous random access for the projection registry only.
|
|
78
|
+
* @param seq - event sequence number.
|
|
79
|
+
* @returns the event, or undefined beyond the log.
|
|
80
|
+
*/
|
|
81
|
+
eventAt(seq: SessionSeq): SessionEvent | undefined
|
|
82
|
+
/**
|
|
83
|
+
* Transitional synchronous range read for the projection registry only.
|
|
84
|
+
* @param fromSeq - inclusive start; defaults to 0.
|
|
85
|
+
* @param toSeqExclusive - exclusive end; defaults to {@link seq}.
|
|
86
|
+
* @returns a frozen array; the full-log array is reused until the next append.
|
|
87
|
+
*/
|
|
88
|
+
snapshotEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): readonly SessionEvent[]
|
|
89
|
+
/**
|
|
90
|
+
* Evaluate one state definition over the committed events at the current log
|
|
91
|
+
* end. The first call for a definition object starts from
|
|
92
|
+
* `definition.init(header, inheritedEventCount)` and applies every committed
|
|
93
|
+
* event in order; later calls with the same object apply only the events
|
|
94
|
+
* appended since. Each data object keeps its own folds, a read-only view
|
|
95
|
+
* shares the folds of the data that created it, and the definition object
|
|
96
|
+
* itself identifies the fold, so no registration is required. This is the
|
|
97
|
+
* interim main read of Session state; registered definitions read by key
|
|
98
|
+
* will sit beside it.
|
|
99
|
+
* @param definition - the state's key, initial state, and transition.
|
|
100
|
+
* @returns the state at the log end as the reference the data holds internally,
|
|
101
|
+
* which is not frozen; treat it as read-only.
|
|
102
|
+
*/
|
|
103
|
+
evaluateState<S>(definition: SessionStateDefinition<string, S>): S
|
|
104
|
+
/**
|
|
105
|
+
* Create writable child data whose log inherits events `[0..at]`, marks the
|
|
106
|
+
* inherited cut, and closes a turn left open at `at` with forked results.
|
|
107
|
+
* Service-backed data creates the child through the `sessionData` service of
|
|
108
|
+
* its context with this Session as the parent source, so storage receives the
|
|
109
|
+
* complete child log at creation; the shared parent prefix ends at `at + 1`,
|
|
110
|
+
* or at {@link ReadonlySessionData.persistedEventCount} when that is smaller.
|
|
111
|
+
* The child inherits the source header's `cwd` unless `options.meta.cwd`
|
|
112
|
+
* overrides it; other header fields come only from `options.meta`.
|
|
113
|
+
* @param childId - identity of the new child Session.
|
|
114
|
+
* @param options - fork point and child header fields.
|
|
115
|
+
* @returns writable child data owned by the caller until handed to `agents.createFromData`.
|
|
116
|
+
* @throws {@link SessionForkError} with code `INVALID_BOUNDARY` when `at` is not an event of this data.
|
|
117
|
+
* @throws `HarnessError` with code {@link SESSION_DATA_SERVICE_UNAVAILABLE} when no `sessionData` service backs the data.
|
|
118
|
+
*/
|
|
119
|
+
fork(childId: SessionId, options: SessionForkOptions): Promise<SessionData>
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Writable Session data: the event log plus the write handle it owns. */
|
|
123
|
+
export interface SessionData extends ReadonlySessionData {
|
|
124
|
+
/**
|
|
125
|
+
* Add committed events to the in-memory log. Only `Session.append` calls this, after validation.
|
|
126
|
+
* @param events - validated, deeply frozen events at the next sequence numbers.
|
|
127
|
+
*/
|
|
128
|
+
append(events: readonly SessionEvent[]): void
|
|
129
|
+
/**
|
|
130
|
+
* Write the buffered prefix that storage has not received yet: seed events,
|
|
131
|
+
* inherited or resume markers, and synthetic recovery closers. Call this
|
|
132
|
+
* before handing the data to the Session Store and entering its Session. Once
|
|
133
|
+
* the Session is in the store, the persistence backend's `session/event`
|
|
134
|
+
* listener writes live events directly into the same handle; calling
|
|
135
|
+
* `flush()` then can append the same events a second time.
|
|
136
|
+
*/
|
|
137
|
+
flush(): Promise<void>
|
|
138
|
+
/** Release the write handle; idempotent. The view stays readable. */
|
|
139
|
+
close(): Promise<void>
|
|
140
|
+
/**
|
|
141
|
+
* Return this data's read-only view.
|
|
142
|
+
* @returns the same object on every call; it shares the events and sees later appends.
|
|
143
|
+
*/
|
|
144
|
+
readonlyView(): ReadonlySessionData
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** `HarnessError` code with which a read open rejects a stored log that fails Session validation; `cause` is the validation error. */
|
|
148
|
+
export const SESSION_DATA_CORRUPT = 'SESSION_DATA_CORRUPT'
|
|
149
|
+
|
|
150
|
+
/** `HarnessError` code with which read-only Session data rejects `append`. */
|
|
151
|
+
export const SESSION_DATA_READONLY = 'SESSION_DATA_READONLY'
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* `HarnessError` code session data rejects a fork with when no `sessionData`
|
|
155
|
+
* service backs it: the core array data, or service-backed data whose context
|
|
156
|
+
* lacks the service.
|
|
157
|
+
*/
|
|
158
|
+
export const SESSION_DATA_SERVICE_UNAVAILABLE = 'SESSION_DATA_SERVICE_UNAVAILABLE'
|
|
159
|
+
|
|
160
|
+
/** Options for {@link SessionDataService.create}. */
|
|
161
|
+
export interface SessionDataCreateOptions {
|
|
162
|
+
/** Initial replay or fork events, contiguous from seq 0. */
|
|
163
|
+
readonly seed?: readonly SessionEvent[]
|
|
164
|
+
/** Exact fork-inherited prefix length; required when `header.isSeeded`. */
|
|
165
|
+
readonly inheritedEventCount?: SessionLogOffset
|
|
166
|
+
/** Fork parent: storage receives the complete child log, including the inherited marker, at creation. */
|
|
167
|
+
readonly parentSource?: {
|
|
168
|
+
/** Parent Session identity. */
|
|
169
|
+
readonly sessionId: SessionId
|
|
170
|
+
/** Exclusive parent prefix length within `seed`, excluding read-time repairs. */
|
|
171
|
+
readonly endSeq: SessionLogOffset
|
|
172
|
+
}
|
|
173
|
+
/** Cancellation forwarded to storage. */
|
|
174
|
+
readonly signal?: AbortSignal
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Header and creation options of a fork child, as {@link SessionDataService.create} receives them. */
|
|
178
|
+
export interface BuildForkCreationResult {
|
|
179
|
+
/** Seeded child header. */
|
|
180
|
+
readonly header: SessionHeader
|
|
181
|
+
/** Child log, inherited cut, and parent source. */
|
|
182
|
+
readonly options: SessionDataCreateOptions
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Compute the child of {@link ReadonlySessionData.fork} from the source data:
|
|
187
|
+
* validate the fork point, build the seeded child header and the child log
|
|
188
|
+
* (`buildForkSeed`), and the parent source whose shared prefix ends at
|
|
189
|
+
* `at + 1`, or at `source.persistedEventCount` when that is smaller. The child
|
|
190
|
+
* inherits `source.header.cwd` unless `options.meta.cwd` overrides it.
|
|
191
|
+
* @param source - fork source data.
|
|
192
|
+
* @param childId - identity of the new child Session.
|
|
193
|
+
* @param options - fork point and child header fields.
|
|
194
|
+
* @returns what the `sessionData` service creates the child from.
|
|
195
|
+
* @throws {@link SessionForkError} with code `INVALID_BOUNDARY` when `at` is not an event of `source`.
|
|
196
|
+
*/
|
|
197
|
+
export function buildForkCreation(
|
|
198
|
+
source: ReadonlySessionData,
|
|
199
|
+
childId: SessionId,
|
|
200
|
+
options: SessionForkOptions,
|
|
201
|
+
): BuildForkCreationResult {
|
|
202
|
+
const { at, meta } = options
|
|
203
|
+
const events = source.snapshotEvents()
|
|
204
|
+
if (at >= events.length) {
|
|
205
|
+
throw new SessionForkError(
|
|
206
|
+
`fork boundary ${at} does not exist in session "${source.header.id}" (last seq: ${events.at(-1)?.seq ?? 'none'})`,
|
|
207
|
+
'INVALID_BOUNDARY',
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
if (events[at]?.seq !== at) {
|
|
211
|
+
throw new SessionForkError(
|
|
212
|
+
`fork boundary ${at} does not match a contiguous event seq in session "${source.header.id}"`,
|
|
213
|
+
'INVALID_BOUNDARY',
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
const endSeq = SessionLogOffset(Math.min(at + 1, source.persistedEventCount ?? at + 1))
|
|
217
|
+
return {
|
|
218
|
+
header: buildSessionHeader(childId, {
|
|
219
|
+
...(source.header.cwd === undefined ? {} : { cwd: source.header.cwd }),
|
|
220
|
+
...meta,
|
|
221
|
+
isSeeded: true,
|
|
222
|
+
}),
|
|
223
|
+
options: {
|
|
224
|
+
seed: buildForkSeed(events, at),
|
|
225
|
+
inheritedEventCount: SessionLogOffset(at + 1),
|
|
226
|
+
parentSource: { sessionId: source.header.id, endSeq },
|
|
227
|
+
},
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Options for opening stored Session data for writing. */
|
|
232
|
+
export interface SessionDataWriteOptions {
|
|
233
|
+
/** Write access: the data owns the stored Session's write handle. */
|
|
234
|
+
readonly mode: 'write'
|
|
235
|
+
/** Cancellation forwarded to storage open, read, and repair write. */
|
|
236
|
+
readonly signal?: AbortSignal
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Options for opening stored Session data for reading. */
|
|
240
|
+
export interface SessionDataReadOptions {
|
|
241
|
+
/** Read access: the data is a snapshot taken at open and owns no handle. */
|
|
242
|
+
readonly mode: 'read'
|
|
243
|
+
/** Cancellation forwarded to storage open and read. */
|
|
244
|
+
readonly signal?: AbortSignal
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Service (`ctx.sessionData`) that creates and opens {@link SessionData}. */
|
|
248
|
+
export abstract class SessionDataService extends Service {
|
|
249
|
+
constructor(ctx: Context) {
|
|
250
|
+
super(ctx, 'sessionData')
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Validate a new Session's header and seed and create its storage.
|
|
255
|
+
* @param header - complete new header supplied by the caller.
|
|
256
|
+
* @param options - seed, inherited cut, fork parent, and cancellation.
|
|
257
|
+
* @returns writable data owned by the caller, not yet used to construct a Session.
|
|
258
|
+
* @throws when validation or storage creation fails; nothing is left open.
|
|
259
|
+
*/
|
|
260
|
+
create(header: SessionHeader, options?: SessionDataCreateOptions): Promise<SessionData>
|
|
261
|
+
/**
|
|
262
|
+
* Create new Session data from its identity and optional storage metadata;
|
|
263
|
+
* the service builds the current-format header internally.
|
|
264
|
+
* @param id - new Session identity.
|
|
265
|
+
* @param meta - caller-supplied storage metadata folded into the header.
|
|
266
|
+
* @param options - seed, inherited cut, fork parent, and cancellation.
|
|
267
|
+
* @returns writable data owned by the caller, not yet used to construct a Session.
|
|
268
|
+
* @throws when validation or storage creation fails; nothing is left open.
|
|
269
|
+
*/
|
|
270
|
+
create(id: SessionId, meta?: SessionHeaderMeta, options?: SessionDataCreateOptions): Promise<SessionData>
|
|
271
|
+
create(
|
|
272
|
+
headerOrId: SessionHeader | SessionId,
|
|
273
|
+
metaOrOptions?: SessionHeaderMeta | SessionDataCreateOptions,
|
|
274
|
+
options?: SessionDataCreateOptions,
|
|
275
|
+
): Promise<SessionData> {
|
|
276
|
+
if (typeof headerOrId === 'string') {
|
|
277
|
+
return this.createData(
|
|
278
|
+
buildSessionHeader(headerOrId, metaOrOptions as SessionHeaderMeta | undefined),
|
|
279
|
+
options ?? {},
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
return this.createData(headerOrId, (metaOrOptions as SessionDataCreateOptions | undefined) ?? {})
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Validate a complete header and create provider storage for its data.
|
|
287
|
+
* @param header - complete new header to validate.
|
|
288
|
+
* @param options - seed, inherited cut, fork parent, and cancellation.
|
|
289
|
+
* @returns writable data owned by the caller.
|
|
290
|
+
*/
|
|
291
|
+
protected abstract createData(header: SessionHeader, options: SessionDataCreateOptions): Promise<SessionData>
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Open a stored Session for writing, close an interrupted final turn, and store the closers.
|
|
295
|
+
* @param id - stored Session identity.
|
|
296
|
+
* @param options - access mode and cancellation.
|
|
297
|
+
* @returns writable data owned by the caller.
|
|
298
|
+
* @throws `SessionPersistenceNotFoundError` from `@deepseek-ai/dsh-session-persistence` when no stored
|
|
299
|
+
* Session has `id` or no persistence backend is mounted; other storage and validation errors unchanged.
|
|
300
|
+
*/
|
|
301
|
+
abstract open(id: SessionId, options: SessionDataWriteOptions): Promise<SessionData>
|
|
302
|
+
/**
|
|
303
|
+
* Open a stored Session for reading: read its complete log, release the read
|
|
304
|
+
* handle, and close an interrupted final turn in memory without storing the closers.
|
|
305
|
+
* @param id - stored Session identity.
|
|
306
|
+
* @param options - access mode and cancellation.
|
|
307
|
+
* @returns a snapshot taken at open.
|
|
308
|
+
* @throws `SessionPersistenceNotFoundError` as for write opens; `HarnessError` with code
|
|
309
|
+
* {@link SESSION_DATA_CORRUPT} when the stored log fails Session validation; other storage errors unchanged.
|
|
310
|
+
*/
|
|
311
|
+
abstract open(id: SessionId, options: SessionDataReadOptions): Promise<ReadonlySessionData>
|
|
312
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session data layer: the read-only and writable views of one Session's
|
|
3
|
+
* header, inherited cut, and event log; the array-backed implementations; and
|
|
4
|
+
* the abstract `ctx.sessionData` service that creates and opens data. A
|
|
5
|
+
* persistence plugin provides the service; this package is not a plugin.
|
|
6
|
+
* Loading it also augments the `Session` and `SessionStore` interfaces with
|
|
7
|
+
* `readonlyView` and the Store's data construction methods.
|
|
8
|
+
*
|
|
9
|
+
* @module @deepseek-ai/dsh-session-data
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import './augment-session.ts'
|
|
13
|
+
import './augment.ts'
|
|
14
|
+
|
|
15
|
+
export { buildForkCreation, SESSION_DATA_CORRUPT, SESSION_DATA_READONLY, SESSION_DATA_SERVICE_UNAVAILABLE, SessionDataService } from './data.ts'
|
|
16
|
+
export type {
|
|
17
|
+
BuildForkCreationResult, ReadonlySessionData, SessionData, SessionDataCreateOptions, SessionDataReadOptions, SessionDataWriteOptions,
|
|
18
|
+
SessionForkMeta, SessionForkOptions, SessionStateDefinition,
|
|
19
|
+
} from './data.ts'
|
|
20
|
+
export { appendInheritedSeedMarker, ArraySessionData, ReadonlyArraySessionData } from './data-array.ts'
|