Skip to content

Commit 5199ab8

Browse files
committed
feat: Support variable assign
1 parent 6e49901 commit 5199ab8

File tree

13 files changed

+358
-4
lines changed

13 files changed

+358
-4
lines changed

apps/application/flow/step_node/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
from .speech_to_text_step_node import BaseSpeechToTextNode
2525
from .start_node import *
2626
from .text_to_speech_step_node.impl.base_text_to_speech_node import BaseTextToSpeechNode
27+
from .variable_assign import BaseVariableAssignNode
2728

28-
node_list = [BaseStartStepNode, BaseChatNode, BaseSearchDatasetNode, BaseQuestionNode, BaseConditionNode, BaseReplyNode,
29+
node_list = [BaseStartStepNode, BaseChatNode, BaseSearchDatasetNode, BaseQuestionNode,
30+
BaseConditionNode, BaseReplyNode,
2931
BaseFunctionNodeNode, BaseFunctionLibNodeNode, BaseRerankerNode, BaseApplicationNode,
3032
BaseDocumentExtractNode,
31-
BaseImageUnderstandNode, BaseFormNode, BaseSpeechToTextNode, BaseTextToSpeechNode,BaseImageGenerateNode]
33+
BaseImageUnderstandNode, BaseFormNode, BaseSpeechToTextNode, BaseTextToSpeechNode,
34+
BaseImageGenerateNode, BaseVariableAssignNode]
3235

3336

3437
def get_node(node_type):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# coding=utf-8
2+
3+
from .impl import *
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# coding=utf-8
2+
3+
from typing import Type
4+
5+
from django.utils.translation import gettext_lazy as _
6+
from rest_framework import serializers
7+
8+
from application.flow.i_step_node import INode, NodeResult
9+
from common.util.field_message import ErrMessage
10+
11+
12+
class VariableAssignNodeParamsSerializer(serializers.Serializer):
13+
variable_list = serializers.ListField(required=True,
14+
error_messages=ErrMessage.list(_("Reference Field")))
15+
16+
17+
class IVariableAssignNode(INode):
18+
type = 'variable-assign-node'
19+
20+
def get_node_params_serializer_class(self) -> Type[serializers.Serializer]:
21+
return VariableAssignNodeParamsSerializer
22+
23+
def _run(self):
24+
return self.execute(**self.node_params_serializer.data, **self.flow_params_serializer.data)
25+
26+
def execute(self, variable_list, stream, **kwargs) -> NodeResult:
27+
pass
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# coding=utf-8
2+
"""
3+
@project: maxkb
4+
@Author:虎
5+
@file: __init__.py
6+
@date:2024/6/11 17:49
7+
@desc:
8+
"""
9+
from .base_variable_assign_node import *
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# coding=utf-8
2+
from typing import List
3+
4+
from application.flow.i_step_node import NodeResult
5+
from application.flow.step_node.variable_assign.i_variable_assign_node import IVariableAssignNode
6+
7+
8+
class BaseVariableAssignNode(IVariableAssignNode):
9+
def save_context(self, details, workflow_manage):
10+
self.context['variable_list'] = details.get('variable_list')
11+
12+
def execute(self, variable_list, stream, **kwargs) -> NodeResult:
13+
#
14+
for variable in variable_list:
15+
if 'fields' not in variable:
16+
continue
17+
if 'global' == variable['fields'][0]:
18+
if variable['source'] == 'custom':
19+
self.workflow_manage.context[variable['fields'][1]] = variable['value']
20+
else:
21+
reference = self.get_reference_content(variable['reference'])
22+
self.workflow_manage.context[variable['fields'][1]] = reference
23+
# print('variable_list:', variable_list)
24+
25+
return NodeResult({'variable_list': variable_list}, {})
26+
27+
def get_reference_content(self, fields: List[str]):
28+
return str(self.workflow_manage.get_reference_field(
29+
fields[0],
30+
fields[1:]))
31+
32+
def get_details(self, index: int, **kwargs):
33+
return {
34+
'name': self.node.properties.get('stepName'),
35+
"index": index,
36+
'run_time': self.context.get('run_time'),
37+
'type': self.node.type,
38+
'variable_list': self.context.get('variable_list'),
39+
'status': self.status,
40+
'err_message': self.err_message
41+
}

