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