mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 11:42:29 +08:00
fix(segments): Support NoneType. (#6581)
This commit is contained in:
parent
75445a0c66
commit
2bc0632d0d
|
@ -1,11 +1,12 @@
|
|||
from .segment_group import SegmentGroup
|
||||
from .segments import Segment
|
||||
from .segments import NoneSegment, Segment
|
||||
from .types import SegmentType
|
||||
from .variables import (
|
||||
ArrayVariable,
|
||||
FileVariable,
|
||||
FloatVariable,
|
||||
IntegerVariable,
|
||||
NoneVariable,
|
||||
ObjectVariable,
|
||||
SecretVariable,
|
||||
StringVariable,
|
||||
|
@ -23,5 +24,7 @@ __all__ = [
|
|||
'Variable',
|
||||
'SegmentType',
|
||||
'SegmentGroup',
|
||||
'Segment'
|
||||
'Segment',
|
||||
'NoneSegment',
|
||||
'NoneVariable',
|
||||
]
|
||||
|
|
|
@ -10,6 +10,7 @@ from .variables import (
|
|||
FileVariable,
|
||||
FloatVariable,
|
||||
IntegerVariable,
|
||||
NoneVariable,
|
||||
ObjectVariable,
|
||||
SecretVariable,
|
||||
StringVariable,
|
||||
|
@ -39,6 +40,8 @@ def build_variable_from_mapping(m: Mapping[str, Any], /) -> Variable:
|
|||
|
||||
|
||||
def build_anonymous_variable(value: Any, /) -> Variable:
|
||||
if value is None:
|
||||
return NoneVariable(name='anonymous')
|
||||
if isinstance(value, str):
|
||||
return StringVariable(name='anonymous', value=value)
|
||||
if isinstance(value, int):
|
||||
|
|
|
@ -43,6 +43,23 @@ class Segment(BaseModel):
|
|||
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):
|
||||
value_type: SegmentType = SegmentType.STRING
|
||||
value: str
|
||||
|
|
|
@ -14,4 +14,5 @@ class SegmentType(str, Enum):
|
|||
ARRAY_STRING = 'array[string]'
|
||||
ARRAY_NUMBER = 'array[number]'
|
||||
ARRAY_OBJECT = 'array[object]'
|
||||
ARRAY_FILE = 'array[file]'
|
||||
ARRAY_FILE = 'array[file]'
|
||||
NONE = 'none'
|
|
@ -81,3 +81,8 @@ class SecretVariable(StringVariable):
|
|||
@property
|
||||
def log(self) -> str:
|
||||
return encrypter.obfuscated_token(self.value)
|
||||
|
||||
|
||||
class NoneVariable(Variable):
|
||||
value_type: SegmentType = SegmentType.NONE
|
||||
value: None = None
|
|
@ -2,14 +2,16 @@ import pytest
|
|||
from pydantic import ValidationError
|
||||
|
||||
from core.app.segments import (
|
||||
ArrayVariable,
|
||||
FloatVariable,
|
||||
IntegerVariable,
|
||||
NoneVariable,
|
||||
ObjectVariable,
|
||||
SecretVariable,
|
||||
SegmentType,
|
||||
StringVariable,
|
||||
factory,
|
||||
)
|
||||
from core.app.segments.variables import ArrayVariable, ObjectVariable
|
||||
|
||||
|
||||
def test_string_variable():
|
||||
|
@ -134,3 +136,13 @@ def test_variable_to_object():
|
|||
assert var.to_object() == 3.14
|
||||
var = SecretVariable(name='secret', value='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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user