@agentshouse/mdruntime-adapter 0.1.0-alpha.9 → 0.1.0-c09c6f263662c

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 (64) hide show
  1. package/AGENTS.md +272 -33
  2. package/README.md +32 -0
  3. package/dist/actions.d.ts +43 -13
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +270 -351
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +361 -221
  8. package/dist/digest.js +1 -5
  9. package/dist/discovery.d.ts +23 -34
  10. package/dist/discovery.d.ts.map +1 -1
  11. package/dist/discovery.js +154 -231
  12. package/dist/gather.d.ts +4 -0
  13. package/dist/gather.d.ts.map +1 -0
  14. package/dist/gather.js +45 -0
  15. package/dist/handed.d.ts +15 -0
  16. package/dist/handed.d.ts.map +1 -0
  17. package/dist/handed.js +38 -0
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -3
  21. package/dist/map.d.ts +2 -2
  22. package/dist/map.d.ts.map +1 -1
  23. package/dist/map.js +6 -7
  24. package/dist/model.d.ts +8 -18
  25. package/dist/model.d.ts.map +1 -1
  26. package/dist/model.js +63 -57
  27. package/dist/read.d.ts +40 -29
  28. package/dist/read.d.ts.map +1 -1
  29. package/dist/read.js +209 -208
  30. package/dist/referrer.d.ts +7 -0
  31. package/dist/referrer.d.ts.map +1 -0
  32. package/dist/referrer.js +35 -0
  33. package/dist/refusal.d.ts +5 -7
  34. package/dist/refusal.d.ts.map +1 -1
  35. package/dist/refusal.js +47 -69
  36. package/dist/site.d.ts.map +1 -1
  37. package/dist/site.js +144 -153
  38. package/dist/stream.d.ts +17 -6
  39. package/dist/stream.d.ts.map +1 -1
  40. package/dist/stream.js +251 -215
  41. package/dist/transport.d.ts +81 -33
  42. package/dist/transport.d.ts.map +1 -1
  43. package/dist/transport.js +4 -3
  44. package/dist/version.d.ts +2 -0
  45. package/dist/version.d.ts.map +1 -1
  46. package/dist/version.js +32 -18
  47. package/dist/write.d.ts +7 -8
  48. package/dist/write.d.ts.map +1 -1
  49. package/dist/write.js +196 -206
  50. package/package.json +5 -13
  51. package/dist/actions.js.map +0 -1
  52. package/dist/adapter.js.map +0 -1
  53. package/dist/digest.js.map +0 -1
  54. package/dist/discovery.js.map +0 -1
  55. package/dist/index.js.map +0 -1
  56. package/dist/map.js.map +0 -1
  57. package/dist/model.js.map +0 -1
  58. package/dist/read.js.map +0 -1
  59. package/dist/refusal.js.map +0 -1
  60. package/dist/site.js.map +0 -1
  61. package/dist/stream.js.map +0 -1
  62. package/dist/transport.js.map +0 -1
  63. package/dist/version.js.map +0 -1
  64. package/dist/write.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,EAad,MAAM,wBAAwB,CAAC;AAqChC,OAAO,EAQL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAiBxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAA;CAAE,CAAC;AA2BjC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAuQpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,EAgBd,MAAM,wBAAwB,CAAC;AAuChC,OAAO,EASL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAyBxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAwBjC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA8cpF"}
package/dist/adapter.js CHANGED
@@ -1,285 +1,425 @@
1
- import { DISCOVER_PREFIXES, PROVIDER_SELECTION_MODEL_ROOT, capabilities, discoveredRoom, locatorOf, mapDiscovery, parseDiscovery, providerSelection, trackedFrom } from './discovery.js';
2
- import { MODEL_ABSENT, failedModel, heldModel, modelKey, modelShelf } from './model.js';
1
+ 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
4
  import { createSiteAdapter } from './site.js';
