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,47 @@
/*
* 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 { ProjectResourceActionKey } from '@coze-arch/bot-api/plugin_develop';
export const workflowActions = [
{
key: ProjectResourceActionKey.Rename,
enable: true,
},
{
key: ProjectResourceActionKey.Copy,
enable: true,
},
{
key: ProjectResourceActionKey.MoveToLibrary,
enable: false,
hint: '不能移动到资源库',
},
{
key: ProjectResourceActionKey.CopyToLibrary,
enable: true,
hint: '复制到资源库',
},
{
// 切换为 chatflow
key: ProjectResourceActionKey.SwitchToChatflow,
enable: true,
},
{
key: ProjectResourceActionKey.Delete,
enable: true,
},
];

View File

@@ -0,0 +1,18 @@
/*
* 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 { useProjectApi } from './use-project-api';
export { useListenWFMessageEvent } from './use-listen-message-event';

View File

@@ -0,0 +1,56 @@
/*
* 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 {
useIDENavigate,
getURIByResource,
useProjectIDEServices,
} from '@coze-project-ide/framework';
import { usePrimarySidebarStore } from '@coze-project-ide/biz-components';
import { I18n } from '@coze-arch/i18n';
import { WorkflowMode } from '@coze-arch/bot-api/workflow_api';
import { workflowApi } from '@coze-arch/bot-api';
import { Toast } from '@coze-arch/coze-design';
export const useChangeFlowMode = () => {
const refetch = usePrimarySidebarStore(state => state.refetch);
const navigate = useIDENavigate();
const { view } = useProjectIDEServices();
return async (
flowMode: WorkflowMode,
workflowId: string,
spaceId: string,
) => {
await workflowApi.UpdateWorkflowMeta({
workflow_id: workflowId,
space_id: spaceId,
flow_mode: flowMode,
});
Toast.success(
I18n.t('wf_chatflow_123', {
Chatflow: I18n.t(
flowMode === WorkflowMode.ChatFlow ? 'wf_chatflow_76' : 'Workflow',
),
}),
);
await refetch();
const uri = getURIByResource('workflow', workflowId);
const widgetContext = view.getWidgetContextFromURI(uri);
const widgetOpened = Boolean(widgetContext?.widget);
// 已经打开的 widget加 refresh 参数刷新,未打开的直接打开会刷新
navigate(`/workflow/${workflowId}${widgetOpened ? '?refresh=true' : ''}`);
};
};

View File

@@ -0,0 +1,87 @@
/*
* 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 ReactNode } from 'react';
import {
useWorkflowModal,
WorkflowModalFrom,
type WorkFlowModalModeProps,
WorkflowCategory,
} from '@coze-workflow/components';
import {
BizResourceTypeEnum,
useOpenResource,
usePrimarySidebarStore,
useResourceCopyDispatch,
} from '@coze-project-ide/biz-components';
import { WorkflowMode } from '@coze-arch/bot-api/workflow_api';
import { resource_resource_common } from '@coze-arch/bot-api/plugin_develop';
import { useNameValidators } from './use-name-validators';
export const useImportLibraryWorkflow = ({
projectId,
}: {
projectId: string;
}): {
modal: ReactNode;
importLibrary: () => void;
} => {
const refetch = usePrimarySidebarStore(state => state.refetch);
const openResource = useOpenResource();
const importResource = useResourceCopyDispatch();
const onImport: WorkFlowModalModeProps['onImport'] = async item => {
try {
close();
console.log('[ResourceFolder]import library workflow>>>', item);
await importResource({
scene:
resource_resource_common.ResourceCopyScene.CopyResourceFromLibrary,
res_id: item.workflow_id,
res_type: resource_resource_common.ResType.Workflow,
project_id: projectId,
res_name: item.name,
});
} catch (e) {
console.error('[ResourceFolder]import library workflow error>>>', e);
}
};
const nameValidators = useNameValidators();
const { node, open, close } = useWorkflowModal({
from: WorkflowModalFrom.ProjectImportLibrary,
flowMode: WorkflowMode.Workflow,
hiddenExplore: true,
hiddenCreate: true,
hiddenWorkflowCategories: [
WorkflowCategory.Example,
WorkflowCategory.Project,
],
projectId,
onImport,
nameValidators,
onCreateSuccess: async ({ workflowId }) => {
close();
await refetch();
openResource({
resourceType: BizResourceTypeEnum.Workflow,
resourceId: workflowId,
});
},
});
return { modal: node, importLibrary: open };
};

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 { type RefObject } from 'react';
import { useMemoizedFn } from 'ahooks';
import { type WorkflowPlaygroundRef } from '@coze-workflow/playground';
import {
useListenMessageEvent,
type URI,
type MessageEvent,
} from '@coze-project-ide/framework';
export const useListenWFMessageEvent = (
uri: URI,
ref: RefObject<WorkflowPlaygroundRef>,
) => {
const listener = useMemoizedFn((e: MessageEvent) => {
if (e.name === 'process' && e.data?.executeId && ref.current) {
ref.current.getProcess({ executeId: e.data.executeId });
} else if (e.name === 'debug' && ref.current) {
const { nodeId, executeId, subExecuteId } = e?.data || {};
if (nodeId) {
setTimeout(() => {
ref.current?.scrollToNode(nodeId);
}, 1000);
}
if (executeId) {
ref.current.showTestRunResult(executeId, subExecuteId);
}
}
});
useListenMessageEvent(uri, listener);
};

View File

@@ -0,0 +1,55 @@
/*
* 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 RefObject } from 'react';
import {
useResourceList,
type BizResourceType,
} from '@coze-project-ide/biz-components';
import { REPORT_EVENTS } from '@coze-arch/report-events';
import { I18n } from '@coze-arch/i18n';
import { CustomError } from '@coze-arch/bot-error';
export const useNameValidators = ({
currentResourceRef,
}: {
currentResourceRef?: RefObject<BizResourceType | undefined>;
} = {}): Array<{
validator: (rules: unknown[], value: string) => boolean | Error;
}> => {
const { workflowResource } = useResourceList();
return [
{
validator(_, value) {
// 过滤掉当前资源
const otherResource = currentResourceRef?.current
? workflowResource.filter(
r => r.res_id !== currentResourceRef?.current?.res_id,
)
: workflowResource;
if (otherResource.map(item => item.name).includes(value)) {
return new CustomError(
REPORT_EVENTS.formValidation,
I18n.t('project_resource_sidebar_warning_label_exists', {
label: value,
}),
);
}
return true;
},
},
];
};

View File

@@ -0,0 +1,47 @@
/*
* 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 { useMemoizedFn } from 'ahooks';
import { type ProjectApi } from '@coze-workflow/playground';
import {
useSendMessageEvent,
useIDENavigate,
useCurrentWidgetContext,
useIDEGlobalContext,
} from '@coze-project-ide/framework';
/**
* 注入到 workflow 内 project api 的能力。
* 注:非响应式
*/
export const useProjectApi = () => {
const { sendOpen } = useSendMessageEvent();
const { widget: uiWidget } = useCurrentWidgetContext();
const navigate = useIDENavigate();
const ideGlobalContext = useIDEGlobalContext();
const getProjectAPI = useMemoizedFn(() => {
const api: ProjectApi = {
navigate,
ideGlobalStore: ideGlobalContext,
setWidgetUIState: (status: string) => uiWidget.setUIState(status as any),
sendMsgOpenWidget: sendOpen,
};
return api;
});
return getProjectAPI;
};

