# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

from typing import Optional
from typing_extensions import Literal

import httpx

from .steps import (
    StepsResource,
    AsyncStepsResource,
    StepsResourceWithRawResponse,
    AsyncStepsResourceWithRawResponse,
    StepsResourceWithStreamingResponse,
    AsyncStepsResourceWithStreamingResponse,
)
from .trace import (
    TraceResource,
    AsyncTraceResource,
    TraceResourceWithRawResponse,
    AsyncTraceResourceWithRawResponse,
    TraceResourceWithStreamingResponse,
    AsyncTraceResourceWithStreamingResponse,
)
from .usage import (
    UsageResource,
    AsyncUsageResource,
    UsageResourceWithRawResponse,
    AsyncUsageResourceWithRawResponse,
    UsageResourceWithStreamingResponse,
    AsyncUsageResourceWithStreamingResponse,
)
from ...types import StopReasonType, run_list_params
from ..._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given
from ..._utils import maybe_transform
from .messages import (
    MessagesResource,
    AsyncMessagesResource,
    MessagesResourceWithRawResponse,
    AsyncMessagesResourceWithRawResponse,
    MessagesResourceWithStreamingResponse,
    AsyncMessagesResourceWithStreamingResponse,
)
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from ...pagination import SyncArrayPage, AsyncArrayPage
from ..._base_client import AsyncPaginator, make_request_options
from ...types.agents.run import Run
from ...types.stop_reason_type import StopReasonType

__all__ = ["RunsResource", "AsyncRunsResource"]


class RunsResource(SyncAPIResource):
    @cached_property
    def messages(self) -> MessagesResource:
        return MessagesResource(self._client)

    @cached_property
    def usage(self) -> UsageResource:
        return UsageResource(self._client)

    @cached_property
    def steps(self) -> StepsResource:
        return StepsResource(self._client)

    @cached_property
    def trace(self) -> TraceResource:
        return TraceResource(self._client)

    @cached_property
    def with_raw_response(self) -> RunsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/letta-ai/letta-python#accessing-raw-response-data-eg-headers
        """
        return RunsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> RunsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/letta-ai/letta-python#with_streaming_response
        """
        return RunsResourceWithStreamingResponse(self)

    def retrieve(
        self,
        run_id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Run:
        """
        Get the status of a run.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        return self._get(
            f"/v1/runs/{run_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=Run,
        )

    def list(
        self,
        *,
        active: bool | Omit = omit,
        after: Optional[str] | Omit = omit,
        agent_id: Optional[str] | Omit = omit,
        agent_ids: Optional[SequenceNotStr[str]] | Omit = omit,
        ascending: bool | Omit = omit,
        background: Optional[bool] | Omit = omit,
        before: Optional[str] | Omit = omit,
        conversation_id: Optional[str] | Omit = omit,
        limit: Optional[int] | Omit = omit,
        order: Literal["asc", "desc"] | Omit = omit,
        order_by: Literal["created_at"] | Omit = omit,
        statuses: Optional[SequenceNotStr[str]] | Omit = omit,
        stop_reason: Optional[StopReasonType] | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SyncArrayPage[Run]:
        """
        List all runs.

        Args:
          active: Filter for active runs.

          after: Run ID cursor for pagination. Returns runs that come after this run ID in the
              specified sort order

          agent_id: The unique identifier of the agent associated with the run.

          agent_ids: The unique identifiers of the agents associated with the run. Deprecated in
              favor of agent_id field.

          ascending: Whether to sort agents oldest to newest (True) or newest to oldest (False,
              default). Deprecated in favor of order field.

          background: If True, filters for runs that were created in background mode.

          before: Run ID cursor for pagination. Returns runs that come before this run ID in the
              specified sort order

          conversation_id: Filter runs by conversation ID.

          limit: Maximum number of runs to return

          order: Sort order for runs by creation time. 'asc' for oldest first, 'desc' for newest
              first

          order_by: Field to sort by

          statuses: Filter runs by status. Can specify multiple statuses.

          stop_reason: Filter runs by stop reason.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._get_api_list(
            "/v1/runs/",
            page=SyncArrayPage[Run],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "active": active,
                        "after": after,
                        "agent_id": agent_id,
                        "agent_ids": agent_ids,
                        "ascending": ascending,
                        "background": background,
                        "before": before,
                        "conversation_id": conversation_id,
                        "limit": limit,
                        "order": order,
                        "order_by": order_by,
                        "statuses": statuses,
                        "stop_reason": stop_reason,
                    },
                    run_list_params.RunListParams,
                ),
            ),
            model=Run,
        )


