@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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Array-backed Session data and its read-only forwarding view.
|
|
3
|
+
* @module @deepseek-ai/dsh-session-data/data-array
|
|
4
|
+
*/
|
|
5
|
+
import { SessionLogOffset, SessionSeq } from '@deepseek-ai/dsh-session';
|
|
6
|
+
import type { CheckSessionLogResult, SessionEvent, SessionHeader, SessionId } from '@deepseek-ai/dsh-session';
|
|
7
|
+
import type { ReadonlySessionData, SessionData, SessionForkOptions, SessionStateDefinition } from './data.ts';
|
|
8
|
+
/**
|
|
9
|
+
* Array-backed Session data holding all array behavior. The `writable` flag
|
|
10
|
+
* alone separates read-only instances (this class) from {@link ArraySessionData}.
|
|
11
|
+
* `flush` and `close` do nothing.
|
|
12
|
+
*/
|
|
13
|
+
export declare class ReadonlyArraySessionData implements ReadonlySessionData {
|
|
14
|
+
readonly header: SessionHeader;
|
|
15
|
+
readonly inheritedEventCount: SessionLogOffset;
|
|
16
|
+
/** Accepted events; `append` grows them only on writable instances. */
|
|
17
|
+
protected readonly events: SessionEvent[];
|
|
18
|
+
/** Cached immutable full snapshot, reused until the next append. */
|
|
19
|
+
protected eventsSnapshot: readonly SessionEvent[] | undefined;
|
|
20
|
+
private view;
|
|
21
|
+
/** Incremental state evaluations by definition object: the state and how many events it reflects. */
|
|
22
|
+
private readonly folds;
|
|
23
|
+
/**
|
|
24
|
+
* @param log - log from `checkSessionLog`; the data takes ownership of `log.events`.
|
|
25
|
+
*/
|
|
26
|
+
constructor(log: CheckSessionLogResult);
|
|
27
|
+
get seq(): SessionLogOffset;
|
|
28
|
+
get isEmpty(): boolean;
|
|
29
|
+
eventAt(seq: SessionSeq): SessionEvent | undefined;
|
|
30
|
+
snapshotEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): readonly SessionEvent[];
|
|
31
|
+
readEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): Promise<readonly SessionEvent[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Return this data's read-only view: a read-only instance itself, otherwise one cached forwarding view.
|
|
34
|
+
* @returns the same object on every call.
|
|
35
|
+
*/
|
|
36
|
+
readonlyView(): ReadonlySessionData;
|
|
37
|
+
evaluateState<S>(definition: SessionStateDefinition<string, S>): S;
|
|
38
|
+
fork(_childId: SessionId, _options: SessionForkOptions): Promise<SessionData>;
|
|
39
|
+
/** Whether this instance accepts `append`; writable subclasses override it to return true. */
|
|
40
|
+
protected get writable(): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Add committed events to the in-memory log; see {@link SessionData.append}.
|
|
43
|
+
* @param events - validated, deeply frozen events at the next sequence numbers.
|
|
44
|
+
* @throws `HarnessError` with code {@link SESSION_DATA_READONLY} on a read-only instance.
|
|
45
|
+
*/
|
|
46
|
+
append(events: readonly SessionEvent[]): void;
|
|
47
|
+
/** Do nothing: array data owns no write handle; see {@link SessionData.flush}. */
|
|
48
|
+
flush(): Promise<void>;
|
|
49
|
+
/** Do nothing: array data owns no write handle; see {@link SessionData.close}. */
|
|
50
|
+
close(): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
/** Writable array-backed {@link SessionData} without a write handle: {@link ReadonlyArraySessionData} with its `writable` flag set. */
|
|
53
|
+
export declare class ArraySessionData extends ReadonlyArraySessionData implements SessionData {
|
|
54
|
+
protected get writable(): boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Append the tagged inherited marker that constructing a fresh fork child would
|
|
58
|
+
* append, so storage can receive the complete child log before the Session exists.
|
|
59
|
+
* Does nothing for an unseeded header or when the event at the inherited cut is already that marker.
|
|
60
|
+
* @param log - log from `checkSessionLog`; `log.events` grows in place.
|
|
61
|
+
*/
|
|
62
|
+
export declare function appendInheritedSeedMarker(log: CheckSessionLogResult): void;
|
|
63
|
+
//# sourceMappingURL=data-array.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-array.d.ts","sourceRoot":"","sources":["../../src/data-array.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAA4B,gBAAgB,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AACjG,OAAO,KAAK,EAAE,qBAAqB,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AAE7G,OAAO,KAAK,EAAE,mBAAmB,EAAE,WAAW,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAA;AAE7G;;;;GAIG;AACH,qBAAa,wBAAyB,YAAW,mBAAmB;IAClE,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAA;IAC9B,QAAQ,CAAC,mBAAmB,EAAE,gBAAgB,CAAA;IAC9C,uEAAuE;IACvE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,YAAY,EAAE,CAAA;IACzC,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AC,CAAA;AACrC,CAAC"}
|
|
@@ -0,0 +1,247 @@
|
|
|
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
|
+
import { Context, Service } from '@deepseek-ai/cordis';
|
|
7
|
+
import { SessionLogOffset } from '@deepseek-ai/dsh-session';
|
|
8
|
+
import type { SessionEvent, SessionHeader, SessionHeaderMeta, SessionId, SessionSeq } from '@deepseek-ai/dsh-session';
|
|
9
|
+
declare module '@deepseek-ai/cordis' {
|
|
10
|
+
interface Context {
|
|
11
|
+
sessionData: SessionDataService;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* One state derived from a Session log: a key naming the state, the state of
|
|
16
|
+
* the empty log, and a pure transition over committed events. Data evaluates a
|
|
17
|
+
* definition with {@link ReadonlySessionData.evaluateState}, which keeps one
|
|
18
|
+
* incremental fold per definition object.
|
|
19
|
+
*/
|
|
20
|
+
export interface SessionStateDefinition<K extends string, S> {
|
|
21
|
+
/** Name of the state. */
|
|
22
|
+
readonly key: K;
|
|
23
|
+
/**
|
|
24
|
+
* State for the empty log and the Session's immutable metadata.
|
|
25
|
+
* @param header - immutable metadata of the Session.
|
|
26
|
+
* @param inheritedEventCount - exact fork-inherited prefix length.
|
|
27
|
+
* @returns the initial state.
|
|
28
|
+
*/
|
|
29
|
+
readonly init: (header: SessionHeader, inheritedEventCount: SessionLogOffset) => S;
|
|
30
|
+
/**
|
|
31
|
+
* Pure transition from the state covering all prior events and one committed event to the next state.
|
|
32
|
+
* @param state - the state covering all prior events.
|
|
33
|
+
* @param event - the next committed event.
|
|
34
|
+
* @returns the next state; the same reference when the event does not change it.
|
|
35
|
+
*/
|
|
36
|
+
readonly apply: (state: S, event: SessionEvent) => S;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Child header fields a fork caller may set; the fork marks the child as
|
|
40
|
+
* seeded, stamps its creation time, and inherits the source cwd unless `cwd`
|
|
41
|
+
* is provided here. Other fields are never inherited from the source.
|
|
42
|
+
*/
|
|
43
|
+
export type SessionForkMeta = Pick<SessionHeaderMeta, 'cwd' | 'parentSession' | 'origin' | 'delegationDepth' | 'agentPreset'>;
|
|
44
|
+
/** Inputs of {@link ReadonlySessionData.fork}. */
|
|
45
|
+
export interface SessionForkOptions {
|
|
46
|
+
/** Inclusive fork point: the child inherits events `[0..at]`. */
|
|
47
|
+
readonly at: SessionSeq;
|
|
48
|
+
/** Child header fields; omitted `cwd` inherits the source header's cwd. */
|
|
49
|
+
readonly meta?: SessionForkMeta;
|
|
50
|
+
}
|
|
51
|
+
/** Read-only access to one Session's header, inherited cut, and events. */
|
|
52
|
+
export interface ReadonlySessionData {
|
|
53
|
+
/** Validated, deeply frozen header. */
|
|
54
|
+
readonly header: SessionHeader;
|
|
55
|
+
/** Number of leading events inherited from the fork parent. */
|
|
56
|
+
readonly inheritedEventCount: SessionLogOffset;
|
|
57
|
+
/** Sequence number of the next event, equal to the current event count. */
|
|
58
|
+
readonly seq: SessionLogOffset;
|
|
59
|
+
/** Whether the data holds no event, that is, {@link seq} is 0. */
|
|
60
|
+
readonly isEmpty: boolean;
|
|
61
|
+
/** Events read from storage, excluding in-memory interrupted-turn closers; only data opened for reading has it. */
|
|
62
|
+
readonly persistedEventCount?: SessionLogOffset;
|
|
63
|
+
/**
|
|
64
|
+
* Read an immutable snapshot of a half-open event range.
|
|
65
|
+
* @param fromSeq - inclusive start; defaults to 0.
|
|
66
|
+
* @param toSeqExclusive - exclusive end; defaults to {@link seq}.
|
|
67
|
+
* @returns a frozen array of deeply frozen events.
|
|
68
|
+
*/
|
|
69
|
+
readEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): Promise<readonly SessionEvent[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Transitional synchronous random access for the projection registry only.
|
|
72
|
+
* @param seq - event sequence number.
|
|
73
|
+
* @returns the event, or undefined beyond the log.
|
|
74
|
+
*/
|
|
75
|
+
eventAt(seq: SessionSeq): SessionEvent | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Transitional synchronous range read for the projection registry only.
|
|
78
|
+
* @param fromSeq - inclusive start; defaults to 0.
|
|
79
|
+
* @param toSeqExclusive - exclusive end; defaults to {@link seq}.
|
|
80
|
+
* @returns a frozen array; the full-log array is reused until the next append.
|
|
81
|
+
*/
|
|
82
|
+
snapshotEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): readonly SessionEvent[];
|
|
83
|
+
/**
|
|
84
|
+
* Evaluate one state definition over the committed events at the current log
|
|
85
|
+
* end. The first call for a definition object starts from
|
|
86
|
+
* `definition.init(header, inheritedEventCount)` and applies every committed
|
|
87
|
+
* event in order; later calls with the same object apply only the events
|
|
88
|
+
* appended since. Each data object keeps its own folds, a read-only view
|
|
89
|
+
* shares the folds of the data that created it, and the definition object
|
|
90
|
+
* itself identifies the fold, so no registration is required. This is the
|
|
91
|
+
* interim main read of Session state; registered definitions read by key
|
|
92
|
+
* will sit beside it.
|
|
93
|
+
* @param definition - the state's key, initial state, and transition.
|
|
94
|
+
* @returns the state at the log end as the reference the data holds internally,
|
|
95
|
+
* which is not frozen; treat it as read-only.
|
|
96
|
+
*/
|
|
97
|
+
evaluateState<S>(definition: SessionStateDefinition<string, S>): S;
|
|
98
|
+
/**
|
|
99
|
+
* Create writable child data whose log inherits events `[0..at]`, marks the
|
|
100
|
+
* inherited cut, and closes a turn left open at `at` with forked results.
|
|
101
|
+
* Service-backed data creates the child through the `sessionData` service of
|
|
102
|
+
* its context with this Session as the parent source, so storage receives the
|
|
103
|
+
* complete child log at creation; the shared parent prefix ends at `at + 1`,
|
|
104
|
+
* or at {@link ReadonlySessionData.persistedEventCount} when that is smaller.
|
|
105
|
+
* The child inherits the source header's `cwd` unless `options.meta.cwd`
|
|
106
|
+
* overrides it; other header fields come only from `options.meta`.
|
|
107
|
+
* @param childId - identity of the new child Session.
|
|
108
|
+
* @param options - fork point and child header fields.
|
|
109
|
+
* @returns writable child data owned by the caller until handed to `agents.createFromData`.
|
|
110
|
+
* @throws {@link SessionForkError} with code `INVALID_BOUNDARY` when `at` is not an event of this data.
|
|
111
|
+
* @throws `HarnessError` with code {@link SESSION_DATA_SERVICE_UNAVAILABLE} when no `sessionData` service backs the data.
|
|
112
|
+
*/
|
|
113
|
+
fork(childId: SessionId, options: SessionForkOptions): Promise<SessionData>;
|
|
114
|
+
}
|
|
115
|
+
/** Writable Session data: the event log plus the write handle it owns. */
|
|
116
|
+
export interface SessionData extends ReadonlySessionData {
|
|
117
|
+
/**
|
|
118
|
+
* Add committed events to the in-memory log. Only `Session.append` calls this, after validation.
|
|
119
|
+
* @param events - validated, deeply frozen events at the next sequence numbers.
|
|
120
|
+
*/
|
|
121
|
+
append(events: readonly SessionEvent[]): void;
|
|
122
|
+
/**
|
|
123
|
+
* Write the buffered prefix that storage has not received yet: seed events,
|
|
124
|
+
* inherited or resume markers, and synthetic recovery closers. Call this
|
|
125
|
+
* before handing the data to the Session Store and entering its Session. Once
|
|
126
|
+
* the Session is in the store, the persistence backend's `session/event`
|
|
127
|
+
* listener writes live events directly into the same handle; calling
|
|
128
|
+
* `flush()` then can append the same events a second time.
|
|
129
|
+
*/
|
|
130
|
+
flush(): Promise<void>;
|
|
131
|
+
/** Release the write handle; idempotent. The view stays readable. */
|
|
132
|
+
close(): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Return this data's read-only view.
|
|
135
|
+
* @returns the same object on every call; it shares the events and sees later appends.
|
|
136
|
+
*/
|
|
137
|
+
readonlyView(): ReadonlySessionData;
|
|
138
|
+
}
|
|
139
|
+
/** `HarnessError` code with which a read open rejects a stored log that fails Session validation; `cause` is the validation error. */
|
|
140
|
+
export declare const SESSION_DATA_CORRUPT = "SESSION_DATA_CORRUPT";
|
|
141
|
+
/** `HarnessError` code with which read-only Session data rejects `append`. */
|
|
142
|
+
export declare const SESSION_DATA_READONLY = "SESSION_DATA_READONLY";
|
|
143
|
+
/**
|
|
144
|
+
* `HarnessError` code session data rejects a fork with when no `sessionData`
|
|
145
|
+
* service backs it: the core array data, or service-backed data whose context
|
|
146
|
+
* lacks the service.
|
|
147
|
+
*/
|
|
148
|
+
export declare const SESSION_DATA_SERVICE_UNAVAILABLE = "SESSION_DATA_SERVICE_UNAVAILABLE";
|
|
149
|
+
/** Options for {@link SessionDataService.create}. */
|
|
150
|
+
export interface SessionDataCreateOptions {
|
|
151
|
+
/** Initial replay or fork events, contiguous from seq 0. */
|
|
152
|
+
readonly seed?: readonly SessionEvent[];
|
|
153
|
+
/** Exact fork-inherited prefix length; required when `header.isSeeded`. */
|
|
154
|
+
readonly inheritedEventCount?: SessionLogOffset;
|
|
155
|
+
/** Fork parent: storage receives the complete child log, including the inherited marker, at creation. */
|
|
156
|
+
readonly parentSource?: {
|
|
157
|
+
/** Parent Session identity. */
|
|
158
|
+
readonly sessionId: SessionId;
|
|
159
|
+
/** Exclusive parent prefix length within `seed`, excluding read-time repairs. */
|
|
160
|
+
readonly endSeq: SessionLogOffset;
|
|
161
|
+
};
|
|
162
|
+
/** Cancellation forwarded to storage. */
|
|
163
|
+
readonly signal?: AbortSignal;
|
|
164
|
+
}
|
|
165
|
+
/** Header and creation options of a fork child, as {@link SessionDataService.create} receives them. */
|
|
166
|
+
export interface BuildForkCreationResult {
|
|
167
|
+
/** Seeded child header. */
|
|
168
|
+
readonly header: SessionHeader;
|
|
169
|
+
/** Child log, inherited cut, and parent source. */
|
|
170
|
+
readonly options: SessionDataCreateOptions;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Compute the child of {@link ReadonlySessionData.fork} from the source data:
|
|
174
|
+
* validate the fork point, build the seeded child header and the child log
|
|
175
|
+
* (`buildForkSeed`), and the parent source whose shared prefix ends at
|
|
176
|
+
* `at + 1`, or at `source.persistedEventCount` when that is smaller. The child
|
|
177
|
+
* inherits `source.header.cwd` unless `options.meta.cwd` overrides it.
|
|
178
|
+
* @param source - fork source data.
|
|
179
|
+
* @param childId - identity of the new child Session.
|
|
180
|
+
* @param options - fork point and child header fields.
|
|
181
|
+
* @returns what the `sessionData` service creates the child from.
|
|
182
|
+
* @throws {@link SessionForkError} with code `INVALID_BOUNDARY` when `at` is not an event of `source`.
|
|
183
|
+
*/
|
|
184
|
+
export declare function buildForkCreation(source: ReadonlySessionData, childId: SessionId, options: SessionForkOptions): BuildForkCreationResult;
|
|
185
|
+
/** Options for opening stored Session data for writing. */
|
|
186
|
+
export interface SessionDataWriteOptions {
|
|
187
|
+
/** Write access: the data owns the stored Session's write handle. */
|
|
188
|
+
readonly mode: 'write';
|
|
189
|
+
/** Cancellation forwarded to storage open, read, and repair write. */
|
|
190
|
+
readonly signal?: AbortSignal;
|
|
191
|
+
}
|
|
192
|
+
/** Options for opening stored Session data for reading. */
|
|
193
|
+
export interface SessionDataReadOptions {
|
|
194
|
+
/** Read access: the data is a snapshot taken at open and owns no handle. */
|
|
195
|
+
readonly mode: 'read';
|
|
196
|
+
/** Cancellation forwarded to storage open and read. */
|
|
197
|
+
readonly signal?: AbortSignal;
|
|
198
|
+
}
|
|
199
|
+
/** Service (`ctx.sessionData`) that creates and opens {@link SessionData}. */
|
|
200
|
+
export declare abstract class SessionDataService extends Service {
|
|
201
|
+
constructor(ctx: Context);
|
|
202
|
+
/**
|
|
203
|
+
* Validate a new Session's header and seed and create its storage.
|
|
204
|
+
* @param header - complete new header supplied by the caller.
|
|
205
|
+
* @param options - seed, inherited cut, fork parent, and cancellation.
|
|
206
|
+
* @returns writable data owned by the caller, not yet used to construct a Session.
|
|
207
|
+
* @throws when validation or storage creation fails; nothing is left open.
|
|
208
|
+
*/
|
|
209
|
+
create(header: SessionHeader, options?: SessionDataCreateOptions): Promise<SessionData>;
|
|
210
|
+
/**
|
|
211
|
+
* Create new Session data from its identity and optional storage metadata;
|
|
212
|
+
* the service builds the current-format header internally.
|
|
213
|
+
* @param id - new Session identity.
|
|
214
|
+
* @param meta - caller-supplied storage metadata folded into the header.
|
|
215
|
+
* @param options - seed, inherited cut, fork parent, and cancellation.
|
|
216
|
+
* @returns writable data owned by the caller, not yet used to construct a Session.
|
|
217
|
+
* @throws when validation or storage creation fails; nothing is left open.
|
|
218
|
+
*/
|
|
219
|
+
create(id: SessionId, meta?: SessionHeaderMeta, options?: SessionDataCreateOptions): Promise<SessionData>;
|
|
220
|
+
/**
|
|
221
|
+
* Validate a complete header and create provider storage for its data.
|
|
222
|
+
* @param header - complete new header to validate.
|
|
223
|
+
* @param options - seed, inherited cut, fork parent, and cancellation.
|
|
224
|
+
* @returns writable data owned by the caller.
|
|
225
|
+
*/
|
|
226
|
+
protected abstract createData(header: SessionHeader, options: SessionDataCreateOptions): Promise<SessionData>;
|
|
227
|
+
/**
|
|
228
|
+
* Open a stored Session for writing, close an interrupted final turn, and store the closers.
|
|
229
|
+
* @param id - stored Session identity.
|
|
230
|
+
* @param options - access mode and cancellation.
|
|
231
|
+
* @returns writable data owned by the caller.
|
|
232
|
+
* @throws `SessionPersistenceNotFoundError` from `@deepseek-ai/dsh-session-persistence` when no stored
|
|
233
|
+
* Session has `id` or no persistence backend is mounted; other storage and validation errors unchanged.
|
|
234
|
+
*/
|
|
235
|
+
abstract open(id: SessionId, options: SessionDataWriteOptions): Promise<SessionData>;
|
|
236
|
+
/**
|
|
237
|
+
* Open a stored Session for reading: read its complete log, release the read
|
|
238
|
+
* handle, and close an interrupted final turn in memory without storing the closers.
|
|
239
|
+
* @param id - stored Session identity.
|
|
240
|
+
* @param options - access mode and cancellation.
|
|
241
|
+
* @returns a snapshot taken at open.
|
|
242
|
+
* @throws `SessionPersistenceNotFoundError` as for write opens; `HarnessError` with code
|
|
243
|
+
* {@link SESSION_DATA_CORRUPT} when the stored log fails Session validation; other storage errors unchanged.
|
|
244
|
+
*/
|
|
245
|
+
abstract open(id: SessionId, options: SessionDataReadOptions): Promise<ReadonlySessionData>;
|
|
246
|
+
}
|
|
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AC,EAAE,EAAE,MAAM,EAAE;SACtD;KACF,CAAA;AACH,CAAC;AAkBD,8EAA8E;AAC9E,MAAM,OAAgB,kBAAmB,SAAQ,OAAO;IACtD,YAAY,GAAY;QACtB,KAAK,CAAC,GAAG,EAAE,aAAa,CAAC,CAAA;IAC3B,CAAC;IAoBD,MAAM,CACJ,UAAqC,EACrC,aAA4D,EAC5D,OAAkC;QAElC,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO,IAAI,CAAC,UAAU,CACpB,kBAAkB,CAAC,UAAU,EAAE,aAA8C,CAAC,EAC9E,OAAO,IAAI,EAAE,CACd,CAAA;QACH,CAAC;QACD,OAAO,IAAI,CAAC,UAAU,CAAC,UAAU,EAAG,aAAsD,IAAI,EAAE,CAAC,CAAA;IACnG,CAAC;CA6BF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
import './augment-session.ts';
|
|
12
|
+
import './augment.ts';
|
|
13
|
+
export { buildForkCreation, SESSION_DATA_CORRUPT, SESSION_DATA_READONLY, SESSION_DATA_SERVICE_UNAVAILABLE, SessionDataService } from './data.ts';
|
|
14
|
+
export type { BuildForkCreationResult, ReadonlySessionData, SessionData, SessionDataCreateOptions, SessionDataReadOptions, SessionDataWriteOptions, SessionForkMeta, SessionForkOptions, SessionStateDefinition, } from './data.ts';
|
|
15
|
+
export { appendInheritedSeedMarker, ArraySessionData, ReadonlyArraySessionData } from './data-array.ts';
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,sBAAsB,CAAA;AAC7B,OAAO,cAAc,CAAA;AAErB,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAChJ,YAAY,EACV,uBAAuB,EAAE,mBAAmB,EAAE,WAAW,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,uBAAuB,EACpI,eAAe,EAAE,kBAAkB,EAAE,sBAAsB,GAC5D,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,sBAAsB,CAAA;AAC7B,OAAO,cAAc,CAAA;AAErB,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAKhJ,OAAO,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepseek-ai/dsh-session-data",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Session data layer for the DeepSeek Harness: the data views behind ctx.sessions and the abstract ctx.sessionData service that creates and opens them",
|
|
4
|
+
"version": "0.2.3-alpha.4",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/deepseek-ai/deepseek-harness.git",
|
|
11
|
+
"directory": "packages/core/session-data"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"types": "lib/types/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./lib/types/index.d.ts",
|
|
19
|
+
"default": "./lib/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./src/*": "./src/*",
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"lib/index.js",
|
|
26
|
+
"lib/types/**/*.d.ts",
|
|
27
|
+
"lib/types/**/*.d.ts.map",
|
|
28
|
+
"lib/**/*.js.map",
|
|
29
|
+
"src"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@deepseek-ai/cordis": "~4.2.0-alpha.3",
|
|
34
|
+
"@deepseek-ai/dsh-session": "0.2.3-alpha.4"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@deepseek-ai/cordis": "~4.2.0-alpha.3",
|
|
38
|
+
"@deepseek-ai/dsh-agent-loop-testkit": "0.2.3-alpha.4",
|
|
39
|
+
"@deepseek-ai/dsh-brand": "0.2.3-alpha.4",
|
|
40
|
+
"@deepseek-ai/dsh-session": "0.2.3-alpha.4",
|
|
41
|
+
"@deepseek-ai/dsh-session-store": "0.2.3-alpha.4"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@deepseek-ai/dsh-error": "0.2.3-alpha.4",
|
|
45
|
+
"@deepseek-ai/dsh-util-values": "0.2.3-alpha.4"
|
|
46
|
+
}
|
|
6
47
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module augmentation of the Session definition with `readonlyView`, which
|
|
3
|
+
* reads this Session's data. A program that calls the member must load this
|
|
4
|
+
* package.
|
|
5
|
+
* @module @deepseek-ai/dsh-session-data/augment-session
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ReadonlySessionData } from './data.ts'
|
|
9
|
+
|
|
10
|
+
declare module '@deepseek-ai/dsh-session' {
|
|
11
|
+
interface Session {
|
|
12
|
+
/**
|
|
13
|
+
* Return the read-only view of this Session's data: the same object on every
|
|
14
|
+
* call, sharing the log and seeing later appends.
|
|
15
|
+
* @returns the data's view.
|
|
16
|
+
*/
|
|
17
|
+
readonlyView(): ReadonlySessionData
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/augment.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module augmentation of the SessionStore definition with `prepare`, which
|
|
3
|
+
* constructs over Session data. A program that calls the member must load
|
|
4
|
+
* this package.
|
|
5
|
+
* @module @deepseek-ai/dsh-session-data/augment
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { SessionData } from './data.ts'
|
|
9
|
+
import type { Session, SessionConstructionOptions } from '@deepseek-ai/dsh-session'
|
|
10
|
+
|
|
11
|
+
declare module '@deepseek-ai/dsh-session' {
|
|
12
|
+
interface SessionStore {
|
|
13
|
+
/**
|
|
14
|
+
* Construct a store-owned, unentered Session from data that no other Session
|
|
15
|
+
* was constructed from, with the store's message interpreters. Does not
|
|
16
|
+
* flush; the caller retains ownership and pairs this with data flush,
|
|
17
|
+
* `enter`, and `announce`.
|
|
18
|
+
* @param data - writable data; the caller still owns it when this throws.
|
|
19
|
+
* @param options.mode - `create` for new data, `restore` for opened stored data.
|
|
20
|
+
* @returns the store-owned session, not yet entered and read-only until announced.
|
|
21
|
+
* @throws if a session with the data's id already exists or surface replay fails.
|
|
22
|
+
*/
|
|
23
|
+
prepare(data: SessionData, options: SessionConstructionOptions): Session
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The Session interface this package augments. */
|
|
28
|
+
export type { Session }
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Array-backed Session data and its read-only forwarding view.
|
|
3
|
+
* @module @deepseek-ai/dsh-session-data/data-array
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { HarnessError } from '@deepseek-ai/dsh-error'
|
|
7
|
+
import { deepFreeze } from '@deepseek-ai/dsh-util-values'
|
|
8
|
+
import { needsInheritedSeedMarker, SessionLogOffset, SessionSeq } from '@deepseek-ai/dsh-session'
|
|
9
|
+
import type { CheckSessionLogResult, SessionEvent, SessionHeader, SessionId } from '@deepseek-ai/dsh-session'
|
|
10
|
+
import { SESSION_DATA_READONLY, SESSION_DATA_SERVICE_UNAVAILABLE } from './data.ts'
|
|
11
|
+
import type { ReadonlySessionData, SessionData, SessionForkOptions, SessionStateDefinition } from './data.ts'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Array-backed Session data holding all array behavior. The `writable` flag
|
|
15
|
+
* alone separates read-only instances (this class) from {@link ArraySessionData}.
|
|
16
|
+
* `flush` and `close` do nothing.
|
|
17
|
+
*/
|
|
18
|
+
export class ReadonlyArraySessionData implements ReadonlySessionData {
|
|
19
|
+
readonly header: SessionHeader
|
|
20
|
+
readonly inheritedEventCount: SessionLogOffset
|
|
21
|
+
/** Accepted events; `append` grows them only on writable instances. */
|
|
22
|
+
protected readonly events: SessionEvent[]
|
|
23
|
+
/** Cached immutable full snapshot, reused until the next append. */
|
|
24
|
+
protected eventsSnapshot: readonly SessionEvent[] | undefined
|
|
25
|
+
private view: ReadonlySessionData | undefined
|
|
26
|
+
/** Incremental state evaluations by definition object: the state and how many events it reflects. */
|
|
27
|
+
private readonly folds = new WeakMap<object, { state: unknown; seq: number }>()
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param log - log from `checkSessionLog`; the data takes ownership of `log.events`.
|
|
31
|
+
*/
|
|
32
|
+
constructor(log: CheckSessionLogResult) {
|
|
33
|
+
this.header = log.header
|
|
34
|
+
this.inheritedEventCount = log.inheritedEventCount
|
|
35
|
+
this.events = log.events
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get seq(): SessionLogOffset {
|
|
39
|
+
return SessionLogOffset(this.events.length)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get isEmpty(): boolean {
|
|
43
|
+
return this.events.length === 0
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
eventAt(seq: SessionSeq): SessionEvent | undefined {
|
|
47
|
+
return this.events[seq]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
snapshotEvents(
|
|
51
|
+
fromSeq: SessionLogOffset = SessionLogOffset(0),
|
|
52
|
+
toSeqExclusive: SessionLogOffset = this.seq,
|
|
53
|
+
): readonly SessionEvent[] {
|
|
54
|
+
if (fromSeq === 0 && toSeqExclusive === this.events.length) {
|
|
55
|
+
this.eventsSnapshot ??= Object.freeze([...this.events])
|
|
56
|
+
return this.eventsSnapshot
|
|
57
|
+
}
|
|
58
|
+
return Object.freeze(this.events.slice(fromSeq, toSeqExclusive))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
readEvents(
|
|
62
|
+
fromSeq: SessionLogOffset = SessionLogOffset(0),
|
|
63
|
+
toSeqExclusive: SessionLogOffset = this.seq,
|
|
64
|
+
): Promise<readonly SessionEvent[]> {
|
|
65
|
+
return Promise.resolve(this.snapshotEvents(fromSeq, toSeqExclusive))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Return this data's read-only view: a read-only instance itself, otherwise one cached forwarding view.
|
|
70
|
+
* @returns the same object on every call.
|
|
71
|
+
*/
|
|
72
|
+
readonlyView(): ReadonlySessionData {
|
|
73
|
+
if (!this.writable) return this
|
|
74
|
+
return this.view ??= new ReadonlySessionDataView(this)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
evaluateState<S>(definition: SessionStateDefinition<string, S>): S {
|
|
78
|
+
const fold = this.folds.get(definition) ?? { state: definition.init(this.header, this.inheritedEventCount), seq: 0 }
|
|
79
|
+
// The fold only ever holds results of this definition.
|
|
80
|
+
let state = fold.state as S
|
|
81
|
+
for (let seq = fold.seq; seq < this.events.length; seq++) state = definition.apply(state, this.events[seq] as SessionEvent)
|
|
82
|
+
this.folds.set(definition, { state, seq: this.events.length })
|
|
83
|
+
return state
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fork(_childId: SessionId, _options: SessionForkOptions): Promise<SessionData> {
|
|
87
|
+
return Promise.reject(new HarnessError(
|
|
88
|
+
`session "${this.header.id}" data is not backed by a session data service; forking is unavailable`,
|
|
89
|
+
SESSION_DATA_SERVICE_UNAVAILABLE,
|
|
90
|
+
))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Whether this instance accepts `append`; writable subclasses override it to return true. */
|
|
94
|
+
protected get writable(): boolean {
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Add committed events to the in-memory log; see {@link SessionData.append}.
|
|
100
|
+
* @param events - validated, deeply frozen events at the next sequence numbers.
|
|
101
|
+
* @throws `HarnessError` with code {@link SESSION_DATA_READONLY} on a read-only instance.
|
|
102
|
+
*/
|
|
103
|
+
append(events: readonly SessionEvent[]): void {
|
|
104
|
+
if (!this.writable) {
|
|
105
|
+
throw new HarnessError(`session "${this.header.id}" data is read-only`, SESSION_DATA_READONLY)
|
|
106
|
+
}
|
|
107
|
+
for (const event of events) this.events.push(event)
|
|
108
|
+
this.eventsSnapshot = undefined
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Do nothing: array data owns no write handle; see {@link SessionData.flush}. */
|
|
112
|
+
flush(): Promise<void> {
|
|
113
|
+
return Promise.resolve()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Do nothing: array data owns no write handle; see {@link SessionData.close}. */
|
|
117
|
+
close(): Promise<void> {
|
|
118
|
+
return Promise.resolve()
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** View that forwards every read to its source data. */
|
|
123
|
+
class ReadonlySessionDataView implements ReadonlySessionData {
|
|
124
|
+
constructor(private readonly source: ReadonlyArraySessionData) {}
|
|
125
|
+
|
|
126
|
+
get header(): SessionHeader {
|
|
127
|
+
return this.source.header
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get inheritedEventCount(): SessionLogOffset {
|
|
131
|
+
return this.source.inheritedEventCount
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get seq(): SessionLogOffset {
|
|
135
|
+
return this.source.seq
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
get isEmpty(): boolean {
|
|
139
|
+
return this.source.isEmpty
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
readEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): Promise<readonly SessionEvent[]> {
|
|
143
|
+
return this.source.readEvents(fromSeq, toSeqExclusive)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
eventAt(seq: SessionSeq): SessionEvent | undefined {
|
|
147
|
+
return this.source.eventAt(seq)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
snapshotEvents(fromSeq?: SessionLogOffset, toSeqExclusive?: SessionLogOffset): readonly SessionEvent[] {
|
|
151
|
+
return this.source.snapshotEvents(fromSeq, toSeqExclusive)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
evaluateState<S>(definition: SessionStateDefinition<string, S>): S {
|
|
155
|
+
return this.source.evaluateState(definition)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
fork(childId: SessionId, options: SessionForkOptions): Promise<SessionData> {
|
|
159
|
+
return this.source.fork(childId, options)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Writable array-backed {@link SessionData} without a write handle: {@link ReadonlyArraySessionData} with its `writable` flag set. */
|
|
164
|
+
export class ArraySessionData extends ReadonlyArraySessionData implements SessionData {
|
|
165
|
+
protected override get writable(): boolean {
|
|
166
|
+
return true
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Append the tagged inherited marker that constructing a fresh fork child would
|
|
172
|
+
* append, so storage can receive the complete child log before the Session exists.
|
|
173
|
+
* Does nothing for an unseeded header or when the event at the inherited cut is already that marker.
|
|
174
|
+
* @param log - log from `checkSessionLog`; `log.events` grows in place.
|
|
175
|
+
*/
|
|
176
|
+
export function appendInheritedSeedMarker(log: CheckSessionLogResult): void {
|
|
177
|
+
if (!needsInheritedSeedMarker(log.header, log.events, log.inheritedEventCount)) return
|
|
178
|
+
const marker: SessionEvent<'session/end-seed'> = {
|
|
179
|
+
type: 'session/end-seed',
|
|
180
|
+
seq: SessionSeq(log.events.length),
|
|
181
|
+
time: Date.now(),
|
|
182
|
+
data: { inherited: true },
|
|
183
|
+
}
|
|
184
|
+
log.events.push(deepFreeze(marker))
|
|
185
|
+
}
|