mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-22 19:30:15 +01:00
refactor: replace getCustomConfig with getAppConfig in Conversation and Message models, update tempChatRetention functions to use AppConfig type
This commit is contained in:
parent
c82c47ab6a
commit
57513f7ac9
4 changed files with 32 additions and 30 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
const { logger } = require('@librechat/data-schemas');
|
const { logger } = require('@librechat/data-schemas');
|
||||||
const { createTempChatExpirationDate } = require('@librechat/api');
|
const { createTempChatExpirationDate } = require('@librechat/api');
|
||||||
const { getCustomConfig } = require('~/server/services/Config/getCustomConfig');
|
const { getAppConfig } = require('~/server/services/Config/app');
|
||||||
const { getMessages, deleteMessages } = require('./Message');
|
const { getMessages, deleteMessages } = require('./Message');
|
||||||
const { Conversation } = require('~/db/models');
|
const { Conversation } = require('~/db/models');
|
||||||
|
|
||||||
|
|
@ -102,8 +102,8 @@ module.exports = {
|
||||||
|
|
||||||
if (req?.body?.isTemporary) {
|
if (req?.body?.isTemporary) {
|
||||||
try {
|
try {
|
||||||
const customConfig = await getCustomConfig();
|
const appConfig = await getAppConfig();
|
||||||
update.expiredAt = createTempChatExpirationDate(customConfig);
|
update.expiredAt = createTempChatExpirationDate(appConfig?.interfaceConfig);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('Error creating temporary chat expiration date:', err);
|
logger.error('Error creating temporary chat expiration date:', err);
|
||||||
logger.info(`---\`saveConvo\` context: ${metadata?.context}`);
|
logger.info(`---\`saveConvo\` context: ${metadata?.context}`);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const { z } = require('zod');
|
const { z } = require('zod');
|
||||||
const { logger } = require('@librechat/data-schemas');
|
const { logger } = require('@librechat/data-schemas');
|
||||||
const { createTempChatExpirationDate } = require('@librechat/api');
|
const { createTempChatExpirationDate } = require('@librechat/api');
|
||||||
const { getCustomConfig } = require('~/server/services/Config/getCustomConfig');
|
const { getAppConfig } = require('~/server/services/Config/app');
|
||||||
const { Message } = require('~/db/models');
|
const { Message } = require('~/db/models');
|
||||||
|
|
||||||
const idSchema = z.string().uuid();
|
const idSchema = z.string().uuid();
|
||||||
|
|
@ -57,8 +57,8 @@ async function saveMessage(req, params, metadata) {
|
||||||
|
|
||||||
if (req?.body?.isTemporary) {
|
if (req?.body?.isTemporary) {
|
||||||
try {
|
try {
|
||||||
const customConfig = await getCustomConfig();
|
const appConfig = await getAppConfig();
|
||||||
update.expiredAt = createTempChatExpirationDate(customConfig);
|
update.expiredAt = createTempChatExpirationDate(appConfig?.interfaceConfig);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('Error creating temporary chat expiration date:', err);
|
logger.error('Error creating temporary chat expiration date:', err);
|
||||||
logger.info(`---\`saveMessage\` context: ${metadata?.context}`);
|
logger.info(`---\`saveMessage\` context: ${metadata?.context}`);
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
|
import type { AppConfig } from '~/types';
|
||||||
import {
|
import {
|
||||||
|
createTempChatExpirationDate,
|
||||||
|
getTempChatRetentionHours,
|
||||||
|
DEFAULT_RETENTION_HOURS,
|
||||||
MIN_RETENTION_HOURS,
|
MIN_RETENTION_HOURS,
|
||||||
MAX_RETENTION_HOURS,
|
MAX_RETENTION_HOURS,
|
||||||
DEFAULT_RETENTION_HOURS,
|
|
||||||
getTempChatRetentionHours,
|
|
||||||
createTempChatExpirationDate,
|
|
||||||
} from './tempChatRetention';
|
} from './tempChatRetention';
|
||||||
import type { TCustomConfig } from 'librechat-data-provider';
|
|
||||||
|
|
||||||
describe('tempChatRetention', () => {
|
describe('tempChatRetention', () => {
|
||||||
const originalEnv = process.env;
|
const originalEnv = process.env;
|
||||||
|
|
@ -33,43 +33,43 @@ describe('tempChatRetention', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use config value when set', () => {
|
it('should use config value when set', () => {
|
||||||
const config: Partial<TCustomConfig> = {
|
const config: Partial<AppConfig> = {
|
||||||
interface: {
|
interface: {
|
||||||
temporaryChatRetention: 12,
|
temporaryChatRetention: 12,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const result = getTempChatRetentionHours(config);
|
const result = getTempChatRetentionHours(config?.interfaceConfig);
|
||||||
expect(result).toBe(12);
|
expect(result).toBe(12);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should prioritize config over environment variable', () => {
|
it('should prioritize config over environment variable', () => {
|
||||||
process.env.TEMP_CHAT_RETENTION_HOURS = '48';
|
process.env.TEMP_CHAT_RETENTION_HOURS = '48';
|
||||||
const config: Partial<TCustomConfig> = {
|
const config: Partial<AppConfig> = {
|
||||||
interface: {
|
interface: {
|
||||||
temporaryChatRetention: 12,
|
temporaryChatRetention: 12,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const result = getTempChatRetentionHours(config);
|
const result = getTempChatRetentionHours(config?.interfaceConfig);
|
||||||
expect(result).toBe(12);
|
expect(result).toBe(12);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should enforce minimum retention period', () => {
|
it('should enforce minimum retention period', () => {
|
||||||
const config: Partial<TCustomConfig> = {
|
const config: Partial<AppConfig> = {
|
||||||
interface: {
|
interface: {
|
||||||
temporaryChatRetention: 0,
|
temporaryChatRetention: 0,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const result = getTempChatRetentionHours(config);
|
const result = getTempChatRetentionHours(config?.interfaceConfig);
|
||||||
expect(result).toBe(MIN_RETENTION_HOURS);
|
expect(result).toBe(MIN_RETENTION_HOURS);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should enforce maximum retention period', () => {
|
it('should enforce maximum retention period', () => {
|
||||||
const config: Partial<TCustomConfig> = {
|
const config: Partial<AppConfig> = {
|
||||||
interface: {
|
interface: {
|
||||||
temporaryChatRetention: 10000,
|
temporaryChatRetention: 10000,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const result = getTempChatRetentionHours(config);
|
const result = getTempChatRetentionHours(config?.interfaceConfig);
|
||||||
expect(result).toBe(MAX_RETENTION_HOURS);
|
expect(result).toBe(MAX_RETENTION_HOURS);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -80,12 +80,12 @@ describe('tempChatRetention', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle invalid config value', () => {
|
it('should handle invalid config value', () => {
|
||||||
const config: Partial<TCustomConfig> = {
|
const config: Partial<AppConfig> = {
|
||||||
interface: {
|
interface: {
|
||||||
temporaryChatRetention: 'invalid' as unknown as number,
|
temporaryChatRetention: 'invalid' as unknown as number,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const result = getTempChatRetentionHours(config);
|
const result = getTempChatRetentionHours(config?.interfaceConfig);
|
||||||
expect(result).toBe(DEFAULT_RETENTION_HOURS);
|
expect(result).toBe(DEFAULT_RETENTION_HOURS);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -103,13 +103,13 @@ describe('tempChatRetention', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create expiration date with custom retention period', () => {
|
it('should create expiration date with custom retention period', () => {
|
||||||
const config: Partial<TCustomConfig> = {
|
const config: Partial<AppConfig> = {
|
||||||
interface: {
|
interface: {
|
||||||
temporaryChatRetention: 12,
|
temporaryChatRetention: 12,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = createTempChatExpirationDate(config);
|
const result = createTempChatExpirationDate(config?.interfaceConfig);
|
||||||
|
|
||||||
const expectedDate = new Date();
|
const expectedDate = new Date();
|
||||||
expectedDate.setHours(expectedDate.getHours() + 12);
|
expectedDate.setHours(expectedDate.getHours() + 12);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { logger } from '@librechat/data-schemas';
|
import { logger } from '@librechat/data-schemas';
|
||||||
import type { TCustomConfig } from 'librechat-data-provider';
|
import type { AppConfig } from '~/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default retention period for temporary chats in hours
|
* Default retention period for temporary chats in hours
|
||||||
|
|
@ -18,10 +18,12 @@ export const MAX_RETENTION_HOURS = 8760;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the temporary chat retention period from environment variables or config
|
* Gets the temporary chat retention period from environment variables or config
|
||||||
* @param config - The custom configuration object
|
* @param interfaceConfig - The custom configuration object
|
||||||
* @returns The retention period in hours
|
* @returns The retention period in hours
|
||||||
*/
|
*/
|
||||||
export function getTempChatRetentionHours(config?: Partial<TCustomConfig> | null): number {
|
export function getTempChatRetentionHours(
|
||||||
|
interfaceConfig?: AppConfig['interfaceConfig'] | null,
|
||||||
|
): number {
|
||||||
let retentionHours = DEFAULT_RETENTION_HOURS;
|
let retentionHours = DEFAULT_RETENTION_HOURS;
|
||||||
|
|
||||||
// Check environment variable first
|
// Check environment variable first
|
||||||
|
|
@ -37,8 +39,8 @@ export function getTempChatRetentionHours(config?: Partial<TCustomConfig> | null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check config file (takes precedence over environment variable)
|
// Check config file (takes precedence over environment variable)
|
||||||
if (config?.interface?.temporaryChatRetention !== undefined) {
|
if (interfaceConfig?.temporaryChatRetention !== undefined) {
|
||||||
const configValue = config.interface.temporaryChatRetention;
|
const configValue = interfaceConfig.temporaryChatRetention;
|
||||||
if (typeof configValue === 'number' && !isNaN(configValue)) {
|
if (typeof configValue === 'number' && !isNaN(configValue)) {
|
||||||
retentionHours = configValue;
|
retentionHours = configValue;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -66,11 +68,11 @@ export function getTempChatRetentionHours(config?: Partial<TCustomConfig> | null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an expiration date for temporary chats
|
* Creates an expiration date for temporary chats
|
||||||
* @param config - The custom configuration object
|
* @param interfaceConfig - The custom configuration object
|
||||||
* @returns The expiration date
|
* @returns The expiration date
|
||||||
*/
|
*/
|
||||||
export function createTempChatExpirationDate(config?: Partial<TCustomConfig>): Date {
|
export function createTempChatExpirationDate(interfaceConfig?: AppConfig['interfaceConfig']): Date {
|
||||||
const retentionHours = getTempChatRetentionHours(config);
|
const retentionHours = getTempChatRetentionHours(interfaceConfig);
|
||||||
const expiredAt = new Date();
|
const expiredAt = new Date();
|
||||||
expiredAt.setHours(expiredAt.getHours() + retentionHours);
|
expiredAt.setHours(expiredAt.getHours() + retentionHours);
|
||||||
return expiredAt;
|
return expiredAt;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue