浏览代码

fix(segments): Support NoneType. (#6581)

tags/0.6.15
-LAN- 1年前
父节点
当前提交
2bc0632d0d
没有帐户链接到提交者的电子邮件

+ 5
- 2
api/core/app/segments/__init__.py 查看文件

from .segment_group import SegmentGroup from .segment_group import SegmentGroup
from .segments import Segment
from .segments import NoneSegment, Segment
from .types import SegmentType from .types import SegmentType
from .variables import ( from .variables import (
ArrayVariable, ArrayVariable,
FileVariable, FileVariable,
FloatVariable, FloatVariable,
IntegerVariable, IntegerVariable,
NoneVariable,
ObjectVariable, ObjectVariable,
SecretVariable, SecretVariable,
StringVariable, StringVariable,
'Variable', 'Variable',
'SegmentType', 'SegmentType',
'SegmentGroup', 'SegmentGroup',
'Segment'
'Segment',
'NoneSegment',
'NoneVariable',
] ]

+ 3
- 0
api/core/app/segments/factory.py 查看文件

FileVariable, FileVariable,
FloatVariable, FloatVariable,
IntegerVariable, IntegerVariable,
NoneVariable,
ObjectVariable, ObjectVariable,
SecretVariable, SecretVariable,
StringVariable, StringVariable,




def build_anonymous_variable(value: Any, /) -> Variable: def build_anonymous_variable(value: Any, /) -> Variable:
if value is None:
return NoneVariable(name='anonymous')
if isinstance(value, str): if isinstance(value, str):
return StringVariable(name='anonymous', value=value) return StringVariable(name='anonymous', value=value)
if isinstance(value, int): if isinstance(value, int):

+ 17
- 0
api/core/app/segments/segments.py 查看文件

return self.value return self.value




class NoneSegment(Segment):
value_type: SegmentType = SegmentType.NONE
value: None = None

@property
def text(self) -> str:
return 'null'

@property
def log(self) -> str:
return 'null'

@property
def markdown(self) -> str:
return 'null'


class StringSegment(Segment): class StringSegment(Segment):
value_type: SegmentType = SegmentType.STRING value_type: SegmentType = SegmentType.STRING
value: str value: str

+ 2
- 1
api/core/app/segments/types.py 查看文件

ARRAY_STRING = 'array[string]' ARRAY_STRING = 'array[string]'
ARRAY_NUMBER = 'array[number]' ARRAY_NUMBER = 'array[number]'
ARRAY_OBJECT = 'array[object]' ARRAY_OBJECT = 'array[object]'
ARRAY_FILE = 'array[file]'
ARRAY_FILE = 'array[file]'
NONE = 'none'

+ 5
- 0
api/core/app/segments/variables.py 查看文件

@property @property
def log(self) -> str: def log(self) -> str:
return encrypter.obfuscated_token(self.value) return encrypter.obfuscated_token(self.value)


class NoneVariable(Variable):
value_type: SegmentType = SegmentType.NONE
value: None = None

+ 13
- 1
api/tests/unit_tests/app/test_variables.py 查看文件

from pydantic import ValidationError from pydantic import ValidationError


from core.app.segments import ( from core.app.segments import (
ArrayVariable,
FloatVariable, FloatVariable,
IntegerVariable, IntegerVariable,
NoneVariable,
ObjectVariable,
SecretVariable, SecretVariable,
SegmentType, SegmentType,
StringVariable, StringVariable,
factory, factory,
) )
from core.app.segments.variables import ArrayVariable, ObjectVariable




def test_string_variable(): def test_string_variable():
assert var.to_object() == 3.14 assert var.to_object() == 3.14
var = SecretVariable(name='secret', value='secret_value') var = SecretVariable(name='secret', value='secret_value')
assert var.to_object() == 'secret_value' assert var.to_object() == 'secret_value'


def test_build_a_object_variable_with_none_value():
var = factory.build_anonymous_variable(
{
'key1': None,
}
)
assert isinstance(var, ObjectVariable)
assert isinstance(var.value['key1'], NoneVariable)

正在加载...
取消
保存