wiki-graph-sdk 0.8.0 → 0.8.2

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 (64) hide show
  1. package/README.md +46 -0
  2. package/dist/archives.cjs +98 -17
  3. package/dist/archives.cjs.map +1 -1
  4. package/dist/archives.d.ts +10 -2
  5. package/dist/archives.js +100 -17
  6. package/dist/archives.js.map +1 -1
  7. package/dist/conversions.cjs +23 -5
  8. package/dist/conversions.cjs.map +1 -1
  9. package/dist/conversions.d.ts +6 -1
  10. package/dist/conversions.js +24 -5
  11. package/dist/conversions.js.map +1 -1
  12. package/dist/default-worker.cjs +84 -21
  13. package/dist/default-worker.cjs.map +1 -1
  14. package/dist/default-worker.d.ts +1 -0
  15. package/dist/default-worker.js +88 -21
  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 +3 -1
  21. package/dist/embedding.js.map +1 -1
  22. package/dist/gc.cjs +52 -10
  23. package/dist/gc.cjs.map +1 -1
  24. package/dist/gc.d.ts +3 -0
  25. package/dist/gc.js +56 -10
  26. package/dist/gc.js.map +1 -1
  27. package/dist/host-pQiVAKEY.d.ts +10 -0
  28. package/dist/index.cjs +422 -57
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.d.ts +33 -12
  31. package/dist/index.js +427 -59
  32. package/dist/index.js.map +1 -1
  33. package/dist/{jobs-xIm_kshM.d.ts → jobs-CRK-DZ40.d.ts} +5 -0
  34. package/dist/local-config.cjs +2 -1
  35. package/dist/local-config.cjs.map +1 -1
  36. package/dist/local-config.js +3 -1
  37. package/dist/local-config.js.map +1 -1
  38. package/dist/maintenance.cjs +2 -1
  39. package/dist/maintenance.cjs.map +1 -1
  40. package/dist/maintenance.d.ts +4 -1
  41. package/dist/maintenance.js +3 -1
  42. package/dist/maintenance.js.map +1 -1
  43. package/dist/node-platform.cjs +18 -4
  44. package/dist/node-platform.cjs.map +1 -1
  45. package/dist/node-platform.d.ts +12 -3
  46. package/dist/node-platform.js +18 -4
  47. package/dist/node-platform.js.map +1 -1
  48. package/dist/runtime-config.cjs +4 -3
  49. package/dist/runtime-config.cjs.map +1 -1
  50. package/dist/runtime-config.d.ts +4 -1
  51. package/dist/runtime-config.js +5 -3
  52. package/dist/runtime-config.js.map +1 -1
  53. package/dist/stage-BnWI3Rqt.d.ts +33 -0
  54. package/dist/stage.cjs +22 -12
  55. package/dist/stage.cjs.map +1 -1
  56. package/dist/stage.d.ts +2 -14
  57. package/dist/stage.js +23 -12
  58. package/dist/stage.js.map +1 -1
  59. package/dist/worker.cjs +83 -43
  60. package/dist/worker.cjs.map +1 -1
  61. package/dist/worker.d.ts +7 -2
  62. package/dist/worker.js +87 -43
  63. package/dist/worker.js.map +1 -1
  64. package/package.json +2 -2
@@ -69,12 +69,16 @@ function getWikiGraphSDKRuntimeContext() {
69
69
  return runtimeContext.getStore() ?? {
70
70
  cwd: process.cwd(),
71
71
  env: process.env,
72
- envPolicy: "production"
72
+ envPolicy: "production",
73
+ providers: {}
73
74
  };
74
75
  }
75
76
  function getWikiGraphSDKStateDir() {
76
77
  return getWikiGraphSDKRuntimeContext().stateDir;
77
78
  }
79
+ function getWikiGraphSDKProviders() {
80
+ return getWikiGraphSDKRuntimeContext().providers ?? {};
81
+ }
78
82
 
79
83
  // src/node-platform.ts
80
84
  var asyncHooks = __toESM(require("async_hooks"), 1);
