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,69 @@
/*
* 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 { useRef, useEffect } from 'react';
export function useUnmountSignal() {
const controllerRef = useRef<AbortController | null>(null);
if (controllerRef.current === null) {
controllerRef.current = new AbortController();
}
useEffect(
() => () => {
if (controllerRef.current) {
controllerRef.current.abort();
}
},
[],
);
return controllerRef.current.signal;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- 得是 any
export function abortable<T extends (...args: any[]) => any | Promise<any>>(
func: T,
abortSignal: AbortSignal,
): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {
return async (...args) => {
try {
if (abortSignal.aborted) {
throw new Error('Function aborted');
}
const result = func(...args);
if (result instanceof Promise) {
return await Promise.race([
result,
new Promise((_, reject) => {
abortSignal.addEventListener(
'abort',
() => reject(new Error('Function aborted')),
{ once: true },
);
}),
]);
}
return result;
} catch (e) {
console.log(e);
}
};
}

View File

@@ -0,0 +1,65 @@
/*
* 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 { useState } from 'react';
import { I18n } from '@coze-arch/i18n';
import { ColumnType } from '@coze-arch/bot-api/knowledge';
import SinglelineSelect, {
type SinglelineSelectProps,
} from '../singleline-select';
export const getDataTypeText = (value: ColumnType) => {
const dataTypes = {
[ColumnType.Unknown]: 'Unknown',
[ColumnType.Text]: I18n.t('db_add_table_field_type_txt'),
[ColumnType.Number]: I18n.t('db_add_table_field_type_int'),
[ColumnType.Date]: I18n.t('db_add_table_field_type_time'),
[ColumnType.Float]: I18n.t('db_add_table_field_type_number'),
[ColumnType.Boolean]: I18n.t('db_add_table_field_type_bool'),
[ColumnType.Image]: I18n.t('knowledge_insert_img_010'),
};
return dataTypes[value] || '';
};
export const getDataTypeOptions = () => [
{ value: ColumnType.Text, label: getDataTypeText(ColumnType.Text) },
{ value: ColumnType.Number, label: getDataTypeText(ColumnType.Number) },
{ value: ColumnType.Date, label: getDataTypeText(ColumnType.Date) },
{ value: ColumnType.Float, label: getDataTypeText(ColumnType.Float) },
{ value: ColumnType.Boolean, label: getDataTypeText(ColumnType.Boolean) },
{ value: ColumnType.Image, label: getDataTypeText(ColumnType.Image) },
];
export const DataTypeSelect = (props: SinglelineSelectProps) => {
const [selectValue, setSelectValue] = useState<
SinglelineSelectProps['value']
>(props.value);
return (
<SinglelineSelect
value={selectValue}
selectProps={{
...props.selectProps,
optionList: props.selectProps?.optionList || getDataTypeOptions(),
}}
errorMsg={props.errorMsg}
handleChange={v => {
setSelectValue(v as SinglelineSelectProps['value']);
props.handleChange?.(v);
}}
/>
);
};

View File

@@ -0,0 +1,12 @@
.limit-count {
overflow: hidden;
padding-right: 12px;
padding-left: 8px;
font-size: 12px;
font-weight: 400;
font-style: normal;
line-height: 16px;
color: var(--coz-fg-secondary);
}

View File

@@ -0,0 +1,67 @@
/*
* 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 { useMemo } from 'react';
import { Input, type InputProps, withField } from '@coze-arch/coze-design';
import 'utility-types';
import s from './index.module.less';
interface LimitCountProps {
maxLen: number;
len: number;
}
const LimitCount: React.FC<LimitCountProps> = ({ maxLen, len }) => (
<span className={s['limit-count']}>
<span>{len}</span>
<span>/</span>
<span>{maxLen}</span>
</span>
);
export interface InputWithCountProps extends InputProps {
// 设置字数限制并显示字数统计
getValueLength?: (value?: InputProps['value'] | string) => number;
}
export const InputWithCount: React.FC<InputWithCountProps> = props => {
const { value, maxLength, getValueLength } = props;
const len = useMemo(() => {
if (getValueLength) {
return getValueLength(value);
} else if (value) {
return value.toString().length;
} else {
return 0;
}
}, [value, getValueLength]);
return (
<Input
{...props}
autoComplete="off"
suffix={
Boolean(maxLength) && <LimitCount maxLen={maxLength ?? 0} len={len} />
}
/>
);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const CozeInputWithCountField: any = withField(InputWithCount);

View File

@@ -0,0 +1,32 @@
/* stylelint-disable declaration-no-important */
.select-wapper {
width: 100%;
span {
width: 100%;
}
:global(.singleline-select-error-content) {
height: 20px;
}
:global(.select-error-text) {
position: absolute;
z-index: 100;
padding-top: 2px;
padding-left: 12px;
font-size: 12px;
font-weight: 400;
color: var(--coz-fg-hglt-red);
}
}
.error-wapper {
:global {
.semi-select {
border: 1px solid var(--coz-fg-hglt-red) !important;
}
}
}