4
- import { authorityRoot, bootstrapFrom, contentPrefixes, definitionsFrom, modelPrefix, parseEnumeration, parseEnvelope, parseGrant, refreshFrom } from './read.js';
5
+ import { bootstrapOf, contentPrefixes, matchesScope, parseEnumeration, parseEnvelope, parseGrant, refreshFrom, viewOf, } from './read.js';
6
+ import { acceptedHanded, enumeratedHanded, heldHanded, scopeHanded, streamedHanded, } from './handed.js';
5
7
  import { houseAction } from './actions.js';
6
- import { classifyDoor, discoveryFailure } from './refusal.js';
8
+ import { classifyDoor, classifyRead, discoveryFailure, doorRefusal } from './refusal.js';
7
9
  import { houseNotifications } from './stream.js';
8
- import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_REVISIONS, PRIVATE_AUTHORITY } from './transport.js';
9
- import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable } from './write.js';
10
- import { logPosition, presented } from './version.js';
11
- const UNCERTAIN = {
12
- kind: 'transport-uncertainty'
13
- };
14
- const RESOLVED_OUTCOMES = new Set([
15
- 'accepted',
16
- 'conflicted',
17
- 'rejected',
18
- 'expired'
19
- ]);
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 { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
14
+ const UNCERTAIN = { kind: 'transport-uncertainty' };
15
+ const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
20
16
  function lapsedGrant(outcome) {
21
17
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
22
18
  }
23
- function modelFailure(classified, envelope) {
24
- if (classified === null) {
25
- return envelope === null ? {
26
- kind: 'transport-uncertainty'
27
- } : MODEL_ABSENT;
28
- }
29
- return failedModel(classified);
30
- }
31
19
  export function createAgentsHouseAdapter(options) {
32
- if ('site' in options) return createSiteAdapter(options.site);
20
+ if ('site' in options)
21
+ return createSiteAdapter(options.site);
33
22
  const transport = options.transport;
34
23
  const notifications = houseNotifications(transport, 'credential');
35
24
  const tracked = new Map();
36
- const models = modelShelf();
37
- const submissions = new Map();
38
- function held(envelope, entry, modelRoot, identity, failure) {
39
- return heldModel(envelope === null ? null : definitionsFrom(envelope, entry, modelRoot), identity, failure);
25
+ const handed = new Map();
26
+ let bounds;
27
+ function declared() {
28
+ if (bounds === undefined)
29
+ throw new Error('no Discovery declared the bounds');
30
+ return bounds;
40
31
  }
41
- async function acquireModels(entries) {
42
- const acquired = new Map();
43
- const privateEntry = entries.find((entry)=>entry.authority === PRIVATE_AUTHORITY);
44
- const prefixes = entries.map((entry)=>modelPrefix(entry, entry.modelRoot));
45
- if (privateEntry !== undefined) {
46
- prefixes.push(`${authorityRoot(undefined)}/${PROVIDER_SELECTION_MODEL_ROOT}`);
47
- }
48
- const answer = await transport.door(DOOR_BOOTSTRAP, {
49
- prefixes
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));
38
+ else
39
+ handed.delete(entry.locator);
40
+ 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
+ return {
48
+ ...bootstrapOf(scope),
49
+ ...(models === undefined ? {} : { models }),
50
+ ...(continuation === null ? {} : { pageToken: JSON.stringify(continuation) }),
51
+ };
52
+ }
53
+ async function enumeratedRoom(enumeration, ask) {
54
+ const view = viewOf(enumeration, ask.entry);
55
+ if (view === undefined)
56
+ return { kind: 'room-not-readable', locator: ask.entry.locator };
57
+ notifications.reach(ask.entry.authority, view);
58
+ const answer = refreshFrom(view, ask.fromRoomVersion);
59
+ if (answer.kind !== 'net-advancement')
60
+ return answer;
61
+ handed.set(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
62
+ const models = await viewModels(view, ask.entry);
63
+ return models === undefined ? answer : { ...answer, models };
64
+ }
65
+ async function streamed(entry, request, from) {
66
+ const held = handed.get(entry.locator);
67
+ if (held?.roomVersion !== request.fromRoomVersion)
68
+ return null;
69
+ const advanced = notifications.advance(entry, from, (path, revision) => held.revisions.get(path) === revision);
70
+ if (advanced === null)
71
+ return null;
72
+ handed.set(entry.locator, streamedHanded(held, advanced));
73
+ const models = await streamedModels(advanced.schemas, entry, 'credential');
74
+ return models === undefined ? advanced.page : { ...advanced.page, models };
75
+ }
76
+ function held(rooms) {
77
+ const kept = rooms.flatMap((room) => {
78
+ const entry = tracked.get(room.locator);
79
+ const at = logPosition(room.roomVersion);
80
+ if (entry === undefined || at === null)
81
+ return [];
82
+ handed.set(room.locator, heldHanded(room));
83
+ notifications.reach(entry.authority, at);
84
+ return [presented(entry.authority, at)];
50
85
  });
51
- const classified = classifyDoor(answer.status, answer.body, PRIVATE_AUTHORITY);
52
- const envelope = classified === null ? parseEnvelope(await bundled(answer.body)) : null;
53
- const failure = modelFailure(classified, envelope);
54
- for (const entry of entries){
55
- acquired.set(modelKey(entry.locator, 'collection'), await held(envelope, entry, entry.modelRoot, `${authorityRoot(entry.handle)}/${entry.modelRoot}`, failure));
56
- }
57
- if (privateEntry !== undefined) {
58
- acquired.set(modelKey(privateEntry.locator, 'typed-selection'), await held(envelope, privateEntry, PROVIDER_SELECTION_MODEL_ROOT, `${authorityRoot(undefined)}/${PROVIDER_SELECTION_MODEL_ROOT}`, failure));
59
- }
60
- return acquired;
86
+ notifications.send(kept);
61
87
  }
62
- function roomOf(authority, acquired) {
63
- const locator = locatorOf(authority);
64
- const key = modelKey(locator, 'collection');
65
- const collection = models.retain(key, acquired.get(key));
66
- if (authority.kind !== 'private') {
67
- return discoveredRoom(authority, collection?.identity ?? null, undefined);
68
- }
69
- const selection = models.retain(modelKey(locator, 'typed-selection'), acquired.get(modelKey(locator, 'typed-selection')));
70
- return discoveredRoom(authority, collection?.identity ?? null, providerSelection(selection?.identity ?? null));
88
+ function adopted(entry, outcome) {
89
+ if (outcome.kind !== 'accepted')
90
+ return outcome;
91
+ const held = handed.get(entry.locator);
92
+ if (held === undefined)
93
+ return outcome;
94
+ const follows = followsVersion(held.roomVersion, outcome.roomVersion);
95
+ handed.set(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
96
+ const at = logPosition(outcome.roomVersion);
97
+ if (follows && at !== null)
98
+ notifications.reach(entry.authority, at);
99
+ return outcome;
71
100
  }
72
101
  function authorityFor(locator) {
73
102
  return tracked.get(locator);
74
103
  }
75
- async function bundled(body) {
76
- const grant = parseGrant(body);
77
- if (grant === null) return body;
78
- if (grant.method !== 'GET') return null;
79
- const answer = await transport.transfer({
80
- act: 'working_copy_bundle',
81
- method: 'GET',
82
- url: grant.url
104
+ function withAt(result) {
105
+ if (result.kind !== 'action')
106
+ return result;
107
+ const body = result.result;
108
+ if (typeof body !== 'object' || body === null)
109
+ return result;
110
+ const { authority, position, log_epoch: logEpoch } = body;
111
+ if (typeof position !== 'string' || typeof logEpoch !== 'string')
112
+ return result;
113
+ const locator = [...tracked.values()].find((entry) => entry.authority === authority)?.locator;
114
+ if (locator === undefined)
115
+ return result;
116
+ return { ...result, at: { locator, roomVersion: roomVersion({ position, logEpoch }) } };
117
+ }
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,
83
157
  });
84
- return answer.status === 200 ? answer.body : null;
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);
85
166
  }
86
- async function uploaded(head, changes, delta, bytes, entry) {
167
+ function layered(asks) {
168
+ const depths = new Map();
169
+ const layers = [];
170
+ for (const ask of asks) {
171
+ const depth = depths.get(ask.entry.authority) ?? 0;
172
+ depths.set(ask.entry.authority, depth + 1);
173
+ (layers[depth] ??= []).push(ask);
174
+ }
175
+ return layers;
176
+ }
177
+ 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));
180
+ for (const part of parts) {
181
+ let open = [...part];
182
+ while (open.length > 0) {
183
+ 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)),
186
+ });
187
+ const classified = open.map((ask) => classifyRead(answer.status, answer.body, ask.entry.locator));
188
+ if (classified[0]?.kind === 'beyond-horizon') {
189
+ const named = doorRefusal(answer.body)?.fields.authority;
190
+ const expired = open.filter((ask) => ask.entry.authority === named);
191
+ const answered = expired.length === 0 ? UNCERTAIN_READ : { kind: 'beyond-horizon' };
192
+ const gone = expired.length === 0 ? open : expired;
193
+ for (const ask of gone)
194
+ answers.set(ask, answered);
195
+ open = open.filter((ask) => !gone.includes(ask));
196
+ continue;
197
+ }
198
+ const enumeration = classified[0] === null ? parseEnumeration(answer.body, 'credential') : null;
199
+ for (const [index, ask] of open.entries()) {
200
+ const failure = classified[index];
201
+ if (failure !== null && failure !== undefined) {
202
+ answers.set(ask, failure);
203
+ continue;
204
+ }
205
+ answers.set(ask, enumeration === null ? UNCERTAIN_READ : await enumeratedRoom(enumeration, ask));
206
+ }
207
+ open = [];
208
+ }
209
+ }
210
+ return asks.map((ask) => answers.get(ask) ?? UNCERTAIN_READ);
211
+ }
212
+ const bootstrapTogether = gatheredEach(bootstrapped);
213
+ const enumerateTogether = gathered(enumerated);
214
+ async function uploaded(head, changes, delta, bytes, entry, handed, keep) {
87
215
  const paths = batchPaths(changes);
88
216
  const sha256 = await deltaDigest(delta);
89
- const retained = submissions.get(head.operation_id);
90
- if (retained !== undefined && retained.sha256 === sha256 && retained.body.app_ref === head.app_ref && retained.body.release === head.release) return {
91
- body: retained.body
92
- };
93
- const minted = await transport.door(DOOR_BATCH_UPLOAD, {
94
- ...head,
95
- paths,
96
- bytes,
97
- sha256
98
- });
217
+ if (handed !== undefined && handed.sha256 === sha256) {
218
+ return { body: { ...head, paths, upload: handed, runtime: notifications.runtime } };
219
+ }
220
+ const minted = await transport.door(DOOR_BATCH_UPLOAD, { ...head, paths, bytes, sha256 });
99
221
  if (minted.status !== 200) {
100
- return {
101
- outcome: mutationOutcome(minted.status, minted.body, entry.locator, entry.handle)
102
- };
222
+ return { outcome: mutationOutcome(minted.status, minted.body, entry.locator, entry.handle) };
103
223
  }
104
224
  const grant = parseGrant(minted.body);
105
- if (grant === null || grant.method !== 'POST') return {
106
- outcome: UNCERTAIN
107
- };
225
+ if (grant === null || grant.method !== 'POST')
226
+ return { outcome: UNCERTAIN };
108
227
  const delivered = await transport.transfer({
109
228
  act: 'authored_batch_upload',
110
- method: 'POST',
111
229
  url: grant.url,
112
- body: `${grant.operation}\n${delta}`
230
+ body: `${grant.operation}\n${delta}`,
113
231
  });
114
- if (delivered.status !== 200) return {
115
- outcome: UNCERTAIN
116
- };
232
+ if (delivered.status !== 200)
233
+ return { outcome: UNCERTAIN };
117
234
  const holding = parseGrant(delivered.body);
118
- if (holding === null) return {
119
- outcome: UNCERTAIN
120
- };
121
- const body = {
122
- ...head,
123
- paths,
124
- upload: {
125
- url: holding.url,
126
- operation: holding.operation,
127
- bytes,
128
- sha256
129
- }
130
- };
131
- submissions.set(head.operation_id, {
132
- sha256,
133
- body
134
- });
135
- return {
136
- body
137
- };
235
+ if (holding === null || holding.method !== 'POST')
236
+ return { outcome: UNCERTAIN };
237
+ const upload = { url: holding.url, operation: holding.operation, bytes, sha256 };
238
+ await keep(upload);
239
+ return { body: { ...head, paths, upload, runtime: notifications.runtime } };
138
240
  }
