wiki-graph-sdk 0.8.4 → 0.9.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.cjs CHANGED
@@ -1505,6 +1505,9 @@ function resolveWikiGraphRuntimePath(path) {
1505
1505
  var import_promises3 = require("fs/promises");
1506
1506
  var import_path3 = require("path");
1507
1507
  var import_wiki_graph_core2 = require("wiki-graph-core");
1508
+ function requireWikiGraphArchiveFile(location) {
1509
+ return location.archiveFile;
1510
+ }
1508
1511
  var WikiGraphArchiveOwnershipError = class extends Error {
1509
1512
  code = "WIKI_GRAPH_ARCHIVE_OWNERSHIP_MISMATCH";
1510
1513
  constructor(message) {
@@ -1651,11 +1654,12 @@ async function findMatchingMembers(libraries, candidate, candidateIdentity) {
1651
1654
  ...library.isDefault ? {} : { publicId: library.publicId }
1652
1655
  });
1653
1656
  for (const archive of archives) {
1654
- if (archive.file === void 0) continue;
1657
+ const physical = archive.file ?? archive.directory;
1658
+ if (physical === void 0) continue;
1655
1659
  members.push({
1656
1660
  archive,
1657
1661
  library,
1658
- memberPath: getNodeResourcePath(archive.file)
1662
+ memberPath: getNodeResourcePath(physical)
1659
1663
  });
1660
1664
  }
1661
1665
  }
@@ -2166,13 +2170,25 @@ function formatLocatedChapterUri(archiveUri, chapter) {
2166
2170
  var import_wiki_graph_core6 = require("wiki-graph-core");
2167
2171
  async function writeWikiGraphArchiveLocation(location, operation, options = {}) {
2168
2172
  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
- });
2173
+ const write = async (archiveFile = location.archiveFile) => {
2174
+ const writeOptions = {
2175
+ ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
2176
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
2177
+ invalidateContinuationCursors: options.invalidateContinuationCursors
2178
+ },
2179
+ ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
2180
+ };
2181
+ if (archiveFile.kind === "directory") {
2182
+ return await new import_wiki_graph_core6.WikiGraphDirectoryFile(archiveFile).write(
2183
+ operation,
2184
+ writeOptions
2185
+ );
2186
+ }
2187
+ return await new import_wiki_graph_core6.WikiGraphArchiveFile(archiveFile).write(
2188
+ operation,
2189
+ writeOptions
2190
+ );
2191
+ };
2176
2192
  if (location.libraryArchiveTarget === void 0 || cacheOnly || options.refreshLibraryIndex === false) {
2177
2193
  return await write();
2178
2194
  }
@@ -2884,7 +2900,7 @@ var WikiGraphArchiveHandle = class {
2884
2900
  return await this.#writeDocument(async (document) => {
2885
2901
  const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2886
2902
  await assertChapterResetAllowed(
2887
- this.#location.archiveFile,
2903
+ requireWikiGraphArchiveFile(this.#location),
2888
2904
  chapterId,
2889
2905
  stage
2890
2906
  );
@@ -3130,14 +3146,18 @@ var WikiGraphArchiveHandle = class {
3130
3146
  }, options);
3131
3147
  }
3132
3148
  async readCover(options = {}) {
3133
- const app = new import_wiki_graph_core7.WikiGraph({});
3134
- return await this.#runtime.run(
3135
- async () => await app.openSession(
3149
+ return await this.#runtime.run(async () => {
3150
+ if (this.#location.archiveFile.kind === "directory") {
3151
+ return await this.#readDocument(
3152
+ async (document) => await document.readCover()
3153
+ );
3154
+ }
3155
+ const app = new import_wiki_graph_core7.WikiGraph({});
3156
+ return await app.openSession(
3136
3157
  this.#location.archiveFile,
3137
3158
  async (archive) => await archive.readCover()
3138
- ),
3139
- options.signal
3140
- );
3159
+ );
3160
+ }, options.signal);
3141
3161
  }
