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

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 (79) hide show
  1. package/README.md +3 -3
  2. package/dist/actions.d.ts +15 -6
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +198 -324
  5. package/dist/adapter.d.ts +1 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +309 -228
  8. package/dist/authored-path.d.ts +61 -0
  9. package/dist/authored-path.d.ts.map +1 -0
  10. package/dist/authored-path.js +166 -0
  11. package/dist/chunks.d.ts +36 -0
  12. package/dist/chunks.d.ts.map +1 -0
  13. package/dist/chunks.js +281 -0
  14. package/dist/digest.d.ts +0 -1
  15. package/dist/discovery.d.ts +46 -20
  16. package/dist/discovery.d.ts.map +1 -1
  17. package/dist/discovery.js +265 -67
  18. package/dist/history.d.ts +6 -0
  19. package/dist/history.d.ts.map +1 -0
  20. package/dist/history.js +85 -0
  21. package/dist/index.d.ts +10 -3
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +9 -1
  24. package/dist/map.d.ts +0 -1
  25. package/dist/model.d.ts +6 -6
  26. package/dist/model.d.ts.map +1 -1
  27. package/dist/model.js +35 -38
  28. package/dist/read.d.ts +55 -48
  29. package/dist/read.d.ts.map +1 -1
  30. package/dist/read.js +209 -265
  31. package/dist/refusal.d.ts +9 -11
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +54 -27
  34. package/dist/renamed.d.ts +10 -0
  35. package/dist/renamed.d.ts.map +1 -0
  36. package/dist/renamed.js +20 -0
  37. package/dist/room-query.d.ts +4 -0
  38. package/dist/room-query.d.ts.map +1 -0
  39. package/dist/room-query.js +63 -0
  40. package/dist/search.d.ts +4 -0
  41. package/dist/search.d.ts.map +1 -0
  42. package/dist/search.js +47 -0
  43. package/dist/session-content/check.d.ts +17 -0
  44. package/dist/session-content/index.d.ts +6 -0
  45. package/dist/session-content/parts.d.ts +30 -0
  46. package/dist/session-content/state.d.ts +59 -0
  47. package/dist/session-content/turn.d.ts +137 -0
  48. package/dist/site.d.ts +1 -2
  49. package/dist/site.d.ts.map +1 -1
  50. package/dist/site.js +161 -108
  51. package/dist/space-schema.d.ts +6 -0
  52. package/dist/space-schema.d.ts.map +1 -0
  53. package/dist/space-schema.js +11 -0
  54. package/dist/stream.d.ts +14 -13
  55. package/dist/stream.d.ts.map +1 -1
  56. package/dist/stream.js +281 -149
  57. package/dist/transport.d.ts +131 -31
  58. package/dist/transport.d.ts.map +1 -1
  59. package/dist/transport.js +90 -10
  60. package/dist/uncertain.d.ts +5 -0
  61. package/dist/uncertain.d.ts.map +1 -0
  62. package/dist/uncertain.js +11 -0
  63. package/dist/upload.d.ts +17 -0
  64. package/dist/upload.d.ts.map +1 -0
  65. package/dist/upload.js +106 -0
  66. package/dist/version.d.ts +1 -1
  67. package/dist/version.d.ts.map +1 -1
  68. package/dist/version.js +1 -1
  69. package/dist/write.d.ts +15 -12
  70. package/dist/write.d.ts.map +1 -1
  71. package/dist/write.js +223 -134
  72. package/package.json +11 -7
  73. package/AGENTS.md +0 -238
  74. package/dist/gather.d.ts +0 -3
  75. package/dist/gather.d.ts.map +0 -1
  76. package/dist/gather.js +0 -40
  77. package/dist/links.d.ts +0 -20
  78. package/dist/links.d.ts.map +0 -1
  79. package/dist/links.js +0 -104
