({
repoData,
env,
ctx,
}: {
repoData: RepoData;
env: CloudflareEnvironment;
ctx: any;
})
| 24 | |
| 25 | // Add env parameter to access Cloudflare's bindings |
| 26 | export async function fetchDocumentation({ |
| 27 | repoData, |
| 28 | env, |
| 29 | ctx, |
| 30 | }: { |
| 31 | repoData: RepoData; |
| 32 | env: CloudflareEnvironment; |
| 33 | ctx: any; |
| 34 | }): Promise<FetchDocumentationResult> { |
| 35 | const { owner, repo, urlType } = repoData; |
| 36 | const cacheTTL = 30 * 60; // 30 minutes in seconds |
| 37 | |
| 38 | // Try fetching from cache first |
| 39 | if (owner && repo) { |
| 40 | const cachedResult = await getCachedFetchDocResult(owner, repo, env); |
| 41 | if (cachedResult) { |
| 42 | console.log( |
| 43 | `Returning cached fetchDocumentation result for ${owner}/${repo}`, |
| 44 | ); |
| 45 | return cachedResult; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Initialize fileUsed to prevent "used before assigned" error |
| 50 | let fileUsed = "unknown"; |
| 51 | let content: string | null = null; |
| 52 | let docsPath: string = ""; |
| 53 | let docsBranch: string = ""; |
| 54 | let blockedByRobots = false; |
| 55 | |
| 56 | // Check for subdomain pattern: {subdomain}.gitmcp.io/{path} |
| 57 | if (urlType === "subdomain") { |
| 58 | // Map to github.io |
| 59 | const githubIoDomain = `${owner}.github.io`; |
| 60 | const pathWithSlash = repo ? `/${repo}` : ""; |
| 61 | const baseURL = `https://${githubIoDomain}${pathWithSlash}/`; |
| 62 | |
| 63 | // Try to fetch llms.txt with robots.txt check |
| 64 | const llmsResult = await fetchFileWithRobotsTxtCheck( |
| 65 | baseURL + "llms.txt", |
| 66 | env, |
| 67 | ); |
| 68 | |
| 69 | if (llmsResult.blockedByRobots) { |
| 70 | blockedByRobots = true; |
| 71 | console.log(`Access to ${baseURL}llms.txt disallowed by robots.txt`); |
| 72 | } else if (llmsResult.content) { |
| 73 | content = llmsResult.content; |
| 74 | fileUsed = "llms.txt"; |
| 75 | } else { |
| 76 | // If llms.txt is not found or disallowed, fall back to the landing page |
| 77 | console.warn( |
| 78 | `llms.txt not found or not allowed at ${baseURL}, trying base URL`, |
| 79 | ); |
| 80 | const indexResult = await fetchFileWithRobotsTxtCheck(baseURL, env); |
| 81 | |
| 82 | if (indexResult.blockedByRobots) { |
| 83 | blockedByRobots = true; |
no test coverage detected