LibreChat/api/server/routes/messages.js
Danny Avila 3f442e4dbe
🪶 perf: Load Tool-Call Previews and Fetch Full Parts on Demand (#16753)
* 🧾 perf: Load Tool-Call Previews and Fetch Full Parts on Demand

Long agent conversations shipped every tool output, argument string and
subagent transcript on load, though the cards render collapsed: 12-18 MB
of message JSON per chat, enough to crash iPhone Safari tabs.

A client that adds ?toolPreviews=1 to the conversation load now receives
bounded previews of settled tool calls (start and end of long outputs,
JSON arguments and outputs kept valid with only their longest strings
shortened, exit-status trailers kept whole, subagent transcripts omitted)
with explicit markers and full lengths. Opening a card or a subagent
panel fetches the stored part from a new owner-scoped endpoint,
GET /api/messages/:conversationId/:messageId/parts/:partIndex, cached
under its own React Query key. Clients that do not ask, exports and share
links keep the full payload; bounds are configurable under
toolCallPreviews in librechat.yaml, and a client-authored message that
carries previews is refused rather than saved.

* 🧾 fix: Validate Part Reads in Parallel and Preview Fork Responses

The full-part read now starts beside conversation-access validation
instead of after it, and keeps the active-job fallback the conversation
read has, so a conversation still live before its first save can expand
its previews. Fork, duplicate and shared-link fork responses honor the
preview parameter, so they no longer seed the conversation cache with
the full history. Background-task results whose structure cannot fit a
valid JSON preview go whole, image cards keep their prompts, and the
subagent panel offers a retry when its stored part fails to load.

* 🧾 fix: Key Hydrated Parts by Preview Revision and Call Identity

A fetched part is now cached under the preview's revision and replaces
only the fields the server shortened, so a call whose stored output
changes later (a harvested background result) is fetched again rather
than rolled back. Part lookups carry the run-step and agent identity and
refuse an ambiguous fallback, since provider tool-call ids can repeat
within one response. A subagent that finished without final text has
its transcript omitted like any other settled call, and JSON nested past
a fixed depth falls back to a text preview; a call whose preview fails
is sent whole instead of failing the conversation load.

* 🧾 fix: Release Hydrated Parts With Their History and Bound JSON Work

Full tool-call parts fetched for a conversation are removed when its
message history is released or cleared, so they cannot outlive it under
their own key. The structure-preserving JSON preview now bails out before
cloning when the value has more nodes or strings than could ever fit,
keeps a __proto__ key as ordinary data, and the part endpoint accepts any
safe-integer index instead of an arbitrary ceiling.

* 🧾 fix: Count Property Names Before Cloning a JSON Preview

Property names are never shortened, so the pre-check now adds their
length to its budget and falls back to the text preview before any
cloning when they alone could not fit.

* 🧾 fix: Preview Artifact and Branch Responses and Rehydrate Subagent Prompts

Artifact edits and parallel-response branches merge the message the
server returns into the conversation cache, so both requests now ask
for previews and their routes honor it. The subagent panel reads the
prompt from the stored arguments once they load, and the part endpoint
no longer caps identifier length, so any previewed call can be fetched.

* 🧾 fix: Preview Only Tool Calls That Can Be Fetched Back by Identity

A call without an id has nothing but its position to name it, and the
position can shift on the client, so it is left whole instead of being
previewed. The part endpoint now requires a tool-call id, so an indexed
lookup always confirms the call it returns.

* 🧾 fix: Fetch Parts by Exact Identity and Preview Only Unique Ones

The part endpoint now finds a call by its exact identity (provider id,
run-step id, agent) instead of trusting the client's index, and the
preview pass only shortens calls whose identity is unique in their
message, so every preview names exactly one stored part. Legacy
web-search failures that carry their error phrase mid-output go whole,
since the collapsed card reads that phrase anywhere.

* 🧾 fix: Bound the JSON Fit Walk Before Queueing and Normalize Empty Identities

The structure check now counts an array's or object's children against
the node budget before queueing them, so a very wide value is refused
without building a work list its size. An empty run-step or agent id is
treated as absent by the part lookup, matching how the preview pass and
the client already treat it.

* 🧾 fix: Key Fetched Parts to the Stored Version and Hydrate Search Details

Each preview now carries `previewRevision`, when its message last
changed, and the client keys fetched parts to that plus a fingerprint of
the whole preview, so any change to the stored call is fetched again.
The web-search details card asks for the stored call when it opens, so
its query is never left shortened.

* 🧾 fix: Cache Fetched Parts by Identity and Hold Copy Until Hydrated

The part query key drops the client-side index, which only rides along in the request; the server locates the call by message and exact identity, so a shifted index serves the cached copy instead of downloading it again. Copy controls inside a previewed part are disabled until the stored content arrives, so a slow or failed fetch cannot export shortened text.

* 🧾 fix: Shape Shared Forks With the Recipient's Bounds and Keep JSON Numbers Exact

A shared-link fork resolves the share owner's config for its content checks, but the forked conversation belongs to the requester, so its preview bounds now come from the requester's tenant and role (prepareResultToolCallPreviews). A structure-preserving JSON preview now requires every number in the text to survive a parse and re-serialization as the same value; an integer past 2^53, an out-of-range exponent or an over-precise decimal falls back to the text preview (or the whole output for parsed-output tools) instead of showing a changed value.
2026-10-05 07:42:45 -04:00

834 lines
29 KiB
JavaScript

const express = require('express');
const { v4: uuidv4 } = require('uuid');
const { logger, CLIENT_MESSAGE_SELECT, MEILI_SEARCH_LIMIT } = require('@librechat/data-schemas');
const {
ContentTypes,
feedbackSchema,
isAssistantsEndpoint,
stripReasoningLabelMetadata,
} = require('librechat-data-provider');
const {
unescapeLaTeX,
countTokens,
sendFeedbackScore,
traceIdForMessage,
mergeQuotedTextForCount,
requireFeedbackEnabled,
CHILD_THREAD_READ_ONLY_ERROR,
isSubagentThreadWriteBlocked,
createContentFilter,
extractFeedbackContent,
extractStoredMessageContent,
assertStoredMessageMutationAllowed,
assertChatMutationAllowed,
assertStoredMessageBranchAllowed,
reportLocatorTraversalFailure,
mergeUserSubmittedPaths,
mergeUserSubmittedMessageFieldPaths,
isContentFilterError,
withoutTraceRefs,
createPrivateTextView,
stripPrivateMessageFields,
applyForcedRetention,
prepareToolCallPreviews,
createToolCallPartHandler,
rejectToolCallPreviewWrites,
withMessageToolCallPreviews,
} = require('@librechat/api');
const subagentThreadTaskStore = require('~/server/services/Endpoints/agents/subagentThreadStore');
const { getAppConfig } = require('~/server/services/Config');
const { findAllArtifacts, replaceArtifactContent } = require('~/server/services/Artifacts/update');
const {
requireJwtAuth,
validateMessageReq,
configMiddleware,
sendValidationResponse,
canReadActiveJobConversation,
createMessageRequestValidation,
prepareMessageRequestValidation,
} = require('~/server/middleware');
const db = require('~/models');
const retentionStore = { stampForcedRetention: db.stampForcedRetention };
const router = express.Router();
const filterStoredMessageContent = createContentFilter({
messageCount: 1,
onTraversalFailure: reportLocatorTraversalFailure,
getFilters: (req) => req.config?.filters,
getMessageRoles: (req) => [req.body?.role],
getOpaqueFileInput: (req) => req.body,
getFiles: db.getFiles,
extract: (req) => extractStoredMessageContent(req.body),
});
const filterFeedbackContent = createContentFilter({
onTraversalFailure: reportLocatorTraversalFailure,
getFilters: (req) => req.config?.filters,
extract: (req) => extractFeedbackContent(req.body),
});
const toolCallPreviewDeps = { getAppConfig };
const readToolCallPart = createToolCallPartHandler({
getMessages: db.getMessages,
validate: (req) => createMessageRequestValidation(req, { activeJobMessageRead: true }),
sendValidationResponse: (res, result) => sendValidationResponse(res, result),
});
const messageMutationMiddleware = [validateMessageReq, configMiddleware];
const storedMessageMutationMiddleware = [
validateMessageReq,
rejectToolCallPreviewWrites,
configMiddleware,
filterStoredMessageContent,
];
router.use(requireJwtAuth);
router.post(
'/:conversationId/owner-text',
createPrivateTextView({
read: db.getPrivateMessageTexts,
getKey: () => process.env.CREDS_KEY ?? '',
}),
);
async function rejectSubagentThreadWrite(req, res, conversationId) {
const blocked = await isSubagentThreadWriteBlocked(
{
getConvo: async (...args) => {
const conversation = await db.getConvo(...args);
req.resolvedConversation = conversation;
return conversation;
},
store: subagentThreadTaskStore,
},
{
userId: req.user.id,
conversationId,
...(typeof req.user.tenantId === 'string' && req.user.tenantId !== ''
? { tenantId: req.user.tenantId }
: {}),
},
);
if (!blocked) {
return false;
}
res.status(409).json({ error: CHILD_THREAD_READ_ONLY_ERROR });
return true;
}
router.get('/', async (req, res) => {
try {
const user = req.user.id ?? '';
const {
cursor = null,
sortBy = 'updatedAt',
sortDirection = 'desc',
pageSize: pageSizeRaw,
conversationId,
messageId,
search,
} = req.query;
const parsedPageSize = parseInt(pageSizeRaw, 10);
const pageSize = Number.isFinite(parsedPageSize) && parsedPageSize > 0 ? parsedPageSize : 25;
let response;
const sortField = ['endpoint', 'createdAt', 'updatedAt'].includes(sortBy)
? sortBy
: 'createdAt';
const sortOrder = sortDirection === 'asc' ? 1 : -1;
let scopedMessageRead;
const previewToolCalls = prepareToolCallPreviews(req, toolCallPreviewDeps);
if (typeof conversationId === 'string') {
const ownershipRead = db.getConvoOwnership(user, conversationId);
/** Client-facing reads never expose server-private fields such as `contextMeta`. */
const messageRead = messageId
? db.getMessages({ conversationId, messageId, user }, CLIENT_MESSAGE_SELECT)
: db.getMessagesByCursor(
{ conversationId, user },
{ sortField, sortOrder, limit: pageSize, cursor, select: CLIENT_MESSAGE_SELECT },
);
scopedMessageRead = Promise.resolve(messageRead).then(
(value) => ({ ok: true, value }),
(error) => ({ ok: false, error }),
);
const conversation = await ownershipRead;
const canReadActiveJob =
conversation == null &&
!messageId &&
(await canReadActiveJobConversation(req, conversationId));
if ((!conversation && !canReadActiveJob) || conversation?.subagentThread != null) {
return res.status(404).json({ error: 'Conversation not found' });
}
} else if (conversationId) {
return res.status(404).json({ error: 'Conversation not found' });
}
if (conversationId && messageId) {
const messageResult = await scopedMessageRead;
if (!messageResult.ok) {
throw messageResult.error;
}
const messages = messageResult.value;
response = {
messages: await previewToolCalls(messages?.length ? [messages[0]] : []),
nextCursor: null,
};
} else if (conversationId) {
const messageResult = await scopedMessageRead;
if (!messageResult.ok) {
throw messageResult.error;
}
response = {
...messageResult.value,
messages: await previewToolCalls(messageResult.value.messages),
};
} else if (search) {
const searchResults = await db.searchMessages(
search,
{
filter: `user = "${user}"`,
limit: Math.min(pageSize, MEILI_SEARCH_LIMIT),
},
true,
);
const messages = searchResults.hits || [];
const result = await db.getConvosQueried(req.user.id, messages, cursor, pageSize);
const messageIds = [];
const cleanedMessages = [];
for (let i = 0; i < messages.length; i++) {
let message = messages[i];
if (result.convoMap[message.conversationId]) {
messageIds.push(message.messageId);
cleanedMessages.push(message);
}
}
const dbMessages = await db.getMessages(
{
user,
messageId: { $in: messageIds },
},
CLIENT_MESSAGE_SELECT,
);
const dbMessageMap = {};
for (const dbMessage of dbMessages) {
dbMessageMap[dbMessage.messageId] = dbMessage;
}
const activeMessages = [];
for (const message of cleanedMessages) {
const convo = result.convoMap[message.conversationId];
const dbMessage = dbMessageMap[message.messageId];
/** Search may hydrate server-private fields; only a public projection leaves. */
const publicHit = stripPrivateMessageFields(message);
activeMessages.push({
...publicHit,
title: convo.title,
conversationId: message.conversationId,
model: convo.model,
isCreatedByUser: dbMessage?.isCreatedByUser,
endpoint: dbMessage?.endpoint,
iconURL: dbMessage?.iconURL,
});
}
response = { messages: activeMessages, nextCursor: null };
} else {
response = { messages: [], nextCursor: null };
}
res.status(200).json(response);
} catch (error) {
logger.error('Error fetching messages:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
/**
* Creates a new branch message from a specific agent's content within a parallel response message.
* Filters the original message's content to only include parts attributed to the specified agentId.
* Only available for non-user messages with content attributions.
*
* @route POST /branch
* @param {string} req.body.messageId - The ID of the source message
* @param {string} req.body.agentId - The agentId to filter content by
* @returns {TMessage} The newly created branch message
*/
/**
* Projects a saved row for a client response. The context meta stays in the
* database for the next turn; no client-facing read or write response carries it.
* @param {TMessage} message
* @returns {TMessage}
*/
function toClientMessage(message) {
return stripPrivateMessageFields(message);
}
router.post('/branch', configMiddleware, async (req, res) => {
try {
const { messageId, agentId } = req.body;
const userId = req.user.id;
if (!messageId || !agentId) {
return res.status(400).json({ error: 'messageId and agentId are required' });
}
const sourceMessage = await db.getMessage({ user: userId, messageId });
if (!sourceMessage) {
return res.status(404).json({ error: 'Source message not found' });
}
if (await rejectSubagentThreadWrite(req, res, sourceMessage.conversationId)) {
return;
}
if (sourceMessage.isCreatedByUser) {
return res.status(400).json({ error: 'Cannot branch from user messages' });
}
if (!Array.isArray(sourceMessage.content)) {
return res.status(400).json({ error: 'Message does not have content' });
}
const hasAgentMetadata = sourceMessage.content.some((part) => part?.agentId);
if (!hasAgentMetadata) {
return res
.status(400)
.json({ error: 'Message does not have parallel content with attributions' });
}
/** @type {Array<import('librechat-data-provider').TMessageContentParts>} */
const filteredContent = [];
const sourceUserSubmittedPaths = Array.isArray(sourceMessage.userSubmittedPaths)
? sourceMessage.userSubmittedPaths.filter((path) => typeof path === 'string')
: [];
const sourceUserSubmittedMessageFieldPaths = Array.isArray(
sourceMessage.userSubmittedMessageFieldPaths,
)
? sourceMessage.userSubmittedMessageFieldPaths.filter(
(entry) => entry != null && typeof entry.path === 'string',
)
: [];
const remappedUserSubmittedPaths = sourceUserSubmittedPaths.filter((path) =>
path.startsWith('/attachments/'),
);
const remappedUserSubmittedMessageFieldPaths = [];
for (let sourceIndex = 0; sourceIndex < sourceMessage.content.length; sourceIndex++) {
const part = sourceMessage.content[sourceIndex];
if (part?.agentId === agentId) {
const targetIndex = filteredContent.length;
const { agentId: _a, groupId: _g, ...cleanPart } = part;
filteredContent.push(cleanPart);
const sourcePrefix = `/content/${sourceIndex}`;
const targetPrefix = `/content/${targetIndex}`;
for (const path of sourceUserSubmittedPaths) {
if (path === sourcePrefix || path.startsWith(`${sourcePrefix}/`)) {
remappedUserSubmittedPaths.push(`${targetPrefix}${path.slice(sourcePrefix.length)}`);
}
}
for (const entry of sourceUserSubmittedMessageFieldPaths) {
if (entry.path === sourcePrefix || entry.path.startsWith(`${sourcePrefix}/`)) {
remappedUserSubmittedMessageFieldPaths.push({
...entry,
path: `${targetPrefix}${entry.path.slice(sourcePrefix.length)}`,
});
}
}
}
}
if (filteredContent.length === 0) {
return res.status(400).json({ error: 'No content found for the specified agentId' });
}
const newMessageId = uuidv4();
/** @type {import('librechat-data-provider').TMessage} */
const newMessage = {
messageId: newMessageId,
conversationId: sourceMessage.conversationId,
parentMessageId: sourceMessage.parentMessageId,
attachments: sourceMessage.attachments,
isCreatedByUser: false,
model: sourceMessage.model,
endpoint: sourceMessage.endpoint,
sender: sourceMessage.sender,
iconURL: sourceMessage.iconURL,
// Server-private context meta (calibration and fading tier) travels with the branch so
// the next turn seeds its pruner the same way it would from the source response.
...(sourceMessage.contextMeta != null && { contextMeta: sourceMessage.contextMeta }),
...(typeof sourceMessage.isUserSubmitted === 'boolean' && {
isUserSubmitted: sourceMessage.isUserSubmitted,
}),
...(remappedUserSubmittedPaths.length > 0 && {
userSubmittedPaths: mergeUserSubmittedPaths(remappedUserSubmittedPaths),
}),
...(remappedUserSubmittedMessageFieldPaths.length > 0 && {
userSubmittedMessageFieldPaths: mergeUserSubmittedMessageFieldPaths(
remappedUserSubmittedMessageFieldPaths,
),
}),
content: filteredContent,
unfinished: false,
error: false,
user: userId,
};
await assertStoredMessageBranchAllowed(
{
filters: req.config?.filters,
legacyPii: req.config?.messageFilter?.pii,
message: newMessage,
user: req.user,
},
{ getFiles: db.getFiles, onTraversalFailure: reportLocatorTraversalFailure },
);
const savedMessage = await db.saveMessage(
{
userId: req?.user?.id,
isTemporary: sourceMessage.isTemporary,
expiredAt: sourceMessage.expiredAt,
interfaceConfig: req?.config?.interfaceConfig,
},
newMessage,
{ context: 'POST /api/messages/branch' },
);
if (!savedMessage) {
return res.status(500).json({ error: 'Failed to save branch message' });
}
await applyForcedRetention(retentionStore, {
ctx: {
userId,
isTemporary: sourceMessage.isTemporary,
expiredAt: savedMessage.expiredAt ?? sourceMessage.expiredAt,
interfaceConfig: req?.config?.interfaceConfig,
},
conversationId: sourceMessage.conversationId,
});
res.status(201).json(withMessageToolCallPreviews(req, toClientMessage(savedMessage)));
} catch (error) {
if (isContentFilterError(error)) {
return res.status(error.statusCode).json(error.body);
}
logger.error('Error creating branch message:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
router.post('/artifact/:messageId', configMiddleware, async (req, res) => {
try {
const { messageId } = req.params;
const { index, original, updated } = req.body;
if (typeof index !== 'number' || index < 0 || original == null || updated == null) {
return res.status(400).json({ error: 'Invalid request parameters' });
}
assertStoredMessageMutationAllowed(req.config?.filters, { original, updated });
const message = await db.getMessage({ user: req.user.id, messageId });
if (!message) {
return res.status(404).json({ error: 'Message not found' });
}
if (await rejectSubagentThreadWrite(req, res, message.conversationId)) {
return;
}
const artifacts = findAllArtifacts(message);
if (index >= artifacts.length) {
return res.status(400).json({ error: 'Artifact index out of bounds' });
}
// Unescape LaTeX preprocessing done by the frontend
// The frontend escapes $ signs for display, but the database has unescaped versions
const unescapedOriginal = unescapeLaTeX(original);
const unescapedUpdated = unescapeLaTeX(updated);
const targetArtifact = artifacts[index];
let updatedText = null;
if (targetArtifact.source === 'content') {
const part = message.content[targetArtifact.partIndex];
updatedText = replaceArtifactContent(
part.text,
targetArtifact,
unescapedOriginal,
unescapedUpdated,
);
if (updatedText) {
part.text = updatedText;
}
} else {
updatedText = replaceArtifactContent(
message.text,
targetArtifact,
unescapedOriginal,
unescapedUpdated,
);
if (updatedText) {
message.text = updatedText;
}
}
if (!updatedText) {
return res.status(400).json({ error: 'Original content not found in target artifact' });
}
const filteredArtifact =
targetArtifact.source === 'content'
? { content: [{ text: updatedText }] }
: { text: updatedText };
assertStoredMessageMutationAllowed(req.config?.filters, filteredArtifact);
const reqCtx = {
userId: req?.user?.id,
isTemporary: message.isTemporary,
expiredAt: message.expiredAt,
interfaceConfig: req?.config?.interfaceConfig,
};
const context = 'POST /api/messages/artifact/:messageId';
const savedMessage = await db.saveMessage(
reqCtx,
{
messageId,
conversationId: message.conversationId,
text: message.text,
content: message.content,
userSubmittedPaths: mergeUserSubmittedPaths(
message.userSubmittedPaths,
targetArtifact.source === 'content'
? `/content/${targetArtifact.partIndex}/text`
: '/text',
),
user: req.user.id,
},
{ context },
);
await applyForcedRetention(retentionStore, {
ctx: reqCtx,
conversationId: message.conversationId,
});
res.status(200).json(
withMessageToolCallPreviews(req, {
conversationId: savedMessage.conversationId,
content: savedMessage.content,
text: savedMessage.text,
}),
);
} catch (error) {
if (isContentFilterError(error)) {
return res.status(error.statusCode).json(error.body);
}
logger.error('Error editing artifact:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
router.get('/:conversationId', prepareMessageRequestValidation, async (req, res) => {
try {
const { conversationId } = req.params;
const validation = req.messageRequestValidation;
const previewToolCalls = prepareToolCallPreviews(req, toolCallPreviewDeps);
// This intentionally starts a user-scoped read before validation resolves;
// the response remains gated on validation success below.
const messagesPromise = validation.shouldFetchMessages
? db.getMessages({ conversationId, user: req.user.id }, CLIENT_MESSAGE_SELECT).then(
(messages) => ({ messages }),
(error) => ({ error }),
)
: null;
const validationResult = await validation.promise;
if (!validationResult.ok) {
return sendValidationResponse(res, validationResult);
}
const messagesResult = await messagesPromise;
if (messagesResult?.error) {
throw messagesResult.error;
}
const messages = messagesResult?.messages ?? [];
res.status(200).json(await previewToolCalls(messages));
} catch (error) {
logger.error('Error fetching messages:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
router.post('/:conversationId', storedMessageMutationMiddleware, async (req, res) => {
try {
if (await rejectSubagentThreadWrite(req, res, req.params.conversationId)) {
return;
}
/** Trace sampling fields are ownership claims only the server writes. */
const message = withoutTraceRefs({ ...req.body, conversationId: req.params.conversationId });
delete message.isUserSubmitted;
delete message.userSubmittedPaths;
delete message.userSubmittedMessageFieldPaths;
/** Server-private run state: a client-authored row must never seed a run's
* calibration or fading tiers, so the field only ever comes from the server. */
delete message.contextMeta;
const reqCtx = {
userId: req?.user?.id,
isTemporary: req.resolvedConversation?.isTemporary ?? req?.body?.isTemporary,
expiredAt: req.resolvedConversation?.expiredAt,
interfaceConfig: req?.config?.interfaceConfig,
};
const savedMessage = await db.saveMessage(
reqCtx,
{ ...message, user: req.user.id, isUserSubmitted: true },
{ context: 'POST /api/messages/:conversationId' },
);
if (!savedMessage) {
return res.status(400).json({ error: 'Message not saved' });
}
const conversationUpdate = {
conversationId: savedMessage.conversationId,
...(message.endpoint !== undefined && { endpoint: savedMessage.endpoint }),
...(message.model !== undefined && { model: savedMessage.model }),
...(message.iconURL !== undefined && { iconURL: savedMessage.iconURL }),
};
await db.saveConvo(reqCtx, conversationUpdate, {
context: 'POST /api/messages/:conversationId',
...(savedMessage._id != null ? { appendMessageIds: [savedMessage._id] } : {}),
});
res.status(201).json(toClientMessage(savedMessage));
} catch (error) {
logger.error('Error saving message:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
router.get('/:conversationId/:messageId', validateMessageReq, async (req, res) => {
try {
const { conversationId, messageId } = req.params;
const message = await db.getMessages(
{ conversationId, messageId, user: req.user.id },
CLIENT_MESSAGE_SELECT,
);
if (!message) {
return res.status(404).json({ error: 'Message not found' });
}
res.status(200).json(message);
} catch (error) {
logger.error('Error fetching message:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
router.get('/:conversationId/:messageId/parts/:partIndex', readToolCallPart);
router.put('/:conversationId/:messageId', messageMutationMiddleware, async (req, res) => {
try {
const { conversationId, messageId } = req.params;
const message = (
await db.getMessages(
{ messageId, user: req.user.id },
'conversationId content tokenCount quotes isCreatedByUser userSubmittedPaths',
)
)?.[0];
if (!message || message.conversationId !== conversationId) {
return res.status(404).json({ error: 'Message not found' });
}
if (await rejectSubagentThreadWrite(req, res, message.conversationId)) {
return;
}
const { text, index, model } = req.body;
if (index !== undefined && (typeof index !== 'number' || index < 0)) {
return res.status(400).json({ error: 'Invalid index' });
}
const reqCtx = { userId: req?.user?.id, interfaceConfig: req?.config?.interfaceConfig };
if (index === undefined) {
assertStoredMessageMutationAllowed(req.config?.filters, { text });
/** A user turn's persisted `quotes` are re-prepended into the prompt on
* every send, but this edit only changes `text`. Count the merged
* text+quotes so the stored `tokenCount` stays authoritative (matching the
* send path); a plain text-only count under-reports by the quote block. */
const textToCount = mergeQuotedTextForCount(
text,
message.quotes,
message.isCreatedByUser === true,
);
assertChatMutationAllowed(req.config?.filters, {
text,
quotes: message.isCreatedByUser === true ? message.quotes : undefined,
});
const tokenCount = await countTokens(textToCount, model);
const result = await db.updateMessage(req?.user?.id, {
messageId,
text,
tokenCount,
userSubmittedPaths: mergeUserSubmittedPaths(message.userSubmittedPaths, '/text'),
});
await applyForcedRetention(retentionStore, {
ctx: reqCtx,
conversationId,
messageId,
});
return res.status(200).json(result);
}
const existingContent = message.content;
if (!Array.isArray(existingContent) || index >= existingContent.length) {
return res.status(400).json({ error: 'Invalid index' });
}
const updatedContent = [...existingContent];
if (!updatedContent[index]) {
return res.status(400).json({ error: 'Content part not found' });
}
const currentPartType = updatedContent[index].type;
if (currentPartType !== ContentTypes.TEXT && currentPartType !== ContentTypes.THINK) {
return res.status(400).json({ error: 'Cannot update non-text content' });
}
assertStoredMessageMutationAllowed(req.config?.filters, {
content: [{ [currentPartType]: text }],
});
/** A text part is `string | { value, annotations }`. The Assistants thread sync
* persists the structured form with its file citations intact, and the editor
* reads it through the same union, so an edit has to be written into `value`
* rather than over the whole part. The same object is what gets counted below,
* and the tokenizer measures `length`, which an object does not have. */
const currentPart = updatedContent[index];
const currentValue = currentPart[currentPartType];
const isStructuredValue = currentValue != null && typeof currentValue === 'object';
const oldText = isStructuredValue ? (currentValue.value ?? '') : currentValue;
const editedPart = {
...currentPart,
[currentPartType]: isStructuredValue ? { ...currentValue, value: text } : text,
};
updatedContent[index] =
currentPartType === ContentTypes.THINK ? stripReasoningLabelMetadata(editedPart) : editedPart;
assertStoredMessageMutationAllowed(req.config?.filters, {
content: [updatedContent[index]],
});
let tokenCount = message.tokenCount;
if (tokenCount !== undefined) {
const oldTokenCount = await countTokens(oldText, model);
const newTokenCount = await countTokens(text, model);
tokenCount = Math.max(0, tokenCount - oldTokenCount) + newTokenCount;
}
const result = await db.updateMessage(req?.user?.id, {
messageId,
content: updatedContent,
tokenCount,
userSubmittedPaths: mergeUserSubmittedPaths(
message.userSubmittedPaths,
`/content/${index}/${currentPartType}`,
),
});
await applyForcedRetention(retentionStore, {
ctx: reqCtx,
conversationId,
messageId,
});
return res.status(200).json(result);
} catch (error) {
if (isContentFilterError(error)) {
return res.status(error.statusCode).json(error.body);
}
logger.error('Error updating message:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
router.put(
'/:conversationId/:messageId/feedback',
validateMessageReq,
configMiddleware,
requireFeedbackEnabled,
filterFeedbackContent,
async (req, res) => {
try {
const { conversationId, messageId } = req.params;
const { feedback } = req.body;
const feedbackResult = feedback == null ? null : feedbackSchema.safeParse(feedback);
if (feedbackResult && !feedbackResult.success) {
return res.status(400).json({ error: 'Invalid feedback' });
}
const updatedMessage = await db.updateMessage(
req?.user?.id,
{
messageId,
feedback: feedbackResult?.data ?? null,
},
{ context: 'updateFeedback' },
);
// Best-effort: Assistants messages do not have deterministic AgentRun traces.
if (!isAssistantsEndpoint(updatedMessage.endpoint)) {
sendFeedbackScore({
traceId: traceIdForMessage(updatedMessage.langfuseRunId ?? messageId),
sampled: updatedMessage.langfuseSampled,
destinationIds: updatedMessage.langfuseDestinationIds,
feedback: updatedMessage.feedback,
appConfig: req.config,
metadata: {
messageId: updatedMessage.messageId ?? messageId,
parentMessageId: updatedMessage.parentMessageId,
conversationId: updatedMessage.conversationId ?? conversationId,
sessionId: updatedMessage.conversationId ?? conversationId,
userId: req?.user?.id,
tenantId: req?.user?.tenantId,
endpoint: updatedMessage.endpoint,
sender: updatedMessage.sender,
isCreatedByUser: updatedMessage.isCreatedByUser,
tokenCount: updatedMessage.tokenCount,
},
}).catch((err) => logger.error('[langfuse] feedback score failed:', err));
}
await applyForcedRetention(retentionStore, {
ctx: { userId: req?.user?.id, interfaceConfig: req?.config?.interfaceConfig },
conversationId: updatedMessage.conversationId,
messageId,
});
res.json({
messageId,
conversationId,
feedback: updatedMessage.feedback,
});
} catch (error) {
logger.error('Error updating message feedback:', error);
res.status(500).json({ error: 'Failed to update feedback' });
}
},
);
router.delete('/:conversationId/:messageId', validateMessageReq, async (req, res) => {
try {
const { conversationId, messageId } = req.params;
if (await rejectSubagentThreadWrite(req, res, conversationId)) {
return;
}
await db.deleteMessages({ messageId, conversationId, user: req.user.id });
res.status(204).send();
} catch (error) {
logger.error('Error deleting message:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
module.exports = router;