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,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 { useNavigateBack } from './use-navigate-back';

View File

@@ -0,0 +1,156 @@
/*
* 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 } from 'react-router-dom';
import { useMemo } from 'react';
import { type WorkflowGlobalStateEntity } from '@coze-workflow/playground';
import { type WorkflowMode, workflowApi } from '@coze-workflow/base/api';
import { reporter } from '@coze-arch/logger';
import {
usePageJumpResponse,
PageType,
usePageJumpService,
SceneType,
} from '@coze-arch/bot-hooks';
import { CustomError } from '@coze-arch/bot-error';
const MAX_HISTORY_COUNT = 2;
const hasHistory = (): boolean =>
Boolean(
window.history.length > MAX_HISTORY_COUNT && window.document?.referrer,
);
type NavigateScene = 'publish' | 'exit';
/**
* 返回上一页逻辑
*/
export const useNavigateBack = () => {
const pageJumpResponse = usePageJumpResponse(PageType.WORKFLOW);
const { jump } = usePageJumpService();
const navigate = useNavigate();
/** 流程详情页路由更新会导致 pageJumpResponse 失效, 因此需要缓存一份原始版本 */
const memoPageJumpResponse = useMemo(() => pageJumpResponse, []);
const navigateBack = async (
workflowState: WorkflowGlobalStateEntity,
scene: NavigateScene,
) => {
reporter.event({
namespace: 'workflow',
eventName: 'workflow_navigate_back',
});
const { spaceId, info, workflowId, flowMode } = workflowState;
const { plugin_id } = info;
if (!spaceId || !workflowId) {
reporter.errorEvent({
namespace: 'workflow',
eventName: 'workflow_navigate_back_failed',
error: new CustomError(
'navigate_back_failed',
'no spaceId or workflowId',
),
});
return;
}
// 跳转回bot详情页并带上场景参数
if (memoPageJumpResponse?.scene) {
if (scene === 'publish') {
let pluginID = plugin_id;
// 第一次发布流程时需要重新从接口获取pluginID
if (!pluginID || pluginID === '0') {
const workflowRes = await workflowApi.GetCanvasInfo(
{
workflow_id: workflowId,
space_id: spaceId,
},
{
__disableErrorToast: true,
},
);
pluginID = workflowRes.data?.workflow?.plugin_id || '0';
}
if (
memoPageJumpResponse.scene === SceneType.BOT__VIEW__WORKFLOW &&
memoPageJumpResponse.botID
) {
const { botID, agentID, workflowOpenMode } = memoPageJumpResponse;
reporter.event({
namespace: 'workflow',
eventName: 'workflow_navigate_back_to_bot',
});
jump(SceneType.WORKFLOW_PUBLISHED__BACK__BOT, {
spaceID: spaceId,
botID,
workflowID: workflowId,
pluginID,
agentID,
workflowOpenMode,
flowMode: flowMode as WorkflowMode,
});
return;
}
}
if (scene === 'exit') {
if (
memoPageJumpResponse.scene === SceneType.BOT__VIEW__WORKFLOW &&
memoPageJumpResponse.botID
) {
const { botID, agentID, workflowOpenMode, workflowModalState } =
memoPageJumpResponse;
jump(SceneType.WORKFLOW__BACK__BOT, {
spaceID: spaceId,
botID,
workflowModalState,
agentID,
workflowOpenMode,
flowMode: flowMode as WorkflowMode,
});
return;
}
}
}
if (hasHistory()) {
reporter.event({
namespace: 'workflow',
eventName: 'workflow_navigate_back_to_history',
});
navigate(-1);
} else {
reporter.event({
namespace: 'workflow',
eventName: 'workflow_navigate_back_to_list',
});
// 如果 history 就一个页面,此时 navigate(-1) 不生效,需要手动指定路径
// 目前先写死路径,缺点是需要跟随页面结构更改而更改,后面再优化更好的实现
// /library 为 coze 2.0 混排资源列表页
navigate(`/space/${spaceId}/library`);
}
};
return {
navigateBack,
};
};

View File

@@ -0,0 +1,116 @@
/*
* 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 { useMount } from 'ahooks';
import { type WorkflowPlaygroundProps } from '@coze-workflow/playground';
import { OperateType } from '@coze-workflow/base/api';
import { I18n } from '@coze-arch/i18n';
import { Toast } from '@coze-arch/coze-design';
/** 流程详情页参数 */
interface SearchParams {
workflow_id: string;
space_id: string;
/** 流程版本, 当多人协作时有版本概念, 单独设置时可预览对应版本流程 */
version?: string;
/** 是否要恢复到目标版本, 如果设置, 则流程草稿会自动设置到对应 version */
set_version?: string;
/** 对应 version 的操作类型 */
opt_type?: string;
/** 流程页面打开来源 */
from?: WorkflowPlaygroundProps['from'];
/** 节点id 配置会自动定位到对应节点 */
node_id?: string;
/** 执行id 配置会展示对应执行结果 */
execute_id?: string;
/** 子流程执行id */
sub_execute_id?: string;
}
export function usePageParams() {
const searchParams = useMemo(() => {
const target: SearchParams = {
workflow_id: '',
space_id: '',
};
const params = new URLSearchParams(window.location.search);
params.forEach((value, key) => {
target[key] = value;
});
return target;
}, []);
const {
space_id: spaceId,
workflow_id: workflowId,
version,
set_version: setVersion,
opt_type,
from,
node_id: nodeId,
execute_id: executeId,
sub_execute_id: subExecuteId,
} = searchParams;
const optType = opt_type
? (Number(opt_type) as OperateType)
: OperateType.SubmitOperate;
useMount(() => {
const newSearchParams = new URLSearchParams(window.location.search);
let needUpdateSearch = false;
if (from === 'createSuccess') {
Toast.success(I18n.t('Create_success'));
newSearchParams.delete('from');
needUpdateSearch = true;
} else if (from === 'dupSuccess') {
Toast.success(I18n.t('Duplicate_success'));
newSearchParams.delete('from');
needUpdateSearch = true;
}
// 强制设置历史版本
if (setVersion) {
newSearchParams.delete('set_version');
newSearchParams.delete('version');
newSearchParams.delete('opt_type');
needUpdateSearch = true;
}
if (needUpdateSearch) {
history.replaceState(
{},
'',
`${location.origin + location.pathname}?${newSearchParams.toString()}`,
);
}
});
return {
spaceId,
workflowId,
version,
setVersion: Boolean(searchParams.set_version),
optType,
from,
nodeId,
executeId,
subExecuteId,
};
}