@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

main
ts 70 lines 2.86 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5
6 const page = (title: string, body: string) =>
7 HttpServerResponse.html(
8 `<!doctype html><html><head><title>${title}</title></head>` + `<body>${body}</body></html>`,
9 );
10
11 /**
12 * A tiny Worker that serves a small crawlable site. Its `workers.dev` URL is a
13 * domain the account owns, so it can seed an AI Search web-crawler instance
14 * (which rejects domains the account hasn't verified).
15 *
16 * It serves both a `robots.txt` (advertising the sitemap) and a `sitemap.xml`
17 * listing every page, plus inline links between pages. Cloudflare validates a
18 * web-crawler seed synchronously at create time and rejects with
19 * `missing_sitemap` if it finds no content — which happened intermittently
20 * when relying on link-discovery alone against a freshly-deployed
21 * `workers.dev` URL. Serving a real sitemap (and `robots.txt` pointing at it)
22 * makes the seed valid no matter which discovery path Cloudflare takes.
23 */
24 export default class AiSearchCrawlTargetWorker extends Cloudflare.Worker<AiSearchCrawlTargetWorker>()(
25 "AiSearchCrawlTargetWorker",
26 {
27 main: import.meta.url,
28 },
29 Effect.gen(function* () {
30 return {
31 fetch: Effect.gen(function* () {
32 const request = yield* HttpServerRequest;
33 // `HttpServerRequest.url` is the request path (e.g. `/docs`), not an
34 // absolute URL — `new URL(request.url)` would throw and 500. Match on
35 // the path prefix directly. Build absolute URLs from the `Host`
36 // header so the sitemap works behind the dynamic `workers.dev` host.
37 const host = request.headers.host ?? "localhost";
38 const origin = `https://${host}`;
39
40 if (request.url.startsWith("/robots.txt")) {
41 return HttpServerResponse.text(
42 `User-agent: *\nAllow: /\nSitemap: ${origin}/sitemap.xml\n`,
43 );
44 }
45
46 if (request.url.startsWith("/sitemap.xml")) {
47 return HttpServerResponse.text(
48 `<?xml version="1.0" encoding="UTF-8"?>` +
49 `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
50 `<url><loc>${origin}/</loc></url>` +
51 `<url><loc>${origin}/docs</loc></url>` +
52 `</urlset>`,
53 { headers: { "content-type": "application/xml" } },
54 );
55 }
56
57 if (request.url.startsWith("/docs")) {
58 return page("Docs", "<h1>Alchemy docs</h1><p>Indexable documentation content.</p>");
59 }
60
61 return page(
62 "Crawl Target",
63 "<h1>Alchemy AI Search crawl target</h1>" +
64 "<p>This page exists so AI Search has something to index.</p>" +
65 '<p><a href="/docs">Docs</a></p>',
66 );
67 }),
68 };
69 }),
70 ) {}