quivr/backend/modules/brain/repository/composite_brains_connections.py
Zineb El Bachiri 90c4a44525
feat: composite brains get (#1837)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2023-12-06 15:44:36 +01:00

62 lines
2.0 KiB
Python

from uuid import UUID
from logger import get_logger
from models.settings import get_supabase_client
from modules.brain.entity.composite_brain_connection_entity import (
CompositeBrainConnectionEntity,
)
from modules.brain.repository.interfaces import CompositeBrainsConnectionsInterface
logger = get_logger(__name__)
class CompositeBrainsConnections(CompositeBrainsConnectionsInterface):
def __init__(self):
self.db = get_supabase_client()
def connect_brain(
self, composite_brain_id: UUID, connected_brain_id: UUID
) -> CompositeBrainConnectionEntity:
response = (
self.db.table("composite_brain_connections")
.insert(
{
"composite_brain_id": str(composite_brain_id),
"connected_brain_id": str(connected_brain_id),
}
)
.execute()
)
return CompositeBrainConnectionEntity(**response.data[0])
def get_connected_brains(self, composite_brain_id: UUID) -> list[UUID]:
response = (
self.db.from_("composite_brain_connections")
.select("connected_brain_id")
.filter("composite_brain_id", "eq", str(composite_brain_id))
.execute()
)
return [item["connected_brain_id"] for item in response.data]
def disconnect_brain(
self, composite_brain_id: UUID, connected_brain_id: UUID
) -> None:
self.db.table("composite_brain_connections").delete().match(
{
"composite_brain_id": composite_brain_id,
"connected_brain_id": connected_brain_id,
}
).execute()
def is_connected_brain(self, brain_id: UUID) -> bool:
response = (
self.db.from_("composite_brain_connections")
.select("connected_brain_id")
.filter("connected_brain_id", "eq", str(brain_id))
.execute()
)
return len(response.data) > 0