🛠️ fix(Azure/Assistants): Handle Long Domain Names & Other Minor chores (#2475)

* chore: replace violation cache accessors with enum

* chore: fix test

* chore(fileSchema): index timestamps

* fix(ActionService): use encoding/caching strategy for handling assistant function character length limit

* refactor(actions): async `domainParser` also resolve retrieved model (which is deployment name) to user-defined model

* style(AssistantAction): add `whitespace-nowrap` for ellipsis

* refactor(ActionService): if domain is less than or equal to encoded domain fixed length, return domain with replacement of separator

* refactor(actions): use sessions/transactions for updating Assistant Action database records

* chore: remove TTL from ENCODED_DOMAINS cache

* refactor(domainParser): minor optimization and add tests

* fix(spendTokens): use txData.user for token usage logging

* refactor(actions): add helper function `withSession` for database operations with sessions/transactions

* fix(PluginsClient): logger debug `message` field edge case
This commit is contained in:
Danny Avila 2024-04-20 15:02:56 -04:00 committed by GitHub
parent 5d642d0187
commit 8c22bb1d3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 365 additions and 63 deletions

View file

@ -1,10 +1,11 @@
const { v4 } = require('uuid');
const express = require('express');
const { actionDelimiter } = require('librechat-data-provider');
const { initializeClient } = require('~/server/services/Endpoints/assistants');
const { encryptMetadata, domainParser } = require('~/server/services/ActionService');
const { actionDelimiter, EModelEndpoint } = require('librechat-data-provider');
const { initializeClient } = require('~/server/services/Endpoints/assistants');
const { updateAction, getActions, deleteAction } = require('~/models/Action');
const { updateAssistant, getAssistant } = require('~/models/Assistant');
const { withSession } = require('~/server/utils');
const { logger } = require('~/config');
const router = express.Router();
@ -46,7 +47,7 @@ router.post('/:assistant_id', async (req, res) => {
let { domain } = metadata;
/* Azure doesn't support periods in function names */
domain = domainParser(req, domain, true);
domain = await domainParser(req, domain, true);
if (!domain) {
return res.status(400).json({ message: 'No domain provided' });
@ -110,7 +111,8 @@ router.post('/:assistant_id', async (req, res) => {
const promises = [];
promises.push(
updateAssistant(
withSession(
updateAssistant,
{ assistant_id },
{
actions,
@ -119,7 +121,9 @@ router.post('/:assistant_id', async (req, res) => {
),
);
promises.push(openai.beta.assistants.update(assistant_id, { tools }));
promises.push(updateAction({ action_id }, { metadata, assistant_id, user: req.user.id }));
promises.push(
withSession(updateAction, { action_id }, { metadata, assistant_id, user: req.user.id }),
);
/** @type {[AssistantDocument, Assistant, Action]} */
const resolved = await Promise.all(promises);
@ -129,6 +133,15 @@ router.post('/:assistant_id', async (req, res) => {
delete resolved[2].metadata[field];
}
}
/* Map Azure OpenAI model to the assistant as defined by config */
if (req.app.locals[EModelEndpoint.azureOpenAI]?.assistants) {
resolved[1] = {
...resolved[1],
model: req.body.model,
};
}
res.json(resolved);
} catch (error) {
const message = 'Trouble updating the Assistant Action';
@ -171,7 +184,7 @@ router.delete('/:assistant_id/:action_id/:model', async (req, res) => {
return true;
});
domain = domainParser(req, domain, true);
domain = await domainParser(req, domain, true);
const updatedTools = tools.filter(
(tool) => !(tool.function && tool.function.name.includes(domain)),
@ -179,7 +192,8 @@ router.delete('/:assistant_id/:action_id/:model', async (req, res) => {
const promises = [];
promises.push(
updateAssistant(
withSession(
updateAssistant,
{ assistant_id },
{
actions: updatedActions,
@ -188,7 +202,7 @@ router.delete('/:assistant_id/:action_id/:model', async (req, res) => {
),
);
promises.push(openai.beta.assistants.update(assistant_id, { tools: updatedTools }));
promises.push(deleteAction({ action_id }));
promises.push(withSession(deleteAction, { action_id }));
await Promise.all(promises);
res.status(200).json({ message: 'Action deleted successfully' });