mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 17:30:16 +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
148 lines
4.6 KiB
JavaScript
148 lines
4.6 KiB
JavaScript
const { AuthTypeEnum, EModelEndpoint, actionDomainSeparator } = require('librechat-data-provider');
|
|
const { encryptV2, decryptV2 } = require('~/server/utils/crypto');
|
|
const { getActions } = require('~/models/Action');
|
|
const { logger } = require('~/config');
|
|
|
|
/**
|
|
* Parses the domain for an action.
|
|
*
|
|
* Azure OpenAI Assistants API doesn't support periods in function
|
|
* names due to `[a-zA-Z0-9_-]*` Regex Validation.
|
|
*
|
|
* @param {Express.Request} req - Express Request object
|
|
* @param {string} domain - The domain for the actoin
|
|
* @param {boolean} inverse - If true, replaces periods with `actionDomainSeparator`
|
|
* @returns {string} The parsed domain
|
|
*/
|
|
function domainParser(req, domain, inverse = false) {
|
|
if (!domain) {
|
|
return;
|
|
}
|
|
|
|
if (!req.app.locals[EModelEndpoint.azureOpenAI]?.assistants) {
|
|
return domain;
|
|
}
|
|
|
|
if (inverse) {
|
|
return domain.replace(/\./g, actionDomainSeparator);
|
|
}
|
|
|
|
return domain.replace(actionDomainSeparator, '.');
|
|
}
|
|
|
|
/**
|
|
* Loads action sets based on the user and assistant ID.
|
|
*
|
|
* @param {Object} searchParams - The parameters for loading action sets.
|
|
* @param {string} searchParams.user - The user identifier.
|
|
* @param {string} searchParams.assistant_id - The assistant identifier.
|
|
* @returns {Promise<Action[] | null>} A promise that resolves to an array of actions or `null` if no match.
|
|
*/
|
|
async function loadActionSets(searchParams) {
|
|
return await getActions(searchParams, true);
|
|
}
|
|
|
|
/**
|
|
* Creates a general tool for an entire action set.
|
|
*
|
|
* @param {Object} params - The parameters for loading action sets.
|
|
* @param {Action} params.action - The action set. Necessary for decrypting authentication values.
|
|
* @param {ActionRequest} params.requestBuilder - The ActionRequest builder class to execute the API call.
|
|
* @returns { { _call: (toolInput: Object) => unknown} } An object with `_call` method to execute the tool input.
|
|
*/
|
|
function createActionTool({ action, requestBuilder }) {
|
|
action.metadata = decryptMetadata(action.metadata);
|
|
const _call = async (toolInput) => {
|
|
try {
|
|
requestBuilder.setParams(toolInput);
|
|
if (action.metadata.auth && action.metadata.auth.type !== AuthTypeEnum.None) {
|
|
await requestBuilder.setAuth(action.metadata);
|
|
}
|
|
const res = await requestBuilder.execute();
|
|
if (typeof res.data === 'object') {
|
|
return JSON.stringify(res.data);
|
|
}
|
|
return res.data;
|
|
} catch (error) {
|
|
logger.error(`API call to ${action.metadata.domain} failed`, error);
|
|
if (error.response) {
|
|
const { status, data } = error.response;
|
|
return `API call to ${
|
|
action.metadata.domain
|
|
} failed with status ${status}: ${JSON.stringify(data)}`;
|
|
}
|
|
|
|
return `API call to ${action.metadata.domain} failed.`;
|
|
}
|
|
};
|
|
|
|
return {
|
|
_call,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Encrypts sensitive metadata values for an action.
|
|
*
|
|
* @param {ActionMetadata} metadata - The action metadata to encrypt.
|
|
* @returns {ActionMetadata} The updated action metadata with encrypted values.
|
|
*/
|
|
function encryptMetadata(metadata) {
|
|
const encryptedMetadata = { ...metadata };
|
|
|
|
// ServiceHttp
|
|
if (metadata.auth && metadata.auth.type === AuthTypeEnum.ServiceHttp) {
|
|
if (metadata.api_key) {
|
|
encryptedMetadata.api_key = encryptV2(metadata.api_key);
|
|
}
|
|
}
|
|
|
|
// OAuth
|
|
else if (metadata.auth && metadata.auth.type === AuthTypeEnum.OAuth) {
|
|
if (metadata.oauth_client_id) {
|
|
encryptedMetadata.oauth_client_id = encryptV2(metadata.oauth_client_id);
|
|
}
|
|
if (metadata.oauth_client_secret) {
|
|
encryptedMetadata.oauth_client_secret = encryptV2(metadata.oauth_client_secret);
|
|
}
|
|
}
|
|
|
|
return encryptedMetadata;
|
|
}
|
|
|
|
/**
|
|
* Decrypts sensitive metadata values for an action.
|
|
*
|
|
* @param {ActionMetadata} metadata - The action metadata to decrypt.
|
|
* @returns {ActionMetadata} The updated action metadata with decrypted values.
|
|
*/
|
|
function decryptMetadata(metadata) {
|
|
const decryptedMetadata = { ...metadata };
|
|
|
|
// ServiceHttp
|
|
if (metadata.auth && metadata.auth.type === AuthTypeEnum.ServiceHttp) {
|
|
if (metadata.api_key) {
|
|
decryptedMetadata.api_key = decryptV2(metadata.api_key);
|
|
}
|
|
}
|
|
|
|
// OAuth
|
|
else if (metadata.auth && metadata.auth.type === AuthTypeEnum.OAuth) {
|
|
if (metadata.oauth_client_id) {
|
|
decryptedMetadata.oauth_client_id = decryptV2(metadata.oauth_client_id);
|
|
}
|
|
if (metadata.oauth_client_secret) {
|
|
decryptedMetadata.oauth_client_secret = decryptV2(metadata.oauth_client_secret);
|
|
}
|
|
}
|
|
|
|
return decryptedMetadata;
|
|
}
|
|
|
|
module.exports = {
|
|
loadActionSets,
|
|
createActionTool,
|
|
encryptMetadata,
|
|
decryptMetadata,
|
|
domainParser,
|
|
};
|