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.js CHANGED
@@ -43,6 +43,7 @@ import {
43
43
  getChapterTree,
44
44
  WikiGraph as WikiGraph2,
45
45
  WikiGraphArchiveFile as WikiGraphArchiveFile2,
46
+ WikiGraphDirectoryFile as WikiGraphDirectoryFile2,
46
47
  TOC_FILE_VERSION,
47
48
  writeWikgArchive,
48
49
  ObjectMetadataKind
@@ -1420,6 +1421,9 @@ import {
1420
1421
  resolveWikiGraphLibrary,
1421
1422
  resolveWikiGraphLibraryArchiveFile
1422
1423
  } from "wiki-graph-core";
1424
+ function requireWikiGraphArchiveFile(location) {
1425
+ return location.archiveFile;
1426
+ }
1423
1427
  var WikiGraphArchiveOwnershipError = class extends Error {
1424
1428
  code = "WIKI_GRAPH_ARCHIVE_OWNERSHIP_MISMATCH";
1425
1429
  constructor(message) {
@@ -1566,11 +1570,12 @@ async function findMatchingMembers(libraries, candidate, candidateIdentity) {
1566
1570
  ...library.isDefault ? {} : { publicId: library.publicId }
1567
1571
  });
1568
1572
  for (const archive of archives) {
1569
- if (archive.file === void 0) continue;
1573
+ const physical = archive.file ?? archive.directory;
1574
+ if (physical === void 0) continue;
1570
1575
  members.push({
1571
1576
  archive,
1572
1577
  library,
1573
- memberPath: getNodeResourcePath(archive.file)
1578
+ memberPath: getNodeResourcePath(physical)
1574
1579
  });
1575
1580
  }
1576
1581
  }
@@ -2085,17 +2090,30 @@ function formatLocatedChapterUri(archiveUri, chapter) {
2085
2090
  // src/archive/write.ts
2086
2091
  import {
2087
2092
  WikiGraphArchiveFile,
2093
+ WikiGraphDirectoryFile,
2088
2094
  writeWikiGraphLibraryArchive
2089
2095
  } from "wiki-graph-core";
2090
2096
  async function writeWikiGraphArchiveLocation(location, operation, options = {}) {
2091
2097
  const cacheOnly = options.searchIndexWritebackPolicy === "cache";
2092
- const write = async (archiveFile = location.archiveFile) => await new WikiGraphArchiveFile(archiveFile).write(operation, {
2093
- ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
2094
- ...options.invalidateContinuationCursors === void 0 ? {} : {
2095
- invalidateContinuationCursors: options.invalidateContinuationCursors
2096
- },
2097
- ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
2098
- });
2098
+ const write = async (archiveFile = location.archiveFile) => {
2099
+ const writeOptions = {
2100
+ ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
2101
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
2102
+ invalidateContinuationCursors: options.invalidateContinuationCursors
2103
+ },
2104
+ ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
2105
+ };
2106
+ if (archiveFile.kind === "directory") {
2107
+ return await new WikiGraphDirectoryFile(archiveFile).write(
2108
+ operation,
2109
+ writeOptions
2110
+ );
2111
+ }
2112
+ return await new WikiGraphArchiveFile(archiveFile).write(
2113
+ operation,
2114
+ writeOptions
2115
+ );
2116
+ };
2099
2117
  if (location.libraryArchiveTarget === void 0 || cacheOnly || options.refreshLibraryIndex === false) {
2100
2118
  return await write();
2101
2119
  }
@@ -2807,7 +2825,7 @@ var WikiGraphArchiveHandle = class {
2807
2825
  return await this.#writeDocument(async (document) => {
2808
2826
  const chapterId = await resolveChapterPathReadonly(document, path);
2809
2827
  await assertChapterResetAllowed(
2810
- this.#location.archiveFile,
2828
+ requireWikiGraphArchiveFile(this.#location),
2811
2829
  chapterId,
2812
2830
  stage
2813
2831
  );
@@ -3053,14 +3071,18 @@ var WikiGraphArchiveHandle = class {
3053
3071
  }, options);
3054
3072
  }
3055
3073
  async readCover(options = {}) {
3056
- const app = new WikiGraph2({});
3057
- return await this.#runtime.run(
3058
- async () => await app.openSession(
3074
+ return await this.#runtime.run(async () => {
3075
+ if (this.#location.archiveFile.kind === "directory") {
3076
+ return await this.#readDocument(
3077
+ async (document) => await document.readCover()
3078
+ );
3079
+ }
3080
+ const app = new WikiGraph2({});
3081
+ return await app.openSession(
3059
3082
  this.#location.archiveFile,
3060
3083
  async (archive) => await archive.readCover()
3061
- ),
3062
- options.signal
3063
- );
3084
+ );
3085
+ }, options.signal);
3064
3086
  }
