segments.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import json
  2. import sys
  3. from collections.abc import Mapping, Sequence
  4. from typing import Any
  5. from pydantic import BaseModel, ConfigDict, field_validator
  6. from .types import SegmentType
  7. class Segment(BaseModel):
  8. model_config = ConfigDict(frozen=True)
  9. value_type: SegmentType
  10. value: Any
  11. @field_validator("value_type")
  12. @classmethod
  13. def validate_value_type(cls, value):
  14. """
  15. This validator checks if the provided value is equal to the default value of the 'value_type' field.
  16. If the value is different, a ValueError is raised.
  17. """
  18. if value != cls.model_fields["value_type"].default:
  19. raise ValueError("Cannot modify 'value_type'")
  20. return value
  21. @property
  22. def text(self) -> str:
  23. return str(self.value)
  24. @property
  25. def log(self) -> str:
  26. return str(self.value)
  27. @property
  28. def markdown(self) -> str:
  29. return str(self.value)
  30. @property
  31. def size(self) -> int:
  32. return sys.getsizeof(self.value)
  33. def to_object(self) -> Any:
  34. return self.value
  35. class NoneSegment(Segment):
  36. value_type: SegmentType = SegmentType.NONE
  37. value: None = None
  38. @property
  39. def text(self) -> str:
  40. return "null"
  41. @property
  42. def log(self) -> str:
  43. return "null"
  44. @property
  45. def markdown(self) -> str:
  46. return "null"
  47. class StringSegment(Segment):
  48. value_type: SegmentType = SegmentType.STRING
  49. value: str
  50. class FloatSegment(Segment):
  51. value_type: SegmentType = SegmentType.NUMBER
  52. value: float
  53. class IntegerSegment(Segment):
  54. value_type: SegmentType = SegmentType.NUMBER
  55. value: int
  56. class ObjectSegment(Segment):
  57. value_type: SegmentType = SegmentType.OBJECT
  58. value: Mapping[str, Any]
  59. @property
  60. def text(self) -> str:
  61. return json.dumps(self.model_dump()["value"], ensure_ascii=False)
  62. @property
  63. def log(self) -> str:
  64. return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2)
  65. @property
  66. def markdown(self) -> str:
  67. return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2)
  68. class ArraySegment(Segment):
  69. @property
  70. def markdown(self) -> str:
  71. items = []
  72. for item in self.value:
  73. if hasattr(item, "to_markdown"):
  74. items.append(item.to_markdown())
  75. else:
  76. items.append(str(item))
  77. return "\n".join(items)
  78. class ArrayAnySegment(ArraySegment):
  79. value_type: SegmentType = SegmentType.ARRAY_ANY
  80. value: Sequence[Any]
  81. class ArrayStringSegment(ArraySegment):
  82. value_type: SegmentType = SegmentType.ARRAY_STRING
  83. value: Sequence[str]
  84. class ArrayNumberSegment(ArraySegment):
  85. value_type: SegmentType = SegmentType.ARRAY_NUMBER
  86. value: Sequence[float | int]
  87. class ArrayObjectSegment(ArraySegment):
  88. value_type: SegmentType = SegmentType.ARRAY_OBJECT
  89. value: Sequence[Mapping[str, Any]]