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.
- package/README.md +22 -1
- package/dist/archives.cjs +494 -656
- package/dist/archives.cjs.map +1 -1
- package/dist/archives.d.ts +48 -80
- package/dist/archives.js +431 -585
- package/dist/archives.js.map +1 -1
- package/dist/conversions.cjs +183 -33
- package/dist/conversions.cjs.map +1 -1
- package/dist/conversions.d.ts +8 -1
- package/dist/conversions.js +188 -29
- package/dist/conversions.js.map +1 -1
- package/dist/default-worker.cjs +322 -62
- package/dist/default-worker.cjs.map +1 -1
- package/dist/default-worker.d.ts +1 -0
- package/dist/default-worker.js +336 -60
- package/dist/default-worker.js.map +1 -1
- package/dist/embedding.cjs +2 -1
- package/dist/embedding.cjs.map +1 -1
- package/dist/embedding.d.ts +4 -4
- package/dist/embedding.js +2 -1
- package/dist/embedding.js.map +1 -1
- package/dist/index.cjs +1295 -949
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +58 -24
- package/dist/index.js +1164 -805
- package/dist/index.js.map +1 -1
- package/dist/{jobs-Dd5PMSId.d.ts → jobs-CRK-DZ40.d.ts} +27 -14
- package/dist/local-config.cjs +2 -1
- package/dist/local-config.cjs.map +1 -1
- package/dist/local-config.js +2 -1
- package/dist/local-config.js.map +1 -1
- package/dist/maintenance.cjs +140 -30
- package/dist/maintenance.cjs.map +1 -1
- package/dist/maintenance.d.ts +5 -1
- package/dist/maintenance.js +145 -26
- package/dist/maintenance.js.map +1 -1
- package/dist/runtime-config.cjs +4 -3
- package/dist/runtime-config.cjs.map +1 -1
- package/dist/runtime-config.d.ts +4 -1
- package/dist/runtime-config.js +4 -3
- package/dist/runtime-config.js.map +1 -1
- package/dist/stage-BnWI3Rqt.d.ts +33 -0
- package/dist/stage.cjs +22 -12
- package/dist/stage.cjs.map +1 -1
- package/dist/stage.d.ts +2 -14
- package/dist/stage.js +22 -12
- package/dist/stage.js.map +1 -1
- package/dist/target-CZ7elwmj.d.ts +14 -0
- package/dist/wikispine.cjs +14 -10
- package/dist/wikispine.cjs.map +1 -1
- package/dist/wikispine.js +14 -10
- package/dist/wikispine.js.map +1 -1
- package/dist/worker.cjs +295 -25
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.ts +28 -5
- package/dist/worker.js +313 -28
- package/dist/worker.js.map +1 -1
- package/package.json +2 -2
package/dist/default-worker.cjs
CHANGED
|
@@ -35,7 +35,7 @@ __export(default_worker_exports, {
|
|
|
35
35
|
runWikiGraphQueueWorker: () => runWikiGraphQueueWorker
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(default_worker_exports);
|
|
38
|
-
var
|
|
38
|
+
var import_wiki_graph_core8 = require("wiki-graph-core");
|
|
39
39
|
var import_worker3 = require("wiki-graph-core/worker");
|
|
40
40
|
|
|
41
41
|
// src/embedding.ts
|
|
@@ -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);
|
|
@@ -303,6 +307,14 @@ function decodeNodeResourceIdentity(identity, kind) {
|
|
|
303
307
|
return void 0;
|
|
304
308
|
}
|
|
305
309
|
}
|
|
310
|
+
function getNodeResourcePath(resource) {
|
|
311
|
+
const brandedPath = getBrandedNodeResourcePath(resource, resource.kind);
|
|
312
|
+
if (brandedPath !== void 0) return brandedPath;
|
|
313
|
+
if (typeof resource === "object" && resource !== null && "path" in resource && typeof resource.path === "string") {
|
|
314
|
+
return resource.path;
|
|
315
|
+
}
|
|
316
|
+
throw new TypeError("Expected a Node File or Directory resource");
|
|
317
|
+
}
|
|
306
318
|
function assertChildName(name) {
|
|
307
319
|
if (name.length === 0 || name === "." || name === ".." || pathModule.isAbsolute(name) || name.includes("/") || name.includes("\\")) {
|
|
308
320
|
throw new TypeError(`Directory child must be a relative name: ${name}`);
|
|
@@ -314,42 +326,42 @@ var NodeDatabaseConnection = class {
|
|
|
314
326
|
}
|
|
315
327
|
database;
|
|
316
328
|
async close() {
|
|
317
|
-
await new Promise((
|
|
329
|
+
await new Promise((resolve4, reject) => {
|
|
318
330
|
this.database.close((error) => {
|
|
319
331
|
if (error) reject(error);
|
|
320
|
-
else
|
|
332
|
+
else resolve4();
|
|
321
333
|
});
|
|
322
334
|
});
|
|
323
335
|
}
|
|
324
336
|
async execute(sql) {
|
|
325
|
-
await new Promise((
|
|
337
|
+
await new Promise((resolve4, reject) => {
|
|
326
338
|
this.database.exec(sql, (error) => {
|
|
327
339
|
if (error) reject(error);
|
|
328
|
-
else
|
|
340
|
+
else resolve4();
|
|
329
341
|
});
|
|
330
342
|
});
|
|
331
343
|
}
|
|
332
344
|
async queryAll(sql, params = []) {
|
|
333
|
-
return await new Promise((
|
|
345
|
+
return await new Promise((resolve4, reject) => {
|
|
334
346
|
this.database.all(sql, [...params], (error, rows) => {
|
|
335
347
|
if (error) reject(error);
|
|
336
|
-
else
|
|
348
|
+
else resolve4(rows);
|
|
337
349
|
});
|
|
338
350
|
});
|
|
339
351
|
}
|
|
340
352
|
async queryOne(sql, params = []) {
|
|
341
|
-
return await new Promise((
|
|
353
|
+
return await new Promise((resolve4, reject) => {
|
|
342
354
|
this.database.get(sql, [...params], (error, row) => {
|
|
343
355
|
if (error) reject(error);
|
|
344
|
-
else
|
|
356
|
+
else resolve4(row);
|
|
345
357
|
});
|
|
346
358
|
});
|
|
347
359
|
}
|
|
348
360
|
async run(sql, params = []) {
|
|
349
|
-
await new Promise((
|
|
361
|
+
await new Promise((resolve4, reject) => {
|
|
350
362
|
this.database.run(sql, [...params], (error) => {
|
|
351
363
|
if (error) reject(error);
|
|
352
|
-
else
|
|
364
|
+
else resolve4();
|
|
353
365
|
});
|
|
354
366
|
});
|
|
355
367
|
}
|
|
@@ -363,10 +375,10 @@ async function openNativeNodeDatabase(file, flags) {
|
|
|
363
375
|
if (path === void 0) {
|
|
364
376
|
throw new TypeError("The Node database adapter requires a NodeFile");
|
|
365
377
|
}
|
|
366
|
-
return await new Promise((
|
|
378
|
+
return await new Promise((resolve4, reject) => {
|
|
367
379
|
const database = new nodeSqlite3.Database(path, flags, (error) => {
|
|
368
380
|
if (error) reject(error);
|
|
369
|
-
else
|
|
381
|
+
else resolve4(database);
|
|
370
382
|
});
|
|
371
383
|
});
|
|
372
384
|
}
|
|
@@ -375,19 +387,19 @@ async function openNodeZip(file) {
|
|
|
375
387
|
if (path === void 0) {
|
|
376
388
|
throw new TypeError("The Node ZIP adapter requires a NodeFile");
|
|
377
389
|
}
|
|
378
|
-
const zipFile = await new Promise((
|
|
390
|
+
const zipFile = await new Promise((resolve4, reject) => {
|
|
379
391
|
yauzl.open(
|
|
380
392
|
path,
|
|
381
393
|
{ autoClose: false, lazyEntries: true },
|
|
382
394
|
(error, opened) => {
|
|
383
395
|
if (error || opened === void 0)
|
|
384
396
|
reject(error ?? new Error("Cannot open ZIP"));
|
|
385
|
-
else
|
|
397
|
+
else resolve4(opened);
|
|
386
398
|
}
|
|
387
399
|
);
|
|
388
400
|
});
|
|
389
401
|
const entries = await new Promise(
|
|
390
|
-
(
|
|
402
|
+
(resolve4, reject) => {
|
|
391
403
|
const discovered = /* @__PURE__ */ new Map();
|
|
392
404
|
const rejectAndClose = (error) => {
|
|
393
405
|
zipFile.close();
|
|
@@ -397,7 +409,7 @@ async function openNodeZip(file) {
|
|
|
397
409
|
};
|
|
398
410
|
const resolveEntries = () => {
|
|
399
411
|
zipFile.off("error", rejectAndClose);
|
|
400
|
-
|
|
412
|
+
resolve4(discovered);
|
|
401
413
|
};
|
|
402
414
|
zipFile.on("entry", (entry) => {
|
|
403
415
|
if (!entry.fileName.endsWith("/")) {
|
|
@@ -507,12 +519,12 @@ async function collectDecodedNodeStreamRange(input, offset, length) {
|
|
|
507
519
|
return Buffer.concat(chunks, collected);
|
|
508
520
|
}
|
|
509
521
|
async function openNodeZipEntryStream(zipFile, entry, options) {
|
|
510
|
-
return await new Promise((
|
|
522
|
+
return await new Promise((resolve4, reject) => {
|
|
511
523
|
const callback = (error, opened) => {
|
|
512
524
|
if (error || opened === void 0) {
|
|
513
525
|
reject(error ?? new Error(`Cannot read ZIP entry: ${entry.fileName}`));
|
|
514
526
|
} else {
|
|
515
|
-
|
|
527
|
+
resolve4(opened);
|
|
516
528
|
}
|
|
517
529
|
};
|
|
518
530
|
if (options === void 0) zipFile.openReadStream(entry, callback);
|
|
@@ -643,11 +655,11 @@ async function writeNodeStream(input, writer) {
|
|
|
643
655
|
);
|
|
644
656
|
}
|
|
645
657
|
async function collectNodeStream(input) {
|
|
646
|
-
return await new Promise((
|
|
658
|
+
return await new Promise((resolve4, reject) => {
|
|
647
659
|
const chunks = [];
|
|
648
660
|
input.on("data", (chunk) => chunks.push(chunk));
|
|
649
661
|
input.once("error", reject);
|
|
650
|
-
input.once("end", () =>
|
|
662
|
+
input.once("end", () => resolve4(Buffer.concat(chunks)));
|
|
651
663
|
});
|
|
652
664
|
}
|
|
653
665
|
var nodeWikiGraphPlatform = {
|
|
@@ -1082,8 +1094,8 @@ var inlineLLMConfigSchema = import_zod.z.object({
|
|
|
1082
1094
|
});
|
|
1083
1095
|
async function loadWikiGraphRuntimeConfig(options) {
|
|
1084
1096
|
const [embedding, localLLM, concurrent, wikimedia, wikispine] = await Promise.all([
|
|
1085
|
-
readLocalConfigSection("embeddings"),
|
|
1086
|
-
readLocalConfigSection("llm"),
|
|
1097
|
+
options?.skipEmbeddingConfig === true ? Promise.resolve({}) : readLocalConfigSection("embeddings"),
|
|
1098
|
+
options?.skipLLMConfig === true ? Promise.resolve({}) : readLocalConfigSection("llm"),
|
|
1087
1099
|
readLocalConfigSection("concurrent"),
|
|
1088
1100
|
readLocalConfigSection("wikimedia"),
|
|
1089
1101
|
readLocalConfigSection("wikispine")
|
|
@@ -1358,7 +1370,10 @@ function createOpenAICompatibleName2(baseURL) {
|
|
|
1358
1370
|
|
|
1359
1371
|
// src/stage.ts
|
|
1360
1372
|
function createStageLLM(config, options) {
|
|
1361
|
-
const llmOptions = buildWikiGraphLLMOptions(config)
|
|
1373
|
+
const llmOptions = options?.provider === void 0 ? buildWikiGraphLLMOptions(config) : {
|
|
1374
|
+
...config.concurrent?.request === void 0 ? {} : { concurrent: config.concurrent.request },
|
|
1375
|
+
streamProvider: options.provider
|
|
1376
|
+
};
|
|
1362
1377
|
return new import_wiki_graph_core4.LLM({
|
|
1363
1378
|
sampling: (0, import_wiki_graph_core3.createDefaultWikiGraphSampling)({
|
|
1364
1379
|
...llmOptions.temperature === void 0 ? {} : { temperature: llmOptions.temperature },
|
|
@@ -1372,10 +1387,13 @@ function createStageLLM(config, options) {
|
|
|
1372
1387
|
});
|
|
1373
1388
|
}
|
|
1374
1389
|
async function loadRequiredStageConfig(options) {
|
|
1390
|
+
const injected = options.llmJSON === void 0 ? getWikiGraphSDKProviders().llm : void 0;
|
|
1375
1391
|
const config = await loadWikiGraphRuntimeConfig({
|
|
1376
|
-
...options.llmJSON === void 0 ? {} : { llmJSON: options.llmJSON }
|
|
1392
|
+
...options.llmJSON === void 0 ? {} : { llmJSON: options.llmJSON },
|
|
1393
|
+
...options.llmJSON === void 0 && injected !== void 0 ? { skipLLMConfig: true } : {},
|
|
1394
|
+
...getWikiGraphSDKProviders().embedding === void 0 ? {} : { skipEmbeddingConfig: true }
|
|
1377
1395
|
});
|
|
1378
|
-
if (config.llm?.provider === void 0 || config.llm.model === void 0) {
|
|
1396
|
+
if (injected === void 0 && (config.llm?.provider === void 0 || config.llm.model === void 0)) {
|
|
1379
1397
|
throw new Error(
|
|
1380
1398
|
"Missing LLM configuration. Set --llm for one run, or configure `wikg://local/config/llm` with provider and model."
|
|
1381
1399
|
);
|
|
@@ -1389,12 +1407,13 @@ var DEFAULT_GENERATION_JOB_CONCURRENCY = 3;
|
|
|
1389
1407
|
// src/wikispine.ts
|
|
1390
1408
|
var import_child_process = require("child_process");
|
|
1391
1409
|
var nodeWikispineCommandRunner = {
|
|
1392
|
-
run: async ({ args, command, input, onStdout, signal }) => await new Promise((
|
|
1410
|
+
run: async ({ args, command, input, onStdout, signal }) => await new Promise((resolve4, reject) => {
|
|
1393
1411
|
signal?.throwIfAborted();
|
|
1394
1412
|
const child = (0, import_child_process.spawn)(command, [...args], {
|
|
1395
1413
|
stdio: ["pipe", "pipe", "pipe"]
|
|
1396
1414
|
});
|
|
1397
1415
|
const stderr = [];
|
|
1416
|
+
let stdoutQueue = Promise.resolve();
|
|
1398
1417
|
let settled = false;
|
|
1399
1418
|
const settle = (operation) => {
|
|
1400
1419
|
if (settled) return;
|
|
@@ -1410,14 +1429,15 @@ var nodeWikispineCommandRunner = {
|
|
|
1410
1429
|
child.stdout.setEncoding("utf8");
|
|
1411
1430
|
child.stdout.on("data", (chunk) => {
|
|
1412
1431
|
if (settled) return;
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1432
|
+
child.stdout.pause();
|
|
1433
|
+
stdoutQueue = stdoutQueue.then(async () => await onStdout(chunk)).then(() => {
|
|
1434
|
+
child.stdout.resume();
|
|
1435
|
+
}).catch((error) => {
|
|
1416
1436
|
child.kill();
|
|
1417
1437
|
settle(
|
|
1418
1438
|
() => reject(error instanceof Error ? error : new Error(String(error)))
|
|
1419
1439
|
);
|
|
1420
|
-
}
|
|
1440
|
+
});
|
|
1421
1441
|
});
|
|
1422
1442
|
child.stderr.on("data", (chunk) => stderr.push(chunk));
|
|
1423
1443
|
child.on("error", (error) => {
|
|
@@ -1428,12 +1448,14 @@ var nodeWikispineCommandRunner = {
|
|
|
1428
1448
|
);
|
|
1429
1449
|
});
|
|
1430
1450
|
child.on("close", (exitCode) => {
|
|
1431
|
-
|
|
1432
|
-
(
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1451
|
+
void stdoutQueue.then(() => {
|
|
1452
|
+
settle(
|
|
1453
|
+
() => resolve4({
|
|
1454
|
+
exitCode,
|
|
1455
|
+
stderr: Buffer.concat(stderr).toString("utf8")
|
|
1456
|
+
})
|
|
1457
|
+
);
|
|
1458
|
+
});
|
|
1437
1459
|
});
|
|
1438
1460
|
child.stdin.end(input);
|
|
1439
1461
|
})
|
|
@@ -1444,7 +1466,221 @@ function abortError(signal) {
|
|
|
1444
1466
|
|
|
1445
1467
|
// src/worker.ts
|
|
1446
1468
|
var import_worker = require("wiki-graph-core/worker");
|
|
1469
|
+
var import_wiki_graph_core7 = require("wiki-graph-core");
|
|
1470
|
+
|
|
1471
|
+
// src/archive/target.ts
|
|
1472
|
+
var import_promises2 = require("fs/promises");
|
|
1473
|
+
var import_path3 = require("path");
|
|
1447
1474
|
var import_wiki_graph_core5 = require("wiki-graph-core");
|
|
1475
|
+
|
|
1476
|
+
// src/runtime-path.ts
|
|
1477
|
+
var import_os2 = require("os");
|
|
1478
|
+
var import_path2 = require("path");
|
|
1479
|
+
function resolveWikiGraphRuntimePath(path) {
|
|
1480
|
+
const context = getWikiGraphSDKRuntimeContext();
|
|
1481
|
+
const environmentHome = context.env.HOME?.trim();
|
|
1482
|
+
const home = environmentHome === void 0 || environmentHome === "" ? (0, import_os2.homedir)() : environmentHome;
|
|
1483
|
+
if (path === "~") return home;
|
|
1484
|
+
if (path.startsWith("~/")) return (0, import_path2.resolve)(home, path.slice(2));
|
|
1485
|
+
return (0, import_path2.isAbsolute)(path) ? path : (0, import_path2.resolve)(context.cwd, path);
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
// src/archive/target.ts
|
|
1489
|
+
var WikiGraphArchiveOwnershipError = class extends Error {
|
|
1490
|
+
code = "WIKI_GRAPH_ARCHIVE_OWNERSHIP_MISMATCH";
|
|
1491
|
+
constructor(message) {
|
|
1492
|
+
super(message);
|
|
1493
|
+
this.name = "WikiGraphArchiveOwnershipError";
|
|
1494
|
+
}
|
|
1495
|
+
};
|
|
1496
|
+
async function resolveWikiGraphArchiveLocation(target) {
|
|
1497
|
+
if (target.kind === "standalone") {
|
|
1498
|
+
return await resolveStandaloneLocation(target);
|
|
1499
|
+
}
|
|
1500
|
+
return await resolveLibraryLocation(target);
|
|
1501
|
+
}
|
|
1502
|
+
async function assertStandaloneWikiGraphArchivePath(inputPath) {
|
|
1503
|
+
const path = resolveWikiGraphRuntimePath(inputPath);
|
|
1504
|
+
const candidate = await canonicalPath(path);
|
|
1505
|
+
const candidateIdentity = await readFileIdentity(path);
|
|
1506
|
+
const libraries = await (0, import_wiki_graph_core5.listWikiGraphLibraries)();
|
|
1507
|
+
const matches = await findMatchingMembers(
|
|
1508
|
+
libraries,
|
|
1509
|
+
candidate,
|
|
1510
|
+
candidateIdentity
|
|
1511
|
+
);
|
|
1512
|
+
if (matches.length > 0) {
|
|
1513
|
+
const match = requireUnambiguousManagedMatch(path, matches);
|
|
1514
|
+
throw new WikiGraphArchiveOwnershipError(
|
|
1515
|
+
`Standalone archive path belongs to registered library ${(0, import_wiki_graph_core5.formatWikiGraphLibraryUri)(
|
|
1516
|
+
match.library.isDefault ? void 0 : match.library.publicId
|
|
1517
|
+
)}: ${path}. Use the library UUID URI ${match.archive.uri} instead.`
|
|
1518
|
+
);
|
|
1519
|
+
}
|
|
1520
|
+
for (const library of libraries) {
|
|
1521
|
+
const rootPath = getNodeResourcePath(library.folder);
|
|
1522
|
+
const root = await canonicalPath(rootPath);
|
|
1523
|
+
if (!isWithin(root, candidate)) continue;
|
|
1524
|
+
throw new WikiGraphArchiveOwnershipError(
|
|
1525
|
+
`Standalone archive path belongs to registered library ${(0, import_wiki_graph_core5.formatWikiGraphLibraryUri)(
|
|
1526
|
+
library.isDefault ? void 0 : library.publicId
|
|
1527
|
+
)}: ${path}. Scan the library, then address the archive by its library UUID URI.`
|
|
1528
|
+
);
|
|
1529
|
+
}
|
|
1530
|
+
return path;
|
|
1531
|
+
}
|
|
1532
|
+
async function resolveWikiGraphArchiveTarget(file) {
|
|
1533
|
+
const path = getNodeResourcePath(file);
|
|
1534
|
+
const candidate = await canonicalPath(path);
|
|
1535
|
+
const candidateIdentity = await readFileIdentity(path);
|
|
1536
|
+
const matches = await findMatchingMembers(
|
|
1537
|
+
await (0, import_wiki_graph_core5.listWikiGraphLibraries)(),
|
|
1538
|
+
candidate,
|
|
1539
|
+
candidateIdentity
|
|
1540
|
+
);
|
|
1541
|
+
if (matches.length > 0) {
|
|
1542
|
+
const match = requireUnambiguousManagedMatch(path, matches);
|
|
1543
|
+
return { kind: "library", uri: match.archive.uri };
|
|
1544
|
+
}
|
|
1545
|
+
return { kind: "standalone", path: getNodeResourcePath(file) };
|
|
1546
|
+
}
|
|
1547
|
+
async function resolveStandaloneLocation(target) {
|
|
1548
|
+
if (target.path.startsWith("wikg://lib/")) {
|
|
1549
|
+
throw new WikiGraphArchiveOwnershipError(
|
|
1550
|
+
`A library archive URI cannot be used as a standalone path: ${target.path}`
|
|
1551
|
+
);
|
|
1552
|
+
}
|
|
1553
|
+
const path = await assertStandaloneWikiGraphArchivePath(target.path);
|
|
1554
|
+
const normalized = {
|
|
1555
|
+
kind: "standalone",
|
|
1556
|
+
path,
|
|
1557
|
+
...target.objectUri === void 0 ? {} : { objectUri: target.objectUri }
|
|
1558
|
+
};
|
|
1559
|
+
return {
|
|
1560
|
+
archiveFile: new NodeFile(path),
|
|
1561
|
+
archiveKey: path,
|
|
1562
|
+
archivePath: path,
|
|
1563
|
+
indexScope: { archiveKey: path, archivePath: path, kind: "archive-index" },
|
|
1564
|
+
locatedUri: (0, import_wiki_graph_core5.formatLocatedWikiGraphUri)(path, target.objectUri),
|
|
1565
|
+
target: normalized
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
async function resolveLibraryLocation(target) {
|
|
1569
|
+
const parsedLocated = (0, import_wiki_graph_core5.parseLocatedWikiGraphUri)(target.uri);
|
|
1570
|
+
const archiveUri = parsedLocated.archivePath ?? target.uri;
|
|
1571
|
+
const parsed = (0, import_wiki_graph_core5.parseWikiGraphLibraryUri)(archiveUri);
|
|
1572
|
+
if (parsed?.kind !== "archive") {
|
|
1573
|
+
throw new WikiGraphArchiveOwnershipError(
|
|
1574
|
+
`Library archive target must be a UUID archive URI: ${target.uri}`
|
|
1575
|
+
);
|
|
1576
|
+
}
|
|
1577
|
+
await (0, import_wiki_graph_core5.resolveWikiGraphLibrary)(parsed);
|
|
1578
|
+
const archiveFile = await (0, import_wiki_graph_core5.resolveWikiGraphLibraryArchiveFile)(archiveUri);
|
|
1579
|
+
const normalized = {
|
|
1580
|
+
kind: "library",
|
|
1581
|
+
uri: target.uri
|
|
1582
|
+
};
|
|
1583
|
+
return {
|
|
1584
|
+
archiveFile,
|
|
1585
|
+
archiveKey: archiveUri,
|
|
1586
|
+
archivePath: archiveUri,
|
|
1587
|
+
indexScope: {
|
|
1588
|
+
archiveKey: archiveUri,
|
|
1589
|
+
archivePath: archiveUri,
|
|
1590
|
+
kind: "archive-index"
|
|
1591
|
+
},
|
|
1592
|
+
libraryArchiveTarget: parsed,
|
|
1593
|
+
locatedUri: target.uri,
|
|
1594
|
+
publicArchiveUri: archiveUri,
|
|
1595
|
+
target: normalized
|
|
1596
|
+
};
|
|
1597
|
+
}
|
|
1598
|
+
async function canonicalPath(path) {
|
|
1599
|
+
try {
|
|
1600
|
+
return await (0, import_promises2.realpath)(path);
|
|
1601
|
+
} catch {
|
|
1602
|
+
const absolute = (0, import_path3.resolve)(path);
|
|
1603
|
+
const parent = (0, import_path3.dirname)(absolute);
|
|
1604
|
+
return parent === absolute ? absolute : (0, import_path3.join)(await canonicalPath(parent), (0, import_path3.basename)(absolute));
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
function isWithin(root, candidate) {
|
|
1608
|
+
const child = (0, import_path3.relative)(root, candidate);
|
|
1609
|
+
return child === "" || !child.startsWith("..") && !(0, import_path3.isAbsolute)(child);
|
|
1610
|
+
}
|
|
1611
|
+
async function findMatchingMembers(libraries, candidate, candidateIdentity) {
|
|
1612
|
+
const members = [];
|
|
1613
|
+
for (const library of libraries) {
|
|
1614
|
+
const archives = await (0, import_wiki_graph_core5.listWikiGraphLibraryArchives)({
|
|
1615
|
+
isDefault: library.isDefault,
|
|
1616
|
+
kind: "scope",
|
|
1617
|
+
...library.isDefault ? {} : { publicId: library.publicId }
|
|
1618
|
+
});
|
|
1619
|
+
for (const archive of archives) {
|
|
1620
|
+
if (archive.file === void 0) continue;
|
|
1621
|
+
members.push({
|
|
1622
|
+
archive,
|
|
1623
|
+
library,
|
|
1624
|
+
memberPath: getNodeResourcePath(archive.file)
|
|
1625
|
+
});
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
const exact = [];
|
|
1629
|
+
for (const member of members) {
|
|
1630
|
+
if (await canonicalPath(member.memberPath) === candidate) {
|
|
1631
|
+
exact.push(member);
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
if (exact.length > 0 || candidateIdentity === void 0) return exact;
|
|
1635
|
+
const identityMatches = [];
|
|
1636
|
+
for (const member of members) {
|
|
1637
|
+
if (await readFileIdentity(member.memberPath) === candidateIdentity) {
|
|
1638
|
+
identityMatches.push(member);
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
return identityMatches;
|
|
1642
|
+
}
|
|
1643
|
+
function requireUnambiguousManagedMatch(path, matches) {
|
|
1644
|
+
if (matches.length === 1) return matches[0];
|
|
1645
|
+
throw new WikiGraphArchiveOwnershipError(
|
|
1646
|
+
`Archive file identity is ambiguous across managed archives: ${path}. Use an explicit library UUID URI (${matches.map((match) => match.archive.uri).join(", ")}).`
|
|
1647
|
+
);
|
|
1648
|
+
}
|
|
1649
|
+
async function readFileIdentity(path) {
|
|
1650
|
+
try {
|
|
1651
|
+
const value = await (0, import_promises2.stat)(path);
|
|
1652
|
+
return `${value.dev}:${value.ino}`;
|
|
1653
|
+
} catch {
|
|
1654
|
+
return void 0;
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
// src/archive/write.ts
|
|
1659
|
+
var import_wiki_graph_core6 = require("wiki-graph-core");
|
|
1660
|
+
async function writeWikiGraphArchiveLocation(location, operation, options = {}) {
|
|
1661
|
+
const cacheOnly = options.searchIndexWritebackPolicy === "cache";
|
|
1662
|
+
const write = async (archiveFile = location.archiveFile) => await new import_wiki_graph_core6.WikiGraphArchiveFile(archiveFile).write(operation, {
|
|
1663
|
+
...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
|
|
1664
|
+
...options.invalidateContinuationCursors === void 0 ? {} : {
|
|
1665
|
+
invalidateContinuationCursors: options.invalidateContinuationCursors
|
|
1666
|
+
},
|
|
1667
|
+
...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
|
|
1668
|
+
});
|
|
1669
|
+
if (location.libraryArchiveTarget === void 0 || cacheOnly || options.refreshLibraryIndex === false) {
|
|
1670
|
+
return await write();
|
|
1671
|
+
}
|
|
1672
|
+
return await (0, import_wiki_graph_core6.writeWikiGraphLibraryArchive)({
|
|
1673
|
+
additionalDerivedStateKeys: [location.archiveKey, location.archivePath],
|
|
1674
|
+
...options.invalidateContinuationCursors === void 0 ? {} : {
|
|
1675
|
+
invalidateContinuationCursors: options.invalidateContinuationCursors
|
|
1676
|
+
},
|
|
1677
|
+
...options.onIndexSyncError === void 0 ? {} : { onIndexSyncError: options.onIndexSyncError },
|
|
1678
|
+
operation: async (archiveFile) => await write(archiveFile),
|
|
1679
|
+
target: location.libraryArchiveTarget
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
// src/worker.ts
|
|
1448
1684
|
var import_worker2 = require("wiki-graph-core/worker");
|
|
1449
1685
|
async function runBuildJobWorker(options) {
|
|
1450
1686
|
installNodeWikiGraphPlatform();
|
|
@@ -1457,21 +1693,29 @@ async function runBuildJobWorker(options) {
|
|
|
1457
1693
|
|
|
1458
1694
|
// src/default-worker.ts
|
|
1459
1695
|
async function runWikiGraphQueueWorker(options = {}) {
|
|
1460
|
-
const
|
|
1696
|
+
const providers = getWikiGraphSDKProviders();
|
|
1697
|
+
const config = await loadWikiGraphRuntimeConfig({
|
|
1698
|
+
...providers.embedding === void 0 ? {} : { skipEmbeddingConfig: true },
|
|
1699
|
+
...providers.llm === void 0 ? {} : { skipLLMConfig: true }
|
|
1700
|
+
});
|
|
1461
1701
|
await runBuildJobWorker({
|
|
1462
1702
|
concurrency: config.concurrent?.job ?? DEFAULT_GENERATION_JOB_CONCURRENCY,
|
|
1463
1703
|
executeJob: async (job, reporter, context) => {
|
|
1464
|
-
await (0,
|
|
1704
|
+
await (0, import_wiki_graph_core8.withLoggingContext)(
|
|
1465
1705
|
{ logDirectory: job.log, operation: "build-job" },
|
|
1466
1706
|
async () => await executeBuildJob(job, reporter, context)
|
|
1467
1707
|
);
|
|
1468
1708
|
},
|
|
1709
|
+
...options.idleTimeoutMs === void 0 ? {} : { idleTimeoutMs: options.idleTimeoutMs },
|
|
1469
1710
|
...options.signal === void 0 ? {} : { signal: options.signal }
|
|
1470
1711
|
});
|
|
1471
1712
|
}
|
|
1472
1713
|
async function executeBuildJob(job, reporter, context) {
|
|
1714
|
+
const providers = getWikiGraphSDKProviders();
|
|
1473
1715
|
const config = await loadWikiGraphRuntimeConfig({
|
|
1474
|
-
...job.llmJSON === void 0 ? {} : { llmJSON: job.llmJSON }
|
|
1716
|
+
...job.llmJSON === void 0 ? {} : { llmJSON: job.llmJSON },
|
|
1717
|
+
...providers.embedding === void 0 ? {} : { skipEmbeddingConfig: true },
|
|
1718
|
+
...job.llmJSON === void 0 && providers.llm !== void 0 ? { skipLLMConfig: true } : {}
|
|
1475
1719
|
});
|
|
1476
1720
|
const execution = await openJobExecutor(
|
|
1477
1721
|
job,
|
|
@@ -1533,9 +1777,9 @@ async function executeStep(job, kind, execution, reporter, context, inputOptions
|
|
|
1533
1777
|
const stepWorkspace = await resetStepWorkspace(job.workspace, kind);
|
|
1534
1778
|
const inputFile = await stepWorkspace.createFile("input.jsonl");
|
|
1535
1779
|
const outputWorkspace = await stepWorkspace.createDirectory("output");
|
|
1536
|
-
const revision = await new
|
|
1780
|
+
const revision = await new import_wiki_graph_core8.WikiGraphArchiveFile(job.archive).readDocument(
|
|
1537
1781
|
async (document) => {
|
|
1538
|
-
await (0,
|
|
1782
|
+
await (0, import_wiki_graph_core8.getChapterDetails)(document, job.chapterId);
|
|
1539
1783
|
return await (0, import_worker3.writeChapterJobInputFile)(
|
|
1540
1784
|
document,
|
|
1541
1785
|
job.chapterId,
|
|
@@ -1545,7 +1789,7 @@ async function executeStep(job, kind, execution, reporter, context, inputOptions
|
|
|
1545
1789
|
);
|
|
1546
1790
|
}
|
|
1547
1791
|
);
|
|
1548
|
-
await (0,
|
|
1792
|
+
await (0, import_wiki_graph_core8.recordBuildJobInputRevision)({
|
|
1549
1793
|
currentRevision: revision,
|
|
1550
1794
|
jobId: job.jobId,
|
|
1551
1795
|
ownerId: requireRunningJobOwnerId(job)
|
|
@@ -1575,16 +1819,23 @@ async function executeStep(job, kind, execution, reporter, context, inputOptions
|
|
|
1575
1819
|
total: 1,
|
|
1576
1820
|
unit: "item"
|
|
1577
1821
|
});
|
|
1578
|
-
await
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1822
|
+
const target = await resolveWikiGraphArchiveTarget(job.archive);
|
|
1823
|
+
const resolvedLocation = await resolveWikiGraphArchiveLocation(target);
|
|
1824
|
+
const location = target.kind === "standalone" ? { ...resolvedLocation, archiveFile: job.archive } : resolvedLocation;
|
|
1825
|
+
await writeWikiGraphArchiveLocation(
|
|
1826
|
+
location,
|
|
1827
|
+
async (document) => {
|
|
1828
|
+
assertJobStillRunning(await (0, import_wiki_graph_core8.getBuildJob)(job.jobId));
|
|
1829
|
+
await (0, import_worker3.applyChapterJobArtifactFile)(
|
|
1830
|
+
document,
|
|
1831
|
+
job.chapterId,
|
|
1832
|
+
kind,
|
|
1833
|
+
revision,
|
|
1834
|
+
result.artifactFile
|
|
1835
|
+
);
|
|
1836
|
+
},
|
|
1837
|
+
{ invalidateContinuationCursors: true }
|
|
1838
|
+
);
|
|
1588
1839
|
await reporter.updatePhase({
|
|
1589
1840
|
done: 1,
|
|
1590
1841
|
phase: "committing",
|
|
@@ -1592,37 +1843,46 @@ async function executeStep(job, kind, execution, reporter, context, inputOptions
|
|
|
1592
1843
|
unit: "item"
|
|
1593
1844
|
});
|
|
1594
1845
|
await reporter.stepCompleted(kind);
|
|
1595
|
-
assertJobStillRunning(await (0,
|
|
1846
|
+
assertJobStillRunning(await (0, import_wiki_graph_core8.getBuildJob)(job.jobId));
|
|
1596
1847
|
}
|
|
1597
1848
|
async function openJobExecutor(job, config, signal, reporter) {
|
|
1598
1849
|
const generation = !job.target.startsWith("index-");
|
|
1599
1850
|
const stageConfig = generation ? await loadRequiredStageConfig({
|
|
1600
1851
|
...job.llmJSON === void 0 ? {} : { llmJSON: job.llmJSON }
|
|
1601
1852
|
}) : void 0;
|
|
1853
|
+
const injectedLLM = job.llmJSON === void 0 ? getWikiGraphSDKProviders().llm : void 0;
|
|
1602
1854
|
const llm = stageConfig === void 0 ? void 0 : createStageLLM(stageConfig, {
|
|
1603
1855
|
cacheDirectory: job.cache,
|
|
1604
1856
|
logDirectory: job.log,
|
|
1605
1857
|
onTokenUsage: async (usage) => {
|
|
1606
1858
|
await reporter.addTokenUsage(usage);
|
|
1607
|
-
}
|
|
1859
|
+
},
|
|
1860
|
+
onStreamProgress: async ({ outputCharacters }) => {
|
|
1861
|
+
await reporter.addOutputCharacters(outputCharacters);
|
|
1862
|
+
},
|
|
1863
|
+
...injectedLLM === void 0 ? {} : { provider: injectedLLM }
|
|
1608
1864
|
});
|
|
1609
1865
|
const promptSource = job.prompt ?? stageConfig?.prompt;
|
|
1610
1866
|
const wikispine = job.target === "knowledge-graph" ? requireKnowledgeGraphWikispineConfig(config) : void 0;
|
|
1611
1867
|
const request = llm === void 0 ? void 0 : async (messages, index, maxRetries) => await llm.request(messages, {
|
|
1612
1868
|
retryIndex: index,
|
|
1613
1869
|
retryMax: maxRetries,
|
|
1614
|
-
scope:
|
|
1870
|
+
scope: import_wiki_graph_core8.WikiGraphScope.ReaderExtraction,
|
|
1615
1871
|
signal
|
|
1616
1872
|
});
|
|
1617
1873
|
if (request !== void 0 && llm !== void 0) {
|
|
1618
1874
|
request.lazy = async (operation) => await llm.request(async () => await operation(request));
|
|
1619
1875
|
}
|
|
1620
|
-
const wikimediaResolver = job.target !== "knowledge-graph" ? void 0 : await (0,
|
|
1876
|
+
const wikimediaResolver = job.target !== "knowledge-graph" ? void 0 : await (0, import_wiki_graph_core8.openWikimediaResolver)(
|
|
1621
1877
|
config.wikimedia === void 0 ? {
|
|
1622
1878
|
kind: "local",
|
|
1623
1879
|
llmRequest: requireValue(
|
|
1624
1880
|
request,
|
|
1625
1881
|
"Local Wikimedia resolution requires an LLM."
|
|
1882
|
+
),
|
|
1883
|
+
llmModelId: requireValue(
|
|
1884
|
+
stageConfig?.llm?.model ?? injectedLLM?.model,
|
|
1885
|
+
"Local Wikimedia resolution requires an LLM model."
|
|
1626
1886
|
)
|
|
1627
1887
|
} : {
|
|
1628
1888
|
endpoint: config.wikimedia.endpoint,
|
|
@@ -1630,7 +1890,7 @@ async function openJobExecutor(job, config, signal, reporter) {
|
|
|
1630
1890
|
token: config.wikimedia.token
|
|
1631
1891
|
}
|
|
1632
1892
|
);
|
|
1633
|
-
const embeddingProvider = job.target === "index-embedding-source" || job.target === "index-embedding-summary" ? buildSearchIndexEmbeddingProvider(requireEmbeddingConfig(config)) : void 0;
|
|
1893
|
+
const embeddingProvider = job.target === "index-embedding-source" || job.target === "index-embedding-summary" ? getWikiGraphSDKProviders().embedding ?? buildSearchIndexEmbeddingProvider(requireEmbeddingConfig(config)) : void 0;
|
|
1634
1894
|
return {
|
|
1635
1895
|
async close() {
|
|
1636
1896
|
await wikimediaResolver?.close();
|
|
@@ -1660,12 +1920,12 @@ async function resetStepWorkspace(workspace, kind) {
|
|
|
1660
1920
|
return await workspace.createDirectory(name);
|
|
1661
1921
|
}
|
|
1662
1922
|
async function readChapterStage(job) {
|
|
1663
|
-
return await new
|
|
1664
|
-
async (document) => (await (0,
|
|
1923
|
+
return await new import_wiki_graph_core8.WikiGraphArchiveFile(job.archive).readDocument(
|
|
1924
|
+
async (document) => (await (0, import_wiki_graph_core8.getChapterDetails)(document, job.chapterId)).stage
|
|
1665
1925
|
);
|
|
1666
1926
|
}
|
|
1667
1927
|
async function assertCurrentFtsArtifact(job) {
|
|
1668
|
-
await new
|
|
1928
|
+
await new import_wiki_graph_core8.WikiGraphArchiveFile(job.archive).readDocument(async (document) => {
|
|
1669
1929
|
const revision = await document.serials.getRevision(job.chapterId);
|
|
1670
1930
|
const artifact = await document.indexArtifacts.get(job.chapterId, "fts");
|
|
1671
1931
|
if (artifact?.sourceRevision !== revision) {
|