3142
3162
  async replaceMetadata(objectPath, value, options = {}) {
3143
3163
  return await this.#writeDocument(async (document) => {
@@ -3178,9 +3198,11 @@ var WikiGraphArchiveHandle = class {
3178
3198
  }
3179
3199
  async #readDocument(operation, options = {}) {
3180
3200
  return await this.#runtime.run(
3181
- async () => await new import_wiki_graph_core7.WikiGraphArchiveFile(this.#location.archiveFile).readDocument(
3182
- operation
3183
- ),
3201
+ async () => this.#location.archiveFile.kind === "directory" ? await new import_wiki_graph_core7.WikiGraphDirectoryFile(
3202
+ this.#location.archiveFile
3203
+ ).readDocument(operation) : await new import_wiki_graph_core7.WikiGraphArchiveFile(
3204
+ this.#location.archiveFile
3205
+ ).readDocument(operation),
3184
3206
  options.signal
3185
3207
  );
3186
3208
  }
@@ -3617,54 +3639,53 @@ async function continueArchiveCursor(cursor, limit) {
3617
3639
  const location = await resolveWikiGraphArchiveLocation(
3618
3640
  archiveTargetFromLogicalLocator(getCursorArchivePath(cursor))
3619
3641
  );
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
- }
3642
+ const read = async (document) => {
3643
+ switch (cursor.kind) {
3644
+ case "source-locators":
3645
+ return await (0, import_wiki_graph_core9.listArchiveSourceLocators)(document, cursor.targetUri, {
3646
+ cursor: cursor.cursor,
3647
+ limit
3648
+ });
3649
+ case "collection":
3650
+ return await (0, import_wiki_graph_core9.listArchiveCollection)(
3651
+ document,
3652
+ createCollectionOptions(cursor, limit)
3653
+ );
3654
+ case "search":
3655
+ return await (0, import_wiki_graph_core9.findArchiveObjects)(
3656
+ document,
3657
+ cursor.query ?? "",
3658
+ await createFindOptions(cursor, limit)
3659
+ );
3660
+ case "evidence":
3661
+ return await (0, import_wiki_graph_core9.listArchiveEvidence)(document, cursor.targetUri, {
3662
+ ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3663
+ cursor: cursor.cursor,
3664
+ limit,
3665
+ order: cursor.order,
3666
+ ...cursor.query === void 0 ? {} : { query: cursor.query },
3667
+ ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3668
+ ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3669
+ ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3670
+ ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3671
+ });
3672
+ case "related":
3673
+ return await (0, import_wiki_graph_core9.listRelatedArchiveObjects)(document, cursor.targetUri, {
3674
+ ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3675
+ cursor: cursor.cursor,
3676
+ ...cursor.evidenceLimit === void 0 ? {} : { evidenceLimit: cursor.evidenceLimit },
3677
+ limit,
3678
+ order: cursor.order,
3679
+ ...cursor.query === void 0 ? {} : { query: cursor.query },
3680
+ ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3681
+ ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3682
+ ...cursor.role === void 0 ? {} : { role: cursor.role },
3683
+ ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3684
+ ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3685
+ });
3666
3686
  }
3667
- );
3687
+ };
3688
+ return location.archiveFile.kind === "directory" ? await new import_wiki_graph_core9.WikiGraphDirectoryFile(location.archiveFile).readDocument(read) : await new import_wiki_graph_core9.WikiGraphArchiveFile(location.archiveFile).readDocument(read);
3668
3689
  }
3669
3690
  async function continueLibraryCursor(cursor, limit) {
3670
3691
  const embeddingProvider = cursor.kind === "evidence" || cursor.kind === "related" ? await resolveCursorEmbeddingProvider(cursor) : void 0;
@@ -4504,7 +4525,7 @@ var WikiGraphJobManager = class {
4504
4525
  return {
4505
4526
  job: await this.#backend.add({
4506
4527
  ...options,
4507
- archive: location.archiveFile
4528
+ archive: requireWikiGraphArchiveFile(location)
4508
4529
  }),
4509
4530
  target: location.target
4510
4531
  };
