Coverage for src/fastoai/models/generated/thread.py: 75%

1 from datetime import datetime

2 from typing import TYPE_CHECKING, Annotated

3

4 from openai.types.beta.thread import Thread as _Thread

5 from openai.types.beta.thread import ToolResources

6 from pydantic import field_serializer

7 from sqlalchemy.ext.asyncio import AsyncAttrs

8 from sqlmodel import Field, Relationship

9

10 from .._metadata import WithMetadata

11 from .._types import as_sa_type

12 from .._utils import now, random_id_with_prefix

13

14 if TYPE_CHECKING:

15 from .message import Message

16 from .run import Run

17 from .run_step import RunStep

18

19 class Thread(AsyncAttrs, WithMetadata, table=True):

20 id: Annotated[str, Field(primary_key=True, default_factory=random_id_with_prefix('thread_'))]

21 created_at: Annotated[datetime, Field(default_factory=now)]

22 tool_resources: Annotated[ToolResources | None, Field(sa_type=as_sa_type(ToolResources), nullable=True)] = None

23

24 async def to_openai_model(self) -> _Thread:

25 value = self.model_dump(by_alias=True)

26 value['object'] = 'thread'

27 return _Thread.model_validate(value)

28

29 @field_serializer('created_at')

30 def serialize_datetime(self, dt: datetime) -> int:

31 return int(dt.timestamp())

32 messages: list['Message'] = Relationship(back_populates='thread')

33 runs: list['Run'] = Relationship(back_populates='thread')

34 steps: list['RunStep'] = Relationship(back_populates='thread')