feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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 { isEmpty } from 'lodash-es';
|
||||
import {
|
||||
type FlowNodeEntity,
|
||||
FlowNodeBaseType,
|
||||
} from '@flowgram-adapter/fixed-layout-editor';
|
||||
|
||||
export const getStoreNode = (node: FlowNodeEntity) => {
|
||||
const isBlockOrderIcon =
|
||||
node.flowNodeType === FlowNodeBaseType.BLOCK_ORDER_ICON;
|
||||
const isBlockIcon = node.flowNodeType === FlowNodeBaseType.BLOCK_ICON;
|
||||
return {
|
||||
node: isBlockOrderIcon || isBlockIcon ? node.parent! : node,
|
||||
updateCurrent: !(isBlockOrderIcon || isBlockIcon),
|
||||
};
|
||||
};
|
||||
|
||||
export const updateNodeExtInfo = (
|
||||
renderNode: FlowNodeEntity,
|
||||
info: Record<string, any>,
|
||||
) => {
|
||||
const { node, updateCurrent } = getStoreNode(renderNode);
|
||||
if (!updateCurrent) {
|
||||
renderNode.updateExtInfo(info);
|
||||
}
|
||||
if (isEmpty(node.getExtInfo())) {
|
||||
return;
|
||||
} else {
|
||||
node.updateExtInfo(info);
|
||||
}
|
||||
};
|
||||
|
||||
export const getNodeExtInfo = (renderNode: FlowNodeEntity) => {
|
||||
const { node } = getStoreNode(renderNode);
|
||||
return node.getExtInfo();
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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 {
|
||||
getParentChildrenCount,
|
||||
getTreeIdFromNodeId,
|
||||
getNodeIdFromTreeId,
|
||||
} from './node';
|
||||
export { getLineId, calcDistance } from './line';
|
||||
export { getStoreNode, updateNodeExtInfo, getNodeExtInfo } from './ext-info';
|
||||
export {
|
||||
getFrom,
|
||||
transformKnowledge,
|
||||
transformTable,
|
||||
transformPlugin,
|
||||
isLoop,
|
||||
} from './transform-tree';
|
||||
export { isDepEmpty } from './status';
|
||||
38
frontend/packages/components/resource-tree/src/utils/line.ts
Normal file
38
frontend/packages/components/resource-tree/src/utils/line.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 { Bezier } from 'bezier-js';
|
||||
import { type IPoint, Point } from '@flowgram-adapter/fixed-layout-editor';
|
||||
|
||||
import { type CustomLine } from '../typings';
|
||||
import { getBezierVerticalControlPoints } from '../components/lines-render/utils';
|
||||
|
||||
export const getLineId = (line?: CustomLine) => {
|
||||
if (!line) {
|
||||
return undefined;
|
||||
}
|
||||
return `${line.from.id}${line.to.id}`;
|
||||
};
|
||||
|
||||
export const calcDistance = (pos: IPoint, line?: CustomLine) => {
|
||||
if (!line) {
|
||||
return Number.MAX_SAFE_INTEGER;
|
||||
}
|
||||
const { fromPoint, toPoint } = line;
|
||||
const controls = getBezierVerticalControlPoints(line.fromPoint, line.toPoint);
|
||||
const bezier = new Bezier([fromPoint, ...controls, toPoint]);
|
||||
return Point.getDistance(pos, bezier.project(pos));
|
||||
};
|
||||
39
frontend/packages/components/resource-tree/src/utils/node.ts
Normal file
39
frontend/packages/components/resource-tree/src/utils/node.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 FlowNodeEntity } from '@flowgram-adapter/fixed-layout-editor';
|
||||
|
||||
const getParentOfNoBlock = (
|
||||
node: FlowNodeEntity,
|
||||
): FlowNodeEntity | undefined => {
|
||||
if (!node.parent) {
|
||||
return undefined;
|
||||
}
|
||||
if (node.parent.flowNodeType === 'block') {
|
||||
return getParentOfNoBlock(node.parent);
|
||||
}
|
||||
return node.parent;
|
||||
};
|
||||
|
||||
export const getParentChildrenCount = (node: FlowNodeEntity) => {
|
||||
const parent = getParentOfNoBlock(node);
|
||||
return parent?.children?.length || 0;
|
||||
};
|
||||
|
||||
export const getTreeIdFromNodeId = (id: string) =>
|
||||
id.replace('$blockIcon$', '');
|
||||
|
||||
export const getNodeIdFromTreeId = (id: string) => `$blockIcon$${id}`;
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 DependencyTree } from '@coze-arch/bot-api/workflow_api';
|
||||
|
||||
export const isDepEmpty = (data?: DependencyTree) => {
|
||||
if (!data) {
|
||||
return true;
|
||||
}
|
||||
if (data.edge_list?.length) {
|
||||
return false;
|
||||
}
|
||||
const rootNode = data.node_list?.[0]?.dependency || {};
|
||||
const hasKnowledge = rootNode.knowledge_list?.length;
|
||||
const hasPlugins = rootNode.plugin_version?.length;
|
||||
const hasTable = rootNode.table_list?.length;
|
||||
const hasSubWorkflow = rootNode.workflow_version?.length;
|
||||
return !hasKnowledge && !hasPlugins && !hasTable && !hasSubWorkflow;
|
||||
};
|
||||
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* 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 { nanoid } from 'nanoid';
|
||||
import type {
|
||||
DependencyTree,
|
||||
DependencyTreeNode,
|
||||
KnowledgeInfo,
|
||||
PluginVersionInfo,
|
||||
TableInfo,
|
||||
WorkflowVersionInfo,
|
||||
} from '@coze-arch/bot-api/workflow_api';
|
||||
|
||||
import { DependencyOrigin, NodeType } from '../typings';
|
||||
import { NANO_ID_NUM } from '../constants';
|
||||
|
||||
export const getFrom = (project_id?: boolean, is_product?: boolean) => {
|
||||
let from = DependencyOrigin.LIBRARY;
|
||||
if (project_id) {
|
||||
from = DependencyOrigin.APP;
|
||||
}
|
||||
if (is_product) {
|
||||
from = DependencyOrigin.SHOP;
|
||||
}
|
||||
return from;
|
||||
};
|
||||
|
||||
export const transformKnowledge = (depth: number, knowledge: KnowledgeInfo) => {
|
||||
const from = getFrom(
|
||||
Boolean(knowledge.project_id && knowledge.project_id !== '0'),
|
||||
knowledge.is_product,
|
||||
);
|
||||
|
||||
return {
|
||||
id: `${knowledge.id}_${nanoid(NANO_ID_NUM)}`,
|
||||
type: 'custom',
|
||||
meta: {
|
||||
isNodeEnd: true,
|
||||
},
|
||||
data: {
|
||||
id: knowledge.id,
|
||||
name: knowledge.name,
|
||||
depth,
|
||||
collapsed: false,
|
||||
type: NodeType.KNOWLEDGE,
|
||||
from,
|
||||
version: undefined,
|
||||
icon: knowledge.icon,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const transformTable = (depth: number, table: TableInfo) => {
|
||||
const from = getFrom(
|
||||
Boolean(table.project_id && table.project_id !== '0'),
|
||||
table.is_product,
|
||||
);
|
||||
|
||||
return {
|
||||
id: `${table.id}_${nanoid(NANO_ID_NUM)}`,
|
||||
type: 'custom',
|
||||
meta: {
|
||||
isNodeEnd: true,
|
||||
},
|
||||
data: {
|
||||
id: table.id,
|
||||
name: table.name,
|
||||
depth,
|
||||
collapsed: false,
|
||||
type: NodeType.DATABASE,
|
||||
from,
|
||||
version: undefined,
|
||||
icon: table.icon,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const transformPlugin = (depth: number, plugin: PluginVersionInfo) => {
|
||||
const from = getFrom(
|
||||
Boolean(plugin.project_id && plugin.project_id !== '0'),
|
||||
plugin.is_product,
|
||||
);
|
||||
return {
|
||||
id: `${plugin.id}_${plugin.version}`,
|
||||
type: 'custom',
|
||||
meta: {
|
||||
isNodeEnd: true,
|
||||
},
|
||||
data: {
|
||||
id: plugin.id,
|
||||
name: plugin.name,
|
||||
depth,
|
||||
collapsed: false,
|
||||
type: NodeType.PLUGIN,
|
||||
from,
|
||||
version: plugin.version,
|
||||
icon: plugin.icon,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const findNode = (
|
||||
tree: DependencyTree,
|
||||
id?: string,
|
||||
): DependencyTreeNode | undefined => {
|
||||
if (!id) {
|
||||
return undefined;
|
||||
}
|
||||
const nodeList = tree.node_list || [];
|
||||
const node = nodeList.find(n => n?.id === id);
|
||||
return node;
|
||||
};
|
||||
|
||||
/**
|
||||
* 判断是否循环
|
||||
*/
|
||||
export const isLoop = (id: string, json: DependencyTree) => {
|
||||
const node = findNode(json, id);
|
||||
// 只有 workflow 会存在循环,因此只需要关注 workflow_version
|
||||
if (node?.dependency?.workflow_version?.length) {
|
||||
return hasSameId(id, node.dependency.workflow_version, json);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// 判断循环
|
||||
const hasSameId = (
|
||||
id: string,
|
||||
arr: WorkflowVersionInfo[],
|
||||
json: DependencyTree,
|
||||
): boolean => {
|
||||
const hasSame = arr.some(n => n?.id === id);
|
||||
if (hasSame) {
|
||||
return true;
|
||||
}
|
||||
return arr.some(n => {
|
||||
const node = findNode(json, n?.id);
|
||||
return hasSameId(id, node?.dependency?.workflow_version || [], json);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user