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,67 @@
/*
* 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';
import { type InputValueVO, type OutputValueVO } from '@coze-workflow/base';
import { ConfigProvider } from '@coze-arch/coze-design';
import { useReadonly } from '@/nodes-v2/hooks/use-readonly';
import { type CodeEditorValue } from '@/form-extensions/setters/code/types';
import { type InputParams } from '@/form-extensions/setters/code/hooks/use-ide-input-output-type';
import { CodeSetterContext } from '@/form-extensions/setters/code/context';
import { CodeEditorWithBizIDE } from '@/form-extensions/setters/code/code-with-biz-ide';
import { useField, withField } from '@/form';
export const CodeField = withField(
({
tooltip,
outputParams,
inputParams,
}: {
tooltip?: string;
outputParams?: OutputValueVO[];
inputParams?: InputValueVO[];
}) => {
const { value, onChange, errors } = useField<CodeEditorValue>();
const readonly = useReadonly();
const feedbackText = errors?.[0]?.message || '';
const feedbackStatus = feedbackText ? 'error' : undefined;
const flowNodeEntity = useCurrentEntity();
return (
<ConfigProvider getPopupContainer={() => document.body}>
<CodeSetterContext.Provider
value={{
readonly,
flowNodeEntity,
}}
>
<CodeEditorWithBizIDE
feedbackStatus={feedbackStatus}
feedbackText={feedbackText}
inputParams={inputParams as InputParams}
onChange={onChange}
outputParams={outputParams}
outputPath={'/outputs'}
tooltip={tooltip}
value={value}
/>
</CodeSetterContext.Provider>
</ConfigProvider>
);
},
);

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 { CodeField } from './code-field';

View File

@@ -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 { nanoid } from 'nanoid';
import { ViewVariableType } from '@coze-workflow/variable';
// 路径
export const INPUT_PATH = 'inputParameters';
export const CODE_PATH = 'codeParams';
export const OUTPUT_PATH = 'outputs';
// 默认值
export const DEFAULT_OUTPUTS = [
{
key: nanoid(),
name: 'key0',
type: ViewVariableType.String,
},
{
key: nanoid(),
name: 'key1',
type: ViewVariableType.ArrayString,
},
{
key: nanoid(),
name: 'key2',
type: ViewVariableType.Object,
children: [
{
key: nanoid(),
name: 'key21',
type: ViewVariableType.String,
},
],
},
];
export const DEFAULT_INPUTS = [{ name: 'input' }];

View File

@@ -0,0 +1,68 @@
/*
* 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 NodeContext } from '@flowgram-adapter/free-layout-editor';
import { type NodeDataDTO } from '@coze-workflow/base';
import { getDefaultValue } from '@/form-extensions/setters/code/defaults';
import { type FormData } from './types';
import { DEFAULT_INPUTS, DEFAULT_OUTPUTS } from './constants';
/**
* 节点后端数据 -> 前端表单数据
*/
export const transformOnInit = (
value: NodeDataDTO | undefined,
context: NodeContext,
) => {
const { globalState } = context.playgroundContext;
const { isBindDouyin } = globalState;
const defaultCodeParams = getDefaultValue({ isBindDouyin });
// 初始值设置
const initValue = value || {
inputs: {
inputParameters: DEFAULT_INPUTS,
...defaultCodeParams,
},
outputs: DEFAULT_OUTPUTS,
};
const { inputs = {}, ...others } = initValue;
return {
...others,
inputParameters: inputs.inputParameters,
codeParams: {
code: inputs.code,
language: inputs.language,
},
nodeMeta: value?.nodeMeta,
};
};
/**
* 前端表单数据 -> 节点后端数据
* @param value
* @returns
*/
export const transformOnSubmit = (value: FormData) => ({
nodeMeta: value.nodeMeta,
inputs: {
inputParameters: value.inputParameters,
...value.codeParams,
},
outputs: value.outputs,
});

View File

