wiki-graph-sdk 0.8.4 → 0.9.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.
Files changed (51) hide show
  1. package/dist/archives.cjs +345 -152
  2. package/dist/archives.cjs.map +1 -1
  3. package/dist/archives.js +356 -157
  4. package/dist/archives.js.map +1 -1
  5. package/dist/conversions.cjs +269 -99
  6. package/dist/conversions.cjs.map +1 -1
  7. package/dist/conversions.js +275 -103
  8. package/dist/conversions.js.map +1 -1
  9. package/dist/default-worker.cjs +278 -108
  10. package/dist/default-worker.cjs.map +1 -1
  11. package/dist/default-worker.js +285 -112
  12. package/dist/default-worker.js.map +1 -1
  13. package/dist/embedding.cjs +214 -54
  14. package/dist/embedding.cjs.map +1 -1
  15. package/dist/embedding.js +214 -54
  16. package/dist/embedding.js.map +1 -1
  17. package/dist/gc.cjs +214 -54
  18. package/dist/gc.cjs.map +1 -1
  19. package/dist/gc.js +214 -54
  20. package/dist/gc.js.map +1 -1
  21. package/dist/index.cjs +442 -226
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +461 -236
  25. package/dist/index.js.map +1 -1
  26. package/dist/local-config.cjs +214 -54
  27. package/dist/local-config.cjs.map +1 -1
  28. package/dist/local-config.js +214 -54
  29. package/dist/local-config.js.map +1 -1
  30. package/dist/maintenance.cjs +265 -97
  31. package/dist/maintenance.cjs.map +1 -1
  32. package/dist/maintenance.js +271 -101
  33. package/dist/maintenance.js.map +1 -1
  34. package/dist/node-platform.cjs +214 -54
  35. package/dist/node-platform.cjs.map +1 -1
  36. package/dist/node-platform.d.ts +2 -0
  37. package/dist/node-platform.js +214 -54
  38. package/dist/node-platform.js.map +1 -1
  39. package/dist/runtime-config.cjs +214 -54
  40. package/dist/runtime-config.cjs.map +1 -1
  41. package/dist/runtime-config.js +214 -54
  42. package/dist/runtime-config.js.map +1 -1
  43. package/dist/stage.cjs +214 -54
  44. package/dist/stage.cjs.map +1 -1
  45. package/dist/stage.js +214 -54
  46. package/dist/stage.js.map +1 -1
  47. package/dist/worker.cjs +277 -103
  48. package/dist/worker.cjs.map +1 -1
  49. package/dist/worker.js +284 -107
  50. package/dist/worker.js.map +1 -1
  51. package/package.json +2 -2
