mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-26 04:14:10 +01:00
🪣 fix: Serve Fresh Presigned URLs on Agent List Cache Hits (#11902)
* fix: serve cached presigned URLs on agent list cache hits On a cache hit the list endpoint was skipping the S3 refresh and returning whatever presigned URL was stored in MongoDB, which could be expired if the S3 URL TTL is shorter than the 30-minute cache window. refreshListAvatars now collects a urlCache map (agentId -> refreshed filepath) alongside its existing stats. The controller stores this map in the cache instead of a plain boolean and re-applies it to every paginated response, guaranteeing clients always receive a URL that was valid as of the last refresh rather than a potentially stale DB value. * fix: improve avatar refresh cache handling and logging Updated the avatar refresh logic to validate cached refresh data before proceeding with S3 URL updates. Enhanced logging to exclude sensitive `urlCache` details while still providing relevant statistics. Added error handling for cache invalidation during avatar updates to ensure robustness. * fix: update avatar refresh logic to clear urlCache on no change Modified the avatar refresh function to clear the urlCache when no new path is generated, ensuring that stale URLs are not retained. This change improves cache handling and aligns with the updated logic for avatar updates. * fix: enhance avatar refresh logic to handle legacy cache entries Updated the avatar refresh logic to accommodate legacy boolean cache entries, ensuring they are treated as cache misses and triggering a refresh. The cache now stores a structured `urlCache` map instead of a boolean, improving cache handling. Added tests to verify correct behavior for cache hits and misses, ensuring clients receive valid URLs based on the latest refresh.
This commit is contained in:
parent
7ce898d6a0
commit
b349f2f876
4 changed files with 193 additions and 31 deletions
|
|
@ -530,10 +530,10 @@ const getListAgentsHandler = async (req, res) => {
|
|||
*/
|
||||
const cache = getLogStores(CacheKeys.S3_EXPIRY_INTERVAL);
|
||||
const refreshKey = `${userId}:agents_avatar_refresh`;
|
||||
const alreadyChecked = await cache.get(refreshKey);
|
||||
if (alreadyChecked) {
|
||||
logger.debug('[/Agents] S3 avatar refresh already checked, skipping');
|
||||
} else {
|
||||
let cachedRefresh = await cache.get(refreshKey);
|
||||
const isValidCachedRefresh =
|
||||
cachedRefresh != null && typeof cachedRefresh === 'object' && cachedRefresh.urlCache != null;
|
||||
if (!isValidCachedRefresh) {
|
||||
try {
|
||||
const fullList = await getListAgentsByAccess({
|
||||
accessibleIds,
|
||||
|
|
@ -541,16 +541,19 @@ const getListAgentsHandler = async (req, res) => {
|
|||
limit: MAX_AVATAR_REFRESH_AGENTS,
|
||||
after: null,
|
||||
});
|
||||
await refreshListAvatars({
|
||||
const { urlCache } = await refreshListAvatars({
|
||||
agents: fullList?.data ?? [],
|
||||
userId,
|
||||
refreshS3Url,
|
||||
updateAgent,
|
||||
});
|
||||
await cache.set(refreshKey, true, Time.THIRTY_MINUTES);
|
||||
cachedRefresh = { urlCache };
|
||||
await cache.set(refreshKey, cachedRefresh, Time.THIRTY_MINUTES);
|
||||
} catch (err) {
|
||||
logger.error('[/Agents] Error refreshing avatars for full list: %o', err);
|
||||
}
|
||||
} else {
|
||||
logger.debug('[/Agents] S3 avatar refresh already checked, skipping');
|
||||
}
|
||||
|
||||
// Use the new ACL-aware function
|
||||
|
|
@ -568,11 +571,20 @@ const getListAgentsHandler = async (req, res) => {
|
|||
|
||||
const publicSet = new Set(publiclyAccessibleIds.map((oid) => oid.toString()));
|
||||
|
||||
const urlCache = cachedRefresh?.urlCache;
|
||||
data.data = agents.map((agent) => {
|
||||
try {
|
||||
if (agent?._id && publicSet.has(agent._id.toString())) {
|
||||
agent.isPublic = true;
|
||||
}
|
||||
if (
|
||||
urlCache &&
|
||||
agent?.id &&
|
||||
agent?.avatar?.source === FileSources.s3 &&
|
||||
urlCache[agent.id]
|
||||
) {
|
||||
agent.avatar = { ...agent.avatar, filepath: urlCache[agent.id] };
|
||||
}
|
||||
} catch (e) {
|
||||
// Silently ignore mapping errors
|
||||
void e;
|
||||
|
|
@ -658,6 +670,14 @@ const uploadAgentAvatarHandler = async (req, res) => {
|
|||
const updatedAgent = await updateAgent({ id: agent_id }, data, {
|
||||
updatingUserId: req.user.id,
|
||||
});
|
||||
|
||||
try {
|
||||
const avatarCache = getLogStores(CacheKeys.S3_EXPIRY_INTERVAL);
|
||||
await avatarCache.delete(`${req.user.id}:agents_avatar_refresh`);
|
||||
} catch (cacheErr) {
|
||||
logger.error('[/:agent_id/avatar] Error invalidating avatar refresh cache', cacheErr);
|
||||
}
|
||||
|
||||
res.status(201).json(updatedAgent);
|
||||
} catch (error) {
|
||||
const message = 'An error occurred while updating the Agent Avatar';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue