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

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 (85) hide show
  1. package/README.md +32 -0
  2. package/dist/actions.d.ts +8 -16
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +43 -436
  5. package/dist/adapter.d.ts +1 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +352 -231
  8. package/dist/authored-path.d.ts +9 -0
  9. package/dist/authored-path.d.ts.map +1 -0
  10. package/dist/authored-path.js +34 -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/digest.js +1 -5
  16. package/dist/discovery.d.ts +49 -42
  17. package/dist/discovery.d.ts.map +1 -1
  18. package/dist/discovery.js +317 -232
  19. package/dist/history.d.ts +6 -0
  20. package/dist/history.d.ts.map +1 -0
  21. package/dist/history.js +85 -0
  22. package/dist/index.d.ts +10 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +9 -3
  25. package/dist/map.d.ts +2 -3
  26. package/dist/map.d.ts.map +1 -1
  27. package/dist/map.js +6 -7
  28. package/dist/model.d.ts +8 -19
  29. package/dist/model.d.ts.map +1 -1
  30. package/dist/model.js +43 -57
  31. package/dist/read.d.ts +65 -54
  32. package/dist/read.d.ts.map +1 -1
  33. package/dist/read.js +217 -295
  34. package/dist/refusal.d.ts +7 -11
  35. package/dist/refusal.d.ts.map +1 -1
  36. package/dist/refusal.js +53 -69
  37. package/dist/search.d.ts +4 -0
  38. package/dist/search.d.ts.map +1 -0
  39. package/dist/search.js +47 -0
  40. package/dist/session-content/bounds.d.ts +7 -0
  41. package/dist/session-content/check.d.ts +18 -0
  42. package/dist/session-content/index.d.ts +7 -0
  43. package/dist/session-content/parts.d.ts +103 -0
  44. package/dist/session-content/state.d.ts +61 -0
  45. package/dist/session-content/turn.d.ts +328 -0
  46. package/dist/site.d.ts +1 -2
  47. package/dist/site.d.ts.map +1 -1
  48. package/dist/site.js +142 -172
  49. package/dist/space-schema.d.ts +6 -0
  50. package/dist/space-schema.d.ts.map +1 -0
  51. package/dist/space-schema.js +11 -0
  52. package/dist/stream.d.ts +20 -9
  53. package/dist/stream.d.ts.map +1 -1
  54. package/dist/stream.js +394 -236
  55. package/dist/transport.d.ts +172 -47
  56. package/dist/transport.d.ts.map +1 -1
  57. package/dist/transport.js +58 -12
  58. package/dist/uncertain.d.ts +5 -0
  59. package/dist/uncertain.d.ts.map +1 -0
  60. package/dist/uncertain.js +11 -0
  61. package/dist/upload.d.ts +13 -0
  62. package/dist/upload.d.ts.map +1 -0
  63. package/dist/upload.js +65 -0
  64. package/dist/version.d.ts +3 -1
  65. package/dist/version.d.ts.map +1 -1
  66. package/dist/version.js +32 -18
  67. package/dist/write.d.ts +24 -11
  68. package/dist/write.d.ts.map +1 -1
  69. package/dist/write.js +263 -241
  70. package/package.json +10 -14
  71. package/AGENTS.md +0 -79
  72. package/dist/actions.js.map +0 -1
  73. package/dist/adapter.js.map +0 -1
  74. package/dist/digest.js.map +0 -1
  75. package/dist/discovery.js.map +0 -1
  76. package/dist/index.js.map +0 -1
  77. package/dist/map.js.map +0 -1
  78. package/dist/model.js.map +0 -1
  79. package/dist/read.js.map +0 -1
  80. package/dist/refusal.js.map +0 -1
  81. package/dist/site.js.map +0 -1
  82. package/dist/stream.js.map +0 -1
  83. package/dist/transport.js.map +0 -1
  84. package/dist/version.js.map +0 -1
  85. package/dist/write.js.map +0 -1
