@hraness/wordcell 0.21.3 → 0.22.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/README.md +170 -64
- package/dist/{authority-4sp9d552.js → authority-1pgftmwq.js} +55 -10
- package/dist/benchmark.js +3 -3
- package/dist/capture.js +6 -5
- package/dist/cli.js +37 -31
- package/dist/clip/acquire.js +3 -2
- package/dist/clip/cli.js +6 -5
- package/dist/clip/doctor.js +4 -3
- package/dist/clip/jobs.js +3 -3
- package/dist/clip/refresh.js +1 -1
- package/dist/evaluation-builder.js +18 -17
- package/dist/evaluation-kb.js +14 -13
- package/dist/graph-authority.js +3 -3
- package/dist/graph-percolation.js +5 -5
- package/dist/index-0dqfnrz9.js +1985 -0
- package/dist/{index-0k2x4nn9.js → index-1tm7bgx7.js} +16 -4
- package/dist/{index-vdw5yjfa.js → index-2hf27mws.js} +108 -18
- package/dist/index-3agn8scn.js +306 -0
- package/dist/{index-0rfcqab6.js → index-4ag64rc1.js} +555 -59
- package/dist/index-4knsp9qj.js +34 -0
- package/dist/index-66pshdtx.js +341 -0
- package/dist/index-9rf81m0p.js +368 -0
- package/dist/{index-59ssmqqm.js → index-a6kbv718.js} +1 -1
- package/dist/{index-51bap0mq.js → index-avbce90h.js} +9 -9
- package/dist/{index-9zcpdbjg.js → index-de2w8crk.js} +13 -13
- package/dist/{index-pbr6rv47.js → index-e6p9e926.js} +6 -6
- package/dist/{index-bcfn9xah.js → index-famy7fhs.js} +5 -5
- package/dist/{index-van48n3g.js → index-fc4dr114.js} +4 -2
- package/dist/{index-jzcws0zn.js → index-g5vsqmdy.js} +27 -57
- package/dist/{index-115b07ap.js → index-hcw140eb.js} +5 -5
- package/dist/index-j70m75wd.js +217 -0
- package/dist/{index-3ehtt59c.js → index-qwgsmtsz.js} +1 -1
- package/dist/{index-bgfzwt4h.js → index-t2bs9xdr.js} +4 -4
- package/dist/index.js +285 -93
- package/dist/portfolio.js +14 -13
- package/dist/publish-model.js +36 -0
- package/dist/publish-reader/reader.css +1042 -0
- package/dist/publish-reader/reader.js +1534 -0
- package/dist/publish-reader/theme.js +92 -0
- package/dist/publish-search.js +52 -0
- package/dist/publish.js +36 -0
- package/dist/rerank-typesafe.js +13 -0
- package/dist/rerank.js +14 -0
- package/dist/sdk.js +14 -11
- package/dist/search.js +2 -2
- package/dist/semantic.js +4 -4
- package/dist/{support-vm4mkw0k.js → support-mywv1em0.js} +38 -32
- package/dist/workflows/decision-context.js +13 -12
- package/dist/workflows/index.js +13 -12
- package/package.json +63 -5
- package/skills/wordcell/AGENTS.md +1 -0
- package/skills/wordcell/SKILL.md +2 -1
- package/skills/wordcell/agents/openai.yaml +1 -1
- package/skills/wordcell/references/publish.md +93 -0
- package/skills/wordcell/references/query.md +22 -0
- package/src/cli-program.ts +347 -9
- package/src/index.ts +4 -0
- package/src/oh/authority.ts +14 -5
- package/src/oh/canonical-rust.ts +127 -0
- package/src/oh/projection-rust.ts +63 -0
- package/src/oh-adoption.ts +7 -1
- package/src/publish-graph.ts +0 -0
- package/src/publish-index.ts +195 -0
- package/src/publish-markdown.ts +617 -0
- package/src/publish-model.ts +599 -0
- package/src/publish-nav.ts +204 -0
- package/src/publish-pages.ts +382 -0
- package/src/publish-reader/reader.css +1042 -0
- package/src/publish-reader/reader.ts +1017 -0
- package/src/publish-reader/theme.ts +97 -0
- package/src/publish-search.ts +418 -0
- package/src/publish-select.ts +419 -0
- package/src/publish-theme.ts +106 -0
- package/src/publish.ts +904 -0
- package/src/rerank-credentials.ts +56 -0
- package/src/rerank-typesafe.ts +472 -0
- package/src/rerank.ts +389 -0
- package/src/sdk.ts +157 -6
- package/src/serve.ts +180 -0
- package/src/vault.ts +23 -9
- package/dist/{index-why54q5p.js → index-gm9t95d9.js} +3 -3
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
import {
|
|
3
3
|
classifyPlatformUrl
|
|
4
4
|
} from "./index-hgve9rh2.js";
|
|
5
|
+
import {
|
|
6
|
+
filterCookieProviderResult,
|
|
7
|
+
readCookieFile,
|
|
8
|
+
renderCookieHeader
|
|
9
|
+
} from "./index-2gv8y733.js";
|
|
5
10
|
import {
|
|
6
11
|
startNetworkProxy
|
|
7
12
|
} from "./index-w2zc0vwa.js";
|
|
@@ -20,63 +25,28 @@ import {
|
|
|
20
25
|
sanitizeArtifactUrl
|
|
21
26
|
} from "./index-mxxxytys.js";
|
|
22
27
|
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} from "./index-2gv8y733.js";
|
|
28
|
+
findKbPackageRoot,
|
|
29
|
+
resolvePackageDirectory
|
|
30
|
+
} from "./index-4knsp9qj.js";
|
|
27
31
|
|
|
28
32
|
// src/clip/acquire.ts
|
|
29
33
|
import {
|
|
30
34
|
chmodSync,
|
|
31
|
-
existsSync
|
|
35
|
+
existsSync,
|
|
32
36
|
lstatSync,
|
|
33
37
|
mkdtempSync,
|
|
34
|
-
readFileSync
|
|
38
|
+
readFileSync,
|
|
35
39
|
realpathSync,
|
|
36
40
|
rmSync,
|
|
37
41
|
statSync,
|
|
38
42
|
writeFileSync
|
|
39
43
|
} from "fs";
|
|
40
44
|
import { homedir, tmpdir } from "os";
|
|
41
|
-
import { basename, dirname
|
|
45
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "path";
|
|
42
46
|
import { getCookies } from "@steipete/sweet-cookie";
|
|
43
|
-
|
|
44
|
-
// src/clip/package-root.ts
|
|
45
|
-
import { existsSync, readFileSync } from "fs";
|
|
46
|
-
import { createRequire } from "module";
|
|
47
|
-
import { dirname, join, resolve } from "path";
|
|
48
|
-
function isPackageManifest(value) {
|
|
49
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
50
|
-
}
|
|
51
|
-
function findKbPackageRoot(startDirectory = import.meta.dir, dependencies = {}) {
|
|
52
|
-
const exists = dependencies.exists ?? existsSync;
|
|
53
|
-
const readText = dependencies.readText ?? ((path) => readFileSync(path, "utf8"));
|
|
54
|
-
let directory = resolve(startDirectory);
|
|
55
|
-
for (let depth = 0;depth < 8; depth += 1) {
|
|
56
|
-
const manifestPath = join(directory, "package.json");
|
|
57
|
-
if (exists(manifestPath)) {
|
|
58
|
-
try {
|
|
59
|
-
const parsed = JSON.parse(readText(manifestPath));
|
|
60
|
-
if (isPackageManifest(parsed) && typeof parsed.name === "string" && parsed.name === "@hraness/wordcell" && typeof parsed.version === "string")
|
|
61
|
-
return directory;
|
|
62
|
-
} catch {}
|
|
63
|
-
}
|
|
64
|
-
const parent = dirname(directory);
|
|
65
|
-
if (parent === directory)
|
|
66
|
-
break;
|
|
67
|
-
directory = parent;
|
|
68
|
-
}
|
|
69
|
-
throw new Error("Could not locate the wordcell package root.");
|
|
70
|
-
}
|
|
71
|
-
function resolvePackageDirectory(packageName, parentUrl = import.meta.url) {
|
|
72
|
-
const manifest = createRequire(parentUrl).resolve(`${packageName}/package.json`);
|
|
73
|
-
return dirname(manifest);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// src/clip/acquire.ts
|
|
77
|
-
var agentBrowserBinDirectory = join2(resolvePackageDirectory("agent-browser"), "bin");
|
|
47
|
+
var agentBrowserBinDirectory = join(resolvePackageDirectory("agent-browser"), "bin");
|
|
78
48
|
function agentBrowserCommand() {
|
|
79
|
-
return [process.execPath,
|
|
49
|
+
return [process.execPath, join(agentBrowserBinDirectory, "agent-browser.js")];
|
|
80
50
|
}
|
|
81
51
|
var inheritedProxyKeys = new Set([
|
|
82
52
|
"ALL_PROXY",
|
|
@@ -99,9 +69,9 @@ function isolatedAgentBrowserEnvironment(source, socketDirectory) {
|
|
|
99
69
|
return environment;
|
|
100
70
|
}
|
|
101
71
|
function createAgentBrowserIsolation(directory) {
|
|
102
|
-
const configPath =
|
|
72
|
+
const configPath = join(directory, "agent-browser.config.json");
|
|
103
73
|
const socketRoot = process.platform === "win32" ? tmpdir() : "/tmp";
|
|
104
|
-
const socketDirectory = mkdtempSync(
|
|
74
|
+
const socketDirectory = mkdtempSync(join(socketRoot, "jc-ab-"));
|
|
105
75
|
try {
|
|
106
76
|
chmodSync(socketDirectory, 448);
|
|
107
77
|
writeFileSync(configPath, `{}
|
|
@@ -209,7 +179,7 @@ async function runAgentBrowserBatch(globalArgs, commands, options) {
|
|
|
209
179
|
throw new Error("agent-browser batch failed");
|
|
210
180
|
}
|
|
211
181
|
async function discoverChromeProfiles(timeoutMs = 15000) {
|
|
212
|
-
const directory = mkdtempSync(
|
|
182
|
+
const directory = mkdtempSync(join(tmpdir(), "hraness-wordcell-profiles-"));
|
|
213
183
|
chmodSync(directory, 448);
|
|
214
184
|
let socketDirectory = null;
|
|
215
185
|
try {
|
|
@@ -501,10 +471,10 @@ function browserNavigationReachedTarget(target, before, after, navigationCommand
|
|
|
501
471
|
return navigationCommandSucceeded && before !== null && navigationIdentity(before) !== afterIdentity;
|
|
502
472
|
}
|
|
503
473
|
async function terminateAgentBrowserSession(session, socketDirectory) {
|
|
504
|
-
const pidPath =
|
|
505
|
-
if (!
|
|
474
|
+
const pidPath = join(socketDirectory, `${session}.pid`);
|
|
475
|
+
if (!existsSync(pidPath))
|
|
506
476
|
return;
|
|
507
|
-
const rawPid =
|
|
477
|
+
const rawPid = readFileSync(pidPath, "utf8").trim();
|
|
508
478
|
if (!/^\d+$/.test(rawPid))
|
|
509
479
|
return;
|
|
510
480
|
const pid = Number(rawPid);
|
|
@@ -529,7 +499,7 @@ function pathInside(root, target) {
|
|
|
529
499
|
}
|
|
530
500
|
function canonicalPotentialPath(value, label) {
|
|
531
501
|
const suffix = [];
|
|
532
|
-
let ancestor =
|
|
502
|
+
let ancestor = resolve(value);
|
|
533
503
|
while (true) {
|
|
534
504
|
try {
|
|
535
505
|
lstatSync(ancestor);
|
|
@@ -539,12 +509,12 @@ function canonicalPotentialPath(value, label) {
|
|
|
539
509
|
} catch {
|
|
540
510
|
throw new Error(`${label} contains an unresolved symbolic link.`);
|
|
541
511
|
}
|
|
542
|
-
return
|
|
512
|
+
return resolve(canonicalAncestor, ...suffix);
|
|
543
513
|
} catch (error) {
|
|
544
514
|
if (error.code !== "ENOENT")
|
|
545
515
|
throw error;
|
|
546
516
|
}
|
|
547
|
-
const parent =
|
|
517
|
+
const parent = dirname(ancestor);
|
|
548
518
|
if (parent === ancestor)
|
|
549
519
|
throw new Error(`${label} has no resolvable filesystem ancestor.`);
|
|
550
520
|
suffix.unshift(basename(ancestor));
|
|
@@ -555,7 +525,7 @@ function profilePath(value) {
|
|
|
555
525
|
const pathLike = isAbsolute(value) || value.startsWith(`.${sep}`) || value.startsWith(`..${sep}`) || value.startsWith(`~${sep}`) || value.includes("/") || value.includes("\\");
|
|
556
526
|
if (!pathLike)
|
|
557
527
|
return null;
|
|
558
|
-
const expanded = value.startsWith(`~${sep}`) ?
|
|
528
|
+
const expanded = value.startsWith(`~${sep}`) ? join(homedir(), value.slice(2)) : resolve(value);
|
|
559
529
|
return canonicalPotentialPath(expanded, "Persistent browser profile");
|
|
560
530
|
}
|
|
561
531
|
function assertSafePersistentProfile(options) {
|
|
@@ -828,7 +798,7 @@ async function acquireBrowser(options, temporaryDirectory, useDiscoveredProfile
|
|
|
828
798
|
warnings.push("Rendered text was truncated at its configured output boundary.");
|
|
829
799
|
let screenshotPath;
|
|
830
800
|
if ((options.evidence === "screenshot" || options.evidence === "all") && browserPageProvenanceIntact) {
|
|
831
|
-
const requestedScreenshotPath =
|
|
801
|
+
const requestedScreenshotPath = join(temporaryDirectory, "page.png");
|
|
832
802
|
screenshotPath = requestedScreenshotPath;
|
|
833
803
|
try {
|
|
834
804
|
await runBrowser(globalArgs, ["screenshot", requestedScreenshotPath], {
|
|
@@ -841,7 +811,7 @@ async function acquireBrowser(options, temporaryDirectory, useDiscoveredProfile
|
|
|
841
811
|
rmSync(requestedScreenshotPath, { force: true });
|
|
842
812
|
warnings.push("Browser screenshot changed pages during capture and was discarded.");
|
|
843
813
|
screenshotPath = undefined;
|
|
844
|
-
} else if (!
|
|
814
|
+
} else if (!existsSync(requestedScreenshotPath)) {
|
|
845
815
|
warnings.push("Browser screenshot was requested but agent-browser did not create it.");
|
|
846
816
|
screenshotPath = undefined;
|
|
847
817
|
}
|
|
@@ -1028,7 +998,7 @@ async function acquireFile(options) {
|
|
|
1028
998
|
if (stats.size > options.maxHtmlBytes) {
|
|
1029
999
|
throw new Error(`HTML input is ${stats.size} bytes; limit is ${options.maxHtmlBytes}`);
|
|
1030
1000
|
}
|
|
1031
|
-
return
|
|
1001
|
+
return readFileSync(options.htmlFile, "utf8");
|
|
1032
1002
|
})();
|
|
1033
1003
|
return {
|
|
1034
1004
|
body,
|
|
@@ -1039,4 +1009,4 @@ async function acquireFile(options) {
|
|
|
1039
1009
|
};
|
|
1040
1010
|
}
|
|
1041
1011
|
|
|
1042
|
-
export {
|
|
1012
|
+
export { agentBrowserCommand, isolatedAgentBrowserEnvironment, discoverChromeProfiles, mergeRenderedTextSnapshots, browserExpansionLimits, browserExpansionScript, readBrowserExpansionTelemetry, browserExpansionWarnings, browserExpansionStayedOnPage, browserNavigationReachedTarget, assertSafePersistentProfile, browserCookieCommands, seedOwnedBrowserCookies, browserProxyArguments, acquireBrowser, acquireHttp, acquireCookieHttp, createCookieRecordReader, createCookieHeaderReader, acquireCookieRecords, acquireCookieHeader, acquireFile };
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
import {
|
|
3
|
+
fuseRankedCandidates,
|
|
4
|
+
validateSearchQuery
|
|
5
|
+
} from "./index-gm9t95d9.js";
|
|
2
6
|
import {
|
|
3
7
|
MAX_NOTE_UTF8_BYTES,
|
|
4
8
|
MAX_SCANNED_NOTES,
|
|
5
9
|
MAX_VAULT_UTF8_BYTES,
|
|
6
10
|
VaultScanBudgetError,
|
|
7
11
|
scanVault
|
|
8
|
-
} from "./index-
|
|
9
|
-
import {
|
|
10
|
-
fuseRankedCandidates,
|
|
11
|
-
validateSearchQuery
|
|
12
|
-
} from "./index-why54q5p.js";
|
|
12
|
+
} from "./index-1tm7bgx7.js";
|
|
13
13
|
|
|
14
14
|
// src/semantic.ts
|
|
15
15
|
import { createHash as createHash2 } from "crypto";
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/rerank.ts
|
|
3
|
+
var MAX_RERANK_CANDIDATES = 25;
|
|
4
|
+
var MAX_RERANK_SNIPPET_BYTES = 512;
|
|
5
|
+
var MAX_RERANK_STATE_BYTES = 24 * 1024;
|
|
6
|
+
var MALFORMED_RERANK_RESULT_MESSAGE = "Rerank engine returned a malformed result.";
|
|
7
|
+
var MAX_RERANK_DIAGNOSTIC_BYTES = 512;
|
|
8
|
+
var MAX_RERANK_MODEL_BYTES = 128;
|
|
9
|
+
var MAX_RERANK_TOKEN_COUNT = 1e9;
|
|
10
|
+
function dataRecord(value) {
|
|
11
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
12
|
+
return null;
|
|
13
|
+
const prototype = Object.getPrototypeOf(value);
|
|
14
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
15
|
+
return null;
|
|
16
|
+
const keys = Reflect.ownKeys(value);
|
|
17
|
+
if (keys.length > MAX_RERANK_CANDIDATES)
|
|
18
|
+
return null;
|
|
19
|
+
const output = Object.create(null);
|
|
20
|
+
for (const key of keys) {
|
|
21
|
+
if (typeof key !== "string")
|
|
22
|
+
return null;
|
|
23
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
24
|
+
if (descriptor === undefined || !("value" in descriptor) || !descriptor.enumerable)
|
|
25
|
+
return null;
|
|
26
|
+
output[key] = descriptor.value;
|
|
27
|
+
}
|
|
28
|
+
return output;
|
|
29
|
+
}
|
|
30
|
+
function hasOnlyKeys(value, allowed) {
|
|
31
|
+
return Object.keys(value).every((key) => allowed.includes(key));
|
|
32
|
+
}
|
|
33
|
+
function boundedDiagnostic(value) {
|
|
34
|
+
return typeof value === "string" && value !== "" && Buffer.byteLength(value, "utf8") <= MAX_RERANK_DIAGNOSTIC_BYTES && !/[\u0000-\u001f\u007f]/u.test(value);
|
|
35
|
+
}
|
|
36
|
+
function tokenCount(value) {
|
|
37
|
+
return Number.isSafeInteger(value) && value >= 0 && value <= MAX_RERANK_TOKEN_COUNT;
|
|
38
|
+
}
|
|
39
|
+
function inspectRerankDetails(result) {
|
|
40
|
+
const model = result["model"];
|
|
41
|
+
if (model !== undefined && (typeof model !== "string" || !/^[\x20-\x7e]+$/u.test(model) || Buffer.byteLength(model, "utf8") > MAX_RERANK_MODEL_BYTES)) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const rawUsage = result["usage"];
|
|
45
|
+
let usage;
|
|
46
|
+
if (rawUsage !== undefined) {
|
|
47
|
+
const usageRecord = dataRecord(rawUsage);
|
|
48
|
+
if (usageRecord === null || !hasOnlyKeys(usageRecord, ["inputTokens", "outputTokens"]) || usageRecord["inputTokens"] !== undefined && !tokenCount(usageRecord["inputTokens"]) || usageRecord["outputTokens"] !== undefined && !tokenCount(usageRecord["outputTokens"])) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
usage = {
|
|
52
|
+
...usageRecord["inputTokens"] === undefined ? {} : { inputTokens: usageRecord["inputTokens"] },
|
|
53
|
+
...usageRecord["outputTokens"] === undefined ? {} : { outputTokens: usageRecord["outputTokens"] }
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const rawAccounting = result["accounting"];
|
|
57
|
+
let accounting;
|
|
58
|
+
if (rawAccounting !== undefined) {
|
|
59
|
+
const record = dataRecord(rawAccounting);
|
|
60
|
+
if (record === null || !hasOnlyKeys(record, ["candidates", "attempted", "completed", "elapsedMs", "usageComplete"]))
|
|
61
|
+
return null;
|
|
62
|
+
const { candidates, attempted, completed, elapsedMs, usageComplete } = record;
|
|
63
|
+
if (!Number.isSafeInteger(candidates) || candidates < 0 || candidates > MAX_RERANK_CANDIDATES || !Number.isSafeInteger(attempted) || attempted < 0 || attempted > candidates || !Number.isSafeInteger(completed) || completed < 0 || completed > attempted || typeof elapsedMs !== "number" || !Number.isFinite(elapsedMs) || elapsedMs < 0 || elapsedMs > 86400000 || typeof usageComplete !== "boolean" || usageComplete && (completed !== attempted || attempted > 0 && (usage?.inputTokens === undefined || usage.outputTokens === undefined)))
|
|
64
|
+
return null;
|
|
65
|
+
accounting = Object.freeze({
|
|
66
|
+
candidates,
|
|
67
|
+
attempted,
|
|
68
|
+
completed,
|
|
69
|
+
elapsedMs,
|
|
70
|
+
usageComplete
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
...model === undefined ? {} : { model },
|
|
75
|
+
...usage === undefined ? {} : { usage: Object.freeze(usage) },
|
|
76
|
+
...accounting === undefined ? {} : { accounting }
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function inspectRerankResult(value) {
|
|
80
|
+
let details = {};
|
|
81
|
+
const malformed = () => ({
|
|
82
|
+
status: "failed",
|
|
83
|
+
message: MALFORMED_RERANK_RESULT_MESSAGE,
|
|
84
|
+
...details
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
const result = dataRecord(value);
|
|
88
|
+
if (result === null || typeof result["status"] !== "string") {
|
|
89
|
+
return malformed();
|
|
90
|
+
}
|
|
91
|
+
const inspectedDetails = inspectRerankDetails(result);
|
|
92
|
+
if (inspectedDetails === null)
|
|
93
|
+
return malformed();
|
|
94
|
+
details = inspectedDetails;
|
|
95
|
+
if (result["status"] === "failed" || result["status"] === "unavailable") {
|
|
96
|
+
if (!hasOnlyKeys(result, ["status", "message", "model", "usage", "accounting"]) || !boundedDiagnostic(result["message"])) {
|
|
97
|
+
return malformed();
|
|
98
|
+
}
|
|
99
|
+
return { status: result["status"], message: result["message"], ...details };
|
|
100
|
+
}
|
|
101
|
+
if (result["status"] !== "ready" || !hasOnlyKeys(result, ["status", "ordering", "probabilities", "confidence", "model", "usage", "accounting"])) {
|
|
102
|
+
return malformed();
|
|
103
|
+
}
|
|
104
|
+
if (!Array.isArray(result["ordering"]) || result["ordering"].length > MAX_RERANK_CANDIDATES) {
|
|
105
|
+
return malformed();
|
|
106
|
+
}
|
|
107
|
+
const ordering = [];
|
|
108
|
+
const rawOrdering = result["ordering"];
|
|
109
|
+
for (let index = 0;index < rawOrdering.length; index += 1) {
|
|
110
|
+
const descriptor = Object.getOwnPropertyDescriptor(rawOrdering, String(index));
|
|
111
|
+
const id = descriptor !== undefined && "value" in descriptor ? descriptor.value : undefined;
|
|
112
|
+
if (typeof id !== "string" || id === "" || Buffer.byteLength(id, "utf8") > MAX_RERANK_STATE_BYTES) {
|
|
113
|
+
return malformed();
|
|
114
|
+
}
|
|
115
|
+
ordering.push(id);
|
|
116
|
+
}
|
|
117
|
+
const rawProbabilities = dataRecord(result["probabilities"]);
|
|
118
|
+
if (rawProbabilities === null) {
|
|
119
|
+
return malformed();
|
|
120
|
+
}
|
|
121
|
+
const probabilities = Object.create(null);
|
|
122
|
+
for (const [id, probability] of Object.entries(rawProbabilities)) {
|
|
123
|
+
if (typeof probability !== "number" || !Number.isFinite(probability) || probability < 0 || probability > 1) {
|
|
124
|
+
return malformed();
|
|
125
|
+
}
|
|
126
|
+
probabilities[id] = probability;
|
|
127
|
+
}
|
|
128
|
+
const confidence = result["confidence"];
|
|
129
|
+
if (confidence !== undefined && (typeof confidence !== "number" || !Number.isFinite(confidence) || confidence < 0 || confidence > 1)) {
|
|
130
|
+
return malformed();
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
status: "ready",
|
|
134
|
+
ordering: Object.freeze(ordering),
|
|
135
|
+
probabilities,
|
|
136
|
+
...confidence === undefined ? {} : { confidence },
|
|
137
|
+
...details
|
|
138
|
+
};
|
|
139
|
+
} catch {
|
|
140
|
+
return malformed();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function failedRerank(hits, message, details) {
|
|
144
|
+
const result = {
|
|
145
|
+
status: "failed",
|
|
146
|
+
message,
|
|
147
|
+
...details.model === undefined ? {} : { model: details.model },
|
|
148
|
+
...details.usage === undefined ? {} : { usage: details.usage },
|
|
149
|
+
...details.accounting === undefined ? {} : { accounting: details.accounting }
|
|
150
|
+
};
|
|
151
|
+
return { status: "failed", hits, placements: new Map, result, message };
|
|
152
|
+
}
|
|
153
|
+
function applyRerank(hits, result, expectedWindowIds) {
|
|
154
|
+
const inspected = inspectRerankResult(result);
|
|
155
|
+
if (inspected.status !== "ready") {
|
|
156
|
+
return {
|
|
157
|
+
status: inspected.status,
|
|
158
|
+
hits,
|
|
159
|
+
placements: new Map,
|
|
160
|
+
result: inspected,
|
|
161
|
+
message: inspected.message
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
const ordering = inspected.ordering;
|
|
165
|
+
if (expectedWindowIds !== undefined && (ordering.length !== expectedWindowIds.length || expectedWindowIds.some((id) => !ordering.includes(id)))) {
|
|
166
|
+
return failedRerank(hits, MALFORMED_RERANK_RESULT_MESSAGE, inspected);
|
|
167
|
+
}
|
|
168
|
+
const windowIds = new Set;
|
|
169
|
+
for (const id of ordering) {
|
|
170
|
+
if (windowIds.has(id)) {
|
|
171
|
+
return failedRerank(hits, "Rerank ordering contained a duplicate candidate id.", inspected);
|
|
172
|
+
}
|
|
173
|
+
windowIds.add(id);
|
|
174
|
+
}
|
|
175
|
+
const window = hits.slice(0, ordering.length);
|
|
176
|
+
const windowById = new Map(window.map((hit) => [hit.id, hit]));
|
|
177
|
+
for (const id of ordering) {
|
|
178
|
+
if (!windowById.has(id)) {
|
|
179
|
+
return failedRerank(hits, "Rerank ordering contained an unknown candidate id.", inspected);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (const hit of window) {
|
|
183
|
+
if (!windowIds.has(hit.id)) {
|
|
184
|
+
return failedRerank(hits, "Rerank ordering omitted a window candidate id.", inspected);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const probabilityKeys = Object.keys(inspected.probabilities);
|
|
188
|
+
for (const id of probabilityKeys) {
|
|
189
|
+
if (!windowIds.has(id)) {
|
|
190
|
+
return failedRerank(hits, "Rerank probabilities contained an unknown candidate id.", inspected);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (const id of ordering) {
|
|
194
|
+
if (inspected.probabilities[id] === undefined) {
|
|
195
|
+
return failedRerank(hits, "Rerank probability for a candidate id was missing or invalid.", inspected);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const probabilityOf = (id) => inspected.probabilities[id];
|
|
199
|
+
const sorted = window.toSorted((left, right) => Number(right.identity === true) - Number(left.identity === true) || probabilityOf(right.id) - probabilityOf(left.id) || left.rank - right.rank || left.id.localeCompare(right.id));
|
|
200
|
+
const placements = new Map;
|
|
201
|
+
const reordered = sorted.map((hit, index) => {
|
|
202
|
+
placements.set(hit.id, {
|
|
203
|
+
baselineRank: hit.rank,
|
|
204
|
+
rerankRank: index + 1,
|
|
205
|
+
probability: probabilityOf(hit.id)
|
|
206
|
+
});
|
|
207
|
+
return hit.rank === index + 1 ? hit : { ...hit, rank: index + 1 };
|
|
208
|
+
});
|
|
209
|
+
return {
|
|
210
|
+
status: "ready",
|
|
211
|
+
hits: Object.freeze([...reordered, ...hits.slice(ordering.length)]),
|
|
212
|
+
placements,
|
|
213
|
+
result: inspected
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export { MAX_RERANK_CANDIDATES, MAX_RERANK_SNIPPET_BYTES, MAX_RERANK_STATE_BYTES, applyRerank };
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
import {
|
|
3
|
+
percolateVault
|
|
4
|
+
} from "./index-nd6nynv2.js";
|
|
2
5
|
import {
|
|
3
6
|
createGraphSnapshot,
|
|
4
7
|
openGraphAuthority
|
|
5
|
-
} from "./index-
|
|
8
|
+
} from "./index-famy7fhs.js";
|
|
6
9
|
import {
|
|
7
10
|
GraphAuthorityError,
|
|
8
11
|
validateGraphPercolationOptions
|
|
9
12
|
} from "./index-11621h23.js";
|
|
10
|
-
import {
|
|
11
|
-
percolateVault
|
|
12
|
-
} from "./index-nd6nynv2.js";
|
|
13
13
|
import {
|
|
14
14
|
analyzeVault,
|
|
15
15
|
lookupNote,
|