@agentshouse/mdruntime-adapter 0.1.0-c38cdd8a6d962 → 0.1.0-c392f202fd8ec

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 +163 -3
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +121 -219
  5. package/dist/adapter.d.ts +1 -0
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +279 -208
  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 +16 -15
  15. package/dist/discovery.d.ts.map +1 -1
  16. package/dist/discovery.js +75 -52
  17. package/dist/house-model.d.ts +2983 -0
  18. package/dist/house-model.d.ts.map +1 -0
  19. package/dist/house-model.js +1643 -0
  20. package/dist/index.d.ts +9 -2
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +8 -1
  23. package/dist/model.d.ts +6 -3
  24. package/dist/model.d.ts.map +1 -1
  25. package/dist/model.js +23 -28
  26. package/dist/part.d.ts +14 -0
  27. package/dist/part.d.ts.map +1 -0
  28. package/dist/part.js +90 -0
  29. package/dist/read.d.ts +52 -41
  30. package/dist/read.d.ts.map +1 -1
  31. package/dist/read.js +248 -234
  32. package/dist/refusal.d.ts +3 -9
  33. package/dist/refusal.d.ts.map +1 -1
  34. package/dist/refusal.js +25 -19
  35. package/dist/room-stream.d.ts +19 -0
  36. package/dist/room-stream.d.ts.map +1 -0
  37. package/dist/room-stream.js +57 -0
  38. package/dist/room-types.d.ts +7 -0
  39. package/dist/room-types.d.ts.map +1 -0
  40. package/dist/room-types.js +126 -0
  41. package/dist/site.d.ts +1 -1
  42. package/dist/site.d.ts.map +1 -1
  43. package/dist/site.js +129 -106
  44. package/dist/stream.d.ts +12 -11
  45. package/dist/stream.d.ts.map +1 -1
  46. package/dist/stream.js +174 -145
  47. package/dist/transport.d.ts +79 -23
  48. package/dist/transport.d.ts.map +1 -1
  49. package/dist/transport.js +51 -10
  50. package/dist/upload.d.ts +12 -0
  51. package/dist/upload.d.ts.map +1 -0
  52. package/dist/upload.js +63 -0
  53. package/dist/version.d.ts +1 -0
  54. package/dist/version.d.ts.map +1 -1
  55. package/dist/version.js +1 -1
  56. package/dist/write.d.ts +18 -9
  57. package/dist/write.d.ts.map +1 -1
  58. package/dist/write.js +165 -41
  59. package/package.json +9 -6
  60. package/AGENTS.md +0 -238
  61. package/dist/gather.d.ts +0 -3
  62. package/dist/gather.d.ts.map +0 -1
  63. package/dist/gather.js +0 -40
  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/read.js CHANGED