package/dist/read.js CHANGED
@@ -1,13 +1,77 @@
1
- import { readPosition, roomVersion } from './version.js';
1
+ import { patching, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
+ import { REVISION_REFERENCE_PATTERN } from '@agentshouse/mdmodel';
3
+ import { roomVersion } from './version.js';
4
+ function fileOf(revision) {
5
+ return REVISION_REFERENCE_PATTERN.exec(revision)?.[1];
6
+ }
7
+ export function answering() {
8
+ return {
9
+ documents: new Map(),
10
+ facts: new Map(),
11
+ revisions: new Map(),
12
+ fileIds: new Map(),
13
+ created: new Map(),
14
+ changes: new Map(),
15
+ attachments: new Map(),
16
+ removals: new Set(),
17
+ folders: new Set(),
18
+ folderRemovals: new Set(),
19
+ };
20
+ }
21
+ export function revised(answer, path, revision) {
22
+ answer.revisions.set(path, revision);
23
+ const file = fileOf(revision);
24
+ if (file !== undefined)
25
+ answer.fileIds.set(path, file);
26
+ }
27
+ export function forgotten(answer, path) {
28
+ answer.documents.delete(path);
29
+ answer.facts.delete(path);
30
+ answer.revisions.delete(path);
31
+ answer.fileIds.delete(path);
32
+ answer.created.delete(path);
33
+ answer.changes.delete(path);
34
+ answer.attachments.delete(path);
35
+ answer.removals.delete(path);
36
+ }
37
+ export function folded(answer, path, removed) {
38
+ if (removed) {
39
+ answer.folders.delete(path);
40
+ answer.folderRemovals.add(path);
41
+ }
42
+ else {
43
+ answer.folderRemovals.delete(path);
44
+ answer.folders.add(path);
45
+ }
46
+ }
47
+ function* pathsOf(paths) {
48
+ const list = [];
49
+ for (const path of paths) {
50
+ list.push(path);
51
+ yield ROW_WEIGHT;
52
+ }
53
+ return list;
54
+ }
55
+ export function* answered(answer) {
56
+ const removals = yield* pathsOf(answer.removals);
57
+ return {
58
+ documents: yield* patching({}, answer.documents),
59
+ facts: yield* patching({}, answer.facts),
60
+ revisions: yield* patching({}, answer.revisions),
61
+ ...(answer.fileIds.size === 0 ? {} : { fileIds: yield* patching({}, answer.fileIds) }),
62
+ ...(answer.created.size === 0 ? {} : { created: yield* patching({}, answer.created) }),
63
+ changes: yield* patching({}, answer.changes),
64
+ attachments: yield* patching({}, answer.attachments),
65
+ removals,
66
+ ...(answer.folders.size === 0 ? {} : { folders: yield* pathsOf(answer.folders) }),
67
+ ...(answer.folderRemovals.size === 0
68
+ ? {}
69
+ : { folderRemovals: yield* pathsOf(answer.folderRemovals) }),
70
+ };
71
+ }
2
72
  export function authorityRoot(handle) {
3
73
  return `/rooms/${handle}`;
4
74
  }
5
- export function contentPrefixes(tracked) {
6
- return [
7
- authorityRoot(tracked.handle),
8
- ...tracked.schemas.map((schema) => schemaPath(tracked.handle, schema)),
9
- ];
10
- }
11
75
  export function schemaPath(handle, schema) {
12
76
  return `${authorityRoot(handle)}/${schema}`;
13
77
  }
@@ -20,313 +84,193 @@ export function parseGrant(body) {
20
84
  const transfer = body.transfer;
21
85
  if (typeof transfer.url !== 'string')
22
86
  return null;
23
- if (transfer.method === 'GET')
24
- return { method: 'GET', url: transfer.url };
25
87
  if (transfer.method !== 'POST' || typeof transfer.operation !== 'string')
26
88
  return null;
27
- return { method: 'POST', operation: transfer.operation, url: transfer.url };
89
+ return { operation: transfer.operation, url: transfer.url };
90
+ }
91
+ function fileCopies(copies) {
92
+ if (copies === undefined)
93
+ return [];
94
+ if (!Array.isArray(copies))
95
+ return null;
96
+ const read = copies.map((copy) => {
97
+ if (typeof copy !== 'object' || copy === null)
98
+ return null;
99
+ const { download, width } = copy;
100
+ return typeof download === 'string' && typeof width === 'number'
101
+ ? { url: download, width }
102
+ : null;
103
+ });
104
+ return read.every((copy) => copy !== null) ? read : null;
105
+ }
106
+ export function parseFileAddress(body) {
107
+ if (typeof body !== 'object' || body === null)
108
+ return null;
109
+ const { download, name, sha256, media_type: mediaType, copies } = body;
110
+ const offered = fileCopies(copies);
111
+ if (typeof download !== 'string' ||
112
+ typeof name !== 'string' ||
113
+ typeof sha256 !== 'string' ||
114
+ typeof mediaType !== 'string' ||
115
+ offered === null) {
116
+ return null;
117
+ }
118
+ return { url: download, name, sha256, mediaType, copies: offered };
28
119
  }
29
120
  function changeAuthor(value) {
30
121
  if (typeof value !== 'object' || value === null)
31
122
  return null;
32
- const { user, site, name } = value;
123
+ const { user, site, inbox, app, name } = value;
33
124
  if (typeof name !== 'string')
34
125
  return null;
35
- if (typeof user === 'string' && site === undefined)
126
+ const named = [user, site, inbox, app].filter((id) => id !== undefined);
127
+ if (named.length !== 1)
128
+ return null;
129
+ if (typeof user === 'string')
36
130
  return { kind: 'user', id: user, name };
37
- if (typeof site === 'string' && user === undefined)
131
+ if (typeof site === 'string')
38
132
  return { kind: 'site', id: site, name };
133
+ if (typeof inbox === 'string')
134
+ return { kind: 'inbox', id: inbox, name };
135
+ if (typeof app === 'string')
136
+ return { kind: 'app', id: app, name };
39
137
  return null;
40
138
  }
41
139
  export function headChange(held) {
42
140
  const author = held.author === undefined ? null : changeAuthor(held.author);
43
141
  if (held.author !== undefined && author === null)
44
142
  return null;
45
- if (held.created_at === undefined)
143
+ if (held.changed_at === undefined)
46
144
  return undefined;
47
- if (typeof held.created_at !== 'string')
145
+ if (typeof held.changed_at !== 'string')
48
146
  return null;
49
- return { author, time: held.created_at };
147
+ return { author, time: held.changed_at };
148
+ }
149
+ function createdOf(held) {
150
+ if (held.created_at === undefined)
151
+ return undefined;
152
+ return typeof held.created_at === 'string' ? held.created_at : null;
153
+ }
154
+ function createdHeld(created) {
155
+ return created === undefined ? {} : { created };
50
156
  }
51
157
  function headed(head) {
52
158
  return head === undefined ? {} : { head };
53
159
  }
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)
160
+ function side(value) {
161
+ return Number.isInteger(value) && value > 0;
162
+ }
163
+ function imageSize(width, height) {
164
+ if (width === undefined && height === undefined)
165
+ return { width: null, height: null };
166
+ return side(width) && side(height) ? { width, height } : null;
167
+ }
168
+ export function attachmentOf(held) {
169
+ if (held.media_type === undefined)
170
+ return undefined;
171
+ const { bytes, media_type: mediaType, sha256 } = held;
172
+ if (typeof bytes !== 'number' || typeof mediaType !== 'string' || typeof sha256 !== 'string')
61
173
  return null;
62
- if (mediaType !== null && typeof mediaType !== 'string')
174
+ const size = imageSize(held.width, held.height);
175
+ if (size === null)
63
176
  return null;
64
- return { reference: attachment, name, byteLength: bytes, mediaType, entry: value };
177
+ return { byteLength: bytes, mediaType, sha256, ...size };
65
178
  }
