@agentshouse/mdruntime-adapter 0.1.0-c5b15f4efd6f3 → 0.1.0-c5b58f5096f44

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 (66) hide show
  1. package/README.md +2 -2
  2. package/dist/actions.d.ts +164 -3
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +124 -219
  5. package/dist/adapter.d.ts +1 -0
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +340 -123
  8. package/dist/app-reference.d.ts +2 -0
  9. package/dist/app-reference.d.ts.map +1 -0
  10. package/dist/app-reference.js +2 -0
  11. package/dist/crockford.d.ts +4 -0
  12. package/dist/crockford.d.ts.map +1 -0
  13. package/dist/crockford.js +3 -0
  14. package/dist/discovery.d.ts +9 -13
  15. package/dist/discovery.d.ts.map +1 -1
  16. package/dist/discovery.js +57 -39
  17. package/dist/gather.d.ts +1 -1
  18. package/dist/gather.d.ts.map +1 -1
  19. package/dist/gather.js +15 -20
  20. package/dist/house-model.d.ts +2939 -0
  21. package/dist/house-model.d.ts.map +1 -0
  22. package/dist/house-model.js +1618 -0
  23. package/dist/index.d.ts +9 -2
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +8 -1
  26. package/dist/model.d.ts +6 -2
  27. package/dist/model.d.ts.map +1 -1
  28. package/dist/model.js +32 -20
  29. package/dist/part.d.ts +14 -0
  30. package/dist/part.d.ts.map +1 -0
  31. package/dist/part.js +90 -0
  32. package/dist/read.d.ts +74 -20
  33. package/dist/read.d.ts.map +1 -1
  34. package/dist/read.js +353 -168
  35. package/dist/refusal.d.ts +5 -9
  36. package/dist/refusal.d.ts.map +1 -1
  37. package/dist/refusal.js +31 -17
  38. package/dist/room-stream.d.ts +19 -0
  39. package/dist/room-stream.d.ts.map +1 -0
  40. package/dist/room-stream.js +57 -0
  41. package/dist/room-types.d.ts +7 -0
  42. package/dist/room-types.d.ts.map +1 -0
  43. package/dist/room-types.js +126 -0
  44. package/dist/site.d.ts +1 -1
  45. package/dist/site.d.ts.map +1 -1
  46. package/dist/site.js +134 -69
  47. package/dist/stream.d.ts +4 -4
  48. package/dist/stream.d.ts.map +1 -1
  49. package/dist/stream.js +143 -46
  50. package/dist/transport.d.ts +87 -24
  51. package/dist/transport.d.ts.map +1 -1
  52. package/dist/transport.js +50 -10
  53. package/dist/upload.d.ts +12 -0
  54. package/dist/upload.d.ts.map +1 -0
  55. package/dist/upload.js +63 -0
  56. package/dist/version.d.ts +1 -0
  57. package/dist/version.d.ts.map +1 -1
  58. package/dist/version.js +1 -1
  59. package/dist/write.d.ts +18 -9
  60. package/dist/write.d.ts.map +1 -1
  61. package/dist/write.js +169 -47
  62. package/package.json +9 -6
  63. package/AGENTS.md +0 -217
  64. package/dist/links.d.ts +0 -20
  65. package/dist/links.d.ts.map +0 -1
  66. package/dist/links.js +0 -104
package/dist/adapter.js CHANGED
@@ -1,65 +1,141 @@
1
+ import { stepped } from '@agentshouse/mdruntime';
1
2
  import { DISCOVER_PREFIXES, capabilities, discoveredRoom, mapDiscovery, parseDiscovery, trackedFrom, } from './discovery.js';
2
- import { scopeModels, streamedModels, viewModels } from './model.js';
3
+ import { streamedModels, viewModels } from './model.js';
3
4
  import { gathered, selections } from './gather.js';
4
5
  import { createSiteAdapter } from './site.js';
5
- import { bootstrapOf, contentPrefixes, matchesScope, parseEnumeration, parseEnvelope, parseGrant, refreshFrom, viewOf, } from './read.js';
6
- import { acceptedLinks, enumeratedLinks, heldLinks, scopeLinks, streamedLinks, } from './links.js';
6
+ import { authorityRoot, bootstrapping, parseEnumeration, parseFileAddress, parseGrant, refreshFrom, viewOf, wholeView, } from './read.js';
7
+ import { roomStreams } from './room-stream.js';
8
+ import { sha256Hex } from './digest.js';
9
+ import { finishedUpload, houseUpload } from './upload.js';
7
10
  import { houseAction } from './actions.js';
