
Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in>
170 lines
3.6 KiB
Python
170 lines
3.6 KiB
Python
import json
|
|
import sys
|
|
from collections.abc import Mapping, Sequence
|
|
from typing import Any
|
|
|
|
from pydantic import BaseModel, ConfigDict, field_validator
|
|
|
|
from core.file import File
|
|
|
|
from .types import SegmentType
|
|
|
|
|
|
class Segment(BaseModel):
|
|
model_config = ConfigDict(frozen=True)
|
|
|
|
value_type: SegmentType
|
|
value: Any
|
|
|
|
@field_validator("value_type")
|
|
@classmethod
|
|
def validate_value_type(cls, value):
|
|
"""
|
|
This validator checks if the provided value is equal to the default value of the 'value_type' field.
|
|
If the value is different, a ValueError is raised.
|
|
"""
|
|
if value != cls.model_fields["value_type"].default:
|
|
raise ValueError("Cannot modify 'value_type'")
|
|
return value
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return str(self.value)
|
|
|
|
@property
|
|
def log(self) -> str:
|
|
return str(self.value)
|
|
|
|
@property
|
|
def markdown(self) -> str:
|
|
return str(self.value)
|
|
|
|
@property
|
|
def size(self) -> int:
|
|
"""
|
|
Return the size of the value in bytes.
|
|
"""
|
|
return sys.getsizeof(self.value)
|
|
|
|
def to_object(self) -> Any:
|
|
return self.value
|
|
|
|
|
|
class NoneSegment(Segment):
|
|
value_type: SegmentType = SegmentType.NONE
|
|
value: None = None
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return ""
|
|
|
|
@property
|
|
def log(self) -> str:
|
|
return ""
|
|
|
|
@property
|
|
def markdown(self) -> str:
|
|
return ""
|
|
|
|
|
|
class StringSegment(Segment):
|
|
value_type: SegmentType = SegmentType.STRING
|
|
value: str
|
|
|
|
|
|
class FloatSegment(Segment):
|
|
value_type: SegmentType = SegmentType.NUMBER
|
|
value: float
|
|
|
|
|
|
class IntegerSegment(Segment):
|
|
value_type: SegmentType = SegmentType.NUMBER
|
|
value: int
|
|
|
|
|
|
class ObjectSegment(Segment):
|
|
value_type: SegmentType = SegmentType.OBJECT
|
|
value: Mapping[str, Any]
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return json.dumps(self.model_dump()["value"], ensure_ascii=False)
|
|
|
|
@property
|
|
def log(self) -> str:
|
|
return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2)
|
|
|
|
@property
|
|
def markdown(self) -> str:
|
|
return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2)
|
|
|
|
|
|
class ArraySegment(Segment):
|
|
@property
|
|
def markdown(self) -> str:
|
|
items = []
|
|
for item in self.value:
|
|
items.append(str(item))
|
|
return "\n".join(items)
|
|
|
|
|
|
class FileSegment(Segment):
|
|
value_type: SegmentType = SegmentType.FILE
|
|
value: File
|
|
|
|
@property
|
|
def markdown(self) -> str:
|
|
return self.value.markdown
|
|
|
|
@property
|
|
def log(self) -> str:
|
|
return ""
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return ""
|
|
|
|
|
|
class ArrayAnySegment(ArraySegment):
|
|
value_type: SegmentType = SegmentType.ARRAY_ANY
|
|
value: Sequence[Any]
|
|
|
|
|
|
class ArrayStringSegment(ArraySegment):
|
|
value_type: SegmentType = SegmentType.ARRAY_STRING
|
|
value: Sequence[str]
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return json.dumps(self.value)
|
|
|
|
|
|
class ArrayNumberSegment(ArraySegment):
|
|
value_type: SegmentType = SegmentType.ARRAY_NUMBER
|
|
value: Sequence[float | int]
|
|
|
|
|
|
class ArrayObjectSegment(ArraySegment):
|
|
value_type: SegmentType = SegmentType.ARRAY_OBJECT
|
|
value: Sequence[Mapping[str, Any]]
|
|
|
|
|
|
class ArrayFileSegment(ArraySegment):
|
|
value_type: SegmentType = SegmentType.ARRAY_FILE
|
|
value: Sequence[File]
|
|
|
|
@property
|
|
def markdown(self) -> str:
|
|
items = []
|
|
for item in self.value:
|
|
items.append(item.markdown)
|
|
return "\n".join(items)
|
|
|
|
@property
|
|
def log(self) -> str:
|
|
return ""
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return ""
|