@unieai/rabi-api-session-controller 0.0.0-stage → 0.2.2-rc.5

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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +7 -2
  3. package/lib/client.js +4 -0
  4. package/lib/index.js +4300 -0
  5. package/lib/typert.host.d.ts +3 -0
  6. package/lib/typert.host.js +5013 -0
  7. package/lib/typert.remote-client.d.ts +115 -0
  8. package/lib/typert.remote-client.js +2714 -0
  9. package/lib/types/agent.d.ts +164 -0
  10. package/lib/types/agent.js +484 -0
  11. package/lib/types/archived-session-gate.d.ts +33 -0
  12. package/lib/types/archived-session-gate.js +54 -0
  13. package/lib/types/assistant-stream.d.ts +26 -0
  14. package/lib/types/assistant-stream.js +82 -0
  15. package/lib/types/catalog.d.ts +24 -0
  16. package/lib/types/catalog.js +107 -0
  17. package/lib/types/client/contract/events.d.ts +102 -0
  18. package/lib/types/client/contract/events.js +111 -0
  19. package/lib/types/client/contract/session.d.ts +178 -0
  20. package/lib/types/client/contract/session.js +1 -0
  21. package/lib/types/client/contract/sessions.d.ts +162 -0
  22. package/lib/types/client/contract/sessions.js +1 -0
  23. package/lib/types/client/contract/snapshot.d.ts +84 -0
  24. package/lib/types/client/contract/snapshot.js +1 -0
  25. package/lib/types/client/index.d.ts +39 -0
  26. package/lib/types/client/index.js +60 -0
  27. package/lib/types/client/ordered-baseline.d.ts +12 -0
  28. package/lib/types/client/ordered-baseline.js +40 -0
  29. package/lib/types/client/scope.d.ts +46 -0
  30. package/lib/types/client/scope.js +46 -0
  31. package/lib/types/client/session-wire-event.d.ts +11 -0
  32. package/lib/types/client/session-wire-event.js +42 -0
  33. package/lib/types/client/sessions/assistant-stream.d.ts +56 -0
  34. package/lib/types/client/sessions/assistant-stream.js +182 -0
  35. package/lib/types/client/sessions/history-records.d.ts +22 -0
  36. package/lib/types/client/sessions/history-records.js +25 -0
  37. package/lib/types/client/sessions/lineage.d.ts +48 -0
  38. package/lib/types/client/sessions/lineage.js +55 -0
  39. package/lib/types/client/sessions/manager.d.ts +272 -0
  40. package/lib/types/client/sessions/manager.js +774 -0
  41. package/lib/types/client/sessions/notifier.d.ts +39 -0
  42. package/lib/types/client/sessions/notifier.js +97 -0
  43. package/lib/types/client/sessions/projection-store.d.ts +128 -0
  44. package/lib/types/client/sessions/projection-store.js +161 -0
  45. package/lib/types/client/sessions/remotes.d.ts +29 -0
  46. package/lib/types/client/sessions/remotes.js +7 -0
  47. package/lib/types/client/sessions/service.d.ts +263 -0
  48. package/lib/types/client/sessions/service.js +597 -0
  49. package/lib/types/client/sessions/session.d.ts +303 -0
  50. package/lib/types/client/sessions/session.js +991 -0
  51. package/lib/types/client/time-zone.d.ts +8 -0
  52. package/lib/types/client/time-zone.js +13 -0
  53. package/lib/types/client/transport.d.ts +77 -0
  54. package/lib/types/client/transport.js +130 -0
  55. package/lib/types/commands.d.ts +92 -0
  56. package/lib/types/commands.js +575 -0
  57. package/lib/types/continuation.d.ts +35 -0
  58. package/lib/types/continuation.js +47 -0
  59. package/lib/types/control.d.ts +20 -0
  60. package/lib/types/control.js +106 -0
  61. package/lib/types/file-references.d.ts +27 -0
  62. package/lib/types/file-references.js +68 -0
  63. package/lib/types/history/assistant-stream.d.ts +43 -0
  64. package/lib/types/history/assistant-stream.js +79 -0
  65. package/lib/types/history/controller.d.ts +104 -0
  66. package/lib/types/history/controller.js +293 -0
  67. package/lib/types/history/listener.d.ts +105 -0
  68. package/lib/types/history/listener.js +296 -0
  69. package/lib/types/history.d.ts +58 -0
  70. package/lib/types/history.js +152 -0
  71. package/lib/types/index.d.ts +262 -0
  72. package/lib/types/index.js +693 -0
  73. package/lib/types/list.d.ts +62 -0
  74. package/lib/types/list.js +343 -0
  75. package/lib/types/media-references.d.ts +18 -0
  76. package/lib/types/media-references.js +84 -0
  77. package/lib/types/remote-events.d.ts +8 -0
  78. package/lib/types/remote-events.js +1 -0
  79. package/lib/types/skill-catalog.d.ts +28 -0
  80. package/lib/types/skill-catalog.js +203 -0
  81. package/lib/types/state/controller.d.ts +45 -0
  82. package/lib/types/state/controller.js +288 -0
  83. package/lib/types/state/listener.d.ts +40 -0
  84. package/lib/types/state/listener.js +111 -0
  85. package/lib/types/types.d.ts +687 -0
  86. package/lib/types/types.js +5 -0
  87. package/lib/types/web/controller.d.ts +25 -0
  88. package/lib/types/web/controller.js +138 -0
  89. package/locale/de.json +6 -0
  90. package/locale/en.json +5 -0
  91. package/locale/es.json +6 -0
  92. package/locale/fr.json +6 -0
  93. package/locale/ja.json +6 -0
  94. package/locale/pt.json +6 -0
  95. package/locale/ru.json +6 -0
  96. package/locale/zh.json +6 -0
  97. package/package.json +119 -4
  98. package/src/agent.ts +474 -0
  99. package/src/archived-session-gate.ts +59 -0
  100. package/src/assistant-stream.ts +102 -0
  101. package/src/catalog.ts +117 -0
  102. package/src/client/contract/events.ts +205 -0
  103. package/src/client/contract/session.ts +174 -0
  104. package/src/client/contract/sessions.ts +169 -0
  105. package/src/client/contract/snapshot.ts +94 -0
  106. package/src/client/index.ts +149 -0
  107. package/src/client/ordered-baseline.ts +43 -0
  108. package/src/client/scope.ts +76 -0
  109. package/src/client/session-wire-event.ts +46 -0
  110. package/src/client/sessions/assistant-stream.ts +229 -0
  111. package/src/client/sessions/history-records.ts +35 -0
  112. package/src/client/sessions/lineage.ts +96 -0
  113. package/src/client/sessions/manager.ts +857 -0
  114. package/src/client/sessions/notifier.ts +97 -0
  115. package/src/client/sessions/projection-store.ts +244 -0
  116. package/src/client/sessions/remotes.ts +44 -0
  117. package/src/client/sessions/service.ts +741 -0
  118. package/src/client/sessions/session.ts +1055 -0
  119. package/src/client/time-zone.ts +14 -0
  120. package/src/client/transport.ts +247 -0
  121. package/src/commands.ts +602 -0
  122. package/src/continuation.ts +58 -0
  123. package/src/control.ts +121 -0
  124. package/src/file-references.ts +42 -0
  125. package/src/history/assistant-stream.ts +102 -0
  126. package/src/history/controller.ts +256 -0
  127. package/src/history/listener.ts +302 -0
  128. package/src/history.ts +189 -0
  129. package/src/index.ts +651 -0
  130. package/src/list.ts +382 -0
  131. package/src/media-references.ts +84 -0
  132. package/src/remote-events.ts +14 -0
  133. package/src/skill-catalog.ts +108 -0
  134. package/src/state/controller.ts +221 -0
  135. package/src/state/listener.ts +116 -0
  136. package/src/types.ts +720 -0
  137. package/src/web/controller.ts +78 -0
