@agentshouse/mdruntime-adapter 0.1.0-cc63dd043d0e2 → 0.1.0-cc65657e82614

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 (83) hide show
  1. package/README.md +5 -5
  2. package/dist/actions.d.ts +6 -6
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +25 -326
  5. package/dist/adapter.d.ts +1 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +284 -250
  8. package/dist/app-reference.d.ts +1 -0
  9. package/dist/app-reference.d.ts.map +1 -0
  10. package/dist/app-reference.js +2 -0
  11. package/dist/authored-path.d.ts +10 -0
  12. package/dist/authored-path.d.ts.map +1 -0
  13. package/dist/authored-path.js +35 -0
  14. package/dist/chunks.d.ts +37 -0
  15. package/dist/chunks.d.ts.map +1 -0
  16. package/dist/chunks.js +247 -0
  17. package/dist/crockford.d.ts +3 -0
  18. package/dist/crockford.d.ts.map +1 -0
  19. package/dist/crockford.js +3 -0
  20. package/dist/digest.d.ts +0 -1
  21. package/dist/discovery.d.ts +28 -28
  22. package/dist/discovery.d.ts.map +1 -1
  23. package/dist/discovery.js +112 -79
  24. package/dist/history.d.ts +6 -0
  25. package/dist/history.d.ts.map +1 -0
  26. package/dist/history.js +84 -0
  27. package/dist/house-model.d.ts +830 -0
  28. package/dist/house-model.d.ts.map +1 -0
  29. package/dist/house-model.js +465 -0
  30. package/dist/index.d.ts +12 -3
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +11 -1
  33. package/dist/map.d.ts +0 -1
  34. package/dist/model.d.ts +8 -19
  35. package/dist/model.d.ts.map +1 -1
  36. package/dist/model.js +53 -39
  37. package/dist/read.d.ts +63 -43
  38. package/dist/read.d.ts.map +1 -1
  39. package/dist/read.js +179 -265
  40. package/dist/refusal.d.ts +5 -12
  41. package/dist/refusal.d.ts.map +1 -1
  42. package/dist/refusal.js +31 -18
  43. package/dist/room-types.d.ts +4 -0
  44. package/dist/room-types.d.ts.map +1 -0
  45. package/dist/room-types.js +135 -0
  46. package/dist/search.d.ts +4 -0
  47. package/dist/search.d.ts.map +1 -0
  48. package/dist/search.js +47 -0
  49. package/dist/session-content/bounds.d.ts +7 -0
  50. package/dist/session-content/check.d.ts +18 -0
  51. package/dist/session-content/index.d.ts +7 -0
  52. package/dist/session-content/parts.d.ts +102 -0
  53. package/dist/session-content/state.d.ts +61 -0
  54. package/dist/session-content/turn.d.ts +328 -0
  55. package/dist/site.d.ts +1 -2
  56. package/dist/site.d.ts.map +1 -1
  57. package/dist/site.js +128 -142
  58. package/dist/space-schema.d.ts +6 -0
  59. package/dist/space-schema.d.ts.map +1 -0
  60. package/dist/space-schema.js +18 -0
  61. package/dist/stream.d.ts +18 -12
  62. package/dist/stream.d.ts.map +1 -1
  63. package/dist/stream.js +345 -144
  64. package/dist/transport.d.ts +124 -27
  65. package/dist/transport.d.ts.map +1 -1
  66. package/dist/transport.js +55 -10
  67. package/dist/upload.d.ts +11 -0
  68. package/dist/upload.d.ts.map +1 -0
  69. package/dist/upload.js +63 -0
  70. package/dist/version.d.ts +2 -1
  71. package/dist/version.d.ts.map +1 -1
  72. package/dist/version.js +10 -1
  73. package/dist/write.d.ts +20 -5
  74. package/dist/write.d.ts.map +1 -1
  75. package/dist/write.js +170 -60
  76. package/package.json +9 -6
  77. package/AGENTS.md +0 -215
  78. package/dist/gather.d.ts +0 -3
  79. package/dist/gather.d.ts.map +0 -1
  80. package/dist/gather.js +0 -40
  81. package/dist/links.d.ts +0 -12
  82. package/dist/links.d.ts.map +0 -1
  83. package/dist/links.js +0 -66
