1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* This val creates a website that generates optimized prompts for Val Town based on user input.
* It uses the OpenAI API to generate prompts and incorporates a loading animation.
* The generated prompt is tailored to Val Town's specific features and best practices.
*/
/** @jsxImportSource https://esm.sh/react */
import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
const valTownFeatures = [
"HTTP vals",
"Scheduled vals",
"SQLite database",
"Blob storage",
"Email sending",
"OpenAI integration",
"React support",
"TypeScript support",
"Deno runtime",
"Web APIs",
];
function App() {
const [idea, setIdea] = useState("");
const [prompt, setPrompt] = useState("");
const [loading, setLoading] = useState(false);
const generatePrompt = async () => {
setLoading(true);
try {
const response = await fetch("/generate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ idea }),
});
if (!response.ok) throw new Error("Failed to generate prompt");
const data = await response.json();
setPrompt(data.prompt);
} catch (error) {
console.error("Error generating prompt:", error);
setPrompt("An error occurred while generating the prompt. Please try again.");
} finally {
setLoading(false);
}
};
return (
<div className="container">
<h1>Val Town Prompt Generator</h1>
<textarea
value={idea}
onChange={(e) => setIdea(e.target.value)}
placeholder="Enter your Val Town application idea"
rows={4}
/>
<button onClick={generatePrompt} disabled={loading || !idea}>
Generate Optimized Prompt
</button>
{loading && (
<div className="loading">
<div className="spinner"></div>
<p>Generating prompt...</p>
</div>
)}
{prompt && (
<div className="result">
<h2>Generated Prompt:</h2>
<pre>{prompt}</pre>
</div>
)}
<a
href={import.meta.url.replace("esm.town", "val.town")}
target="_blank"
rel="noopener noreferrer"
className="view-source"
>
View Source
</a>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") {
client();
}
async function server(request: Request): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/generate" && request.method === "POST") {
const { OpenAI } = await import("https://esm.town/v/std/openai");
const openai = new OpenAI();
try {
const { idea } = await request.json();
const completion = await openai.chat.completions.create({