# 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.response import Response
from ..types.response_bash_exec_result import ResponseBashExecResult
from ..types.response_bash_output_result import ResponseBashOutputResult
from ..types.response_bash_session_info import ResponseBashSessionInfo
from ..types.response_list_bash_session_info import ResponseListBashSessionInfo
from .raw_client import AsyncRawBashClient, RawBashClient

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


class BashClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawBashClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawBashClient
        """
        return self._raw_client

    def exec(
        self,
        *,
        command: str,
        session_id: typing.Optional[str] = OMIT,
        exec_dir: typing.Optional[str] = OMIT,
        env: typing.Optional[typing.Dict[str, typing.Optional[str]]] = OMIT,
        async_mode: typing.Optional[bool] = OMIT,
        timeout: typing.Optional[float] = OMIT,
        hard_timeout: typing.Optional[float] = OMIT,
        max_output_length: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ResponseBashExecResult:
        """
        Execute a bash command.

        - async_mode=true: returns immediately with RUNNING status
        - async_mode=false + timeout: waits up to timeout, then returns RUNNING if not done
        - async_mode=false + no timeout: waits until command completes
        - hard_timeout: kills the process after this many seconds
        - env: extra environment variables for this command only

        Parameters
        ----------
        command : str
            Shell command to execute

        session_id : typing.Optional[str]
            Target session ID. If not provided, a new session is created automatically.

        exec_dir : typing.Optional[str]
            Working directory for command execution (absolute path)

        env : typing.Optional[typing.Dict[str, typing.Optional[str]]]
            Extra environment variables to inject for this command only.

        async_mode : typing.Optional[bool]
            If true, return immediately with running status. Use /output to poll results.

        timeout : typing.Optional[float]
            HTTP timeout (seconds). Only effective when async_mode=false. If the command does not complete within this time, HTTP returns running status and the command continues in the background. Use /output to get results.

        hard_timeout : typing.Optional[float]
            Hard execution timeout (seconds). When reached, the process is killed and status becomes timed_out. None means no limit.

        max_output_length : typing.Optional[int]
            Maximum character length for stdout/stderr in the response. When output exceeds this limit, middle truncation is applied (head and tail preserved, middle replaced with a marker). Only effective in sync mode (async_mode=false). Set to 0 to disable truncation.

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

        Returns
        -------
        ResponseBashExecResult
            Successful Response

        Examples
        --------
        from agent_sandbox import Sandbox

        client = Sandbox(
            base_url="https://yourhost.com/path/to/api",
        )
        client.bash.exec(
            command="command",
        )
        """
        _response = self._raw_client.exec(
            command=command,
            session_id=session_id,
            exec_dir=exec_dir,
            env=env,
            async_mode=async_mode,
            timeout=timeout,
            hard_timeout=hard_timeout,
            max_output_length=max_output_length,
            request_options=request_options,
        )
        return _response.data

    def output(
        self,
        *,
        session_id: str,
        command_id: typing.Optional[str] = OMIT,
        offset: typing.Optional[int] = OMIT,
        stderr_offset: typing.Optional[int] = OMIT,
        wait: typing.Optional[bool] = OMIT,
        wait_timeout: typing.Optional[float] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ResponseBashOutputResult:
        """
        Read output from a bash session using offset-based streaming.

        - offset/stderr_offset: byte offsets to read from (use values from previous response)
        - wait=true: long-poll until new output arrives or wait_timeout

        Parameters
        ----------
        session_id : str
            Target session ID

        command_id : typing.Optional[str]
            Target a specific async command. If not set, uses session-level output.

        offset : typing.Optional[int]
            Stdout byte offset to read from

        stderr_offset : typing.Optional[int]
            Stderr byte offset to read from

        wait : typing.Optional[bool]
            If true, long-poll until new output is available or wait_timeout is reached.

        wait_timeout : typing.Optional[float]
            Max seconds to wait for new output when wait=true.

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

        Returns
        -------
        ResponseBashOutputResult
            Successful Response

        Examples
        --------
        from agent_sandbox import Sandbox

        client = Sandbox(
            base_url="https://yourhost.com/path/to/api",
        )
        client.bash.output(
            session_id="session_id",
        )
        """
        _response = self._raw_client.output(
            session_id=session_id,
            command_id=command_id,
            offset=offset,
            stderr_offset=stderr_offset,
            wait=wait,
            wait_timeout=wait_timeout,
            request_options=request_options,
        )
        return _response.data

    def write(
        self,
        *,
        session_id: str,
        input: str,
        command_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Response:
        """
        Write input to a bash session's stdin.

        Parameters
        ----------
        session_id : str
            Target session ID

        input : str
            Content to write to the process stdin

        command_id : typing.Optional[str]
            Target a specific async command. If not set, writes to current command.

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

        Returns
        -------
        Response
            Successful Response

        Examples
        --------
        from agent_sandbox import Sandbox

        client = Sandbox(
            base_url="https://yourhost.com/path/to/api",
        )
        client.bash.write(
            session_id="session_id",
            input="input",
        )
        """
        _response = self._raw_client.write(
            session_id=session_id, input=input, command_id=command_id, request_options=request_options
        )
        return _response.data

    def kill(
        self,
        *,
        session_id: str,
        signal: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Response:
        """
        Send a signal to a bash session's process.

        Parameters
        ----------
        session_id : str
            Target session ID

        signal : typing.Optional[str]
            Signal to send: SIGTERM, SIGKILL, or SIGINT

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

        Returns
        -------
        Response
            Successful Response

        Examples
        --------
        from agent_sandbox import Sandbox

        client = Sandbox(
            base_url="https://yourhost.com/path/to/api",
        )
        client.bash.kill(
            session_id="session_id",
        )
        """
        _response = self._raw_client.kill(session_id=session_id, signal=signal, request_options=request_options)
        return _response.data

    def sessions(self, *, request_options: typing.Optional[RequestOptions] = None) -> ResponseListBashSessionInfo:
        """
        List all active bash sessions.

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

        Returns
        -------
        ResponseListBashSessionInfo
            Successful Response

        Examples
        --------
        from agent_sandbox import Sandbox

        client = Sandbox(
            base_url="https://yourhost.com/path/to/api",
        )
        client.bash.sessions()
        """
        _response = self._raw_client.sessions(request_options=request_options)
        return _response.data

    def create_session(
        self,
        *,
        session_id: typing.Optional[str] = OMIT,
        exec_dir: typing.Optional[str] = OMIT,
        snapshot_path: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ResponseBashSessionInfo:
        """
        Create a new bash session.

        Parameters
        ----------
        session_id : typing.Optional[str]
            Session ID. Auto-generated if not provided.

        exec_dir : typing.Optional[str]
            Working directory for the new session (absolute path)

        snapshot_path : typing.Optional[str]
            Path to a shell snapshot script to source on session init

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

        Returns
        -------
        ResponseBashSessionInfo
            Successful Response

        Examples
        --------
        from agent_sandbox import Sandbox

        client = Sandbox(
            base_url="https://yourhost.com/path/to/api",
        )
        client.bash.create_session()
        """
        _response = self._raw_client.create_session(
            session_id=session_id, exec_dir=exec_dir, snapshot_path=snapshot_path, request_options=request_options
        )
        return _response.data

    def close_session(self, session_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Response:
        """
        Close a bash session.

        Parameters
        ----------
        session_id : str

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

        Returns
        -------
        Response
            Successful Response

        Examples
        --------
        from agent_sandbox import Sandbox

        client = Sandbox(
            base_url="https://yourhost.com/path/to/api",
        )
        client.bash.close_session(
            session_id="session_id",
        )
        """
        _response = self._raw_client.close_session(session_id, request_options=request_options)
        return _response.data


class AsyncBashClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawBashClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawBashClient
        """
        return self._raw_client

    async def exec(
        self,
        *,
        command: str,
        session_id: typing.Optional[str] = OMIT,
        exec_dir: typing.Optional[str] = OMIT,
        env: typing.Optional[typing.Dict[str, typing.Optional[str]]] = OMIT,
        async_mode: typing.Optional[bool] = OMIT,
        timeout: typing.Optional[float] = OMIT,
        hard_timeout: typing.Optional[float] = OMIT,
        max_output_length: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ResponseBashExecResult:
        """
        Execute a bash command.

        - async_mode=true: returns immediately with RUNNING status
        - async_mode=false + timeout: waits up to timeout, then returns RUNNING if not done
        - async_mode=false + no timeout: waits until command completes
        - hard_timeout: kills the process after this many seconds
        - env: extra environment variables for this command only

        Parameters
        ----------
        command : str
            Shell command to execute

        session_id : typing.Optional[str]
            Target session ID. If not provided, a new session is created automatically.

        exec_dir : typing.Optional[str]
            Working directory for command execution (absolute path)

        env : typing.Optional[typing.Dict[str, typing.Optional[str]]]
            Extra environment variables to inject for this command only.

        async_mode : typing.Optional[bool]
            If true, return immediately with running status. Use /output to poll results.

        timeout : typing.Optional[float]
            HTTP timeout (seconds). Only effective when async_mode=false. If the command does not complete within this time, HTTP returns running status and the command continues in the background. Use /output to get results.

        hard_timeout : typing.Optional[float]
            Hard execution timeout (seconds). When reached, the process is killed and status becomes timed_out. None means no limit.

        max_output_length : typing.Optional[int]
            Maximum character length for stdout/stderr in the response. When output exceeds this limit, middle truncation is applied (head and tail preserved, middle replaced with a marker). Only effective in sync mode (async_mode=false). Set to 0 to disable truncation.

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

        Returns
        -------
        ResponseBashExecResult
            Successful Response

        Examples
        --------
        import asyncio

        from agent_sandbox import AsyncSandbox

        client = AsyncSandbox(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.bash.exec(
                command="command",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.exec(
            command=command,
            session_id=session_id,
            exec_dir=exec_dir,
            env=env,
            async_mode=async_mode,
            timeout=timeout,
            hard_timeout=hard_timeout,
            max_output_length=max_output_length,
            request_options=request_options,
        )
        return _response.data

    async def output(
        self,
        *,
        session_id: str,
        command_id: typing.Optional[str] = OMIT,
        offset: typing.Optional[int] = OMIT,
        stderr_offset: typing.Optional[int] = OMIT,
        wait: typing.Optional[bool] = OMIT,
        wait_timeout: typing.Optional[float] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ResponseBashOutputResult:
        """
        Read output from a bash session using offset-based streaming.

        - offset/stderr_offset: byte offsets to read from (use values from previous response)
        - wait=true: long-poll until new output arrives or wait_timeout

        Parameters
        ----------
        session_id : str
            Target session ID

        command_id : typing.Optional[str]
            Target a specific async command. If not set, uses session-level output.

        offset : typing.Optional[int]
            Stdout byte offset to read from

        stderr_offset : typing.Optional[int]
            Stderr byte offset to read from

        wait : typing.Optional[bool]
            If true, long-poll until new output is available or wait_timeout is reached.

        wait_timeout : typing.Optional[float]
            Max seconds to wait for new output when wait=true.

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

        Returns
        -------
        ResponseBashOutputResult
            Successful Response

        Examples
        --------
        import asyncio

        from agent_sandbox import AsyncSandbox

        client = AsyncSandbox(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.bash.output(
                session_id="session_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.output(
            session_id=session_id,
            command_id=command_id,
            offset=offset,
            stderr_offset=stderr_offset,
            wait=wait,
            wait_timeout=wait_timeout,
            request_options=request_options,
        )
        return _response.data

    async def write(
        self,
        *,
        session_id: str,
        input: str,
        command_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Response:
        """
        Write input to a bash session's stdin.

        Parameters
        ----------
        session_id : str
            Target session ID

        input : str
            Content to write to the process stdin

        command_id : typing.Optional[str]
            Target a specific async command. If not set, writes to current command.

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

        Returns
        -------
        Response
            Successful Response

        Examples
        --------
        import asyncio

        from agent_sandbox import AsyncSandbox

        client = AsyncSandbox(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.bash.write(
                session_id="session_id",
                input="input",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.write(
            session_id=session_id, input=input, command_id=command_id, request_options=request_options
        )
        return _response.data

    async def kill(
        self,
        *,
        session_id: str,
        signal: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Response:
        """
        Send a signal to a bash session's process.

        Parameters
        ----------
        session_id : str
            Target session ID

        signal : typing.Optional[str]
            Signal to send: SIGTERM, SIGKILL, or SIGINT

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

        Returns
        -------
        Response
            Successful Response

        Examples
        --------
        import asyncio

        from agent_sandbox import AsyncSandbox

        client = AsyncSandbox(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.bash.kill(
                session_id="session_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.kill(session_id=session_id, signal=signal, request_options=request_options)
        return _response.data

    async def sessions(self, *, request_options: typing.Optional[RequestOptions] = None) -> ResponseListBashSessionInfo:
        """
        List all active bash sessions.

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

        Returns
        -------
        ResponseListBashSessionInfo
            Successful Response

        Examples
        --------
        import asyncio

        from agent_sandbox import AsyncSandbox

        client = AsyncSandbox(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.bash.sessions()


        asyncio.run(main())
        """
        _response = await self._raw_client.sessions(request_options=request_options)
        return _response.data

    async def create_session(
        self,
        *,
        session_id: typing.Optional[str] = OMIT,
        exec_dir: typing.Optional[str] = OMIT,
        snapshot_path: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ResponseBashSessionInfo:
        """
        Create a new bash session.

        Parameters
        ----------
        session_id : typing.Optional[str]
            Session ID. Auto-generated if not provided.

        exec_dir : typing.Optional[str]
            Working directory for the new session (absolute path)

        snapshot_path : typing.Optional[str]
            Path to a shell snapshot script to source on session init

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

        Returns
        -------
        ResponseBashSessionInfo
            Successful Response

        Examples
        --------
        import asyncio

        from agent_sandbox import AsyncSandbox

        client = AsyncSandbox(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.bash.create_session()


        asyncio.run(main())
        """
        _response = await self._raw_client.create_session(
            session_id=session_id, exec_dir=exec_dir, snapshot_path=snapshot_path, request_options=request_options
        )
        return _response.data

    async def close_session(
        self, session_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> Response:
        """
        Close a bash session.

        Parameters
        ----------
        session_id : str

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

        Returns
        -------
        Response
            Successful Response

        Examples
        --------
        import asyncio

        from agent_sandbox import AsyncSandbox

        client = AsyncSandbox(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.bash.close_session(
                session_id="session_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.close_session(session_id, request_options=request_options)
        return _response.data
