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 app generates cute daily notes for a daughter using OpenAI's GPT model.
* It stores the generated notes in SQLite for persistence and displays them on a simple web interface.
* The app uses React for the frontend and Deno's runtime environment in Val Town for the backend.
*/
/** @jsxImportSource https://esm.sh/react */
import React, { useEffect, useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [note, setNote] = useState("");
const [loading, setLoading] = useState(true);
useEffect(() => {
fetchTodayNote();
}, []);
const fetchTodayNote = async () => {
setLoading(true);
const response = await fetch("/get-note");
const data = await response.json();
setNote(data.note);
setLoading(false);
};
const generateNewNote = async () => {
setLoading(true);
await fetch("/generate-note", { method: "POST" });
await fetchTodayNote();
};
return (
<div className="container">
<h1>Daily Note for My Daughter đź’–</h1>
{loading ? <p>Loading...</p> : (
<div className="note-container">
<p className="note">{note}</p>
</div>
)}
<button onClick={generateNewNote} disabled={loading}>
Generate New Note
</button>
<p className="footer">
<a href={import.meta.url.replace("esm.town", "val.town")}>View Source</a>
</p>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
const { OpenAI } = await import("https://esm.town/v/std/openai");
const { sqlite } = await import("https://esm.town/v/stevekrouse/sqlite");
const KEY = new URL(import.meta.url).pathname.split("/").at(-1);
// Create table if not exists
await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${KEY}_daughter_notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
note TEXT NOT NULL,
date TEXT NOT NULL UNIQUE
)
`);
const path = new URL(request.url).pathname;
if (path === "/get-note") {
const today = new Date().toISOString().split("T")[0];
const result = await sqlite.execute(`SELECT note FROM ${KEY}_daughter_notes WHERE date = ?`, [today]);
if (result.rows.length > 0) {
return new Response(JSON.stringify({ note: result.rows[0].note }), {
headers: { "Content-Type": "application/json" },
});
} else {
return new Response(JSON.stringify({ note: "No note generated for today yet. Click 'Generate New Note'!" }), {
headers: { "Content-Type": "application/json" },
});
}
} else if (path === "/generate-note" && request.method === "POST") {
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{
role: "system",
content: "You are a loving mother writing a short, cute note to your daughter. Keep it under 50 words.",
},
{ role: "user", content: "Write a cute, encouraging note for my daughter." },
],
model: "gpt-4o-mini",
max_tokens: 100,
});
const newNote = completion.choices[0].message.content.trim();