ui/src/enums/workflow.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export enum WorkflowType {
1212
Application = 'application-node',
1313
DocumentExtractNode = 'document-extract-node',
1414
ImageUnderstandNode = 'image-understand-node',
15+
VariableAssignNode = 'variable-assign-node',
1516
FormNode = 'form-node',
1617
TextToSpeechNode = 'text-to-speech-node',
1718
SpeechToTextNode = 'speech-to-text-node',

ui/src/locales/lang/en-US/views/application-workflow.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ export default {
212212
requiredMessage: 'Please select an image'
213213
}
214214
},
215+
variableAssignNode: {
216+
label: 'Variable assign',
217+
text: 'Update the value of the global variable',
218+
variable: {
219+
label: 'select variable',
220+
requiredMessage: 'Please select a variable'
221+
},
222+
value: {
223+
label: 'Set value',
224+
},
225+
source: {
226+
reference: 'Reference Variable',
227+
custom: 'Custom'
228+
},
229+
addVariable: 'Add Variable',
230+
},
215231
imageGenerateNode: {
216232
label: 'Image Generation',
217233
text: 'Generate images based on provided text content',

ui/src/locales/lang/zh-CN/views/application-workflow.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@ export default {
211211
requiredMessage: '请选择图片'
212212
}
213213
},
214+
variableAssignNode: {
215+
label: '变量赋值',
216+
text: '更新全局变量的值',
217+
variable: {
218+
label: '选择变量',
219+
requiredMessage: '请选择变量'
220+
},
221+
value: {
222+
label: '赋值',
223+
},
224+
source: {
225+
reference: '引用变量',
226+
custom: '自定义'
227+
},
228+
addVariable: '添加变量'
229+
},
214230
imageGenerateNode: {
215231
label: '图片生成',
216232
text: '根据提供的文本内容生成图片',

ui/src/locales/lang/zh-Hant/views/application-workflow.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ export default {
210210
requiredMessage: '請選擇圖片'
211211
}
212212
},
213+
variableAssignNode: {
214+
label: '變數賦值',
215+
text: '更新全域變數的值',
216+
variable: {
217+
label: '選擇變數',
218+
requiredMessage: '請選擇變數'
219+
},
220+
value: {
221+
label: '設置值',
222+
},
223+
source: {
224+
reference: '引用變數',
225+
custom: '自定義'
226+
},
227+
addVariable: '添加變數',
228+
},
213229
imageGenerateNode: {
214230
label: '圖片生成',
215231
text: '根據提供的文本內容生成圖片',

ui/src/workflow/common/data.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@ export const imageUnderstandNode = {
239239
}
240240
}
241241

242+
export const variableAssignNode = {
243+
type: WorkflowType.VariableAssignNode,
244+
text: t('views.applicationWorkflow.nodes.variableAssignNode.text'),
245+
label: t('views.applicationWorkflow.nodes.variableAssignNode.label'),
246+
height: 252,
247+
properties: {
248+
stepName: t('views.applicationWorkflow.nodes.variableAssignNode.label'),
249+
config: {
250+
251+
}
252+
}
253+
}
254+
242255
export const imageGenerateNode = {
243256
type: WorkflowType.ImageGenerateNode,
244257
text: t('views.applicationWorkflow.nodes.imageGenerateNode.text'),
@@ -307,7 +320,8 @@ export const menuNodes = [
307320
questionNode,
308321
documentExtractNode,
309322
speechToTextNode,
310-
textToSpeechNode
323+
textToSpeechNode,
324+
variableAssignNode
311325
]
312326

313327
/**
@@ -400,7 +414,8 @@ export const nodeDict: any = {
400414
[WorkflowType.ImageUnderstandNode]: imageUnderstandNode,
401415
[WorkflowType.TextToSpeechNode]: textToSpeechNode,
402416
[WorkflowType.SpeechToTextNode]: speechToTextNode,
403-
[WorkflowType.ImageGenerateNode]: imageGenerateNode
417+
[WorkflowType.ImageGenerateNode]: imageGenerateNode,
418+
[WorkflowType.VariableAssignNode]: variableAssignNode,
404419
}
405420
export function isWorkFlow(type: string | undefined) {
406421
return type === 'WORK_FLOW'

0 commit comments

Comments
 (0)