feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AxiosError, type AxiosResponse } from 'axios';
import { logger } from '@coze-arch/logger';
// 上报事件枚举
export enum ReportEventNames {
NetworkError = 'flow-infra-network-error',
ApiError = 'flow-infra-api-error',
}
interface ApiErrorOptions {
hasShowedError?: boolean;
}
export class ApiError extends AxiosError {
hasShowedError: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public raw?: any;
type: string;
// eslint-disable-next-line max-params
constructor(
public code: string,
public msg: string | undefined,
response: AxiosResponse,
options: ApiErrorOptions = {},
) {
const { hasShowedError = false } = options;
super(msg, code, response.config, response.request, response);
this.name = 'ApiError';
this.type = 'Api Response Error';
this.hasShowedError = hasShowedError;
this.raw = response.data;
}
}
export const isApiError = (error: unknown): error is ApiError =>
error instanceof ApiError;
// 上报http错误apiError&axiosError
export const reportHttpError = (
eventName: ReportEventNames,
error: AxiosError,
) => {
try {
const { response, config } = error;
const {
code = '',
msg = '',
message,
} = response?.data as {
code?: string;
msg?: string;
message?: string;
};
const { status: httpStatusCode, headers } = response || {};
const { method: httpMethod, url: urlPath } = config || {};
const logId = headers?.['x-tt-logid'];
const customErrorCode = String(code);
const customErrorMsg = message ?? msg;
logger.persist.error({
eventName,
error,
meta: {
message: error.message,
name: error.name,
httpStatusCode: String(httpStatusCode),
httpMethod,
urlPath,
logId,
customErrorCode,
customErrorMsg,
},
});
} catch (e) {
logger.persist.error({
error: e as Error,
message: error.message,
});
}
};

View File

