Add web frontend

This commit is contained in:
2026-02-13 23:15:12 -08:00
parent 5faa57a741
commit 16a668b6ee
37 changed files with 4149 additions and 69 deletions

View File

@@ -2,12 +2,59 @@ import { z } from "zod";
import type { FastifyInstance } from "fastify";
import { prisma } from "./db.js";
import { requireAdmin } from "./auth.js";
import { env } from "./env.js";
import { runMultiplex } from "./llm/multiplexer.js";
import { runMultiplexStream } from "./llm/streaming.js";
type IncomingChatMessage = {
role: "system" | "user" | "assistant" | "tool";
content: string;
name?: string;
};
function sameMessage(a: IncomingChatMessage, b: IncomingChatMessage) {
return a.role === b.role && a.content === b.content && (a.name ?? null) === (b.name ?? null);
}
async function storeNonAssistantMessages(chatId: string, messages: IncomingChatMessage[]) {
const incoming = messages.filter((m) => m.role !== "assistant");
if (!incoming.length) return;
const existing = await prisma.message.findMany({
where: { chatId },
orderBy: { createdAt: "asc" },
select: { role: true, content: true, name: true },
});
const existingNonAssistant = existing.filter((m) => m.role !== "assistant");
let sharedPrefix = 0;
const max = Math.min(existingNonAssistant.length, incoming.length);
while (sharedPrefix < max && sameMessage(existingNonAssistant[sharedPrefix] as IncomingChatMessage, incoming[sharedPrefix])) {
sharedPrefix += 1;
}
if (sharedPrefix === incoming.length) return;
const toInsert = sharedPrefix === existingNonAssistant.length ? incoming.slice(existingNonAssistant.length) : incoming;
if (!toInsert.length) return;
await prisma.message.createMany({
data: toInsert.map((m) => ({
chatId,
role: m.role as any,
content: m.content,
name: m.name,
})),
});
}
export async function registerRoutes(app: FastifyInstance) {
app.get("/health", async () => ({ ok: true }));
app.get("/v1/auth/session", async (req) => {
requireAdmin(req);
return { authenticated: true, mode: env.ADMIN_TOKEN ? "token" : "open" };
});
app.get("/v1/chats", async (req) => {
requireAdmin(req);
const chats = await prisma.chat.findMany({
@@ -92,14 +139,9 @@ export async function registerRoutes(app: FastifyInstance) {
if (!exists) return app.httpErrors.notFound("chat not found");
}
// store user messages (anything not assistant) for DB fidelity
// Store only new non-assistant messages to avoid duplicate history entries.
if (body.chatId) {
const toInsert = body.messages.filter((m) => m.role !== "assistant");
if (toInsert.length) {
await prisma.message.createMany({
data: toInsert.map((m) => ({ chatId: body.chatId!, role: m.role as any, content: m.content, name: m.name })),
});
}
await storeNonAssistantMessages(body.chatId, body.messages);
}
const result = await runMultiplex(body);
@@ -137,14 +179,9 @@ export async function registerRoutes(app: FastifyInstance) {
if (!exists) return app.httpErrors.notFound("chat not found");
}
// store user messages (anything not assistant) for DB fidelity
// Store only new non-assistant messages to avoid duplicate history entries.
if (body.chatId) {
const toInsert = body.messages.filter((m) => m.role !== "assistant");
if (toInsert.length) {
await prisma.message.createMany({
data: toInsert.map((m) => ({ chatId: body.chatId!, role: m.role as any, content: m.content, name: m.name })),
});
}
await storeNonAssistantMessages(body.chatId, body.messages);
}
reply.raw.writeHead(200, {