@agentshouse/mdruntime-adapter 0.1.0-ce7ce34e4ced0 → 0.1.0-ce861e3fb788a

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.
package/dist/write.js CHANGED
@@ -1,18 +1,14 @@
1
- import { departure, partsChanges, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
- import { answered, answering, binaryFileOf, headChange, revised } from './read.js';
1
+ import { departure, isPart, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
+ import { answered, answering, attachmentOf, headChange, revised } from './read.js';
3
3
  import { sha256Hex, textBytes } from './digest.js';
4
4
  import { nativePath, roomLocalPath } from './map.js';
5
5
  import { answeredFailure, classifyDoor, doorRefusal, resetOf, scopedRefusal } from './refusal.js';
6
6
  import { readPosition, roomVersion } from './version.js';
7
7
  import { unusable } from './uncertain.js';
8
- function priorField(batch, path, name) {
9
- return batch.preconditions.find((precondition) => precondition.kind === 'field' && precondition.path === path && precondition.name === name);
10
- }
11
- function priorSection(batch, path, name) {
12
- return batch.preconditions.find((precondition) => precondition.kind === 'section' && precondition.path === path && precondition.name === name);
13
- }
14
- function priorPreamble(batch, path) {
15
- return batch.preconditions.find((precondition) => precondition.kind === 'preamble' && precondition.path === path);
8
+ function priorPart(batch, change) {
9
+ return batch.preconditions.find((held) => held.kind === change.kind &&
10
+ held.path === change.path &&
11
+ (change.kind === 'preamble' || ('name' in held && held.name === change.name)));
16
12
  }
17
13
  function priorAbsence(batch, path) {
18
14
  return batch.preconditions.find((precondition) => precondition.kind === 'absent' && precondition.path === path);
@@ -23,60 +19,30 @@ function priorExistence(batch, path) {
23
19
  return undefined;
24
20
  return precondition.kind === 'revision' ? { base: precondition.revision } : { kind: 'presence' };
25
21
  }
26
- function created(change, path) {
27
- if ('content' in change)
28
- return { op: 'create', path, content: change.content };
29
- const { type, ...fields } = change.fields;
30
- if (typeof type !== 'string' || type.length === 0)
22
+ function textOf(batch, change, path, document) {
23
+ if (isPart(change) && priorPart(batch, change) === undefined)
24
+ return { rule: 'precondition-missing' };
25
+ if (change.kind === 'create' && priorAbsence(batch, change.path) === undefined)
26
+ return { rule: 'precondition-missing' };
27
+ if (change.kind === 'replace' && priorExistence(batch, change.path) === undefined)
28
+ return { rule: 'precondition-missing' };
29
+ if (document === null || document === undefined)
31
30
  return null;
32
- return { op: 'create_typed', path, type, fields };
31
+ if (document.stale)
32
+ return { rule: 'precondition-stale' };
33
+ if (document.created)
34
+ return { operation: { op: 'write', path, content: document.content } };
35
+ if (document.base === null)
36
+ return { rule: 'presence-base-unrepresentable' };
37
+ return { operation: { op: 'write', path, base: document.base, content: document.content } };
33
38
  }
34
- function operationOf(batch, change, handle) {
39
+ function operationOf(batch, index, handle, written) {
40
+ const change = batch.changes[index];
35
41
  const path = nativePath(change.path, handle);
36
- if (change.kind === 'field') {
37
- const prior = priorField(batch, change.path, change.name);
38
- if (prior === undefined)
39
- return { rule: 'precondition-missing' };
40
- return {
41
- operation: {
42
- op: 'set_field',
43
- path,
44
- field: change.name,
45
- value: change.value,
46
- ...(prior.value === undefined ? {} : { base: prior.value }),
47
- },
48
- };
49
- }
50
- if (change.kind === 'section') {
51
- const prior = priorSection(batch, change.path, change.name);
52
- if (prior === undefined)
53
- return { rule: 'precondition-missing' };
54
- return {
55
- operation: {
56
- op: 'replace_section',
57
- path,
58
- section: change.name,
59
- base: prior.value,
60
- content: change.value,
61
- },
62
- };
63
- }
64
- if (change.kind === 'preamble') {
65
- const prior = priorPreamble(batch, change.path);
66
- if (prior === undefined)
67
- return { rule: 'precondition-missing' };
68
- return {
69
- operation: { op: 'replace_preamble', path, base: prior.value, content: change.value },
70
- };
71
- }
72
- if (change.kind === 'create') {
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
- }
42
+ if (isPart(change) || change.kind === 'create' || change.kind === 'replace')
43
+ return textOf(batch, change, path, written[index]);
78
44
  if (change.kind === 'binary') {
79
- const written = {
45
+ const uploaded = {
80
46
  op: 'upload',
81
47
  path,
82
48
  local_id: change.localId,
@@ -84,45 +50,61 @@ function operationOf(batch, change, handle) {
84
50
  sha256: change.sha256,
85
51
  media_type: change.mediaType,
86
52
  };
87
- const from = departure(batch.changes, batch.changes.indexOf(change));
53
+ const from = departure(batch.changes, index);
88
54
  if (from === change.path && priorAbsence(batch, change.path) !== undefined)
89
- return { operation: written };
55
+ return { operation: uploaded };
90
56
  const replaced = priorExistence(batch, from);
91
57
  if (replaced === undefined)
92
58
  return { rule: 'precondition-missing' };
93
59
  if (!('base' in replaced))
94
60
  return { rule: 'presence-base-unrepresentable' };
95
- return { operation: { ...written, base: replaced.base } };
61
+ return { operation: { ...uploaded, base: replaced.base } };
96
62
  }
97
63
  const existence = priorExistence(batch, change.path);
98
64
  if (existence === undefined)
99
65
  return { rule: 'precondition-missing' };
100
66
  if (!('base' in existence))
101
67
  return { rule: 'presence-base-unrepresentable' };
102
- if (change.kind === 'replace') {
103
- return { operation: { op: 'replace', path, base: existence.base, content: change.content } };
104
- }
105
68
  if (change.kind === 'move') {
106
69
  if (priorAbsence(batch, change.to) === undefined)
107
70
  return { rule: 'precondition-missing' };
108
71
  return {
109
- operation: { op: 'rename', path, to: nativePath(change.to, handle), base: existence.base },
72
+ operation: { op: 'move', path, to: nativePath(change.to, handle), base: existence.base },
110
73
  };
111
74
  }
112
- return { operation: { op: 'remove', path, base: existence.base } };
75
+ return { operation: { op: 'delete', path, base: existence.base } };
113
76
  }
114
- export function houseChanges(batch, handle) {
77
+ export function houseChanges(batch, handle, written) {
115
78
  const changes = [];
116
- for (const change of batch.changes) {
117
- const mapped = operationOf(batch, change, handle);
79
+ const moves = new Map();
80
+ const before = new Map();
81
+ for (const [index, change] of batch.changes.entries()) {
82
+ const mapped = operationOf(batch, index, handle, written);
83
+ if (mapped === null)
84
+ continue;
118
85
  if ('rule' in mapped)
119
86
  return { kind: 'unmapped', path: change.path, rule: mapped.rule };
120
- changes.push(mapped.operation);
87
+ const { operation } = mapped;
88
+ const from = departure(batch.changes, index);
89
+ if (change.kind === 'move' && !moves.has(from))
90
+ moves.set(from, changes.length);
91
+ const moved = moves.get(from);
92
+ if (change.kind !== 'move' &&
93
+ moved !== undefined &&
94
+ operation.op === 'write' &&
95
+ 'base' in operation) {
96
+ before.set(moved, { ...operation, path: nativePath(from, handle) });
97
+ continue;
98
+ }
99
+ changes.push(operation);
121
100
  }
122
- return { kind: 'changes', changes };
123
- }
124
- export function houseParts(saved, handle) {
125
- return houseChanges({ id: '', baseRoomVersion: '', ...partsChanges(saved) }, handle);
101
+ return {
102
+ kind: 'changes',
103
+ changes: changes.flatMap((operation, at) => {
104
+ const written = before.get(at);
105
+ return written === undefined ? [operation] : [written, operation];
106
+ }),
107
+ };
126
108
  }
127
109
  export function houseMove(batch, source, destination) {
128
110
  const changes = [];
@@ -134,7 +116,7 @@ export function houseMove(batch, source, destination) {
134
116
  return { kind: 'unmapped', path, rule: 'presence-base-unrepresentable' };
135
117
  }
136
118
  changes.push({
137
- op: 'rename',
119
+ op: 'move',
138
120
  path: nativePath(path, source),
139
121
  to: nativePath(to, destination),
140
122
  base: existence.base,
@@ -142,14 +124,14 @@ export function houseMove(batch, source, destination) {
142
124
  }
143
125
  return { kind: 'changes', changes };
144
126
  }
145
- export function operationHead(batch, room) {
127
+ export function operationHead(batch) {
146
128
  const writer = batch.writtenBy;
147
- const head = { operation_id: batch.id, room_ref: room };
129
+ const head = { operation_id: batch.id };
148
130
  return writer === undefined ? head : { ...head, app_ref: writer.app, release: writer.release };
149
131
  }
150
132
  export function batchPaths(changes) {
151
133
  return [
152
- ...new Set(changes.flatMap((change) => change.op === 'rename' ? [String(change.path), String(change.to)] : [String(change.path)])),
134
+ ...new Set(changes.flatMap((change) => change.op === 'move' ? [String(change.path), String(change.to)] : [String(change.path)])),
153
135
  ];
154
136
  }
155
137
  export function deltaBody(changes) {
@@ -182,7 +164,7 @@ function* material(value, handle, within = () => true) {
182
164
  return null;
183
165
  if (file.revision !== null && file.revision !== undefined && typeof file.revision !== 'string')
184
166
  return null;
185
- const binary = binaryFileOf(file);
167
+ const binary = attachmentOf(file);
186
168
  if (binary === null)
187
169
  return null;
188
170
  const head = headChange(file);
@@ -225,14 +207,14 @@ function* acceptedOf(at, contents) {
225
207
  if (entry.content !== null)
226
208
  answer.documents.set(entry.path, entry.content);
227
209
  if (entry.binary !== undefined)
228
- answer.binaryFiles.set(entry.path, entry.binary);
210
+ answer.attachments.set(entry.path, entry.binary);
229
211
  revised(answer, entry.path, entry.revision);
230
212
  if (entry.head !== undefined)
231
213
  answer.changes.set(entry.path, entry.head);
232
214
  }
233
- const unbinaried = answer.binaryFiles.size === 0;
234
- const { binaryFiles, facts: _facts, ...room } = yield* answered(answer);
235
- return { roomVersion: roomVersion(at), ...room, ...(unbinaried ? {} : { binaryFiles }) };
215
+ const unbinaried = answer.attachments.size === 0;
216
+ const { attachments, facts: _facts, ...room } = yield* answered(answer);
217
+ return { roomVersion: roomVersion(at), ...room, ...(unbinaried ? {} : { attachments }) };
236
218
  }
237
219
  function* movedOutcome(held, move) {
238
220
  const authorities = Array.isArray(held.authorities) ? held.authorities : [];
@@ -391,14 +373,14 @@ export function historyOutcome(status, body, locator, request) {
391
373
  typeof entry.created_at !== 'string') {
392
374
  return unusable();
393
375
  }
394
- const binaryFile = binaryFileOf(entry);
395
- if (binaryFile === null)
376
+ const attachment = attachmentOf(entry);
377
+ if (attachment === null)
396
378
  return unusable();
397
379
  revisions.push({
398
380
  revision: entry.revision,
399
381
  author,
400
382
  time: entry.created_at,
401
- ...(binaryFile === undefined ? {} : { binaryFile }),
383
+ ...(attachment === undefined ? {} : { attachment }),
402
384
  });
403
385
  }
404
386
  const next = held.next_before;
package/package.json CHANGED
@@ -1,6 +1,5 @@
1
1
  {
2
2
  "name": "@agentshouse/mdruntime-adapter",
3
- "version": "0.1.0-ce7ce34e4ced0",
4
3
  "description": "Agents House Adapter for the MD Runtime",
5
4
  "license": "MIT",
6
5
  "type": "module",
@@ -24,12 +23,13 @@
24
23
  "LICENSE"
25
24
  ],
26
25
  "dependencies": {
27
- "@agentshouse/mdmodel": "0.1.0-c1dd1aa113301",
28
- "@agentshouse/mdruntime": "0.1.0-c29328e50ab93"
26
+ "@agentshouse/mdmodel": "0.1.0-c4c6dbb0dc160",
27
+ "@agentshouse/mdruntime": "0.1.0-c0f755c73b599"
29
28
  },
30
29
  "repository": {
31
30
  "type": "git",
32
31
  "url": "git+https://github.com/agentshouse/core.git",
33
32
  "directory": "mdruntime/packages/mdruntime-adapter"
34
- }
33
+ },
34
+ "version": "0.1.0-ce861e3fb788a"
35
35
  }