Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ def out_evaluation(self, variable, value):
else:
self.workflow_manage.out_context[variable['fields'][1]] = value

def convert(self, val, target_type):
if not target_type or val is None:
return val

if target_type == 'json_object':
if isinstance(val, dict) or isinstance(val, list):
return val
return json.loads(val)
elif target_type == 'json_string':
if isinstance(val, str):
return val
return json.dumps(val, ensure_ascii=False)
elif target_type == 'string':
if isinstance(val, str):
return val
return str(val)
elif target_type == 'int':
if isinstance(val, int):
return val
return int(val)
elif target_type == 'float':
if isinstance(val, float):
return val
return float(val)
elif target_type == 'boolean':
if isinstance(val, bool):
return val
return bool(val)
else:
return val

def handle(self, variable, evaluation):
result = {
'name': variable['name'],
Expand All @@ -49,44 +80,54 @@ def handle(self, variable, evaluation):
val = variable['value']
else:
val = json.loads(variable['value'])
val = self.convert(val, variable.get('target_type'))
evaluation(variable, val)
result['output_value'] = variable['value'] = val
elif variable['type'] == 'string':
# 变量解析 例如:{{global.xxx}}
val = self.workflow_manage.generate_prompt(variable['value'])
val = self.convert(val, variable.get('target_type'))
evaluation(variable, val)
result['output_value'] = val
else:
val = variable['value']
val = self.convert(val, variable.get('target_type'))
evaluation(variable, val)
result['output_value'] = val
elif variable['source'] == 'null':
val = None
evaluation(variable, val)
result['output_value'] = val
else:
reference = self.get_reference_content(variable['reference'])
reference = self.convert(reference, variable.get('target_type'))
evaluation(variable, reference)
result['output_value'] = reference
return result

def execute(self, variable_list, **kwargs) -> NodeResult:
#
result_list = []
is_chat = False
contains_chat_variable = False
for variable in variable_list:
if 'fields' not in variable:
continue

if 'global' == variable['fields'][0]:
result = self.handle(variable, self.global_evaluation)
result_list.append(result)
if 'chat' == variable['fields'][0]:
elif 'chat' == variable['fields'][0]:
result = self.handle(variable, self.chat_evaluation)
result_list.append(result)
is_chat = True
if 'loop' == variable['fields'][0]:
contains_chat_variable = True
elif 'loop' == variable['fields'][0]:
result = self.handle(variable, self.loop_evaluation)
result_list.append(result)
if 'output' == variable['fields'][0]:
elif 'output' == variable['fields'][0]:
result = self.handle(variable, self.out_evaluation)
result_list.append(result)
if is_chat:

if contains_chat_variable:
from application.flow.loop_workflow_manage import LoopWorkflowManage
if isinstance(self.workflow_manage, LoopWorkflowManage):
self.workflow_manage.parentWorkflowManage.get_chat_info().set_chat_variable(
Expand Down
2 changes: 2 additions & 0 deletions ui/src/locales/lang/en-US/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ You are a master of problem optimization, adept at accurately inferring user int
label: 'Variable Assign',
text: 'Update the value of the global variable',
assign: 'Set Value',
convertType: 'Convert type',
doNotConvert: 'Do not convert',
},
variableAggregationNode: {
label: 'Variable Aggregation',
Expand Down
2 changes: 2 additions & 0 deletions ui/src/locales/lang/zh-CN/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ export default {
label: '变量赋值',
text: '更新全局变量的值',
assign: '赋值',
convertType: '转换类型',
doNotConvert: '不转换',
},
mcpNode: {
label: 'MCP 调用',
Expand Down
2 changes: 2 additions & 0 deletions ui/src/locales/lang/zh-Hant/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ export default {
label: '變數賦值',
text: '更新全域變數的值',
assign: '賦值',
convertType: '轉換類型',
doNotConvert: '不轉換',
},
variableAggregationNode: {
label: '變量聚合',
Expand Down
8 changes: 7 additions & 1 deletion ui/src/workflow/nodes/variable-assign-node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ class VariableAssignNode extends AppNode {
}
}

class VariableAssignModel extends AppNodeModel {
get_width() {
return 450
}
}

export default {
type: 'variable-assign-node',
model: AppNodeModel,
model: VariableAssignModel,
view: VariableAssignNode
}
35 changes: 29 additions & 6 deletions ui/src/workflow/nodes/variable-assign-node/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<el-select :teleported="false" v-model="item.source" size="small" style="width: 85px">
<el-option :label="$t('workflow.variable.Referencing')" value="referencing" />
<el-option :label="$t('common.custom')" value="custom" />
<el-option label="null" value="null" />
</el-select>
</div>

Expand All @@ -67,6 +68,7 @@
</el-select>

<el-form-item
class="w-full"
v-if="item.type === 'string'"
:prop="'variable_list.' + index + '.value'"
:rules="{
Expand All @@ -76,6 +78,7 @@
}"
>
<el-input
class="w-full"
v-model="item.value"
:placeholder="$t('common.inputPlaceholder')"
show-word-limit
Expand Down Expand Up @@ -120,10 +123,8 @@
<CodemirrorEditor
title="JSON"
v-model="item.value"
:style="{
height: '100px',
width: '155px',
}"
class="w-full"
style="height: 100px"
@submitDialog="(val: string) => (form_data.variable_list[index].value = val)"
/>
</el-form-item>
Expand All @@ -141,15 +142,28 @@
<el-option label="false" :value="false" />
</el-select>
</el-form-item>

<el-select
v-if="item.type === 'string'"
v-model="item.target_type"
style="max-width: 120px; margin-left: 8px"
:placeholder="$t('workflow.nodes.variableAssignNode.convertType')"
>
<el-option v-for="item2 in targetTypeOptions" :key="item2.key" :label="item2.label" :value="item2.key" />
</el-select>
</div>
<el-form-item v-else>
<el-form-item v-else-if="item.source === 'referencing'">
<NodeCascader
ref="nodeCascaderRef2"
:nodeModel="nodeModel"
class="w-full"
style="width: 230px"
:placeholder="$t('workflow.variable.placeholder')"
v-model="item.reference"
/>

<el-select v-model="item.target_type" style="max-width: 120px; margin-left: 8px" :placeholder="$t('workflow.nodes.variableAssignNode.convertType')">
<el-option v-for="item2 in targetTypeOptions" :key="item2.key" :label="item2.label" :value="item2.key" />
</el-select>
</el-form-item>
</el-card>
</template>
Expand All @@ -174,6 +188,15 @@ const workflowMode = inject('workflowMode') as WorkflowMode
const props = defineProps<{ nodeModel: any }>()

const typeOptions = ['string', 'num', 'json', 'bool']
const targetTypeOptions = [
{ label: t('workflow.nodes.variableAssignNode.doNotConvert'), key: '' },
{ label: 'string', key: 'string' },
{ label: 'int', key: 'int' },
{ label: 'float', key: 'float' },
{ label: 'json_object', key: 'json_object' },
{ label: 'json_string', key: 'json_string' },
{ label: 'boolean', key: 'boolean' },
]

const wheel = (e: any) => {
if (e.ctrlKey === true) {
Expand Down
Loading