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,66 @@
/*
* 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 { FrequencyType } from '@coze-arch/bot-api/memory';
import { type AuthFrequencyInfo } from '@coze-arch/bot-api/knowledge';
import { Select } from '@coze-arch/coze-design';
interface AccountFrequencyItemProps {
accountInfo: AuthFrequencyInfo;
onFrequencyChange: (account: AuthFrequencyInfo) => void;
}
// TODO: hzf 需要修改为i18n
const FREQUENCY_OPTIONS = [
{ label: I18n.t('knowledge_weixin_015'), value: FrequencyType.None },
{ label: I18n.t('knowledge_weixin_016'), value: FrequencyType.EveryDay },
{ label: I18n.t('knowledge_weixin_017'), value: FrequencyType.EveryThreeDay },
{ label: I18n.t('knowledge_weixin_018'), value: FrequencyType.EverySevenDay },
];
export const AccountFrequencyItem = ({
accountInfo,
onFrequencyChange,
}: AccountFrequencyItemProps) => {
const [frequency, setFrequency] = useState<FrequencyType>(
accountInfo.auth_frequency_type,
);
const handleFrequencyChange = (value: FrequencyType) => {
setFrequency(value);
onFrequencyChange({
...accountInfo,
auth_frequency_type: value,
});
};
return (
<div className="flex flex-col">
<div className="text-[14px] coz-fg-primary mb-1 font-medium">
{accountInfo.auth_name}
</div>
<Select
value={frequency}
onChange={value => handleFrequencyChange(value as FrequencyType)}
optionList={FREQUENCY_OPTIONS}
className="w-full"
/>
</div>
);
};

View File

@@ -0,0 +1,102 @@
/*
* 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, useEffect } from 'react';
import { I18n } from '@coze-arch/i18n';
import { type AuthFrequencyInfo } from '@coze-arch/bot-api/knowledge';
import { Toast, Modal } from '@coze-arch/coze-design';
import { saveSettingChange } from '../service/use-case/save-setting-change';
import { useInit } from '../hooks/life-cycle/use-init';
import { AccountFrequencyItem } from './account-frequency-item';
export const SetAppendFrequencyModal = (props: {
datasetId: string;
onFinish: () => void;
onClose?: () => void;
}) => {
const { datasetId, onFinish, onClose } = props;
const [pendingAccounts, setPendingAccounts] = useState<AuthFrequencyInfo[]>(
[],
);
const [loading, setLoading] = useState(false);
const { initAccountList, initLoading } = useInit(datasetId);
useEffect(() => {
if (initAccountList) {
setPendingAccounts(initAccountList);
}
}, [initAccountList]);
return (
<Modal
// @ts-expect-error --TODO:hzf 需要修改为i18n
title={I18n.t('设置追加频率')}
className="w-[520px]"
centered
visible
cancelText={I18n.t('Cancel')}
okText={I18n.t('knowledge_optimize_007')}
okButtonProps={{ loading: loading || initLoading }}
onOk={async () => {
try {
setLoading(true);
await saveSettingChange({
datasetId,
pendingAccounts,
});
Toast.success(I18n.t('Update_success'));
onClose?.();
onFinish();
} catch {
Toast.error(I18n.t('Update_failed'));
} finally {
setLoading(false);
}
}}
onCancel={() => {
onClose?.();
if (initAccountList) {
setPendingAccounts(initAccountList);
}
}}
>
<>
<div className="text-[14px] coz-fg-primary mb-[30px]">
{/**@ts-expect-error --TODO:hzf 需要修改为i18n */}
{I18n.t('设置追加频率后,当前频率自动追加')}
</div>
<div className="flex flex-col gap-2">
{pendingAccounts.map(account => (
<AccountFrequencyItem
key={account.auth_id}
accountInfo={account}
onFrequencyChange={newAccount => {
setPendingAccounts(prev =>
prev.map(item =>
item.auth_id === newAccount.auth_id ? newAccount : item,
),
);
}}
/>
))}
</div>
</>
</Modal>
);
};

View File

@@ -0,0 +1,44 @@
/*
* 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 { useRequest } from 'ahooks';
import { DataNamespace, dataReporter } from '@coze-data/reporter';
import { REPORT_EVENTS } from '@coze-arch/report-events';
import { KnowledgeApi } from '@coze-arch/bot-api';
export const useInit = (datasetId: string) => {
const { data: initAccountList, loading: initLoading } = useRequest(
async () => {
const response = await KnowledgeApi.GetAppendFrequency({
dataset_id: datasetId,
});
return response.auth_frequency_info;
},
{
onError: error => {
dataReporter.errorEvent(DataNamespace.KNOWLEDGE, {
eventName: REPORT_EVENTS.KnowledgeGetAuthList,
error,
});
},
},
);
return {
initAccountList,
initLoading,
};
};

View File

@@ -0,0 +1,48 @@
/*
* 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 { SetAppendFrequencyModal } from '../../components/main';
export const useSetAppendFrequencyModal = (modalProps: {
datasetId: string;
onFinish: () => void;
}) => {
const [visible, setVisible] = useState(false);
const open = () => {
setVisible(true);
};
const close = () => {
setVisible(false);
};
const node = visible ? (
<SetAppendFrequencyModal
datasetId={modalProps.datasetId}
onFinish={modalProps.onFinish}
onClose={close}
/>
) : null;
return {
node,
open,
close,
};
};

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.
*/
export { useSetAppendFrequencyModal } from './hooks/use-case/use-set-append-frequency-modal';

View File

@@ -0,0 +1,43 @@
/*
* 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 { DataNamespace, dataReporter } from '@coze-data/reporter';
import { REPORT_EVENTS } from '@coze-arch/report-events';
import { type AuthFrequencyInfo } from '@coze-arch/bot-api/knowledge';
import { KnowledgeApi } from '@coze-arch/bot-api';
export const saveSettingChange = async (params: {
datasetId: string;
pendingAccounts: AuthFrequencyInfo[];
}) => {
const { datasetId, pendingAccounts } = params;
try {
await KnowledgeApi.SetAppendFrequency({
dataset_id: datasetId,
auth_frequency_info: pendingAccounts.map(account => ({
auth_id: account.auth_id,
auth_frequency_type: account.auth_frequency_type,
})),
});
} catch (error) {
dataReporter.errorEvent(DataNamespace.KNOWLEDGE, {
eventName: REPORT_EVENTS.KnowledgeUpdateWechatFrequency,
error: error as Error,
});
throw error;
}
};