package/dist/write.js CHANGED
@@ -1,389 +1,411 @@
1
+ import { departure, partsChanges, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
+ import { answered, answering, binaryFileOf, headChange, revised } from './read.js';
1
3
  import { sha256Hex, textBytes } from './digest.js';
2
4
  import { nativePath, roomLocalPath } from './map.js';
3
- import { classifyDoor, doorRefusal, scopedRefusal } from './refusal.js';
5
+ import { answeredFailure, classifyDoor, doorRefusal, resetOf, scopedRefusal } from './refusal.js';
4
6
  import { readPosition, roomVersion } from './version.js';
5
- export const INLINE_BODY_LIMIT_BYTES = 4 * 1024 * 1024;
6
- export const UPLOAD_LINE_BYTES = 37;
7
- export const UPLOAD_BYTES_MAX = 32 * 1024 * 1024 - UPLOAD_LINE_BYTES;
7
+ import { unusable } from './uncertain.js';
8
8
  function priorField(batch, path, name) {
9
- return batch.preconditions.find((precondition)=>precondition.kind === 'field' && precondition.path === path && precondition.name === name);
9
+ return batch.preconditions.find((precondition) => precondition.kind === 'field' && precondition.path === path && precondition.name === name);
10
10
  }
11
11
  function priorSection(batch, path, name) {
12
- return batch.preconditions.find((precondition)=>precondition.kind === 'section' && precondition.path === path && precondition.name === name);
12
+ return batch.preconditions.find((precondition) => precondition.kind === 'section' && precondition.path === path && precondition.name === name);
13
13
  }
14
14
  function priorPreamble(batch, path) {
15
- return batch.preconditions.find((precondition)=>precondition.kind === 'preamble' && precondition.path === path);
15
+ return batch.preconditions.find((precondition) => precondition.kind === 'preamble' && precondition.path === path);
16
16
  }
17
17
  function priorAbsence(batch, path) {
18
- return batch.preconditions.find((precondition)=>precondition.kind === 'absent' && precondition.path === path);
18
+ return batch.preconditions.find((precondition) => precondition.kind === 'absent' && precondition.path === path);
19
19
  }
20
20
  function priorExistence(batch, path) {
21
- const precondition = batch.preconditions.find((held)=>held.path === path && (held.kind === 'revision' || held.kind === 'present'));
22
- if (precondition === undefined) return undefined;
23
- return precondition.kind === 'revision' ? {
24
- base: precondition.revision
25
- } : {
26
- kind: 'presence'
27
- };
21
+ const precondition = batch.preconditions.find((held) => held.path === path && (held.kind === 'revision' || held.kind === 'present'));
22
+ if (precondition === undefined)
23
+ return undefined;
24
+ return precondition.kind === 'revision' ? { base: precondition.revision } : { kind: 'presence' };
28
25
  }
29
- function typedCreate(change, path) {
26
+ function created(change, path) {
27
+ if ('content' in change)
28
+ return { op: 'create', path, content: change.content };
30
29
  const { type, ...fields } = change.fields;
31
- if (typeof type !== 'string' || type.length === 0) return null;
32
- return {
33
- op: 'create_typed',
34
- path,
35
- type,
36
- fields,
37
- sections: change.sections
38
- };
30
+ if (typeof type !== 'string' || type.length === 0)
31
+ return null;
32
+ return { op: 'create_typed', path, type, fields };
39
33
  }
40
34
  function operationOf(batch, change, handle) {
41
35
  const path = nativePath(change.path, handle);
42
36
  if (change.kind === 'field') {
43
37
  const prior = priorField(batch, change.path, change.name);
44
- if (prior === undefined) return {
45
- rule: 'precondition-missing'
46
- };
38
+ if (prior === undefined)
39
+ return { rule: 'precondition-missing' };
47
40
  return {
48
41
  operation: {
49
42
  op: 'set_field',
50
43
  path,
51
44
  field: change.name,
52
45
  value: change.value,
53
- ...prior.value === undefined ? {} : {
54
- base: prior.value
55
- }
56
- }
46
+ ...(prior.value === undefined ? {} : { base: prior.value }),
47
+ },
57
48
  };
58
49
  }
59
50
  if (change.kind === 'section') {
60
51
  const prior = priorSection(batch, change.path, change.name);
61
- if (prior === undefined) return {
62
- rule: 'precondition-missing'
63
- };
52
+ if (prior === undefined)
53
+ return { rule: 'precondition-missing' };
64
54
  return {
65
55
  operation: {
66
56
  op: 'replace_section',
67
57
  path,
68
58
  section: change.name,
69
59
  base: prior.value,
70
- content: change.value
71
- }
60
+ content: change.value,
61
+ },
72
62
  };
73
63
  }
74
64
  if (change.kind === 'preamble') {
75
65
  const prior = priorPreamble(batch, change.path);
76
- if (prior === undefined) return {
77
- rule: 'precondition-missing'
78
- };
66
+ if (prior === undefined)
67
+ return { rule: 'precondition-missing' };
79
68
  return {
80
- operation: {
81
- op: 'replace_preamble',
82
- path,
83
- base: prior.value,
84
- content: change.value
85
- }
69
+ operation: { op: 'replace_preamble', path, base: prior.value, content: change.value },
86
70
  };
87
71
  }
88
72
  if (change.kind === 'create') {
89
- if (priorAbsence(batch, change.path) === undefined) return {
90
- rule: 'precondition-missing'
91
- };
92
- const created = typedCreate(change, path);
93
- return created === null ? {
94
- rule: 'type-missing'
95
- } : {
96
- operation: created
73
+ if (priorAbsence(batch, change.path) === undefined)
74
+ return { rule: 'precondition-missing' };
75
+ const operation = created(change, path);
76
+ return operation === null ? { rule: 'type-missing' } : { operation };
77
+ }
78
+ if (change.kind === 'binary') {
79
+ const written = {
80
+ op: 'upload',
81
+ path,
82
+ local_id: change.localId,
83
+ bytes: change.byteLength,
84
+ sha256: change.sha256,
85
+ media_type: change.mediaType,
97
86
  };
87
+ const from = departure(batch.changes, batch.changes.indexOf(change));
88
+ if (from === change.path && priorAbsence(batch, change.path) !== undefined)
89
+ return { operation: written };
90
+ const replaced = priorExistence(batch, from);
91
+ if (replaced === undefined)
92
+ return { rule: 'precondition-missing' };
93
+ if (!('base' in replaced))
94
+ return { rule: 'presence-base-unrepresentable' };
95
+ return { operation: { ...written, base: replaced.base } };
98
96
  }
99
97
  const existence = priorExistence(batch, change.path);
100
- if (existence === undefined) return {
101
- rule: 'precondition-missing'
102
- };
103
- if (!('base' in existence)) return {
104
- rule: 'presence-base-unrepresentable'
105
- };
98
+ if (existence === undefined)
99
+ return { rule: 'precondition-missing' };
100
+ if (!('base' in existence))
101
+ return { rule: 'presence-base-unrepresentable' };
106
102
  if (change.kind === 'replace') {
107
- return {
108
- operation: {
109
- op: 'replace',
110
- path,
111
- base: existence.base,
112
- content: change.content
113
- }
114
- };
103
+ return { operation: { op: 'replace', path, base: existence.base, content: change.content } };
115
104
  }
116
105
  if (change.kind === 'move') {
117
- if (priorAbsence(batch, change.to) === undefined) return {
118
- rule: 'precondition-missing'
119
- };
106
+ if (priorAbsence(batch, change.to) === undefined)
107
+ return { rule: 'precondition-missing' };
120
108
  return {
121
- operation: {
122
- op: 'rename',
123
- path,
124
- to: nativePath(change.to, handle),
125
- base: existence.base
126
- }
109
+ operation: { op: 'rename', path, to: nativePath(change.to, handle), base: existence.base },
127
110
  };
128
111
  }
129
- return {
130
- operation: {
131
- op: 'remove',
132
- path,
133
- base: existence.base
134
- }
135
- };
112
+ return { operation: { op: 'remove', path, base: existence.base } };
136
113
  }
137
114
  export function houseChanges(batch, handle) {
138
115
  const changes = [];
139
- for (const change of batch.changes){
116
+ for (const change of batch.changes) {
140
117
  const mapped = operationOf(batch, change, handle);
141
- if ('rule' in mapped) return {
142
- kind: 'unmapped',
143
- path: change.path,
144
- rule: mapped.rule
145
- };
118
+ if ('rule' in mapped)
119
+ return { kind: 'unmapped', path: change.path, rule: mapped.rule };
146
120
  changes.push(mapped.operation);
147
121
  }
148
- return {
149
- kind: 'changes',
150
- changes
151
- };
122
+ return { kind: 'changes', changes };
123
+ }
124
+ export function houseParts(saved, handle) {
125
+ return houseChanges({ id: '', baseRoomVersion: '', ...partsChanges(saved) }, handle);
126
+ }
127
+ export function houseMove(batch, source, destination) {
128
+ const changes = [];
129
+ for (const { path, to } of batch.between?.moves ?? []) {
130
+ const existence = priorExistence(batch, path);
131
+ if (existence === undefined)
132
+ return { kind: 'unmapped', path, rule: 'precondition-missing' };
133
+ if (!('base' in existence)) {
134
+ return { kind: 'unmapped', path, rule: 'presence-base-unrepresentable' };
135
+ }
136
+ changes.push({
137
+ op: 'rename',
138
+ path: nativePath(path, source),
139
+ to: nativePath(to, destination),
140
+ base: existence.base,
141
+ });
142
+ }
143
+ return { kind: 'changes', changes };
152
144
  }
153
- export function operationHead(batch) {
145
+ export function operationHead(batch, room) {
154
146
  const writer = batch.writtenBy;
155
- return writer === undefined ? {
156
- operation_id: batch.id
157
- } : {
158
- operation_id: batch.id,
159
- app_ref: writer.app,
160
- release: writer.release
161
- };
147
+ const head = { operation_id: batch.id, room_ref: room };
148
+ return writer === undefined ? head : { ...head, app_ref: writer.app, release: writer.release };
162
149
  }
163
150
  export function batchPaths(changes) {
164
151
  return [
165
- ...new Set(changes.flatMap((change)=>change.op === 'rename' ? [
166
- String(change.path),
167
- String(change.to)
168
- ] : [
169
- String(change.path)
170
- ]))
171
- ].sort();
152
+ ...new Set(changes.flatMap((change) => change.op === 'rename' ? [String(change.path), String(change.to)] : [String(change.path)])),
153
+ ];
172
154
  }
173
155
  export function deltaBody(changes) {
174
- return JSON.stringify({
175
- changes
176
- });
156
+ return JSON.stringify({ changes });
177
157
  }
178
158
  export function deltaBytes(delta) {
179
159
  return textBytes(delta);
180
160
  }
181
- export function uploadable(bytes) {
182
- return bytes > INLINE_BODY_LIMIT_BYTES && bytes <= UPLOAD_BYTES_MAX;
161
+ export function uploadable(inline, bytes, bounds) {
162
+ return (textBytes(JSON.stringify(inline)) > bounds.max_inline_body_bytes &&
163
+ bytes <= bounds.max_upload_bytes);
183
164
  }
184
165
  export function deltaDigest(delta) {
185
166
  return sha256Hex(delta);
186
167
  }
187
- function material(value, handle) {
188
- if (!Array.isArray(value)) return null;
168
+ function* material(value, handle, within = () => true) {
169
+ if (!Array.isArray(value))
170
+ return null;
189
171
  const held = [];
190
- for (const entry of value){
191
- if (typeof entry !== 'object' || entry === null) return null;
172
+ for (const entry of value) {
173
+ yield ROW_WEIGHT;
174
+ if (typeof entry !== 'object' || entry === null)
175
+ return null;
192
176
  const file = entry;
193
- if (typeof file.path !== 'string') return null;
194
- if (file.content !== null && typeof file.content !== 'string') return null;
195
- if (file.revision !== null && file.revision !== undefined && typeof file.revision !== 'string') return null;
177
+ if (typeof file.path !== 'string')
178
+ return null;
179
+ if (!within(file.path))
180
+ continue;
181
+ if (file.content !== null && typeof file.content !== 'string')
182
+ return null;
183
+ if (file.revision !== null && file.revision !== undefined && typeof file.revision !== 'string')
184
+ return null;
185
+ const binary = binaryFileOf(file);
186
+ if (binary === null)
187
+ return null;
188
+ const head = headChange(file);
189
+ if (head === null)
190
+ return null;
196
191
  let path;
197
192
  try {
198
193
  path = roomLocalPath(file.path, handle);
199
- } catch {
194
+ }
195
+ catch {
200
196
  return null;
201
197
  }
202
198
  held.push({
203
199
  path,
204
200
  content: file.content,
205
- revision: typeof file.revision === 'string' ? file.revision : null
201
+ revision: typeof file.revision === 'string' ? file.revision : null,
202
+ ...(binary === undefined ? {} : { binary }),
203
+ ...(head === undefined ? {} : { head }),
206
204
  });
207
205
  }
208
206
  return held;
209
207
  }
210
- function affectedOf(held) {
208
+ function* affectedOf(held) {
211
209
  const affected = {};
212
- for (const entry of held){
213
- if (entry.content !== null) affected[entry.path] = entry.content;
210
+ for (const entry of held) {
211
+ if (entry.content !== null)
212
+ affected[entry.path] = entry.content;
213
+ yield ROW_WEIGHT;
214
214
  }
215
215
  return affected;
216
216
  }
217
- export function mutationOutcome(status, body, locator, handle) {
218
- const refusal = doorRefusal(body);
219
- if (refusal !== null && refusal.code === 'rate_limited') {
220
- const reset = refusal.fields.retry_at;
221
- return {
222
- kind: 'retryable-refusal',
223
- code: refusal.code,
224
- fields: refusal.fields,
225
- ...typeof reset === 'string' ? {
226
- reset
227
- } : {}
228
- };
217
+ function* acceptedOf(at, contents) {
218
+ const answer = answering();
219
+ for (const entry of contents) {
220
+ yield ROW_WEIGHT + (entry.content?.length ?? 0);
221
+ if (entry.revision === null) {
222
+ answer.removals.add(entry.path);
223
+ continue;
224
+ }
225
+ if (entry.content !== null)
226
+ answer.documents.set(entry.path, entry.content);
227
+ if (entry.binary !== undefined)
228
+ answer.binaryFiles.set(entry.path, entry.binary);
229
+ revised(answer, entry.path, entry.revision);
230
+ if (entry.head !== undefined)
231
+ answer.changes.set(entry.path, entry.head);
229
232
  }
230
- if (refusal !== null && refusal.code === 'operation_expired') return {
231
- kind: 'expired'
233
+ const unbinaried = answer.binaryFiles.size === 0;
234
+ const { binaryFiles, facts: _facts, ...room } = yield* answered(answer);
235
+ return { roomVersion: roomVersion(at), ...room, ...(unbinaried ? {} : { binaryFiles }) };
236
+ }
237
+ function* movedOutcome(held, move) {
238
+ const authorities = Array.isArray(held.authorities) ? held.authorities : [];
239
+ const at = (authority) => {
240
+ const entry = authorities.find((one) => typeof one === 'object' &&
241
+ one !== null &&
242
+ one.authority === authority);
243
+ return entry === undefined ? null : readPosition(entry);
244
+ };
245
+ const left = at(move.source.authority);
246
+ const arrived = at(move.destination.authority);
247
+ if (left === null || arrived === null)
248
+ return unusable();
249
+ const under = (handle) => (path) => path.startsWith(`/rooms/${handle}/`);
250
+ const leaving = yield* material(held.contents, move.source.handle, under(move.source.handle));
251
+ const arriving = yield* material(held.contents, move.destination.handle, under(move.destination.handle));
252
+ if (leaving === null || arriving === null)
253
+ return unusable();
254
+ return {
255
+ kind: 'accepted',
256
+ ...(yield* acceptedOf(left, leaving)),
257
+ destination: yield* acceptedOf(arrived, arriving),
232
258
  };
259
+ }
260
+ export function* mutationOutcome(status, body, locator, handle, move) {
261
+ const refusal = doorRefusal(body);
262
+ if (refusal !== null && refusal.code === 'service_mode_write_halted') {
263
+ return { kind: 'write-halted' };
264
+ }
233
265
  if (refusal !== null && refusal.code === 'edit_conflict') {
234
266
  const { current, ...fields } = refusal.fields;
235
- const held = material(current, handle) ?? [];
267
+ const held = (yield* material(current, handle)) ?? [];
236
268
  return {
237
269
  kind: 'conflicted',
238
- affected: affectedOf(held),
270
+ affected: yield* affectedOf(held),
239
271
  code: refusal.code,
240
- fields
272
+ fields,
273
+ retryable: refusal.retryable,
241
274
  };
242
275
  }
243
276
  if (refusal !== null && status === 404) {
244
277
  return scopedRefusal(locator, 'undisclosed', refusal);
245
278
  }
246
279
  const classified = classifyDoor(status, body, locator);
247
- if (classified !== null && classified.kind !== 'scoped-refusal') {
248
- return classified.kind === 'beyond-horizon' ? {
249
- kind: 'expired'
250
- } : classified;
280
+ const reset = resetOf(refusal);
281
+ if (refusal !== null && reset !== undefined && classified?.kind === 'transport-uncertainty') {
282
+ return {
283
+ kind: 'retryable-refusal',
284
+ code: refusal.code,
285
+ fields: refusal.fields,
286
+ retryable: true,
287
+ reset,
288
+ };
289
+ }
290
+ if (classified !== null && classified.kind !== 'scoped-refusal')
291
+ return classified;
292
+ if (classified !== null && status === 403 && refusal?.code === 'operation_denied') {
293
+ return classified;
251
294
  }
252
- if (classified !== null && status === 403) return classified;
253
295
  if (refusal !== null && status >= 400) {
254
296
  return {
255
297
  kind: 'rejected',
256
298
  affected: {},
257
299
  code: refusal.code,
258
- fields: refusal.fields
259
- };
260
- }
261
- if (status !== 200 || typeof body !== 'object' || body === null) {
262
- return {
263
- kind: 'transport-uncertainty'
300
+ fields: refusal.fields,
301
+ retryable: refusal.retryable,
264
302
  };
265
303
  }
304
+ if (status !== 200)
305
+ return answeredFailure(status, body);
306
+ if (typeof body !== 'object' || body === null)
307
+ return unusable();
266
308
  const held = body;
309
+ if (move !== undefined)
310
+ return yield* movedOutcome(held, move);
267
311
  const at = readPosition(held);
268
- if (at === null) return {
269
- kind: 'transport-uncertainty'
270
- };
271
- const contents = material(held.contents, handle);
272
- if (contents === null) return {
273
- kind: 'transport-uncertainty'
274
- };
275
- const documents = {};
276
- const revisions = {};
277
- const removals = [];
278
- for (const entry of contents){
279
- if (entry.content === null || entry.revision === null) {
280
- removals.push(entry.path);
281
- continue;
282
- }
283
- documents[entry.path] = entry.content;
284
- revisions[entry.path] = entry.revision;
285
- }
286
- return {
287
- kind: 'accepted',
288
- roomVersion: roomVersion(at),
289
- documents,
290
- revisions,
291
- removals: removals.sort()
292
- };
312
+ if (at === null)
313
+ return unusable();
314
+ const contents = yield* material(held.contents, handle);
315
+ if (contents === null)
316
+ return unusable();
317
+ return { kind: 'accepted', ...(yield* acceptedOf(at, contents)) };
293
318
  }
294
319
  export function rejectedMapping(mapping) {
295
320
  return {
296
321
  kind: 'rejected',
297
322
  affected: {},
298
323
  code: mapping.rule,
299
- fields: {
300
- path: mapping.path
301
- }
324
+ fields: { path: mapping.path },
325
+ retryable: false,
302
326
  };
303
327
  }
304
328
  export function revisionBody(input) {
305
329
  return {
306
330
  path: nativePath(input.path, input.handle),
307
- ...input.revision === undefined ? {} : {
308
- revision: input.revision
309
- },
310
- ...input.pageToken === undefined ? {} : {
311
- before: Number(input.pageToken)
312
- }
331
+ ...(input.revision === undefined ? {} : { revision: input.revision }),
332
+ ...(input.pageToken === undefined ? {} : { before: Number(input.pageToken) }),
313
333
  };
314
334
  }
335
+ function revisionAuthor(entry) {
336
+ const user = entry.author_user;
337
+ const userName = entry.author_user_name;
338
+ const site = entry.author_site;
339
+ const siteName = entry.author_site_name;
340
+ const inbox = entry.author_inbox;
341
+ const inboxName = entry.author_inbox_name;
342
+ const app = entry.author_app;
343
+ const appName = entry.author_app_name;
344
+ const named = [site, siteName, inbox, inboxName, app, appName].filter((held) => held !== undefined);
345
+ if (typeof user === 'string' && typeof userName === 'string' && named.length === 0)
346
+ return { kind: 'user', id: user, name: userName };
347
+ if (user !== null || userName !== undefined || named.length !== 2)
348
+ return null;
349
+ if (typeof site === 'string' && typeof siteName === 'string') {
350
+ return { kind: 'site', id: site, name: siteName };
351
+ }
352
+ if (typeof inbox === 'string' && typeof inboxName === 'string') {
353
+ return { kind: 'inbox', id: inbox, name: inboxName };
354
+ }
355
+ if (typeof app === 'string' && typeof appName === 'string') {
356
+ return { kind: 'app', id: app, name: appName };
357
+ }
358
+ return null;
359
+ }
315
360
  export function historyOutcome(status, body, locator, request) {
316
361
  const classified = classifyDoor(status, body, locator);
317
- if (classified !== null) {
318
- return classified.kind === 'beyond-horizon' ? {
319
- kind: 'transport-uncertainty'
320
- } : classified;
321
- }
362
+ if (classified !== null)
363
+ return classified;
322
364
  if (status !== 200 || typeof body !== 'object' || body === null) {
323
- return {
324
- kind: 'transport-uncertainty'
325
- };
365
+ return unusable();
326
366
  }
327
367
  const held = body;
328
- if (!Array.isArray(held.revisions)) return {
329
- kind: 'transport-uncertainty'
330
- };
368
+ if (!Array.isArray(held.revisions))
369
+ return unusable();
331
370
  const entries = [];
332
- for (const entry of held.revisions){
333
- if (typeof entry !== 'object' || entry === null) return {
334
- kind: 'transport-uncertainty'
335
- };
371
+ for (const entry of held.revisions) {
372
+ if (typeof entry !== 'object' || entry === null)
373
+ return unusable();
336
374
  entries.push(entry);
337
375
  }
338
- if (request.revision !== undefined) {
339
- const named = entries[0];
340
- if (named === undefined) return {
341
- kind: 'no-history'
342
- };
343
- if (named.unavailable === true) {
344
- return {
345
- kind: 'scoped-refusal',
346
- locator,
347
- scope: 'content',
348
- code: 'retained_history_unavailable',
349
- fields: {
350
- path: request.path,
351
- revision: request.revision
352
- }
353
- };
354
- }
355
- if (typeof named.content !== 'string') return {
356
- kind: 'transport-uncertainty'
357
- };
358
- return {
359
- kind: 'revision',
360
- content: named.content
361
- };
376
+ const named = entries[0];
377
+ if (request.revision !== undefined && typeof named?.media_type !== 'string') {
378
+ if (named === undefined)
379
+ return { kind: 'no-history' };
380
+ if (typeof named.content !== 'string')
381
+ return unusable();
382
+ return { kind: 'revision', content: named.content };
362
383
  }
363
- if (entries.length === 0) return {
364
- kind: 'no-history'
365
- };
384
+ if (entries.length === 0)
385
+ return { kind: 'no-history' };
366
386
  const revisions = [];
367
- for (const entry of entries){
368
- if (typeof entry.revision !== 'string' || typeof entry.author_user !== 'string' || typeof entry.created_at !== 'string') {
369
- return {
370
- kind: 'transport-uncertainty'
371
- };
387
+ for (const entry of entries) {
388
+ const author = revisionAuthor(entry);
389
+ if (typeof entry.revision !== 'string' ||
390
+ author === null ||
391
+ typeof entry.created_at !== 'string') {
392
+ return unusable();
372
393
  }
394
+ const binaryFile = binaryFileOf(entry);
395
+ if (binaryFile === null)
396
+ return unusable();
373
397
  revisions.push({
374
398
  revision: entry.revision,
375
- author: entry.author_user,
376
- time: entry.created_at
399
+ author,
400
+ time: entry.created_at,
401
+ ...(binaryFile === undefined ? {} : { binaryFile }),
377
402
  });
378
403
  }
379
404
  const next = held.next_before;
380
405
  return {
381
406
  kind: 'history',
382
407
  revisions,
383
- ...typeof next === 'number' ? {
384
- pageToken: String(next)
385
- } : {}
408
+ ...(typeof next === 'number' ? { pageToken: String(next) } : {}),
409
+ ...(held.deleted === true ? { deleted: true } : {}),
386
410
  };
387
411
  }
388
-
389
- //# sourceMappingURL=write.js.map