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

27
server/src/db-init.ts Normal file
View File

@@ -0,0 +1,27 @@
import { execFileSync } from "node:child_process";
import { existsSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import type { FastifyBaseLogger } from "fastify";
function runPrismaCli(rootDir: string, args: string[]) {
const prismaJs = join(rootDir, "node_modules", "prisma", "build", "index.js");
if (existsSync(prismaJs)) {
execFileSync(process.execPath, [prismaJs, ...args], {
cwd: rootDir,
stdio: "inherit",
env: { ...process.env, PRISMA_HIDE_UPDATE_MESSAGE: "1" },
});
return;
}
throw new Error("Prisma CLI not found at node_modules/prisma/build/index.js. Run `npm install` in /server.");
}
export async function ensureDatabaseReady(logger: FastifyBaseLogger) {
const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const startedAt = Date.now();
logger.info("Applying Prisma migrations...");
runPrismaCli(rootDir, ["migrate", "deploy"]);
logger.info({ durationMs: Date.now() - startedAt }, "Prisma migrations applied");
}

View File

@@ -4,6 +4,7 @@ import swagger from "@fastify/swagger";
import swaggerUI from "@fastify/swagger-ui";
import sensible from "@fastify/sensible";
import { env } from "./env.js";
import { ensureDatabaseReady } from "./db-init.js";
import { registerRoutes } from "./routes.js";
const app = Fastify({
@@ -15,6 +16,8 @@ const app = Fastify({
},
});
await ensureDatabaseReady(app.log);
await app.register(cors, { origin: true, credentials: true });
await app.register(swagger, {

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, {