mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 21:08:17 +00:00
* 🧯 refactor: State Failed Turns in Copy a Reader Can Act On Every client-facing error code now has authored copy, so no failed turn renders raw JSON, an unresolved `com_error_*` key, or a provider message cut at 512 characters. `client/src/components/Messages/Content/Error.tsx` becomes a module directory: a dispatcher resolving `json.code ?? json.type`, a registry splitting one-sentence copy from payload-aware renderers, and shared parts that resolve the provider, model, user-key ownership and compaction capability behind the failure. Message-limit violations persist `resetAt`/`retryAfterSeconds`, so the row counts down to the window's reset instead of naming a window length alone. * 📸 chore: pin before/after error copy screenshots for the pull request * 🧹 chore: drop the pinned pull request screenshots from the tree * 🔻 style: Trail the error disclosure chevron after its label * ⏱️ refactor: Derive Limiter Resets in packages/api, Stamp Key Expiry as ISO The message limiter's reset fallback and retry arithmetic move out of the CJS middleware into `getRateLimitReset` in packages/api, which both the message and agent-event handlers now call, so /api keeps only the wiring. `checkUserKeyExpiry` persists `expiredAt` with `toISOString()` instead of the server's `toLocaleString()`, which a client in another locale could parse with day and month swapped. * 🪪 fix: Attribute Failed Turns to the Row, Agent and Payload That Failed Error content parts render through `Part` without their message, so their copy named the conversation's current endpoint and model rather than the ones that failed. The chat, flat-thread and search row renderers now supply the row's identity through `ErrorSourceProvider`, and the dispatcher reads it whenever no row is passed directly. A saved agent's row carries the `agents` endpoint and the agent id as its model, so identity, key ownership and the key dialog resolve against the agent's own provider and model; an endpoint named by the payload (an expired key) outranks both. The agent builder is only suggested where the side panel would offer it: agents configured, builder not disabled, USE and CREATE held, and an EDIT grant or administrator role. Also in the renderers: - structured refusals read Anthropic's `stop_details.explanation` - provider bodies nesting their message under `error` keep that message - upstream model failures name the provider when it is known - `INVALID_USER_KEY` offers the key dialog on a user-provided endpoint - an oversized message is never told to compact the conversation - the countdown is bounded by the limiter's window, not a fixed day, and its ticking text is hidden from the assertive live region - an expiry persisted in a server locale is shown as written - search rows that failed render through the dispatcher, not as markdown - the payload type is an explicit JSON value type * 🧪 chore: Seed Real Refusal, Legacy Expiry and Nested Provider Error Shapes The gallery's refusal case used a string `info` no producer writes; it now carries Anthropic's stop metadata and a Bedrock content filter. It also seeds an expiry stamped in a server locale, an unreadable key on a user-provided endpoint, and a provider body nesting its message under `error`, and removes whatever it inserted when a write fails. * 🧵 fix: Keep the Reason From a Provider Body Embedded in Failed-Run Text A failed run persists a provider error as `<base message>: <SDK message>`, and Anthropic's SDK message embeds the response body, so the persisted text reads `…request: 400 {"type":"error","error":{"message":…}}`. The fallback renderer dropped that body and kept only the prefix, losing the reason. It now swaps the body for the message it carries. * 🔀 fix: Resolve Handoff Agents, URL-Owned Credentials and Nested Error Codes An error part after an `AGENT_UPDATE` belongs to the agent the run was handed to. `ErrorSourceProvider` now records where a row's handoffs sit, and an error part resolves the agent active at its own `partIndex`; search and shared rows give error parts that position too. A handoff the agents map cannot resolve names no provider instead of the agent that handed off. A user-provided base URL makes the reader's own record hold the key as well, so key failures on such an endpoint offer the key dialog. `NO_BASE_URL` and `INVALID_BASE_URL`, which only a user-provided URL produces, render through the same renderer with named copy and an add or update action. OpenAI-compatible bodies nest `code` and `type` under `error`; when the top level names neither, the dispatcher reads the envelope, so a nested `invalid_api_key` reaches the key renderer. * 🕰️ fix: Keep Unknown Key Ownership Generic, Measure Resets From the Row A shared link's viewer has no endpoint configuration, so whether the reader or the deployment owns a key is unknown; key copy there read that as deployment-owned and told the viewer to ask an administrator. Ownership is now undefined without a configuration, and the copy stays generic. The countdown's window bound used this device's clock, so a device running behind the server could drop a valid reset. The window now runs from the row's own timestamp, which the server stamped alongside `resetAt`. * 🗜️ fix: Suggest Compaction Only Where the Compact Action Is Offered Manual compaction is only reachable from the context usage popover, which `interface.contextUsage: false` removes. Context errors on such a deployment suggested compacting anyway; they now fall back to the standard next steps. * 🧭 fix: Keep Row Identity Authoritative and Every Generation Charge Visible A row's own endpoint and model now stand even where it lacks them, so a restored failure no longer borrows whatever the conversation selected since; only an error rendered with no row reads the conversation, and an agents row with no stored agent id (the live row of a turn that just failed) still resolves the conversation's agent. Also: - a token-balance row lists every generation instead of the first 20 - search rows re-render when their `error` flag changes - `AUTH_CROSS_ORIGIN`, added since this branch began, renders the login page's copy and has a gallery case, so the seed's completeness check runs; a table test now fails for any enum member without authored copy - upstream model failures keep the provider-neutral sentence the server persists as their text, which the e2e suite reads the row by * 🪞 fix: Resolve Ephemeral Agent Ids and Keep an Agents Row's Model Name An agents row whose model is a model name, as an ephemeral agent's is, lost both its provider and its model whenever no saved agent resolved. Agent ids now resolve the way sibling headers resolve them: a saved agent through the map without the parallel `____N` suffix, an ephemeral agent through the endpoint and model its id encodes. A row that still resolves nothing keeps the model name it recorded; an agent id is never shown. * 🛤️ fix: Attribute Lane Errors to Their Agent, Count Live Retries Relatively A parallel run stamps each part with the agent of its lane, and an error part in one lane was attributed to whichever handoff last preceded it. The row now records error parts' own agents alongside its handoff positions, and an error part's own agent comes first. A live countdown subtracted this device's clock from the server's `resetAt`, so a device running fast could report the limit already over. `retryAfterSeconds` is now counted from the row's own timestamp, which the device stamps on a live row and the server on a restored one. Search results read a cached startup config but have no compact control, so compaction guidance now also requires the chat surface. * 🧷 fix: Resolve an Agents Row From Its Own Model, Never the Current Agent An agents row that stored a model name, as an ephemeral agent's does, fell back to the conversation's current agent when no saved agent resolved, so switching the conversation to a saved agent later rewrote who produced the old failure. The fallback served no real row: the client's live placeholder for an agents turn stores the conversation's agent id as `model`, as persisted rows do. The row's `model` now decides on its own — a saved or encoded ephemeral id resolves, a model name is kept, and the `ephemeral` placeholder names nothing; only an error with no row reads the conversation. --------- Co-authored-by: Danny Avila <danny@librechat.ai>
119 lines
3.5 KiB
JavaScript
119 lines
3.5 KiB
JavaScript
const rateLimit = require('express-rate-limit');
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
|
const { limiterCache, removePorts, getRateLimitReset } = require('@librechat/api');
|
|
const denyRequest = require('~/server/middleware/denyRequest');
|
|
const { logViolation } = require('~/cache');
|
|
|
|
const {
|
|
MESSAGE_IP_MAX = 40,
|
|
MESSAGE_IP_WINDOW = 1,
|
|
MESSAGE_USER_MAX = 40,
|
|
MESSAGE_USER_WINDOW = 1,
|
|
MESSAGE_VIOLATION_SCORE: score,
|
|
} = process.env;
|
|
|
|
const ipWindowMs = MESSAGE_IP_WINDOW * 60 * 1000;
|
|
const ipMax = MESSAGE_IP_MAX;
|
|
const ipWindowInMinutes = ipWindowMs / 60000;
|
|
|
|
const userWindowMs = MESSAGE_USER_WINDOW * 60 * 1000;
|
|
const userMax = MESSAGE_USER_MAX;
|
|
const userWindowInMinutes = userWindowMs / 60000;
|
|
|
|
/**
|
|
* Creates either an IP/User message request rate limiter for excessive requests
|
|
* that properly logs and denies the violation.
|
|
*
|
|
* @param {boolean} [ip=true] - Whether to create an IP limiter or a user limiter.
|
|
* @returns {function} A rate limiter function.
|
|
*
|
|
*/
|
|
const createHandler = (ip = true) => {
|
|
return async (req, res) => {
|
|
const type = ViolationTypes.MESSAGE_LIMIT;
|
|
const errorMessage = {
|
|
type,
|
|
max: ip ? ipMax : userMax,
|
|
limiter: ip ? 'ip' : 'user',
|
|
windowInMinutes: ip ? ipWindowInMinutes : userWindowInMinutes,
|
|
...getRateLimitReset(req.rateLimit, ip ? ipWindowMs : userWindowMs),
|
|
};
|
|
|
|
await logViolation(req, res, type, errorMessage, score);
|
|
return await denyRequest(req, res, errorMessage);
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Message request rate limiters
|
|
*/
|
|
const ipLimiterOptions = {
|
|
windowMs: ipWindowMs,
|
|
max: ipMax,
|
|
handler: createHandler(),
|
|
keyGenerator: removePorts,
|
|
store: limiterCache('message_ip_limiter'),
|
|
};
|
|
|
|
const userLimiterOptions = {
|
|
windowMs: userWindowMs,
|
|
max: userMax,
|
|
handler: createHandler(false),
|
|
keyGenerator: function (req) {
|
|
return req.user?.id;
|
|
},
|
|
store: limiterCache('message_user_limiter'),
|
|
};
|
|
|
|
/**
|
|
* Message request rate limiter by IP
|
|
*/
|
|
const messageIpLimiter = rateLimit(ipLimiterOptions);
|
|
|
|
/**
|
|
* Message request rate limiter by userId
|
|
*/
|
|
const messageUserLimiter = rateLimit(userLimiterOptions);
|
|
|
|
/**
|
|
* Event admission has its own API-principal bucket. The durable worker later
|
|
* consumes the normal message-user bucket when it executes the delivery, so
|
|
* sharing that limiter here would charge every event twice.
|
|
*/
|
|
let configuredAgentEventUserLimiter;
|
|
const agentEventUserLimiter = (req, res, next) => {
|
|
if (configuredAgentEventUserLimiter == null) {
|
|
const max = Number(process.env.AGENT_EVENT_USER_MAX ?? 40);
|
|
const windowInMinutes = Number(process.env.AGENT_EVENT_USER_WINDOW ?? 1);
|
|
configuredAgentEventUserLimiter = rateLimit({
|
|
windowMs: windowInMinutes * 60 * 1000,
|
|
max,
|
|
handler: (limitedReq, limitedRes) => {
|
|
const { retryAfterSeconds } = getRateLimitReset(
|
|
limitedReq.rateLimit,
|
|
windowInMinutes * 60 * 1000,
|
|
);
|
|
limitedRes.set('Retry-After', String(retryAfterSeconds));
|
|
return limitedRes
|
|
.status(429)
|
|
.type('application/json')
|
|
.json({
|
|
error: {
|
|
code: 'agent_event_rate_limited',
|
|
message: 'Agent event admission rate limit exceeded.',
|
|
type: 'rate_limit_error',
|
|
},
|
|
});
|
|
},
|
|
keyGenerator: (limitedReq) => String(limitedReq.apiKeyId ?? limitedReq.user?.id),
|
|
store: limiterCache('agent_event_user_limiter'),
|
|
});
|
|
}
|
|
return configuredAgentEventUserLimiter(req, res, next);
|
|
};
|
|
|
|
module.exports = {
|
|
agentEventUserLimiter,
|
|
messageIpLimiter,
|
|
messageUserLimiter,
|
|
};
|