joel memories

This commit is contained in:
2026-02-01 17:55:21 +01:00
parent c13ffc93c0
commit 0c0efa645a
22 changed files with 2463 additions and 304 deletions

View File

@@ -9,6 +9,7 @@ import { eq } from "drizzle-orm";
import { requireAuth } from "./session";
import * as oauth from "./oauth";
import type { BotClient } from "../core/client";
import { personalitiesList, viewPromptModal, editPromptModal } from "./templates";
export function createApiRoutes(client: BotClient) {
const api = new Hono();
@@ -64,9 +65,20 @@ export function createApiRoutes(client: BotClient) {
return c.json({ error: "Access denied" }, 403);
}
const body = await c.req.json<{ name: string; system_prompt: string }>();
const contentType = c.req.header("content-type");
let name: string, system_prompt: string;
if (contentType?.includes("application/x-www-form-urlencoded")) {
const form = await c.req.parseBody();
name = form.name as string;
system_prompt = form.system_prompt as string;
} else {
const body = await c.req.json<{ name: string; system_prompt: string }>();
name = body.name;
system_prompt = body.system_prompt;
}
if (!body.name || !body.system_prompt) {
if (!name || !system_prompt) {
return c.json({ error: "Name and system_prompt are required" }, 400);
}
@@ -74,11 +86,68 @@ export function createApiRoutes(client: BotClient) {
await db.insert(personalities).values({
id,
guild_id: guildId,
name: body.name,
system_prompt: body.system_prompt,
name,
system_prompt,
});
return c.json({ id, guild_id: guildId, name: body.name, system_prompt: body.system_prompt }, 201);
// Check if HTMX request
if (c.req.header("hx-request")) {
const guildPersonalities = await db
.select()
.from(personalities)
.where(eq(personalities.guild_id, guildId));
return c.html(personalitiesList(guildId, guildPersonalities));
}
return c.json({ id, guild_id: guildId, name, system_prompt }, 201);
});
// View a personality (returns modal HTML for HTMX)
api.get("/guilds/:guildId/personalities/:personalityId/view", async (c) => {
const guildId = c.req.param("guildId");
const personalityId = c.req.param("personalityId");
const session = c.get("session");
const hasAccess = await verifyGuildAccess(session.accessToken, guildId, client);
if (!hasAccess) {
return c.json({ error: "Access denied" }, 403);
}
const result = await db
.select()
.from(personalities)
.where(eq(personalities.id, personalityId))
.limit(1);
if (result.length === 0) {
return c.json({ error: "Personality not found" }, 404);
}
return c.html(viewPromptModal(result[0]));
});
// Edit form for a personality (returns modal HTML for HTMX)
api.get("/guilds/:guildId/personalities/:personalityId/edit", async (c) => {
const guildId = c.req.param("guildId");
const personalityId = c.req.param("personalityId");
const session = c.get("session");
const hasAccess = await verifyGuildAccess(session.accessToken, guildId, client);
if (!hasAccess) {
return c.json({ error: "Access denied" }, 403);
}
const result = await db
.select()
.from(personalities)
.where(eq(personalities.id, personalityId))
.limit(1);
if (result.length === 0) {
return c.json({ error: "Personality not found" }, 404);
}
return c.html(editPromptModal(guildId, result[0]));
});
// Update a personality
@@ -92,16 +161,37 @@ export function createApiRoutes(client: BotClient) {
return c.json({ error: "Access denied" }, 403);
}
const body = await c.req.json<{ name?: string; system_prompt?: string }>();
const contentType = c.req.header("content-type");
let name: string | undefined, system_prompt: string | undefined;
if (contentType?.includes("application/x-www-form-urlencoded")) {
const form = await c.req.parseBody();
name = form.name as string;
system_prompt = form.system_prompt as string;
} else {
const body = await c.req.json<{ name?: string; system_prompt?: string }>();
name = body.name;
system_prompt = body.system_prompt;
}
await db
.update(personalities)
.set({
...body,
name,
system_prompt,
updated_at: new Date().toISOString(),
})
.where(eq(personalities.id, personalityId));
// Check if HTMX request
if (c.req.header("hx-request")) {
const guildPersonalities = await db
.select()
.from(personalities)
.where(eq(personalities.guild_id, guildId));
return c.html(personalitiesList(guildId, guildPersonalities));
}
return c.json({ success: true });
});
@@ -118,6 +208,15 @@ export function createApiRoutes(client: BotClient) {
await db.delete(personalities).where(eq(personalities.id, personalityId));
// Check if HTMX request
if (c.req.header("hx-request")) {
const guildPersonalities = await db
.select()
.from(personalities)
.where(eq(personalities.guild_id, guildId));
return c.html(personalitiesList(guildId, guildPersonalities));
}
return c.json({ success: true });
});