@agentshouse/mdruntime 0.1.0-c3903a68dcbbf → 0.1.0-c3a5b012b4a8c

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 (197) hide show
  1. package/README.md +26 -13
  2. package/dist/adapter.d.ts +244 -88
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/adapter.js +1 -9
  5. package/dist/attached-model.d.ts +8 -0
  6. package/dist/attached-model.d.ts.map +1 -0
  7. package/dist/attached-model.js +46 -0
  8. package/dist/attempt.d.ts +16 -0
  9. package/dist/attempt.d.ts.map +1 -0
  10. package/dist/attempt.js +70 -0
  11. package/dist/binary.d.ts +18 -0
  12. package/dist/binary.d.ts.map +1 -0
  13. package/dist/binary.js +8 -0
  14. package/dist/bootstrap.d.ts +40 -6
  15. package/dist/bootstrap.d.ts.map +1 -1
  16. package/dist/bootstrap.js +336 -52
  17. package/dist/chain.d.ts +12 -0
  18. package/dist/chain.d.ts.map +1 -0
  19. package/dist/chain.js +64 -0
  20. package/dist/change-set.d.ts +9 -14
  21. package/dist/change-set.d.ts.map +1 -1
  22. package/dist/change-set.js +11 -60
  23. package/dist/change.d.ts +46 -0
  24. package/dist/change.d.ts.map +1 -1
  25. package/dist/change.js +34 -0
  26. package/dist/client.d.ts +44 -24
  27. package/dist/client.d.ts.map +1 -1
  28. package/dist/client.js +161 -36
  29. package/dist/collection.d.ts +13 -16
  30. package/dist/collection.d.ts.map +1 -1
  31. package/dist/collection.js +52 -66
  32. package/dist/compile.d.ts +11 -0
  33. package/dist/compile.d.ts.map +1 -0
  34. package/dist/compile.js +717 -0
  35. package/dist/connection.d.ts +69 -44
  36. package/dist/connection.d.ts.map +1 -1
  37. package/dist/connection.js +4581 -1472
  38. package/dist/context.d.ts +2 -0
  39. package/dist/context.d.ts.map +1 -0
  40. package/dist/context.js +7 -0
  41. package/dist/conversation-row.d.ts +33 -0
  42. package/dist/conversation-row.d.ts.map +1 -0
  43. package/dist/conversation-row.js +47 -0
  44. package/dist/conversation.d.ts +76 -27
  45. package/dist/conversation.d.ts.map +1 -1
  46. package/dist/conversation.js +1052 -273
  47. package/dist/digest.d.ts +7 -0
  48. package/dist/digest.d.ts.map +1 -0
  49. package/dist/digest.js +113 -0
  50. package/dist/durability.d.ts +10 -1
  51. package/dist/durability.d.ts.map +1 -1
  52. package/dist/durability.js +5 -0
  53. package/dist/entry.d.ts +20 -0
  54. package/dist/entry.d.ts.map +1 -0
  55. package/dist/entry.js +161 -0
  56. package/dist/expectation.d.ts +21 -0
  57. package/dist/expectation.d.ts.map +1 -0
  58. package/dist/expectation.js +70 -0
  59. package/dist/failure.d.ts +13 -0
  60. package/dist/failure.d.ts.map +1 -0
  61. package/dist/failure.js +19 -0
  62. package/dist/file.d.ts +2 -19
  63. package/dist/file.d.ts.map +1 -1
  64. package/dist/file.js +7 -122
  65. package/dist/host.d.ts +17 -3
  66. package/dist/host.d.ts.map +1 -1
  67. package/dist/host.js +163 -75
  68. package/dist/identity.d.ts +3 -0
  69. package/dist/identity.d.ts.map +1 -1
  70. package/dist/identity.js +13 -1
  71. package/dist/index.d.ts +26 -11
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +12 -1
  74. package/dist/job.d.ts +33 -0
  75. package/dist/job.d.ts.map +1 -0
  76. package/dist/job.js +54 -0
  77. package/dist/live.d.ts +23 -0
  78. package/dist/live.d.ts.map +1 -0
  79. package/dist/live.js +165 -0
  80. package/dist/media.d.ts +8 -0
  81. package/dist/media.d.ts.map +1 -0
  82. package/dist/media.js +218 -0
  83. package/dist/memory.d.ts +30 -9
  84. package/dist/memory.d.ts.map +1 -1
  85. package/dist/memory.js +337 -87
  86. package/dist/messages.d.ts +52 -0
  87. package/dist/messages.d.ts.map +1 -0
  88. package/dist/messages.js +168 -0
  89. package/dist/model.d.ts +17 -16
  90. package/dist/model.d.ts.map +1 -1
  91. package/dist/model.js +9 -9
  92. package/dist/name.d.ts +4 -0
  93. package/dist/name.d.ts.map +1 -0
  94. package/dist/name.js +122 -0
  95. package/dist/observation.d.ts +22 -2
  96. package/dist/observation.d.ts.map +1 -1
  97. package/dist/observation.js +133 -17
  98. package/dist/open.d.ts +2 -0
  99. package/dist/open.d.ts.map +1 -0
  100. package/dist/open.js +1 -0
  101. package/dist/opened.d.ts +22 -0
  102. package/dist/opened.d.ts.map +1 -0
  103. package/dist/opened.js +107 -0
  104. package/dist/outcome.d.ts +72 -21
  105. package/dist/outcome.d.ts.map +1 -1
  106. package/dist/outcome.js +81 -1
  107. package/dist/overlay.d.ts +16 -5
  108. package/dist/overlay.d.ts.map +1 -1
  109. package/dist/overlay.js +147 -180
  110. package/dist/parts.d.ts +59 -0
  111. package/dist/parts.d.ts.map +1 -0
  112. package/dist/parts.js +118 -0
  113. package/dist/paths.d.ts +1 -1
  114. package/dist/paths.d.ts.map +1 -1
  115. package/dist/paths.js +4 -16
  116. package/dist/query.d.ts +56 -180
  117. package/dist/query.d.ts.map +1 -1
  118. package/dist/query.js +3 -422
  119. package/dist/questions.d.ts +32 -0
  120. package/dist/questions.d.ts.map +1 -0
  121. package/dist/questions.js +118 -0
  122. package/dist/reach.d.ts +54 -0
  123. package/dist/reach.d.ts.map +1 -0
  124. package/dist/reach.js +483 -0
  125. package/dist/react.d.ts +45 -0
  126. package/dist/react.d.ts.map +1 -0
  127. package/dist/react.js +54 -0
  128. package/dist/read.d.ts +7 -0
  129. package/dist/read.d.ts.map +1 -0
  130. package/dist/read.js +17 -0
  131. package/dist/readiness.d.ts +50 -0
  132. package/dist/readiness.d.ts.map +1 -0
  133. package/dist/readiness.js +27 -0
  134. package/dist/recovery.d.ts +26 -0
  135. package/dist/recovery.d.ts.map +1 -0
  136. package/dist/recovery.js +232 -0
  137. package/dist/refresh.d.ts +21 -7
  138. package/dist/refresh.d.ts.map +1 -1
  139. package/dist/refresh.js +155 -55
  140. package/dist/replica.d.ts +61 -33
  141. package/dist/replica.d.ts.map +1 -1
  142. package/dist/replica.js +355 -162
  143. package/dist/room-draft.d.ts +62 -11
  144. package/dist/room-draft.d.ts.map +1 -1
  145. package/dist/room-draft.js +281 -83
  146. package/dist/rows.d.ts +32 -34
  147. package/dist/rows.d.ts.map +1 -1
  148. package/dist/rows.js +65 -87
  149. package/dist/save.d.ts +74 -6
  150. package/dist/save.d.ts.map +1 -1
  151. package/dist/save.js +902 -183
  152. package/dist/search.d.ts +15 -54
  153. package/dist/search.d.ts.map +1 -1
  154. package/dist/search.js +10 -121
  155. package/dist/seed.d.ts +16 -0
  156. package/dist/seed.d.ts.map +1 -0
  157. package/dist/seed.js +75 -0
  158. package/dist/shared.d.ts +15 -0
  159. package/dist/shared.d.ts.map +1 -0
  160. package/dist/shared.js +421 -0
  161. package/dist/source.d.ts +61 -0
  162. package/dist/source.d.ts.map +1 -0
  163. package/dist/source.js +81 -0
  164. package/dist/status.d.ts +7 -4
  165. package/dist/status.d.ts.map +1 -1
  166. package/dist/status.js +3 -15
  167. package/dist/store.d.ts +178 -80
  168. package/dist/store.d.ts.map +1 -1
  169. package/dist/store.js +372 -381
  170. package/dist/stored.d.ts +156 -0
  171. package/dist/stored.d.ts.map +1 -0
  172. package/dist/stored.js +637 -0
  173. package/dist/tables.d.ts +388 -0
  174. package/dist/tables.d.ts.map +1 -0
  175. package/dist/tables.js +706 -0
  176. package/dist/transfer.d.ts +0 -5
  177. package/dist/transfer.d.ts.map +1 -1
  178. package/dist/turn.d.ts +9 -0
  179. package/dist/turn.d.ts.map +1 -0
  180. package/dist/turn.js +75 -0
  181. package/dist/validation.d.ts +29 -0
  182. package/dist/validation.d.ts.map +1 -0
  183. package/dist/validation.js +104 -0
  184. package/dist/windows.d.ts +11 -0
  185. package/dist/windows.d.ts.map +1 -0
  186. package/dist/windows.js +16 -0
  187. package/dist/wire.d.ts +199 -19
  188. package/dist/wire.d.ts.map +1 -1
  189. package/dist/wire.js +92 -67
  190. package/package.json +33 -10
  191. package/AGENTS.md +0 -210
  192. package/dist/attachment.d.ts +0 -3
  193. package/dist/attachment.d.ts.map +0 -1
  194. package/dist/attachment.js +0 -8
  195. package/dist/read-side.d.ts +0 -14
  196. package/dist/read-side.d.ts.map +0 -1
  197. package/dist/read-side.js +0 -371
