import { z } from "zod";
import { searchDocsIndex, SECTIONS, type SectionId } from "../data/docs-index.js";
export const searchDocsSchema = {
query: z.string().describe("Search query to find in documentation"),
section: z
.enum(["all", "cli", "headless", "framework", "mdx", "ui"])
.optional()
.describe("Filter results to a specific documentation section"),
};
export type SearchDocsParams = {
query: string;
section?: "all" | SectionId;
};
export async function searchDocs(params: SearchDocsParams): Promise<string> {
const { query, section } = params;
if (!query || query.trim().length === 0) {
return "Error: Please provide a search query.";
}
const sectionFilter = section === "all" ? undefined : (section as SectionId);
const results = searchDocsIndex(query, sectionFilter);
if (results.length === 0) {
const suggestions = [
"No results found for: " + query,
"",
"Suggestions:",
"- Try different keywords",
"- Use more general terms",
"- Check spelling",
"",
"Popular topics:",
"- 'manual installation' - Setup guides for existing projects",
"- 'components' - UI component documentation",
"- 'search' - Search implementation",
"- 'mdx' - MDX content source setup",
];
return suggestions.join("\n");
}
const output: string[] = [
`# Search Results for "${query}"`,
`Found ${results.length} result${results.length === 1 ? "" : "s"}${sectionFilter ? ` in ${SECTIONS[sectionFilter]}` : ""}:\n`,
];
// Limit to top 15 results
const topResults = results.slice(0, 15);
for (const entry of topResults) {
output.push(`## ${entry.title}`);
output.push(`**Section:** ${SECTIONS[entry.section as SectionId]} | **Path:** ${entry.path}`);
output.push(`${entry.description}\n`);
}
if (results.length > 15) {
output.push(`\n---\n*Showing top 15 of ${results.length} results. Refine your search for more specific results.*`);
}
output.push("\n---");
output.push("Use `get_page` with a path to fetch the full content of a documentation page.");
return output.join("\n");
}