5import { ShareButton } from "./ui.tsx";
6
7export function Example() {
8 const [context, setContext] = useState<any>();
9 useEffect(() => {
3const baseUrl = "https://miniapp-starter.val.run";
4
5export default async function(interval: Interval) {
6 await sendNotificationToAllUsers({
7 title: "Example daily notification",
2// Run this script manually ONCE to create/ensure the tables exist.
3
4export default async function setupStevensDb() {
5 try {
6 // Import SQLite module
5export const BLOGS_TABLE = "petit_prince_generated_blogs";
6
7export async function createBlogTable() {
8 try {
9 console.log(`Creating blog table if it doesn't exist: ${BLOGS_TABLE}`);
33 * Store a chat message in the database
34 */
35export async function storeChatMessage(
36 chatId: number | string, // Added types
37 senderId: string,
66 * Retrieve chat history for a specific chat
67 */
68export async function getChatHistory(chatId: number | string, limit = 50): Promise<any[]> { // Added return type
69 try {
70 const { sqlite } = await import("https://esm.town/v/stevekrouse/sqlite");
91 * Format chat history for AI SDK (CoreMessage format)
92 */
93function formatChatHistoryForAI(history: any[]): CoreMessage[] { // Updated return type
94 const messages: CoreMessage[] = [];
95
116 * Analyze a Telegram message and extract memories from it using OpenRouter
117 */
118async function analyzeMessageContent(
119 username: string,
120 messageText: string,
214 }`;
215
216 // Prepare formatted chat history using the updated function
217 const formattedMessages: CoreMessage[] = formatChatHistoryForAI(chatHistory);
218
576
577// Handle webhook requests
578export default async function(req: Request): Promise<Response> {
579 if (req.method === "POST") {
580 // Set webhook lazily only when needed and not already set
35});
36
37function generateHtml(baseUrl: string, path: string = "/"): any {
38 return (
39 <html>
4import { DateTime } from "https://esm.sh/luxon@3.4.4";
5
6export async function testDailyBrief() {
7 try {
8 const testChatId = Deno.env.get("TEST_TELEGRAM_CHAT_ID");
13} from "../memoryUtils.ts";
14
15async function generateBriefingContent(anthropic, memories, today, isSunday) {
16 try {
17 const weekdaysHelp = generateWeekDays(today);
96}
97
98export async function sendDailyBriefing(chatId?: string, today?: DateTime) {
99 // Get API keys from environment
100 const apiKey = Deno.env.get("ANTHROPIC_API_KEY");
135 const lastSunday = today.startOf("week").minus({ days: 1 });
136
137 // Fetch relevant memories using the utility function
138 const memories = await getRelevantMemories();
139
216}
217
218function generateWeekDays(today) {
219 let output = [];
220
239// console.log(weekDays);
240
241// Export a function that calls sendDailyBriefing with no parameters
242// This maintains backward compatibility with existing cron jobs
243export default async function (overrideToday?: DateTime) {
244 return await sendDailyBriefing(undefined, overrideToday);
245}
16In a normal server environment, you would likely use a middleware [like this one](https://hono.dev/docs/getting-started/nodejs#serve-static-files) to serve static files. Some frameworks or deployment platforms automatically make any content inside a `public/` folder public.
17
18However in Val Town you need to handle this yourself, and it can be suprisingly difficult to read and serve files in a Val Town Project. This template uses helper functions from [stevekrouse/utils/serve-public](https://www.val.town/x/stevekrouse/utils/branch/main/code/serve-public/README.md), which handle reading project files in a way that will work across branches and forks, automatically transpiles typescript to javascript, and assigns content-types based on the file's extension.
19
20### `index.html`
26## CRUD API Routes
27
28This app has two CRUD API routes: for reading and inserting into the messages table. They both speak JSON, which is standard. They import their functions from `/backend/database/queries.ts`. These routes are called from the React app to refresh and update data.
29
30## Errors
4
5* `migrations.ts` - code to set up the database tables the app needs
6* `queries.ts` - functions to run queries against those tables, which are imported and used in the main Hono server in `/backend/index.ts`
7
8## Migrations
18The queries file is where running the migrations happen in this app. It'd also be reasonable for that to happen in index.ts, or as is said above, for that line to be commented out, and only run when actual changes are made to your database schema.
19
20The queries file exports functions to get and write data. It relies on shared types and data imported from the `/shared` directory.
A helper function to build a file's email
Simple functional CSS library for Val Town
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
if (req.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
LangChain (https://langchain.com) Ambassador, KubeSphere (https://kubesphere.io) Ambassador, CNCF OpenFunction (https://openfunction.dev) TOC Member.