feat: Support for conversational streaming (#809)

### What problem does this PR solve?

feat: Support for conversational streaming
#709

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2024-05-16 20:15:02 +08:00
committed by GitHub
parent 95f809187e
commit c6c9dbde64
21 changed files with 424 additions and 255 deletions

View File

@ -1,5 +1,5 @@
import { Authorization, Token, UserInfo } from '@/constants/authorization';
import { getSearchValue } from './commonUtil';
const KeySet = [Authorization, Token, UserInfo];
const storage = {
@ -21,7 +21,7 @@ const storage = {
setToken: (value: string) => {
localStorage.setItem(Token, value);
},
setUserInfo: (value: string | Object) => {
setUserInfo: (value: string | Record<string, unknown>) => {
let valueStr = typeof value !== 'string' ? JSON.stringify(value) : value;
localStorage.setItem(UserInfo, valueStr);
},
@ -46,4 +46,13 @@ const storage = {
},
};
export const getAuthorization = () => {
const sharedId = getSearchValue('shared_id');
const authorization = sharedId
? 'Bearer ' + sharedId
: storage.getAuthorization() || '';
return authorization;
};
export default storage;

View File

@ -1,12 +1,12 @@
import { Authorization } from '@/constants/authorization';
import i18n from '@/locales/config';
import authorizationUtil from '@/utils/authorizationUtil';
import authorizationUtil, { getAuthorization } from '@/utils/authorizationUtil';
import { message, notification } from 'antd';
import { history } from 'umi';
import { RequestMethod, extend } from 'umi-request';
import { convertTheKeysOfTheObjectToSnake, getSearchValue } from './commonUtil';
import { convertTheKeysOfTheObjectToSnake } from './commonUtil';
const ABORT_REQUEST_ERR_MESSAGE = 'The user aborted a request.'; // 手动中断请求。errorHandler 抛出的error message
const ABORT_REQUEST_ERR_MESSAGE = 'The user aborted a request.';
const RetcodeMessage = {
200: i18n.t('message.200'),
@ -41,9 +41,7 @@ type ResultCode =
| 502
| 503
| 504;
/**
* 异常处理程序
*/
interface ResponseType {
retcode: number;
data: any;
@ -55,7 +53,6 @@ const errorHandler = (error: {
message: string;
}): Response => {
const { response } = error;
// 手动中断请求 abort
if (error.message === ABORT_REQUEST_ERR_MESSAGE) {
console.log('user abort request');
} else {
@ -77,20 +74,13 @@ const errorHandler = (error: {
return response;
};
/**
* 配置request请求时的默认参数
*/
const request: RequestMethod = extend({
errorHandler, // 默认错误处理
errorHandler,
timeout: 300000,
getResponse: true,
});
request.interceptors.request.use((url: string, options: any) => {
const sharedId = getSearchValue('shared_id');
const authorization = sharedId
? 'Bearer ' + sharedId
: authorizationUtil.getAuthorization();
const data = convertTheKeysOfTheObjectToSnake(options.data);
const params = convertTheKeysOfTheObjectToSnake(options.params);
@ -101,7 +91,9 @@ request.interceptors.request.use((url: string, options: any) => {
data,
params,
headers: {
...(options.skipToken ? undefined : { [Authorization]: authorization }),
...(options.skipToken
? undefined
: { [Authorization]: getAuthorization() }),
...options.headers,
},
interceptors: true,
@ -109,16 +101,11 @@ request.interceptors.request.use((url: string, options: any) => {
};
});
/*
* 请求response拦截器
* */
request.interceptors.response.use(async (response: any, options) => {
if (options.responseType === 'blob') {
return response;
}
const data: ResponseType = await response.clone().json();
// response 拦截
if (data.retcode === 401 || data.retcode === 401) {
notification.error({