View File

@@ -0,0 +1,68 @@
/*
* 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 cs from 'classnames';
import { useReactive } from 'ahooks';
import { Select, type SelectProps } from '@coze-arch/coze-design';
import type { InputProps } from '@coze-arch/bot-semi/Input';
import { CommonE2e } from '@coze-data/e2e';
import s from './index.module.less';
export interface SLSelectRefType {
triggerFocus?: () => void;
}
export type SinglelineSelectProps = InputProps & {
value: SelectProps['value'];
handleChange?: (v: SelectProps['value']) => void;
errorMsg?: string;
selectProps?: SelectProps;
};
export const SinglelineSelect: React.FC<SinglelineSelectProps> = props => {
const $state = useReactive({
value: props.value,
});
return (
<div
data-testid={CommonE2e.CommonDataTypeSelect}
className={cs(
s['select-wapper'],
props?.errorMsg ? s['error-wapper'] : null,
)}
>
<Select
{...props.selectProps}
style={{ width: '100%' }}
clickToHide={true}
value={$state.value}
onChange={v => {
($state.value as SelectProps['value']) = v;
props?.handleChange?.(v);
}}
/>
{props?.errorMsg ? (
<div className="singleline-select-error-content">
<div className="select-error-text">{props?.errorMsg}</div>
</div>
) : null}
</div>
);
};
export default SinglelineSelect;

View File

@@ -0,0 +1,21 @@
/* stylelint-disable declaration-no-important */
div.field {
padding-bottom: 24px !important;
&:has(:global(.semi-form-field-error-message)) {
padding-bottom: 0 !important;
}
:global {
.semi-form-field-error-message {
margin-top: 4px;
line-height: 20px;
}
.semi-input-textarea-counter {
padding-right: 5px!important;
padding-left: 5px;
}
}
}

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.
*/
import { forwardRef } from 'react';
import cs from 'classnames';
import { TextArea, withField } from '@coze-arch/coze-design';
import s from './index.module.less';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const TextAreaInner: any = withField(TextArea, {});
export const CozeFormTextArea: typeof TextAreaInner = forwardRef(
// @ts-expect-error -- to fix
({ fieldClassName, ...props }, ref) => (
<TextAreaInner
ref={ref}
{...props}
fieldClassName={cs(fieldClassName, s.field)}
/>
),
);

View File

@@ -0,0 +1,49 @@
/*
* 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.
*/
interface DatabasePageQuery {
page_mode?: 'modal' | 'normal';
from?: 'bot' | 'workflow' | 'library' | 'create';
bot_id?: string;
workflow_id?: string;
}
export const isDatabasePathname = (): boolean => {
const databasePagePathReg = new RegExp('/space/[0-9]+/database(/[0-9]+)*');
return databasePagePathReg.test(location.pathname);
};
export const getDatabasePageQuery = (): DatabasePageQuery => {
const queryParams = new URLSearchParams(location.search);
return {
page_mode: queryParams.get('page_mode') as DatabasePageQuery['page_mode'],
from: queryParams.get('from') as DatabasePageQuery['from'],
bot_id: queryParams.get('bot_id') || '',
workflow_id: queryParams.get('workflow_id') || '',
};
};
/** 获取 databse 页面模式,如果等于 'modal' 则需要使用全屏形态 */
export const getDatabasePageMode = (): DatabasePageQuery['page_mode'] => {
if (isDatabasePathname()) {
return getDatabasePageQuery()?.page_mode;
}
return 'normal';
};
/** 当前 Database 页面模式是弹窗(全屏)形式 */
export const databasePageModeIsModal = (): boolean =>
getDatabasePageMode() === 'modal';