@@ -0,0 +1,6 @@
1
+ export type SpaceSchemas = {
2
+ held(space: string, revision: string): string | undefined;
3
+ keep(space: string, revision: string, text: string): void;
4
+ };
5
+ export declare function spaceSchemas(): SpaceSchemas;
6
+ export declare function spaceIdentity(space: string): string;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"space-schema.d.ts","sourceRoot":"","sources":["../src/space-schema.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC1D,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3D,CAAC;AAEF,wBAAgB,YAAY,IAAI,YAAY,CAY3C;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEnD"}
@@ -0,0 +1,18 @@
1
+ const HELD_TEXTS = 16;
2
+ export function spaceSchemas() {
3
+ const texts = new Map();
4
+ const key = (space, revision) => `${space} ${revision}`;
5
+ return {
6
+ held: (space, revision) => texts.get(key(space, revision)),
7
+ keep(space, revision, text) {
8
+ const named = key(space, revision);
9
+ texts.delete(named);
10
+ texts.set(named, text);
11
+ if (texts.size > HELD_TEXTS)
12
+ texts.delete(texts.keys().next().value);
13
+ },
14
+ };
15
+ }
16
+ export function spaceIdentity(space) {
17
+ return `space:${space}`;
18
+ }
package/dist/stream.d.ts CHANGED
@@ -1,18 +1,24 @@
1
- import type { ConversationStream, RefreshPage, SelectedRooms, ServiceModeSignal } from '@agentshouse/mdruntime';
1
+ import { type AuthorityChange, type Discovery, type ConversationStream, type HeldRoom, type RoomStreamEvent, type ServiceModeSignal } from '@agentshouse/mdruntime';
2
2
  import type { TrackedAuthority } from './discovery.js';