139
- async function submit(head, changes, delta, bytes, entry) {
140
- const prepared = uploadable(bytes) ? await uploaded(head, changes, delta, bytes, entry) : {
141
- body: {
142
- ...head,
143
- changes
144
- }
145
- };
146
- if ('outcome' in prepared) return prepared.outcome;
241
+ async function staged(answer, batch) {
242
+ const refusal = doorRefusal(answer.body);
243
+ if (refusal?.code !== 'files_unstaged')
244
+ return null;
245
+ const grants = refusal.fields.upload;
246
+ if (!Array.isArray(grants))
247
+ 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)
252
+ 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)
260
+ return false;
261
+ }
262
+ return true;
263
+ }
264
+ async function submit(batch, head, changes, delta, bytes, entry, handed, keep) {
265
+ const inline = { ...head, changes, runtime: notifications.runtime };
266
+ const prepared = uploadable(inline, bytes, declared())
267
+ ? await uploaded(head, changes, delta, bytes, entry, handed, keep)
268
+ : { body: inline };
269
+ if ('outcome' in prepared)
270
+ return prepared.outcome;
147
271
  const answer = await transport.door(DOOR_BATCH, prepared.body);
148
- return mutationOutcome(answer.status, answer.body, entry.locator, entry.handle);
272
+ const stagedFiles = await staged(answer, batch);
273
+ if (stagedFiles === false)
274
+ return UNCERTAIN;
275
+ const settled = stagedFiles === null ? answer : await transport.door(DOOR_BATCH, prepared.body);
276
+ if (stagedFiles === true && (await staged(settled, batch)) !== null)
277
+ return UNCERTAIN;
278
+ return mutationOutcome(settled.status, settled.body, entry.locator, entry.handle);
279
+ }
280
+ async function downloaded(answer, locator) {
281
+ const failure = classifyDoor(answer.status, answer.body, locator);
282
+ if (failure !== null) {
283
+ return failure.kind === 'scoped-refusal'
284
+ ? {
285
+ kind: 'rejected',
286
+ code: failure.code,
287
+ fields: failure.fields,
288
+ retryable: failure.retryable,
289
+ }
290
+ : failure;
291
+ }
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)
297
+ return UNCERTAIN_READ;
298
+ return { kind: 'binary', blob: read.blob };
149
299
  }
