coze-studio/frontend/packages/agent-ide/space-bot/src/hook/use-node-id.ts

40 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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 { useCurrentEntity } from '@flowgram-adapter/free-layout-editor';
/**
* 获取当前节点 id若返回 undefined 则代表不在 multi 模式的节点上下文
*
* Q1这个 hook 应该放到 multi-agent 模块下?
* A1multi-agent 模块下已有类似 hook。
* 单独提出来是为了避免某些在 single 和 multi 都会复用的组件要使用 node id 时,产生不合理的 import 路径
*
* Q2在 single 和 multi 都复用的组件,应该由调用方传入 node id
* A2太理想了。有些组件业务逻辑过于复杂由调用方传入的话会导致层层传参的深度略显夸张比如 bot 的 workflow 弹窗。
*/
export function useCurrentNodeId() {
let nodeId: string | undefined;
try {
// eslint-disable-next-line react-hooks/rules-of-hooks -- 并未条件性调用 hook
nodeId = useCurrentEntity().id;
// eslint-disable-next-line @coze-arch/use-error-in-catch -- SDK 符合预期的报错,无需额外处理
} catch {
nodeId = undefined;
}
return nodeId;
}