# This file was auto-generated by Fern from our API Definition.

import typing

from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.request_options import RequestOptions
from ...types.letta_message_union import LettaMessageUnion
from ...types.message_role import MessageRole
from .raw_client import AsyncRawMessagesClient, RawMessagesClient


class MessagesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawMessagesClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawMessagesClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawMessagesClient
        """
        return self._raw_client

    def list(
        self,
        run_id: str,
        *,
        before: typing.Optional[str] = None,
        after: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        order: typing.Optional[str] = None,
        role: typing.Optional[MessageRole] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[LettaMessageUnion]:
        """
        Get messages associated with a run with filtering options.

        Args:
            run_id: ID of the run
            before: A cursor for use in pagination. `before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, starting with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
            after: A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.
            limit: Maximum number of messages to return
            order: Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.
            role: Filter by role (user/assistant/system/tool)
            return_message_object: Whether to return Message objects or LettaMessage objects
            user_id: ID of the user making the request

        Returns:
            A list of messages associated with the run. Default is List[LettaMessage].

        Parameters
        ----------
        run_id : str

        before : typing.Optional[str]
            Cursor for pagination

        after : typing.Optional[str]
            Cursor for pagination

        limit : typing.Optional[int]
            Maximum number of messages to return

        order : typing.Optional[str]
            Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.

        role : typing.Optional[MessageRole]
            Filter by role

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.List[LettaMessageUnion]
            Successful Response

        Examples
        --------
        from letta_client import Letta

        client = Letta(
            project="YOUR_PROJECT",
            token="YOUR_TOKEN",
        )
        client.runs.messages.list(
            run_id="run_id",
        )
        """
        _response = self._raw_client.list(
            run_id, before=before, after=after, limit=limit, order=order, role=role, request_options=request_options
        )
        return _response.data


class AsyncMessagesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawMessagesClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawMessagesClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawMessagesClient
        """
        return self._raw_client

    async def list(
        self,
        run_id: str,
        *,
        before: typing.Optional[str] = None,
        after: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        order: typing.Optional[str] = None,
        role: typing.Optional[MessageRole] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[LettaMessageUnion]:
        """
        Get messages associated with a run with filtering options.

        Args:
            run_id: ID of the run
            before: A cursor for use in pagination. `before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, starting with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
            after: A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.
            limit: Maximum number of messages to return
            order: Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.
            role: Filter by role (user/assistant/system/tool)
            return_message_object: Whether to return Message objects or LettaMessage objects
            user_id: ID of the user making the request

        Returns:
            A list of messages associated with the run. Default is List[LettaMessage].

        Parameters
        ----------
        run_id : str

        before : typing.Optional[str]
            Cursor for pagination

        after : typing.Optional[str]
            Cursor for pagination

        limit : typing.Optional[int]
            Maximum number of messages to return

        order : typing.Optional[str]
            Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.

        role : typing.Optional[MessageRole]
            Filter by role

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.List[LettaMessageUnion]
            Successful Response

        Examples
        --------
        import asyncio

        from letta_client import AsyncLetta

        client = AsyncLetta(
            project="YOUR_PROJECT",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.runs.messages.list(
                run_id="run_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            run_id, before=before, after=after, limit=limit, order=order, role=role, request_options=request_options
        )
        return _response.data