@@ -1,13 +1,72 @@
1
+ import { paced, patching, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
+ import { gunzipped } from './part.js';
1
3
  import { readPosition, roomVersion } from './version.js';
4
+ const REVISION_FILE = /^fr_([0-9a-z]+)\.[0-9a-z]+\.[0-9a-z]$/u;
5
+ function fileOf(revision) {
6
+ return REVISION_FILE.exec(revision)?.[1];
7
+ }
8
+ export function answering() {
9
+ return {
10
+ documents: new Map(),
11
+ revisions: new Map(),
12
+ fileIds: new Map(),
13
+ changes: new Map(),
14
+ binaryFiles: new Map(),
15
+ removals: new Set(),
16
+ folders: new Set(),
17
+ folderRemovals: new Set(),
18
+ };
19
+ }
20
+ export function revised(answer, path, revision) {
21
+ answer.revisions.set(path, revision);
22
+ const file = fileOf(revision);
23
+ if (file !== undefined)
24
+ answer.fileIds.set(path, file);
25
+ }
26
+ export function forgotten(answer, path) {
27
+ answer.documents.delete(path);
28
+ answer.revisions.delete(path);
29
+ answer.fileIds.delete(path);
30
+ answer.changes.delete(path);
31
+ answer.binaryFiles.delete(path);
32
+ answer.removals.delete(path);
33
+ }
34
+ export function folded(answer, path, removed) {
35
+ if (removed) {
36
+ answer.folders.delete(path);
37
+ answer.folderRemovals.add(path);
38
+ }
39
+ else {
40
+ answer.folderRemovals.delete(path);
41
+ answer.folders.add(path);
42
+ }
43
+ }
44
+ function* pathsOf(paths) {
45
+ const list = [];
46
+ for (const path of paths) {
47
+ list.push(path);
48
+ yield ROW_WEIGHT;
49
+ }
50
+ return list;
51
+ }
52
+ export function* answered(answer) {
53
+ const removals = yield* pathsOf(answer.removals);
54
+ return {
55
+ documents: yield* patching({}, answer.documents),
56
+ revisions: yield* patching({}, answer.revisions),
57
+ ...(answer.fileIds.size === 0 ? {} : { fileIds: yield* patching({}, answer.fileIds) }),
58
+ changes: yield* patching({}, answer.changes),
59
+ binaryFiles: yield* patching({}, answer.binaryFiles),
60
+ removals,
61
+ ...(answer.folders.size === 0 ? {} : { folders: yield* pathsOf(answer.folders) }),
62
+ ...(answer.folderRemovals.size === 0
63
+ ? {}
64
+ : { folderRemovals: yield* pathsOf(answer.folderRemovals) }),
65
+ };
66
+ }
2
67
  export function authorityRoot(handle) {
3
68
  return `/rooms/${handle}`;
4
69
  }
5
- export function contentPrefixes(tracked) {
6
- return [
7
- authorityRoot(tracked.handle),
8
- ...tracked.schemas.map((schema) => schemaPath(tracked.handle, schema)),
9
- ];
10
- }
11
70
  export function schemaPath(handle, schema) {
12
71
  return `${authorityRoot(handle)}/${schema}`;
13
72
  }
@@ -20,22 +79,39 @@ export function parseGrant(body) {
20
79
  const transfer = body.transfer;
21
80
  if (typeof transfer.url !== 'string')
22
81
  return null;
23
- if (transfer.method === 'GET')
24
- return { method: 'GET', url: transfer.url };
25
82
  if (transfer.method !== 'POST' || typeof transfer.operation !== 'string')
26
83
  return null;
27
- return { method: 'POST', operation: transfer.operation, url: transfer.url };
84
+ return { operation: transfer.operation, url: transfer.url };
85
+ }
86
+ export function parseFileAddress(body) {
87
+ if (typeof body !== 'object' || body === null)
88
+ return null;
89
+ const { download, name, sha256, media_type: mediaType } = body;
90
+ if (typeof download !== 'string' ||
91
+ typeof name !== 'string' ||
92
+ typeof sha256 !== 'string' ||
93
+ typeof mediaType !== 'string') {
94
+ return null;
95
+ }
96
+ return { url: download, name, sha256, mediaType };
28
97
  }
29
98
  function changeAuthor(value) {
30
99
  if (typeof value !== 'object' || value === null)
31
100
  return null;
32
- const { user, site, name } = value;
101
+ const { user, site, inbox, app, name } = value;
33
102
  if (typeof name !== 'string')
34
103
  return null;
35
- if (typeof user === 'string' && site === undefined)
104
+ const named = [user, site, inbox, app].filter((id) => id !== undefined);
105
+ if (named.length !== 1)
106
+ return null;
107
+ if (typeof user === 'string')
36
108
  return { kind: 'user', id: user, name };
37
- if (typeof site === 'string' && user === undefined)
109
+ if (typeof site === 'string')
38
110
  return { kind: 'site', id: site, name };
111
+ if (typeof inbox === 'string')
112
+ return { kind: 'inbox', id: inbox, name };
113
+ if (typeof app === 'string')
114
+ return { kind: 'app', id: app, name };
39
115
  return null;
40
116
  }
41
117
  export function headChange(held) {
@@ -51,32 +127,16 @@ export function headChange(held) {
51
127
  function headed(head) {
52
128
  return head === undefined ? {} : { head };
53
129
  }
54
- function parseOriginal(value) {
55
- if (typeof value !== 'object' || value === null)
56
- return null;
57
- const { attachment, name, bytes, media_type: mediaType } = value;
58
- if (typeof attachment !== 'string' || typeof name !== 'string')
59
- return null;
60
- if (typeof bytes !== 'number' || !Number.isSafeInteger(bytes) || bytes < 0)
61
- return null;
62
- if (mediaType !== null && typeof mediaType !== 'string')
63
- return null;
64
- return { reference: attachment, name, byteLength: bytes, mediaType, entry: value };
65
- }
66
- export function parseOriginals(value) {
67
- if (!Array.isArray(value))
130
+ export function binaryFileOf(held) {
131
+ if (held.media_type === undefined)
132
+ return undefined;
133
+ const { bytes, media_type: mediaType, sha256 } = held;
134
+ if (typeof bytes !== 'number' || typeof mediaType !== 'string' || typeof sha256 !== 'string')
68
135
  return null;
69
- const attachments = [];
70
- for (const entry of value) {
71
- const attachment = parseOriginal(entry);
72
- if (attachment === null)
73
- return null;
74
- attachments.push(attachment);
75
- }
76
- return attachments;
136
+ return { byteLength: bytes, mediaType, sha256 };
77
137
  }
78
- function declared(attachments) {
79
- return attachments === undefined ? {} : { attachments };
138
+ function binaried(binary) {
139
+ return binary === undefined ? {} : { binary };
80
140
  }
81
141
  function parseFile(value, mode) {
82
142
  if (typeof value !== 'object' || value === null)
@@ -85,24 +145,54 @@ function parseFile(value, mode) {
85
145
  if (typeof file.path !== 'string')
86
146
  return null;
87
147
  const head = headChange(file);
88
- if (head === null)
148
+ const binary = binaryFileOf(file);
149
+ if (head === null || binary === null)
89
150
  return null;
90
151
  if (mode === 'site') {
91
152
  if (typeof file.sha256 !== 'string')
92
153
  return null;
93
- return { path: file.path, revision: undefined, version: file.sha256, ...headed(head) };
154
+ return {
155
+ path: file.path,
156
+ revision: binary?.sha256,
157
+ version: file.sha256,
158
+ ...headed(head),
159
+ ...binaried(binary),
160
+ };
94
161
  }
95
162
  if (typeof file.revision !== 'string')
96
163
  return null;
97
- return { path: file.path, revision: file.revision, version: file.revision, ...headed(head) };
164
+ return {
165
+ path: file.path,
166
+ revision: file.revision,
167
+ version: file.revision,
168
+ ...(typeof file.sha256 === 'string' ? { sha256: file.sha256 } : {}),
169
+ ...headed(head),
170
+ ...binaried(binary),
171
+ };
172
+ }
173
+ function parseModels(value, mode) {
174
+ if (!Array.isArray(value))
175
+ return null;
176
+ const files = [];
177
+ const contents = [];
178
+ for (const entry of value) {
179
+ const file = parseFile(entry, mode);
180
+ const { content } = entry;
181
+ if (file === null || typeof content !== 'string')
182
+ return null;
183
+ files.push(file);
184
+ contents.push({ path: file.path, content });
185
+ }
186
+ return { files, contents };
98
187
  }
99
- function parseScope(value, mode) {
188
+ function parseRoomHead(value, mode) {
100
189
  if (typeof value !== 'object' || value === null)
101
190
  return null;
102
191
  const held = value;
103
192
  if (typeof held.manifest !== 'object' || held.manifest === null)
104
193
  return null;
105
- if (!Array.isArray(held.contents))
194
+ const models = parseModels(held.models, mode);
195
+ if (models === null)
106
196
  return null;
107
197
  const manifest = held.manifest;
108
198
  if (manifest.scope !== 'room')
@@ -112,221 +202,145 @@ function parseScope(value, mode) {
112
202
  return null;
113
203
  if (typeof manifest.room_handle !== 'string' || manifest.room_handle.length === 0)
114
204
  return null;
115
- if (!Array.isArray(manifest.files))
116
- return null;
117
- const files = [];
118
- for (const entry of manifest.files) {
119
- const file = parseFile(entry, mode);
120
- if (file === null)
121
- return null;
122
- files.push(file);
123
- }
124
- const contents = [];
125
- for (const entry of held.contents) {
126
- if (typeof entry !== 'object' || entry === null)
127
- return null;
128
- const content = entry;
129
- if (typeof content.path !== 'string' || typeof content.content !== 'string')
130
- return null;
131
- contents.push({ path: content.path, content: content.content });
132
- }
133
- if (mode === 'site')
134
- return { anchor: manifest.anchor, ...at, files, contents };
135
- const attachments = parseOriginals(held.originals);
136
- if (attachments === null)
137
- return null;
138
- return { anchor: manifest.anchor, ...at, files, contents, attachments };
205
+ return { anchor: manifest.anchor, ...at, models };
139
206
  }
140
- export function parseEnvelope(body) {
141
- if (typeof body !== 'object' || body === null)
142
- return null;
143
- if (!('scopes' in body) || !Array.isArray(body.scopes))
207
+ export async function roomHead(payload, mode) {
208
+ let text = '';
209
+ try {
210
+ for await (const chunk of gunzipped(payload))
211
+ text += chunk;
212
+ return text.endsWith('\n') && text.indexOf('\n') === text.length - 1
213
+ ? parseRoomHead(JSON.parse(text), mode)
214
+ : null;
215
+ }
216
+ catch {
144
217
  return null;
145
- const scopes = [];
146
- for (const entry of body.scopes) {
147
- const scope = parseScope(entry, 'credential');
148
- if (scope === null)
149
- return null;
150
- scopes.push(scope);
151
218
  }
152
- return { scopes };
153
219
  }
154
- export function parseSiteBootstrap(body) {
155
- return parseScope(body, 'site');
220
+ export function manifested(room, pageToken, models) {
221
+ return {
222
+ kind: 'bootstrap',
223
+ roomVersion: roomVersion(room),
224
+ documents: {},
225
+ revisions: {},
226
+ changes: {},
227
+ binaryFiles: {},
228
+ pageToken,
229
+ ...(models === undefined ? {} : { models }),
230
+ };
156
231
  }
157
- function parseChange(value, mode) {
158
- if (typeof value !== 'object' || value === null)
159
- return null;
160
- const held = value;
161
- if (typeof held.path !== 'string')
162
- return null;
163
- if (held.removed === true)
164
- return { path: held.path, removed: true };
165
- const head = headChange(held);
166
- if (head === null)
167
- return null;
168
- if (mode === 'site') {
169
- if (typeof held.content !== 'string' || typeof held.sha256 !== 'string')
170
- return null;
171
- return {
172
- path: held.path,
173
- content: held.content,
174
- revision: undefined,
175
- version: held.sha256,
176
- ...headed(head),
177
- };
232
+ export function modelScope(room) {
233
+ return { ...emptyScope(room), files: room.models.files, contents: room.models.contents };
234
+ }
235
+ export function emptyScope(room) {
236
+ return {
237
+ anchor: room.anchor,
238
+ position: room.position,
239
+ logEpoch: room.logEpoch,
240
+ files: [],
241
+ contents: [],
242
+ };
243
+ }
244
+ function partLine(line, mode, read) {
245
+ if (read.ended)
246
+ return false;
247
+ let entry;
248
+ try {
249
+ entry = JSON.parse(line);
178
250
  }
179
- if (typeof held.content === 'string' && typeof held.revision === 'string') {
180
- return {
181
- path: held.path,
182
- content: held.content,
183
- revision: held.revision,
184
- version: held.revision,
185
- ...headed(head),
186
- };
251
+ catch {
252
+ return false;
187
253
  }
188
- if (typeof held.reference === 'object' && held.reference !== null) {
189
- const reference = held.reference;
190
- if (typeof reference.revision !== 'string')
191
- return null;
192
- return { path: held.path, reference: { revision: reference.revision } };
254
+ if (endLine(entry)) {
255
+ read.ended = true;
256
+ return true;
193
257
  }
194
- return null;
258
+ if (folderLine(entry)) {
259
+ read.folders.push(entry.folder);
260
+ return true;
261
+ }
262
+ const file = parseFile(entry, mode);
263
+ if (file === null)
264
+ return false;
265
+ read.files.push(file);
266
+ const { content } = entry;
267
+ if (typeof content === 'string')
268
+ read.contents.push({ path: file.path, content });
269
+ else if (content !== undefined)
270
+ return false;
271
+ return true;
195
272
  }
196
- export function parseEnumeration(body, mode) {
197
- if (typeof body !== 'object' || body === null)
198
- return null;
199
- if (!('authorities' in body) || !Array.isArray(body.authorities))
200
- return null;
201
- const authorities = [];
202
- for (const entry of body.authorities) {
203
- if (typeof entry !== 'object' || entry === null)
204
- return null;
205
- const held = entry;
206
- const at = readPosition(held);
207
- if (typeof held.authority !== 'string' || at === null)
208
- return null;
209
- if (!Array.isArray(held.changes))
210
- return null;
211
- const changes = [];
212
- for (const change of held.changes) {
213
- const mapped = parseChange(change, mode);
214
- if (mapped === null)
273
+ async function partRead(chunks, mode) {
274
+ const read = { files: [], contents: [], folders: [], ended: false };
275
+ const pace = paced();
276
+ const unfinished = [];
277
+ for (let chunk = await chunks.read(); chunk.done !== true; chunk = await chunks.read()) {
278
+ const text = chunk.value;
279
+ let start = 0;
280
+ for (let end = text.indexOf('\n'); end !== -1; end = text.indexOf('\n', start)) {
281
+ const piece = text.slice(start, end);
282
+ const line = unfinished.length === 0 ? piece : unfinished.splice(0).join('') + piece;
283
+ if (!partLine(line, mode, read)) {
284
+ await chunks.cancel();
215
285
  return null;
216
- changes.push(mapped);
286
+ }
287
+ start = end + 1;
288
+ const due = pace(line.length + ROW_WEIGHT);
289
+ if (due instanceof Promise)
290
+ await due;
217
291
  }
218
- if (mode === 'site') {
219
- authorities.push({ authority: held.authority, ...at, changes });
220
- continue;
221
- }
222
- const attachments = parseOriginals(held.originals);
223
- if (attachments === null)
224
- return null;
225
- authorities.push({ authority: held.authority, ...at, changes, attachments });
292
+ if (start < text.length)
293
+ unfinished.push(text.slice(start));
226
294
  }
227
- return { authorities };
295
+ return unfinished.length === 0 ? read : null;
228
296
  }
229
- export function advancedScope(scope, view) {
230
- const files = new Map(scope.files.map((file) => [file.path, file]));
231
- const contents = new Map(scope.contents.map((entry) => [entry.path, entry.content]));
232
- for (const change of view.changes) {
233
- files.delete(change.path);
234
- contents.delete(change.path);
235
- if ('removed' in change)
236
- continue;
237
- if ('content' in change) {
238
- files.set(change.path, {
239
- path: change.path,
240
- revision: change.revision,
241
- version: change.version,
242
- ...headed(change.head),
243
- });
244
- contents.set(change.path, change.content);
245
- continue;
246
- }
247
- const { revision } = change.reference;
248
- files.set(change.path, { path: change.path, revision, version: revision });
249
- }
250
- return {
251
- ...scope,
252
- position: view.position,
253
- logEpoch: view.logEpoch,
254
- files: [...files.values()],
255
- contents: [...contents].map(([path, content]) => ({ path, content })),
256
- };
297
+ function endLine(entry) {
298
+ if (typeof entry !== 'object' || entry === null || !('end' in entry))
299
+ return false;
300
+ return entry.end === true && Object.keys(entry).length === 1;
301
+ }
302
+ function folderLine(entry) {
303
+ if (typeof entry !== 'object' || entry === null || !('folder' in entry))
304
+ return false;
305
+ return typeof entry.folder === 'string' && Object.keys(entry).length === 1;
257
306
  }
258
- export function matchesScope(scope, tracked) {
259
- return scope.anchor === tracked.authority;
307
+ export async function housePart(room, payload, mode) {
308
+ const read = await partRead(gunzipped(payload).getReader(), mode).catch(() => null);
309
+ if (read === null)
310
+ return { kind: 'transport-uncertainty' };
311
+ const { ended, ...scope } = read;
312
+ return { scope: { ...emptyScope(room), ...scope }, ended };
260
313
  }
261
- export function bootstrapOf(scope) {
262
- const contents = new Map(scope.contents.map((entry) => [entry.path, entry.content]));
263
- const documents = {};
264
- const revisions = {};
265
- const changes = {};
266
- const binaryTargets = [];
314
+ export function* bootstrapping(scope) {
315
+ const contents = new Map();
316
+ for (const entry of scope.contents) {
317
+ contents.set(entry.path, entry.content);
318
+ yield ROW_WEIGHT;
319
+ }
320
+ const answer = answering();
267
321
  for (const file of scope.files) {
268
- if (file.revision !== undefined)
269
- revisions[file.path] = file.revision;
322
+ yield ROW_WEIGHT;
270
323
  if (file.head !== undefined)
271
- changes[file.path] = file.head;
324
+ answer.changes.set(file.path, file.head);
272
325
  const content = contents.get(file.path);
273
- if (content === undefined)
274
- binaryTargets.push(file.path);
275
- else
276
- documents[file.path] = content;
326
+ if (file.binary !== undefined)
327
+ answer.binaryFiles.set(file.path, file.binary);
328
+ else if (content !== undefined)
329
+ answer.documents.set(file.path, content);
330
+ if (file.revision !== undefined)
331
+ revised(answer, file.path, file.revision);
277
332
  }
278
- return {
279
- kind: 'bootstrap',
280
- roomVersion: roomVersion(scope),
281
- documents,
282
- revisions,
283
- changes,
284
- binaryTargets: binaryTargets.sort(),
285
- ...declared(scope.attachments),
286
- };
333
+ for (const folder of scope.folders ?? [])
334
+ folded(answer, folder, false);
335
+ const { removals: _removals, folderRemovals: _folderRemovals, ...page } = yield* answered(answer);
336
+ return { kind: 'bootstrap', roomVersion: roomVersion(scope), ...page };
287
337
  }
288
338
  export function schemaOf(scope, schema) {
289
339
  const file = scope.files.find((entry) => entry.path === schema);
290
340
  if (file === undefined)
291
- return { schema: null, version: null };
341
+ return { schema: null, version: null, revision: null };
292
342
  const content = scope.contents.find((entry) => entry.path === schema);
293
- return content === undefined ? null : { schema: content.content, version: file.version };
294
- }
295
- export function viewOf(enumeration, tracked) {
296
- return enumeration.authorities.find((entry) => entry.authority === tracked.authority);
297
- }
298
- export function refreshFrom(view, fromRoomVersion) {
299
- if (roomVersion(view) === fromRoomVersion)
300
- return { kind: 'still-current' };
301
- const documents = {};
302
- const revisions = {};
303
- const changes = {};
304
- const binaryTargets = [];
305
- const removals = [];
306
- for (const change of view.changes) {
307
- if ('removed' in change) {
308
- removals.push(change.path);
309
- continue;
310
- }
311
- if ('content' in change) {
312
- documents[change.path] = change.content;
313
- if (change.revision !== undefined)
314
- revisions[change.path] = change.revision;
315
- if (change.head !== undefined)
316
- changes[change.path] = change.head;
317
- continue;
318
- }
319
- binaryTargets.push(change.path);
320
- revisions[change.path] = change.reference.revision;
321
- }
322
- return {
323
- kind: 'net-advancement',
324
- roomVersion: roomVersion(view),
325
- documents,
326
- revisions,
327
- changes,
328
- binaryTargets: binaryTargets.sort(),
329
- removals: removals.sort(),
330
- ...declared(view.attachments),
331
- };
343
+ return content === undefined
344
+ ? null
345
+ : { schema: content.content, version: file.version, revision: file.revision ?? null };
332
346
  }
package/dist/refusal.d.ts CHANGED
@@ -1,12 +1,6 @@
1
1
  import type { AdapterFailure, DiscoverResult, ProviderRefusal, ScopedRefusal } from '@agentshouse/mdruntime';
2
- export type DoorRefusal = ProviderRefusal & {
3
- retryable: boolean;
4
- };
5
- export declare function doorRefusal(body: unknown): DoorRefusal | null;
6
- export declare function scopedRefusal(locator: string, scope: ScopedRefusal['scope'], refusal: DoorRefusal): ScopedRefusal;
2
+ export declare function doorRefusal(body: unknown): ProviderRefusal | null;
3
+ export declare function scopedRefusal(locator: string, scope: ScopedRefusal['scope'], refusal: ProviderRefusal): ScopedRefusal;
7
4
  export declare function classifyDoor(status: number, body: unknown, locator: string): AdapterFailure | null;
8
- export declare function classifyRead(status: number, body: unknown, locator: string): AdapterFailure | {
9
- kind: 'beyond-horizon';
10
- } | null;
11
- export declare function discoveryFailure(classified: AdapterFailure): DiscoverResult;
5
+ export declare function discoveryFailure(status: number, body: unknown): DiscoverResult | null;
12
6
  //# sourceMappingURL=refusal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"refusal.d.ts","sourceRoot":"","sources":["../src/refusal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,eAAe,EACf,aAAa,EACd,MAAM,wBAAwB,CAAC;AAEhC,MAAM,MAAM,WAAW,GAAG,eAAe,GAAG;IAAE,SAAS,EAAE,OAAO,CAAA;CAAE,CAAC;AAEnE,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,GAAG,IAAI,CAO7D;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAC7B,OAAO,EAAE,WAAW,GACnB,aAAa,CAEf;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG,IAAI,CAYvB;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG;IAAE,IAAI,EAAE,gBAAgB,CAAA;CAAE,GAAG,IAAI,CAGpD;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,cAAc,GAAG,cAAc,CAM3E"}
1
+ {"version":3,"file":"refusal.d.ts","sourceRoot":"","sources":["../src/refusal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,eAAe,EACf,aAAa,EACd,MAAM,wBAAwB,CAAC;AAEhC,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,IAAI,CAOjE;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAC7B,OAAO,EAAE,eAAe,GACvB,aAAa,CAEf;AAqBD,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG,IAAI,CAMvB;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,cAAc,GAAG,IAAI,CAQrF"}
package/dist/refusal.js CHANGED
@@ -6,41 +6,47 @@ export function doorRefusal(body) {
6
6
  const error = body.error;
7
7
  if (typeof error.code !== 'string')
8
8
  return null;
9
- const { code, ...fields } = error;
10
- return { code, fields, retryable: error.retryable === true };
9
+ const { code, retryable, ...fields } = error;
10
+ return { code, fields, retryable: retryable === true };
11
11
  }
12
12
  export function scopedRefusal(locator, scope, refusal) {
13
- return { kind: 'scoped-refusal', locator, scope, code: refusal.code, fields: refusal.fields };
13
+ return { kind: 'scoped-refusal', locator, scope, ...refusal };
14
14
  }
15
- export function classifyDoor(status, body, locator) {
15
+ function doorFailure(status, body) {
16
16
  if (status === 0)
17
17
  return { kind: 'transport-uncertainty' };
18
18
  const refusal = doorRefusal(body);
19
19
  if (status === 401)
20
20
  return { kind: 'revoked-authorization' };
21
- if (refusal?.retryable === true)
21
+ if (refusal?.retryable === true || refusal?.code === 'update_required') {
22
22
  return { kind: 'transport-uncertainty' };
23
- if (status === 404 || refusal?.code === 'room_not_found') {
24
- return { kind: 'room-not-readable', locator };
25
23
  }
24
+ if (status === 404 || refusal?.code === 'room_not_found')
25
+ return { kind: 'room-not-readable' };
26
26
  if (status < 400)
27
27
  return null;
28
28
  if (refusal === null)
29
29
  return { kind: 'transport-uncertainty' };
30
- if (status === 403)
31
- return scopedRefusal(locator, 'operation', refusal);
32
- return scopedRefusal(locator, 'undisclosed', refusal);
30
+ return { kind: 'refused', scope: status === 403 ? 'operation' : 'undisclosed', refusal };
33
31
  }
34
- export function classifyRead(status, body, locator) {
35
- if (doorRefusal(body)?.code === 'expired')
36
- return { kind: 'beyond-horizon' };
37
- return classifyDoor(status, body, locator);
32
+ export function classifyDoor(status, body, locator) {
33
+ const failure = doorFailure(status, body);
34
+ if (failure === null)
35
+ return null;
36
+ if (failure.kind === 'room-not-readable')
37
+ return { kind: 'room-not-readable', locator };
38
+ if (failure.kind === 'refused')
39
+ return scopedRefusal(locator, failure.scope, failure.refusal);
40
+ return failure;
38
41
  }
39
- export function discoveryFailure(classified) {
40
- if (classified.kind === 'revoked-authorization')
41
- return classified;
42
- if (classified.kind === 'scoped-refusal') {
43
- return { kind: 'contract-unavailable', code: classified.code, fields: classified.fields };
42
+ export function discoveryFailure(status, body) {
43
+ const failure = doorFailure(status, body);
44
+ if (failure === null)
45
+ return null;
46
+ if (failure.kind === 'revoked-authorization')
47
+ return failure;
48
+ if (failure.kind === 'refused') {
49
+ return { kind: 'contract-unavailable', ...failure.refusal };
44
50
  }
45
51
  return { kind: 'transport-uncertainty' };
46
52
  }