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

* chore: comment back handlePlusCommand * chore: ignore .git dir * refactor: pass newConversation to `useSelectMention` refactor: pass newConversation to Mention component refactor: useChatFunctions for modular use of `ask` and `regenerate` refactor: set latest message only for the first index in useChatFunctions refactor: pass setLatestMessage to useChatFunctions refactor: Pass setSubmission to useChatFunctions for submission handling refactor: consolidate event handlers to separate hook from useSSE WIP: additional response handlers feat: responsive added convo, clears on new chat/navigating to chat, assistants excluded feat: Add conversationByKeySelector to select any conversation by index WIP: handle second submission with messages paired to root * style: surface-primary-contrast * refactor: remove unnecessary console.log statement in useChatFunctions * refactor: Consolidate imports in ChatForm and Input hooks * refactor: compositional usage of useSSE for multiple streams * WIP: set latest 'multi' message * WIP: first pass, added response streaming * pass: performant multi-message stream * fix: styling and message render * second pass: modular, performant multi-stream * fix: align parentMessageId of multiMessage * refactor: move resetting latestMultiMessage * chore: update footer text in Chat component * fix: stop button styling * fix: handle abortMessage request for multi-response * clear messages but bug with latest message reset present * fix: add delay for additional message generation * fix: access LAST_CONVO_SETUP by index * style: add div to prevent layout shift before hover buttons render * chore: Update Message component styling for card messages * chore: move hook use order * fix: abort middleware using unsent field from req.body * feat: support multi-response stream from initial message * refactor: buildTree function to improve readability and remove unused code * feat: add logger for frontend dev * refactor: use depth to track if message is really last in its branch * fix(buildTree): default export * fix: share parent message Id and avoid duplication error for multi-response streams * fix: prevent addedConvo reset to response convo * feat: allow setting multi message as latest message to control which to respond to * chore: wrap setSiblingIdxRev with useCallback * chore: styling and allow editing messages * style: styling fixes * feat: Add "AddMultiConvo" component to Chat Header * feat: prevent clearing added convos on endpoint, preset, mention, or modelSpec switch * fix: message styling fixes, mainly related to code blocks * fix: stop button visibility logic * fix: Handle edge case in abortMiddleware for non-existant `abortControllers` * refactor: optimize/memoize icons * chore(GoogleClient): change info to debug logs * style: active message styling * style: prevent layout shift due to placeholder row * chore: remove unused code * fix: Update BaseClient to handle optional request body properties * fix(ci): `onStart` now accepts 2 args, the 2nd being responseMessageId * chore: bump data-provider
203 lines
5.6 KiB
JavaScript
203 lines
5.6 KiB
JavaScript
const express = require('express');
|
|
const throttle = require('lodash/throttle');
|
|
const { getResponseSender } = require('librechat-data-provider');
|
|
const {
|
|
handleAbort,
|
|
createAbortController,
|
|
handleAbortError,
|
|
setHeaders,
|
|
validateModel,
|
|
validateEndpoint,
|
|
buildEndpointOption,
|
|
moderateText,
|
|
} = require('~/server/middleware');
|
|
const { sendMessage, createOnProgress, formatSteps, formatAction } = require('~/server/utils');
|
|
const { initializeClient } = require('~/server/services/Endpoints/gptPlugins');
|
|
const { saveMessage, getConvoTitle, getConvo } = require('~/models');
|
|
const { validateTools } = require('~/app');
|
|
const { logger } = require('~/config');
|
|
|
|
const router = express.Router();
|
|
|
|
router.use(moderateText);
|
|
router.post('/abort', handleAbort());
|
|
|
|
router.post(
|
|
'/',
|
|
validateEndpoint,
|
|
validateModel,
|
|
buildEndpointOption,
|
|
setHeaders,
|
|
async (req, res) => {
|
|
let {
|
|
text,
|
|
generation,
|
|
endpointOption,
|
|
conversationId,
|
|
responseMessageId,
|
|
isContinued = false,
|
|
parentMessageId = null,
|
|
overrideParentMessageId = null,
|
|
} = req.body;
|
|
|
|
logger.debug('[/edit/gptPlugins]', {
|
|
text,
|
|
generation,
|
|
isContinued,
|
|
conversationId,
|
|
...endpointOption,
|
|
});
|
|
|
|
let userMessage;
|
|
let promptTokens;
|
|
const sender = getResponseSender({
|
|
...endpointOption,
|
|
model: endpointOption.modelOptions.model,
|
|
});
|
|
const userMessageId = parentMessageId;
|
|
const user = req.user.id;
|
|
|
|
const plugin = {
|
|
loading: true,
|
|
inputs: [],
|
|
latest: null,
|
|
outputs: null,
|
|
};
|
|
|
|
const getReqData = (data = {}) => {
|
|
for (let key in data) {
|
|
if (key === 'userMessage') {
|
|
userMessage = data[key];
|
|
} else if (key === 'responseMessageId') {
|
|
responseMessageId = data[key];
|
|
} else if (key === 'promptTokens') {
|
|
promptTokens = data[key];
|
|
}
|
|
}
|
|
};
|
|
|
|
const throttledSaveMessage = throttle(saveMessage, 3000, { trailing: false });
|
|
const {
|
|
onProgress: progressCallback,
|
|
sendIntermediateMessage,
|
|
getPartialText,
|
|
} = createOnProgress({
|
|
generation,
|
|
onProgress: ({ text: partialText }) => {
|
|
if (plugin.loading === true) {
|
|
plugin.loading = false;
|
|
}
|
|
|
|
throttledSaveMessage({
|
|
messageId: responseMessageId,
|
|
sender,
|
|
conversationId,
|
|
parentMessageId: overrideParentMessageId || userMessageId,
|
|
text: partialText,
|
|
model: endpointOption.modelOptions.model,
|
|
unfinished: true,
|
|
isEdited: true,
|
|
error: false,
|
|
user,
|
|
});
|
|
},
|
|
});
|
|
|
|
const onChainEnd = (data) => {
|
|
let { intermediateSteps: steps } = data;
|
|
plugin.outputs = steps && steps[0].action ? formatSteps(steps) : 'An error occurred.';
|
|
plugin.loading = false;
|
|
saveMessage({ ...userMessage, user });
|
|
sendIntermediateMessage(res, {
|
|
plugin,
|
|
parentMessageId: userMessage.messageId,
|
|
messageId: responseMessageId,
|
|
});
|
|
// logger.debug('CHAIN END', plugin.outputs);
|
|
};
|
|
|
|
const getAbortData = () => ({
|
|
sender,
|
|
conversationId,
|
|
messageId: responseMessageId,
|
|
parentMessageId: overrideParentMessageId ?? userMessageId,
|
|
text: getPartialText(),
|
|
plugin: { ...plugin, loading: false },
|
|
userMessage,
|
|
promptTokens,
|
|
});
|
|
const { abortController, onStart } = createAbortController(req, res, getAbortData, getReqData);
|
|
|
|
try {
|
|
endpointOption.tools = await validateTools(user, endpointOption.tools);
|
|
const { client } = await initializeClient({ req, res, endpointOption });
|
|
|
|
const onAgentAction = (action, start = false) => {
|
|
const formattedAction = formatAction(action);
|
|
plugin.inputs.push(formattedAction);
|
|
plugin.latest = formattedAction.plugin;
|
|
if (!start && !client.skipSaveUserMessage) {
|
|
saveMessage({ ...userMessage, user });
|
|
}
|
|
sendIntermediateMessage(res, {
|
|
plugin,
|
|
parentMessageId: userMessage.messageId,
|
|
messageId: responseMessageId,
|
|
});
|
|
// logger.debug('PLUGIN ACTION', formattedAction);
|
|
};
|
|
|
|
let response = await client.sendMessage(text, {
|
|
user,
|
|
generation,
|
|
isContinued,
|
|
isEdited: true,
|
|
conversationId,
|
|
parentMessageId,
|
|
responseMessageId,
|
|
overrideParentMessageId,
|
|
getReqData,
|
|
onAgentAction,
|
|
onChainEnd,
|
|
onStart,
|
|
...endpointOption,
|
|
progressCallback,
|
|
progressOptions: {
|
|
res,
|
|
text,
|
|
plugin,
|
|
// parentMessageId: overrideParentMessageId || userMessageId,
|
|
},
|
|
abortController,
|
|
});
|
|
|
|
if (overrideParentMessageId) {
|
|
response.parentMessageId = overrideParentMessageId;
|
|
}
|
|
|
|
logger.debug('[/edit/gptPlugins] CLIENT RESPONSE', response);
|
|
response.plugin = { ...plugin, loading: false };
|
|
await saveMessage({ ...response, user });
|
|
|
|
sendMessage(res, {
|
|
title: await getConvoTitle(user, conversationId),
|
|
final: true,
|
|
conversation: await getConvo(user, conversationId),
|
|
requestMessage: userMessage,
|
|
responseMessage: response,
|
|
});
|
|
res.end();
|
|
} catch (error) {
|
|
const partialText = getPartialText();
|
|
handleAbortError(res, req, error, {
|
|
partialText,
|
|
conversationId,
|
|
sender,
|
|
messageId: responseMessageId,
|
|
parentMessageId: userMessageId ?? parentMessageId,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
|
|
module.exports = router;
|