2025-06-25 17:16:26 -04:00
|
|
|
import { logger } from '@librechat/data-schemas';
|
2025-08-26 12:10:18 -04:00
|
|
|
import type { AppConfig } from '~/types';
|
2025-06-25 17:16:26 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default retention period for temporary chats in hours
|
|
|
|
|
*/
|
|
|
|
|
export const DEFAULT_RETENTION_HOURS = 24 * 30; // 30 days
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Minimum allowed retention period in hours
|
|
|
|
|
*/
|
|
|
|
|
export const MIN_RETENTION_HOURS = 1;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maximum allowed retention period in hours (1 year = 8760 hours)
|
|
|
|
|
*/
|
|
|
|
|
export const MAX_RETENTION_HOURS = 8760;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the temporary chat retention period from environment variables or config
|
2025-08-26 12:10:18 -04:00
|
|
|
* @param interfaceConfig - The custom configuration object
|
2025-06-25 17:16:26 -04:00
|
|
|
* @returns The retention period in hours
|
|
|
|
|
*/
|
2025-08-26 12:10:18 -04:00
|
|
|
export function getTempChatRetentionHours(
|
|
|
|
|
interfaceConfig?: AppConfig['interfaceConfig'] | null,
|
|
|
|
|
): number {
|
2025-06-25 17:16:26 -04:00
|
|
|
let retentionHours = DEFAULT_RETENTION_HOURS;
|
|
|
|
|
|
|
|
|
|
// Check environment variable first
|
|
|
|
|
if (process.env.TEMP_CHAT_RETENTION_HOURS) {
|
|
|
|
|
const envValue = parseInt(process.env.TEMP_CHAT_RETENTION_HOURS, 10);
|
|
|
|
|
if (!isNaN(envValue)) {
|
|
|
|
|
retentionHours = envValue;
|
|
|
|
|
} else {
|
|
|
|
|
logger.warn(
|
|
|
|
|
`Invalid TEMP_CHAT_RETENTION_HOURS environment variable: ${process.env.TEMP_CHAT_RETENTION_HOURS}. Using default: ${DEFAULT_RETENTION_HOURS} hours.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check config file (takes precedence over environment variable)
|
2025-08-26 12:10:18 -04:00
|
|
|
if (interfaceConfig?.temporaryChatRetention !== undefined) {
|
|
|
|
|
const configValue = interfaceConfig.temporaryChatRetention;
|
2025-06-25 17:16:26 -04:00
|
|
|
if (typeof configValue === 'number' && !isNaN(configValue)) {
|
|
|
|
|
retentionHours = configValue;
|
|
|
|
|
} else {
|
|
|
|
|
logger.warn(
|
|
|
|
|
`Invalid temporaryChatRetention in config: ${configValue}. Using ${retentionHours} hours.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate the retention period
|
|
|
|
|
if (retentionHours < MIN_RETENTION_HOURS) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
`Temporary chat retention period ${retentionHours} is below minimum ${MIN_RETENTION_HOURS} hours. Using minimum value.`,
|
|
|
|
|
);
|
|
|
|
|
retentionHours = MIN_RETENTION_HOURS;
|
|
|
|
|
} else if (retentionHours > MAX_RETENTION_HOURS) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
`Temporary chat retention period ${retentionHours} exceeds maximum ${MAX_RETENTION_HOURS} hours. Using maximum value.`,
|
|
|
|
|
);
|
|
|
|
|
retentionHours = MAX_RETENTION_HOURS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retentionHours;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an expiration date for temporary chats
|
2025-08-26 12:10:18 -04:00
|
|
|
* @param interfaceConfig - The custom configuration object
|
2025-06-25 17:16:26 -04:00
|
|
|
* @returns The expiration date
|
|
|
|
|
*/
|
2025-08-26 12:10:18 -04:00
|
|
|
export function createTempChatExpirationDate(interfaceConfig?: AppConfig['interfaceConfig']): Date {
|
|
|
|
|
const retentionHours = getTempChatRetentionHours(interfaceConfig);
|
2025-10-04 01:53:37 -04:00
|
|
|
return new Date(Date.now() + retentionHours * 60 * 60 * 1000);
|
2025-06-25 17:16:26 -04:00
|
|
|
}
|