LibreChat/api/server/middleware
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
..
__tests__ 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
accessResources ♻️ refactor: Native Prompts Behind a Shared Service and Adapter (#16506) 2026-10-02 08:40:39 -04:00
assistants
config 📧 feat: add email address change (#14597) 2026-10-01 03:46:16 +00:00
limiters 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
roles
spec
validate
abortMiddleware.js 💬 refactor: ChatForm UI redesign with unified palette (#14473) 2026-10-01 03:46:18 +00:00
abortMiddleware.spec.js 🔔 feat: Unseen Reply Indicators, Reply Notifications, and Mark as Unread (#15025) 2026-10-01 03:46:16 +00:00
abortRun.js
blockRetiredSetupToken.js 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
buildEndpointOption.js 💬 refactor: ChatForm UI redesign with unified palette (#14473) 2026-10-01 03:46:18 +00:00
buildEndpointOption.spec.js 💬 refactor: ChatForm UI redesign with unified palette (#14473) 2026-10-01 03:46:18 +00:00
canAccessSharedLink.js
canDeleteAccount.js
canDeleteAccount.spec.js
checkBan.js 🛶 fix: Keep Trigger Bans Off Shared Loopback IPs (#16535) 2026-09-29 20:50:23 -04:00
checkBan.spec.js 🛶 fix: Keep Trigger Bans Off Shared Loopback IPs (#16535) 2026-09-29 20:50:23 -04:00
checkDomainAllowed.js
checkInviteUser.js
checkInviteUser.spec.js
checkPeoplePickerAccess.js
checkSharePublicAccess.js
checkSharePublicAccess.spec.js
denyRequest.js 📔 feat: Preserve Owner Text During PII Redaction (#16311) 2026-10-02 11:43:10 -04:00
denyRequest.privateText.spec.js 📔 feat: Preserve Owner Text During PII Redaction (#16311) 2026-10-02 11:43:10 -04:00
error.js 🔔 feat: Unseen Reply Indicators, Reply Notifications, and Mark as Unread (#15025) 2026-10-01 03:46:16 +00:00
error.spec.js 🔔 feat: Unseen Reply Indicators, Reply Notifications, and Mark as Unread (#15025) 2026-10-01 03:46:16 +00:00
index.js 🪶 perf: Load Tool-Call Previews and Fetch Full Parts on Demand (#16753) 2026-10-05 07:42:45 -04:00
logHeaders.js
messageValidation.js
moderateText.js
moderateText.spec.js
noIndex.js
oauthNavigation.js
optionalJwtAuth.js 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
optionalJwtAuth.spec.js 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
optionalShareFileAuth.js 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
optionalShareFileAuth.spec.js 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
requireJwtAuth.js 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
requireLdapAuth.js
requireLocalAuth.js
requireSameOrigin.js 🧿 fix: Bind Login Flows to the Browser That Started Them (#15880) 2026-09-13 11:47:29 -04:00
setHeaders.js
setTwoFactorTempUser.js 🔐 feat: Enforce Required Two-Factor Authentication (#14731) 2026-10-01 23:39:51 +02:00
uaParser.js
validateEmailLogin.js
validateImageRequest.js
validateMessageReq.js
validateModel.js
validatePasswordReset.js
validateRegistration.js