@vpxa/kb 0.1.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/LICENSE +21 -0
- package/README.md +1140 -0
- package/bin/kb.mjs +10 -0
- package/package.json +67 -0
- package/packages/analyzers/dist/blast-radius-analyzer.d.ts +23 -0
- package/packages/analyzers/dist/blast-radius-analyzer.js +114 -0
- package/packages/analyzers/dist/dependency-analyzer.d.ts +29 -0
- package/packages/analyzers/dist/dependency-analyzer.js +425 -0
- package/packages/analyzers/dist/diagram-generator.d.ts +13 -0
- package/packages/analyzers/dist/diagram-generator.js +86 -0
- package/packages/analyzers/dist/entry-point-analyzer.d.ts +19 -0
- package/packages/analyzers/dist/entry-point-analyzer.js +239 -0
- package/packages/analyzers/dist/index.d.ts +14 -0
- package/packages/analyzers/dist/index.js +23 -0
- package/packages/analyzers/dist/knowledge-producer.d.ts +32 -0
- package/packages/analyzers/dist/knowledge-producer.js +113 -0
- package/packages/analyzers/dist/pattern-analyzer.d.ts +12 -0
- package/packages/analyzers/dist/pattern-analyzer.js +359 -0
- package/packages/analyzers/dist/regex-call-graph.d.ts +17 -0
- package/packages/analyzers/dist/regex-call-graph.js +428 -0
- package/packages/analyzers/dist/structure-analyzer.d.ts +11 -0
- package/packages/analyzers/dist/structure-analyzer.js +258 -0
- package/packages/analyzers/dist/symbol-analyzer.d.ts +10 -0
- package/packages/analyzers/dist/symbol-analyzer.js +442 -0
- package/packages/analyzers/dist/ts-call-graph.d.ts +27 -0
- package/packages/analyzers/dist/ts-call-graph.js +160 -0
- package/packages/analyzers/dist/types.d.ts +98 -0
- package/packages/analyzers/dist/types.js +1 -0
- package/packages/chunker/dist/call-graph-extractor.d.ts +22 -0
- package/packages/chunker/dist/call-graph-extractor.js +90 -0
- package/packages/chunker/dist/chunker-factory.d.ts +7 -0
- package/packages/chunker/dist/chunker-factory.js +36 -0
- package/packages/chunker/dist/chunker.interface.d.ts +10 -0
- package/packages/chunker/dist/chunker.interface.js +1 -0
- package/packages/chunker/dist/code-chunker.d.ts +14 -0
- package/packages/chunker/dist/code-chunker.js +134 -0
- package/packages/chunker/dist/generic-chunker.d.ts +12 -0
- package/packages/chunker/dist/generic-chunker.js +72 -0
- package/packages/chunker/dist/index.d.ts +8 -0
- package/packages/chunker/dist/index.js +21 -0
- package/packages/chunker/dist/markdown-chunker.d.ts +14 -0
- package/packages/chunker/dist/markdown-chunker.js +122 -0
- package/packages/chunker/dist/treesitter-chunker.d.ts +47 -0
- package/packages/chunker/dist/treesitter-chunker.js +234 -0
- package/packages/cli/dist/commands/analyze.d.ts +3 -0
- package/packages/cli/dist/commands/analyze.js +112 -0
- package/packages/cli/dist/commands/context-cmds.d.ts +3 -0
- package/packages/cli/dist/commands/context-cmds.js +155 -0
- package/packages/cli/dist/commands/environment.d.ts +3 -0
- package/packages/cli/dist/commands/environment.js +204 -0
- package/packages/cli/dist/commands/execution.d.ts +3 -0
- package/packages/cli/dist/commands/execution.js +137 -0
- package/packages/cli/dist/commands/graph.d.ts +3 -0
- package/packages/cli/dist/commands/graph.js +81 -0
- package/packages/cli/dist/commands/init.d.ts +8 -0
- package/packages/cli/dist/commands/init.js +87 -0
- package/packages/cli/dist/commands/knowledge.d.ts +3 -0
- package/packages/cli/dist/commands/knowledge.js +139 -0
- package/packages/cli/dist/commands/search.d.ts +3 -0
- package/packages/cli/dist/commands/search.js +267 -0
- package/packages/cli/dist/commands/system.d.ts +3 -0
- package/packages/cli/dist/commands/system.js +241 -0
- package/packages/cli/dist/commands/workspace.d.ts +3 -0
- package/packages/cli/dist/commands/workspace.js +388 -0
- package/packages/cli/dist/context.d.ts +5 -0
- package/packages/cli/dist/context.js +14 -0
- package/packages/cli/dist/helpers.d.ts +52 -0
- package/packages/cli/dist/helpers.js +458 -0
- package/packages/cli/dist/index.d.ts +8 -0
- package/packages/cli/dist/index.js +69 -0
- package/packages/cli/dist/kb-init.d.ts +57 -0
- package/packages/cli/dist/kb-init.js +82 -0
- package/packages/cli/dist/types.d.ts +7 -0
- package/packages/cli/dist/types.js +1 -0
- package/packages/core/dist/constants.d.ts +49 -0
- package/packages/core/dist/constants.js +43 -0
- package/packages/core/dist/content-detector.d.ts +9 -0
- package/packages/core/dist/content-detector.js +79 -0
- package/packages/core/dist/errors.d.ts +18 -0
- package/packages/core/dist/errors.js +40 -0
- package/packages/core/dist/index.d.ts +6 -0
- package/packages/core/dist/index.js +9 -0
- package/packages/core/dist/logger.d.ts +9 -0
- package/packages/core/dist/logger.js +34 -0
- package/packages/core/dist/types.d.ts +108 -0
- package/packages/core/dist/types.js +1 -0
- package/packages/embeddings/dist/embedder.interface.d.ts +24 -0
- package/packages/embeddings/dist/embedder.interface.js +1 -0
- package/packages/embeddings/dist/index.d.ts +3 -0
- package/packages/embeddings/dist/index.js +5 -0
- package/packages/embeddings/dist/onnx-embedder.d.ts +24 -0
- package/packages/embeddings/dist/onnx-embedder.js +82 -0
- package/packages/indexer/dist/file-hasher.d.ts +11 -0
- package/packages/indexer/dist/file-hasher.js +13 -0
- package/packages/indexer/dist/filesystem-crawler.d.ts +27 -0
- package/packages/indexer/dist/filesystem-crawler.js +125 -0
- package/packages/indexer/dist/graph-extractor.d.ts +22 -0
- package/packages/indexer/dist/graph-extractor.js +111 -0
- package/packages/indexer/dist/incremental-indexer.d.ts +47 -0
- package/packages/indexer/dist/incremental-indexer.js +278 -0
- package/packages/indexer/dist/index.d.ts +5 -0
- package/packages/indexer/dist/index.js +14 -0
- package/packages/server/dist/api.d.ts +8 -0
- package/packages/server/dist/api.js +9 -0
- package/packages/server/dist/config.d.ts +3 -0
- package/packages/server/dist/config.js +75 -0
- package/packages/server/dist/curated-manager.d.ts +86 -0
- package/packages/server/dist/curated-manager.js +357 -0
- package/packages/server/dist/index.d.ts +2 -0
- package/packages/server/dist/index.js +134 -0
- package/packages/server/dist/replay-interceptor.d.ts +11 -0
- package/packages/server/dist/replay-interceptor.js +38 -0
- package/packages/server/dist/resources/resources.d.ts +4 -0
- package/packages/server/dist/resources/resources.js +40 -0
- package/packages/server/dist/server.d.ts +21 -0
- package/packages/server/dist/server.js +247 -0
- package/packages/server/dist/tools/analyze.tools.d.ts +11 -0
- package/packages/server/dist/tools/analyze.tools.js +288 -0
- package/packages/server/dist/tools/forge.tools.d.ts +12 -0
- package/packages/server/dist/tools/forge.tools.js +501 -0
- package/packages/server/dist/tools/forget.tool.d.ts +4 -0
- package/packages/server/dist/tools/forget.tool.js +43 -0
- package/packages/server/dist/tools/graph.tool.d.ts +4 -0
- package/packages/server/dist/tools/graph.tool.js +110 -0
- package/packages/server/dist/tools/list.tool.d.ts +4 -0
- package/packages/server/dist/tools/list.tool.js +56 -0
- package/packages/server/dist/tools/lookup.tool.d.ts +4 -0
- package/packages/server/dist/tools/lookup.tool.js +53 -0
- package/packages/server/dist/tools/onboard.tool.d.ts +5 -0
- package/packages/server/dist/tools/onboard.tool.js +112 -0
- package/packages/server/dist/tools/produce.tool.d.ts +3 -0
- package/packages/server/dist/tools/produce.tool.js +74 -0
- package/packages/server/dist/tools/read.tool.d.ts +4 -0
- package/packages/server/dist/tools/read.tool.js +49 -0
- package/packages/server/dist/tools/reindex.tool.d.ts +7 -0
- package/packages/server/dist/tools/reindex.tool.js +70 -0
- package/packages/server/dist/tools/remember.tool.d.ts +4 -0
- package/packages/server/dist/tools/remember.tool.js +45 -0
- package/packages/server/dist/tools/replay.tool.d.ts +3 -0
- package/packages/server/dist/tools/replay.tool.js +89 -0
- package/packages/server/dist/tools/search.tool.d.ts +5 -0
- package/packages/server/dist/tools/search.tool.js +331 -0
- package/packages/server/dist/tools/status.tool.d.ts +4 -0
- package/packages/server/dist/tools/status.tool.js +68 -0
- package/packages/server/dist/tools/toolkit.tools.d.ts +35 -0
- package/packages/server/dist/tools/toolkit.tools.js +1674 -0
- package/packages/server/dist/tools/update.tool.d.ts +4 -0
- package/packages/server/dist/tools/update.tool.js +42 -0
- package/packages/server/dist/tools/utility.tools.d.ts +15 -0
- package/packages/server/dist/tools/utility.tools.js +461 -0
- package/packages/store/dist/graph-store.interface.d.ts +104 -0
- package/packages/store/dist/graph-store.interface.js +1 -0
- package/packages/store/dist/index.d.ts +6 -0
- package/packages/store/dist/index.js +9 -0
- package/packages/store/dist/lance-store.d.ts +32 -0
- package/packages/store/dist/lance-store.js +258 -0
- package/packages/store/dist/sqlite-graph-store.d.ts +43 -0
- package/packages/store/dist/sqlite-graph-store.js +374 -0
- package/packages/store/dist/store-factory.d.ts +9 -0
- package/packages/store/dist/store-factory.js +14 -0
- package/packages/store/dist/store.interface.d.ts +48 -0
- package/packages/store/dist/store.interface.js +1 -0
- package/packages/tools/dist/batch.d.ts +21 -0
- package/packages/tools/dist/batch.js +45 -0
- package/packages/tools/dist/changelog.d.ts +34 -0
- package/packages/tools/dist/changelog.js +112 -0
- package/packages/tools/dist/check.d.ts +26 -0
- package/packages/tools/dist/check.js +59 -0
- package/packages/tools/dist/checkpoint.d.ts +17 -0
- package/packages/tools/dist/checkpoint.js +43 -0
- package/packages/tools/dist/codemod.d.ts +37 -0
- package/packages/tools/dist/codemod.js +69 -0
- package/packages/tools/dist/compact.d.ts +41 -0
- package/packages/tools/dist/compact.js +60 -0
- package/packages/tools/dist/data-transform.d.ts +10 -0
- package/packages/tools/dist/data-transform.js +124 -0
- package/packages/tools/dist/dead-symbols.d.ts +21 -0
- package/packages/tools/dist/dead-symbols.js +71 -0
- package/packages/tools/dist/delegate.d.ts +34 -0
- package/packages/tools/dist/delegate.js +130 -0
- package/packages/tools/dist/diff-parse.d.ts +26 -0
- package/packages/tools/dist/diff-parse.js +153 -0
- package/packages/tools/dist/digest.d.ts +53 -0
- package/packages/tools/dist/digest.js +242 -0
- package/packages/tools/dist/encode.d.ts +14 -0
- package/packages/tools/dist/encode.js +46 -0
- package/packages/tools/dist/env-info.d.ts +28 -0
- package/packages/tools/dist/env-info.js +58 -0
- package/packages/tools/dist/eval.d.ts +13 -0
- package/packages/tools/dist/eval.js +79 -0
- package/packages/tools/dist/evidence-map.d.ts +79 -0
- package/packages/tools/dist/evidence-map.js +203 -0
- package/packages/tools/dist/file-summary.d.ts +32 -0
- package/packages/tools/dist/file-summary.js +106 -0
- package/packages/tools/dist/file-walk.d.ts +4 -0
- package/packages/tools/dist/file-walk.js +75 -0
- package/packages/tools/dist/find-examples.d.ts +25 -0
- package/packages/tools/dist/find-examples.js +48 -0
- package/packages/tools/dist/find.d.ts +47 -0
- package/packages/tools/dist/find.js +120 -0
- package/packages/tools/dist/forge-classify.d.ts +44 -0
- package/packages/tools/dist/forge-classify.js +319 -0
- package/packages/tools/dist/forge-ground.d.ts +64 -0
- package/packages/tools/dist/forge-ground.js +184 -0
- package/packages/tools/dist/git-context.d.ts +22 -0
- package/packages/tools/dist/git-context.js +46 -0
- package/packages/tools/dist/graph-query.d.ts +89 -0
- package/packages/tools/dist/graph-query.js +194 -0
- package/packages/tools/dist/health.d.ts +14 -0
- package/packages/tools/dist/health.js +118 -0
- package/packages/tools/dist/http-request.d.ts +23 -0
- package/packages/tools/dist/http-request.js +58 -0
- package/packages/tools/dist/index.d.ts +49 -0
- package/packages/tools/dist/index.js +273 -0
- package/packages/tools/dist/lane.d.ts +39 -0
- package/packages/tools/dist/lane.js +227 -0
- package/packages/tools/dist/measure.d.ts +38 -0
- package/packages/tools/dist/measure.js +119 -0
- package/packages/tools/dist/onboard.d.ts +41 -0
- package/packages/tools/dist/onboard.js +1139 -0
- package/packages/tools/dist/parse-output.d.ts +80 -0
- package/packages/tools/dist/parse-output.js +158 -0
- package/packages/tools/dist/process-manager.d.ts +18 -0
- package/packages/tools/dist/process-manager.js +69 -0
- package/packages/tools/dist/queue.d.ts +38 -0
- package/packages/tools/dist/queue.js +126 -0
- package/packages/tools/dist/regex-test.d.ts +31 -0
- package/packages/tools/dist/regex-test.js +39 -0
- package/packages/tools/dist/rename.d.ts +29 -0
- package/packages/tools/dist/rename.js +70 -0
- package/packages/tools/dist/replay.d.ts +56 -0
- package/packages/tools/dist/replay.js +108 -0
- package/packages/tools/dist/schema-validate.d.ts +23 -0
- package/packages/tools/dist/schema-validate.js +141 -0
- package/packages/tools/dist/scope-map.d.ts +52 -0
- package/packages/tools/dist/scope-map.js +72 -0
- package/packages/tools/dist/snippet.d.ts +34 -0
- package/packages/tools/dist/snippet.js +80 -0
- package/packages/tools/dist/stash.d.ts +12 -0
- package/packages/tools/dist/stash.js +60 -0
- package/packages/tools/dist/stratum-card.d.ts +31 -0
- package/packages/tools/dist/stratum-card.js +239 -0
- package/packages/tools/dist/symbol.d.ts +28 -0
- package/packages/tools/dist/symbol.js +87 -0
- package/packages/tools/dist/test-run.d.ts +23 -0
- package/packages/tools/dist/test-run.js +55 -0
- package/packages/tools/dist/text-utils.d.ts +16 -0
- package/packages/tools/dist/text-utils.js +31 -0
- package/packages/tools/dist/time-utils.d.ts +18 -0
- package/packages/tools/dist/time-utils.js +135 -0
- package/packages/tools/dist/trace.d.ts +24 -0
- package/packages/tools/dist/trace.js +114 -0
- package/packages/tools/dist/truncation.d.ts +22 -0
- package/packages/tools/dist/truncation.js +45 -0
- package/packages/tools/dist/watch.d.ts +30 -0
- package/packages/tools/dist/watch.js +61 -0
- package/packages/tools/dist/web-fetch.d.ts +45 -0
- package/packages/tools/dist/web-fetch.js +249 -0
- package/packages/tools/dist/web-search.d.ts +23 -0
- package/packages/tools/dist/web-search.js +46 -0
- package/packages/tools/dist/workset.d.ts +45 -0
- package/packages/tools/dist/workset.js +77 -0
- package/packages/tui/dist/App.d.ts +8 -0
- package/packages/tui/dist/App.js +52659 -0
- package/packages/tui/dist/index.d.ts +19 -0
- package/packages/tui/dist/index.js +54742 -0
- package/packages/tui/dist/panels/CuratedPanel.d.ts +8 -0
- package/packages/tui/dist/panels/CuratedPanel.js +34452 -0
- package/packages/tui/dist/panels/LogPanel.d.ts +3 -0
- package/packages/tui/dist/panels/LogPanel.js +51894 -0
- package/packages/tui/dist/panels/SearchPanel.d.ts +10 -0
- package/packages/tui/dist/panels/SearchPanel.js +34985 -0
- package/packages/tui/dist/panels/StatusPanel.d.ts +8 -0
- package/packages/tui/dist/panels/StatusPanel.js +34465 -0
- package/skills/knowledge-base/SKILL.md +316 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { cosineSimilarity, estimateTokens, segment } from "./text-utils.js";
|
|
2
|
+
const DEFAULT_PIN_FIELDS = [
|
|
3
|
+
"status",
|
|
4
|
+
"files",
|
|
5
|
+
"decisions",
|
|
6
|
+
"blockers",
|
|
7
|
+
"artifacts",
|
|
8
|
+
"next"
|
|
9
|
+
];
|
|
10
|
+
const PIN_PATTERNS = {
|
|
11
|
+
status: /\b(?:status|result)\s*[:=]/i,
|
|
12
|
+
files: /\b(?:files?|modified|created|deleted)\s*[:=]/i,
|
|
13
|
+
artifacts: /\b(?:artifacts?)\s*[:=]/i,
|
|
14
|
+
decisions: /\b(?:decisions?|decided|chose|selected)\s*[:=]/i,
|
|
15
|
+
blockers: /\b(?:blockers?|blocked|blocking)\s*[:=]/i,
|
|
16
|
+
next: /\b(?:next\s*(?:steps?|actions?)|todo|follow.up)\s*[:=]/i
|
|
17
|
+
};
|
|
18
|
+
const VALUE_PATTERNS = {
|
|
19
|
+
status: /(?:^|\b)(?:status|result)\s*[:=]\s*(.+)$/i,
|
|
20
|
+
files: /(?:^|\b)(?:files?|modified|created|deleted)\s*[:=]\s*(.+)$/i,
|
|
21
|
+
artifacts: /(?:^|\b)(?:artifacts?)\s*[:=]\s*(.+)$/i,
|
|
22
|
+
decisions: /(?:^|\b)(?:decisions?|decided|chose|selected)\s*[:=]\s*(.+)$/i,
|
|
23
|
+
blockers: /(?:^|\b)(?:blockers?|blocked|blocking)\s*[:=]\s*(.+)$/i,
|
|
24
|
+
next: /(?:^|\b)(?:next\s*(?:steps?|actions?)|todo|follow.up)\s*[:=]\s*(.+)$/i
|
|
25
|
+
};
|
|
26
|
+
const STATUS_MARKERS = /\b(?:APPROVED|NEEDS_REVISION|BLOCKED|SUCCESS|PARTIAL|FAILED|ESCALATE)\b/i;
|
|
27
|
+
const EPSILON = 0.01;
|
|
28
|
+
function normalizePinFields(pinFields) {
|
|
29
|
+
const requested = pinFields ?? [...DEFAULT_PIN_FIELDS];
|
|
30
|
+
return [...new Set(requested.map((field) => field.toLowerCase()))];
|
|
31
|
+
}
|
|
32
|
+
function resolveWeight(weight) {
|
|
33
|
+
if (typeof weight !== "number" || !Number.isFinite(weight) || weight <= 0) {
|
|
34
|
+
return 1;
|
|
35
|
+
}
|
|
36
|
+
return weight;
|
|
37
|
+
}
|
|
38
|
+
function findPinnedFields(text, pinFields) {
|
|
39
|
+
const matched = /* @__PURE__ */ new Set();
|
|
40
|
+
for (const field of pinFields) {
|
|
41
|
+
const pattern = PIN_PATTERNS[field];
|
|
42
|
+
if (pattern?.test(text)) {
|
|
43
|
+
matched.add(field);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (pinFields.includes("status") && STATUS_MARKERS.test(text)) {
|
|
47
|
+
matched.add("status");
|
|
48
|
+
}
|
|
49
|
+
return [...matched];
|
|
50
|
+
}
|
|
51
|
+
function extractFieldValue(field, text) {
|
|
52
|
+
const lines = text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
|
|
53
|
+
for (const line of lines) {
|
|
54
|
+
const pattern = VALUE_PATTERNS[field];
|
|
55
|
+
const match = pattern?.exec(line);
|
|
56
|
+
if (match?.[1]) {
|
|
57
|
+
return match[1].trim();
|
|
58
|
+
}
|
|
59
|
+
if (field === "status") {
|
|
60
|
+
const marker = line.match(STATUS_MARKERS);
|
|
61
|
+
if (marker) {
|
|
62
|
+
return marker[0];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return text.trim();
|
|
67
|
+
}
|
|
68
|
+
function addFieldEntry(fields, field, sourceId, value) {
|
|
69
|
+
if (!fields[field]) {
|
|
70
|
+
fields[field] = [];
|
|
71
|
+
}
|
|
72
|
+
fields[field].push({ sourceId, value });
|
|
73
|
+
}
|
|
74
|
+
function prepareSources(sources, strategy, pinFields, fields) {
|
|
75
|
+
return sources.map((source) => {
|
|
76
|
+
const segments = segment(source.text, strategy).map((text, index) => {
|
|
77
|
+
const pinnedFields = findPinnedFields(text, pinFields);
|
|
78
|
+
for (const field of pinnedFields) {
|
|
79
|
+
addFieldEntry(fields, field, source.id, extractFieldValue(field, text));
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
index,
|
|
83
|
+
text,
|
|
84
|
+
pinnedFields,
|
|
85
|
+
score: 0
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
const pinnedChars = segments.filter((segment2) => segment2.pinnedFields.length > 0).reduce((total, segment2) => total + segment2.text.length, 0);
|
|
89
|
+
const nonPinnedChars = segments.filter((segment2) => segment2.pinnedFields.length === 0).reduce((total, segment2) => total + segment2.text.length, 0);
|
|
90
|
+
return {
|
|
91
|
+
id: source.id,
|
|
92
|
+
originalText: source.text,
|
|
93
|
+
weight: resolveWeight(source.weight),
|
|
94
|
+
segments,
|
|
95
|
+
pinnedChars,
|
|
96
|
+
nonPinnedChars
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function allocateBudgets(sources, remainingChars) {
|
|
101
|
+
const budgets = new Map(sources.map((source) => [source.id, 0]));
|
|
102
|
+
let budgetLeft = Math.max(0, remainingChars);
|
|
103
|
+
let active = sources.filter((source) => source.nonPinnedChars > 0);
|
|
104
|
+
while (budgetLeft > EPSILON && active.length > 0) {
|
|
105
|
+
const totalWeight = active.reduce((sum, source) => sum + source.weight, 0);
|
|
106
|
+
let allocatedThisRound = 0;
|
|
107
|
+
const nextActive = [];
|
|
108
|
+
for (const source of active) {
|
|
109
|
+
const alreadyAllocated = budgets.get(source.id) ?? 0;
|
|
110
|
+
const remainingCapacity = source.nonPinnedChars - alreadyAllocated;
|
|
111
|
+
if (remainingCapacity <= EPSILON) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const share = budgetLeft * (source.weight / totalWeight);
|
|
115
|
+
const allocation = Math.min(share, remainingCapacity);
|
|
116
|
+
budgets.set(source.id, alreadyAllocated + allocation);
|
|
117
|
+
allocatedThisRound += allocation;
|
|
118
|
+
if (remainingCapacity - allocation > EPSILON) {
|
|
119
|
+
nextActive.push(source);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (allocatedThisRound <= EPSILON) {
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
budgetLeft -= allocatedThisRound;
|
|
126
|
+
active = nextActive;
|
|
127
|
+
}
|
|
128
|
+
return budgets;
|
|
129
|
+
}
|
|
130
|
+
function formatDigestText(blocks) {
|
|
131
|
+
return blocks.map((block) => `[${block.id}]
|
|
132
|
+
${block.text}`).join("\n\n");
|
|
133
|
+
}
|
|
134
|
+
function ratio(originalChars, compressedChars) {
|
|
135
|
+
if (originalChars === 0) {
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
|
138
|
+
return compressedChars / originalChars;
|
|
139
|
+
}
|
|
140
|
+
async function digest(embedder, options) {
|
|
141
|
+
const { sources, query, maxChars = 4e3, pinFields, segmentation = "paragraph" } = options;
|
|
142
|
+
const fields = {};
|
|
143
|
+
const effectivePinFields = normalizePinFields(pinFields);
|
|
144
|
+
const preparedSources = prepareSources(sources, segmentation, effectivePinFields, fields);
|
|
145
|
+
const totalOriginalChars = sources.reduce((total, source) => total + source.text.length, 0);
|
|
146
|
+
if (totalOriginalChars <= maxChars) {
|
|
147
|
+
const blocks2 = preparedSources.filter((source) => source.originalText.trim().length > 0).map((source) => ({ id: source.id, text: source.originalText }));
|
|
148
|
+
const sourceStats2 = preparedSources.map((source) => ({
|
|
149
|
+
id: source.id,
|
|
150
|
+
originalChars: source.originalText.length,
|
|
151
|
+
keptChars: source.originalText.length,
|
|
152
|
+
segmentsKept: source.segments.length,
|
|
153
|
+
segmentsTotal: source.segments.length
|
|
154
|
+
}));
|
|
155
|
+
return {
|
|
156
|
+
text: formatDigestText(blocks2),
|
|
157
|
+
fields,
|
|
158
|
+
sourceStats: sourceStats2,
|
|
159
|
+
totalOriginalChars,
|
|
160
|
+
totalCompressedChars: totalOriginalChars,
|
|
161
|
+
ratio: totalOriginalChars === 0 ? 0 : 1
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
const queryVector = await embedder.embedQuery(query);
|
|
165
|
+
for (const source of preparedSources) {
|
|
166
|
+
for (const segment2 of source.segments) {
|
|
167
|
+
if (segment2.pinnedFields.length > 0) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const vector = await embedder.embed(segment2.text);
|
|
171
|
+
segment2.score = cosineSimilarity(queryVector, vector);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const reservedChars = preparedSources.reduce((total, source) => total + source.pinnedChars, 0);
|
|
175
|
+
const budgets = allocateBudgets(preparedSources, maxChars - reservedChars);
|
|
176
|
+
const sourceStats = [];
|
|
177
|
+
const blocks = [];
|
|
178
|
+
let totalCompressedChars = 0;
|
|
179
|
+
for (const source of preparedSources) {
|
|
180
|
+
const budget = budgets.get(source.id) ?? 0;
|
|
181
|
+
const selectedTexts = /* @__PURE__ */ new Map();
|
|
182
|
+
let selectedNonPinnedChars = 0;
|
|
183
|
+
const rankedSegments = source.segments.filter((segment2) => segment2.pinnedFields.length === 0).sort(
|
|
184
|
+
(left, right) => right.score - left.score || estimateTokens(left.text) - estimateTokens(right.text) || left.index - right.index
|
|
185
|
+
);
|
|
186
|
+
let overflowCandidate;
|
|
187
|
+
for (const segment2 of rankedSegments) {
|
|
188
|
+
const available = budget - selectedNonPinnedChars;
|
|
189
|
+
if (available <= EPSILON) {
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
if (segment2.text.length <= available + EPSILON) {
|
|
193
|
+
selectedTexts.set(segment2.index, segment2.text);
|
|
194
|
+
selectedNonPinnedChars += segment2.text.length;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (!overflowCandidate) {
|
|
198
|
+
overflowCandidate = segment2;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const leftover = Math.floor(budget - selectedNonPinnedChars);
|
|
202
|
+
if (overflowCandidate && leftover > 0) {
|
|
203
|
+
const truncated = overflowCandidate.text.slice(0, leftover).trimEnd();
|
|
204
|
+
if (truncated.length > 0) {
|
|
205
|
+
selectedTexts.set(overflowCandidate.index, truncated);
|
|
206
|
+
selectedNonPinnedChars += truncated.length;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const keptSegments = source.segments.filter((segment2) => segment2.pinnedFields.length > 0 || selectedTexts.has(segment2.index)).map((segment2) => ({
|
|
210
|
+
index: segment2.index,
|
|
211
|
+
text: selectedTexts.get(segment2.index) ?? segment2.text
|
|
212
|
+
}));
|
|
213
|
+
const keptChars = keptSegments.reduce((total, segment2) => total + segment2.text.length, 0);
|
|
214
|
+
sourceStats.push({
|
|
215
|
+
id: source.id,
|
|
216
|
+
originalChars: source.originalText.length,
|
|
217
|
+
keptChars,
|
|
218
|
+
segmentsKept: keptSegments.length,
|
|
219
|
+
segmentsTotal: source.segments.length
|
|
220
|
+
});
|
|
221
|
+
totalCompressedChars += keptChars;
|
|
222
|
+
if (keptSegments.length === 0) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
blocks.push({
|
|
226
|
+
id: source.id,
|
|
227
|
+
text: keptSegments.map((segment2) => segment2.text).join("\n\n")
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
text: formatDigestText(blocks),
|
|
232
|
+
fields,
|
|
233
|
+
sourceStats,
|
|
234
|
+
totalOriginalChars,
|
|
235
|
+
totalCompressedChars,
|
|
236
|
+
ratio: ratio(totalOriginalChars, totalCompressedChars)
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
export {
|
|
240
|
+
digest
|
|
241
|
+
};
|
|
242
|
+
//# sourceMappingURL=digest.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kb_encode — Encoding, decoding, and hashing utilities.
|
|
3
|
+
*/
|
|
4
|
+
export type EncodeOperation = 'base64_encode' | 'base64_decode' | 'url_encode' | 'url_decode' | 'sha256' | 'md5' | 'jwt_decode' | 'hex_encode' | 'hex_decode';
|
|
5
|
+
export interface EncodeOptions {
|
|
6
|
+
operation: EncodeOperation;
|
|
7
|
+
input: string;
|
|
8
|
+
}
|
|
9
|
+
export interface EncodeResult {
|
|
10
|
+
output: string;
|
|
11
|
+
operation: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function encode(options: EncodeOptions): EncodeResult;
|
|
14
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
function encode(options) {
|
|
3
|
+
const { operation, input } = options;
|
|
4
|
+
let output;
|
|
5
|
+
switch (operation) {
|
|
6
|
+
case "base64_encode":
|
|
7
|
+
output = Buffer.from(input).toString("base64");
|
|
8
|
+
break;
|
|
9
|
+
case "base64_decode":
|
|
10
|
+
output = Buffer.from(input, "base64").toString("utf8");
|
|
11
|
+
break;
|
|
12
|
+
case "url_encode":
|
|
13
|
+
output = encodeURIComponent(input);
|
|
14
|
+
break;
|
|
15
|
+
case "url_decode":
|
|
16
|
+
output = decodeURIComponent(input);
|
|
17
|
+
break;
|
|
18
|
+
case "sha256":
|
|
19
|
+
output = createHash("sha256").update(input).digest("hex");
|
|
20
|
+
break;
|
|
21
|
+
case "md5":
|
|
22
|
+
output = createHash("md5").update(input).digest("hex");
|
|
23
|
+
break;
|
|
24
|
+
case "jwt_decode": {
|
|
25
|
+
const parts = input.split(".");
|
|
26
|
+
if (parts.length !== 3) throw new Error("Invalid JWT: expected 3 dot-separated parts");
|
|
27
|
+
const header = JSON.parse(Buffer.from(parts[0], "base64url").toString());
|
|
28
|
+
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString());
|
|
29
|
+
output = JSON.stringify({ header, payload }, null, 2);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case "hex_encode":
|
|
33
|
+
output = Buffer.from(input).toString("hex");
|
|
34
|
+
break;
|
|
35
|
+
case "hex_decode":
|
|
36
|
+
output = Buffer.from(input, "hex").toString("utf8");
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Unknown operation: ${operation}`);
|
|
40
|
+
}
|
|
41
|
+
return { output, operation };
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
encode
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=encode.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kb_env — System environment and runtime information.
|
|
3
|
+
*/
|
|
4
|
+
export interface EnvInfoOptions {
|
|
5
|
+
includeEnv?: boolean;
|
|
6
|
+
filterEnv?: string;
|
|
7
|
+
showSensitive?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface EnvInfoResult {
|
|
10
|
+
system: {
|
|
11
|
+
platform: string;
|
|
12
|
+
arch: string;
|
|
13
|
+
release: string;
|
|
14
|
+
hostname: string;
|
|
15
|
+
type: string;
|
|
16
|
+
cpus: number;
|
|
17
|
+
memoryTotalGb: number;
|
|
18
|
+
memoryFreeGb: number;
|
|
19
|
+
};
|
|
20
|
+
runtime: {
|
|
21
|
+
node: string;
|
|
22
|
+
v8: string;
|
|
23
|
+
};
|
|
24
|
+
cwd: string;
|
|
25
|
+
env?: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
export declare function envInfo(options?: EnvInfoOptions): EnvInfoResult;
|
|
28
|
+
//# sourceMappingURL=env-info.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
arch,
|
|
3
|
+
cpus,
|
|
4
|
+
freemem,
|
|
5
|
+
hostname,
|
|
6
|
+
type as osType,
|
|
7
|
+
platform,
|
|
8
|
+
release,
|
|
9
|
+
totalmem
|
|
10
|
+
} from "node:os";
|
|
11
|
+
const SENSITIVE_PATTERNS = [
|
|
12
|
+
/key/i,
|
|
13
|
+
/secret/i,
|
|
14
|
+
/token/i,
|
|
15
|
+
/password/i,
|
|
16
|
+
/passwd/i,
|
|
17
|
+
/credential/i,
|
|
18
|
+
/private/i,
|
|
19
|
+
/certificate/i
|
|
20
|
+
];
|
|
21
|
+
function envInfo(options = {}) {
|
|
22
|
+
const { includeEnv = false, filterEnv, showSensitive = false } = options;
|
|
23
|
+
const result = {
|
|
24
|
+
system: {
|
|
25
|
+
platform: platform(),
|
|
26
|
+
arch: arch(),
|
|
27
|
+
release: release(),
|
|
28
|
+
hostname: hostname(),
|
|
29
|
+
type: osType(),
|
|
30
|
+
cpus: cpus().length,
|
|
31
|
+
memoryTotalGb: Math.round(totalmem() / 1024 ** 3 * 10) / 10,
|
|
32
|
+
memoryFreeGb: Math.round(freemem() / 1024 ** 3 * 10) / 10
|
|
33
|
+
},
|
|
34
|
+
runtime: {
|
|
35
|
+
node: process.versions.node,
|
|
36
|
+
v8: process.versions.v8
|
|
37
|
+
},
|
|
38
|
+
cwd: process.cwd()
|
|
39
|
+
};
|
|
40
|
+
if (includeEnv) {
|
|
41
|
+
const env = {};
|
|
42
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
43
|
+
if (!value) continue;
|
|
44
|
+
if (filterEnv && !key.toLowerCase().includes(filterEnv.toLowerCase())) continue;
|
|
45
|
+
if (!showSensitive && SENSITIVE_PATTERNS.some((p) => p.test(key))) {
|
|
46
|
+
env[key] = "[REDACTED]";
|
|
47
|
+
} else {
|
|
48
|
+
env[key] = value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
result.env = env;
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
envInfo
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=env-info.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface EvalOptions {
|
|
2
|
+
code: string;
|
|
3
|
+
lang?: 'js' | 'ts';
|
|
4
|
+
timeout?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface EvalResult {
|
|
7
|
+
success: boolean;
|
|
8
|
+
output: string;
|
|
9
|
+
error?: string;
|
|
10
|
+
durationMs: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function evaluate(options: EvalOptions): EvalResult;
|
|
13
|
+
//# sourceMappingURL=eval.d.ts.map
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import vm from "node:vm";
|
|
2
|
+
function evaluate(options) {
|
|
3
|
+
const { code, lang = "js", timeout = 5e3 } = options;
|
|
4
|
+
const start = Date.now();
|
|
5
|
+
try {
|
|
6
|
+
const execCode = lang === "ts" ? stripTypeAnnotations(code) : code;
|
|
7
|
+
const logs = [];
|
|
8
|
+
const sandbox = {
|
|
9
|
+
console: {
|
|
10
|
+
log: (...args) => logs.push(args.map(String).join(" ")),
|
|
11
|
+
error: (...args) => logs.push(`[error] ${args.map(String).join(" ")}`),
|
|
12
|
+
warn: (...args) => logs.push(`[warn] ${args.map(String).join(" ")}`)
|
|
13
|
+
},
|
|
14
|
+
setTimeout: void 0,
|
|
15
|
+
setInterval: void 0,
|
|
16
|
+
setImmediate: void 0,
|
|
17
|
+
fetch: void 0,
|
|
18
|
+
process: void 0,
|
|
19
|
+
require: void 0,
|
|
20
|
+
JSON,
|
|
21
|
+
Math,
|
|
22
|
+
Date,
|
|
23
|
+
Array,
|
|
24
|
+
Object,
|
|
25
|
+
String,
|
|
26
|
+
Number,
|
|
27
|
+
Boolean,
|
|
28
|
+
Map,
|
|
29
|
+
Set,
|
|
30
|
+
RegExp,
|
|
31
|
+
Error,
|
|
32
|
+
Promise,
|
|
33
|
+
parseInt,
|
|
34
|
+
parseFloat,
|
|
35
|
+
isNaN,
|
|
36
|
+
isFinite
|
|
37
|
+
};
|
|
38
|
+
const context = vm.createContext(sandbox, {
|
|
39
|
+
codeGeneration: {
|
|
40
|
+
strings: false,
|
|
41
|
+
wasm: false
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const result = vm.runInContext(execCode, context, { timeout });
|
|
45
|
+
const output = logs.length > 0 ? logs.join("\n") + (result !== void 0 ? `
|
|
46
|
+
\u2192 ${formatValue(result)}` : "") : result !== void 0 ? formatValue(result) : "(no output)";
|
|
47
|
+
return {
|
|
48
|
+
success: true,
|
|
49
|
+
output,
|
|
50
|
+
durationMs: Date.now() - start
|
|
51
|
+
};
|
|
52
|
+
} catch (error) {
|
|
53
|
+
return {
|
|
54
|
+
success: false,
|
|
55
|
+
output: "",
|
|
56
|
+
error: error.message,
|
|
57
|
+
durationMs: Date.now() - start
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function formatValue(value) {
|
|
62
|
+
if (value === void 0) return "undefined";
|
|
63
|
+
if (value === null) return "null";
|
|
64
|
+
if (typeof value === "object") {
|
|
65
|
+
try {
|
|
66
|
+
return JSON.stringify(value, null, 2);
|
|
67
|
+
} catch {
|
|
68
|
+
return String(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return String(value);
|
|
72
|
+
}
|
|
73
|
+
function stripTypeAnnotations(code) {
|
|
74
|
+
return code.replace(/^\s*import\s+type\s+.*?;\s*$/gm, "").replace(/^\s*(?:export\s+)?interface\s+\w+[^{]*\{[\s\S]*?^\s*}\s*$/gm, "").replace(/^\s*(?:export\s+)?type\s+\w+\s*=.*?;\s*$/gm, "").replace(/([,(]\s*[A-Za-z_$][\w$]*)\s*:\s*[^,)=\n]+/g, "$1").replace(/\)\s*:\s*[^={\n]+(?=\s*(?:=>|\{))/g, ")").replace(/\s+as\s+[A-Za-z_$][\w$<>,[\]|&\s.]*/g, "").replace(/<(?:[A-Za-z_$][\w$]*\s*,?\s*)+>(?=\s*\()/g, "");
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
evaluate
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=eval.js.map
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kb_evidence_map — FORGE Evidence Map CRUD + Gate evaluator.
|
|
3
|
+
*
|
|
4
|
+
* Structured storage, validation, and gate evaluation for FORGE Evidence Map entries.
|
|
5
|
+
* Persisted in .kb-state/evidence-maps.json.
|
|
6
|
+
*/
|
|
7
|
+
export type EvidenceStatus = 'V' | 'A' | 'U';
|
|
8
|
+
export type UnknownType = 'contract' | 'convention' | 'freshness' | 'runtime' | 'data-flow' | 'impact';
|
|
9
|
+
export type GateDecision = 'YIELD' | 'HOLD' | 'HARD_BLOCK' | 'FORCED_DELIVERY';
|
|
10
|
+
export type ForgeTier = 'floor' | 'standard' | 'critical';
|
|
11
|
+
export interface EvidenceEntry {
|
|
12
|
+
id: number;
|
|
13
|
+
claim: string;
|
|
14
|
+
status: EvidenceStatus;
|
|
15
|
+
receipt: string;
|
|
16
|
+
criticalPath: boolean;
|
|
17
|
+
unknownType?: UnknownType;
|
|
18
|
+
}
|
|
19
|
+
export interface EvidenceMapState {
|
|
20
|
+
taskId: string;
|
|
21
|
+
tier: ForgeTier;
|
|
22
|
+
entries: EvidenceEntry[];
|
|
23
|
+
createdAt: string;
|
|
24
|
+
updatedAt: string;
|
|
25
|
+
}
|
|
26
|
+
export interface GateResult {
|
|
27
|
+
decision: GateDecision;
|
|
28
|
+
reason: string;
|
|
29
|
+
unresolvedCritical: EvidenceEntry[];
|
|
30
|
+
warnings: string[];
|
|
31
|
+
stats: {
|
|
32
|
+
total: number;
|
|
33
|
+
verified: number;
|
|
34
|
+
assumed: number;
|
|
35
|
+
unresolved: number;
|
|
36
|
+
};
|
|
37
|
+
annotation?: string;
|
|
38
|
+
}
|
|
39
|
+
export type EvidenceMapAction = {
|
|
40
|
+
action: 'create';
|
|
41
|
+
taskId: string;
|
|
42
|
+
tier: ForgeTier;
|
|
43
|
+
} | {
|
|
44
|
+
action: 'add';
|
|
45
|
+
taskId: string;
|
|
46
|
+
claim: string;
|
|
47
|
+
status: EvidenceStatus;
|
|
48
|
+
receipt: string;
|
|
49
|
+
criticalPath?: boolean;
|
|
50
|
+
unknownType?: UnknownType;
|
|
51
|
+
} | {
|
|
52
|
+
action: 'update';
|
|
53
|
+
taskId: string;
|
|
54
|
+
id: number;
|
|
55
|
+
status: EvidenceStatus;
|
|
56
|
+
receipt: string;
|
|
57
|
+
} | {
|
|
58
|
+
action: 'get';
|
|
59
|
+
taskId: string;
|
|
60
|
+
} | {
|
|
61
|
+
action: 'gate';
|
|
62
|
+
taskId: string;
|
|
63
|
+
retryCount?: number;
|
|
64
|
+
} | {
|
|
65
|
+
action: 'list';
|
|
66
|
+
} | {
|
|
67
|
+
action: 'delete';
|
|
68
|
+
taskId: string;
|
|
69
|
+
};
|
|
70
|
+
export interface EvidenceMapResult {
|
|
71
|
+
state?: EvidenceMapState;
|
|
72
|
+
states?: EvidenceMapState[];
|
|
73
|
+
entry?: EvidenceEntry;
|
|
74
|
+
gate?: GateResult;
|
|
75
|
+
deleted?: boolean;
|
|
76
|
+
formattedMap?: string;
|
|
77
|
+
}
|
|
78
|
+
export declare function evidenceMap(action: EvidenceMapAction, cwd?: string): EvidenceMapResult;
|
|
79
|
+
//# sourceMappingURL=evidence-map.d.ts.map
|