mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-17 13:33:45 +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>
140 lines
4.9 KiB
TypeScript
140 lines
4.9 KiB
TypeScript
import path from 'path';
|
|
import axios from 'axios';
|
|
import { logger } from '@librechat/data-schemas';
|
|
import { ErrorTypes } from 'librechat-data-provider';
|
|
import { readFileAsString } from './files';
|
|
|
|
export interface GoogleServiceKey {
|
|
type?: string;
|
|
project_id?: string;
|
|
private_key_id?: string;
|
|
private_key?: string;
|
|
client_email?: string;
|
|
client_id?: string;
|
|
auth_uri?: string;
|
|
token_uri?: string;
|
|
auth_provider_x509_cert_url?: string;
|
|
client_x509_cert_url?: string;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
/**
|
|
* Load Google service key from file path, URL, or stringified JSON
|
|
* @param keyPath - The path to the service key file, URL to fetch it from, or stringified JSON
|
|
* @returns The parsed service key object or null if failed
|
|
*/
|
|
export async function loadServiceKey(keyPath: string): Promise<GoogleServiceKey | null> {
|
|
if (!keyPath) {
|
|
return null;
|
|
}
|
|
|
|
let serviceKey: unknown;
|
|
|
|
// Check if it's base64 encoded (common pattern for storing in env vars)
|
|
if (keyPath.trim().match(/^[A-Za-z0-9+/]+=*$/)) {
|
|
try {
|
|
const decoded = Buffer.from(keyPath.trim(), 'base64').toString('utf-8');
|
|
// Try to parse the decoded string as JSON
|
|
serviceKey = JSON.parse(decoded);
|
|
} catch {
|
|
// Not base64 or not valid JSON after decoding, continue with other methods
|
|
// Silent failure - not critical
|
|
}
|
|
}
|
|
|
|
// Check if it's a stringified JSON (starts with '{')
|
|
if (!serviceKey && keyPath.trim().startsWith('{')) {
|
|
try {
|
|
serviceKey = JSON.parse(keyPath);
|
|
} catch (error) {
|
|
logger.error('Failed to parse service key from stringified JSON', error);
|
|
return null;
|
|
}
|
|
}
|
|
// Check if it's a URL
|
|
else if (!serviceKey && /^https?:\/\//.test(keyPath)) {
|
|
try {
|
|
const response = await axios.get(keyPath);
|
|
serviceKey = response.data;
|
|
} catch (error) {
|
|
logger.error(`Failed to fetch the service key from URL: ${keyPath}`, error);
|
|
return null;
|
|
}
|
|
} else if (!serviceKey) {
|
|
// It's a file path
|
|
try {
|
|
const absolutePath = path.isAbsolute(keyPath) ? keyPath : path.resolve(keyPath);
|
|
const { content: fileContent } = await readFileAsString(absolutePath);
|
|
serviceKey = JSON.parse(fileContent);
|
|
} catch (error) {
|
|
logger.error(`Failed to load service key from file: ${keyPath}`, error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// If the response is a string (e.g., from a URL that returns JSON as text), parse it
|
|
if (typeof serviceKey === 'string') {
|
|
try {
|
|
serviceKey = JSON.parse(serviceKey);
|
|
} catch (parseError) {
|
|
logger.error(`Failed to parse service key JSON from ${keyPath}`, parseError);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Validate the service key has required fields
|
|
if (!serviceKey || typeof serviceKey !== 'object') {
|
|
logger.error(`Invalid service key format from ${keyPath}`);
|
|
return null;
|
|
}
|
|
|
|
// Fix private key formatting if needed
|
|
const key = serviceKey as GoogleServiceKey;
|
|
if (key.private_key && typeof key.private_key === 'string') {
|
|
// Replace escaped newlines with actual newlines
|
|
// When JSON.parse processes "\\n", it becomes "\n" (single backslash + n)
|
|
// When JSON.parse processes "\n", it becomes an actual newline character
|
|
key.private_key = key.private_key.replace(/\\n/g, '\n');
|
|
|
|
// Also handle the String.raw`\n` case mentioned in Stack Overflow
|
|
key.private_key = key.private_key.split(String.raw`\n`).join('\n');
|
|
|
|
// Ensure proper PEM format
|
|
if (!key.private_key.includes('\n')) {
|
|
// If no newlines are present, try to format it properly
|
|
const privateKeyMatch = key.private_key.match(
|
|
/^(-----BEGIN [A-Z ]+-----)(.*)(-----END [A-Z ]+-----)$/,
|
|
);
|
|
if (privateKeyMatch) {
|
|
const [, header, body, footer] = privateKeyMatch;
|
|
// Add newlines after header and before footer
|
|
key.private_key = `${header}\n${body}\n${footer}`;
|
|
}
|
|
}
|
|
}
|
|
|
|
return key;
|
|
}
|
|
|
|
/**
|
|
* Checks if a user key has expired based on the provided expiration date and endpoint.
|
|
* If the key has expired, it throws an Error with details including the type of error,
|
|
* the expiration date, and the endpoint.
|
|
*
|
|
* @param expiresAt - The expiration date of the user key in a format that can be parsed by the Date constructor
|
|
* @param endpoint - The endpoint associated with the user key to be checked
|
|
* @throws Error if the user key has expired. The error message is a stringified JSON object
|
|
* containing the type of error (`ErrorTypes.EXPIRED_USER_KEY`), the expiration date as an ISO 8601
|
|
* timestamp the client formats in the reader's locale, and the endpoint.
|
|
*/
|
|
export function checkUserKeyExpiry(expiresAt: string, endpoint: string): void {
|
|
const expiresAtDate = new Date(expiresAt);
|
|
if (expiresAtDate < new Date()) {
|
|
const errorMessage = JSON.stringify({
|
|
type: ErrorTypes.EXPIRED_USER_KEY,
|
|
expiredAt: expiresAtDate.toISOString(),
|
|
endpoint,
|
|
});
|
|
throw new Error(errorMessage);
|
|
}
|
|
}
|