package/dist/index.cjs CHANGED
@@ -61,6 +61,7 @@ __export(src_exports, {
61
61
  WikiGraphLibraryManager: () => WikiGraphLibraryManager,
62
62
  WikiGraphMaintenanceManager: () => WikiGraphMaintenanceManager,
63
63
  WikiGraphSDK: () => WikiGraphSDK,
64
+ WikiGraphSchemaVersionError: () => import_wiki_graph_core19.WikiGraphSchemaVersionError,
64
65
  buildSearchIndexEmbeddingProvider: () => buildSearchIndexEmbeddingProvider,
65
66
  buildWikiGraphLLMOptions: () => buildWikiGraphLLMOptions,
66
67
  clearLocalConfigSection: () => clearLocalConfigSection,
@@ -103,6 +104,7 @@ __export(src_exports, {
103
104
  isSourceLocatorScopeUri: () => import_wiki_graph_core19.isSourceLocatorScopeUri,
104
105
  isWikiGraphJobUri: () => import_wiki_graph_core19.isWikiGraphJobUri,
105
106
  isWikiGraphLibraryUri: () => import_wiki_graph_core19.isWikiGraphLibraryUri,
107
+ isWikiGraphSchemaVersionError: () => import_wiki_graph_core19.isWikiGraphSchemaVersionError,
106
108
  isWikiGraphUri: () => import_wiki_graph_core19.isWikiGraphUri,
107
109
  iterateItems: () => iterateItems,
108
110
  iteratePages: () => iteratePages,
@@ -255,6 +257,15 @@ var NodeFile = class {
255
257
  this.name = name;
256
258
  }
257
259
  path;
260
+ async getPhysicalIdentity() {
261
+ const value = await fsPromises.stat(this.path).catch((error) => {
262
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")
263
+ return void 0;
264
+ throw error;
265
+ });
266
+ if (value === void 0) return void 0;
267
+ return `${value.dev}:${value.ino}`;
268
+ }
258
269
  kind = "file";
259
270
  [NODE_RESOURCE_KIND] = "file";
260
271
  identity;
@@ -379,6 +390,15 @@ var NodeDirectory = class _NodeDirectory {
379
390
  this.name = pathModule.basename(path);
380
391
  }
381
392
  path;
393
+ async getPhysicalIdentity() {
394
+ const value = await fsPromises.stat(this.path).catch((error) => {
395
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")
396
+ return void 0;
397
+ throw error;
398
+ });
399
+ if (value === void 0) return void 0;
400
+ return `${value.dev}:${value.ino}`;
401
+ }
382
402
  kind = "directory";
383
403
  [NODE_RESOURCE_KIND] = "directory";
384
404
  identity;
@@ -388,8 +408,10 @@ var NodeDirectory = class _NodeDirectory {
388
408
  const file = new NodeFile(pathModule.join(this.path, name));
389
409
  try {
390
410
  return (await fsPromises.stat(file.path)).isFile() ? file : void 0;
391
- } catch {
392
- return void 0;
411
+ } catch (error) {
412
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
413
+ return void 0;
414
+ throw error;
393
415
  }
394
416
  }
395
417
  async getLastModified() {
@@ -407,8 +429,10 @@ var NodeDirectory = class _NodeDirectory {
407
429
  const directory = new _NodeDirectory(pathModule.join(this.path, name));
408
430
  try {
409
431
  return (await fsPromises.stat(directory.path)).isDirectory() ? directory : void 0;
410
- } catch {
411
- return void 0;
432
+ } catch (error) {
433
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
434
+ return void 0;
435
+ throw error;
412
436
  }
413
437
  }
414
438
  async list() {
@@ -532,14 +556,23 @@ async function openNativeNodeDatabase(file, flags) {
532
556
  });
533
557
  });
534
558
  }
535
- async function openNodeZip(file) {
559
+ async function openNodeZip(file, options = {}) {
536
560
  const path = getBrandedNodeResourcePath(file, "file");
537
561
  if (path === void 0) {
538
562
  throw new TypeError("The Node ZIP adapter requires a NodeFile");
539
563
  }
540
- const zipFile = await new Promise((resolve4, reject) => {
541
- yauzl.open(
542
- path,
564
+ const handle = await fsPromises.open(path, "r");
565
+ let size;
566
+ try {
567
+ size = (await handle.stat()).size;
568
+ } catch (error) {
569
+ await handle.close();
570
+ throw error;
571
+ }
572
+ const open2 = () => new Promise((resolve4, reject) => {
573
+ yauzl.fromRandomAccessReader(
574
+ new NodeZipSource(handle),
575
+ size,
543
576
  { autoClose: false, lazyEntries: true },
544
577
  (error, opened) => {
545
578
  if (error || opened === void 0)
@@ -548,63 +581,192 @@ async function openNodeZip(file) {
548
581
  }
549
582
  );
550
583
  });
551
- const entries = await new Promise(
552
- (resolve4, reject) => {
553
- const discovered = /* @__PURE__ */ new Map();
554
- const rejectAndClose = (error) => {
555
- zipFile.close();
556
- reject(
557
- error instanceof Error ? error : new Error("Cannot scan ZIP central directory")
558
- );
559
- };
560
- const resolveEntries = () => {
561
- zipFile.off("error", rejectAndClose);
562
- resolve4(discovered);
563
- };
564
- zipFile.on("entry", (entry) => {
565
- if (!entry.fileName.endsWith("/")) {
566
- discovered.set(entry.fileName, entry);
567
- }
568
- zipFile.readEntry();
569
- });
570
- zipFile.once("error", rejectAndClose);
571
- zipFile.once("end", resolveEntries);
572
- zipFile.readEntry();
573
- }
574
- );
584
+ try {
585
+ (await open2()).close();
586
+ } catch (error) {
587
+ await handle.close();
588
+ throw error;
589
+ }
575
590
  let closed = false;
591
+ let pending = Promise.resolve();
592
+ let closing;
593
+ const run = (operation) => {
594
+ if (closed)
595
+ return Promise.reject(new Error("Cannot read a closed ZIP archive"));
596
+ const result = pending.then(async () => {
597
+ const zip = await open2();
598
+ try {
599
+ return await operation(zip);
600
+ } finally {
601
+ zip.close();
602
+ }
603
+ });
604
+ pending = result.then(
605
+ () => void 0,
606
+ () => void 0
607
+ );
608
+ return result;
609
+ };
610
+ const withEntry = (name, operation) => run(async (zip) => {
611
+ let exact;
612
+ let alias;
613
+ let aliasCollision = false;
614
+ await scanNodeZip(zip, (entry) => {
615
+ if (entry.fileName === name) exact = entry;
616
+ if (normalizeNodeZipName(entry.fileName) === name) {
617
+ if (alias !== void 0 && alias.fileName !== entry.fileName)
618
+ aliasCollision = true;
619
+ alias = entry;
620
+ }
621
+ });
622
+ if ((exact === void 0 || options.entryNameMode === "normalized") && aliasCollision) {
623
+ let bestPosition = -1;
624
+ const candidates = await open2();
625
+ try {
626
+ await scanNodeZip(candidates, async (entry) => {
627
+ if (normalizeNodeZipName(entry.fileName) !== name) return;
628
+ const probe = await open2();
629
+ let position = 0;
630
+ try {
631
+ await scanNodeZip(probe, (candidate) => {
632
+ if (candidate.fileName === entry.fileName) return false;
633
+ position++;
634
+ });
635
+ } finally {
636
+ probe.close();
637
+ }
638
+ if (position >= bestPosition) {
639
+ bestPosition = position;
640
+ alias = entry;
641
+ }
642
+ });
643
+ return await operation(candidates, alias);
644
+ } finally {
645
+ candidates.close();
646
+ }
647
+ }
648
+ return await operation(
649
+ zip,
650
+ options.entryNameMode === "normalized" ? alias : exact ?? alias
651
+ );
652
+ });
576
653
  return {
577
654
  close: () => {
578
- if (!closed) zipFile.close();
579
655
  closed = true;
580
- return Promise.resolve();
656
+ closing ??= pending.then(() => handle.close());
657
+ return closing;
581
658
  },
582
- copyEntry: async (name, target) => {
583
- if (closed) throw new Error("Cannot read a closed ZIP archive");
584
- const entry = entries.get(name);
659
+ copyEntry: (name, target) => withEntry(name, async (zip, entry) => {
585
660
  if (entry === void 0) return false;
586
- await copyNodeZipEntry(zipFile, entry, target);
661
+ await copyNodeZipEntry(zip, entry, target);
587
662
  return true;
588
- },
589
- getEntrySize: (name) => {
590
- if (closed) throw new Error("Cannot read a closed ZIP archive");
591
- return Promise.resolve(entries.get(name)?.uncompressedSize);
592
- },
593
- listEntries: () => Promise.resolve([...entries.keys()]),
594
- readEntryRange: async (name, offset, length) => {
595
- if (closed) throw new Error("Cannot read a closed ZIP archive");
596
- const entry = entries.get(name);
663
+ }),
664
+ getEntrySize: (name) => withEntry(
665
+ name,
666
+ (_zip, entry) => Promise.resolve(entry?.uncompressedSize)
667
+ ),
668
+ listEntries: () => run(async (zip) => {
669
+ const names = /* @__PURE__ */ new Set();
670
+ await scanNodeZip(zip, (entry) => {
671
+ names.add(entry.fileName);
672
+ });
673
+ return [...names];
674
+ }),
675
+ readEntryRange: (name, offset, length) => withEntry(name, async (zip, entry) => {
597
676
  if (entry === void 0) return void 0;
598
- return await readNodeZipEntryRange(zipFile, entry, offset, length);
599
- },
600
- readEntry: async (name) => {
601
- if (closed) throw new Error("Cannot read a closed ZIP archive");
602
- const entry = entries.get(name);
677
+ return await readNodeZipEntryRange(zip, entry, offset, length);
678
+ }),
679
+ readEntry: (name) => withEntry(name, async (zip, entry) => {
603
680
  if (entry === void 0) return void 0;
604
- return await readNodeZipEntry(zipFile, entry);
605
- }
681
+ return await readNodeZipEntry(zip, entry);
682
+ })
606
683
  };
607
684
  }
685
+ var NodeZipSource = class extends yauzl.RandomAccessReader {
686
+ constructor(handle) {
687
+ super();
688
+ this.handle = handle;
689
+ }
690
+ handle;
691
+ read(buffer, offset, length, position, callback) {
692
+ const read2 = (written) => {
693
+ if (written === length) {
694
+ callback(null);
695
+ return;
696
+ }
697
+ fs.read(
698
+ this.handle.fd,
699
+ buffer,
700
+ offset + written,
701
+ length - written,
702
+ position + written,
703
+ (error, bytesRead) => {
704
+ if (error) callback(error);
705
+ else if (bytesRead === 0)
706
+ callback(new Error("Unexpected end of ZIP file"));
707
+ else read2(written + bytesRead);
708
+ }
709
+ );
710
+ };
711
+ read2(0);
712
+ }
713
+ _readStreamForRange(start, end) {
714
+ const handle = this.handle;
715
+ return import_node_stream.Readable.from(
716
+ (async function* () {
717
+ for (let position = start; position < end; ) {
718
+ const buffer = Buffer.allocUnsafe(
719
+ Math.min(64 * 1024, end - position)
720
+ );
721
+ const { bytesRead } = await handle.read(
722
+ buffer,
723
+ 0,
724
+ buffer.length,
725
+ position
726
+ );
727
+ if (bytesRead === 0) return;
728
+ position += bytesRead;
729
+ yield buffer.subarray(0, bytesRead);
730
+ }
731
+ })()
732
+ );
733
+ }
734
+ };
735
+ function normalizeNodeZipName(name) {
736
+ const normalized = name.replaceAll("\\", "/").trim().split("/").filter((part) => part !== "" && part !== ".").join("/");
737
+ return normalized.split("/").includes("..") ? void 0 : normalized;
738
+ }
739
+ async function scanNodeZip(zip, visit) {
740
+ await new Promise((resolve4, reject) => {
741
+ const cleanup = () => {
742
+ zip.off("entry", onEntry);
743
+ zip.off("end", onEnd);
744
+ zip.off("error", onError);
745
+ };
746
+ const onError = (error) => {
747
+ cleanup();
748
+ reject(error);
749
+ };
750
+ const onEnd = () => {
751
+ cleanup();
752
+ resolve4();
753
+ };
754
+ const onEntry = (entry) => {
755
+ void Promise.resolve().then(() => entry.fileName.endsWith("/") ? void 0 : visit(entry)).then((keepReading) => {
756
+ if (keepReading === false) onEnd();
757
+ else zip.readEntry();
758
+ }).catch(
759
+ (error) => onError(
760
+ error instanceof Error ? error : new Error("Cannot scan ZIP", { cause: error })
761
+ )
762
+ );
763
+ };
764
+ zip.on("entry", onEntry);
765
+ zip.once("end", onEnd);
766
+ zip.once("error", onError);
767
+ zip.readEntry();
768
+ });
769
+ }
608
770
  async function copyNodeZipEntry(zipFile, entry, target) {
609
771
  const writer = await target.openWriter();
610
772
  try {
@@ -1505,6 +1667,19 @@ function resolveWikiGraphRuntimePath(path) {
1505
1667
  var import_promises3 = require("fs/promises");
1506
1668
  var import_path3 = require("path");
1507
1669
  var import_wiki_graph_core2 = require("wiki-graph-core");
1670
+ function rethrowArchiveSchemaError(error, target) {
1671
+ if ((0, import_wiki_graph_core2.isWikiGraphSchemaVersionError)(error) && error.target.kind === "archive") {
1672
+ throw new import_wiki_graph_core2.WikiGraphSchemaVersionError(
1673
+ target.kind === "standalone" ? { kind: "archive", path: resolveWikiGraphRuntimePath(target.path) } : { kind: "library", uri: target.uri.split("/arc/")[0] },
1674
+ error.actualVersion,
1675
+ error.supportedVersion
1676
+ );
1677
+ }
1678
+ throw error;
1679
+ }
1680
+ function requireWikiGraphArchiveFile(location) {
1681
+ return location.archiveFile;
1682
+ }
1508
1683
  var WikiGraphArchiveOwnershipError = class extends Error {
1509
1684
  code = "WIKI_GRAPH_ARCHIVE_OWNERSHIP_MISMATCH";
1510
1685
  constructor(message) {
@@ -1522,12 +1697,7 @@ async function assertStandaloneWikiGraphArchivePath(inputPath) {
1522
1697
  const path = resolveWikiGraphRuntimePath(inputPath);
1523
1698
  const candidate = await canonicalPath(path);
1524
1699
  const candidateIdentity = await readFileIdentity(path);
1525
- const libraries = await (0, import_wiki_graph_core2.listWikiGraphLibraries)();
1526
- const matches = await findMatchingMembers(
1527
- libraries,
1528
- candidate,
1529
- candidateIdentity
1530
- );
1700
+ const matches = await findMatchingMembers(candidate, candidateIdentity);
1531
1701
  if (matches.length > 0) {
1532
1702
  const match = requireUnambiguousManagedMatch(path, matches);
1533
1703
  throw new WikiGraphArchiveOwnershipError(
@@ -1536,15 +1706,26 @@ async function assertStandaloneWikiGraphArchivePath(inputPath) {
1536
1706
  )}: ${path}. Use the library UUID URI ${match.archive.uri} instead.`
1537
1707
  );
1538
1708
  }
1539
- for (const library of libraries) {
1540
- const rootPath = getNodeResourcePath(library.folder);
1541
- const root = await canonicalPath(rootPath);
1542
- if (!isWithin(root, candidate)) continue;
1543
- throw new WikiGraphArchiveOwnershipError(
1544
- `Standalone archive path belongs to registered library ${(0, import_wiki_graph_core2.formatWikiGraphLibraryUri)(
1545
- library.isDefault ? void 0 : library.publicId
1546
- )}: ${path}. Scan the library, then address the archive by its library UUID URI.`
1709
+ const ancestors = /* @__PURE__ */ new Set();
1710
+ for (const start of [candidate, (0, import_path3.resolve)(path)]) {
1711
+ for (let parent = (0, import_path3.dirname)(start); ; parent = (0, import_path3.dirname)(parent)) {
1712
+ ancestors.add(parent);
1713
+ if ((0, import_path3.dirname)(parent) === parent) break;
1714
+ }
1715
+ }
1716
+ for (const parent of ancestors) {
1717
+ const identity = await readFileIdentity(parent);
1718
+ const library = await (0, import_wiki_graph_core2.findWikiGraphLibraryByPhysicalIdentity)(
1719
+ identity,
1720
+ new NodeDirectory(parent).identity
1547
1721
  );
1722
+ if (library !== void 0) {
1723
+ throw new WikiGraphArchiveOwnershipError(
1724
+ `Standalone archive path belongs to registered library ${(0, import_wiki_graph_core2.formatWikiGraphLibraryUri)(
1725
+ library.isDefault ? void 0 : library.publicId
1726
+ )}: ${path}. Scan the library, then address the archive by its library UUID URI.`
1727
+ );
1728
+ }
1548
1729
  }
1549
1730
  return path;
1550
1731
  }
@@ -1562,11 +1743,7 @@ async function resolveWikiGraphArchiveTarget(file) {
1562
1743
  const path = getNodeResourcePath(file);
1563
1744
  const candidate = await canonicalPath(path);
1564
1745
  const candidateIdentity = await readFileIdentity(path);
1565
- const matches = await findMatchingMembers(
1566
- await (0, import_wiki_graph_core2.listWikiGraphLibraries)(),
1567
- candidate,
1568
- candidateIdentity
1569
- );
1746
+ const matches = await findMatchingMembers(candidate, candidateIdentity);
1570
1747
  if (matches.length > 0) {
1571
1748
  const match = requireUnambiguousManagedMatch(path, matches);
1572
1749
  return { kind: "library", uri: match.archive.uri };
@@ -1632,47 +1809,35 @@ function withObjectUri(archiveLocator, objectUri) {
1632
1809
  async function canonicalPath(path) {
1633
1810
  try {
1634
1811
  return await (0, import_promises3.realpath)(path);
1635
- } catch {
1812
+ } catch (error) {
1813
+ if (!(typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR")))
1814
+ throw error;
1636
1815
  const absolute = (0, import_path3.resolve)(path);
1637
1816
  const parent = (0, import_path3.dirname)(absolute);
1638
1817
  return parent === absolute ? absolute : (0, import_path3.join)(await canonicalPath(parent), (0, import_path3.basename)(absolute));
1639
1818
  }
1640
1819
  }
1641
- function isWithin(root, candidate) {
1642
- const child = (0, import_path3.relative)(root, candidate);
1643
- return child === "" || !child.startsWith("..") && !(0, import_path3.isAbsolute)(child);
1644
- }
1645
- async function findMatchingMembers(libraries, candidate, candidateIdentity) {
1820
+ async function findMatchingMembers(candidate, candidateIdentity) {
1821
+ if (candidateIdentity === void 0) return [];
1822
+ const archives = await (0, import_wiki_graph_core2.findWikiGraphLibraryArchivePhysicalAliases)(candidateIdentity);
1646
1823
  const members = [];
1647
- for (const library of libraries) {
1648
- const archives = await (0, import_wiki_graph_core2.listWikiGraphLibraryArchives)({
1649
- isDefault: library.isDefault,
1650
- kind: "scope",
1651
- ...library.isDefault ? {} : { publicId: library.publicId }
1652
- });
1653
- for (const archive of archives) {
1654
- if (archive.file === void 0) continue;
1824
+ for (const archive of archives) {
1825
+ const parsed = (0, import_wiki_graph_core2.parseWikiGraphLibraryUri)(archive.libraryUri);
1826
+ const library = await (0, import_wiki_graph_core2.resolveWikiGraphLibrary)(parsed);
1827
+ const physical = archive.file ?? archive.directory;
1828
+ if (library !== void 0 && physical !== void 0)
1655
1829
  members.push({
1656
1830
  archive,
1657
1831
  library,
1658
- memberPath: getNodeResourcePath(archive.file)
1832
+ memberPath: getNodeResourcePath(physical)
1659
1833
  });
1660
- }
1661
1834
  }
1662
1835
  const exact = [];
1663
1836
  for (const member of members) {
1664
- if (await canonicalPath(member.memberPath) === candidate) {
1837
+ if (await canonicalPath(member.memberPath) === candidate)
1665
1838
  exact.push(member);
1666
- }
1667
- }
1668
- if (exact.length > 0 || candidateIdentity === void 0) return exact;
1669
- const identityMatches = [];
1670
- for (const member of members) {
1671
- if (await readFileIdentity(member.memberPath) === candidateIdentity) {
1672
- identityMatches.push(member);
1673
- }
1674
1839
  }
1675
- return identityMatches;
1840
+ return exact.length > 0 ? exact : members;
1676
1841
  }
1677
1842
  function requireUnambiguousManagedMatch(path, matches) {
1678
1843
  if (matches.length === 1) return matches[0];
@@ -1684,8 +1849,10 @@ async function readFileIdentity(path) {
1684
1849
  try {
1685
1850
  const value = await (0, import_promises3.stat)(path);
1686
1851
  return `${value.dev}:${value.ino}`;
1687
- } catch {
1688
- return void 0;
1852
+ } catch (error) {
1853
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
1854
+ return void 0;
1855
+ throw error;
1689
1856
  }
1690
1857
  }
1691
1858
 
@@ -1747,11 +1914,15 @@ var WikiGraphConversionManager = class {
1747
1914
  if (!("path" in options.input)) {
1748
1915
  throw new Error("wikg input requires a file path.");
1749
1916
  }
1917
+ const inputPath = options.input.path;
1750
1918
  await app.openSession(
1751
- new NodeFile(
1752
- await assertStandaloneWikiGraphArchivePath(options.input.path)
1753
- ),
1919
+ new NodeFile(await assertStandaloneWikiGraphArchivePath(inputPath)),
1754
1920
  write
1921
+ ).catch(
1922
+ (error) => rethrowArchiveSchemaError(error, {
1923
+ kind: "standalone",
1924
+ path: inputPath
1925
+ })
1755
1926
  );
1756
1927
  } else if ("stream" in options.input) {
1757
1928
  await app.digestTextStreamSession(
@@ -2166,13 +2337,31 @@ function formatLocatedChapterUri(archiveUri, chapter) {
2166
2337
  var import_wiki_graph_core6 = require("wiki-graph-core");
2167
2338
  async function writeWikiGraphArchiveLocation(location, operation, options = {}) {
2168
2339
  const cacheOnly = options.searchIndexWritebackPolicy === "cache";
2169
- const write = async (archiveFile = location.archiveFile) => await new import_wiki_graph_core6.WikiGraphArchiveFile(archiveFile).write(operation, {
2170
- ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
2171
- ...options.invalidateContinuationCursors === void 0 ? {} : {
2172
- invalidateContinuationCursors: options.invalidateContinuationCursors
2173
- },
2174
- ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
2175
- });
2340
+ const write = async (archiveFile = location.archiveFile) => {
2341
+ if (cacheOnly) {
2342
+ return await new import_wiki_graph_core6.WikiGraphArchiveFile(archiveFile).readDocument(
2343
+ operation,
2344
+ { searchIndexWritebackPolicy: "cache" }
2345
+ );
2346
+ }
2347
+ const writeOptions = {
2348
+ ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
2349
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
2350
+ invalidateContinuationCursors: options.invalidateContinuationCursors
2351
+ },
2352
+ ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
2353
+ };
2354
+ if (archiveFile.kind === "directory") {
2355
+ return await new import_wiki_graph_core6.WikiGraphDirectoryFile(archiveFile).write(
2356
+ operation,
2357
+ writeOptions
2358
+ );
2359
+ }
2360
+ return await new import_wiki_graph_core6.WikiGraphArchiveFile(archiveFile).write(
2361
+ operation,
2362
+ writeOptions
2363
+ );
2364
+ };
2176
2365
  if (location.libraryArchiveTarget === void 0 || cacheOnly || options.refreshLibraryIndex === false) {
2177
2366
  return await write();
2178
2367
  }
@@ -2530,10 +2719,15 @@ var WikiGraphArchiveManager = class {
2530
2719
  }, options.signal);
2531
2720
  }
2532
2721
  async open(target, options = {}) {
2533
- const location = await this.#runtime.run(
2534
- async () => await resolveWikiGraphArchiveLocation(target),
2535
- options.signal
2536
- );
2722
+ const location = await this.#runtime.run(async () => {
2723
+ const location2 = await resolveWikiGraphArchiveLocation(target);
2724
+ try {
2725
+ await (0, import_wiki_graph_core7.ensureWikiGraphArchiveSchemaCurrent)(location2.archiveFile);
2726
+ } catch (error) {
2727
+ rethrowArchiveSchemaError(error, target);
2728
+ }
2729
+ return location2;
2730
+ }, options.signal);
2537
2731
  return new WikiGraphArchiveHandle(this.#runtime, location);
2538
2732
  }
2539
2733
  };
@@ -2584,15 +2778,11 @@ var WikiGraphArchiveHandle = class {
2584
2778
  options.queryMode,
2585
2779
  options.signal
2586
2780
  );
2587
- const result = await this.#writeDocument(
2781
+ const result = await this.#readDocument(
2588
2782
  async (document) => {
2589
2783
  const chapters = await this.#resolveQueryChapters(document, options, {
2590
2784
  embeddingProvider
2591
2785
  });
2592
- await ensureArchiveSearchIndex(document, {
2593
- ...chapters === void 0 ? {} : { chapters },
2594
- ...embeddingProvider === void 0 ? {} : { embeddingProvider }
2595
- });
2596
2786
  return await (0, import_wiki_graph_core7.findArchiveObjects)(document, query, {
2597
2787
  ...withoutOperationAndScope(options),
2598
2788
  archiveKey: this.archiveKey,
@@ -2601,7 +2791,6 @@ var WikiGraphArchiveHandle = class {
2601
2791
  });
2602
2792
  },
2603
2793
  {
2604
- searchIndexWritebackPolicy: "cache",
2605
2794
  ...options.signal === void 0 ? {} : { signal: options.signal }
2606
2795
  }
2607
2796
  );
@@ -2706,6 +2895,7 @@ var WikiGraphArchiveHandle = class {
2706
2895
  async (document) => {
2707
2896
  const chapters = await this.#resolveScope(document, options);
2708
2897
  await ensureArchiveSearchIndex(document, {
2898
+ rebuild: true,
2709
2899
  ...chapters === void 0 ? {} : { chapters },
2710
2900
  ...embeddingProvider === void 0 ? {} : { embeddingProvider }
2711
2901
  });
@@ -2884,7 +3074,7 @@ var WikiGraphArchiveHandle = class {
2884
3074
  return await this.#writeDocument(async (document) => {
2885
3075
  const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2886
3076
  await assertChapterResetAllowed(
2887
- this.#location.archiveFile,
3077
+ requireWikiGraphArchiveFile(this.#location),
2888
3078
  chapterId,
2889
3079
  stage
2890
3080
  );
@@ -2995,15 +3185,11 @@ var WikiGraphArchiveHandle = class {
2995
3185
  options.queryMode,
2996
3186
  options.signal
2997
3187
  );
2998
- const result = await this.#writeDocument(
3188
+ const result = await this.#readDocument(
2999
3189
  async (document) => {
3000
3190
  const chapters = await this.#resolveQueryChapters(document, options, {
3001
3191
  embeddingProvider
3002
3192
  });
3003
- await ensureArchiveSearchIndex(document, {
3004
- ...chapters === void 0 ? {} : { chapters },
3005
- ...embeddingProvider === void 0 ? {} : { embeddingProvider }
3006
- });
3007
3193
  return await (0, import_wiki_graph_core7.listRelatedArchiveObjects)(document, objectUri, {
3008
3194
  ...withoutOperation(options),
3009
3195
  ...embeddingProvider === void 0 ? {} : { embeddingProvider },
@@ -3011,7 +3197,6 @@ var WikiGraphArchiveHandle = class {
3011
3197
  });
3012
3198
  },
3013
3199
  {
3014
- searchIndexWritebackPolicy: "cache",
3015
3200
  ...options.signal === void 0 ? {} : { signal: options.signal }
3016
3201
  }
3017
3202
  );
@@ -3055,15 +3240,11 @@ var WikiGraphArchiveHandle = class {
3055
3240
  options.queryMode,
3056
3241
  options.signal
3057
3242
  );
3058
- const result = await this.#writeDocument(
3243
+ const result = await this.#readDocument(
3059
3244
  async (document) => {
3060
3245
  const chapters = await this.#resolveQueryChapters(document, options, {
3061
3246
  embeddingProvider
3062
3247
  });
3063
- await ensureArchiveSearchIndex(document, {
3064
- ...chapters === void 0 ? {} : { chapters },
3065
- ...embeddingProvider === void 0 ? {} : { embeddingProvider }
3066
- });
3067
3248
  return await (0, import_wiki_graph_core7.listArchiveEvidence)(document, objectUri, {
3068
3249
  ...withoutOperation(options),
3069
3250
  ...embeddingProvider === void 0 ? {} : { embeddingProvider },
@@ -3071,7 +3252,6 @@ var WikiGraphArchiveHandle = class {
3071
3252
  });
3072
3253
  },
3073
3254
  {
3074
- searchIndexWritebackPolicy: "cache",
3075
3255
  ...options.signal === void 0 ? {} : { signal: options.signal }
3076
3256
  }
3077
3257
  );
@@ -3130,14 +3310,18 @@ var WikiGraphArchiveHandle = class {
3130
3310
  }, options);
3131
3311
  }
3132
3312
  async readCover(options = {}) {
3133
- const app = new import_wiki_graph_core7.WikiGraph({});
3134
- return await this.#runtime.run(
3135
- async () => await app.openSession(
3313
+ return await this.#runtime.run(async () => {
3314
+ if (this.#location.archiveFile.kind === "directory") {
3315
+ return await this.#readDocument(
3316
+ async (document) => await document.readCover()
3317
+ );
3318
+ }
3319
+ const app = new import_wiki_graph_core7.WikiGraph({});
3320
+ return await app.openSession(
3136
3321
  this.#location.archiveFile,
3137
3322
  async (archive) => await archive.readCover()
3138
- ),
3139
- options.signal
3140
- );
3323
+ );
3324
+ }, options.signal);
3141
3325
  }
3142
3326
  async replaceMetadata(objectPath, value, options = {}) {
3143
3327
  return await this.#writeDocument(async (document) => {
@@ -3178,10 +3362,14 @@ var WikiGraphArchiveHandle = class {
3178
3362
  }
3179
3363
  async #readDocument(operation, options = {}) {
3180
3364
  return await this.#runtime.run(
3181
- async () => await new import_wiki_graph_core7.WikiGraphArchiveFile(this.#location.archiveFile).readDocument(
3182
- operation
3183
- ),
3365
+ async () => this.#location.archiveFile.kind === "directory" ? await new import_wiki_graph_core7.WikiGraphDirectoryFile(
3366
+ this.#location.archiveFile
3367
+ ).readDocument(operation) : await new import_wiki_graph_core7.WikiGraphArchiveFile(
3368
+ this.#location.archiveFile
3369
+ ).readDocument(operation),
3184
3370
  options.signal
3371
+ ).catch(
3372
+ (error) => rethrowArchiveSchemaError(error, this.#location.target)
3185
3373
  );
3186
3374
  }
3187
3375
  async #writeDocument(operation, options = {}) {
@@ -3193,33 +3381,32 @@ var WikiGraphArchiveHandle = class {
3193
3381
  searchIndexWritebackPolicy: options.searchIndexWritebackPolicy
3194
3382
  }
3195
3383
  });
3196
- }, options.signal);
3384
+ }, options.signal).catch(
3385
+ (error) => rethrowArchiveSchemaError(error, this.#location.target)
3386
+ );
3197
3387
  }
3198
3388
  async #resolveScope(document, options) {
3199
3389
  if (options.chapters !== void 0) return options.chapters;
3200
3390
  return (await resolveArchiveScope(document, this.objectUri, options.depth))?.chapterIds;
3201
3391
  }
3202
3392
  async #resolveQueryChapters(document, options, capabilities) {
3203
- const chapters = await this.#resolveScope(document, options);
3393
+ const parsed = parseChapterScopePath(this.objectUri);
3394
+ const chapters = options.chapters ?? (parsed === void 0 || parsed.kind === "collection" && options.depth === void 0 ? void 0 : await (0, import_wiki_graph_core7.resolveIndexedArchiveChapterScope)(
3395
+ document,
3396
+ parsed.kind === "chapter" ? parsed.chapterPath : void 0,
3397
+ options.depth
3398
+ ));
3204
3399
  const coverageOptions = {
3205
3400
  ...chapters === void 0 ? {} : { chapters },
3206
3401
  ...capabilities.embeddingProvider === void 0 ? {} : { embeddingProvider: capabilities.embeddingProvider },
3207
3402
  requireEmbeddingProvider: true,
3208
3403
  ...options.queryMode === void 0 ? {} : { queryMode: options.queryMode }
3209
3404
  };
3210
- if (options.skipUnindexed !== true) {
3211
- await (0, import_wiki_graph_core7.assertArchiveIndexArtifactsReady)(document, coverageOptions);
3212
- return chapters;
3213
- }
3214
- const queryable = await (0, import_wiki_graph_core7.listArchiveQueryableChapterIds)(document, {
3215
- ...coverageOptions
3405
+ await (0, import_wiki_graph_core7.assertArchiveQueryReady)(document, {
3406
+ ...coverageOptions,
3407
+ ...options.skipUnindexed === void 0 ? {} : { skipUnindexed: options.skipUnindexed }
3216
3408
  });
3217
- if (queryable.length === 0) {
3218
- throw new Error(
3219
- "Wiki Graph query is not ready. No chapters in this scope have a current FTS artifact or source embedding artifact."
3220
- );
3221
- }
3222
- return queryable;
3409
+ return chapters;
3223
3410
  }
3224
3411
  async #resolveQueryEmbeddingProvider(queryMode, signal) {
3225
3412
  if (queryMode === "fts") return void 0;
@@ -3238,6 +3425,10 @@ var WikiGraphArchiveHandle = class {
3238
3425
  async function ensureArchiveSearchIndex(document, options = {}) {
3239
3426
  const scope = options.chapters === void 0 ? {} : { chapters: options.chapters };
3240
3427
  if (await (0, import_wiki_graph_core7.isArchiveSearchIndexCurrent)(document, scope)) return;
3428
+ if (options.rebuild !== true)
3429
+ throw new Error(
3430
+ "Archive search index is missing or dirty. Run `wg <archive-uri>/index sync` explicitly before querying."
3431
+ );
3241
3432
  await (0, import_wiki_graph_core7.rebuildArchiveSearchIndex)(document, void 0, {
3242
3433
  ...scope,
3243
3434
  ...options.embeddingProvider === void 0 ? {} : { embeddingProvider: options.embeddingProvider }
@@ -3617,53 +3808,54 @@ async function continueArchiveCursor(cursor, limit) {
3617
3808
  const location = await resolveWikiGraphArchiveLocation(
3618
3809
  archiveTargetFromLogicalLocator(getCursorArchivePath(cursor))
3619
3810
  );
3620
- return await new import_wiki_graph_core9.WikiGraphArchiveFile(location.archiveFile).readDocument(
3621
- async (document) => {
3622
- switch (cursor.kind) {
3623
- case "source-locators":
3624
- return await (0, import_wiki_graph_core9.listArchiveSourceLocators)(document, cursor.targetUri, {
3625
- cursor: cursor.cursor,
3626
- limit
3627
- });
3628
- case "collection":
3629
- return await (0, import_wiki_graph_core9.listArchiveCollection)(
3630
- document,
3631
- createCollectionOptions(cursor, limit)
3632
- );
3633
- case "search":
3634
- return await (0, import_wiki_graph_core9.findArchiveObjects)(
3635
- document,
3636
- cursor.query ?? "",
3637
- await createFindOptions(cursor, limit)
3638
- );
3639
- case "evidence":
3640
- return await (0, import_wiki_graph_core9.listArchiveEvidence)(document, cursor.targetUri, {
3641
- ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3642
- cursor: cursor.cursor,
3643
- limit,
3644
- order: cursor.order,
3645
- ...cursor.query === void 0 ? {} : { query: cursor.query },
3646
- ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3647
- ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3648
- ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3649
- ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3650
- });
3651
- case "related":
3652
- return await (0, import_wiki_graph_core9.listRelatedArchiveObjects)(document, cursor.targetUri, {
3653
- ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3654
- cursor: cursor.cursor,
3655
- ...cursor.evidenceLimit === void 0 ? {} : { evidenceLimit: cursor.evidenceLimit },
3656
- limit,
3657
- order: cursor.order,
3658
- ...cursor.query === void 0 ? {} : { query: cursor.query },
3659
- ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3660
- ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3661
- ...cursor.role === void 0 ? {} : { role: cursor.role },
3662
- ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3663
- ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3664
- });
3665
- }
3811
+ const read2 = async (document) => {
3812
+ switch (cursor.kind) {
3813
+ case "source-locators":
3814
+ return await (0, import_wiki_graph_core9.listArchiveSourceLocators)(document, cursor.targetUri, {
3815
+ cursor: cursor.cursor,
3816
+ limit
3817
+ });
3818
+ case "collection":
3819
+ return await (0, import_wiki_graph_core9.listArchiveCollection)(
3820
+ document,
3821
+ createCollectionOptions(cursor, limit)
3822
+ );
3823
+ case "search":
3824
+ return await (0, import_wiki_graph_core9.findArchiveObjects)(
3825
+ document,
3826
+ cursor.query ?? "",
3827
+ await createFindOptions(cursor, limit)
3828
+ );
3829
+ case "evidence":
3830
+ return await (0, import_wiki_graph_core9.listArchiveEvidence)(document, cursor.targetUri, {
3831
+ ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3832
+ cursor: cursor.cursor,
3833
+ limit,
3834
+ order: cursor.order,
3835
+ ...cursor.query === void 0 ? {} : { query: cursor.query },
3836
+ ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3837
+ ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3838
+ ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3839
+ ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3840
+ });
3841
+ case "related":
3842
+ return await (0, import_wiki_graph_core9.listRelatedArchiveObjects)(document, cursor.targetUri, {
3843
+ ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3844
+ cursor: cursor.cursor,
3845
+ ...cursor.evidenceLimit === void 0 ? {} : { evidenceLimit: cursor.evidenceLimit },
3846
+ limit,
3847
+ order: cursor.order,
3848
+ ...cursor.query === void 0 ? {} : { query: cursor.query },
3849
+ ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3850
+ ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3851
+ ...cursor.role === void 0 ? {} : { role: cursor.role },
3852
+ ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3853
+ ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3854
+ });
3666
3855
  }
3856
+ };
3857
+ return await (location.archiveFile.kind === "directory" ? new import_wiki_graph_core9.WikiGraphDirectoryFile(location.archiveFile).readDocument(read2) : new import_wiki_graph_core9.WikiGraphArchiveFile(location.archiveFile).readDocument(read2)).catch(
3858
+ (error) => rethrowArchiveSchemaError(error, location.target)
3667
3859
  );
3668
3860
  }
3669
3861
  async function continueLibraryCursor(cursor, limit) {
@@ -4504,7 +4696,7 @@ var WikiGraphJobManager = class {
4504
4696
  return {
4505
4697
  job: await this.#backend.add({
4506
4698
  ...options,
4507
- archive: location.archiveFile
4699
+ archive: requireWikiGraphArchiveFile(location)
4508
4700
  }),
4509
4701
  target: location.target
4510
4702
  };
@@ -4536,7 +4728,9 @@ var WikiGraphJobManager = class {
4536
4728
  ...options.activeOnly === void 0 ? {} : { activeOnly: options.activeOnly },
4537
4729
  ...options.all === void 0 ? {} : { all: options.all },
4538
4730
  ...options.archive === void 0 ? {} : {
4539
- archive: (await resolveWikiGraphArchiveLocation(options.archive)).archiveFile
4731
+ archive: requireWikiGraphArchiveFile(
4732
+ await resolveWikiGraphArchiveLocation(options.archive)
4733
+ )
4540
4734
  }
4541
4735
  };
4542
4736
  const jobs = await this.#backend.list(normalized);
@@ -4561,7 +4755,7 @@ var WikiGraphJobManager = class {
4561
4755
  archiveJobs[0].archive
4562
4756
  );
4563
4757
  const chapters = await new import_wiki_graph_core15.WikiGraphArchiveFile(
4564
- location.archiveFile
4758
+ requireWikiGraphArchiveFile(location)
4565
4759
  ).readDocument(async (document) => await (0, import_wiki_graph_core15.listChapters)(document));
4566
4760
  const chaptersById = new Map(
4567
4761
  chapters.map((chapter) => [chapter.chapterId, chapter])
@@ -4604,7 +4798,7 @@ var WikiGraphJobManager = class {
4604
4798
  await validateQueueTargetConfig(target, llmJSON);
4605
4799
  const selectedExplicitly = options.chapterId !== void 0 || options.chapterIds !== void 0 || options.chapterPath !== void 0 || options.depth !== void 0;
4606
4800
  const candidates = await new import_wiki_graph_core15.WikiGraphArchiveFile(
4607
- location.archiveFile
4801
+ requireWikiGraphArchiveFile(location)
4608
4802
  ).readDocument(async (document) => {
4609
4803
  const chapters = await (0, import_wiki_graph_core15.listChapters)(document);
4610
4804
  const selected = await selectQueueChapters(
@@ -4648,7 +4842,7 @@ var WikiGraphJobManager = class {
4648
4842
  chapter: candidate.chapter,
4649
4843
  job: await this.#createHandle(
4650
4844
  await this.#backend.add({
4651
- archive: location.archiveFile,
4845
+ archive: requireWikiGraphArchiveFile(location),
4652
4846
  boost: options.boost ?? false,
4653
4847
  chapterId: candidate.chapter.chapterId,
4654
4848
  ...llmJSON === void 0 ? {} : { llmJSON },
@@ -4679,7 +4873,7 @@ var WikiGraphJobManager = class {
4679
4873
  await validateQueueTargetConfig(target, llmJSON);
4680
4874
  const selectedExplicitly = options.chapterId !== void 0 || options.chapterIds !== void 0 || options.chapterPath !== void 0 || options.depth !== void 0;
4681
4875
  const candidates = await new import_wiki_graph_core15.WikiGraphArchiveFile(
4682
- location.archiveFile
4876
+ requireWikiGraphArchiveFile(location)
4683
4877
  ).readDocument(async (document) => {
4684
4878
  const chapters = await (0, import_wiki_graph_core15.listChapters)(document);
4685
4879
  const selected = await selectQueueChapters(document, chapters, options);
@@ -5009,6 +5203,8 @@ function forwardAbort(source, target) {
5009
5203
  // src/libraries.ts
5010
5204
  var import_wiki_graph_core16 = require("wiki-graph-core");
5011
5205
  var import_promises6 = require("fs/promises");
5206
+ var import_os4 = require("os");
5207
+ var import_path6 = require("path");
5012
5208
  async function resolveQueryEmbeddingProvider(queryMode) {
5013
5209
  if (queryMode === "fts") return void 0;
5014
5210
  const provider = await createConfiguredEmbeddingProvider();
@@ -5098,10 +5294,28 @@ var WikiGraphLibraryManager = class {
5098
5294
  async () => {
5099
5295
  throwIfAborted(options.signal);
5100
5296
  const archive = await (0, import_wiki_graph_core16.getWikiGraphLibraryArchive)(parsedTarget);
5101
- const file = requireReadableLibraryArchiveFile(archive);
5102
- const reader = await file.openReader();
5297
+ const source = requireReadableLibraryArchiveFile(archive);
5298
+ await (0, import_wiki_graph_core16.ensureWikiGraphArchiveSchemaCurrent)(source).catch(
5299
+ (error) => rethrowArchiveSchemaError(error, {
5300
+ kind: "library",
5301
+ uri: archive.uri
5302
+ })
5303
+ );
5304
+ let temporaryDirectory;
5305
+ let reader;
5103
5306
  let active = true;
5104
5307
  try {
5308
+ const file = source.kind === "directory" ? await (async () => {
5309
+ temporaryDirectory = await (0, import_promises6.mkdtemp)(
5310
+ (0, import_path6.join)((0, import_os4.tmpdir)(), "wikigraph-library-read-")
5311
+ );
5312
+ const output = new NodeFile(
5313
+ (0, import_path6.join)(temporaryDirectory, "archive.wikg")
5314
+ );
5315
+ await (0, import_wiki_graph_core16.writeWikgArchive)(source, output);
5316
+ return output;
5317
+ })() : source;
5318
+ reader = await file.openReader();
5105
5319
  const content = {
5106
5320
  mediaType: "application/vnd.wiki-graph.archive",
5107
5321
  size: reader.size,
@@ -5118,7 +5332,12 @@ var WikiGraphLibraryManager = class {
5118
5332
  );
5119
5333
  } finally {
5120
5334
  active = false;
5121
- await reader.close();
5335
+ try {
5336
+ await reader?.close();
5337
+ } finally {
5338
+ if (temporaryDirectory !== void 0)
5339
+ await (0, import_promises6.rm)(temporaryDirectory, { force: true, recursive: true });
5340
+ }
5122
5341
  }
5123
5342
  },
5124
5343
  options.signal === void 0 ? {} : { signal: options.signal }
@@ -5480,7 +5699,7 @@ function toLibrarySnapshot(record) {
5480
5699
  };
5481
5700
  }
5482
5701
  function toArchiveSnapshot(record) {
5483
- const { file: _file, ...snapshot } = record;
5702
+ const { file: _file, directory: _directory, ...snapshot } = record;
5484
5703
  return snapshot;
5485
5704
  }
5486
5705
  function toScanSnapshot(result) {
@@ -5513,12 +5732,12 @@ function requireReadableLibraryArchiveFile(archive) {
5513
5732
  `Wiki Graph library archive has a conflict and cannot be read: ${archive.uri}`
5514
5733
  );
5515
5734
  }
5516
- if (archive.file === void 0) {
5735
+ if (archive.file === void 0 && archive.directory === void 0) {
5517
5736
  throw new Error(
5518
5737
  `Wiki Graph library archive is unavailable: ${archive.uri}`
5519
5738
  );
5520
5739
  }
5521
- return archive.file;
5740
+ return archive.file ?? archive.directory;
5522
5741
  }
5523
5742
  var ARCHIVE_STREAM_CHUNK_SIZE = 64 * 1024;
5524
5743
  function createCallbackScopedArchiveStream(reader, isActive, signal) {
@@ -5591,7 +5810,7 @@ function abortReason(signal) {
5591
5810
 
5592
5811
  // src/maintenance.ts
5593
5812
  var import_wiki_graph_core17 = require("wiki-graph-core");
5594
- var import_path6 = require("path");
5813
+ var import_path7 = require("path");
5595
5814
  var WikiGraphMaintenanceManager = class {
5596
5815
  #runtime;
5597
5816
  constructor(runtime) {
@@ -5619,7 +5838,9 @@ var WikiGraphMaintenanceManager = class {
5619
5838
  return await (0, import_wiki_graph_core17.upgradeWikiGraphMaintenanceTarget)({
5620
5839
  file: new NodeFile(path),
5621
5840
  kind: "archive"
5622
- });
5841
+ }).catch(
5842
+ (error) => rethrowArchiveSchemaError(error, { kind: "standalone", path })
5843
+ );
5623
5844
  },
5624
5845
  void 0,
5625
5846
  { skipHomeBootstrap: target.kind === "home" }
@@ -5631,7 +5852,7 @@ var WikiGraphMaintenanceManager = class {
5631
5852
  const resolvedOutputPath = resolveWikiGraphRuntimePath(outputPath);
5632
5853
  await (0, import_wiki_graph_core17.migrateLegacySdpubToWikg)(
5633
5854
  new NodeFile(resolvedInputPath),
5634
- new NodeFile(resolvedOutputPath, (0, import_path6.basename)(resolvedOutputPath))
5855
+ new NodeFile(resolvedOutputPath, (0, import_path7.basename)(resolvedOutputPath))
5635
5856
  );
5636
5857
  return {
5637
5858
  inputPath: resolvedInputPath,
@@ -5648,7 +5869,6 @@ function defaultWikgOutputPath(inputPath) {
5648
5869
  var import_wiki_graph_core18 = require("wiki-graph-core");
5649
5870
  var WikiGraphSDK = class {
5650
5871
  #context;
5651
- #homeReady;
5652
5872
  archives;
5653
5873
  conversions;
5654
5874
  config;
@@ -5679,13 +5899,7 @@ var WikiGraphSDK = class {
5679
5899
  throwIfAborted2(signal);
5680
5900
  const runWithContext = async () => await withWikiGraphSDKRuntimeContext(this.#context, async () => {
5681
5901
  if (options.skipHomeBootstrap !== true) {
5682
- this.#homeReady ??= (0, import_wiki_graph_core18.ensureWikiGraphHomeSchemaCurrent)().catch(
5683
- (error) => {
5684
- this.#homeReady = void 0;
5685
- throw error;
5686
- }
5687
- );
5688
- await this.#homeReady;
5902
+ await (0, import_wiki_graph_core18.ensureWikiGraphHomeSchemaCurrent)();
5689
5903
  }
5690
5904
  return await operation();
5691
5905
  });
@@ -5745,6 +5959,7 @@ var import_wiki_graph_core19 = require("wiki-graph-core");
5745
5959
  WikiGraphLibraryManager,
5746
5960
  WikiGraphMaintenanceManager,
5747
5961
  WikiGraphSDK,
5962
+ WikiGraphSchemaVersionError,
5748
5963
  buildSearchIndexEmbeddingProvider,
5749
5964
  buildWikiGraphLLMOptions,
5750
5965
  clearLocalConfigSection,
@@ -5787,6 +6002,7 @@ var import_wiki_graph_core19 = require("wiki-graph-core");
5787
6002
  isSourceLocatorScopeUri,
5788
6003
  isWikiGraphJobUri,
5789
6004
  isWikiGraphLibraryUri,
6005
+ isWikiGraphSchemaVersionError,
5790
6006
  isWikiGraphUri,
5791
6007
  iterateItems,
5792
6008
  iteratePages,