@blueking/chat-helper 0.0.1-beta.3 → 0.0.1-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -533,7 +533,7 @@ export declare const useAgent: (options: IUseChatHelperOptions, http: IHttpModul
|
|
|
533
533
|
};
|
|
534
534
|
}>;
|
|
535
535
|
isInfoLoading: import("vue").Ref<boolean, boolean>;
|
|
536
|
-
chat: () => void;
|
|
536
|
+
chat: (url: string) => void;
|
|
537
537
|
stopChat: () => Promise<void>;
|
|
538
538
|
getAgentInfo: () => Promise<void>;
|
|
539
539
|
};
|
|
@@ -67,7 +67,7 @@ export const useAgent = (options, http, message)=>{
|
|
|
67
67
|
isInfoLoading.value = false;
|
|
68
68
|
});
|
|
69
69
|
};
|
|
70
|
-
const chat = ()=>{
|
|
70
|
+
const chat = (url)=>{
|
|
71
71
|
chatController = new AbortController();
|
|
72
72
|
const protocol = options.protocol || new AGUIProtocol();
|
|
73
73
|
// ag-ui 协议需要注入消息模块
|
|
@@ -75,7 +75,7 @@ export const useAgent = (options, http, message)=>{
|
|
|
75
75
|
protocol.injectMessageModule(message);
|
|
76
76
|
}
|
|
77
77
|
fetchClient.streamRequest({
|
|
78
|
-
url: '
|
|
78
|
+
url: url || '/chat_completion/',
|
|
79
79
|
controller: chatController,
|
|
80
80
|
onDone: protocol.onDone,
|
|
81
81
|
onError: protocol.onError,
|
|
@@ -29,14 +29,12 @@ export interface RequestError extends Error {
|
|
|
29
29
|
code?: string;
|
|
30
30
|
config: RequestConfig;
|
|
31
31
|
isAxiosError: boolean;
|
|
32
|
-
request?: Request;
|
|
33
32
|
response?: Response;
|
|
34
33
|
}
|
|
35
34
|
export interface Response<T = unknown> {
|
|
36
35
|
config: RequestConfig;
|
|
37
36
|
data: T;
|
|
38
37
|
headers: Headers;
|
|
39
|
-
request?: Request;
|
|
40
38
|
status: number;
|
|
41
39
|
statusText: string;
|
|
42
40
|
}
|
|
@@ -70,10 +68,15 @@ export declare class FetchClient {
|
|
|
70
68
|
post<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
71
69
|
prepareRequest(config: RequestConfig, isStream?: boolean): {
|
|
72
70
|
url: string;
|
|
73
|
-
headers: Headers;
|
|
74
|
-
body: BodyInit;
|
|
75
71
|
requestConfig: RequestConfig;
|
|
76
|
-
|
|
72
|
+
fetchConfig: {
|
|
73
|
+
method: string;
|
|
74
|
+
credentials: "include" | "omit" | "same-origin";
|
|
75
|
+
mode: "same-origin" | "cors" | "no-cors";
|
|
76
|
+
headers: Headers;
|
|
77
|
+
body: BodyInit;
|
|
78
|
+
controller: AbortController;
|
|
79
|
+
};
|
|
77
80
|
};
|
|
78
81
|
put<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<T>;
|
|
79
82
|
request<T = unknown>(config: RequestConfig): Promise<T>;
|
|
@@ -192,8 +192,9 @@ export class FetchClient {
|
|
|
192
192
|
prepareRequest(config, isStream = false) {
|
|
193
193
|
// 合并配置
|
|
194
194
|
const mergedConfig = mergeConfig(this.defaults, config);
|
|
195
|
-
//
|
|
195
|
+
// 总的请求配置
|
|
196
196
|
let requestConfig = mergedConfig;
|
|
197
|
+
// 应用请求拦截器
|
|
197
198
|
this.interceptors.request.forEach((interceptor)=>{
|
|
198
199
|
if (interceptor.fulfilled) {
|
|
199
200
|
try {
|
|
@@ -208,7 +209,7 @@ export class FetchClient {
|
|
|
208
209
|
});
|
|
209
210
|
// 构建完整 URL
|
|
210
211
|
let url = requestConfig.url || '';
|
|
211
|
-
if (requestConfig.baseURL) {
|
|
212
|
+
if (requestConfig.baseURL && !url.startsWith('http')) {
|
|
212
213
|
url = requestConfig.baseURL + url;
|
|
213
214
|
}
|
|
214
215
|
url = buildURL(url, requestConfig.params);
|
|
@@ -219,6 +220,7 @@ export class FetchClient {
|
|
|
219
220
|
if (isStream && !headers.has('Accept')) {
|
|
220
221
|
headers.set('Accept', 'text/event-stream');
|
|
221
222
|
}
|
|
223
|
+
// 处理请求体
|
|
222
224
|
if (body !== undefined && body !== null) {
|
|
223
225
|
var _headers_get;
|
|
224
226
|
if (requestConfig.transformRequest) {
|
|
@@ -229,13 +231,20 @@ export class FetchClient {
|
|
|
229
231
|
}
|
|
230
232
|
// 创建 AbortController
|
|
231
233
|
const controller = requestConfig.controller ? requestConfig.controller : new AbortController();
|
|
232
|
-
|
|
233
|
-
|
|
234
|
+
// 请求配置
|
|
235
|
+
const fetchConfig = {
|
|
236
|
+
method: requestConfig.method,
|
|
237
|
+
credentials: requestConfig.credentials,
|
|
238
|
+
mode: requestConfig.mode,
|
|
234
239
|
headers,
|
|
235
240
|
body,
|
|
236
|
-
requestConfig,
|
|
237
241
|
controller
|
|
238
242
|
};
|
|
243
|
+
return {
|
|
244
|
+
url,
|
|
245
|
+
requestConfig,
|
|
246
|
+
fetchConfig
|
|
247
|
+
};
|
|
239
248
|
}
|
|
240
249
|
put(url, data, config) {
|
|
241
250
|
return this.request(_object_spread_props(_object_spread({}, config), {
|
|
@@ -248,17 +257,12 @@ export class FetchClient {
|
|
|
248
257
|
var _this = this;
|
|
249
258
|
return _async_to_generator(function*() {
|
|
250
259
|
// 准备请求
|
|
251
|
-
const { url,
|
|
260
|
+
const { url, fetchConfig, requestConfig } = _this.prepareRequest(config);
|
|
252
261
|
// 创建超时控制
|
|
253
|
-
const timeoutId = requestConfig.timeout && requestConfig.timeout > 0 ? setTimeout(()=>controller.abort(), requestConfig.timeout) : undefined;
|
|
262
|
+
const timeoutId = requestConfig.timeout && requestConfig.timeout > 0 ? setTimeout(()=>fetchConfig.controller.abort(), requestConfig.timeout) : undefined;
|
|
254
263
|
try {
|
|
255
264
|
// 发送请求
|
|
256
|
-
const
|
|
257
|
-
headers,
|
|
258
|
-
body,
|
|
259
|
-
signal: controller.signal
|
|
260
|
-
}));
|
|
261
|
-
const fetchResponse = yield fetch(request);
|
|
265
|
+
const fetchResponse = yield fetch(url, fetchConfig);
|
|
262
266
|
// 清除超时定时器
|
|
263
267
|
if (timeoutId) {
|
|
264
268
|
clearTimeout(timeoutId);
|
|
@@ -299,13 +303,12 @@ export class FetchClient {
|
|
|
299
303
|
status: fetchResponse.status,
|
|
300
304
|
statusText: fetchResponse.statusText,
|
|
301
305
|
headers: fetchResponse.headers,
|
|
302
|
-
config: requestConfig
|
|
303
|
-
request
|
|
306
|
+
config: requestConfig
|
|
304
307
|
};
|
|
305
308
|
// 验证状态码
|
|
306
309
|
const validateStatus = requestConfig.validateStatus || _this.defaults.validateStatus;
|
|
307
310
|
if (!validateStatus(fetchResponse.status)) {
|
|
308
|
-
throw createError(`Request failed with status code ${fetchResponse.status}`, requestConfig, `ERR_BAD_RESPONSE`,
|
|
311
|
+
throw createError(`Request failed with status code ${fetchResponse.status}`, requestConfig, `ERR_BAD_RESPONSE`, response);
|
|
309
312
|
}
|
|
310
313
|
// 应用响应拦截器
|
|
311
314
|
let finalResponse = response;
|
|
@@ -331,7 +334,7 @@ export class FetchClient {
|
|
|
331
334
|
0,
|
|
332
335
|
'success'
|
|
333
336
|
].includes(apiResponse.code)) {
|
|
334
|
-
throw createError(apiResponse.message, requestConfig, apiResponse.code,
|
|
337
|
+
throw createError(apiResponse.message, requestConfig, apiResponse.code, finalResponse);
|
|
335
338
|
}
|
|
336
339
|
return apiResponse.data;
|
|
337
340
|
} catch (error) {
|
|
@@ -341,11 +344,11 @@ export class FetchClient {
|
|
|
341
344
|
}
|
|
342
345
|
// 处理中断错误
|
|
343
346
|
if (error instanceof Error && error.name === 'AbortError') {
|
|
344
|
-
const requestError = createError('Request timeout', requestConfig, 'ECONNABORTED', undefined
|
|
347
|
+
const requestError = createError('Request timeout', requestConfig, 'ECONNABORTED', undefined);
|
|
345
348
|
throw _this.applyResponseErrorInterceptors(requestError);
|
|
346
349
|
}
|
|
347
350
|
// 处理其他错误
|
|
348
|
-
const requestError = error.isAxiosError === true ? error : createError(error.message, requestConfig, error.code, undefined
|
|
351
|
+
const requestError = error.isAxiosError === true ? error : createError(error.message, requestConfig, error.code, undefined);
|
|
349
352
|
throw _this.applyResponseErrorInterceptors(requestError);
|
|
350
353
|
}
|
|
351
354
|
})();
|
|
@@ -361,22 +364,17 @@ export class FetchClient {
|
|
|
361
364
|
var _this = this;
|
|
362
365
|
return _async_to_generator(function*() {
|
|
363
366
|
// 准备请求(标记为流式请求)
|
|
364
|
-
const { url,
|
|
365
|
-
// 发送请求
|
|
366
|
-
const request = new Request(url, _object_spread_props(_object_spread({}, requestConfig), {
|
|
367
|
-
headers,
|
|
368
|
-
body,
|
|
369
|
-
signal: controller.signal
|
|
370
|
-
}));
|
|
367
|
+
const { url, fetchConfig, requestConfig } = _this.prepareRequest(config, true);
|
|
371
368
|
try {
|
|
372
369
|
var // 触发 onStart 回调
|
|
373
370
|
_config_onStart, _fetchResponse_body;
|
|
374
|
-
|
|
371
|
+
// 发送请求
|
|
372
|
+
const fetchResponse = yield fetch(url, fetchConfig);
|
|
375
373
|
// 验证状态码
|
|
376
374
|
const validateStatus = requestConfig.validateStatus || _this.defaults.validateStatus;
|
|
377
375
|
if (!validateStatus(fetchResponse.status)) {
|
|
378
376
|
var _config_onError;
|
|
379
|
-
const error = createError(`Request failed with status code ${fetchResponse.status}`, requestConfig, `ERR_BAD_RESPONSE`,
|
|
377
|
+
const error = createError(`Request failed with status code ${fetchResponse.status}`, requestConfig, `ERR_BAD_RESPONSE`, undefined);
|
|
380
378
|
(_config_onError = config.onError) === null || _config_onError === void 0 ? void 0 : _config_onError.call(config, error);
|
|
381
379
|
return;
|
|
382
380
|
}
|
|
@@ -429,7 +427,7 @@ export class FetchClient {
|
|
|
429
427
|
constructor(config = {}){
|
|
430
428
|
_define_property(this, "defaults", void 0);
|
|
431
429
|
_define_property(this, "interceptors", void 0);
|
|
432
|
-
this.defaults =
|
|
430
|
+
this.defaults = mergeConfig({
|
|
433
431
|
method: 'GET',
|
|
434
432
|
headers: {
|
|
435
433
|
'Content-Type': 'application/json'
|
|
@@ -468,11 +466,10 @@ function buildURL(url, params) {
|
|
|
468
466
|
return url;
|
|
469
467
|
}
|
|
470
468
|
// 创建错误对象
|
|
471
|
-
function createError(message, config, code,
|
|
469
|
+
function createError(message, config, code, response) {
|
|
472
470
|
const error = new Error(message);
|
|
473
471
|
error.config = config;
|
|
474
472
|
error.code = String(code);
|
|
475
|
-
error.request = request;
|
|
476
473
|
error.response = response;
|
|
477
474
|
error.isAxiosError = true;
|
|
478
475
|
return error;
|
package/dist/index.d.ts
CHANGED
|
@@ -536,7 +536,7 @@ export declare const useChatHelper: (options: IUseChatHelperOptions) => {
|
|
|
536
536
|
};
|
|
537
537
|
}>;
|
|
538
538
|
isInfoLoading: import("vue").Ref<boolean, boolean>;
|
|
539
|
-
chat: () => void;
|
|
539
|
+
chat: (url: string) => void;
|
|
540
540
|
stopChat: () => Promise<void>;
|
|
541
541
|
getAgentInfo: () => Promise<void>;
|
|
542
542
|
};
|