wiki-graph-sdk 0.7.1 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +22 -1
  2. package/dist/archives.cjs +494 -656
  3. package/dist/archives.cjs.map +1 -1
  4. package/dist/archives.d.ts +48 -80
  5. package/dist/archives.js +431 -585
  6. package/dist/archives.js.map +1 -1
  7. package/dist/conversions.cjs +183 -33
  8. package/dist/conversions.cjs.map +1 -1
  9. package/dist/conversions.d.ts +8 -1
  10. package/dist/conversions.js +188 -29
  11. package/dist/conversions.js.map +1 -1
  12. package/dist/default-worker.cjs +322 -62
  13. package/dist/default-worker.cjs.map +1 -1
  14. package/dist/default-worker.d.ts +1 -0
  15. package/dist/default-worker.js +336 -60
  16. package/dist/default-worker.js.map +1 -1
  17. package/dist/embedding.cjs +2 -1
  18. package/dist/embedding.cjs.map +1 -1
  19. package/dist/embedding.d.ts +4 -4
  20. package/dist/embedding.js +2 -1
  21. package/dist/embedding.js.map +1 -1
  22. package/dist/index.cjs +1295 -949
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +58 -24
  25. package/dist/index.js +1164 -805
  26. package/dist/index.js.map +1 -1
  27. package/dist/{jobs-Dd5PMSId.d.ts → jobs-CRK-DZ40.d.ts} +27 -14
  28. package/dist/local-config.cjs +2 -1
  29. package/dist/local-config.cjs.map +1 -1
  30. package/dist/local-config.js +2 -1
  31. package/dist/local-config.js.map +1 -1
  32. package/dist/maintenance.cjs +140 -30
  33. package/dist/maintenance.cjs.map +1 -1
  34. package/dist/maintenance.d.ts +5 -1
  35. package/dist/maintenance.js +145 -26
  36. package/dist/maintenance.js.map +1 -1
  37. package/dist/runtime-config.cjs +4 -3
  38. package/dist/runtime-config.cjs.map +1 -1
  39. package/dist/runtime-config.d.ts +4 -1
  40. package/dist/runtime-config.js +4 -3
  41. package/dist/runtime-config.js.map +1 -1
  42. package/dist/stage-BnWI3Rqt.d.ts +33 -0
  43. package/dist/stage.cjs +22 -12
  44. package/dist/stage.cjs.map +1 -1
  45. package/dist/stage.d.ts +2 -14
  46. package/dist/stage.js +22 -12
  47. package/dist/stage.js.map +1 -1
  48. package/dist/target-CZ7elwmj.d.ts +14 -0
  49. package/dist/wikispine.cjs +14 -10
  50. package/dist/wikispine.cjs.map +1 -1
  51. package/dist/wikispine.js +14 -10
  52. package/dist/wikispine.js.map +1 -1
  53. package/dist/worker.cjs +295 -25
  54. package/dist/worker.cjs.map +1 -1
  55. package/dist/worker.d.ts +28 -5
  56. package/dist/worker.js +313 -28
  57. package/dist/worker.js.map +1 -1
  58. package/package.json +2 -2
package/dist/worker.js CHANGED
@@ -5,9 +5,13 @@ globalThis.__WIKIGRAPH_DATA_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL
5
5
  // src/worker.ts
6
6
  import {
7
7
  applyChapterJobArtifactFile,
8
- runBuildJobWorker as runCoreBuildJobWorker
8
+ runBuildJobWorker as runCoreBuildJobWorker,
9
+ writeChapterJobInputFile
9
10
  } from "wiki-graph-core/worker";
10
- import { WikiGraphArchiveFile } from "wiki-graph-core";
11
+ import {
12
+ resolveChapterPathReadonly,
13
+ WikiGraphArchiveFile as WikiGraphArchiveFile2
14
+ } from "wiki-graph-core";
11
15
 
12
16
  // src/node-platform.ts
13
17
  import * as asyncHooks from "async_hooks";
@@ -16,7 +20,7 @@ import * as fs from "fs";
16
20
  import * as fsPromises from "fs/promises";
17
21
  import * as os from "os";