@@ -0,0 +1,187 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import axios, { type AxiosResponse, isAxiosError } from 'axios';
import { redirect } from '@coze-arch/web-context';
import { logger } from '@coze-arch/logger';
import { emitAPIErrorEvent, APIErrorEvent } from './eventbus';
import { ApiError, reportHttpError, ReportEventNames } from './api-error';
interface UnauthorizedResponse {
data: {
redirect_uri: string;
};
code: number;
msg: string;
}
export enum ErrorCodes {
NOT_LOGIN = 700012006,
COUNTRY_RESTRICTED = 700012015,
COZE_TOKEN_INSUFFICIENT = 702082020,
COZE_TOKEN_INSUFFICIENT_WORKFLOW = 702095072,
}
export const axiosInstance = axios.create();
const HTTP_STATUS_COE_UNAUTHORIZED = 401;
type ResponseInterceptorOnFulfilled = (res: AxiosResponse) => AxiosResponse;
const customInterceptors = {
response: new Set<ResponseInterceptorOnFulfilled>(),
};
axiosInstance.interceptors.response.use(
response => {
logger.info({
namespace: 'api',
scope: 'response',
message: '----',
meta: { response },
});
const { data = {} } = response;
// 新增接口返回message字段
const { code, msg, message } = data;
if (code !== 0) {
const apiError = new ApiError(String(code), message ?? msg, response);
switch (code) {
case ErrorCodes.NOT_LOGIN: {
// @ts-expect-error type safe
apiError.config.__disableErrorToast = true;
emitAPIErrorEvent(APIErrorEvent.UNAUTHORIZED, apiError);
break;
}
case ErrorCodes.COUNTRY_RESTRICTED: {
// @ts-expect-error type safe
apiError.config.__disableErrorToast = true;
emitAPIErrorEvent(APIErrorEvent.COUNTRY_RESTRICTED, apiError);
break;
}
case ErrorCodes.COZE_TOKEN_INSUFFICIENT: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
apiError.config.__disableErrorToast = true;
emitAPIErrorEvent(APIErrorEvent.COZE_TOKEN_INSUFFICIENT, apiError);
break;
}
case ErrorCodes.COZE_TOKEN_INSUFFICIENT_WORKFLOW: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
apiError.config.__disableErrorToast = true;
emitAPIErrorEvent(APIErrorEvent.COZE_TOKEN_INSUFFICIENT, apiError);
break;
}
default: {
break;
}
}
reportHttpError(ReportEventNames.ApiError, apiError);
return Promise.reject(apiError);
}
let res = response;
for (const interceptor of customInterceptors.response) {
res = interceptor(res);
}
return res;
},
error => {
if (isAxiosError(error)) {
reportHttpError(ReportEventNames.NetworkError, error);
if (error.response?.status === HTTP_STATUS_COE_UNAUTHORIZED) {
// 401 身份过期&没有身份
if (typeof error.response.data === 'object') {
const unauthorizedData = error.response.data as UnauthorizedResponse;
const redirectUri = unauthorizedData?.data?.redirect_uri;
if (redirectUri) {
redirect(redirectUri);
}
}
}
}
return Promise.reject(error);
},
);
axiosInstance.interceptors.request.use(config => {
const setHeader = (key: string, value: string) => {
if (typeof config.headers.set === 'function') {
config.headers.set(key, value);
} else {
config.headers[key] = value;
}
};
const getHeader = (key: string) => {
if (typeof config.headers.get === 'function') {
return config.headers.get(key);
}
return config.headers[key];
};
setHeader('x-requested-with', 'XMLHttpRequest');
if (
['post', 'get'].includes(config.method?.toLowerCase() ?? '') &&
!getHeader('content-type')
) {
// 新的 csrf 防护需要 post/get 请求全部带上这个 header
setHeader('content-type', 'application/json');
if (!config.data) {
// axios 会自动在 data 为空时清除 content-type所以需要设置一个空对象
config.data = {};
}
}
return config;
});
type AddRequestInterceptorShape = typeof axiosInstance.interceptors.request.use;
/**
* 添加全局 axios 的 interceptor 处理器,方便在上层扩展 axios 行为。
* 请注意,该接口会影响所有 bot-http 下的请求,请注意保证行为的稳定性
*/
export const addGlobalRequestInterceptor: AddRequestInterceptorShape = (
onFulfilled,
onRejected?,
) => {
// PS: 这里不期望直接暴露 axios 实例到上层,因为不知道会被怎么修改使用
// 因此,这里需要暴露若干方法,将行为与副作用限制在可控范围内
const id = axiosInstance.interceptors.request.use(onFulfilled, onRejected);
return id;
};
type RemoveRequestInterceptorShape =
typeof axiosInstance.interceptors.request.eject;
/**
* 删除全局 axios 的 interceptor 处理器其中id 参数为调用 addGlobalRequestInterceptor 返回的值
*/
export const removeGlobalRequestInterceptor: RemoveRequestInterceptorShape = (
id: number,
) => {
axiosInstance.interceptors.request.eject(id);
};
export const addGlobalResponseInterceptor = (
onFulfilled: ResponseInterceptorOnFulfilled,
) => {
customInterceptors.response.add(onFulfilled);
return () => {
customInterceptors.response.delete(onFulfilled);
};
};

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { GlobalEventBus } from '@coze-arch/web-context';
// api 请求有关事件
export enum APIErrorEvent {
// 无登录状态
UNAUTHORIZED = 'unauthorized',
// 登录了 没权限
NOACCESS = 'noAccess',
// 风控拦截
SHARK_BLOCK = 'sharkBlocked',
// 国家限制
COUNTRY_RESTRICTED = 'countryRestricted',
// COZE TOKEN 不足
COZE_TOKEN_INSUFFICIENT = 'cozeTokenInsufficient',
}
const getEventBus = () => GlobalEventBus.create<APIErrorEvent>('bot-http');
export const emitAPIErrorEvent = (event: APIErrorEvent, ...data: unknown[]) => {
const evenBus = getEventBus();
evenBus.emit(event, ...data);
};
export const handleAPIErrorEvent = (
event: APIErrorEvent,
fn: (...args: unknown[]) => void,
) => {
const evenBus = getEventBus();
evenBus.on(event, fn);
};
export const removeAPIErrorEvent = (
event: APIErrorEvent,
fn: (...args: unknown[]) => void,
) => {
const evenBus = getEventBus();
evenBus.off(event, fn);
};
export const stopAPIErrorEvent = () => {
const evenBus = getEventBus();
evenBus.stop();
};
export const startAPIErrorEvent = () => {
const evenBus = getEventBus();
evenBus.start();
};
export const clearAPIErrorEvent = () => {
const evenBus = getEventBus();
evenBus.clear();
};

View File

@@ -0,0 +1,17 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference types='@coze-arch/bot-typings' />

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {
APIErrorEvent,
handleAPIErrorEvent,
removeAPIErrorEvent,
stopAPIErrorEvent,
startAPIErrorEvent,
clearAPIErrorEvent,
emitAPIErrorEvent,
} from './eventbus';
export {
axiosInstance,
addGlobalRequestInterceptor,
removeGlobalRequestInterceptor,
addGlobalResponseInterceptor,
ErrorCodes,
} from './axios';
export { ApiError, isApiError } from './api-error';
export { type AxiosRequestConfig } from 'axios';