wiki-graph-sdk 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +13 -3
  2. package/dist/archives.cjs +382 -25
  3. package/dist/archives.cjs.map +1 -1
  4. package/dist/archives.d.ts +4 -216
  5. package/dist/archives.js +372 -13
  6. package/dist/archives.js.map +1 -1
  7. package/dist/conversions.cjs +11 -12
  8. package/dist/conversions.cjs.map +1 -1
  9. package/dist/conversions.js +6 -7
  10. package/dist/conversions.js.map +1 -1
  11. package/dist/default-worker.cjs +40 -17
  12. package/dist/default-worker.cjs.map +1 -1
  13. package/dist/default-worker.js +37 -14
  14. package/dist/default-worker.js.map +1 -1
  15. package/dist/embedding.cjs +6 -7
  16. package/dist/embedding.cjs.map +1 -1
  17. package/dist/embedding.js +6 -7
  18. package/dist/embedding.js.map +1 -1
  19. package/dist/gc.cjs +61 -9
  20. package/dist/gc.cjs.map +1 -1
  21. package/dist/gc.d.ts +1 -1
  22. package/dist/gc.js +61 -9
  23. package/dist/gc.js.map +1 -1
  24. package/dist/host-IbqJ8WSV.d.ts +18 -0
  25. package/dist/index-DQzLAnjf.d.ts +346 -0
  26. package/dist/index.cjs +622 -40
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.ts +20 -12
  29. package/dist/index.js +601 -31
  30. package/dist/index.js.map +1 -1
  31. package/dist/local-config.cjs +6 -7
  32. package/dist/local-config.cjs.map +1 -1
  33. package/dist/local-config.js +6 -7
  34. package/dist/local-config.js.map +1 -1
  35. package/dist/maintenance.cjs.map +1 -1
  36. package/dist/maintenance.js.map +1 -1
  37. package/dist/runtime-config.cjs +6 -7
  38. package/dist/runtime-config.cjs.map +1 -1
  39. package/dist/runtime-config.js +6 -7
  40. package/dist/runtime-config.js.map +1 -1
  41. package/dist/stage.cjs +6 -7
  42. package/dist/stage.cjs.map +1 -1
  43. package/dist/stage.js +6 -7
  44. package/dist/stage.js.map +1 -1
  45. package/dist/worker.cjs +55 -31
  46. package/dist/worker.cjs.map +1 -1
  47. package/dist/worker.d.ts +1 -1
  48. package/dist/worker.js +61 -37
  49. package/dist/worker.js.map +1 -1
  50. package/package.json +2 -2
  51. package/dist/host-pQiVAKEY.d.ts +0 -10
package/dist/archives.js CHANGED
@@ -15,6 +15,8 @@ import {
15
15
  assertNoActiveBuildJobConflicts,
16
16
  assertNoActiveBuildJobs,
17
17
  deleteArchiveSearchSessions,
18
+ createContinuationCursor,
19
+ deleteContinuationCursor,
18
20
  DirectoryDocument,
19
21
  findArchiveObjects,
20
22
  formatLocatedWikiGraphUri as formatLocatedWikiGraphUri2,
@@ -839,13 +841,15 @@ function getWikiGraphSDKRuntimeContext() {
839
841
  };
840
842
  }
841
843
  function getWikiGraphSDKStateDir() {
842
- return getWikiGraphSDKRuntimeContext().stateDir;
844
+ const context = getWikiGraphSDKRuntimeContext();
845
+ return context.host?.stateDir ?? context.stateDir;
843
846
  }
844
847
  function getWikiGraphSDKProviders() {
845
848
  return getWikiGraphSDKRuntimeContext().providers ?? {};
846
849
  }
847
850
 
848
851
  // src/local-config.ts
852
+ import { mkdir as mkdir2 } from "fs/promises";
849
853
  import { join as join2 } from "path";
850
854
  import { homedir as homedir2 } from "os";
