@agentshouse/mdruntime-adapter 0.1.0-cb42b23b5d2fb → 0.1.0-cb44dd4d174e6

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