Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/app/clients/specs/BaseClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,7 @@ describe('BaseClient', () => {
temperature: 0.7,
modelLabel: 'GPT-3.5',
pinned: true,
tagIds: ['507f1f77bcf86cd799439011'],
subagentThread: {
rootConversationId: 'root-conversation',
parentConversationId: 'parent-conversation',
Expand Down Expand Up @@ -1856,6 +1857,10 @@ describe('BaseClient', () => {
// Sidebar metadata is never part of endpointOptions, so sweeping it would
// unpin a chat every time it received a message.
expect(saveOptions.unsetFields).not.toHaveProperty('pinned');
for (const [, fields, options] of saveConvo.mock.calls) {
expect(fields).not.toHaveProperty('tagIds');
expect(options.unsetFields).not.toHaveProperty('tagIds');
}

// Mock saveConvo to return the expected fields
saveConvo.mockImplementation((req, fields) => {
Expand Down
5 changes: 3 additions & 2 deletions api/db/connect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require('dotenv').config();
const { isEnabled, instrumentMongooseQueryMetrics } = require('@librechat/api');
const { logger } = require('@librechat/data-schemas');
const { logger, assertConversationTagMigration } = require('@librechat/data-schemas');

const mongoose = require('mongoose');
const MONGO_URI = process.env.MONGO_URI;
Expand Down Expand Up @@ -71,7 +71,8 @@ async function connectDb() {
logger.info('Mongo Connection options');
logger.info(JSON.stringify(opts, null, 2));
mongoose.set('strictQuery', true);
cached.promise = mongoose.connect(MONGO_URI, opts).then((mongoose) => {
cached.promise = mongoose.connect(MONGO_URI, opts).then(async (mongoose) => {
await assertConversationTagMigration(mongoose.connection);
return mongoose;
});
}
Expand Down
5 changes: 5 additions & 0 deletions api/db/indexSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ async function performSync(flowManager, flowId, flowType) {
);
}

const tagModel = mongoose.models.ConversationTag;
// Deleted catalog rows cannot leave Mongo flags; every pass also sweeps their index entries.
if (tagModel?.syncWithMeili) await tagModel.syncWithMeili();
Comment thread
dustinhealy marked this conversation as resolved.
Resolved

if (messageSyncError) {
throw messageSyncError;
}
Expand Down Expand Up @@ -410,6 +414,7 @@ async function indexSync() {
}
await Message.syncWithMeili();
await Conversation.syncWithMeili();
await mongoose.models.ConversationTag?.syncWithMeili?.();
} catch (err) {
logger.error('[indexSync] Trouble creating indices, try restarting the server.', err);
}
Expand Down
22 changes: 22 additions & 0 deletions api/db/indexSync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,28 @@ describe('performSync() - syncThreshold logic', () => {
expect(Conversation.cleanupExcludedMeiliIndex).not.toHaveBeenCalled();
});

test('retries catalog orphan cleanup even when surviving Mongo rows are fully indexed', async () => {
const complete = { totalProcessed: 0, totalDocuments: 0, pendingCleanup: 0, isComplete: true };
Message.getSyncProgress.mockResolvedValue(complete);
Conversation.getSyncProgress.mockResolvedValue(complete);
const Tag = createMockModel('conversationtags');
Tag.getSyncProgress.mockResolvedValue(complete);
Tag.syncWithMeili
.mockRejectedValueOnce(new Error('catalog cleanup unavailable'))
.mockResolvedValue(undefined);
const currentMongoose = require('mongoose');
currentMongoose.models.ConversationTag = Tag;
try {
const indexSync = require('./indexSync');
await expect(indexSync()).rejects.toThrow('catalog cleanup unavailable');
await indexSync();
expect(Tag.syncWithMeili).toHaveBeenCalledTimes(2);
expect(Tag.getSyncProgress).not.toHaveBeenCalled();
} finally {
delete currentMongoose.models.ConversationTag;
}
});

test('continues conversation cleanup when message cleanup fails transiently', async () => {
const cleanupError = new Error('message cleanup timed out');
Message.getSyncProgress.mockResolvedValue({
Expand Down
2 changes: 2 additions & 0 deletions api/server/routes/__test-utils__/convos-route-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ module.exports = {
conversationModel: () => ({
getConvosByCursor: jest.fn(),
getConvo: jest.fn(),
getConvoWithTags: jest.fn(),
deleteConvos: jest.fn(),
deleteMessages: jest.fn().mockResolvedValue({ deletedCount: 0 }),
saveConvo: jest.fn(),
Expand All @@ -197,6 +198,7 @@ module.exports = {
sharedModels: () => ({
getConvosByCursor: jest.fn(),
getConvo: jest.fn(),
getConvoWithTags: jest.fn(),
deleteConvos: jest.fn(),
deleteMessages: jest.fn().mockResolvedValue({ deletedCount: 0 }),
archiveAllConvos: jest.fn(),
Expand Down
12 changes: 6 additions & 6 deletions api/server/routes/__tests__/convos.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('Convos Routes', () => {
deleteToolCalls,
deleteConvos,
deleteMessages,
getConvo,
getConvoWithTags,
saveConvo,
} = require('~/models');
const {
Expand Down Expand Up @@ -203,30 +203,30 @@ describe('Convos Routes', () => {

describe('GET /:conversationId', () => {
it('returns an ordinary owned conversation', async () => {
getConvo.mockResolvedValue({ conversationId: 'ordinary', title: 'Ordinary' });
getConvoWithTags.mockResolvedValue({ conversationId: 'ordinary', title: 'Ordinary' });

const response = await request(app).get('/api/convos/ordinary');

expect(response.status).toBe(200);
expect(response.body).toEqual({ conversationId: 'ordinary', title: 'Ordinary' });
expect(getConvo).toHaveBeenCalledWith('test-user-123', 'ordinary');
expect(getConvoWithTags).toHaveBeenCalledWith('test-user-123', 'ordinary');
});

it('returns the same not-found response for an owned child thread', async () => {
getConvo.mockResolvedValue({
getConvoWithTags.mockResolvedValue({
conversationId: 'child',
subagentThread: { parentConversationId: 'parent' },
});

const childResponse = await request(app).get('/api/convos/child');
getConvo.mockResolvedValue(null);
getConvoWithTags.mockResolvedValue(null);
const missingResponse = await request(app).get('/api/convos/missing');

expect(childResponse.status).toBe(404);
expect(childResponse.text).toBe('');
expect(childResponse.status).toBe(missingResponse.status);
expect(childResponse.text).toBe(missingResponse.text);
expect(getConvo).toHaveBeenNthCalledWith(1, 'test-user-123', 'child');
expect(getConvoWithTags).toHaveBeenNthCalledWith(1, 'test-user-123', 'child');
});
});

Expand Down
72 changes: 72 additions & 0 deletions api/server/routes/__tests__/tags.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const express = require('express');
const request = require('supertest');
const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');
const { createModels, createMethods } = require('@librechat/data-schemas');

jest.mock('~/models', () => ({}));
jest.mock('~/server/middleware', () => ({
requireJwtAuth: (req, res, next) => {
req.user = { id: 'owner' };
next();
},
}));
jest.mock('@librechat/api', () => ({
generateCheckAccess: () => (req, res, next) => next(),
}));

let mongo;
let app;
let methods;

beforeAll(async () => {
mongo = await MongoMemoryServer.create();
await mongoose.connect(mongo.getUri(), { autoIndex: false });
createModels(mongoose);
methods = createMethods(mongoose);
Object.assign(require('~/models'), methods);
app = express();
app.use(express.json());
app.use('/tags', require('../tags'));
});

afterEach(async () => {
jest.restoreAllMocks();
await mongoose.models.Conversation.deleteMany({});
await mongoose.models.ConversationTag.deleteMany({});
});

afterAll(async () => {
await mongoose.disconnect();
await mongo.stop();
});

it('returns the created bookmark on success', async () => {
const response = await request(app).post('/tags').send({ tag: 'bookmark' }).expect(200);
expect(response.body).toMatchObject({ tag: 'bookmark', count: 0 });
expect(response.body._id).toEqual(expect.any(String));
});

it('returns a failed response when deletion wins a create-and-attach race', async () => {
const Conversation = mongoose.models.Conversation;
await Conversation.create({ user: 'owner', conversationId: 'convo', endpoint: 'openAI' });
const tag = await methods.createConversationTag('owner', { tag: 'bookmark' });
const original = Conversation.collection.findOneAndUpdate.bind(Conversation.collection);
jest
.spyOn(Conversation.collection, 'findOneAndUpdate')
.mockImplementationOnce(async (...args) => {
await methods.deleteConversationTag('owner', String(tag._id), null, true);
return original(...args);
});

const response = await request(app)
.post('/tags')
.send({ tag: 'bookmark', addToConversation: true, conversationId: 'convo' })
.expect(404);

expect(response.body).toEqual({ error: 'Tag not found' });
expect(await Conversation.findOne({ conversationId: 'convo' }).lean()).toMatchObject({
tagIds: [],
});
expect(await mongoose.models.ConversationTag.countDocuments({ user: 'owner' })).toBe(0);
});
3 changes: 2 additions & 1 deletion api/server/routes/convos.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ router.get('/', async (req, res) => {
isArchived,
pinned,
tags,
tagIds: req.query.tagIds == null ? undefined : [].concat(req.query.tagIds),
Comment thread
dustinhealy marked this conversation as resolved.
Resolved
search,
sortBy,
sortDirection,
Expand Down Expand Up @@ -202,7 +203,7 @@ router.get('/:parentConversationId/subagents/:threadId', subagentThreadViewHandl

router.get('/:conversationId', async (req, res) => {
const { conversationId } = req.params;
const convo = await db.getConvo(req.user.id, conversationId);
const convo = await db.getConvoWithTags(req.user.id, conversationId);

if (convo && convo.subagentThread == null) {
res.status(200).json(convo);
Expand Down
46 changes: 37 additions & 9 deletions api/server/routes/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ router.use(checkBookmarkAccess);
*/
router.get('/', async (req, res) => {
try {
const tags = await getConversationTags(req.user.id);
const tags = await getConversationTags(req.user.id, req.user.tenantId ?? null);
if (tags) {
res.status(200).json(tags);
} else {
Expand All @@ -51,7 +51,10 @@ router.get('/', async (req, res) => {
*/
router.post('/', async (req, res) => {
try {
const tag = await createConversationTag(req.user.id, req.body);
const tag = await createConversationTag(req.user.id, req.body, req.user.tenantId ?? null);
if (!tag) {
return res.status(404).json({ error: 'Tag not found' });
}
res.status(200).json(tag);
} catch (error) {
logger.error('Error creating conversation tag:', error);
Expand All @@ -65,10 +68,19 @@ router.post('/', async (req, res) => {
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
router.put('/:tag', async (req, res) => {
router.put(['/id/:tagId', '/:tag'], async (req, res) => {
try {
const decodedTag = decodeURIComponent(req.params.tag);
const tag = await updateConversationTag(req.user.id, decodedTag, req.body);
const decodedTag = req.params.tagId ?? req.params.tag;
if (req.body.tag && req.body.tag !== decodedTag && req.body.position !== undefined) {
return res.status(400).json({ error: 'Rename and position changes must be sent separately' });
Comment thread
dustinhealy marked this conversation as resolved.
Resolved
}
const tag = await updateConversationTag(
req.user.id,
decodedTag,
req.body,
req.user.tenantId ?? null,
req.params.tagId != null,
);
if (tag) {
res.status(200).json(tag);
} else {
Expand All @@ -86,10 +98,15 @@ router.put('/:tag', async (req, res) => {
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
router.delete('/:tag', async (req, res) => {
router.delete(['/id/:tagId', '/:tag'], async (req, res) => {
try {
const decodedTag = decodeURIComponent(req.params.tag);
const tag = await deleteConversationTag(req.user.id, decodedTag);
const decodedTag = req.params.tagId ?? req.params.tag;
const tag = await deleteConversationTag(
req.user.id,
decodedTag,
req.user.tenantId ?? null,
req.params.tagId != null,
);
if (tag) {
res.status(200).json(tag);
} else {
Expand All @@ -109,10 +126,21 @@ router.delete('/:tag', async (req, res) => {
*/
router.put('/convo/:conversationId', async (req, res) => {
try {
const byId = req.body.tagIds !== undefined;
const values = byId ? req.body.tagIds : req.body.tags;
if (
!Array.isArray(values) ||
values.some((value) => typeof value !== 'string') ||
(byId && req.body.tags !== undefined)
) {
return res.status(400).json({ error: 'Provide either tags or tagIds as a string array' });
}
const conversationTags = await updateTagsForConversation(
req.user.id,
req.params.conversationId,
req.body.tags,
values,
req.user.tenantId ?? null,
byId,
Comment thread
dustinhealy marked this conversation as resolved.
Resolved
);
res.status(200).json(conversationTags);
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions api/server/utils/import/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async function forkConversation({
new Date(),
originalConvo,
);
await importBatchBuilder.saveBatch();
await importBatchBuilder.saveBatch({ tagSource: 'owned' });
logger.debug(
`user: ${requestUserId} | New conversation "${
newTitle || originalConvo.title
Expand Down Expand Up @@ -599,7 +599,7 @@ async function duplicateConversation({

const duplicateTitle = title || originalConvo.title;
const result = importBatchBuilder.finishConversation(duplicateTitle, new Date(), originalConvo);
await importBatchBuilder.saveBatch();
await importBatchBuilder.saveBatch({ tagSource: 'owned' });
logger.debug('Conversation duplicated', {
userId,
sourceConversationId: conversationId,
Expand Down
4 changes: 2 additions & 2 deletions api/server/utils/import/importBatchBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ImportBatchBuilder {
* @returns {Promise<void>} A promise that resolves when the batch is saved.
* @throws {Error} If there is an error saving the batch.
*/
async saveBatch() {
async saveBatch({ tagSource = 'portable' } = {}) {
const tenantId = getTenantId();
assertConversationImportWriteSize({
conversations: this.conversations,
Expand Down Expand Up @@ -217,7 +217,7 @@ class ImportBatchBuilder {

try {
await executeConversationImportWrites({
saveConversations: () => bulkSaveConvos(this.conversations),
saveConversations: () => bulkSaveConvos(this.conversations, { tagSource }),
saveMessages: () => bulkSaveMessages(this.messages, true),
updateTagCounts: () => bulkIncrementTagCounts(this.requestUserId, tags),
deleteMessages: () => deleteImportedMessages(cleanupScope),
Expand Down
Loading
Loading