@@ -4536,7 +4557,9 @@ var WikiGraphJobManager = class {
4536
4557
  ...options.activeOnly === void 0 ? {} : { activeOnly: options.activeOnly },
4537
4558
  ...options.all === void 0 ? {} : { all: options.all },
4538
4559
  ...options.archive === void 0 ? {} : {
4539
- archive: (await resolveWikiGraphArchiveLocation(options.archive)).archiveFile
4560
+ archive: requireWikiGraphArchiveFile(
4561
+ await resolveWikiGraphArchiveLocation(options.archive)
4562
+ )
4540
4563
  }
4541
4564
  };
4542
4565
  const jobs = await this.#backend.list(normalized);
@@ -4561,7 +4584,7 @@ var WikiGraphJobManager = class {
4561
4584
  archiveJobs[0].archive
4562
4585
  );
4563
4586
  const chapters = await new import_wiki_graph_core15.WikiGraphArchiveFile(
4564
- location.archiveFile
4587
+ requireWikiGraphArchiveFile(location)
4565
4588
  ).readDocument(async (document) => await (0, import_wiki_graph_core15.listChapters)(document));
4566
4589
  const chaptersById = new Map(
4567
4590
  chapters.map((chapter) => [chapter.chapterId, chapter])
@@ -4604,7 +4627,7 @@ var WikiGraphJobManager = class {
4604
4627
  await validateQueueTargetConfig(target, llmJSON);
4605
4628
  const selectedExplicitly = options.chapterId !== void 0 || options.chapterIds !== void 0 || options.chapterPath !== void 0 || options.depth !== void 0;
4606
4629
  const candidates = await new import_wiki_graph_core15.WikiGraphArchiveFile(
4607
- location.archiveFile
4630
+ requireWikiGraphArchiveFile(location)
4608
4631
  ).readDocument(async (document) => {
4609
4632
  const chapters = await (0, import_wiki_graph_core15.listChapters)(document);
4610
4633
  const selected = await selectQueueChapters(
@@ -4648,7 +4671,7 @@ var WikiGraphJobManager = class {
4648
4671
  chapter: candidate.chapter,
4649
4672
  job: await this.#createHandle(
4650
4673
  await this.#backend.add({
4651
- archive: location.archiveFile,
4674
+ archive: requireWikiGraphArchiveFile(location),
4652
4675
  boost: options.boost ?? false,
4653
4676
  chapterId: candidate.chapter.chapterId,
4654
4677
  ...llmJSON === void 0 ? {} : { llmJSON },
@@ -4679,7 +4702,7 @@ var WikiGraphJobManager = class {
4679
4702
  await validateQueueTargetConfig(target, llmJSON);
4680
4703
  const selectedExplicitly = options.chapterId !== void 0 || options.chapterIds !== void 0 || options.chapterPath !== void 0 || options.depth !== void 0;
4681
4704
  const candidates = await new import_wiki_graph_core15.WikiGraphArchiveFile(
4682
- location.archiveFile
4705
+ requireWikiGraphArchiveFile(location)
4683
4706
  ).readDocument(async (document) => {
4684
4707
  const chapters = await (0, import_wiki_graph_core15.listChapters)(document);
4685
4708
  const selected = await selectQueueChapters(document, chapters, options);
@@ -5009,6 +5032,8 @@ function forwardAbort(source, target) {
5009
5032
  // src/libraries.ts
5010
5033
  var import_wiki_graph_core16 = require("wiki-graph-core");
5011
5034
  var import_promises6 = require("fs/promises");
5035
+ var import_os4 = require("os");
5036
+ var import_path6 = require("path");
5012
5037
  async function resolveQueryEmbeddingProvider(queryMode) {
5013
5038
  if (queryMode === "fts") return void 0;
5014
5039
  const provider = await createConfiguredEmbeddingProvider();
@@ -5098,7 +5123,18 @@ var WikiGraphLibraryManager = class {
5098
5123
  async () => {
5099
5124
  throwIfAborted(options.signal);
5100
5125
  const archive = await (0, import_wiki_graph_core16.getWikiGraphLibraryArchive)(parsedTarget);
5101
- const file = requireReadableLibraryArchiveFile(archive);
5126
+ const source = requireReadableLibraryArchiveFile(archive);
5127
+ let temporaryDirectory;
5128
+ const file = source.kind === "directory" ? await (async () => {
5129
+ temporaryDirectory = await (0, import_promises6.mkdtemp)(
5130
+ (0, import_path6.join)((0, import_os4.tmpdir)(), "wikigraph-library-read-")
5131
+ );
5132
+ const output = new NodeFile(
5133
+ (0, import_path6.join)(temporaryDirectory, "archive.wikg")
5134
+ );
5135
+ await (0, import_wiki_graph_core16.writeWikgArchive)(source, output);
5136
+ return output;
5137
+ })() : source;
5102
5138
  const reader = await file.openReader();
5103
5139
  let active = true;
5104
5140
  try {
@@ -5119,6 +5155,8 @@ var WikiGraphLibraryManager = class {
5119
5155
  } finally {
5120
5156
  active = false;
5121
5157
  await reader.close();
5158
+ if (temporaryDirectory !== void 0)
5159
+ await (0, import_promises6.rm)(temporaryDirectory, { force: true, recursive: true });
5122
5160
  }
5123
5161
  },
5124
5162
  options.signal === void 0 ? {} : { signal: options.signal }
@@ -5480,7 +5518,7 @@ function toLibrarySnapshot(record) {
5480
5518
  };
5481
5519
  }
5482
5520
  function toArchiveSnapshot(record) {
5483
- const { file: _file, ...snapshot } = record;
5521
+ const { file: _file, directory: _directory, ...snapshot } = record;
5484
5522
  return snapshot;
5485
5523
  }
5486
5524
  function toScanSnapshot(result) {
@@ -5513,12 +5551,12 @@ function requireReadableLibraryArchiveFile(archive) {
5513
5551
  `Wiki Graph library archive has a conflict and cannot be read: ${archive.uri}`
5514
5552
  );
5515
5553
  }
5516
- if (archive.file === void 0) {
5554
+ if (archive.file === void 0 && archive.directory === void 0) {
5517
5555
  throw new Error(
5518
5556
  `Wiki Graph library archive is unavailable: ${archive.uri}`
5519
5557
  );
5520
5558
  }
5521
- return archive.file;
5559
+ return archive.file ?? archive.directory;
5522
5560
  }
5523
5561
  var ARCHIVE_STREAM_CHUNK_SIZE = 64 * 1024;
5524
5562
  function createCallbackScopedArchiveStream(reader, isActive, signal) {
@@ -5591,7 +5629,7 @@ function abortReason(signal) {
5591
5629
 
5592
5630
  // src/maintenance.ts
5593
5631
  var import_wiki_graph_core17 = require("wiki-graph-core");
5594
- var import_path6 = require("path");
5632
+ var import_path7 = require("path");
5595
5633
  var WikiGraphMaintenanceManager = class {
5596
5634
  #runtime;
5597
5635
  constructor(runtime) {
@@ -5631,7 +5669,7 @@ var WikiGraphMaintenanceManager = class {
5631
5669
  const resolvedOutputPath = resolveWikiGraphRuntimePath(outputPath);
5632
5670
  await (0, import_wiki_graph_core17.migrateLegacySdpubToWikg)(
5633
5671
  new NodeFile(resolvedInputPath),
5634
- new NodeFile(resolvedOutputPath, (0, import_path6.basename)(resolvedOutputPath))
5672
+ new NodeFile(resolvedOutputPath, (0, import_path7.basename)(resolvedOutputPath))
5635
5673
  );
5636
5674
  return {
5637
5675
  inputPath: resolvedInputPath,