8
- import { classifyDoor, discoveryFailure, doorRefusal } from './refusal.js';
11
+ import { classifyDoor, classifyRead, discoveryFailure, doorRefusal } from './refusal.js';
9
12
  import { houseNotifications } from './stream.js';
10
- import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_REVISIONS, PRIVATE_LOCATOR, } from './transport.js';
11
- import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
13
+ import { houseDoorRoute, } from './transport.js';
14
+ import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, houseMove, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
15
+ import { HOUSE_NAMEABLE_ROOM_TYPES, HOUSE_ROOM_TYPES } from './room-types.js';
12
16
  import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
13
17
  const UNCERTAIN = { kind: 'transport-uncertainty' };
14
18
  const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
19
+ const OCTET_STREAM = 'application/octet-stream';
20
+ const EXPIRED_ADDRESS = 403;
15
21
  function lapsedGrant(outcome) {
16
22
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
17
23
  }
24
+ async function heldFiles(authority, held, bound) {
25
+ const listed = await held.files(null, bound + 1);
26
+ const files = listed.slice(0, bound);
27
+ const through = listed.length > bound ? (files.at(-1)?.path ?? null) : null;
28
+ return files.length === 0 ? null : { room: authority, through, files };
29
+ }
30
+ async function reused(scope, held) {
31
+ const sent = new Set(scope.contents.map((entry) => entry.path));
32
+ const contents = [...scope.contents];
33
+ const asked = scope.files.filter((file) => file.binary === undefined && !sent.has(file.path));
34
+ if (asked.length === 0)
35
+ return scope;
36
+ if (held === undefined)
37
+ return null;
38
+ const kept = await held.contents(asked.map(({ path }) => path));
39
+ for (const file of asked) {
40
+ const found = kept.get(file.path);
41
+ if (found === undefined ||
42
+ found.revision !== file.revision ||
43
+ (await sha256Hex(found.content)) !== file.sha256) {
44
+ return null;
45
+ }
46
+ contents.push({ path: file.path, content: found.content });
47
+ }
48
+ return { ...scope, contents };
49
+ }
18
50
  export function createAgentsHouseAdapter(options) {
19
51
  if ('site' in options)
20
- return createSiteAdapter(options.site);
52
+ return createSiteAdapter(options.site, options.snapshot === true);
21
53
  const transport = options.transport;
22
54
  const notifications = houseNotifications(transport, 'credential');
23
55
  const tracked = new Map();
24
56
  const handed = new Map();
57
+ const rooms = roomStreams('credential');
58
+ const unreused = new Set();
25
59
  let bounds;
26
60
  function declared() {
27
61
  if (bounds === undefined)
28
62
  throw new Error('no Discovery declared the bounds');
29
63
  return bounds;
30
64
  }
31
- async function bootstrappedRoom(envelope, entry) {
32
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, entry));
33
- if (scope === undefined)
34
- return { kind: 'room-not-readable', locator: entry.locator };
35
- handed.set(entry.locator, scopeLinks(scope));
65
+ async function streamedPage(entry, token, held) {
66
+ const part = await rooms.part(entry, token);
67
+ if ('kind' in part)
68
+ return part;
69
+ const scope = await reused(part.scope, held);
70
+ if (scope === null) {
71
+ unreused.add(entry.locator);
72
+ return UNCERTAIN_READ;
73
+ }
74
+ if (part.first && part.ended)
75
+ handed.set(entry.locator, roomVersion(scope));
76
+ else
77
+ handed.delete(entry.locator);
36
78
  notifications.reach(entry.authority, scope);
37
- const models = await scopeModels(scope, entry);
38
- return { ...bootstrapOf(scope), ...(models === undefined ? {} : { models }) };
79
+ return {
80
+ ...(await stepped(bootstrapping(scope))),
81
+ ...(part.models === undefined ? {} : { models: part.models }),
82
+ ...(part.pageToken === undefined ? {} : { pageToken: part.pageToken }),
83
+ bytes: part.bytes,
84
+ };
85
+ }
86
+ async function openedRoom(entry, request) {
87
+ const reusing = unreused.delete(entry.locator) ? undefined : request.held;
88
+ const files = reusing === undefined
89
+ ? null
90
+ : await heldFiles(entry.authority, reusing, declared().max_held_files);
91
+ const answered = await transport.streamed(houseDoorRoute('bootstrap'), {
92
+ prefixes: [authorityRoot(entry.handle)],
93
+ ...(files === null ? {} : { held: [files] }),
94
+ }, request.signal ?? new AbortController().signal);
95
+ if (answered.room === null)
96
+ return classifyDoor(answered.status, answered.body, entry.locator) ?? UNCERTAIN_READ;
97
+ return rooms.head(entry, answered.room, request.arriving, request.signal);
98
+ }
99
+ async function enumeratedPage(ask, page) {
100
+ const answer = await transport.door(houseDoorRoute('enumerate'), {
101
+ prefixes: [authorityRoot(ask.entry.handle)],
102
+ positions: [
103
+ {
104
+ ...presented(ask.entry.authority, { position: page.after, logEpoch: ask.from.logEpoch }),
105
+ page: { through: page.through, read: page.read },
106
+ },
107
+ ],
108
+ });
109
+ const failure = classifyRead(answer.status, answer.body, ask.entry.locator);
110
+ if (failure !== null)
111
+ return failure;
112
+ const enumeration = parseEnumeration(answer.body, 'credential');
113
+ if (enumeration === null)
114
+ return UNCERTAIN_READ;
115
+ return (viewOf(enumeration, ask.entry) ?? { kind: 'room-not-readable', locator: ask.entry.locator });
39
116
  }
