Files
Rich Haines eafa640057 Add AI agent detection and automatic markdown rewrites (#351)
* Add AI agent detection and automatic markdown rewrites

When AI agents (Claude, ChatGPT, Cursor, etc.) request docs pages,
the proxy now detects them and transparently rewrites to the markdown
route — matching the geistdocs template default.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Fix: Missing `detectionMethod` property in `TrackMdRequestParams` type causes TypeScript compilation error and build failure.

This commit fixes the issue reported at apps/docs/lib/geistdocs/md-tracking.ts:24

**Bug explanation:**

The `trackMdRequest` function in `apps/docs/lib/geistdocs/md-tracking.ts` destructures `detectionMethod` from its parameter (line 24) and includes it in the JSON body sent to the tracking endpoint (line 38). However, the `TrackMdRequestParams` type definition (lines 6-12) does not include `detectionMethod` as a property.

In `apps/docs/proxy.ts` (line 82-87), `trackMdRequest` is called with `detectionMethod: agentResult.method` when an AI agent is detected, where `agentResult.method` is of type `DetectionMethod | null` from `@/lib/ai-agent-detection`.

This causes an exact TypeScript compilation error confirmed in the build logs:
```
./lib/geistdocs/md-tracking.ts:24:3
Type error: Property 'detectionMethod' does not exist on type 'TrackMdRequestParams'.
```

This error causes the entire `docs` build to fail (`next build` exits with code 1).

**Fix explanation:**

Added `detectionMethod?: DetectionMethod | null` as an optional property to the `TrackMdRequestParams` type, and added the corresponding `import type { DetectionMethod } from "@/lib/ai-agent-detection"` import at the top of the file. The property is optional (`?`) because most call sites (for `.md` URL tracking, `llms.txt` tracking, and header-negotiated tracking) don't pass a `detectionMethod` — only the agent-rewrite tracking path does. The `| null` union matches the `DetectionResult.method` type from `ai-agent-detection.ts`.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: molebox <hello@richardhaines.dev>

* format

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Dominik Ferber <dominik.ferber@gmail.com>
2026-03-29 02:19:37 +00:00

57 lines
1.4 KiB
TypeScript

import { siteId } from "@/geistdocs";
import type { DetectionMethod } from "@/lib/ai-agent-detection";
const PLATFORM_URL = "https://geistdocs.com/md-tracking";
type TrackMdRequestParams = {
path: string;
userAgent: string | null;
referer: string | null;
acceptHeader: string | null;
/** How the markdown was requested: 'md-url' for direct .md URLs, 'header-negotiated' for Accept header */
requestType?: "md-url" | "header-negotiated" | "agent-rewrite";
/** Which detection method identified the AI agent */
detectionMethod?: DetectionMethod | null;
};
/**
* Track a markdown page request via the geistdocs platform.
* Fire-and-forget: errors are logged but don't affect the response.
*/
export async function trackMdRequest({
path,
userAgent,
referer,
acceptHeader,
requestType,
detectionMethod,
}: TrackMdRequestParams): Promise<void> {
try {
const response = await fetch(PLATFORM_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
path,
siteId: siteId ?? "geistdocs-unknown",
userAgent,
referer,
acceptHeader,
requestType,
detectionMethod,
}),
});
if (!response.ok) {
console.error(
"MD tracking failed:",
response.status,
await response.text()
);
}
} catch (error) {
console.error("MD tracking error:", error);
}
}