@@ -746,15 +750,28 @@ function resolveNodeDataDirectory() {
746
750
  directory = parent;
747
751
  }
748
752
  }
749
- function createNodeWikiGraphStorage(stateRoot = pathModule.join(os.homedir(), ".wikigraph")) {
753
+ function createNodeWikiGraphStorage(stateRootOrOptions = pathModule.join(
754
+ os.homedir(),
755
+ ".wikigraph"
756
+ )) {
757
+ const libraryRoot = typeof stateRootOrOptions === "string" ? stateRootOrOptions : stateRootOrOptions.libraryRoot;
758
+ const documentStoreRoot = typeof stateRootOrOptions === "string" ? pathModule.join(stateRootOrOptions, "documents") : stateRootOrOptions.documentStoreRoot;
750
759
  return {
751
- library: new NodeDirectory(stateRoot),
752
- documentStore: new NodeDirectory(pathModule.join(stateRoot, "documents"))
760
+ library: new NodeDirectory(libraryRoot),
761
+ documentStore: new NodeDirectory(documentStoreRoot)
753
762
  };
754
763
  }
755
- function installNodeWikiGraphPlatform(stateRoot) {
756
- (0, import_platform.installWikiGraphPlatform)(nodeWikiGraphPlatform);
757
- (0, import_platform.installWikiGraphStorage)(createNodeWikiGraphStorage(stateRoot));
764
+ function ensureNodeWikiGraphPlatform() {
765
+ try {
766
+ (0, import_platform.getWikiGraphPlatform)();
767
+ } catch {
768
+ (0, import_platform.installWikiGraphPlatform)(nodeWikiGraphPlatform);
769
+ }
770
+ try {
771
+ (0, import_platform.getWikiGraphStorage)();
772
+ } catch {
773
+ (0, import_platform.installWikiGraphStorage)(createNodeWikiGraphStorage());
774
+ }
758
775
  }
759
776
  async function withNodeWikiGraphStorage(stateRoot, operation) {
760
777
  return await (0, import_platform.withWikiGraphStorage)(
@@ -1090,8 +1107,8 @@ var inlineLLMConfigSchema = import_zod.z.object({
1090
1107
  });
1091
1108
  async function loadWikiGraphRuntimeConfig(options) {
1092
1109
  const [embedding, localLLM, concurrent, wikimedia, wikispine] = await Promise.all([
1093
- readLocalConfigSection("embeddings"),
1094
- readLocalConfigSection("llm"),
1110
+ options?.skipEmbeddingConfig === true ? Promise.resolve({}) : readLocalConfigSection("embeddings"),
1111
+ options?.skipLLMConfig === true ? Promise.resolve({}) : readLocalConfigSection("llm"),
1095
1112
  readLocalConfigSection("concurrent"),
1096
1113
  readLocalConfigSection("wikimedia"),
1097
1114
  readLocalConfigSection("wikispine")
@@ -1366,7 +1383,10 @@ function createOpenAICompatibleName2(baseURL) {
1366
1383
 
1367
1384
  // src/stage.ts
1368
1385
  function createStageLLM(config, options) {
1369
- const llmOptions = buildWikiGraphLLMOptions(config);
1386
+ const llmOptions = options?.provider === void 0 ? buildWikiGraphLLMOptions(config) : {
1387
+ ...config.concurrent?.request === void 0 ? {} : { concurrent: config.concurrent.request },
1388
+ streamProvider: options.provider
1389
+ };
1370
1390
  return new import_wiki_graph_core4.LLM({
1371
1391
  sampling: (0, import_wiki_graph_core3.createDefaultWikiGraphSampling)({
1372
1392
  ...llmOptions.temperature === void 0 ? {} : { temperature: llmOptions.temperature },
@@ -1380,10 +1400,13 @@ function createStageLLM(config, options) {
1380
1400
  });
1381
1401
  }
1382
1402
  async function loadRequiredStageConfig(options) {
1403
+ const injected = options.llmJSON === void 0 ? getWikiGraphSDKProviders().llm : void 0;
1383
1404
  const config = await loadWikiGraphRuntimeConfig({
1384
- ...options.llmJSON === void 0 ? {} : { llmJSON: options.llmJSON }
1405
+ ...options.llmJSON === void 0 ? {} : { llmJSON: options.llmJSON },
1406
+ ...options.llmJSON === void 0 && injected !== void 0 ? { skipLLMConfig: true } : {},
1407
+ ...getWikiGraphSDKProviders().embedding === void 0 ? {} : { skipEmbeddingConfig: true }
1385
1408
  });
1386
- if (config.llm?.provider === void 0 || config.llm.model === void 0) {
1409
+ if (injected === void 0 && (config.llm?.provider === void 0 || config.llm.model === void 0)) {
1387
1410
  throw new Error(
1388
1411
  "Missing LLM configuration. Set --llm for one run, or configure `wikg://local/config/llm` with provider and model."
1389
1412
  );
@@ -1458,6 +1481,31 @@ function abortError(signal) {
1458
1481
  var import_worker = require("wiki-graph-core/worker");
1459
1482
  var import_wiki_graph_core7 = require("wiki-graph-core");
1460
1483
 
1484
+ // src/host.ts
1485
+ var import_platform2 = require("wiki-graph-core/platform");
1486
+ function prepareWikiGraphSDKHost(host) {
1487
+ if (host?.platform === void 0) {
1488
+ ensureNodeWikiGraphPlatform();
1489
+ return;
1490
+ }
1491
+ (0, import_platform2.installWikiGraphPlatform)(host.platform);
1492
+ }
1493
+ function assertWikiGraphSDKHostOptions(options) {
1494
+ if (options.host?.storage !== void 0 && options.stateDir !== void 0) {
1495
+ throw new TypeError(
1496
+ "Wiki Graph SDK host.storage and stateDir cannot be used together."
1497
+ );
1498
+ }
1499
+ }
1500
+ async function withWikiGraphSDKHost(options, operation) {
1501
+ assertWikiGraphSDKHostOptions(options);
1502
+ prepareWikiGraphSDKHost(options.host);
1503
+ if (options.host?.storage !== void 0) {
1504
+ return await (0, import_platform2.withWikiGraphStorage)(options.host.storage, operation);
1505
+ }
1506
+ return await withNodeWikiGraphStorage(options.stateDir, operation);
1507
+ }
1508
+
1461
1509
  // src/archive/target.ts
1462
1510
  var import_promises2 = require("fs/promises");
1463
1511
  var import_path3 = require("path");
@@ -1673,17 +1721,23 @@ async function writeWikiGraphArchiveLocation(location, operation, options = {})
1673
1721
  // src/worker.ts
1674
1722
  var import_worker2 = require("wiki-graph-core/worker");
1675
1723
  async function runBuildJobWorker(options) {
1676
- installNodeWikiGraphPlatform();
1677
- const { stateDir, ...coreOptions } = options;
1678
- await withNodeWikiGraphStorage(
1679
- stateDir,
1724
+ const { host, stateDir, ...coreOptions } = options;
1725
+ await withWikiGraphSDKHost(
1726
+ {
1727
+ ...host === void 0 ? {} : { host },
1728
+ ...stateDir === void 0 ? {} : { stateDir }
1729
+ },
1680
1730
  async () => await (0, import_worker.runBuildJobWorker)(coreOptions)
1681
1731
  );
1682
1732
  }
1683
1733
 
1684
1734
  // src/default-worker.ts
1685
1735
  async function runWikiGraphQueueWorker(options = {}) {
1686
- const config = await loadWikiGraphRuntimeConfig();
1736
+ const providers = getWikiGraphSDKProviders();
1737
+ const config = await loadWikiGraphRuntimeConfig({
1738
+ ...providers.embedding === void 0 ? {} : { skipEmbeddingConfig: true },
1739
+ ...providers.llm === void 0 ? {} : { skipLLMConfig: true }
1740
+ });
1687
1741
  await runBuildJobWorker({
1688
1742
  concurrency: config.concurrent?.job ?? DEFAULT_GENERATION_JOB_CONCURRENCY,
1689
1743
  executeJob: async (job, reporter, context) => {
@@ -1692,12 +1746,16 @@ async function runWikiGraphQueueWorker(options = {}) {
1692
1746
  async () => await executeBuildJob(job, reporter, context)
1693
1747
  );
1694
1748
  },
1749
+ ...options.idleTimeoutMs === void 0 ? {} : { idleTimeoutMs: options.idleTimeoutMs },
1695
1750
  ...options.signal === void 0 ? {} : { signal: options.signal }
1696
1751
  });
1697
1752
  }
1698
1753
  async function executeBuildJob(job, reporter, context) {
1754
+ const providers = getWikiGraphSDKProviders();
1699
1755
  const config = await loadWikiGraphRuntimeConfig({
1700
- ...job.llmJSON === void 0 ? {} : { llmJSON: job.llmJSON }
1756
+ ...job.llmJSON === void 0 ? {} : { llmJSON: job.llmJSON },
1757
+ ...providers.embedding === void 0 ? {} : { skipEmbeddingConfig: true },
1758
+ ...job.llmJSON === void 0 && providers.llm !== void 0 ? { skipLLMConfig: true } : {}
1701
1759
  });
1702
1760
  const execution = await openJobExecutor(
1703
1761
  job,
@@ -1832,12 +1890,17 @@ async function openJobExecutor(job, config, signal, reporter) {
1832
1890
  const stageConfig = generation ? await loadRequiredStageConfig({
1833
1891
  ...job.llmJSON === void 0 ? {} : { llmJSON: job.llmJSON }
1834
1892
  }) : void 0;
1893
+ const injectedLLM = job.llmJSON === void 0 ? getWikiGraphSDKProviders().llm : void 0;
1835
1894
  const llm = stageConfig === void 0 ? void 0 : createStageLLM(stageConfig, {
1836
1895
  cacheDirectory: job.cache,
1837
1896
  logDirectory: job.log,
1838
1897
  onTokenUsage: async (usage) => {
1839
1898
  await reporter.addTokenUsage(usage);
1840
- }
1899
+ },
1900
+ onStreamProgress: async ({ outputCharacters }) => {
1901
+ await reporter.addOutputCharacters(outputCharacters);
1902
+ },
1903
+ ...injectedLLM === void 0 ? {} : { provider: injectedLLM }
1841
1904
  });
1842
1905
  const promptSource = job.prompt ?? stageConfig?.prompt;
1843
1906
  const wikispine = job.target === "knowledge-graph" ? requireKnowledgeGraphWikispineConfig(config) : void 0;
@@ -1858,7 +1921,7 @@ async function openJobExecutor(job, config, signal, reporter) {
1858
1921
  "Local Wikimedia resolution requires an LLM."
1859
1922
  ),
1860
1923
  llmModelId: requireValue(
1861
- stageConfig?.llm?.model,
1924
+ stageConfig?.llm?.model ?? injectedLLM?.model,
1862
1925
  "Local Wikimedia resolution requires an LLM model."
1863
1926
  )
1864
1927
  } : {
@@ -1867,7 +1930,7 @@ async function openJobExecutor(job, config, signal, reporter) {
1867
1930
  token: config.wikimedia.token
1868
1931
  }
1869
1932
  );
1870
- const embeddingProvider = job.target === "index-embedding-source" || job.target === "index-embedding-summary" ? buildSearchIndexEmbeddingProvider(requireEmbeddingConfig(config)) : void 0;
1933
+ const embeddingProvider = job.target === "index-embedding-source" || job.target === "index-embedding-summary" ? getWikiGraphSDKProviders().embedding ?? buildSearchIndexEmbeddingProvider(requireEmbeddingConfig(config)) : void 0;
1871
1934
  return {
1872
1935
  async close() {
1873
1936
  await wikimediaResolver?.close();