40
117
  async function enumeratedRoom(enumeration, ask) {
41
- const view = viewOf(enumeration, ask.entry);
42
- if (view === undefined)
118
+ const first = viewOf(enumeration, ask.entry);
119
+ if (first === undefined)
43
120
  return { kind: 'room-not-readable', locator: ask.entry.locator };
121
+ const view = await wholeView(first, (page) => enumeratedPage(ask, page));
122
+ if ('kind' in view)
123
+ return view;
44
124
  notifications.reach(ask.entry.authority, view);
45
- const answer = refreshFrom(view, ask.fromRoomVersion);
125
+ const answer = await refreshFrom(view, ask.fromRoomVersion);
46
126
  if (answer.kind !== 'net-advancement')
47
127
  return answer;
48
- handed.set(ask.entry.locator, enumeratedLinks(view, handed.get(ask.entry.locator)));
128
+ handed.set(ask.entry.locator, roomVersion(view));
49
129
  const models = await viewModels(view, ask.entry);
50
130
  return models === undefined ? answer : { ...answer, models };
51
131
  }
52
132
  async function streamed(entry, request, from) {
53
- const links = handed.get(entry.locator);
54
- if (links?.roomVersion !== request.fromRoomVersion)
133
+ if (handed.get(entry.locator) !== request.fromRoomVersion)
55
134
  return null;
56
- const advanced = notifications.advance(entry, from, (path, revision) => links.revisions.get(path) === revision);
135
+ const advanced = await stepped(notifications.advance(entry, from));
57
136
  if (advanced === null)
58
137
  return null;
59
- const relinked = streamedLinks(links, advanced);
60
- if (relinked === null)
61
- return null;
62
- handed.set(entry.locator, relinked);
138
+ handed.set(entry.locator, advanced.page.roomVersion);
63
139
  const models = await streamedModels(advanced.schemas, entry, 'credential');
64
140
  return models === undefined ? advanced.page : { ...advanced.page, models };
65
141
  }
@@ -69,28 +145,31 @@ export function createAgentsHouseAdapter(options) {
69
145
  const at = logPosition(room.roomVersion);
70
146
  if (entry === undefined || at === null)
71
147
  return [];
72
- handed.set(room.locator, heldLinks(room));
148
+ handed.set(room.locator, room.roomVersion);
73
149
  notifications.reach(entry.authority, at);
74
150
  return [presented(entry.authority, at)];
75
151
  });
76
152
  notifications.send(kept);
77
153
  }
