@theokit/sdk-memory 0.5.3 → 0.6.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/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- export { createInMemoryMarkdownProvider } from "./in-memory-provider.js";
1
+ export { createInMemoryMarkdownProvider, type InMemoryMarkdownProviderOptions, } from "./in-memory-provider.js";
2
2
  export * from "./internal/active-memory/active-memory.js";
3
3
  export * from "./internal/circuit-breaker.js";
4
4
  export * from "./internal/embedding/embedding-adapter.js";
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { createInMemoryMarkdownProvider } from "./in-memory-provider.js";
1
+ export { createInMemoryMarkdownProvider, type InMemoryMarkdownProviderOptions, } from "./in-memory-provider.js";
2
2
  export * from "./internal/active-memory/active-memory.js";
3
3
  export * from "./internal/circuit-breaker.js";
4
4
  export * from "./internal/embedding/embedding-adapter.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;AAczE,cAAc,2CAA2C,CAAC;AAO1D,cAAc,+BAA+B,CAAC;AAO9C,cAAc,2CAA2C,CAAC;AAkB1D,cAAc,iDAAiD,CAAC;AAOhE,cAAc,iDAAiD,CAAC;AAChE,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AAGjD,cAAc,kCAAkC,CAAC;AAWjD,cAAc,uCAAuC,CAAC;AAQtD,cAAc,wCAAwC,CAAC;AAgBvD,cAAc,qCAAqC,CAAC;AAEpD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,0CAA0C,CAAC;AACzD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,0CAA0C,CAAC;AACzD,cAAc,wCAAwC,CAAC;AACvD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,0CAA0C,CAAC;AAuBzD,cAAc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zD,cAAc,wCAAwC,CAAC;AACvD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,0CAA0C,CAAC;AAuBzD,cAAc,2CAA2C,CAAC;AAmB1D,cAAc,0CAA0C,CAAC;AACzD,cAAc,8CAA8C,CAAC;AAC7D,cAAc,0CAA0C,CAAC;AAezD,cAAc,8BAA8B,CAAC;AAiB7C,cAAc,mCAAmC,CAAC;AAOlD,cAAc,4CAA4C,CAAC;AAW3D,cAAc,4CAA4C,CAAC;AAK3D,cAAc,kCAAkC,CAAC;AAajD,cAAc,iCAAiC,CAAC;AAchD,cAAc,0CAA0C,CAAC;AAWzD,cAAc,kCAAkC,CAAC;AAiBjD,cAAc,6CAA6C,CAAC;AAc5D,cAAc,+BAA+B,CAAC;AAQ9C,cAAc,uCAAuC,CAAC;AActD,cAAc,+BAA+B,CAAC;AAW9C,cAAc,4BAA4B,CAAC;AAQ3C,cAAc,oCAAoC,CAAC;AAanD,cAAc,oCAAoC,CAAC;AAUnD,cAAc,4BAA4B,CAAC;AAU3C,cAAc,oCAAoC,CAAC;AAanD,cAAc,4CAA4C,CAAC;AAY3D,cAAc,sCAAsC,CAAC;AASrD,cAAc,iCAAiC,CAAC;AAehD,cAAc,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { mkdir, readdir, readFile, stat, access, unlink } from 'fs/promises';
2
2
  import { join, resolve } from 'path';
