@agentshouse/mdruntime 0.1.0-cbb8d8acc9d19 → 0.1.0-cbe358f60d1d3
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/README.md +24 -11
- package/dist/adapter.d.ts +177 -51
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +5 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +38 -4
- package/dist/attempt.d.ts +16 -0
- package/dist/attempt.d.ts.map +1 -0
- package/dist/attempt.js +70 -0
- package/dist/binary.d.ts +18 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +33 -5
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +202 -48
- package/dist/chain.d.ts +12 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +9 -14
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +11 -56
- package/dist/change.d.ts +45 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +34 -0
- package/dist/client.d.ts +42 -17
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +158 -20
- package/dist/collection.d.ts +13 -14
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +52 -56
- package/dist/compile.d.ts +11 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +586 -0
- package/dist/connection.d.ts +63 -21
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +3827 -896
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation.d.ts +50 -20
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +714 -242
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/durability.d.ts +10 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +20 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +159 -0
- package/dist/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/failure.d.ts +13 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +19 -0
- package/dist/fence.d.ts +6 -0
- package/dist/fence.d.ts.map +1 -0
- package/dist/fence.js +10 -0
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/host.d.ts +11 -3
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +132 -25
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +13 -1
- package/dist/index.d.ts +24 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -0
- package/dist/job.d.ts +14 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +28 -0
- package/dist/live.d.ts +23 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +127 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +20 -7
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +252 -98
- package/dist/messages.d.ts +28 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +89 -0
- package/dist/model.d.ts +13 -6
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +8 -5
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +22 -4
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +120 -14
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/outcome.d.ts +72 -5
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +81 -1
- package/dist/overlay.d.ts +15 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +136 -180
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -0
- package/dist/query.d.ts +42 -181
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -461
- package/dist/reach.d.ts +18 -4
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +236 -69
- package/dist/react.d.ts +33 -20
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +13 -21
- package/dist/readiness.d.ts +50 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +27 -0
- package/dist/recovery.d.ts +26 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +224 -0
- package/dist/refresh.d.ts +20 -7
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +148 -59
- package/dist/replica.d.ts +59 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +320 -156
- package/dist/room-draft.d.ts +59 -11
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +276 -80
- package/dist/rows.d.ts +23 -35
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +61 -117
- package/dist/save.d.ts +73 -4
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +825 -150
- package/dist/search.d.ts +15 -54
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +10 -121
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +75 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +421 -0
- package/dist/status.d.ts +7 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -9
- package/dist/store.d.ts +107 -60
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +319 -344
- package/dist/stored.d.ts +129 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +495 -0
- package/dist/tables.d.ts +328 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +587 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +75 -0
- package/dist/validation.d.ts +29 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +104 -0
- package/dist/windows.d.ts +11 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +16 -0
- package/dist/wire.d.ts +178 -33
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +71 -31
- package/package.json +26 -15
- package/AGENTS.md +0 -244
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
- package/dist/read-side.d.ts +0 -18
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -433
package/dist/replica.js
CHANGED
|
@@ -1,158 +1,196 @@
|
|
|
1
|
-
import { parse } from '@agentshouse/mdmodel';
|
|
2
|
-
import { immutable } from './change.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
export function compositionOf(record) {
|
|
1
|
+
import { equalValue, parse } from '@agentshouse/mdmodel';
|
|
2
|
+
import { immutable, landing } from './change.js';
|
|
3
|
+
import { collectionOf } from './collection.js';
|
|
4
|
+
import { storedModel } from './model.js';
|
|
5
|
+
import { acceptedBinaries, overlaying } from './overlay.js';
|
|
6
|
+
import { effectiveIn, rewriting } from './save.js';
|
|
7
|
+
import { ROW_WEIGHT } from './turn.js';
|
|
8
|
+
export function compositionOf(record, builtIn, typedPaths) {
|
|
9
9
|
return {
|
|
10
10
|
model: record.model,
|
|
11
11
|
selection: record.typedSelection === null
|
|
12
12
|
? null
|
|
13
|
-
: { roots: record.typedSelection.roots, model:
|
|
13
|
+
: { roots: record.typedSelection.roots, model: builtIn },
|
|
14
|
+
typedPaths,
|
|
14
15
|
};
|
|
15
16
|
}
|
|
16
|
-
export function
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
: { text: previous.record.documents, collection: previous.collection });
|
|
20
|
-
return read.kind === 'read'
|
|
21
|
-
? { kind: 'staged', confirmed: { record, collection: read.collection } }
|
|
22
|
-
: read;
|
|
17
|
+
export function storedRecord(record) {
|
|
18
|
+
const { key: _key, ...stored } = record;
|
|
19
|
+
return JSON.stringify({ ...stored, model: storedModel(stored.model) });
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
const PROBES = new Set([
|
|
22
|
+
'then',
|
|
23
|
+
'toJSON',
|
|
24
|
+
'constructor',
|
|
25
|
+
'valueOf',
|
|
26
|
+
'toString',
|
|
27
|
+
]);
|
|
28
|
+
class UnreadPath extends Error {
|
|
29
|
+
constructor(path) {
|
|
30
|
+
super(`the replica slice holds no row for ${path}`);
|
|
31
|
+
}
|
|
27
32
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
(
|
|
31
|
-
|
|
32
|
-
record.model !== previous.record.model ||
|
|
33
|
-
record.selectionModel !== previous.record.selectionModel)) {
|
|
34
|
-
const verdict = validateCollection(record.documents, record.binaryTargets, compositionOf(record));
|
|
35
|
-
if (verdict.verdict === 'refuse' && verdict.rule !== 'model-unavailable') {
|
|
36
|
-
return { kind: 'refused', refusal: verdict };
|
|
33
|
+
function strict(values, held) {
|
|
34
|
+
const guard = (property) => {
|
|
35
|
+
if (typeof property === 'string' && !held.has(property) && !PROBES.has(property)) {
|
|
36
|
+
throw new UnreadPath(property);
|
|
37
37
|
}
|
|
38
|
-
}
|
|
39
|
-
return
|
|
38
|
+
};
|
|
39
|
+
return new Proxy(values, {
|
|
40
|
+
get(target, property, receiver) {
|
|
41
|
+
guard(property);
|
|
42
|
+
return Reflect.get(target, property, receiver);
|
|
43
|
+
},
|
|
44
|
+
has(target, property) {
|
|
45
|
+
guard(property);
|
|
46
|
+
return Reflect.has(target, property);
|
|
47
|
+
},
|
|
48
|
+
getOwnPropertyDescriptor(target, property) {
|
|
49
|
+
guard(property);
|
|
50
|
+
return Reflect.getOwnPropertyDescriptor(target, property);
|
|
51
|
+
},
|
|
52
|
+
});
|
|
40
53
|
}
|
|
41
|
-
export function
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
export function* slicing(entries, paths) {
|
|
55
|
+
const held = new Set(paths);
|
|
56
|
+
for (const entry of entries)
|
|
57
|
+
held.add(entry.path);
|
|
58
|
+
const documents = {};
|
|
59
|
+
const binaries = {};
|
|
60
|
+
const revisions = {};
|
|
61
|
+
const fileIds = {};
|
|
62
|
+
const changes = {};
|
|
63
|
+
const written = {};
|
|
64
|
+
for (const entry of entries) {
|
|
65
|
+
if (entry.binary !== null)
|
|
66
|
+
binaries[entry.path] = entry.binary;
|
|
67
|
+
else if (entry.content !== null)
|
|
68
|
+
documents[entry.path] = entry.content;
|
|
69
|
+
if (entry.revision !== null)
|
|
70
|
+
revisions[entry.path] = entry.revision;
|
|
71
|
+
if (entry.fileId !== null)
|
|
72
|
+
fileIds[entry.path] = entry.fileId;
|
|
73
|
+
if (entry.change !== null)
|
|
74
|
+
changes[entry.path] = entry.change;
|
|
75
|
+
if (entry.written !== null)
|
|
76
|
+
written[entry.path] = entry.written;
|
|
77
|
+
yield ROW_WEIGHT;
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
documents: strict(documents, held),
|
|
81
|
+
binaries: strict(binaries, held),
|
|
82
|
+
revisions: strict(revisions, held),
|
|
83
|
+
fileIds: strict(fileIds, held),
|
|
84
|
+
changes: strict(changes, held),
|
|
85
|
+
written: strict(written, held),
|
|
86
|
+
held,
|
|
87
|
+
};
|
|
48
88
|
}
|
|
49
|
-
export function
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
89
|
+
export function entryAt(layer, path) {
|
|
90
|
+
const content = layer.documents[path];
|
|
91
|
+
const binary = layer.binaries[path];
|
|
92
|
+
if (content === undefined && binary === undefined)
|
|
93
|
+
return null;
|
|
94
|
+
return {
|
|
95
|
+
path,
|
|
96
|
+
content: content ?? null,
|
|
97
|
+
binary: binary ?? null,
|
|
98
|
+
revision: layer.revisions[path] ?? null,
|
|
99
|
+
fileId: layer.fileIds[path] ?? null,
|
|
100
|
+
change: layer.changes[path] ?? null,
|
|
101
|
+
written: layer.written?.[path] ?? null,
|
|
56
102
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
103
|
+
}
|
|
104
|
+
export function* slicingWith(slice, set, removed) {
|
|
105
|
+
const entries = new Map();
|
|
106
|
+
for (const path of slice.held) {
|
|
107
|
+
const entry = entryAt(slice, path);
|
|
108
|
+
if (entry !== null)
|
|
109
|
+
entries.set(path, { ...entry, type: null });
|
|
110
|
+
yield ROW_WEIGHT;
|
|
61
111
|
}
|
|
62
|
-
for (const path of
|
|
63
|
-
delete
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
112
|
+
for (const path of removed)
|
|
113
|
+
entries.delete(path);
|
|
114
|
+
for (const entry of set)
|
|
115
|
+
entries.set(entry.path, { ...entry, type: null });
|
|
116
|
+
return yield* slicing([...entries.values()], [...slice.held, ...set.map(({ path }) => path), ...removed]);
|
|
117
|
+
}
|
|
118
|
+
function changePaths(change) {
|
|
119
|
+
return change.kind === 'move' ? [change.path, change.to] : [change.path];
|
|
120
|
+
}
|
|
121
|
+
export function touchedBy(queue) {
|
|
122
|
+
const paths = new Set();
|
|
123
|
+
for (const batch of queue) {
|
|
124
|
+
for (const change of batch.changes)
|
|
125
|
+
for (const path of changePaths(change))
|
|
126
|
+
paths.add(path);
|
|
127
|
+
for (const precondition of batch.preconditions)
|
|
128
|
+
paths.add(precondition.path);
|
|
129
|
+
for (const move of batch.between?.moves ?? [])
|
|
130
|
+
paths.add(move.path);
|
|
68
131
|
}
|
|
69
|
-
return
|
|
70
|
-
...confirmed.record,
|
|
71
|
-
roomVersion,
|
|
72
|
-
documents: nextDocuments,
|
|
73
|
-
revisions: nextRevisions,
|
|
74
|
-
changes: nextChanges,
|
|
75
|
-
written: nextWritten,
|
|
76
|
-
binaryTargets: [...binaryTargets].sort(),
|
|
77
|
-
attachments: attachments === undefined
|
|
78
|
-
? confirmed.record.attachments
|
|
79
|
-
: declaredAttachments([attachments]),
|
|
80
|
-
}, confirmed);
|
|
132
|
+
return paths;
|
|
81
133
|
}
|
|
82
|
-
function
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
old.collection.composition.selection?.model === composition.selection?.model &&
|
|
95
|
-
JSON.stringify(old.collection.composition.selection?.roots) ===
|
|
96
|
-
JSON.stringify(composition.selection?.roots);
|
|
97
|
-
const collection = hasMove
|
|
98
|
-
? reuseRewrite
|
|
99
|
-
? old.collection
|
|
100
|
-
: parseCollection(before.documents, composition, old?.collection === undefined
|
|
101
|
-
? { text: confirmed.record.documents, collection: confirmed.collection }
|
|
102
|
-
: { text: old.before.documents, collection: old.collection })
|
|
103
|
-
: undefined;
|
|
104
|
-
const effective = collection === undefined
|
|
105
|
-
? batch
|
|
106
|
-
: reuseRewrite
|
|
107
|
-
? old.effective
|
|
108
|
-
: providerRewritten(batch, { ...before, collection });
|
|
109
|
-
layer =
|
|
110
|
-
old === undefined
|
|
111
|
-
? overlay(before, [effective])
|
|
112
|
-
: overlayChanged(before, [effective], old.before, [old.effective], old.after);
|
|
113
|
-
replay.push({
|
|
114
|
-
id: batch.id,
|
|
115
|
-
before,
|
|
116
|
-
after: layer,
|
|
117
|
-
effective,
|
|
118
|
-
...(collection === undefined ? {} : { collection }),
|
|
119
|
-
});
|
|
134
|
+
export function movedBy(queue, locator) {
|
|
135
|
+
const paths = new Set();
|
|
136
|
+
for (const batch of queue) {
|
|
137
|
+
if (!rewriting(batch, locator))
|
|
138
|
+
continue;
|
|
139
|
+
for (const change of batch.changes)
|
|
140
|
+
if (change.kind === 'move')
|
|
141
|
+
paths.add(change.path);
|
|
142
|
+
if (batch.between?.source.room === locator) {
|
|
143
|
+
for (const move of batch.between.moves)
|
|
144
|
+
paths.add(move.path);
|
|
145
|
+
}
|
|
120
146
|
}
|
|
121
|
-
return
|
|
147
|
+
return paths;
|
|
122
148
|
}
|
|
123
|
-
export function
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
149
|
+
export function* replaying(confirmed, queue, locator, composition) {
|
|
150
|
+
let layer = confirmed;
|
|
151
|
+
const touched = new Set();
|
|
152
|
+
for (const batch of queue) {
|
|
153
|
+
const effective = rewriting(batch, locator)
|
|
154
|
+
? yield* effectiveIn(batch, locator, {
|
|
155
|
+
...layer,
|
|
156
|
+
collection: collectionOf(layer.documents, composition),
|
|
157
|
+
})
|
|
158
|
+
: batch;
|
|
159
|
+
for (const change of effective.changes)
|
|
160
|
+
for (const path of changePaths(change))
|
|
161
|
+
touched.add(path);
|
|
162
|
+
layer = yield* overlaying(layer, [effective]);
|
|
163
|
+
yield ROW_WEIGHT;
|
|
138
164
|
}
|
|
139
|
-
const { layer, replay } = predicted(confirmed, batches, previous);
|
|
140
165
|
return {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
replay,
|
|
144
|
-
visible: {
|
|
145
|
-
...layer,
|
|
146
|
-
collection: parseCollection(layer.documents, compositionOf(confirmed.record), previous === undefined
|
|
147
|
-
? undefined
|
|
148
|
-
: { text: previous.visible.documents, collection: previous.visible.collection }),
|
|
149
|
-
},
|
|
166
|
+
visible: { ...layer, collection: collectionOf(layer.documents, composition) },
|
|
167
|
+
touched,
|
|
150
168
|
};
|
|
151
169
|
}
|
|
170
|
+
export function* overlayOf(visible, touched) {
|
|
171
|
+
const rows = [];
|
|
172
|
+
for (const path of touched) {
|
|
173
|
+
yield ROW_WEIGHT;
|
|
174
|
+
const entry = entryAt(visible, path);
|
|
175
|
+
rows.push(entry === null
|
|
176
|
+
? {
|
|
177
|
+
path,
|
|
178
|
+
present: false,
|
|
179
|
+
content: null,
|
|
180
|
+
binary: null,
|
|
181
|
+
revision: null,
|
|
182
|
+
fileId: null,
|
|
183
|
+
change: null,
|
|
184
|
+
written: null,
|
|
185
|
+
}
|
|
186
|
+
: { ...entry, written: null, present: true });
|
|
187
|
+
}
|
|
188
|
+
return rows;
|
|
189
|
+
}
|
|
152
190
|
function present(layer, path) {
|
|
153
|
-
return layer.documents[path] !== undefined || layer.
|
|
191
|
+
return layer.documents[path] !== undefined || layer.binaries[path] !== undefined;
|
|
154
192
|
}
|
|
155
|
-
function holds(layer, preconditions) {
|
|
193
|
+
export function holds(layer, preconditions) {
|
|
156
194
|
const parsed = new Map();
|
|
157
195
|
const documentAt = (path) => {
|
|
158
196
|
if (!parsed.has(path)) {
|
|
@@ -171,6 +209,8 @@ function holds(layer, preconditions) {
|
|
|
171
209
|
if (precondition.kind === 'revision') {
|
|
172
210
|
return present(layer, path) && layer.revisions[path] === precondition.revision;
|
|
173
211
|
}
|
|
212
|
+
if (precondition.kind === 'content')
|
|
213
|
+
return layer.documents[path] === precondition.content;
|
|
174
214
|
const document = documentAt(path);
|
|
175
215
|
if (document === null)
|
|
176
216
|
return false;
|
|
@@ -215,53 +255,177 @@ function effects(change) {
|
|
|
215
255
|
{ kind: 'present', path: change.to },
|
|
216
256
|
];
|
|
217
257
|
}
|
|
258
|
+
if (change.kind === 'binary' || change.kind === 'arrival')
|
|
259
|
+
return [{ kind: 'present', path }];
|
|
218
260
|
return [{ kind: 'absent', path }];
|
|
219
261
|
}
|
|
220
|
-
function after(changes, index, path) {
|
|
221
|
-
let at = path;
|
|
222
|
-
for (const change of changes.slice(index + 1)) {
|
|
223
|
-
if (change.path !== at)
|
|
224
|
-
continue;
|
|
225
|
-
if (change.kind === 'remove')
|
|
226
|
-
return null;
|
|
227
|
-
if (change.kind === 'move')
|
|
228
|
-
at = change.to;
|
|
229
|
-
}
|
|
230
|
-
return at;
|
|
231
|
-
}
|
|
232
262
|
export function landed(batch, confirmed, current = {}) {
|
|
233
|
-
const
|
|
263
|
+
const documents = new Proxy(confirmed.documents, {
|
|
264
|
+
get(target, property, receiver) {
|
|
265
|
+
if (typeof property === 'string' && Object.hasOwn(current, property))
|
|
266
|
+
return current[property];
|
|
267
|
+
return Reflect.get(target, property, receiver);
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
const layer = { ...confirmed, documents };
|
|
234
271
|
return batch.changes.every((change, index) => {
|
|
235
272
|
if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
|
|
236
|
-
const at =
|
|
273
|
+
const at = landing(batch.changes, index, change.path);
|
|
237
274
|
return at === null || layer.documents[at] === change.content;
|
|
238
275
|
}
|
|
276
|
+
if (change.kind === 'binary') {
|
|
277
|
+
const at = landing(batch.changes, index, change.path);
|
|
278
|
+
return at === null || layer.binaries[at]?.sha256 === change.sha256;
|
|
279
|
+
}
|
|
239
280
|
return effects(change).every((effect) => {
|
|
240
281
|
if (effect.kind === 'absent')
|
|
241
282
|
return holds(layer, [effect]);
|
|
242
|
-
const at =
|
|
283
|
+
const at = landing(batch.changes, index, effect.path);
|
|
243
284
|
return at === null || holds(layer, [{ ...effect, path: at }]);
|
|
244
285
|
});
|
|
245
286
|
});
|
|
246
287
|
}
|
|
247
|
-
|
|
288
|
+
function founded(batch, layer) {
|
|
289
|
+
let changed = false;
|
|
290
|
+
const preconditions = batch.preconditions.map((precondition) => {
|
|
291
|
+
if (precondition.kind !== 'content')
|
|
292
|
+
return precondition;
|
|
293
|
+
const revision = layer.revisions[precondition.path];
|
|
294
|
+
if (revision === undefined || layer.documents[precondition.path] !== precondition.content) {
|
|
295
|
+
return precondition;
|
|
296
|
+
}
|
|
297
|
+
changed = true;
|
|
298
|
+
return { kind: 'revision', path: precondition.path, revision };
|
|
299
|
+
});
|
|
300
|
+
return changed ? immutable({ ...batch, preconditions }) : batch;
|
|
301
|
+
}
|
|
302
|
+
export function* standing(confirmed, queue, withdrawable, local, locator, composition) {
|
|
248
303
|
if (!queue.some(withdrawable))
|
|
249
|
-
return [];
|
|
250
|
-
|
|
251
|
-
let layer = confirmed.record;
|
|
304
|
+
return { withdrawn: [], kept: queue };
|
|
305
|
+
let layer = confirmed;
|
|
252
306
|
const withdrawn = [];
|
|
307
|
+
const kept = [];
|
|
253
308
|
for (const batch of queue) {
|
|
254
309
|
if (withdrawable(batch) && !holds(layer, batch.preconditions)) {
|
|
255
310
|
withdrawn.push(batch);
|
|
256
311
|
continue;
|
|
257
312
|
}
|
|
258
|
-
const effective = batch
|
|
259
|
-
?
|
|
313
|
+
const effective = rewriting(batch, locator)
|
|
314
|
+
? yield* effectiveIn(batch, locator, {
|
|
260
315
|
...layer,
|
|
261
|
-
collection:
|
|
316
|
+
collection: collectionOf(layer.documents, composition),
|
|
262
317
|
})
|
|
263
318
|
: batch;
|
|
264
|
-
|
|
319
|
+
let unapplied = false;
|
|
320
|
+
const next = yield* overlaying(layer, [effective], () => {
|
|
321
|
+
unapplied = true;
|
|
322
|
+
});
|
|
323
|
+
if (local && unapplied) {
|
|
324
|
+
withdrawn.push(batch);
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
kept.push(local || !withdrawable(batch) ? batch : founded(batch, layer));
|
|
328
|
+
layer = next;
|
|
329
|
+
}
|
|
330
|
+
return { withdrawn, kept };
|
|
331
|
+
}
|
|
332
|
+
export function returnedPaths(submitted, accepted) {
|
|
333
|
+
const paths = new Set([
|
|
334
|
+
...Object.keys(accepted.documents),
|
|
335
|
+
...Object.keys(accepted.revisions),
|
|
336
|
+
...Object.keys(accepted.fileIds ?? {}),
|
|
337
|
+
...Object.keys(accepted.changes),
|
|
338
|
+
...Object.keys(accepted.binaryFiles ?? {}),
|
|
339
|
+
...accepted.removals,
|
|
340
|
+
]);
|
|
341
|
+
for (const change of submitted.changes) {
|
|
342
|
+
if (change.kind === 'move') {
|
|
343
|
+
paths.add(change.path);
|
|
344
|
+
paths.add(change.to);
|
|
345
|
+
}
|
|
346
|
+
if (change.kind === 'arrival')
|
|
347
|
+
paths.add(change.path);
|
|
348
|
+
}
|
|
349
|
+
return paths;
|
|
350
|
+
}
|
|
351
|
+
export function* returning(confirmed, submitted, accepted, writtenAt) {
|
|
352
|
+
const documents = new Map();
|
|
353
|
+
const binaries = new Map();
|
|
354
|
+
const revisions = new Map();
|
|
355
|
+
const fileIds = new Map();
|
|
356
|
+
const changes = new Map();
|
|
357
|
+
const written = new Map();
|
|
358
|
+
const read = (changed, held, path) => (changed.has(path) ? changed.get(path) : held[path]);
|
|
359
|
+
for (const [path, content] of Object.entries(accepted.documents)) {
|
|
360
|
+
yield ROW_WEIGHT;
|
|
361
|
+
documents.set(path, content);
|
|
362
|
+
fileIds.set(path, undefined);
|
|
363
|
+
written.set(path, writtenAt);
|
|
364
|
+
}
|
|
365
|
+
for (const [path, revision] of Object.entries(accepted.revisions)) {
|
|
366
|
+
yield ROW_WEIGHT;
|
|
367
|
+
revisions.set(path, revision);
|
|
368
|
+
fileIds.set(path, undefined);
|
|
369
|
+
if (confirmed.revisions[path] !== revision)
|
|
370
|
+
changes.set(path, undefined);
|
|
371
|
+
}
|
|
372
|
+
for (const [path, fileId] of Object.entries(accepted.fileIds ?? {}))
|
|
373
|
+
fileIds.set(path, fileId);
|
|
374
|
+
for (const [path, change] of Object.entries(accepted.changes))
|
|
375
|
+
changes.set(path, change);
|
|
376
|
+
for (const path of accepted.removals) {
|
|
377
|
+
documents.set(path, undefined);
|
|
378
|
+
revisions.set(path, undefined);
|
|
379
|
+
fileIds.set(path, undefined);
|
|
380
|
+
changes.set(path, undefined);
|
|
381
|
+
written.set(path, undefined);
|
|
382
|
+
}
|
|
383
|
+
for (const change of submitted.changes) {
|
|
384
|
+
if (change.kind === 'arrival' && 'binary' in change) {
|
|
385
|
+
if (accepted.revisions[change.path] !== undefined) {
|
|
386
|
+
binaries.set(change.path, { state: 'accepted', ...change.binary });
|
|
387
|
+
}
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
if (change.kind !== 'move')
|
|
391
|
+
continue;
|
|
392
|
+
const moved = read(binaries, confirmed.binaries, change.path);
|
|
393
|
+
if (moved === undefined || accepted.revisions[change.to] === undefined)
|
|
394
|
+
continue;
|
|
395
|
+
binaries.set(change.to, moved);
|
|
396
|
+
binaries.set(change.path, undefined);
|
|
397
|
+
}
|
|
398
|
+
for (const [path, binary] of acceptedBinaries(accepted.binaryFiles ?? {}))
|
|
399
|
+
binaries.set(path, binary);
|
|
400
|
+
for (const path of accepted.removals)
|
|
401
|
+
binaries.set(path, undefined);
|
|
402
|
+
const paths = new Set([
|
|
403
|
+
...documents.keys(),
|
|
404
|
+
...binaries.keys(),
|
|
405
|
+
...revisions.keys(),
|
|
406
|
+
...fileIds.keys(),
|
|
407
|
+
...changes.keys(),
|
|
408
|
+
...written.keys(),
|
|
409
|
+
]);
|
|
410
|
+
const set = [];
|
|
411
|
+
const removed = [];
|
|
412
|
+
for (const path of paths) {
|
|
413
|
+
yield ROW_WEIGHT;
|
|
414
|
+
const content = read(documents, confirmed.documents, path);
|
|
415
|
+
const binary = read(binaries, confirmed.binaries, path);
|
|
416
|
+
if (content === undefined && binary === undefined) {
|
|
417
|
+
removed.push(path);
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
set.push({
|
|
421
|
+
path,
|
|
422
|
+
content: binary === undefined ? (content ?? null) : null,
|
|
423
|
+
binary: binary ?? null,
|
|
424
|
+
revision: read(revisions, confirmed.revisions, path) ?? null,
|
|
425
|
+
fileId: read(fileIds, confirmed.fileIds, path) ?? null,
|
|
426
|
+
change: read(changes, confirmed.changes, path) ?? null,
|
|
427
|
+
written: read(written, confirmed.written, path) ?? null,
|
|
428
|
+
});
|
|
265
429
|
}
|
|
266
|
-
return
|
|
430
|
+
return { set, removed };
|
|
267
431
|
}
|
package/dist/room-draft.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ProviderRefusal } from './adapter.js';
|
|
2
|
+
import type { Open } from './open.js';
|
|
2
3
|
import type { Change, Precondition } from './change.js';
|
|
3
4
|
import type { VisibleLayer } from './replica.js';
|
|
4
5
|
export type FieldValue = {
|
|
@@ -31,11 +32,24 @@ export type RoomDraftParts = {
|
|
|
31
32
|
readonly creation?: RoomDraftCreation;
|
|
32
33
|
readonly move?: {
|
|
33
34
|
readonly to: string;
|
|
35
|
+
readonly room?: string;
|
|
34
36
|
readonly base?: string | null;
|
|
35
37
|
};
|
|
36
38
|
readonly removal?: {
|
|
37
39
|
readonly base?: string | null;
|
|
38
40
|
};
|
|
41
|
+
readonly file?: {
|
|
42
|
+
readonly blob: Blob;
|
|
43
|
+
readonly base?: string | null;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export type HeldFile = {
|
|
47
|
+
readonly localId: string;
|
|
48
|
+
readonly name?: string;
|
|
49
|
+
readonly byteLength: number;
|
|
50
|
+
readonly mediaType: string;
|
|
51
|
+
readonly sha256: string;
|
|
52
|
+
readonly base: string | null;
|
|
39
53
|
};
|
|
40
54
|
type HeldParts = {
|
|
41
55
|
readonly fields?: Readonly<Record<string, {
|
|
@@ -57,11 +71,13 @@ type HeldParts = {
|
|
|
57
71
|
readonly creation?: RoomDraftCreation;
|
|
58
72
|
readonly move?: {
|
|
59
73
|
readonly to: string;
|
|
74
|
+
readonly room?: string;
|
|
60
75
|
readonly base: string | null;
|
|
61
76
|
};
|
|
62
77
|
readonly removal?: {
|
|
63
78
|
readonly base: string | null;
|
|
64
79
|
};
|
|
80
|
+
readonly file?: HeldFile;
|
|
65
81
|
};
|
|
66
82
|
type ChangedUnderneath = {
|
|
67
83
|
readonly fields?: Readonly<Record<string, FieldValue>>;
|
|
@@ -71,14 +87,12 @@ type ChangedUnderneath = {
|
|
|
71
87
|
readonly creation?: string;
|
|
72
88
|
readonly move?: string | null;
|
|
73
89
|
readonly removal?: string | null;
|
|
90
|
+
readonly file?: string | null;
|
|
74
91
|
};
|
|
75
|
-
export type ReturnReason =
|
|
76
|
-
readonly kind: 'conflicted' | 'rejected'
|
|
77
|
-
}) | {
|
|
78
|
-
readonly kind: 'expired';
|
|
79
|
-
} | {
|
|
80
|
-
readonly kind: 'withdrawn';
|
|
92
|
+
export type ReturnReason = ProviderRefusal & {
|
|
93
|
+
readonly kind: Open<'conflicted' | 'rejected' | 'withdrawn'>;
|
|
81
94
|
};
|
|
95
|
+
export declare function returnedBy(kind: 'withdrawn'): ReturnReason;
|
|
82
96
|
export type RoomDraft = {
|
|
83
97
|
readonly room: string;
|
|
84
98
|
readonly path: string;
|
|
@@ -92,11 +106,14 @@ export type RoomDraftWrite = {
|
|
|
92
106
|
readonly roomDraft: RoomDraft | null;
|
|
93
107
|
} | {
|
|
94
108
|
readonly outcome: 'stale';
|
|
109
|
+
readonly code: string;
|
|
110
|
+
readonly retryable: boolean;
|
|
95
111
|
readonly current: RoomDraft | null;
|
|
96
112
|
};
|
|
113
|
+
export declare function staleWrite(current: RoomDraft | null): RoomDraftWrite;
|
|
97
114
|
export type ChangedPart = {
|
|
98
115
|
readonly path: string;
|
|
99
|
-
readonly part: 'field' | 'section' | 'preamble' | 'markdown' | '
|
|
116
|
+
readonly part: Open<'field' | 'section' | 'preamble' | 'markdown' | 'move' | 'removal' | 'file'>;
|
|
100
117
|
readonly name?: string;
|
|
101
118
|
};
|
|
102
119
|
export type RoomDraftRecord = {
|
|
@@ -110,18 +127,49 @@ export type SavedDraft = {
|
|
|
110
127
|
readonly path: string;
|
|
111
128
|
readonly parts: HeldParts;
|
|
112
129
|
};
|
|
113
|
-
export declare function readParts(value: unknown): RoomDraftParts;
|
|
114
|
-
export declare function
|
|
115
|
-
export declare function
|
|
130
|
+
export declare function readParts(value: unknown, path: string): RoomDraftParts;
|
|
131
|
+
export declare function gatheredParts(earlier: unknown, later: unknown): unknown;
|
|
132
|
+
export declare function held(current: HeldParts | undefined, parts: RoomDraftParts, file?: HeldFile): HeldParts;
|
|
116
133
|
export declare function underneath(parts: HeldParts, path: string, visible: VisibleLayer | undefined): ChangedUnderneath;
|
|
117
|
-
export declare function
|
|
134
|
+
export declare function heldFile(blob: Blob, base: string | null): Promise<HeldFile>;
|
|
135
|
+
export declare function binaryBase(visible: VisibleLayer | undefined, path: string): string | null;
|
|
136
|
+
export declare function changedParts(path: string, parts: HeldParts, visible: VisibleLayer | undefined): readonly ChangedPart[];
|
|
137
|
+
export declare function lineEnded(text: string): string;
|
|
138
|
+
export type TextParts = {
|
|
139
|
+
readonly fields?: Readonly<Record<string, {
|
|
140
|
+
readonly value: unknown;
|
|
141
|
+
}>>;
|
|
142
|
+
readonly sections?: Readonly<Record<string, {
|
|
143
|
+
readonly value: string;
|
|
144
|
+
}>>;
|
|
145
|
+
readonly preamble?: {
|
|
146
|
+
readonly value: string;
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
export declare function partsAfter(markdown: string, parts: TextParts): string;
|
|
118
150
|
export declare function returnedInto(existing: HeldParts | undefined, returned: HeldParts): HeldParts;
|
|
119
151
|
export declare function moveTarget(move: {
|
|
120
152
|
readonly to: string;
|
|
121
153
|
}): string;
|
|
154
|
+
export declare function readSavedParts(named: readonly {
|
|
155
|
+
readonly path: string;
|
|
156
|
+
readonly parts: unknown;
|
|
157
|
+
}[]): readonly {
|
|
158
|
+
readonly path: string;
|
|
159
|
+
readonly parts: RoomDraftParts;
|
|
160
|
+
}[];
|
|
161
|
+
export declare function partsChanges(named: readonly {
|
|
162
|
+
readonly path: string;
|
|
163
|
+
readonly parts: unknown;
|
|
164
|
+
}[]): {
|
|
165
|
+
readonly changes: readonly Change[];
|
|
166
|
+
readonly preconditions: readonly Precondition[];
|
|
167
|
+
};
|
|
122
168
|
export declare function changesOf(path: string, parts: HeldParts): {
|
|
123
169
|
changes: readonly Change[];
|
|
124
170
|
bases: readonly Precondition[];
|
|
125
171
|
};
|
|
172
|
+
export declare function joinedDraft(carried: SavedDraft, saved: readonly SavedDraft[]): SavedDraft;
|
|
173
|
+
export declare function withdrawnDrafts(drafts: readonly SavedDraft[], changes: readonly Change[]): readonly SavedDraft[];
|
|
126
174
|
export {};
|
|
127
175
|
//# sourceMappingURL=room-draft.d.ts.map
|