View File

@@ -0,0 +1,33 @@
/*
* 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 { DataSourceType } from '@coze-arch/bot-api/memory';
import { DocumentSource } from '@coze-arch/bot-api/knowledge';
import { UnitType } from '@coze-data/knowledge-resource-processor-core';
export const isFeishuOrLarkDocumentSource = (
source: DocumentSource | undefined,
) => source === DocumentSource.FeishuWeb || source === DocumentSource.LarkWeb;
export const isFeishuOrLarkDataSourceType = (
source: DataSourceType | undefined,
) => source === DataSourceType.FeishuWeb || source === DataSourceType.LarkWeb;
export const isFeishuOrLarkTextUnit = (unitType: UnitType | undefined) =>
unitType === UnitType.TEXT_FEISHU || unitType === UnitType.TEXT_LARK;
export const isFeishuOrLarkTableUnit = (unitType: UnitType | undefined) =>
unitType === UnitType.TABLE_FEISHU || unitType === UnitType.TABLE_LARK;

View File

@@ -0,0 +1,13 @@
/* stylelint-disable no-duplicate-selectors */
/* stylelint-disable declaration-no-important */
.ui-data-modal {
:global {
.semi-modal-content {
@apply coz-bg-plus;
}
.semi-modal-content {
@apply coz-bg-plus;
}
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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 { useState } from 'react';
import cls from 'classnames';
// import { type ModalHeight } from '@coze-arch/coze-design/types';
// import { type ButtonColor } from '@coze-arch/coze-design/types';
import {
// type ButtonProps,
Modal,
type ModalProps,
} from '@coze-arch/coze-design';
import { type UseModalReturnValue } from '@coze-arch/bot-semi/src/components/ui-modal';
import { type UseModalParams, useModal } from '@coze-arch/bot-semi';
import styles from './index.module.less';
export const useDataModal = (params: UseModalParams): UseModalReturnValue => {
const { className, ...props } = params;
const modal = useModal({
...props,
className: cls(styles['ui-data-modal'], className),
});
return modal;
};
export type UseModalParamsCoze = Omit<ModalProps, 'visible'> & {
hideOkButton?: boolean;
hideCancelButton?: boolean;
showCloseIcon?: boolean;
hideContent?: boolean;
showScrollBar?: boolean;
// okButtonColor?: ButtonColor;
};
export const useDataModalWithCoze = ({
// type = 'info',
centered = true,
// height = 'fit-content',
...params
}: UseModalParamsCoze): UseModalReturnValue & {
canOk: boolean;
enableOk: () => void;
disableOk: () => void;
} => {
const [visible, setVisible] = useState(false);
const [disableOk, setDisableOk] = useState(false);
return {
modal: inner => (
<Modal
closeOnEsc
centered={Boolean(centered)}
// height={height as ModalHeight}
visible={visible}
okButtonProps={{
disabled: disableOk,
}}
{...(params as unknown as ModalProps)}
>
{inner}
</Modal>
),
open: () => setVisible(true),
close: () => setVisible(false),
visible,
disableOk: () => setDisableOk(true),
enableOk: () => setDisableOk(false),
canOk: !disableOk,
};
};

View File

@@ -0,0 +1,51 @@
/*
* 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 { isValidUrl, completeUrl } from './url';
export { getFormatTypeFromUnitType } from './knowledge-page';
export {
isDatabasePathname,
getDatabasePageQuery,
getDatabasePageMode,
databasePageModeIsModal,
} from './database-page';
export { FilterKnowledgeType, DocumentUpdateInterval } from './types';
export {
isFeishuOrLarkDocumentSource,
isFeishuOrLarkTextUnit,
isFeishuOrLarkTableUnit,
isFeishuOrLarkDataSourceType,
} from './feishu-lark';
export {
getUpdateIntervalOptions,
getUpdateTypeOptions,
} from './update-interval';
export {
DataTypeSelect,
getDataTypeText,
getDataTypeOptions,
} from './components/data-type-select';
export { CozeInputWithCountField } from './components/input-with-count';
export { CozeFormTextArea } from './components/text-area';
export { abortable, useUnmountSignal } from './abortable';
export {
useDataModal,
useDataModalWithCoze,
type UseModalParamsCoze,
} from './hooks/use-data-modal';

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.
*/
import { UnitType } from '@coze-data/knowledge-resource-processor-core';
import { FormatType } from '@coze-arch/bot-api/knowledge';
export const getFormatTypeFromUnitType = (type: UnitType) => {
switch (type) {
case UnitType.TABLE:
case UnitType.TABLE_API:
case UnitType.TABLE_DOC:
case UnitType.TABLE_CUSTOM:
case UnitType.TABLE_FEISHU:
case UnitType.TABLE_GOOGLE_DRIVE:
return FormatType.Table;
case UnitType.IMAGE:
case UnitType.IMAGE_FILE:
return FormatType.Image;
default:
return FormatType.Text;
}
};

View File

@@ -0,0 +1,30 @@
/*
* 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 enum FilterKnowledgeType {
ALL = 'ALL',
TEXT = 'TEXT',
TABLE = 'TABLE',
IMAGE = 'IMAGE',
}
export enum DocumentUpdateInterval {
NotUpdate = 0,
EveryDay = 1,
ThreeDay = 3,
SevenDay = 7,
ThirtyDay = 30,
}

View File

@@ -0,0 +1,25 @@
/*
* 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' />
/// <reference types='@coze-arch/bot-env/typings' />
declare const IS_DEV_MODE: boolean;
declare module '*.less' {
const resource: { [key: string]: string };
export = resource;
}

View File

@@ -0,0 +1,98 @@
/*
* 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 { I18n } from '@coze-arch/i18n';
import { FormatType } from '@coze-arch/bot-api/memory';
import { type DocumentSource } from '@coze-arch/bot-api/knowledge';
import { isFeishuOrLarkDocumentSource } from './feishu-lark';
/**
* FIXME: 由于后端限制前端需要在Feishu/Lark路径上去除30天的更新频率等后续后端解决后即可去掉
*/
export const getUpdateIntervalOptions = (
params: {
documentSource?: DocumentSource;
} = {},
) => {
const { documentSource } = params;
return [
{
value: 0,
label: I18n.t('datasets_frequencyModal_frequency_noUpdate'),
},
{
value: 1,
label: I18n.t('datasets_frequencyModal_frequency_day', {
num: 1,
}),
},
{
value: 3,
label: I18n.t('datasets_frequencyModal_frequency_day', {
num: 3,
}),
},
{
value: 7,
label: I18n.t('datasets_frequencyModal_frequency_day', {
num: 7,
}),
},
...(isFeishuOrLarkDocumentSource(documentSource)
? []
: [
{
value: 30,
label: I18n.t('datasets_frequencyModal_frequency_day', {
num: 30,
}),
},
]),
];
};
export const getAppendUpdateIntervalOptions = () => [
{
value: 0,
label: I18n.t('knowledge_weixin_015'),
},
{
value: 1,
label: I18n.t('knowledge_weixin_016'),
},
{
value: 3,
label: I18n.t('knowledge_weixin_017'),
},
{
value: 7,
label: I18n.t('knowledge_weixin_018'),
},
];
// table类型暂时禁用追加更新的更新类型待后续支持后下掉区分逻辑
export const getUpdateTypeOptions = (type: FormatType) => [
{
value: 1,
label: I18n.t('datasets_frequencyModal_whenUpdate_overwrite'),
},
{
value: 2,
disabled: type === FormatType.Table,
label: I18n.t('datasets_frequencyModal_whenUpdate_overwrite_keep'),
},
];

View File

@@ -0,0 +1,42 @@
/*
* 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.
*/
const DOMAIN_REGEXP = /^([0-9a-zA-Z-]{1,}\.)+([a-zA-Z]{2,})$/;
export function isValidUrl(url: string): boolean {
try {
const urlObject = new URL(url);
return (
DOMAIN_REGEXP.test(urlObject.hostname) &&
// cp-disable-next-line
(url.indexOf('https://') !== -1 || url.indexOf('http://') !== -1)
);
// eslint-disable-next-line @coze-arch/use-error-in-catch -- 根据函数功能无需 throw error
} catch {
return false;
}
}
export function completeUrl(url: string): string {
let newUrl = url.trim();
if (!newUrl.includes('://')) {
// cp-disable-next-line
newUrl = `http://${newUrl}`;
}
return newUrl;
}