From b107181752935b46b6b3d940cd2277363fad609c Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Sun, 28 Jul 2024 15:28:32 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 + index.html | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 43 +++++++++++++ package.json | 4 ++ wrangler.toml | 7 +++ 5 files changed, 227 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 index.js create mode 100644 package.json create mode 100644 wrangler.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41a2578 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.wrangler/ +node_modules/ diff --git a/index.html b/index.html new file mode 100644 index 0000000..9a617ee --- /dev/null +++ b/index.html @@ -0,0 +1,171 @@ + + + + + + + third rule + + + +
+
+

The Secret Third Rule

+
+
+

Generating

+
+ + +
+ + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..1f89c4a --- /dev/null +++ b/index.js @@ -0,0 +1,43 @@ +import index from "./index.html"; + +export default { + async fetch(request, env, ctx) { + const cacheUrl = new URL(request.url); + + // Construct the cache key from the cache URL + const cacheKey = new Request(cacheUrl.toString(), request); + const cache = caches.default; + let response = await cache.match(cacheKey); + if (response) { + return response; + } + + const url = new URL(request.url); + const { pathname } = url; + + switch (pathname) { + case "/": + return new Response(index, { + headers: { "Content-Type": "text/html" } + }); + + case "/rule.json": + 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.' + }; + + const reply = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', simple); + + let response = new Response(JSON.stringify(reply), { + headers: { "Content-Type": "application/json", + "Cache-Control": "s-maxage=43200" } + }); + + ctx.waitUntil(cache.put(cacheKey, response.clone())); + return response; + default: + return new Response("not found", {status: 404}); + } + + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..3931196 --- /dev/null +++ b/package.json @@ -0,0 +1,4 @@ +{ + "name": "thirdrule", + "version": "1.0.0", +} diff --git a/wrangler.toml b/wrangler.toml new file mode 100644 index 0000000..12f87a4 --- /dev/null +++ b/wrangler.toml @@ -0,0 +1,7 @@ +name = "thirdrule" +main = "index.js" +compatibility_date = "2023-08-23" +account_id = "7dc420732ea679a530aee304ea49a63c" + +[ai] +binding = "AI"