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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.pagination import AsyncPager, SyncPager
from ..core.request_options import RequestOptions
from ..types.list_messages_v2response import ListMessagesV2Response
from ..types.ui_message import UiMessage
from .raw_client import AsyncRawMessagesV2Client, RawMessagesV2Client
from .types.messages_v2list_request_direction import MessagesV2ListRequestDirection


class MessagesV2Client:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawMessagesV2Client(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawMessagesV2Client
        """
        return self._raw_client

    def list(
        self,
        *,
        task_id: str,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        direction: typing.Optional[MessagesV2ListRequestDirection] = None,
        parent_tool_use_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[UiMessage, ListMessagesV2Response]:
        """
        List messages for a task in Vercel AI SDK UIMessage format.

            This endpoint returns messages transformed from raw SDK events into
            a normalized UIMessage format suitable for frontend consumption.

            Features:
            - Tool merging: tool_use and tool_result are combined into single tool parts
            - Complete-units pagination: Never splits tool request/result pairs across pages
            - Runtime transformation: Raw SDK messages are transformed on-demand

            Args:
                task_id: The task ID to filter messages by
                limit: Maximum number of complete messages to return (default: 50)
                cursor: Opaque cursor string for pagination
                direction: Pagination direction - "older" (default) or "newer"
                parent_tool_use_id: Filter messages by parent tool use ID (for subagent context)

            Returns:
                ListMessagesV2Response with UIMessages and pagination info

        Parameters
        ----------
        task_id : str
            The task ID

        limit : typing.Optional[int]

        cursor : typing.Optional[str]

        direction : typing.Optional[MessagesV2ListRequestDirection]

        parent_tool_use_id : typing.Optional[str]

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

        Returns
        -------
        SyncPager[UiMessage, ListMessagesV2Response]
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        response = client.messages_v2.list(
            task_id="task_id",
        )
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        return self._raw_client.list(
            task_id=task_id,
            limit=limit,
            cursor=cursor,
            direction=direction,
            parent_tool_use_id=parent_tool_use_id,
            request_options=request_options,
        )

    def retrieve(
        self, message_id: str, *, task_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.Optional[UiMessage]:
        """
        Get a single message by ID in UIMessage format.

            Returns the transformed UIMessage or null if not found.

        Parameters
        ----------
        message_id : str

        task_id : str
            The task ID

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

        Returns
        -------
        typing.Optional[UiMessage]
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.messages_v2.retrieve(
            message_id="message_id",
            task_id="task_id",
        )
        """
        _response = self._raw_client.retrieve(message_id, task_id=task_id, request_options=request_options)
        return _response.data


class AsyncMessagesV2Client:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawMessagesV2Client(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawMessagesV2Client
        """
        return self._raw_client

    async def list(
        self,
        *,
        task_id: str,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        direction: typing.Optional[MessagesV2ListRequestDirection] = None,
        parent_tool_use_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[UiMessage, ListMessagesV2Response]:
        """
        List messages for a task in Vercel AI SDK UIMessage format.

            This endpoint returns messages transformed from raw SDK events into
            a normalized UIMessage format suitable for frontend consumption.

            Features:
            - Tool merging: tool_use and tool_result are combined into single tool parts
            - Complete-units pagination: Never splits tool request/result pairs across pages
            - Runtime transformation: Raw SDK messages are transformed on-demand

            Args:
                task_id: The task ID to filter messages by
                limit: Maximum number of complete messages to return (default: 50)
                cursor: Opaque cursor string for pagination
                direction: Pagination direction - "older" (default) or "newer"
                parent_tool_use_id: Filter messages by parent tool use ID (for subagent context)

            Returns:
                ListMessagesV2Response with UIMessages and pagination info

        Parameters
        ----------
        task_id : str
            The task ID

        limit : typing.Optional[int]

        cursor : typing.Optional[str]

        direction : typing.Optional[MessagesV2ListRequestDirection]

        parent_tool_use_id : typing.Optional[str]

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

        Returns
        -------
        AsyncPager[UiMessage, ListMessagesV2Response]
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            response = await client.messages_v2.list(
                task_id="task_id",
            )
            async for item in response:
                yield item

            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page


        asyncio.run(main())
        """
        return await self._raw_client.list(
            task_id=task_id,
            limit=limit,
            cursor=cursor,
            direction=direction,
            parent_tool_use_id=parent_tool_use_id,
            request_options=request_options,
        )

    async def retrieve(
        self, message_id: str, *, task_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.Optional[UiMessage]:
        """
        Get a single message by ID in UIMessage format.

            Returns the transformed UIMessage or null if not found.

        Parameters
        ----------
        message_id : str

        task_id : str
            The task ID

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

        Returns
        -------
        typing.Optional[UiMessage]
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.messages_v2.retrieve(
                message_id="message_id",
                task_id="task_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.retrieve(message_id, task_id=task_id, request_options=request_options)
        return _response.data
