Skip to content
Open
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
15 changes: 13 additions & 2 deletions examples/memory/file_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from agents.memory.session import Session
from agents.memory.session_settings import SessionSettings
from agents.run_context import RunContextWrapper


class FileSession(Session):
Expand Down Expand Up @@ -43,14 +44,24 @@ async def get_session_id(self) -> str:
"""Return the session id, creating one if needed."""
return await self._ensure_session_id()

async def get_items(self, limit: int | None = None) -> list[Any]:
async def get_items(
self,
limit: int | None = None,
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> list[Any]:
session_id = await self._ensure_session_id()
items = await self._read_items(session_id)
if limit is not None and limit >= 0:
return items[-limit:]
return items

async def add_items(self, items: list[Any]) -> None:
async def add_items(
self,
items: list[Any],
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> None:
if not items:
return
session_id = await self._ensure_session_id()
Expand Down
13 changes: 11 additions & 2 deletions src/agents/extensions/memory/advanced_sqlite_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ...items import TResponseInputItem
from ...memory import SQLiteSession
from ...memory.session_settings import SessionSettings, resolve_session_limit
from ...run_context import RunContextWrapper


class AdvancedSQLiteSession(SQLiteSession):
Expand Down Expand Up @@ -122,14 +123,19 @@ def _init_structure_tables(self):

conn.commit()

async def add_items(self, items: list[TResponseInputItem]) -> None:
async def add_items(
self,
items: list[TResponseInputItem],
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> None:
"""Add items to the session.

Args:
items: The items to add to the session
"""
# Add to base table first
await super().add_items(items)
await super().add_items(items, wrapper=wrapper)

# Extract structure metadata with precise sequencing
if items:
Expand All @@ -138,12 +144,15 @@ async def add_items(self, items: list[TResponseInputItem]) -> None:
async def get_items(
self,
limit: int | None = None,
*,
wrapper: RunContextWrapper[Any] | None = None,
branch_id: str | None = None,
) -> list[TResponseInputItem]:
"""Get items from current or specified branch.

Args:
limit: Maximum number of items to return. If None, uses session_settings.limit.
wrapper: Optional runtime wrapper for the current run context.
branch_id: Branch to get items from. If None, uses current branch.

Returns:
Expand Down
17 changes: 14 additions & 3 deletions src/agents/extensions/memory/async_sqlite_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from pathlib import Path
from typing import cast
from typing import Any, cast

import aiosqlite

from ...items import TResponseInputItem
from ...memory import SessionABC
from ...memory.session_settings import SessionSettings
from ...run_context import RunContextWrapper


class AsyncSQLiteSession(SessionABC):
Expand Down Expand Up @@ -102,7 +103,12 @@ async def _locked_connection(self) -> AsyncIterator[aiosqlite.Connection]:
conn = await self._get_connection()
yield conn

async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
async def get_items(
self,
limit: int | None = None,
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> list[TResponseInputItem]:
"""Retrieve the conversation history for this session.

Args:
Expand Down Expand Up @@ -150,7 +156,12 @@ async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:

return items

async def add_items(self, items: list[TResponseInputItem]) -> None:
async def add_items(
self,
items: list[TResponseInputItem],
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> None:
"""Add new items to the conversation history.

Args:
Expand Down
16 changes: 14 additions & 2 deletions src/agents/extensions/memory/dapr_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import time
from typing import Any, Final, Literal

from ...run_context import RunContextWrapper

try:
from dapr.aio.clients import DaprClient
from dapr.clients.grpc._state import Concurrency, Consistency, StateOptions
Expand Down Expand Up @@ -232,7 +234,12 @@ async def _handle_concurrency_conflict(self, error: Exception, attempt: int) ->
# Session protocol implementation
# ------------------------------------------------------------------

async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
async def get_items(
self,
limit: int | None = None,
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> list[TResponseInputItem]:
"""Retrieve the conversation history for this session.

Args:
Expand Down Expand Up @@ -271,7 +278,12 @@ async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
continue
return items

async def add_items(self, items: list[TResponseInputItem]) -> None:
async def add_items(
self,
items: list[TResponseInputItem],
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> None:
"""Add new items to the conversation history.

Args:
Expand Down
Loading