@appsoftwareltd/etherpk-mcp 0.31.0 → 0.33.0

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/main.js CHANGED
@@ -820,7 +820,7 @@ function looksLikeAgentSetupCode(value) {
820
820
  }
821
821
  var package_default = {
822
822
  name: "@appsoftwareltd/etherpk-mcp",
823
- version: "0.31.0",
823
+ version: "0.33.0",
824
824
  license: "Elastic-2.0",
825
825
  description: "EtherPK Headless Client: an MCP server over a synced knowledge graph, run beside the agent on the user's own machine.",
826
826
  type: "module",
@@ -10887,6 +10887,8 @@ var RelayUnansweredError = class extends Error {};
10887
10887
  function createGraphSync(deps) {
10888
10888
  const engines = /* @__PURE__ */ new Map();
10889
10889
  const retained = /* @__PURE__ */ new Map([[deps.rootDocId, 1]]);
10890
+ /** Documents watched for edits (`watchDocs`), by how many watches hold each. */
10891
+ const watched = /* @__PURE__ */ new Map();
10890
10892
  const retiring = /* @__PURE__ */ new Set();
10891
10893
  /**
10892
10894
  * Batch walks holding an engine: one count per `seedDocsFromCache` or `readyDocs` not yet
@@ -11146,13 +11148,28 @@ function createGraphSync(deps) {
11146
11148
  * reconnect.
11147
11149
  */
11148
11150
  function subscribeRetained() {
11149
- const docIds = [...retained.keys()];
11151
+ const docIds = [.../* @__PURE__ */ new Set([...retained.keys(), ...watched.keys()])];
11150
11152
  const batchSize = SYNC_PROTOCOL_LIMITS.maxSubscriptionDocIds;
11151
11153
  for (let start = 0; start < docIds.length; start += batchSize) sendNow({
11152
11154
  type: "subscribe",
11153
11155
  docIds: docIds.slice(start, start + batchSize)
11154
11156
  });
11155
11157
  }
11158
+ /** Subscribe to documents, or unsubscribe, in batches the relay accepts. Closed, the next open subscribes. */
11159
+ function sendSubscription(type, docIds) {
11160
+ if (!open || docIds.length === 0) return;
11161
+ const batchSize = SYNC_PROTOCOL_LIMITS.maxSubscriptionDocIds;
11162
+ for (let start = 0; start < docIds.length; start += batchSize) {
11163
+ const batch = docIds.slice(start, start + batchSize);
11164
+ rawSend(type === "subscribe" ? {
11165
+ type: "subscribe",
11166
+ docIds: batch
11167
+ } : {
11168
+ type: "unsubscribe",
11169
+ docIds: batch
11170
+ });
11171
+ }
11172
+ }
11156
11173
  function pumpCatchups() {
11157
11174
  if (!open || !socket || catchupInFlight) return;
11158
11175
  catchupInFlight = foregroundCatchups.shift() ?? backgroundCatchups.shift();
@@ -11222,6 +11239,8 @@ function createGraphSync(deps) {
11222
11239
  sendNow(message);
11223
11240
  }
11224
11241
  const readied = /* @__PURE__ */ new Map();
11242
+ /** Documents a `catchUpDoc` is waiting on: their catch-up goes ahead of background walks. */
11243
+ const urgentCatchups = /* @__PURE__ */ new Map();
11225
11244
  const syncEnabled = /* @__PURE__ */ new Set();
11226
11245
  function activate(docId, e) {
11227
11246
  readied.set(docId, e.ready());
@@ -11352,7 +11371,7 @@ function createGraphSync(deps) {
11352
11371
  onIdle: () => queueMicrotask(() => retireEngineIfIdle(docId)),
11353
11372
  onQueueChange: () => noteQueue(docId),
11354
11373
  catchupPriority: () => {
11355
- return retained.has(docId) ? "foreground" : "background";
11374
+ return retained.has(docId) || urgentCatchups.has(docId) ? "foreground" : "background";
11356
11375
  }
11357
11376
  });
11358
11377
  engines.set(docId, e);
@@ -11544,7 +11563,16 @@ function createGraphSync(deps) {
11544
11563
  engine(message.docId).receive(message).catch(report).finally(() => finishCatchup(message.requestId));
11545
11564
  return;
11546
11565
  }
11547
- engine(message.docId).receive(message).catch(report);
11566
+ engineForPushed(message.docId).receive(message).catch(report);
11567
+ }
11568
+ /**
11569
+ * The engine a relay message for `docId` goes to. An edit to a watched document nobody shows
11570
+ * makes one to take it in; it goes once idle, as an engine a read made does, and the watch's
11571
+ * subscription brings the next edit.
11572
+ */
11573
+ function engineForPushed(docId) {
11574
+ if (!engines.has(docId) && watched.has(docId) && !retained.has(docId)) retiring.add(docId);
11575
+ return engine(docId);
11548
11576
  }
11549
11577
  /**
11550
11578
  * Forget everything that belonged to one socket generation. Durable IndexedDB rows, not these
@@ -11732,7 +11760,7 @@ function createGraphSync(deps) {
11732
11760
  if (open) retainedEngine.advertisePresence().catch(reportError);
11733
11761
  return;
11734
11762
  }
11735
- rawSend({
11763
+ if (!watched.has(docId)) rawSend({
11736
11764
  type: "unsubscribe",
11737
11765
  docIds: [docId]
11738
11766
  });
@@ -11740,6 +11768,35 @@ function createGraphSync(deps) {
11740
11768
  });
11741
11769
  };
11742
11770
  },
11771
+ watchDocs(docIds) {
11772
+ const ids = [...new Set(docIds)].filter((docId) => docId !== deps.rootDocId);
11773
+ const started = [];
11774
+ for (const docId of ids) {
11775
+ const count = watched.get(docId) ?? 0;
11776
+ watched.set(docId, count + 1);
11777
+ if (count === 0 && !retained.has(docId)) started.push(docId);
11778
+ }
11779
+ sendSubscription("subscribe", started);
11780
+ let released = false;
11781
+ return () => {
11782
+ if (released || disposed) return;
11783
+ released = true;
11784
+ const ended = [];
11785
+ for (const docId of ids) {
11786
+ const next = (watched.get(docId) ?? 1) - 1;
11787
+ if (next > 0) {
11788
+ watched.set(docId, next);
11789
+ continue;
11790
+ }
11791
+ watched.delete(docId);
11792
+ if (retained.has(docId)) continue;
11793
+ ended.push(docId);
11794
+ retiring.add(docId);
11795
+ retireEngineIfIdle(docId);
11796
+ }
11797
+ sendSubscription("unsubscribe", ended);
11798
+ };
11799
+ },
11743
11800
  deleteDoc: (docId) => engine(docId).delete(),
11744
11801
  compactDoc: (docId) => deps.cache.docCache(docId).compact(),
11745
11802
  uploadSnapshot: (docId) => engine(docId).compact(),
@@ -12141,6 +12198,20 @@ function createGraphSync(deps) {
12141
12198
  retireDocs: releaseBatch,
12142
12199
  firstCatchupPageDoc: (docId) => engine(docId).firstCatchupPage(),
12143
12200
  caughtUpDoc: (docId) => engine(docId).caughtUp(),
12201
+ async catchUpDoc(docId) {
12202
+ const e = engine(docId);
12203
+ await (readied.get(docId) ?? Promise.resolve());
12204
+ if (disposed || !open || engines.get(docId) !== e) return;
12205
+ urgentCatchups.set(docId, (urgentCatchups.get(docId) ?? 0) + 1);
12206
+ try {
12207
+ e.resync();
12208
+ await e.caughtUp();
12209
+ } finally {
12210
+ const left = (urgentCatchups.get(docId) ?? 1) - 1;
12211
+ if (left > 0) urgentCatchups.set(docId, left);
12212
+ else urgentCatchups.delete(docId);
12213
+ }
12214
+ },
12144
12215
  async docsNeedingCatchup(docIds) {
12145
12216
  const changed = [];
12146
12217
  for (let start = 0; start < docIds.length; start += SYNC_PROTOCOL_LIMITS.maxSubscriptionDocIds) {
@@ -25821,36 +25892,56 @@ function isDocumentFile(name) {
25821
25892
  function fileKey(subdir, fileName) {
25822
25893
  return `${subdir}/${fileName}`;
25823
25894
  }
25895
+ /** A file's text, or null when it is no longer there. Any other failure is the caller's. */
25896
+ async function readUnlessGone(adapter, subdir, name) {
25897
+ try {
25898
+ return (await adapter.read(subdir, name)).text;
25899
+ } catch (error) {
25900
+ if (error?.name === "NotFoundError") return null;
25901
+ throw error;
25902
+ }
25903
+ }
25904
+ /**
25905
+ * The entry for one listed document file: the last scan's, when the listing's stamps have not moved
25906
+ * (or a save of ours is writing it), else read from the file. Null for a file removed since the
25907
+ * listing (a delete or a rename finishing while a reconcile scans): it is gone.
25908
+ */
25909
+ async function entryFor(context, subdir, kind, listed) {
25910
+ const { name, lastModified, size } = listed;
25911
+ const known = context.previous.get(fileKey(subdir, name));
25912
+ if (known && (known.lastModified === lastModified && known.size === size || context.writeInFlight(subdir, name))) return known;
25913
+ const text = await readUnlessGone(context.adapter, subdir, name);
25914
+ if (text === null) return null;
25915
+ const fm = parseFrontmatter(text);
25916
+ const concept = kind === "journal" ? journalConceptOf(name) : conceptOf$1(fm, fileStem(name));
25917
+ return {
25918
+ kind,
25919
+ concept,
25920
+ key: conceptKey(concept),
25921
+ subdir,
25922
+ fileName: name,
25923
+ aliases: aliasesOf(fm),
25924
+ lastModified,
25925
+ size
25926
+ };
25927
+ }
25824
25928
  async function scanGraph(adapter, options = {}) {
25825
25929
  const previous = /* @__PURE__ */ new Map();
25826
25930
  for (const entry of options.previous ?? []) previous.set(fileKey(entry.subdir, entry.fileName), entry);
25827
- const writeInFlight = options.writeInFlight ?? (() => false);
25931
+ const context = {
25932
+ adapter,
25933
+ previous,
25934
+ writeInFlight: options.writeInFlight ?? (() => false)
25935
+ };
25828
25936
  const journals = [];
25829
25937
  const pages = [];
25830
25938
  for (const subdir of SCANNED_SUBDIRS) {
25831
25939
  const kind = documentKindOf(subdir);
25832
25940
  if (!kind) continue;
25833
- for (const { name, lastModified, size } of await adapter.list(subdir)) {
25834
- if (!isDocumentFile(name)) continue;
25835
- const known = previous.get(fileKey(subdir, name));
25836
- if (known !== void 0 && (known.lastModified === lastModified && known.size === size || writeInFlight(subdir, name))) {
25837
- (kind === "journal" ? journals : pages).push(known);
25838
- continue;
25839
- }
25840
- const { text } = await adapter.read(subdir, name);
25841
- const fm = parseFrontmatter(text);
25842
- const concept = kind === "journal" ? journalConceptOf(name) : conceptOf$1(fm, fileStem(name));
25843
- const entry = {
25844
- kind,
25845
- concept,
25846
- key: conceptKey(concept),
25847
- subdir,
25848
- fileName: name,
25849
- aliases: aliasesOf(fm),
25850
- lastModified,
25851
- size
25852
- };
25853
- (kind === "journal" ? journals : pages).push(entry);
25941
+ for (const listed of await adapter.list(subdir)) {
25942
+ if (!isDocumentFile(listed.name)) continue;
25943
+ const entry = await entryFor(context, subdir, kind, listed);
25944
+ if (entry) (kind === "journal" ? journals : pages).push(entry);
25854
25945
  }
25855
25946
  }
25856
25947
  journals.sort((a, b) => b.concept.localeCompare(a.concept));
@@ -27459,6 +27550,12 @@ function createServerDocumentStore(graph, options) {
27459
27550
  const health = graph.docSync(docId).health();
27460
27551
  if (health === "key-unavailable" || health === "ciphertext-corrupt") throw new DocumentSyncDegradedError(target, health);
27461
27552
  },
27553
+ async whenCurrent(target) {
27554
+ const docId = docIdFor(target);
27555
+ if (!docId) return;
27556
+ await this.whenReady(target);
27557
+ await reachedWithin(graph.catchUpDoc(docId), Math.max(0, options.readyTimeoutMs ?? 2e3));
27558
+ },
27462
27559
  getAwareness(target) {
27463
27560
  const docId = docIdFor(target);
27464
27561
  return docId ? graph.docSync(docId).awareness : void 0;
@@ -27467,6 +27564,10 @@ function createServerDocumentStore(graph, options) {
27467
27564
  const docId = docIdFor(target);
27468
27565
  return docId ? graph.retainDoc(docId) : () => {};
27469
27566
  },
27567
+ watchDocuments(targets) {
27568
+ const docIds = targets.map((target) => docIdFor(target)).filter((docId) => docId !== void 0);
27569
+ return graph.watchDocs(docIds);
27570
+ },
27470
27571
  async scan() {
27471
27572
  await graph.ready();
27472
27573
  if (graph.isConnected() || await new Promise((resolve) => {