-
Notifications
You must be signed in to change notification settings - Fork 854
DC-5820 AI Agents Served Markdown #7237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6dda56a
ai crawler check successful
aidankmcalister 5c72050
ai crawlers checked
aidankmcalister daf7d45
Merge branch 'main' into DC-5820ai-agents-serve-md
aidankmcalister 3fc91b3
update
aidankmcalister 4cf6736
added anthropic
aidankmcalister 68c23d9
middleware update
aidankmcalister 8d328be
improve detection
mhartington File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| interface Env { | ||
| ASSETS: { | ||
| fetch: typeof fetch; | ||
| }; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const AI_CRAWLER_PATTERNS = [ | ||
| 'GPTBot', | ||
| 'OAI-SearchBot', | ||
| 'ChatGPT-User', | ||
| 'ChatGPT-User 2.0', | ||
| 'anthropic-ai', | ||
| 'OpenAI', | ||
| 'ClaudeBot', | ||
| 'claude-web', | ||
| 'Claude/', | ||
| 'Claude-User', | ||
| 'anthropic', | ||
| 'PerplexityBot', | ||
| 'Perplexity-User', | ||
| 'Google-Extended', | ||
| 'BingBot', | ||
| 'Amazonbot', | ||
| 'Applebot', | ||
| 'Applebot-Extended', | ||
| 'FacebookBot', | ||
| 'meta-externalagent', | ||
| 'LinkedInBot', | ||
| 'Bytespider', | ||
| 'AI2Bot', | ||
| 'CCBot', | ||
| 'Diffbot', | ||
| 'DuckAssistBot', | ||
| 'DuckDuckGo', | ||
| 'DeepSeekBot', | ||
| 'cohere-ai', | ||
| 'omgili', | ||
| 'TimpiBot', | ||
| 'YouBot', | ||
| 'MistralAI-User', | ||
| 'ImagesiftBot', | ||
| 'Scrapy', | ||
|
|
||
| 'chatgpt', | ||
| 'openai', | ||
| 'gpt', | ||
| 'claude', | ||
| 'anthropic', | ||
| 'cursor', | ||
| 'windsurf', | ||
| 'perplexity', | ||
| 'copilot', | ||
| 'ai-agent', | ||
| 'llm-agent', | ||
| ]; | ||
|
|
||
| function isAICrawler(request: Request): boolean { | ||
| // if (!userAgent) return false; | ||
|
|
||
| const userAgent = request.headers.get('user-agent') || ''; | ||
| const accept = request.headers.get('accept') || ''; | ||
|
|
||
| const hasHtml = accept.includes('text/html'); | ||
|
|
||
| const prefersNonHtml = | ||
| !hasHtml && | ||
| (accept.includes('application/json') || | ||
| accept.includes('text/plain') || | ||
| accept.includes('application/xml')); | ||
|
|
||
| const hasAiUserAgent = AI_CRAWLER_PATTERNS.some((pattern) => userAgent.toLowerCase().includes(pattern.toLowerCase())); | ||
|
|
||
| return prefersNonHtml || hasAiUserAgent | ||
|
|
||
| // const normalizedUA = userAgent.toLowerCase(); | ||
| // return AI_CRAWLER_PATTERNS.some((pattern) => normalizedUA.includes(pattern.toLowerCase())); | ||
| } | ||
|
|
||
| export const onRequest: PagesFunction<Env> = async (context) => { | ||
| const { request, next } = context; | ||
|
|
||
| if (isAICrawler(request)) { | ||
| const url = new URL(request.url); | ||
| const urlPath = url.pathname; | ||
|
|
||
| const markdownPath = `${urlPath}.md`; | ||
|
|
||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const markdownRequest = new Request(new URL(markdownPath, url.origin), { | ||
| method: request.method, | ||
| headers: request.headers, | ||
| }); | ||
|
|
||
| const response = await context.env.ASSETS.fetch(markdownRequest); | ||
|
|
||
| if (response.ok) { | ||
| // Check what content type we actually got | ||
| const actualContentType = response.headers.get('content-type'); | ||
| console.log(`Fetched ${markdownPath}, got content-type: ${actualContentType}`); | ||
|
|
||
| return new Response(response.body, { | ||
| status: 200, | ||
| headers: { | ||
| 'Content-Type': 'text/markdown; charset=utf-8', | ||
| 'Cache-Control': 'public, max-age=3600', | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| return next(); | ||
| } | ||
|
|
||
| return next(); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // TypeScript definitions for Cloudflare Pages Functions | ||
| interface EventContext<Env = unknown> { | ||
| request: Request; | ||
| env: Env; | ||
| params: Record<string, string>; | ||
| data: Record<string, unknown>; | ||
| next: () => Promise<Response>; | ||
| waitUntil: (promise: Promise<unknown>) => void; | ||
| passThroughOnException: () => void; | ||
| } | ||
|
|
||
| type PagesFunction<Env = unknown> = (context: EventContext<Env>) => Response | Promise<Response>; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.