mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: Expand add_memory to accept MemoryEntry
The `add_memory` methods in `Context` and `BaseMemoryService` now accept `MemoryEntry` objects in addition to strings. The Vertex AI Memory Bank service implementation is updated to handle these new types Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 872108561
This commit is contained in:
committed by
Copybara-Service
parent
2d8b6a2f5b
commit
f27a9cfb87
@@ -22,7 +22,9 @@ from google.adk.agents.callback_context import CallbackContext
|
||||
from google.adk.auth.auth_credential import AuthCredential
|
||||
from google.adk.auth.auth_credential import AuthCredentialTypes
|
||||
from google.adk.auth.auth_tool import AuthConfig
|
||||
from google.adk.memory.memory_entry import MemoryEntry
|
||||
from google.adk.tools.tool_context import ToolContext
|
||||
from google.genai import types
|
||||
from google.genai.types import Part
|
||||
import pytest
|
||||
|
||||
@@ -417,7 +419,9 @@ class TestCallbackContextAddEventsToMemory:
|
||||
"""Tests that add_memory forwards memories and metadata."""
|
||||
memory_service = AsyncMock()
|
||||
mock_invocation_context.memory_service = memory_service
|
||||
memories = ["fact one"]
|
||||
memories = [
|
||||
MemoryEntry(content=types.Content(parts=[types.Part(text="fact one")]))
|
||||
]
|
||||
metadata = {"ttl": "6000s"}
|
||||
|
||||
context = CallbackContext(mock_invocation_context)
|
||||
@@ -430,6 +434,27 @@ class TestCallbackContextAddEventsToMemory:
|
||||
custom_metadata=metadata,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_memory_accepts_memory_entries(
|
||||
self, mock_invocation_context
|
||||
):
|
||||
"""Tests that add_memory forwards MemoryEntry inputs unchanged."""
|
||||
memory_service = AsyncMock()
|
||||
mock_invocation_context.memory_service = memory_service
|
||||
memory_entry = MemoryEntry(
|
||||
content=types.Content(parts=[types.Part(text="fact one")])
|
||||
)
|
||||
|
||||
context = CallbackContext(mock_invocation_context)
|
||||
await context.add_memory(memories=[memory_entry])
|
||||
|
||||
memory_service.add_memory.assert_called_once_with(
|
||||
app_name=mock_invocation_context.session.app_name,
|
||||
user_id=mock_invocation_context.session.user_id,
|
||||
memories=[memory_entry],
|
||||
custom_metadata=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_memory_no_service_raises(self, mock_invocation_context):
|
||||
"""Tests that add_memory raises ValueError with no service."""
|
||||
@@ -441,7 +466,13 @@ class TestCallbackContextAddEventsToMemory:
|
||||
ValueError,
|
||||
match=r"Cannot add memory: memory service is not available\.",
|
||||
):
|
||||
await context.add_memory(memories=["fact one"])
|
||||
await context.add_memory(
|
||||
memories=[
|
||||
MemoryEntry(
|
||||
content=types.Content(parts=[types.Part(text="fact one")])
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class TestToolContextAddSessionToMemory:
|
||||
|
||||
Reference in New Issue
Block a user