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,119 @@
/*
* 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 { createContext, type PropsWithChildren, type FC, useRef } from 'react';
import {
type ProcessingKnowledgeStore,
createProcessingKnowledgeStore,
} from './processing-knowledge';
import {
type IParams,
type ParamsStore,
createParamsStore,
} from './params-store';
import {
type KnowledgePreviewStore,
createKnowledgePreviewStore,
} from './knowledge-preview';
export type WidgetUIState = 'loading' | 'saving' | 'error' | 'normal';
export interface PluginNavType {
toResource?: (
res: string,
resID?: string,
query?: Record<string, string>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Record<string, any>,
) => void;
// submodule
upload?: (
query?: Record<string, string>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Record<string, any>,
) => void;
navigateTo?: (
path: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Record<string, any>,
) => void;
}
export interface CallbacksType {
onUpdateDisplayName?: (displayName: string) => void;
onStatusChange?: (status: WidgetUIState) => void;
}
interface TParamsStoreContext {
paramsStore: ParamsStore | undefined;
knowledgeStore: KnowledgePreviewStore | undefined;
processingKnowledge: ProcessingKnowledgeStore | undefined;
callbacks: CallbacksType;
resourceNavigate: PluginNavType;
}
export const KnowledgeParamsStoreContext = createContext<TParamsStoreContext>({
paramsStore: undefined,
knowledgeStore: undefined,
processingKnowledge: undefined,
callbacks: {},
resourceNavigate: {},
});
export const KnowledgeParamsStoreProvider: FC<
PropsWithChildren<{
params: IParams;
onUpdateDisplayName?: (displayName: string) => void;
onStatusChange?: (status: WidgetUIState) => void;
resourceNavigate: PluginNavType;
}>
> = ({
children,
params,
onUpdateDisplayName,
onStatusChange,
resourceNavigate,
}) => {
const paramsStoreRef = useRef<ParamsStore>();
const knowledgeStoreRef = useRef<KnowledgePreviewStore>();
const processingStoreRef = useRef<ProcessingKnowledgeStore>();
paramsStoreRef.current = createParamsStore(params);
if (!knowledgeStoreRef.current) {
knowledgeStoreRef.current = createKnowledgePreviewStore({
version: params.version,
});
}
if (!processingStoreRef.current) {
processingStoreRef.current = createProcessingKnowledgeStore();
}
return (
<KnowledgeParamsStoreContext.Provider
value={{
paramsStore: paramsStoreRef.current,
knowledgeStore: knowledgeStoreRef.current,
processingKnowledge: processingStoreRef.current,
callbacks: { onUpdateDisplayName, onStatusChange },
resourceNavigate,
}}
>
{children}
</KnowledgeParamsStoreContext.Provider>
);
};

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 { useContext } from 'react';
import { useStoreWithEqualityFn } from 'zustand/traditional';
import { shallow } from 'zustand/shallow';
import { REPORT_EVENTS } from '@coze-arch/report-events';
import { CustomError } from '@coze-arch/bot-error';
import {
type ProcessingKnowledgeInfo,
type ProcessingKnowledgeInfoAction,
} from './processing-knowledge';
import { type IParamsStore } from './params-store';
import { type ILevelSegmentsSlice } from './level-segments-slice';
import {
type KnowledgePreviewAction,
type KnowledgePreviewState,
} from './knowledge-preview';
import {
type CallbacksType,
type PluginNavType,
KnowledgeParamsStoreContext,
} from './context';
export const useKnowledgeParamsStore: <T>(
selector: (store: IParamsStore) => T,
) => T = selector => {
const context = useContext(KnowledgeParamsStoreContext);
if (!context.paramsStore) {
throw new CustomError(REPORT_EVENTS.normalError, 'params store context');
}
return useStoreWithEqualityFn(context.paramsStore, selector, shallow);
};
export const useKnowledgeParams = () => {
const params = useKnowledgeParamsStore(store => store.params);
return params;
};
export const useDataCallbacks: () => CallbacksType = () => {
const {
callbacks: { onStatusChange, onUpdateDisplayName },
} = useContext(KnowledgeParamsStoreContext);
return { onStatusChange, onUpdateDisplayName };
};
export const useDataNavigate: () => PluginNavType = () => {
const { resourceNavigate } = useContext(KnowledgeParamsStoreContext);
return resourceNavigate;
};
export const useKnowledgeStore: <T>(
selector: (
store: KnowledgePreviewState & KnowledgePreviewAction & ILevelSegmentsSlice,
) => T,
) => T = selector => {
const context = useContext(KnowledgeParamsStoreContext);
if (!context.knowledgeStore) {
throw new CustomError(REPORT_EVENTS.normalError, 'params store context');
}
return useStoreWithEqualityFn(context.knowledgeStore, selector, shallow);
};
export const useProcessingStore: <T>(
selector: (
store: ProcessingKnowledgeInfo & ProcessingKnowledgeInfoAction,
) => T,
) => T = selector => {
const context = useContext(KnowledgeParamsStoreContext);
if (!context.processingKnowledge) {
throw new CustomError(REPORT_EVENTS.normalError, 'params store context');
}
return useStoreWithEqualityFn(context.processingKnowledge, selector, shallow);
};

View File

@@ -0,0 +1,50 @@
/*
* 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 {
useDataCallbacks,
useDataNavigate,
useKnowledgeParams,
useKnowledgeParamsStore,
useKnowledgeStore,
useProcessingStore,
} from './hooks';
export {
KnowledgeParamsStoreContext,
KnowledgeParamsStoreProvider,
type WidgetUIState,
type PluginNavType,
} from './context';
export { type IParams as IKnowledgeParams } from './params-store';
export { FilterPhotoType } from './knowledge-preview';
export {
getDefaultLevelSegmentsState,
createLevelSegmentsSlice,
ILevelSegmentsSlice,
ILevelSegment,
IImageDetail,
ITableDetail,
} from './level-segments-slice';
export {
getDefaultStorageStrategyState,
createStorageStrategySlice,
IStorageStrategySlice,
} from './storage-strategy-slice';

View File

@@ -0,0 +1,109 @@
/*
* 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 { devtools } from 'zustand/middleware';
import { create } from 'zustand';
import { type DocumentInfo, type Dataset } from '@coze-arch/bot-api/knowledge';
import {
createLevelSegmentsSlice,
getDefaultLevelSegmentsState,
type ILevelSegmentsSlice,
} from './level-segments-slice';
export enum FilterPhotoType {
/**
* 全部
*/
All = 'All',
/**
* 已标注
*/
HasCaption = 'HasCaption',
/**
* 未标注
*/
NoCaption = 'NoCaption',
}
export interface KnowledgePreviewState {
canEdit?: boolean;
dataSetDetail: Dataset;
documentList: DocumentInfo[];
searchValue: string;
curDocId: string;
/**
* 图片类型是否已标注
*/
photoFilterValue: FilterPhotoType;
}
export interface KnowledgePreviewAction {
setCanEdit: (editable: boolean) => void;
setDataSetDetail: (dataSet: Dataset) => void;
setDocumentList: (documentList: DocumentInfo[]) => void;
setSearchValue: (v: string) => void;
setPhotoFilterValue: (v: FilterPhotoType) => void;
setCurDocId: (curDocId: string) => void;
}
const getInitialState = (options?: { version?: string }) => ({
canEdit: !options?.version,
dataSetDetail: {},
documentList: [],
curDocId: '',
searchValue: '',
photoFilterValue: FilterPhotoType.All,
});
export const createKnowledgePreviewStore = (options?: { version?: string }) =>
create<
KnowledgePreviewState & KnowledgePreviewAction & ILevelSegmentsSlice
>()(
devtools(
(set, get, ...args) => ({
...getInitialState(options),
...getDefaultLevelSegmentsState(),
...createLevelSegmentsSlice(set, get, ...args),
setDataSetDetail: (dataSetDetail: Dataset) => {
set({ dataSetDetail });
set({ canEdit: dataSetDetail?.can_edit && !options?.version });
},
setDocumentList: (documentList: DocumentInfo[]) => {
set({ documentList });
},
setSearchValue: (v: string) => {
set({ searchValue: v });
},
setPhotoFilterValue: (v: FilterPhotoType) => {
set({ photoFilterValue: v });
},
setCanEdit: editable => {
set({ canEdit: editable });
},
setCurDocId: (curDocId: string) => {
set({ curDocId });
},
reset: () => {
set(getInitialState(options));
},
}),
{ name: 'DEV_TOOLS_NAME_SPACE', enabled: IS_DEV_MODE },
),
);
export type KnowledgePreviewStore = ReturnType<
typeof createKnowledgePreviewStore
>;

