mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
* 🧯 feat: Recover Conversations Whose Attachments Keep Failing Once a branch carries a problem attachment, every turn resends it and fails the same way. A failed response whose branch carries files now offers "Retry without files" (re-asks the failed turn with no files as a new version) and, when earlier turns carry files, "Branch without files" (a copy up to the last good answer with every upload and code output left out, with the failed message restored to the composer). File-shaped errors say so in the card. The edit view shows a user message's files as removable chips; "Update & rerun" sends only the kept files and Save is disabled after a removal. Fork is offered on a failed turn and gains a "Leave out files" option, backed by a new `excludeFiles` fork option that drops `files`, `attachments` and the conversation's `files` from the copy. Bedrock rejects a request whose documents share a name, and history resends every earlier document, so repeated names are now numbered before formatting. * 🧯 fix: Cover Steered Files, Hand Off the Whole Failed Turn, Name Replayed Documents Once - Excluding files from a fork also strips attachments steered into a response (`content[].files` on steer parts), which replay like uploads, and the decision lives in `forkFileScope` in packages/api. - The recovery card counts steered attachments as files, and reads the specific code inside a generic `error` envelope, so a rate limit is not offered files as the fix. - "Branch without files" hands the failed turn to the copy's composer the way a message is put back for editing: text through the in-memory handoff, quotes, manual skills and the reasoning override through their compose-time atoms. - Document names are deduplicated over the formatted payload after the steer stamp, so replayed steer documents are numbered too. - Fork stays withheld on a failed response, whose copy would carry the error as an ordinary answer; the card's branch action copies up to the turn before. - Remove-file buttons are natively disabled while the editor is locked. * 🧯 fix: Keep Search Sources, Scope Root Retries, Skip Legacy Rate Limits - A fork without files keeps metadata-only attachments (search sources, file search results) and drops only file-backed ones; the recovery card likewise counts only file-backed attachments as files. - A turn sent at the root takes none of the conversation's file refs: its siblings' uploads (an edited first message, or one retried without its files) no longer come back through the unanchored-continuation fallback. - Rows that carry a legacy LangChain model, key or rate-limit code are not offered files as the fix. * 🧯 fix: Align File Exclusion With Replay's File Shapes, Skip Assistants, Verify the Copy - Shared `messageCarriesFiles` (with `isFileContentPart`, `hasContentPartFiles`, `isFileAttachment`) in data-provider names every shape historical replay reads a file by: uploads, file-backed attachments, attached `files` on any content part, and `image_file`, `file` or direct `file_id` locators. The fork drops all of them and the recovery card detects all of them. - Assistants conversations get no recovery actions: their upstream thread keeps every attachment, and those endpoints offer no fork either. - "Branch without files" checks the returned copy; a server that ignored `excludeFiles` gets an error toast instead of a false recovery. * 🧯 fix: Type Replay's Provider-Native File Locators for the Part Predicates
206 lines
6.5 KiB
JavaScript
206 lines
6.5 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const { MongoMemoryServer } = require('mongodb-memory-server');
|
|
const { Constants, EModelEndpoint, ForkOptions } = require('librechat-data-provider');
|
|
const { createModels, runAsSystem, tenantStorage } = require('@librechat/data-schemas');
|
|
|
|
jest.mock('~/server/services/Config', () => ({
|
|
getAppConfig: jest.fn().mockResolvedValue({ interfaceConfig: {} }),
|
|
getEndpointsConfig: jest.fn().mockResolvedValue({ openAI: { userProvide: false } }),
|
|
}));
|
|
|
|
jest.mock('~/server/controllers/ModelController', () => ({
|
|
getModelsConfig: jest.fn().mockResolvedValue({ openAI: ['gpt-4o'] }),
|
|
}));
|
|
|
|
createModels(mongoose);
|
|
const { forkConversation } = require('./fork');
|
|
|
|
const owner = 'fork-owner';
|
|
const tenantId = 'fork-tenant';
|
|
const conversationId = 'source-conversation';
|
|
|
|
const screenshot = {
|
|
file_id: 'screenshot-1',
|
|
filename: 'screenshot.png',
|
|
filepath: '/images/screenshot.png',
|
|
type: 'image/png',
|
|
};
|
|
const chart = {
|
|
file_id: 'chart-1',
|
|
filename: 'chart.png',
|
|
filepath: '/images/chart.png',
|
|
type: 'image/png',
|
|
toolCallId: 'call-1',
|
|
};
|
|
|
|
/** One branch: a question with a screenshot, an answer with a code output, a follow-up, and a
|
|
* failed answer; plus a sibling branch off the first question that the direct path skips. */
|
|
const sourceMessages = [
|
|
{
|
|
messageId: 'user-1',
|
|
parentMessageId: Constants.NO_PARENT,
|
|
text: 'What does this screenshot show?',
|
|
isCreatedByUser: true,
|
|
files: [screenshot],
|
|
tokenCount: 1500,
|
|
},
|
|
{
|
|
messageId: 'assistant-1',
|
|
parentMessageId: 'user-1',
|
|
text: 'A chart of weekly sales.',
|
|
isCreatedByUser: false,
|
|
content: [
|
|
{ type: 'text', text: 'A chart of weekly sales.' },
|
|
{
|
|
type: 'steer',
|
|
steer: 'Use this screenshot instead',
|
|
steerId: 'steer-1',
|
|
files: [screenshot],
|
|
},
|
|
],
|
|
attachments: [chart],
|
|
tokenCount: 12,
|
|
},
|
|
{
|
|
messageId: 'user-2',
|
|
parentMessageId: 'assistant-1',
|
|
text: 'Plot it again in blue.',
|
|
isCreatedByUser: true,
|
|
tokenCount: 8,
|
|
},
|
|
{
|
|
messageId: 'assistant-2',
|
|
parentMessageId: 'user-2',
|
|
text: 'Error uploading code environment file: 429',
|
|
isCreatedByUser: false,
|
|
error: true,
|
|
},
|
|
{
|
|
messageId: 'assistant-sibling',
|
|
parentMessageId: 'user-1',
|
|
text: 'An alternate answer.',
|
|
isCreatedByUser: false,
|
|
},
|
|
];
|
|
|
|
async function seedSourceConversation() {
|
|
await runAsSystem(async () => {
|
|
await mongoose.models.Conversation.create({
|
|
conversationId,
|
|
user: owner,
|
|
tenantId,
|
|
title: 'Screenshots',
|
|
endpoint: EModelEndpoint.openAI,
|
|
model: 'gpt-4o',
|
|
files: [screenshot.file_id, chart.file_id],
|
|
});
|
|
await mongoose.models.Message.insertMany(
|
|
sourceMessages.map((message, index) => ({
|
|
...message,
|
|
conversationId,
|
|
user: owner,
|
|
tenantId,
|
|
sender: message.isCreatedByUser ? 'User' : 'GPT-4o',
|
|
endpoint: EModelEndpoint.openAI,
|
|
model: 'gpt-4o',
|
|
createdAt: new Date(Date.UTC(2026, 0, 1, 0, index)),
|
|
})),
|
|
);
|
|
});
|
|
}
|
|
|
|
function fork(options) {
|
|
return tenantStorage.run({ tenantId, userId: owner }, () =>
|
|
forkConversation({
|
|
requestUserId: owner,
|
|
originalConvoId: conversationId,
|
|
targetMessageId: 'assistant-2',
|
|
option: ForkOptions.DIRECT_PATH,
|
|
records: true,
|
|
...options,
|
|
}),
|
|
);
|
|
}
|
|
|
|
function readPersisted(forkedConversationId) {
|
|
return runAsSystem(async () => ({
|
|
conversation: await mongoose.models.Conversation.findOne({
|
|
conversationId: forkedConversationId,
|
|
}).lean(),
|
|
messages: await mongoose.models.Message.find({ conversationId: forkedConversationId })
|
|
.sort({ createdAt: 1 })
|
|
.lean(),
|
|
}));
|
|
}
|
|
|
|
describe('forkConversation with excludeFiles (database)', () => {
|
|
let mongoServer;
|
|
|
|
beforeAll(async () => {
|
|
mongoServer = await MongoMemoryServer.create();
|
|
await mongoose.connect(mongoServer.getUri());
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await mongoose.connection.dropDatabase();
|
|
await seedSourceConversation();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await mongoose.disconnect();
|
|
await mongoServer?.stop();
|
|
});
|
|
|
|
it('copies the visible branch with no uploads, code outputs or conversation file list', async () => {
|
|
const result = await fork({ excludeFiles: true });
|
|
const persisted = await readPersisted(result.conversation.conversationId);
|
|
|
|
expect(persisted.conversation.conversationId).not.toBe(conversationId);
|
|
expect(persisted.conversation.files ?? []).toEqual([]);
|
|
expect(persisted.messages.map((message) => message.text)).toEqual([
|
|
'What does this screenshot show?',
|
|
'A chart of weekly sales.',
|
|
'Plot it again in blue.',
|
|
'Error uploading code environment file: 429',
|
|
]);
|
|
for (const message of persisted.messages) {
|
|
expect(message.files ?? []).toEqual([]);
|
|
expect(message.attachments ?? []).toEqual([]);
|
|
}
|
|
expect(persisted.messages[1].content).toEqual([
|
|
{ type: 'text', text: 'A chart of weekly sales.' },
|
|
{ type: 'steer', steer: 'Use this screenshot instead', steerId: 'steer-1' },
|
|
]);
|
|
|
|
const [question, answer, followUp, failure] = persisted.messages;
|
|
expect(question.parentMessageId).toBe(Constants.NO_PARENT);
|
|
expect(answer.parentMessageId).toBe(question.messageId);
|
|
expect(followUp.parentMessageId).toBe(answer.messageId);
|
|
expect(failure.parentMessageId).toBe(followUp.messageId);
|
|
/** A count that covered dropped uploads or steered files goes with them; text-only turns
|
|
* keep theirs. */
|
|
expect(question.tokenCount).toBeUndefined();
|
|
expect(answer.tokenCount).toBeUndefined();
|
|
expect(followUp.tokenCount).toBe(8);
|
|
});
|
|
|
|
it('keeps the original conversation and its files intact', async () => {
|
|
await fork({ excludeFiles: true });
|
|
const original = await readPersisted(conversationId);
|
|
|
|
expect(original.conversation.files).toEqual([screenshot.file_id, chart.file_id]);
|
|
expect(original.messages).toHaveLength(sourceMessages.length);
|
|
const byId = new Map(original.messages.map((message) => [message.messageId, message]));
|
|
expect(byId.get('user-1').files).toEqual([screenshot]);
|
|
expect(byId.get('assistant-1').attachments).toEqual([chart]);
|
|
});
|
|
|
|
it('still copies files when the option is off', async () => {
|
|
const result = await fork({});
|
|
const persisted = await readPersisted(result.conversation.conversationId);
|
|
|
|
expect(persisted.conversation.files).toEqual([screenshot.file_id, chart.file_id]);
|
|
expect(persisted.messages[0].files).toEqual([screenshot]);
|
|
expect(persisted.messages[1].attachments).toEqual([chart]);
|
|
});
|
|
});
|