18
22
  import * as pathModule from "path";
19
- import * as process from "process";
23
+ import * as process2 from "process";
20
24
  import { Readable, Writable } from "stream";
21
25
  import { pipeline } from "stream/promises";
22
26
  import * as sqlite3 from "sqlite3";
@@ -30,7 +34,7 @@ import {
30
34
  withWikiGraphStorage,
31
35
  normalizeTemplateName
32
36
  } from "wiki-graph-core/platform";
33
- var nodeProcess = process.default ?? process;
37
+ var nodeProcess = process2.default ?? process2;
34
38
  var nodeSqlite3 = sqlite3.default ?? sqlite3;
35
39
  var NODE_RESOURCE_KIND = /* @__PURE__ */ Symbol.for("wiki-graph-sdk.node-resource.v1");
36
40
  var NodeFile = class {
@@ -242,6 +246,14 @@ function decodeNodeResourceIdentity(identity, kind) {
242
246
  return void 0;
243
247
  }
244
248
  }
249
+ function getNodeResourcePath(resource) {
250
+ const brandedPath = getBrandedNodeResourcePath(resource, resource.kind);
251
+ if (brandedPath !== void 0) return brandedPath;
252
+ if (typeof resource === "object" && resource !== null && "path" in resource && typeof resource.path === "string") {
253
+ return resource.path;
254
+ }
255
+ throw new TypeError("Expected a Node File or Directory resource");
256
+ }
245
257
  function assertChildName(name) {
246
258
  if (name.length === 0 || name === "." || name === ".." || pathModule.isAbsolute(name) || name.includes("/") || name.includes("\\")) {
247
259
  throw new TypeError(`Directory child must be a relative name: ${name}`);
@@ -253,42 +265,42 @@ var NodeDatabaseConnection = class {
253
265
  }
254
266
  database;
255
267
  async close() {
256
- await new Promise((resolve2, reject) => {
268
+ await new Promise((resolve4, reject) => {
257
269
  this.database.close((error) => {
258
270
  if (error) reject(error);
259
- else resolve2();
271
+ else resolve4();
260
272
  });
261
273
  });
262
274
  }
263
275
  async execute(sql) {
264
- await new Promise((resolve2, reject) => {
276
+ await new Promise((resolve4, reject) => {
265
277
  this.database.exec(sql, (error) => {
266
278
  if (error) reject(error);
267
- else resolve2();
279
+ else resolve4();
268
280
  });
269
281
  });
270
282
  }
271
283
  async queryAll(sql, params = []) {
272
- return await new Promise((resolve2, reject) => {
284
+ return await new Promise((resolve4, reject) => {
273
285
  this.database.all(sql, [...params], (error, rows) => {
274
286
  if (error) reject(error);
275
- else resolve2(rows);
287
+ else resolve4(rows);
276
288
  });
277
289
  });
278
290
  }
279
291
  async queryOne(sql, params = []) {
280
- return await new Promise((resolve2, reject) => {
292
+ return await new Promise((resolve4, reject) => {
281
293
  this.database.get(sql, [...params], (error, row) => {
282
294
  if (error) reject(error);
283
- else resolve2(row);
295
+ else resolve4(row);
284
296
  });
285
297
  });
286
298
  }
287
299
  async run(sql, params = []) {
288
- await new Promise((resolve2, reject) => {
300
+ await new Promise((resolve4, reject) => {
289
301
  this.database.run(sql, [...params], (error) => {
290
302
  if (error) reject(error);
291
- else resolve2();
303
+ else resolve4();
292
304
  });
293
305
  });
294
306
  }
@@ -302,10 +314,10 @@ async function openNativeNodeDatabase(file, flags) {
302
314
  if (path === void 0) {
303
315
  throw new TypeError("The Node database adapter requires a NodeFile");
304
316
  }
305
- return await new Promise((resolve2, reject) => {
317
+ return await new Promise((resolve4, reject) => {
306
318
  const database = new nodeSqlite3.Database(path, flags, (error) => {
307
319
  if (error) reject(error);
308
- else resolve2(database);
320
+ else resolve4(database);
309
321
  });
310
322
  });
311
323
  }
@@ -314,19 +326,19 @@ async function openNodeZip(file) {
314
326
  if (path === void 0) {
315
327
  throw new TypeError("The Node ZIP adapter requires a NodeFile");
316
328
  }
317
- const zipFile = await new Promise((resolve2, reject) => {
329
+ const zipFile = await new Promise((resolve4, reject) => {
318
330
  yauzl.open(
319
331
  path,
320
332
  { autoClose: false, lazyEntries: true },
321
333
  (error, opened) => {
322
334
  if (error || opened === void 0)
323
335
  reject(error ?? new Error("Cannot open ZIP"));
324
- else resolve2(opened);
336
+ else resolve4(opened);
325
337
  }
326
338
  );
327
339
  });
328
340
  const entries = await new Promise(
329
- (resolve2, reject) => {
341
+ (resolve4, reject) => {
330
342
  const discovered = /* @__PURE__ */ new Map();
331
343
  const rejectAndClose = (error) => {
332
344
  zipFile.close();
@@ -336,7 +348,7 @@ async function openNodeZip(file) {
336
348
  };
337
349
  const resolveEntries = () => {
338
350
  zipFile.off("error", rejectAndClose);
339
- resolve2(discovered);
351
+ resolve4(discovered);
340
352
  };
341
353
  zipFile.on("entry", (entry) => {
342
354
  if (!entry.fileName.endsWith("/")) {
@@ -446,12 +458,12 @@ async function collectDecodedNodeStreamRange(input, offset, length) {
446
458
  return Buffer.concat(chunks, collected);
447
459
  }
448
460
  async function openNodeZipEntryStream(zipFile, entry, options) {
449
- return await new Promise((resolve2, reject) => {
461
+ return await new Promise((resolve4, reject) => {
450
462
  const callback = (error, opened) => {
451
463
  if (error || opened === void 0) {
452
464
  reject(error ?? new Error(`Cannot read ZIP entry: ${entry.fileName}`));
453
465
  } else {
454
- resolve2(opened);
466
+ resolve4(opened);
455
467
  }
456
468
  };
457
469
  if (options === void 0) zipFile.openReadStream(entry, callback);
@@ -582,11 +594,11 @@ async function writeNodeStream(input, writer) {
582
594
  );
583
595
  }
584
596
  async function collectNodeStream(input) {
585
- return await new Promise((resolve2, reject) => {
597
+ return await new Promise((resolve4, reject) => {
586
598
  const chunks = [];
587
599
  input.on("data", (chunk) => chunks.push(chunk));
588
600
  input.once("error", reject);
589
- input.once("end", () => resolve2(Buffer.concat(chunks)));
601
+ input.once("end", () => resolve4(Buffer.concat(chunks)));
590
602
  });
591
603
  }
592
604
  var nodeWikiGraphPlatform = {
@@ -702,17 +714,288 @@ async function withNodeWikiGraphStorage(stateRoot, operation) {
702
714
  );
703
715
  }
704
716
 
717
+ // src/archive/target.ts
718
+ import { realpath, stat as stat2 } from "fs/promises";
719
+ import { basename as basename2, dirname as dirname2, isAbsolute as isAbsolute3, join as join2, relative, resolve as resolve3 } from "path";
720
+ import {
721
+ formatLocatedWikiGraphUri,
722
+ formatWikiGraphLibraryUri,
723
+ listWikiGraphLibraries,
724
+ listWikiGraphLibraryArchives,
725
+ parseLocatedWikiGraphUri,
726
+ parseWikiGraphLibraryUri,
727
+ resolveWikiGraphLibrary,
728
+ resolveWikiGraphLibraryArchiveFile
729
+ } from "wiki-graph-core";
730
+
731
+ // src/runtime-path.ts
732
+ import { homedir as homedir2 } from "os";
733
+ import { isAbsolute as isAbsolute2, resolve as resolve2 } from "path";
734
+
735
+ // src/runtime-context.ts
736
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
737
+ var RUNTIME_CONTEXT_KEY = /* @__PURE__ */ Symbol.for("wiki-graph-sdk.runtime-context.v1");
738
+ var runtimeContext = getRuntimeContextStorage();
739
+ function getRuntimeContextStorage() {
740
+ const runtimeGlobal = globalThis;
741
+ const existing = runtimeGlobal[RUNTIME_CONTEXT_KEY];
742
+ if (existing !== void 0) {
743
+ return existing;
744
+ }
745
+ const storage = new AsyncLocalStorage2();
746
+ Object.defineProperty(runtimeGlobal, RUNTIME_CONTEXT_KEY, {
747
+ configurable: false,
748
+ enumerable: false,
749
+ value: storage,
750
+ writable: false
751
+ });
752
+ return storage;
753
+ }
754
+ function getWikiGraphSDKRuntimeContext() {
755
+ return runtimeContext.getStore() ?? {
756
+ cwd: process.cwd(),
757
+ env: process.env,
758
+ envPolicy: "production",
759
+ providers: {}
760
+ };
761
+ }
762
+
763
+ // src/runtime-path.ts
764
+ function resolveWikiGraphRuntimePath(path) {
765
+ const context = getWikiGraphSDKRuntimeContext();
766
+ const environmentHome = context.env.HOME?.trim();
767
+ const home = environmentHome === void 0 || environmentHome === "" ? homedir2() : environmentHome;
768
+ if (path === "~") return home;
769
+ if (path.startsWith("~/")) return resolve2(home, path.slice(2));
770
+ return isAbsolute2(path) ? path : resolve2(context.cwd, path);
771
+ }
772
+
773
+ // src/archive/target.ts
774
+ var WikiGraphArchiveOwnershipError = class extends Error {
775
+ code = "WIKI_GRAPH_ARCHIVE_OWNERSHIP_MISMATCH";
776
+ constructor(message) {
777
+ super(message);
778
+ this.name = "WikiGraphArchiveOwnershipError";
779
+ }
780
+ };
781
+ async function resolveWikiGraphArchiveLocation(target) {
782
+ if (target.kind === "standalone") {
783
+ return await resolveStandaloneLocation(target);
784
+ }
785
+ return await resolveLibraryLocation(target);
786
+ }
787
+ async function assertStandaloneWikiGraphArchivePath(inputPath) {
788
+ const path = resolveWikiGraphRuntimePath(inputPath);
789
+ const candidate = await canonicalPath(path);
790
+ const candidateIdentity = await readFileIdentity(path);
791
+ const libraries = await listWikiGraphLibraries();
792
+ const matches = await findMatchingMembers(
793
+ libraries,
794
+ candidate,
795
+ candidateIdentity
796
+ );
797
+ if (matches.length > 0) {
798
+ const match = requireUnambiguousManagedMatch(path, matches);
799
+ throw new WikiGraphArchiveOwnershipError(
800
+ `Standalone archive path belongs to registered library ${formatWikiGraphLibraryUri(
801
+ match.library.isDefault ? void 0 : match.library.publicId
802
+ )}: ${path}. Use the library UUID URI ${match.archive.uri} instead.`
803
+ );
804
+ }
805
+ for (const library of libraries) {
806
+ const rootPath = getNodeResourcePath(library.folder);
807
+ const root = await canonicalPath(rootPath);
808
+ if (!isWithin(root, candidate)) continue;
809
+ throw new WikiGraphArchiveOwnershipError(
810
+ `Standalone archive path belongs to registered library ${formatWikiGraphLibraryUri(
811
+ library.isDefault ? void 0 : library.publicId
812
+ )}: ${path}. Scan the library, then address the archive by its library UUID URI.`
813
+ );
814
+ }
815
+ return path;
816
+ }
817
+ async function resolveStandaloneLocation(target) {
818
+ if (target.path.startsWith("wikg://lib/")) {
819
+ throw new WikiGraphArchiveOwnershipError(
820
+ `A library archive URI cannot be used as a standalone path: ${target.path}`
821
+ );
822
+ }
823
+ const path = await assertStandaloneWikiGraphArchivePath(target.path);
824
+ const normalized = {
825
+ kind: "standalone",
826
+ path,
827
+ ...target.objectUri === void 0 ? {} : { objectUri: target.objectUri }
828
+ };
829
+ return {
830
+ archiveFile: new NodeFile(path),
831
+ archiveKey: path,
832
+ archivePath: path,
833
+ indexScope: { archiveKey: path, archivePath: path, kind: "archive-index" },
834
+ locatedUri: formatLocatedWikiGraphUri(path, target.objectUri),
835
+ target: normalized
836
+ };
837
+ }
838
+ async function resolveLibraryLocation(target) {
839
+ const parsedLocated = parseLocatedWikiGraphUri(target.uri);
840
+ const archiveUri = parsedLocated.archivePath ?? target.uri;
841
+ const parsed = parseWikiGraphLibraryUri(archiveUri);
842
+ if (parsed?.kind !== "archive") {
843
+ throw new WikiGraphArchiveOwnershipError(
844
+ `Library archive target must be a UUID archive URI: ${target.uri}`
845
+ );
846
+ }
847
+ await resolveWikiGraphLibrary(parsed);
848
+ const archiveFile = await resolveWikiGraphLibraryArchiveFile(archiveUri);
849
+ const normalized = {
850
+ kind: "library",
851
+ uri: target.uri
852
+ };
853
+ return {
854
+ archiveFile,
855
+ archiveKey: archiveUri,
856
+ archivePath: archiveUri,
857
+ indexScope: {
858
+ archiveKey: archiveUri,
859
+ archivePath: archiveUri,
860
+ kind: "archive-index"
861
+ },
862
+ libraryArchiveTarget: parsed,
863
+ locatedUri: target.uri,
864
+ publicArchiveUri: archiveUri,
865
+ target: normalized
866
+ };
867
+ }
868
+ async function canonicalPath(path) {
869
+ try {
870
+ return await realpath(path);
871
+ } catch {
872
+ const absolute = resolve3(path);
873
+ const parent = dirname2(absolute);
874
+ return parent === absolute ? absolute : join2(await canonicalPath(parent), basename2(absolute));
875
+ }
876
+ }
877
+ function isWithin(root, candidate) {
878
+ const child = relative(root, candidate);
879
+ return child === "" || !child.startsWith("..") && !isAbsolute3(child);
880
+ }
881
+ async function findMatchingMembers(libraries, candidate, candidateIdentity) {
882
+ const members = [];
883
+ for (const library of libraries) {
884
+ const archives = await listWikiGraphLibraryArchives({
885
+ isDefault: library.isDefault,
886
+ kind: "scope",
887
+ ...library.isDefault ? {} : { publicId: library.publicId }
888
+ });
889
+ for (const archive of archives) {
890
+ if (archive.file === void 0) continue;
891
+ members.push({
892
+ archive,
893
+ library,
894
+ memberPath: getNodeResourcePath(archive.file)
895
+ });
896
+ }
897
+ }
898
+ const exact = [];
899
+ for (const member of members) {
900
+ if (await canonicalPath(member.memberPath) === candidate) {
901
+ exact.push(member);
902
+ }
903
+ }
904
+ if (exact.length > 0 || candidateIdentity === void 0) return exact;
905
+ const identityMatches = [];
906
+ for (const member of members) {
907
+ if (await readFileIdentity(member.memberPath) === candidateIdentity) {
908
+ identityMatches.push(member);
909
+ }
910
+ }
911
+ return identityMatches;
912
+ }
913
+ function requireUnambiguousManagedMatch(path, matches) {
914
+ if (matches.length === 1) return matches[0];
915
+ throw new WikiGraphArchiveOwnershipError(
916
+ `Archive file identity is ambiguous across managed archives: ${path}. Use an explicit library UUID URI (${matches.map((match) => match.archive.uri).join(", ")}).`
917
+ );
918
+ }
919
+ async function readFileIdentity(path) {
920
+ try {
921
+ const value = await stat2(path);
922
+ return `${value.dev}:${value.ino}`;
923
+ } catch {
924
+ return void 0;
925
+ }
926
+ }
927
+
928
+ // src/archive/write.ts
929
+ import {
930
+ WikiGraphArchiveFile,
931
+ writeWikiGraphLibraryArchive
932
+ } from "wiki-graph-core";
933
+ async function writeWikiGraphArchiveLocation(location, operation, options = {}) {
934
+ const cacheOnly = options.searchIndexWritebackPolicy === "cache";
935
+ const write = async (archiveFile = location.archiveFile) => await new WikiGraphArchiveFile(archiveFile).write(operation, {
936
+ ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
937
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
938
+ invalidateContinuationCursors: options.invalidateContinuationCursors
939
+ },
940
+ ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
941
+ });
942
+ if (location.libraryArchiveTarget === void 0 || cacheOnly || options.refreshLibraryIndex === false) {
943
+ return await write();
944
+ }
945
+ return await writeWikiGraphLibraryArchive({
946
+ additionalDerivedStateKeys: [location.archiveKey, location.archivePath],
947
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
948
+ invalidateContinuationCursors: options.invalidateContinuationCursors
949
+ },
950
+ ...options.onIndexSyncError === void 0 ? {} : { onIndexSyncError: options.onIndexSyncError },
951
+ operation: async (archiveFile) => await write(archiveFile),
952
+ target: location.libraryArchiveTarget
953
+ });
954
+ }
955
+
705
956
  // src/worker.ts
706
957
  import {
707
958
  applyChapterJobArtifactFile as applyChapterJobArtifactFile2,
708
959
  createLocalChapterJobFileExecutor,
709
- writeChapterJobInputFile
960
+ writeChapterJobInputFile as writeChapterJobInputFile2
710
961
  } from "wiki-graph-core/worker";
962
+ async function extractWikiGraphJobSnapshots(options) {
963
+ ensureNodeWikiGraphPlatform();
964
+ return await withNodeWikiGraphStorage(
965
+ options.stateDir,
966
+ async () => await new WikiGraphArchiveFile2(
967
+ new NodeFile(options.wikgPath)
968
+ ).readDocument(async (document) => {
969
+ const results = [];
970
+ for (const request of options.requests) {
971
+ options.signal?.throwIfAborted();
972
+ const chapterId = await resolveChapterPathReadonly(
973
+ document,
974
+ request.chapterPath
975
+ );
976
+ const revision = await writeChapterJobInputFile(
977
+ document,
978
+ chapterId,
979
+ request.kind,
980
+ new NodeFile(request.outputPath),
981
+ request.options
982
+ );
983
+ const result = { ...request, chapterId, revision };
984
+ results.push(result);
985
+ await options.onSnapshot?.(result);
986
+ }
987
+ options.signal?.throwIfAborted();
988
+ return results;
989
+ })
990
+ );
991
+ }
711
992
  async function applyWikiGraphJobArtifacts(options) {
712
993
  ensureNodeWikiGraphPlatform();
713
994
  return await withNodeWikiGraphStorage(options.stateDir, async () => {
995
+ const location = await resolveWikiGraphArchiveLocation(options.archive);
714
996
  let applied = 0;
715
- await new WikiGraphArchiveFile(new NodeFile(options.wikgPath)).write(
997
+ await writeWikiGraphArchiveLocation(
998
+ location,
716
999
  async (document) => {
717
1000
  for await (const artifact of options.artifacts) {
718
1001
  options.signal?.throwIfAborted();
@@ -728,7 +1011,8 @@ async function applyWikiGraphJobArtifacts(options) {
728
1011
  );
729
1012
  applied += 1;
730
1013
  }
731
- }
1014
+ },
1015
+ { invalidateContinuationCursors: true }
732
1016
  );
733
1017
  options.signal?.throwIfAborted();
734
1018
  return { applied };
@@ -746,7 +1030,8 @@ export {
746
1030
  applyChapterJobArtifactFile2 as applyChapterJobArtifactFile,
747
1031
  applyWikiGraphJobArtifacts,
748
1032
  createLocalChapterJobFileExecutor,
1033
+ extractWikiGraphJobSnapshots,
749
1034
  runBuildJobWorker,
750
- writeChapterJobInputFile
1035
+ writeChapterJobInputFile2 as writeChapterJobInputFile
751
1036
  };
752
1037
  //# sourceMappingURL=worker.js.map