wiki-graph-sdk 0.7.0 → 0.8.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/archives.cjs CHANGED
@@ -34,22 +34,25 @@ __export(archive_exports, {
34
34
  WikiGraphArchiveExistsError: () => WikiGraphArchiveExistsError,
35
35
  WikiGraphArchiveHandle: () => WikiGraphArchiveHandle,
36
36
  WikiGraphArchiveManager: () => WikiGraphArchiveManager,
37
- createWikiGraphArchiveInspectReport: () => createWikiGraphArchiveInspectReport,
38
- resolveWikiGraphArchiveLocation: () => resolveWikiGraphArchiveLocation
37
+ WikiGraphArchiveOwnershipError: () => WikiGraphArchiveOwnershipError,
38
+ inspectWikiGraphArchive: () => inspectWikiGraphArchive
39
39
  });
40
40
  module.exports = __toCommonJS(archive_exports);
41
41
 
42
42
  // src/archive/archive.ts
43
- var import_crypto = require("crypto");
44
- var import_promises3 = require("fs/promises");
43
+ var import_crypto2 = require("crypto");
44
+ var import_promises4 = require("fs/promises");
45
45
  var import_os3 = require("os");
46
- var import_path3 = require("path");
46
+ var import_path5 = require("path");
47
47
  var import_stream = require("stream");
48
- var import_wiki_graph_core5 = require("wiki-graph-core");
48
+ var import_wiki_graph_core7 = require("wiki-graph-core");
49
49
 
50
50
  // src/conversions.ts
51
- var import_promises2 = require("fs/promises");
52
- var import_wiki_graph_core2 = require("wiki-graph-core");
51
+ var import_crypto = require("crypto");
52
+ var import_fs = require("fs");
53
+ var import_promises3 = require("fs/promises");
54
+ var import_path4 = require("path");
55
+ var import_wiki_graph_core3 = require("wiki-graph-core");
53
56
 
54
57
  // src/llm.ts
55
58
  var import_anthropic = require("@ai-sdk/anthropic");
@@ -369,42 +372,42 @@ var NodeDatabaseConnection = class {
369
372
  }
370
373
  database;
