Add web frontend
This commit is contained in:
2
server/.gitignore
vendored
2
server/.gitignore
vendored
@@ -3,7 +3,7 @@ node_modules
|
||||
.env.*
|
||||
dev.db
|
||||
*.db
|
||||
*.db-journal
|
||||
prisma/migrations
|
||||
.DS_Store
|
||||
dist
|
||||
|
||||
|
||||
@@ -13,12 +13,20 @@ Backend API for:
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
npm run db:migrate
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Migrations are applied automatically on server startup (`prisma migrate deploy`).
|
||||
|
||||
Open docs: `http://localhost:8787/docs`
|
||||
|
||||
## Run Modes
|
||||
|
||||
- `npm run dev`: runs `src/index.ts` with `tsx` in watch mode (auto-restart on file changes). Use for local development.
|
||||
- `npm run start`: runs compiled `dist/index.js` with Node.js (no watch mode). Use for production-like runs.
|
||||
|
||||
Both modes run startup checks (`predev` / `prestart`) and apply migrations at app boot.
|
||||
|
||||
## Auth
|
||||
|
||||
Set `ADMIN_TOKEN` and send:
|
||||
@@ -34,6 +42,7 @@ If `ADMIN_TOKEN` is not set, the server runs in open mode (dev).
|
||||
|
||||
## API
|
||||
- `GET /health`
|
||||
- `GET /v1/auth/session`
|
||||
- `GET /v1/chats`
|
||||
- `POST /v1/chats`
|
||||
- `GET /v1/chats/:chatId`
|
||||
@@ -41,6 +50,8 @@ If `ADMIN_TOKEN` is not set, the server runs in open mode (dev).
|
||||
- `POST /v1/chat-completions`
|
||||
- `POST /v1/chat-completions/stream` (SSE)
|
||||
|
||||
When `chatId` is provided to completion endpoints, you can send full conversation context. The server now stores only new non-assistant messages to avoid duplicate history rows.
|
||||
|
||||
`POST /v1/chat-completions` body example:
|
||||
|
||||
```json
|
||||
|
||||
38
server/package-lock.json
generated
38
server/package-lock.json
generated
@@ -19,11 +19,11 @@
|
||||
"fastify": "^5.7.2",
|
||||
"openai": "^6.16.0",
|
||||
"pino-pretty": "^13.1.3",
|
||||
"prisma": "^6.6.0",
|
||||
"zod": "^4.3.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.0.10",
|
||||
"prisma": "^6.6.0",
|
||||
"tsx": "^4.21.0",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
@@ -64,7 +64,6 @@
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -81,7 +80,6 @@
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -98,7 +96,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -115,7 +112,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -132,7 +128,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -149,7 +144,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -166,7 +160,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -183,7 +176,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -200,7 +192,6 @@
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -217,7 +208,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -234,7 +224,6 @@
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -251,7 +240,6 @@
|
||||
"cpu": [
|
||||
"loong64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -268,7 +256,6 @@
|
||||
"cpu": [
|
||||
"mips64el"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -285,7 +272,6 @@
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -302,7 +288,6 @@
|
||||
"cpu": [
|
||||
"riscv64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -319,7 +304,6 @@
|
||||
"cpu": [
|
||||
"s390x"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -336,7 +320,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -353,7 +336,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -370,7 +352,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -387,7 +368,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -404,7 +384,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -421,7 +400,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -438,7 +416,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -455,7 +432,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -472,7 +448,6 @@
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -489,7 +464,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -826,7 +800,6 @@
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@prisma/config/-/config-6.6.0.tgz",
|
||||
"integrity": "sha512-d8FlXRHsx72RbN8nA2QCRORNv5AcUnPXgtPvwhXmYkQSMF/j9cKaJg+9VcUzBRXGy9QBckNzEQDEJZdEOZ+ubA==",
|
||||
"devOptional": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"esbuild": ">=0.12 <1",
|
||||
@@ -837,14 +810,12 @@
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-6.6.0.tgz",
|
||||
"integrity": "sha512-DL6n4IKlW5k2LEXzpN60SQ1kP/F6fqaCgU/McgaYsxSf43GZ8lwtmXLke9efS+L1uGmrhtBUP4npV/QKF8s2ZQ==",
|
||||
"devOptional": true,
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/@prisma/engines": {
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-6.6.0.tgz",
|
||||
"integrity": "sha512-nC0IV4NHh7500cozD1fBoTwTD1ydJERndreIjpZr/S3mno3P6tm8qnXmIND5SwUkibNeSJMpgl4gAnlqJ/gVlg==",
|
||||
"devOptional": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
@@ -858,14 +829,12 @@
|
||||
"version": "6.6.0-53.f676762280b54cd07c770017ed3711ddde35f37a",
|
||||
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-6.6.0-53.f676762280b54cd07c770017ed3711ddde35f37a.tgz",
|
||||
"integrity": "sha512-JzRaQ5Em1fuEcbR3nUsMNYaIYrOT1iMheenjCvzZblJcjv/3JIuxXN7RCNT5i6lRkLodW5ojCGhR7n5yvnNKrw==",
|
||||
"devOptional": true,
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/@prisma/fetch-engine": {
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-6.6.0.tgz",
|
||||
"integrity": "sha512-Ohfo8gKp05LFLZaBlPUApM0M7k43a0jmo86YY35u1/4t+vuQH9mRGU7jGwVzGFY3v+9edeb/cowb1oG4buM1yw==",
|
||||
"devOptional": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@prisma/debug": "6.6.0",
|
||||
@@ -877,7 +846,6 @@
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-6.6.0.tgz",
|
||||
"integrity": "sha512-3qCwmnT4Jh5WCGUrkWcc6VZaw0JY7eWN175/pcb5Z6FiLZZ3ygY93UX0WuV41bG51a6JN/oBH0uywJ90Y+V5eA==",
|
||||
"devOptional": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@prisma/debug": "6.6.0"
|
||||
@@ -1061,7 +1029,6 @@
|
||||
"version": "0.27.2",
|
||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.2.tgz",
|
||||
"integrity": "sha512-HyNQImnsOC7X9PMNaCIeAm4ISCQXs5a5YasTXVliKv4uuBo1dKrG0A+uQS8M5eXjVMnLg3WgXaKvprHlFJQffw==",
|
||||
"devOptional": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
@@ -1103,7 +1070,6 @@
|
||||
"version": "3.6.0",
|
||||
"resolved": "https://registry.npmjs.org/esbuild-register/-/esbuild-register-3.6.0.tgz",
|
||||
"integrity": "sha512-H2/S7Pm8a9CL1uhp9OvjwrBh5Pvx0H8qVOxNu8Wed9Y7qv56MPtq+GGM8RJpq6glYJn9Wspr8uw7l55uyinNeg==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"debug": "^4.3.4"
|
||||
@@ -1276,7 +1242,6 @@
|
||||
"version": "2.3.3",
|
||||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
|
||||
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
@@ -1679,7 +1644,6 @@
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://registry.npmjs.org/prisma/-/prisma-6.6.0.tgz",
|
||||
"integrity": "sha512-SYCUykz+1cnl6Ugd8VUvtTQq5+j1Q7C0CtzKPjQ8JyA2ALh0EEJkMCS+KgdnvKW1lrxjtjCyJSHOOT236mENYg==",
|
||||
"devOptional": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
|
||||
@@ -9,12 +9,12 @@
|
||||
"predev": "node scripts/ensure-prisma-client.mjs",
|
||||
"prestart": "node scripts/ensure-prisma-client.mjs",
|
||||
"prebuild": "node scripts/ensure-prisma-client.mjs",
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"dev": "node ./node_modules/tsx/dist/cli.mjs watch src/index.ts",
|
||||
"start": "node dist/index.js",
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"prisma:generate": "prisma generate",
|
||||
"db:migrate": "prisma migrate dev",
|
||||
"db:studio": "prisma studio"
|
||||
"build": "node ./node_modules/typescript/bin/tsc -p tsconfig.json",
|
||||
"prisma:generate": "node ./node_modules/prisma/build/index.js generate",
|
||||
"db:migrate": "node ./node_modules/prisma/build/index.js migrate dev",
|
||||
"db:studio": "node ./node_modules/prisma/build/index.js studio"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.71.2",
|
||||
@@ -26,12 +26,12 @@
|
||||
"dotenv": "^17.2.3",
|
||||
"fastify": "^5.7.2",
|
||||
"openai": "^6.16.0",
|
||||
"prisma": "^6.6.0",
|
||||
"pino-pretty": "^13.1.3",
|
||||
"zod": "^4.3.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.0.10",
|
||||
"prisma": "^6.6.0",
|
||||
"tsx": "^4.21.0",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ const scriptDir = dirname(fileURLToPath(import.meta.url));
|
||||
const rootDir = resolve(scriptDir, "..");
|
||||
const clientDir = join(rootDir, "node_modules", ".prisma", "client");
|
||||
const clientIndex = join(clientDir, "index.js");
|
||||
const prismaBin = join(rootDir, "node_modules", ".bin", "prisma");
|
||||
const prismaJs = join(rootDir, "node_modules", "prisma", "build", "index.js");
|
||||
const genericEngine = join(clientDir, "libquery_engine.node");
|
||||
|
||||
function parseExpectedEngineFiles() {
|
||||
@@ -22,15 +22,18 @@ function missingEngineFiles(expectedFiles) {
|
||||
}
|
||||
|
||||
function runPrismaGenerate() {
|
||||
if (!existsSync(prismaBin)) {
|
||||
throw new Error(
|
||||
"Prisma CLI not found. Install dev dependencies and run `npm run prisma:generate`."
|
||||
);
|
||||
if (existsSync(prismaJs)) {
|
||||
execFileSync(process.execPath, [prismaJs, "generate"], {
|
||||
cwd: rootDir,
|
||||
stdio: "inherit",
|
||||
env: { ...process.env, PRISMA_HIDE_UPDATE_MESSAGE: "1" },
|
||||
});
|
||||
return;
|
||||
}
|
||||
execFileSync(prismaBin, ["generate"], {
|
||||
cwd: rootDir,
|
||||
stdio: "inherit",
|
||||
});
|
||||
|
||||
throw new Error(
|
||||
"Prisma CLI not found at node_modules/prisma/build/index.js. Install dependencies and run `npm run prisma:generate`."
|
||||
);
|
||||
}
|
||||
|
||||
let expectedFiles = parseExpectedEngineFiles();
|
||||
@@ -54,4 +57,3 @@ if (missingFiles.length) {
|
||||
)}. Ensure deployment copies node_modules/.prisma (including dot-directories).`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
27
server/src/db-init.ts
Normal file
27
server/src/db-init.ts
Normal 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");
|
||||
}
|
||||
@@ -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, {
|
||||
|
||||
@@ -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, {
|
||||
|
||||
Reference in New Issue
Block a user