3065
3087
  async replaceMetadata(objectPath, value, options = {}) {
3066
3088
  return await this.#writeDocument(async (document) => {
@@ -3101,9 +3123,11 @@ var WikiGraphArchiveHandle = class {
3101
3123
  }
3102
3124
  async #readDocument(operation, options = {}) {
3103
3125
  return await this.#runtime.run(
3104
- async () => await new WikiGraphArchiveFile2(this.#location.archiveFile).readDocument(
3105
- operation
3106
- ),
3126
+ async () => this.#location.archiveFile.kind === "directory" ? await new WikiGraphDirectoryFile2(
3127
+ this.#location.archiveFile
3128
+ ).readDocument(operation) : await new WikiGraphArchiveFile2(
3129
+ this.#location.archiveFile
3130
+ ).readDocument(operation),
3107
3131
  options.signal
3108
3132
  );
3109
3133
  }
@@ -3506,7 +3530,8 @@ import {
3506
3530
  listWikiGraphLibraryObjects,
3507
3531
  readContinuationCursor,
3508
3532
  resolveWikiGraphLibraryQueryTargetById,
3509
- WikiGraphArchiveFile as WikiGraphArchiveFile3
3533
+ WikiGraphArchiveFile as WikiGraphArchiveFile3,
3534
+ WikiGraphDirectoryFile as WikiGraphDirectoryFile3
3510
3535
  } from "wiki-graph-core";
