mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
get entity content via tool
This commit is contained in:
@@ -146,11 +146,17 @@ for dep in deps:
|
||||
],
|
||||
output_model=EntityResponse
|
||||
)
|
||||
async def get_entity(path_id: PathId) -> EntityResponse:
|
||||
"""Get a specific entity by its path_id."""
|
||||
async def get_entity(path_id: PathId, content: bool = False) -> EntityResponse:
|
||||
"""Get a specific entity by its path_id.
|
||||
|
||||
Args:
|
||||
path_id: Path identifier for the entity
|
||||
content: If True, includes the full markdown content of the entity
|
||||
"""
|
||||
try:
|
||||
url = f"/knowledge/entities/{path_id}"
|
||||
response = await client.get(url)
|
||||
params = {"content": "true"} if content else {}
|
||||
response = await client.get(url, params=params)
|
||||
if response.status_code == 404:
|
||||
raise EntityNotFoundError(f"Entity not found: {path_id}")
|
||||
response.raise_for_status()
|
||||
|
||||
@@ -57,8 +57,11 @@ class EntityOperations:
|
||||
# 1. Create entity in DB
|
||||
db_entity = await self.entity_service.create_entity(entity)
|
||||
|
||||
# if content is provided use that, otherwise write the entity info
|
||||
content = entity.content or None
|
||||
|
||||
# 2. Write file and get checksum
|
||||
_, checksum = await self.file_operations.write_entity_file(db_entity, content=entity.content)
|
||||
_, checksum = await self.file_operations.write_entity_file(db_entity, content=content)
|
||||
|
||||
# 3. Update DB with checksum
|
||||
updated = await self.entity_service.update_entity(
|
||||
|
||||
@@ -25,7 +25,7 @@ async def test_get_basic_entity(client):
|
||||
create_result = await create_entities(entity_request)
|
||||
path_id = create_result.entities[0].path_id
|
||||
|
||||
# Get the entity
|
||||
# Get the entity without content
|
||||
entity = await get_entity(path_id)
|
||||
|
||||
# Verify entity details
|
||||
@@ -40,6 +40,36 @@ async def test_get_basic_entity(client):
|
||||
assert obs.content == "First observation"
|
||||
assert obs.category == ObservationCategory.NOTE
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_entity_with_content(client):
|
||||
"""Test retrieving a basic entity."""
|
||||
# First create an entity
|
||||
entity_request = CreateEntityRequest(
|
||||
entities=[
|
||||
Entity(
|
||||
name="TestEntity",
|
||||
entity_type="test",
|
||||
content="A test entity",
|
||||
observations=["First observation"],
|
||||
)
|
||||
]
|
||||
)
|
||||
create_result = await create_entities(entity_request)
|
||||
path_id = create_result.entities[0].path_id
|
||||
|
||||
# Get entity with content
|
||||
entity = await get_entity(path_id, content=True)
|
||||
assert entity.content is not None
|
||||
|
||||
# if we passed in content, it should just be the
|
||||
assert "A test entity" in entity.content
|
||||
|
||||
# Check observations
|
||||
assert len(entity.observations) == 1
|
||||
obs = entity.observations[0]
|
||||
assert obs.content == "First observation"
|
||||
assert obs.category == ObservationCategory.NOTE
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_entity_with_relations(client):
|
||||
@@ -65,7 +95,7 @@ async def test_get_entity_with_relations(client):
|
||||
)
|
||||
await create_relations(relation_request)
|
||||
|
||||
# Get and verify source entity
|
||||
# Get and verify source entity without content
|
||||
source = await get_entity("source_entity")
|
||||
assert len(source.relations) == 1
|
||||
relation = source.relations[0]
|
||||
@@ -99,7 +129,7 @@ async def test_get_entity_with_categorized_observations(client):
|
||||
)
|
||||
await add_observations(obs_request)
|
||||
|
||||
# Get and verify entity
|
||||
# Get and verify entity without content
|
||||
entity = await get_entity(path_id)
|
||||
assert len(entity.observations) == 3
|
||||
categories = {obs.category for obs in entity.observations}
|
||||
|
||||
Reference in New Issue
Block a user