@appsoftwareltd/etherpk-mcp 0.33.0 → 0.34.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.33.0",
823
+ version: "0.34.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",
@@ -17212,36 +17212,76 @@ function createAssetStore(adapter) {
17212
17212
  * is what this did before — stored the same bytes twice whenever the stem differed.
17213
17213
  */
17214
17214
  let contentIndex;
17215
- async function assetsByContent() {
17216
- if (contentIndex) return contentIndex;
17215
+ /**
17216
+ * The index, listed once. The promise is what is kept, not the map: saves run several at once
17217
+ * (an upload's pool), and each one that found no map yet used to list the folder into a map of
17218
+ * its own. The last listing to finish replaced the rest, and every write recorded in the others
17219
+ * was forgotten, so adding the same files again stored and reported most of them as new.
17220
+ */
17221
+ function assetsByContent() {
17222
+ contentIndex ??= listByContent().catch((err) => {
17223
+ contentIndex = void 0;
17224
+ throw err;
17225
+ });
17226
+ return contentIndex;
17227
+ }
17228
+ async function listByContent() {
17217
17229
  const index = /* @__PURE__ */ new Map();
17218
17230
  for (const { name } of await adapter.list("assets")) {
17219
17231
  const parsed = assetHashFromName(name);
17220
17232
  if (parsed && !index.has(contentKey(parsed.hash, parsed.ext))) index.set(contentKey(parsed.hash, parsed.ext), name);
17221
17233
  }
17222
- contentIndex = index;
17223
17234
  return index;
17224
17235
  }
17236
+ /**
17237
+ * Content key → the save storing those bytes right now. Two saves of the same bytes at once (a
17238
+ * file dropped twice in one batch) both missed the index and wrote the file twice, under each
17239
+ * name. The second waits for the first and references its file instead.
17240
+ */
17241
+ const storing = /* @__PURE__ */ new Map();
17242
+ /** The file holding `bytes` under `key`: one the folder already has, or one written now. */
17243
+ async function store(key, name, hash, bytes) {
17244
+ const index = await assetsByContent();
17245
+ const held = index.get(key);
17246
+ if (held !== void 0 && await adapter.exists("assets", held)) return {
17247
+ fileName: held,
17248
+ reused: true
17249
+ };
17250
+ const fileName = assetFileName(name, hash);
17251
+ await adapter.writeBinary("assets", fileName, bytes);
17252
+ index.set(key, fileName);
17253
+ return {
17254
+ fileName,
17255
+ reused: false
17256
+ };
17257
+ }
17225
17258
  return {
17226
17259
  async save({ name, bytes }, onBytes) {
17227
17260
  const hash = await contentHash(bytes);
17228
17261
  const { stem, ext } = splitNameExt(name);
17229
- const index = await assetsByContent();
17230
17262
  const key = contentKey(hash, ext);
17231
- const held = index.get(key);
17232
- const existing = held !== void 0 && await adapter.exists("assets", held) ? held : void 0;
17233
- const fileName = existing ?? assetFileName(name, hash);
17234
- if (!existing) {
17235
- await adapter.writeBinary("assets", fileName, bytes);
17236
- index.set(key, fileName);
17263
+ let stored;
17264
+ const pending = storing.get(key);
17265
+ if (pending) stored = await pending.then((fileName) => ({
17266
+ fileName,
17267
+ reused: true
17268
+ }), () => store(key, name, hash, bytes));
17269
+ else {
17270
+ const work = store(key, name, hash, bytes);
17271
+ storing.set(key, work.then((done) => done.fileName));
17272
+ try {
17273
+ stored = await work;
17274
+ } finally {
17275
+ storing.delete(key);
17276
+ }
17237
17277
  }
17238
17278
  onBytes?.(bytes.length);
17239
17279
  return {
17240
- ref: `../assets/${fileName}`,
17241
- name: fileName,
17280
+ ref: `../assets/${stored.fileName}`,
17281
+ name: stored.fileName,
17242
17282
  stem: publishSlug(stem) || "asset",
17243
17283
  isImage: isImageExt(ext),
17244
- ...existing ? { reused: true } : {}
17284
+ ...stored.reused ? { reused: true } : {}
17245
17285
  };
17246
17286
  },
17247
17287
  async readBytes(ref) {
@@ -17294,7 +17334,7 @@ function createAssetStore(adapter) {
17294
17334
  if (url) URL.revokeObjectURL(url);
17295
17335
  urlCache.delete(from);
17296
17336
  const parsed = assetHashFromName(to);
17297
- if (parsed) contentIndex?.set(contentKey(parsed.hash, parsed.ext), to);
17337
+ if (parsed) contentIndex?.then((index) => index.set(contentKey(parsed.hash, parsed.ext), to)).catch(() => {});
17298
17338
  }
17299
17339
  };
17300
17340
  }