@agentshouse/mdruntime-adapter 0.1.0-alpha.9 → 0.1.0-c0074032f9909

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 (85) hide show
  1. package/README.md +32 -0
  2. package/dist/actions.d.ts +8 -16
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +43 -436
  5. package/dist/adapter.d.ts +1 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +352 -231
  8. package/dist/authored-path.d.ts +9 -0
  9. package/dist/authored-path.d.ts.map +1 -0
  10. package/dist/authored-path.js +34 -0
  11. package/dist/chunks.d.ts +36 -0
  12. package/dist/chunks.d.ts.map +1 -0
  13. package/dist/chunks.js +281 -0
  14. package/dist/digest.d.ts +0 -1
  15. package/dist/digest.js +1 -5
  16. package/dist/discovery.d.ts +49 -42
  17. package/dist/discovery.d.ts.map +1 -1
  18. package/dist/discovery.js +317 -232
  19. package/dist/history.d.ts +6 -0
  20. package/dist/history.d.ts.map +1 -0
  21. package/dist/history.js +85 -0
  22. package/dist/index.d.ts +10 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +9 -3
  25. package/dist/map.d.ts +2 -3
  26. package/dist/map.d.ts.map +1 -1
  27. package/dist/map.js +6 -7
  28. package/dist/model.d.ts +8 -19
  29. package/dist/model.d.ts.map +1 -1
  30. package/dist/model.js +43 -57
  31. package/dist/read.d.ts +65 -54
  32. package/dist/read.d.ts.map +1 -1
  33. package/dist/read.js +217 -295
  34. package/dist/refusal.d.ts +7 -11
  35. package/dist/refusal.d.ts.map +1 -1
  36. package/dist/refusal.js +53 -69
  37. package/dist/search.d.ts +4 -0
  38. package/dist/search.d.ts.map +1 -0
  39. package/dist/search.js +47 -0
  40. package/dist/session-content/bounds.d.ts +7 -0
  41. package/dist/session-content/check.d.ts +18 -0
  42. package/dist/session-content/index.d.ts +7 -0
  43. package/dist/session-content/parts.d.ts +103 -0
  44. package/dist/session-content/state.d.ts +61 -0
  45. package/dist/session-content/turn.d.ts +328 -0
  46. package/dist/site.d.ts +1 -2
  47. package/dist/site.d.ts.map +1 -1
  48. package/dist/site.js +142 -172
  49. package/dist/space-schema.d.ts +6 -0
  50. package/dist/space-schema.d.ts.map +1 -0
  51. package/dist/space-schema.js +11 -0
  52. package/dist/stream.d.ts +20 -9
  53. package/dist/stream.d.ts.map +1 -1
  54. package/dist/stream.js +394 -236
  55. package/dist/transport.d.ts +172 -47
  56. package/dist/transport.d.ts.map +1 -1
  57. package/dist/transport.js +58 -12
  58. package/dist/uncertain.d.ts +5 -0
  59. package/dist/uncertain.d.ts.map +1 -0
  60. package/dist/uncertain.js +11 -0
  61. package/dist/upload.d.ts +13 -0
  62. package/dist/upload.d.ts.map +1 -0
  63. package/dist/upload.js +65 -0
  64. package/dist/version.d.ts +3 -1
  65. package/dist/version.d.ts.map +1 -1
  66. package/dist/version.js +32 -18
  67. package/dist/write.d.ts +24 -11
  68. package/dist/write.d.ts.map +1 -1
  69. package/dist/write.js +263 -241
  70. package/package.json +10 -14
  71. package/AGENTS.md +0 -79
  72. package/dist/actions.js.map +0 -1
  73. package/dist/adapter.js.map +0 -1
  74. package/dist/digest.js.map +0 -1
  75. package/dist/discovery.js.map +0 -1
  76. package/dist/index.js.map +0 -1
  77. package/dist/map.js.map +0 -1
  78. package/dist/model.js.map +0 -1
  79. package/dist/read.js.map +0 -1
  80. package/dist/refusal.js.map +0 -1
  81. package/dist/site.js.map +0 -1
  82. package/dist/stream.js.map +0 -1
  83. package/dist/transport.js.map +0 -1
  84. package/dist/version.js.map +0 -1
  85. package/dist/write.js.map +0 -1