851
855
  var LOCAL_CONFIG_SCHEMA_SQL = `
@@ -875,13 +879,10 @@ function validateLocalConfigSection(section, value) {
875
879
  }
876
880
  }
877
881
  async function withLocalConfigDatabase(operation) {
882
+ const stateDir = getWikiGraphSDKStateDir() ?? join2(homedir2(), ".wikigraph");
883
+ await mkdir2(stateDir, { recursive: true });
878
884
  const database = await openSharedStateDatabase(
879
- new NodeFile(
880
- join2(
881
- getWikiGraphSDKStateDir() ?? join2(homedir2(), ".wikigraph"),
882
- "core.sqlite"
883
- )
884
- ),
885
+ new NodeFile(join2(stateDir, "core.sqlite")),
885
886
  LOCAL_CONFIG_SCHEMA_SQL
886
887
  );
887
888
  try {
@@ -1926,6 +1927,236 @@ async function writeWikiGraphArchiveLocation(location, operation, options = {})
1926
1927
  });
1927
1928
  }
1928
1929
 
1930
+ // src/pages.ts
1931
+ function createCursor(implementation) {
1932
+ let released = false;
1933
+ const assertLive = () => {
1934
+ if (released) throw new Error("Cursor has been released.");
1935
+ };
1936
+ return {
1937
+ pageClass: implementation.pageClass,
1938
+ async next(options = {}) {
1939
+ assertLive();
1940
+ return await implementation.next(options);
1941
+ },
1942
+ async token() {
1943
+ assertLive();
1944
+ return await implementation.token();
1945
+ },
1946
+ async release() {
1947
+ if (released) return;
1948
+ released = true;
1949
+ await implementation.release?.();
1950
+ }
1951
+ };
1952
+ }
1953
+ function createPageCursor(implementation) {
1954
+ let token;
1955
+ return createCursor({
1956
+ pageClass: implementation.pageClass,
1957
+ next: async (options) => await implementation.next(implementation.rawCursor, options),
1958
+ token: async () => {
1959
+ token ??= await implementation.createToken(implementation.rawCursor);
1960
+ return token;
1961
+ },
1962
+ release: async () => {
1963
+ if (token !== void 0) await implementation.releaseToken?.(token);
1964
+ token = void 0;
1965
+ }
1966
+ });
1967
+ }
1968
+
1969
+ // src/result-pages.ts
1970
+ var ArchiveFindPage = class _ArchiveFindPage {
1971
+ result;
1972
+ chapters;
1973
+ items;
1974
+ lens;
1975
+ lensHint;
1976
+ limit;
1977
+ match;
1978
+ rawNextCursor;
1979
+ order;
1980
+ query;
1981
+ terms;
1982
+ types;
1983
+ #cursor;
1984
+ constructor(result, cursor = null) {
1985
+ this.result = result;
1986
+ this.chapters = result.chapters;
1987
+ this.items = result.items;
1988
+ this.lens = result.lens;
1989
+ this.lensHint = result.lensHint;
1990
+ this.limit = result.limit;
1991
+ this.match = result.match;
1992
+ this.rawNextCursor = result.nextCursor;
1993
+ this.order = result.order;
1994
+ this.query = result.query;
1995
+ this.terms = result.terms;
1996
+ this.types = result.types;
1997
+ this.#cursor = cursor;
1998
+ }
1999
+ static pageClass = _ArchiveFindPage;
2000
+ [Symbol.iterator]() {
2001
+ return this.items[Symbol.iterator]();
2002
+ }
2003
+ nextCursor() {
2004
+ return this.#cursor;
2005
+ }
2006
+ };
2007
+ var ArchiveCollectionPage = class _ArchiveCollectionPage {
2008
+ result;
2009
+ chapters;
2010
+ ids;
2011
+ items;
2012
+ limit;
2013
+ rawNextCursor;
2014
+ order;
2015
+ types;
2016
+ #cursor;
2017
+ constructor(result, cursor = null) {
2018
+ this.result = result;
2019
+ this.chapters = result.chapters;
2020
+ this.ids = result.ids;
2021
+ this.items = result.items;
2022
+ this.limit = result.limit;
2023
+ this.rawNextCursor = result.nextCursor;
2024
+ this.order = result.order;
2025
+ this.types = result.types;
2026
+ this.#cursor = cursor;
2027
+ }
2028
+ static pageClass = _ArchiveCollectionPage;
2029
+ [Symbol.iterator]() {
2030
+ return this.items[Symbol.iterator]();
2031
+ }
2032
+ nextCursor() {
2033
+ return this.#cursor;
2034
+ }
2035
+ };
2036
+ var ArchiveEvidencePage = class _ArchiveEvidencePage {
2037
+ result;
2038
+ items;
2039
+ limit;
2040
+ rawNextCursor;
2041
+ #cursor;
2042
+ constructor(result, cursor = null) {
2043
+ this.result = result;
2044
+ this.items = result.items;
2045
+ this.limit = result.limit;
2046
+ this.rawNextCursor = result.nextCursor;
2047
+ this.#cursor = cursor;
2048
+ }
2049
+ static pageClass = _ArchiveEvidencePage;
2050
+ [Symbol.iterator]() {
2051
+ return this.items[Symbol.iterator]();
2052
+ }
2053
+ nextCursor() {
2054
+ return this.#cursor;
2055
+ }
2056
+ };
2057
+ var ArchiveRelatedPage = class _ArchiveRelatedPage {
2058
+ result;
2059
+ items;
2060
+ limit;
2061
+ rawNextCursor;
2062
+ #cursor;
2063
+ constructor(result, cursor = null) {
2064
+ this.result = result;
2065
+ this.items = result.items;
2066
+ this.limit = result.limit;
2067
+ this.rawNextCursor = result.nextCursor;
2068
+ this.#cursor = cursor;
2069
+ }
2070
+ static pageClass = _ArchiveRelatedPage;
2071
+ [Symbol.iterator]() {
2072
+ return this.items[Symbol.iterator]();
2073
+ }
2074
+ nextCursor() {
2075
+ return this.#cursor;
2076
+ }
2077
+ };
2078
+ var ArchiveSourceLocatorPage = class _ArchiveSourceLocatorPage {
2079
+ result;
2080
+ items;
2081
+ limit;
2082
+ rawNextCursor;
2083
+ #cursor;
2084
+ constructor(result, cursor = null) {
2085
+ this.result = result;
2086
+ this.items = result.items;
2087
+ this.limit = result.limit;
2088
+ this.rawNextCursor = result.nextCursor;
2089
+ this.#cursor = cursor;
2090
+ }
2091
+ static pageClass = _ArchiveSourceLocatorPage;
2092
+ [Symbol.iterator]() {
2093
+ return this.items[Symbol.iterator]();
2094
+ }
2095
+ nextCursor() {
2096
+ return this.#cursor;
2097
+ }
2098
+ };
2099
+ function createArchiveFindPage(result, next, createToken, releaseToken) {
2100
+ return new ArchiveFindPage(
2101
+ result,
2102
+ result.nextCursor === null ? null : createPageCursor({
2103
+ pageClass: ArchiveFindPage,
2104
+ rawCursor: result.nextCursor,
2105
+ next,
2106
+ createToken,
2107
+ releaseToken
2108
+ })
2109
+ );
2110
+ }
2111
+ function createArchiveCollectionPage(result, next, createToken, releaseToken) {
2112
+ return new ArchiveCollectionPage(
2113
+ result,
2114
+ result.nextCursor === null ? null : createPageCursor({
2115
+ pageClass: ArchiveCollectionPage,
2116
+ rawCursor: result.nextCursor,
2117
+ next,
2118
+ createToken,
2119
+ releaseToken
2120
+ })
2121
+ );
2122
+ }
2123
+ function createArchiveEvidencePage(result, next, createToken, releaseToken) {
2124
+ return new ArchiveEvidencePage(
2125
+ result,
2126
+ result.nextCursor === null ? null : createPageCursor({
2127
+ pageClass: ArchiveEvidencePage,
2128
+ rawCursor: result.nextCursor,
2129
+ next,
2130
+ createToken,
2131
+ releaseToken
2132
+ })
2133
+ );
2134
+ }
2135
+ function createArchiveRelatedPage(result, next, createToken, releaseToken) {
2136
+ return new ArchiveRelatedPage(
2137
+ result,
2138
+ result.nextCursor === null ? null : createPageCursor({
2139
+ pageClass: ArchiveRelatedPage,
2140
+ rawCursor: result.nextCursor,
2141
+ next,
2142
+ createToken,
2143
+ releaseToken
2144
+ })
2145
+ );
2146
+ }
2147
+ function createArchiveSourceLocatorPage(result, next, createToken, releaseToken) {
2148
+ return new ArchiveSourceLocatorPage(
2149
+ result,
2150
+ result.nextCursor === null ? null : createPageCursor({
2151
+ pageClass: ArchiveSourceLocatorPage,
2152
+ rawCursor: result.nextCursor,
2153
+ next,
2154
+ createToken,
2155
+ releaseToken
2156
+ })
2157
+ );
2158
+ }
2159
+
1929
2160
  // src/archive/archive.ts
1930
2161
  var WikiGraphArchiveExistsError = class extends Error {
1931
2162
  code = "WIKI_GRAPH_ARCHIVE_EXISTS";
@@ -2013,6 +2244,14 @@ var WikiGraphArchiveHandle = class {
2013
2244
  get target() {
2014
2245
  return this.#location.target;
2015
2246
  }
2247
+ async #persistCursor(cursor) {
2248
+ return await this.#runtime.run(
2249
+ async () => await createContinuationCursor(cursor)
2250
+ );
2251
+ }
2252
+ async #releaseCursor(cursor) {
2253
+ await this.#runtime.run(async () => await deleteContinuationCursor(cursor));
2254
+ }
2016
2255
  async inspect(options = {}) {
2017
2256
  return await this.#readDocument(
2018
2257
  async (document) => await inspectWikiGraphArchive(document, {
@@ -2027,7 +2266,7 @@ var WikiGraphArchiveHandle = class {
2027
2266
  options.queryMode,
2028
2267
  options.signal
2029
2268
  );
2030
- return await this.#writeDocument(
2269
+ const result = await this.#writeDocument(
2031
2270
  async (document) => {
2032
2271
  const chapters = await this.#resolveQueryChapters(document, options, {
2033
2272
  embeddingProvider
@@ -2048,9 +2287,38 @@ var WikiGraphArchiveHandle = class {
2048
2287
  ...options.signal === void 0 ? {} : { signal: options.signal }
2049
2288
  }
2050
2289
  );
2290
+ return createArchiveFindPage(
2291
+ result,
2292
+ async (cursor, nextOptions) => await this.search(query, {
2293
+ ...options,
2294
+ cursor,
2295
+ ...nextOptions.limit === void 0 ? {} : { limit: nextOptions.limit },
2296
+ ...nextOptions.signal === void 0 ? {} : { signal: nextOptions.signal }
2297
+ }),
2298
+ (cursor) => this.#persistCursor({
2299
+ archiveKey: this.archiveKey,
2300
+ archivePath: this.path,
2301
+ chapters: options.chapters ?? null,
2302
+ cursor,
2303
+ format: "json",
2304
+ indexScope: this.indexScope,
2305
+ kind: "search",
2306
+ ...options.backlinks === void 0 ? {} : { backlinks: options.backlinks },
2307
+ ...options.evidenceLimit === void 0 ? {} : { evidenceLimit: options.evidenceLimit },
2308
+ ...options.match === void 0 ? {} : { match: options.match },
2309
+ ...options.order === void 0 ? {} : { order: options.order },
2310
+ query,
2311
+ ...options.queryMode === void 0 ? {} : { queryMode: options.queryMode },
2312
+ ...options.skipUnindexed === void 0 ? {} : { skipUnindexed: options.skipUnindexed },
2313
+ ...options.sourceContext === void 0 ? {} : { sourceContext: options.sourceContext },
2314
+ ...options.triplePattern === void 0 ? {} : { triplePattern: options.triplePattern },
2315
+ types: options.types ?? null
2316
+ }),
2317
+ (token) => this.#releaseCursor(token)
2318
+ );
2051
2319
  }
2052
2320
  async list(options = {}) {
2053
- return await this.#readDocument(async (document) => {
2321
+ const result = await this.#readDocument(async (document) => {
2054
2322
  if (isSourceLocatorScopeUri(this.objectUri)) {
2055
2323
  return await listArchiveSourceLocators(document, this.objectUri, {
2056
2324
  ...options.cursor === void 0 ? {} : { cursor: options.cursor },
@@ -2063,6 +2331,53 @@ var WikiGraphArchiveHandle = class {
2063
2331
  ...chapters === void 0 ? {} : { chapters }
2064
2332
  });
2065
2333
  }, options);
2334
+ if (isSourceLocatorScopeUri(this.objectUri)) {
2335
+ return createArchiveSourceLocatorPage(
2336
+ result,
2337
+ async (cursor, nextOptions) => await this.list({
2338
+ ...options,
2339
+ cursor,
2340
+ ...nextOptions.limit === void 0 ? {} : { limit: nextOptions.limit },
2341
+ ...nextOptions.signal === void 0 ? {} : { signal: nextOptions.signal }
2342
+ }),
2343
+ (cursor) => this.#persistCursor({
2344
+ archiveKey: this.archiveKey,
2345
+ archivePath: this.path,
2346
+ cursor,
2347
+ format: "json",
2348
+ indexScope: this.indexScope,
2349
+ kind: "source-locators",
2350
+ targetUri: this.objectUri
2351
+ }),
2352
+ (token) => this.#releaseCursor(token)
2353
+ );
2354
+ }
2355
+ return createArchiveCollectionPage(
2356
+ result,
2357
+ async (cursor, nextOptions) => await this.list({
2358
+ ...options,
2359
+ cursor,
2360
+ ...nextOptions.limit === void 0 ? {} : { limit: nextOptions.limit },
2361
+ ...nextOptions.signal === void 0 ? {} : { signal: nextOptions.signal }
2362
+ }),
2363
+ (cursor) => this.#persistCursor({
2364
+ archiveKey: this.archiveKey,
2365
+ archivePath: this.path,
2366
+ ...options.backlinks === void 0 ? {} : { backlinks: options.backlinks },
2367
+ chapters: options.chapters ?? null,
2368
+ cursor,
2369
+ ...options.evidenceLimit === void 0 ? {} : { evidenceLimit: options.evidenceLimit },
2370
+ format: "json",
2371
+ ids: options.ids ?? null,
2372
+ indexScope: this.indexScope,
2373
+ kind: "collection",
2374
+ order: options.order ?? "doc-asc",
2375
+ ...options.sourceContext === void 0 ? {} : { sourceContext: options.sourceContext },
2376
+ ...options.triplePattern === void 0 ? {} : { triplePattern: options.triplePattern },
2377
+ types: options.types ?? null
2378
+ }),
2379
+ (token) => this.#releaseCursor(token)
2380
+ );
2066
2381
  }
2067
2382
  async ensureSearchIndex(options = {}) {
2068
2383
  const embeddingProvider = await this.#runtime.run(
@@ -2339,19 +2654,30 @@ var WikiGraphArchiveHandle = class {
2339
2654
  if (options.queryMode !== void 0) {
2340
2655
  throw new Error("`queryMode` requires `query`.");
2341
2656
  }
2342
- return await this.#readDocument(async (document) => {
2657
+ const result2 = await this.#readDocument(async (document) => {
2343
2658
  const chapters = await this.#resolveScope(document, {});
2344
2659
  return await listRelatedArchiveObjects(document, objectUri, {
2345
2660
  ...withoutOperation(options),
2346
2661
  ...chapters === void 0 ? {} : { chapters }
2347
2662
  });
2348
2663
  }, options);
2664
+ return createArchiveRelatedPage(
2665
+ result2,
2666
+ async (cursor, nextOptions) => await this.related(objectUri, {
2667
+ ...options,
2668
+ cursor,
2669
+ ...nextOptions.limit === void 0 ? {} : { limit: nextOptions.limit },
2670
+ ...nextOptions.signal === void 0 ? {} : { signal: nextOptions.signal }
2671
+ }),
2672
+ (cursor) => Promise.resolve(cursor),
2673
+ () => Promise.resolve()
2674
+ );
2349
2675
  }
2350
2676
  const embeddingProvider = await this.#resolveQueryEmbeddingProvider(
2351
2677
  options.queryMode,
2352
2678
  options.signal
2353
2679
  );
2354
- return await this.#writeDocument(
2680
+ const result = await this.#writeDocument(
2355
2681
  async (document) => {
2356
2682
  const chapters = await this.#resolveQueryChapters(document, options, {
2357
2683
  embeddingProvider
@@ -2371,25 +2697,47 @@ var WikiGraphArchiveHandle = class {
2371
2697
  ...options.signal === void 0 ? {} : { signal: options.signal }
2372
2698
  }
2373
2699
  );
2700
+ return createArchiveRelatedPage(
2701
+ result,
2702
+ async (cursor, nextOptions) => await this.related(objectUri, {
2703
+ ...options,
2704
+ cursor,
2705
+ ...nextOptions.limit === void 0 ? {} : { limit: nextOptions.limit },
2706
+ ...nextOptions.signal === void 0 ? {} : { signal: nextOptions.signal }
2707
+ }),
2708
+ (cursor) => Promise.resolve(cursor),
2709
+ () => Promise.resolve()
2710
+ );
2374
2711
  }
2375
2712
  async evidence(objectUri = this.objectUri, options = {}) {
2376
2713
  if (options.query === void 0) {
2377
2714
  if (options.queryMode !== void 0) {
2378
2715
  throw new Error("`queryMode` requires `query`.");
2379
2716
  }
2380
- return await this.#readDocument(async (document) => {
2717
+ const result2 = await this.#readDocument(async (document) => {
2381
2718
  const chapters = await this.#resolveScope(document, {});
2382
2719
  return await listArchiveEvidence(document, objectUri, {
2383
2720
  ...withoutOperation(options),
2384
2721
  ...chapters === void 0 ? {} : { chapters }
2385
2722
  });
2386
2723
  }, options);
2724
+ return createArchiveEvidencePage(
2725
+ result2,
2726
+ async (cursor, nextOptions) => await this.evidence(objectUri, {
2727
+ ...options,
2728
+ cursor,
2729
+ ...nextOptions.limit === void 0 ? {} : { limit: nextOptions.limit },
2730
+ ...nextOptions.signal === void 0 ? {} : { signal: nextOptions.signal }
2731
+ }),
2732
+ (cursor) => Promise.resolve(cursor),
2733
+ () => Promise.resolve()
2734
+ );
2387
2735
  }
2388
2736
  const embeddingProvider = await this.#resolveQueryEmbeddingProvider(
2389
2737
  options.queryMode,
2390
2738
  options.signal
2391
2739
  );
2392
- return await this.#writeDocument(
2740
+ const result = await this.#writeDocument(
2393
2741
  async (document) => {
2394
2742
  const chapters = await this.#resolveQueryChapters(document, options, {
2395
2743
  embeddingProvider
@@ -2409,6 +2757,17 @@ var WikiGraphArchiveHandle = class {
2409
2757
  ...options.signal === void 0 ? {} : { signal: options.signal }
2410
2758
  }
2411
2759
  );
2760
+ return createArchiveEvidencePage(
2761
+ result,
2762
+ async (cursor, nextOptions) => await this.evidence(objectUri, {
2763
+ ...options,
2764
+ cursor,
2765
+ ...nextOptions.limit === void 0 ? {} : { limit: nextOptions.limit },
2766
+ ...nextOptions.signal === void 0 ? {} : { signal: nextOptions.signal }
2767
+ }),
2768
+ (cursor) => Promise.resolve(cursor),
2769
+ () => Promise.resolve()
2770
+ );
2412
2771
  }
2413
2772
  async pack(objectUri = this.objectUri, budget = 5e3, options = {}) {
2414
2773
  return await this.#readDocument(