@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,14 @@
1
+ /** Browser-owned time-zone sampling for one prompt RPC. */
2
+
3
+ /**
4
+ * Resolve the current browser IANA zone for one outbound operation.
5
+ * @returns The browser-provided canonical zone.
6
+ * @throws when the runtime cannot provide a non-empty zone.
7
+ */
8
+ export function resolvedClientTimeZone(): string {
9
+ const timeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone
10
+ if (typeof timeZone !== 'string' || timeZone.length === 0) {
11
+ throw new Error('browser time zone is unavailable')
12
+ }
13
+ return timeZone
14
+ }
@@ -0,0 +1,247 @@
1
+ /** Session-specific adapters for Gateway-owned Remote stream lifecycles. */
2
+
3
+ import type {} from '@unieai/rabi-api-session-controller/remote'
4
+ import { RemoteError } from '@unieai/rabi-typert-protocol'
5
+ import {
6
+ RemoteJournalStream,
7
+ RemoteSnapshotStream,
8
+ RemoteStreamCarrierError,
9
+ type ClientRemote,
10
+ type RemoteJournalChange,
11
+ type RemoteJournalFrame,
12
+ } from '@unieai/rabi-api-gateway/client'
13
+ import type {
14
+ SessionAddress,
15
+ SessionAssistantStreamBaseline,
16
+ SessionAssistantStreamFrame,
17
+ SessionControlFrame,
18
+ SessionHistoryRecord,
19
+ SessionPage,
20
+ SessionPageRequest,
21
+ SessionProjectionBaseline,
22
+ } from '../types.ts'
23
+ import {
24
+ historyEntries,
25
+ historyRecordFirstSeq,
26
+ historyRecordLastSeq,
27
+ } from './sessions/history-records.ts'
28
+ import type { SessionEventLikeEntry, SessionLiveEventEntry } from './contract/events.ts'
29
+ import type { SessionRemotes } from './sessions/remotes.ts'
30
+ import { assertSessionWireEvent } from './session-wire-event.ts'
31
+
32
+ export {
33
+ SESSION_SEARCH_RESULT_LIMIT,
34
+ SESSION_SEARCH_SNIPPET_MAX_CODE_POINTS,
35
+ } from '../types.ts'
36
+
37
+ /** Pagination fields bound to an already-addressed Session journal. */
38
+ export type ClientSessionPageRequest = Omit<SessionPageRequest, 'address' | 'throughSeq'>
39
+
40
+ /** Complete generated `ctx.remote.session` namespace. */
41
+ export type SessionRemote = ClientRemote['session']
42
+
43
+ /** Opening metadata carried only by a follow snapshot, never by loadOlder pages. */
44
+ interface SessionJournalPage extends SessionPage {
45
+ readonly projections?: SessionProjectionBaseline
46
+ readonly assistantStream?: SessionAssistantStreamBaseline
47
+ }
48
+
49
+ /** One complete publication from the Session journal stream. */
50
+ export type SessionJournalChange =
51
+ | {
52
+ readonly type: 'replace' | 'prepend'
53
+ readonly page: SessionJournalPage
54
+ readonly entries: readonly SessionEventLikeEntry[]
55
+ readonly hasMore: boolean
56
+ }
57
+ | { readonly type: 'append'; readonly entry: SessionLiveEventEntry }
58
+ | { readonly type: 'assistant-stream'; readonly frame: SessionAssistantStreamFrame }
59
+
60
+ function toSessionJournalChange(
61
+ change: RemoteJournalChange<
62
+ SessionJournalPage, SessionHistoryRecord, SessionAssistantStreamFrame
63
+ >,
64
+ ): SessionJournalChange {
65
+ switch (change.type) {
66
+ case 'replace':
67
+ case 'prepend':
68
+ return { ...change, entries: historyEntries(change.entries) }
69
+ case 'append': {
70
+ return {
71
+ type: 'append',
72
+ entry: change.entry as unknown as SessionLiveEventEntry,
73
+ }
74
+ }
75
+ case 'notification':
76
+ return { type: 'assistant-stream', frame: change.notification }
77
+ }
78
+ }
79
+
80
+ type SessionControlBaselineFrame = Extract<SessionControlFrame, { type: 'baseline' }>
81
+ type SessionControlDeltaFrame = Exclude<SessionControlFrame, SessionControlBaselineFrame>
82
+
83
+ /** Gateway-owned control snapshot stream configured for Session frames. */
84
+ export type SessionControlStream = RemoteSnapshotStream<
85
+ SessionControlBaselineFrame,
86
+ SessionControlDeltaFrame
87
+ >
88
+
89
+ /** Domain sinks used by the Host-wide Session control stream. */
90
+ export interface SessionControlStreamOptions {
91
+ /** Apply a complete baseline or one later update. */
92
+ readonly accept: (frame: SessionControlFrame) => void
93
+ /** Observe a retryable carrier loss before reconnection. */
94
+ readonly carrierFailed?: (error: RemoteStreamCarrierError) => void
95
+ /** Publish a terminal business or protocol failure. */
96
+ readonly failed: (error: unknown) => void
97
+ }
98
+
99
+ /** Domain sinks used by one addressed Session event journal. */
100
+ export interface SessionEventStreamOptions {
101
+ /** Apply one complete event-window change. */
102
+ readonly publish: (change: SessionJournalChange) => void
103
+ /** Observe a retryable carrier loss before reconnection. */
104
+ readonly carrierFailed?: (error: RemoteStreamCarrierError) => void
105
+ /** Publish a terminal stream, page, or protocol failure after opening. */
106
+ readonly failed: (error: unknown) => void
107
+ }
108
+
109
+ /**
110
+ * Create the Host-wide Session control snapshot stream.
111
+ * @param remote - generated Session namespace and Gateway stream factory.
112
+ * @param options - Session state destinations.
113
+ * @returns an unstarted stream owned by the Client Session runtime.
114
+ */
115
+ export function createSessionControlStream(
116
+ remote: SessionRemotes,
117
+ options: SessionControlStreamOptions,
118
+ ): SessionControlStream {
119
+ const stream = remote.$stream<SessionControlFrame>({
120
+ name: 'session control stream',
121
+ open: signal => remote.session.control(signal),
122
+ ended: accepted => accepted
123
+ ? new RemoteStreamCarrierError('session control stream ended without a terminal result')
124
+ : new Error('session control stream ended before its opening snapshot'),
125
+ ...(options.carrierFailed === undefined ? {} : { carrierFailed: options.carrierFailed }),
126
+ })
127
+ return new RemoteSnapshotStream(stream, {
128
+ name: 'session control stream',
129
+ isSnapshot: (frame): frame is SessionControlBaselineFrame => frame.type === 'baseline',
130
+ replace: options.accept,
131
+ update: options.accept,
132
+ failed: options.failed,
133
+ })
134
+ }
135
+
136
+ /** Gateway-owned event journal bound to one ordinary or direct-subagent Session address. */
137
+ export class SessionEventStream extends RemoteJournalStream<
138
+ SessionJournalPage,
139
+ SessionHistoryRecord,
140
+ number,
141
+ ClientSessionPageRequest,
142
+ SessionAssistantStreamFrame
143
+ > {
144
+ /**
145
+ * @param remote - generated Session namespace and Gateway stream factory.
146
+ * @param address - durable ordinary-Session or direct-subagent address.
147
+ * @param options - Session event-window destinations.
148
+ */
149
+ constructor(
150
+ private readonly remote: SessionRemotes,
151
+ private readonly address: SessionAddress,
152
+ options: SessionEventStreamOptions,
153
+ ) {
154
+ super(remote, {
155
+ name: 'session event stream',
156
+ emptyCursor: -1,
157
+ entries: page => page.records,
158
+ hasMore: page => page.hasMore,
159
+ first: historyRecordFirstSeq,
160
+ last: historyRecordLastSeq,
161
+ compare: (left, right) => left - right,
162
+ follows: (left, right) => right === left + 1,
163
+ publish: (change) => { options.publish(toSessionJournalChange(change)) },
164
+ ...(options.carrierFailed === undefined
165
+ ? {}
166
+ : { carrierFailed: options.carrierFailed }),
167
+ failed: options.failed,
168
+ })
169
+ }
170
+
171
+ /** @inheritdoc */
172
+ protected override async * follow(
173
+ request: ClientSessionPageRequest,
174
+ signal: AbortSignal,
175
+ ): AsyncIterable<RemoteJournalFrame<
176
+ SessionHistoryRecord, number, SessionJournalPage, SessionAssistantStreamFrame
177
+ >> {
178
+ let assistantRevision: number | undefined
179
+ for await (const frame of this.remote.session.follow({
180
+ address: this.address,
181
+ activate: true,
182
+ assistantStream: true,
183
+ ...this.repairRequest(request),
184
+ }, signal)) {
185
+ if (frame.type === 'snapshot') {
186
+ for (const record of frame.records) assertSessionWireEvent(record.event)
187
+ if (frame.assistantStream === undefined) {
188
+ throw new RemoteError(
189
+ 'gateway/internal',
190
+ 'session assistant stream omitted its opted-in opening baseline',
191
+ {},
192
+ )
193
+ }
194
+ assistantRevision = frame.assistantStream.revision
195
+ yield {
196
+ type: 'opened',
197
+ cursor: frame.cursor,
198
+ page: {
199
+ records: frame.records,
200
+ hasMore: frame.hasMore,
201
+ projections: frame.projections,
202
+ assistantStream: frame.assistantStream,
203
+ },
204
+ }
205
+ continue
206
+ }
207
+ if (frame.type === 'assistant-stream') {
208
+ const expected = (assistantRevision ?? 0) + 1
209
+ if (frame.frame.revision !== expected) {
210
+ throw new RemoteStreamCarrierError(
211
+ `session assistant stream skipped revision ${String(expected)}`,
212
+ )
213
+ }
214
+ assistantRevision = frame.frame.revision
215
+ yield { type: 'notification', notification: frame.frame }
216
+ continue
217
+ }
218
+ assertSessionWireEvent(frame.event)
219
+ yield { type: 'entry', entry: frame }
220
+ }
221
+ }
222
+
223
+ /** @inheritdoc */
224
+ protected override async readPage(
225
+ request: ClientSessionPageRequest,
226
+ throughSeq: number,
227
+ signal: AbortSignal,
228
+ ): Promise<SessionJournalPage> {
229
+ const result = await this.remote.session.page(
230
+ { address: this.address, throughSeq, ...request },
231
+ signal,
232
+ )
233
+ if (!result.ok) throw result.error
234
+ for (const record of result.value.records) assertSessionWireEvent(record.event)
235
+ return result.value
236
+ }
237
+
238
+ /** @inheritdoc */
239
+ protected override repairRequest(
240
+ request: ClientSessionPageRequest,
241
+ ): ClientSessionPageRequest {
242
+ return {
243
+ ...(request.maxMessages === undefined ? {} : { maxMessages: request.maxMessages }),
244
+ ...(request.turnWindow === undefined ? {} : { turnWindow: request.turnWindow }),
245
+ }
246
+ }
247
+ }