@equationalapplications/core-llm-wiki 4.18.1 → 4.20.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.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as MemoryBundle, F as FormatContextOptions, G as GraphNeighborhood, a as MemoryDump, b as FormattedMemoryDump, R as ReadOptions, S as SQLiteAdapter, W as WikiOptions, c as WikiMemory } from './testing-BfxdUHg9.mjs';
2
- export { E as EntityStatus, d as ExtractedFact, e as ExtractedFactEdge, f as ExtractedFactWithOntology, g as ExtractedTask, h as GraphTraversalOptions, H as HOOK_TIMEOUT_MARKER, L as LLMProvider, O as OntologyConfig, i as OntologyEdgeType, j as OntologyManifest, k as OntologyMode, l as OntologyNodeType, m as OntologyPromptContext, n as OntologyUpdates, P as PromptOverrides, o as PromptService, p as PrunePartialFailureError, V as VectorRanker, q as VectorRankerFallback, r as VectorRankerRankArgs, s as VectorRankerSemanticResult, t as WikiBusyError, u as WikiBusyOperation, v as WikiCheckpoint, w as WikiConfig, x as WikiEdge, y as WikiEvent, z as WikiFact, A as WikiMemoryTestAccess, B as WikiOutboxEvent, C as WikiTask } from './testing-BfxdUHg9.mjs';
1
+ import { M as MemoryBundle, F as FormatContextOptions, G as GraphNeighborhood, a as MemoryDump, b as FormattedMemoryDump, R as ReadOptions, S as SQLiteAdapter, W as WikiOptions, c as WikiMemory } from './testing-BVN_g414.mjs';
2
+ export { E as EntityStatus, d as ExtractedFact, e as ExtractedFactEdge, f as ExtractedFactWithOntology, g as ExtractedTask, h as GraphTraversalOptions, H as HOOK_TIMEOUT_MARKER, L as LLMProvider, O as OntologyConfig, i as OntologyEdgeType, j as OntologyManifest, k as OntologyMode, l as OntologyNodeType, m as OntologyPromptContext, n as OntologyUpdates, P as PromptOverrides, o as PromptService, p as PrunePartialFailureError, V as VectorRanker, q as VectorRankerFallback, r as VectorRankerRankArgs, s as VectorRankerSemanticResult, t as WikiBusyError, u as WikiBusyOperation, v as WikiCheckpoint, w as WikiConfig, x as WikiEdge, y as WikiEvent, z as WikiFact, A as WikiMemoryTestAccess, B as WikiOutboxEvent, C as WikiTask, D as WikiTransactionError } from './testing-BVN_g414.mjs';
3
3
  import { OkfFile } from '@equationalapplications/core-okf';
4
4
  import 'minisearch';
5
5
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as MemoryBundle, F as FormatContextOptions, G as GraphNeighborhood, a as MemoryDump, b as FormattedMemoryDump, R as ReadOptions, S as SQLiteAdapter, W as WikiOptions, c as WikiMemory } from './testing-BfxdUHg9.js';
2
- export { E as EntityStatus, d as ExtractedFact, e as ExtractedFactEdge, f as ExtractedFactWithOntology, g as ExtractedTask, h as GraphTraversalOptions, H as HOOK_TIMEOUT_MARKER, L as LLMProvider, O as OntologyConfig, i as OntologyEdgeType, j as OntologyManifest, k as OntologyMode, l as OntologyNodeType, m as OntologyPromptContext, n as OntologyUpdates, P as PromptOverrides, o as PromptService, p as PrunePartialFailureError, V as VectorRanker, q as VectorRankerFallback, r as VectorRankerRankArgs, s as VectorRankerSemanticResult, t as WikiBusyError, u as WikiBusyOperation, v as WikiCheckpoint, w as WikiConfig, x as WikiEdge, y as WikiEvent, z as WikiFact, A as WikiMemoryTestAccess, B as WikiOutboxEvent, C as WikiTask } from './testing-BfxdUHg9.js';
1
+ import { M as MemoryBundle, F as FormatContextOptions, G as GraphNeighborhood, a as MemoryDump, b as FormattedMemoryDump, R as ReadOptions, S as SQLiteAdapter, W as WikiOptions, c as WikiMemory } from './testing-BVN_g414.js';
2
+ export { E as EntityStatus, d as ExtractedFact, e as ExtractedFactEdge, f as ExtractedFactWithOntology, g as ExtractedTask, h as GraphTraversalOptions, H as HOOK_TIMEOUT_MARKER, L as LLMProvider, O as OntologyConfig, i as OntologyEdgeType, j as OntologyManifest, k as OntologyMode, l as OntologyNodeType, m as OntologyPromptContext, n as OntologyUpdates, P as PromptOverrides, o as PromptService, p as PrunePartialFailureError, V as VectorRanker, q as VectorRankerFallback, r as VectorRankerRankArgs, s as VectorRankerSemanticResult, t as WikiBusyError, u as WikiBusyOperation, v as WikiCheckpoint, w as WikiConfig, x as WikiEdge, y as WikiEvent, z as WikiFact, A as WikiMemoryTestAccess, B as WikiOutboxEvent, C as WikiTask, D as WikiTransactionError } from './testing-BVN_g414.js';
3
3
  import { OkfFile } from '@equationalapplications/core-okf';
