| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from copy import deepcopy |
| 4 |
from typing import TYPE_CHECKING, Any, List, Union, Generic, Optional |
| 5 |
from typing_extensions import Self, TypeVar, Annotated, TypeAlias, override |
| 6 |
|
| 7 |
from pydantic import Field as FieldInfo |
| 8 |
|
| 9 |
from .message import Message |
| 10 |
from .._compat import PYDANTIC_V1 |
| 11 |
from .._models import UnionDiscriminator |
| 12 |
from .text_block import TextBlock |
| 13 |
from .thinking_block import ThinkingBlock |
| 14 |
from .tool_use_block import ToolUseBlock |
| 15 |
from .server_tool_use_block import ServerToolUseBlock |
| 16 |
from .container_upload_block import ContainerUploadBlock |
| 17 |
from .redacted_thinking_block import RedactedThinkingBlock |
| 18 |
from .web_fetch_tool_result_block import WebFetchToolResultBlock |
| 19 |
from .web_search_tool_result_block import WebSearchToolResultBlock |
| 20 |
from .tool_search_tool_result_block import ToolSearchToolResultBlock |
| 21 |
from .code_execution_tool_result_block import CodeExecutionToolResultBlock |
| 22 |
from .bash_code_execution_tool_result_block import BashCodeExecutionToolResultBlock |
| 23 |
from .text_editor_code_execution_tool_result_block import TextEditorCodeExecutionToolResultBlock |
| 24 |
|
| 25 |
ResponseFormatT = TypeVar("ResponseFormatT", default=None) |
| 26 |
|
| 27 |
|
| 28 |
__all__ = [ |
| 29 |
"ParsedTextBlock", |
| 30 |
"ParsedContentBlock", |
| 31 |
"ParsedMessage", |
| 32 |
] |
| 33 |
|
| 34 |
|
| 35 |
class ParsedTextBlock(TextBlock, Generic[ResponseFormatT]): |
| 36 |
parsed_output: Optional[ResponseFormatT] = FieldInfo(default=None, exclude=True) |
| 37 |
|
| 38 |
__api_exclude__ = {"parsed_output"} |
| 39 |
|
| 40 |
if PYDANTIC_V1: |
| 41 |
# pydantic v1's copy() drops fields that are excluded from dumps, so carry the parsed value over |
| 42 |
@override |
| 43 |
def copy(self, **kwargs: Any) -> Self: |
| 44 |
copied = super().copy(**kwargs) # pyright: ignore[reportDeprecated] |
| 45 |
if "parsed_output" not in (kwargs.get("update") or {}): |
| 46 |
value = deepcopy(self.parsed_output) if kwargs.get("deep") else self.parsed_output |
| 47 |
object.__setattr__(copied, "parsed_output", value) |
| 48 |
return copied |
| 49 |
|
| 50 |
|
| 51 |
# Note that generic unions are not valid for pydantic at runtime |
| 52 |
ParsedContentBlock: TypeAlias = Annotated[ |
| 53 |
Union[ |
| 54 |
ParsedTextBlock[ResponseFormatT], |
| 55 |
ThinkingBlock, |
| 56 |
RedactedThinkingBlock, |
| 57 |
ToolUseBlock, |
| 58 |
ServerToolUseBlock, |
| 59 |
WebSearchToolResultBlock, |
| 60 |
WebFetchToolResultBlock, |
| 61 |
CodeExecutionToolResultBlock, |
| 62 |
BashCodeExecutionToolResultBlock, |
| 63 |
TextEditorCodeExecutionToolResultBlock, |
| 64 |
ToolSearchToolResultBlock, |
| 65 |
ContainerUploadBlock, |
| 66 |
], |
| 67 |
UnionDiscriminator("type"), |
| 68 |
] |
| 69 |
|
| 70 |
|
| 71 |
class ParsedMessage(Message, Generic[ResponseFormatT]): |
| 72 |
if TYPE_CHECKING: |
| 73 |
content: List[ParsedContentBlock[ResponseFormatT]] # type: ignore[assignment] |
| 74 |
else: |
| 75 |
content: List[ParsedContentBlock] |
| 76 |
|
| 77 |
@property |
| 78 |
def parsed_output(self) -> Optional[ResponseFormatT]: |
| 79 |
for content in self.content: |
| 80 |
if content.type == "text" and content.parsed_output: |
| 81 |
return content.parsed_output |
| 82 |
return None |