@mmon-co/ncept-local 0.3.11 → 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/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 ContentDigest, 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";
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";
@@ -6,6 +6,7 @@ export interface LocalSession extends HostSession {
6
6
  readonly surface: SurfaceId;
7
7
  readonly about: readonly NodeId[];
8
8
  readonly closed: boolean;
9
+ readonly closedAt?: string;
9
10
  /** Identifies a pending close; resuming the Session clears it. */
10
11
  readonly closing?: string;
11
12
  readonly mission?: string;
@@ -29,7 +30,13 @@ export declare class Runtime<S> {
29
30
  now: string;
30
31
  state: S;
31
32
  observations: readonly Observation<unknown>[];
32
- bundle: import("@mmon-co/ncept-core").ClosedPatchBundle;
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;
33
40
  }>;
34
41
  observations(profile: ValidatedProfile, now: string): readonly Observation[];
35
42
  prepare<T>(program: Program<T>, session?: LocalSession, snapshot?: Awaited<ReturnType<Runtime<S>["snapshot"]>>): Promise<{
@@ -43,21 +50,24 @@ export declare class Runtime<S> {
43
50
  }>;
44
51
  context(session: LocalSession, view?: Awaited<ReturnType<Runtime<S>["snapshot"]>>): Promise<ChatContext<S>>;
45
52
  prospective(content: PatchContent): Promise<{
53
+ personal: PatchContent;
46
54
  profile: {
47
- patches: import("@mmon-co/ncept-core").ClosedPatchBundle;
48
55
  manifest: {
49
56
  personal: ExactPatchRef;
50
- format: 1;
51
- log?: ContentDigest;
57
+ format: 2;
58
+ log?: import("@mmon-co/ncept-core").ContentDigest;
52
59
  theory: import("@mmon-co/ncept-core").TheoryRequirement;
53
60
  theories: readonly string[];
54
61
  sessions: Readonly<Record<string, string>>;
55
62
  };
56
- blobs: ReadonlyMap<ContentDigest, Uint8Array>;
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>;
57
67
  sessions: ReadonlyMap<string, string>;
58
68
  log: readonly ProfileLogEntry[];
59
69
  };
60
- bundle: import("@mmon-co/ncept-core").ClosedPatchBundle;
70
+ bundle: import("@mmon-co/ncept-core").SparsePatchBundle;
61
71
  graph: import("@mmon-co/ncept-core").ResolvedGraph;
62
72
  observations: readonly Observation<unknown>[];
63
73
  state: S;
package/dist/runtime.js CHANGED
@@ -1,7 +1,8 @@
1
- import { notebookBundleForRevision, notebookSelection, candidateRevision, effectiveObservations, isObservation, isContentDigest, readTheoryState, resolvePatchBundle, resolveTheory, revisionContent, sameExactPatchRef, } from "@mmon-co/ncept-core";
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()}`,
@@ -26,7 +27,7 @@ export function sessionStates(profile) {
26
27
  const states = new Map();
27
28
  for (const entry of profile.log) {
28
29
  const payload = localPayload(entry, "local.session");
29
- if (payload && object(payload.session)) {
30
+ if (payload && isObservation(entry) && object(payload.session)) {
30
31
  const session = payload.session;
31
32
  if (!nonempty(session.id) ||
32
33
  !isSurface(session.surface) ||
@@ -38,6 +39,7 @@ export function sessionStates(profile) {
38
39
  surface: session.surface,
39
40
  mode: session.mode,
40
41
  closed: session.closed,
42
+ ...(session.closed ? { closedAt: entry.at } : {}),
41
43
  ...(typeof session.closing === "string" ? { closing: session.closing } : {}),
42
44
  about: nodeIds(session.about),
43
45
  ...(session.entry === undefined ? {} : { entry: modeEntry(session.entry) }),
@@ -112,20 +114,10 @@ export class Runtime {
112
114
  async snapshot(ref) {
113
115
  const profile = await this.profile.read();
114
116
  const theory = resolveTheory(profile.manifest.theory, this.theories);
115
- let bundle = profile.patches;
116
- if (ref && !sameExactPatchRef(ref, bundle.terminal)) {
117
- const files = await this.profile.files();
118
- const contents = new Map();
119
- for (const [path, bytes] of files)
120
- if (path.startsWith("patches/")) {
121
- const digest = path.slice(8, -5);
122
- if (!isContentDigest(digest))
123
- throw new Error("Invalid revision filename");
124
- contents.set(digest, JSON.parse(new TextDecoder().decode(bytes)));
125
- }
126
- bundle = notebookBundleForRevision(ref, contents);
127
- }
128
- 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;
129
121
  const now = new Date().toISOString();
130
122
  const observations = this.observations(profile, now);
131
123
  return {
@@ -136,8 +128,21 @@ export class Runtime {
136
128
  state: readTheoryState(theory, graph, observations, now),
137
129
  observations,
138
130
  bundle,
131
+ personal,
139
132
  };
140
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
+ }
141
146
  observations(profile, now) {
142
147
  const standing = new Map();
143
148
  for (const entry of profile.log) {
@@ -160,7 +165,7 @@ export class Runtime {
160
165
  learnerDay: learnerDayAt(observation.at, this.timezone),
161
166
  ...(object(observation.payload) && object(observation.payload.material)
162
167
  ? {
163
- materialPresent: resolvePatchBundle(profile.patches, undefined, notebookSelection(profile.patches)).materials.some((material) => object(observation.payload) &&
168
+ materialPresent: profile.graph.materials.some((material) => object(observation.payload) &&
164
169
  object(observation.payload.material) &&
165
170
  material.id === observation.payload.material.id &&
166
171
  material.playground === observation.payload.material.playground),
@@ -240,7 +245,7 @@ export class Runtime {
240
245
  const select = request.select;
241
246
  switch (select.kind) {
242
247
  case "patch": {
243
- const patch = select.ref ? await this.snapshot(select.ref) : view;
248
+ const patch = select.ref ? await this.readPatch(select.ref, view.profile) : view;
244
249
  sent = { patch: patch.graph.terminal, graph: patch.graph };
245
250
  break;
246
251
  }
@@ -310,18 +315,19 @@ export class Runtime {
310
315
  async prospective(content) {
311
316
  const view = await this.snapshot();
312
317
  const revision = candidateRevision(view.graph.terminal.identity, content);
313
- const contents = new Map(view.bundle.revisions.map((revision) => [revision.ref.digest, revisionContent(revision)]));
314
- contents.set(revision.ref.digest, content);
315
- const bundle = notebookBundleForRevision(revision.ref, contents);
316
- const graph = resolvePatchBundle(bundle, new Set(["local"]), notebookSelection(bundle));
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;
317
322
  const profile = {
318
323
  ...view.profile,
319
- patches: bundle,
324
+ ...reading,
320
325
  manifest: { ...view.profile.manifest, personal: revision.ref },
321
326
  };
322
327
  const observations = this.observations(profile, view.now);
323
328
  return {
324
329
  ...view,
330
+ personal: content,
325
331
  profile,
326
332
  bundle,
327
333
  graph,
@@ -331,9 +337,6 @@ export class Runtime {
331
337
  }
332
338
  async content() {
333
339
  const view = await this.snapshot();
334
- const revision = view.bundle.revisions.find(({ ref }) => sameExactPatchRef(ref, view.bundle.terminal));
335
- if (!revision)
336
- throw new Error("Missing personal revision");
337
- return revisionContent(revision);
340
+ return view.personal;
338
341
  }
339
342
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmon-co/ncept-local",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,16 +32,16 @@
32
32
  }
33
33
  },
34
34
  "scripts": {
35
- "build": "bun run --cwd ../core build && tsc -p tsconfig.build.json",
35
+ "build": "bun run --cwd ../default-theory build && tsc -p tsconfig.build.json",
36
36
  "prepack": "bun run build"
37
37
  },
38
38
  "dependencies": {
39
39
  "@mmon-co/ncept-core": "^0.4.0",
40
+ "@mmon-co/ncept-default-theory": "^3.0.0",
40
41
  "@modelcontextprotocol/sdk": "1.32.1",
41
42
  "proper-lockfile": "4.1.2"
42
43
  },
43
44
  "devDependencies": {
44
- "@mmon-co/ncept-default-theory": "^3.0.0",
45
45
  "@types/proper-lockfile": "^4.1.4",
46
46
  "bun-types": "^1.3.14",
47
47
  "typescript": "6.0.3"