View File

@@ -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 { useEffect, type RefObject } from 'react';
import { type WorkflowPlaygroundRef } from '@coze-workflow/playground';
import {
useIDEParams,
useIDENavigate,
useCurrentWidget,
getURLByURI,
type ProjectIDEWidget,
} from '@coze-project-ide/framework';
export const useRefresh = (ref: RefObject<WorkflowPlaygroundRef>) => {
const widget = useCurrentWidget<ProjectIDEWidget>();
const params = useIDEParams();
const navigate = useIDENavigate();
useEffect(() => {
if (params.refresh) {
ref.current?.reload();
navigate(getURLByURI(widget.uri!.removeQueryObject('refresh')), {
replace: true,
});
}
}, [params.refresh, ref, widget, navigate]);
};

View File

@@ -0,0 +1,55 @@
/*
* 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 BizResourceType,
useResourceCopyDispatch,
} from '@coze-project-ide/biz-components';
import { resource_resource_common } from '@coze-arch/bot-api/plugin_develop';
export interface ResourceOperationProps {
projectId: string;
}
export const useResourceOperation = ({ projectId }: ResourceOperationProps) => {
const copyDispatch = useResourceCopyDispatch();
return async ({
scene,
resource,
}: {
scene: resource_resource_common.ResourceCopyScene;
resource?: BizResourceType;
}) => {
try {
console.log(
`[ResourceFolder]workflow resource copy dispatch, scene ${scene}>>>`,
resource,
);
await copyDispatch({
scene,
res_id: resource?.id,
res_type: resource_resource_common.ResType.Workflow,
project_id: projectId,
res_name: resource?.name || '',
});
} catch (e) {
console.error(
`[ResourceFolder]workflow resource copy dispatch, scene ${scene} error>>>`,
e,
);
}
};
};

View File

@@ -0,0 +1,274 @@
/*
* 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 React, {
type ReactNode,
useCallback,
useMemo,
useRef,
type RefObject,
} from 'react';
import {
useCreateWorkflowModal,
WorkflowModalFrom,
} from '@coze-workflow/components';
import {
type ResourceFolderProps,
type ResourceType,
useProjectId,
useSpaceId,
} from '@coze-project-ide/framework';
import {
BizResourceContextMenuBtnType,
type BizResourceType,
BizResourceTypeEnum,
type ResourceFolderCozeProps,
useOpenResource,
usePrimarySidebarStore,
} from '@coze-project-ide/biz-components';
import { I18n } from '@coze-arch/i18n';
import { WorkflowMode } from '@coze-arch/bot-api/workflow_api';
import { ResourceCopyScene } from '@coze-arch/bot-api/plugin_develop';
import { workflowApi } from '@coze-arch/bot-api';
import { Toast } from '@coze-arch/coze-design';
import { WORKFLOW_SUB_TYPE_ICON_MAP } from '@/constants';
import { WorkflowTooltip } from '@/components';
import { useResourceOperation } from './use-resource-operation';
import { useNameValidators } from './use-name-validators';
import { useImportLibraryWorkflow } from './use-import-library-workflow';
import { useChangeFlowMode } from './use-change-flow-mode';
type UseWorkflowResourceReturn = Pick<
ResourceFolderCozeProps,
| 'onCustomCreate'
| 'onDelete'
| 'onChangeName'
| 'onAction'
| 'createResourceConfig'
| 'iconRender'
> & { modals: ReactNode };
// eslint-disable-next-line @coze-arch/max-line-per-function
export const useWorkflowResource = (): UseWorkflowResourceReturn => {
const refetch = usePrimarySidebarStore(state => state.refetch);
const spaceId = useSpaceId();
const projectId = useProjectId();
const openResource = useOpenResource();
const currentResourceRef = useRef<BizResourceType>();
const nameValidators = useNameValidators({
currentResourceRef: currentResourceRef as RefObject<
BizResourceType | undefined
>,
});
const {
createWorkflowModal,
workflowModal: templateWorkflowModal,
openCreateModal,
handleEditWorkflow,
} = useCreateWorkflowModal({
from: WorkflowModalFrom.ProjectAddWorkflowResource,
spaceId,
projectId,
hiddenTemplateEntry: true,
nameValidators,
refreshPage: () => {
currentResourceRef.current = undefined;
refetch?.();
},
onCreateSuccess: async ({ workflowId }) => {
await refetch?.();
openResource({
resourceType: BizResourceTypeEnum.Workflow,
resourceId: workflowId,
});
},
});
const onCustomCreate: ResourceFolderCozeProps['onCustomCreate'] = (
resourceType,
subType,
) => {
console.log('[ResourceFolder]on custom create>>>', resourceType, subType);
openCreateModal(subType as WorkflowMode);
};
const onChangeName: ResourceFolderProps['onChangeName'] = useCallback(
async changeNameEvent => {
try {
console.log('[ResourceFolder]on change name>>>', changeNameEvent);
const resp = await workflowApi.UpdateWorkflowMeta({
space_id: spaceId,
workflow_id: changeNameEvent.id,
name: changeNameEvent.name,
});
console.log('[ResourceFolder]rename workflow response>>>', resp);
} catch (e) {
console.log('[ResourceFolder]rename workflow error>>>', e);
} finally {
refetch();
}
},
[refetch, spaceId],
);
const updateDesc = useCallback(
async (resource?: BizResourceType) => {
if (!resource?.res_id) {
return;
}
currentResourceRef.current = resource;
const resp = await workflowApi.GetWorkflowDetail({
space_id: spaceId,
workflow_ids: [resource?.res_id],
});
const workflowInfo = resp?.data?.[0];
if (!workflowInfo) {
return;
}
handleEditWorkflow({
space_id: workflowInfo.space_id,
workflow_id: workflowInfo.workflow_id,
url: workflowInfo.icon,
icon_uri: workflowInfo.icon_uri,
name: workflowInfo.name,
desc: workflowInfo.desc,
});
},
[spaceId, handleEditWorkflow],
);
const onDelete = useCallback(
async (resources: ResourceType[]) => {
try {
console.log('[ResourceFolder]on delete>>>', resources);
console.log('delete start>>>', Date.now());
const resp = await workflowApi.BatchDeleteWorkflow({
space_id: spaceId,
workflow_id_list: resources
.filter(r => r.type === BizResourceTypeEnum.Workflow)
.map(r => r.id),
});
console.log('delete end>>>', Date.now());
Toast.success(I18n.t('Delete_success'));
refetch().then(() => console.log('refetch end>>>', Date.now()));
console.log('[ResourceFolder]delete workflow response>>>', resp);
} catch (e) {
console.log('[ResourceFolder]delete workflow error>>>', e);
Toast.error(I18n.t('Delete_failed'));
}
},
[refetch, spaceId],
);
const { modal: workflowModal, importLibrary } = useImportLibraryWorkflow({
projectId,
});
const changeFlowMode = useChangeFlowMode();
const resourceOperation = useResourceOperation({ projectId });
const onAction = (
action: BizResourceContextMenuBtnType,
resource?: BizResourceType,
) => {
switch (action) {
case BizResourceContextMenuBtnType.ImportLibraryResource:
return importLibrary();
case BizResourceContextMenuBtnType.DuplicateResource:
return resourceOperation({
scene: ResourceCopyScene.CopyProjectResource,
resource,
});
case BizResourceContextMenuBtnType.MoveToLibrary:
return resourceOperation({
scene: ResourceCopyScene.MoveResourceToLibrary,
resource,
});
case BizResourceContextMenuBtnType.CopyToLibrary:
return resourceOperation({
scene: ResourceCopyScene.CopyResourceToLibrary,
resource,
});
case BizResourceContextMenuBtnType.UpdateDesc:
return updateDesc(resource);
case BizResourceContextMenuBtnType.SwitchToChatflow:
return changeFlowMode(
WorkflowMode.ChatFlow,
resource?.res_id ?? '',
spaceId,
);
case BizResourceContextMenuBtnType.SwitchToWorkflow:
return changeFlowMode(
WorkflowMode.Workflow,
resource?.res_id ?? '',
spaceId,
);
default:
console.warn('[WorkflowResource]unsupported action>>>', action);
break;
}
};
const createResourceConfig = useMemo(
() =>
[
{
icon: WORKFLOW_SUB_TYPE_ICON_MAP[WorkflowMode.Workflow],
label: I18n.t('project_resource_sidebar_create_new_resource', {
resource: I18n.t('library_resource_type_workflow'),
}),
subType: WorkflowMode.Workflow,
tooltip: <WorkflowTooltip flowMode={WorkflowMode.Workflow} />,
},
// 社区版本暂不支持对话流
IS_OPEN_SOURCE
? null
: {
icon: WORKFLOW_SUB_TYPE_ICON_MAP[WorkflowMode.ChatFlow],
label: I18n.t('project_resource_sidebar_create_new_resource', {
resource: I18n.t('wf_chatflow_76'),
}),
subType: WorkflowMode.ChatFlow,
tooltip: <WorkflowTooltip flowMode={WorkflowMode.ChatFlow} />,
},
].filter(Boolean) as ResourceFolderCozeProps['createResourceConfig'],
[],
);
const iconRender: ResourceFolderCozeProps['iconRender'] = useMemo(
() =>
({ resource }) => (
<>
{
WORKFLOW_SUB_TYPE_ICON_MAP[
resource.res_sub_type || WorkflowMode.Workflow
]
}
</>
),
[],
);
return {
onChangeName,
onAction,
onDelete,
onCustomCreate,
createResourceConfig,
iconRender,
modals: [workflowModal, createWorkflowModal, templateWorkflowModal],
};
};