Skip to content

Commit 8043c18

Browse files
abrookinsclaude
andcommitted
Add context_usage_percentage field and tests to SDK client
- Add context_usage_percentage field to WorkingMemoryResponse model - Add comprehensive test suite for the new field covering: - Field creation and default values - Serialization behavior - Validation of different percentage values - Dictionary-to-model conversion 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent b5c55d1 commit 8043c18

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

agent-memory-client/agent_memory_client/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ class SessionListResponse(BaseModel):
215215
class WorkingMemoryResponse(WorkingMemory):
216216
"""Response from working memory operations"""
217217

218-
pass
218+
context_usage_percentage: float | None = Field(
219+
default=None,
220+
description="Percentage of context window used before auto-summarization triggers (0-100)",
221+
)
219222

220223

221224
class MemoryRecordResult(MemoryRecord):

agent-memory-client/tests/test_client.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,113 @@ def test_validation_with_none_values(self, enhanced_test_client):
653653

654654
# Should not raise
655655
enhanced_test_client.validate_memory_record(memory)
656+
657+
658+
class TestContextUsagePercentage:
659+
"""Tests for context usage percentage functionality."""
660+
661+
@pytest.mark.asyncio
662+
async def test_working_memory_response_with_context_percentage(
663+
self, enhanced_test_client
664+
):
665+
"""Test that WorkingMemoryResponse properly handles context_usage_percentage field."""
666+
session_id = "test-session"
667+
668+
# Test with context percentage set
669+
working_memory_response = WorkingMemoryResponse(
670+
session_id=session_id,
671+
messages=[],
672+
memories=[],
673+
data={},
674+
context=None,
675+
user_id=None,
676+
context_usage_percentage=45.5,
677+
)
678+
679+
assert working_memory_response.context_usage_percentage == 45.5
680+
assert working_memory_response.session_id == session_id
681+
682+
# Test with None context percentage (default)
683+
working_memory_response_none = WorkingMemoryResponse(
684+
session_id=session_id,
685+
messages=[],
686+
memories=[],
687+
data={},
688+
context=None,
689+
user_id=None,
690+
)
691+
692+
assert working_memory_response_none.context_usage_percentage is None
693+
694+
@pytest.mark.asyncio
695+
async def test_context_percentage_serialization(self, enhanced_test_client):
696+
"""Test that context_usage_percentage is properly serialized."""
697+
session_id = "test-session"
698+
699+
# Create response with context percentage
700+
working_memory_response = WorkingMemoryResponse(
701+
session_id=session_id,
702+
messages=[],
703+
memories=[],
704+
data={},
705+
context=None,
706+
user_id=None,
707+
context_usage_percentage=75.0,
708+
)
709+
710+
# Test model_dump includes the field
711+
dumped = working_memory_response.model_dump()
712+
assert "context_usage_percentage" in dumped
713+
assert dumped["context_usage_percentage"] == 75.0
714+
715+
# Test JSON serialization
716+
json_data = working_memory_response.model_dump_json()
717+
assert "context_usage_percentage" in json_data
718+
assert "75.0" in json_data
719+
720+
@pytest.mark.asyncio
721+
async def test_context_percentage_validation(self, enhanced_test_client):
722+
"""Test that context_usage_percentage accepts valid values."""
723+
session_id = "test-session"
724+
725+
# Test valid percentages
726+
valid_percentages = [0.0, 25.5, 50.0, 99.9, 100.0, None]
727+
728+
for percentage in valid_percentages:
729+
working_memory_response = WorkingMemoryResponse(
730+
session_id=session_id,
731+
messages=[],
732+
memories=[],
733+
data={},
734+
context=None,
735+
user_id=None,
736+
context_usage_percentage=percentage,
737+
)
738+
assert working_memory_response.context_usage_percentage == percentage
739+
740+
def test_working_memory_response_from_dict_with_context_percentage(self):
741+
"""Test that WorkingMemoryResponse can be created from dict with context_usage_percentage."""
742+
session_id = "test-session"
743+
744+
# Test creating WorkingMemoryResponse from dict (simulating API response parsing)
745+
response_dict = {
746+
"session_id": session_id,
747+
"messages": [],
748+
"memories": [],
749+
"data": {},
750+
"context": None,
751+
"user_id": None,
752+
"context_usage_percentage": 33.3,
753+
"tokens": 0,
754+
"namespace": None,
755+
"ttl_seconds": None,
756+
"last_accessed": "2024-01-01T00:00:00Z",
757+
}
758+
759+
# This simulates what happens when the API client parses the JSON response
760+
result = WorkingMemoryResponse(**response_dict)
761+
762+
# Verify the context_usage_percentage is included
763+
assert isinstance(result, WorkingMemoryResponse)
764+
assert result.context_usage_percentage == 33.3
765+
assert result.session_id == session_id

docs/memory-types.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,8 @@ Long-term memory supports three types of memories:
202202
# Create long-term memories
203203
POST /v1/long-term-memory/
204204
205-
# Search long-term memories only
205+
# Search long-term memories
206206
POST /v1/long-term-memory/search
207-
208-
# Search across all memory types
209-
POST /v1/memory/search
210207
```
211208

212209
### Search Capabilities

dump.rdb

88 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)