@agentshouse/mdruntime-adapter 0.1.0-c6296cfbceb56 → 0.1.0-c658cd7165027

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