diff --git a/src/index.ts b/src/index.ts index 5529398..e01320d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,71 @@ +export interface Env { + AI: Ai; +} + +const prompt = `You are HuskBot. Reply "Husk" if the message matches one of these: + Contains "veeee 😭" or variants + Mentions Nix or NixOS positively + Is racist, misogynist, homophobic, transphobic, etc. + Mentions betterdiscord + Talks about Nettspend + Mentions KSI, Skibidi Toilet, Hawk Tuah, Thick Of It, Prime, Gyat, Sigma + + Otherwise, reply "Noh". If the message is positive or is otherwise normal, also say Noh.`; + export default { async fetch(request, env, ctx): Promise { - return new Response("Hello World!"); + try { + const body: any = await request.json(); + if (request.method === "OPTIONS") { + return new Response(null, { + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", + "Access-Control-Max-Age": "86400", + }, + }); + } + if (!body.message) + return new Response("i sharted", { + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", + "Access-Control-Max-Age": "86400", + }, + }); + const resp = await env.AI.run("@cf/meta/llama-3.1-8b-instruct", { + max_tokens: 5, + messages: [ + { + role: "system", + content: prompt, + }, + { + role: "user", + content: body.message, + }, + ], + }); + + return new Response( + JSON.stringify({ + huskable: (resp as any).response.includes("Husk"), + }), + { + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", + "Access-Control-Max-Age": "86400", + }, + } + ); + } catch { + return new Response("i sharted", { + // @ts-ignore + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", + "Access-Control-Max-Age": "86400", + }); + } }, } satisfies ExportedHandler;