segments.py 2.9 KB

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