2.10.0
中文
Skip to content

XRequest ​

XRequest(baseURL, options) returns an instance with run, abort and manual. baseURL must be a non-empty string.

When handed to the Provider, manual must be true. Callbacks are written in options.callbacks: onUpdate, onSuccess, onError. run(params) merges these parameters with options.params and sends them out.

Responses are routed by content-type: text/event-stream goes to SSE, application/json goes to JSON. Other types throw unsupported content-type. You can also pass timeout, streamTimeout, middlewares, transformStream, custom separators, failure retries, and a custom fetch. SSE retries include the latest event id in Last-Event-ID.

Basic usage ​

Request Log
Chunks0
Last
-
Basic usage

The button triggers a request. fetch is replaced with local SSE, and the page does not access the external network.

Custom input ​

Request Log
-
Custom input

Attach custom fields such as agentId in params, still using the local mock.

Custom transformer ​

Custom transformer

transformStream + NDJSON mock sends the original chunks directly into onUpdate.

Model integration ​

Model Request Log
idle
-
Model integration

Manual mode with an input box. A mock OpenAI SSE stream can be aborted.

Timeout ​

Request Log
request -
Status-
timeout2000ms (mock arrives at 3000ms)
Timeout

timeout: 2000, the mock returns in 3000ms, triggering TimeoutError.

API ​

XRequestFunction ​

ts
type XRequestFunction<Input = AnyObject, Output = SSEOutput> = (
  baseURL: string,
  options?: XRequestOptions<Input, Output>,
) => XRequestClass<Input, Output>;
PropDescriptionTypeDefaultVersion
baseURLAPI addressstring--
optionsRequest configurationXRequestOptions<Input, Output>--

XRequestOptions ​

PropDescriptionTypeDefaultVersion
callbacksRequest callbacksXRequestCallbacks<Output>--
paramsRequest parametersInput--
headersAdditional request headersRecord<string, string>--
timeoutRequest timeout (milliseconds)number--
streamTimeoutStream data timeout (milliseconds)number--
fetchCustom fetch(baseURL, options) => Promise<Response>--
middlewaresPre- and post-request middlewareXFetchMiddlewares<Input, Output>--
transformStreamStream processorXStreamOptions<Output>['transformStream']((baseURL: string, responseHeaders: Headers) => XStreamOptions<Output>['transformStream'])-
streamSeparatorStream event separator; ignored when transformStream is setstring\n\n2.2.0
partSeparatorField separator inside one event; ignored when transformStream is setstring\n2.2.0
kvSeparatorSeparator between field names and values; ignored when transformStream is setstring:2.2.0
manualWhether to send the request manually; if true, run must be calledbooleanfalse-
retryIntervalRetry interval after a failure, in milliseconds; disabled when omittednumber--
retryTimesMaximum retry countnumber--

XRequestCallbacks ​

PropDescriptionTypeDefaultVersion
onSuccessSuccess callback; receives the assembled message when used with a Chat Provider(chunks: Output[], responseHeaders: Headers, message?: MessageInfo<ChatMessage>) => void--
onErrorError callback; return a number to override the retry interval for this failure. With a Chat Provider it also receives the fallback message(error: Error, errorInfo?: any, responseHeaders?: Headers, fallbackMsg?: MessageInfo<ChatMessage>) => number | void--
onUpdateChunk update callback; receives the assembled message when used with a Chat Provider(chunk: Output, responseHeaders: Headers, message?: MessageInfo<ChatMessage>) => void--

XRequestClass ​

PropDescriptionTypeDefaultVersion
abortCancel request() => void--
runManually execute request (effective when manual=true)(params?: Input) => void--
isRequestingWhether the request is in progressboolean--
manualWhether it is manual modeboolean--

setXRequestGlobalOptions ​

ts
type setXRequestGlobalOptions<Input, Output> = (
  options: XRequestGlobalOptions<Input, Output>,
) => void;

XRequestGlobalOptions ​

ts
type XRequestGlobalOptions<Input, Output> = Pick<
  XRequestOptions<Input, Output>,
  'headers' | 'timeout' | 'streamTimeout' | 'middlewares' | 'fetch' | 'transformStream' | 'manual'
>;

XFetchMiddlewares ​

ts
interface XFetchMiddlewares<Input, Output> {
  onRequest?: (
    ...args: [Parameters<typeof fetch>[0], XRequestOptions<Input, Output>]
  ) => Promise<[Parameters<typeof fetch>[0], XRequestOptions<Input, Output>]>;
  onResponse?: (response: Response) => Promise<Response>;
}

The new XRequest({ baseURL, model, dangerouslyApiKey }).create in 1.x is now called XRequestLegacy, documentation at XRequest Request.