Spaces:
Sleeping
Sleeping
File size: 3,385 Bytes
64462d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | """Test session manager to verify context persistence across conversations."""
import asyncio
import sys
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))
from agent_framework import Agent, LlmClient, InMemorySessionManager, display_trace
from agent_tools import calculator
async def main():
"""Test session persistence."""
print("=" * 60)
print("Session Manager Test - Context Persistence")
print("=" * 60)
# Create a shared session manager
session_manager = InMemorySessionManager()
# Create agent with session support
agent = Agent(
model=LlmClient(model="gpt-4o-mini"),
tools=[calculator],
instructions="You are a helpful assistant with memory. Remember what users tell you.",
max_steps=5,
session_manager=session_manager
)
session_id = "test-user-123"
# === Conversation 1: Introduce yourself ===
print("\n" + "-" * 60)
print("Conversation 1: User introduces themselves")
print("-" * 60)
result1 = await agent.run(
"Hi! My name is Alice and I'm a software engineer. I love Python.",
session_id=session_id
)
print(f"User: Hi! My name is Alice and I'm a software engineer. I love Python.")
print(f"Agent: {result1.output}")
print(f"Events in context: {len(result1.context.events)}")
# === Conversation 2: Ask about something else ===
print("\n" + "-" * 60)
print("Conversation 2: Continue conversation")
print("-" * 60)
result2 = await agent.run(
"What's 1234 * 5678?",
session_id=session_id
)
print(f"User: What's 1234 * 5678?")
print(f"Agent: {result2.output}")
print(f"Events in context: {len(result2.context.events)}")
# === Conversation 3: Test if it remembers ===
print("\n" + "-" * 60)
print("Conversation 3: Test memory - Does it remember?")
print("-" * 60)
result3 = await agent.run(
"What's my name and what do I do for work?",
session_id=session_id
)
print(f"User: What's my name and what do I do for work?")
print(f"Agent: {result3.output}")
print(f"Events in context: {len(result3.context.events)}")
# === Test with a DIFFERENT session ===
print("\n" + "-" * 60)
print("Conversation 4: Different session (should NOT remember)")
print("-" * 60)
result4 = await agent.run(
"What's my name?",
session_id="different-user-456" # Different session!
)
print(f"User: What's my name?")
print(f"Agent: {result4.output}")
print(f"Events in context: {len(result4.context.events)}")
# === Show session storage ===
print("\n" + "=" * 60)
print("Session Storage Summary")
print("=" * 60)
# Access internal storage to show what's stored
for sid, session in session_manager._sessions.items():
print(f"\nSession ID: {sid}")
print(f" Events: {len(session.events)}")
print(f" State keys: {list(session.state.keys())}")
print(f" Created: {session.created_at}")
# === Optional: Show full trace ===
print("\n" + "=" * 60)
print("Full Trace for Session 'test-user-123' (Last Conversation)")
print("=" * 60)
display_trace(result3.context)
if __name__ == "__main__":
asyncio.run(main())
|