mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-18 08:28:10 +01:00
Merge branch 'main' into refactor/package-auth
This commit is contained in:
commit
02b9c9d447
340 changed files with 18559 additions and 14872 deletions
|
|
@ -58,7 +58,7 @@ function redactMessage(str: string, trimLength?: number): string {
|
|||
* @returns The modified log information object.
|
||||
*/
|
||||
const redactFormat = winston.format((info: winston.Logform.TransformableInfo) => {
|
||||
if (info.level === 'error') {
|
||||
if (info && info.level === 'error') {
|
||||
// Type guard to ensure message is a string
|
||||
if (typeof info.message === 'string') {
|
||||
info.message = redactMessage(info.message);
|
||||
|
|
|
|||
|
|
@ -3,5 +3,6 @@ export * from './schema';
|
|||
export { createModels } from './models';
|
||||
export { createMethods } from './methods';
|
||||
export type * from './types';
|
||||
export type * from './methods';
|
||||
export { default as logger } from './config/winston';
|
||||
export { default as meiliLogger } from './config/meiliLogger';
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ import { createUserMethods, type UserMethods } from './user';
|
|||
import { createSessionMethods, type SessionMethods } from './session';
|
||||
import { createTokenMethods, type TokenMethods } from './token';
|
||||
import { createRoleMethods, type RoleMethods } from './role';
|
||||
/* Memories */
|
||||
import { createMemoryMethods, type MemoryMethods } from './memory';
|
||||
|
||||
/**
|
||||
* Creates all database methods for all collections
|
||||
|
|
@ -12,7 +14,9 @@ export function createMethods(mongoose: typeof import('mongoose')) {
|
|||
...createSessionMethods(mongoose),
|
||||
...createTokenMethods(mongoose),
|
||||
...createRoleMethods(mongoose),
|
||||
...createMemoryMethods(mongoose),
|
||||
};
|
||||
}
|
||||
|
||||
export type AllMethods = UserMethods & SessionMethods & TokenMethods & RoleMethods;
|
||||
export type { MemoryMethods };
|
||||
export type AllMethods = UserMethods & SessionMethods & TokenMethods & RoleMethods & MemoryMethods;
|
||||
|
|
|
|||
168
packages/data-schemas/src/methods/memory.ts
Normal file
168
packages/data-schemas/src/methods/memory.ts
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
import { Types } from 'mongoose';
|
||||
import logger from '~/config/winston';
|
||||
import type * as t from '~/types';
|
||||
|
||||
/**
|
||||
* Formats a date in YYYY-MM-DD format
|
||||
*/
|
||||
const formatDate = (date: Date): string => {
|
||||
return date.toISOString().split('T')[0];
|
||||
};
|
||||
|
||||
// Factory function that takes mongoose instance and returns the methods
|
||||
export function createMemoryMethods(mongoose: typeof import('mongoose')) {
|
||||
const MemoryEntry = mongoose.models.MemoryEntry;
|
||||
|
||||
/**
|
||||
* Creates a new memory entry for a user
|
||||
* Throws an error if a memory with the same key already exists
|
||||
*/
|
||||
async function createMemory({
|
||||
userId,
|
||||
key,
|
||||
value,
|
||||
tokenCount = 0,
|
||||
}: t.SetMemoryParams): Promise<t.MemoryResult> {
|
||||
try {
|
||||
if (key?.toLowerCase() === 'nothing') {
|
||||
return { ok: false };
|
||||
}
|
||||
|
||||
const existingMemory = await MemoryEntry.findOne({ userId, key });
|
||||
if (existingMemory) {
|
||||
throw new Error('Memory with this key already exists');
|
||||
}
|
||||
|
||||
await MemoryEntry.create({
|
||||
userId,
|
||||
key,
|
||||
value,
|
||||
tokenCount,
|
||||
updated_at: new Date(),
|
||||
});
|
||||
|
||||
return { ok: true };
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to create memory: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets or updates a memory entry for a user
|
||||
*/
|
||||
async function setMemory({
|
||||
userId,
|
||||
key,
|
||||
value,
|
||||
tokenCount = 0,
|
||||
}: t.SetMemoryParams): Promise<t.MemoryResult> {
|
||||
try {
|
||||
if (key?.toLowerCase() === 'nothing') {
|
||||
return { ok: false };
|
||||
}
|
||||
|
||||
await MemoryEntry.findOneAndUpdate(
|
||||
{ userId, key },
|
||||
{
|
||||
value,
|
||||
tokenCount,
|
||||
updated_at: new Date(),
|
||||
},
|
||||
{
|
||||
upsert: true,
|
||||
new: true,
|
||||
},
|
||||
);
|
||||
|
||||
return { ok: true };
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to set memory: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a specific memory entry for a user
|
||||
*/
|
||||
async function deleteMemory({ userId, key }: t.DeleteMemoryParams): Promise<t.MemoryResult> {
|
||||
try {
|
||||
const result = await MemoryEntry.findOneAndDelete({ userId, key });
|
||||
return { ok: !!result };
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to delete memory: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all memory entries for a user
|
||||
*/
|
||||
async function getAllUserMemories(
|
||||
userId: string | Types.ObjectId,
|
||||
): Promise<t.IMemoryEntryLean[]> {
|
||||
try {
|
||||
return (await MemoryEntry.find({ userId }).lean()) as t.IMemoryEntryLean[];
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to get all memories: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets and formats all memories for a user in two different formats
|
||||
*/
|
||||
async function getFormattedMemories({
|
||||
userId,
|
||||
}: t.GetFormattedMemoriesParams): Promise<t.FormattedMemoriesResult> {
|
||||
try {
|
||||
const memories = await getAllUserMemories(userId);
|
||||
|
||||
if (!memories || memories.length === 0) {
|
||||
return { withKeys: '', withoutKeys: '', totalTokens: 0 };
|
||||
}
|
||||
|
||||
const sortedMemories = memories.sort(
|
||||
(a, b) => new Date(a.updated_at!).getTime() - new Date(b.updated_at!).getTime(),
|
||||
);
|
||||
|
||||
const totalTokens = sortedMemories.reduce((sum, memory) => {
|
||||
return sum + (memory.tokenCount || 0);
|
||||
}, 0);
|
||||
|
||||
const withKeys = sortedMemories
|
||||
.map((memory, index) => {
|
||||
const date = formatDate(new Date(memory.updated_at!));
|
||||
const tokenInfo = memory.tokenCount ? ` [${memory.tokenCount} tokens]` : '';
|
||||
return `${index + 1}. [${date}]. ["key": "${memory.key}"]${tokenInfo}. ["value": "${memory.value}"]`;
|
||||
})
|
||||
.join('\n\n');
|
||||
|
||||
const withoutKeys = sortedMemories
|
||||
.map((memory, index) => {
|
||||
const date = formatDate(new Date(memory.updated_at!));
|
||||
return `${index + 1}. [${date}]. ${memory.value}`;
|
||||
})
|
||||
.join('\n\n');
|
||||
|
||||
return { withKeys, withoutKeys, totalTokens };
|
||||
} catch (error) {
|
||||
logger.error('Failed to get formatted memories:', error);
|
||||
return { withKeys: '', withoutKeys: '', totalTokens: 0 };
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
setMemory,
|
||||
createMemory,
|
||||
deleteMemory,
|
||||
getAllUserMemories,
|
||||
getFormattedMemories,
|
||||
};
|
||||
}
|
||||
|
||||
export type MemoryMethods = ReturnType<typeof createMemoryMethods>;
|
||||
|
|
@ -13,7 +13,9 @@ export class SessionError extends Error {
|
|||
}
|
||||
|
||||
const { REFRESH_TOKEN_EXPIRY } = process.env ?? {};
|
||||
const expires = eval(REFRESH_TOKEN_EXPIRY ?? '0') ?? 1000 * 60 * 60 * 24 * 7; // 7 days default
|
||||
const expires = REFRESH_TOKEN_EXPIRY
|
||||
? eval(REFRESH_TOKEN_EXPIRY)
|
||||
: 1000 * 60 * 60 * 24 * 7; // 7 days default
|
||||
|
||||
// Factory function that takes mongoose instance and returns the methods
|
||||
export function createSessionMethods(mongoose: typeof import('mongoose')) {
|
||||
|
|
|
|||
163
packages/data-schemas/src/methods/user.test.ts
Normal file
163
packages/data-schemas/src/methods/user.test.ts
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
import mongoose from 'mongoose';
|
||||
import { createUserMethods } from './user';
|
||||
import { signPayload } from '~/crypto';
|
||||
import type { IUser } from '~/types';
|
||||
|
||||
jest.mock('~/crypto', () => ({
|
||||
signPayload: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('User Methods', () => {
|
||||
const mockSignPayload = signPayload as jest.MockedFunction<typeof signPayload>;
|
||||
let userMethods: ReturnType<typeof createUserMethods>;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
userMethods = createUserMethods(mongoose);
|
||||
});
|
||||
|
||||
describe('generateToken', () => {
|
||||
const mockUser = {
|
||||
_id: 'user123',
|
||||
username: 'testuser',
|
||||
provider: 'local',
|
||||
email: 'test@example.com',
|
||||
name: 'Test User',
|
||||
avatar: '',
|
||||
role: 'user',
|
||||
emailVerified: false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
} as IUser;
|
||||
|
||||
afterEach(() => {
|
||||
delete process.env.SESSION_EXPIRY;
|
||||
delete process.env.JWT_SECRET;
|
||||
});
|
||||
|
||||
it('should default to 15 minutes when SESSION_EXPIRY is not set', async () => {
|
||||
process.env.JWT_SECRET = 'test-secret';
|
||||
mockSignPayload.mockResolvedValue('mocked-token');
|
||||
|
||||
await userMethods.generateToken(mockUser);
|
||||
|
||||
expect(mockSignPayload).toHaveBeenCalledWith({
|
||||
payload: {
|
||||
id: mockUser._id,
|
||||
username: mockUser.username,
|
||||
provider: mockUser.provider,
|
||||
email: mockUser.email,
|
||||
},
|
||||
secret: 'test-secret',
|
||||
expirationTime: 900, // 15 minutes in seconds
|
||||
});
|
||||
});
|
||||
|
||||
it('should default to 15 minutes when SESSION_EXPIRY is empty string', async () => {
|
||||
process.env.SESSION_EXPIRY = '';
|
||||
process.env.JWT_SECRET = 'test-secret';
|
||||
mockSignPayload.mockResolvedValue('mocked-token');
|
||||
|
||||
await userMethods.generateToken(mockUser);
|
||||
|
||||
expect(mockSignPayload).toHaveBeenCalledWith({
|
||||
payload: {
|
||||
id: mockUser._id,
|
||||
username: mockUser.username,
|
||||
provider: mockUser.provider,
|
||||
email: mockUser.email,
|
||||
},
|
||||
secret: 'test-secret',
|
||||
expirationTime: 900, // 15 minutes in seconds
|
||||
});
|
||||
});
|
||||
|
||||
it('should use custom expiry when SESSION_EXPIRY is set to a valid expression', async () => {
|
||||
process.env.SESSION_EXPIRY = '1000 * 60 * 30'; // 30 minutes
|
||||
process.env.JWT_SECRET = 'test-secret';
|
||||
mockSignPayload.mockResolvedValue('mocked-token');
|
||||
|
||||
await userMethods.generateToken(mockUser);
|
||||
|
||||
expect(mockSignPayload).toHaveBeenCalledWith({
|
||||
payload: {
|
||||
id: mockUser._id,
|
||||
username: mockUser.username,
|
||||
provider: mockUser.provider,
|
||||
email: mockUser.email,
|
||||
},
|
||||
secret: 'test-secret',
|
||||
expirationTime: 1800, // 30 minutes in seconds
|
||||
});
|
||||
});
|
||||
|
||||
it('should default to 15 minutes when SESSION_EXPIRY evaluates to falsy value', async () => {
|
||||
process.env.SESSION_EXPIRY = '0'; // This will evaluate to 0, which is falsy
|
||||
process.env.JWT_SECRET = 'test-secret';
|
||||
mockSignPayload.mockResolvedValue('mocked-token');
|
||||
|
||||
await userMethods.generateToken(mockUser);
|
||||
|
||||
expect(mockSignPayload).toHaveBeenCalledWith({
|
||||
payload: {
|
||||
id: mockUser._id,
|
||||
username: mockUser.username,
|
||||
provider: mockUser.provider,
|
||||
email: mockUser.email,
|
||||
},
|
||||
secret: 'test-secret',
|
||||
expirationTime: 900, // 15 minutes in seconds
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw error when no user is provided', async () => {
|
||||
process.env.JWT_SECRET = 'test-secret';
|
||||
|
||||
await expect(userMethods.generateToken(null as unknown as IUser)).rejects.toThrow(
|
||||
'No user provided',
|
||||
);
|
||||
});
|
||||
|
||||
it('should return the token from signPayload', async () => {
|
||||
process.env.SESSION_EXPIRY = '1000 * 60 * 60'; // 1 hour
|
||||
process.env.JWT_SECRET = 'test-secret';
|
||||
const expectedToken = 'generated-jwt-token';
|
||||
mockSignPayload.mockResolvedValue(expectedToken);
|
||||
|
||||
const token = await userMethods.generateToken(mockUser);
|
||||
|
||||
expect(token).toBe(expectedToken);
|
||||
});
|
||||
|
||||
it('should handle invalid SESSION_EXPIRY expressions gracefully', async () => {
|
||||
process.env.SESSION_EXPIRY = 'invalid expression';
|
||||
process.env.JWT_SECRET = 'test-secret';
|
||||
mockSignPayload.mockResolvedValue('mocked-token');
|
||||
|
||||
// Mock console.warn to verify it's called
|
||||
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
|
||||
|
||||
await userMethods.generateToken(mockUser);
|
||||
|
||||
// Should use default value when eval fails
|
||||
expect(mockSignPayload).toHaveBeenCalledWith({
|
||||
payload: {
|
||||
id: mockUser._id,
|
||||
username: mockUser.username,
|
||||
provider: mockUser.provider,
|
||||
email: mockUser.email,
|
||||
},
|
||||
secret: 'test-secret',
|
||||
expirationTime: 900, // 15 minutes in seconds (default)
|
||||
});
|
||||
|
||||
// Verify warning was logged
|
||||
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
||||
'Invalid SESSION_EXPIRY expression, using default:',
|
||||
expect.any(SyntaxError),
|
||||
);
|
||||
|
||||
consoleWarnSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -145,7 +145,18 @@ export function createUserMethods(mongoose: typeof import('mongoose')) {
|
|||
throw new Error('No user provided');
|
||||
}
|
||||
|
||||
const expires = eval(process.env.SESSION_EXPIRY ?? '0') ?? 1000 * 60 * 15;
|
||||
let expires = 1000 * 60 * 15;
|
||||
|
||||
if (process.env.SESSION_EXPIRY !== undefined && process.env.SESSION_EXPIRY !== '') {
|
||||
try {
|
||||
const evaluated = eval(process.env.SESSION_EXPIRY);
|
||||
if (evaluated) {
|
||||
expires = evaluated;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Invalid SESSION_EXPIRY expression, using default:', error);
|
||||
}
|
||||
}
|
||||
|
||||
return await signPayload({
|
||||
payload: {
|
||||
|
|
@ -159,6 +170,35 @@ export function createUserMethods(mongoose: typeof import('mongoose')) {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user's personalization memories setting.
|
||||
* Handles the edge case where the personalization object doesn't exist.
|
||||
*/
|
||||
async function toggleUserMemories(
|
||||
userId: string,
|
||||
memoriesEnabled: boolean,
|
||||
): Promise<IUser | null> {
|
||||
const User = mongoose.models.User;
|
||||
|
||||
// First, ensure the personalization object exists
|
||||
const user = await User.findById(userId);
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Use $set to update the nested field, which will create the personalization object if it doesn't exist
|
||||
const updateOperation = {
|
||||
$set: {
|
||||
'personalization.memories': memoriesEnabled,
|
||||
},
|
||||
};
|
||||
|
||||
return (await User.findByIdAndUpdate(userId, updateOperation, {
|
||||
new: true,
|
||||
runValidators: true,
|
||||
}).lean()) as IUser | null;
|
||||
}
|
||||
|
||||
// Return all methods
|
||||
return {
|
||||
findUser,
|
||||
|
|
@ -168,6 +208,7 @@ export function createUserMethods(mongoose: typeof import('mongoose')) {
|
|||
getUserById,
|
||||
deleteUserById,
|
||||
generateToken,
|
||||
toggleUserMemories,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,21 @@
|
|||
import type * as t from '~/types';
|
||||
import mongoMeili from '~/models/plugins/mongoMeili';
|
||||
import convoSchema from '~/schema/convo';
|
||||
|
||||
/**
|
||||
* Creates or returns the Conversation model using the provided mongoose instance and schema
|
||||
*/
|
||||
export function createConversationModel(mongoose: typeof import('mongoose')) {
|
||||
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
|
||||
convoSchema.plugin(mongoMeili, {
|
||||
mongoose,
|
||||
host: process.env.MEILI_HOST,
|
||||
apiKey: process.env.MEILI_MASTER_KEY,
|
||||
/** Note: Will get created automatically if it doesn't exist already */
|
||||
indexName: 'convos',
|
||||
primaryKey: 'conversationId',
|
||||
});
|
||||
}
|
||||
return (
|
||||
mongoose.models.Conversation || mongoose.model<t.IConversation>('Conversation', convoSchema)
|
||||
);
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import { createPromptGroupModel } from './promptGroup';
|
|||
import { createConversationTagModel } from './conversationTag';
|
||||
import { createSharedLinkModel } from './sharedLink';
|
||||
import { createToolCallModel } from './toolCall';
|
||||
import { createMemoryModel } from './memory';
|
||||
|
||||
/**
|
||||
* Creates all database models for all collections
|
||||
|
|
@ -48,5 +49,6 @@ export function createModels(mongoose: typeof import('mongoose')) {
|
|||
ConversationTag: createConversationTagModel(mongoose),
|
||||
SharedLink: createSharedLinkModel(mongoose),
|
||||
ToolCall: createToolCallModel(mongoose),
|
||||
MemoryEntry: createMemoryModel(mongoose),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
6
packages/data-schemas/src/models/memory.ts
Normal file
6
packages/data-schemas/src/models/memory.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import memorySchema from '~/schema/memory';
|
||||
import type { IMemoryEntry } from '~/types/memory';
|
||||
|
||||
export function createMemoryModel(mongoose: typeof import('mongoose')) {
|
||||
return mongoose.models.MemoryEntry || mongoose.model<IMemoryEntry>('MemoryEntry', memorySchema);
|
||||
}
|
||||
|
|
@ -1,9 +1,20 @@
|
|||
import messageSchema from '~/schema/message';
|
||||
import type * as t from '~/types';
|
||||
import mongoMeili from '~/models/plugins/mongoMeili';
|
||||
import messageSchema from '~/schema/message';
|
||||
|
||||
/**
|
||||
* Creates or returns the Message model using the provided mongoose instance and schema
|
||||
*/
|
||||
export function createMessageModel(mongoose: typeof import('mongoose')) {
|
||||
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
|
||||
messageSchema.plugin(mongoMeili, {
|
||||
mongoose,
|
||||
host: process.env.MEILI_HOST,
|
||||
apiKey: process.env.MEILI_MASTER_KEY,
|
||||
indexName: 'messages',
|
||||
primaryKey: 'messageId',
|
||||
});
|
||||
}
|
||||
|
||||
return mongoose.models.Message || mongoose.model<t.IMessage>('Message', messageSchema);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,14 @@
|
|||
import _ from 'lodash';
|
||||
import { MeiliSearch, Index } from 'meilisearch';
|
||||
import mongoose, { Schema, Document, Model, Query } from 'mongoose';
|
||||
import type {
|
||||
CallbackWithoutResultAndOptionalError,
|
||||
FilterQuery,
|
||||
Document,
|
||||
Schema,
|
||||
Query,
|
||||
Types,
|
||||
Model,
|
||||
} from 'mongoose';
|
||||
import logger from '~/config/meiliLogger';
|
||||
|
||||
interface MongoMeiliOptions {
|
||||
|
|
@ -8,6 +16,7 @@ interface MongoMeiliOptions {
|
|||
apiKey: string;
|
||||
indexName: string;
|
||||
primaryKey: string;
|
||||
mongoose: typeof import('mongoose');
|
||||
}
|
||||
|
||||
interface MeiliIndexable {
|
||||
|
|
@ -23,12 +32,12 @@ interface ContentItem {
|
|||
interface DocumentWithMeiliIndex extends Document {
|
||||
_meiliIndex?: boolean;
|
||||
preprocessObjectForIndex?: () => Record<string, unknown>;
|
||||
addObjectToMeili?: () => Promise<void>;
|
||||
updateObjectToMeili?: () => Promise<void>;
|
||||
deleteObjectFromMeili?: () => Promise<void>;
|
||||
postSaveHook?: () => void;
|
||||
postUpdateHook?: () => void;
|
||||
postRemoveHook?: () => void;
|
||||
addObjectToMeili?: (next: CallbackWithoutResultAndOptionalError) => Promise<void>;
|
||||
updateObjectToMeili?: (next: CallbackWithoutResultAndOptionalError) => Promise<void>;
|
||||
deleteObjectFromMeili?: (next: CallbackWithoutResultAndOptionalError) => Promise<void>;
|
||||
postSaveHook?: (next: CallbackWithoutResultAndOptionalError) => void;
|
||||
postUpdateHook?: (next: CallbackWithoutResultAndOptionalError) => void;
|
||||
postRemoveHook?: (next: CallbackWithoutResultAndOptionalError) => void;
|
||||
conversationId?: string;
|
||||
content?: ContentItem[];
|
||||
messageId?: string;
|
||||
|
|
@ -219,7 +228,7 @@ const createMeiliMongooseModel = ({
|
|||
);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('[syncWithMeili] Error adding document to Meili', error);
|
||||
logger.error('[syncWithMeili] Error adding document to Meili:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -305,28 +314,48 @@ const createMeiliMongooseModel = ({
|
|||
/**
|
||||
* Adds the current document to the MeiliSearch index
|
||||
*/
|
||||
async addObjectToMeili(this: DocumentWithMeiliIndex): Promise<void> {
|
||||
async addObjectToMeili(
|
||||
this: DocumentWithMeiliIndex,
|
||||
next: CallbackWithoutResultAndOptionalError,
|
||||
): Promise<void> {
|
||||
const object = this.preprocessObjectForIndex!();
|
||||
try {
|
||||
await index.addDocuments([object]);
|
||||
} catch (error) {
|
||||
logger.error('[addObjectToMeili] Error adding document to Meili', error);
|
||||
logger.error('[addObjectToMeili] Error adding document to Meili:', error);
|
||||
return next();
|
||||
}
|
||||
|
||||
await this.collection.updateMany(
|
||||
{ _id: this._id as mongoose.Types.ObjectId },
|
||||
{ $set: { _meiliIndex: true } },
|
||||
);
|
||||
try {
|
||||
await this.collection.updateMany(
|
||||
{ _id: this._id as Types.ObjectId },
|
||||
{ $set: { _meiliIndex: true } },
|
||||
);
|
||||
} catch (error) {
|
||||
logger.error('[addObjectToMeili] Error updating _meiliIndex field:', error);
|
||||
return next();
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the current document in the MeiliSearch index
|
||||
*/
|
||||
async updateObjectToMeili(this: DocumentWithMeiliIndex): Promise<void> {
|
||||
const object = _.omitBy(_.pick(this.toJSON(), attributesToIndex), (v, k) =>
|
||||
k.startsWith('$'),
|
||||
);
|
||||
await index.updateDocuments([object]);
|
||||
async updateObjectToMeili(
|
||||
this: DocumentWithMeiliIndex,
|
||||
next: CallbackWithoutResultAndOptionalError,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const object = _.omitBy(_.pick(this.toJSON(), attributesToIndex), (v, k) =>
|
||||
k.startsWith('$'),
|
||||
);
|
||||
await index.updateDocuments([object]);
|
||||
next();
|
||||
} catch (error) {
|
||||
logger.error('[updateObjectToMeili] Error updating document in Meili:', error);
|
||||
return next();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -334,8 +363,17 @@ const createMeiliMongooseModel = ({
|
|||
*
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async deleteObjectFromMeili(this: DocumentWithMeiliIndex): Promise<void> {
|
||||
await index.deleteDocument(this._id as string);
|
||||
async deleteObjectFromMeili(
|
||||
this: DocumentWithMeiliIndex,
|
||||
next: CallbackWithoutResultAndOptionalError,
|
||||
): Promise<void> {
|
||||
try {
|
||||
await index.deleteDocument(this._id as string);
|
||||
next();
|
||||
} catch (error) {
|
||||
logger.error('[deleteObjectFromMeili] Error deleting document from Meili:', error);
|
||||
return next();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -344,11 +382,11 @@ const createMeiliMongooseModel = ({
|
|||
* If the document is already indexed (i.e. `_meiliIndex` is true), it updates it;
|
||||
* otherwise, it adds the document to the index.
|
||||
*/
|
||||
postSaveHook(this: DocumentWithMeiliIndex): void {
|
||||
postSaveHook(this: DocumentWithMeiliIndex, next: CallbackWithoutResultAndOptionalError): void {
|
||||
if (this._meiliIndex) {
|
||||
this.updateObjectToMeili!();
|
||||
this.updateObjectToMeili!(next);
|
||||
} else {
|
||||
this.addObjectToMeili!();
|
||||
this.addObjectToMeili!(next);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -358,9 +396,14 @@ const createMeiliMongooseModel = ({
|
|||
* This hook is triggered after a document update, ensuring that changes are
|
||||
* propagated to the MeiliSearch index if the document is indexed.
|
||||
*/
|
||||
postUpdateHook(this: DocumentWithMeiliIndex): void {
|
||||
postUpdateHook(
|
||||
this: DocumentWithMeiliIndex,
|
||||
next: CallbackWithoutResultAndOptionalError,
|
||||
): void {
|
||||
if (this._meiliIndex) {
|
||||
this.updateObjectToMeili!();
|
||||
this.updateObjectToMeili!(next);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -370,9 +413,14 @@ const createMeiliMongooseModel = ({
|
|||
* This hook is triggered after a document is removed, ensuring that the document
|
||||
* is also removed from the MeiliSearch index if it was previously indexed.
|
||||
*/
|
||||
postRemoveHook(this: DocumentWithMeiliIndex): void {
|
||||
postRemoveHook(
|
||||
this: DocumentWithMeiliIndex,
|
||||
next: CallbackWithoutResultAndOptionalError,
|
||||
): void {
|
||||
if (this._meiliIndex) {
|
||||
this.deleteObjectFromMeili!();
|
||||
this.deleteObjectFromMeili!(next);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -398,6 +446,7 @@ const createMeiliMongooseModel = ({
|
|||
* @param options.primaryKey - The primary key field for indexing.
|
||||
*/
|
||||
export default function mongoMeili(schema: Schema, options: MongoMeiliOptions): void {
|
||||
const mongoose = options.mongoose;
|
||||
validateOptions(options);
|
||||
|
||||
// Add _meiliIndex field to the schema to track if a document has been indexed in MeiliSearch.
|
||||
|
|
@ -427,16 +476,16 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
|
|||
schema.loadClass(createMeiliMongooseModel({ index, attributesToIndex }));
|
||||
|
||||
// Register Mongoose hooks
|
||||
schema.post('save', function (doc: DocumentWithMeiliIndex) {
|
||||
doc.postSaveHook?.();
|
||||
schema.post('save', function (doc: DocumentWithMeiliIndex, next) {
|
||||
doc.postSaveHook?.(next);
|
||||
});
|
||||
|
||||
schema.post('updateOne', function (doc: DocumentWithMeiliIndex) {
|
||||
doc.postUpdateHook?.();
|
||||
schema.post('updateOne', function (doc: DocumentWithMeiliIndex, next) {
|
||||
doc.postUpdateHook?.(next);
|
||||
});
|
||||
|
||||
schema.post('deleteOne', function (doc: DocumentWithMeiliIndex) {
|
||||
doc.postRemoveHook?.();
|
||||
schema.post('deleteOne', function (doc: DocumentWithMeiliIndex, next) {
|
||||
doc.postRemoveHook?.(next);
|
||||
});
|
||||
|
||||
// Pre-deleteMany hook: remove corresponding documents from MeiliSearch when multiple documents are deleted.
|
||||
|
|
@ -452,7 +501,7 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
|
|||
const convoIndex = client.index('convos');
|
||||
const deletedConvos = await mongoose
|
||||
.model('Conversation')
|
||||
.find(conditions as mongoose.FilterQuery<unknown>)
|
||||
.find(conditions as FilterQuery<unknown>)
|
||||
.lean();
|
||||
const promises = deletedConvos.map((convo: Record<string, unknown>) =>
|
||||
convoIndex.deleteDocument(convo.conversationId as string),
|
||||
|
|
@ -464,7 +513,7 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
|
|||
const messageIndex = client.index('messages');
|
||||
const deletedMessages = await mongoose
|
||||
.model('Message')
|
||||
.find(conditions as mongoose.FilterQuery<unknown>)
|
||||
.find(conditions as FilterQuery<unknown>)
|
||||
.lean();
|
||||
const promises = deletedMessages.map((message: Record<string, unknown>) =>
|
||||
messageIndex.deleteDocument(message.messageId as string),
|
||||
|
|
@ -484,13 +533,13 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
|
|||
});
|
||||
|
||||
// Post-findOneAndUpdate hook
|
||||
schema.post('findOneAndUpdate', async function (doc: DocumentWithMeiliIndex) {
|
||||
schema.post('findOneAndUpdate', async function (doc: DocumentWithMeiliIndex, next) {
|
||||
if (!meiliEnabled) {
|
||||
return;
|
||||
return next();
|
||||
}
|
||||
|
||||
if (doc.unfinished) {
|
||||
return;
|
||||
return next();
|
||||
}
|
||||
|
||||
let meiliDoc: Record<string, unknown> | undefined;
|
||||
|
|
@ -507,9 +556,9 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
|
|||
}
|
||||
|
||||
if (meiliDoc && meiliDoc.title === doc.title) {
|
||||
return;
|
||||
return next();
|
||||
}
|
||||
|
||||
doc.postSaveHook?.();
|
||||
doc.postSaveHook?.(next);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { Schema } from 'mongoose';
|
||||
import mongoMeili from '~/models/plugins/mongoMeili';
|
||||
import { conversationPreset } from './defaults';
|
||||
import { IConversation } from '~/types';
|
||||
|
||||
|
|
@ -48,14 +47,4 @@ convoSchema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 });
|
|||
convoSchema.index({ createdAt: 1, updatedAt: 1 });
|
||||
convoSchema.index({ conversationId: 1, user: 1 }, { unique: true });
|
||||
|
||||
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
|
||||
convoSchema.plugin(mongoMeili, {
|
||||
host: process.env.MEILI_HOST,
|
||||
apiKey: process.env.MEILI_MASTER_KEY,
|
||||
/** Note: Will get created automatically if it doesn't exist already */
|
||||
indexName: 'convos',
|
||||
primaryKey: 'conversationId',
|
||||
});
|
||||
}
|
||||
|
||||
export default convoSchema;
|
||||
|
|
|
|||
|
|
@ -21,3 +21,4 @@ export { default as tokenSchema } from './token';
|
|||
export { default as toolCallSchema } from './toolCall';
|
||||
export { default as transactionSchema } from './transaction';
|
||||
export { default as userSchema } from './user';
|
||||
export { default as memorySchema } from './memory';
|
||||
|
|
|
|||
33
packages/data-schemas/src/schema/memory.ts
Normal file
33
packages/data-schemas/src/schema/memory.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { Schema } from 'mongoose';
|
||||
import type { IMemoryEntry } from '~/types/memory';
|
||||
|
||||
const MemoryEntrySchema: Schema<IMemoryEntry> = new Schema({
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'User',
|
||||
index: true,
|
||||
required: true,
|
||||
},
|
||||
key: {
|
||||
type: String,
|
||||
required: true,
|
||||
validate: {
|
||||
validator: (v: string) => /^[a-z_]+$/.test(v),
|
||||
message: 'Key must only contain lowercase letters and underscores',
|
||||
},
|
||||
},
|
||||
value: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
tokenCount: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
updated_at: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
});
|
||||
|
||||
export default MemoryEntrySchema;
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
import mongoose, { Schema } from 'mongoose';
|
||||
import type { IMessage } from '~/types/message';
|
||||
import mongoMeili from '~/models/plugins/mongoMeili';
|
||||
|
||||
const messageSchema: Schema<IMessage> = new Schema(
|
||||
{
|
||||
|
|
@ -166,13 +165,4 @@ messageSchema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 });
|
|||
messageSchema.index({ createdAt: 1 });
|
||||
messageSchema.index({ messageId: 1, user: 1 }, { unique: true });
|
||||
|
||||
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
|
||||
messageSchema.plugin(mongoMeili, {
|
||||
host: process.env.MEILI_HOST,
|
||||
apiKey: process.env.MEILI_MASTER_KEY,
|
||||
indexName: 'messages',
|
||||
primaryKey: 'messageId',
|
||||
});
|
||||
}
|
||||
|
||||
export default messageSchema;
|
||||
|
|
|
|||
|
|
@ -13,6 +13,13 @@ const rolePermissionsSchema = new Schema(
|
|||
[Permissions.USE]: { type: Boolean, default: true },
|
||||
[Permissions.CREATE]: { type: Boolean, default: true },
|
||||
},
|
||||
[PermissionTypes.MEMORIES]: {
|
||||
[Permissions.USE]: { type: Boolean, default: true },
|
||||
[Permissions.CREATE]: { type: Boolean, default: true },
|
||||
[Permissions.UPDATE]: { type: Boolean, default: true },
|
||||
[Permissions.READ]: { type: Boolean, default: true },
|
||||
[Permissions.OPT_OUT]: { type: Boolean, default: true },
|
||||
},
|
||||
[PermissionTypes.AGENTS]: {
|
||||
[Permissions.SHARED_GLOBAL]: { type: Boolean, default: false },
|
||||
[Permissions.USE]: { type: Boolean, default: true },
|
||||
|
|
@ -45,6 +52,12 @@ const roleSchema: Schema<IRole> = new Schema({
|
|||
[Permissions.USE]: true,
|
||||
[Permissions.CREATE]: true,
|
||||
},
|
||||
[PermissionTypes.MEMORIES]: {
|
||||
[Permissions.USE]: true,
|
||||
[Permissions.CREATE]: true,
|
||||
[Permissions.UPDATE]: true,
|
||||
[Permissions.READ]: true,
|
||||
},
|
||||
[PermissionTypes.AGENTS]: {
|
||||
[Permissions.SHARED_GLOBAL]: false,
|
||||
[Permissions.USE]: true,
|
||||
|
|
|
|||
|
|
@ -129,6 +129,15 @@ const userSchema = new Schema<IUser>(
|
|||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
personalization: {
|
||||
type: {
|
||||
memories: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
},
|
||||
default: {},
|
||||
},
|
||||
},
|
||||
{ timestamps: true },
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
import type { Types } from 'mongoose';
|
||||
|
||||
export type ObjectId = Types.ObjectId;
|
||||
export * from './user';
|
||||
export * from './token';
|
||||
export * from './convo';
|
||||
|
|
@ -10,3 +13,5 @@ export * from './role';
|
|||
export * from './action';
|
||||
export * from './assistant';
|
||||
export * from './file';
|
||||
/* Memories */
|
||||
export * from './memory';
|
||||
|
|
|
|||
48
packages/data-schemas/src/types/memory.ts
Normal file
48
packages/data-schemas/src/types/memory.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import type { Types, Document } from 'mongoose';
|
||||
|
||||
// Base memory interfaces
|
||||
export interface IMemoryEntry extends Document {
|
||||
userId: Types.ObjectId;
|
||||
key: string;
|
||||
value: string;
|
||||
tokenCount?: number;
|
||||
updated_at?: Date;
|
||||
}
|
||||
|
||||
export interface IMemoryEntryLean {
|
||||
_id: Types.ObjectId;
|
||||
userId: Types.ObjectId;
|
||||
key: string;
|
||||
value: string;
|
||||
tokenCount?: number;
|
||||
updated_at?: Date;
|
||||
__v?: number;
|
||||
}
|
||||
|
||||
// Method parameter interfaces
|
||||
export interface SetMemoryParams {
|
||||
userId: string | Types.ObjectId;
|
||||
key: string;
|
||||
value: string;
|
||||
tokenCount?: number;
|
||||
}
|
||||
|
||||
export interface DeleteMemoryParams {
|
||||
userId: string | Types.ObjectId;
|
||||
key: string;
|
||||
}
|
||||
|
||||
export interface GetFormattedMemoriesParams {
|
||||
userId: string | Types.ObjectId;
|
||||
}
|
||||
|
||||
// Result interfaces
|
||||
export interface MemoryResult {
|
||||
ok: boolean;
|
||||
}
|
||||
|
||||
export interface FormattedMemoriesResult {
|
||||
withKeys: string;
|
||||
withoutKeys: string;
|
||||
totalTokens?: number;
|
||||
}
|
||||
|
|
@ -12,6 +12,12 @@ export interface IRole extends Document {
|
|||
[Permissions.USE]?: boolean;
|
||||
[Permissions.CREATE]?: boolean;
|
||||
};
|
||||
[PermissionTypes.MEMORIES]?: {
|
||||
[Permissions.USE]?: boolean;
|
||||
[Permissions.CREATE]?: boolean;
|
||||
[Permissions.UPDATE]?: boolean;
|
||||
[Permissions.READ]?: boolean;
|
||||
};
|
||||
[PermissionTypes.AGENTS]?: {
|
||||
[Permissions.SHARED_GLOBAL]?: boolean;
|
||||
[Permissions.USE]?: boolean;
|
||||
|
|
|
|||
|
|
@ -30,6 +30,9 @@ export interface IUser extends Document {
|
|||
}>;
|
||||
expiresAt?: Date;
|
||||
termsAccepted?: boolean;
|
||||
personalization?: {
|
||||
memories?: boolean;
|
||||
};
|
||||
createdAt?: Date;
|
||||
updatedAt?: Date;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue