LibreChat/packages/api/src/utils/axios.ts
Marco Beretta 8718338728
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
⏱️ fix: Bound Outbound Calls Made Inside a Request (#17263)
* fix: Bound Outbound Calls Made Inside a Request

RAG queries for file search and message context, OCR provider calls,
action HTTP calls, the moderation check, the stored-image fetch for
base64 providers, and Flux result polling had no timeout. A stalled
upstream hung the chat turn, the upload or the tool call indefinitely,
and Flux kept polling forever on statuses that never become Ready.

Each now runs under a budget from a new librechat.yaml timeouts block
with finite defaults (getOutboundTimeoutMs in @librechat/api). Action
calls also receive the run's abort signal, so stopping a run closes the
socket.

* test: Cover a Stalled RAG Query in the Mock Harness

* fix: Share One Flux Budget Across Submit and Polls, Bound the Vertex Token Exchange

* refactor: Poll Flux Results Through pollUntilSettled

* refactor: Leave Flux Polling for a Separate Migration

* fix: Cap Configured Timeouts at the Node Timer Limit

* fix: Bound the Action Token Refresh and the Whole Image Download

* fix: Cap Every Outbound Budget by Wall-Clock Time

Axios's timeout only fires on an idle socket, so a RAG, OCR, moderation,
action or token response that keeps trickling bytes never ended.
outboundRequestBudget pairs each timeout with an AbortSignal deadline, and
action calls combine it with the run's own signal.

* fix: Name the Budget That Ran Out and Give Actions Five Minutes

The actions default rises to five minutes, since action endpoints are
arbitrary third-party APIs and the old minute could cut off legitimately
slow ones after an upgrade.

A budget overrun now fails with an error naming the timeouts key and its
value, whether it was the idle timeout or the wall-clock deadline, and
logAxiosError logs it as a warning so operators know what to raise. A
deadline cancellation is no longer read as a user stop, so an action that
runs out of time reports a timeout instead of looking like a stopped run.

* fix: Hold Outbound Deadlines Weakly So Finished Requests Leave No Timers

* fix: Check the Budget Before Reading an Error Message

* fix: Report a Wrapped Deadline as a Failure, Not a Stop

* fix: Bound the OAuth Code Exchange and Keep the OCR Budget Hint When Protected

* chore: Sort Imports in axios.ts After Rebase
2026-10-21 19:10:13 +02:00

146 lines
5 KiB
TypeScript

import axios from 'axios';
import { Buffer } from 'buffer';
import { logger } from '@librechat/data-schemas';
import type { AxiosInstance, AxiosProxyConfig, AxiosError } from 'axios';
import { getOutboundTimeout } from './timeouts';
import { isClientDisconnect } from './streams';
/**
* Render `error.response.data` as a small, log-safe value. Axios surfaces
* the response body in whichever native shape the request asked for, so
* `responseType: 'arraybuffer'` yields a `Buffer` (raw bytes — JSON-
* serializes as `{type: 'Buffer', data: [123, 34, ...]}`, ~4 chars per
* byte) and `responseType: 'stream'` yields a `Readable` (whose internal
* state JSON-serializes the full readableState ring buffer + socket
* fields, easily megabytes per error). Both are useless for diagnostics
* and drown the log line. Decode small buffers as UTF-8 (truncated) and
* stub streams entirely.
*/
const renderResponseData = (data: unknown): unknown => {
if (data == null) return data;
if (Buffer.isBuffer(data)) {
const MAX = 2048;
const text = data.subarray(0, MAX).toString('utf8');
return data.length > MAX ? `${text}…[+${data.length - MAX} bytes]` : text;
}
// Readable streams (responseType: 'stream') and other piped sources.
if (
typeof data === 'object' &&
data !== null &&
typeof (data as { pipe?: unknown }).pipe === 'function'
) {
return '[stream]';
}
return data;
};
/**
* Logs Axios errors based on the error object and a custom message.
* @param options - The options object.
* @param options.message - The custom message to be logged.
* @param options.error - The Axios error object.
* @returns The log message.
*/
export const logAxiosError = ({
message,
error,
}: {
message: string;
error: AxiosError | Error | unknown;
}): string => {
if (isClientDisconnect(error)) {
const logMessage = `${message} Client disconnected`;
logger.debug(logMessage);
return logMessage;
}
let logMessage = message;
try {
const timeout = getOutboundTimeout(error);
if (timeout) {
logMessage = `${message} ${timeout.message}`;
logger.warn(logMessage);
return logMessage;
}
const stack =
error != null
? (error as Error | AxiosError)?.stack || 'No stack trace available'
: 'No stack trace available';
const errorMessage =
error != null
? (error as Error | AxiosError)?.message || 'No error message available'
: 'No error message available';
if (axios.isAxiosError(error) && error.response && error.response?.status) {
const { status, headers, data } = error.response;
logMessage = `${message} The server responded with status ${status}: ${error.message}`;
logger.error(logMessage, {
status,
headers,
data: renderResponseData(data),
stack,
});
} else if (axios.isAxiosError(error) && error.request) {
const { method, url } = error.config || {};
logMessage = `${message} No response received for ${method ? method.toUpperCase() : ''} ${url || ''}: ${error.message}`;
logger.error(logMessage, {
requestInfo: { method, url },
stack,
});
} else if (errorMessage?.includes("Cannot read properties of undefined (reading 'status')")) {
logMessage = `${message} It appears the request timed out or was unsuccessful: ${errorMessage}`;
logger.error(logMessage, { stack });
} else {
logMessage = `${message} An error occurred while setting up the request: ${errorMessage}`;
logger.error(logMessage, { stack });
}
} catch (err: unknown) {
logMessage = `Error in logAxiosError: ${(err as Error).message}`;
logger.error(logMessage, { stack: (err as Error).stack || 'No stack trace available' });
}
return logMessage;
};
/** Adds upload context without discarding the Axios response metadata used by
* Code API recovery. Transport-specific branching remains in this typed module. */
export function wrapCodeApiUploadError(error: unknown, message: string): Error {
const wrapped = new Error(logAxiosError({ message, error }), { cause: error });
if (axios.isAxiosError(error)) {
Object.assign(wrapped, {
isAxiosError: true,
response: error.response,
});
}
return wrapped;
}
/**
* Creates and configures an Axios instance with optional proxy settings.
* @returns A configured Axios instance
* @throws If there's an issue creating the Axios instance or parsing the proxy URL
*/
export function createAxiosInstance(): AxiosInstance {
const instance = axios.create();
if (process.env.proxy) {
try {
const url = new URL(process.env.proxy);
const proxyConfig: Partial<AxiosProxyConfig> = {
host: url.hostname.replace(/^\[|\]$/g, ''),
protocol: url.protocol.replace(':', ''),
};
if (url.port) {
proxyConfig.port = parseInt(url.port, 10);
}
instance.defaults.proxy = proxyConfig as AxiosProxyConfig;
} catch (error) {
console.error('Error parsing proxy URL:', error);
throw new Error(`Invalid proxy URL: ${process.env.proxy}`);
}
}
return instance;
}