78
- function adopted(entry, outcome) {
154
+ async function adoptedMove(entry, into, outcome) {
155
+ if (outcome.kind === 'accepted' && outcome.destination !== undefined) {
156
+ await adopted(into, { kind: 'accepted', ...outcome.destination });
157
+ }
158
+ return adopted(entry, outcome);
159
+ }
160
+ async function adopted(entry, outcome) {
79
161
  if (outcome.kind !== 'accepted')
80
162
  return outcome;
81
- const { originals, ...accepted } = outcome;
82
- const links = handed.get(entry.locator);
83
- if (links === undefined)
84
- return accepted;
85
- const follows = followsVersion(links.roomVersion, accepted.roomVersion);
86
- const relinked = acceptedLinks(links, accepted, follows ? accepted.roomVersion : links.roomVersion, originals);
87
- handed.set(entry.locator, relinked.links);
88
- const at = logPosition(accepted.roomVersion);
163
+ const held = handed.get(entry.locator);
164
+ if (held === undefined)
165
+ return outcome;
166
+ const follows = followsVersion(held, outcome.roomVersion);
167
+ if (follows)
168
+ handed.set(entry.locator, outcome.roomVersion);
169
+ const at = logPosition(outcome.roomVersion);
89
170
  if (follows && at !== null)
90
171
  notifications.reach(entry.authority, at);
91
- return relinked.attachments === undefined
92
- ? accepted
93
- : { ...accepted, attachments: relinked.attachments };
172
+ return outcome;
94
173
  }
95
174
  function authorityFor(locator) {
96
175
  return tracked.get(locator);
@@ -104,165 +183,248 @@ export function createAgentsHouseAdapter(options) {
104
183
  const { authority, position, log_epoch: logEpoch } = body;
105
184
  if (typeof position !== 'string' || typeof logEpoch !== 'string')
106
185
  return result;
107
- const locator = authority === undefined
108
- ? PRIVATE_LOCATOR
109
- : [...tracked.values()].find((entry) => entry.authority === authority)?.locator;
186
+ const locator = [...tracked.values()].find((entry) => entry.authority === authority)?.locator;
110
187
  if (locator === undefined)
111
188
  return result;
112
189
  return { ...result, at: { locator, roomVersion: roomVersion({ position, logEpoch }) } };
113
190
  }
114
- async function bundled(body) {
115
- const grant = parseGrant(body);
116
- if (grant === null)
117
- return body;
118
- if (grant.method !== 'GET')
119
- return null;
120
- const answer = await transport.transfer({
121
- act: 'local_copy_bundle',
122
- method: 'GET',
123
- url: grant.url,
124
- answer: 'json',
125
- });
126
- return answer.status === 200 ? answer.body : null;
127
- }
128
- function weight(entry) {
129
- return contentPrefixes(entry).length;
130
- }
131
- async function bootstrapped(entries) {
132
- const answers = [];
133
- for (const part of selections(entries, weight, declared().max_prefixes)) {
134
- const answer = await transport.door(DOOR_BOOTSTRAP, {
135
- prefixes: part.flatMap((entry) => contentPrefixes(entry)),
136
- });
137
- const classified = part.map((entry) => classifyDoor(answer.status, answer.body, entry.locator));
138
- const envelope = classified[0] === null ? parseEnvelope(await bundled(answer.body)) : null;
139
- for (const [index, entry] of part.entries()) {
140
- const failure = classified[index];
141
- if (failure === null || failure === undefined) {
142
- answers.push(envelope === null ? UNCERTAIN_READ : await bootstrappedRoom(envelope, entry));
143
- continue;
144
- }
145
- answers.push(failure.kind === 'beyond-horizon' ? UNCERTAIN_READ : failure);
146
- }
147
- }
148
- return answers;
149
- }
150
191
  function layered(asks) {
151
192
  const depths = new Map();
152
193
  const layers = [];
153
194
  for (const ask of asks) {
154
- const depth = depths.get(ask.entry.authority) ?? 0;
155
- depths.set(ask.entry.authority, depth + 1);
195
+ const depth = depths.get(ask.ask.entry.authority) ?? 0;
196
+ depths.set(ask.ask.entry.authority, depth + 1);
156
197
  (layers[depth] ??= []).push(ask);
157
198
  }
158
199
  return layers;
159
200
  }
160
201
  async function enumerated(asks) {
161
- const answers = new Map();
162
- const parts = layered(asks).flatMap((layer) => selections(layer, (ask) => weight(ask.entry), declared().max_prefixes));
202
+ const answers = [];
203
+ const parts = layered(asks.map((ask, index) => ({ ask, index }))).flatMap((layer) => selections(layer, declared().max_prefixes));
163
204
  for (const part of parts) {
164
205
  let open = [...part];
165
206
  while (open.length > 0) {
166
- const answer = await transport.door(DOOR_ENUMERATE, {
167
- prefixes: open.flatMap((ask) => contentPrefixes(ask.entry)),
168
- positions: open.map((ask) => presented(ask.entry.authority, ask.from)),
207
+ const answer = await transport.door(houseDoorRoute('enumerate'), {
208
+ prefixes: open.map(({ ask }) => authorityRoot(ask.entry.handle)),
209
+ positions: open.map(({ ask }) => presented(ask.entry.authority, ask.from)),
169
210
  });
170
- const classified = open.map((ask) => classifyDoor(answer.status, answer.body, ask.entry.locator));
211
+ const classified = open.map(({ ask }) => classifyRead(answer.status, answer.body, ask.entry.locator));
171
212
  if (classified[0]?.kind === 'beyond-horizon') {
172
213
  const named = doorRefusal(answer.body)?.fields.authority;
173
- const expired = open.filter((ask) => ask.entry.authority === named);
214
+ const expired = open.filter(({ ask }) => ask.entry.authority === named);
174
215
  const answered = expired.length === 0 ? UNCERTAIN_READ : { kind: 'beyond-horizon' };
175
216
  const gone = expired.length === 0 ? open : expired;
176
217
  for (const ask of gone)
177
- answers.set(ask, answered);
218
+ answers[ask.index] = answered;
178
219
  open = open.filter((ask) => !gone.includes(ask));
179
220
  continue;
180
221
  }
181
222
  const enumeration = classified[0] === null ? parseEnumeration(answer.body, 'credential') : null;
182
- for (const [index, ask] of open.entries()) {
183
- const failure = classified[index];
223
+ for (const [answerIndex, { ask, index }] of open.entries()) {
224
+ const failure = classified[answerIndex];
184
225
  if (failure !== null && failure !== undefined) {
185
- answers.set(ask, failure);
226
+ answers[index] = failure;
186
227
  continue;
187
228
  }
188
- answers.set(ask, enumeration === null ? UNCERTAIN_READ : await enumeratedRoom(enumeration, ask));
229
+ answers[index] =
230
+ enumeration === null ? UNCERTAIN_READ : await enumeratedRoom(enumeration, ask);
189
231
  }
190
232
  open = [];
191
233
  }
192
234
  }
193
- return asks.map((ask) => answers.get(ask) ?? UNCERTAIN_READ);
235
+ return answers;
194
236
  }
195
- const bootstrapTogether = gathered(bootstrapped);
196
237
  const enumerateTogether = gathered(enumerated);
197
238
  async function uploaded(head, changes, delta, bytes, entry, handed, keep) {
198
239
  const paths = batchPaths(changes);
199
240
  const sha256 = await deltaDigest(delta);
200
241
  if (handed !== undefined && handed.sha256 === sha256) {
201
- return { body: { ...head, paths, upload: handed } };
242
+ return { body: { ...head, paths, upload: handed, runtime: notifications.runtime } };
202
243
  }
203
- const minted = await transport.door(DOOR_BATCH_UPLOAD, { ...head, paths, bytes, sha256 });
244
+ const minted = await transport.door(houseDoorRoute('batch-upload'), {
245
+ ...head,
246
+ paths,
247
+ bytes,
248
+ sha256,
249
+ });
204
250
  if (minted.status !== 200) {
205
- return { outcome: mutationOutcome(minted.status, minted.body, entry.locator, entry.handle) };
251
+ return {
252
+ outcome: await stepped(mutationOutcome(minted.status, minted.body, entry.locator, entry.handle)),
253
+ };
206
254
  }
207
255
  const grant = parseGrant(minted.body);
208
- if (grant === null || grant.method !== 'POST')
256
+ if (grant === null)
209
257
  return { outcome: UNCERTAIN };
210
258
  const delivered = await transport.transfer({
211
259
  act: 'authored_batch_upload',
212
- method: 'POST',
213
260
  url: grant.url,
214
- answer: 'json',
215
261
  body: `${grant.operation}\n${delta}`,
216
262
  });
217
263
  if (delivered.status !== 200)
218
264
  return { outcome: UNCERTAIN };
219
265
  const holding = parseGrant(delivered.body);
220
- if (holding === null || holding.method !== 'POST')
266
+ if (holding === null)
221
267
  return { outcome: UNCERTAIN };
222
268
  const upload = { url: holding.url, operation: holding.operation, bytes, sha256 };
223
269
  await keep(upload);
224
- return { body: { ...head, paths, upload } };
270
+ return { body: { ...head, paths, upload, runtime: notifications.runtime } };
225
271
  }
226
- async function submit(head, changes, delta, bytes, entry, handed, keep) {
227
- const inline = { ...head, changes };
272
+ async function staged(answer, { changeBatch: batch, file }) {
273
+ const refusal = doorRefusal(answer.body);
274
+ if (refusal?.code !== 'files_unstaged')
275
+ return null;
276
+ const uploads = refusal.fields.upload;
277
+ if (!Array.isArray(uploads))
278
+ return false;
279
+ for (const named of uploads) {
280
+ const upload = houseUpload(named);
281
+ const change = batch.changes.find((held) => held.kind === 'binary' && held.sha256 === named.sha256);
282
+ if (upload === null || change === undefined)
283
+ return false;
284
+ const finished = await finishedUpload(transport, upload, await file(change.localId));
285
+ if (finished?.body?.staged !== true)
286
+ return false;
287
+ }
288
+ return true;
289
+ }
290
+ async function submit(request, head, changes, delta, bytes, entry, handed, keep, into) {
291
+ const inline = { ...head, changes, runtime: notifications.runtime };
228
292
  const prepared = uploadable(inline, bytes, declared())
229
293
  ? await uploaded(head, changes, delta, bytes, entry, handed, keep)
230
294
  : { body: inline };
231
295
  if ('outcome' in prepared)
232
296
  return prepared.outcome;
233
- const answer = await transport.door(DOOR_BATCH, prepared.body);
234
- return mutationOutcome(answer.status, answer.body, entry.locator, entry.handle);
297
+ const answer = await transport.door(houseDoorRoute('batch'), prepared.body);
298
+ const stagedFiles = await staged(answer, request);
299
+ if (stagedFiles === false)
300
+ return UNCERTAIN;
301
+ const settled = stagedFiles === null ? answer : await transport.door(houseDoorRoute('batch'), prepared.body);
302
+ if (stagedFiles === true && (await staged(settled, request)) !== null)
303
+ return UNCERTAIN;
304
+ return stepped(mutationOutcome(settled.status, settled.body, entry.locator, entry.handle, into === undefined
305
+ ? undefined
306
+ : {
307
+ source: { authority: entry.authority, handle: entry.handle },
308
+ destination: { authority: into.authority, handle: into.handle },
309
+ }));
310
+ }
311
+ function addressed(answer, locator) {
312
+ const failure = classifyDoor(answer.status, answer.body, locator);
313
+ if (failure !== null) {
314
+ return failure.kind === 'scoped-refusal'
315
+ ? {
316
+ kind: 'rejected',
317
+ code: failure.code,
318
+ fields: failure.fields,
319
+ retryable: failure.retryable,
320
+ }
321
+ : failure;
322
+ }
323
+ const address = parseFileAddress(answer.body);
324
+ return address === null ? UNCERTAIN_READ : { kind: 'address', ...address };
325
+ }
326
+ function linked(answer, locator) {
327
+ const address = addressed(answer, locator);
328
+ return address.kind === 'address'
329
+ ? { kind: 'link', url: address.url, name: address.name, mediaType: address.mediaType }
330
+ : address;
331
+ }
332
+ function refusedAttach(answer, locator) {
333
+ const failure = classifyDoor(answer.status, answer.body, locator);
334
+ if (failure === null)
335
+ return null;
336
+ return failure.kind === 'scoped-refusal'
337
+ ? {
338
+ kind: 'rejected',
339
+ code: failure.code,
340
+ fields: failure.fields,
341
+ retryable: failure.retryable,
342
+ }
343
+ : failure;
344
+ }
345
+ async function downloaded(ask, locator) {
346
+ let address = addressed(await ask(), locator);
347
+ if (address.kind !== 'address')
348
+ return address;
349
+ let read = await transport.download(address.url);
350
+ if (read.status === EXPIRED_ADDRESS) {
351
+ address = addressed(await ask(), locator);
352
+ if (address.kind !== 'address')
353
+ return address;
354
+ read = await transport.download(address.url);
355
+ }
356
+ if (read.status !== 200 || read.body === null)
357
+ return UNCERTAIN_READ;
358
+ return {
359
+ kind: 'binary',
360
+ body: read.body,
361
+ mediaType: address.mediaType,
362
+ sha256: address.sha256,
363
+ };
364
+ }
365
+ function pathAddress(request) {
366
+ const entry = authorityFor(request.locator);
367
+ if (entry === undefined)
368
+ return null;
369
+ return () => transport.door(houseDoorRoute('files/get'), {
370
+ room_ref: entry.authority,
371
+ path: request.path,
372
+ revision: request.revision,
373
+ });
235
374
  }
236
375
  return {
376
+ roomTypes: HOUSE_ROOM_TYPES,
377
+ nameableRoomTypes: HOUSE_NAMEABLE_ROOM_TYPES,
237
378
  async authorize() {
238
379
  return options.authorize();
239
380
  },
240
- async discover(_authorization) {
241
- const answer = await transport.door(DOOR_DISCOVER, { prefixes: [...DISCOVER_PREFIXES] });
242
- const classified = classifyDoor(answer.status, answer.body, PRIVATE_LOCATOR);
243
- if (classified !== null)
244
- return discoveryFailure(classified);
381
+ async discover(_authorization, scope) {
382
+ const answer = await transport.door(houseDoorRoute('discover'), {
383
+ prefixes: [...DISCOVER_PREFIXES],
384
+ ...(scope === undefined ? {} : { rooms: scope.authorities }),
385
+ });
386
+ const failed = discoveryFailure(answer.status, answer.body);
387
+ if (failed !== null)
388
+ return failed;
245
389
  const document = parseDiscovery(answer.body);
246
390
  if (document === null)
247
391
  return { kind: 'transport-uncertainty' };
248
392
  bounds = document.bounds;
249
393
  const entries = trackedFrom(document);
250
- tracked.clear();
394
+ const covered = scope === undefined
395
+ ? null
396
+ : [
397
+ ...new Set([
398
+ ...[...tracked.values()]
399
+ .filter((entry) => scope.authorities.includes(entry.authority))
400
+ .map((entry) => entry.locator),
401
+ ...entries.map((entry) => entry.locator),
402
+ ]),
403
+ ];
404
+ if (covered === null)
405
+ tracked.clear();
406
+ else
407
+ for (const locator of covered)
408
+ tracked.delete(locator);
251
409
  for (const entry of entries)
252
410
  tracked.set(entry.locator, entry);
253
411
  for (const locator of handed.keys())
254
412
  if (!tracked.has(locator))
255
413
  handed.delete(locator);
256
- notifications.track(entries);
257
- return mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions));
414
+ notifications.track([...tracked.values()]);
415
+ return {
416
+ ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions, [...tracked.values()])),
417
+ member: document.member,
418
+ ...(covered === null ? {} : { covered }),
419
+ };
258
420
  },
259
421
  async bootstrap(_authorization, request) {
260
422
  const entry = authorityFor(request.locator);
261
423
  if (entry === undefined)
262
424
  return { kind: 'room-not-readable', locator: request.locator };
263
- if (request.pageToken !== undefined)
264
- return { kind: 'transport-uncertainty' };
265
- return bootstrapTogether(entry);
425
+ return request.pageToken === undefined
426
+ ? openedRoom(entry, request)
427
+ : streamedPage(entry, request.pageToken, request.held);
266
428
  },
267
429
  async refresh(_authorization, request) {
268
430
  const entry = authorityFor(request.locator);
@@ -282,23 +444,33 @@ export function createAgentsHouseAdapter(options) {
282
444
  const entry = authorityFor(request.locator);
283
445
  if (entry === undefined)
284
446
  return { kind: 'room-not-readable', locator: request.locator };
285
- const mapped = houseChanges(request.changeBatch, entry.handle);
447
+ const batch = request.changeBatch;
448
+ const destination = batch.between?.destination.room;
449
+ const into = destination === undefined ? undefined : authorityFor(destination);
450
+ if (destination !== undefined && into === undefined) {
451
+ return { kind: 'room-not-readable', locator: destination };
452
+ }
453
+ const mapped = into === undefined
454
+ ? houseChanges(batch, entry.handle)
455
+ : houseMove(batch, entry.handle, into.handle);
286
456
  if (mapped.kind === 'unmapped')
287
457
  return rejectedMapping(mapped);
288
- const head = operationHead(request.changeBatch);
458
+ const head = operationHead(batch);
289
459
  const delta = deltaBody(mapped.changes);
290
460
  const bytes = deltaBytes(delta);
291
461
  const handed = request.upload;
292
- const outcome = await submit(head, mapped.changes, delta, bytes, entry, handed, keep);
462
+ const answer = (upload) => submit(request, head, mapped.changes, delta, bytes, entry, upload, keep, into);
463
+ const settle = (outcome) => into === undefined ? adopted(entry, outcome) : adoptedMove(entry, into, outcome);
464
+ const outcome = await answer(handed);
293
465
  if (handed === undefined || !lapsedGrant(outcome))
294
- return adopted(entry, outcome);
295
- return adopted(entry, await submit(head, mapped.changes, delta, bytes, entry, undefined, keep));
466
+ return settle(outcome);
467
+ return settle(await answer(undefined));
296
468
  },
297
469
  async history(_authorization, request) {
298
470
  const entry = authorityFor(request.locator);
299
471
  if (entry === undefined)
300
472
  return { kind: 'room-not-readable', locator: request.locator };
301
- const answer = await transport.door(DOOR_REVISIONS, revisionBody({
473
+ const answer = await transport.door(houseDoorRoute('revisions'), revisionBody({
302
474
  path: request.path,
303
475
  handle: entry.handle,
304
476
  ...(request.revision === undefined ? {} : { revision: request.revision }),
@@ -309,6 +481,47 @@ export function createAgentsHouseAdapter(options) {
309
481
  ...(request.revision === undefined ? {} : { revision: request.revision }),
310
482
  });
311
483
  },
484
+ async binary(_authorization, request) {
485
+ const ask = pathAddress(request);
486
+ if (ask === null)
487
+ return { kind: 'room-not-readable', locator: request.locator };
488
+ return downloaded(ask, request.locator);
489
+ },
490
+ async link(_authorization, request) {
491
+ const ask = pathAddress(request);
492
+ if (ask === null)
493
+ return { kind: 'room-not-readable', locator: request.locator };
494
+ return linked(await ask(), request.locator);
495
+ },
496
+ async attach(_authorization, request) {
497
+ const entry = authorityFor(request.locator);
498
+ if (entry === undefined)
499
+ return { kind: 'room-not-readable', locator: request.locator };
500
+ const declared = await transport.door(houseDoorRoute('files/attach'), {
501
+ room_ref: entry.authority,
502
+ name: request.name,
503
+ bytes: request.byteLength,
504
+ sha256: request.sha256,
505
+ media_type: request.mediaType.length > 0 ? request.mediaType : OCTET_STREAM,
506
+ });
507
+ const refused = refusedAttach(declared, request.locator);
508
+ if (refused !== null)
509
+ return refused;
510
+ const upload = houseUpload(declared.body?.upload);
511
+ if (upload === null)
512
+ return UNCERTAIN_READ;
513
+ const finished = await finishedUpload(transport, upload, request.blob);
514
+ if (finished === null)
515
+ return UNCERTAIN_READ;
516
+ const unfinished = refusedAttach(finished, request.locator);
517
+ if (unfinished !== null)
518
+ return unfinished;
519
+ const { path, revision } = (finished.body ?? {});
520
+ const root = `/rooms/${entry.handle}/`;
521
+ return typeof path === 'string' && path.startsWith(root) && typeof revision === 'string'
522
+ ? { kind: 'attached', path: path.slice(root.length), revision }
523
+ : UNCERTAIN_READ;
524
+ },
312
525
  reached(locator, version, target) {
313
526
  return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
314
527
  },
@@ -318,7 +531,11 @@ export function createAgentsHouseAdapter(options) {
318
531
  async action(_authorization, request) {
319
532
  return withAt(await houseAction(transport, request));
320
533
  },
321
- roomStream: { hold: held, observe: notifications.observe },
534
+ roomStream: {
535
+ hold: held,
536
+ reopen: notifications.reopen,
537
+ observe: notifications.observe,
538
+ },
322
539
  conversations: notifications.conversations,
323
540
  serviceMode: notifications.serviceMode,
324
541
  };
@@ -0,0 +1,2 @@
1
+ export declare const APP_REFERENCE_PATTERN: RegExp;
2
+ //# sourceMappingURL=app-reference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-reference.d.ts","sourceRoot":"","sources":["../src/app-reference.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,qBAAqB,QAEjC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { CROCKFORD_CHARACTER } from './crockford.js';
2
+ export const APP_REFERENCE_PATTERN = new RegExp(`^a_(${CROCKFORD_CHARACTER}+)\\.(${CROCKFORD_CHARACTER})$`);
@@ -0,0 +1,4 @@
1
+ export declare const CROCKFORD_ALPHABET = "0123456789abcdefghjkmnpqrstvwxyz";
2
+ export declare const CROCKFORD_CHARACTER = "[0123456789abcdefghjkmnpqrstvwxyz]";
3
+ export declare const CROCKFORD_LEADING_CHARACTER: string;
4
+ //# sourceMappingURL=crockford.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crockford.d.ts","sourceRoot":"","sources":["../src/crockford.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,qCAAqC,CAAC;AAErE,eAAO,MAAM,mBAAmB,uCAA4B,CAAC;AAE7D,eAAO,MAAM,2BAA2B,QAAqC,CAAC"}