mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-05-13 16:07:30 +00:00
* feat: Enhance favorites functionality to support model specs * refactor(FavoritesList): reorder imports based on lenght * feat: improve favorite modelSpec controller; refactor: useIsActiveItem hook * refactor: consolidate Favorite type, harden controller, add tests - Add canonical TUserFavorite type in data-provider, replace three duplicate definitions (data-service, data-schemas, store/favorites) - Consolidate FavoritesController spec validation into single block, add return on 500 paths, add maxlength to mongoose sub-schema - Fix import order in FavoritesList.tsx, merge namespace type imports in FavoriteItem.tsx and FavoritesList.tsx - Add focus-visible:ring-inset on pin buttons to prevent ring clipping - Add explicit return type and JSDoc on useIsActiveItem hook - Use props.type for narrowing consistency in FavoriteItem getTypeLabel - Add 22 backend tests for FavoritesController (spec validation, typeCount exclusivity, persistence, GET path) - Add 40 frontend tests: useFavorites spec methods, useIsActiveItem observer lifecycle, ModelSpecItem pin button, FavoriteItem all three type branches, FavoritesList spec rendering * fix: address PR review findings for pin model specs - Harden backend validation to reject partial cross-type fields (e.g. spec+endpoint, agentId+model without endpoint) - Add stale-spec auto-cleanup in FavoritesList mirroring agent cleanup - Add type="button" to pin buttons in ModelSpecItem/EndpointModelItem - Fix import order violations in EndpointModelItem and ModelSpecItem - Remove hollow test, dead key prop, inline trivial helpers - Fix misleading test description, add onSelectSpec to test mock - Add return to controller success responses for consistency - Add 6 backend tests for partial cross-type field validation * fix: guard stale-spec cleanup against unloaded startupConfig Prevents race condition where spec favorites are incorrectly deleted on cold start before startupConfig has loaded. Mirrors the existing agentsMap === undefined guard pattern used for stale agent cleanup. Also adds tests for stale-spec cleanup persistence and fixes namespace import pattern in FavoritesList.spec.tsx. * fix: replace nested ternaries with if/else in FavoriteItem Resolves ESLint no-nested-ternary warnings for name and typeLabel derivations. --------- Co-authored-by: Danny Avila <danny@librechat.ai>
128 lines
3.9 KiB
JavaScript
128 lines
3.9 KiB
JavaScript
const { updateUser, getUserById } = require('~/models');
|
|
|
|
const MAX_FAVORITES = 50;
|
|
const MAX_STRING_LENGTH = 256;
|
|
|
|
const updateFavoritesController = async (req, res) => {
|
|
try {
|
|
const { favorites } = req.body;
|
|
const userId = req.user.id;
|
|
|
|
if (!favorites) {
|
|
return res.status(400).json({ message: 'Favorites data is required' });
|
|
}
|
|
|
|
if (!Array.isArray(favorites)) {
|
|
return res.status(400).json({ message: 'Favorites must be an array' });
|
|
}
|
|
|
|
if (favorites.length > MAX_FAVORITES) {
|
|
return res.status(400).json({
|
|
code: 'MAX_FAVORITES_EXCEEDED',
|
|
message: `Maximum ${MAX_FAVORITES} favorites allowed`,
|
|
limit: MAX_FAVORITES,
|
|
});
|
|
}
|
|
|
|
for (const fav of favorites) {
|
|
const hasAgent = !!fav.agentId;
|
|
const hasModel = !!(fav.model && fav.endpoint);
|
|
const hasSpec = !!fav.spec;
|
|
|
|
if (fav.agentId && fav.agentId.length > MAX_STRING_LENGTH) {
|
|
return res
|
|
.status(400)
|
|
.json({ message: `agentId exceeds maximum length of ${MAX_STRING_LENGTH}` });
|
|
}
|
|
if (fav.model && fav.model.length > MAX_STRING_LENGTH) {
|
|
return res
|
|
.status(400)
|
|
.json({ message: `model exceeds maximum length of ${MAX_STRING_LENGTH}` });
|
|
}
|
|
if (fav.endpoint && fav.endpoint.length > MAX_STRING_LENGTH) {
|
|
return res
|
|
.status(400)
|
|
.json({ message: `endpoint exceeds maximum length of ${MAX_STRING_LENGTH}` });
|
|
}
|
|
if (fav.spec !== undefined && fav.spec !== null) {
|
|
if (typeof fav.spec !== 'string' || fav.spec.length === 0) {
|
|
return res.status(400).json({ message: 'spec must be a non-empty string' });
|
|
}
|
|
if (fav.spec.length > MAX_STRING_LENGTH) {
|
|
return res
|
|
.status(400)
|
|
.json({ message: `spec exceeds maximum length of ${MAX_STRING_LENGTH}` });
|
|
}
|
|
}
|
|
|
|
const hasPartialModel = !hasModel && !!(fav.model || fav.endpoint);
|
|
|
|
if (hasPartialModel && !hasAgent && !hasSpec) {
|
|
return res.status(400).json({ message: 'model and endpoint must be provided together' });
|
|
}
|
|
|
|
const typeCount = [hasAgent, hasModel, hasSpec].filter(Boolean).length;
|
|
if (typeCount === 0) {
|
|
return res.status(400).json({
|
|
message: 'Each favorite must have either agentId, model+endpoint, or spec',
|
|
});
|
|
}
|
|
|
|
if (typeCount > 1) {
|
|
return res.status(400).json({
|
|
message: 'Favorite cannot have multiple types (agentId, model/endpoint, or spec)',
|
|
});
|
|
}
|
|
|
|
if (hasSpec && (fav.agentId || fav.model || fav.endpoint)) {
|
|
return res
|
|
.status(400)
|
|
.json({ message: 'spec cannot be combined with agentId, model, or endpoint' });
|
|
}
|
|
if (hasAgent && (fav.model || fav.endpoint)) {
|
|
return res
|
|
.status(400)
|
|
.json({ message: 'agentId cannot be combined with model or endpoint' });
|
|
}
|
|
}
|
|
|
|
const user = await updateUser(userId, { favorites });
|
|
|
|
if (!user) {
|
|
return res.status(404).json({ message: 'User not found' });
|
|
}
|
|
|
|
return res.status(200).json(user.favorites);
|
|
} catch (error) {
|
|
console.error('Error updating favorites:', error);
|
|
return res.status(500).json({ message: 'Internal server error' });
|
|
}
|
|
};
|
|
|
|
const getFavoritesController = async (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
const user = await getUserById(userId, 'favorites');
|
|
|
|
if (!user) {
|
|
return res.status(404).json({ message: 'User not found' });
|
|
}
|
|
|
|
let favorites = user.favorites || [];
|
|
|
|
if (!Array.isArray(favorites)) {
|
|
favorites = [];
|
|
await updateUser(userId, { favorites: [] });
|
|
}
|
|
|
|
return res.status(200).json(favorites);
|
|
} catch (error) {
|
|
console.error('Error fetching favorites:', error);
|
|
return res.status(500).json({ message: 'Internal server error' });
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
updateFavoritesController,
|
|
getFavoritesController,
|
|
};
|