@blackbelt-technology/pi-dashboard-kb 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,100 @@
1
+ // Core types + the KbStore abstraction (design §2.5).
2
+ // Storage engine is accessed ONLY through KbStore — never via direct SQL in
3
+ // the chunker/indexer/search/CLI — so a future Turso/Tantivy or better-sqlite3
4
+ // backend is a swap, not a rewrite.
5
+
6
+ export type DocType = "doc" | "agents" | "source-md";
7
+
8
+ /** A structural (heading) chunk of a markdown file (design §3). */
9
+ export interface Chunk {
10
+ root: string; // configured source root id (relative or label)
11
+ path: string; // relative path within the source, portable
12
+ chunkId: string; // stable id within file (e.g. "<sha8>:<ordinal>")
13
+ headingPath: string; // breadcrumb: "A > B > C"
14
+ heading: string; // leaf heading
15
+ level: number; // 0 = preamble/file, 1..6 = ATX depth
16
+ parentChunkId: string | null;
17
+ docType: DocType;
18
+ body: string; // section text (breadcrumb is indexed via columns, not prepended)
19
+ bodyHash: string; // sha256 of trimmed body (exact-content dedup)
20
+ }
21
+
22
+ export interface GraphNode {
23
+ type: "file" | "heading" | "tag" | "entity";
24
+ name: string; // canonical label (headingPath for headings, path for files)
25
+ path: string | null;
26
+ }
27
+ export interface GraphEdge {
28
+ src: string; // node name
29
+ dst: string; // node name
30
+ rel: "child_of" | "links_to" | "references" | "has_tag";
31
+ weight?: number;
32
+ }
33
+
34
+ export interface KbHit {
35
+ root: string;
36
+ path: string;
37
+ headingPath: string;
38
+ chunkId: string;
39
+ docType: DocType;
40
+ score: number; // lower = more relevant (BM25 convention)
41
+ snippet: string;
42
+ akaPaths?: string[]; // duplicate copies collapsed by dedup
43
+ parent?: KbHit | null; // small-to-big: parent section/file context (expand.parent)
44
+ }
45
+
46
+ /** A pluggable reranker: rescoring BM25 top-k. Default = none (no-op). */
47
+ export type Reranker = (query: string, candidates: KbHit[]) => Promise<KbHit[]> | KbHit[];
48
+
49
+ export interface SearchOpts {
50
+ limit?: number;
51
+ root?: string;
52
+ docType?: DocType;
53
+ dedup?: boolean; // exact-content collapse (default true)
54
+ fieldWeights?: { headingPath: number; heading: number; body: number };
55
+ rootPriority?: Record<string, number>; // root id → priority (higher = preferred on dedup)
56
+ proximityBoost?: boolean; // Tier A: in-order/proximity aux ranker
57
+ diversity?: { enabled: boolean; lambda: number }; // Tier A: lexical MMR
58
+ expandParent?: boolean; // Tier B: attach parent section/file context
59
+ expandGraph?: boolean; // Tier B: pull neighbors/backlinks (opt-in)
60
+ rerank?: boolean; // Tier C: cross-encoder rerank (off by default; no-op without model)
61
+ reranker?: Reranker; // injected reranker; if absent, --rerank is a clean no-op
62
+ queryExpansion?: "off" | "prf" | "synonym" | "agent";
63
+ synonyms?: Record<string, string[]>; // curated glossary for synonym expansion
64
+ }
65
+
66
+ export interface FileState {
67
+ mtimeMs: number;
68
+ sha256: string;
69
+ }
70
+
71
+ /** The storage seam. Default impl = SqliteFtsStore over node:sqlite. */
72
+ export interface KbStore {
73
+ // schema lifecycle
74
+ init(): void;
75
+ begin(): void;
76
+ commit(): void;
77
+ rollback(): void;
78
+ close(): void;
79
+
80
+ // change detection
81
+ getFileState(root: string, path: string): FileState | null;
82
+ setFileState(root: string, path: string, state: FileState): void;
83
+ listPaths(root: string): string[];
84
+ deleteByPath(root: string, path: string): void; // chunks + outbound edges + file row
85
+
86
+ // indexing
87
+ insertChunk(c: Chunk): void;
88
+ addNode(n: GraphNode): void;
89
+ addEdge(e: GraphEdge): void;
90
+
91
+ // query
92
+ search(query: string, opts?: SearchOpts): KbHit[];
93
+ neighbors(node: string, depth: number, rel?: GraphEdge["rel"]): GraphNode[];
94
+ backlinks(node: string): GraphNode[];
95
+ getChunk(root: string, path: string, headingPath?: string): Chunk | null;
96
+ getChunkById(root: string, chunkId: string): Chunk | null;
97
+
98
+ // stats
99
+ counts(): { files: number; chunks: number; nodes: number; edges: number };
100
+ }