package/dist/stream.js CHANGED
@@ -1,298 +1,456 @@
1
- import { actionOutcome, HISTORY_PAGE, readConversationHistory } from './actions.js';
2
- import { selects } from './read.js';
3
- import { PRIVATE_AUTHORITY } from './transport.js';
4
- import { roomVersion } from './version.js';
5
- const HELD_POSITIONS_MAX = 256;
1
+ import { ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { acceptedMessage, changeCursor, houseHistory } from './history.js';
3
+ import { spaceSchemaTexts, streamedModels } from './model.js';
4
+ import { spaceSchemas } from './space-schema.js';
5
+ import { answered, answering, documentEntry, folded, forgotten, kept, } from './read.js';
6
+ import { DECIMAL, logPosition, presented, roomVersion } from './version.js';
7
+ function turnOperation(frame) {
8
+ return 'at' in frame ? { op: 'begin', at: frame.at } : structuredClone(frame.operation);
9
+ }
10
+ function listedAgent(agent) {
11
+ return {
12
+ id: agent.id,
13
+ name: agent.name,
14
+ revision: agent.revision,
15
+ environment: agent.environment,
16
+ environment_name: agent.environment_name,
17
+ environment_connected: agent.environment_connected,
18
+ launch_directory: agent.launch_directory,
19
+ cli: agent.cli,
20
+ model: agent.model,
21
+ effort: agent.effort,
22
+ mode: agent.mode,
23
+ base_instructions: agent.base_instructions,
24
+ in_telegram: agent.in_telegram,
25
+ created: agent.created,
26
+ retired_at: agent.retired_at,
27
+ };
28
+ }
29
+ function listedConversation(listed) {
30
+ return {
31
+ conversation: listed.conversation_id,
32
+ agent: listed.agent_id,
33
+ name: listed.name,
34
+ created: listed.created_at,
35
+ finished: listed.finished_at,
36
+ outcome: listed.outcome,
37
+ waiting: listed.waiting,
38
+ newest: listed.newest === null ? null : changeCursor(listed.newest),
39
+ processEnded: listed.process_ended_at,
40
+ subject: listed.subject,
41
+ };
42
+ }
6
43
  function position(value) {
7
- if (!/^(?:0|[1-9][0-9]*)$/.test(value)) return null;
44
+ if (!DECIMAL.test(value))
45
+ return null;
8
46
  return BigInt(value);
9
47
  }
10
- function messageTurn(message) {
11
- if (typeof message !== 'object' || message === null) return null;
12
- const turn = message.message_id;
13
- return typeof turn === 'string' ? turn : null;
48
+ function* changed(tracked, entries, version, mode, schemas) {
49
+ const answer = answering();
50
+ for (const entry of entries) {
51
+ yield ROW_WEIGHT + (entry.content?.length ?? 0);
52
+ if (entry.kind === 'folder') {
53
+ folded(answer, entry.path, entry.removed === true);
54
+ continue;
55
+ }
56
+ if (entry.kind === 'removed') {
57
+ if (entry.path === tracked.schema)
58
+ schemas.set(entry.path, null);
59
+ forgotten(answer, entry.path);
60
+ answer.removals.add(entry.path);
61
+ continue;
62
+ }
63
+ const file = documentEntry(entry, mode);
64
+ if (file === null)
65
+ throw new Error(`the stream carried ${entry.path} as no file`);
66
+ if (entry.path === tracked.schema)
67
+ schemas.set(entry.path, file);
68
+ forgotten(answer, file.path);
69
+ if (file.space === undefined)
70
+ kept(answer, file);
71
+ else
72
+ answer.removals.add(file.path);
73
+ }
74
+ return { kind: 'net-advancement', roomVersion: version, ...(yield* answered(answer)) };
75
+ }
76
+ async function changePage(tracked, entries, version, mode, whole) {
77
+ const schemas = new Map();
78
+ const page = await stepped(changed(tracked, entries, version, mode, schemas));
79
+ const models = await streamedModels(schemas, tracked, whole);
80
+ return models === undefined ? page : { ...page, models };
14
81
  }
15
- export function houseNotifications(transport, mode) {
82
+ export function houseNotifications(transport, mode, discovering) {
16
83
  const stream = transport.stream;
17
- const lines = new Map();
18
- const locators = new Map();
19
- const authorities = new Map();
84
+ const staged = new Map();
85
+ let spaces = spaceSchemas();
86
+ let socket = null;
87
+ const tracking = new Map();
88
+ const located = new Map();
20
89
  const observers = new Set();
90
+ const replaying = new Map();
21
91
  const conversationObservers = new Set();
22
- const subscribedConversations = new Set();
23
- let selected = [];
24
- let viewed = [];
25
- let listening = false;
26
- function subscribe() {
27
- const named = [];
28
- const rooms = new Set();
29
- for (const locator of selected){
30
- const authority = authorities.get(locator);
31
- if (authority === undefined || authority === PRIVATE_AUTHORITY) continue;
32
- rooms.add(authority);
33
- }
34
- for (const room of rooms)named.push({
35
- kind: 'room',
36
- room
37
- });
38
- if (authorities.has(PRIVATE_AUTHORITY)) named.push({
39
- kind: 'private'
40
- });
41
- for (const conversation of viewed){
42
- named.push({
43
- kind: 'conversation',
44
- conversation
45
- });
92
+ const modeObservers = new Set();
93
+ let replicated = [];
94
+ let listedCursor = null;
95
+ const unanswered = [];
96
+ const heldAtZero = new Set();
97
+ const live = new Set();
98
+ let opening = null;
99
+ let announced = null;
100
+ const runtime = crypto.randomUUID();
101
+ function emit(event) {
102
+ for (const observer of observers) {
103
+ const held = replaying.get(observer);
104
+ if (held === undefined)
105
+ observer(event);
106
+ else
107
+ held.push(event);
46
108
  }
47
- stream.subscribe(named);
48
109
  }
49
110
  function emitConversation(event) {
50
- if (!viewed.includes(event.conversation)) return;
51
- for (const observer of conversationObservers)observer(event);
111
+ if (event.kind === 'turn' ||
112
+ event.kind === 'questions' ||
113
+ event.kind === 'process' ||
114
+ event.kind === 'commands' ||
115
+ event.kind === 'options' ||
116
+ event.kind === 'starting' ||
117
+ event.kind === 'state' ||
118
+ event.kind === 'link' ||
119
+ event.kind === 'deliveries') {
120
+ live.add(event.conversation);
121
+ }
122
+ for (const observer of conversationObservers)
123
+ observer(event);
124
+ }
125
+ function reopened(next) {
126
+ opening = next;
127
+ announced = null;
128
+ socket = next;
129
+ staged.clear();
130
+ spaces = spaceSchemas();
131
+ unanswered.length = 0;
132
+ heldAtZero.clear();
133
+ const gapped = new Set([...replicated, ...live]);
134
+ live.clear();
135
+ for (const conversation of gapped)
136
+ emitConversation({ kind: 'gap', conversation });
137
+ emit({ kind: 'opened' });
138
+ if (mode === 'site')
139
+ announce(next, null);
52
140
  }
53
- async function catchUp(conversation, history) {
54
- const viewedAnswer = await transport.door('/door/conversations/view', {
55
- conversation_id: conversation
141
+ function announce(via, frame) {
142
+ void discovering.opened(frame).then((discovery) => {
143
+ if (socket !== via)
144
+ return;
145
+ if (discovery === null) {
146
+ via.close();
147
+ return;
148
+ }
149
+ announced = { kind: 'discovered', discovery };
150
+ emit(announced);
151
+ }, (failure) => {
152
+ if (socket === via)
153
+ via.close(failure);
56
154
  });
57
- const summary = actionOutcome(viewedAnswer, 'conversations/view');
58
- if (summary.kind === 'action' && typeof summary.result === 'object' && summary.result !== null) {
155
+ }
156
+ function converse(frame) {
157
+ const conversation = frame.conversation_id;
158
+ if (frame.type === 'turn') {
59
159
  emitConversation({
60
- kind: 'session-summary',
160
+ kind: 'turn',
61
161
  conversation,
62
- summary: structuredClone('session' in summary.result ? summary.result.session : null)
162
+ turn: frame.turn_id,
163
+ operation: turnOperation(frame),
63
164
  });
165
+ return;
64
166
  }
65
- if (!history) return;
66
- const heldHistory = await readConversationHistory(transport, conversation);
67
- if ('outcome' in heldHistory) return;
68
- const newest = heldHistory.messages.slice(-HISTORY_PAGE);
69
- for (const entry of newest){
70
- const turn = messageTurn(entry);
71
- if (turn === null) return;
167
+ if (frame.type === 'process') {
168
+ emitConversation({ kind: 'process', conversation, running: frame.running });
169
+ if (frame.running)
170
+ return;
171
+ emitConversation({ kind: 'commands', conversation, commands: [] });
172
+ emitConversation({ kind: 'options', conversation, options: [] });
173
+ emitConversation({ kind: 'state', conversation, state: null });
174
+ return;
175
+ }
176
+ if (frame.type === 'commands') {
72
177
  emitConversation({
73
- kind: 'accepted-message',
178
+ kind: 'commands',
74
179
  conversation,
75
- message: {
76
- turn,
77
- message: structuredClone(entry)
78
- }
180
+ commands: structuredClone(frame.commands),
79
181
  });
182
+ return;
80
183
  }
81
- }
82
- function announce(authority, at) {
83
- const version = roomVersion(at);
84
- for (const locator of locators.get(authority) ?? []){
85
- for (const observer of observers)observer({
86
- locator,
87
- roomVersion: version
88
- });
184
+ if (frame.type === 'options') {
185
+ emitConversation({ kind: 'options', conversation, options: structuredClone(frame.options) });
186
+ return;
89
187
  }
90
- }
91
- function interrupt(authority) {
92
- const line = lines.get(authority);
93
- if (line !== undefined) lines.set(authority, {
94
- logEpoch: line.logEpoch,
95
- frames: []
96
- });
97
- }
98
- function resubscribed(authority, position, logEpoch) {
99
- if (position === undefined || logEpoch === undefined) {
100
- interrupt(authority);
188
+ if (frame.type === 'starting') {
189
+ emitConversation({ kind: 'starting', conversation, step: frame.step });
101
190
  return;
102
191
  }
103
- const held = lines.get(authority)?.logEpoch;
104
- lines.set(authority, {
105
- logEpoch,
106
- frames: []
107
- });
108
- if (held !== logEpoch) announce(authority, {
109
- position,
110
- logEpoch
111
- });
112
- }
113
- function receive(frame) {
114
- if (frame.type === 'draft') {
192
+ if (frame.type === 'state') {
193
+ emitConversation({ kind: 'state', conversation, state: structuredClone(frame.state) });
194
+ return;
195
+ }
196
+ if (frame.type === 'link') {
197
+ emitConversation({ kind: 'link', conversation, connected: frame.connected });
198
+ return;
199
+ }
200
+ if (frame.type === 'deliveries') {
115
201
  emitConversation({
116
- kind: 'growing-text',
117
- conversation: frame.conversation_id,
118
- turn: frame.turn_id,
119
- text: frame.text
202
+ kind: 'deliveries',
203
+ conversation,
204
+ waiting: [...frame.waiting],
205
+ unknown: [...frame.unknown],
120
206
  });
121
207
  return;
122
208
  }
123
- if (frame.type === 'message') {
124
- const turn = messageTurn(frame.message);
125
- if (turn === null) return;
209
+ if (frame.type === 'interactions') {
126
210
  emitConversation({
127
- kind: 'accepted-message',
128
- conversation: frame.conversation_id,
129
- message: {
130
- turn,
131
- message: structuredClone(frame.message)
132
- }
211
+ kind: 'questions',
212
+ conversation,
213
+ questions: structuredClone(frame.interactions),
133
214
  });
134
215
  return;
135
216
  }
136
- if (frame.type === 'session') {
217
+ const message = acceptedMessage(frame.message);
218
+ if (message === null)
219
+ return;
220
+ emitConversation({ kind: 'accepted-message', conversation, message });
221
+ }
222
+ function stage(frame, via) {
223
+ const entries = spaceSchemaTexts(frame.entries, spaces);
224
+ const held = staged.get(frame.carry);
225
+ if (frame.index !== (held?.parts ?? 0)) {
226
+ staged.delete(frame.carry);
227
+ return;
228
+ }
229
+ if (held === undefined) {
230
+ for (const [carry, other] of staged) {
231
+ if (other.authority === frame.authority)
232
+ staged.delete(carry);
233
+ }
234
+ }
235
+ const next = held ?? { authority: frame.authority, parts: 0, entries: [] };
236
+ next.entries.push(...entries);
237
+ next.parts += 1;
238
+ staged.set(frame.carry, next);
239
+ via?.received(frame.authority, frame.carry, frame.index);
240
+ }
241
+ function carried(frame) {
242
+ const held = staged.get(frame.carry);
243
+ staged.delete(frame.carry);
244
+ if ((held?.parts ?? 0) !== frame.parts)
245
+ return;
246
+ publish(frame, held?.entries ?? []);
247
+ }
248
+ function handle(frame, via) {
249
+ if (frame.type === 'signal') {
250
+ const mode = frame.service_mode;
251
+ if (mode !== 'open' && mode !== 'write-halted')
252
+ return;
253
+ for (const observer of modeObservers)
254
+ observer(mode);
255
+ return;
256
+ }
257
+ if (frame.type === 'opening') {
258
+ if (via !== null && mode === 'credential')
259
+ announce(via, frame);
260
+ return;
261
+ }
262
+ if (frame.type === 'caught_up') {
263
+ const expired = frame.expired.flatMap((authority) => tracking.get(authority) ?? []);
264
+ discovering.expired?.(expired);
265
+ emit({
266
+ kind: 'caught-up',
267
+ expired: [...expired.map(({ locator }) => locator), ...(unanswered.shift() ?? [])],
268
+ });
269
+ return;
270
+ }
271
+ if (frame.type === 'report') {
272
+ emit({ kind: 'reported' });
273
+ return;
274
+ }
275
+ if (frame.type === 'authority_changed') {
276
+ const change = discovering.changed(frame);
277
+ if (change !== null)
278
+ emit(change);
279
+ return;
280
+ }
281
+ if (frame.type === 'conversations') {
137
282
  emitConversation({
138
- kind: 'session-summary',
139
- conversation: frame.conversation_id,
140
- summary: structuredClone(frame.summary)
283
+ kind: 'list',
284
+ agents: frame.agents.map(listedAgent),
285
+ entries: frame.conversations.map(listedConversation),
286
+ whole: frame.whole,
287
+ more: frame.more,
288
+ cursor: frame.cursor,
141
289
  });
142
290
  return;
143
291
  }
144
- if (frame.type === 'subscribed' && viewed.includes(frame.authority)) {
145
- const reconnect = subscribedConversations.has(frame.authority);
146
- subscribedConversations.add(frame.authority);
147
- void catchUp(frame.authority, reconnect);
292
+ if (frame.type === 'conversation') {
293
+ emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
148
294
  return;
149
295
  }
150
- if (frame.type === 'subscribed') {
151
- resubscribed(frame.authority, frame.position, frame.log_epoch);
296
+ if (frame.type === 'agent') {
297
+ emitConversation({ kind: 'agent', agent: listedAgent(frame.agent) });
152
298
  return;
153
299
  }
154
- if (frame.type !== 'entries') {
155
- interrupt(frame.authority);
300
+ if (frame.type === 'conversation_deleted') {
301
+ emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
156
302
  return;
157
303
  }
158
- const at = position(frame.position);
159
- if (at === null) {
160
- interrupt(frame.authority);
304
+ if (frame.type === 'turn' ||
305
+ frame.type === 'process' ||
306
+ frame.type === 'commands' ||
307
+ frame.type === 'options' ||
308
+ frame.type === 'starting' ||
309
+ frame.type === 'state' ||
310
+ frame.type === 'link' ||
311
+ frame.type === 'deliveries' ||
312
+ frame.type === 'interactions' ||
313
+ frame.type === 'message') {
314
+ converse(frame);
161
315
  return;
162
316
  }
163
- const line = lines.get(frame.authority);
164
- if (line?.logEpoch === frame.log_epoch) {
165
- const last = line.frames.at(-1);
166
- const continued = last !== undefined && last.position + 1n === at;
167
- const next = continued ? [
168
- ...line.frames,
169
- {
170
- position: at,
171
- entries: frame.entries
172
- }
173
- ] : [
174
- {
175
- position: at,
176
- entries: frame.entries
177
- }
178
- ];
179
- lines.set(frame.authority, {
180
- logEpoch: line.logEpoch,
181
- frames: next.length > HELD_POSITIONS_MAX ? [] : next
317
+ if (frame.type === 'part')
318
+ stage(frame, via);
319
+ else if (frame.type === 'carried')
320
+ carried(frame);
321
+ }
322
+ function publish(frame, entries) {
323
+ const at = position(frame.position);
324
+ const after = position(frame.after);
325
+ if (at === null || after === null || after >= at)
326
+ return;
327
+ const from = after === 0n && !heldAtZero.has(frame.authority)
328
+ ? null
329
+ : roomVersion({ position: frame.after, logEpoch: frame.log_epoch });
330
+ const version = roomVersion({ position: frame.position, logEpoch: frame.log_epoch });
331
+ for (const tracked of tracking.get(frame.authority) ?? []) {
332
+ emit({
333
+ kind: 'room-change',
334
+ locator: tracked.locator,
335
+ from,
336
+ roomVersion: version,
337
+ read: () => changePage(tracked, entries, version, mode, from === null),
182
338
  });
183
- } else {
184
- interrupt(frame.authority);
185
339
  }
186
- announce(frame.authority, {
187
- position: frame.position,
188
- logEpoch: frame.log_epoch
189
- });
190
340
  }
341
+ stream.observe((frame) => handle(frame, socket));
342
+ stream.opened(reopened);
343
+ stream.closed(({ revoked, lost, authorityChanged }) => {
344
+ opening = null;
345
+ announced = null;
346
+ socket = null;
347
+ staged.clear();
348
+ spaces = spaceSchemas();
349
+ unanswered.length = 0;
350
+ heldAtZero.clear();
351
+ emit({ kind: 'closed', revoked, lost, authorityChanged });
352
+ });
191
353
  return {
354
+ runtime,
355
+ serviceMode: {
356
+ observe(notify) {
357
+ modeObservers.add(notify);
358
+ return () => {
359
+ modeObservers.delete(notify);
360
+ };
361
+ },
362
+ },
192
363
  conversations: {
193
- view (next) {
194
- const open = new Set(next);
195
- for (const conversation of subscribedConversations){
196
- if (!open.has(conversation)) subscribedConversations.delete(conversation);
197
- }
198
- viewed = [
199
- ...next
200
- ];
201
- subscribe();
364
+ replicate(next) {
365
+ replicated = [...next];
366
+ },
367
+ hold(cursor) {
368
+ listedCursor = cursor;
202
369
  },
203
- observe (notify) {
370
+ observe(notify) {
204
371
  conversationObservers.add(notify);
205
- if (!listening) {
206
- listening = true;
207
- stream.observe(receive);
208
- }
209
- return ()=>{
372
+ return () => {
210
373
  conversationObservers.delete(notify);
211
374
  };
212
- }
213
- },
214
- track (entries) {
215
- locators.clear();
216
- authorities.clear();
217
- for (const entry of entries){
218
- locators.set(entry.authority, [
219
- ...locators.get(entry.authority) ?? [],
220
- entry.locator
221
- ]);
222
- authorities.set(entry.locator, entry.authority);
223
- if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
224
- lines.set(entry.authority, {
225
- logEpoch: entry.logEpoch,
226
- frames: []
227
- });
228
- }
229
- }
230
- for (const authority of lines.keys()){
231
- if (!locators.has(authority)) lines.delete(authority);
232
- }
233
- },
234
- select (named) {
235
- selected = [
236
- ...named
237
- ];
238
- subscribe();
375
+ },
376
+ ...(mode === 'credential' ? { history: (request) => houseHistory(transport, request) } : {}),
239
377
  },
240
- observe (notify) {
378
+ observe(notify) {
241
379
  observers.add(notify);
242
- if (!listening) {
243
- listening = true;
244
- stream.observe(receive);
380
+ if (opening !== null) {
381
+ const replayed = [{ kind: 'opened' }];
382
+ if (announced !== null)
383
+ replayed.push(announced);
384
+ replaying.set(notify, replayed);
385
+ queueMicrotask(() => {
386
+ replaying.delete(notify);
387
+ for (const event of replayed)
388
+ if (observers.has(notify))
389
+ notify(event);
390
+ });
245
391
  }
246
- return ()=>{
392
+ return () => {
247
393
  observers.delete(notify);
248
394
  };
249
395
  },
250
- advance (tracked, from) {
251
- const line = lines.get(tracked.authority);
252
- const start = position(from.position);
253
- if (line === undefined || start === null || line.logEpoch !== from.logEpoch) return null;
254
- const at = line.frames.findIndex((frame)=>frame.position === start + 1n);
255
- if (at === -1) return null;
256
- const pending = line.frames.slice(at);
257
- const documents = {};
258
- const revisions = {};
259
- const removals = [];
260
- for (const frame of pending){
261
- for (const entry of frame.entries){
262
- if (!selects(tracked, entry.path)) continue;
263
- if (entry.kind === 'removed') {
264
- delete documents[entry.path];
265
- delete revisions[entry.path];
266
- if (!removals.includes(entry.path)) removals.push(entry.path);
267
- continue;
268
- }
269
- if (entry.text === undefined) return null;
270
- if (mode === 'credential' && entry.revision === undefined) return null;
271
- documents[entry.path] = entry.text;
272
- if (entry.revision !== undefined) revisions[entry.path] = entry.revision;
273
- const removed = removals.indexOf(entry.path);
274
- if (removed !== -1) removals.splice(removed, 1);
275
- }
396
+ send(rooms) {
397
+ opening = null;
398
+ announced = null;
399
+ if (socket === null)
400
+ return;
401
+ const named = rooms.flatMap((room) => {
402
+ const entry = located.get(room.locator);
403
+ const at = room.roomVersion === null ? null : logPosition(room.roomVersion);
404
+ return entry === undefined || at === null ? [] : [{ entry, at }];
405
+ });
406
+ const unheld = rooms.flatMap((room) => {
407
+ const entry = located.get(room.locator);
408
+ return entry === undefined || room.roomVersion !== null ? [] : [entry];
409
+ });
410
+ for (const { entry, at } of named) {
411
+ if (at.position === '0')
412
+ heldAtZero.add(entry.authority);
413
+ else
414
+ heldAtZero.delete(entry.authority);
276
415
  }
277
- const last = pending.at(-1);
278
- if (last === undefined) return null;
279
- lines.set(tracked.authority, {
280
- logEpoch: line.logEpoch,
281
- frames: pending
416
+ for (const entry of unheld)
417
+ heldAtZero.delete(entry.authority);
418
+ const site = mode === 'site';
419
+ unanswered.push(site ? unheld.map((entry) => entry.locator) : []);
420
+ socket.hold([
421
+ ...named.map(({ entry, at }) => presented(entry.authority, at)),
422
+ ...(site ? [] : unheld).map((entry) => presented(entry.authority, { position: '0', logEpoch: entry.logEpoch })),
423
+ ], runtime, listedCursor);
424
+ if (site)
425
+ return;
426
+ const made = unheld.filter((entry) => entry.position === '0');
427
+ queueMicrotask(() => {
428
+ for (const entry of made) {
429
+ const version = roomVersion(entry);
430
+ emit({
431
+ kind: 'room-change',
432
+ locator: entry.locator,
433
+ from: null,
434
+ roomVersion: version,
435
+ read: () => changePage(entry, [], version, mode, true),
436
+ });
437
+ }
282
438
  });
283
- return {
284
- kind: 'net-advancement',
285
- roomVersion: roomVersion({
286
- position: last.position.toString(),
287
- logEpoch: line.logEpoch
288
- }),
289
- documents,
290
- revisions,
291
- binaryTargets: [],
292
- removals: removals.sort()
293
- };
294
- }
439
+ },
440
+ track(entries) {
441
+ tracking.clear();
442
+ located.clear();
443
+ for (const entry of entries) {
444
+ tracking.set(entry.authority, [...(tracking.get(entry.authority) ?? []), entry]);
445
+ }
446
+ for (const held of tracking.values()) {
447
+ for (const entry of held)
448
+ located.set(entry.locator, entry);
449
+ }
450
+ for (const [carry, held] of staged) {
451
+ if (!tracking.has(held.authority))
452
+ staged.delete(carry);
453
+ }
454
+ },
295
455
  };
296
456
  }
297
-
298
- //# sourceMappingURL=stream.js.map