150
300
  return {
151
- async authorize () {
301
+ async authorize() {
152
302
  return options.authorize();
153
303
  },
154
- async model (_authorization, request) {
155
- return models.answer(request);
156
- },
157
- async discover (_authorization) {
158
- const answer = await transport.door(DOOR_DISCOVER, {
159
- prefixes: [
160
- ...DISCOVER_PREFIXES
161
- ]
162
- });
163
- const classified = classifyDoor(answer.status, answer.body, PRIVATE_AUTHORITY);
164
- if (classified !== null) return discoveryFailure(classified);
304
+ async discover(_authorization) {
305
+ const answer = await transport.door(DOOR_DISCOVER, { prefixes: [...DISCOVER_PREFIXES] });
306
+ const failed = discoveryFailure(answer.status, answer.body);
307
+ if (failed !== null)
308
+ return failed;
165
309
  const document = parseDiscovery(answer.body);
166
- if (document === null) return {
167
- kind: 'transport-uncertainty'
168
- };
310
+ if (document === null)
311
+ return { kind: 'transport-uncertainty' };
312
+ bounds = document.bounds;
169
313
  const entries = trackedFrom(document);
170
314
  tracked.clear();
171
- for (const entry of entries)tracked.set(entry.locator, entry);
315
+ for (const entry of entries)
316
+ tracked.set(entry.locator, entry);
317
+ for (const locator of handed.keys())
318
+ if (!tracked.has(locator))
319
+ handed.delete(locator);
172
320
  notifications.track(entries);
173
- const acquired = await acquireModels(entries);
174
- models.keep(entries.map((entry)=>entry.locator));
175
- return mapDiscovery(document, document.authorities.map((authority)=>roomOf(authority, acquired)), capabilities());
321
+ return {
322
+ ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document)),
323
+ member: document.member,
324
+ };
176
325
  },
177
- async bootstrap (_authorization, request) {
326
+ async bootstrap(_authorization, request) {
178
327
  const entry = authorityFor(request.locator);
179
- if (entry === undefined) return {
180
- kind: 'room-not-readable',
181
- locator: request.locator
182
- };
183
- if (request.pageToken !== undefined) return {
184
- kind: 'transport-uncertainty'
185
- };
186
- const answer = await transport.door(DOOR_BOOTSTRAP, {
187
- prefixes: [
188
- ...contentPrefixes(entry)
189
- ]
190
- });
191
- const classified = classifyDoor(answer.status, answer.body, entry.locator);
192
- if (classified !== null) {
193
- return classified.kind === 'beyond-horizon' ? {
194
- kind: 'transport-uncertainty'
195
- } : classified;
328
+ if (entry === undefined)
329
+ return { kind: 'room-not-readable', locator: request.locator };
330
+ if (request.pageToken !== undefined) {
331
+ return continuedBootstrap(entry, JSON.parse(request.pageToken));
196
332
  }
197
- const envelope = parseEnvelope(await bundled(answer.body));
198
- if (envelope === null) return {
199
- kind: 'transport-uncertainty'
200
- };
201
- return bootstrapFrom(envelope, entry);
333
+ return bootstrapTogether(entry);
202
334
  },
203
- async refresh (_authorization, request) {
335
+ async refresh(_authorization, request) {
204
336
  const entry = authorityFor(request.locator);
205
- if (entry === undefined) return {
206
- kind: 'room-not-readable',
207
- locator: request.locator
208
- };
209
- if (request.pageToken !== undefined) return {
210
- kind: 'transport-uncertainty'
211
- };
337
+ if (entry === undefined)
338
+ return { kind: 'room-not-readable', locator: request.locator };
339
+ if (request.pageToken !== undefined)
340
+ return { kind: 'transport-uncertainty' };
212
341
  const from = logPosition(request.fromRoomVersion);
213
- if (from === null) return {
214
- kind: 'beyond-horizon'
215
- };
216
- const streamed = notifications.advance(entry, from);
217
- if (streamed !== null) return streamed;
218
- const answer = await transport.door(DOOR_ENUMERATE, {
219
- prefixes: [
220
- ...contentPrefixes(entry)
221
- ],
222
- positions: [
223
- presented(entry.authority, from)
224
- ]
225
- });
226
- const classified = classifyDoor(answer.status, answer.body, entry.locator);
227
- if (classified !== null) return classified;
228
- const enumeration = parseEnumeration(answer.body, 'credential');
229
- if (enumeration === null) return {
230
- kind: 'transport-uncertainty'
231
- };
232
- return refreshFrom(enumeration, entry, request.fromRoomVersion);
342
+ if (from === null)
343
+ return { kind: 'beyond-horizon' };
344
+ const answered = await streamed(entry, request, from);
345
+ if (answered !== null)
346
+ return answered;
347
+ return enumerateTogether({ entry, from, fromRoomVersion: request.fromRoomVersion });
233
348
  },
234
- async mutate (_authorization, request) {
349
+ async mutate(_authorization, request, keep) {
235
350
  const entry = authorityFor(request.locator);
236
- if (entry === undefined) return {
237
- kind: 'room-not-readable',
238
- locator: request.locator
239
- };
351
+ if (entry === undefined)
352
+ return { kind: 'room-not-readable', locator: request.locator };
240
353
  const mapped = houseChanges(request.changeBatch, entry.handle);
241
- if (mapped.kind === 'unmapped') return rejectedMapping(mapped);
354
+ if (mapped.kind === 'unmapped')
355
+ return rejectedMapping(mapped);
242
356
  const head = operationHead(request.changeBatch);
243
357
  const delta = deltaBody(mapped.changes);
244
358
  const bytes = deltaBytes(delta);
245
- const resent = submissions.has(head.operation_id);
246
- let outcome = await submit(head, mapped.changes, delta, bytes, entry);
247
- if (resent && lapsedGrant(outcome)) {
248
- submissions.delete(head.operation_id);
249
- outcome = await submit(head, mapped.changes, delta, bytes, entry);
250
- }
251
- if (RESOLVED_OUTCOMES.has(outcome.kind)) submissions.delete(head.operation_id);
252
- return outcome;
359
+ const handed = request.upload;
360
+ const batch = request.changeBatch;
361
+ const outcome = await submit(batch, head, mapped.changes, delta, bytes, entry, handed, keep);
362
+ 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));
253
365
  },
254
- async history (_authorization, request) {
366
+ async history(_authorization, request) {
255
367
  const entry = authorityFor(request.locator);
256
- if (entry === undefined) return {
257
- kind: 'room-not-readable',
258
- locator: request.locator
259
- };
368
+ if (entry === undefined)
369
+ return { kind: 'room-not-readable', locator: request.locator };
260
370
  const answer = await transport.door(DOOR_REVISIONS, revisionBody({
261
371
  path: request.path,
262
372
  handle: entry.handle,
263
- ...request.revision === undefined ? {} : {
264
- revision: request.revision
265
- },
266
- ...request.pageToken === undefined ? {} : {
267
- pageToken: request.pageToken
268
- }
373
+ ...(request.revision === undefined ? {} : { revision: request.revision }),
374
+ ...(request.pageToken === undefined ? {} : { pageToken: request.pageToken }),
269
375
  }));
270
376
  return historyOutcome(answer.status, answer.body, entry.locator, {
271
377
  path: request.path,
272
- ...request.revision === undefined ? {} : {
273
- revision: request.revision
274
- }
378
+ ...(request.revision === undefined ? {} : { revision: request.revision }),
275
379
  });
276
380
  },
277
- async action (_authorization, request) {
278
- return houseAction(transport, request);
381
+ 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);
402
+ },
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 }) };
411
+ },
412
+ reached(locator, version, target) {
413
+ return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
414
+ },
415
+ follows(_locator, version, next) {
416
+ return followsVersion(version, next);
417
+ },
418
+ async action(_authorization, request) {
419
+ return withAt(await houseAction(transport, request));
279
420
  },
280
- roomVersions: notifications,
281
- conversations: notifications.conversations
421
+ roomStream: { hold: held, reopen: notifications.reopen, observe: notifications.observe },
422
+ conversations: notifications.conversations,
423
+ serviceMode: notifications.serviceMode,
282
424
  };
283
425
  }
284
-
285
- //# sourceMappingURL=adapter.js.map