class AsyncRunsResource(AsyncAPIResource):
    @cached_property
    def messages(self) -> AsyncMessagesResource:
        return AsyncMessagesResource(self._client)

    @cached_property
    def usage(self) -> AsyncUsageResource:
        return AsyncUsageResource(self._client)

    @cached_property
    def steps(self) -> AsyncStepsResource:
        return AsyncStepsResource(self._client)

    @cached_property
    def trace(self) -> AsyncTraceResource:
        return AsyncTraceResource(self._client)

    @cached_property
    def with_raw_response(self) -> AsyncRunsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/letta-ai/letta-python#accessing-raw-response-data-eg-headers
        """
        return AsyncRunsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncRunsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/letta-ai/letta-python#with_streaming_response
        """
        return AsyncRunsResourceWithStreamingResponse(self)

    async def retrieve(
        self,
        run_id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Run:
        """
        Get the status of a run.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        return await self._get(
            f"/v1/runs/{run_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=Run,
        )

    def list(
        self,
        *,
        active: bool | Omit = omit,
        after: Optional[str] | Omit = omit,
        agent_id: Optional[str] | Omit = omit,
        agent_ids: Optional[SequenceNotStr[str]] | Omit = omit,
        ascending: bool | Omit = omit,
        background: Optional[bool] | Omit = omit,
        before: Optional[str] | Omit = omit,
        conversation_id: Optional[str] | Omit = omit,
        limit: Optional[int] | Omit = omit,
        order: Literal["asc", "desc"] | Omit = omit,
        order_by: Literal["created_at"] | Omit = omit,
        statuses: Optional[SequenceNotStr[str]] | Omit = omit,
        stop_reason: Optional[StopReasonType] | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> AsyncPaginator[Run, AsyncArrayPage[Run]]:
        """
        List all runs.

        Args:
          active: Filter for active runs.

          after: Run ID cursor for pagination. Returns runs that come after this run ID in the
              specified sort order

          agent_id: The unique identifier of the agent associated with the run.

          agent_ids: The unique identifiers of the agents associated with the run. Deprecated in
              favor of agent_id field.

          ascending: Whether to sort agents oldest to newest (True) or newest to oldest (False,
              default). Deprecated in favor of order field.

          background: If True, filters for runs that were created in background mode.

          before: Run ID cursor for pagination. Returns runs that come before this run ID in the
              specified sort order

          conversation_id: Filter runs by conversation ID.

          limit: Maximum number of runs to return

          order: Sort order for runs by creation time. 'asc' for oldest first, 'desc' for newest
              first

          order_by: Field to sort by

          statuses: Filter runs by status. Can specify multiple statuses.

          stop_reason: Filter runs by stop reason.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._get_api_list(
            "/v1/runs/",
            page=AsyncArrayPage[Run],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "active": active,
                        "after": after,
                        "agent_id": agent_id,
                        "agent_ids": agent_ids,
                        "ascending": ascending,
                        "background": background,
                        "before": before,
                        "conversation_id": conversation_id,
                        "limit": limit,
                        "order": order,
                        "order_by": order_by,
                        "statuses": statuses,
                        "stop_reason": stop_reason,
                    },
                    run_list_params.RunListParams,
                ),
            ),
            model=Run,
        )


class RunsResourceWithRawResponse:
    def __init__(self, runs: RunsResource) -> None:
        self._runs = runs

        self.retrieve = to_raw_response_wrapper(
            runs.retrieve,
        )
        self.list = to_raw_response_wrapper(
            runs.list,
        )

    @cached_property
    def messages(self) -> MessagesResourceWithRawResponse:
        return MessagesResourceWithRawResponse(self._runs.messages)

    @cached_property
    def usage(self) -> UsageResourceWithRawResponse:
        return UsageResourceWithRawResponse(self._runs.usage)

    @cached_property
    def steps(self) -> StepsResourceWithRawResponse:
        return StepsResourceWithRawResponse(self._runs.steps)

    @cached_property
    def trace(self) -> TraceResourceWithRawResponse:
        return TraceResourceWithRawResponse(self._runs.trace)


class AsyncRunsResourceWithRawResponse:
    def __init__(self, runs: AsyncRunsResource) -> None:
        self._runs = runs

        self.retrieve = async_to_raw_response_wrapper(
            runs.retrieve,
        )
        self.list = async_to_raw_response_wrapper(
            runs.list,
        )

    @cached_property
    def messages(self) -> AsyncMessagesResourceWithRawResponse:
        return AsyncMessagesResourceWithRawResponse(self._runs.messages)

    @cached_property
    def usage(self) -> AsyncUsageResourceWithRawResponse:
        return AsyncUsageResourceWithRawResponse(self._runs.usage)

    @cached_property
    def steps(self) -> AsyncStepsResourceWithRawResponse:
        return AsyncStepsResourceWithRawResponse(self._runs.steps)

    @cached_property
    def trace(self) -> AsyncTraceResourceWithRawResponse:
        return AsyncTraceResourceWithRawResponse(self._runs.trace)


class RunsResourceWithStreamingResponse:
    def __init__(self, runs: RunsResource) -> None:
        self._runs = runs

        self.retrieve = to_streamed_response_wrapper(
            runs.retrieve,
        )
        self.list = to_streamed_response_wrapper(
            runs.list,
        )

    @cached_property
    def messages(self) -> MessagesResourceWithStreamingResponse:
        return MessagesResourceWithStreamingResponse(self._runs.messages)

    @cached_property
    def usage(self) -> UsageResourceWithStreamingResponse:
        return UsageResourceWithStreamingResponse(self._runs.usage)

    @cached_property
    def steps(self) -> StepsResourceWithStreamingResponse:
        return StepsResourceWithStreamingResponse(self._runs.steps)

    @cached_property
    def trace(self) -> TraceResourceWithStreamingResponse:
        return TraceResourceWithStreamingResponse(self._runs.trace)


class AsyncRunsResourceWithStreamingResponse:
    def __init__(self, runs: AsyncRunsResource) -> None:
        self._runs = runs

        self.retrieve = async_to_streamed_response_wrapper(
            runs.retrieve,
        )
        self.list = async_to_streamed_response_wrapper(
            runs.list,
        )

    @cached_property
    def messages(self) -> AsyncMessagesResourceWithStreamingResponse:
        return AsyncMessagesResourceWithStreamingResponse(self._runs.messages)

    @cached_property
    def usage(self) -> AsyncUsageResourceWithStreamingResponse:
        return AsyncUsageResourceWithStreamingResponse(self._runs.usage)

    @cached_property
    def steps(self) -> AsyncStepsResourceWithStreamingResponse:
        return AsyncStepsResourceWithStreamingResponse(self._runs.steps)

    @cached_property
    def trace(self) -> AsyncTraceResourceWithStreamingResponse:
        return AsyncTraceResourceWithStreamingResponse(self._runs.trace)
