9
10// Initialize the database
11async function initDB() {
12 await sqlite.execute(`
13 CREATE TABLE IF NOT EXISTS ${TABLE_NAME}_${SCHEMA_VERSION} (
21
22// Generate embedding for a given text
23async function generateEmbedding(text: string): Promise<number[]> {
24 const response = await openai.embeddings.create({
25 model: "text-embedding-ada-002",
30
31// Add a new memory to the bank
32export async function addMemory(content: string): Promise<void> {
33 await initDB();
34 const embedding = await generateEmbedding(content);
40
41// Retrieve similar memories
42export async function getSimilarMemories(query: string, limit: number = 5): Promise<string[]> {
43 await initDB();
44 const queryEmbedding = await generateEmbedding(query);
59
60// Cosine similarity calculation
61function cosineSimilarity(vecA: number[], vecB: number[]): number {
62 const dotProduct = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0);
63 const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));
67
68// Example usage
69export async function memoryBankDemo() {
70 // Add some memories
71 await addMemory("Create a RESTful API using Express.js and PostgreSQL");
31
32// ------------
33// Functions
34// ------------
35
36async function execute(statement: InStatement, args?: InArgs): Promise<ResultSet> {
37 const res = await fetch(`${API_URL}/v1/sqlite/execute`, {
38 method: "POST",
49}
50
51async function batch(statements: InStatement[], mode?: TransactionMode): Promise<ResultSet[]> {
52 const res = await fetch(`${API_URL}/v1/sqlite/batch`, {
53 method: "POST",
64}
65
66function createResError(body: string) {
67 try {
68 const e = zLibsqlError.parse(JSON.parse(body));
85}
86
87function normalizeStatement(statement: InStatement, args?: InArgs) {
88 if (Array.isArray(statement)) {
89 // for the case of an array of arrays
107}
108
109function upgradeResultSet(results: ImpoverishedResultSet): ResultSet {
110 return {
111 ...results,
116// adapted from
117// https://github.com/tursodatabase/libsql-client-ts/blob/17dd996b840c950dd22b871adfe4ba0eb4a5ead3/packages/libsql-client/src/sqlite3.ts#L314C1-L337C2
118function rowFromSql(
119 sqlRow: Array<unknown>,
120 columns: Array<string>,
15
16// Use Browserbase (with proxy) to search and scrape Reddit results
17export async function redditSearch({
18 query,
19 apiKey = Deno.env.get("BROWSERBASE_API_KEY"),
46}
47
48function constructSearchUrl(query: string): string {
49 const encodedQuery = encodeURIComponent(query).replace(/%20/g, "+");
50 return `https://www.reddit.com/search/?q=${encodedQuery}&type=link&t=week`;
51}
52
53async function extractPostData(page: any): Promise<Partial<ThreadResult>[]> {
54 return page.evaluate(() => {
55 const posts = document.querySelectorAll("div[data-testid=\"search-post-unit\"]");
67}
68
69async function processPostData(postData: Partial<ThreadResult>[]): Promise<ThreadResult[]> {
70 const processedData: ThreadResult[] = [];
71
133. Adjust the if statement to detect changes and update your blob
14
154. Craft a message to be sent with `sendNotification()` function
2import { easyAQI } from "https://esm.town/v/stevekrouse/easyAQI?v=5";
3
4export async function aqi(interval: Interval) {
5 const location = "guangzhou"; // <-- change to place, city, or zip code
6 const data = await easyAQI({ location });
84];
85
86function Header({ logoUrl, themeColor1, themeColor2 }) {
87 return (
88 <div className="py-4" style={{ backgroundColor: themeColor1, color: themeColor2 }}>
96}
97
98function EraSelector({ activeEra, onSelectEra }) {
99 return (
100 <div className="flex justify-center items-stretch w-full h-[40vh] overflow-hidden">
125}
126
127function truncatePrompt(prompt, maxLength = 50) {
128 if (prompt.length <= maxLength) return prompt;
129 return prompt.substring(0, maxLength - 3) + "...";
130}
131
132function ImageGallery({ images, activeEra, onLike, onDelete }) {
133 const downloadImage = async (url, filename) => {
134 try {
193}
194
195function App() {
196 const [prompt, setPrompt] = useState("");
197 const [imageUrl, setImageUrl] = useState("");
384}
385
386function client() {
387 createRoot(document.getElementById("root")).render(<App />);
388}
389if (typeof document !== "undefined") { client(); }
390
391export default async function server(req: Request): Promise<Response> {
392 const { sqlite } = await import("https://esm.town/v/stevekrouse/sqlite");
393 const { falProxyRequest } = await import("https://esm.town/v/stevekrouse/falProxyRequest");
1export default async function fetchNewPublicGitHubRepos() {
2 // Generate today's date in YYYY-MM-DD format
3 const today = new Date().toISOString().split("T")[0];
348
349
350 // Helper function to get the supported MIME type
351 function getSupportedMimeType() {
352 const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
353 const mimeTypes = [
3This val can be used in other vals to send notifications to a segment using [OneSignal's REST API](https://documentation.onesignal.com/reference/create-notification)
4
5This is really handy if you want to send push notifications to your phone without building a native app! I built a barebones React PWA that asks for a password then loads the OneSignal Web SDK that I deployed to [Netlify](https://www.netlify.com/) for free. OneSignal has [easy to follow docs](https://documentation.onesignal.com/docs/web-sdk-setup) so you can build this functionality into a React, Angular, Vue app or even Wordpress! Then [install the PWA](https://www.bitcot.com/how-to-install-a-pwa-to-your-device/) on your platform of choice and you're off to the races!
6
7## Setup
1export function extractValInfo(url: string | URL) {
2 const { pathname, search } = new URL(url);
3 const [author, filename] = pathname.split("/").slice(-2);