@xh/hoist-docs 0.0.0-stage → 0.0.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.md +190 -0
- package/README.md +145 -2
- package/bin/hoist-docs.mjs +16 -0
- package/dist/IndexStore.d.ts +78 -0
- package/dist/IndexStore.js +249 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +258 -0
- package/dist/config.d.ts +17 -0
- package/dist/config.js +18 -0
- package/dist/docs/index.d.ts +70 -0
- package/dist/docs/index.js +101 -0
- package/dist/index/DocEntry.d.ts +16 -0
- package/dist/index/DocEntry.js +32 -0
- package/dist/index/DocIdResolver.d.ts +31 -0
- package/dist/index/DocIdResolver.js +159 -0
- package/dist/index/DocSearch.d.ts +41 -0
- package/dist/index/DocSearch.js +180 -0
- package/dist/index/LibraryIndex.d.ts +62 -0
- package/dist/index/LibraryIndex.js +153 -0
- package/dist/index/SymbolSearch.d.ts +52 -0
- package/dist/index/SymbolSearch.js +249 -0
- package/dist/index/doc-sections.d.ts +94 -0
- package/dist/index/doc-sections.js +260 -0
- package/dist/index/search-text.d.ts +78 -0
- package/dist/index/search-text.js +140 -0
- package/dist/index/types.d.ts +115 -0
- package/dist/index/types.js +11 -0
- package/dist/libraries.d.ts +57 -0
- package/dist/libraries.js +38 -0
- package/dist/local/cache.d.ts +25 -0
- package/dist/local/cache.js +62 -0
- package/dist/local/checkout.d.ts +6 -0
- package/dist/local/checkout.js +178 -0
- package/dist/local/installed.d.ts +22 -0
- package/dist/local/installed.js +99 -0
- package/dist/local/maven.d.ts +49 -0
- package/dist/local/maven.js +184 -0
- package/dist/local/npm.d.ts +24 -0
- package/dist/local/npm.js +66 -0
- package/dist/local/project.d.ts +26 -0
- package/dist/local/project.js +103 -0
- package/dist/local/registry.d.ts +15 -0
- package/dist/local/registry.js +67 -0
- package/dist/local/sources.d.ts +30 -0
- package/dist/local/sources.js +50 -0
- package/dist/local/tar.d.ts +6 -0
- package/dist/local/tar.js +31 -0
- package/dist/log.d.ts +10 -0
- package/dist/log.js +31 -0
- package/dist/mcp.d.ts +23 -0
- package/dist/mcp.js +396 -0
- package/dist/server.d.ts +15 -0
- package/dist/server.js +88 -0
- package/dist/tools/DocTools.d.ts +19 -0
- package/dist/tools/DocTools.js +245 -0
- package/dist/tools/SymbolTools.d.ts +27 -0
- package/dist/tools/SymbolTools.js +572 -0
- package/dist/tools/types.d.ts +6 -0
- package/dist/tools/types.js +7 -0
- package/dist/usage.d.ts +10 -0
- package/dist/usage.js +11 -0
- package/package.json +62 -4
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file belongs to Hoist, an application development toolkit
|
|
3
|
+
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
|
|
4
|
+
*
|
|
5
|
+
* Copyright © 2026 Extremely Heavy Industries Inc.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Browser-safe docs library: registry, content and ranked search over the docs of one or more
|
|
9
|
+
* Hoist libraries, for docs viewers such as Toolbox's. Same ranking as the MCP tools, because it
|
|
10
|
+
* is the same code. Imports nothing from Node, so it bundles for the browser.
|
|
11
|
+
*
|
|
12
|
+
* Input is a docs payload per library: the docs part of an index file, written by
|
|
13
|
+
* `hoist-docs export-docs` (see {@link extractDocs}). Results keep the shapes of the former
|
|
14
|
+
* `docs/*` HTTP API, which Toolbox's viewer already reads.
|
|
15
|
+
*/
|
|
16
|
+
import { DocEntry } from '../index/DocEntry.js';
|
|
17
|
+
import { DocSearch } from '../index/DocSearch.js';
|
|
18
|
+
import { SCHEMA_VERSION } from '../index/types.js';
|
|
19
|
+
import { LIBRARIES, libraryDef } from '../libraries.js';
|
|
20
|
+
const LIBRARY_IDS = LIBRARIES.map(l => l.id);
|
|
21
|
+
/** The docs part of a full index file. */
|
|
22
|
+
export function extractDocs(index) {
|
|
23
|
+
return { schemaVersion: index.schemaVersion, library: index.library, docs: index.docs };
|
|
24
|
+
}
|
|
25
|
+
/** One library's docs, searchable. */
|
|
26
|
+
export class LibraryDocs {
|
|
27
|
+
library;
|
|
28
|
+
/** Source name viewers use for this library: `hoist-react`, `hoist-core`, or the library id. */
|
|
29
|
+
source;
|
|
30
|
+
categories;
|
|
31
|
+
docs;
|
|
32
|
+
docsById;
|
|
33
|
+
_search = null;
|
|
34
|
+
constructor(payload) {
|
|
35
|
+
if (payload.schemaVersion !== SCHEMA_VERSION) {
|
|
36
|
+
throw new Error(`Unsupported docs schemaVersion ${payload.schemaVersion} - expected ${SCHEMA_VERSION}. Upgrade @xh/hoist-docs.`);
|
|
37
|
+
}
|
|
38
|
+
this.library = payload.library;
|
|
39
|
+
this.source = libraryDef(payload.library.id)?.source ?? payload.library.id;
|
|
40
|
+
this.categories = payload.docs.viewerCategories ?? [];
|
|
41
|
+
this.docs = payload.docs.entries.map(e => new DocEntry(e));
|
|
42
|
+
this.docsById = new Map(this.docs.map(d => [d.id, d]));
|
|
43
|
+
}
|
|
44
|
+
/** Search index, built on first use. */
|
|
45
|
+
get docSearch() {
|
|
46
|
+
return (this._search ??= new DocSearch(this));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The docs of several libraries, hoist-react first. */
|
|
50
|
+
export class DocsCatalog {
|
|
51
|
+
libraries;
|
|
52
|
+
constructor(payloads) {
|
|
53
|
+
const order = (id) => LIBRARY_IDS.indexOf(id) + 1 || 99;
|
|
54
|
+
this.libraries = payloads
|
|
55
|
+
.map(p => new LibraryDocs(p))
|
|
56
|
+
.sort((a, b) => order(a.library.id) - order(b.library.id));
|
|
57
|
+
}
|
|
58
|
+
/** Every doc, with each source's categories and version. */
|
|
59
|
+
registry() {
|
|
60
|
+
const entries = [], sources = {};
|
|
61
|
+
for (const lib of this.libraries) {
|
|
62
|
+
sources[lib.source] = {
|
|
63
|
+
label: libraryDef(lib.library.id)?.label ?? lib.library.name,
|
|
64
|
+
categories: lib.categories,
|
|
65
|
+
version: lib.library.version,
|
|
66
|
+
versions: [lib.library.version]
|
|
67
|
+
};
|
|
68
|
+
for (const d of lib.docs) {
|
|
69
|
+
entries.push({
|
|
70
|
+
id: d.id,
|
|
71
|
+
source: lib.source,
|
|
72
|
+
title: d.title,
|
|
73
|
+
category: d.viewerCategory,
|
|
74
|
+
description: d.description,
|
|
75
|
+
keywords: d.keywords
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { entries, sources };
|
|
80
|
+
}
|
|
81
|
+
/** Markdown of one doc, or null if the source has no such doc. */
|
|
82
|
+
content(source, docId) {
|
|
83
|
+
return this.librarySource(source)?.docsById.get(docId)?.markdown ?? null;
|
|
84
|
+
}
|
|
85
|
+
/** Ranked docs across every library, each once under its best-matching section. */
|
|
86
|
+
search(query, limit = 25) {
|
|
87
|
+
return this.libraries
|
|
88
|
+
.flatMap(lib => lib.docSearch.searchDocs(query, limit).map(r => ({
|
|
89
|
+
source: lib.source,
|
|
90
|
+
id: r.entry.id,
|
|
91
|
+
score: r.score,
|
|
92
|
+
section: r.section.ref,
|
|
93
|
+
snippet: r.excerpt
|
|
94
|
+
})))
|
|
95
|
+
.sort((a, b) => b.score - a.score)
|
|
96
|
+
.slice(0, limit);
|
|
97
|
+
}
|
|
98
|
+
librarySource(source) {
|
|
99
|
+
return this.libraries.find(l => l.source === source);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type DocSection } from './doc-sections.ts';
|
|
2
|
+
import type { DocEntryJson } from './types.ts';
|
|
3
|
+
/** One doc of a library, with its sections parsed on first use. Browser-safe. */
|
|
4
|
+
export declare class DocEntry {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly title: string;
|
|
7
|
+
readonly mcpCategory: string;
|
|
8
|
+
readonly viewerCategory: string;
|
|
9
|
+
readonly description: string;
|
|
10
|
+
readonly keywords: string[];
|
|
11
|
+
readonly aliases: string[];
|
|
12
|
+
readonly markdown: string;
|
|
13
|
+
private _sections;
|
|
14
|
+
constructor(json: DocEntryJson);
|
|
15
|
+
get sections(): DocSection[];
|
|
16
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file belongs to Hoist, an application development toolkit
|
|
3
|
+
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
|
|
4
|
+
*
|
|
5
|
+
* Copyright © 2026 Extremely Heavy Industries Inc.
|
|
6
|
+
*/
|
|
7
|
+
import { parseDocSections } from './doc-sections.js';
|
|
8
|
+
/** One doc of a library, with its sections parsed on first use. Browser-safe. */
|
|
9
|
+
export class DocEntry {
|
|
10
|
+
id;
|
|
11
|
+
title;
|
|
12
|
+
mcpCategory;
|
|
13
|
+
viewerCategory;
|
|
14
|
+
description;
|
|
15
|
+
keywords;
|
|
16
|
+
aliases;
|
|
17
|
+
markdown;
|
|
18
|
+
_sections = null;
|
|
19
|
+
constructor(json) {
|
|
20
|
+
this.id = json.id;
|
|
21
|
+
this.title = json.title;
|
|
22
|
+
this.mcpCategory = json.mcpCategory;
|
|
23
|
+
this.viewerCategory = json.viewerCategory;
|
|
24
|
+
this.description = json.description ?? '';
|
|
25
|
+
this.keywords = json.keywords ?? [];
|
|
26
|
+
this.aliases = json.aliases ?? [];
|
|
27
|
+
this.markdown = json.markdown ?? '';
|
|
28
|
+
}
|
|
29
|
+
get sections() {
|
|
30
|
+
return (this._sections ??= parseDocSections(this, this.markdown));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tolerant doc id resolution for one library's docs. Accepts the shortened or slightly-off ids
|
|
3
|
+
* agents naturally try (`grid`, `cmp/grid`, `v85`) and resolves them when the mapping is
|
|
4
|
+
* unambiguous, failing closed with suggestions otherwise.
|
|
5
|
+
*
|
|
6
|
+
* Tiers, in order: exact id, trivial normalization (`./`, leading and doubled slashes), case,
|
|
7
|
+
* aliases (generated from paths plus each entry's declared `aliases`), then structural suffix
|
|
8
|
+
* completion (`X.md`, `X/README.md`, `docs/X.md`, ...). Generated aliases that would map to more
|
|
9
|
+
* than one doc are dropped, and suffix completion matching more than one doc resolves nothing.
|
|
10
|
+
*/
|
|
11
|
+
import type { DocEntry } from './DocEntry.ts';
|
|
12
|
+
export type DocResolution = {
|
|
13
|
+
kind: 'exact';
|
|
14
|
+
entry: DocEntry;
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'normalized';
|
|
17
|
+
entry: DocEntry;
|
|
18
|
+
matchedAs: string;
|
|
19
|
+
} | {
|
|
20
|
+
kind: 'unknown';
|
|
21
|
+
suggestions: string[];
|
|
22
|
+
};
|
|
23
|
+
export declare class DocIdResolver {
|
|
24
|
+
private readonly entries;
|
|
25
|
+
private readonly byId;
|
|
26
|
+
private readonly aliases;
|
|
27
|
+
constructor(entries: DocEntry[]);
|
|
28
|
+
resolve(input: string): DocResolution;
|
|
29
|
+
/** Up to 5 ids similar to the input, ranked by where it matches. */
|
|
30
|
+
private suggest;
|
|
31
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file belongs to Hoist, an application development toolkit
|
|
3
|
+
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
|
|
4
|
+
*
|
|
5
|
+
* Copyright © 2026 Extremely Heavy Industries Inc.
|
|
6
|
+
*/
|
|
7
|
+
export class DocIdResolver {
|
|
8
|
+
entries;
|
|
9
|
+
byId;
|
|
10
|
+
aliases;
|
|
11
|
+
constructor(entries) {
|
|
12
|
+
this.entries = entries;
|
|
13
|
+
this.byId = new Map(entries.map(e => [e.id, e]));
|
|
14
|
+
this.aliases = buildAliasMap(entries);
|
|
15
|
+
}
|
|
16
|
+
resolve(input) {
|
|
17
|
+
const raw = (input ?? '').trim();
|
|
18
|
+
if (!raw)
|
|
19
|
+
return { kind: 'unknown', suggestions: [] };
|
|
20
|
+
const exact = this.byId.get(raw);
|
|
21
|
+
if (exact)
|
|
22
|
+
return { kind: 'exact', entry: exact };
|
|
23
|
+
const cleaned = raw
|
|
24
|
+
.replace(/^\.\//, '')
|
|
25
|
+
.replace(/^\/+/, '')
|
|
26
|
+
.replace(/\/+/g, '/')
|
|
27
|
+
.replace(/\/+$/, '');
|
|
28
|
+
const normalized = (entry) => ({
|
|
29
|
+
kind: 'normalized',
|
|
30
|
+
entry,
|
|
31
|
+
matchedAs: raw
|
|
32
|
+
});
|
|
33
|
+
if (cleaned !== raw) {
|
|
34
|
+
const hit = this.byId.get(cleaned);
|
|
35
|
+
if (hit)
|
|
36
|
+
return normalized(hit);
|
|
37
|
+
}
|
|
38
|
+
const lower = cleaned.toLowerCase(), caseHit = this.entries.find(e => e.id.toLowerCase() === lower);
|
|
39
|
+
if (caseHit)
|
|
40
|
+
return normalized(caseHit);
|
|
41
|
+
const aliasTarget = this.aliases.get(lower), aliasHit = aliasTarget ? this.byId.get(aliasTarget) : null;
|
|
42
|
+
if (aliasHit)
|
|
43
|
+
return normalized(aliasHit);
|
|
44
|
+
const matches = new Set();
|
|
45
|
+
for (const candidate of suffixCandidates(cleaned)) {
|
|
46
|
+
const hit = this.byId.get(candidate);
|
|
47
|
+
if (hit)
|
|
48
|
+
matches.add(hit);
|
|
49
|
+
}
|
|
50
|
+
if (matches.size === 1)
|
|
51
|
+
return normalized([...matches][0]);
|
|
52
|
+
return { kind: 'unknown', suggestions: this.suggest(raw) };
|
|
53
|
+
}
|
|
54
|
+
/** Up to 5 ids similar to the input, ranked by where it matches. */
|
|
55
|
+
suggest(input) {
|
|
56
|
+
const lower = input.toLowerCase();
|
|
57
|
+
if (!lower)
|
|
58
|
+
return [];
|
|
59
|
+
const scored = [];
|
|
60
|
+
for (const { id } of this.entries) {
|
|
61
|
+
const idLower = id.toLowerCase(), segs = idLower
|
|
62
|
+
.replace(/\.md$/, '')
|
|
63
|
+
.replace(/\/readme$/, '')
|
|
64
|
+
.split('/')
|
|
65
|
+
.filter(Boolean), lastSeg = segs.at(-1) ?? '';
|
|
66
|
+
let score = 0;
|
|
67
|
+
if (idLower.includes(lower))
|
|
68
|
+
score += 10;
|
|
69
|
+
if (lastSeg.includes(lower))
|
|
70
|
+
score += 5;
|
|
71
|
+
if (segs.some(s => s.startsWith(lower)))
|
|
72
|
+
score += 3;
|
|
73
|
+
if (score > 0)
|
|
74
|
+
scored.push({ id, score });
|
|
75
|
+
}
|
|
76
|
+
scored.sort((a, b) => b.score - a.score || a.id.length - b.id.length);
|
|
77
|
+
return scored.slice(0, 5).map(s => s.id);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
//------------------------
|
|
81
|
+
// Implementation
|
|
82
|
+
//------------------------
|
|
83
|
+
function suffixCandidates(input) {
|
|
84
|
+
if (input.endsWith('.md')) {
|
|
85
|
+
return [`${input.slice(0, -3)}/README.md`, `docs/${input}`];
|
|
86
|
+
}
|
|
87
|
+
if (input.endsWith('/README'))
|
|
88
|
+
return [`${input}.md`];
|
|
89
|
+
const ret = [
|
|
90
|
+
`${input}.md`,
|
|
91
|
+
`${input}/README.md`,
|
|
92
|
+
`docs/${input}.md`,
|
|
93
|
+
`docs/${input}/README.md`
|
|
94
|
+
], version = /^v(\d+)(?:-upgrade-notes)?$/i.exec(input);
|
|
95
|
+
if (version)
|
|
96
|
+
ret.push(`docs/upgrade-notes/v${version[1]}-upgrade-notes.md`);
|
|
97
|
+
return ret;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Lowercased alias to canonical id. Generated aliases claimed by more than one doc are dropped.
|
|
101
|
+
* Declared aliases then override them, except one that collides with another doc's id, and the
|
|
102
|
+
* first declaration of an alias wins.
|
|
103
|
+
*/
|
|
104
|
+
function buildAliasMap(entries) {
|
|
105
|
+
const candidates = new Map();
|
|
106
|
+
for (const e of entries) {
|
|
107
|
+
const canonical = e.id.toLowerCase();
|
|
108
|
+
for (const alias of pathAliasesFor(e.id)) {
|
|
109
|
+
const lower = alias.toLowerCase();
|
|
110
|
+
if (lower === canonical)
|
|
111
|
+
continue;
|
|
112
|
+
let ids = candidates.get(lower);
|
|
113
|
+
if (!ids)
|
|
114
|
+
candidates.set(lower, (ids = new Set()));
|
|
115
|
+
ids.add(e.id);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const ret = new Map();
|
|
119
|
+
for (const [key, ids] of candidates) {
|
|
120
|
+
if (ids.size === 1)
|
|
121
|
+
ret.set(key, [...ids][0]);
|
|
122
|
+
}
|
|
123
|
+
const canonicalIds = new Set(entries.map(e => e.id.toLowerCase())), claimed = new Set();
|
|
124
|
+
for (const e of entries) {
|
|
125
|
+
for (const alias of e.aliases) {
|
|
126
|
+
const lower = alias.toLowerCase();
|
|
127
|
+
if (canonicalIds.has(lower) && lower !== e.id.toLowerCase())
|
|
128
|
+
continue;
|
|
129
|
+
if (claimed.has(lower))
|
|
130
|
+
continue;
|
|
131
|
+
claimed.add(lower);
|
|
132
|
+
ret.set(lower, e.id);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return ret;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Shortenings of a doc path, e.g. `cmp/grid/README.md` gives `cmp/grid/README`, `cmp/grid` and
|
|
139
|
+
* `grid`; `docs/upgrade-notes/v85-upgrade-notes.md` also gives `v85`.
|
|
140
|
+
*/
|
|
141
|
+
function pathAliasesFor(path) {
|
|
142
|
+
const ret = new Set(), noMd = path.endsWith('.md') ? path.slice(0, -3) : path, noReadme = noMd.endsWith('/README') ? noMd.slice(0, -'/README'.length) : noMd;
|
|
143
|
+
ret.add(noMd);
|
|
144
|
+
ret.add(noReadme);
|
|
145
|
+
const lastSeg = noReadme.split('/').filter(Boolean).at(-1);
|
|
146
|
+
if (lastSeg)
|
|
147
|
+
ret.add(lastSeg);
|
|
148
|
+
if (noMd.startsWith('docs/')) {
|
|
149
|
+
const stripped = noMd.slice('docs/'.length);
|
|
150
|
+
ret.add(stripped);
|
|
151
|
+
const docsLast = stripped.split('/').filter(Boolean).at(-1);
|
|
152
|
+
if (docsLast)
|
|
153
|
+
ret.add(docsLast);
|
|
154
|
+
}
|
|
155
|
+
const version = /\/v(\d+)-upgrade-notes\.md$/.exec(path);
|
|
156
|
+
if (version)
|
|
157
|
+
ret.add(`v${version[1]}`);
|
|
158
|
+
return [...ret];
|
|
159
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type DocSection } from './doc-sections.ts';
|
|
2
|
+
import type { DocEntry } from './DocEntry.ts';
|
|
3
|
+
/** A single ranked section. */
|
|
4
|
+
export interface DocSearchResult {
|
|
5
|
+
entry: DocEntry;
|
|
6
|
+
section: DocSection;
|
|
7
|
+
/** BM25+ relevance score. Comparable only within a single result set. */
|
|
8
|
+
score: number;
|
|
9
|
+
/** Leading text of the section, trimmed to a sentence boundary. */
|
|
10
|
+
excerpt: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const DEFAULT_SEARCH_LIMIT = 5, MAX_SEARCH_LIMIT = 10;
|
|
13
|
+
export declare class DocSearch {
|
|
14
|
+
private readonly miniSearch;
|
|
15
|
+
/** Indexed sections by MiniSearch document id. */
|
|
16
|
+
private readonly sections;
|
|
17
|
+
/** Processed terms from each doc's title, description, and keywords. */
|
|
18
|
+
private readonly metaTerms;
|
|
19
|
+
private readonly weights;
|
|
20
|
+
/** Indexed compound terms (`showfeedbackdialog`) mapped to their leading part (`show`). */
|
|
21
|
+
private readonly compoundHeads;
|
|
22
|
+
/** Index one library's docs - anything with a `docs` list, such as a LibraryIndex. */
|
|
23
|
+
constructor(library: {
|
|
24
|
+
docs: DocEntry[];
|
|
25
|
+
});
|
|
26
|
+
/** Ranked sections, as the MCP tools return them: at most 10, and two per doc. */
|
|
27
|
+
search(query: string, category?: string | null, limit?: number | null): DocSearchResult[];
|
|
28
|
+
/**
|
|
29
|
+
* Ranked docs for docs viewers: each doc once, represented by its best-matching section.
|
|
30
|
+
* Uses the same ranking as {@link search}.
|
|
31
|
+
*/
|
|
32
|
+
searchDocs(query: string, limit?: number): DocSearchResult[];
|
|
33
|
+
private rank;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Leading text of a section, trimmed to about {@link EXCERPT_CHARS} at a sentence boundary.
|
|
37
|
+
* Blank lines, TOC rows, table rules, and link definitions are dropped and whitespace collapsed.
|
|
38
|
+
* Reading stops at the first code block once there is enough prose to describe the section -
|
|
39
|
+
* code is included only when the section leads with it.
|
|
40
|
+
*/
|
|
41
|
+
export declare function makeExcerpt(body: string): string;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file belongs to Hoist, an application development toolkit
|
|
3
|
+
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
|
|
4
|
+
*
|
|
5
|
+
* Copyright © 2026 Extremely Heavy Industries Inc.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Section-level ranked search over one library's docs.
|
|
9
|
+
*
|
|
10
|
+
* Every doc is split into sections (see `doc-sections.ts`) and indexed with MiniSearch (BM25+).
|
|
11
|
+
* Each section is indexed on two fields: its heading breadcrumb (`Doc title > H2 > H3`, boosted)
|
|
12
|
+
* and its own body text. A doc's `title`, `description`, and `keywords` act as a doc-level boost
|
|
13
|
+
* for terms they contain.
|
|
14
|
+
*
|
|
15
|
+
* Text processing (camelCase splitting, stop words, stemming) and the ranking helpers are shared
|
|
16
|
+
* with symbol search - see `search-text.ts`. Queries use prefix matching and light fuzziness,
|
|
17
|
+
* combined with OR and re-ranked by the share of query terms each section matches.
|
|
18
|
+
*/
|
|
19
|
+
import MiniSearch, {} from 'minisearch';
|
|
20
|
+
import { LINK_DEF_RE, stripLinks } from './doc-sections.js';
|
|
21
|
+
import { createTermProcessor, isRedundantCompoundMatch, queryTerms, rankByCoverage, tokenize } from './search-text.js';
|
|
22
|
+
export const DEFAULT_SEARCH_LIMIT = 5, MAX_SEARCH_LIMIT = 10;
|
|
23
|
+
//------------------------
|
|
24
|
+
// Tuning
|
|
25
|
+
//------------------------
|
|
26
|
+
/** At most this many sections from one doc per MCP result set. */
|
|
27
|
+
const MAX_PER_DOC = 2;
|
|
28
|
+
/** Target excerpt length in characters (~90 tokens). */
|
|
29
|
+
const EXCERPT_CHARS = 360;
|
|
30
|
+
/** Field boosts. The breadcrumb is short and highly descriptive. */
|
|
31
|
+
const FIELD_BOOST = { heading: 3, body: 1 };
|
|
32
|
+
/** Multiplier for a term that also appears in the doc's registry metadata. */
|
|
33
|
+
const DOC_META_BOOST = 1.3;
|
|
34
|
+
/**
|
|
35
|
+
* Per-doc score multipliers. Upgrade notes and the doc index mention nearly every API but are
|
|
36
|
+
* rarely the best answer to a how-to query, so they rank below the primary docs.
|
|
37
|
+
*/
|
|
38
|
+
function docWeight(entry) {
|
|
39
|
+
if (entry.id.startsWith('docs/upgrade-notes/'))
|
|
40
|
+
return 0.5;
|
|
41
|
+
// The old MCP server's own README documents tooling, not app development.
|
|
42
|
+
if (entry.id === 'mcp/README.md')
|
|
43
|
+
return 0.5;
|
|
44
|
+
if (entry.mcpCategory === 'index')
|
|
45
|
+
return 0.6;
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
export class DocSearch {
|
|
49
|
+
miniSearch;
|
|
50
|
+
/** Indexed sections by MiniSearch document id. */
|
|
51
|
+
sections = [];
|
|
52
|
+
/** Processed terms from each doc's title, description, and keywords. */
|
|
53
|
+
metaTerms = new Map();
|
|
54
|
+
weights = new Map();
|
|
55
|
+
/** Indexed compound terms (`showfeedbackdialog`) mapped to their leading part (`show`). */
|
|
56
|
+
compoundHeads;
|
|
57
|
+
/** Index one library's docs - anything with a `docs` list, such as a LibraryIndex. */
|
|
58
|
+
constructor(library) {
|
|
59
|
+
const { processTerm, compoundHeads } = createTermProcessor(), docs = [];
|
|
60
|
+
this.compoundHeads = compoundHeads;
|
|
61
|
+
this.miniSearch = new MiniSearch({
|
|
62
|
+
fields: ['heading', 'body'],
|
|
63
|
+
tokenize,
|
|
64
|
+
processTerm
|
|
65
|
+
});
|
|
66
|
+
for (const entry of library.docs) {
|
|
67
|
+
this.weights.set(entry.id, docWeight(entry));
|
|
68
|
+
this.metaTerms.set(entry.id, new Set(queryTerms([entry.title, entry.description, ...entry.keywords].join(' '))));
|
|
69
|
+
for (const section of entry.sections) {
|
|
70
|
+
const body = indexableBody(section.body);
|
|
71
|
+
// Skip sections with no text of their own (e.g. a `##` that only groups `###`s) -
|
|
72
|
+
// their children carry the parent heading in their breadcrumbs.
|
|
73
|
+
if (body.replace(/[^\p{L}\p{N}]/gu, '').length < 20)
|
|
74
|
+
continue;
|
|
75
|
+
const id = this.sections.length;
|
|
76
|
+
this.sections.push({ section, entry });
|
|
77
|
+
docs.push({
|
|
78
|
+
id,
|
|
79
|
+
heading: [section.breadcrumb, ...subHeadings(body)].join(' '),
|
|
80
|
+
body
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.miniSearch.addAll(docs);
|
|
85
|
+
}
|
|
86
|
+
/** Ranked sections, as the MCP tools return them: at most 10, and two per doc. */
|
|
87
|
+
search(query, category, limit) {
|
|
88
|
+
const max = Math.min(Math.max(limit ?? DEFAULT_SEARCH_LIMIT, 1), MAX_SEARCH_LIMIT);
|
|
89
|
+
return this.rank(query, category, max, MAX_PER_DOC);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Ranked docs for docs viewers: each doc once, represented by its best-matching section.
|
|
93
|
+
* Uses the same ranking as {@link search}.
|
|
94
|
+
*/
|
|
95
|
+
searchDocs(query, limit = 25) {
|
|
96
|
+
return this.rank(query, null, Math.max(limit, 1), 1);
|
|
97
|
+
}
|
|
98
|
+
rank(query, category, max, maxPerDoc) {
|
|
99
|
+
const terms = queryTerms(query);
|
|
100
|
+
if (!terms.length)
|
|
101
|
+
return [];
|
|
102
|
+
const cat = category && category !== 'all' ? category : null, querySet = new Set(terms), opts = {
|
|
103
|
+
boost: FIELD_BOOST,
|
|
104
|
+
prefix: term => term.length >= 3,
|
|
105
|
+
fuzzy: term => (term.length >= 6 ? 0.2 : false),
|
|
106
|
+
maxFuzzy: 2,
|
|
107
|
+
combineWith: 'OR',
|
|
108
|
+
tokenize: s => s.split(' '),
|
|
109
|
+
processTerm: t => t,
|
|
110
|
+
boostDocument: (id, term) => {
|
|
111
|
+
if (isRedundantCompoundMatch(this.compoundHeads, term, terms, querySet)) {
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
const docId = this.sections[id].entry.id;
|
|
115
|
+
return (this.weights.get(docId) *
|
|
116
|
+
(this.metaTerms.get(docId)?.has(term) ? DOC_META_BOOST : 1));
|
|
117
|
+
},
|
|
118
|
+
filter: cat ? hit => this.sections[hit.id].entry.mcpCategory === cat : undefined
|
|
119
|
+
}, ranked = rankByCoverage(this.miniSearch.search(terms.join(' '), opts), terms.length);
|
|
120
|
+
const ret = [], perDoc = new Map();
|
|
121
|
+
for (const { hit, score } of ranked) {
|
|
122
|
+
if (ret.length >= max)
|
|
123
|
+
break;
|
|
124
|
+
const { section, entry } = this.sections[hit.id], count = perDoc.get(entry.id) ?? 0;
|
|
125
|
+
if (count >= maxPerDoc)
|
|
126
|
+
continue;
|
|
127
|
+
perDoc.set(entry.id, count + 1);
|
|
128
|
+
ret.push({ entry, section, score, excerpt: makeExcerpt(section.body) });
|
|
129
|
+
}
|
|
130
|
+
return ret;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//------------------------
|
|
134
|
+
// Text processing
|
|
135
|
+
//------------------------
|
|
136
|
+
const TOC_ROW_RE = /^\s*\|\s*\[[^\]]+\]\(#[^)]*\)\s*\|/, TABLE_RULE_RE = /^\s*\|?[\s:|-]+\|?\s*$/, FENCE_RE = /^\s*(```|~~~)/, SUBHEADING_RE = /^#{4,6}\s+(.+)$/gm;
|
|
137
|
+
/** Section body with table-of-contents rows removed - they match every query and answer none. */
|
|
138
|
+
function indexableBody(body) {
|
|
139
|
+
return body
|
|
140
|
+
.split('\n')
|
|
141
|
+
.filter(line => !TOC_ROW_RE.test(line))
|
|
142
|
+
.join('\n');
|
|
143
|
+
}
|
|
144
|
+
/** `####`-and-deeper headings within a body, indexed with the (boosted) heading field. */
|
|
145
|
+
function subHeadings(body) {
|
|
146
|
+
return [...body.matchAll(SUBHEADING_RE)].map(m => m[1]);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Leading text of a section, trimmed to about {@link EXCERPT_CHARS} at a sentence boundary.
|
|
150
|
+
* Blank lines, TOC rows, table rules, and link definitions are dropped and whitespace collapsed.
|
|
151
|
+
* Reading stops at the first code block once there is enough prose to describe the section -
|
|
152
|
+
* code is included only when the section leads with it.
|
|
153
|
+
*/
|
|
154
|
+
export function makeExcerpt(body) {
|
|
155
|
+
const kept = [];
|
|
156
|
+
let inFence = false, length = 0;
|
|
157
|
+
for (const raw of body.split('\n')) {
|
|
158
|
+
const line = raw.trim();
|
|
159
|
+
if (FENCE_RE.test(line)) {
|
|
160
|
+
if (!inFence && length >= EXCERPT_CHARS / 3)
|
|
161
|
+
break;
|
|
162
|
+
inFence = !inFence;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (!line || TOC_ROW_RE.test(line) || TABLE_RULE_RE.test(line) || LINK_DEF_RE.test(line)) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
kept.push(stripLinks(line.replace(/^#{1,6}\s+/, '')));
|
|
169
|
+
length += line.length;
|
|
170
|
+
if (length > EXCERPT_CHARS)
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
const text = kept.join(' ').replace(/\s+/g, ' ');
|
|
174
|
+
if (text.length <= EXCERPT_CHARS)
|
|
175
|
+
return text;
|
|
176
|
+
const cut = text.slice(0, EXCERPT_CHARS), sentenceEnds = [...cut.matchAll(/[.!?:](?=\s)/g)], lastSentence = sentenceEnds.at(-1)?.index ?? -1;
|
|
177
|
+
if (lastSentence >= EXCERPT_CHARS / 2)
|
|
178
|
+
return cut.slice(0, lastSentence + 1);
|
|
179
|
+
return cut.slice(0, cut.lastIndexOf(' ')) + '...';
|
|
180
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { DocEntry } from './DocEntry.ts';
|
|
2
|
+
import { DocIdResolver } from './DocIdResolver.ts';
|
|
3
|
+
import { DocSearch } from './DocSearch.ts';
|
|
4
|
+
import { SymbolSearch } from './SymbolSearch.ts';
|
|
5
|
+
import { type Category, type IndexFile, type MemberIndexEntry, type SymbolEntry, type SymbolKind } from './types.ts';
|
|
6
|
+
/**
|
|
7
|
+
* One library version's index file, loaded into memory: its docs, symbols with their members,
|
|
8
|
+
* and the member index. Search indexes over these are built on first use. See
|
|
9
|
+
* docs/index-schema.md for the file format.
|
|
10
|
+
*/
|
|
11
|
+
export { DocEntry };
|
|
12
|
+
export declare class LibraryIndex {
|
|
13
|
+
/** Library id used in tool arguments: `react`, `core`, or a companion id. */
|
|
14
|
+
readonly id: string;
|
|
15
|
+
/** Package name, e.g. `@xh/hoist`. */
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly version: string;
|
|
18
|
+
readonly language: string;
|
|
19
|
+
readonly gitSha: string | null;
|
|
20
|
+
readonly builtAt: string;
|
|
21
|
+
readonly description: string | null;
|
|
22
|
+
/** Display name from the built-in library table (`Hoist Chat Kit`), else null. */
|
|
23
|
+
readonly label: string | null;
|
|
24
|
+
readonly instructions: string | null;
|
|
25
|
+
readonly mcpCategories: Category[];
|
|
26
|
+
readonly viewerCategories: Category[];
|
|
27
|
+
readonly docs: DocEntry[];
|
|
28
|
+
readonly docsById: Map<string, DocEntry>;
|
|
29
|
+
readonly symbols: SymbolEntry[];
|
|
30
|
+
readonly memberIndex: MemberIndexEntry[];
|
|
31
|
+
readonly resolver: DocIdResolver;
|
|
32
|
+
/** Symbols keyed by lowercase name, in file order. */
|
|
33
|
+
private readonly symbolsByKey;
|
|
34
|
+
private _docSearch;
|
|
35
|
+
private _symbolSearch;
|
|
36
|
+
/**
|
|
37
|
+
* @param opts.trusted - false to drop the index's agent instructions, for an index from outside
|
|
38
|
+
* the `@xh` scope. Default true.
|
|
39
|
+
*/
|
|
40
|
+
constructor(json: IndexFile, opts?: {
|
|
41
|
+
trusted?: boolean;
|
|
42
|
+
});
|
|
43
|
+
/** Read an index file, gzipped when its name ends in `.gz`. */
|
|
44
|
+
static load(path: string, opts?: {
|
|
45
|
+
trusted?: boolean;
|
|
46
|
+
}): LibraryIndex;
|
|
47
|
+
/** An index file's raw contents, gzipped when its name ends in `.gz`. */
|
|
48
|
+
static readJson(path: string): IndexFile;
|
|
49
|
+
get isTypeScript(): boolean;
|
|
50
|
+
get docSearch(): DocSearch;
|
|
51
|
+
get symbolSearch(): SymbolSearch;
|
|
52
|
+
/** Exact-name lookup, optionally narrowed by file and kind. Prefers exported symbols. */
|
|
53
|
+
findSymbol(name: string, filePath?: string | null, kind?: string | null): SymbolEntry | null;
|
|
54
|
+
/** Every declaration of a name in the file it resolves to, primary first. */
|
|
55
|
+
findDeclarations(name: string, filePath?: string | null, kind?: SymbolKind | null): SymbolEntry[];
|
|
56
|
+
/** Other exported symbols with this name, in other files. */
|
|
57
|
+
findAlternates(name: string, selectedFilePath: string): SymbolEntry[];
|
|
58
|
+
/** For a Props interface, its component consts; for a component const, its Props interface. */
|
|
59
|
+
findCompanions(entry: SymbolEntry): SymbolEntry[];
|
|
60
|
+
/** Symbols with exactly this name, in file order. */
|
|
61
|
+
private named;
|
|
62
|
+
}
|