feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -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 { nanoid } from '@flowgram-adapter/free-layout-editor';
|
||||
import { ViewVariableType } from '@coze-workflow/nodes';
|
||||
import { I18n } from '@coze-arch/i18n';
|
||||
|
||||
export const DEFAULT_SUCCESS_OUTPUT = [
|
||||
{
|
||||
key: nanoid(),
|
||||
name: 'isSuccess',
|
||||
type: ViewVariableType.Boolean,
|
||||
required: true,
|
||||
description: I18n.t(
|
||||
'workflow_detail_variable_set_output_tooltip',
|
||||
{},
|
||||
'变量设置是否成功',
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
export const DEFAULT_GET_OUTPUT = [
|
||||
{
|
||||
key: nanoid(),
|
||||
name: '',
|
||||
type: ViewVariableType.String,
|
||||
required: true,
|
||||
},
|
||||
];
|
||||
|
||||
export enum ModeValue {
|
||||
Get = 'get',
|
||||
Set = 'set',
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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 '@flowgram-adapter/free-layout-editor';
|
||||
import { variableUtils } from '@coze-workflow/variable';
|
||||
import { ViewVariableType } from '@coze-workflow/nodes';
|
||||
import { type InputValueDTO, type InputValueVO } from '@coze-workflow/base';
|
||||
|
||||
import { ModeValue } from './constants';
|
||||
|
||||
export function transformOnInit(value, context) {
|
||||
const { playgroundContext } = context;
|
||||
const { variableService } = playgroundContext;
|
||||
const { inputs = {}, outputs = [], nodeMeta } = value || {};
|
||||
|
||||
const { mode = ModeValue.Set, inputParameters = [] } = inputs;
|
||||
|
||||
// 处理输入参数
|
||||
const formattedInputParameters: InputValueVO[] = [];
|
||||
inputParameters.forEach(input => {
|
||||
if (!input) {
|
||||
return;
|
||||
}
|
||||
formattedInputParameters.push(
|
||||
variableUtils.inputValueToVO(input, variableService),
|
||||
);
|
||||
});
|
||||
|
||||
const isSetMode = mode === ModeValue.Set;
|
||||
|
||||
// 处理输出参数
|
||||
const formattedOutputs =
|
||||
outputs.length > 0
|
||||
? outputs
|
||||
: [
|
||||
{
|
||||
key: nanoid(),
|
||||
name: isSetMode ? 'isSuccess' : '',
|
||||
type: isSetMode
|
||||
? ViewVariableType.Boolean
|
||||
: ViewVariableType.String,
|
||||
},
|
||||
];
|
||||
|
||||
return {
|
||||
nodeMeta,
|
||||
mode,
|
||||
inputParameters: formattedInputParameters,
|
||||
outputs: formattedOutputs,
|
||||
};
|
||||
}
|
||||
|
||||
export function transformOnSubmit(value, context) {
|
||||
const { playgroundContext, node } = context;
|
||||
const { variableService } = playgroundContext;
|
||||
|
||||
const { nodeMeta, mode, inputParameters, outputs } = value;
|
||||
|
||||
// 处理输入参数
|
||||
const formattedInputParameters: InputValueDTO[] = [];
|
||||
inputParameters.forEach(input => {
|
||||
if (!input) {
|
||||
return;
|
||||
}
|
||||
const inputValue = variableUtils.inputValueToDTO(input, variableService, {
|
||||
node,
|
||||
}) as InputValueDTO;
|
||||
formattedInputParameters.push(inputValue);
|
||||
});
|
||||
|
||||
return {
|
||||
nodeMeta,
|
||||
inputs: {
|
||||
mode,
|
||||
inputParameters: formattedInputParameters,
|
||||
},
|
||||
outputs,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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 {
|
||||
ValidateTrigger,
|
||||
type FormMetaV2,
|
||||
} from '@flowgram-adapter/free-layout-editor';
|
||||
|
||||
import { provideNodeOutputVariablesEffect } from '@/nodes-v2/materials/provide-node-output-variables';
|
||||
import { fireNodeTitleChange } from '@/nodes-v2/materials/fire-node-title-change';
|
||||
import { createValueExpressionInputValidate } from '@/nodes-v2/materials/create-value-expression-input-validate';
|
||||
|
||||
import type { FormData } from './types';
|
||||
import FormRender from './form';
|
||||
import { transformOnInit, transformOnSubmit } from './data-transformer';
|
||||
|
||||
const INPUT_PARAMETERS_FIELD_NAME = 'inputParameters.*.name';
|
||||
|
||||
export const VARIABLE_NODE_FORM_META: FormMetaV2<FormData> = {
|
||||
// 节点表单渲染
|
||||
render: () => <FormRender />,
|
||||
|
||||
// 验证触发时机
|
||||
validateTrigger: ValidateTrigger.onChange,
|
||||
|
||||
// 验证规则
|
||||
validate: {
|
||||
[INPUT_PARAMETERS_FIELD_NAME]: ({ value }) => {
|
||||
if (/^.+$/.test(value)) {
|
||||
return undefined;
|
||||
}
|
||||
return I18n.t('bot_edit_variable_field_required_error');
|
||||
},
|
||||
'inputParameters.*.input': createValueExpressionInputValidate({
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
|
||||
// 副作用管理
|
||||
effect: {
|
||||
nodeMeta: fireNodeTitleChange,
|
||||
outputs: provideNodeOutputVariablesEffect,
|
||||
},
|
||||
|
||||
// 节点后端数据 -> 前端表单数据
|
||||
formatOnInit: transformOnInit,
|
||||
|
||||
// 前端表单数据 -> 节点后端数据
|
||||
formatOnSubmit: transformOnSubmit,
|
||||
};
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 { NodeConfigForm } from '@/node-registries/common/components';
|
||||
import { useWatch } from '@/form';
|
||||
|
||||
import {
|
||||
InputsParametersField,
|
||||
OutputsField,
|
||||
RadioSetterField,
|
||||
} from '../common/fields';
|
||||
import { ModeValue } from './constants';
|
||||
|
||||
const Render = () => {
|
||||
const mode = useWatch('mode');
|
||||
const isSetMode = mode === ModeValue.Set;
|
||||
|
||||
return (
|
||||
<NodeConfigForm nodeDisabled readonlyAllowDeleteOperation>
|
||||
<RadioSetterField
|
||||
name="mode"
|
||||
defaultValue={ModeValue.Set}
|
||||
options={{
|
||||
key: 'mode',
|
||||
mode: 'button',
|
||||
options: [
|
||||
{
|
||||
value: ModeValue.Set,
|
||||
label: I18n.t(
|
||||
'workflow_detail_variable_set_title',
|
||||
{},
|
||||
'设置变量值',
|
||||
),
|
||||
},
|
||||
{
|
||||
value: ModeValue.Get,
|
||||
label: I18n.t(
|
||||
'workflow_detail_variable_get_title',
|
||||
{},
|
||||
'获取变量值',
|
||||
),
|
||||
},
|
||||
],
|
||||
}}
|
||||
customReadonly
|
||||
/>
|
||||
<InputsParametersField
|
||||
name="inputParameters"
|
||||
tooltip={I18n.t(
|
||||
'workflow_detail_variable_subtitle',
|
||||
{},
|
||||
'用于在智能体中读取和写入变量,变量名必须与智能体中的变量名匹配。',
|
||||
)}
|
||||
nameProps={{ isPureText: !isSetMode, readonly: true }}
|
||||
customReadonly
|
||||
/>
|
||||
<OutputsField
|
||||
title={I18n.t('workflow_detail_node_output')}
|
||||
tooltip={I18n.t('workflow_detail_variable_set_output_tooltip')}
|
||||
id="variable-node-outputs"
|
||||
name="outputs"
|
||||
topLevelReadonly={true}
|
||||
customReadonly={isSetMode}
|
||||
/>
|
||||
</NodeConfigForm>
|
||||
);
|
||||
};
|
||||
|
||||
export default Render;
|
||||
@@ -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 { VARIABLE_NODE_REGISTRY } from './node-registry';
|
||||
export { VariableContent } from './node-content';
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 from 'react';
|
||||
|
||||
import { InputParameters, Outputs } from '../common/components';
|
||||
|
||||
export function VariableContent() {
|
||||
return (
|
||||
<>
|
||||
<InputParameters />
|
||||
<Outputs />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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,
|
||||
type WorkflowNodeRegistry,
|
||||
} from '@coze-workflow/nodes';
|
||||
import { StandardNodeType } from '@coze-workflow/base';
|
||||
|
||||
import { test, type NodeTestMeta } from './node-test';
|
||||
import { VARIABLE_NODE_FORM_META } from './form-meta';
|
||||
|
||||
export const VARIABLE_NODE_REGISTRY: WorkflowNodeRegistry<NodeTestMeta> = {
|
||||
type: StandardNodeType.Variable,
|
||||
meta: {
|
||||
nodeDTOType: StandardNodeType.Variable,
|
||||
headerReadonly: true,
|
||||
headerReadonlyAllowDeleteOperation: true,
|
||||
size: { width: DEFAULT_NODE_SIZE.width, height: 156.7 },
|
||||
nodeMetaPath: DEFAULT_NODE_META_PATH,
|
||||
outputsPath: DEFAULT_OUTPUTS_PATH,
|
||||
useDynamicPort: true,
|
||||
inputParametersPath: '/inputParameters',
|
||||
test,
|
||||
},
|
||||
variablesMeta: {
|
||||
outputsPathList: ['outputs'],
|
||||
inputsPathList: [],
|
||||
},
|
||||
formMeta: VARIABLE_NODE_FORM_META,
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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,
|
||||
generateEnvToRelatedContextProperties,
|
||||
} from '@/test-run-kit';
|
||||
import { type NodeTestMeta } from '@/test-run-kit';
|
||||
|
||||
export const test: NodeTestMeta = {
|
||||
generateRelatedContext(node, context) {
|
||||
const { isInProject } = context;
|
||||
if (isInProject) {
|
||||
return {};
|
||||
}
|
||||
return generateEnvToRelatedContextProperties({
|
||||
isNeedBot: true,
|
||||
hasVariableAssignNode: true,
|
||||
});
|
||||
},
|
||||
generateFormInputProperties(node) {
|
||||
const formData = node
|
||||
.getData(FlowNodeFormData)
|
||||
.formModel.getFormItemValueByPath('/');
|
||||
const inputParameter = formData?.inputParameters;
|
||||
return generateParametersToProperties(inputParameter, { node });
|
||||
},
|
||||
};
|
||||
export type { NodeTestMeta };
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 {
|
||||
InputValueVO,
|
||||
NodeDataDTO,
|
||||
OutputValueVO,
|
||||
} from '@coze-workflow/base';
|
||||
|
||||
export interface FormData {
|
||||
nodeMeta: NodeDataDTO['nodeMeta'];
|
||||
mode: 'set' | 'get';
|
||||
inputParameters: InputValueVO[];
|
||||
outputs: OutputValueVO[];
|
||||
}
|
||||
Reference in New Issue
Block a user