Skip to content

Commit 56eb2a6

Browse files
committed
docs: added example script based on sqlite_session example
1 parent f3827bb commit 56eb2a6

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
Example demonstrating encrypted session memory functionality.
3+
4+
This example shows how to use encrypted session memory to maintain conversation history
5+
across multiple agent runs with automatic encryption and TTL-based expiration.
6+
The EncryptedSession wrapper provides transparent encryption over any underlying session.
7+
"""
8+
9+
import asyncio
10+
11+
from agents import Agent, Runner, SQLiteSession
12+
from agents.extensions.memory import EncryptedSession
13+
14+
15+
async def main():
16+
# Create an agent
17+
agent = Agent(
18+
name="Assistant",
19+
instructions="Reply very concisely.",
20+
)
21+
22+
# Create an underlying session (SQLiteSession in this example)
23+
session_id = "conversation_123"
24+
underlying_session = SQLiteSession(session_id)
25+
26+
# Wrap with encrypted session for automatic encryption and TTL
27+
session = EncryptedSession(
28+
session_id=session_id,
29+
underlying_session=underlying_session,
30+
encryption_key="my-secret-encryption-key", # In production, use ENCRYPTION_KEY env var
31+
ttl=3600, # 1 hour TTL for messages
32+
)
33+
34+
print("=== Encrypted Session Example ===")
35+
print("The agent will remember previous messages automatically with encryption.\n")
36+
37+
# First turn
38+
print("First turn:")
39+
print("User: What city is the Golden Gate Bridge in?")
40+
result = await Runner.run(
41+
agent,
42+
"What city is the Golden Gate Bridge in?",
43+
session=session,
44+
)
45+
print(f"Assistant: {result.final_output}")
46+
print()
47+
48+
# Second turn - the agent will remember the previous conversation
49+
print("Second turn:")
50+
print("User: What state is it in?")
51+
result = await Runner.run(agent, "What state is it in?", session=session)
52+
print(f"Assistant: {result.final_output}")
53+
print()
54+
55+
# Third turn - continuing the conversation
56+
print("Third turn:")
57+
print("User: What's the population of that state?")
58+
result = await Runner.run(
59+
agent,
60+
"What's the population of that state?",
61+
session=session,
62+
)
63+
print(f"Assistant: {result.final_output}")
64+
print()
65+
66+
print("=== Conversation Complete ===")
67+
print("Notice how the agent remembered the context from previous turns!")
68+
print("All conversation history was automatically encrypted and stored securely.")
69+
70+
# Demonstrate the limit parameter - get only the latest 2 items
71+
print("\n=== Latest Items Demo ===")
72+
latest_items = await session.get_items(limit=2)
73+
print("Latest 2 items (automatically decrypted):")
74+
for i, msg in enumerate(latest_items, 1):
75+
role = msg.get("role", "unknown")
76+
content = msg.get("content", "")
77+
print(f" {i}. {role}: {content}")
78+
79+
print(f"\nFetched {len(latest_items)} out of total conversation history.")
80+
81+
# Get all items to show the difference
82+
all_items = await session.get_items()
83+
print(f"Total items in session: {len(all_items)}")
84+
85+
# Show that underlying storage is encrypted
86+
print("\n=== Encryption Demo ===")
87+
print("Checking underlying storage to verify encryption...")
88+
raw_items = await underlying_session.get_items()
89+
print("Raw encrypted items in underlying storage:")
90+
for i, item in enumerate(raw_items, 1):
91+
if isinstance(item, dict) and item.get("__enc__") == 1:
92+
print(
93+
f" {i}. Encrypted envelope: __enc__={item['__enc__']}, payload length={len(item.get('payload', ''))}"
94+
)
95+
else:
96+
print(f" {i}. Unencrypted item: {item}")
97+
98+
print(f"\nAll {len(raw_items)} items are stored encrypted with TTL-based expiration.")
99+
100+
# Clean up
101+
underlying_session.close()
102+
103+
104+
if __name__ == "__main__":
105+
asyncio.run(main())

0 commit comments

Comments
 (0)