40
41- **Backend**: `/backend/index.ts` - Handles API requests and serves frontend assets
42 - OpenAI integration for chat completions
43 - API endpoints for authentication, chat, and user data
44 - Supabase integration for database and authentication
68- **Database**: Supabase
69- **Authentication**: Supabase Auth
70- **AI**: OpenAI API
71- **Styling**: Custom CSS with Tailwind utility classes
72
1import { serveFile, readFile } from "https://esm.town/v/std/utils@85-main/index.ts";
2import { Hono } from "npm:hono";
3import { OpenAI } from "https://esm.town/v/std/openai";
4import { supabase, getSession } from "../shared/supabase.ts";
5import { AIAgent, Message, Conversation, UserSettings } from "../shared/types.ts";
7
8const app = new Hono();
9const openai = new OpenAI();
10
11// Default AI agents if database is not set up
232 };
233
234 // Prepare context for OpenAI
235 // Only use the last 10 messages for context to stay within token limits
236 const recentMessages = messageHistory.slice(-10).map((msg: Message) => ({
251 });
252
253 const completion = await openai.chat.completions.create({
254 messages: recentMessages,
255 model: agent.model,
88Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
89
90### OpenAI
91
92```ts
93import { OpenAI } from "https://esm.town/v/std/openai";
94const openai = new OpenAI();
95const completion = await openai.chat.completions.create({
96 messages: [
97 { role: "user", content: "Say hello in a creative way" },
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
95
96### OpenAI
97
98```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102 messages: [
103 { role: "user", content: "Say hello in a creative way" },
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
95
96### OpenAI
97
98```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102 messages: [
103 { role: "user", content: "Say hello in a creative way" },
1246 <a href="?q=function" className="example-link">function</a>
1247 <a href="?q=discord" className="example-link">discord</a>
1248 <a href="?q=openai" className="example-link">openai</a>
1249 <a href="?q=react" className="example-link">react</a>
1250 </div>
1401 <a href="?q=function" className="example-link">function</a>
1402 <a href="?q=discord" className="example-link">discord</a>
1403 <a href="?q=openai" className="example-link">openai</a>
1404 <a href="?q=react" className="example-link">react</a>
1405 </div>
1import { fetch } from "https://esm.town/v/std/fetch";
2import { OpenAI } from "https://esm.town/v/std/openai";
3import { z } from "npm:zod";
4
263}
264
265// --- OpenAI Call Wrapper (Minor Abbreviation) ---
266async function callOpenAI(
267 sysP: string,
268 userP: string,
273 log("DB", "oai", `Call tid=${tid}`, { spL: sysP.length, upL: userP.length }, mid, tid);
274 try {
275 if (!process.env.OPENAI_API_KEY) {
276 log("ER", "oai", "Key missing.", {}, mid, tid);
277 throw new Error("OpenAI Key missing.");
278 }
279 const oai = new OpenAI();
280 const comp = await oai.chat.completions.create({
281 model: "gpt-4o-mini",
690 try {
691 const { sys, user } = cfg.p(p);
692 const rawC = await callOpenAI(sys, user, mid, tid, log);
693 if (!rawC) {
694 log("WN", cfg.n, `No LLM content tid=${tid}.`, {}, mid, tid);
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
95
96### OpenAI
97
98```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102 messages: [
103 { role: "user", content: "Say hello in a creative way" },
1246 <a href="?q=function" className="example-link">function</a>
1247 <a href="?q=discord" className="example-link">discord</a>
1248 <a href="?q=openai" className="example-link">openai</a>
1249 <a href="?q=react" className="example-link">react</a>
1250 </div>
1401 <a href="?q=function" className="example-link">function</a>
1402 <a href="?q=discord" className="example-link">discord</a>
1403 <a href="?q=openai" className="example-link">openai</a>
1404 <a href="?q=react" className="example-link">react</a>
1405 </div>
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3// Initialize OpenAI client
4const openai = new OpenAI();
5
6// For debugging
204
205 try {
206 const response = await openai.chat.completions.create({
207 model: "gpt-4o",
208 messages: [
293
294 try {
295 const response = await openai.chat.completions.create({
296 model: "gpt-4o",
297 messages: [
334 ).join("\n\n");
335
336 const response = await openai.chat.completions.create({
337 model: "gpt-4o",
338 messages: [