4
4
  import 'minisearch';
5
5
 
package/dist/index.js CHANGED
@@ -117,6 +117,127 @@ async function setupDatabase(db, prefix) {
117
117
  `);
118
118
  }
119
119
 
120
+ // src/db/sqliteCodes.ts
121
+ var SQLITE_RESULT_CODE_NAMES = {
122
+ 1: "SQLITE_ERROR",
123
+ 2: "SQLITE_INTERNAL",
124
+ 3: "SQLITE_PERM",
125
+ 4: "SQLITE_ABORT",
126
+ 5: "SQLITE_BUSY",
127
+ 6: "SQLITE_LOCKED",
128
+ 7: "SQLITE_NOMEM",
129
+ 8: "SQLITE_READONLY",
130
+ 9: "SQLITE_INTERRUPT",
131
+ 10: "SQLITE_IOERR",
132
+ 11: "SQLITE_CORRUPT",
133
+ 12: "SQLITE_NOTFOUND",
134
+ 13: "SQLITE_FULL",
135
+ 14: "SQLITE_CANTOPEN",
136
+ 15: "SQLITE_PROTOCOL",
137
+ 16: "SQLITE_EMPTY",
138
+ 17: "SQLITE_SCHEMA",
139
+ 18: "SQLITE_TOOBIG",
140
+ 19: "SQLITE_CONSTRAINT",
141
+ 20: "SQLITE_MISMATCH",
142
+ 21: "SQLITE_MISUSE",
143
+ 22: "SQLITE_NOLFS",
144
+ 23: "SQLITE_AUTH",
145
+ 24: "SQLITE_FORMAT",
146
+ 25: "SQLITE_RANGE",
147
+ 26: "SQLITE_NOTADB",
148
+ 27: "SQLITE_NOTICE",
149
+ 28: "SQLITE_WARNING",
150
+ 100: "SQLITE_ROW",
151
+ 101: "SQLITE_DONE"
152
+ };
153
+ function nameForSqliteResultCode(n) {
154
+ return SQLITE_RESULT_CODE_NAMES[n] ?? `SQLITE_${n}`;
155
+ }
156
+ function extractSqliteCode(err) {
157
+ if (typeof err !== "object" || err === null) return void 0;
158
+ const { code, message } = err;
159
+ if (typeof code === "string" && code.startsWith("SQLITE_")) return code;
160
+ if (typeof message === "string") {
161
+ const m = /^Error code (\d+):/.exec(message);
162
+ if (m) return nameForSqliteResultCode(Number(m[1]));
163
+ }
164
+ return void 0;
165
+ }
166
+
167
+ // src/types.ts
168
+ var WikiBusyError = class extends Error {
169
+ constructor(operation, entityId) {
170
+ super(`${operation} already running for entity ${entityId}`);
171
+ this.name = "WikiBusyError";
172
+ this.operation = operation;
173
+ this.entityId = entityId;
174
+ }
175
+ };
176
+ var WikiTransactionError = class extends Error {
177
+ constructor(message, options) {
178
+ super(message, options);
179
+ this.name = "WikiTransactionError";
180
+ this.sqliteErrorCode = extractSqliteCode(options.cause);
181
+ }
182
+ };
183
+ var PrunePartialFailureError = class extends Error {
184
+ constructor(deleted, failedAt, remaining, cause, deletedTasks = 0, deletedEvents = 0) {
185
+ super(`Prune partially failed: deleted ${deleted}, failed at ${failedAt}, ${remaining} remaining`);
186
+ this.name = "PrunePartialFailureError";
187
+ this.deleted = deleted;
188
+ this.failedAt = failedAt;
189
+ this.remaining = remaining;
190
+ this.deletedTasks = deletedTasks;
191
+ this.deletedEvents = deletedEvents;
192
+ this.cause = cause;
193
+ }
194
+ };
195
+ var HOOK_TIMEOUT_MARKER = /* @__PURE__ */ Symbol("WikiMemoryHookTimeout");
196
+
197
+ // src/db/serializedAdapter.ts
198
+ function isDriverError(err) {
199
+ return extractSqliteCode(err) !== void 0;
200
+ }
201
+ function withOverrides(target, overrides) {
202
+ return new Proxy(target, {
203
+ get(obj, prop, receiver) {
204
+ if (prop in overrides) return Reflect.get(overrides, prop, receiver);
205
+ const value = Reflect.get(obj, prop, obj);
206
+ return typeof value === "function" ? value.bind(obj) : value;
207
+ }
208
+ });
209
+ }
210
+ function guardReentrancy(tx) {
211
+ return withOverrides(tx, {
212
+ withTransactionAsync() {
213
+ throw new Error(
214
+ "Nested withTransactionAsync is not supported: you are already inside a transaction. Pass the current `tx` down instead of opening a new transaction."
215
+ );
216
+ }
217
+ });
218
+ }
219
+ var DEADLOCK_WARN_MS = 1e4;
220
+ function withSerializedTransactions(db) {
221
+ let queue = Promise.resolve();
222
+ return withOverrides(db, {
223
+ withTransactionAsync(fn) {
224
+ const warn = setTimeout(() => {
225
+ console.warn(
226
+ "[core-llm-wiki] Transaction queued >10s \u2014 possible deadlock. Inside a transaction callback, use the `tx` parameter, never the outer database handle."
227
+ );
228
+ }, DEADLOCK_WARN_MS);
229
+ const run = queue.catch(() => void 0).then(() => {
230
+ clearTimeout(warn);
231
+ return db.withTransactionAsync((tx) => fn(guardReentrancy(tx ?? db)));
232
+ }).catch((e) => {
233
+ throw isDriverError(e) ? new WikiTransactionError("Transaction failed", { cause: e }) : e;
234
+ });
235
+ queue = run;
236
+ return run;
237
+ }
238
+ });
239
+ }
240
+
120
241
  // src/db/migrations.ts
121
242
  var MIGRATIONS = [
122
243
  {
@@ -1643,6 +1764,9 @@ function validateInlineEdges(sourceType, _targetType, edges, manifest) {
1643
1764
  }
1644
1765
 
1645
1766
  // src/repositories/MetadataRepository.ts
1767
+ function entitySummaryMetaKey(entityId) {
1768
+ return `entity_summary:${entityId}`;
1769
+ }
1646
1770
  var MetadataRepository = class extends BaseRepository {
1647
1771
  // CHECKPOINTS TABLE METHODS
1648
1772
  async getCheckpoint(entityId, tx) {
@@ -1701,6 +1825,13 @@ var MetadataRepository = class extends BaseRepository {
1701
1825
  [key, value]
1702
1826
  );
1703
1827
  }
1828
+ async deleteMeta(key, tx) {
1829
+ const executor = this.getExecutor(tx);
1830
+ await executor.runAsync(
1831
+ `DELETE FROM ${this.prefix}meta WHERE key = ?`,
1832
+ [key]
1833
+ );
1834
+ }
1704
1835
  async clearDimensionMismatch(tx) {
1705
1836
  const executor = this.getExecutor(tx);
1706
1837
  await executor.runAsync(
@@ -2026,29 +2157,6 @@ _SearchService.MAX_VECTOR_CACHE_ENTITIES = 16;
2026
2157
  _SearchService.MAX_VECTOR_CACHE_FACTS_PER_ENTITY = 500;
2027
2158
  var SearchService = _SearchService;
2028
2159
 
2029
- // src/types.ts
2030
- var WikiBusyError = class extends Error {
2031
- constructor(operation, entityId) {
2032
- super(`${operation} already running for entity ${entityId}`);
2033
- this.name = "WikiBusyError";
2034
- this.operation = operation;
2035
- this.entityId = entityId;
2036
- }
2037
- };
2038
- var PrunePartialFailureError = class extends Error {
2039
- constructor(deleted, failedAt, remaining, cause, deletedTasks = 0, deletedEvents = 0) {
2040
- super(`Prune partially failed: deleted ${deleted}, failed at ${failedAt}, ${remaining} remaining`);
2041
- this.name = "PrunePartialFailureError";
2042
- this.deleted = deleted;
2043
- this.failedAt = failedAt;
2044
- this.remaining = remaining;
2045
- this.deletedTasks = deletedTasks;
2046
- this.deletedEvents = deletedEvents;
2047
- this.cause = cause;
2048
- }
2049
- };
2050
- var HOOK_TIMEOUT_MARKER = /* @__PURE__ */ Symbol("WikiMemoryHookTimeout");
2051
-
2052
2160
  // src/services/JobManager.ts
2053
2161
  var JobManager = class {
2054
2162
  constructor(prefix) {
@@ -2994,6 +3102,7 @@ var MaintenanceService = class {
2994
3102
  deletedEntries = await this.entryRepo.bulkSoftDeleteByEntityId(entityId, tx);
2995
3103
  deletedTasks = await this.taskRepo.bulkSoftDeleteByEntityId(entityId, tx);
2996
3104
  await this.metadataRepo.updateCheckpoint(entityId, { memory: 0, heal: 0 }, tx);
3105
+ await this.metadataRepo.deleteMeta(entitySummaryMetaKey(entityId), tx);
2997
3106
  } else {
2998
3107
  const hasIdSelectors = params.entryId !== void 0 || params.taskId !== void 0;
2999
3108
  const hasSourceSelectors = params.sourceRef !== void 0 || params.sourceHash !== void 0;
@@ -3342,11 +3451,12 @@ var ImportExportService = class {
3342
3451
  }
3343
3452
  }
3344
3453
  async getFullBundle(entityId, opts) {
3345
- const [factsRaw, tasks, events, edges] = await Promise.all([
3454
+ const [factsRaw, tasks, events, edges, summaryValue] = await Promise.all([
3346
3455
  opts?.includeBlobs ? this.entryRepo.findAllByEntityIdWithBlobs(entityId) : this.entryRepo.findAllByEntityId(entityId),
3347
3456
  this.taskRepo.findAllByEntityId(entityId),
3348
3457
  this.eventRepo.getByEntityId(entityId, opts?.maxEvents),
3349
- this.edgeRepo.getByEntityId(entityId)
3458
+ this.edgeRepo.getByEntityId(entityId),
3459
+ this.metadataRepo.getMeta(entitySummaryMetaKey(entityId))
3350
3460
  ]);
3351
3461
  const facts = factsRaw.map((f) => {
3352
3462
  const {
@@ -3365,7 +3475,13 @@ var ImportExportService = class {
3365
3475
  tags: typeof factBase.tags === "string" ? JSON.parse(factBase.tags) : factBase.tags
3366
3476
  };
3367
3477
  });
3368
- return { facts, tasks, events, edges };
3478
+ return {
3479
+ facts,
3480
+ tasks,
3481
+ events,
3482
+ edges,
3483
+ ...summaryValue != null ? { summary: summaryValue } : {}
3484
+ };
3369
3485
  }
3370
3486
  /** Single-entity import transaction + post-processing; package-internal hook for tests. */
3371
3487
  async doImportEntity(entityId, bundle, merge) {
@@ -3390,6 +3506,11 @@ var ImportExportService = class {
3390
3506
  await this.edgeRepo.bulkDeleteByEntityId(entityId, tx);
3391
3507
  await this.metadataRepo.deleteCheckpoint(entityId, tx);
3392
3508
  }
3509
+ if (bundle.summary !== void 0) {
3510
+ await this.metadataRepo.setMeta(entitySummaryMetaKey(entityId), bundle.summary, tx);
3511
+ } else if (!merge) {
3512
+ await this.metadataRepo.deleteMeta(entitySummaryMetaKey(entityId), tx);
3513
+ }
3393
3514
  const factIds = bundle.facts.map((fact) => fact.id);
3394
3515
  const existingFactsById = /* @__PURE__ */ new Map();
3395
3516
  const existingFacts = await this.entryRepo.findExistingMetadataByIds(
@@ -4624,7 +4745,7 @@ var WikiMemory = class {
4624
4745
  constructor(db, options) {
4625
4746
  /** Emits `__testAccess` console warning at most once per instance when NODE_ENV ≠ "test". */
4626
4747
  __privateAdd(this, _testAccessNonTestEnvWarned, false);
4627
- this.db = db;
4748
+ this.db = withSerializedTransactions(db);
4628
4749
  this.options = options;
4629
4750
  this.prefix = options.config?.tablePrefix ?? "llm_wiki_";
4630
4751
  if (!TABLE_PREFIX_PATTERN.test(this.prefix)) {
@@ -4632,12 +4753,12 @@ var WikiMemory = class {
4632
4753
  `Invalid tablePrefix: ${JSON.stringify(this.prefix)}. Must match ${TABLE_PREFIX_PATTERN} (letter, then alphanumeric/underscore, ending in "_", max 32 chars total).`
4633
4754
  );
4634
4755
  }
4635
- this.outboxRepo = new OutboxRepository(db, this.prefix, !!options.config?.enableOutbox);
4636
- this.entryRepo = new EntryRepository(db, this.prefix, this.outboxRepo);
4637
- this.taskRepo = new TaskRepository(db, this.prefix, this.outboxRepo);
4638
- this.eventRepo = new EventRepository(db, this.prefix);
4639
- this.edgeRepo = new EdgeRepository(db, this.prefix);
4640
- this.metadataRepo = new MetadataRepository(db, this.prefix);
4756
+ this.outboxRepo = new OutboxRepository(this.db, this.prefix, !!options.config?.enableOutbox);
4757
+ this.entryRepo = new EntryRepository(this.db, this.prefix, this.outboxRepo);
4758
+ this.taskRepo = new TaskRepository(this.db, this.prefix, this.outboxRepo);
4759
+ this.eventRepo = new EventRepository(this.db, this.prefix);
4760
+ this.edgeRepo = new EdgeRepository(this.db, this.prefix);
4761
+ this.metadataRepo = new MetadataRepository(this.db, this.prefix);
4641
4762
  this.ontologyService = new OntologyService(
4642
4763
  this.metadataRepo,
4643
4764
  this.edgeRepo,
@@ -4836,6 +4957,10 @@ var WikiMemory = class {
4836
4957
  async exportDump(entityIds) {
4837
4958
  return this.importExportService.exportDump(entityIds);
4838
4959
  }
4960
+ /** Entity summary prose persisted from an OKF profile ≥ 1 import; null when none stored. */
4961
+ async getEntitySummary(entityId) {
4962
+ return this.metadataRepo.getMeta(entitySummaryMetaKey(entityId));
4963
+ }
4839
4964
  async importDump(dump, opts) {
4840
4965
  return this.importExportService.importDump(dump, opts);
4841
4966
  }
@@ -5660,6 +5785,7 @@ exports.PromptService = PromptService;
5660
5785
  exports.PrunePartialFailureError = PrunePartialFailureError;
5661
5786
  exports.WikiBusyError = WikiBusyError;
5662
5787
  exports.WikiMemory = WikiMemory;
5788
+ exports.WikiTransactionError = WikiTransactionError;
5663
5789
  exports.configureRandomSource = configureRandomSource;
5664
5790
  exports.createWiki = createWiki;
5665
5791
  exports.formatContext = formatContext;