66
- export function parseOriginals(value) {
67
- if (!Array.isArray(value))
68
- 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;
179
+ function withAttachment(binary) {
180
+ return binary === undefined ? {} : { binary };
181
+ }
182
+ function carriedFacts(held) {
183
+ return held.facts === undefined ? {} : { facts: held.facts };
77
184
  }
78
- function declared(attachments) {
79
- return attachments === undefined ? {} : { attachments };
185
+ function carriedSpace(held) {
186
+ return typeof held.space === 'string' ? { space: held.space } : {};
80
187
  }
81
- function parseFile(value, mode) {
188
+ export function documentEntry(value, mode) {
82
189
  if (typeof value !== 'object' || value === null)
83
190
  return null;
84
191
  const file = value;
85
192
  if (typeof file.path !== 'string')
86
193
  return null;
194
+ if (file.content !== undefined && typeof file.content !== 'string')
195
+ return null;
87
196
  const head = headChange(file);
88
- if (head === null)
197
+ const created = createdOf(file);
198
+ const binary = attachmentOf(file);
199
+ if (head === null || created === null || binary === null)
89
200
  return null;
201
+ const content = file.content === undefined ? {} : { content: file.content };
90
202
  if (mode === 'site') {
91
203
  if (typeof file.sha256 !== 'string')
92
204
  return null;
93
- return { path: file.path, revision: undefined, version: file.sha256, ...headed(head) };
94
- }
95
- if (typeof file.revision !== 'string')
96
- return null;
97
- return { path: file.path, revision: file.revision, version: file.revision, ...headed(head) };
98
- }
99
- function parseScope(value, mode) {
100
- if (typeof value !== 'object' || value === null)
101
- return null;
102
- const held = value;
103
- if (typeof held.manifest !== 'object' || held.manifest === null)
104
- return null;
105
- if (!Array.isArray(held.contents))
106
- return null;
107
- const manifest = held.manifest;
108
- if (manifest.scope !== 'room')
109
- return null;
110
- const at = readPosition(manifest);
111
- if (typeof manifest.anchor !== 'string' || manifest.anchor.length === 0 || at === null)
112
- return null;
113
- if (typeof manifest.room_handle !== 'string' || manifest.room_handle.length === 0)
114
- 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 };
139
- }
140
- export function parseEnvelope(body) {
141
- if (typeof body !== 'object' || body === null)
142
- return null;
143
- if (!('scopes' in body) || !Array.isArray(body.scopes))
144
- 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
- }
152
- return { scopes };
153
- }
154
- export function parseSiteBootstrap(body) {
155
- return parseScope(body, 'site');
156
- }
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
- };
178
- }
179
- if (typeof held.content === 'string' && typeof held.revision === 'string') {
180
205
  return {
181
- path: held.path,
182
- content: held.content,
183
- revision: held.revision,
184
- version: held.revision,
206
+ path: file.path,
207
+ revision: binary?.sha256,
208
+ version: file.sha256,
185
209
  ...headed(head),
210
+ ...withAttachment(binary),
211
+ ...carriedFacts(file),
212
+ ...content,
186
213
  };
187
214
  }
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 } };
193
- }
194
- return null;
195
- }
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))
215
+ if (typeof file.revision !== 'string')
200
216
  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)
