| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | from collections.abc import Mapping, Sequence | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | from typing import Any | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from pydantic import BaseModel, ConfigDict, field_validator | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from core.file import File | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | from .types import SegmentType | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Segment(BaseModel): | 
					
						
							|  |  |  |     model_config = ConfigDict(frozen=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     value_type: SegmentType | 
					
						
							|  |  |  |     value: Any | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     @field_validator("value_type") | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     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. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         if value != cls.model_fields["value_type"].default: | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |             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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def size(self) -> int: | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Return the size of the value in bytes. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |         return sys.getsizeof(self.value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 16:22:06 +08:00
										 |  |  |     def to_object(self) -> Any: | 
					
						
							|  |  |  |         return self.value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:32 +08:00
										 |  |  | class NoneSegment(Segment): | 
					
						
							|  |  |  |     value_type: SegmentType = SegmentType.NONE | 
					
						
							|  |  |  |     value: None = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def text(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-10-22 17:52:22 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def log(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-10-22 17:52:22 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def markdown(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-10-22 17:52:22 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2024-07-23 17:59:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | class StringSegment(Segment): | 
					
						
							|  |  |  |     value_type: SegmentType = SegmentType.STRING | 
					
						
							|  |  |  |     value: str | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-27 14:43:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2024-08-07 16:20:22 +08:00
										 |  |  |     value: Mapping[str, Any] | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def text(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         return json.dumps(self.model_dump()["value"], ensure_ascii=False) | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def log(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2) | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def markdown(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2) | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ArraySegment(Segment): | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def markdown(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-08-15 13:09:49 +08:00
										 |  |  |         items = [] | 
					
						
							|  |  |  |         for item in self.value: | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             items.append(str(item)) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         return "\n".join(items) | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | class FileSegment(Segment): | 
					
						
							|  |  |  |     value_type: SegmentType = SegmentType.FILE | 
					
						
							|  |  |  |     value: File | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def markdown(self) -> str: | 
					
						
							|  |  |  |         return self.value.markdown | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def log(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-11-22 16:30:22 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def text(self) -> str: | 
					
						
							| 
									
										
										
										
											2024-11-22 16:30:22 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-27 14:43:51 +08:00
										 |  |  | class ArrayAnySegment(ArraySegment): | 
					
						
							|  |  |  |     value_type: SegmentType = SegmentType.ARRAY_ANY | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     value: Sequence[Any] | 
					
						
							| 
									
										
										
										
											2024-07-26 15:03:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-27 14:43:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ArrayStringSegment(ArraySegment): | 
					
						
							|  |  |  |     value_type: SegmentType = SegmentType.ARRAY_STRING | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     value: Sequence[str] | 
					
						
							| 
									
										
										
										
											2024-07-27 14:43:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-21 10:38:44 +08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def text(self) -> str: | 
					
						
							| 
									
										
										
										
											2025-03-10 09:22:41 +08:00
										 |  |  |         return json.dumps(self.value, ensure_ascii=False) | 
					
						
							| 
									
										
										
										
											2025-01-21 10:38:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-27 14:43:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ArrayNumberSegment(ArraySegment): | 
					
						
							|  |  |  |     value_type: SegmentType = SegmentType.ARRAY_NUMBER | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     value: Sequence[float | int] | 
					
						
							| 
									
										
										
										
											2024-07-27 14:43:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ArrayObjectSegment(ArraySegment): | 
					
						
							|  |  |  |     value_type: SegmentType = SegmentType.ARRAY_OBJECT | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     value: Sequence[Mapping[str, Any]] | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							| 
									
										
										
										
											2024-11-22 16:30:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def log(self) -> str: | 
					
						
							|  |  |  |         return "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def text(self) -> str: | 
					
						
							|  |  |  |         return "" |