@blueking/chat-helper 0.0.1-beta.4 → 0.0.1-beta.6
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,
|
package/dist/event/ag-ui.d.ts
CHANGED
|
@@ -131,7 +131,7 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
131
131
|
handleToolCallStartEvent(event: IToolCallStartEvent): void;
|
|
132
132
|
injectMessageModule(message: IMessageModule): void;
|
|
133
133
|
onDone(): void;
|
|
134
|
-
onError(error:
|
|
134
|
+
onError(error: Error): void;
|
|
135
135
|
onMessage(message: unknown): void;
|
|
136
136
|
onStart(): void;
|
|
137
137
|
}
|
package/dist/event/ag-ui.ts.js
CHANGED
|
@@ -320,12 +320,31 @@ let tempTimestamp = 0;
|
|
|
320
320
|
this.messageModule = message;
|
|
321
321
|
}
|
|
322
322
|
onDone() {
|
|
323
|
-
var
|
|
323
|
+
var // 回调给上层
|
|
324
|
+
_this_onDoneCallback, _this;
|
|
324
325
|
(_this_onDoneCallback = (_this = this).onDoneCallback) === null || _this_onDoneCallback === void 0 ? void 0 : _this_onDoneCallback.call(_this);
|
|
326
|
+
// 处理完成
|
|
327
|
+
const message = this.messageModule.getCurrentLoadingMessage();
|
|
328
|
+
if (message) {
|
|
329
|
+
message.status = MessageStatus.Complete;
|
|
330
|
+
}
|
|
325
331
|
}
|
|
326
332
|
onError(error) {
|
|
327
|
-
var
|
|
333
|
+
var // 回调给上层
|
|
334
|
+
_this_onErrorCallback, _this;
|
|
328
335
|
(_this_onErrorCallback = (_this = this).onErrorCallback) === null || _this_onErrorCallback === void 0 ? void 0 : _this_onErrorCallback.call(_this, error);
|
|
336
|
+
// 处理错误
|
|
337
|
+
const message = this.messageModule.getCurrentLoadingMessage();
|
|
338
|
+
if (message) {
|
|
339
|
+
message.status = MessageStatus.Error;
|
|
340
|
+
message.content = [
|
|
341
|
+
{
|
|
342
|
+
type: MessageContentType.Text,
|
|
343
|
+
data: error.message,
|
|
344
|
+
status: MessageContentStatus.Error
|
|
345
|
+
}
|
|
346
|
+
];
|
|
347
|
+
}
|
|
329
348
|
}
|
|
330
349
|
onMessage(message) {
|
|
331
350
|
var // 回调给上层
|
|
@@ -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,20 +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
|
-
method: requestConfig.method,
|
|
258
|
-
credentials: requestConfig.credentials,
|
|
259
|
-
mode: requestConfig.mode,
|
|
260
|
-
headers,
|
|
261
|
-
body,
|
|
262
|
-
signal: controller.signal
|
|
263
|
-
});
|
|
264
|
-
const fetchResponse = yield fetch(request);
|
|
265
|
+
const fetchResponse = yield fetch(url, fetchConfig);
|
|
265
266
|
// 清除超时定时器
|
|
266
267
|
if (timeoutId) {
|
|
267
268
|
clearTimeout(timeoutId);
|
|
@@ -302,13 +303,12 @@ export class FetchClient {
|
|
|
302
303
|
status: fetchResponse.status,
|
|
303
304
|
statusText: fetchResponse.statusText,
|
|
304
305
|
headers: fetchResponse.headers,
|
|
305
|
-
config: requestConfig
|
|
306
|
-
request
|
|
306
|
+
config: requestConfig
|
|
307
307
|
};
|
|
308
308
|
// 验证状态码
|
|
309
309
|
const validateStatus = requestConfig.validateStatus || _this.defaults.validateStatus;
|
|
310
310
|
if (!validateStatus(fetchResponse.status)) {
|
|
311
|
-
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);
|
|
312
312
|
}
|
|
313
313
|
// 应用响应拦截器
|
|
314
314
|
let finalResponse = response;
|
|
@@ -334,7 +334,7 @@ export class FetchClient {
|
|
|
334
334
|
0,
|
|
335
335
|
'success'
|
|
336
336
|
].includes(apiResponse.code)) {
|
|
337
|
-
throw createError(apiResponse.message, requestConfig, apiResponse.code,
|
|
337
|
+
throw createError(apiResponse.message, requestConfig, apiResponse.code, finalResponse);
|
|
338
338
|
}
|
|
339
339
|
return apiResponse.data;
|
|
340
340
|
} catch (error) {
|
|
@@ -344,11 +344,11 @@ export class FetchClient {
|
|
|
344
344
|
}
|
|
345
345
|
// 处理中断错误
|
|
346
346
|
if (error instanceof Error && error.name === 'AbortError') {
|
|
347
|
-
const requestError = createError('Request timeout', requestConfig, 'ECONNABORTED', undefined
|
|
347
|
+
const requestError = createError('Request timeout', requestConfig, 'ECONNABORTED', undefined);
|
|
348
348
|
throw _this.applyResponseErrorInterceptors(requestError);
|
|
349
349
|
}
|
|
350
350
|
// 处理其他错误
|
|
351
|
-
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);
|
|
352
352
|
throw _this.applyResponseErrorInterceptors(requestError);
|
|
353
353
|
}
|
|
354
354
|
})();
|
|
@@ -364,25 +364,17 @@ export class FetchClient {
|
|
|
364
364
|
var _this = this;
|
|
365
365
|
return _async_to_generator(function*() {
|
|
366
366
|
// 准备请求(标记为流式请求)
|
|
367
|
-
const { url,
|
|
368
|
-
// 发送请求
|
|
369
|
-
const request = new Request(url, {
|
|
370
|
-
method: requestConfig.method,
|
|
371
|
-
credentials: requestConfig.credentials,
|
|
372
|
-
mode: requestConfig.mode,
|
|
373
|
-
headers,
|
|
374
|
-
body,
|
|
375
|
-
signal: controller.signal
|
|
376
|
-
});
|
|
367
|
+
const { url, fetchConfig, requestConfig } = _this.prepareRequest(config, true);
|
|
377
368
|
try {
|
|
378
369
|
var // 触发 onStart 回调
|
|
379
370
|
_config_onStart, _fetchResponse_body;
|
|
380
|
-
|
|
371
|
+
// 发送请求
|
|
372
|
+
const fetchResponse = yield fetch(url, fetchConfig);
|
|
381
373
|
// 验证状态码
|
|
382
374
|
const validateStatus = requestConfig.validateStatus || _this.defaults.validateStatus;
|
|
383
375
|
if (!validateStatus(fetchResponse.status)) {
|
|
384
376
|
var _config_onError;
|
|
385
|
-
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);
|
|
386
378
|
(_config_onError = config.onError) === null || _config_onError === void 0 ? void 0 : _config_onError.call(config, error);
|
|
387
379
|
return;
|
|
388
380
|
}
|
|
@@ -435,7 +427,7 @@ export class FetchClient {
|
|
|
435
427
|
constructor(config = {}){
|
|
436
428
|
_define_property(this, "defaults", void 0);
|
|
437
429
|
_define_property(this, "interceptors", void 0);
|
|
438
|
-
this.defaults =
|
|
430
|
+
this.defaults = mergeConfig({
|
|
439
431
|
method: 'GET',
|
|
440
432
|
headers: {
|
|
441
433
|
'Content-Type': 'application/json'
|
|
@@ -474,11 +466,10 @@ function buildURL(url, params) {
|
|
|
474
466
|
return url;
|
|
475
467
|
}
|
|
476
468
|
// 创建错误对象
|
|
477
|
-
function createError(message, config, code,
|
|
469
|
+
function createError(message, config, code, response) {
|
|
478
470
|
const error = new Error(message);
|
|
479
471
|
error.config = config;
|
|
480
472
|
error.code = String(code);
|
|
481
|
-
error.request = request;
|
|
482
473
|
error.response = response;
|
|
483
474
|
error.isAxiosError = true;
|
|
484
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
|
};
|