215
- return null;
216
- changes.push(mapped);
217
- }
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 });
226
- }
227
- return { authorities };
228
- }
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
217
  return {
251
- ...scope,
252
- position: view.position,
253
- logEpoch: view.logEpoch,
254
- files: [...files.values()],
255
- contents: [...contents].map(([path, content]) => ({ path, content })),
218
+ path: file.path,
219
+ revision: file.revision,
220
+ version: file.revision,
221
+ ...headed(head),
222
+ ...createdHeld(created),
223
+ ...withAttachment(binary),
224
+ ...carriedFacts(file),
225
+ ...carriedSpace(file),
226
+ ...content,
256
227
  };
257
228
  }
258
- export function matchesScope(scope, tracked) {
259
- return scope.anchor === tracked.authority;
229
+ export function kept(answer, file) {
230
+ if (file.head !== undefined)
231
+ answer.changes.set(file.path, file.head);
232
+ if (file.created !== undefined)
233
+ answer.created.set(file.path, file.created);
234
+ if (file.binary !== undefined)
235
+ answer.attachments.set(file.path, file.binary);
236
+ else if (file.content !== undefined)
237
+ answer.documents.set(file.path, file.content);
238
+ if (file.binary === undefined && file.facts !== undefined)
239
+ answer.facts.set(file.path, file.facts);
240
+ if (file.revision !== undefined)
241
+ revised(answer, file.path, file.revision);
260
242
  }
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 = [];
267
- for (const file of scope.files) {
268
- if (file.revision !== undefined)
269
- revisions[file.path] = file.revision;
270
- if (file.head !== undefined)
271
- changes[file.path] = file.head;
272
- const content = contents.get(file.path);
273
- if (content === undefined)
274
- binaryTargets.push(file.path);
275
- else
276
- documents[file.path] = content;
277
- }
243
+ export function modelScope(room) {
278
244
  return {
279
- kind: 'bootstrap',
280
- roomVersion: roomVersion(scope),
281
- documents,
282
- revisions,
283
- changes,
284
- binaryTargets: binaryTargets.sort(),
285
- ...declared(scope.attachments),
245
+ anchor: room.anchor,
246
+ position: room.position,
247
+ logEpoch: room.logEpoch,
248
+ files: room.models,
286
249
  };
287
250
  }
288
- export function schemaOf(scope, schema) {
289
- const file = scope.files.find((entry) => entry.path === schema);
290
- if (file === undefined)
291
- return { schema: null, version: null };
292
- const content = scope.contents.find((entry) => entry.path === schema);
293
- return content === undefined ? null : { schema: content.content, version: file.version };
251
+ export function* bootstrapping(scope) {
252
+ const answer = answering();
253
+ for (const file of scope.files) {
254
+ yield ROW_WEIGHT;
255
+ if (file.space === undefined)
256
+ kept(answer, file);
257
+ }
258
+ for (const folder of scope.folders ?? [])
259
+ folded(answer, folder, false);
260
+ const { removals: _removals, folderRemovals: _folderRemovals, ...page } = yield* answered(answer);
261
+ return { kind: 'bootstrap', roomVersion: roomVersion(scope), ...page };
294
262
  }
295
- export function viewOf(enumeration, tracked) {
296
- return enumeration.authorities.find((entry) => entry.authority === tracked.authority);
263
+ export function schemaFile(file) {
264
+ return file.content === undefined
265
+ ? null
266
+ : {
267
+ schema: file.content,
268
+ version: file.version,
269
+ revision: file.revision ?? null,
270
+ ...(file.space === undefined ? {} : { space: file.space }),
271
+ };
297
272
  }
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
- };
273
+ export function schemaOf(scope, schema) {
274
+ const file = scope.files.find((entry) => entry.path === schema);
275
+ return file === undefined ? { schema: null, version: null, revision: null } : schemaFile(file);
332
276
  }
package/dist/refusal.d.ts CHANGED
@@ -1,12 +1,10 @@
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;
1
+ import type { AdapterFailure, DiscoverResult, ProviderRefusal, RevokedAuthorization, ScopedRefusal, TransportUncertainty } from '@agentshouse/mdruntime';
2
+ export declare function doorRefusal(body: unknown): ProviderRefusal | null;
3
+ export declare function answeredFailure(status: number, body: unknown): TransportUncertainty;
4
+ export declare function resetOf(refusal: ProviderRefusal | null): string | undefined;
5
+ export declare function scopedRefusal(locator: string, scope: ScopedRefusal['scope'], refusal: ProviderRefusal): ScopedRefusal;
7
6
  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;
12
- //# sourceMappingURL=refusal.d.ts.map
7
+ export declare function keyedFailure(status: number, body: unknown): TransportUncertainty | RevokedAuthorization | (ProviderRefusal & {
8
+ kind: 'rejected';
9
+ }) | null;
10
+ export declare function discoveryFailure(status: number, body: unknown): DiscoverResult | null;
@@ -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,oBAAoB,EACpB,aAAa,EACb,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAGhC,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,IAAI,CAOjE;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,oBAAoB,CAInF;AAED,wBAAgB,OAAO,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,GAAG,MAAM,GAAG,SAAS,CAG3E;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAC7B,OAAO,EAAE,eAAe,GACvB,aAAa,CAEf;AAoBD,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,cAAc,GAAG,IAAI,CAMvB;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,GACZ,oBAAoB,GAAG,oBAAoB,GAAG,CAAC,eAAe,GAAG;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,GAAG,IAAI,CAO/F;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,cAAc,GAAG,IAAI,CAQrF"}
package/dist/refusal.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { refusedFor, UNANSWERED, unusable } from './uncertain.js';
1
2
  export function doorRefusal(body) {
2
3
  if (typeof body !== 'object' || body === null)
3
4
  return null;
@@ -6,41 +7,67 @@ export function doorRefusal(body) {
6
7
  const error = body.error;
7
8
  if (typeof error.code !== 'string')
8
9
  return null;
9
- const { code, ...fields } = error;
10
- return { code, fields, retryable: error.retryable === true };
10
+ const { code, retryable, ...fields } = error;
11
+ return { code, fields, retryable: retryable === true };
12
+ }
13
+ export function answeredFailure(status, body) {
14
+ const refusal = doorRefusal(body);
15
+ if (refusal !== null)
16
+ return refusedFor(refusal);
17
+ return status === 0 || (status >= 500 && body === null) ? UNANSWERED : unusable();
18
+ }
19
+ export function resetOf(refusal) {
20
+ const reset = refusal?.fields.retry_at;
21
+ return refusal?.retryable === true && typeof reset === 'string' ? reset : undefined;
11
22
  }
12
23
  export function scopedRefusal(locator, scope, refusal) {
13
- return { kind: 'scoped-refusal', locator, scope, code: refusal.code, fields: refusal.fields };
24
+ return { kind: 'scoped-refusal', locator, scope, ...refusal };
14
25
  }
15
- export function classifyDoor(status, body, locator) {
26
+ function doorFailure(status, body) {
16
27
  if (status === 0)
17
- return { kind: 'transport-uncertainty' };
18
- const refusal = doorRefusal(body);
28
+ return UNANSWERED;
19
29
  if (status === 401)
20
30
  return { kind: 'revoked-authorization' };
21
- if (refusal?.retryable === true)
22
- return { kind: 'transport-uncertainty' };
23
- if (status === 404 || refusal?.code === 'room_not_found') {
24
- return { kind: 'room-not-readable', locator };
25
- }
31
+ const refusal = doorRefusal(body);
32
+ if (refusal?.retryable === true || refusal?.code === 'update_required')
33
+ return refusedFor(refusal);
34
+ if (status === 404 || refusal?.code === 'room_not_found')
35
+ return { kind: 'room-not-readable' };
26
36
  if (status < 400)
27
37
  return null;
28
38
  if (refusal === null)
29
- return { kind: 'transport-uncertainty' };
30
- if (status === 403)
31
- return scopedRefusal(locator, 'operation', refusal);
32
- return scopedRefusal(locator, 'undisclosed', refusal);
33
- }
34
- export function classifyRead(status, body, locator) {
35
- if (doorRefusal(body)?.code === 'expired')
36
- return { kind: 'beyond-horizon' };
37
- return classifyDoor(status, body, locator);
38
- }
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 };
39
+ return answeredFailure(status, body);
40
+ return { kind: 'refused', scope: status === 403 ? 'operation' : 'undisclosed', refusal };
41
+ }
42
+ export function classifyDoor(status, body, locator) {
43
+ const failure = doorFailure(status, body);
44
+ if (failure === null)
45
+ return null;
46
+ if (failure.kind === 'room-not-readable')
47
+ return { kind: 'room-not-readable', locator };
48
+ if (failure.kind === 'refused')
49
+ return scopedRefusal(locator, failure.scope, failure.refusal);
50
+ return failure;
51
+ }
52
+ export function keyedFailure(status, body) {
53
+ const failure = doorFailure(status, body);
54
+ if (failure === null)
55
+ return null;
56
+ if (failure.kind === 'refused')
57
+ return { kind: 'rejected', ...failure.refusal };
58
+ if (failure.kind !== 'room-not-readable')
59
+ return failure;
60
+ const refusal = doorRefusal(body);
61
+ return refusal === null ? answeredFailure(status, body) : { kind: 'rejected', ...refusal };
62
+ }
63
+ export function discoveryFailure(status, body) {
64
+ const failure = doorFailure(status, body);
65
+ if (failure === null)
66
+ return null;
67
+ if (failure.kind === 'revoked-authorization')
68
+ return failure;
69
+ if (failure.kind === 'refused') {
70
+ return { kind: 'contract-unavailable', ...failure.refusal };
44
71
  }
45
- return { kind: 'transport-uncertainty' };
72
+ return failure.kind === 'transport-uncertainty' ? failure : answeredFailure(status, body);
46
73
  }