@mmon-co/ncept-local 0.3.12 → 0.3.13
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 +9 -7
- package/dist/directory.d.ts +2 -1
- package/dist/directory.js +66 -31
- package/dist/exchange.d.ts +17 -10
- package/dist/exchange.js +171 -77
- package/dist/host-tools.js +11 -9
- package/dist/hub.d.ts +4 -4
- package/dist/hub.js +120 -104
- package/dist/learning.js +1 -1
- package/dist/packed.d.ts +21 -0
- package/dist/packed.js +80 -0
- package/dist/runtime.d.ts +16 -7
- package/dist/runtime.js +28 -26
- package/package.json +1 -1
package/dist/hub.js
CHANGED
|
@@ -2,12 +2,13 @@ import { mkdtemp, rename, rm } from "node:fs/promises";
|
|
|
2
2
|
import { dirname, basename, join } from "node:path";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import metadata from "../package.json" with { type: "json" };
|
|
5
|
-
import { attachmentDigest,
|
|
5
|
+
import { attachmentDigest, contentAttachments, extractSparseClosure, exactPatchRefKey, isContentDigest, isMissionRevision, latestMissionRevision, isPatchId, migrateTheoryPatchClosure, PATCH_MISSIONS_PROPERTY, readReifiedMissions, reifyProfileMissions, relocateGuidance, reifyView, releasePrecedenceKey, resolveTheory, resolveSparsePatch, resolvedGraphAttachments, revisionContent, theoryPatchNeedsMigration, verifySparseBundle, sameExactPatchRef, selectGraph, withReifiedMissions, } from "@mmon-co/ncept-core";
|
|
6
6
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
7
7
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
8
8
|
import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
|
|
9
9
|
import { hubBaseUrl, HubAuth, HubSignInRequired } from "./hub-auth.js";
|
|
10
10
|
import { unpackDirectory } from "./directory.js";
|
|
11
|
+
import { PackedPatches } from "./packed.js";
|
|
11
12
|
import { object, parsePiSession } from "./sessions.js";
|
|
12
13
|
import { validate, unpack } from "./exchange.js";
|
|
13
14
|
/**
|
|
@@ -15,8 +16,7 @@ import { validate, unpack } from "./exchange.js";
|
|
|
15
16
|
* the release carries that map alone whatever Patches it was learnt from, and offering the
|
|
16
17
|
* Mission to whoever opens it.
|
|
17
18
|
*/
|
|
18
|
-
export function missionBundle(
|
|
19
|
-
const graph = resolvePatchBundle(patches, new Set(["local"]), notebookSelection(patches));
|
|
19
|
+
export async function missionBundle(graph, mission, title, origin) {
|
|
20
20
|
const cone = selectGraph(graph, { cone: { targets: mission.targets.map(({ node }) => node) } });
|
|
21
21
|
if (!cone.nodes.length)
|
|
22
22
|
throw new Error("This Mission has no map yet");
|
|
@@ -48,9 +48,9 @@ export function missionBundle(patches, mission, title, origin) {
|
|
|
48
48
|
citations: reified.metadata.citations.filter((citation) => citation.kind !== "reified_from"),
|
|
49
49
|
}, [suggestion]),
|
|
50
50
|
};
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
51
|
+
const store = new PackedPatches();
|
|
52
|
+
const terminal = { identity: `patch-${randomUUID()}`, digest: store.add(content) };
|
|
53
|
+
const bundle = await extractSparseClosure(terminal, store, { completeTerminal: true });
|
|
54
54
|
return { bundle, skills: cone.nodes.length };
|
|
55
55
|
}
|
|
56
56
|
function releaseAddress(value, base) {
|
|
@@ -74,49 +74,67 @@ async function fetched(url, init) {
|
|
|
74
74
|
throw new Error("Hub request failed (" + response.status + "): " + (await response.text()));
|
|
75
75
|
return response;
|
|
76
76
|
}
|
|
77
|
-
function
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
77
|
+
async function fetchReleaseBlobs(endpoint, headers, graph) {
|
|
78
|
+
const hashes = [...new Set(Array.from(resolvedGraphAttachments(graph), (item) => item.hash))];
|
|
79
|
+
const blobs = new Map();
|
|
80
|
+
function retain(hash, bytes) {
|
|
81
|
+
if (attachmentDigest(bytes) !== hash)
|
|
82
|
+
throw new Error("Hub attachment digest mismatch: " + hash);
|
|
83
|
+
blobs.set(hash, bytes);
|
|
84
|
+
}
|
|
85
|
+
let legacy = false;
|
|
86
|
+
async function batch(group) {
|
|
87
|
+
if (!legacy) {
|
|
88
|
+
const response = await fetch(endpoint + "/attachments", {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
91
|
+
body: JSON.stringify({ hashes: group }),
|
|
92
|
+
signal: AbortSignal.timeout(120_000),
|
|
93
|
+
});
|
|
94
|
+
const batchRoute = response.headers.has("X-Hub-Attachment-Batch");
|
|
95
|
+
if (response.ok && batchRoute) {
|
|
96
|
+
const bytes = await response.arrayBuffer();
|
|
97
|
+
const frame = new DataView(bytes);
|
|
98
|
+
let offset = 0;
|
|
99
|
+
for (const hash of group) {
|
|
100
|
+
const length = frame.getUint32(offset);
|
|
101
|
+
retain(hash, new Uint8Array(bytes, offset + 4, length));
|
|
102
|
+
offset += 4 + length;
|
|
103
|
+
}
|
|
104
|
+
if (offset !== bytes.byteLength)
|
|
105
|
+
throw new Error("Unexpected Hub attachment frame");
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (response.status === 413) {
|
|
109
|
+
if (group.length > 1) {
|
|
110
|
+
const mid = Math.ceil(group.length / 2);
|
|
111
|
+
await batch(group.slice(0, mid));
|
|
112
|
+
await batch(group.slice(mid));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
91
115
|
}
|
|
116
|
+
else if (!batchRoute && (response.ok || response.status === 404 || response.status === 405)) {
|
|
117
|
+
await response.body?.cancel();
|
|
118
|
+
legacy = true;
|
|
119
|
+
}
|
|
120
|
+
else
|
|
121
|
+
throw new Error("Hub request failed (" + response.status + "): " + (await response.text()));
|
|
92
122
|
}
|
|
123
|
+
for (const hash of group)
|
|
124
|
+
retain(hash, new Uint8Array(await (await fetched(endpoint + "/attachments/" + hash, { headers })).arrayBuffer()));
|
|
93
125
|
}
|
|
94
|
-
|
|
126
|
+
for (let i = 0; i < hashes.length; i += 256)
|
|
127
|
+
await batch(hashes.slice(i, i + 256));
|
|
128
|
+
return blobs;
|
|
95
129
|
}
|
|
96
|
-
|
|
97
|
-
function
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
exact(revision.ref) &&
|
|
105
|
-
Array.isArray(revision.bases) &&
|
|
106
|
-
revision.bases.every(exact) &&
|
|
107
|
-
object(revision.delta) &&
|
|
108
|
-
Array.isArray(revision.delta.operations) &&
|
|
109
|
-
object(revision.metadata) &&
|
|
110
|
-
object(revision.metadata.properties) &&
|
|
111
|
-
Array.isArray(revision.metadata.citations);
|
|
112
|
-
const revisions = value.revisions.map((revision) => {
|
|
113
|
-
if (!isRevision(revision))
|
|
114
|
-
throw new Error("Invalid Hub revision");
|
|
115
|
-
return revision;
|
|
116
|
-
});
|
|
117
|
-
const bundle = { terminal: value.terminal, revisions };
|
|
118
|
-
resolvePatchBundle(bundle);
|
|
119
|
-
return bundleForRevision(bundle.terminal, new Map(revisions.map((revision) => [revision.ref.digest, revisionContent(revision)])));
|
|
130
|
+
const captureReading = { completeTerminal: true, includeCompleteBases: true };
|
|
131
|
+
function bundleStore(bundle) {
|
|
132
|
+
const store = new PackedPatches();
|
|
133
|
+
for (const [d, manifest] of bundle.manifests)
|
|
134
|
+
store.manifests.set(d, manifest);
|
|
135
|
+
for (const [d, chunk] of bundle.chunks)
|
|
136
|
+
store.chunks.set(d, chunk);
|
|
137
|
+
return store;
|
|
120
138
|
}
|
|
121
139
|
function theoryRequirement(value) {
|
|
122
140
|
if (!object(value) ||
|
|
@@ -175,14 +193,17 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
175
193
|
});
|
|
176
194
|
return hash;
|
|
177
195
|
}
|
|
178
|
-
/** Upload
|
|
196
|
+
/** Upload supplied content, capture it as a new Hub Patch and release it. */
|
|
179
197
|
async function release(bundle, blobs, version, visibility) {
|
|
198
|
+
const { completeContents } = await verifySparseBundle(bundle, captureReading);
|
|
199
|
+
const uploads = new Map([...completeContents.values()].flatMap(contentAttachments).map((attachment) => [attachment.hash, attachment]));
|
|
180
200
|
return connected(async (call) => {
|
|
181
|
-
for (const attachment of
|
|
182
|
-
|
|
183
|
-
if
|
|
184
|
-
|
|
185
|
-
|
|
201
|
+
for (const [hash, attachment] of uploads) {
|
|
202
|
+
// A complete revision can still come from a sparse reading that omitted some blobs.
|
|
203
|
+
// Capture decides if the Hub can reuse those bytes or requires an upload.
|
|
204
|
+
const bytes = blobs.get(hash);
|
|
205
|
+
if (bytes)
|
|
206
|
+
await upload(call, bytes, attachment.mediaType);
|
|
186
207
|
}
|
|
187
208
|
const captured = await call("capture", { bundle });
|
|
188
209
|
await call("name", { patchId: captured.ref.identity, digest: captured.ref.digest, version });
|
|
@@ -202,10 +223,10 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
202
223
|
if (!object(response) || response.patchId !== selected.patchId || response.version !== selected.version)
|
|
203
224
|
throw new Error("Hub release address mismatch");
|
|
204
225
|
const requirement = theoryRequirement(response.theoryRequirement);
|
|
205
|
-
const bundle =
|
|
226
|
+
const { bundle, graph } = await verifySparseBundle(response.bundle);
|
|
206
227
|
if (bundle.terminal.identity !== selected.patchId)
|
|
207
228
|
throw new Error("Hub release identity mismatch");
|
|
208
|
-
const
|
|
229
|
+
const refs = new Set(graph.patchOrder.map(exactPatchRefKey));
|
|
209
230
|
if (!Array.isArray(response.baseTheoryRequirements))
|
|
210
231
|
throw new Error("Invalid Hub base Theory requirements");
|
|
211
232
|
const baseRequirements = new Map();
|
|
@@ -216,49 +237,45 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
216
237
|
!isContentDigest(declaration.ref.digest))
|
|
217
238
|
throw new Error("Invalid Hub base Theory requirement");
|
|
218
239
|
const key = exactPatchRefKey({ identity: declaration.ref.identity, digest: declaration.ref.digest });
|
|
219
|
-
if (!
|
|
240
|
+
if (!refs.has(key) || baseRequirements.has(key) || key === exactPatchRefKey(bundle.terminal))
|
|
220
241
|
throw new Error("Invalid Hub base Theory requirement");
|
|
221
242
|
baseRequirements.set(key, theoryRequirement(declaration.requirement));
|
|
222
243
|
}
|
|
223
|
-
const blobs = new Map();
|
|
224
|
-
for (const { hash } of attachments(bundle)) {
|
|
225
|
-
const bytes = new Uint8Array(await (await fetched(endpoint + "/attachments/" + hash, { headers })).arrayBuffer());
|
|
226
|
-
if (attachmentDigest(bytes) !== hash)
|
|
227
|
-
throw new Error("Hub attachment digest mismatch: " + hash);
|
|
228
|
-
blobs.set(hash, bytes);
|
|
229
|
-
}
|
|
230
244
|
const current = await profile.read();
|
|
231
245
|
const theory = resolveTheory(current.manifest.theory, theories);
|
|
232
|
-
let
|
|
233
|
-
let adopted;
|
|
246
|
+
let reading = { bundle, graph };
|
|
234
247
|
try {
|
|
235
|
-
|
|
248
|
+
const rewrites = [requirement, ...baseRequirements.values()].map((pin) => theoryPatchNeedsMigration(theory, pin));
|
|
249
|
+
if (rewrites.some(Boolean)) {
|
|
250
|
+
const { terminal, revisions } = migrateTheoryPatchClosure(theory, bundleStore(bundle).dense(bundle.terminal), requirement, baseRequirements);
|
|
251
|
+
if (!sameExactPatchRef(terminal.ref, bundle.terminal)) {
|
|
252
|
+
const store = new PackedPatches();
|
|
253
|
+
for (const revision of revisions)
|
|
254
|
+
store.add(revisionContent(revision));
|
|
255
|
+
reading = await verifySparseBundle(await extractSparseClosure(terminal.ref, store));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
236
258
|
}
|
|
237
259
|
catch (error) {
|
|
238
260
|
throw new Error("Cannot adopt Hub release; its publisher must republish for the installed Theory: " + String(error));
|
|
239
261
|
}
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const personal = current.
|
|
246
|
-
if (!personal)
|
|
247
|
-
throw new Error("Missing personal Patch");
|
|
262
|
+
const blobs = await fetchReleaseBlobs(endpoint, headers, graph);
|
|
263
|
+
const adopted = reading.bundle.terminal;
|
|
264
|
+
const manifest = new Map(reading.bundle.manifests).get(adopted.digest);
|
|
265
|
+
if (!manifest)
|
|
266
|
+
throw new Error("Missing Hub terminal manifest");
|
|
267
|
+
const personal = current.personal;
|
|
248
268
|
const content = {
|
|
249
|
-
...
|
|
250
|
-
bases: [...personal.bases.filter((ref) => ref.identity !== adopted.
|
|
269
|
+
...personal,
|
|
270
|
+
bases: [...personal.bases.filter((ref) => ref.identity !== adopted.identity), adopted],
|
|
251
271
|
};
|
|
252
|
-
const
|
|
253
|
-
contents.set(next.ref.digest, content);
|
|
254
|
-
resolvePatchBundle(bundleForRevision(next.ref, contents));
|
|
255
|
-
const suggestions = readReifiedMissions(revisionContent(adopted), resolvePatchBundle(bundleForRevision(adopted.ref, contents)));
|
|
272
|
+
const suggestions = readReifiedMissions(manifest, reading.graph);
|
|
256
273
|
const existing = new Set();
|
|
257
274
|
for (const entry of current.log) {
|
|
258
275
|
if (!isMissionRevision(entry))
|
|
259
276
|
continue;
|
|
260
277
|
const source = entry.mission.source;
|
|
261
|
-
if (source?.patch.identity === adopted.
|
|
278
|
+
if (source?.patch.identity === adopted.identity)
|
|
262
279
|
existing.add(source.suggestionId);
|
|
263
280
|
}
|
|
264
281
|
const selectedMissions = [];
|
|
@@ -276,7 +293,7 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
276
293
|
id: `mission-${randomUUID()}`,
|
|
277
294
|
statement: item.statement,
|
|
278
295
|
priority: 1,
|
|
279
|
-
targets: item.targetNodes.map((node) => ({ node, provenance: adopted
|
|
296
|
+
targets: item.targetNodes.map((node) => ({ node, provenance: adopted })),
|
|
280
297
|
...(item.guidance === undefined ? {} : { guidance: item.guidance }),
|
|
281
298
|
...(item.assessment === undefined ? {} : { assessment: item.assessment }),
|
|
282
299
|
source: {
|
|
@@ -287,18 +304,16 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
287
304
|
},
|
|
288
305
|
});
|
|
289
306
|
}
|
|
290
|
-
// Nothing touches the live head until every downloaded
|
|
307
|
+
// Nothing touches the live head until every downloaded manifest, chunk and blob verifies.
|
|
291
308
|
for (const bytes of blobs.values())
|
|
292
309
|
await profile.addBlob(bytes);
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
await profile.addRevision(revision.ref.identity, revisionContent(revision));
|
|
298
|
-
await profile.setPersonal(await profile.addRevision(personal.ref.identity, content));
|
|
310
|
+
await profile.addSparseClosure(bundle);
|
|
311
|
+
if (reading.bundle !== bundle)
|
|
312
|
+
await profile.addSparseClosure(reading.bundle);
|
|
313
|
+
await profile.setPersonal(await profile.addRevision(current.manifest.personal.identity, content));
|
|
299
314
|
for (const mission of selectedMissions)
|
|
300
315
|
await createMission(mission);
|
|
301
|
-
return adopted
|
|
316
|
+
return adopted;
|
|
302
317
|
},
|
|
303
318
|
async signIn() {
|
|
304
319
|
await (await HubAuth.open(resource, options)).signIn();
|
|
@@ -306,18 +321,21 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
306
321
|
async publish(input) {
|
|
307
322
|
releasePrecedenceKey(input.version);
|
|
308
323
|
const current = await profile.read();
|
|
309
|
-
|
|
310
|
-
const original = bundleForRevision(input.patch, contents);
|
|
311
|
-
const terminal = original.revisions.find(({ ref }) => sameExactPatchRef(ref, input.patch));
|
|
312
|
-
if (!terminal)
|
|
324
|
+
if (!current.graph.patchOrder.some((ref) => sameExactPatchRef(ref, input.patch)))
|
|
313
325
|
throw new Error("Patch is not in this Profile");
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
const {
|
|
317
|
-
|
|
318
|
-
|
|
326
|
+
const store = PackedPatches.fromFiles(await profile.files());
|
|
327
|
+
const terminal = store.content(input.patch);
|
|
328
|
+
const { graph } = await resolveSparsePatch(input.patch, store);
|
|
329
|
+
for (const base of graph.patchOrder) {
|
|
330
|
+
const manifest = store.manifests.get(base.digest);
|
|
331
|
+
if (!sameExactPatchRef(base, input.patch) &&
|
|
332
|
+
manifest &&
|
|
333
|
+
PATCH_MISSIONS_PROPERTY in manifest.metadata.properties)
|
|
334
|
+
throw new Error("Cannot publish a Patch with private Mission pointers in a pinned base");
|
|
335
|
+
}
|
|
336
|
+
const { metadata } = reifyProfileMissions(terminal, current.log, graph, { missionIds: input.missions ?? [] });
|
|
319
337
|
const packaged = {
|
|
320
|
-
...
|
|
338
|
+
...terminal,
|
|
321
339
|
metadata: {
|
|
322
340
|
...metadata,
|
|
323
341
|
properties: {
|
|
@@ -327,10 +345,8 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
327
345
|
},
|
|
328
346
|
},
|
|
329
347
|
};
|
|
330
|
-
const ref =
|
|
331
|
-
|
|
332
|
-
const bundle = bundleForRevision(ref, contents);
|
|
333
|
-
resolvePatchBundle(bundle);
|
|
348
|
+
const ref = { identity: input.patch.identity, digest: store.add(packaged) };
|
|
349
|
+
const bundle = await extractSparseClosure(ref, store, captureReading);
|
|
334
350
|
if (!(await confirm({
|
|
335
351
|
kind: "hub.publish",
|
|
336
352
|
title: "Publish " + input.title + " " + input.version,
|
|
@@ -351,7 +367,7 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
351
367
|
throw new Error("Unknown Mission");
|
|
352
368
|
const { entry, position } = revision;
|
|
353
369
|
const { mission } = entry;
|
|
354
|
-
const { bundle, skills } = missionBundle(current.
|
|
370
|
+
const { bundle, skills } = await missionBundle(current.graph, mission, title, {
|
|
355
371
|
missionId: mission.id,
|
|
356
372
|
logEntryId: entry.id,
|
|
357
373
|
logPosition: position,
|
|
@@ -400,7 +416,7 @@ export function connectHub(profile, theories, confirm, createMission, options =
|
|
|
400
416
|
const download = await call("export_profile", {});
|
|
401
417
|
return new Uint8Array(await (await fetched(download.url)).arrayBuffer());
|
|
402
418
|
});
|
|
403
|
-
const imported = validate(unpack(archive, theories), theories);
|
|
419
|
+
const imported = await validate(await unpack(archive, theories), theories);
|
|
404
420
|
const backup = profile.dir + ".backup-" + randomUUID();
|
|
405
421
|
if (!(await confirm({
|
|
406
422
|
kind: "hub.import",
|
package/dist/learning.js
CHANGED
|
@@ -329,7 +329,7 @@ export class LearningHost {
|
|
|
329
329
|
for (const target of mission.targets) {
|
|
330
330
|
if (!view.graph.nodes.some(({ id }) => id === target.node))
|
|
331
331
|
throw new Error("Mission target is not in the graph");
|
|
332
|
-
const source = await this.runtime.
|
|
332
|
+
const source = await this.runtime.readPatch(target.provenance, view.profile);
|
|
333
333
|
if (!source.graph.nodes.some(({ id }) => id === target.node))
|
|
334
334
|
throw new Error("Invalid Mission target provenance");
|
|
335
335
|
}
|
package/dist/packed.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ContentDigest, type ExactPatchRef, type PatchChunk, type PatchContent, type PatchManifest, type SparsePatchBundle } from "@mmon-co/ncept-core";
|
|
2
|
+
import type { ProfileFiles } from "./tar.js";
|
|
3
|
+
export declare class PackedPatches {
|
|
4
|
+
readonly manifests: Map<`sha256:${string}`, PatchManifest>;
|
|
5
|
+
readonly chunks: Map<`sha256:${string}`, PatchChunk>;
|
|
6
|
+
static fromFiles(files: ProfileFiles): PackedPatches;
|
|
7
|
+
getManifest: (d: ContentDigest) => Promise<PatchManifest | undefined>;
|
|
8
|
+
getChunk: (d: ContentDigest) => Promise<PatchChunk | undefined>;
|
|
9
|
+
add(content: PatchContent): `sha256:${string}`;
|
|
10
|
+
content(ref: ExactPatchRef): PatchContent;
|
|
11
|
+
reading(terminal: ExactPatchRef): Promise<{
|
|
12
|
+
patches: SparsePatchBundle;
|
|
13
|
+
graph: import("@mmon-co/ncept-core").ResolvedGraph;
|
|
14
|
+
personal: PatchContent;
|
|
15
|
+
}>;
|
|
16
|
+
dense(ref: ExactPatchRef): import("@mmon-co/ncept-core").ClosedPatchBundle;
|
|
17
|
+
}
|
|
18
|
+
export declare function packedFiles(parts: {
|
|
19
|
+
manifests: Iterable<SparsePatchBundle["manifests"][number]>;
|
|
20
|
+
chunks: Iterable<SparsePatchBundle["chunks"][number]>;
|
|
21
|
+
}): Map<string, Uint8Array<ArrayBufferLike>>;
|
package/dist/packed.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { chunkDigest, isPatchChunk, isPatchManifest, manifestDigest, packPatchContent, extractSparseClosure, sparseNotebookSelection, unpackPatchContent, verifySparseBundle, notebookBundleForRevision, } from "@mmon-co/ncept-core";
|
|
2
|
+
export class PackedPatches {
|
|
3
|
+
manifests = new Map();
|
|
4
|
+
chunks = new Map();
|
|
5
|
+
static fromFiles(files) {
|
|
6
|
+
const store = new PackedPatches();
|
|
7
|
+
for (const [path, bytes] of files) {
|
|
8
|
+
if (!path.startsWith("manifests/") && !path.startsWith("chunks/"))
|
|
9
|
+
continue;
|
|
10
|
+
const value = JSON.parse(new TextDecoder().decode(bytes));
|
|
11
|
+
if (path.startsWith("manifests/") && isPatchManifest(value)) {
|
|
12
|
+
const digest = manifestDigest(value);
|
|
13
|
+
if (path === "manifests/" + digest + ".json") {
|
|
14
|
+
store.manifests.set(digest, value);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (path.startsWith("chunks/") && isPatchChunk(value)) {
|
|
19
|
+
const digest = chunkDigest(value);
|
|
20
|
+
if (path === "chunks/" + digest + ".json") {
|
|
21
|
+
store.chunks.set(digest, value);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
throw new Error("Patch content digest mismatch: " + path);
|
|
26
|
+
}
|
|
27
|
+
return store;
|
|
28
|
+
}
|
|
29
|
+
getManifest = async (d) => this.manifests.get(d);
|
|
30
|
+
getChunk = async (d) => this.chunks.get(d);
|
|
31
|
+
add(content) {
|
|
32
|
+
const { manifest, chunks } = packPatchContent(content);
|
|
33
|
+
const digest = manifestDigest(manifest);
|
|
34
|
+
this.manifests.set(digest, manifest);
|
|
35
|
+
for (const [d, c] of chunks)
|
|
36
|
+
this.chunks.set(d, c);
|
|
37
|
+
return digest;
|
|
38
|
+
}
|
|
39
|
+
content(ref) {
|
|
40
|
+
const manifest = this.manifests.get(ref.digest);
|
|
41
|
+
if (!manifest)
|
|
42
|
+
throw new Error("missing Patch manifest: " + ref.digest);
|
|
43
|
+
return unpackPatchContent(manifest, (d) => this.chunks.get(d));
|
|
44
|
+
}
|
|
45
|
+
async reading(terminal) {
|
|
46
|
+
const manifest = this.manifests.get(terminal.digest);
|
|
47
|
+
if (!manifest)
|
|
48
|
+
throw new Error("missing Patch manifest: " + terminal.digest);
|
|
49
|
+
const options = {
|
|
50
|
+
selection: sparseNotebookSelection(manifest),
|
|
51
|
+
completeTerminal: true,
|
|
52
|
+
availablePlaygrounds: new Set(["local"]),
|
|
53
|
+
};
|
|
54
|
+
const patches = await extractSparseClosure(terminal, this, options);
|
|
55
|
+
const { graph } = await verifySparseBundle(patches, options);
|
|
56
|
+
return { patches, graph, personal: this.content(terminal) };
|
|
57
|
+
}
|
|
58
|
+
dense(ref) {
|
|
59
|
+
const contents = new Map();
|
|
60
|
+
const visit = (r) => {
|
|
61
|
+
if (contents.has(r.digest))
|
|
62
|
+
return;
|
|
63
|
+
const content = this.content(r);
|
|
64
|
+
contents.set(r.digest, content);
|
|
65
|
+
content.bases.forEach(visit);
|
|
66
|
+
};
|
|
67
|
+
visit(ref);
|
|
68
|
+
return notebookBundleForRevision(ref, contents);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export function packedFiles(parts) {
|
|
72
|
+
const files = new Map();
|
|
73
|
+
for (const [folder, entries] of [
|
|
74
|
+
["manifests", parts.manifests],
|
|
75
|
+
["chunks", parts.chunks],
|
|
76
|
+
])
|
|
77
|
+
for (const [digest, value] of entries)
|
|
78
|
+
files.set(folder + "/" + digest + ".json", new TextEncoder().encode(JSON.stringify(value) + "\n"));
|
|
79
|
+
return files;
|
|
80
|
+
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ChatContext, type
|
|
1
|
+
import { type ChatContext, type ExactPatchRef, type NodeId, type Observation, type PatchContent, type PlatformEvent, type ProfileLogEntry, type Program, type SurfaceId, type Theory, type Transcript, type TranscriptSpan } from "@mmon-co/ncept-core";
|
|
2
2
|
import type { ProfileDirectory } from "./directory.js";
|
|
3
3
|
import type { HostSession, Model } from "./host.js";
|
|
4
4
|
import type { ValidatedProfile } from "./exchange.js";
|
|
@@ -30,7 +30,13 @@ export declare class Runtime<S> {
|
|
|
30
30
|
now: string;
|
|
31
31
|
state: S;
|
|
32
32
|
observations: readonly Observation<unknown>[];
|
|
33
|
-
bundle: import("@mmon-co/ncept-core").
|
|
33
|
+
bundle: import("@mmon-co/ncept-core").SparsePatchBundle;
|
|
34
|
+
personal: PatchContent;
|
|
35
|
+
}>;
|
|
36
|
+
/** A published Patch is a reading; only a Personal revision must retain its entire authored delta. */
|
|
37
|
+
readPatch(ref: ExactPatchRef, profile: ValidatedProfile): Promise<{
|
|
38
|
+
bundle: import("@mmon-co/ncept-core").SparsePatchBundle;
|
|
39
|
+
graph: import("@mmon-co/ncept-core").ResolvedGraph;
|
|
34
40
|
}>;
|
|
35
41
|
observations(profile: ValidatedProfile, now: string): readonly Observation[];
|
|
36
42
|
prepare<T>(program: Program<T>, session?: LocalSession, snapshot?: Awaited<ReturnType<Runtime<S>["snapshot"]>>): Promise<{
|
|
@@ -44,21 +50,24 @@ export declare class Runtime<S> {
|
|
|
44
50
|
}>;
|
|
45
51
|
context(session: LocalSession, view?: Awaited<ReturnType<Runtime<S>["snapshot"]>>): Promise<ChatContext<S>>;
|
|
46
52
|
prospective(content: PatchContent): Promise<{
|
|
53
|
+
personal: PatchContent;
|
|
47
54
|
profile: {
|
|
48
|
-
patches: import("@mmon-co/ncept-core").ClosedPatchBundle;
|
|
49
55
|
manifest: {
|
|
50
56
|
personal: ExactPatchRef;
|
|
51
|
-
format:
|
|
52
|
-
log?: ContentDigest;
|
|
57
|
+
format: 2;
|
|
58
|
+
log?: import("@mmon-co/ncept-core").ContentDigest;
|
|
53
59
|
theory: import("@mmon-co/ncept-core").TheoryRequirement;
|
|
54
60
|
theories: readonly string[];
|
|
55
61
|
sessions: Readonly<Record<string, string>>;
|
|
56
62
|
};
|
|
57
|
-
|
|
63
|
+
patches: import("@mmon-co/ncept-core").SparsePatchBundle;
|
|
64
|
+
graph: import("@mmon-co/ncept-core").ResolvedGraph;
|
|
65
|
+
personal: PatchContent;
|
|
66
|
+
blobs: ReadonlyMap<import("@mmon-co/ncept-core").ContentDigest, Uint8Array>;
|
|
58
67
|
sessions: ReadonlyMap<string, string>;
|
|
59
68
|
log: readonly ProfileLogEntry[];
|
|
60
69
|
};
|
|
61
|
-
bundle: import("@mmon-co/ncept-core").
|
|
70
|
+
bundle: import("@mmon-co/ncept-core").SparsePatchBundle;
|
|
62
71
|
graph: import("@mmon-co/ncept-core").ResolvedGraph;
|
|
63
72
|
observations: readonly Observation<unknown>[];
|
|
64
73
|
state: S;
|
package/dist/runtime.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { candidateRevision, effectiveObservations, extractSparseClosure, resolveSparseBundle, isObservation, readTheoryState, resolveTheory, sameExactPatchRef, } from "@mmon-co/ncept-core";
|
|
2
2
|
import { object, parsePiSession, piActiveBranch } from "./sessions.js";
|
|
3
3
|
import { learnerDayAt } from "./days.js";
|
|
4
4
|
import { isMaterial, isObjectIdentity, isSurface, modeEntry, nodeIds, nonempty } from "./inputs.js";
|
|
5
|
+
import { PackedPatches } from "./packed.js";
|
|
5
6
|
export function platform(payload, surface) {
|
|
6
7
|
return {
|
|
7
8
|
id: `obs-${crypto.randomUUID()}`,
|
|
@@ -113,20 +114,10 @@ export class Runtime {
|
|
|
113
114
|
async snapshot(ref) {
|
|
114
115
|
const profile = await this.profile.read();
|
|
115
116
|
const theory = resolveTheory(profile.manifest.theory, this.theories);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
for (const [path, bytes] of files)
|
|
121
|
-
if (path.startsWith("patches/")) {
|
|
122
|
-
const digest = path.slice(8, -5);
|
|
123
|
-
if (!isContentDigest(digest))
|
|
124
|
-
throw new Error("Invalid revision filename");
|
|
125
|
-
contents.set(digest, JSON.parse(new TextDecoder().decode(bytes)));
|
|
126
|
-
}
|
|
127
|
-
bundle = notebookBundleForRevision(ref, contents);
|
|
128
|
-
}
|
|
129
|
-
const graph = resolvePatchBundle(bundle, new Set(["local"]), notebookSelection(bundle));
|
|
117
|
+
const reading = ref && !sameExactPatchRef(ref, profile.manifest.personal)
|
|
118
|
+
? await PackedPatches.fromFiles(await this.profile.files()).reading(ref)
|
|
119
|
+
: profile;
|
|
120
|
+
const { patches: bundle, graph, personal } = reading;
|
|
130
121
|
const now = new Date().toISOString();
|
|
131
122
|
const observations = this.observations(profile, now);
|
|
132
123
|
return {
|
|
@@ -137,8 +128,21 @@ export class Runtime {
|
|
|
137
128
|
state: readTheoryState(theory, graph, observations, now),
|
|
138
129
|
observations,
|
|
139
130
|
bundle,
|
|
131
|
+
personal,
|
|
140
132
|
};
|
|
141
133
|
}
|
|
134
|
+
/** A published Patch is a reading; only a Personal revision must retain its entire authored delta. */
|
|
135
|
+
async readPatch(ref, profile) {
|
|
136
|
+
if (sameExactPatchRef(ref, profile.manifest.personal))
|
|
137
|
+
return { bundle: profile.patches, graph: profile.graph };
|
|
138
|
+
const store = PackedPatches.fromFiles(await this.profile.files());
|
|
139
|
+
if (ref.identity === profile.manifest.personal.identity) {
|
|
140
|
+
const { patches, graph } = await store.reading(ref);
|
|
141
|
+
return { bundle: patches, graph };
|
|
142
|
+
}
|
|
143
|
+
const bundle = await extractSparseClosure(ref, store);
|
|
144
|
+
return { bundle, graph: await resolveSparseBundle(bundle) };
|
|
145
|
+
}
|
|
142
146
|
observations(profile, now) {
|
|
143
147
|
const standing = new Map();
|
|
144
148
|
for (const entry of profile.log) {
|
|
@@ -161,7 +165,7 @@ export class Runtime {
|
|
|
161
165
|
learnerDay: learnerDayAt(observation.at, this.timezone),
|
|
162
166
|
...(object(observation.payload) && object(observation.payload.material)
|
|
163
167
|
? {
|
|
164
|
-
materialPresent:
|
|
168
|
+
materialPresent: profile.graph.materials.some((material) => object(observation.payload) &&
|
|
165
169
|
object(observation.payload.material) &&
|
|
166
170
|
material.id === observation.payload.material.id &&
|
|
167
171
|
material.playground === observation.payload.material.playground),
|
|
@@ -241,7 +245,7 @@ export class Runtime {
|
|
|
241
245
|
const select = request.select;
|
|
242
246
|
switch (select.kind) {
|
|
243
247
|
case "patch": {
|
|
244
|
-
const patch = select.ref ? await this.
|
|
248
|
+
const patch = select.ref ? await this.readPatch(select.ref, view.profile) : view;
|
|
245
249
|
sent = { patch: patch.graph.terminal, graph: patch.graph };
|
|
246
250
|
break;
|
|
247
251
|
}
|
|
@@ -311,18 +315,19 @@ export class Runtime {
|
|
|
311
315
|
async prospective(content) {
|
|
312
316
|
const view = await this.snapshot();
|
|
313
317
|
const revision = candidateRevision(view.graph.terminal.identity, content);
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
const
|
|
317
|
-
const
|
|
318
|
+
const store = PackedPatches.fromFiles(await this.profile.files());
|
|
319
|
+
store.add(content);
|
|
320
|
+
const reading = await store.reading(revision.ref);
|
|
321
|
+
const { patches: bundle, graph } = reading;
|
|
318
322
|
const profile = {
|
|
319
323
|
...view.profile,
|
|
320
|
-
|
|
324
|
+
...reading,
|
|
321
325
|
manifest: { ...view.profile.manifest, personal: revision.ref },
|
|
322
326
|
};
|
|
323
327
|
const observations = this.observations(profile, view.now);
|
|
324
328
|
return {
|
|
325
329
|
...view,
|
|
330
|
+
personal: content,
|
|
326
331
|
profile,
|
|
327
332
|
bundle,
|
|
328
333
|
graph,
|
|
@@ -332,9 +337,6 @@ export class Runtime {
|
|
|
332
337
|
}
|
|
333
338
|
async content() {
|
|
334
339
|
const view = await this.snapshot();
|
|
335
|
-
|
|
336
|
-
if (!revision)
|
|
337
|
-
throw new Error("Missing personal revision");
|
|
338
|
-
return revisionContent(revision);
|
|
340
|
+
return view.personal;
|
|
339
341
|
}
|
|
340
342
|
}
|