3
- import { Security } from '@theokit/sdk';
3
+ import { Security, ConfigurationError as ConfigurationError$1 } from '@theokit/sdk';
4
+ import { resolveMaxFacts, notesDir, resolveMemoryRoot, persistActiveMemoryTranscript, readFactsFromMarkdown, appendDiaryEntry, lanceStoragePath, defaultIndexPath, projectMemoryDir, collectMarkdownFiles, readAllSqliteFacts, memoryMdPath, appendFactToMarkdown, ceilingReachedError } from '@theokit/sdk/internal/memory-store';
5
+ export { MEMORY_INDEX_MAX_BYTES, MEMORY_INDEX_MAX_LINES, appendDiaryEntry, appendFact, appendFactToMarkdown, asMemoryRoot, ceilingReachedError, claudeProjectMemoryDir, collectMarkdownFiles, defaultIndexPath, diaryPath, discoverSessionFiles, discoverWikiFiles, entryHash, indexBudgetWarning, lanceStoragePath, memoryIndexRoot, memoryMdPath, memoryReadRoots, notesDir, persistActiveMemoryTranscript, projectMemoryDir, readAllSqliteFacts, readFacts, readFactsFromMarkdown, renderDiaryEntry, resolveMaxFacts, resolveMemoryRoot, sessionSummaryPath, sessionsDir, wikiDir, writeSessionSummary } from '@theokit/sdk/internal/memory-store';
4
6
  import { replaceFileAtomic, withCwdMutex, openSqliteResilient, sanitizeFts5Query } from '@theokit/sdk/persistence';
5
- import { notesDir, resolveMemoryRoot, persistActiveMemoryTranscript, readFactsFromMarkdown, appendDiaryEntry, lanceStoragePath, defaultIndexPath, projectMemoryDir, collectMarkdownFiles, readAllSqliteFacts, memoryMdPath, appendFactToMarkdown } from '@theokit/sdk/internal/memory-store';
6
- export { MEMORY_INDEX_MAX_BYTES, MEMORY_INDEX_MAX_LINES, appendDiaryEntry, appendFact, appendFactToMarkdown, asMemoryRoot, claudeProjectMemoryDir, collectMarkdownFiles, defaultIndexPath, diaryPath, discoverSessionFiles, discoverWikiFiles, entryHash, indexBudgetWarning, lanceStoragePath, memoryIndexRoot, memoryMdPath, memoryReadRoots, notesDir, persistActiveMemoryTranscript, projectMemoryDir, readAllSqliteFacts, readFacts, readFactsFromMarkdown, renderDiaryEntry, resolveMemoryRoot, sessionSummaryPath, sessionsDir, wikiDir, writeSessionSummary } from '@theokit/sdk/internal/memory-store';
7
7
  import { createHash } from 'crypto';
8
8
  import { createOpenAiCompatibleRuntime } from '@theokit/sdk/internal/memory-adapters';
9
9
  export { createOpenAiCompatibleRuntime } from '@theokit/sdk/internal/memory-adapters';
@@ -14,6 +14,7 @@ import { createRequire } from 'module';
14
14
 
15
15
  // src/in-memory-provider.ts
16
16
  var ADAPTER_ID = "in-memory-md";
17
+ var IN_MEMORY_REMEDY = "merge or delete facts with the adapter's delete(id), or raise maxFacts";
17
18
  var INTERNAL_STATE = /* @__PURE__ */ Symbol("sdk-memory.in-memory.state");
