Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/google/adk/agents/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from collections.abc import Mapping
from collections.abc import Sequence
import logging
from typing import Any
from typing import TYPE_CHECKING

Expand All @@ -36,6 +37,8 @@
from ..tools.tool_confirmation import ToolConfirmation
from .invocation_context import InvocationContext

logger = logging.getLogger('google_adk.' + __name__)


class Context(ReadonlyContext):
"""The context within an agent run."""
Expand Down Expand Up @@ -329,6 +332,13 @@ async def my_after_agent_callback(ctx: Context):
raise ValueError(
"Cannot add session to memory: memory service is not available."
)
logger.debug(
'Adding session to memory. app_name=%s user_id=%s session_id=%s events=%d',
self._invocation_context.session.app_name,
self._invocation_context.session.user_id,
self._invocation_context.session.id,
len(self._invocation_context.session.events),
)
await self._invocation_context.memory_service.add_session_to_memory(
self._invocation_context.session
)
Comment on lines +335 to 344
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved readability and to avoid repeated attribute access, consider assigning self._invocation_context.session to a local variable. This variable can then be used for both the logging statement and the add_session_to_memory call, making the code cleaner and slightly more efficient.

Suggested change
logger.debug(
'Adding session to memory. app_name=%s user_id=%s session_id=%s events=%d',
self._invocation_context.session.app_name,
self._invocation_context.session.user_id,
self._invocation_context.session.id,
len(self._invocation_context.session.events),
)
await self._invocation_context.memory_service.add_session_to_memory(
self._invocation_context.session
)
session = self._invocation_context.session
logger.debug(
'Adding session to memory. app_name=%s user_id=%s session_id=%s events=%d',
session.app_name,
session.user_id,
session.id,
len(session.events),
)
await self._invocation_context.memory_service.add_session_to_memory(
session
)

Expand All @@ -355,6 +365,13 @@ async def add_events_to_memory(
raise ValueError(
"Cannot add events to memory: memory service is not available."
)
logger.debug(
'Adding explicit events to memory. app_name=%s user_id=%s session_id=%s events=%d',
self._invocation_context.session.app_name,
self._invocation_context.session.user_id,
self._invocation_context.session.id,
len(events),
)
await self._invocation_context.memory_service.add_events_to_memory(
app_name=self._invocation_context.session.app_name,
user_id=self._invocation_context.session.user_id,
Expand Down
21 changes: 20 additions & 1 deletion src/google/adk/memory/vertex_ai_memory_bank_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ async def add_events_to_memory(
custom_metadata: Optional service-specific metadata for generate config.
"""
_ = session_id
logger.debug(
'Vertex memory add_events_to_memory called. app_name=%s user_id=%s events=%d',
app_name,
user_id,
len(events),
)
await self._add_events_to_memory_from_events(
app_name=app_name,
user_id=user_id,
Expand Down Expand Up @@ -238,6 +244,7 @@ async def _add_events_to_memory_from_events(
raise ValueError('Agent Engine ID is required for Memory Bank.')

direct_events = []
input_event_count = len(events_to_process)
for event in events_to_process:
if _should_filter_out_event(event.content):
continue
Expand All @@ -248,6 +255,13 @@ async def _add_events_to_memory_from_events(
if direct_events:
api_client = self._get_api_client()
config = _build_generate_memories_config(custom_metadata)
logger.debug(
'Generating memories in Vertex Memory Bank. app_name=%s user_id=%s direct_events=%d filtered_events=%d',
app_name,
user_id,
len(direct_events),
input_event_count - len(direct_events),
)
operation = await api_client.agent_engines.memories.generate(
name='reasoningEngines/' + self._agent_engine_id,
direct_contents_source={'events': direct_events},
Expand All @@ -260,7 +274,12 @@ async def _add_events_to_memory_from_events(
logger.info('Generate memory response received.')
logger.debug('Generate memory response: %s', operation)
else:
logger.info('No events to add to memory.')
logger.info(
'No events to add to memory after filtering. app_name=%s user_id=%s input_events=%d',
app_name,
user_id,
input_event_count,
)

async def _add_memories_via_create(
self,
Expand Down
27 changes: 27 additions & 0 deletions src/google/adk/sessions/vertex_ai_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ async def create_session(
)

reasoning_engine_id = self._get_reasoning_engine_id(app_name)
logger.debug(
'Creating Vertex session. app_name=%s user_id=%s reasoning_engine_id=%s',
app_name,
user_id,
reasoning_engine_id,
)

config = {'session_state': state} if state else {}
config.update(kwargs)
Expand Down Expand Up @@ -140,6 +146,13 @@ async def get_session(
config: Optional[GetSessionConfig] = None,
) -> Optional[Session]:
reasoning_engine_id = self._get_reasoning_engine_id(app_name)
logger.debug(
'Fetching Vertex session. app_name=%s user_id=%s session_id=%s reasoning_engine_id=%s',
app_name,
user_id,
session_id,
reasoning_engine_id,
)
session_resource_name = (
f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}'
)
Expand Down Expand Up @@ -204,6 +217,12 @@ async def list_sessions(
self, *, app_name: str, user_id: Optional[str] = None
) -> ListSessionsResponse:
reasoning_engine_id = self._get_reasoning_engine_id(app_name)
logger.debug(
'Listing Vertex sessions. app_name=%s user_id=%s reasoning_engine_id=%s',
app_name,
user_id,
reasoning_engine_id,
)

async with self._get_api_client() as api_client:
sessions = []
Expand Down Expand Up @@ -250,6 +269,14 @@ async def append_event(self, session: Session, event: Event) -> Event:
await super().append_event(session=session, event=event)

reasoning_engine_id = self._get_reasoning_engine_id(session.app_name)
logger.debug(
'Appending event to Vertex session. app_name=%s user_id=%s session_id=%s author=%s has_content=%s',
session.app_name,
session.user_id,
session.id,
event.author,
bool(event.content),
)

config = {}
if event.content:
Expand Down