mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* feat: add timer duration to showToast, show toast for preset selection * refactor: replace old /chat/ route with /c/. e2e tests will fail here * refactor: move typedefs to root of /api/ and add a few to assistant types in TS * refactor: reorganize data-provider imports, fix dependency cycle, strategize new plan to separate react dependent packages * feat: add dataService for uploading images * feat(data-provider): add mutation keys * feat: file resizing and upload * WIP: initial API image handling * fix: catch JSON.parse of localStorage tools * chore: experimental: use module-alias for absolute imports * refactor: change temp_file_id strategy * fix: updating files state by using Map and defining react query callbacks in a way that keeps them during component unmount, initial delete handling * feat: properly handle file deletion * refactor: unexpose complete filepath and resize from server for higher fidelity * fix: make sure resized height, width is saved, catch bad requests * refactor: use absolute imports * fix: prevent setOptions from being called more than once for OpenAIClient, made note to fix for PluginsClient * refactor: import supportsFiles and models vars from schemas * fix: correctly replace temp file id * refactor(BaseClient): use absolute imports, pass message 'opts' to buildMessages method, count tokens for nested objects/arrays * feat: add validateVisionModel to determine if model has vision capabilities * chore(checkBalance): update jsdoc * feat: formatVisionMessage: change message content format dependent on role and image_urls passed * refactor: add usage to File schema, make create and updateFile, correctly set and remove TTL * feat: working vision support TODO: file size, type, amount validations, making sure they are styled right, and making sure you can add images from the clipboard/dragging * feat: clipboard support for uploading images * feat: handle files on drop to screen, refactor top level view code to Presentation component so the useDragHelpers hook has ChatContext * fix(Images): replace uploaded images in place * feat: add filepath validation to protect sensitive files * fix: ensure correct file_ids are push and not the Map key values * fix(ToastContext): type issue * feat: add basic file validation * fix(useDragHelpers): correct context issue with `files` dependency * refactor: consolidate setErrors logic to setError * feat: add dialog Image overlay on image click * fix: close endpoints menu on click * chore: set detail to auto, make note for configuration * fix: react warning (button desc. of button) * refactor: optimize filepath handling, pass file_ids to images for easier re-use * refactor: optimize image file handling, allow re-using files in regen, pass more file metadata in messages * feat: lazy loading images including use of upload preview * fix: SetKeyDialog closing, stopPropagation on Dialog content click * style(EndpointMenuItem): tighten up the style, fix dark theme showing in lightmode, make menu more ux friendly * style: change maxheight of all settings textareas to 138px from 300px * style: better styling for textarea and enclosing buttons * refactor(PresetItems): swap back edit and delete icons * feat: make textarea placeholder dynamic to endpoint * style: show user hover buttons only on hover when message is streaming * fix: ordered list not going past 9, fix css * feat: add User/AI labels; style: hide loading spinner * feat: add back custom footer, change original footer text * feat: dynamic landing icons based on endpoint * chore: comment out assistants route * fix: autoScroll to newest on /c/ view * fix: Export Conversation on new UI * style: match message style of official more closely * ci: fix api jest unit tests, comment out e2e tests for now as they will fail until addressed * feat: more file validation and use blob in preview field, not filepath, to fix temp deletion * feat: filefilter for multer * feat: better AI labels based on custom name, model, and endpoint instead of `ChatGPT`
158 lines
4.2 KiB
JavaScript
158 lines
4.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { sendMessage, createOnProgress } = require('~/server/utils');
|
|
const { saveMessage, getConvoTitle, getConvo } = require('~/models');
|
|
const { getResponseSender } = require('~/server/routes/endpoints/schemas');
|
|
const { addTitle, initializeClient } = require('~/server/routes/endpoints/openAI');
|
|
const {
|
|
handleAbort,
|
|
createAbortController,
|
|
handleAbortError,
|
|
setHeaders,
|
|
validateEndpoint,
|
|
buildEndpointOption,
|
|
} = require('~/server/middleware');
|
|
|
|
router.post('/abort', handleAbort());
|
|
|
|
router.post('/', validateEndpoint, buildEndpointOption, setHeaders, async (req, res) => {
|
|
let {
|
|
text,
|
|
endpointOption,
|
|
conversationId,
|
|
parentMessageId = null,
|
|
overrideParentMessageId = null,
|
|
} = req.body;
|
|
console.log('ask log');
|
|
console.dir({ text, conversationId, endpointOption }, { depth: null });
|
|
let metadata;
|
|
let userMessage;
|
|
let promptTokens;
|
|
let userMessageId;
|
|
let responseMessageId;
|
|
let lastSavedTimestamp = 0;
|
|
let saveDelay = 100;
|
|
const sender = getResponseSender(endpointOption);
|
|
const newConvo = !conversationId;
|
|
const user = req.user.id;
|
|
|
|
const addMetadata = (data) => (metadata = data);
|
|
|
|
const getReqData = (data = {}) => {
|
|
for (let key in data) {
|
|
if (key === 'userMessage') {
|
|
userMessage = data[key];
|
|
userMessageId = data[key].messageId;
|
|
} else if (key === 'responseMessageId') {
|
|
responseMessageId = data[key];
|
|
} else if (key === 'promptTokens') {
|
|
promptTokens = data[key];
|
|
} else if (!conversationId && key === 'conversationId') {
|
|
conversationId = data[key];
|
|
}
|
|
}
|
|
};
|
|
|
|
const { onProgress: progressCallback, getPartialText } = createOnProgress({
|
|
onProgress: ({ text: partialText }) => {
|
|
const currentTimestamp = Date.now();
|
|
|
|
if (currentTimestamp - lastSavedTimestamp > saveDelay) {
|
|
lastSavedTimestamp = currentTimestamp;
|
|
saveMessage({
|
|
messageId: responseMessageId,
|
|
sender,
|
|
conversationId,
|
|
parentMessageId: overrideParentMessageId ?? userMessageId,
|
|
text: partialText,
|
|
model: endpointOption.modelOptions.model,
|
|
unfinished: true,
|
|
cancelled: false,
|
|
error: false,
|
|
user,
|
|
});
|
|
}
|
|
|
|
if (saveDelay < 500) {
|
|
saveDelay = 500;
|
|
}
|
|
},
|
|
});
|
|
|
|
const getAbortData = () => ({
|
|
sender,
|
|
conversationId,
|
|
messageId: responseMessageId,
|
|
parentMessageId: overrideParentMessageId ?? userMessageId,
|
|
text: getPartialText(),
|
|
userMessage,
|
|
promptTokens,
|
|
});
|
|
|
|
const { abortController, onStart } = createAbortController(req, res, getAbortData);
|
|
|
|
try {
|
|
const { client } = await initializeClient({ req, res, endpointOption });
|
|
const messageOptions = {
|
|
user,
|
|
parentMessageId,
|
|
conversationId,
|
|
overrideParentMessageId,
|
|
getReqData,
|
|
onStart,
|
|
addMetadata,
|
|
abortController,
|
|
onProgress: progressCallback.call(null, {
|
|
res,
|
|
text,
|
|
parentMessageId: overrideParentMessageId || userMessageId,
|
|
}),
|
|
};
|
|
|
|
let response = await client.sendMessage(text, messageOptions);
|
|
|
|
if (overrideParentMessageId) {
|
|
response.parentMessageId = overrideParentMessageId;
|
|
}
|
|
|
|
if (metadata) {
|
|
response = { ...response, ...metadata };
|
|
}
|
|
|
|
if (client.options.attachments) {
|
|
userMessage.files = client.options.attachments;
|
|
delete userMessage.image_urls;
|
|
}
|
|
|
|
sendMessage(res, {
|
|
title: await getConvoTitle(user, conversationId),
|
|
final: true,
|
|
conversation: await getConvo(user, conversationId),
|
|
requestMessage: userMessage,
|
|
responseMessage: response,
|
|
});
|
|
res.end();
|
|
|
|
await saveMessage({ ...response, user });
|
|
await saveMessage(userMessage);
|
|
|
|
if (parentMessageId === '00000000-0000-0000-0000-000000000000' && newConvo) {
|
|
addTitle(req, {
|
|
text,
|
|
response,
|
|
client,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
const partialText = getPartialText();
|
|
handleAbortError(res, req, error, {
|
|
partialText,
|
|
conversationId,
|
|
sender,
|
|
messageId: responseMessageId,
|
|
parentMessageId: userMessageId ?? parentMessageId,
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|