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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.add_thread_to_space_response import AddThreadToSpaceResponse
from ..types.add_vector_store_to_space_response import AddVectorStoreToSpaceResponse
from ..types.space_list_response import SpaceListResponse
from ..types.space_response import SpaceResponse
from ..types.space_thread_list_response import SpaceThreadListResponse
from ..types.space_vector_store_list_response import SpaceVectorStoreListResponse

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


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

    def list_spaces(
        self,
        *,
        organization_id: str,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SpaceListResponse]:
        """
        List all spaces user has access to in an organization

        Parameters
        ----------
        organization_id : str
            Organization ID to filter by

        limit : typing.Optional[int]
            Maximum number of results

        cursor : typing.Optional[str]
            Pagination cursor

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

        Returns
        -------
        HttpResponse[SpaceListResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/spaces/",
            method="GET",
            params={
                "organization_id": organization_id,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceListResponse,
                    parse_obj_as(
                        type_=SpaceListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def create_space(
        self,
        *,
        organization_id: str,
        name: str,
        description: typing.Optional[str] = OMIT,
        access_mode: typing.Optional[str] = OMIT,
        access_users: typing.Optional[typing.Sequence[str]] = OMIT,
        access_departments: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_users: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        visible_to_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SpaceResponse]:
        """
        Create a new space with auto-linked vector store for memory management

        Parameters
        ----------
        organization_id : str
            Organization ID

        name : str
            Space name

        description : typing.Optional[str]
            Optional space description

        access_mode : typing.Optional[str]
            Access mode: private, organization, or public

        access_users : typing.Optional[typing.Sequence[str]]
            User IDs with view access

        access_departments : typing.Optional[typing.Sequence[str]]
            Department IDs with view access

        editable_by_users : typing.Optional[typing.Sequence[str]]
            User IDs with edit access (supports wildcard '*')

        editable_by_roles : typing.Optional[typing.Sequence[str]]
            Role IDs with edit access

        visible_to_roles : typing.Optional[typing.Sequence[str]]
            Role IDs with view access

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

        Returns
        -------
        HttpResponse[SpaceResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/spaces/",
            method="POST",
            json={
                "organization_id": organization_id,
                "name": name,
                "description": description,
                "access_mode": access_mode,
                "access_users": access_users,
                "access_departments": access_departments,
                "editable_by_users": editable_by_users,
                "editable_by_roles": editable_by_roles,
                "visible_to_roles": visible_to_roles,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceResponse,
                    parse_obj_as(
                        type_=SpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get_space(
        self, space_id: str, *, organization_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[SpaceResponse]:
        """
        Retrieve space details by ID with access validation

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        HttpResponse[SpaceResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}",
            method="GET",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceResponse,
                    parse_obj_as(
                        type_=SpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def delete_space(
        self, space_id: str, *, organization_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[None]:
        """
        Soft delete space (requires owner access)

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}",
            method="DELETE",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def update_space(
        self,
        space_id: str,
        *,
        organization_id: str,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        access_mode: typing.Optional[str] = OMIT,
        access_users: typing.Optional[typing.Sequence[str]] = OMIT,
        access_departments: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_users: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        visible_to_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SpaceResponse]:
        """
        Update space properties with access validation

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

        name : typing.Optional[str]
            New space name

        description : typing.Optional[str]
            New space description

        access_mode : typing.Optional[str]
            New access mode

        access_users : typing.Optional[typing.Sequence[str]]
            New access users list

        access_departments : typing.Optional[typing.Sequence[str]]
            New access departments list

        editable_by_users : typing.Optional[typing.Sequence[str]]
            New editable users list

        editable_by_roles : typing.Optional[typing.Sequence[str]]
            New editable roles list

        visible_to_roles : typing.Optional[typing.Sequence[str]]
            New visible roles list

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

        Returns
        -------
        HttpResponse[SpaceResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}",
            method="PATCH",
            params={
                "organization_id": organization_id,
            },
            json={
                "name": name,
                "description": description,
                "access_mode": access_mode,
                "access_users": access_users,
                "access_departments": access_departments,
                "editable_by_users": editable_by_users,
                "editable_by_roles": editable_by_roles,
                "visible_to_roles": visible_to_roles,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceResponse,
                    parse_obj_as(
                        type_=SpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def add_thread_to_space(
        self,
        space_id: str,
        thread_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[AddThreadToSpaceResponse]:
        """
        Associate a thread with a space (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        thread_id : str
            Thread ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        HttpResponse[AddThreadToSpaceResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/threads/{jsonable_encoder(thread_id)}",
            method="POST",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AddThreadToSpaceResponse,
                    parse_obj_as(
                        type_=AddThreadToSpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def remove_thread_from_space(
        self,
        space_id: str,
        thread_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[None]:
        """
        Remove thread from space and cleanup vectors (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        thread_id : str
            Thread ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/threads/{jsonable_encoder(thread_id)}",
            method="DELETE",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def list_space_threads(
        self,
        space_id: str,
        *,
        organization_id: str,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SpaceThreadListResponse]:
        """
        List all threads associated with a space (requires view access)

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

        limit : typing.Optional[int]
            Maximum number of threads to return

        cursor : typing.Optional[str]
            Pagination cursor from previous response

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

        Returns
        -------
        HttpResponse[SpaceThreadListResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/threads",
            method="GET",
            params={
                "organization_id": organization_id,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceThreadListResponse,
                    parse_obj_as(
                        type_=SpaceThreadListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def add_vector_store_to_space(
        self,
        space_id: str,
        vector_store_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[AddVectorStoreToSpaceResponse]:
        """
        Attach an additional external vector store to a space (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        vector_store_id : str
            Vector store ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        HttpResponse[AddVectorStoreToSpaceResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/vector-stores/{jsonable_encoder(vector_store_id)}",
            method="POST",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AddVectorStoreToSpaceResponse,
                    parse_obj_as(
                        type_=AddVectorStoreToSpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def remove_vector_store_from_space(
        self,
        space_id: str,
        vector_store_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[None]:
        """
        Remove an additional vector store from a space (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        vector_store_id : str
            Vector store ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/vector-stores/{jsonable_encoder(vector_store_id)}",
            method="DELETE",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def list_space_vector_stores(
        self, space_id: str, *, organization_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[SpaceVectorStoreListResponse]:
        """
        List all additional vector stores attached to a space (requires view access)

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        HttpResponse[SpaceVectorStoreListResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/vector-stores",
            method="GET",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceVectorStoreListResponse,
                    parse_obj_as(
                        type_=SpaceVectorStoreListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


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

    async def list_spaces(
        self,
        *,
        organization_id: str,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SpaceListResponse]:
        """
        List all spaces user has access to in an organization

        Parameters
        ----------
        organization_id : str
            Organization ID to filter by

        limit : typing.Optional[int]
            Maximum number of results

        cursor : typing.Optional[str]
            Pagination cursor

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

        Returns
        -------
        AsyncHttpResponse[SpaceListResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/spaces/",
            method="GET",
            params={
                "organization_id": organization_id,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceListResponse,
                    parse_obj_as(
                        type_=SpaceListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def create_space(
        self,
        *,
        organization_id: str,
        name: str,
        description: typing.Optional[str] = OMIT,
        access_mode: typing.Optional[str] = OMIT,
        access_users: typing.Optional[typing.Sequence[str]] = OMIT,
        access_departments: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_users: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        visible_to_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SpaceResponse]:
        """
        Create a new space with auto-linked vector store for memory management

        Parameters
        ----------
        organization_id : str
            Organization ID

        name : str
            Space name

        description : typing.Optional[str]
            Optional space description

        access_mode : typing.Optional[str]
            Access mode: private, organization, or public

        access_users : typing.Optional[typing.Sequence[str]]
            User IDs with view access

        access_departments : typing.Optional[typing.Sequence[str]]
            Department IDs with view access

        editable_by_users : typing.Optional[typing.Sequence[str]]
            User IDs with edit access (supports wildcard '*')

        editable_by_roles : typing.Optional[typing.Sequence[str]]
            Role IDs with edit access

        visible_to_roles : typing.Optional[typing.Sequence[str]]
            Role IDs with view access

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

        Returns
        -------
        AsyncHttpResponse[SpaceResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/spaces/",
            method="POST",
            json={
                "organization_id": organization_id,
                "name": name,
                "description": description,
                "access_mode": access_mode,
                "access_users": access_users,
                "access_departments": access_departments,
                "editable_by_users": editable_by_users,
                "editable_by_roles": editable_by_roles,
                "visible_to_roles": visible_to_roles,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceResponse,
                    parse_obj_as(
                        type_=SpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get_space(
        self, space_id: str, *, organization_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[SpaceResponse]:
        """
        Retrieve space details by ID with access validation

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        AsyncHttpResponse[SpaceResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}",
            method="GET",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceResponse,
                    parse_obj_as(
                        type_=SpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def delete_space(
        self, space_id: str, *, organization_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[None]:
        """
        Soft delete space (requires owner access)

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}",
            method="DELETE",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def update_space(
        self,
        space_id: str,
        *,
        organization_id: str,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        access_mode: typing.Optional[str] = OMIT,
        access_users: typing.Optional[typing.Sequence[str]] = OMIT,
        access_departments: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_users: typing.Optional[typing.Sequence[str]] = OMIT,
        editable_by_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        visible_to_roles: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SpaceResponse]:
        """
        Update space properties with access validation

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

        name : typing.Optional[str]
            New space name

        description : typing.Optional[str]
            New space description

        access_mode : typing.Optional[str]
            New access mode

        access_users : typing.Optional[typing.Sequence[str]]
            New access users list

        access_departments : typing.Optional[typing.Sequence[str]]
            New access departments list

        editable_by_users : typing.Optional[typing.Sequence[str]]
            New editable users list

        editable_by_roles : typing.Optional[typing.Sequence[str]]
            New editable roles list

        visible_to_roles : typing.Optional[typing.Sequence[str]]
            New visible roles list

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

        Returns
        -------
        AsyncHttpResponse[SpaceResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}",
            method="PATCH",
            params={
                "organization_id": organization_id,
            },
            json={
                "name": name,
                "description": description,
                "access_mode": access_mode,
                "access_users": access_users,
                "access_departments": access_departments,
                "editable_by_users": editable_by_users,
                "editable_by_roles": editable_by_roles,
                "visible_to_roles": visible_to_roles,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceResponse,
                    parse_obj_as(
                        type_=SpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def add_thread_to_space(
        self,
        space_id: str,
        thread_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[AddThreadToSpaceResponse]:
        """
        Associate a thread with a space (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        thread_id : str
            Thread ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        AsyncHttpResponse[AddThreadToSpaceResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/threads/{jsonable_encoder(thread_id)}",
            method="POST",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AddThreadToSpaceResponse,
                    parse_obj_as(
                        type_=AddThreadToSpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def remove_thread_from_space(
        self,
        space_id: str,
        thread_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[None]:
        """
        Remove thread from space and cleanup vectors (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        thread_id : str
            Thread ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/threads/{jsonable_encoder(thread_id)}",
            method="DELETE",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def list_space_threads(
        self,
        space_id: str,
        *,
        organization_id: str,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SpaceThreadListResponse]:
        """
        List all threads associated with a space (requires view access)

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

        limit : typing.Optional[int]
            Maximum number of threads to return

        cursor : typing.Optional[str]
            Pagination cursor from previous response

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

        Returns
        -------
        AsyncHttpResponse[SpaceThreadListResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/threads",
            method="GET",
            params={
                "organization_id": organization_id,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceThreadListResponse,
                    parse_obj_as(
                        type_=SpaceThreadListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def add_vector_store_to_space(
        self,
        space_id: str,
        vector_store_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[AddVectorStoreToSpaceResponse]:
        """
        Attach an additional external vector store to a space (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        vector_store_id : str
            Vector store ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        AsyncHttpResponse[AddVectorStoreToSpaceResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/vector-stores/{jsonable_encoder(vector_store_id)}",
            method="POST",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AddVectorStoreToSpaceResponse,
                    parse_obj_as(
                        type_=AddVectorStoreToSpaceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def remove_vector_store_from_space(
        self,
        space_id: str,
        vector_store_id: str,
        *,
        organization_id: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[None]:
        """
        Remove an additional vector store from a space (requires edit access)

        Parameters
        ----------
        space_id : str
            Space ID

        vector_store_id : str
            Vector store ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/vector-stores/{jsonable_encoder(vector_store_id)}",
            method="DELETE",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def list_space_vector_stores(
        self, space_id: str, *, organization_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[SpaceVectorStoreListResponse]:
        """
        List all additional vector stores attached to a space (requires view access)

        Parameters
        ----------
        space_id : str
            Space ID

        organization_id : str
            Organization ID

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

        Returns
        -------
        AsyncHttpResponse[SpaceVectorStoreListResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/spaces/{jsonable_encoder(space_id)}/vector-stores",
            method="GET",
            params={
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SpaceVectorStoreListResponse,
                    parse_obj_as(
                        type_=SpaceVectorStoreListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