@@ -0,0 +1,97 @@
1
+ import { notifySubscribers } from '@unieai/rabi-client-store'
2
+
3
+ /**
4
+ * Batches structural updates in microtasks and stream updates by animation
5
+ * frame. Reads may rebuild a dirty snapshot without consuming the pending
6
+ * subscriber notification.
7
+ */
8
+ export class Notifier {
9
+ private listeners = new Set<() => void>()
10
+ private dirty = false
11
+ private notifyPending = false
12
+ private scheduled: 'none' | 'microtask' | 'frame' = 'none'
13
+ private scheduleGeneration = 0
14
+
15
+ /** @param rebuild - snapshot rebuild function injected by the owner (writes the owner's snapshotCache). */
16
+ constructor(private readonly rebuild: () => void) {}
17
+
18
+ /**
19
+ * uSES subscription entry.
20
+ * @param listener - change callback.
21
+ * @returns the unsubscribe function.
22
+ */
23
+ subscribe(listener: () => void): () => void {
24
+ this.listeners.add(listener)
25
+ return () => {
26
+ this.listeners.delete(listener)
27
+ }
28
+ }
29
+
30
+ /** Mark the snapshot dirty and notify in a microtask. */
31
+ markDirty(): void {
32
+ this.dirty = true
33
+ this.notifyPending = true
34
+ if (this.scheduled === 'microtask') return
35
+ this.schedule('microtask')
36
+ }
37
+
38
+ /** Mark the snapshot dirty and publish cumulative state at most once per frame. */
39
+ markFrameDirty(): void {
40
+ this.dirty = true
41
+ this.notifyPending = true
42
+ if (this.scheduled !== 'none') return
43
+ this.schedule(typeof globalThis.requestAnimationFrame === 'function' ? 'frame' : 'microtask')
44
+ }
45
+
46
+ /**
47
+ * Synchronous flush: controlled-input writes must notify in the same tick as
48
+ * onChange, or React rolls the DOM back to the stale value and the caret jumps to the end.
49
+ */
50
+ notifyNow(): void {
51
+ this.dirty = true
52
+ this.notifyPending = true
53
+ this.invalidateSchedule()
54
+ this.flush()
55
+ }
56
+
57
+ /**
58
+ * Pre-getSnapshot check: rebuild synchronously when dirty (read path
59
+ * before first subscribe / while unobserved). Notification stays pending.
60
+ */
61
+ ensureFresh(): void {
62
+ if (!this.dirty) return
63
+ this.dirty = false
64
+ this.rebuild()
65
+ }
66
+
67
+ private schedule(kind: 'microtask' | 'frame'): void {
68
+ const generation = ++this.scheduleGeneration
69
+ this.scheduled = kind
70
+ const publish = () => {
71
+ if (generation !== this.scheduleGeneration) return
72
+ this.scheduled = 'none'
73
+ this.flush()
74
+ }
75
+ if (kind === 'frame') {
76
+ globalThis.requestAnimationFrame(publish)
77
+ } else {
78
+ queueMicrotask(publish)
79
+ }
80
+ }
81
+
82
+ private invalidateSchedule(): void {
83
+ this.scheduleGeneration++
84
+ this.scheduled = 'none'
85
+ }
86
+
87
+ private flush(): void {
88
+ if (!this.notifyPending) return
89
+ if (this.listeners.size === 0) return // lazy: dirty (if still set) rebuilds on next getSnapshot
90
+ this.notifyPending = false
91
+ if (this.dirty) {
92
+ this.dirty = false
93
+ this.rebuild()
94
+ }
95
+ notifySubscribers(this.listeners, '[session-controller]')
96
+ }
97
+ }
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Generic per-session projection value store (push model; see the
3
+ * session-projection subsystem page, docs/subsystems/session-projection.md):
4
+ * the host is the only computation site; the client holds finished whole
5
+ * values per key in one of two row kinds. A `sequenced` row
6
+ * (`{ value, seq }`) comes from the connected Host — a follow opening
7
+ * baseline, a Session Controller `projection` frame, a list block the Host
8
+ * computed for an attached Session — and merges under **higher seq wins**
9
+ * against other sequenced rows. A `cached` row (`{ value }`) comes from
10
+ * a Host view of the persisted checkpoint, carries no comparable
11
+ * seq, and yields to every sequenced write. No client-side domain folding
12
+ * exists: a domain ships projection support with zero client code. Per-key
13
+ * bare observable faces feed `useProjection` (ui-renderer binds them).
14
+ */
15
+ import type { SessionProjectionMap } from '@unieai/rabi-session-projection/types'
16
+ import type { SessionSeqCursor } from '@unieai/rabi-session/types'
17
+ import type { ObservableSnapshot } from '@unieai/rabi-client-store'
18
+ import { Notifier } from './notifier.ts'
19
+
20
+ // The single projection type table, typed end to end (host unit, wire block,
21
+ // client store, React hook) — the Service Definition package's pure-type outlet
22
+ // (`/types`, zero imports), never the package root: the root's rabi-agent →
23
+ // rabi-session chain would drag the host `Context.sessions` merge into the
24
+ // client program (one program must not hold both sides). No second
25
+ // client-side "views" table (rejected in the Alternatives of
26
+ // .agents/notes/proposed/architecture/2026-07-27-session-projection-and-command-log.md).
27
+ export type { SessionProjectionMap } from '@unieai/rabi-session-projection/types'
28
+
29
+ /**
30
+ * The fifth framework hook seat (see the session-projection subsystem page,
31
+ * docs/subsystems/session-projection.md): key-addressed
32
+ * projection reader delivered through the standard kit. `undefined` uniformly
33
+ * means capability absent — host unit unmounted, or no baseline/frame has
34
+ * carried the key yet. The selector overload mirrors useSession (per-key uSES
35
+ * binding; reference stability holds because a key's value reference changes
36
+ * only when a frame or baseline lands).
37
+ */
38
+ export type UseProjection = {
39
+ <K extends Extract<keyof SessionProjectionMap, string>>(key: K): SessionProjectionMap[K] | undefined
40
+ <K extends Extract<keyof SessionProjectionMap, string>, S>(
41
+ key: K,
42
+ selector: (value: SessionProjectionMap[K] | undefined) => S,
43
+ eq?: (a: S, b: S) => boolean,
44
+ ): S
45
+ }
46
+
47
+ /**
48
+ * Follow-opening projection baseline, restated here so the
49
+ * React-free store depends only on the type table, not the wire package's
50
+ * response vocabulary.
51
+ */
52
+ export interface ProjectionsBaseline {
53
+ /** The consistent-cut seq (equals the window tail seq by construction). */
54
+ asOfSeq: SessionSeqCursor
55
+ /** Whole current values by key; a registered key absent here means the capability is absent. */
56
+ values: Readonly<Record<string, unknown>>
57
+ }
58
+
59
+ /**
60
+ * One key's row. A `sequenced` row was computed by the Host for this Session
61
+ * within the current connection generation and carries the seq it is
62
+ * consistent with; seqs of sequenced rows are comparable with each other. A
63
+ * `cached` row was viewed from the persisted projection checkpoint by a
64
+ * header-only listing without a Session: its origin cannot vouch that its
65
+ * stored seq is comparable with the log the client later opens, so it carries
66
+ * none and yields to every sequenced write.
67
+ */
68
+ type Row =
69
+ | { kind: 'cached'; value: unknown }
70
+ | { kind: 'sequenced'; value: unknown; seq: SessionSeqCursor }
71
+
72
+ /** Per-key notification channel: the bare face plus its batching notifier. */
73
+ interface Channel {
74
+ face: ObservableSnapshot<unknown>
75
+ notifier: Notifier
76
+ }
77
+
78
+ /**
79
+ * One session's projection values. Framework semantics, uniform across every
80
+ * key. Sequenced writes (a baseline seeds rows at its cut, a push frame
81
+ * updates one row) compare seqs among themselves: a lower-or-equal seq within
82
+ * the Host generation loses, so a replayed frame cannot regress a value and a
83
+ * stale baseline cannot overwrite a newer frame. Cached writes (the session
84
+ * list's zero-I/O block) only fill keys no sequenced row holds, and a baseline
85
+ * discards every cached row before it seeds, regardless of seq: the connected
86
+ * Session is the truth and a cached value never outranks it. A key the store
87
+ * has never seen reads `undefined` (capability absent). Faces are identity-stable
88
+ * per key (create-on-demand, cached) so the React side binds each exactly
89
+ * once; the store-level channel (`subscribeAny`) serves coarse consumers (the
90
+ * manager's list projection reads the `title` key).
91
+ */
92
+ export class ProjectionValueStore {
93
+ private readonly rows = new Map<string, Row>()
94
+ private readonly channels = new Map<string, Channel>()
95
+ private valuesCache: Readonly<Partial<SessionProjectionMap>> | undefined
96
+ /** Coarse any-key channel (no snapshot cache to rebuild: reads hit rows directly). */
97
+ private readonly anyNotifier = new Notifier(() => {})
98
+
99
+ /**
100
+ * Key-addressed bare observable face (the useProjection resolution path).
101
+ * Always defined — absence is an `undefined` snapshot, never a missing
102
+ * face, so a component may subscribe before the key ever carries a value.
103
+ * @param key - projection key.
104
+ * @returns the identity-stable face for this key.
105
+ */
106
+ faceOf(key: string): ObservableSnapshot<unknown> {
107
+ return this.channel(key).face
108
+ }
109
+
110
+ /**
111
+ * Current whole value for a key (erased framework read; typed reads go
112
+ * through `useProjection`'s map lookup).
113
+ * @param key - projection key.
114
+ * @returns the value, or undefined while the key is absent.
115
+ */
116
+ get(key: string): unknown {
117
+ return this.rows.get(key)?.value
118
+ }
119
+
120
+ /**
121
+ * Read the accepted Host watermark without subscribing or copying a value.
122
+ * @param key - projection key.
123
+ * @returns the current sequence, or undefined for absent and cached values.
124
+ */
125
+ seqOf(key: string): SessionSeqCursor | undefined {
126
+ const row = this.rows.get(key)
127
+ return row?.kind === 'sequenced' ? row.seq : undefined
128
+ }
129
+
130
+ /**
131
+ * Read every current projection value as one reference-stable snapshot.
132
+ * @returns The same frozen value map until a row changes.
133
+ */
134
+ values(): Readonly<Partial<SessionProjectionMap>> {
135
+ if (this.valuesCache === undefined) {
136
+ this.valuesCache = Object.freeze(Object.fromEntries(
137
+ [...this.rows].map(([key, row]) => [key, row.value]),
138
+ ))
139
+ }
140
+ return this.valuesCache
141
+ }
142
+
143
+ /**
144
+ * Subscribe to any-key changes (microtask-batched) — the manager's list
145
+ * rebuild channel.
146
+ * @param listener - change callback.
147
+ * @returns the unsubscribe function.
148
+ */
149
+ subscribeAny(listener: () => void): () => void {
150
+ return this.anyNotifier.subscribe(listener)
151
+ }
152
+
153
+ /**
154
+ * Apply one finished value from the Session control stream.
155
+ * @param key - projection key.
156
+ * @param value - whole value computed by the host unit.
157
+ * @param seq - the unit's watermark at emission.
158
+ */
159
+ apply(key: string, value: unknown, seq: SessionSeqCursor): void {
160
+ const row = this.rows.get(key)
161
+ // higher seq wins among sequenced rows; replays and stale frames drop. A
162
+ // cached row has no comparable seq and always yields.
163
+ if (row?.kind === 'sequenced' && seq <= row.seq) return
164
+ this.rows.set(key, { kind: 'sequenced', value, seq })
165
+ this.changed(key)
166
+ }
167
+
168
+ /**
169
+ * Fill keys from a Host block labeled `cached`: a zero-I/O
170
+ * view of the persisted checkpoint. A cached value lands only where no
171
+ * sequenced row exists: a connected Session has already answered for such
172
+ * a key, and the list's view of the persisted checkpoint cannot be newer
173
+ * than it.
174
+ * @param values - whole values by key viewed from the persisted checkpoint.
175
+ */
176
+ applyCached(values: Readonly<Record<string, unknown>>): void {
177
+ for (const key of Object.keys(values)) {
178
+ if (this.rows.get(key)?.kind === 'sequenced') continue
179
+ this.rows.set(key, { kind: 'cached', value: values[key] })
180
+ this.changed(key)
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Seed from a history tail page's projections block. Every cached row is
186
+ * discarded first, regardless of seq: the block comes from the connected
187
+ * Session, and a value viewed from the persisted checkpoint never outranks
188
+ * it. Then every carried key lands under the same seq rule as frames, and a
189
+ * key the block omits is capability-absent as of the cut — its row clears
190
+ * unless a newer frame already superseded the cut (a stale baseline can
191
+ * neither overwrite nor clear newer sequenced values).
192
+ * @param baseline - the response's projections block.
193
+ */
194
+ seed(baseline: ProjectionsBaseline): void {
195
+ for (const [key, row] of this.rows) {
196
+ if (row.kind !== 'cached') continue
197
+ this.rows.delete(key)
198
+ this.changed(key)
199
+ }
200
+ // Erased walk: the framework crosses the open key space; per-key typing
201
+ // is re-established at the consumer (useProjection's map lookup).
202
+ const values = baseline.values as Record<string, unknown>
203
+ for (const key of Object.keys(values)) this.apply(key, values[key], baseline.asOfSeq)
204
+ for (const [key, row] of this.rows) {
205
+ if (Object.hasOwn(values, key)) continue
206
+ // Every cached row was deleted above; the kind test only narrows the
207
+ // type so `row.seq` is readable.
208
+ if (row.kind === 'sequenced' && row.seq > baseline.asOfSeq) continue
209
+ this.rows.delete(key)
210
+ this.changed(key)
211
+ }
212
+ }
213
+
214
+ /** Discard one Host generation's values and watermarks while preserving subscribed faces. */
215
+ clear(): void {
216
+ for (const key of this.rows.keys()) {
217
+ this.rows.delete(key)
218
+ this.changed(key)
219
+ }
220
+ }
221
+
222
+ private changed(key: string): void {
223
+ this.valuesCache = undefined
224
+ this.channels.get(key)?.notifier.markDirty()
225
+ this.anyNotifier.markDirty()
226
+ }
227
+
228
+ private channel(key: string): Channel {
229
+ let channel = this.channels.get(key)
230
+ if (channel === undefined) {
231
+ // The notifier only batches (no snapshot cache to rebuild: faces read rows directly).
232
+ const notifier = new Notifier(() => {})
233
+ channel = {
234
+ notifier,
235
+ face: {
236
+ getSnapshot: () => this.rows.get(key)?.value,
237
+ subscribe: listener => notifier.subscribe(listener),
238
+ },
239
+ }
240
+ this.channels.set(key, channel)
241
+ }
242
+ return channel
243
+ }
244
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Remote namespaces the Session cluster calls. One parameter for one concept:
3
+ * the generated surface a Session and its manager reach the Host through.
4
+ *
5
+ * @module @unieai/rabi-api-session-controller/client/sessions/remotes
6
+ */
7
+
8
+ import type { ClientRemote } from '@unieai/rabi-api-gateway/client'
9
+ import type { CommandSubmitAttachment } from '@unieai/rabi-commands/types'
10
+ import type { SessionId } from '@unieai/rabi-session/types'
11
+ import type {
12
+ SubagentInterruptReceipt, SubagentPromptReceipt, SubagentPromptRequest,
13
+ } from '@unieai/rabi-subagent/client'
14
+ import type { RemoteResult } from '@unieai/rabi-typert-protocol'
15
+ import type { SessionRemote } from '../transport.ts'
16
+
17
+ /** Narrow Commands namespace consumed by a Client Session. */
18
+ export interface SessionCommandsRemote {
19
+ execute(
20
+ agentId: SessionId,
21
+ line: string,
22
+ attachments: readonly CommandSubmitAttachment[],
23
+ signal?: AbortSignal,
24
+ ): Promise<RemoteResult<object | undefined>>
25
+ }
26
+
27
+ /** Narrow subagent namespace consumed by a Client Session and its manager. */
28
+ export interface SessionSubagentsRemote {
29
+ prompt(
30
+ request: SubagentPromptRequest,
31
+ signal?: AbortSignal,
32
+ ): Promise<RemoteResult<SubagentPromptReceipt>>
33
+ interruptByUser(
34
+ childSessionId: SessionId,
35
+ ): Promise<RemoteResult<SubagentInterruptReceipt>>
36
+ }
37
+
38
+ /** Generated Remote namespaces consumed by the Client Session object layer. */
39
+ export interface SessionRemotes {
40
+ readonly $stream: ClientRemote['$stream']
41
+ readonly commands: SessionCommandsRemote
42
+ readonly session: SessionRemote
43
+ readonly subagents: SessionSubagentsRemote
44
+ }