ant-design-x-vue-next
Version:
Ant Design X for Vue — community continuation aligned with @ant-design/x
160 lines (159 loc) • 5.74 kB
TypeScript
import type { SSEOutput, XStreamOptions } from '../x-stream';
import type { XFetchMiddlewares, XFetchOptions } from './x-fetch';
import type { AnyObject } from '../_util/type';
import { MaybeRefOrGetter } from 'vue';
export interface XRequestBaseOptions {
/**
* @description Base URL, e.g., 'https://api.example.com/v1/chat'
*/
baseURL: string;
/**
* @description Model name, e.g., 'gpt-3.5-turbo'
*/
model?: string;
/**
* @warning 🔥🔥 Its dangerously!
*
* Enabling the dangerouslyApiKey option can be dangerous because it exposes
* your secret API credentials in the client-side code. Web browsers are inherently
* less secure than server environments, any user with access to the browser can
* potentially inspect, extract, and misuse these credentials. This could lead to
* unauthorized access using your credentials and potentially compromise sensitive
* data or functionality.
*/
dangerouslyApiKey?: string;
}
interface XRequestCustomOptions {
/**
* @description Extra headers merged into every request
*/
headers?: Record<string, string>;
/**
* @description Custom fetch
*/
fetch?: XFetchOptions['fetch'];
/**
* @description Middleware for request and response
*/
middlewares?: XFetchMiddlewares;
/**
* @description Request timeout in ms (abort when exceeded before first byte)
*/
timeout?: number;
/**
* @description Idle timeout between stream chunks in ms
*/
streamTimeout?: number;
/**
* @description Delay before retry after failure (ms). Set to enable retry.
*/
retryInterval?: number;
/**
* @description Max retry attempts when retryInterval is set
*/
retryTimes?: number;
/**
* @description Separator for stream data parsing
*/
streamSeparator?: string;
/**
* @description Separator for different parts within the stream
*/
partSeparator?: string;
/**
* @description Separator for key-value pairs in the stream data
*/
kvSeparator?: string;
}
export type XRequestOptions = XRequestBaseOptions & XRequestCustomOptions;
/** Global defaults applied to every XRequest instance (aligned with @ant-design/x-sdk). */
export type XRequestGlobalOptions = Pick<XRequestOptions, 'headers' | 'timeout' | 'streamTimeout' | 'middlewares' | 'fetch' | 'retryInterval' | 'retryTimes' | 'streamSeparator' | 'partSeparator' | 'kvSeparator'>;
/**
* Set app-wide XRequest defaults (headers / timeout / middlewares / fetch…).
* Instance options override global ones.
*/
export declare function setXRequestGlobalOptions(options: XRequestGlobalOptions): void;
/** Test / debug helper */
export declare function getXRequestGlobalOptions(): Readonly<XRequestGlobalOptions>;
/** Reset globals (tests) */
export declare function resetXRequestGlobalOptions(): void;
type XRequestMessageContent = string | AnyObject;
interface XRequestMessage extends AnyObject {
role?: string;
content?: XRequestMessageContent;
}
/**
* Compatible with the parameters of OpenAI's chat.completions.create,
* with plans to support more parameters and adapters in the future
*/
export interface XRequestParams {
/**
* @description Model name, e.g., 'gpt-3.5-turbo'
* @default XRequestOptions.model
*/
model?: string;
/**
* @description Indicates whether to use streaming for the response
*/
stream?: boolean;
/**
* @description The messages to be sent to the model
*/
messages?: XRequestMessage[];
}
export interface XRequestCallbacks<Output> {
/**
* @description Callback when the request is successful
*/
onSuccess: (chunks: Output[]) => void;
/**
* @description Callback when the request fails.
* Return a number to override retryInterval for this error.
*/
onError: (error: Error) => void | number;
/**
* @description Callback when the request is updated
*/
onUpdate: (chunk: Output) => void;
/**
* @description Callback monitoring and control the stream
*/
onStream?: (abortController: AbortController) => void;
}
export type XRequestFunction<Input = AnyObject, Output = SSEOutput> = (params: XRequestParams & Input, callbacks: XRequestCallbacks<Output>, transformStream?: XStreamOptions<Output>['transformStream']) => Promise<void>;
declare class XRequestClass {
readonly baseURL: string;
readonly model: string;
private defaultHeaders;
private customOptions;
private abortController?;
private timeoutHandler?;
private streamTimeoutHandler?;
private retryTimer?;
private _isTimeout;
private _isStreamTimeout;
private _isRequesting;
private retryTimes;
private lastEventId?;
private lastParams?;
private lastCallbacks?;
private lastTransformStream?;
private constructor();
static init(options: XRequestOptions): XRequestClass;
get isTimeout(): boolean;
get isStreamTimeout(): boolean;
get isRequesting(): boolean;
/** Abort in-flight request and clear timers */
abort: () => void;
create: <Input = AnyObject, Output = SSEOutput>(params: XRequestParams & Input, callbacks?: XRequestCallbacks<Output>, transformStream?: XStreamOptions<Output>["transformStream"]) => Promise<void>;
private resetRetryState;
private clearTimers;
private executeCreate;
private handleError;
private processStream;
private customResponseHandler;
private sseResponseHandler;
private jsonResponseHandler;
}
declare const XRequest: (options: MaybeRefOrGetter<XRequestOptions>) => import("vue").ComputedRef<XRequestClass>;
export default XRequest;