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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..commons.types.entity_id_base import EntityIdBase
from .types.knowledge_base_type import KnowledgeBaseType
from ..commons.types.precondition import Precondition
from ..core.request_options import RequestOptions
from .types.knowledge_base_response import KnowledgeBaseResponse
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.pydantic_utilities import parse_obj_as
from ..commons.errors.not_found_error import NotFoundError
from ..commons.types.error_message import ErrorMessage
from ..commons.errors.bad_request_error import BadRequestError
from ..commons.errors.server_error import ServerError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..core.jsonable_encoder import jsonable_encoder
from .types.knowledge_base_version_type import KnowledgeBaseVersionType
from .types.knowledge_base_version import KnowledgeBaseVersion
from .types.knowledge_document_content_type import KnowledgeDocumentContentType
import datetime as dt
from .types.knowledge_document_response import KnowledgeDocumentResponse
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def create_or_update_knowledge_base(
        self,
        *,
        knowledge_base_id: EntityIdBase,
        name: str,
        type: KnowledgeBaseType,
        url: typing.Optional[str] = OMIT,
        precondition: typing.Optional[Precondition] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeBaseResponse:
        """
        Update a knowledge base or create it if it doesn't exist.

        Parameters
        ----------
        knowledge_base_id : EntityIdBase
            ID that uniquely identifies this knowledge base

        name : str
            The name of the knowledge base

        type : KnowledgeBaseType
            The type of the knowledge base. Can not be changed once created.

        url : typing.Optional[str]
            The URL to pull content from for RSS and URL knowledge bases.

        precondition : typing.Optional[Precondition]
            (Beta) The preconditions that must be met for knowledge base be relevant to a conversation. Can be used to limit knowledge to certain types of users.

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

        Returns
        -------
        KnowledgeBaseResponse

        Examples
        --------
        from mavenagi import MavenAGI
        from mavenagi.commons import EntityIdBase

        client = MavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )
        client.knowledge.create_or_update_knowledge_base(
            knowledge_base_id=EntityIdBase(
                reference_id="help-center",
            ),
            name="Help center",
            type="API",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/knowledge",
            method="PUT",
            json={
                "knowledgeBaseId": convert_and_respect_annotation_metadata(
                    object_=knowledge_base_id, annotation=EntityIdBase, direction="write"
                ),
                "name": name,
                "type": type,
                "url": url,
                "precondition": convert_and_respect_annotation_metadata(
                    object_=precondition, annotation=Precondition, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_base(
        self, knowledge_base_reference_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> KnowledgeBaseResponse:
        """
        Get an existing knowledge base by its supplied ID

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to get. All other entity ID fields are inferred from the request.

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

        Returns
        -------
        KnowledgeBaseResponse

        Examples
        --------
        from mavenagi import MavenAGI

        client = MavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )
        client.knowledge.get_knowledge_base(
            knowledge_base_reference_id="help-center",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_base_version(
        self,
        knowledge_base_reference_id: str,
        *,
        type: KnowledgeBaseVersionType,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeBaseVersion:
        """
        Create a new knowledge base version. Only supported on API knowledge bases. Will throw an exception if there is an existing version in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to create a version for. All other entity ID fields are inferred from the request.

        type : KnowledgeBaseVersionType
            Indicates whether the completed version constitutes a full or partial refresh of the knowledge base. Deleting and updating documents is only supported for partial refreshes.

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

        Returns
        -------
        KnowledgeBaseVersion

        Examples
        --------
        from mavenagi import MavenAGI

        client = MavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )
        client.knowledge.create_knowledge_base_version(
            knowledge_base_reference_id="help-center",
            type="FULL",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/version",
            method="POST",
            json={
                "type": type,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseVersion,
                    parse_obj_as(
                        type_=KnowledgeBaseVersion,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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 finalize_knowledge_base_version(
        self, knowledge_base_reference_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Finalize the latest knowledge base version. Required to indicate the version is complete. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to finalize a version for. All other entity ID fields are inferred from the request.

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

        Returns
        -------
        None

        Examples
        --------
        from mavenagi import MavenAGI

        client = MavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )
        client.knowledge.finalize_knowledge_base_version(
            knowledge_base_reference_id="help-center",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/version/finalize",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_document(
        self,
        knowledge_base_reference_id: str,
        *,
        knowledge_document_id: EntityIdBase,
        content_type: KnowledgeDocumentContentType,
        content: str,
        title: str,
        url: typing.Optional[str] = OMIT,
        language: typing.Optional[str] = OMIT,
        created_at: typing.Optional[dt.datetime] = OMIT,
        updated_at: typing.Optional[dt.datetime] = OMIT,
        author: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeDocumentResponse:
        """
        Create knowledge document. Requires an existing knowledge base with an in progress version. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to create a document for. All other entity ID fields are inferred from the request.

        knowledge_document_id : EntityIdBase
            ID that uniquely identifies this knowledge document within its knowledge base

        content_type : KnowledgeDocumentContentType

        content : str
            The content of the document. Not shown directly to users. May be provided in HTML or markdown. HTML will be converted to markdown automatically. Images are not currently supported and will be ignored.

        title : str
            The title of the document. Will be shown as part of answers.

        url : typing.Optional[str]
            The URL of the document. Should be visible to end users. Will be shown as part of answers. Not used for crawling.

        language : typing.Optional[str]
            The document language. Must be a valid ISO 639-1 language code.

        created_at : typing.Optional[dt.datetime]
            The time at which this document was created.

        updated_at : typing.Optional[dt.datetime]
            The time at which this document was last modified.

        author : typing.Optional[str]
            The name of the author who created this document.

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

        Returns
        -------
        KnowledgeDocumentResponse

        Examples
        --------
        from mavenagi import MavenAGI
        from mavenagi.commons import EntityIdBase

        client = MavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )
        client.knowledge.create_knowledge_document(
            knowledge_base_reference_id="help-center",
            knowledge_document_id=EntityIdBase(
                reference_id="getting-started",
            ),
            content_type="MARKDOWN",
            content="## Getting started\\nThis is a getting started guide for the help center.",
            title="Getting started",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/document",
            method="POST",
            json={
                "knowledgeDocumentId": convert_and_respect_annotation_metadata(
                    object_=knowledge_document_id, annotation=EntityIdBase, direction="write"
                ),
                "contentType": content_type,
                "content": content,
                "title": title,
                "url": url,
                "language": language,
                "createdAt": created_at,
                "updatedAt": updated_at,
                "author": author,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeDocumentResponse,
                    parse_obj_as(
                        type_=KnowledgeDocumentResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_document(
        self,
        knowledge_base_reference_id: str,
        *,
        knowledge_document_id: EntityIdBase,
        content_type: KnowledgeDocumentContentType,
        content: str,
        title: str,
        url: typing.Optional[str] = OMIT,
        language: typing.Optional[str] = OMIT,
        created_at: typing.Optional[dt.datetime] = OMIT,
        updated_at: typing.Optional[dt.datetime] = OMIT,
        author: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeDocumentResponse:
        """
        Not yet implemented. Update knowledge document. Requires an existing knowledge base with an in progress version of type PARTIAL. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base that contains the document to update. All other entity ID fields are inferred from the request.

        knowledge_document_id : EntityIdBase
            ID that uniquely identifies this knowledge document within its knowledge base

        content_type : KnowledgeDocumentContentType

        content : str
            The content of the document. Not shown directly to users. May be provided in HTML or markdown. HTML will be converted to markdown automatically. Images are not currently supported and will be ignored.

        title : str
            The title of the document. Will be shown as part of answers.

        url : typing.Optional[str]
            The URL of the document. Should be visible to end users. Will be shown as part of answers. Not used for crawling.

        language : typing.Optional[str]
            The document language. Must be a valid ISO 639-1 language code.

        created_at : typing.Optional[dt.datetime]
            The time at which this document was created.

        updated_at : typing.Optional[dt.datetime]
            The time at which this document was last modified.

        author : typing.Optional[str]
            The name of the author who created this document.

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

        Returns
        -------
        KnowledgeDocumentResponse

        Examples
        --------
        from mavenagi import MavenAGI
        from mavenagi.commons import EntityIdBase

        client = MavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )
        client.knowledge.update_knowledge_document(
            knowledge_base_reference_id="help-center",
            knowledge_document_id=EntityIdBase(
                reference_id="getting-started",
            ),
            content_type="MARKDOWN",
            content="## Getting started\\nThis is a getting started guide for the help center.",
            title="Getting started",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/document",
            method="PUT",
            json={
                "knowledgeDocumentId": convert_and_respect_annotation_metadata(
                    object_=knowledge_document_id, annotation=EntityIdBase, direction="write"
                ),
                "contentType": content_type,
                "content": content,
                "title": title,
                "url": url,
                "language": language,
                "createdAt": created_at,
                "updatedAt": updated_at,
                "author": author,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeDocumentResponse,
                    parse_obj_as(
                        type_=KnowledgeDocumentResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_document(
        self,
        knowledge_base_reference_id: str,
        knowledge_document_reference_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Not yet implemented. Delete knowledge document. Requires an existing knowledge base with an in progress version of type PARTIAL. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base that contains the document to delete. All other entity ID fields are inferred from the request

        knowledge_document_reference_id : str
            The reference ID of the knowledge document to delete. All other entity ID fields are inferred from the request.

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

        Returns
        -------
        None

        Examples
        --------
        from mavenagi import MavenAGI

        client = MavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )
        client.knowledge.delete_knowledge_document(
            knowledge_base_reference_id="help-center",
            knowledge_document_reference_id="getting-started",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/{jsonable_encoder(knowledge_document_reference_id)}/document",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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 AsyncKnowledgeClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create_or_update_knowledge_base(
        self,
        *,
        knowledge_base_id: EntityIdBase,
        name: str,
        type: KnowledgeBaseType,
        url: typing.Optional[str] = OMIT,
        precondition: typing.Optional[Precondition] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeBaseResponse:
        """
        Update a knowledge base or create it if it doesn't exist.

        Parameters
        ----------
        knowledge_base_id : EntityIdBase
            ID that uniquely identifies this knowledge base

        name : str
            The name of the knowledge base

        type : KnowledgeBaseType
            The type of the knowledge base. Can not be changed once created.

        url : typing.Optional[str]
            The URL to pull content from for RSS and URL knowledge bases.

        precondition : typing.Optional[Precondition]
            (Beta) The preconditions that must be met for knowledge base be relevant to a conversation. Can be used to limit knowledge to certain types of users.

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

        Returns
        -------
        KnowledgeBaseResponse

        Examples
        --------
        import asyncio

        from mavenagi import AsyncMavenAGI
        from mavenagi.commons import EntityIdBase

        client = AsyncMavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )


        async def main() -> None:
            await client.knowledge.create_or_update_knowledge_base(
                knowledge_base_id=EntityIdBase(
                    reference_id="help-center",
                ),
                name="Help center",
                type="API",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/knowledge",
            method="PUT",
            json={
                "knowledgeBaseId": convert_and_respect_annotation_metadata(
                    object_=knowledge_base_id, annotation=EntityIdBase, direction="write"
                ),
                "name": name,
                "type": type,
                "url": url,
                "precondition": convert_and_respect_annotation_metadata(
                    object_=precondition, annotation=Precondition, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_base(
        self, knowledge_base_reference_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> KnowledgeBaseResponse:
        """
        Get an existing knowledge base by its supplied ID

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to get. All other entity ID fields are inferred from the request.

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

        Returns
        -------
        KnowledgeBaseResponse

        Examples
        --------
        import asyncio

        from mavenagi import AsyncMavenAGI

        client = AsyncMavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )


        async def main() -> None:
            await client.knowledge.get_knowledge_base(
                knowledge_base_reference_id="help-center",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_base_version(
        self,
        knowledge_base_reference_id: str,
        *,
        type: KnowledgeBaseVersionType,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeBaseVersion:
        """
        Create a new knowledge base version. Only supported on API knowledge bases. Will throw an exception if there is an existing version in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to create a version for. All other entity ID fields are inferred from the request.

        type : KnowledgeBaseVersionType
            Indicates whether the completed version constitutes a full or partial refresh of the knowledge base. Deleting and updating documents is only supported for partial refreshes.

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

        Returns
        -------
        KnowledgeBaseVersion

        Examples
        --------
        import asyncio

        from mavenagi import AsyncMavenAGI

        client = AsyncMavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )


        async def main() -> None:
            await client.knowledge.create_knowledge_base_version(
                knowledge_base_reference_id="help-center",
                type="FULL",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/version",
            method="POST",
            json={
                "type": type,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseVersion,
                    parse_obj_as(
                        type_=KnowledgeBaseVersion,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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 finalize_knowledge_base_version(
        self, knowledge_base_reference_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Finalize the latest knowledge base version. Required to indicate the version is complete. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to finalize a version for. All other entity ID fields are inferred from the request.

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from mavenagi import AsyncMavenAGI

        client = AsyncMavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )


        async def main() -> None:
            await client.knowledge.finalize_knowledge_base_version(
                knowledge_base_reference_id="help-center",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/version/finalize",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_document(
        self,
        knowledge_base_reference_id: str,
        *,
        knowledge_document_id: EntityIdBase,
        content_type: KnowledgeDocumentContentType,
        content: str,
        title: str,
        url: typing.Optional[str] = OMIT,
        language: typing.Optional[str] = OMIT,
        created_at: typing.Optional[dt.datetime] = OMIT,
        updated_at: typing.Optional[dt.datetime] = OMIT,
        author: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeDocumentResponse:
        """
        Create knowledge document. Requires an existing knowledge base with an in progress version. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base to create a document for. All other entity ID fields are inferred from the request.

        knowledge_document_id : EntityIdBase
            ID that uniquely identifies this knowledge document within its knowledge base

        content_type : KnowledgeDocumentContentType

        content : str
            The content of the document. Not shown directly to users. May be provided in HTML or markdown. HTML will be converted to markdown automatically. Images are not currently supported and will be ignored.

        title : str
            The title of the document. Will be shown as part of answers.

        url : typing.Optional[str]
            The URL of the document. Should be visible to end users. Will be shown as part of answers. Not used for crawling.

        language : typing.Optional[str]
            The document language. Must be a valid ISO 639-1 language code.

        created_at : typing.Optional[dt.datetime]
            The time at which this document was created.

        updated_at : typing.Optional[dt.datetime]
            The time at which this document was last modified.

        author : typing.Optional[str]
            The name of the author who created this document.

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

        Returns
        -------
        KnowledgeDocumentResponse

        Examples
        --------
        import asyncio

        from mavenagi import AsyncMavenAGI
        from mavenagi.commons import EntityIdBase

        client = AsyncMavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )


        async def main() -> None:
            await client.knowledge.create_knowledge_document(
                knowledge_base_reference_id="help-center",
                knowledge_document_id=EntityIdBase(
                    reference_id="getting-started",
                ),
                content_type="MARKDOWN",
                content="## Getting started\\nThis is a getting started guide for the help center.",
                title="Getting started",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/document",
            method="POST",
            json={
                "knowledgeDocumentId": convert_and_respect_annotation_metadata(
                    object_=knowledge_document_id, annotation=EntityIdBase, direction="write"
                ),
                "contentType": content_type,
                "content": content,
                "title": title,
                "url": url,
                "language": language,
                "createdAt": created_at,
                "updatedAt": updated_at,
                "author": author,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeDocumentResponse,
                    parse_obj_as(
                        type_=KnowledgeDocumentResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_document(
        self,
        knowledge_base_reference_id: str,
        *,
        knowledge_document_id: EntityIdBase,
        content_type: KnowledgeDocumentContentType,
        content: str,
        title: str,
        url: typing.Optional[str] = OMIT,
        language: typing.Optional[str] = OMIT,
        created_at: typing.Optional[dt.datetime] = OMIT,
        updated_at: typing.Optional[dt.datetime] = OMIT,
        author: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeDocumentResponse:
        """
        Not yet implemented. Update knowledge document. Requires an existing knowledge base with an in progress version of type PARTIAL. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base that contains the document to update. All other entity ID fields are inferred from the request.

        knowledge_document_id : EntityIdBase
            ID that uniquely identifies this knowledge document within its knowledge base

        content_type : KnowledgeDocumentContentType

        content : str
            The content of the document. Not shown directly to users. May be provided in HTML or markdown. HTML will be converted to markdown automatically. Images are not currently supported and will be ignored.

        title : str
            The title of the document. Will be shown as part of answers.

        url : typing.Optional[str]
            The URL of the document. Should be visible to end users. Will be shown as part of answers. Not used for crawling.

        language : typing.Optional[str]
            The document language. Must be a valid ISO 639-1 language code.

        created_at : typing.Optional[dt.datetime]
            The time at which this document was created.

        updated_at : typing.Optional[dt.datetime]
            The time at which this document was last modified.

        author : typing.Optional[str]
            The name of the author who created this document.

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

        Returns
        -------
        KnowledgeDocumentResponse

        Examples
        --------
        import asyncio

        from mavenagi import AsyncMavenAGI
        from mavenagi.commons import EntityIdBase

        client = AsyncMavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )


        async def main() -> None:
            await client.knowledge.update_knowledge_document(
                knowledge_base_reference_id="help-center",
                knowledge_document_id=EntityIdBase(
                    reference_id="getting-started",
                ),
                content_type="MARKDOWN",
                content="## Getting started\\nThis is a getting started guide for the help center.",
                title="Getting started",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/document",
            method="PUT",
            json={
                "knowledgeDocumentId": convert_and_respect_annotation_metadata(
                    object_=knowledge_document_id, annotation=EntityIdBase, direction="write"
                ),
                "contentType": content_type,
                "content": content,
                "title": title,
                "url": url,
                "language": language,
                "createdAt": created_at,
                "updatedAt": updated_at,
                "author": author,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeDocumentResponse,
                    parse_obj_as(
                        type_=KnowledgeDocumentResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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_knowledge_document(
        self,
        knowledge_base_reference_id: str,
        knowledge_document_reference_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Not yet implemented. Delete knowledge document. Requires an existing knowledge base with an in progress version of type PARTIAL. Will throw an exception if the latest version is not in progress.

        Parameters
        ----------
        knowledge_base_reference_id : str
            The reference ID of the knowledge base that contains the document to delete. All other entity ID fields are inferred from the request

        knowledge_document_reference_id : str
            The reference ID of the knowledge document to delete. All other entity ID fields are inferred from the request.

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from mavenagi import AsyncMavenAGI

        client = AsyncMavenAGI(
            organization_id="YOUR_ORGANIZATION_ID",
            agent_id="YOUR_AGENT_ID",
            app_id="YOUR_APP_ID",
            app_secret="YOUR_APP_SECRET",
        )


        async def main() -> None:
            await client.knowledge.delete_knowledge_document(
                knowledge_base_reference_id="help-center",
                knowledge_document_reference_id="getting-started",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/knowledge/{jsonable_encoder(knowledge_base_reference_id)}/{jsonable_encoder(knowledge_document_reference_id)}/document",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise ServerError(
                    typing.cast(
                        ErrorMessage,
                        parse_obj_as(
                            type_=ErrorMessage,  # 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)
