feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* 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 { useGetKnowledgeListInfo } from './use-get-knowledge-list-info';
|
||||
export { useKnowledgeNavigate } from './use-knowledge-navigate';
|
||||
export { useTosContent } from './use-tos-content';
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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 { useSpaceStore } from '@coze-arch/bot-studio-store';
|
||||
import { useErrorHandler } from '@coze-arch/logger';
|
||||
import { I18n } from '@coze-arch/i18n';
|
||||
import { CustomError } from '@coze-arch/bot-error';
|
||||
import { KnowledgeApi } from '@coze-arch/bot-api';
|
||||
import { Toast } from '@coze-arch/coze-design';
|
||||
|
||||
export const useGetKnowledgeListInfo = (params: { datasetID: string }) => {
|
||||
const spaceId = useSpaceStore(s => s.space.id);
|
||||
const cacheKey = `dataset-${params.datasetID}`;
|
||||
const capture = useErrorHandler();
|
||||
return useRequest(
|
||||
async () => {
|
||||
if (!params.datasetID) {
|
||||
throw new CustomError(
|
||||
'useListDataSetReq_error',
|
||||
'datasetid cannot be empty',
|
||||
);
|
||||
}
|
||||
const res = await KnowledgeApi.ListDataset({
|
||||
filter: {
|
||||
dataset_ids: [params.datasetID],
|
||||
},
|
||||
space_id: spaceId,
|
||||
});
|
||||
|
||||
if (res?.total) {
|
||||
return res?.dataset_list?.find(i => i.dataset_id === params.datasetID);
|
||||
} else if (res?.total !== 0) {
|
||||
capture(new CustomError('useListDataSetReq_error', res.msg || ''));
|
||||
}
|
||||
},
|
||||
{
|
||||
cacheKey,
|
||||
setCache: data => sessionStorage.setItem(cacheKey, JSON.stringify(data)),
|
||||
getCache: () => JSON.parse(sessionStorage.getItem(cacheKey) || '{}'),
|
||||
onError: error => {
|
||||
Toast.error({
|
||||
content: I18n.t('Network_error'),
|
||||
showClose: false,
|
||||
});
|
||||
capture(error);
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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 {
|
||||
useNavigate,
|
||||
type To,
|
||||
type Path,
|
||||
type NavigateOptions,
|
||||
} from 'react-router-dom';
|
||||
|
||||
import { getKnowledgeIDEQuery } from '@coze-data/knowledge-common-services/use-case';
|
||||
|
||||
/** knowledge 模块专用的 useNavigate,用于持久化公共 query 参数 */
|
||||
export const useKnowledgeNavigate: typeof useNavigate = () => {
|
||||
const navigate = useNavigate();
|
||||
const knowledgePageQuery = getKnowledgeIDEQuery();
|
||||
const overwriteNavigate = (to: To | number, opt?: NavigateOptions) => {
|
||||
if (typeof to === 'string') {
|
||||
const toPathname = to.startsWith('/') ? to : `${location.pathname}/${to}`;
|
||||
const urlObject = new URL(toPathname, window.location.origin);
|
||||
Object.entries(knowledgePageQuery).forEach(([queryKey, queryValue]) => {
|
||||
if (queryValue && !urlObject.searchParams.has(queryKey)) {
|
||||
urlObject.searchParams.set(queryKey, queryValue);
|
||||
}
|
||||
});
|
||||
const { pathname, search } = urlObject;
|
||||
return navigate(`${pathname}${search}`, opt);
|
||||
}
|
||||
if (isPathType(to)) {
|
||||
const { search } = to;
|
||||
const searchParams = new URLSearchParams(search);
|
||||
Object.entries(knowledgePageQuery).forEach(([queryKey, queryValue]) => {
|
||||
if (queryValue && !searchParams.has(queryKey)) {
|
||||
searchParams.set(queryKey, queryValue);
|
||||
}
|
||||
});
|
||||
return navigate({ ...to, search: searchParams.toString() }, opt);
|
||||
}
|
||||
if (isNumberType(to)) {
|
||||
return navigate(to);
|
||||
}
|
||||
return navigate(to, opt);
|
||||
};
|
||||
|
||||
return overwriteNavigate;
|
||||
};
|
||||
|
||||
const isPathType = (to: To | number): to is Partial<Path> =>
|
||||
typeof to === 'object';
|
||||
const isNumberType = (to: To | number): to is number => typeof to === 'number';
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
export const useTosContent = (tosUrl?: string) => {
|
||||
const {
|
||||
data: content,
|
||||
loading,
|
||||
error,
|
||||
} = useRequest(
|
||||
async () => {
|
||||
if (!tosUrl) {
|
||||
return null;
|
||||
}
|
||||
const response = await fetch(tosUrl, { cache: 'no-cache' });
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch content');
|
||||
}
|
||||
return response.json();
|
||||
},
|
||||
{
|
||||
refreshDeps: [tosUrl],
|
||||
},
|
||||
);
|
||||
|
||||
return { content, loading, error };
|
||||
};
|
||||
Reference in New Issue
Block a user