mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
* chore: bump openai to 4.29.0 and npm audit fix * chore: remove unnecessary stream field from ContentData * feat: new enum and types for AssistantStreamEvent * refactor(AssistantService): remove stream field and add conversationId to text ContentData > - return `finalMessage` and `text` on run completion > - move `processMessages` to services/Threads to avoid circular dependencies with new stream handling > - refactor(processMessages/retrieveAndProcessFile): add new `client` field to differentiate new RunClient type * WIP: new assistants stream handling * chore: stores messages to StreamRunManager * chore: add additional typedefs * fix: pass req and openai to StreamRunManager * fix(AssistantService): pass openai as client to `retrieveAndProcessFile` * WIP: streaming tool i/o, handle in_progress and completed run steps * feat(assistants): process required actions with streaming enabled * chore: condense early return check for useSSE useEffect * chore: remove unnecessary comments and only handle completed tool calls when not function * feat: add TTL for assistants run abort cacheKey * feat: abort stream runs * fix(assistants): render streaming cursor * fix(assistants): hide edit icon as functionality is not supported * fix(textArea): handle pasting edge cases; first, when onChange events wouldn't fire; second, when textarea wouldn't resize * chore: memoize Conversations * chore(useTextarea): reverse args order * fix: load default capabilities when an azure is configured to support assistants, but `assistants` endpoint is not configured * fix(AssistantSelect): update form assistant model on assistant form select * fix(actions): handle azure strict validation for function names to fix crud for actions * chore: remove content data debug log as it fires in rapid succession * feat: improve UX for assistant errors mid-request * feat: add tool call localizations and replace any domain separators from azure action names * refactor(chat): error out tool calls without outputs during handleError * fix(ToolService): handle domain separators allowing Azure use of actions * refactor(StreamRunManager): types and throw Error if tool submission fails
93 lines
2.8 KiB
JavaScript
93 lines
2.8 KiB
JavaScript
const { CacheKeys, RunStatus, isUUID } = require('librechat-data-provider');
|
|
const { initializeClient } = require('~/server/services/Endpoints/assistants');
|
|
const { checkMessageGaps, recordUsage } = require('~/server/services/Threads');
|
|
const { getConvo } = require('~/models/Conversation');
|
|
const getLogStores = require('~/cache/getLogStores');
|
|
const { sendMessage } = require('~/server/utils');
|
|
const { logger } = require('~/config');
|
|
|
|
const three_minutes = 1000 * 60 * 3;
|
|
|
|
async function abortRun(req, res) {
|
|
res.setHeader('Content-Type', 'application/json');
|
|
const { abortKey } = req.body;
|
|
const [conversationId, latestMessageId] = abortKey.split(':');
|
|
const conversation = await getConvo(req.user.id, conversationId);
|
|
|
|
if (conversation?.model) {
|
|
req.body.model = conversation.model;
|
|
}
|
|
|
|
if (!isUUID.safeParse(conversationId).success) {
|
|
logger.error('[abortRun] Invalid conversationId', { conversationId });
|
|
return res.status(400).send({ message: 'Invalid conversationId' });
|
|
}
|
|
|
|
const cacheKey = `${req.user.id}:${conversationId}`;
|
|
const cache = getLogStores(CacheKeys.ABORT_KEYS);
|
|
const runValues = await cache.get(cacheKey);
|
|
const [thread_id, run_id] = runValues.split(':');
|
|
|
|
if (!run_id) {
|
|
logger.warn('[abortRun] Couldn\'t find run for cancel request', { thread_id });
|
|
return res.status(204).send({ message: 'Run not found' });
|
|
} else if (run_id === 'cancelled') {
|
|
logger.warn('[abortRun] Run already cancelled', { thread_id });
|
|
return res.status(204).send({ message: 'Run already cancelled' });
|
|
}
|
|
|
|
let runMessages = [];
|
|
/** @type {{ openai: OpenAI }} */
|
|
const { openai } = await initializeClient({ req, res });
|
|
|
|
try {
|
|
await cache.set(cacheKey, 'cancelled', three_minutes);
|
|
const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id);
|
|
logger.debug('[abortRun] Cancelled run:', cancelledRun);
|
|
} catch (error) {
|
|
logger.error('[abortRun] Error cancelling run', error);
|
|
if (
|
|
error?.message?.includes(RunStatus.CANCELLED) ||
|
|
error?.message?.includes(RunStatus.CANCELLING)
|
|
) {
|
|
return res.end();
|
|
}
|
|
}
|
|
|
|
try {
|
|
const run = await openai.beta.threads.runs.retrieve(thread_id, run_id);
|
|
await recordUsage({
|
|
...run.usage,
|
|
model: run.model,
|
|
user: req.user.id,
|
|
conversationId,
|
|
});
|
|
} catch (error) {
|
|
logger.error('[abortRun] Error fetching or processing run', error);
|
|
}
|
|
|
|
runMessages = await checkMessageGaps({
|
|
openai,
|
|
latestMessageId,
|
|
thread_id,
|
|
run_id,
|
|
conversationId,
|
|
});
|
|
|
|
const finalEvent = {
|
|
title: 'New Chat',
|
|
final: true,
|
|
conversation,
|
|
runMessages,
|
|
};
|
|
|
|
if (res.headersSent && finalEvent) {
|
|
return sendMessage(res, finalEvent);
|
|
}
|
|
|
|
res.json(finalEvent);
|
|
}
|
|
|
|
module.exports = {
|
|
abortRun,
|
|
};
|