package/dist/memory.js CHANGED
@@ -1,9 +1,12 @@
1
- import { ROOM_CONTRACT_VERSION, } from './adapter.js';
2
- import { MD_MODEL } from './collection.js';
1
+ import { documentFacts, parse } from '@agentshouse/mdmodel';
2
+ import { NO_ROOM_TYPES, schemaTypes } from './attached-model.js';
3
3
  import { sameChange } from './overlay.js';
4
+ const ATTACHMENTS = 'attachments';
5
+ function lastName(path) {
6
+ return path.slice(path.lastIndexOf('/') + 1);
7
+ }
4
8
  const FIXTURE_RETENTION = {
5
9
  history: { kind: 'duration', seconds: 1 },
6
- outcome: 1,
7
10
  refreshHorizon: 1,
8
11
  };
9
12
  function declaredRetention(value) {
@@ -15,6 +18,14 @@ function declaredRetention(value) {
15
18
  }
16
19
  throw new Error('history retention duration must be a finite positive number of seconds');
17
20
  }
21
+ const MODEL_ABSENT = { kind: 'model-unavailable', reason: 'not-readable' };
22
+ function modelsOf(room) {
23
+ return { collection: room.model ?? MODEL_ABSENT };
24
+ }
25
+ function changedModels(room, from) {
26
+ const now = modelsOf(room);
27
+ return JSON.stringify(now) === JSON.stringify(modelsOf(from)) ? undefined : now;
28
+ }
18
29
  function named(source, paths) {
19
30
  const taken = {};
20
31
  for (const path of paths) {
@@ -31,8 +42,6 @@ async function resolveScript(answer, fallback, request) {
31
42
  }
32
43
  export function createMemoryAdapter(options = {}) {
33
44
  let rooms = options.rooms === undefined ? [] : [...options.rooms];
34
- const mdModel = options.mdModel ?? MD_MODEL;
35
- const supportedVersions = options.supportedVersions ?? [ROOM_CONTRACT_VERSION];
36
45
  const capabilities = options.capabilities ?? [];
37
46
  const providerLimits = options.providerLimits ?? [];
38
47
  const retention = declaredRetention(options.retention);
@@ -44,11 +53,16 @@ export function createMemoryAdapter(options = {}) {
44
53
  const listeners = new Set();
45
54
  const modeObservers = new Set();
46
55
  const scripted = new Map();
47
- const unsealed = new Map();
48
- const sealed = new Set();
56
+ const sentTurns = new Map();
49
57
  const historyPageSize = options.historyPageSize;
50
- let selected = [];
58
+ const attached = new Map();
59
+ const linked = new WeakMap();
60
+ const carried = new Map();
61
+ let expiring = [];
62
+ let openings = 0;
63
+ const declaredRooms = new Set();
51
64
  let replicated = [];
65
+ let heldCursor = null;
52
66
  let issued = 0;
53
67
  function scriptConversations(entries) {
54
68
  scripted.clear();
@@ -76,8 +90,48 @@ export function createMemoryAdapter(options = {}) {
76
90
  const wanted = ordinals.get(`${locator}\u0000${target}`);
77
91
  return held !== undefined && wanted !== undefined && held >= wanted;
78
92
  }
93
+ function heldBlob(room, path) {
94
+ return attached.get(`${room.locator}\u0000${path}`) ?? room.blobs?.[path];
95
+ }
96
+ function freePath(room, name) {
97
+ const dot = name.lastIndexOf('.');
98
+ const stem = dot > 0 ? name.slice(0, dot) : name;
99
+ const extension = dot > 0 ? name.slice(dot) : '';
100
+ for (let count = 1;; count += 1) {
101
+ const path = `${ATTACHMENTS}/${count === 1 ? stem : `${stem}-${count}`}${extension}`;
102
+ if (heldBlob(room, path) === undefined && room.documents?.[path] === undefined)
103
+ return path;
104
+ }
105
+ }
106
+ const saved = new WeakMap();
107
+ function factsAt(room, path, content) {
108
+ const types = schemaTypes(room.model?.kind === 'model' ? room.model.snapshot.schema : null, NO_ROOM_TYPES);
109
+ let held = saved.get(types);
110
+ if (held === undefined) {
111
+ held = new Map();
112
+ saved.set(types, held);
113
+ }
114
+ const key = `${path}\u0000${content}`;
115
+ if (!held.has(key)) {
116
+ const facts = documentFacts(path, parse(content, path), { types });
117
+ held.set(key, facts === null ? null : JSON.parse(JSON.stringify(facts)));
118
+ }
119
+ return held.get(key);
120
+ }
121
+ function factsIn(room, paths) {
122
+ const facts = {};
123
+ for (const path of paths) {
124
+ const content = room.documents?.[path];
125
+ const held = room.facts?.[path] ?? (content === undefined ? null : factsAt(room, path, content));
126
+ if (held !== null)
127
+ facts[path] = held;
128
+ }
129
+ return facts;
130
+ }
79
131
  function retain(entries) {
80
132
  for (const room of entries) {
133
+ for (const [path, content] of Object.entries(room.documents ?? {}))
134
+ factsAt(room, path, content);
81
135
  note(room.locator, room);
82
136
  const versions = served.get(room.locator) ?? new Map();
83
137
  versions.set(room.roomVersion, room);
@@ -89,43 +143,64 @@ export function createMemoryAdapter(options = {}) {
89
143
  return {
90
144
  name: room.name,
91
145
  locator: room.locator,
146
+ handle: room.handle,
92
147
  roomVersion: room.roomVersion,
93
148
  operations: room.operations,
94
149
  replica: room.replica,
95
- attachedModel: room.attachedModel,
96
150
  ...(room.typedSelection === undefined ? {} : { typedSelection: room.typedSelection }),
97
- ...(room.untypedFolders === undefined ? {} : { untypedFolders: room.untypedFolders }),
98
151
  providerOwnedPaths: room.providerOwnedPaths,
99
152
  };
100
153
  }
101
- function page(room, from) {
154
+ function page(room, after) {
102
155
  const paths = Object.keys(room.documents ?? {}).sort();
156
+ const opening = after === undefined;
157
+ const from = opening ? 0 : paths.filter((path) => path <= after).length;
103
158
  const size = pageSize ?? paths.length;
104
159
  const taken = paths.slice(from, from + size);
105
160
  const next = from + taken.length;
106
- const targets = from === 0 ? (room.binaryTargets ?? []) : [];
161
+ const targets = opening ? Object.keys(room.binaryFiles ?? {}) : [];
107
162
  return {
108
163
  kind: 'bootstrap',
109
164
  roomVersion: room.roomVersion,
165
+ ...(opening ? { models: modelsOf(room) } : {}),
110
166
  documents: named(room.documents ?? {}, taken),
111
- revisions: named(room.revisions ?? {}, taken),
167
+ facts: factsIn(room, taken),
168
+ revisions: named(room.revisions ?? {}, [...taken, ...targets]),
169
+ ...(room.fileIds === undefined
170
+ ? {}
171
+ : { fileIds: named(room.fileIds, [...taken, ...targets]) }),
172
+ ...(room.created === undefined
173
+ ? {}
174
+ : { created: named(room.created, [...taken, ...targets]) }),
112
175
  changes: named(room.changes ?? {}, [...taken, ...targets]),
113
- binaryTargets: targets,
114
- ...(from === 0 && room.attachments !== undefined ? { attachments: room.attachments } : {}),
115
- ...(next < paths.length ? { pageToken: String(next) } : {}),
176
+ binaryFiles: named(room.binaryFiles ?? {}, targets),
177
+ ...(opening && room.folders !== undefined ? { folders: room.folders } : {}),
178
+ ...(next < paths.length ? { pageToken: taken.at(-1) } : {}),
179
+ };
180
+ }
181
+ function foldersMoved(room, from) {
182
+ const now = new Set(room.folders ?? []);
183
+ const before = new Set(from.folders ?? []);
184
+ const folders = [...now].filter((folder) => !before.has(folder)).sort();
185
+ const folderRemovals = [...before].filter((folder) => !now.has(folder)).sort();
186
+ return {
187
+ ...(folders.length === 0 ? {} : { folders }),
188
+ ...(folderRemovals.length === 0 ? {} : { folderRemovals }),
116
189
  };
117
190
  }
118
- function advancement(room, from, at) {
191
+ function advancement(room, from, at, whole = false) {
119
192
  const current = room.documents ?? {};
120
193
  const previous = from.documents ?? {};
121
- const currentTargets = new Set(room.binaryTargets ?? []);
122
- const previousTargets = new Set(from.binaryTargets ?? []);
194
+ const currentTargets = new Set(Object.keys(room.binaryFiles ?? {}));
195
+ const previousTargets = new Set(Object.keys(from.binaryFiles ?? {}));
123
196
  const headMoved = (path) => room.revisions?.[path] !== from.revisions?.[path] ||
197
+ room.fileIds?.[path] !== from.fileIds?.[path] ||
198
+ room.created?.[path] !== from.created?.[path] ||
124
199
  !sameChange(room.changes?.[path], from.changes?.[path]);
125
200
  const changed = Object.keys(current)
126
201
  .filter((path) => current[path] !== previous[path] || headMoved(path))
127
202
  .sort();
128
- const size = pageSize ?? changed.length;
203
+ const size = whole ? changed.length : (pageSize ?? changed.length);
129
204
  const taken = changed.slice(at, at + size);
130
205
  const next = at + taken.length;
131
206
  const opening = at === 0;
@@ -135,25 +210,52 @@ export function createMemoryAdapter(options = {}) {
135
210
  const targets = opening
136
211
  ? [...currentTargets].filter((target) => !previousTargets.has(target) || headMoved(target))
137
212
  : [];
138
- const attachments = room.attachments ?? [];
139
- const declared = opening && JSON.stringify(attachments) !== JSON.stringify(from.attachments ?? []);
213
+ const models = opening ? changedModels(room, from) : undefined;
140
214
  return {
141
215
  kind: 'net-advancement',
142
216
  roomVersion: room.roomVersion,
217
+ ...(models === undefined ? {} : { models }),
143
218
  documents: named(current, taken),
144
- revisions: named(room.revisions ?? {}, taken),
219
+ facts: factsIn(room, taken),
220
+ revisions: named(room.revisions ?? {}, [...taken, ...targets]),
221
+ ...(room.fileIds === undefined
222
+ ? {}
223
+ : { fileIds: named(room.fileIds, [...taken, ...targets]) }),
224
+ ...(room.created === undefined
225
+ ? {}
226
+ : { created: named(room.created, [...taken, ...targets]) }),
145
227
  changes: named(room.changes ?? {}, [...taken, ...targets]),
146
- binaryTargets: targets,
228
+ binaryFiles: named(room.binaryFiles ?? {}, targets),
229
+ ...(opening ? foldersMoved(room, from) : {}),
147
230
  removals: opening
148
231
  ? [
149
232
  ...Object.keys(previous).filter((path) => !(path in current) && !currentTargets.has(path)),
150
233
  ...[...previousTargets].filter((target) => !currentTargets.has(target) && !(target in current)),
151
234
  ].sort()
152
235
  : [],
153
- ...(declared ? { attachments } : {}),
154
236
  ...(pageToken === undefined ? {} : { pageToken }),
155
237
  };
156
238
  }
239
+ function carry(locator) {
240
+ const at = carried.get(locator);
241
+ const current = rooms.find((entry) => entry.locator === locator);
242
+ if (at === undefined || current === undefined || current.roomVersion === at)
243
+ return;
244
+ const from = served.get(locator)?.get(at);
245
+ carried.set(locator, current.roomVersion);
246
+ if (from === undefined)
247
+ return;
248
+ const page = advancement(current, from, 0, true);
249
+ for (const observer of observers) {
250
+ observer({
251
+ kind: 'room-change',
252
+ locator,
253
+ from: at,
254
+ roomVersion: current.roomVersion,
255
+ read: async () => page,
256
+ });
257
+ }
258
+ }
157
259
  function pageOf(conversation, after) {
158
260
  const messages = conversation.messages ?? [];
159
261
  const first = typeof after === 'string' ? messages.findIndex((message) => message.position > after) : 0;
@@ -168,16 +270,13 @@ export function createMemoryAdapter(options = {}) {
168
270
  : {}),
169
271
  };
170
272
  }
171
- function historiesOf(input) {
172
- const asked = input?.conversations;
173
- if (!Array.isArray(asked))
174
- return { kind: 'rejected', code: 'invalid_request', fields: {} };
273
+ function historiesOf(request) {
175
274
  const histories = [];
176
- for (const entry of asked) {
177
- const conversation = typeof entry.conversation === 'string' ? scripted.get(entry.conversation) : undefined;
275
+ for (const entry of request.conversations) {
276
+ const conversation = scripted.get(entry.conversation);
178
277
  if (conversation === undefined)
179
278
  continue;
180
- const outcome = conversation.outcomes?.['conversation.history'];
279
+ const outcome = conversation.outcomes?.history;
181
280
  if (outcome !== undefined)
182
281
  return outcome;
183
282
  histories.push(pageOf(conversation, entry.after));
@@ -188,52 +287,94 @@ export function createMemoryAdapter(options = {}) {
188
287
  function conversationAction(request) {
189
288
  const name = request.name;
190
289
  const input = (request.input ?? {});
191
- if (name === 'conversation.history')
192
- return historiesOf(request.input);
193
- if (name === 'conversation.create') {
290
+ if (name === 'conversations/create') {
194
291
  issued += 1;
195
292
  const conversation = `conversation-${issued}`;
196
293
  scripted.set(conversation, { conversation });
197
- return { kind: 'action', result: { conversation } };
294
+ return { kind: 'action', result: { conversation_id: conversation } };
198
295
  }
199
- const named = typeof input.conversation === 'string' ? input.conversation : '';
296
+ const named = typeof input.conversation_id === 'string' ? input.conversation_id : '';
200
297
  const conversation = scripted.get(named);
201
298
  if (conversation === undefined) {
202
- return { kind: 'rejected', code: 'unknown-conversation', fields: { conversation: named } };
299
+ return {
300
+ kind: 'rejected',
301
+ code: 'unknown-conversation',
302
+ fields: { conversation: named },
303
+ retryable: false,
304
+ };
203
305
  }
204
306
  const outcome = conversation.outcomes?.[name];
205
307
  if (outcome !== undefined)
206
308
  return outcome;
207
- if (name === 'conversation.send') {
309
+ if (name === 'conversations/send') {
310
+ const operation = typeof input.operation_id === 'string' ? input.operation_id : null;
311
+ const answered = operation === null ? undefined : sentTurns.get(operation);
312
+ if (answered !== undefined)
313
+ return { kind: 'action', result: { message_id: answered } };
208
314
  issued += 1;
209
315
  const turn = `turn-${issued}`;
210
- unsealed.set(turn, named);
211
- return { kind: 'action', result: { turn } };
212
- }
213
- if (name === 'conversation.cancel') {
214
- const turn = typeof input.turn === 'string' ? input.turn : '';
215
- if (sealed.has(turn) || !unsealed.has(turn)) {
216
- return { kind: 'rejected', code: 'turn-sealed', fields: { turn } };
217
- }
218
- unsealed.delete(turn);
219
- return { kind: 'action', result: { turn, cancelled: true } };
316
+ if (operation !== null)
317
+ sentTurns.set(operation, turn);
318
+ return { kind: 'action', result: { message_id: turn } };
220
319
  }
221
- if (name === 'conversation.delete')
320
+ if (name === 'conversations/delete')
222
321
  scripted.delete(named);
223
- return { kind: 'action', result: { conversation: named } };
322
+ return { kind: 'action', result: { conversation_id: named } };
323
+ }
324
+ function announceList() {
325
+ const entries = [...scripted.values()].flatMap(({ conversation, listed, messages = [] }) => listed === undefined
326
+ ? []
327
+ : [{ conversation, ...listed, newest: messages.at(-1)?.position ?? null }]);
328
+ for (const listener of listeners) {
329
+ listener({ kind: 'list', entries, whole: true, more: false, cursor: null });
330
+ }
224
331
  }
225
332
  function discovery() {
226
333
  return {
227
334
  kind: 'discovery',
228
- supportedVersions,
229
- mdModel,
230
335
  rooms: rooms.map(declared),
231
336
  capabilities,
232
337
  providerLimits,
233
338
  retention,
339
+ ...(options.member === undefined ? {} : { member: options.member }),
234
340
  };
235
341
  }
342
+ function discovered() {
343
+ return resolveScript(scripts.discover, discovery, undefined).then((answer) => {
344
+ if (answer.kind === 'discovery')
345
+ for (const room of answer.rooms)
346
+ note(room.locator, room);
347
+ return answer;
348
+ });
349
+ }
350
+ function opened(notify) {
351
+ const opening = ++openings;
352
+ notify({ kind: 'opened' });
353
+ void discovered()
354
+ .catch(() => ({ kind: 'transport-uncertainty' }))
355
+ .then((answer) => {
356
+ if (opening !== openings)
357
+ return;
358
+ if (answer.kind !== 'discovery') {
359
+ openings += 1;
360
+ carried.clear();
361
+ const revoked = answer.kind === 'revoked-authorization';
362
+ notify({ kind: 'closed', revoked, lost: !revoked, authorityChanged: false });
363
+ return;
364
+ }
365
+ declaredRooms.clear();
366
+ for (const room of answer.rooms)
367
+ declaredRooms.add(room.locator);
368
+ notify({ kind: 'discovered', discovery: answer });
369
+ });
370
+ }
371
+ function emitAll(event) {
372
+ for (const observer of observers)
373
+ observer(event);
374
+ }
236
375
  return {
376
+ ...(options.roomTypes === undefined ? {} : { roomTypes: options.roomTypes }),
377
+ ...(options.typedPaths === undefined ? {} : { typedPaths: options.typedPaths }),
237
378
  setRooms(next) {
238
379
  rooms = [...next];
239
380
  retain(rooms);
@@ -241,15 +382,38 @@ export function createMemoryAdapter(options = {}) {
241
382
  setConversations(next) {
242
383
  scriptConversations(next);
243
384
  },
244
- notifyRoomVersion(notification) {
245
- for (const observer of observers)
246
- observer(notification);
385
+ open() {
386
+ carried.clear();
387
+ announceList();
388
+ opened(emitAll);
389
+ },
390
+ close({ revoked = false, lost = !revoked, authorityChanged = false } = {}) {
391
+ openings += 1;
392
+ carried.clear();
393
+ emitAll({ kind: 'closed', revoked, lost, authorityChanged });
394
+ },
395
+ expire(locators) {
396
+ expiring = [...locators];
397
+ },
398
+ notifyRoomVersion({ locator }) {
399
+ carry(locator);
400
+ },
401
+ notifyAuthorityChanged(locators) {
402
+ const named = new Set(locators ?? [...declaredRooms, ...rooms.map((room) => room.locator)]);
403
+ const listed = rooms.filter((room) => named.has(room.locator));
404
+ const left = [...named].filter((locator) => !listed.some((room) => room.locator === locator));
405
+ for (const room of listed)
406
+ declaredRooms.add(room.locator);
407
+ for (const locator of left)
408
+ declaredRooms.delete(locator);
409
+ emitAll({
410
+ kind: 'authority-changed',
411
+ rooms: listed.map(declared),
412
+ left,
413
+ ...(options.member === undefined ? {} : { member: options.member }),
414
+ });
247
415
  },
248
416
  notifyConversation(event) {
249
- if (event.kind === 'accepted-message') {
250
- sealed.add(event.message.turn);
251
- unsealed.delete(event.message.turn);
252
- }
253
417
  for (const listener of listeners)
254
418
  listener(event);
255
419
  },
@@ -257,22 +421,32 @@ export function createMemoryAdapter(options = {}) {
257
421
  for (const observer of modeObservers)
258
422
  observer(mode);
259
423
  },
260
- selectedRooms() {
261
- return selected;
424
+ heldRooms() {
425
+ return [...carried.keys()].sort();
262
426
  },
263
427
  replicatedConversations() {
264
428
  return replicated;
265
429
  },
430
+ heldConversationCursor() {
431
+ return heldCursor;
432
+ },
266
433
  conversations: {
267
434
  replicate(next) {
268
435
  replicated = [...next];
269
436
  },
437
+ hold(cursor) {
438
+ heldCursor = cursor;
439
+ },
270
440
  observe(notify) {
271
441
  listeners.add(notify);
272
442
  return () => {
273
443
  listeners.delete(notify);
274
444
  };
275
445
  },
446
+ history: (request) => Promise.resolve(historiesOf(request)),
447
+ ...(options.agents === undefined
448
+ ? {}
449
+ : { agents: () => Promise.resolve(structuredClone(options.agents)) }),
276
450
  },
277
451
  serviceMode: {
278
452
  observe(notify) {
@@ -282,12 +456,49 @@ export function createMemoryAdapter(options = {}) {
282
456
  };
283
457
  },
284
458
  },
285
- roomVersions: {
286
- select(locators) {
287
- selected = [...locators];
459
+ roomStream: {
460
+ hold(asked) {
461
+ const named = asked.map((room) => room.locator);
462
+ const unknown = asked.flatMap((room) => {
463
+ const current = rooms.find((entry) => entry.locator === room.locator);
464
+ const version = room.roomVersion;
465
+ return current === undefined ||
466
+ (version !== null &&
467
+ (current.roomVersion === version || served.get(room.locator)?.has(version) === true))
468
+ ? []
469
+ : [room.locator];
470
+ });
471
+ const expired = [
472
+ ...new Set([...expiring.filter((locator) => named.includes(locator)), ...unknown]),
473
+ ];
474
+ expiring = expiring.filter((locator) => !named.includes(locator));
475
+ queueMicrotask(() => {
476
+ for (const room of asked) {
477
+ if (expired.includes(room.locator) || room.roomVersion === null)
478
+ carried.delete(room.locator);
479
+ else {
480
+ carried.set(room.locator, room.roomVersion);
481
+ carry(room.locator);
482
+ }
483
+ }
484
+ emitAll({ kind: 'caught-up', expired });
485
+ });
288
486
  },
289
487
  observe(notify) {
290
488
  observers.add(notify);
489
+ queueMicrotask(() => {
490
+ if (!observers.has(notify))
491
+ return;
492
+ if (options.unopened === true) {
493
+ notify({ kind: 'closed', revoked: false, lost: true, authorityChanged: false });
494
+ return;
495
+ }
496
+ announceList();
497
+ opened((event) => {
498
+ if (observers.has(notify))
499
+ notify(event);
500
+ });
501
+ });
291
502
  return () => {
292
503
  observers.delete(notify);
293
504
  };
@@ -299,32 +510,28 @@ export function createMemoryAdapter(options = {}) {
299
510
  else
300
511
  scripts[operation] = answer;
301
512
  },
302
- async model(_authorization, request) {
303
- return resolveScript(scripts.model, () => {
304
- const room = rooms.find((entry) => entry.locator === request.locator);
305
- const answer = request.attached === 'collection' ? room?.model : room?.selectionModel;
306
- return answer ?? { kind: 'model-unavailable', reason: 'not-readable' };
307
- }, request);
308
- },
309
513
  async authorize() {
310
514
  return resolveScript(scripts.authorize, () => ({}), undefined);
311
515
  },
312
516
  async discover(_authorization) {
313
- return resolveScript(scripts.discover, discovery, undefined).then((answer) => {
314
- if (answer.kind === 'discovery')
315
- for (const room of answer.rooms)
316
- note(room.locator, room);
317
- return answer;
318
- });
517
+ return discovered();
319
518
  },
320
519
  reached,
520
+ follows(locator, roomVersion, next) {
521
+ const scripted = scripts.follows;
522
+ if (typeof scripted === 'boolean')
523
+ return scripted;
524
+ if (scripted !== undefined)
525
+ return scripted(locator, roomVersion, next);
526
+ return true;
527
+ },
321
528
  async bootstrap(_authorization, request) {
322
529
  return resolveScript(scripts.bootstrap, () => {
323
530
  const room = rooms.find((entry) => entry.locator === request.locator);
324
531
  if (room === undefined) {
325
532
  return { kind: 'room-not-readable', locator: request.locator };
326
533
  }
327
- return page(room, request.pageToken === undefined ? 0 : Number(request.pageToken));
534
+ return page(room, request.pageToken);
328
535
  }, request).then((answer) => note(request.locator, answer));
329
536
  },
330
537
  async refresh(_authorization, request) {
@@ -342,10 +549,6 @@ export function createMemoryAdapter(options = {}) {
342
549
  }
343
550
  return advancement(continuation.room, continuation.from, continuation.at);
344
551
  }
345
- if (request.minPosition !== undefined &&
346
- !reached(room.locator, room.roomVersion, request.minPosition)) {
347
- return { kind: 'retryable-refusal', code: 'replica_behind', fields: {} };
348
- }
349
552
  if (request.fromRoomVersion === room.roomVersion) {
350
553
  return { kind: 'still-current' };
351
554
  }
@@ -361,8 +564,20 @@ export function createMemoryAdapter(options = {}) {
361
564
  if (room === undefined) {
362
565
  return { kind: 'room-not-readable', locator: request.locator };
363
566
  }
364
- return { kind: 'rejected', code: 'unscripted-mutation', fields: {}, affected: {} };
365
- }, request).then((answer) => note(request.locator, answer));
567
+ return {
568
+ kind: 'rejected',
569
+ code: 'unscripted-mutation',
570
+ fields: {},
571
+ retryable: false,
572
+ affected: {},
573
+ };
574
+ }, request).then((answer) => {
575
+ const destination = request.changeBatch.between?.destination.room;
576
+ if (answer.kind === 'accepted' && answer.destination !== undefined && destination) {
577
+ note(destination, answer.destination);
578
+ }
579
+ return note(request.locator, answer);
580
+ });
366
581
  },
367
582
  async history(_authorization, request) {
368
583
  return resolveScript(scripts.history, () => {
@@ -376,11 +591,46 @@ export function createMemoryAdapter(options = {}) {
376
591
  };
377
592
  }, request);
378
593
  },
594
+ async binary(_authorization, request) {
595
+ return resolveScript(scripts.binary, () => {
596
+ const room = rooms.find((entry) => entry.locator === request.locator);
597
+ if (room === undefined)
598
+ return { kind: 'room-not-readable', locator: request.locator };
599
+ const blob = heldBlob(room, request.path);
600
+ return blob === undefined
601
+ ? { kind: 'rejected', code: 'file_not_found', fields: {}, retryable: false }
602
+ : { kind: 'binary', body: blob.stream(), mediaType: blob.type };
603
+ }, request);
604
+ },
605
+ async link(_authorization, request) {
606
+ return resolveScript(scripts.link, () => {
607
+ const room = rooms.find((entry) => entry.locator === request.locator);
608
+ if (room === undefined)
609
+ return { kind: 'room-not-readable', locator: request.locator };
610
+ const blob = heldBlob(room, request.path);
611
+ if (blob === undefined) {
612
+ return { kind: 'rejected', code: 'file_not_found', fields: {}, retryable: false };
613
+ }
614
+ const url = linked.get(blob) ?? URL.createObjectURL(blob);
615
+ linked.set(blob, url);
616
+ return { kind: 'link', url, name: lastName(request.path), mediaType: blob.type };
617
+ }, request);
618
+ },
619
+ async attach(_authorization, request) {
620
+ return resolveScript(scripts.attach, () => {
621
+ const room = rooms.find((entry) => entry.locator === request.locator);
622
+ if (room === undefined)
623
+ return { kind: 'room-not-readable', locator: request.locator };
624
+ const path = freePath(room, request.name);
625
+ attached.set(`${room.locator}\u0000${path}`, request.blob);
626
+ return { kind: 'attached', path, revision: `attached-${attached.size}` };
627
+ }, request);
628
+ },
379
629
  async action(_authorization, request) {
380
630
  return resolveScript(scripts.action, () => {
381
- if (request.name.startsWith('conversation.'))
631
+ if (request.name.startsWith('conversations/'))
382
632
  return conversationAction(request);
383
- return { kind: 'rejected', code: 'unscripted-action', fields: {} };
633
+ return { kind: 'rejected', code: 'unscripted-action', fields: {}, retryable: false };
384
634
  }, request).then((answer) => {
385
635
  if (answer.kind === 'action' && answer.at !== undefined)
386
636
  note(answer.at.locator, answer.at);