# 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.agent_blueprint_public import AgentBlueprintPublic
from ..types.agent_blueprint_write import AgentBlueprintWrite
from ..types.agent_config_env import AgentConfigEnv
from ..types.blueprint_page_history import BlueprintPageHistory
from .raw_client import AsyncRawAgentConfigsClient, RawAgentConfigsClient

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


class AgentConfigsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawAgentConfigsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawAgentConfigsClient
        """
        return self._raw_client

    def create_agent_config(
        self,
        *,
        blueprint: AgentBlueprintWrite,
        project_id: typing.Optional[str] = OMIT,
        project_name: typing.Optional[str] = OMIT,
        id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Creates a new optimizer config with initial blueprint, or adds a new blueprint to existing config

        Parameters
        ----------
        blueprint : AgentBlueprintWrite

        project_id : typing.Optional[str]
            Project ID. Either project_id or project_name must be provided

        project_name : typing.Optional[str]
            Project name. Either project_id or project_name must be provided

        id : typing.Optional[str]
            Agent config ID. Generated automatically if not provided

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import OpikApi
        from Opik import AgentBlueprintWrite
        from Opik import AgentConfigValueWrite
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.create_agent_config(blueprint=AgentBlueprintWrite(type="blueprint", values=[AgentConfigValueWrite(key='key', type="string", )], ), )
        """
        _response = self._raw_client.create_agent_config(
            blueprint=blueprint,
            project_id=project_id,
            project_name=project_name,
            id=id,
            request_options=request_options,
        )
        return _response.data

    def create_or_update_envs(
        self,
        *,
        project_id: str,
        envs: typing.Sequence[AgentConfigEnv],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Creates or updates environment-to-blueprint mappings

        Parameters
        ----------
        project_id : str

        envs : typing.Sequence[AgentConfigEnv]

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import OpikApi
        from Opik import AgentConfigEnv
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.create_or_update_envs(project_id='project_id', envs=[AgentConfigEnv(env_name='env_name', blueprint_id='blueprint_id', )], )
        """
        _response = self._raw_client.create_or_update_envs(
            project_id=project_id, envs=envs, request_options=request_options
        )
        return _response.data

    def get_blueprint_by_env(
        self,
        env_name: str,
        project_id: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves the blueprint associated with a specific environment

        Parameters
        ----------
        env_name : str

        project_id : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.get_blueprint_by_env(env_name='env_name', project_id='project_id', )
        """
        _response = self._raw_client.get_blueprint_by_env(
            env_name, project_id, mask_id=mask_id, request_options=request_options
        )
        return _response.data

    def set_env_by_blueprint_name(
        self,
        env_name: str,
        project_id: str,
        *,
        blueprint_name: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Sets an environment to point to a blueprint identified by name

        Parameters
        ----------
        env_name : str

        project_id : str

        blueprint_name : str

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.set_env_by_blueprint_name(env_name='env_name', project_id='project_id', blueprint_name='blueprint_name', )
        """
        _response = self._raw_client.set_env_by_blueprint_name(
            env_name, project_id, blueprint_name=blueprint_name, request_options=request_options
        )
        return _response.data

    def delete_env(
        self, env_name: str, project_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Soft-deletes an environment by setting its ended_at timestamp

        Parameters
        ----------
        env_name : str

        project_id : str

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.delete_env(env_name='env_name', project_id='project_id', )
        """
        _response = self._raw_client.delete_env(env_name, project_id, request_options=request_options)
        return _response.data

    def get_blueprint_by_id(
        self,
        blueprint_id: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves a specific blueprint by its ID

        Parameters
        ----------
        blueprint_id : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.get_blueprint_by_id(blueprint_id='blueprint_id', )
        """
        _response = self._raw_client.get_blueprint_by_id(blueprint_id, mask_id=mask_id, request_options=request_options)
        return _response.data

    def get_blueprint_by_name(
        self,
        project_id: str,
        name: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves a specific blueprint by its name within a project

        Parameters
        ----------
        project_id : str

        name : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.get_blueprint_by_name(project_id='project_id', name='name', )
        """
        _response = self._raw_client.get_blueprint_by_name(
            project_id, name, mask_id=mask_id, request_options=request_options
        )
        return _response.data

    def get_blueprint_history(
        self,
        project_id: str,
        *,
        page: typing.Optional[int] = None,
        size: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> BlueprintPageHistory:
        """
        Retrieves paginated blueprint history for a project

        Parameters
        ----------
        project_id : str

        page : typing.Optional[int]

        size : typing.Optional[int]

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

        Returns
        -------
        BlueprintPageHistory
            History retrieved

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.get_blueprint_history(project_id='project_id', )
        """
        _response = self._raw_client.get_blueprint_history(
            project_id, page=page, size=size, request_options=request_options
        )
        return _response.data

    def get_delta_by_id(
        self, blueprint_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AgentBlueprintPublic:
        """
        Retrieves only the changes (delta) introduced in a specific blueprint

        Parameters
        ----------
        blueprint_id : str

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

        Returns
        -------
        AgentBlueprintPublic
            Delta retrieved

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.get_delta_by_id(blueprint_id='blueprint_id', )
        """
        _response = self._raw_client.get_delta_by_id(blueprint_id, request_options=request_options)
        return _response.data

    def get_latest_blueprint(
        self,
        project_id: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves the latest blueprint for a project

        Parameters
        ----------
        project_id : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import OpikApi
        client = OpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        client.agent_configs.get_latest_blueprint(project_id='project_id', )
        """
        _response = self._raw_client.get_latest_blueprint(project_id, mask_id=mask_id, request_options=request_options)
        return _response.data


class AsyncAgentConfigsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawAgentConfigsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawAgentConfigsClient
        """
        return self._raw_client

    async def create_agent_config(
        self,
        *,
        blueprint: AgentBlueprintWrite,
        project_id: typing.Optional[str] = OMIT,
        project_name: typing.Optional[str] = OMIT,
        id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Creates a new optimizer config with initial blueprint, or adds a new blueprint to existing config

        Parameters
        ----------
        blueprint : AgentBlueprintWrite

        project_id : typing.Optional[str]
            Project ID. Either project_id or project_name must be provided

        project_name : typing.Optional[str]
            Project name. Either project_id or project_name must be provided

        id : typing.Optional[str]
            Agent config ID. Generated automatically if not provided

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import AsyncOpikApi
        from Opik import AgentBlueprintWrite
        from Opik import AgentConfigValueWrite
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.create_agent_config(blueprint=AgentBlueprintWrite(type="blueprint", values=[AgentConfigValueWrite(key='key', type="string", )], ), )
        asyncio.run(main())
        """
        _response = await self._raw_client.create_agent_config(
            blueprint=blueprint,
            project_id=project_id,
            project_name=project_name,
            id=id,
            request_options=request_options,
        )
        return _response.data

    async def create_or_update_envs(
        self,
        *,
        project_id: str,
        envs: typing.Sequence[AgentConfigEnv],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Creates or updates environment-to-blueprint mappings

        Parameters
        ----------
        project_id : str

        envs : typing.Sequence[AgentConfigEnv]

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import AsyncOpikApi
        from Opik import AgentConfigEnv
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.create_or_update_envs(project_id='project_id', envs=[AgentConfigEnv(env_name='env_name', blueprint_id='blueprint_id', )], )
        asyncio.run(main())
        """
        _response = await self._raw_client.create_or_update_envs(
            project_id=project_id, envs=envs, request_options=request_options
        )
        return _response.data

    async def get_blueprint_by_env(
        self,
        env_name: str,
        project_id: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves the blueprint associated with a specific environment

        Parameters
        ----------
        env_name : str

        project_id : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.get_blueprint_by_env(env_name='env_name', project_id='project_id', )
        asyncio.run(main())
        """
        _response = await self._raw_client.get_blueprint_by_env(
            env_name, project_id, mask_id=mask_id, request_options=request_options
        )
        return _response.data

    async def set_env_by_blueprint_name(
        self,
        env_name: str,
        project_id: str,
        *,
        blueprint_name: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Sets an environment to point to a blueprint identified by name

        Parameters
        ----------
        env_name : str

        project_id : str

        blueprint_name : str

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.set_env_by_blueprint_name(env_name='env_name', project_id='project_id', blueprint_name='blueprint_name', )
        asyncio.run(main())
        """
        _response = await self._raw_client.set_env_by_blueprint_name(
            env_name, project_id, blueprint_name=blueprint_name, request_options=request_options
        )
        return _response.data

    async def delete_env(
        self, env_name: str, project_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Soft-deletes an environment by setting its ended_at timestamp

        Parameters
        ----------
        env_name : str

        project_id : str

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

        Returns
        -------
        None

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.delete_env(env_name='env_name', project_id='project_id', )
        asyncio.run(main())
        """
        _response = await self._raw_client.delete_env(env_name, project_id, request_options=request_options)
        return _response.data

    async def get_blueprint_by_id(
        self,
        blueprint_id: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves a specific blueprint by its ID

        Parameters
        ----------
        blueprint_id : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.get_blueprint_by_id(blueprint_id='blueprint_id', )
        asyncio.run(main())
        """
        _response = await self._raw_client.get_blueprint_by_id(
            blueprint_id, mask_id=mask_id, request_options=request_options
        )
        return _response.data

    async def get_blueprint_by_name(
        self,
        project_id: str,
        name: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves a specific blueprint by its name within a project

        Parameters
        ----------
        project_id : str

        name : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.get_blueprint_by_name(project_id='project_id', name='name', )
        asyncio.run(main())
        """
        _response = await self._raw_client.get_blueprint_by_name(
            project_id, name, mask_id=mask_id, request_options=request_options
        )
        return _response.data

    async def get_blueprint_history(
        self,
        project_id: str,
        *,
        page: typing.Optional[int] = None,
        size: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> BlueprintPageHistory:
        """
        Retrieves paginated blueprint history for a project

        Parameters
        ----------
        project_id : str

        page : typing.Optional[int]

        size : typing.Optional[int]

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

        Returns
        -------
        BlueprintPageHistory
            History retrieved

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.get_blueprint_history(project_id='project_id', )
        asyncio.run(main())
        """
        _response = await self._raw_client.get_blueprint_history(
            project_id, page=page, size=size, request_options=request_options
        )
        return _response.data

    async def get_delta_by_id(
        self, blueprint_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AgentBlueprintPublic:
        """
        Retrieves only the changes (delta) introduced in a specific blueprint

        Parameters
        ----------
        blueprint_id : str

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

        Returns
        -------
        AgentBlueprintPublic
            Delta retrieved

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.get_delta_by_id(blueprint_id='blueprint_id', )
        asyncio.run(main())
        """
        _response = await self._raw_client.get_delta_by_id(blueprint_id, request_options=request_options)
        return _response.data

    async def get_latest_blueprint(
        self,
        project_id: str,
        *,
        mask_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AgentBlueprintPublic:
        """
        Retrieves the latest blueprint for a project

        Parameters
        ----------
        project_id : str

        mask_id : typing.Optional[str]

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

        Returns
        -------
        AgentBlueprintPublic
            Blueprint retrieved

        Examples
        --------
        from Opik import AsyncOpikApi
        import asyncio
        client = AsyncOpikApi(api_key="YOUR_API_KEY", workspace_name="YOUR_WORKSPACE_NAME", )
        async def main() -> None:
            await client.agent_configs.get_latest_blueprint(project_id='project_id', )
        asyncio.run(main())
        """
        _response = await self._raw_client.get_latest_blueprint(
            project_id, mask_id=mask_id, request_options=request_options
        )
        return _response.data
