-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use React rather than a single, hacky JS script for running web app
- Loading branch information
1 parent
58bf0cb
commit 93fd2e8
Showing
28 changed files
with
5,253 additions
and
1,108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,5 @@ bin | |
node_modules | ||
|
||
web/src/example02 | ||
web/dist | ||
.dev.vars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
``` | ||
npm run watch:css | ||
npm run watch:web | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { build } from "esbuild"; | ||
import { readFileSync, writeFileSync } from "fs"; | ||
|
||
console.log("[esbuild] start " + new Date().toLocaleTimeString()); | ||
|
||
const scripts = build({ | ||
entryPoints: ["./src/index.tsx", "./src/**/*.wasm"], | ||
bundle: true, | ||
minify: true, | ||
sourcemap: true, | ||
outdir: "dist", | ||
outbase: "src", | ||
loader: { | ||
".wasm": "file", | ||
}, | ||
external: ["crypto", "fs", "util"], | ||
}); | ||
|
||
const staticAssets = build({ | ||
entryPoints: ["./src/**/*.html", "./src/wasm-init.js", "./src/assets/**/*.png"], | ||
minify: false, | ||
sourcemap: true, | ||
outdir: "dist", | ||
outbase: "src", | ||
loader: { | ||
".png": "copy", | ||
".html": "copy", | ||
}, | ||
}); | ||
|
||
const functions = build({ | ||
entryPoints: ["./functions/**/*.ts"], | ||
bundle: true, | ||
metafile: true, | ||
minify: false, | ||
sourcemap: true, | ||
outdir: "dist/functions", | ||
external: ["crypto", "fs", "util", "@cloudflare/*"], | ||
}); | ||
|
||
await Promise.all([scripts, staticAssets, functions]); | ||
|
||
// Hack to force wrangler pages to rebuild | ||
const packageJson = readFileSync("package.json", "utf8"); | ||
const pkg = JSON.parse(packageJson); | ||
pkg.scripts["build:time"] = `echo "${new Date().toISOString()}"`; | ||
writeFileSync("package.json", JSON.stringify(pkg, null, 2)); | ||
|
||
console.log("[esbuild] done " + new Date().toLocaleTimeString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Ai } from "@cloudflare/ai"; | ||
|
||
export async function onRequest(context) { | ||
// read the bearer token from the request | ||
const authHeader = context.request.headers.get("Authorization") ?? ""; | ||
const [_, token] = authHeader.split(" "); | ||
if (token !== context.env.SECRET_KEY) { | ||
return new Response("Unauthorized", { status: 401 }); | ||
} | ||
|
||
if (context.env.AI === undefined) { | ||
return new Response( | ||
`{ | ||
print("AI not configured") | ||
choice("Try again", { goto("generate") }) | ||
}`, | ||
{ status: 200 }, | ||
); | ||
} | ||
|
||
const ai = new Ai(context.env.AI); | ||
const input = { | ||
prompt: | ||
"In a realm shrouded in ancient lore and dark magic, a lone adventurer finds themselves ensnared within the labyrinthine depths of a mysterious dungeon. Each corridor and chamber is fraught with peril, housing puzzles of arcane origin and traps designed to thwart any attempts at escape. At the heart of the dungeon lies a promise of freedom, whispered by the enigmatic guardian who watches from the shadows. Yet, as the adventurer delves deeper, they realize that dark forces conspire to ensure that the secrets of the dungeon remain forever concealed. With their courage tested and their wits challenged, the adventurer must navigate the treacherous maze, unravel its mysteries, and confront the guardians that stand in the way of their ultimate escape. Craft a gripping hook that plunges the player into the thrilling journey of a solitary hero against the odds.", | ||
}; | ||
const answer = (await ai.run("@cf/meta/llama-2-7b-chat-int8", input)).response; | ||
const jabl = `{ | ||
${answer | ||
.split("\n") | ||
.map((line) => `print("${line.replace(/^"|"$/g, "")}")`) | ||
.join("\n")} | ||
choice("Continue", { goto("generate") }) | ||
}`; | ||
return new Response(jabl); | ||
} |
Oops, something went wrong.