-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtanstack-docs.js
More file actions
254 lines (213 loc) · 7.23 KB
/
Copy pathtanstack-docs.js
File metadata and controls
254 lines (213 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* tools/tanstack-docs.js
*
* Fetch TanStack documentation — no API key required.
* TanStack exposes a docs index via llms.txt.
* Covers Query, Router, Table, Form, Virtual, Store, Start, and Config.
*
* Tools:
* - get-index → lists all available TanStack doc pages
* - get-page → fetches a specific doc page as Markdown
* - search-docs → searches across all TanStack docs
*/
const BASE = "https://tanstack.com";
export const tools = [
{
name: "get-index",
description:
"Returns a full index of all TanStack documentation pages. " +
"Use this to discover page paths before calling get-page.",
parameters: {},
},
{
name: "get-page",
description:
"Fetches a specific TanStack doc page as clean Markdown. " +
"Use a path like '/query/latest/docs/overview' or '/router/latest/docs/framework/react/quick-start'.",
parameters: {
path: "string — doc path, e.g. '/query/latest/docs/overview'",
},
},
{
name: "search-docs",
description:
"Searches all TanStack documentation for a keyword or topic. " +
"Good for questions like 'useQuery hooks', 'table column defs', or 'form validation'.",
parameters: {
query: "string — what you're looking for",
maxChars: "number (optional) — max characters to return (default 8000)",
},
},
];
/**
* invoke(toolName, args)
*
* @example
* const { index } = await invoke("get-index");
* const { markdown } = await invoke("get-page", { path: "/query/latest/docs/overview" });
* const { result } = await invoke("search-docs", { query: "useQuery" });
*/
export async function invoke(toolName, args = {}) {
switch (toolName) {
case "get-index":
return getIndex();
case "get-page":
return getPage(args);
case "search-docs":
return searchDocs(args);
default:
throw new Error(`Unknown tanstack-docs tool: "${toolName}"`);
}
}
/**
* Normalize a TanStack doc path:
* - Strips absolute https://tanstack.com prefix
* - Removes .md suffix
* - Ensures leading /
*/
export function normalizeTanstackDocPath(path) {
if (!path) return "";
let normalized = path;
if (normalized.startsWith("http://") || normalized.startsWith("https://")) {
try {
const url = new URL(normalized);
normalized = url.pathname;
} catch {
normalized = normalized.replace(/^https?:\/\/[^/]+/, "");
}
}
normalized = normalized.split(/[?#]/)[0];
normalized = normalized.replace(/\/index\.md$/i, "");
normalized = normalized.replace(/\.md$/i, "");
if (!normalized.startsWith("/")) {
normalized = `/${normalized}`;
}
if (normalized.length > 1 && normalized.endsWith("/")) {
normalized = normalized.slice(0, -1);
}
return normalized;
}
// ─── Internal handlers ────────────────────────────────────────────────────────
async function getIndex() {
let res;
try {
res = await fetch(`${BASE}/llms.txt`);
} catch (err) {
throw new Error(`Network error fetching TanStack index: ${err.message}`);
}
if (!res.ok) {
throw new Error(
res.status === 404
? "TanStack docs index not found (404). The site may have changed its structure."
: `Failed to fetch TanStack llms.txt: ${res.status}`,
);
}
const index = await res.text();
return { index };
}
async function getPage({ path }) {
if (!path) throw new Error("get-page requires `path`");
const cleanPath = normalizeTanstackDocPath(path);
for (const candidateUrl of getPageCandidates(cleanPath)) {
let res;
try {
res = await fetch(candidateUrl, {
headers: { Accept: "text/markdown" },
});
} catch {
continue;
}
if (!res.ok) continue;
const text = await res.text();
if (isHtmlResponse(res.headers.get("content-type"), text)) continue;
return { path: cleanPath, markdown: text };
}
throw new Error(`TanStack page not found: ${path}. Check the path or use get-index to discover available pages.`);
}
async function searchDocs({ query, maxChars = 8000 }) {
if (!query) throw new Error("search-docs requires `query`");
let res;
try {
res = await fetch(`${BASE}/llms.txt`);
} catch (err) {
throw new Error(`Network error loading TanStack docs: ${err.message}`);
}
if (!res.ok) {
throw new Error(
res.status === 404
? "TanStack docs index not found (404). The site may have changed its structure."
: `Could not load TanStack docs index: ${res.status}`,
);
}
const fullText = await res.text();
const scored = [];
for (const match of findRelevantChunks(fullText, query)) {
scored.push(match);
}
scored.sort((a, b) => b.score - a.score);
const seen = new Set();
let result = "";
for (const { chunk } of scored) {
const normalized = chunk.trim();
if (!normalized || seen.has(normalized)) continue;
seen.add(normalized);
const separator = result.length > 0 ? "\n\n---\n\n" : "";
const remaining = maxChars - result.length - separator.length;
if (remaining <= 0) break;
const snippet = normalized.length > remaining
? normalized.slice(0, Math.max(0, remaining - 3)).trimEnd() + "..."
: normalized;
if (!snippet.trim()) continue;
result += separator + snippet;
if (snippet.length < normalized.length) break;
}
return {
query,
found: seen.size,
result: result || "No relevant sections found.",
};
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function getPageCandidates(cleanPath) {
return [
`${BASE}${cleanPath}`,
`${BASE}${cleanPath}.md`,
`${BASE}${cleanPath}/index.md`,
];
}
function findRelevantChunks(text, query) {
const lowerQuery = query.toLowerCase();
const escapedQuery = lowerQuery.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const pattern = new RegExp(escapedQuery, "g");
const lines = text.split("\n");
const sections = [];
let buffer = [];
for (const line of lines) {
if (line.startsWith("#") && buffer.length > 0) {
const chunk = buffer.join("\n");
const score = (chunk.toLowerCase().match(pattern) || []).length;
if (score > 0) sections.push({ score: score * 3, chunk: chunk.trim() });
buffer = [line];
} else {
buffer.push(line);
}
}
if (buffer.length > 0) {
const chunk = buffer.join("\n");
const score = (chunk.toLowerCase().match(pattern) || []).length;
if (score > 0) sections.push({ score: score * 3, chunk: chunk.trim() });
}
if (sections.length > 0) return sections;
const windows = [];
for (let index = 0; index < lines.length; index++) {
if (!lines[index].toLowerCase().includes(lowerQuery)) continue;
const start = Math.max(0, index - 6);
const end = Math.min(lines.length, index + 18);
const chunk = lines.slice(start, end).join("\n").trim();
if (chunk) windows.push({ score: 1, chunk });
}
return windows;
}
function isHtmlResponse(contentType, text) {
return (contentType || "").includes("text/html") || text.trimStart().startsWith("<!DOCTYPE html") || text.includes("<html");
}