View File

@@ -0,0 +1,91 @@
/*
* 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 { type StateCreator } from 'zustand';
export interface ITableDetail {
tableIdx: number | null;
tableName: string | null;
caption: string | null;
text: string | null;
cells: string | null;
}
export interface IImageDetail {
base64: string | null;
caption: string | null;
links: string | null;
token: string | null;
name: string | null;
}
export interface ILevelSegment {
id: number;
block_id: number | null;
slide_index: number | null;
slice_id?: string;
slice_sequence?: number;
type:
| 'title'
| 'section-title'
| 'section-text'
| 'text'
| 'image'
| 'table'
| 'caption'
| 'header-footer'
| 'header'
| 'footer'
| 'formula'
| 'footnote'
| 'toc'
| 'code'
| 'page-title';
level: number;
parent: number;
children: number[];
text: string;
label: string;
html_text: string;
positions: string | null;
table_detail: ITableDetail;
image_detail: IImageDetail;
file_detail: string | null;
}
export interface ILevelSegmentsState {
levelSegments: ILevelSegment[];
}
export interface ILevelSegmentsAction {
setLevelSegments: (segments: ILevelSegment[]) => void;
}
export type ILevelSegmentsSlice = ILevelSegmentsState & ILevelSegmentsAction;
export const getDefaultLevelSegmentsState = () => ({
levelSegments: [],
});
export const createLevelSegmentsSlice: StateCreator<
ILevelSegmentsSlice
> = set => ({
...getDefaultLevelSegmentsState(),
setLevelSegments: (content: ILevelSegment[]) =>
set(() => ({
levelSegments: content,
})),
});

View File

@@ -0,0 +1,72 @@
/*
* 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 { devtools, subscribeWithSelector } from 'zustand/middleware';
import { create } from 'zustand';
import {
type UnitType,
type OptType,
} from '@coze-data/knowledge-resource-processor-core';
export enum ActionType {
ADD = 'add',
REMOVE = 'remove',
}
export interface IParams {
version?: string;
projectID?: string;
datasetID?: string;
spaceID?: string;
tableID?: string;
type?: UnitType;
opt?: OptType;
docID?: string;
biz: 'agentIDE' | 'workflow' | 'project' | 'library';
botID?: string;
workflowID?: string;
agentID?: string;
actionType?: ActionType;
initialTab?: 'structure' | 'draft' | 'online';
/** 作用是跳转上传页时能在 url 里带上抖音标记,以在上传页做视图区分 */
isDouyinBot?: boolean;
pageMode?: 'modal' | 'normal';
first_auto_open_edit_document_id?: string;
create?: string;
}
export interface IParamsStore {
params: IParams;
}
export const createParamsStore = (initialState: IParams) =>
create<IParamsStore>()(
devtools(
subscribeWithSelector((set, get) => ({
params: initialState,
//TODO: get
})),
{
enabled: IS_DEV_MODE,
name: 'knowledge.params',
},
),
);
export type ParamsStore = ReturnType<typeof createParamsStore>;

