# 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.publish_stream_events_response import PublishStreamEventsResponse
from ...types.task_message_update import TaskMessageUpdate
from .raw_client import AsyncRawEventsClient, RawEventsClient

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class EventsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawEventsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawEventsClient
        """
        return self._raw_client

    def publish(
        self,
        *,
        events: typing.Sequence[TaskMessageUpdate],
        task_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PublishStreamEventsResponse:
        """
        Publish stream events to a task's Redis stream.

            This endpoint is used by agents to send real-time updates (deltas, full content,
            start/done signals) that clients consume via SSE endpoints like `/tasks/{task_id}/stream`.

            Events are published in order. For optimal performance with high-frequency updates
            (e.g., LLM streaming), batch multiple events in a single request.

            Requires `update` permission on the task.

        Parameters
        ----------
        events : typing.Sequence[TaskMessageUpdate]
            List of stream events to publish

        task_id : str
            Task ID to publish events to

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

        Returns
        -------
        PublishStreamEventsResponse
            Successful Response

        Examples
        --------
        from terminaluse import TaskMessageUpdate_Delta, TerminaluseApi

        client = TerminaluseApi(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.stream.events.publish(
            events=[TaskMessageUpdate_Delta()],
            task_id="task_id",
        )
        """
        _response = self._raw_client.publish(events=events, task_id=task_id, request_options=request_options)
        return _response.data


class AsyncEventsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawEventsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawEventsClient
        """
        return self._raw_client

    async def publish(
        self,
        *,
        events: typing.Sequence[TaskMessageUpdate],
        task_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PublishStreamEventsResponse:
        """
        Publish stream events to a task's Redis stream.

            This endpoint is used by agents to send real-time updates (deltas, full content,
            start/done signals) that clients consume via SSE endpoints like `/tasks/{task_id}/stream`.

            Events are published in order. For optimal performance with high-frequency updates
            (e.g., LLM streaming), batch multiple events in a single request.

            Requires `update` permission on the task.

        Parameters
        ----------
        events : typing.Sequence[TaskMessageUpdate]
            List of stream events to publish

        task_id : str
            Task ID to publish events to

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

        Returns
        -------
        PublishStreamEventsResponse
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminaluseApi, TaskMessageUpdate_Delta

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


        async def main() -> None:
            await client.stream.events.publish(
                events=[TaskMessageUpdate_Delta()],
                task_id="task_id",
            )


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