2
3/**
4 * Retrieves all memories from the database
5 * @param includeDate Whether to include date-specific memories or not
6 * @param startDate Optional start date to filter memories from (ISO format)
6import { Hono } from "npm:hono";
7import { type Memory } from "../shared/types.ts";
8import { createMemory, deleteMemory, getAllMemories, updateMemory } from "./database/queries.ts";
9
10const app = new Hono();
34
35/**
36 * Store a chat message in the database
37 */
38export async function storeChatMessage(
130 }
131
132 console.log(`Weather forecast updated in the database.`);
133 return summary;
134}
125 }
126
127 console.log(`Calendar events imported into the database.`);
128 return events;
129 } catch (error) {
8
9/**
10 * Retrieves previously generated fun facts from the memories database
11 * @returns Array of previous fun facts
12 */
47
48/**
49 * Inserts a fun fact into the memories database
50 * @param date Date for the fun fact in ISO format
51 * @param factText The fun fact text
208```
209โโโ backend/
210โ โโโ database/
211โ โ โโโ migrations.ts # Schema definitions
212โ โ โโโ queries.ts # DB query functions
270- Handle API calls properly with proper error catching
271
272### Database Patterns
273- Run migrations on startup or comment out for performance
274- Change table names when modifying schemas rather than altering
34 const binary = new Uint8Array(await blobResp.arrayBuffer());
35 const SQL = await initSqlJs();
36 const db = new SQL.Database(binary);
37
38 // Helper: Validate SQL query returns a result
126GlobalRateLimitedChatOpenAI(model: string, requestsPerSecond: number): Decorator for ChatOpenAI that enforces a global rate limit (requests per second) using a persistent SQLite table.
127GlobalRateLimiter: Class that implements the rate limiting logic. It checks the number of requests in the current time window and throws an error if the limit is exceeded. It uses a table (global_rate_limit_1) in Val Town's SQLite.
128ensureGlobalRateLimitTableExists: Ensures the rate limit tracking table exists in the database at startup.
129Usage
130Use ChatOpenAI(model) for direct, unlimited access to OpenAI chat completions.
225```
226โโโ backend/
227โ โโโ database/
228โ โ โโโ migrations.ts # Schema definitions
229โ โ โโโ queries.ts # DB query functions
284 ```
285
286### Database Patterns
287- Run migrations on startup or comment out for performance
288- Change table names when modifying schemas rather than altering
4
5export const turso = createClient({
6 url: process.env.TURSO_DATABASE_URL,
7 authToken: process.env.TURSO_AUTH_TOKEN,
8});