make some overall QoL improvements
This commit is contained in:
+1
-1
@@ -309,7 +309,7 @@ export function createApp(options = {}) {
|
||||
app.use(createUploadsRouter(attachmentService, config));
|
||||
if (config.visionDebug) app.use(createVisionDebugRouter({visionDebugService, repositories}));
|
||||
app.use(createSettingsRouter({repositories}));
|
||||
app.use(createChatsRouter({repositories, chatService, modelService, config, chatRateLimiters}));
|
||||
app.use(createChatsRouter({repositories, chatService, modelService, config, chatRateLimiters, attachmentService}));
|
||||
app.use(createPersonasRouter({repositories, chatService, modelService, config}));
|
||||
app.use(createSystemRouter({modelService, config}));
|
||||
app.use(notFoundHandler);
|
||||
|
||||
@@ -87,7 +87,7 @@ export function createRepositories(db, config) {
|
||||
WHERE cs.username = ?
|
||||
ORDER BY cs.is_pinned DESC, datetime(cs.updated_at) DESC
|
||||
`),
|
||||
countChatSessions: db.prepare("SELECT COUNT(*) as count FROM chat_sessions WHERE username = ?"),
|
||||
countChatSessions: db.prepare("SELECT COUNT(*) as count FROM chat_sessions WHERE username = ? AND archived_at IS NULL"),
|
||||
getChatSession: db.prepare(`
|
||||
SELECT cs.id,
|
||||
CASE
|
||||
@@ -887,9 +887,10 @@ export function createRepositories(db, config) {
|
||||
.run(result.lastInsertRowid, version.content, version.thinking, JSON.stringify(version.sources), version.modelName, version.createdAt);
|
||||
}
|
||||
for (const [position, attachment] of (message.attachments || []).slice(0, config.uploadMaxFiles || 5).entries()) {
|
||||
// JSON backups contain references, not files. Only restore existing, owned images.
|
||||
db.prepare("INSERT OR IGNORE INTO message_attachments(message_id,attachment_id,position) SELECT ?,id,? FROM attachments WHERE id=? AND username=? AND status='attached'")
|
||||
// JSON imports reconnect owned images; archive imports also reconnect newly restored images.
|
||||
db.prepare("INSERT OR IGNORE INTO message_attachments(message_id,attachment_id,position) SELECT ?,id,? FROM attachments WHERE id=? AND username=? AND status IN ('temporary','attached')")
|
||||
.run(result.lastInsertRowid, position, attachment.id, username);
|
||||
db.prepare("UPDATE attachments SET status='attached' WHERE id=? AND username=?").run(attachment.id, username);
|
||||
}
|
||||
}
|
||||
statements.updateChatGenerationSettings.run(
|
||||
|
||||
+79
-3
@@ -2,9 +2,11 @@
|
||||
* @file Stellt Chat-Routen für Nachrichten, Streaming, Antwortvarianten, Verzweigungen und Einstellungen bereit.
|
||||
*/
|
||||
import express from "express";
|
||||
import multer from "multer";
|
||||
import {requireLogin} from "../middleware/auth.js";
|
||||
import {validateBody} from "../middleware/validate.js";
|
||||
import {validateWorkspaceImport} from "../services/workspaceImportService.js";
|
||||
import {createWorkspaceArchive, parseWorkspaceArchive, MAX_ARCHIVE_BYTES} from "../services/workspaceArchive.js";
|
||||
|
||||
const createChatValidator = (body) => ({
|
||||
value: {
|
||||
@@ -105,9 +107,29 @@ const preferredModelValidator = (body) => {
|
||||
return {value: {model}};
|
||||
};
|
||||
|
||||
export function createChatsRouter({repositories, chatService, modelService, config, chatRateLimiters}) {
|
||||
export function createChatsRouter({repositories, chatService, modelService, config, chatRateLimiters, attachmentService}) {
|
||||
const router = express.Router();
|
||||
router.use(requireLogin);
|
||||
const receiveArchive = multer({storage: multer.memoryStorage(), limits: {fileSize: MAX_ARCHIVE_BYTES, files: 1, parts: 2}}).single("archive");
|
||||
const readArchive = (req, res, next) => receiveArchive(req, res, error => {
|
||||
if (error) return res.status(error.code === "LIMIT_FILE_SIZE" ? 413 : 400).json({error: "Invalid or oversized workspace archive"});
|
||||
try {
|
||||
if (!req.file) throw new Error("Choose a workspace archive");
|
||||
req.archive = parseWorkspaceArchive(req.file.buffer);
|
||||
next();
|
||||
} catch (failure) { res.status(400).json({error: failure.message}); }
|
||||
});
|
||||
const archiveImages = (workspace, files) => {
|
||||
const images = new Map();
|
||||
for (const chat of workspace.chats) for (const message of chat.messages) for (const attachment of message.attachments) {
|
||||
if (images.has(attachment.id)) continue;
|
||||
const matches = [...files.entries()].filter(([name]) => name.startsWith(`images/${attachment.id}.`));
|
||||
if (matches.length !== 1) throw new Error(`Image ${attachment.id} is missing from archive`);
|
||||
const [name, buffer] = matches[0];
|
||||
images.set(attachment.id, {buffer, size: buffer.length, originalname: name.split("/").at(-1)});
|
||||
}
|
||||
return images;
|
||||
};
|
||||
router.use(["/chats/:id/messages/by-index/:index", "/chats/:id/messages/:messageId"], (req, res, next) => {
|
||||
const chatId = Number(req.params.id);
|
||||
const messageId = req.params.messageId ? Number(req.params.messageId) : chatService.getChatMessageByIndex(chatId, req.params.index)?.id;
|
||||
@@ -144,14 +166,14 @@ export function createChatsRouter({repositories, chatService, modelService, conf
|
||||
return res.json({chat, existing: Boolean(existingChat)});
|
||||
} catch (error) {
|
||||
if (error.message === "CHAT_LIMIT_REACHED") {
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} chats reached. Please delete an old chat to create a new one.`});
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} active chats reached. Archive or delete an active chat to create a new one.`});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
if (repositories.countChats(user) >= config.chatLimit) {
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} chats reached. Please delete an old chat to create a new one.`});
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} active chats reached. Archive or delete an active chat to create a new one.`});
|
||||
}
|
||||
|
||||
return res.json({chat: repositories.createChat(user, title), existing: false});
|
||||
@@ -171,6 +193,9 @@ export function createChatsRouter({repositories, chatService, modelService, conf
|
||||
const chatId = Number(req.params.id);
|
||||
const chat = repositories.getChat(chatId, req.session.user);
|
||||
if (!chat) return res.status(404).json({error: "Chat not found"});
|
||||
if (chat.archived_at && !req.validatedBody.archived && repositories.countChats(req.session.user) >= config.chatLimit) {
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} active chats reached. Archive another chat before restoring this one.`});
|
||||
}
|
||||
repositories.updateChatOrganization(chatId, req.session.user, {
|
||||
folderName: req.validatedBody.folderName,
|
||||
isPinned: req.validatedBody.isPinned,
|
||||
@@ -602,6 +627,57 @@ export function createChatsRouter({repositories, chatService, modelService, conf
|
||||
});
|
||||
|
||||
router.get("/exports/workspace", (req, res) => res.json({workspace: repositories.exportWorkspace(req.session.user)}));
|
||||
router.get("/exports/workspace/archive", async (req, res) => {
|
||||
try {
|
||||
const workspace = repositories.exportWorkspace(req.session.user);
|
||||
const images = new Map();
|
||||
for (const chat of workspace.chats) for (const message of chat.messages) for (const attachment of message.attachments) {
|
||||
if (images.has(attachment.id)) continue;
|
||||
const row = attachmentService.owned(attachment.id, req.session.user);
|
||||
images.set(attachment.id, [`images/${row.id}.${row.mime_type.split("/")[1]}`, await attachmentService.storage.read(row.storage_key)]);
|
||||
}
|
||||
const archive = createWorkspaceArchive(workspace, [...images.values()]);
|
||||
res.set({"Content-Type": "application/gzip", "Content-Disposition": 'attachment; filename="workspace-backup.tar.gz"', "Cache-Control": "private, no-store"});
|
||||
return res.send(archive);
|
||||
} catch (error) {
|
||||
return res.status(error.message.includes("250 MB") ? 413 : 500).json({error: "Workspace archive could not be created"});
|
||||
}
|
||||
});
|
||||
router.post("/imports/workspace/archive/preview", readArchive, (req, res) => {
|
||||
const validation = validateWorkspaceImport(req.archive.workspace, repositories.exportWorkspace(req.session.user));
|
||||
if (validation.ok) {
|
||||
try { archiveImages(validation.workspace, req.archive.images); }
|
||||
catch (error) { validation.ok = false; validation.errors.push(error.message); }
|
||||
}
|
||||
return res.status(validation.ok ? 200 : 400).json({ok: validation.ok, errors: validation.errors, warnings: validation.warnings, preview: validation.preview});
|
||||
});
|
||||
router.post("/imports/workspace/archive", readArchive, async (req, res) => {
|
||||
const validation = validateWorkspaceImport(req.archive.workspace, repositories.exportWorkspace(req.session.user));
|
||||
if (!validation.ok) return res.status(400).json({error: "Workspace import validation failed", errors: validation.errors});
|
||||
if (validation.preview.duplicates.length && req.body?.allowDuplicates !== "true") {
|
||||
return res.status(409).json({error: "Workspace import contains duplicate records", warnings: validation.warnings, preview: validation.preview});
|
||||
}
|
||||
let images;
|
||||
try { images = archiveImages(validation.workspace, req.archive.images); }
|
||||
catch (error) { return res.status(400).json({error: error.message}); }
|
||||
const restored = [];
|
||||
try {
|
||||
const ids = new Map();
|
||||
for (const [oldId, file] of images) {
|
||||
const uploaded = await attachmentService.upload(file, req.session.user, {forImport: true});
|
||||
restored.push(uploaded.id);
|
||||
ids.set(oldId, uploaded.id);
|
||||
}
|
||||
for (const chat of validation.workspace.chats) for (const message of chat.messages) {
|
||||
message.attachments = message.attachments.map(attachment => ({id: ids.get(attachment.id)}));
|
||||
}
|
||||
const imported = repositories.importWorkspace(req.session.user, validation.workspace);
|
||||
return res.json({imported, preview: validation.preview});
|
||||
} catch (error) {
|
||||
for (const id of restored) await attachmentService.remove(id, req.session.user).catch(() => {});
|
||||
return res.status(error.status || 400).json({error: error.message || "Workspace archive import failed"});
|
||||
}
|
||||
});
|
||||
router.post("/imports/workspace/preview", (req, res) => {
|
||||
const validation = validateWorkspaceImport(req.body?.workspace, repositories.exportWorkspace(req.session.user));
|
||||
return res.status(validation.ok ? 200 : 400).json({
|
||||
|
||||
@@ -121,7 +121,7 @@ export function createPersonasRouter({repositories, chatService, modelService, c
|
||||
return res.json({chat: chatService.getOrCreatePersonaChat(user, persona, userPersonaId, null, null)});
|
||||
} catch (error) {
|
||||
if (error.message === "CHAT_LIMIT_REACHED") {
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} chats reached. Please delete an old chat to create a new one.`});
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} active chats reached. Archive or delete an active chat to create a new one.`});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
@@ -245,7 +245,7 @@ export function createPersonasRouter({repositories, chatService, modelService, c
|
||||
chat = chatService.getOrCreatePersonaChat(user, persona, userPersonaId, scenarioPrompt, sceneSummary);
|
||||
} catch (error) {
|
||||
if (error.message === "CHAT_LIMIT_REACHED") {
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} chats reached. Please delete an old chat to create a new one.`});
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} active chats reached. Archive or delete an active chat to create a new one.`});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
@@ -300,7 +300,7 @@ export function createPersonasRouter({repositories, chatService, modelService, c
|
||||
chat = chatService.getOrCreatePersonaChat(user, assistantPersona, userPersonaId, scenarioPrompt, sceneSummary);
|
||||
} catch (error) {
|
||||
if (error.message === "CHAT_LIMIT_REACHED") {
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} chats reached. Please delete an old chat to create a new one.`});
|
||||
return res.status(400).json({error: `Maximum of ${config.chatLimit} active chats reached. Archive or delete an active chat to create a new one.`});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ export function createAttachmentService(db, config, modelService, visionDebugSer
|
||||
db.prepare("DELETE FROM attachments WHERE id=?").run(id);
|
||||
return cleanup();
|
||||
},
|
||||
async upload(file, user) {
|
||||
async upload(file, user, {forImport = false} = {}) {
|
||||
await cleanup();
|
||||
if (!file?.buffer?.length) throw new AttachmentError("Choose a non-empty PNG, JPEG or WebP image");
|
||||
if (file.size > config.uploadMaxSizeMb * 1024 * 1024) throw new AttachmentError("Image is too large", 413);
|
||||
@@ -178,9 +178,9 @@ export function createAttachmentService(db, config, modelService, visionDebugSer
|
||||
try {
|
||||
// Bound abandoned uploads per account as well as per submitted message.
|
||||
db.transaction(() => {
|
||||
if (db.prepare("SELECT COUNT(*) AS n FROM attachments WHERE username=? AND status='temporary'").get(user).n >= config.uploadMaxFiles * 4) throw new AttachmentError("Too many pending images. Remove unused uploads first.");
|
||||
if (!forImport && db.prepare("SELECT COUNT(*) AS n FROM attachments WHERE username=? AND status='temporary'").get(user).n >= config.uploadMaxFiles * 4) throw new AttachmentError("Too many pending images. Remove unused uploads first.");
|
||||
db.prepare("INSERT INTO attachments(id,username,original_name,storage_key,mime_type,size_bytes,width,height) VALUES(?,?,?,?,?,?,?,?)").run(id, user,
|
||||
String(file.originalname || "image").replace(/[\\/\x00-\x1f]/g, "_").slice(0, 255), storageKey, `image/${info.format}`, file.size, info.width, info.height);
|
||||
[...String(file.originalname || "image").replace(/[\\/]/g, "_")].map(character => character.charCodeAt(0) < 32 ? "_" : character).join("").slice(0, 255), storageKey, `image/${info.format}`, file.size, info.width, info.height);
|
||||
})();
|
||||
} catch (error) { await storage.delete(storageKey); throw error; }
|
||||
log("attachmentUploadSuccess", {id, size: file.size, mime: `image/${info.format}`});
|
||||
|
||||
@@ -584,7 +584,7 @@ export function createChatService(repositories, modelService, config, webSearchP
|
||||
|
||||
const cloneChatThroughMessage = ({ user, sourceChat, messageId = null }) => {
|
||||
if (repositories.countChats(user) >= config.chatLimit) {
|
||||
return { error: `Maximum of ${config.chatLimit} chats reached. Please delete an old chat to create a new one.`, status: 400 };
|
||||
return { error: `Maximum of ${config.chatLimit} active chats reached. Archive or delete an active chat to create a new one.`, status: 400 };
|
||||
}
|
||||
|
||||
const sourceMessages = messageId == null
|
||||
|
||||
Reference in New Issue
Block a user