3
- import type { HouseDoor, HouseMode, HouseStreamEntry } from './transport.js';
4
- import { type LogPosition } from './version.js';
5
- export type HeldRevision = (path: string, revision: string) => boolean;
6
- export type StreamedAdvancement = {
7
- page: RefreshPage;
8
- entries: readonly HouseStreamEntry[];
3
+ import type { SpaceSchemas } from './space-schema.js';
4
+ import { type HouseDoor, type HouseMode, type HouseStreamFrame } from './transport.js';
5
+ export type HouseOpeningFrame = Extract<HouseStreamFrame, {
6
+ type: 'opening';
7
+ }>;
8
+ export type HouseAuthorityChangedFrame = Extract<HouseStreamFrame, {
9
+ type: 'authority_changed';
10
+ }>;
11
+ export type HouseDiscovering = {
12
+ opened(frame: HouseOpeningFrame | null): Promise<Discovery | null>;
13
+ changed(frame: HouseAuthorityChangedFrame): AuthorityChange | null;
14
+ expired?(authorities: readonly string[]): void;
9
15
  };
10
- export type HouseNotifications = SelectedRooms & {
16
+ export type HouseNotifications = {
17
+ runtime: string;
18
+ observe(notify: (event: RoomStreamEvent) => void): () => void;
19
+ send(rooms: readonly HeldRoom[]): void;
11
20
  track(authorities: readonly TrackedAuthority[]): void;
12
- hold(authority: string, at: LogPosition): void;
13
- advance(tracked: TrackedAuthority, from: LogPosition, holds?: HeldRevision): StreamedAdvancement | null;
14
21
  conversations: ConversationStream;
15
22
  serviceMode: ServiceModeSignal;
16
23
  };
17
- export declare function houseNotifications(transport: HouseDoor, mode: HouseMode): HouseNotifications;
18
- //# sourceMappingURL=stream.d.ts.map
24
+ export declare function houseNotifications(transport: HouseDoor, mode: HouseMode, discovering: HouseDiscovering, spaces: SpaceSchemas): HouseNotifications;
@@ -1 +1 @@
1
- {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAClB,WAAW,EAEX,aAAa,EAEb,iBAAiB,EAElB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,gBAAgB,EAAoB,MAAM,gBAAgB,CAAC;AAC/F,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAgB7D,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC;AAEvE,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,SAAS,gBAAgB,EAAE,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,aAAa,GAAG;IAC/C,KAAK,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,GAAG,IAAI,CAAC;IACtD,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,GAAG,IAAI,CAAC;IAC/C,OAAO,CACL,OAAO,EAAE,gBAAgB,EACzB,IAAI,EAAE,WAAW,EACjB,KAAK,CAAC,EAAE,YAAY,GACnB,mBAAmB,GAAG,IAAI,CAAC;IAC9B,aAAa,EAAE,kBAAkB,CAAC;IAClC,WAAW,EAAE,iBAAiB,CAAC;CAChC,CAAC;AAYF,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,GAAG,kBAAkB,CA2P5F"}
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,eAAe,EAEpB,KAAK,SAAS,EACd,KAAK,kBAAkB,EAEvB,KAAK,QAAQ,EAGb,KAAK,eAAe,EAEpB,KAAK,iBAAiB,EAEvB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAYtD,OAAO,EAEL,KAAK,SAAS,EAEd,KAAK,SAAS,EAGd,KAAK,gBAAgB,EACtB,MAAM,gBAAgB,CAAC;AAaxB,MAAM,MAAM,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,EAAE;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC;AAE/E,MAAM,MAAM,0BAA0B,GAAG,OAAO,CAAC,gBAAgB,EAAE;IAAE,IAAI,EAAE,mBAAmB,CAAA;CAAE,CAAC,CAAC;AAElG,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;IACnE,OAAO,CAAC,KAAK,EAAE,0BAA0B,GAAG,eAAe,GAAG,IAAI,CAAC;IACnE,OAAO,CAAC,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;CAChD,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IAC9D,IAAI,CAAC,KAAK,EAAE,SAAS,QAAQ,EAAE,GAAG,IAAI,CAAC;IACvC,KAAK,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,GAAG,IAAI,CAAC;IACtD,aAAa,EAAE,kBAAkB,CAAC;IAClC,WAAW,EAAE,iBAAiB,CAAC;CAChC,CAAC;AAwHF,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,SAAS,EACf,WAAW,EAAE,gBAAgB,EAC7B,MAAM,EAAE,YAAY,GACnB,kBAAkB,CA+XpB"}
package/dist/stream.js CHANGED
@@ -1,45 +1,179 @@
1
- import { acceptedMessage } from './actions.js';
2
- import { headChange } from './read.js';
3
- import { roomVersion } from './version.js';
4
- const HELD_FRAMES_MAX = 256;
1
+ import { ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { acceptedMessage, changeCursor, houseHistory } from './history.js';
3
+ import { keptSpaceSchemas, streamedModels } from './model.js';
4
+ import { answered, answering, binaryFileOf, createdOf, folded, forgotten, headChange, revised, } from './read.js';
5
+ import { classifyDoor } from './refusal.js';
6
+ import { houseDoorRoute, } from './transport.js';
7
+ import { DECIMAL, logPosition, presented, roomVersion } from './version.js';
5
8
  function turnOperation(frame) {
6
- const { type: _type, conversation_id: _conversation, turn_id: _turn, ...operation } = frame;
7
- return structuredClone(operation);
9
+ return 'at' in frame ? { op: 'begin', at: frame.at } : structuredClone(frame.operation);
10
+ }
11
+ async function houseAgents(transport) {
12
+ const answer = await transport.door(houseDoorRoute('agents/list'), {});
13
+ if (classifyDoor(answer.status, answer.body, 'agents/list')?.kind === 'transport-uncertainty') {
14
+ throw new Error();
15
+ }
16
+ const agents = answer.status === 200 ? answer.body?.agents : null;
17
+ if (!Array.isArray(agents))
18
+ return null;
19
+ return agents.flatMap((one) => {
20
+ const { id, name, cli, environment, environment_name: environmentName, environment_connected: connected, retired_at: retired, } = (one ?? {});
21
+ return typeof id === 'string' &&
22
+ typeof name === 'string' &&
23
+ typeof cli === 'string' &&
24
+ typeof environment === 'string' &&
25
+ typeof environmentName === 'string' &&
26
+ typeof connected === 'boolean'
27
+ ? [
28
+ {
29
+ agent: id,
30
+ name,
31
+ retired: retired !== null && retired !== undefined,
32
+ cli,
33
+ environment: {
34
+ identity: environment,
35
+ name: environmentName,
36
+ state: connected ? 'online' : 'offline',
37
+ },
38
+ },
39
+ ]
40
+ : [];
41
+ });
42
+ }
43
+ function listedConversation(listed) {
44
+ return {
45
+ conversation: listed.conversation_id,
46
+ agent: listed.agent_id,
47
+ agentName: listed.agent_name,
48
+ agentRetired: listed.agent_retired,
49
+ name: listed.name,
50
+ created: listed.created_at,
51
+ finished: listed.finished_at,
52
+ outcome: listed.outcome,
53
+ waiting: listed.waiting,
54
+ newest: listed.newest === null ? null : changeCursor(listed.newest),
55
+ processEnded: listed.process_ended_at,
56
+ };
8
57
  }
9
58
  function position(value) {
10
- if (!/^(?:0|[1-9][0-9]*)$/.test(value))
59
+ if (!DECIMAL.test(value))
11
60
  return null;
12
61
  return BigInt(value);
13
62
  }
14
- export function houseNotifications(transport, mode) {
15
- const stream = transport.stream;
16
- const lines = new Map();
17
- const locators = new Map();
63
+ function* changed(tracked, entries, version, schemas) {
64
+ const answer = answering();
65
+ for (const entry of entries) {
66
+ yield ROW_WEIGHT + (entry.text?.length ?? 0);
67
+ if (entry.kind === 'folder') {
68
+ folded(answer, entry.path, entry.removed === true);
69
+ continue;
70
+ }
71
+ if (entry.path === tracked.schema)
72
+ schemas.set(entry.path, entry);
73
+ forgotten(answer, entry.path);
74
+ if (entry.space !== undefined) {
75
+ answer.removals.add(entry.path);
76
+ continue;
77
+ }
78
+ if (entry.kind === 'removed') {
79
+ answer.removals.add(entry.path);
80
+ continue;
81
+ }
82
+ const binary = binaryFileOf(entry);
83
+ if (binary === undefined)
84
+ answer.documents.set(entry.path, entry.text ?? '');
85
+ else if (binary !== null)
86
+ answer.binaryFiles.set(entry.path, binary);
87
+ if (binary === undefined && entry.facts !== undefined)
88
+ answer.facts.set(entry.path, entry.facts);
89
+ const revision = entry.revision ?? binary?.sha256;
90
+ if (revision !== undefined)
91
+ revised(answer, entry.path, revision);
92
+ const head = headChange(entry);
93
+ if (head)
94
+ answer.changes.set(entry.path, head);
95
+ const created = createdOf(entry);
96
+ if (created)
97
+ answer.created.set(entry.path, created);
98
+ }
99
+ return { kind: 'net-advancement', roomVersion: version, ...(yield* answered(answer)) };
100
+ }
101
+ async function changePage(tracked, entries, version, mode, spaces, whole) {
18
102
  const schemas = new Map();
103
+ const page = await stepped(changed(tracked, entries, version, schemas));
104
+ const models = await streamedModels(schemas, tracked, mode, spaces, whole);
105
+ return models === undefined ? page : { ...page, models };
106
+ }
107
+ export function houseNotifications(transport, mode, discovering, spaces) {
108
+ const stream = transport.stream;
109
+ const staged = new Map();
110
+ let socket = null;
111
+ const tracking = new Map();
19
112
  const observers = new Set();
113
+ const replaying = new Map();
20
114
  const conversationObservers = new Set();
21
115
  const modeObservers = new Set();
22
116
  let replicated = [];
117
+ let listedCursor = null;
118
+ const unanswered = [];
119
+ const heldAtZero = new Set();
23
120
  const live = new Set();
24
- let listening = false;
25
- let catching = false;
26
- function listen() {
27
- if (listening)
28
- return;
29
- listening = true;
30
- stream.observe(receive);
121
+ let opening = null;
122
+ let announced = null;
123
+ const runtime = crypto.randomUUID();
124
+ function emit(event) {
125
+ for (const observer of observers) {
126
+ const held = replaying.get(observer);
127
+ if (held === undefined)
128
+ observer(event);
129
+ else
130
+ held.push(event);
131
+ }
31
132
  }
32
133
  function emitConversation(event) {
33
- if (event.kind !== 'gap' && event.kind !== 'accepted-message')
134
+ if (event.kind === 'turn' ||
135
+ event.kind === 'questions' ||
136
+ event.kind === 'process' ||
137
+ event.kind === 'commands' ||
138
+ event.kind === 'options' ||
139
+ event.kind === 'starting' ||
140
+ event.kind === 'state' ||
141
+ event.kind === 'link' ||
142
+ event.kind === 'deliveries') {
34
143
  live.add(event.conversation);
144
+ }
35
145
  for (const observer of conversationObservers)
36
146
  observer(event);
37
147
  }
38
- function reopened() {
148
+ function reopened(next) {
149
+ opening = next;
150
+ announced = null;
151
+ socket = next;
152
+ staged.clear();
153
+ unanswered.length = 0;
154
+ heldAtZero.clear();
39
155
  const gapped = new Set([...replicated, ...live]);
40
156
  live.clear();
41
157
  for (const conversation of gapped)
42
158
  emitConversation({ kind: 'gap', conversation });
159
+ emit({ kind: 'opened' });
160
+ if (mode === 'site')
161
+ announce(next, null);
162
+ }
163
+ function announce(via, frame) {
164
+ void discovering.opened(frame).then((discovery) => {
165
+ if (socket !== via)
166
+ return;
167
+ if (discovery === null) {
168
+ via.close();
169
+ return;
170
+ }
171
+ announced = { kind: 'discovered', discovery };
172
+ emit(announced);
173
+ }, () => {
174
+ if (socket === via)
175
+ via.close();
176
+ });
43
177
  }
44
178
  function converse(frame) {
45
179
  const conversation = frame.conversation_id;
@@ -58,6 +192,7 @@ export function houseNotifications(transport, mode) {
58
192
  return;
59
193
  emitConversation({ kind: 'commands', conversation, commands: [] });
60
194
  emitConversation({ kind: 'options', conversation, options: [] });
195
+ emitConversation({ kind: 'state', conversation, state: null });
61
196
  return;
62
197
  }
63
198
  if (frame.type === 'commands') {
@@ -72,6 +207,27 @@ export function houseNotifications(transport, mode) {
72
207
  emitConversation({ kind: 'options', conversation, options: structuredClone(frame.options) });
73
208
  return;
74
209
  }
210
+ if (frame.type === 'starting') {
211
+ emitConversation({ kind: 'starting', conversation, step: frame.step });
212
+ return;
213
+ }
214
+ if (frame.type === 'state') {
215
+ emitConversation({ kind: 'state', conversation, state: structuredClone(frame.state) });
216
+ return;
217
+ }
218
+ if (frame.type === 'link') {
219
+ emitConversation({ kind: 'link', conversation, connected: frame.connected });
220
+ return;
221
+ }
222
+ if (frame.type === 'deliveries') {
223
+ emitConversation({
224
+ kind: 'deliveries',
225
+ conversation,
226
+ waiting: [...frame.waiting],
227
+ unknown: [...frame.unknown],
228
+ });
229
+ return;
230
+ }
75
231
  if (frame.type === 'interactions') {
76
232
  emitConversation({
77
233
  kind: 'questions',
@@ -85,22 +241,33 @@ export function houseNotifications(transport, mode) {
85
241
  return;
86
242
  emitConversation({ kind: 'accepted-message', conversation, message });
87
243
  }
88
- function announce(authority, at, modelChanged) {
89
- const version = roomVersion(at);
90
- for (const locator of locators.get(authority) ?? []) {
91
- for (const observer of observers) {
92
- observer(modelChanged
93
- ? { locator, roomVersion: version, modelChanged: true }
94
- : { locator, roomVersion: version });
244
+ function stage(frame, via) {
245
+ const held = staged.get(frame.carry);
246
+ if (frame.index !== (held?.parts ?? 0)) {
247
+ staged.delete(frame.carry);
248
+ return;
249
+ }
250
+ if (held === undefined) {
251
+ for (const [carry, other] of staged) {
252
+ if (other.authority === frame.authority)
253
+ staged.delete(carry);
95
254
  }
96
255
  }
256
+ const next = held ?? { authority: frame.authority, parts: 0, entries: [] };
257
+ keptSpaceSchemas(frame.entries, spaces);
258
+ next.entries.push(...frame.entries);
259
+ next.parts += 1;
260
+ staged.set(frame.carry, next);
261
+ via?.received(frame.authority, frame.carry, frame.index);
97
262
  }
98
- function interrupt(authority) {
99
- const line = lines.get(authority);
100
- if (line !== undefined)
101
- lines.set(authority, { ...line, frames: [] });
263
+ function carried(frame) {
264
+ const held = staged.get(frame.carry);
265
+ staged.delete(frame.carry);
266
+ if ((held?.parts ?? 0) !== frame.parts)
267
+ return;
268
+ publish(frame, held?.entries ?? []);
102
269
  }
103
- function receive(frame) {
270
+ function handle(frame, via) {
104
271
  if (frame.type === 'signal') {
105
272
  const mode = frame.service_mode;
106
273
  if (mode !== 'open' && mode !== 'write-halted')
@@ -109,45 +276,99 @@ export function houseNotifications(transport, mode) {
109
276
  observer(mode);
110
277
  return;
111
278
  }
279
+ if (frame.type === 'opening') {
280
+ if (via !== null && mode === 'credential')
281
+ announce(via, frame);
282
+ return;
283
+ }
284
+ if (frame.type === 'caught_up') {
285
+ discovering.expired?.(frame.expired);
286
+ emit({
287
+ kind: 'caught-up',
288
+ expired: [
289
+ ...frame.expired.flatMap((authority) => (tracking.get(authority) ?? []).map((tracked) => tracked.locator)),
290
+ ...(unanswered.shift() ?? []),
291
+ ],
292
+ });
293
+ return;
294
+ }
295
+ if (frame.type === 'authority_changed') {
296
+ const change = discovering.changed(frame);
297
+ if (change !== null)
298
+ emit(change);
299
+ return;
300
+ }
301
+ if (frame.type === 'conversations') {
302
+ emitConversation({
303
+ kind: 'list',
304
+ entries: frame.conversations.map(listedConversation),
305
+ whole: frame.whole,
306
+ more: frame.more,
307
+ cursor: frame.cursor,
308
+ });
309
+ return;
310
+ }
311
+ if (frame.type === 'conversation') {
312
+ emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
313
+ return;
314
+ }
315
+ if (frame.type === 'conversation_deleted') {
316
+ emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
317
+ return;
318
+ }
112
319
  if (frame.type === 'turn' ||
113
320
  frame.type === 'process' ||
114
321
  frame.type === 'commands' ||
115
322
  frame.type === 'options' ||
323
+ frame.type === 'starting' ||
324
+ frame.type === 'state' ||
325
+ frame.type === 'link' ||
326
+ frame.type === 'deliveries' ||
116
327
  frame.type === 'interactions' ||
117
328
  frame.type === 'message') {
118
329
  converse(frame);
119
330
  return;
120
331
  }
121
- if (frame.type !== 'entries')
122
- return;
332
+ if (frame.type === 'part')
333
+ stage(frame, via);
334
+ else if (frame.type === 'carried')
335
+ carried(frame);
336
+ }
337
+ function publish(frame, entries) {
123
338
  const at = position(frame.position);
124
339
  const after = position(frame.after);
125
- if (at === null || after === null || after >= at) {
126
- interrupt(frame.authority);
340
+ if (at === null || after === null || after >= at)
127
341
  return;
342
+ const from = after === 0n && !heldAtZero.has(frame.authority)
343
+ ? null
344
+ : roomVersion({ position: frame.after, logEpoch: frame.log_epoch });
345
+ const version = roomVersion({ position: frame.position, logEpoch: frame.log_epoch });
346
+ for (const tracked of tracking.get(frame.authority) ?? []) {
347
+ emit({
348
+ kind: 'room-change',
349
+ locator: tracked.locator,
350
+ from,
351
+ roomVersion: version,
352
+ read: () => changePage(tracked, entries, version, mode, spaces, from === null),
353
+ });
128
354
  }
129
- const line = lines.get(frame.authority);
130
- if (line?.logEpoch === frame.log_epoch) {
131
- if (line.held !== null && at <= line.held)
132
- return;
133
- const last = line.frames.at(-1);
134
- if (last !== undefined && at <= last.position)
135
- return;
136
- const held = { after, position: at, entries: frame.entries };
137
- const next = last !== undefined && after <= last.position ? [...line.frames, held] : [held];
138
- lines.set(frame.authority, { ...line, frames: next.length > HELD_FRAMES_MAX ? [] : next });
139
- }
140
- else {
141
- interrupt(frame.authority);
142
- }
143
- const addresses = schemas.get(frame.authority) ?? [];
144
- announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch }, frame.entries.some((entry) => addresses.includes(entry.path)));
145
355
  }
356
+ stream.observe((frame) => handle(frame, socket));
357
+ stream.opened(reopened);
358
+ stream.closed(({ revoked, lost, authorityChanged }) => {
359
+ opening = null;
360
+ announced = null;
361
+ socket = null;
362
+ staged.clear();
363
+ unanswered.length = 0;
364
+ heldAtZero.clear();
365
+ emit({ kind: 'closed', revoked, lost, authorityChanged });
366
+ });
146
367
  return {
368
+ runtime,
147
369
  serviceMode: {
148
370
  observe(notify) {
149
371
  modeObservers.add(notify);
150
- listen();
151
372
  return () => {
152
373
  modeObservers.delete(notify);
153
374
  };
@@ -157,114 +378,94 @@ export function houseNotifications(transport, mode) {
157
378
  replicate(next) {
158
379
  replicated = [...next];
159
380
  },
381
+ hold(cursor) {
382
+ listedCursor = cursor;
383
+ },
160
384
  observe(notify) {
161
385
  conversationObservers.add(notify);
162
- listen();
163
- if (!catching) {
164
- catching = true;
165
- stream.opened(reopened);
166
- }
167
386
  return () => {
168
387
  conversationObservers.delete(notify);
169
388
  };
170
389
  },
171
- },
172
- track(entries) {
173
- locators.clear();
174
- schemas.clear();
175
- for (const entry of entries) {
176
- locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
177
- schemas.set(entry.authority, entry.schemas);
178
- if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
179
- lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
390
+ ...(mode === 'credential'
391
+ ? {
392
+ agents: () => houseAgents(transport),
393
+ history: (request) => houseHistory(transport, request),
180
394
  }
181
- }
182
- for (const authority of lines.keys()) {
183
- if (!locators.has(authority))
184
- lines.delete(authority);
185
- }
395
+ : {}),
186
396
  },
187
- select() { },
188
397
  observe(notify) {
189
398
  observers.add(notify);
190
- listen();
399
+ if (opening !== null) {
400
+ const replayed = [{ kind: 'opened' }];
401
+ if (announced !== null)
402
+ replayed.push(announced);
403
+ replaying.set(notify, replayed);
404
+ queueMicrotask(() => {
405
+ replaying.delete(notify);
406
+ for (const event of replayed)
407
+ if (observers.has(notify))
408
+ notify(event);
409
+ });
410
+ }
191
411
  return () => {
192
412
  observers.delete(notify);
193
413
  };
194
414
  },
195
- hold(authority, at) {
196
- const line = lines.get(authority);
197
- const held = position(at.position);
198
- if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
415
+ send(rooms) {
416
+ opening = null;
417
+ announced = null;
418
+ if (socket === null)
199
419
  return;
200
- lines.set(authority, { ...line, held });
201
- },
202
- advance(tracked, from, holds) {
203
- const line = lines.get(tracked.authority);
204
- const start = position(from.position);
205
- if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
206
- return null;
207
- const pending = line.frames.filter((frame) => frame.position > start);
208
- const first = pending[0];
209
- if (first === undefined || first.after > start)
210
- return null;
211
- const documents = {};
212
- const revisions = {};
213
- const changes = {};
214
- const removals = [];
215
- const applied = [];
216
- const forget = (path) => {
217
- delete documents[path];
218
- delete revisions[path];
219
- delete changes[path];
220
- const removed = removals.indexOf(path);
221
- if (removed !== -1)
222
- removals.splice(removed, 1);
223
- };
224
- for (const frame of pending) {
225
- for (const entry of frame.entries) {
226
- if (entry.kind === 'edited' &&
227
- entry.text === undefined &&
228
- entry.revision !== undefined &&
229
- holds?.(entry.path, entry.revision) === true) {
230
- forget(entry.path);
231
- continue;
232
- }
233
- applied.push(entry);
234
- if (entry.kind === 'removed') {
235
- forget(entry.path);
236
- removals.push(entry.path);
237
- continue;
238
- }
239
- if (entry.text === undefined)
240
- return null;
241
- if (mode === 'credential' && entry.revision === undefined)
242
- return null;
243
- const head = headChange(entry);
244
- if (head === null)
245
- return null;
246
- forget(entry.path);
247
- documents[entry.path] = entry.text;
248
- if (entry.revision !== undefined)
249
- revisions[entry.path] = entry.revision;
250
- if (head !== undefined)
251
- changes[entry.path] = head;
420
+ const entries = new Map([...tracking.values()].flat().map((tracked) => [tracked.locator, tracked]));
421
+ const named = rooms.flatMap((room) => {
422
+ const entry = entries.get(room.locator);
423
+ const at = room.roomVersion === null ? null : logPosition(room.roomVersion);
424
+ return entry === undefined || at === null ? [] : [{ entry, at }];
425
+ });
426
+ const unheld = rooms.flatMap((room) => {
427
+ const entry = entries.get(room.locator);
428
+ return entry === undefined || room.roomVersion !== null ? [] : [entry];
429
+ });
430
+ for (const { entry, at } of named) {
431
+ if (at.position === '0')
432
+ heldAtZero.add(entry.authority);
433
+ else
434
+ heldAtZero.delete(entry.authority);
435
+ }
436
+ for (const entry of unheld)
437
+ heldAtZero.delete(entry.authority);
438
+ const site = mode === 'site';
439
+ unanswered.push(site ? unheld.map((entry) => entry.locator) : []);
440
+ socket.hold([
441
+ ...named.map(({ entry, at }) => presented(entry.authority, at)),
442
+ ...(site ? [] : unheld).map((entry) => presented(entry.authority, { position: '0', logEpoch: entry.logEpoch })),
443
+ ], runtime, listedCursor);
444
+ if (site)
445
+ return;
446
+ const made = unheld.filter((entry) => entry.position === '0');
447
+ queueMicrotask(() => {
448
+ for (const entry of made) {
449
+ const version = roomVersion(entry);
450
+ emit({
451
+ kind: 'room-change',
452
+ locator: entry.locator,
453
+ from: null,
454
+ roomVersion: version,
455
+ read: () => changePage(entry, [], version, mode, spaces, true),
456
+ });
252
457
  }
458
+ });
459
+ },
460
+ track(entries) {
461
+ tracking.clear();
462
+ for (const entry of entries) {
463
+ tracking.set(entry.authority, [...(tracking.get(entry.authority) ?? []), entry]);
464
+ }
465
+ for (const [carry, held] of staged) {
466
+ if (!tracking.has(held.authority))
467
+ staged.delete(carry);
253
468
  }
254
- const last = pending.at(-1);
255
- lines.set(tracked.authority, { ...line, frames: pending, held: last.position });
256
- return {
257
- page: {
258
- kind: 'net-advancement',
259
- roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),
260
- documents,
261
- revisions,
262
- changes,
263
- binaryTargets: [],
264
- removals: removals.sort(),
265
- },
266
- entries: applied,
267
- };
268
469
  },
269
470
  };
270
471
  }