import index from "./index.html"; async function generateRule(env) { const simple = { prompt: 'Give me a rule for a Discord server that is surreal, bizzare, and outlandish while still being somewhat realistic on first glance. Keep it short and succinct. Do not add any additional information, greetings, or quotation marks.' }; return await env.AI.run('@cf/meta/llama-3.1-8b-instruct', simple); } export default { async scheduled(event, env, ctx) { const { versionId, versionTag, versionTimestamp } = env.CF_VERSION_METADATA; const rule = await generateRule(env); console.log(`generated rule ${JSON.stringify(rule)}`); ctx.waitUntil(env.LOG.put("LATEST", JSON.stringify(rule))); ctx.waitUntil(env.LOG.put(Date.now(), JSON.stringify(rule), { expirationTtl: 31_536_000 })); payload = { status: `The secret third rule of the day is\n\n>${rule.response}`, content_type: 'text/markdown', }; ctx.waitUntil(await fetch(`https://bird.gmem.ca/api/v1/statuses`, { method: "POST", body: JSON.stringify(payload), headers: { "X-Source": "Cloudflare-Workers", "User-Agent": `THIRDRULE ${versionTag}`, "Content-Type": content_type, "Authorization": env.GTS_AUTH }, })); }, async fetch(request, env, ctx) { const url = new URL(request.url); const { pathname } = url; // Construct the cache key from the cache URL const cacheKey = new Request(url.toString(), request); const cache = caches.default; let response = await cache.match(cacheKey); if (response) { return response; } switch (pathname) { case "/": return new Response(index, { headers: { "Content-Type": "text/html" } }); case "/rule.json": const rule = await env.LOG.get("LATEST"); const response = new Response(rule, { headers: { "Content-Type": "application/json", "Cache-Control": "s-maxage=3600"} }); ctx.waitUntil(cache.put(cacheKey, response.clone())); return response; default: return new Response("not found", {status: 404}); } } };