Use cron to generate rule, store in KV
This commit is contained in:
parent
b107181752
commit
64243f517e
35
index.js
35
index.js
|
@ -1,20 +1,33 @@
|
||||||
import index from "./index.html";
|
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 {
|
export default {
|
||||||
|
async scheduled(event, env, ctx) {
|
||||||
|
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 }));
|
||||||
|
},
|
||||||
|
|
||||||
async fetch(request, env, ctx) {
|
async fetch(request, env, ctx) {
|
||||||
const cacheUrl = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
const { pathname } = url;
|
||||||
|
|
||||||
// Construct the cache key from the cache URL
|
// Construct the cache key from the cache URL
|
||||||
const cacheKey = new Request(cacheUrl.toString(), request);
|
const cacheKey = new Request(url.toString(), request);
|
||||||
const cache = caches.default;
|
const cache = caches.default;
|
||||||
let response = await cache.match(cacheKey);
|
let response = await cache.match(cacheKey);
|
||||||
if (response) {
|
if (response) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = new URL(request.url);
|
|
||||||
const { pathname } = url;
|
|
||||||
|
|
||||||
switch (pathname) {
|
switch (pathname) {
|
||||||
case "/":
|
case "/":
|
||||||
return new Response(index, {
|
return new Response(index, {
|
||||||
|
@ -22,17 +35,11 @@ export default {
|
||||||
});
|
});
|
||||||
|
|
||||||
case "/rule.json":
|
case "/rule.json":
|
||||||
const simple = {
|
const rule = await env.LOG.get("LATEST");
|
||||||
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 response = new Response(rule, {
|
||||||
};
|
|
||||||
|
|
||||||
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",
|
headers: { "Content-Type": "application/json",
|
||||||
"Cache-Control": "s-maxage=43200" }
|
"Cache-Control": "s-maxage=3600"}
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.waitUntil(cache.put(cacheKey, response.clone()));
|
ctx.waitUntil(cache.put(cacheKey, response.clone()));
|
||||||
return response;
|
return response;
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"name": "thirdrule",
|
"name": "thirdrule",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0"
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,3 +5,10 @@ account_id = "7dc420732ea679a530aee304ea49a63c"
|
||||||
|
|
||||||
[ai]
|
[ai]
|
||||||
binding = "AI"
|
binding = "AI"
|
||||||
|
|
||||||
|
[triggers]
|
||||||
|
crons = ["0 0 * * *"]
|
||||||
|
|
||||||
|
[[kv_namespaces]]
|
||||||
|
binding = "LOG"
|
||||||
|
id = "b92ed82d29074a8eb892419703cfdfa5"
|
Loading…
Reference in a new issue