import { getFromCache, decodeContentHandle } from "../lib/cache.js";
interface FetchFullContentParams {
contentHandle: string;
outputFormat?: "markdown" | "text" | "html";
}
interface FetchResult {
url: string;
title: string;
markdown?: string;
text?: string;
html?: string;
contentLength?: number;
cached?: boolean;
error?: string;
}
export async function fetchFullContent(
params: FetchFullContentParams
): Promise<FetchResult> {
const { contentHandle, outputFormat = "markdown" } = params;
const url = decodeContentHandle(contentHandle);
if (!url) {
return {
url: "",
title: "",
error: "Invalid contentHandle format. Ensure it was generated by rag() tool.",
};
}
const cached = getFromCache(url);
if (!cached) {
return {
url,
title: "",
error: `Content not found in cache. Cache TTL is 1 hour. Please run rag() again to re-fetch and cache the content.`,
};
}
const result: FetchResult = {
url: cached.url,
title: cached.title,
cached: true,
contentLength:
outputFormat === "markdown"
? cached.markdown.length
: cached.content.length,
};
if (outputFormat === "markdown") {
result.markdown = cached.markdown;
} else if (outputFormat === "text") {
result.text = cached.content;
} else if (outputFormat === "html") {
result.html = cached.content;
}
return result;
}