18
19
  function buildCapabilities() {
19
20
  return {
@@ -25,6 +26,19 @@ function buildCapabilities() {
25
26
  prefetch: false
26
27
  };
27
28
  }
29
+ function admitNewFact(state) {
30
+ if (state.maxFacts !== void 0 && state.facts.size >= state.maxFacts) {
31
+ throw ceilingReachedError(
32
+ "the in-memory store",
33
+ state.facts.size,
34
+ state.maxFacts,
35
+ IN_MEMORY_REMEDY
36
+ );
37
+ }
38
+ }
39
+ function isCeilingRefusal(cause) {
40
+ return cause instanceof ConfigurationError$1 && cause.code === "memory_ceiling_reached";
41
+ }
28
42
  function buildAdapter(state) {
29
43
  return {
30
44
  id: ADAPTER_ID,
@@ -35,6 +49,7 @@ function buildAdapter(state) {
35
49
  async write(content, _ctx) {
36
50
  const text = typeof content === "string" ? content : content.map((m) => m.content).join("\n");
37
51
  const safeText = Security.redact(text);
52
+ admitNewFact(state);
38
53
  const id = `${ADAPTER_ID}:${state.counter.next++}`;
39
54
  state.facts.set(id, {
40
55
  id,
@@ -180,6 +195,12 @@ function buildMemoryRememberTool(state) {
180
195
  return JSON.stringify({ ok: false, error: "empty content" });
181
196
  }
182
197
  const safeContent = Security.redact(content);
198
+ try {
199
+ admitNewFact(state);
200
+ } catch (cause) {
201
+ if (!isCeilingRefusal(cause)) throw cause;
202
+ return JSON.stringify({ ok: false, code: cause.code, error: cause.message });
203
+ }
183
204
  const id = `${ADAPTER_ID}:${state.counter.next++}`;
184
205
  state.facts.set(id, {
185
206
  id,
@@ -190,11 +211,13 @@ function buildMemoryRememberTool(state) {
190
211
  }
191
212
  };
192
213
  }
193
- function createInMemoryMarkdownProvider() {
214
+ function createInMemoryMarkdownProvider(options = {}) {
215
+ const maxFacts = resolveMaxFacts(options.maxFacts);
194
216
  return {
195
217
  async init(opts) {
196
218
  const state = {
197
219
  facts: /* @__PURE__ */ new Map(),
220
+ maxFacts,
198
221
  counter: { next: 0 },
199
222
  cwd: opts.cwd
200
223
  };
@@ -1096,12 +1119,15 @@ async function lightPhase(facts, embedding, threshold = DEFAULT_DEDUP_THRESHOLD)
1096
1119
  }
1097
1120
  var DEFAULT_MAX_FACTS_PER_SWEEP = 500;
1098
1121
  async function remPhase(facts, embedding, threshold = DEFAULT_CLUSTER_THRESHOLD, maxFactsPerSweep = DEFAULT_MAX_FACTS_PER_SWEEP) {
1099
- if (facts.length === 0) return { clusters: [] };
1122
+ if (facts.length === 0) return { clusters: [], factsLeftOut: 0 };
1100
1123
  const capped = facts.length > maxFactsPerSweep ? facts.slice(0, maxFactsPerSweep) : facts;
1101
1124
  const vectors = await embedding.embed(capped.map((f) => f.text));
1102
1125
  const clusterOfIdx = unionFindByPairs(vectors, threshold);
1103
1126
  const groups = bucketFactsByClusterRoot(capped, clusterOfIdx);
1104
- return { clusters: [...groups.values()].map(buildClusterFromMembers) };
1127
+ return {
1128
+ clusters: [...groups.values()].map(buildClusterFromMembers),
1129
+ factsLeftOut: facts.length - capped.length
1130
+ };
1105
1131
  }
1106
1132
  function unionFindByPairs(vectors, threshold) {
1107
1133
  const clusterOfIdx = vectors.map((_, i) => i);
@@ -1194,7 +1220,8 @@ async function runInner(options) {
1194
1220
  duplicatesRemoved: dedup.duplicatesRemoved,
1195
1221
  clustersCreated: rem.clusters.length,
1196
1222
  notesWritten,
1197
- diaryEntryHash: void 0
1223
+ diaryEntryHash: void 0,
1224
+ factsLeftOut: rem.factsLeftOut
1198
1225
  };
1199
1226
  await appendDiaryEntry(resolveMemoryRoot(options.cwd), {
1200
1227
  timestampMs,
@@ -1223,7 +1250,7 @@ async function writeConsolidatedNotes(cwd, clusters, timestampMs) {
1223
1250
  return 1;
1224
1251
  }
1225
1252
  function emptyResult(status) {
1226
- return {
1253
+ const result = {
1227
1254
  status,
1228
1255
  factsBefore: 0,
1229
1256
  factsAfter: 0,
@@ -1232,6 +1259,7 @@ function emptyResult(status) {
1232
1259
  notesWritten: 0,
1233
1260
  diaryEntryHash: void 0
1234
1261
  };
1262
+ return status === "skipped" ? { ...result, factsLeftOut: 0 } : result;
1235
1263
  }
1236
1264
 
1237
1265
  // src/internal/index/index-schema.ts