1
1
import ast
2
2
import dataclasses
3
- from typing import Dict , List , Union
3
+ from typing import Any , Dict , List , Union
4
4
5
5
from . import enums
6
6
from .abstract import ApiClass , get_clean_function_source_code_for_agent , validate_constructor_arg_types
@@ -67,14 +67,16 @@ class WorkflowNodeInputMapping(ApiClass):
67
67
variable_source : str = dataclasses .field (default = None )
68
68
source_prop : str = dataclasses .field (default = None )
69
69
is_required : bool = dataclasses .field (default = True )
70
+ default_value : Any = dataclasses .field (default = None )
70
71
71
72
def to_dict (self ):
72
73
return {
73
74
'name' : self .name ,
74
75
'variable_type' : self .variable_type .value ,
75
76
'variable_source' : self .variable_source ,
76
77
'source_prop' : self .source_prop or self .name ,
77
- 'is_required' : self .is_required
78
+ 'is_required' : self .is_required ,
79
+ 'default_value' : self .default_value
78
80
}
79
81
80
82
@classmethod
@@ -87,7 +89,8 @@ def from_dict(cls, mapping: dict):
87
89
variable_type = enums .WorkflowNodeInputType (mapping ['variable_type' ]),
88
90
variable_source = mapping .get ('variable_source' ),
89
91
source_prop = mapping .get ('source_prop' ) or mapping ['name' ] if mapping .get ('variable_source' ) else None ,
90
- is_required = mapping .get ('is_required' , True )
92
+ is_required = mapping .get ('is_required' , True ),
93
+ default_value = mapping .get ('default_value' )
91
94
)
92
95
93
96
@@ -283,14 +286,14 @@ def __init__(self, name: str, input_mappings: Union[Dict[str, WorkflowNodeInputM
283
286
raise ValueError ('workflow_graph_node' , f'Invalid input mapping. Argument "{ input_name } " not found in function "{ self .function_name } ".' )
284
287
for arg , default in arg_defaults .items ():
285
288
if arg not in input_mapping_args :
286
- self .input_mappings .append (WorkflowNodeInputMapping (name = arg , variable_type = enums .WorkflowNodeInputType .USER_INPUT , is_required = default is None ))
289
+ self .input_mappings .append (WorkflowNodeInputMapping (name = arg , variable_type = enums .WorkflowNodeInputType .USER_INPUT , is_required = default is None , default_value = default . value if default else None ))
287
290
elif isinstance (input_mappings , Dict ) and all (isinstance (key , str ) and isinstance (value , WorkflowNodeInputMapping ) for key , value in input_mappings .items ()):
288
291
is_shortform_input_mappings = True
289
- self .input_mappings = [WorkflowNodeInputMapping (name = arg , variable_type = enums .WorkflowNodeInputType .USER_INPUT , is_required = default is None ) for arg , default in arg_defaults .items () if arg not in input_mappings ]
292
+ self .input_mappings = [WorkflowNodeInputMapping (name = arg , variable_type = enums .WorkflowNodeInputType .USER_INPUT , is_required = default is None , default_value = default . value if default else None ) for arg , default in arg_defaults .items () if arg not in input_mappings ]
290
293
for key , value in input_mappings .items ():
291
294
if key not in arg_defaults :
292
295
raise ValueError ('workflow_graph_node' , f'Invalid input mapping. Argument "{ key } " not found in function "{ self .function_name } ".' )
293
- self .input_mappings .append (WorkflowNodeInputMapping (name = key , variable_type = value .variable_type , variable_source = value .variable_source , source_prop = value .source_prop , is_required = arg_defaults .get (key ) is None ))
296
+ self .input_mappings .append (WorkflowNodeInputMapping (name = key , variable_type = value .variable_type , variable_source = value .variable_source , source_prop = value .source_prop , is_required = arg_defaults .get (key ) is None , default_value = value . default_value ))
294
297
else :
295
298
raise ValueError ('workflow_graph_node' , 'Invalid input mappings. Must be a list of WorkflowNodeInputMapping or a dictionary of input mappings in the form {arg_name: node_name.outputs.prop_name}.' )
296
299
@@ -359,7 +362,7 @@ def from_template(cls, template_name: str, name: str, configs: dict = None, inpu
359
362
if isinstance (input_mappings , List ) and all (isinstance (input , WorkflowNodeInputMapping ) for input in input_mappings ):
360
363
instance_input_mappings = input_mappings
361
364
elif isinstance (input_mappings , Dict ) and all (isinstance (key , str ) and isinstance (value , WorkflowNodeInputMapping ) for key , value in input_mappings .items ()):
362
- instance_input_mappings = [WorkflowNodeInputMapping (name = arg , variable_type = mapping .variable_type , variable_source = mapping .variable_source , source_prop = mapping .source_prop ) for arg , mapping in input_mappings ]
365
+ instance_input_mappings = [WorkflowNodeInputMapping (name = arg , variable_type = mapping .variable_type , variable_source = mapping .variable_source , source_prop = mapping .source_prop , is_required = mapping . is_required , default_value = mapping . default_value ) for arg , mapping in input_mappings ]
363
366
elif input_mappings is None :
364
367
instance_input_mappings = []
365
368
else :
0 commit comments