wiki-graph-core 0.4.0 → 0.4.1

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.cjs CHANGED
@@ -9623,6 +9623,15 @@ async function resolveWikiGraphLibrary(target) {
9623
9623
  async (database) => await requireLibraryRecordByPublicId(database, target.publicId)
9624
9624
  );
9625
9625
  }
9626
+ async function resolveWikiGraphLibraryForScan(target) {
9627
+ if (!target.isDefault) {
9628
+ return await resolveWikiGraphLibrary(target);
9629
+ }
9630
+ const existing = await withLibraryRegistryDatabase(
9631
+ async (database) => await readDefaultLibraryRecord(database)
9632
+ );
9633
+ return existing ?? await ensureDefaultWikiGraphLibrary();
9634
+ }
9626
9635
  async function resolveWikiGraphLibraryById(id) {
9627
9636
  return await withLibraryRegistryDatabase(
9628
9637
  async (database) => await requireLibraryRecordById(database, id)
@@ -10515,41 +10524,53 @@ async function readWikiGraphArchiveSchemaVersion(archivePath) {
10515
10524
  async function upgradeWikiGraphArchiveSchema(archivePath) {
10516
10525
  const resolvedArchivePath = (0, import_path33.resolve)(archivePath);
10517
10526
  const schemaVersion = await readWikiGraphArchiveSchemaVersion(resolvedArchivePath);
10518
- if (schemaVersion === CURRENT_ARCHIVE_SCHEMA_VERSION) {
10519
- return;
10520
- }
10521
10527
  if (schemaVersion > CURRENT_ARCHIVE_SCHEMA_VERSION) {
10522
10528
  throw new Error(
10523
10529
  `Unsupported Wiki Graph archive schema version: ${schemaVersion}.`
10524
10530
  );
10525
10531
  }
10526
- await ensureWikiGraphHomeSchemaCurrent();
10527
- const archiveKey = createArchiveKey(resolvedArchivePath);
10528
- await assertArchiveUpgradeSafe(archiveKey);
10529
- const temporaryPath = (0, import_path33.join)(
10530
- (0, import_path33.dirname)(resolvedArchivePath),
10531
- `.${getArchiveBasename(resolvedArchivePath)}.${process.pid}.${(0, import_crypto10.randomUUID)()}.upgrade.tmp`
10532
- );
10533
10532
  const temporaryDirectories = [];
10534
10533
  try {
10535
10534
  const tocOverlay = await createChapterTocUpgradeOverlay(
10536
10535
  resolvedArchivePath,
10537
10536
  temporaryDirectories
10538
10537
  );
10538
+ const schemaChanged = schemaVersion < CURRENT_ARCHIVE_SCHEMA_VERSION;
10539
+ if (!schemaChanged && tocOverlay === void 0) {
10540
+ return {
10541
+ changed: false,
10542
+ repairedToc: false,
10543
+ schemaChanged: false
10544
+ };
10545
+ }
10546
+ await ensureWikiGraphHomeSchemaCurrent();
10547
+ const archiveKey = createArchiveKey(resolvedArchivePath);
10548
+ await assertArchiveUpgradeSafe(archiveKey);
10549
+ const temporaryPath = (0, import_path33.join)(
10550
+ (0, import_path33.dirname)(resolvedArchivePath),
10551
+ `.${getArchiveBasename(resolvedArchivePath)}.${process.pid}.${(0, import_crypto10.randomUUID)()}.upgrade.tmp`
10552
+ );
10539
10553
  await writeWikgArchiveWithOverlays(
10540
10554
  resolvedArchivePath,
10541
10555
  temporaryPath,
10542
10556
  [
10543
- {
10544
- entryPath: SEARCH_INDEX_DATABASE_PATH2,
10545
- kind: "deleted"
10546
- },
10557
+ ...schemaChanged ? [
10558
+ {
10559
+ entryPath: SEARCH_INDEX_DATABASE_PATH2,
10560
+ kind: "deleted"
10561
+ }
10562
+ ] : [],
10547
10563
  ...tocOverlay === void 0 ? [] : [tocOverlay]
10548
10564
  ],
10549
10565
  { preserveMutationToken: true }
10550
10566
  );
10551
10567
  await (0, import_promises21.rename)(temporaryPath, resolvedArchivePath);
10552
10568
  await cleanupArchiveDerivedData(archiveKey);
10569
+ return {
10570
+ changed: true,
10571
+ repairedToc: tocOverlay !== void 0,
10572
+ schemaChanged
10573
+ };
10553
10574
  } finally {
10554
10575
  await Promise.all(
10555
10576
  temporaryDirectories.map(async (path) => {
@@ -15319,7 +15340,7 @@ function getSearchBucket(type) {
15319
15340
  case "chapter-tree":
15320
15341
  case "meta":
15321
15342
  case "fragment":
15322
- throw new Error(`Unsupported search result bucket type: ${type}`);
15343
+ return 4;
15323
15344
  }
15324
15345
  }
15325
15346
  var init_sort = __esm({
@@ -40257,7 +40278,7 @@ var init_search_index3 = __esm({
40257
40278
 
40258
40279
  // src/library/membership.ts
40259
40280
  async function scanWikiGraphLibrary(target) {
40260
- const library = await resolveWikiGraphLibrary(target);
40281
+ const library = await resolveWikiGraphLibraryForScan(target);
40261
40282
  return await withWikiGraphLibraryLock(library.id, "write", async () => {
40262
40283
  const result = await scanWikiGraphLibraryUnlocked(target, library, {
40263
40284
  pathIdentity: "trusted"
@@ -40325,18 +40346,16 @@ async function scanWikiGraphLibraryUnlocked(target, library, options) {
40325
40346
  status: matchingTokenArchives.length === 0 || file.mutationToken === void 0 ? "present" : "conflict"
40326
40347
  });
40327
40348
  }
40328
- const now = (/* @__PURE__ */ new Date()).toISOString();
40329
40349
  for (const archive of existing) {
40330
40350
  if (seenArchiveIds.has(archive.id) || options.pathIdentity === "trusted" && currentPaths.has(archive.relativePath)) {
40331
40351
  continue;
40332
40352
  }
40333
40353
  await database.run(
40334
40354
  `
40335
- UPDATE library_archives
40336
- SET status = 'missing', updated_at = ?, last_scanned_at = ?
40355
+ DELETE FROM library_archives
40337
40356
  WHERE id = ?
40338
40357
  `,
40339
- [now, now, archive.id]
40358
+ [archive.id]
40340
40359
  );
40341
40360
  }
40342
40361
  });
@@ -40770,6 +40789,7 @@ function archivePathMatchRank(archive) {
40770
40789
  return 2;
40771
40790
  }
40772
40791
  async function listWikgFiles(root) {
40792
+ await assertLibraryFolderExists(root);
40773
40793
  const relativePaths = [];
40774
40794
  await walkLibraryDirectory(root, root, relativePaths);
40775
40795
  const files = [];
@@ -40783,6 +40803,19 @@ async function listWikgFiles(root) {
40783
40803
  }
40784
40804
  return files;
40785
40805
  }
40806
+ async function assertLibraryFolderExists(root) {
40807
+ try {
40808
+ const rootStat = await (0, import_promises53.stat)(root);
40809
+ if (!rootStat.isDirectory()) {
40810
+ throw new Error(`Wiki Graph library folder is not a directory: ${root}`);
40811
+ }
40812
+ } catch (error) {
40813
+ if (isNodeError(error) && error.code === "ENOENT") {
40814
+ throw new Error(`Wiki Graph library folder is missing: ${root}`);
40815
+ }
40816
+ throw error;
40817
+ }
40818
+ }
40786
40819
  async function ensureLibraryArchiveFileHasNoSearchIndexAndInspect(root, relativePath) {
40787
40820
  await ensureLibraryManagedArchiveHasNoSearchIndex((0, import_path67.join)(root, relativePath));
40788
40821
  return await inspectLibraryArchiveFile(root, relativePath);
@@ -41491,12 +41524,46 @@ async function findWikiGraphLibraryObjects(target, query, options = {}) {
41491
41524
  }
41492
41525
  return createFindResult(query, hits, options, result.terms);
41493
41526
  }
41527
+ async function findWikiGraphLibraryArchiveMembers(target, query, options = {}) {
41528
+ const terms = createLibraryArchiveMemberSearchTerms(query);
41529
+ const archives = await listWikiGraphLibraryArchives(target);
41530
+ const hits = archives.map(formatLibraryArchiveMemberHit).filter((hit) => matchesLibraryArchiveMemberSearch(hit, terms));
41531
+ return createFindResult(query, hits, options, terms, "typed");
41532
+ }
41494
41533
  function createLibraryQueryIndexHitLimit(options) {
41495
41534
  return Math.max(
41496
41535
  (options.limit ?? DEFAULT_LIBRARY_PAGE_LIMIT) * LIBRARY_QUERY_INDEX_LIMIT_MULTIPLIER,
41497
41536
  LIBRARY_QUERY_INDEX_MIN_LIMIT
41498
41537
  );
41499
41538
  }
41539
+ function createLibraryArchiveMemberSearchTerms(query) {
41540
+ return query.trim().toLocaleLowerCase().split(/\s+/u).filter((term) => term !== "");
41541
+ }
41542
+ function matchesLibraryArchiveMemberSearch(hit, terms) {
41543
+ if (terms.length === 0) {
41544
+ return true;
41545
+ }
41546
+ const haystack = `${hit.id}
41547
+ ${hit.title}
41548
+ ${hit.snippet}`.toLocaleLowerCase();
41549
+ return terms.every((term) => haystack.includes(term));
41550
+ }
41551
+ function formatLibraryArchiveMemberHit(archive) {
41552
+ const details = [
41553
+ archive.relativePath,
41554
+ archive.status,
41555
+ archive.exists ? "exists" : "missing-file"
41556
+ ].join(" ");
41557
+ return {
41558
+ archiveId: archive.id,
41559
+ field: "metadata",
41560
+ id: archive.uri,
41561
+ libraryArchiveUri: archive.uri,
41562
+ snippet: details,
41563
+ title: archive.relativePath,
41564
+ type: "meta"
41565
+ };
41566
+ }
41500
41567
  async function listWikiGraphLibraryObjects(target, options = {}) {
41501
41568
  const hits = [];
41502
41569
  const result = await listWikiGraphLibrarySearchIndex(target, {
@@ -41524,6 +41591,14 @@ async function listWikiGraphLibraryObjects(target, options = {}) {
41524
41591
  }
41525
41592
  return createCollectionResult(hits, options);
41526
41593
  }
41594
+ async function listWikiGraphLibraryArchiveMembers(target, options = {}) {
41595
+ return createCollectionResult(
41596
+ (await listWikiGraphLibraryArchives(target)).map(
41597
+ formatLibraryArchiveMemberHit
41598
+ ),
41599
+ options
41600
+ );
41601
+ }
41527
41602
  async function readWikiGraphLibraryPage(target, objectUri, options = {}) {
41528
41603
  const pages = await readIndexedArchiveResults(
41529
41604
  target,
@@ -41938,6 +42013,7 @@ __export(src_exports, {
41938
42013
  ensureWikiGraphHomeSchemaCurrent: () => ensureWikiGraphHomeSchemaCurrent,
41939
42014
  finalizeWikiGraphLibraryArchiveWrite: () => finalizeWikiGraphLibraryArchiveWrite,
41940
42015
  findArchiveObjects: () => findArchiveObjects,
42016
+ findWikiGraphLibraryArchiveMembers: () => findWikiGraphLibraryArchiveMembers,
41941
42017
  findWikiGraphLibraryObjects: () => findWikiGraphLibraryObjects,
41942
42018
  formatChapterId: () => formatChapterId,
41943
42019
  formatChapterUri: () => formatChapterUri,
@@ -41983,6 +42059,7 @@ __export(src_exports, {
41983
42059
  listRelatedArchiveObjects: () => listRelatedArchiveObjects,
41984
42060
  listRelatedWikiGraphLibraryObjects: () => listRelatedWikiGraphLibraryObjects,
41985
42061
  listWikiGraphLibraries: () => listWikiGraphLibraries,
42062
+ listWikiGraphLibraryArchiveMembers: () => listWikiGraphLibraryArchiveMembers,
41986
42063
  listWikiGraphLibraryArchives: () => listWikiGraphLibraryArchives,
41987
42064
  listWikiGraphLibraryEvidence: () => listWikiGraphLibraryEvidence,
41988
42065
  listWikiGraphLibraryIndexArchiveIdsForObject: () => listWikiGraphLibraryIndexArchiveIdsForObject,
@@ -43105,14 +43182,14 @@ async function upgradeWikiGraphMaintenanceTarget(target, options = {}) {
43105
43182
  }
43106
43183
  const archivePath = resolveArchiveUpgradeTarget(target);
43107
43184
  const schemaVersionBefore = await readWikiGraphArchiveSchemaVersion(archivePath);
43108
- await upgradeWikiGraphArchiveSchema(archivePath);
43185
+ const upgradeResult = await upgradeWikiGraphArchiveSchema(archivePath);
43109
43186
  const schemaVersionAfter = await readWikiGraphArchiveSchemaVersion(archivePath);
43110
43187
  return {
43111
43188
  kind: "archive",
43112
43189
  path: archivePath,
43113
43190
  schemaVersionBefore,
43114
43191
  schemaVersionAfter,
43115
- status: schemaVersionBefore === schemaVersionAfter ? "already-current" : "upgraded"
43192
+ status: upgradeResult.changed ? "upgraded" : "already-current"
43116
43193
  };
43117
43194
  }
43118
43195
  async function assertWikiGraphLibrarySchemaCurrent(target) {
@@ -43151,18 +43228,23 @@ async function upgradeWikiGraphLibrarySchema(target) {
43151
43228
  const schemaVersionBefore = await readWikiGraphArchiveSchemaVersion(
43152
43229
  archive.path
43153
43230
  );
43154
- if (schemaVersionBefore === CURRENT_ARCHIVE_SCHEMA_VERSION) {
43155
- skipped.push({
43231
+ try {
43232
+ const upgradeResult = await upgradeWikiGraphArchiveSchema(archive.path);
43233
+ const schemaVersionAfter = await readWikiGraphArchiveSchemaVersion(
43234
+ archive.path
43235
+ );
43236
+ const item = {
43156
43237
  path: archive.path,
43157
43238
  publicId: archive.publicId,
43158
- schemaVersionAfter: schemaVersionBefore,
43239
+ schemaVersionAfter,
43159
43240
  schemaVersionBefore,
43160
43241
  uri: archive.uri
43161
- });
43162
- continue;
43163
- }
43164
- try {
43165
- await upgradeWikiGraphArchiveSchema(archive.path);
43242
+ };
43243
+ if (upgradeResult.changed) {
43244
+ upgraded.push(item);
43245
+ } else {
43246
+ skipped.push(item);
43247
+ }
43166
43248
  } catch (error) {
43167
43249
  return {
43168
43250
  failed: {
@@ -43178,15 +43260,6 @@ async function upgradeWikiGraphLibrarySchema(target) {
43178
43260
  upgraded
43179
43261
  };
43180
43262
  }
43181
- upgraded.push({
43182
- path: archive.path,
43183
- publicId: archive.publicId,
43184
- schemaVersionAfter: await readWikiGraphArchiveSchemaVersion(
43185
- archive.path
43186
- ),
43187
- schemaVersionBefore,
43188
- uri: archive.uri
43189
- });
43190
43263
  }
43191
43264
  return {
43192
43265
  kind: "lib",
@@ -43332,6 +43405,7 @@ init_object_stream();
43332
43405
  ensureWikiGraphHomeSchemaCurrent,
43333
43406
  finalizeWikiGraphLibraryArchiveWrite,
43334
43407
  findArchiveObjects,
43408
+ findWikiGraphLibraryArchiveMembers,
43335
43409
  findWikiGraphLibraryObjects,
43336
43410
  formatChapterId,
43337
43411
  formatChapterUri,
@@ -43377,6 +43451,7 @@ init_object_stream();
43377
43451
  listRelatedArchiveObjects,
43378
43452
  listRelatedWikiGraphLibraryObjects,
43379
43453
  listWikiGraphLibraries,
43454
+ listWikiGraphLibraryArchiveMembers,
43380
43455
  listWikiGraphLibraryArchives,
43381
43456
  listWikiGraphLibraryEvidence,
43382
43457
  listWikiGraphLibraryIndexArchiveIdsForObject,