dsh-mnemon-source-memory-spaces 0.5.14 → 0.5.15

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.
Files changed (2) hide show
  1. package/lib/index.js +64 -31
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -348,6 +348,8 @@ var MemorySpaceRegistry = class MemorySpaceRegistry {
348
348
  let normalizedService = this.providerCatalog.normalizeService(providerId, service);
349
349
  const seen = /* @__PURE__ */ new Set();
350
350
  const existing = this.spaces.filter((body) => body.providerId === providerId);
351
+ const existingByExternalId = /* @__PURE__ */ new Map();
352
+ for (const body of existing) if (!existingByExternalId.has(body.externalId)) existingByExternalId.set(body.externalId, body);
351
353
  const reservedIds = new Set(this.spaces.filter((body) => body.providerId !== providerId).map((body) => body.id));
352
354
  const timestamp = this.now().toISOString();
353
355
  const projections = discovered.map((candidate) => {
@@ -359,7 +361,7 @@ var MemorySpaceRegistry = class MemorySpaceRegistry {
359
361
  ...normalizedService,
360
362
  ...connection
361
363
  });
362
- const previous = existing.find((body) => body.externalId === externalId);
364
+ const previous = existingByExternalId.get(externalId);
363
365
  let id = previous?.id ?? validateMemorySpaceId(`${providerId}-${createHash("sha256").update(externalId).digest("hex").slice(0, 24)}`);
364
366
  let suffix = 1;
365
367
  while (reservedIds.has(id)) {
@@ -756,9 +758,10 @@ var MemorySpaceRegistry = class MemorySpaceRegistry {
756
758
  const timestamp = this.now().toISOString();
757
759
  const legacyActive = this.runner.effectiveStore();
758
760
  let changed = false;
761
+ const knownIds = new Set(this.spaces.map((body) => body.id));
759
762
  for (const entry of readdirSync(this.directory, { withFileTypes: true })) {
760
763
  if (!entry.isDirectory() || !ID_PATTERN.test(entry.name) || !existsSync(join(this.directory, entry.name, "mnemon.db"))) continue;
761
- if (this.spaces.some((body) => body.id === entry.name)) continue;
764
+ if (knownIds.has(entry.name)) continue;
762
765
  this.spaces.push({
763
766
  id: entry.name,
764
767
  name: entry.name,
@@ -768,6 +771,7 @@ var MemorySpaceRegistry = class MemorySpaceRegistry {
768
771
  createdAt: timestamp,
769
772
  updatedAt: timestamp
770
773
  });
774
+ knownIds.add(entry.name);
771
775
  changed = true;
772
776
  }
773
777
  if (changed) this.save();
@@ -1797,22 +1801,36 @@ var MemorySpacesService = class {
1797
1801
  }
1798
1802
  const { hints, quality } = evaluation;
1799
1803
  const selected = recoveryPlan === void 0 ? quality.selected : prioritizeRecoveryEvidence(quality.selected, recoveryPlan);
1800
- const qualityStats = (memoryBodyId) => {
1801
- const evaluated = quality.evaluated.filter((candidate) => candidate.candidate.memoryBodyId === memoryBodyId);
1802
- const selected = quality.selected.filter((candidate) => candidate.candidate.memoryBodyId === memoryBodyId);
1803
- return {
1804
- policyId: quality.policyId,
1805
- ...quality.fallbackFrom === void 0 ? {} : { fallbackFrom: quality.fallbackFrom },
1806
- fetched: evaluated.length,
1807
- retained: evaluated.filter((candidate) => candidate.decision.action === "keep").length,
1808
- selected: selected.length,
1809
- droppedLowScore: evaluated.filter((candidate) => candidate.decision.action === "drop" && candidate.decision.reason === "low-score").length,
1810
- droppedNonPositiveScore: evaluated.filter((candidate) => candidate.decision.action === "drop" && candidate.decision.reason === "non-positive-score").length,
1811
- droppedInvalidScore: evaluated.filter((candidate) => candidate.decision.action === "drop" && candidate.decision.reason === "invalid-score").length,
1812
- unscored: evaluated.filter((candidate) => candidate.decision.reason === "unscored").length,
1813
- unscaled: evaluated.filter((candidate) => candidate.decision.reason === "unscaled-score").length
1814
- };
1815
- };
1804
+ const qualityStats = /* @__PURE__ */ new Map();
1805
+ for (const { body } of batches) qualityStats.set(body.id, {
1806
+ policyId: quality.policyId,
1807
+ ...quality.fallbackFrom === void 0 ? {} : { fallbackFrom: quality.fallbackFrom },
1808
+ fetched: 0,
1809
+ retained: 0,
1810
+ selected: 0,
1811
+ droppedLowScore: 0,
1812
+ droppedNonPositiveScore: 0,
1813
+ droppedInvalidScore: 0,
1814
+ unscored: 0,
1815
+ unscaled: 0
1816
+ });
1817
+ for (const { candidate, decision } of quality.evaluated) {
1818
+ const stats = qualityStats.get(candidate.memoryBodyId);
1819
+ if (stats === void 0) continue;
1820
+ stats.fetched += 1;
1821
+ if (decision.action === "keep") stats.retained += 1;
1822
+ if (decision.action === "drop") {
1823
+ if (decision.reason === "low-score") stats.droppedLowScore += 1;
1824
+ if (decision.reason === "non-positive-score") stats.droppedNonPositiveScore += 1;
1825
+ if (decision.reason === "invalid-score") stats.droppedInvalidScore += 1;
1826
+ }
1827
+ if (decision.reason === "unscored") stats.unscored += 1;
1828
+ if (decision.reason === "unscaled-score") stats.unscaled += 1;
1829
+ }
1830
+ for (const { candidate } of quality.selected) {
1831
+ const stats = qualityStats.get(candidate.memoryBodyId);
1832
+ if (stats !== void 0) stats.selected += 1;
1833
+ }
1816
1834
  return {
1817
1835
  query,
1818
1836
  mode,
@@ -1822,7 +1840,7 @@ var MemorySpacesService = class {
1822
1840
  ...decision.normalizedScore === void 0 ? {} : { normalizedScore: decision.normalizedScore }
1823
1841
  })),
1824
1842
  sources: batches.map((batch) => {
1825
- const stats = qualityStats(batch.body.id);
1843
+ const stats = qualityStats.get(batch.body.id);
1826
1844
  if (batch.source.status === "unavailable" || batch.source.status === "unsupported") return {
1827
1845
  ...batch.source,
1828
1846
  quality: stats
@@ -2044,13 +2062,21 @@ var MemorySpacesService = class {
2044
2062
  */
2045
2063
  async rememberMany(requests, signal) {
2046
2064
  this.assertWritable();
2047
- const prepared = requests.map((request) => this.prepareRemember(request));
2065
+ const destinations = /* @__PURE__ */ new Map();
2066
+ const prepared = requests.map((request) => this.prepareRemember(request, destinations));
2048
2067
  const results = new Array(prepared.length);
2049
2068
  const groups = /* @__PURE__ */ new Map();
2050
- for (const [index, entry] of prepared.entries()) groups.set(entry.body.id, [...groups.get(entry.body.id) ?? [], {
2051
- ...entry,
2052
- index
2053
- }]);
2069
+ for (const [index, entry] of prepared.entries()) {
2070
+ const group = groups.get(entry.body.id);
2071
+ if (group === void 0) groups.set(entry.body.id, [{
2072
+ ...entry,
2073
+ index
2074
+ }]);
2075
+ else group.push({
2076
+ ...entry,
2077
+ index
2078
+ });
2079
+ }
2054
2080
  for (const group of groups.values()) {
2055
2081
  const body = group[0].body;
2056
2082
  const provider = this.providerFor(body);
@@ -2066,8 +2092,9 @@ var MemorySpacesService = class {
2066
2092
  providerChanged ||= mutationResultCommitted(result);
2067
2093
  }
2068
2094
  }
2095
+ const batched = new Set(batch);
2069
2096
  for (const entry of group) {
2070
- if (batch.includes(entry)) continue;
2097
+ if (batched.has(entry)) continue;
2071
2098
  const result = await provider.remember(body, entry.request, signal);
2072
2099
  results[entry.index] = this.annotateResult(result, body);
2073
2100
  providerChanged ||= mutationResultCommitted(result);
@@ -2273,10 +2300,14 @@ var MemorySpacesService = class {
2273
2300
  return provider;
2274
2301
  }
2275
2302
  readSpaces(ids) {
2276
- const active = this.memorySpaces.active();
2277
- if (ids === void 0 || ids.length === 0) return active;
2278
- return [...new Set(ids.map((id) => id.trim()).filter((id) => id !== ""))].map((id) => {
2279
- const body = this.memorySpaces.get(id);
2303
+ if (ids === void 0 || ids.length === 0) return this.memorySpaces.active();
2304
+ const requested = [...new Set(ids.map((id) => id.trim()).filter((id) => id !== ""))];
2305
+ const available = /* @__PURE__ */ new Map();
2306
+ for (const body of this.memorySpaces.list()) if (!available.has(body.id)) available.set(body.id, body);
2307
+ return requested.map((id) => {
2308
+ const normalized = validateMemorySpaceId(id);
2309
+ const body = available.get(normalized);
2310
+ if (body === void 0) throw new Error(`unknown memory space: ${normalized}`);
2280
2311
  if (!body.active) throw new Error(`memory space is not active for reading: ${id}`);
2281
2312
  if (!this.isNativeSpace(body) && !this.memorySpaces.providerServiceEnabled(body.provider.id)) throw new Error(`${body.provider.label} is disabled on the dsh-mnemon page under Plugins`);
2282
2313
  return body;
@@ -2303,8 +2334,10 @@ var MemorySpacesService = class {
2303
2334
  if (active.length !== 1) throw new Error("memoryBodyId is required when the number of active memory spaces is not exactly one");
2304
2335
  return active[0];
2305
2336
  }
2306
- prepareRemember(request) {
2307
- const body = this.writeSpace(request.memoryBodyId);
2337
+ prepareRemember(request, destinations) {
2338
+ const key = request.memoryBodyId?.trim() ?? "";
2339
+ const body = destinations?.get(key) ?? this.writeSpace(request.memoryBodyId);
2340
+ destinations?.set(key, body);
2308
2341
  const content = required(request.content, "content", 8192);
2309
2342
  const importance = memoryInputInteger(request.importance, 3, 1, 5);
2310
2343
  const category = allowed(request.category, CATEGORIES$1, "category") ?? "general";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mnemon-source-memory-spaces",
3
- "version": "0.5.14",
3
+ "version": "0.5.15",
4
4
  "description": "Memory Spaces Source and its private Provider SDK.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -65,10 +65,10 @@
65
65
  "@types/node": "^22.0.0",
66
66
  "@types/react": "^18.3.0",
67
67
  "@types/react-dom": "^18.3.0",
68
- "dsh-mnemon": "^0.5.21",
68
+ "dsh-mnemon": "^0.5.23",
69
69
  "dsh-mnemon-provider-byterover": "^0.5.5",
70
70
  "dsh-mnemon-provider-hindsight": "^0.5.5",
71
- "dsh-mnemon-provider-holographic": "^0.5.5",
71
+ "dsh-mnemon-provider-holographic": "^0.5.6",
72
72
  "dsh-mnemon-provider-honcho": "^0.5.5",
73
73
  "dsh-mnemon-provider-mem0": "^0.5.5",
74
74
  "dsh-mnemon-provider-mnemon-native": "^0.5.8",