3511
3536
  var WikiGraphContinuationManager = class {
3512
3537
  #runtime;
@@ -3556,54 +3581,53 @@ async function continueArchiveCursor(cursor, limit) {
3556
3581
  const location = await resolveWikiGraphArchiveLocation(
3557
3582
  archiveTargetFromLogicalLocator(getCursorArchivePath(cursor))
3558
3583
  );
3559
- return await new WikiGraphArchiveFile3(location.archiveFile).readDocument(
3560
- async (document) => {
3561
- switch (cursor.kind) {
3562
- case "source-locators":
3563
- return await listArchiveSourceLocators2(document, cursor.targetUri, {
3564
- cursor: cursor.cursor,
3565
- limit
3566
- });
3567
- case "collection":
3568
- return await listArchiveCollection2(
3569
- document,
3570
- createCollectionOptions(cursor, limit)
3571
- );
3572
- case "search":
3573
- return await findArchiveObjects2(
3574
- document,
3575
- cursor.query ?? "",
3576
- await createFindOptions(cursor, limit)
3577
- );
3578
- case "evidence":
3579
- return await listArchiveEvidence2(document, cursor.targetUri, {
3580
- ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3581
- cursor: cursor.cursor,
3582
- limit,
3583
- order: cursor.order,
3584
- ...cursor.query === void 0 ? {} : { query: cursor.query },
3585
- ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3586
- ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3587
- ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3588
- ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3589
- });
3590
- case "related":
3591
- return await listRelatedArchiveObjects2(document, cursor.targetUri, {
3592
- ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3593
- cursor: cursor.cursor,
3594
- ...cursor.evidenceLimit === void 0 ? {} : { evidenceLimit: cursor.evidenceLimit },
3595
- limit,
3596
- order: cursor.order,
3597
- ...cursor.query === void 0 ? {} : { query: cursor.query },
3598
- ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3599
- ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3600
- ...cursor.role === void 0 ? {} : { role: cursor.role },
3601
- ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3602
- ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3603
- });
3604
- }
3584
+ const read = async (document) => {
3585
+ switch (cursor.kind) {
3586
+ case "source-locators":
3587
+ return await listArchiveSourceLocators2(document, cursor.targetUri, {
3588
+ cursor: cursor.cursor,
3589
+ limit
3590
+ });
3591
+ case "collection":
3592
+ return await listArchiveCollection2(
3593
+ document,
3594
+ createCollectionOptions(cursor, limit)
3595
+ );
3596
+ case "search":
3597
+ return await findArchiveObjects2(
3598
+ document,
3599
+ cursor.query ?? "",
3600
+ await createFindOptions(cursor, limit)
3601
+ );
3602
+ case "evidence":
3603
+ return await listArchiveEvidence2(document, cursor.targetUri, {
3604
+ ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3605
+ cursor: cursor.cursor,
3606
+ limit,
3607
+ order: cursor.order,
3608
+ ...cursor.query === void 0 ? {} : { query: cursor.query },
3609
+ ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3610
+ ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3611
+ ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3612
+ ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3613
+ });
3614
+ case "related":
3615
+ return await listRelatedArchiveObjects2(document, cursor.targetUri, {
3616
+ ...cursor.chapters === null ? {} : { chapters: cursor.chapters },
3617
+ cursor: cursor.cursor,
3618
+ ...cursor.evidenceLimit === void 0 ? {} : { evidenceLimit: cursor.evidenceLimit },
3619
+ limit,
3620
+ order: cursor.order,
3621
+ ...cursor.query === void 0 ? {} : { query: cursor.query },
3622
+ ...cursor.queryMode === void 0 ? {} : { queryMode: cursor.queryMode },
3623
+ ...embeddingProvider === void 0 ? {} : { embeddingProvider },
3624
+ ...cursor.role === void 0 ? {} : { role: cursor.role },
3625
+ ...cursor.skipUnindexed === void 0 ? {} : { skipUnindexed: cursor.skipUnindexed },
3626
+ ...cursor.sourceContext === void 0 ? {} : { sourceContext: cursor.sourceContext }
3627
+ });
3605
3628
  }
3606
- );
3629
+ };
3630
+ return location.archiveFile.kind === "directory" ? await new WikiGraphDirectoryFile3(location.archiveFile).readDocument(read) : await new WikiGraphArchiveFile3(location.archiveFile).readDocument(read);
3607
3631
  }
3608
3632
  async function continueLibraryCursor(cursor, limit) {
3609
3633
  const embeddingProvider = cursor.kind === "evidence" || cursor.kind === "related" ? await resolveCursorEmbeddingProvider(cursor) : void 0;
@@ -4491,7 +4515,7 @@ var WikiGraphJobManager = class {
4491
4515
  return {
4492
4516
  job: await this.#backend.add({
4493
4517
  ...options,
4494
- archive: location.archiveFile
4518
+ archive: requireWikiGraphArchiveFile(location)
4495
4519
  }),
4496
4520
  target: location.target
4497
4521
  };
@@ -4523,7 +4547,9 @@ var WikiGraphJobManager = class {
4523
4547
  ...options.activeOnly === void 0 ? {} : { activeOnly: options.activeOnly },
4524
4548
  ...options.all === void 0 ? {} : { all: options.all },
4525
4549
  ...options.archive === void 0 ? {} : {
4526
- archive: (await resolveWikiGraphArchiveLocation(options.archive)).archiveFile
4550
+ archive: requireWikiGraphArchiveFile(
4551
+ await resolveWikiGraphArchiveLocation(options.archive)
4552
+ )
4527
4553
  }
4528
4554
  };
4529
4555
  const jobs = await this.#backend.list(normalized);
@@ -4548,7 +4574,7 @@ var WikiGraphJobManager = class {
4548
4574
  archiveJobs[0].archive
4549
4575
  );
4550
4576
  const chapters = await new WikiGraphArchiveFile6(
4551
- location.archiveFile
4577
+ requireWikiGraphArchiveFile(location)
4552
4578
  ).readDocument(async (document) => await listChapters3(document));
4553
4579
  const chaptersById = new Map(
4554
4580
  chapters.map((chapter) => [chapter.chapterId, chapter])
@@ -4591,7 +4617,7 @@ var WikiGraphJobManager = class {
4591
4617
  await validateQueueTargetConfig(target, llmJSON);
4592
4618
  const selectedExplicitly = options.chapterId !== void 0 || options.chapterIds !== void 0 || options.chapterPath !== void 0 || options.depth !== void 0;
4593
4619
  const candidates = await new WikiGraphArchiveFile6(
4594
- location.archiveFile
4620
+ requireWikiGraphArchiveFile(location)
4595
4621
  ).readDocument(async (document) => {
4596
4622
  const chapters = await listChapters3(document);
4597
4623
  const selected = await selectQueueChapters(
@@ -4635,7 +4661,7 @@ var WikiGraphJobManager = class {
4635
4661
  chapter: candidate.chapter,
4636
4662
  job: await this.#createHandle(
4637
4663
  await this.#backend.add({
4638
- archive: location.archiveFile,
4664
+ archive: requireWikiGraphArchiveFile(location),
4639
4665
  boost: options.boost ?? false,
4640
4666
  chapterId: candidate.chapter.chapterId,
4641
4667
  ...llmJSON === void 0 ? {} : { llmJSON },
@@ -4666,7 +4692,7 @@ var WikiGraphJobManager = class {
4666
4692
  await validateQueueTargetConfig(target, llmJSON);
4667
4693
  const selectedExplicitly = options.chapterId !== void 0 || options.chapterIds !== void 0 || options.chapterPath !== void 0 || options.depth !== void 0;
4668
4694
  const candidates = await new WikiGraphArchiveFile6(
4669
- location.archiveFile
4695
+ requireWikiGraphArchiveFile(location)
4670
4696
  ).readDocument(async (document) => {
4671
4697
  const chapters = await listChapters3(document);
4672
4698
  const selected = await selectQueueChapters(document, chapters, options);
@@ -5028,9 +5054,12 @@ import {
5028
5054
  replaceWikiGraphLibraryMetadata,
5029
5055
  resolveWikiGraphLibrary as resolveWikiGraphLibrary2,
5030
5056
  scanWikiGraphLibrary,
5031
- withWikiGraphLibraryLock
5057
+ withWikiGraphLibraryLock,
5058
+ writeWikgArchive as writeWikgArchive2
5032
5059
  } from "wiki-graph-core";
5033
- import { mkdir as mkdir3 } from "fs/promises";
5060
+ import { mkdir as mkdir3, mkdtemp as mkdtemp2, rm as rm4 } from "fs/promises";
5061
+ import { tmpdir as tmpdir2 } from "os";
5062
+ import { join as join5 } from "path";
5034
5063
  async function resolveQueryEmbeddingProvider(queryMode) {
5035
5064
  if (queryMode === "fts") return void 0;
5036
5065
  const provider = await createConfiguredEmbeddingProvider();
@@ -5120,7 +5149,18 @@ var WikiGraphLibraryManager = class {
5120
5149
  async () => {
5121
5150
  throwIfAborted(options.signal);
5122
5151
  const archive = await getWikiGraphLibraryArchive(parsedTarget);
5123
- const file = requireReadableLibraryArchiveFile(archive);
5152
+ const source = requireReadableLibraryArchiveFile(archive);
5153
+ let temporaryDirectory;
5154
+ const file = source.kind === "directory" ? await (async () => {
5155
+ temporaryDirectory = await mkdtemp2(
5156
+ join5(tmpdir2(), "wikigraph-library-read-")
5157
+ );
5158
+ const output = new NodeFile(
5159
+ join5(temporaryDirectory, "archive.wikg")
5160
+ );
5161
+ await writeWikgArchive2(source, output);
5162
+ return output;
5163
+ })() : source;
5124
5164
  const reader = await file.openReader();
5125
5165
  let active = true;
5126
5166
  try {
@@ -5141,6 +5181,8 @@ var WikiGraphLibraryManager = class {
5141
5181
  } finally {
5142
5182
  active = false;
5143
5183
  await reader.close();
5184
+ if (temporaryDirectory !== void 0)
5185
+ await rm4(temporaryDirectory, { force: true, recursive: true });
5144
5186
  }
5145
5187
  },
5146
5188
  options.signal === void 0 ? {} : { signal: options.signal }
@@ -5502,7 +5544,7 @@ function toLibrarySnapshot(record) {
5502
5544
  };
5503
5545
  }
5504
5546
  function toArchiveSnapshot(record) {
5505
- const { file: _file, ...snapshot } = record;
5547
+ const { file: _file, directory: _directory, ...snapshot } = record;
5506
5548
  return snapshot;
5507
5549
  }
5508
5550
  function toScanSnapshot(result) {
@@ -5535,12 +5577,12 @@ function requireReadableLibraryArchiveFile(archive) {
5535
5577
  `Wiki Graph library archive has a conflict and cannot be read: ${archive.uri}`
5536
5578
  );
5537
5579
  }
5538
- if (archive.file === void 0) {
5580
+ if (archive.file === void 0 && archive.directory === void 0) {
5539
5581
  throw new Error(
5540
5582
  `Wiki Graph library archive is unavailable: ${archive.uri}`
5541
5583
  );
5542
5584
  }
5543
- return archive.file;
5585
+ return archive.file ?? archive.directory;
5544
5586
  }
5545
5587
  var ARCHIVE_STREAM_CHUNK_SIZE = 64 * 1024;
5546
5588
  function createCallbackScopedArchiveStream(reader, isActive, signal) {