import { z } from "zod";
import { fetchPage, FumadocsError } from "../services/docs-fetcher.js";
export const getPageSchema = {
path: z
.string()
.describe("Documentation path (e.g., '/docs/manual-installation/next' or '/docs/ui/components/tabs')"),
};
export type GetPageParams = {
path: string;
};
export async function getPage(params: GetPageParams): Promise<string> {
const { path } = params;
if (!path || path.trim().length === 0) {
return "Error: Please provide a documentation path.";
}
// Normalize path
let normalizedPath = path.trim();
if (!normalizedPath.startsWith("/")) {
normalizedPath = "/" + normalizedPath;
}
if (!normalizedPath.startsWith("/docs")) {
normalizedPath = "/docs" + normalizedPath;
}
try {
const content = await fetchPage(normalizedPath);
if (!content || content.trim().length === 0) {
return `No content found at path: ${normalizedPath}\n\nTry using \`search_docs\` to find the correct path.`;
}
const output: string[] = [
`# Documentation: ${normalizedPath}\n`,
"---\n",
content,
"\n---",
`\nSource: https://fumadocs.vercel.app${normalizedPath}`,
];
return output.join("\n");
} catch (error) {
if (error instanceof FumadocsError) {
if (error.code === "PAGE_NOT_FOUND") {
return [
`Page not found: ${normalizedPath}`,
"",
"The requested documentation page does not exist.",
"",
"Suggestions:",
"- Check if the path is correct",
"- Use `search_docs` to find the right page",
"- Use `list_topics` to browse available pages",
].join("\n");
}
return `Error fetching page: ${error.message} (${error.code})`;
}
return `Unexpected error fetching page: ${error instanceof Error ? error.message : "Unknown error"}`;
}
}