371
374
  async close() {
372
- await new Promise((resolve3, reject) => {
375
+ await new Promise((resolve4, reject) => {
373
376
  this.database.close((error) => {
374
377
  if (error) reject(error);
375
- else resolve3();
378
+ else resolve4();
376
379
  });
377
380
  });
378
381
  }
379
382
  async execute(sql) {
380
- await new Promise((resolve3, reject) => {
383
+ await new Promise((resolve4, reject) => {
381
384
  this.database.exec(sql, (error) => {
382
385
  if (error) reject(error);
383
- else resolve3();
386
+ else resolve4();
384
387
  });
385
388
  });
386
389
  }
387
390
  async queryAll(sql, params = []) {
388
- return await new Promise((resolve3, reject) => {
391
+ return await new Promise((resolve4, reject) => {
389
392
  this.database.all(sql, [...params], (error, rows) => {
390
393
  if (error) reject(error);
391
- else resolve3(rows);
394
+ else resolve4(rows);
392
395
  });
393
396
  });
394
397
  }
395
398
  async queryOne(sql, params = []) {
396
- return await new Promise((resolve3, reject) => {
399
+ return await new Promise((resolve4, reject) => {
397
400
  this.database.get(sql, [...params], (error, row) => {
398
401
  if (error) reject(error);
399
- else resolve3(row);
402
+ else resolve4(row);
400
403
  });
401
404
  });
402
405
  }
403
406
  async run(sql, params = []) {
404
- await new Promise((resolve3, reject) => {
407
+ await new Promise((resolve4, reject) => {
405
408
  this.database.run(sql, [...params], (error) => {
406
409
  if (error) reject(error);
407
- else resolve3();
410
+ else resolve4();
408
411
  });
409
412
  });
410
413
  }
@@ -418,10 +421,10 @@ async function openNativeNodeDatabase(file, flags) {
418
421
  if (path === void 0) {
419
422
  throw new TypeError("The Node database adapter requires a NodeFile");
420
423
  }
421
- return await new Promise((resolve3, reject) => {
424
+ return await new Promise((resolve4, reject) => {
422
425
  const database = new nodeSqlite3.Database(path, flags, (error) => {
423
426
  if (error) reject(error);
424
- else resolve3(database);
427
+ else resolve4(database);
425
428
  });
426
429
  });
427
430
  }
@@ -430,19 +433,19 @@ async function openNodeZip(file) {
430
433
  if (path === void 0) {
431
434
  throw new TypeError("The Node ZIP adapter requires a NodeFile");
432
435
  }
433
- const zipFile = await new Promise((resolve3, reject) => {
436
+ const zipFile = await new Promise((resolve4, reject) => {
434
437
  yauzl.open(
435
438
  path,
436
439
  { autoClose: false, lazyEntries: true },
437
440
  (error, opened) => {
438
441
  if (error || opened === void 0)
439
442
  reject(error ?? new Error("Cannot open ZIP"));
440
- else resolve3(opened);
443
+ else resolve4(opened);
441
444
  }
442
445
  );
443
446
  });
444
447
  const entries = await new Promise(
445
- (resolve3, reject) => {
448
+ (resolve4, reject) => {
446
449
  const discovered = /* @__PURE__ */ new Map();
447
450
  const rejectAndClose = (error) => {
448
451
  zipFile.close();
@@ -452,7 +455,7 @@ async function openNodeZip(file) {
452
455
  };
453
456
  const resolveEntries = () => {
454
457
  zipFile.off("error", rejectAndClose);
455
- resolve3(discovered);
458
+ resolve4(discovered);
456
459
  };
457
460
  zipFile.on("entry", (entry) => {
458
461
  if (!entry.fileName.endsWith("/")) {
@@ -562,12 +565,12 @@ async function collectDecodedNodeStreamRange(input, offset, length) {
562
565
  return Buffer.concat(chunks, collected);
563
566
  }
564
567
  async function openNodeZipEntryStream(zipFile, entry, options) {
565
- return await new Promise((resolve3, reject) => {
568
+ return await new Promise((resolve4, reject) => {
566
569
  const callback = (error, opened) => {
567
570
  if (error || opened === void 0) {
568
571
  reject(error ?? new Error(`Cannot read ZIP entry: ${entry.fileName}`));
569
572
  } else {
570
- resolve3(opened);
573
+ resolve4(opened);
571
574
  }
572
575
  };
573
576
  if (options === void 0) zipFile.openReadStream(entry, callback);
@@ -698,11 +701,11 @@ async function writeNodeStream(input, writer) {
698
701
  );
699
702
  }
700
703
  async function collectNodeStream(input) {
701
- return await new Promise((resolve3, reject) => {
704
+ return await new Promise((resolve4, reject) => {
702
705
  const chunks = [];
703
706
  input.on("data", (chunk) => chunks.push(chunk));
704
707
  input.once("error", reject);
705
- input.once("end", () => resolve3(Buffer.concat(chunks)));
708
+ input.once("end", () => resolve4(Buffer.concat(chunks)));
706
709
  });
707
710
  }
708
711
  var nodeWikiGraphPlatform = {
@@ -1255,6 +1258,164 @@ function resolveWikiGraphRuntimePath(path) {
1255
1258
  return (0, import_path2.isAbsolute)(path) ? path : (0, import_path2.resolve)(context.cwd, path);
1256
1259
  }
1257
1260
 
1261
+ // src/archive/target.ts
1262
+ var import_promises2 = require("fs/promises");
1263
+ var import_path3 = require("path");
1264
+ var import_wiki_graph_core2 = require("wiki-graph-core");
1265
+ var WikiGraphArchiveOwnershipError = class extends Error {
1266
+ code = "WIKI_GRAPH_ARCHIVE_OWNERSHIP_MISMATCH";
1267
+ constructor(message) {
1268
+ super(message);
1269
+ this.name = "WikiGraphArchiveOwnershipError";
1270
+ }
1271
+ };
1272
+ async function resolveWikiGraphArchiveLocation(target) {
1273
+ if (target.kind === "standalone") {
1274
+ return await resolveStandaloneLocation(target);
1275
+ }
1276
+ return await resolveLibraryLocation(target);
1277
+ }
1278
+ async function assertStandaloneWikiGraphArchivePath(inputPath) {
1279
+ const path = resolveWikiGraphRuntimePath(inputPath);
1280
+ const candidate = await canonicalPath(path);
1281
+ const candidateIdentity = await readFileIdentity(path);
1282
+ const libraries = await (0, import_wiki_graph_core2.listWikiGraphLibraries)();
1283
+ const matches = await findMatchingMembers(
1284
+ libraries,
1285
+ candidate,
1286
+ candidateIdentity
1287
+ );
1288
+ if (matches.length > 0) {
1289
+ const match = requireUnambiguousManagedMatch(path, matches);
1290
+ throw new WikiGraphArchiveOwnershipError(
1291
+ `Standalone archive path belongs to registered library ${(0, import_wiki_graph_core2.formatWikiGraphLibraryUri)(
1292
+ match.library.isDefault ? void 0 : match.library.publicId
1293
+ )}: ${path}. Use the library UUID URI ${match.archive.uri} instead.`
1294
+ );
1295
+ }
1296
+ for (const library of libraries) {
1297
+ const rootPath = getNodeResourcePath(library.folder);
1298
+ const root = await canonicalPath(rootPath);
1299
+ if (!isWithin(root, candidate)) continue;
1300
+ throw new WikiGraphArchiveOwnershipError(
1301
+ `Standalone archive path belongs to registered library ${(0, import_wiki_graph_core2.formatWikiGraphLibraryUri)(
1302
+ library.isDefault ? void 0 : library.publicId
1303
+ )}: ${path}. Scan the library, then address the archive by its library UUID URI.`
1304
+ );
1305
+ }
1306
+ return path;
1307
+ }
1308
+ async function resolveStandaloneLocation(target) {
1309
+ if (target.path.startsWith("wikg://lib/")) {
1310
+ throw new WikiGraphArchiveOwnershipError(
1311
+ `A library archive URI cannot be used as a standalone path: ${target.path}`
1312
+ );
1313
+ }
1314
+ const path = await assertStandaloneWikiGraphArchivePath(target.path);
1315
+ const normalized = {
1316
+ kind: "standalone",
1317
+ path,
1318
+ ...target.objectUri === void 0 ? {} : { objectUri: target.objectUri }
1319
+ };
1320
+ return {
1321
+ archiveFile: new NodeFile(path),
1322
+ archiveKey: path,
1323
+ archivePath: path,
1324
+ indexScope: { archiveKey: path, archivePath: path, kind: "archive-index" },
1325
+ locatedUri: (0, import_wiki_graph_core2.formatLocatedWikiGraphUri)(path, target.objectUri),
1326
+ target: normalized
1327
+ };
1328
+ }
1329
+ async function resolveLibraryLocation(target) {
1330
+ const parsedLocated = (0, import_wiki_graph_core2.parseLocatedWikiGraphUri)(target.uri);
1331
+ const archiveUri = parsedLocated.archivePath ?? target.uri;
1332
+ const parsed = (0, import_wiki_graph_core2.parseWikiGraphLibraryUri)(archiveUri);
1333
+ if (parsed?.kind !== "archive") {
1334
+ throw new WikiGraphArchiveOwnershipError(
1335
+ `Library archive target must be a UUID archive URI: ${target.uri}`
1336
+ );
1337
+ }
1338
+ await (0, import_wiki_graph_core2.resolveWikiGraphLibrary)(parsed);
1339
+ const archiveFile = await (0, import_wiki_graph_core2.resolveWikiGraphLibraryArchiveFile)(archiveUri);
1340
+ const normalized = {
1341
+ kind: "library",
1342
+ uri: target.uri
1343
+ };
1344
+ return {
1345
+ archiveFile,
1346
+ archiveKey: archiveUri,
1347
+ archivePath: archiveUri,
1348
+ indexScope: {
1349
+ archiveKey: archiveUri,
1350
+ archivePath: archiveUri,
1351
+ kind: "archive-index"
1352
+ },
1353
+ libraryArchiveTarget: parsed,
1354
+ locatedUri: target.uri,
1355
+ publicArchiveUri: archiveUri,
1356
+ target: normalized
1357
+ };
1358
+ }
1359
+ async function canonicalPath(path) {
1360
+ try {
1361
+ return await (0, import_promises2.realpath)(path);
1362
+ } catch {
1363
+ const absolute = (0, import_path3.resolve)(path);
1364
+ const parent = (0, import_path3.dirname)(absolute);
1365
+ return parent === absolute ? absolute : (0, import_path3.join)(await canonicalPath(parent), (0, import_path3.basename)(absolute));
1366
+ }
1367
+ }
1368
+ function isWithin(root, candidate) {
1369
+ const child = (0, import_path3.relative)(root, candidate);
1370
+ return child === "" || !child.startsWith("..") && !(0, import_path3.isAbsolute)(child);
1371
+ }
1372
+ async function findMatchingMembers(libraries, candidate, candidateIdentity) {
1373
+ const members = [];
1374
+ for (const library of libraries) {
1375
+ const archives = await (0, import_wiki_graph_core2.listWikiGraphLibraryArchives)({
1376
+ isDefault: library.isDefault,
1377
+ kind: "scope",
1378
+ ...library.isDefault ? {} : { publicId: library.publicId }
1379
+ });
1380
+ for (const archive of archives) {
1381
+ if (archive.file === void 0) continue;
1382
+ members.push({
1383
+ archive,
1384
+ library,
1385
+ memberPath: getNodeResourcePath(archive.file)
1386
+ });
1387
+ }
1388
+ }
1389
+ const exact = [];
1390
+ for (const member of members) {
1391
+ if (await canonicalPath(member.memberPath) === candidate) {
1392
+ exact.push(member);
1393
+ }
1394
+ }
1395
+ if (exact.length > 0 || candidateIdentity === void 0) return exact;
1396
+ const identityMatches = [];
1397
+ for (const member of members) {
1398
+ if (await readFileIdentity(member.memberPath) === candidateIdentity) {
1399
+ identityMatches.push(member);
1400
+ }
1401
+ }
1402
+ return identityMatches;
1403
+ }
1404
+ function requireUnambiguousManagedMatch(path, matches) {
1405
+ if (matches.length === 1) return matches[0];
1406
+ throw new WikiGraphArchiveOwnershipError(
1407
+ `Archive file identity is ambiguous across managed archives: ${path}. Use an explicit library UUID URI (${matches.map((match) => match.archive.uri).join(", ")}).`
1408
+ );
1409
+ }
1410
+ async function readFileIdentity(path) {
1411
+ try {
1412
+ const value = await (0, import_promises2.stat)(path);
1413
+ return `${value.dev}:${value.ino}`;
1414
+ } catch {
1415
+ return void 0;
1416
+ }
1417
+ }
1418
+
1258
1419
  // src/conversions.ts
1259
1420
  var WikiGraphConversionManager = class {
1260
1421
  #runtime;
@@ -1269,6 +1430,9 @@ var WikiGraphConversionManager = class {
1269
1430
  if (options.targetStage !== void 0 && options.output.format !== "wikg") {
1270
1431
  throw new Error("targetStage is only supported for wikg output.");
1271
1432
  }
1433
+ if (options.output.format === "pcex") {
1434
+ throw new Error("pcex is supported only as a conversion input.");
1435
+ }
1272
1436
  if (options.targetStage !== void 0 && options.input.format === "wikg") {
1273
1437
  throw new Error(
1274
1438
  "targetStage is only supported when creating a wikg archive."
@@ -1279,7 +1443,7 @@ var WikiGraphConversionManager = class {
1279
1443
  const config = await loadWikiGraphRuntimeConfig({
1280
1444
  ...options.llmJSON === void 0 ? {} : { llmJSON: options.llmJSON }
1281
1445
  });
1282
- const app = new import_wiki_graph_core2.WikiGraph({
1446
+ const app = new import_wiki_graph_core3.WikiGraph({
1283
1447
  ...options.verbose === true ? { verbose: true } : {},
1284
1448
  ...requiresLLM ? { llm: buildWikiGraphLLMOptions(config) } : {}
1285
1449
  });
@@ -1287,7 +1451,17 @@ var WikiGraphConversionManager = class {
1287
1451
  options.digestDirectory,
1288
1452
  requiresDigest
1289
1453
  );
1454
+ let chapterCount;
1455
+ let chapterPaths;
1290
1456
  const write = async (archive) => {
1457
+ if (requiresDigest) await options.onSourceImported?.();
1458
+ if (typeof archive.readToc === "function") {
1459
+ const toc = await archive.readToc();
1460
+ chapterCount = toc === void 0 ? void 0 : countTocChapters(toc.items);
1461
+ if (options.output.format === "wikg") {
1462
+ chapterPaths = toc === void 0 ? [] : collectTocChapterPaths(toc.items);
1463
+ }
1464
+ }
1291
1465
  await writeArchive(archive, options.output.path, options.output.format);
1292
1466
  };
1293
1467
  if (options.input.format === "wikg") {
@@ -1295,7 +1469,9 @@ var WikiGraphConversionManager = class {
1295
1469
  throw new Error("wikg input requires a file path.");
1296
1470
  }
1297
1471
  await app.openSession(
1298
- new NodeFile(resolveWikiGraphRuntimePath(options.input.path)),
1472
+ new NodeFile(
1473
+ await assertStandaloneWikiGraphArchivePath(options.input.path)
1474
+ ),
1299
1475
  write
1300
1476
  );
1301
1477
  } else if ("stream" in options.input) {
@@ -1320,16 +1496,29 @@ var WikiGraphConversionManager = class {
1320
1496
  };
1321
1497
  if (options.input.format === "epub") {
1322
1498
  await app.digestEpubSession(digestOptions, write);
1499
+ } else if (options.input.format === "pcex") {
1500
+ await app.digestPcexSession(
1501
+ {
1502
+ ...digestOptions,
1503
+ pdfDigest: options.input.sourcePdf.digest,
1504
+ ...options.input.sourcePdf.name === void 0 ? {} : { pdfName: options.input.sourcePdf.name }
1505
+ },
1506
+ write
1507
+ );
1323
1508
  } else if (options.input.format === "markdown") {
1324
1509
  await app.digestMarkdownSession(digestOptions, write);
1325
1510
  } else {
1326
1511
  await app.digestTxtSession(digestOptions, write);
1327
1512
  }
1328
1513
  }
1514
+ const sourceArtifact = await resolveSourceArtifact(options.input);
1329
1515
  return {
1516
+ ...chapterCount === void 0 ? {} : { chapterCount },
1517
+ ...chapterPaths === void 0 ? {} : { chapterPaths },
1330
1518
  inputFormat: options.input.format,
1331
1519
  outputFormat: options.output.format,
1332
- outputPath: options.output.path
1520
+ outputPath: options.output.path,
1521
+ ...sourceArtifact === void 0 ? {} : { sourceArtifact }
1333
1522
  };
1334
1523
  }
1335
1524
  };
@@ -1338,18 +1527,74 @@ async function prepareDigestDirectory(path, required) {
1338
1527
  if (!required || normalized === void 0 || normalized === "")
1339
1528
  return void 0;
1340
1529
  const resolved = resolveWikiGraphRuntimePath(normalized);
1341
- await (0, import_promises2.rm)(resolved, { force: true, recursive: true });
1530
+ await (0, import_promises3.rm)(resolved, { force: true, recursive: true });
1342
1531
  return new NodeDirectory(resolved);
1343
1532
  }
1344
1533
  async function writeArchive(archive, path, format) {
1345
- const file = new NodeFile(resolveWikiGraphRuntimePath(path));
1534
+ const outputPath = format === "wikg" ? await assertStandaloneWikiGraphArchivePath(path) : resolveWikiGraphRuntimePath(path);
1535
+ const file = new NodeFile(outputPath);
1346
1536
  if (format === "epub") await archive.exportEpub(file);
1347
1537
  else if (format === "wikg") await archive.saveAs(file);
1538
+ else if (format === "pcex") throw new Error("pcex output is not supported.");
1348
1539
  else await archive.exportText(file);
1349
1540
  }
1541
+ async function resolveSourceArtifact(input) {
1542
+ if (!("path" in input) || input.format === "wikg") return void 0;
1543
+ if (input.format === "pcex") {
1544
+ return {
1545
+ digest: input.sourcePdf.digest.trim().toLowerCase(),
1546
+ mediaType: "application/pdf",
1547
+ ...input.sourcePdf.name === void 0 ? {} : { name: input.sourcePdf.name }
1548
+ };
1549
+ }
1550
+ if (input.format !== "epub") return void 0;
1551
+ const path = resolveWikiGraphRuntimePath(input.path);
1552
+ const hash = (0, import_crypto.createHash)("sha256");
1553
+ try {
1554
+ for await (const chunk of (0, import_fs.createReadStream)(path)) {
1555
+ hash.update(chunk);
1556
+ }
1557
+ } catch (error) {
1558
+ if (isNodeErrorCode(error, "ENOENT")) return void 0;
1559
+ throw error;
1560
+ }
1561
+ return {
1562
+ digest: hash.digest("hex"),
1563
+ mediaType: "application/epub+zip",
1564
+ name: (0, import_path4.basename)(path)
1565
+ };
1566
+ }
1567
+ function isNodeErrorCode(error, code) {
1568
+ return typeof error === "object" && error !== null && "code" in error && error.code === code;
1569
+ }
1570
+ function countTocChapters(items) {
1571
+ let count = 0;
1572
+ for (const item of items) {
1573
+ if (item.serialId !== void 0) count += 1;
1574
+ count += countTocChapters(
1575
+ item.children
1576
+ );
1577
+ }
1578
+ return count;
1579
+ }
1580
+ function collectTocChapterPaths(items, ancestorKeys = []) {
1581
+ const paths = [];
1582
+ for (const item of items) {
1583
+ const key = item.key ?? `chapter-${item.serialId ?? "group"}`;
1584
+ const chapterPath = [...ancestorKeys, key];
1585
+ if (item.serialId !== void 0) paths.push(chapterPath.join("/"));
1586
+ paths.push(
1587
+ ...collectTocChapterPaths(
1588
+ item.children,
1589
+ chapterPath
1590
+ )
1591
+ );
1592
+ }
1593
+ return paths;
1594
+ }
1350
1595
 
1351
1596
  // src/query-runtime.ts
1352
- var import_wiki_graph_core3 = require("wiki-graph-core");
1597
+ var import_wiki_graph_core4 = require("wiki-graph-core");
1353
1598
 
1354
1599
  // src/embedding.ts
1355
1600
  var import_ai = require("ai");
@@ -1497,7 +1742,7 @@ async function createConfiguredEmbeddingProvider() {
1497
1742
  async function withConfiguredWikimediaResolver(objectUri, operation) {
1498
1743
  if (!objectUri.endsWith("/wikipage")) return await operation({});
1499
1744
  const config = await loadWikiGraphRuntimeConfig();
1500
- const resolver = await (0, import_wiki_graph_core3.openWikimediaResolver)(
1745
+ const resolver = await (0, import_wiki_graph_core4.openWikimediaResolver)(
1501
1746
  config.wikimedia === void 0 ? { kind: "local" } : {
1502
1747
  endpoint: config.wikimedia.endpoint,
1503
1748
  kind: "remote",
@@ -1512,490 +1757,128 @@ async function withConfiguredWikimediaResolver(objectUri, operation) {
1512
1757
  }
1513
1758
 
1514
1759
  // src/archive/inspect.ts
1515
- var import_wiki_graph_core4 = require("wiki-graph-core");
1516
-
1517
- // src/planning.ts
1518
- var DEFAULT_GENERATION_JOB_CONCURRENCY = 3;
1519
- var DEFAULT_GENERATION_REQUEST_CONCURRENCY = 6;
1520
- function planGenerationTask(task, words, chapters, concurrent, model) {
1521
- const profile = getGenerationPlanningProfile(task);
1522
- const calls = Math.max(chapters, Math.ceil(words / profile.wordsPerCall));
1523
- const effectiveConcurrency = Math.max(
1524
- 1,
1525
- task === "reading-graph" ? 1 : concurrent.request
1526
- );
1527
- const callBatches = Math.ceil(calls / effectiveConcurrency);
1528
- const wordSeconds = words * profile.secondsPerWord;
1529
- return {
1530
- model,
1531
- timeSeconds: {
1532
- max: Math.ceil(callBatches * profile.maxSecondsPerCall + wordSeconds),
1533
- min: Math.ceil(callBatches * profile.minSecondsPerCall + wordSeconds)
1534
- },
1535
- tokens: {
1536
- cacheableInput: Math.ceil(words * profile.cacheableInputTokenPerWord),
1537
- input: Math.ceil(words * profile.inputTokenPerWord),
1538
- output: Math.ceil(words * profile.outputTokenPerWord)
1539
- }
1760
+ var import_wiki_graph_core5 = require("wiki-graph-core");
1761
+ async function inspectWikiGraphArchive(document, options) {
1762
+ const [allChapters, indexCacheCurrent, summaryWords, ...coverage] = await Promise.all([
1763
+ (0, import_wiki_graph_core5.listChapters)(document),
1764
+ (0, import_wiki_graph_core5.isArchiveSearchIndexCurrent)(document),
1765
+ readSummaryWords(document),
1766
+ document.indexArtifacts.listCoverage("fts"),
1767
+ document.indexArtifacts.listCoverage("embedding-source"),
1768
+ document.indexArtifacts.listCoverage("embedding-summary")
1769
+ ]);
1770
+ const chapters = selectChapters(allChapters, options.chapterId);
1771
+ const coverageByKind = {
1772
+ fts: byChapterId(coverage[0]),
1773
+ sourceEmbedding: byChapterId(coverage[1]),
1774
+ summaryEmbedding: byChapterId(coverage[2])
1540
1775
  };
1541
- }
1542
- function createGenerationPerformanceHints(input) {
1543
- if (!input.hasJobWork) {
1544
- return [];
1545
- }
1546
- const hints = [];
1547
- const recommendedRequest = input.concurrent.request < 4 ? DEFAULT_GENERATION_REQUEST_CONCURRENCY : input.concurrent.request < 8 ? 8 : void 0;
1548
- if (input.hasRequestWork && recommendedRequest !== void 0) {
1549
- hints.push({
1550
- configKey: "request",
1551
- configValue: recommendedRequest,
1552
- current: input.concurrent.request,
1553
- kind: "request",
1554
- message: "LLM request concurrency can often be higher. Use at least 4; 6-8 is usually faster when the provider allows it.",
1555
- recommended: recommendedRequest
1556
- });
1557
- }
1558
- if (input.concurrent.job < 4 && input.chapters > 1) {
1559
- hints.push({
1560
- configKey: "job",
1561
- configValue: 4,
1562
- current: input.concurrent.job,
1563
- kind: "job",
1564
- message: "Multiple chapters need work and job concurrency is below 4. Raising it lets chapter jobs run in parallel.",
1565
- recommended: 4
1566
- });
1567
- }
1568
- return hints;
1569
- }
1570
- function formatGenerationPlanningModel(llm) {
1571
- if (llm?.model === void 0) {
1572
- return "not configured";
1573
- }
1574
- return llm.provider === void 0 ? llm.model : `${llm.provider}/${llm.model}`;
1575
- }
1576
- function getGenerationPlanningProfile(task) {
1577
- switch (task) {
1578
- case "index-fts":
1579
- return {
1580
- cacheableInputTokenPerWord: 0,
1581
- inputTokenPerWord: 0,
1582
- maxSecondsPerCall: 1,
1583
- minSecondsPerCall: 0,
1584
- outputTokenPerWord: 0,
1585
- secondsPerWord: 5e-4,
1586
- wordsPerCall: 1e5
1587
- };
1588
- case "index-embedding-source":
1589
- case "index-embedding-summary":
1590
- return {
1591
- cacheableInputTokenPerWord: 0,
1592
- inputTokenPerWord: 0,
1593
- maxSecondsPerCall: 10,
1594
- minSecondsPerCall: 1,
1595
- outputTokenPerWord: 0,
1596
- secondsPerWord: 2e-3,
1597
- wordsPerCall: 1e4
1598
- };
1599
- case "knowledge-graph":
1600
- return {
1601
- cacheableInputTokenPerWord: 60,
1602
- inputTokenPerWord: 80,
1603
- maxSecondsPerCall: 35,
1604
- minSecondsPerCall: 15,
1605
- outputTokenPerWord: 25,
1606
- secondsPerWord: 0.02,
1607
- wordsPerCall: 35
1608
- };
1609
- case "reading-graph":
1610
- return {
1611
- cacheableInputTokenPerWord: 20,
1612
- inputTokenPerWord: 25,
1613
- maxSecondsPerCall: 45,
1614
- minSecondsPerCall: 15,
1615
- outputTokenPerWord: 4,
1616
- secondsPerWord: 0.01,
1617
- wordsPerCall: 160
1618
- };
1619
- case "reading-summary":
1776
+ const inspections = await Promise.all(
1777
+ chapters.map(async (chapter) => {
1778
+ const serial = await document.serials.getById(chapter.chapterId);
1620
1779
  return {
1621
- cacheableInputTokenPerWord: 45,
1622
- inputTokenPerWord: 55,
1623
- maxSecondsPerCall: 25,
1624
- minSecondsPerCall: 10,
1625
- outputTokenPerWord: 4,
1626
- secondsPerWord: 0.01,
1627
- wordsPerCall: 110
1780
+ capabilities: {
1781
+ knowledgeGraph: {
1782
+ completed: serial?.knowledgeGraphReady === true
1783
+ },
1784
+ readingGraph: { completed: serial?.topologyReady === true },
1785
+ readingSummary: { completed: chapter.stage === "summarized" }
1786
+ },
1787
+ chapter,
1788
+ indexes: {
1789
+ fts: inspectArtifact(
1790
+ "fts",
1791
+ coverageByKind.fts.get(chapter.chapterId)
1792
+ ),
1793
+ sourceEmbedding: inspectArtifact(
1794
+ "embedding-source",
1795
+ coverageByKind.sourceEmbedding.get(chapter.chapterId)
1796
+ ),
1797
+ summaryEmbedding: inspectArtifact(
1798
+ "embedding-summary",
1799
+ coverageByKind.summaryEmbedding.get(chapter.chapterId)
1800
+ )
1801
+ },
1802
+ revision: serial?.revision ?? 0,
1803
+ summaryWords: summaryWords.get(chapter.chapterId) ?? 0
1628
1804
  };
1629
- }
1630
- }
1631
-
1632
- // src/archive/inspect.ts
1633
- async function createWikiGraphArchiveInspectReport(document, options) {
1634
- const { archiveUri } = options;
1635
- const [
1636
- chapters,
1637
- summaryWords,
1638
- indexCacheCurrent,
1639
- config,
1640
- ftsArtifactCoverage,
1641
- sourceEmbeddingArtifactCoverage,
1642
- summaryEmbeddingArtifactCoverage
1643
- ] = await Promise.all([
1644
- readInspectChapters(document, options.chapterId),
1645
- readSummaryWords(document, options.chapterId),
1646
- (0, import_wiki_graph_core4.isArchiveSearchIndexCurrent)(document),
1647
- loadWikiGraphRuntimeConfig(),
1648
- readIndexArtifactCoverage(document, "fts", options.chapterId),
1649
- readIndexArtifactCoverage(document, "embedding-source", options.chapterId),
1650
- readIndexArtifactCoverage(document, "embedding-summary", options.chapterId)
1651
- ]);
1652
- const selectedChapter = options.chapterId === void 0 ? void 0 : chapters[0];
1653
- const scopeUri = selectedChapter === void 0 ? archiveUri : formatInspectLocatedChapterUri(archiveUri, selectedChapter);
1654
- const concurrent = {
1655
- job: config.concurrent?.job ?? DEFAULT_GENERATION_JOB_CONCURRENCY,
1656
- request: config.concurrent?.request ?? DEFAULT_GENERATION_REQUEST_CONCURRENCY
1657
- };
1658
- const planningModel = formatGenerationPlanningModel(config.llm);
1659
- const contentChapters = chapters.filter(
1660
- (chapter) => chapter.stage !== "planned"
1661
- );
1662
- const sourceWords = sumWords(contentChapters);
1663
- const readingGraphCovered = contentChapters.filter(
1664
- (chapter) => chapter.readingGraphReady
1665
- );
1666
- const knowledgeGraphCovered = contentChapters.filter(
1667
- (chapter) => chapter.knowledgeGraphReady
1668
- );
1669
- const summaryCovered = contentChapters.filter(
1670
- (chapter) => chapter.summaryReady
1671
- );
1672
- const ftsArtifactCovered = filterChaptersByCurrentIndexArtifact(
1673
- contentChapters,
1674
- ftsArtifactCoverage
1675
- );
1676
- const sourceEmbeddingArtifactCovered = filterChaptersByCurrentIndexArtifact(
1677
- contentChapters,
1678
- sourceEmbeddingArtifactCoverage
1679
- );
1680
- const summaryEmbeddingArtifactCovered = filterChaptersByCurrentIndexArtifact(
1681
- contentChapters,
1682
- summaryEmbeddingArtifactCoverage
1683
- );
1684
- const queryBlockedContentChapters = contentChapters.filter(
1685
- (chapter) => !ftsArtifactCovered.includes(chapter) && !sourceEmbeddingArtifactCovered.includes(chapter)
1686
- );
1687
- const queryBlockedChapters = queryBlockedContentChapters.map(
1688
- (chapter) => createInspectChapterReference(archiveUri, chapter)
1805
+ })
1689
1806
  );
1690
- const queryReady = queryBlockedChapters.length === 0;
1691
- const improvements = createInspectImprovements({
1692
- concurrent,
1693
- contentChapters,
1694
- indexCacheCurrent,
1695
- planningModel,
1696
- queryBlockedChapters: queryBlockedContentChapters,
1697
- summaryCovered,
1698
- knowledgeGraphCovered,
1699
- readingGraphCovered
1700
- });
1701
- const performanceHints = createGenerationPerformanceHints({
1702
- chapters: Math.max(
1703
- 0,
1704
- ...improvements.map((improvement) => improvement.missingChapters ?? 0)
1705
- ),
1706
- concurrent,
1707
- hasJobWork: improvements.some(
1708
- (improvement) => improvement.planning !== void 0
1709
- ),
1710
- hasRequestWork: improvements.some(
1711
- (improvement) => improvement.planning !== void 0
1712
- )
1713
- });
1807
+ const selected = inspections[0];
1714
1808
  return {
1715
- archiveUri,
1716
- uri: scopeUri,
1809
+ archiveUri: options.archiveUri,
1810
+ chapters: inspections,
1811
+ localIndexCache: { current: indexCacheCurrent },
1717
1812
  scope: options.chapterId === void 0 ? { type: "archive" } : { chapterId: options.chapterId, type: "chapter" },
1718
- content: {
1719
- chapters: {
1720
- content: contentChapters.length,
1721
- planned: chapters.length - contentChapters.length,
1722
- total: chapters.length
1723
- },
1724
- sourceWords,
1725
- summaryWords
1726
- },
1727
- localIndexCache: {
1728
- current: indexCacheCurrent,
1729
- ...!queryReady ? {
1730
- blockedBy: "missing-search-artifacts",
1731
- impact: "The cache cannot be built until every content chapter has a current FTS or source embedding artifact."
1732
- } : indexCacheCurrent ? {} : {
1733
- impact: "The next archive query may need to sync index cache first.",
1734
- resource: "local CPU/disk time only; no provider calls."
1735
- },
1736
- status: !queryReady ? "blocked" : indexCacheCurrent ? "current" : "missing-or-outdated",
1737
- syncAvailable: queryReady && !indexCacheCurrent
1738
- },
1739
- coverage: {
1740
- ftsIndexArtifact: createInspectCoverage(
1741
- ftsArtifactCovered,
1742
- contentChapters
1743
- ),
1744
- knowledgeGraph: createInspectCoverage(
1745
- knowledgeGraphCovered,
1746
- contentChapters
1747
- ),
1748
- readingGraph: createInspectCoverage(readingGraphCovered, contentChapters),
1749
- summary: createInspectCoverage(summaryCovered, contentChapters),
1750
- sourceEmbeddingIndexArtifact: createInspectCoverage(
1751
- sourceEmbeddingArtifactCovered,
1752
- contentChapters
1753
- ),
1754
- summaryEmbeddingIndexArtifact: createInspectCoverage(
1755
- summaryEmbeddingArtifactCovered,
1756
- contentChapters
1757
- )
1758
- },
1759
- query: {
1760
- ready: queryReady
1761
- },
1762
- searchArtifacts: {
1763
- blockedChapters: queryBlockedChapters,
1764
- ...!queryReady ? {
1765
- resource: "local CPU/disk time only; no provider calls."
1766
- } : {},
1767
- ready: queryReady,
1768
- status: queryReady ? "ready" : "incomplete"
1769
- },
1770
- retrievalGuidance: formatRetrievalGuidance({
1771
- indexCacheCurrent,
1772
- knowledgeGraphCovered,
1773
- readingGraphCovered,
1774
- contentChapters,
1775
- queryReady,
1776
- sourceWords
1777
- }),
1778
- improvements,
1779
- performanceHints,
1780
- summaryCoverageNote: "Summary coverage means a current summary artifact exists for the chapter; it does not guarantee that every source topic is represented in the summary text."
1813
+ uri: selected === void 0 || options.chapterId === void 0 ? options.archiveUri : formatLocatedChapterUri(options.archiveUri, selected.chapter)
1781
1814
  };
1782
1815
  }
1783
- function createInspectChapterReference(archiveUri, chapter) {
1784
- return {
1785
- chapterId: chapter.chapterId,
1786
- locatedUri: formatInspectLocatedChapterUri(archiveUri, chapter),
1787
- title: chapter.title,
1788
- uri: chapter.uri
1789
- };
1790
- }
1791
- function formatInspectLocatedChapterUri(archiveUri, chapter) {
1792
- return `${archiveUri.replace(/\/+$/u, "")}/${chapter.uri.replace(/^wikg:\/\//u, "")}`;
1793
- }
1794
- async function readInspectChapters(document, chapterId) {
1795
- const chapters = chapterId === void 0 ? await (0, import_wiki_graph_core4.listChapters)(document) : (await (0, import_wiki_graph_core4.listChapters)(document)).filter(
1816
+ function selectChapters(chapters, chapterId) {
1817
+ if (chapterId === void 0) return chapters;
1818
+ const selected = chapters.filter(
1796
1819
  (chapter) => chapter.chapterId === chapterId
1797
1820
  );
1798
- if (chapterId !== void 0 && chapters.length === 0) {
1821
+ if (selected.length === 0) {
1799
1822
  throw new Error(`Chapter ${chapterId} does not exist.`);
1800
1823
  }
1801
- return await Promise.all(
1802
- chapters.map(async (chapter) => {
1803
- const serial = await document.serials.getById(chapter.chapterId);
1804
- return {
1805
- ...chapter,
1806
- knowledgeGraphReady: serial?.knowledgeGraphReady === true,
1807
- readingGraphReady: serial?.topologyReady === true,
1808
- summaryReady: chapter.stage === "summarized"
1809
- };
1810
- })
1811
- );
1824
+ return selected;
1812
1825
  }
1813
- async function readSummaryWords(document, chapterId) {
1814
- return await document.readDatabase(
1815
- async (database) => await database.queryOne(
1816
- `
1817
- SELECT COALESCE(SUM(words_count), 0) AS words
1818
- FROM text_sentence_records
1819
- WHERE kind = 2
1820
- ${chapterId === void 0 ? "" : "AND chapter_id = ?"}
1821
- `,
1822
- chapterId === void 0 ? void 0 : [chapterId],
1823
- (row) => Number(row.words)
1824
- ) ?? 0
1825
- );
1826
+ function byChapterId(coverage) {
1827
+ return new Map(coverage.map((item) => [item.serialId, item]));
1826
1828
  }
1827
- async function readIndexArtifactCoverage(document, kind, chapterId) {
1828
- const coverage = await document.indexArtifacts.listCoverage(kind);
1829
- return chapterId === void 0 ? coverage : coverage.filter((item) => item.serialId === chapterId);
1830
- }
1831
- function filterChaptersByCurrentIndexArtifact(chapters, coverage) {
1832
- const currentChapterIds = new Set(
1833
- coverage.filter((item) => item.current).map((item) => item.serialId)
1834
- );
1835
- return chapters.filter((chapter) => currentChapterIds.has(chapter.chapterId));
1836
- }
1837
- function createInspectCoverage(covered, total) {
1838
- const coveredWords = sumWords(covered);
1839
- const totalWords = sumWords(total);
1829
+ function inspectArtifact(kind, coverage) {
1840
1830
  return {
1841
- coveredChapters: covered.length,
1842
- coveredWords,
1843
- percent: formatPercent(coveredWords, totalWords),
1844
- totalChapters: total.length,
1845
- totalWords
1831
+ current: coverage?.current === true,
1832
+ exists: coverage?.sourceRevision !== void 0,
1833
+ kind,
1834
+ ...coverage?.sourceRevision === void 0 ? {} : { sourceRevision: coverage.sourceRevision }
1846
1835
  };
1847
1836
  }
1848
- function formatRetrievalGuidance(input) {
1849
- if (input.sourceWords === 0 || input.contentChapters.length === 0) {
1850
- return [
1851
- "Source content: empty.",
1852
- "Add source text before using query coverage or graph-based retrieval."
1853
- ];
1854
- }
1855
- const lines = [
1856
- input.queryReady ? `Query: available; local index cache is ${input.indexCacheCurrent ? "current" : "missing or outdated and can be synced from artifacts"}.` : "Query: unavailable until every content chapter has a current FTS or source embedding artifact."
1857
- ];
1858
- lines.push(
1859
- formatObjectSearchGuidance(
1860
- "Reading Graph object retrieval",
1861
- input.readingGraphCovered,
1862
- input.contentChapters
1863
- )
1864
- );
1865
- lines.push(
1866
- formatObjectSearchGuidance(
1867
- "Entity/triple retrieval",
1868
- input.knowledgeGraphCovered,
1869
- input.contentChapters
1837
+ async function readSummaryWords(document) {
1838
+ const rows = await document.readDatabase(
1839
+ async (database) => await database.queryAll(
1840
+ `
1841
+ SELECT chapter_id, COALESCE(SUM(words_count), 0) AS words
1842
+ FROM text_sentence_records
1843
+ WHERE kind = 2
1844
+ GROUP BY chapter_id
1845
+ `,
1846
+ void 0,
1847
+ (row) => ({
1848
+ chapterId: Number(row.chapter_id),
1849
+ words: Number(row.words)
1850
+ })
1870
1851
  )
1871
1852
  );
1872
- return lines;
1873
- }
1874
- function formatObjectSearchGuidance(label, covered, total) {
1875
- const coveredWords = sumWords(covered);
1876
- const totalWords = sumWords(total);
1877
- const ratio = totalWords === 0 ? 0 : coveredWords / totalWords;
1878
- const coverage = formatPercent(coveredWords, totalWords);
1879
- if (ratio >= 1) {
1880
- return `${label}: covers all source content; it can represent the full scope for object retrieval.`;
1881
- }
1882
- if (ratio >= 0.9) {
1883
- return `${label}: covers ${coverage}; use it as the main path, but source --query is needed for uncovered content.`;
1884
- }
1885
- if (ratio >= 0.5) {
1886
- return `${label}: covers ${coverage}; use it as leads, not as a full-scope substitute for source --query.`;
1887
- }
1888
- return `${label}: covers ${coverage}; build missing graph coverage before relying on object retrieval.`;
1889
- }
1890
- function createInspectImprovements(input) {
1891
- const improvements = [];
1892
- if (input.contentChapters.length === 0) {
1893
- improvements.push({
1894
- kind: "add-source",
1895
- recommendation: "No source content is available yet; add or import source text before graph or summary generation.",
1896
- title: "Add source content"
1897
- });
1898
- return improvements;
1899
- }
1900
- if (input.queryBlockedChapters.length > 0) {
1901
- improvements.push({
1902
- kind: "build",
1903
- missingChapters: input.queryBlockedChapters.length,
1904
- missingWords: sumWords(input.queryBlockedChapters),
1905
- recommendation: "Build local FTS artifacts to make this scope queryable. This does not call a provider.",
1906
- task: "index-fts",
1907
- title: "Enable query with local FTS"
1908
- });
1909
- } else if (!input.indexCacheCurrent) {
1910
- improvements.push({
1911
- kind: "sync-index",
1912
- recommendation: "Sync the index cache from chapter index artifacts so query starts without a lazy cache rebuild.",
1913
- title: "Sync index cache"
1914
- });
1915
- }
1916
- improvements.push(
1917
- ...createGraphImprovement({
1918
- concurrent: input.concurrent,
1919
- covered: input.readingGraphCovered,
1920
- planningModel: input.planningModel,
1921
- task: "reading-graph",
1922
- title: "Complete Reading Graph coverage",
1923
- total: input.contentChapters
1924
- })
1925
- );
1926
- improvements.push(
1927
- ...createGraphImprovement({
1928
- concurrent: input.concurrent,
1929
- covered: input.knowledgeGraphCovered,
1930
- planningModel: input.planningModel,
1931
- task: "knowledge-graph",
1932
- title: "Complete Knowledge Graph coverage",
1933
- total: input.contentChapters
1934
- })
1935
- );
1936
- improvements.push(
1937
- ...createGraphImprovement({
1938
- concurrent: input.concurrent,
1939
- covered: input.summaryCovered,
1940
- planningModel: input.planningModel,
1941
- task: "reading-summary",
1942
- title: "Complete Summary coverage",
1943
- total: input.contentChapters
1944
- })
1945
- );
1946
- return improvements;
1853
+ return new Map(rows.map((row) => [row.chapterId, row.words]));
1947
1854
  }
1948
- function createGraphImprovement(input) {
1949
- const coveredIds = new Set(input.covered.map((chapter) => chapter.chapterId));
1950
- const missing = input.total.filter(
1951
- (chapter) => !coveredIds.has(chapter.chapterId)
1952
- );
1953
- if (missing.length === 0) {
1954
- return [];
1955
- }
1956
- const missingWords = sumWords(missing);
1957
- return [
1958
- {
1959
- kind: "build",
1960
- missingChapters: missing.length,
1961
- missingWords,
1962
- planning: planGenerationTask(
1963
- input.task,
1964
- missingWords,
1965
- missing.length,
1966
- input.concurrent,
1967
- input.planningModel
1968
- ),
1969
- recommendation: formatImprovementRecommendation(
1970
- input.covered,
1971
- input.total,
1972
- missingWords
1973
- ),
1974
- task: input.task,
1975
- title: input.title
1976
- }
1977
- ];
1978
- }
1979
- function formatImprovementRecommendation(covered, total, missingWords) {
1980
- const totalWords = sumWords(total);
1981
- const coveredWords = sumWords(covered);
1982
- if (totalWords === 0) {
1983
- return "No source content is available.";
1984
- }
1985
- if (total.length === 1 || coveredWords / totalWords >= 0.9 || missingWords <= 1e3) {
1986
- return "Queue the full scope to finish the remaining gap.";
1987
- }
1988
- return "Queue selected chapters first if only part of the scope matters.";
1989
- }
1990
- function sumWords(chapters) {
1991
- return chapters.reduce((total, chapter) => total + chapter.words, 0);
1855
+ function formatLocatedChapterUri(archiveUri, chapter) {
1856
+ return `${archiveUri.replace(/\/+$/u, "")}/${chapter.uri.replace(/^wikg:\/\//u, "")}`;
1992
1857
  }
1993
- function formatPercent(numerator, denominator) {
1994
- if (denominator === 0) {
1995
- return "n/a";
1858
+
1859
+ // src/archive/write.ts
1860
+ var import_wiki_graph_core6 = require("wiki-graph-core");
1861
+ async function writeWikiGraphArchiveLocation(location, operation, options = {}) {
1862
+ const cacheOnly = options.searchIndexWritebackPolicy === "cache";
1863
+ const write = async (archiveFile = location.archiveFile) => await new import_wiki_graph_core6.WikiGraphArchiveFile(archiveFile).write(operation, {
1864
+ ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
1865
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
1866
+ invalidateContinuationCursors: options.invalidateContinuationCursors
1867
+ },
1868
+ ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
1869
+ });
1870
+ if (location.libraryArchiveTarget === void 0 || cacheOnly || options.refreshLibraryIndex === false) {
1871
+ return await write();
1996
1872
  }
1997
- const percent = numerator / denominator * 100;
1998
- return `${Number.isInteger(percent) ? percent.toFixed(0) : percent.toFixed(1)}%`;
1873
+ return await (0, import_wiki_graph_core6.writeWikiGraphLibraryArchive)({
1874
+ additionalDerivedStateKeys: [location.archiveKey, location.archivePath],
1875
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
1876
+ invalidateContinuationCursors: options.invalidateContinuationCursors
1877
+ },
1878
+ ...options.onIndexSyncError === void 0 ? {} : { onIndexSyncError: options.onIndexSyncError },
1879
+ operation: async (archiveFile) => await write(archiveFile),
1880
+ target: location.libraryArchiveTarget
1881
+ });
1999
1882
  }
2000
1883
 
2001
1884
  // src/archive/archive.ts
@@ -2015,13 +1898,13 @@ var WikiGraphArchiveManager = class {
2015
1898
  }
2016
1899
  async create(options) {
2017
1900
  return await this.#runtime.run(async () => {
2018
- const path = resolveWikiGraphRuntimePath(options.path);
1901
+ const path = await assertStandaloneWikiGraphArchivePath(options.path);
2019
1902
  if (options.replace !== true && await nodePathExists(path)) {
2020
1903
  throw new WikiGraphArchiveExistsError(path);
2021
1904
  }
2022
- const outputPath = (0, import_path3.join)(
2023
- (0, import_path3.dirname)(path),
2024
- `.${(0, import_path3.basename)(path)}.${(0, import_crypto.randomUUID)()}.tmp.wikg`
1905
+ const outputPath = (0, import_path5.join)(
1906
+ (0, import_path5.dirname)(path),
1907
+ `.${(0, import_path5.basename)(path)}.${(0, import_crypto2.randomUUID)()}.tmp.wikg`
2025
1908
  );
2026
1909
  try {
2027
1910
  if (options.importPath === void 0) {
@@ -2035,10 +1918,10 @@ var WikiGraphArchiveManager = class {
2035
1918
  });
2036
1919
  }
2037
1920
  if (options.replace === true) {
2038
- await (0, import_promises3.rename)(outputPath, path);
1921
+ await (0, import_promises4.rename)(outputPath, path);
2039
1922
  } else {
2040
1923
  try {
2041
- await (0, import_promises3.link)(outputPath, path);
1924
+ await (0, import_promises4.link)(outputPath, path);
2042
1925
  } catch (error) {
2043
1926
  if (isNodeEEXISTError(error)) {
2044
1927
  throw new WikiGraphArchiveExistsError(path);
@@ -2046,15 +1929,15 @@ var WikiGraphArchiveManager = class {
2046
1929
  throw error;
2047
1930
  }
2048
1931
  }
2049
- return { locatedUri: (0, import_wiki_graph_core5.formatLocatedWikiGraphUri)(path), path };
1932
+ return { locatedUri: (0, import_wiki_graph_core7.formatLocatedWikiGraphUri)(path), path };
2050
1933
  } finally {
2051
- await (0, import_promises3.rm)(outputPath, { force: true, recursive: true });
1934
+ await (0, import_promises4.rm)(outputPath, { force: true, recursive: true });
2052
1935
  }
2053
1936
  }, options.signal);
2054
1937
  }
2055
- async open(uriOrPath, options = {}) {
1938
+ async open(target, options = {}) {
2056
1939
  const location = await this.#runtime.run(
2057
- async () => await resolveWikiGraphArchiveLocation(uriOrPath),
1940
+ async () => await resolveWikiGraphArchiveLocation(target),
2058
1941
  options.signal
2059
1942
  );
2060
1943
  return new WikiGraphArchiveHandle(this.#runtime, location);
@@ -2077,14 +1960,17 @@ var WikiGraphArchiveHandle = class {
2077
1960
  return this.#location.locatedUri;
2078
1961
  }
2079
1962
  get objectUri() {
2080
- return (0, import_wiki_graph_core5.parseLocatedWikiGraphUri)(this.#location.locatedUri).objectUri ?? "wikg://";
1963
+ return (0, import_wiki_graph_core7.parseLocatedWikiGraphUri)(this.#location.locatedUri).objectUri ?? "wikg://";
2081
1964
  }
2082
1965
  get path() {
2083
1966
  return this.#location.archivePath;
2084
1967
  }
1968
+ get target() {
1969
+ return this.#location.target;
1970
+ }
2085
1971
  async inspect(options = {}) {
2086
1972
  return await this.#readDocument(
2087
- async (document) => await createWikiGraphArchiveInspectReport(document, {
1973
+ async (document) => await inspectWikiGraphArchive(document, {
2088
1974
  archiveUri: this.#location.publicArchiveUri ?? this.locatedUri,
2089
1975
  ...options.chapterId === void 0 ? {} : { chapterId: options.chapterId }
2090
1976
  }),
@@ -2103,7 +1989,7 @@ var WikiGraphArchiveHandle = class {
2103
1989
  ...chapters === void 0 ? {} : { chapters },
2104
1990
  ...embeddingProvider === void 0 ? {} : { embeddingProvider }
2105
1991
  });
2106
- return await (0, import_wiki_graph_core5.findArchiveObjects)(document, query, {
1992
+ return await (0, import_wiki_graph_core7.findArchiveObjects)(document, query, {
2107
1993
  ...withoutOperationAndScope(options),
2108
1994
  archiveKey: this.archiveKey,
2109
1995
  ...chapters === void 0 ? {} : { chapters },
@@ -2118,14 +2004,14 @@ var WikiGraphArchiveHandle = class {
2118
2004
  }
2119
2005
  async list(options = {}) {
2120
2006
  return await this.#readDocument(async (document) => {
2121
- if ((0, import_wiki_graph_core5.isSourceLocatorScopeUri)(this.objectUri)) {
2122
- return await (0, import_wiki_graph_core5.listArchiveSourceLocators)(document, this.objectUri, {
2007
+ if ((0, import_wiki_graph_core7.isSourceLocatorScopeUri)(this.objectUri)) {
2008
+ return await (0, import_wiki_graph_core7.listArchiveSourceLocators)(document, this.objectUri, {
2123
2009
  ...options.cursor === void 0 ? {} : { cursor: options.cursor },
2124
2010
  ...options.limit === void 0 ? {} : { limit: options.limit }
2125
2011
  });
2126
2012
  }
2127
2013
  const chapters = await this.#resolveScope(document, options);
2128
- return await (0, import_wiki_graph_core5.listArchiveCollection)(document, {
2014
+ return await (0, import_wiki_graph_core7.listArchiveCollection)(document, {
2129
2015
  ...withoutOperationAndScope(options),
2130
2016
  ...chapters === void 0 ? {} : { chapters }
2131
2017
  });
@@ -2153,8 +2039,8 @@ var WikiGraphArchiveHandle = class {
2153
2039
  async getSearchIndexStatus(options = {}) {
2154
2040
  return await this.#readDocument(
2155
2041
  async (document) => ({
2156
- capabilities: await (0, import_wiki_graph_core5.readSearchIndexCapabilityStatus)(document),
2157
- current: await (0, import_wiki_graph_core5.isArchiveSearchIndexCurrent)(document)
2042
+ capabilities: await (0, import_wiki_graph_core7.readSearchIndexCapabilityStatus)(document),
2043
+ current: await (0, import_wiki_graph_core7.isArchiveSearchIndexCurrent)(document)
2158
2044
  }),
2159
2045
  options
2160
2046
  );
@@ -2162,14 +2048,14 @@ var WikiGraphArchiveHandle = class {
2162
2048
  async syncSearchIndex(options = {}) {
2163
2049
  const rebuilt = await this.#writeDocument(
2164
2050
  async (document) => {
2165
- const scope = options.skipUnindexed === true ? { chapters: await (0, import_wiki_graph_core5.listArchiveQueryableChapterIds)(document) } : {};
2051
+ const scope = options.skipUnindexed === true ? { chapters: await (0, import_wiki_graph_core7.listArchiveQueryableChapterIds)(document) } : {};
2166
2052
  if (scope.chapters?.length === 0) {
2167
2053
  throw new Error(
2168
2054
  "Wiki Graph index cache is not ready. No chapters have a current FTS artifact or source embedding artifact."
2169
2055
  );
2170
2056
  }
2171
- if (await (0, import_wiki_graph_core5.isArchiveSearchIndexCurrent)(document, scope)) return false;
2172
- await (0, import_wiki_graph_core5.rebuildArchiveSearchIndex)(document, options.onProgress, scope);
2057
+ if (await (0, import_wiki_graph_core7.isArchiveSearchIndexCurrent)(document, scope)) return false;
2058
+ await (0, import_wiki_graph_core7.rebuildArchiveSearchIndex)(document, options.onProgress, scope);
2173
2059
  return true;
2174
2060
  },
2175
2061
  {
@@ -2178,7 +2064,7 @@ var WikiGraphArchiveHandle = class {
2178
2064
  }
2179
2065
  );
2180
2066
  await this.#runtime.run(
2181
- async () => await (0, import_wiki_graph_core5.deleteArchiveSearchSessions)(this.path),
2067
+ async () => await (0, import_wiki_graph_core7.deleteArchiveSearchSessions)(this.path),
2182
2068
  options.signal
2183
2069
  );
2184
2070
  return { rebuilt };
@@ -2201,21 +2087,21 @@ var WikiGraphArchiveHandle = class {
2201
2087
  }
2202
2088
  );
2203
2089
  await this.#runtime.run(
2204
- async () => await (0, import_wiki_graph_core5.deleteArchiveSearchSessions)(this.path),
2090
+ async () => await (0, import_wiki_graph_core7.deleteArchiveSearchSessions)(this.path),
2205
2091
  options.signal
2206
2092
  );
2207
2093
  return status;
2208
2094
  }
2209
2095
  async listChapters(options = {}) {
2210
2096
  return await this.#readDocument(
2211
- async (document) => await (0, import_wiki_graph_core5.listChapters)(document),
2097
+ async (document) => await (0, import_wiki_graph_core7.listChapters)(document),
2212
2098
  options
2213
2099
  );
2214
2100
  }
2215
2101
  async getChapter(path, options = {}) {
2216
2102
  return await this.#readDocument(async (document) => {
2217
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2218
- return requireChapter(await (0, import_wiki_graph_core5.listChapters)(document), chapterId);
2103
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2104
+ return requireChapter(await (0, import_wiki_graph_core7.listChapters)(document), chapterId);
2219
2105
  }, options);
2220
2106
  }
2221
2107
  async addChapter(options) {
@@ -2224,17 +2110,17 @@ var WikiGraphArchiveHandle = class {
2224
2110
  document,
2225
2111
  options.parentPath
2226
2112
  );
2227
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobConflicts)({
2113
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobConflicts)({
2228
2114
  archive: this.#location.archiveFile,
2229
2115
  operation: "Adding chapter",
2230
2116
  scope: { kind: "archive" }
2231
2117
  });
2232
- let details = await (0, import_wiki_graph_core5.addChapter)(document, {
2118
+ let details = await (0, import_wiki_graph_core7.addChapter)(document, {
2233
2119
  ...parentChapterId === void 0 ? {} : { parentChapterId },
2234
2120
  ...options.title === void 0 ? {} : { title: options.title }
2235
2121
  });
2236
2122
  if (options.source !== void 0) {
2237
- details = await (0, import_wiki_graph_core5.setChapterSource)(
2123
+ details = await (0, import_wiki_graph_core7.setChapterSource)(
2238
2124
  document,
2239
2125
  details.chapterId,
2240
2126
  asTextStream(options.source)
@@ -2245,7 +2131,7 @@ var WikiGraphArchiveHandle = class {
2245
2131
  }
2246
2132
  async getChapterArtifact(path, kind) {
2247
2133
  return await this.#readDocument(async (document) => {
2248
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2134
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2249
2135
  const [artifact, revision] = await Promise.all([
2250
2136
  document.indexArtifacts.get(chapterId, kind),
2251
2137
  document.serials.getRevision(chapterId)
@@ -2268,25 +2154,25 @@ var WikiGraphArchiveHandle = class {
2268
2154
  }
2269
2155
  async deleteChapterArtifact(path, kind) {
2270
2156
  return await this.#writeDocument(async (document) => {
2271
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2157
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2272
2158
  await document.indexArtifacts.delete(chapterId, kind);
2273
2159
  return { chapterId, deleted: true, kind };
2274
2160
  });
2275
2161
  }
2276
2162
  async moveChapter(path, options) {
2277
2163
  return await this.#writeDocument(async (document) => {
2278
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2164
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2279
2165
  const [afterChapterId, beforeChapterId, parentChapterId] = await Promise.all([
2280
2166
  resolveOptionalChapterPath(document, options.afterPath),
2281
2167
  resolveOptionalChapterPath(document, options.beforePath),
2282
2168
  resolveOptionalChapterPath(document, options.parentPath)
2283
2169
  ]);
2284
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobConflicts)({
2170
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobConflicts)({
2285
2171
  archive: this.#location.archiveFile,
2286
2172
  operation: "Moving chapter",
2287
2173
  scope: { kind: "archive" }
2288
2174
  });
2289
- return await (0, import_wiki_graph_core5.moveChapter)(document, chapterId, {
2175
+ return await (0, import_wiki_graph_core7.moveChapter)(document, chapterId, {
2290
2176
  ...afterChapterId === void 0 ? {} : { afterChapterId },
2291
2177
  ...beforeChapterId === void 0 ? {} : { beforeChapterId },
2292
2178
  ...parentChapterId === void 0 ? {} : { parentChapterId },
@@ -2298,35 +2184,35 @@ var WikiGraphArchiveHandle = class {
2298
2184
  }
2299
2185
  async removeChapter(path, recursive = false) {
2300
2186
  await this.#writeDocument(async (document) => {
2301
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2302
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobConflicts)({
2187
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2188
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobConflicts)({
2303
2189
  archive: this.#location.archiveFile,
2304
2190
  operation: "Removing chapter",
2305
2191
  scope: { kind: "archive" }
2306
2192
  });
2307
- await (0, import_wiki_graph_core5.removeChapter)(document, chapterId, { recursive });
2193
+ await (0, import_wiki_graph_core7.removeChapter)(document, chapterId, { recursive });
2308
2194
  });
2309
2195
  }
2310
2196
  async resetChapter(path, stage) {
2311
2197
  return await this.#writeDocument(async (document) => {
2312
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2198
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2313
2199
  await assertChapterResetAllowed(
2314
2200
  this.#location.archiveFile,
2315
2201
  chapterId,
2316
2202
  stage
2317
2203
  );
2318
- return await (0, import_wiki_graph_core5.resetChapter)(document, chapterId, stage);
2204
+ return await (0, import_wiki_graph_core7.resetChapter)(document, chapterId, stage);
2319
2205
  });
2320
2206
  }
2321
2207
  async setChapterSource(path, source, options = {}) {
2322
2208
  return await this.#writeDocument(async (document) => {
2323
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2324
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobs)({
2209
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2210
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobs)({
2325
2211
  archive: this.#location.archiveFile,
2326
2212
  chapterIds: [chapterId],
2327
2213
  operation: "Setting chapter source"
2328
2214
  });
2329
- return await (0, import_wiki_graph_core5.setChapterSource)(
2215
+ return await (0, import_wiki_graph_core7.setChapterSource)(
2330
2216
  document,
2331
2217
  chapterId,
2332
2218
  asTextStream(source),
@@ -2336,43 +2222,43 @@ var WikiGraphArchiveHandle = class {
2336
2222
  }
2337
2223
  async setChapterSummary(path, summary) {
2338
2224
  return await this.#writeDocument(async (document) => {
2339
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2340
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobs)({
2225
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2226
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobs)({
2341
2227
  archive: this.#location.archiveFile,
2342
2228
  chapterIds: [chapterId],
2343
2229
  operation: "Setting chapter summary",
2344
2230
  requiresTarget: "reading-summary"
2345
2231
  });
2346
- return await (0, import_wiki_graph_core5.setChapterSummary)(document, chapterId, summary);
2232
+ return await (0, import_wiki_graph_core7.setChapterSummary)(document, chapterId, summary);
2347
2233
  });
2348
2234
  }
2349
2235
  async setChapterTitle(path, title) {
2350
2236
  return await this.#writeDocument(async (document) => {
2351
- const chapterId = await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2352
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobs)({
2237
+ const chapterId = await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2238
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobs)({
2353
2239
  archive: this.#location.archiveFile,
2354
2240
  chapterIds: [chapterId],
2355
2241
  operation: "Setting chapter title"
2356
2242
  });
2357
- return await (0, import_wiki_graph_core5.setChapterTitle)(document, chapterId, title);
2243
+ return await (0, import_wiki_graph_core7.setChapterTitle)(document, chapterId, title);
2358
2244
  });
2359
2245
  }
2360
2246
  async getChapterTree() {
2361
2247
  return await this.#readDocument(
2362
- async (document) => await (0, import_wiki_graph_core5.getChapterTree)(document)
2248
+ async (document) => await (0, import_wiki_graph_core7.getChapterTree)(document)
2363
2249
  );
2364
2250
  }
2365
2251
  async applyChapterTree(tree, options = {}) {
2366
2252
  return await this.#writeDocument(
2367
2253
  async (document) => {
2368
2254
  if (options.dryRun !== true) {
2369
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobConflicts)({
2255
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobConflicts)({
2370
2256
  archive: this.#location.archiveFile,
2371
2257
  operation: "Changing chapter tree",
2372
2258
  scope: { kind: "archive" }
2373
2259
  });
2374
2260
  }
2375
- return await (0, import_wiki_graph_core5.applyChapterTree)(document, tree, {
2261
+ return await (0, import_wiki_graph_core7.applyChapterTree)(document, tree, {
2376
2262
  dryRun: options.dryRun ?? false
2377
2263
  });
2378
2264
  },
@@ -2384,8 +2270,8 @@ var WikiGraphArchiveHandle = class {
2384
2270
  async () => await withConfiguredWikimediaResolver(
2385
2271
  objectUri,
2386
2272
  async (wikimediaOptions) => await this.#readDocument(
2387
- async (document) => await (0, import_wiki_graph_core5.readArchivePage)(document, objectUri, {
2388
- ...withoutOperation(options),
2273
+ async (document) => await (0, import_wiki_graph_core7.readArchivePage)(document, objectUri, {
2274
+ ...options,
2389
2275
  ...wikimediaOptions
2390
2276
  }),
2391
2277
  options
@@ -2398,7 +2284,7 @@ var WikiGraphArchiveHandle = class {
2398
2284
  if (options.query === void 0) {
2399
2285
  return await this.#readDocument(async (document) => {
2400
2286
  const chapters = await this.#resolveScope(document, {});
2401
- return await (0, import_wiki_graph_core5.listRelatedArchiveObjects)(document, objectUri, {
2287
+ return await (0, import_wiki_graph_core7.listRelatedArchiveObjects)(document, objectUri, {
2402
2288
  ...withoutOperation(options),
2403
2289
  ...chapters === void 0 ? {} : { chapters }
2404
2290
  });
@@ -2415,7 +2301,7 @@ var WikiGraphArchiveHandle = class {
2415
2301
  ...chapters === void 0 ? {} : { chapters },
2416
2302
  ...embeddingProvider === void 0 ? {} : { embeddingProvider }
2417
2303
  });
2418
- return await (0, import_wiki_graph_core5.listRelatedArchiveObjects)(document, objectUri, {
2304
+ return await (0, import_wiki_graph_core7.listRelatedArchiveObjects)(document, objectUri, {
2419
2305
  ...withoutOperation(options),
2420
2306
  ...chapters === void 0 ? {} : { chapters }
2421
2307
  });
@@ -2430,7 +2316,7 @@ var WikiGraphArchiveHandle = class {
2430
2316
  if (options.query === void 0) {
2431
2317
  return await this.#readDocument(async (document) => {
2432
2318
  const chapters = await this.#resolveScope(document, {});
2433
- return await (0, import_wiki_graph_core5.listArchiveEvidence)(document, objectUri, {
2319
+ return await (0, import_wiki_graph_core7.listArchiveEvidence)(document, objectUri, {
2434
2320
  ...withoutOperation(options),
2435
2321
  ...chapters === void 0 ? {} : { chapters }
2436
2322
  });
@@ -2447,7 +2333,7 @@ var WikiGraphArchiveHandle = class {
2447
2333
  ...chapters === void 0 ? {} : { chapters },
2448
2334
  ...embeddingProvider === void 0 ? {} : { embeddingProvider }
2449
2335
  });
2450
- return await (0, import_wiki_graph_core5.listArchiveEvidence)(document, objectUri, {
2336
+ return await (0, import_wiki_graph_core7.listArchiveEvidence)(document, objectUri, {
2451
2337
  ...withoutOperation(options),
2452
2338
  ...chapters === void 0 ? {} : { chapters }
2453
2339
  });
@@ -2460,7 +2346,7 @@ var WikiGraphArchiveHandle = class {
2460
2346
  }
2461
2347
  async pack(objectUri = this.objectUri, budget = 5e3, options = {}) {
2462
2348
  return await this.#readDocument(
2463
- async (document) => await (0, import_wiki_graph_core5.packArchiveContext)(document, objectUri, budget),
2349
+ async (document) => await (0, import_wiki_graph_core7.packArchiveContext)(document, objectUri, budget),
2464
2350
  options
2465
2351
  );
2466
2352
  }
@@ -2483,7 +2369,7 @@ var WikiGraphArchiveHandle = class {
2483
2369
  }, options);
2484
2370
  }
2485
2371
  async readCover(options = {}) {
2486
- const app = new import_wiki_graph_core5.WikiGraph({});
2372
+ const app = new import_wiki_graph_core7.WikiGraph({});
2487
2373
  return await this.#runtime.run(
2488
2374
  async () => await app.openSession(
2489
2375
  this.#location.archiveFile,
@@ -2531,7 +2417,7 @@ var WikiGraphArchiveHandle = class {
2531
2417
  }
2532
2418
  async #readDocument(operation, options = {}) {
2533
2419
  return await this.#runtime.run(
2534
- async () => await new import_wiki_graph_core5.WikiGraphArchiveFile(this.#location.archiveFile).readDocument(
2420
+ async () => await new import_wiki_graph_core7.WikiGraphArchiveFile(this.#location.archiveFile).readDocument(
2535
2421
  operation
2536
2422
  ),
2537
2423
  options.signal
@@ -2539,17 +2425,13 @@ var WikiGraphArchiveHandle = class {
2539
2425
  }
2540
2426
  async #writeDocument(operation, options = {}) {
2541
2427
  return await this.#runtime.run(async () => {
2542
- const result = await new import_wiki_graph_core5.WikiGraphArchiveFile(
2543
- this.#location.archiveFile
2544
- ).write(operation, {
2428
+ return await writeWikiGraphArchiveLocation(this.#location, operation, {
2429
+ ...options.onIndexSyncError === void 0 ? {} : { onIndexSyncError: options.onIndexSyncError },
2430
+ ...options.refreshLibraryIndex === void 0 ? {} : { refreshLibraryIndex: options.refreshLibraryIndex },
2545
2431
  ...options.searchIndexWritebackPolicy === void 0 ? {} : {
2546
2432
  searchIndexWritebackPolicy: options.searchIndexWritebackPolicy
2547
2433
  }
2548
2434
  });
2549
- if (options.refreshLibraryIndex !== false) {
2550
- await this.#refreshLibraryIndex(options.onIndexSyncError);
2551
- }
2552
- return result;
2553
2435
  }, options.signal);
2554
2436
  }
2555
2437
  async #resolveScope(document, options) {
@@ -2559,7 +2441,7 @@ var WikiGraphArchiveHandle = class {
2559
2441
  async #resolveQueryChapters(document, options) {
2560
2442
  const chapters = await this.#resolveScope(document, options);
2561
2443
  if (options.skipUnindexed !== true) return chapters;
2562
- const queryable = await (0, import_wiki_graph_core5.listArchiveQueryableChapterIds)(document, {
2444
+ const queryable = await (0, import_wiki_graph_core7.listArchiveQueryableChapterIds)(document, {
2563
2445
  ...chapters === void 0 ? {} : { chapters }
2564
2446
  });
2565
2447
  if (queryable.length === 0) {
@@ -2569,56 +2451,11 @@ var WikiGraphArchiveHandle = class {
2569
2451
  }
2570
2452
  return queryable;
2571
2453
  }
2572
- async #refreshLibraryIndex(onError) {
2573
- if (this.#location.libraryDirtyTarget === void 0) return;
2574
- if (this.#location.libraryArchiveTarget !== void 0) {
2575
- await (0, import_wiki_graph_core5.finalizeWikiGraphLibraryArchiveWrite)({
2576
- target: this.#location.libraryArchiveTarget
2577
- });
2578
- }
2579
- try {
2580
- const state = await (0, import_wiki_graph_core5.readWikiGraphLibraryIndexState)(
2581
- this.#location.libraryDirtyTarget
2582
- );
2583
- if (state.status !== "missing") {
2584
- await (0, import_wiki_graph_core5.rebuildWikiGraphLibraryIndex)(this.#location.libraryDirtyTarget);
2585
- }
2586
- } catch (error) {
2587
- onError?.(error);
2588
- }
2589
- }
2590
2454
  };
2591
- async function resolveWikiGraphArchiveLocation(uriOrPath) {
2592
- if (!uriOrPath.startsWith("wikg://")) {
2593
- return createPathLocation(resolveWikiGraphRuntimePath(uriOrPath));
2594
- }
2595
- const parsed = (0, import_wiki_graph_core5.parseLocatedWikiGraphUri)(uriOrPath);
2596
- const portableArchiveLocator = parsed.archivePath ?? uriOrPath;
2597
- const archiveLocator = portableArchiveLocator.startsWith("wikg://lib/") ? portableArchiveLocator : resolveWikiGraphRuntimePath(portableArchiveLocator);
2598
- const libraryArchiveTarget = archiveLocator.startsWith("wikg://lib/") ? (0, import_wiki_graph_core5.parseWikiGraphLibraryUri)(archiveLocator) : void 0;
2599
- const archiveFile = libraryArchiveTarget?.kind === "archive" ? await (0, import_wiki_graph_core5.resolveWikiGraphLibraryArchiveFile)(archiveLocator) : new NodeFile(archiveLocator);
2600
- const archivePath = getNodeResourcePath(archiveFile);
2601
- return {
2602
- archiveFile,
2603
- archiveKey: archivePath,
2604
- archivePath,
2605
- indexScope: { archiveKey: archivePath, archivePath, kind: "archive-index" },
2606
- ...libraryArchiveTarget?.kind === "archive" ? {
2607
- libraryArchiveTarget,
2608
- libraryDirtyTarget: {
2609
- isDefault: libraryArchiveTarget.isDefault,
2610
- kind: "scope",
2611
- ...libraryArchiveTarget.publicId === void 0 ? {} : { publicId: libraryArchiveTarget.publicId }
2612
- },
2613
- publicArchiveUri: portableArchiveLocator
2614
- } : {},
2615
- locatedUri: (0, import_wiki_graph_core5.formatLocatedWikiGraphUri)(archivePath, parsed.objectUri)
2616
- };
2617
- }
2618
2455
  async function ensureArchiveSearchIndex(document, options = {}) {
2619
2456
  const scope = options.chapters === void 0 ? {} : { chapters: options.chapters };
2620
- if (await (0, import_wiki_graph_core5.isArchiveSearchIndexCurrent)(document, scope)) return;
2621
- await (0, import_wiki_graph_core5.rebuildArchiveSearchIndex)(document, void 0, {
2457
+ if (await (0, import_wiki_graph_core7.isArchiveSearchIndexCurrent)(document, scope)) return;
2458
+ await (0, import_wiki_graph_core7.rebuildArchiveSearchIndex)(document, void 0, {
2622
2459
  ...scope,
2623
2460
  ...options.embeddingProvider === void 0 ? {} : { embeddingProvider: options.embeddingProvider }
2624
2461
  });
@@ -2628,10 +2465,10 @@ async function resolveArchiveScope(document, objectUri, depth) {
2628
2465
  if (parsed === void 0 || parsed.kind === "collection" && depth === void 0) {
2629
2466
  return void 0;
2630
2467
  }
2631
- const chapters = await (0, import_wiki_graph_core5.listChapters)(document);
2468
+ const chapters = await (0, import_wiki_graph_core7.listChapters)(document);
2632
2469
  const entries = parsed.kind === "collection" ? chapters.filter((chapter) => chapter.depth <= depth) : selectChapterSubtree(
2633
2470
  chapters,
2634
- await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, parsed.chapterPath),
2471
+ await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, parsed.chapterPath),
2635
2472
  depth
2636
2473
  );
2637
2474
  return { chapterIds: entries.map((entry) => entry.chapterId), entries };
@@ -2653,32 +2490,23 @@ function selectChapterSubtree(chapters, rootChapterId, depth) {
2653
2490
  (chapter) => chapter.chapterId === root.chapterId || chapter.path.startsWith(prefix) && (depth === void 0 || chapter.depth - root.depth <= depth)
2654
2491
  );
2655
2492
  }
2656
- function createPathLocation(path) {
2657
- return {
2658
- archiveFile: new NodeFile(path),
2659
- archiveKey: path,
2660
- archivePath: path,
2661
- indexScope: { archiveKey: path, archivePath: path, kind: "archive-index" },
2662
- locatedUri: (0, import_wiki_graph_core5.formatLocatedWikiGraphUri)(path)
2663
- };
2664
- }
2665
2493
  async function createEmptyArchiveFile(path) {
2666
- const directoryPath = await (0, import_promises3.mkdtemp)(
2667
- (0, import_path3.join)((0, import_os3.tmpdir)(), "wikigraph-archive-write-")
2494
+ const directoryPath = await (0, import_promises4.mkdtemp)(
2495
+ (0, import_path5.join)((0, import_os3.tmpdir)(), "wikigraph-archive-write-")
2668
2496
  );
2669
2497
  try {
2670
2498
  const directory = new NodeDirectory(directoryPath);
2671
- const document = await import_wiki_graph_core5.DirectoryDocument.open(directory);
2499
+ const document = await import_wiki_graph_core7.DirectoryDocument.open(directory);
2672
2500
  try {
2673
2501
  await document.openSession(async (openedDocument) => {
2674
- await openedDocument.writeToc({ items: [], version: import_wiki_graph_core5.TOC_FILE_VERSION });
2502
+ await openedDocument.writeToc({ items: [], version: import_wiki_graph_core7.TOC_FILE_VERSION });
2675
2503
  });
2676
2504
  } finally {
2677
2505
  await document.release();
2678
2506
  }
2679
- await (0, import_wiki_graph_core5.writeWikgArchive)(directory, new NodeFile(path));
2507
+ await (0, import_wiki_graph_core7.writeWikgArchive)(directory, new NodeFile(path));
2680
2508
  } finally {
2681
- await (0, import_promises3.rm)(directoryPath, { force: true, recursive: true });
2509
+ await (0, import_promises4.rm)(directoryPath, { force: true, recursive: true });
2682
2510
  }
2683
2511
  }
2684
2512
  function isNodeEEXISTError(error) {
@@ -2686,7 +2514,7 @@ function isNodeEEXISTError(error) {
2686
2514
  }
2687
2515
  async function nodePathExists(path) {
2688
2516
  try {
2689
- await (0, import_promises3.stat)(path);
2517
+ await (0, import_promises4.stat)(path);
2690
2518
  return true;
2691
2519
  } catch (error) {
2692
2520
  if (isNodeENOENTError(error)) return false;
@@ -2698,13 +2526,13 @@ function isNodeENOENTError(error) {
2698
2526
  }
2699
2527
  function parseObjectMetadataTarget(objectPath) {
2700
2528
  if (objectPath === "") {
2701
- return { kind: import_wiki_graph_core5.ObjectMetadataKind.Archive, objectPath };
2529
+ return { kind: import_wiki_graph_core7.ObjectMetadataKind.Archive, objectPath };
2702
2530
  }
2703
2531
  const chapter = /^chapter\/([1-9][0-9]*)(?:\/.*)?$/u.exec(objectPath);
2704
2532
  if (chapter?.[1] !== void 0) {
2705
2533
  return {
2706
2534
  chapterId: Number(chapter[1]),
2707
- kind: import_wiki_graph_core5.ObjectMetadataKind.Chapter,
2535
+ kind: import_wiki_graph_core7.ObjectMetadataKind.Chapter,
2708
2536
  objectPath
2709
2537
  };
2710
2538
  }
@@ -2712,7 +2540,7 @@ function parseObjectMetadataTarget(objectPath) {
2712
2540
  if (chunk?.[1] !== void 0) {
2713
2541
  return {
2714
2542
  chunkId: Number(chunk[1]),
2715
- kind: import_wiki_graph_core5.ObjectMetadataKind.Chunk,
2543
+ kind: import_wiki_graph_core7.ObjectMetadataKind.Chunk,
2716
2544
  objectPath
2717
2545
  };
2718
2546
  }
@@ -2720,7 +2548,7 @@ function parseObjectMetadataTarget(objectPath) {
2720
2548
  if (entity?.[1] !== void 0) {
2721
2549
  return {
2722
2550
  entityQid: entity[1],
2723
- kind: import_wiki_graph_core5.ObjectMetadataKind.Entity,
2551
+ kind: import_wiki_graph_core7.ObjectMetadataKind.Entity,
2724
2552
  objectPath
2725
2553
  };
2726
2554
  }
@@ -2729,14 +2557,14 @@ function parseObjectMetadataTarget(objectPath) {
2729
2557
  );
2730
2558
  if (triple?.[1] !== void 0) {
2731
2559
  return {
2732
- kind: import_wiki_graph_core5.ObjectMetadataKind.Triple,
2560
+ kind: import_wiki_graph_core7.ObjectMetadataKind.Triple,
2733
2561
  objectPath,
2734
2562
  tripleObjectQid: triple[3],
2735
2563
  triplePredicate: decodeURIComponent(triple[2]),
2736
2564
  tripleSubjectQid: triple[1]
2737
2565
  };
2738
2566
  }
2739
- return { kind: import_wiki_graph_core5.ObjectMetadataKind.Object, objectPath };
2567
+ return { kind: import_wiki_graph_core7.ObjectMetadataKind.Object, objectPath };
2740
2568
  }
2741
2569
  function withoutOperation(options) {
2742
2570
  const { signal: _signal, ...rest } = options;
@@ -2752,7 +2580,7 @@ function withoutOperationAndScope(options) {
2752
2580
  return rest;
2753
2581
  }
2754
2582
  async function resolveOptionalChapterPath(document, path) {
2755
- return path === void 0 ? void 0 : await (0, import_wiki_graph_core5.resolveChapterPathReadonly)(document, path);
2583
+ return path === void 0 ? void 0 : await (0, import_wiki_graph_core7.resolveChapterPathReadonly)(document, path);
2756
2584
  }
2757
2585
  function requireChapter(chapters, chapterId) {
2758
2586
  const chapter = chapters.find((entry) => entry.chapterId === chapterId);
@@ -2766,7 +2594,7 @@ function asTextStream(text) {
2766
2594
  }
2767
2595
  async function assertChapterResetAllowed(archive, chapterId, stage) {
2768
2596
  if (stage === "planned") {
2769
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobs)({
2597
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobs)({
2770
2598
  archive,
2771
2599
  chapterIds: [chapterId],
2772
2600
  operation: "Resetting chapter to planned"
@@ -2774,14 +2602,14 @@ async function assertChapterResetAllowed(archive, chapterId, stage) {
2774
2602
  return;
2775
2603
  }
2776
2604
  if (stage === "sourced") {
2777
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobs)({
2605
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobs)({
2778
2606
  archive,
2779
2607
  chapterIds: [chapterId],
2780
2608
  operation: "Resetting chapter graph",
2781
2609
  requiresTarget: "reading-graph"
2782
2610
  });
2783
2611
  }
2784
- await (0, import_wiki_graph_core5.assertNoActiveBuildJobs)({
2612
+ await (0, import_wiki_graph_core7.assertNoActiveBuildJobs)({
2785
2613
  archive,
2786
2614
  chapterIds: [chapterId],
2787
2615
  operation: "Resetting chapter summary",
@@ -2793,7 +2621,7 @@ async function assertChapterResetAllowed(archive, chapterId, stage) {
2793
2621
  WikiGraphArchiveExistsError,
2794
2622
  WikiGraphArchiveHandle,
2795
2623
  WikiGraphArchiveManager,
2796
- createWikiGraphArchiveInspectReport,
2797
- resolveWikiGraphArchiveLocation
2624
+ WikiGraphArchiveOwnershipError,
2625
+ inspectWikiGraphArchive
2798
2626
  });
2799
2627
  //# sourceMappingURL=archives.cjs.map