dsh-plugin-message-edit 1.0.0 → 1.1.0

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,52 @@
1
+ /** Normalize DSH's live sessions and query snapshots at the host boundary. */
2
+ export function sessionEvents(session) {
3
+ const events = typeof session?.snapshotEvents === 'function'
4
+ ? session.snapshotEvents()
5
+ : session?.events;
6
+ if (!Array.isArray(events)) {
7
+ throw new Error('无法读取 DSH 会话历史:需要 snapshotEvents() 或 events 数组,请检查插件与 DSH 的版本兼容性。');
8
+ }
9
+ return events;
10
+ }
11
+
12
+ /** Read the append-only log length without materializing a modern live log. */
13
+ export function sessionEventCount(session) {
14
+ return typeof session?.snapshotEvents === 'function' && Number.isSafeInteger(session.seq) && session.seq >= 0
15
+ ? session.seq
16
+ : sessionEvents(session).length;
17
+ }
18
+
19
+ /** One stable snapshot for all planning, ancestry, and seed reads in an operation. */
20
+ export function sessionRecord(session) {
21
+ const events = sessionEvents(session);
22
+ const header = session.header ?? session.session;
23
+ if (!header || typeof header.id !== 'string') throw new Error('无法读取 DSH 会话头:缺少会话标识。');
24
+ const inheritedEventCount = session.inheritedEventCount ?? header.seedLength
25
+ ?? (header.isSeeded === true ? undefined : 0);
26
+ if (!Number.isSafeInteger(inheritedEventCount) || inheritedEventCount < 0 || inheritedEventCount > events.length) {
27
+ throw new Error(`会话 ${header.id} 的继承事件边界无效,无法安全创建分支。`);
28
+ }
29
+ return { id: header.id, header, events, inheritedEventCount };
30
+ }
31
+
32
+ /** DSH moved the inherited cut from header.seedLength to a creation option. */
33
+ export function branchSeedOptions(source, inheritedEventCount) {
34
+ return typeof source.header.isSeeded === 'boolean'
35
+ ? { inheritedEventCount, meta: { isSeeded: true } }
36
+ : { meta: { seedLength: inheritedEventCount } };
37
+ }
38
+
39
+ /** Read through the host's restored-session observation API, releasing its lease. */
40
+ export async function readSessionRecord(ctx, sessionId) {
41
+ const live = ctx.sessions.get(sessionId);
42
+ if (live !== undefined) return sessionRecord(live);
43
+ if (typeof ctx.sessionQuery.observeSession === 'function') {
44
+ const observation = await ctx.sessionQuery.observeSession(sessionId, { projectionMode: 'none' });
45
+ try {
46
+ return sessionRecord(observation);
47
+ } finally {
48
+ observation[Symbol.dispose]();
49
+ }
50
+ }
51
+ return sessionRecord(await ctx.sessionQuery.readSession(sessionId));
52
+ }