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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.context_variable import ContextVariable
from ..core.pydantic_utilities import parse_obj_as
from ..errors.not_found_error import NotFoundError
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.tool_id import ToolId
from ..core.serialization import convert_and_respect_annotation_metadata
from ..types.context_variable_read_result import ContextVariableReadResult
from ..core.jsonable_encoder import jsonable_encoder
from ..types.context_variable_tags_update_params import ContextVariableTagsUpdateParams
from ..types.context_variable_value import ContextVariableValue
from ..core.client_wrapper import AsyncClientWrapper

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


class ContextVariablesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def list(
        self,
        *,
        tag_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[ContextVariable]:
        """
        Lists all context variables set for the provided tag or all context variables if no tag is provided

        Parameters
        ----------
        tag_id : typing.Optional[str]
            The tag ID to filter context variables by

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

        Returns
        -------
        typing.List[ContextVariable]
            List of all context variables

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "context-variables",
            method="GET",
            params={
                "tag_id": tag_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[ContextVariable],
                    parse_obj_as(
                        type_=typing.List[ContextVariable],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create(
        self,
        *,
        name: str,
        description: typing.Optional[str] = OMIT,
        tool_id: typing.Optional[ToolId] = OMIT,
        freshness_rules: typing.Optional[str] = OMIT,
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariable:
        """
        Creates a new context variable

        Example uses:
        - Track subscription tiers to control feature access
        - Store usage patterns for personalized recommendations
        - Remember preferences for tailored responses

        Parameters
        ----------
        name : str
            Name of the context variable

        description : typing.Optional[str]
            Description of the context variable's purpose

        tool_id : typing.Optional[ToolId]

        freshness_rules : typing.Optional[str]
            Cron expression defining the freshness rules

        tags : typing.Optional[typing.Sequence[str]]
            List of tags associated with the context variable

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

        Returns
        -------
        ContextVariable
            Context variable type successfully created

        Examples
        --------
        from parlant.client import ParlantClient, ToolId

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.create(
            name="UserBalance",
            description="Stores the account balances of users",
            tool_id=ToolId(
                service_name="finance_service",
                tool_name="balance_checker",
            ),
            freshness_rules="30 2 * * *",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "context-variables",
            method="POST",
            json={
                "name": name,
                "description": description,
                "tool_id": convert_and_respect_annotation_metadata(
                    object_=tool_id, annotation=ToolId, direction="write"
                ),
                "freshness_rules": freshness_rules,
                "tags": tags,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariable,
                    parse_obj_as(
                        type_=ContextVariable,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete_many(
        self,
        *,
        tag_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes all context variables for the provided tag

        Parameters
        ----------
        tag_id : typing.Optional[str]
            The tag ID to filter context variables by

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

        Returns
        -------
        None

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.delete_many()
        """
        _response = self._client_wrapper.httpx_client.request(
            "context-variables",
            method="DELETE",
            params={
                "tag_id": tag_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def retrieve(
        self,
        variable_id: str,
        *,
        include_values: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariableReadResult:
        """
        Retrieves a context variable's details and optionally its values.

        Can return all customer or tag values for this variable type if include_values=True.

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        include_values : typing.Optional[bool]
            Whether to include variable values in the response

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

        Returns
        -------
        ContextVariableReadResult
            Context variable details successfully retrieved

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.retrieve(
            variable_id="v9a8r7i6b5",
            include_values=True,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}",
            method="GET",
            params={
                "include_values": include_values,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariableReadResult,
                    parse_obj_as(
                        type_=ContextVariableReadResult,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete(
        self,
        variable_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes a context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

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

        Returns
        -------
        None

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.delete(
            variable_id="v9a8r7i6b5",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def update(
        self,
        variable_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        tool_id: typing.Optional[ToolId] = OMIT,
        freshness_rules: typing.Optional[str] = OMIT,
        tags: typing.Optional[ContextVariableTagsUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariable:
        """
        Updates an existing context variable.

        Only provided fields will be updated; others remain unchanged.

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        name : typing.Optional[str]
            Name of the context variable

        description : typing.Optional[str]
            Description of the context variable's purpose

        tool_id : typing.Optional[ToolId]

        freshness_rules : typing.Optional[str]
            Cron expression defining the freshness rules

        tags : typing.Optional[ContextVariableTagsUpdateParams]

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

        Returns
        -------
        ContextVariable
            Context variable type successfully updated

        Examples
        --------
        from parlant.client import ContextVariableTagsUpdateParams, ParlantClient, ToolId

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.update(
            variable_id="v9a8r7i6b5",
            name="UserBalance",
            description="Stores the account balances of users",
            tool_id=ToolId(
                service_name="finance_service",
                tool_name="balance_checker",
            ),
            freshness_rules="0 8,20 * * *",
            tags=ContextVariableTagsUpdateParams(
                add=["tag:123", "tag:456"],
                remove=["tag:789", "tag:012"],
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}",
            method="PATCH",
            json={
                "name": name,
                "description": description,
                "tool_id": convert_and_respect_annotation_metadata(
                    object_=tool_id, annotation=ToolId, direction="write"
                ),
                "freshness_rules": freshness_rules,
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags,
                    annotation=ContextVariableTagsUpdateParams,
                    direction="write",
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariable,
                    parse_obj_as(
                        type_=ContextVariable,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get_value(
        self,
        variable_id: str,
        key: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariableValue:
        """
        Retrieves a customer or tag value for the provided context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        key : str
            Key for the variable value

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

        Returns
        -------
        ContextVariableValue
            Retrieved context value for the customer or tag

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.get_value(
            variable_id="v9a8r7i6b5",
            key="user_1",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}/{jsonable_encoder(key)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariableValue,
                    parse_obj_as(
                        type_=ContextVariableValue,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def set_value(
        self,
        variable_id: str,
        key: str,
        *,
        data: typing.Optional[typing.Any] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariableValue:
        """
        Updates a customer or tag value for the provided context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        key : str
            Key for the variable value

        data : typing.Optional[typing.Any]

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

        Returns
        -------
        ContextVariableValue
            Context value successfully updated for the customer or tag

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.set_value(
            variable_id="v9a8r7i6b5",
            key="user_1",
            data={
                "balance": 5000.5,
                "currency": "USD",
                "last_transaction": "2024-03-23T15:30:00Z",
                "status": "active",
            },
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}/{jsonable_encoder(key)}",
            method="PUT",
            json={
                "data": data,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariableValue,
                    parse_obj_as(
                        type_=ContextVariableValue,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete_value(
        self,
        variable_id: str,
        key: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes a customer or tag value for the provided context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        key : str
            Key for the variable value

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

        Returns
        -------
        None

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.context_variables.delete_value(
            variable_id="v9a8r7i6b5",
            key="user_1",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}/{jsonable_encoder(key)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncContextVariablesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        tag_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[ContextVariable]:
        """
        Lists all context variables set for the provided tag or all context variables if no tag is provided

        Parameters
        ----------
        tag_id : typing.Optional[str]
            The tag ID to filter context variables by

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

        Returns
        -------
        typing.List[ContextVariable]
            List of all context variables

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.context_variables.list()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "context-variables",
            method="GET",
            params={
                "tag_id": tag_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[ContextVariable],
                    parse_obj_as(
                        type_=typing.List[ContextVariable],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create(
        self,
        *,
        name: str,
        description: typing.Optional[str] = OMIT,
        tool_id: typing.Optional[ToolId] = OMIT,
        freshness_rules: typing.Optional[str] = OMIT,
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariable:
        """
        Creates a new context variable

        Example uses:
        - Track subscription tiers to control feature access
        - Store usage patterns for personalized recommendations
        - Remember preferences for tailored responses

        Parameters
        ----------
        name : str
            Name of the context variable

        description : typing.Optional[str]
            Description of the context variable's purpose

        tool_id : typing.Optional[ToolId]

        freshness_rules : typing.Optional[str]
            Cron expression defining the freshness rules

        tags : typing.Optional[typing.Sequence[str]]
            List of tags associated with the context variable

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

        Returns
        -------
        ContextVariable
            Context variable type successfully created

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient, ToolId

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


        async def main() -> None:
            await client.context_variables.create(
                name="UserBalance",
                description="Stores the account balances of users",
                tool_id=ToolId(
                    service_name="finance_service",
                    tool_name="balance_checker",
                ),
                freshness_rules="30 2 * * *",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "context-variables",
            method="POST",
            json={
                "name": name,
                "description": description,
                "tool_id": convert_and_respect_annotation_metadata(
                    object_=tool_id, annotation=ToolId, direction="write"
                ),
                "freshness_rules": freshness_rules,
                "tags": tags,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariable,
                    parse_obj_as(
                        type_=ContextVariable,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete_many(
        self,
        *,
        tag_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes all context variables for the provided tag

        Parameters
        ----------
        tag_id : typing.Optional[str]
            The tag ID to filter context variables by

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.context_variables.delete_many()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "context-variables",
            method="DELETE",
            params={
                "tag_id": tag_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def retrieve(
        self,
        variable_id: str,
        *,
        include_values: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariableReadResult:
        """
        Retrieves a context variable's details and optionally its values.

        Can return all customer or tag values for this variable type if include_values=True.

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        include_values : typing.Optional[bool]
            Whether to include variable values in the response

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

        Returns
        -------
        ContextVariableReadResult
            Context variable details successfully retrieved

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.context_variables.retrieve(
                variable_id="v9a8r7i6b5",
                include_values=True,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}",
            method="GET",
            params={
                "include_values": include_values,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariableReadResult,
                    parse_obj_as(
                        type_=ContextVariableReadResult,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete(
        self,
        variable_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes a context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.context_variables.delete(
                variable_id="v9a8r7i6b5",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def update(
        self,
        variable_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        tool_id: typing.Optional[ToolId] = OMIT,
        freshness_rules: typing.Optional[str] = OMIT,
        tags: typing.Optional[ContextVariableTagsUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariable:
        """
        Updates an existing context variable.

        Only provided fields will be updated; others remain unchanged.

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        name : typing.Optional[str]
            Name of the context variable

        description : typing.Optional[str]
            Description of the context variable's purpose

        tool_id : typing.Optional[ToolId]

        freshness_rules : typing.Optional[str]
            Cron expression defining the freshness rules

        tags : typing.Optional[ContextVariableTagsUpdateParams]

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

        Returns
        -------
        ContextVariable
            Context variable type successfully updated

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient, ContextVariableTagsUpdateParams, ToolId

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


        async def main() -> None:
            await client.context_variables.update(
                variable_id="v9a8r7i6b5",
                name="UserBalance",
                description="Stores the account balances of users",
                tool_id=ToolId(
                    service_name="finance_service",
                    tool_name="balance_checker",
                ),
                freshness_rules="0 8,20 * * *",
                tags=ContextVariableTagsUpdateParams(
                    add=["tag:123", "tag:456"],
                    remove=["tag:789", "tag:012"],
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}",
            method="PATCH",
            json={
                "name": name,
                "description": description,
                "tool_id": convert_and_respect_annotation_metadata(
                    object_=tool_id, annotation=ToolId, direction="write"
                ),
                "freshness_rules": freshness_rules,
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags,
                    annotation=ContextVariableTagsUpdateParams,
                    direction="write",
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariable,
                    parse_obj_as(
                        type_=ContextVariable,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get_value(
        self,
        variable_id: str,
        key: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariableValue:
        """
        Retrieves a customer or tag value for the provided context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        key : str
            Key for the variable value

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

        Returns
        -------
        ContextVariableValue
            Retrieved context value for the customer or tag

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.context_variables.get_value(
                variable_id="v9a8r7i6b5",
                key="user_1",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}/{jsonable_encoder(key)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariableValue,
                    parse_obj_as(
                        type_=ContextVariableValue,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def set_value(
        self,
        variable_id: str,
        key: str,
        *,
        data: typing.Optional[typing.Any] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ContextVariableValue:
        """
        Updates a customer or tag value for the provided context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        key : str
            Key for the variable value

        data : typing.Optional[typing.Any]

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

        Returns
        -------
        ContextVariableValue
            Context value successfully updated for the customer or tag

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.context_variables.set_value(
                variable_id="v9a8r7i6b5",
                key="user_1",
                data={
                    "balance": 5000.5,
                    "currency": "USD",
                    "last_transaction": "2024-03-23T15:30:00Z",
                    "status": "active",
                },
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}/{jsonable_encoder(key)}",
            method="PUT",
            json={
                "data": data,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ContextVariableValue,
                    parse_obj_as(
                        type_=ContextVariableValue,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete_value(
        self,
        variable_id: str,
        key: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes a customer or tag value for the provided context variable

        Parameters
        ----------
        variable_id : str
            Unique identifier for the context variable

        key : str
            Key for the variable value

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.context_variables.delete_value(
                variable_id="v9a8r7i6b5",
                key="user_1",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"context-variables/{jsonable_encoder(variable_id)}/{jsonable_encoder(key)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