@@ -0,0 +1,62 @@
/*
* 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 {
ValidateTrigger,
type FormMetaV2,
} from '@flowgram-adapter/free-layout-editor';
import { nodeMetaValidate } from '@/nodes-v2/materials/node-meta-validate';
import {
fireNodeTitleChange,
provideNodeOutputVariablesEffect,
} from '@/node-registries/common/effects';
import { outputTreeMetaValidator } from '../common/fields/outputs';
import { createCodeInputsValidator } from './validators/create-code-inputs-validator';
import { codeEmptyValidator } from './validators/code-empty-validator';
import { type FormData } from './types';
import { FormRender } from './form';
import { transformOnInit, transformOnSubmit } from './data-transformer';
import { CODE_PATH, OUTPUT_PATH } from './constants';
export const CODE_FORM_META: FormMetaV2<FormData> = {
// 节点表单渲染
render: () => <FormRender />,
// 验证触发时机
validateTrigger: ValidateTrigger.onChange,
// 验证规则
validate: {
nodeMeta: nodeMetaValidate,
...createCodeInputsValidator(),
[CODE_PATH]: codeEmptyValidator,
[OUTPUT_PATH]: outputTreeMetaValidator,
},
// 副作用管理
effect: {
nodeMeta: fireNodeTitleChange,
outputs: provideNodeOutputVariablesEffect,
},
// 节点后端数据 -> 前端表单数据
formatOnInit: transformOnInit,
// 前端表单数据 -> 节点后端数据
formatOnSubmit: transformOnSubmit,
};

View File

@@ -0,0 +1,54 @@
/*
* 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 { I18n } from '@coze-arch/i18n';
import { useForm } from '@flowgram-adapter/free-layout-editor';
import { NodeConfigForm } from '@/node-registries/common/components';
import { OutputsField, InputsParametersField } from '../common/fields';
import { CODE_PATH, INPUT_PATH, OUTPUT_PATH } from './constants';
import { CodeField } from './components';
export const FormRender = () => {
const form = useForm();
return (
<NodeConfigForm>
<InputsParametersField
name={INPUT_PATH}
tooltip={I18n.t('workflow_detail_code_input_tooltip')}
isTree={true}
/>
<CodeField
name={CODE_PATH}
tooltip={I18n.t('workflow_detail_code_code_tooltip')}
inputParams={form.getValueIn(INPUT_PATH)}
outputParams={form.getValueIn(OUTPUT_PATH)}
hasFeedback={false}
/>
<OutputsField
title={I18n.t('workflow_detail_node_output')}
tooltip={I18n.t('workflow_detail_code_output_tooltip')}
jsonImport={false}
id="code-node-outputs"
name={OUTPUT_PATH}
hasFeedback={false}
/>
</NodeConfigForm>
);
};

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 { CODE_NODE_REGISTRY } from './node-registry';
export { CodeContent } from './node-content';

View File

@@ -0,0 +1,26 @@
/*
* 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 { InputParameters, Outputs } from '../common/components';
export function CodeContent() {
return (
<>
<InputParameters />
<Outputs />
</>
);
}

View File

@@ -0,0 +1,49 @@
/*
* 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 {
DEFAULT_NODE_META_PATH,
DEFAULT_NODE_SIZE,
DEFAULT_OUTPUTS_PATH,
} from '@coze-workflow/nodes';
import {
StandardNodeType,
type WorkflowNodeRegistry,
} from '@coze-workflow/base';
import { type NodeTestMeta } from '@/test-run-kit';
import { test } from './node-test';
import { CODE_FORM_META } from './form-meta';
import { INPUT_PATH } from './constants';
export const CODE_NODE_REGISTRY: WorkflowNodeRegistry<NodeTestMeta> = {
type: StandardNodeType.Code,
meta: {
nodeDTOType: StandardNodeType.Code,
size: DEFAULT_NODE_SIZE,
style: {
width: 484,
},
test,
nodeMetaPath: DEFAULT_NODE_META_PATH,
outputsPath: DEFAULT_OUTPUTS_PATH,
inputParametersPath: INPUT_PATH, // 入参路径,试运行等功能依赖该路径提取参数
enableCopilotGenerateTestNodeForm: true,
helpLink: '/open/docs/guides/code_node',
},
formMeta: CODE_FORM_META,
};

View File

@@ -0,0 +1,30 @@
/*
* 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 { FlowNodeFormData } from '@flowgram-adapter/free-layout-editor';
import { generateParametersToProperties } from '@/test-run-kit';
import { type NodeTestMeta } from '@/test-run-kit';
export const test: NodeTestMeta = {
generateFormInputProperties(node) {
const formData = node
.getData(FlowNodeFormData)
.formModel.getFormItemValueByPath('/');
const parameters = formData?.inputParameters;
return generateParametersToProperties(parameters, { node });
},
};

View File

@@ -0,0 +1,30 @@
/*
* 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 OutputValueVO,
type InputValueVO,
type NodeDataDTO,
} from '@coze-workflow/base';
import { type CodeEditorValue } from '@/form-extensions/setters/code/types';
export interface FormData {
inputParameters: InputValueVO[];
nodeMeta: NodeDataDTO['nodeMeta'];
outputs: OutputValueVO[];
codeParams: CodeEditorValue;
}

View File

@@ -0,0 +1,25 @@
/*
* 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 { I18n } from '@coze-arch/i18n';
import { type Validate } from '@flowgram-adapter/free-layout-editor';
export const codeEmptyValidator: Validate = ({ value }) => {
const code = value?.code;
if (!code) {
return I18n.t('workflow_running_results_error_code');
}
};

View File

@@ -0,0 +1,25 @@
/*
* 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 Validate } from '@flowgram-adapter/free-layout-editor';
import { createInputTreeValidator } from '../../common/validators/create-input-tree-validator';
export function createCodeInputsValidator(): { [key: string]: Validate } {
return {
inputParameters: createInputTreeValidator(),
};
}