|
| 1 | +"""Typed client for knowledge/entity API operations. |
| 2 | +
|
| 3 | +Encapsulates all /v2/projects/{project_id}/knowledge/* endpoints. |
| 4 | +""" |
| 5 | + |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from httpx import AsyncClient |
| 9 | + |
| 10 | +from basic_memory.mcp.tools.utils import call_get, call_post, call_put, call_patch, call_delete |
| 11 | +from basic_memory.schemas.response import EntityResponse, DeleteEntitiesResponse |
| 12 | + |
| 13 | + |
| 14 | +class KnowledgeClient: |
| 15 | + """Typed client for knowledge graph entity operations. |
| 16 | +
|
| 17 | + Centralizes: |
| 18 | + - API path construction for /v2/projects/{project_id}/knowledge/* |
| 19 | + - Response validation via Pydantic models |
| 20 | + - Consistent error handling through call_* utilities |
| 21 | +
|
| 22 | + Usage: |
| 23 | + async with get_client() as http_client: |
| 24 | + client = KnowledgeClient(http_client, project_id) |
| 25 | + entity = await client.create_entity(entity_data) |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, http_client: AsyncClient, project_id: str): |
| 29 | + """Initialize the knowledge client. |
| 30 | +
|
| 31 | + Args: |
| 32 | + http_client: HTTPX AsyncClient for making requests |
| 33 | + project_id: Project external_id (UUID) for API calls |
| 34 | + """ |
| 35 | + self.http_client = http_client |
| 36 | + self.project_id = project_id |
| 37 | + self._base_path = f"/v2/projects/{project_id}/knowledge" |
| 38 | + |
| 39 | + # --- Entity CRUD Operations --- |
| 40 | + |
| 41 | + async def create_entity(self, entity_data: dict[str, Any]) -> EntityResponse: |
| 42 | + """Create a new entity. |
| 43 | +
|
| 44 | + Args: |
| 45 | + entity_data: Entity data including title, content, folder, etc. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + EntityResponse with created entity details |
| 49 | +
|
| 50 | + Raises: |
| 51 | + ToolError: If the request fails |
| 52 | + """ |
| 53 | + response = await call_post( |
| 54 | + self.http_client, |
| 55 | + f"{self._base_path}/entities", |
| 56 | + json=entity_data, |
| 57 | + ) |
| 58 | + return EntityResponse.model_validate(response.json()) |
| 59 | + |
| 60 | + async def update_entity(self, entity_id: str, entity_data: dict[str, Any]) -> EntityResponse: |
| 61 | + """Update an existing entity (full replacement). |
| 62 | +
|
| 63 | + Args: |
| 64 | + entity_id: Entity external_id (UUID) |
| 65 | + entity_data: Complete entity data for replacement |
| 66 | +
|
| 67 | + Returns: |
| 68 | + EntityResponse with updated entity details |
| 69 | +
|
| 70 | + Raises: |
| 71 | + ToolError: If the request fails |
| 72 | + """ |
| 73 | + response = await call_put( |
| 74 | + self.http_client, |
| 75 | + f"{self._base_path}/entities/{entity_id}", |
| 76 | + json=entity_data, |
| 77 | + ) |
| 78 | + return EntityResponse.model_validate(response.json()) |
| 79 | + |
| 80 | + async def get_entity(self, entity_id: str) -> EntityResponse: |
| 81 | + """Get an entity by ID. |
| 82 | +
|
| 83 | + Args: |
| 84 | + entity_id: Entity external_id (UUID) |
| 85 | +
|
| 86 | + Returns: |
| 87 | + EntityResponse with entity details |
| 88 | +
|
| 89 | + Raises: |
| 90 | + ToolError: If the entity is not found or request fails |
| 91 | + """ |
| 92 | + response = await call_get( |
| 93 | + self.http_client, |
| 94 | + f"{self._base_path}/entities/{entity_id}", |
| 95 | + ) |
| 96 | + return EntityResponse.model_validate(response.json()) |
| 97 | + |
| 98 | + async def patch_entity(self, entity_id: str, patch_data: dict[str, Any]) -> EntityResponse: |
| 99 | + """Partially update an entity. |
| 100 | +
|
| 101 | + Args: |
| 102 | + entity_id: Entity external_id (UUID) |
| 103 | + patch_data: Partial entity data to update |
| 104 | +
|
| 105 | + Returns: |
| 106 | + EntityResponse with updated entity details |
| 107 | +
|
| 108 | + Raises: |
| 109 | + ToolError: If the request fails |
| 110 | + """ |
| 111 | + response = await call_patch( |
| 112 | + self.http_client, |
| 113 | + f"{self._base_path}/entities/{entity_id}", |
| 114 | + json=patch_data, |
| 115 | + ) |
| 116 | + return EntityResponse.model_validate(response.json()) |
| 117 | + |
| 118 | + async def delete_entity(self, entity_id: str) -> DeleteEntitiesResponse: |
| 119 | + """Delete an entity. |
| 120 | +
|
| 121 | + Args: |
| 122 | + entity_id: Entity external_id (UUID) |
| 123 | +
|
| 124 | + Returns: |
| 125 | + DeleteEntitiesResponse confirming deletion |
| 126 | +
|
| 127 | + Raises: |
| 128 | + ToolError: If the entity is not found or request fails |
| 129 | + """ |
| 130 | + response = await call_delete( |
| 131 | + self.http_client, |
| 132 | + f"{self._base_path}/entities/{entity_id}", |
| 133 | + ) |
| 134 | + return DeleteEntitiesResponse.model_validate(response.json()) |
| 135 | + |
| 136 | + async def move_entity(self, entity_id: str, destination: str) -> EntityResponse: |
| 137 | + """Move an entity to a new location. |
| 138 | +
|
| 139 | + Args: |
| 140 | + entity_id: Entity external_id (UUID) |
| 141 | + destination: New file path for the entity |
| 142 | +
|
| 143 | + Returns: |
| 144 | + EntityResponse with updated entity details |
| 145 | +
|
| 146 | + Raises: |
| 147 | + ToolError: If the request fails |
| 148 | + """ |
| 149 | + response = await call_put( |
| 150 | + self.http_client, |
| 151 | + f"{self._base_path}/entities/{entity_id}/move", |
| 152 | + json={"destination": destination}, |
| 153 | + ) |
| 154 | + return EntityResponse.model_validate(response.json()) |
| 155 | + |
| 156 | + # --- Resolution --- |
| 157 | + |
| 158 | + async def resolve_entity(self, identifier: str) -> str: |
| 159 | + """Resolve a string identifier to an entity external_id. |
| 160 | +
|
| 161 | + Args: |
| 162 | + identifier: The identifier to resolve (permalink, title, or path) |
| 163 | +
|
| 164 | + Returns: |
| 165 | + The resolved entity external_id (UUID) |
| 166 | +
|
| 167 | + Raises: |
| 168 | + ToolError: If the identifier cannot be resolved |
| 169 | + """ |
| 170 | + response = await call_post( |
| 171 | + self.http_client, |
| 172 | + f"{self._base_path}/resolve", |
| 173 | + json={"identifier": identifier}, |
| 174 | + ) |
| 175 | + data = response.json() |
| 176 | + return data["external_id"] |
0 commit comments