View File

@@ -0,0 +1,72 @@
/*
* 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 { devtools } from 'zustand/middleware';
import { create } from 'zustand';
const defaultState = {
processingDatasets: new Set<string>(),
};
export interface ProcessingKnowledgeInfo {
processingDatasets: Set<string>;
}
export interface ProcessingKnowledgeInfoAction {
getIsProcessing: (datasetId: string) => boolean;
addProcessingDataset: (datasetId: string) => void;
clearProcessingSet: () => void;
deleteProcessingDataset: (datasetId: string) => void;
}
export const createProcessingKnowledgeStore = () =>
create<ProcessingKnowledgeInfo & ProcessingKnowledgeInfoAction>()(
devtools((set, get) => ({
...defaultState,
getIsProcessing: (datasetId: string) => {
const { processingDatasets } = get();
return processingDatasets.has(datasetId);
},
addProcessingDataset: (datasetId: string) => {
const { processingDatasets } = get();
processingDatasets.add(datasetId);
set({
processingDatasets,
});
},
clearProcessingSet: () => {
const { processingDatasets } = get();
processingDatasets.clear();
set({
processingDatasets,
});
},
deleteProcessingDataset: (datasetId: string) => {
const { processingDatasets } = get();
if (!processingDatasets.has(datasetId)) {
return;
}
processingDatasets.delete(datasetId);
set({
processingDatasets,
});
},
})),
);
export type ProcessingKnowledgeStore = ReturnType<
typeof createProcessingKnowledgeStore
>;

View File

@@ -0,0 +1,59 @@
/*
* 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 { type StateCreator } from 'zustand';
import {
type OpenSearchConfig,
StorageLocation,
} from '@coze-arch/bot-api/knowledge';
export interface IStorageStrategyState {
enableStorageStrategy: boolean;
storageLocation: StorageLocation;
openSearchConfig: OpenSearchConfig;
testConnectionSuccess: boolean;
}
export interface IStorageStrategyAction {
setEnableStorageStrategy: (enableStorageStrategy: boolean) => void;
setStorageLocation: (storageLocation: StorageLocation) => void;
setOpenSearchConfig: (openSearchConfig: OpenSearchConfig) => void;
setTestConnectionSuccess: (testConnectionSuccess: boolean) => void;
}
export type IStorageStrategySlice = IStorageStrategyState &
IStorageStrategyAction;
export const getDefaultStorageStrategyState = (): IStorageStrategyState => ({
enableStorageStrategy: false,
storageLocation: StorageLocation.Default,
openSearchConfig: {},
testConnectionSuccess: false,
});
export const createStorageStrategySlice: StateCreator<
IStorageStrategySlice
> = set => ({
...getDefaultStorageStrategyState(),
setEnableStorageStrategy: (enableStorageStrategy: boolean) =>
set({ enableStorageStrategy }),
setStorageLocation: (storageLocation: StorageLocation) =>
set({ storageLocation }),
setOpenSearchConfig: (openSearchConfig: OpenSearchConfig) =>
set({ openSearchConfig }),
setTestConnectionSuccess: (testConnectionSuccess: boolean) =>
set({ testConnectionSuccess }),
});

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' />