File size: 2,683 Bytes
ff0e97f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import os
import sys
from pathlib import Path

from dotenv import load_dotenv

# Add parent directory to sys.path so we can import ebird_tools
parent_dir = Path(__file__).parent.parent
sys.path.insert(0, str(parent_dir))

from langgraph_agent import AgentFactory
from agent_cache import get_or_create_agent, get_cache_stats

async def test_cache():
    # Test data
    session_id = "test_session_123"
    openai_key = os.environ.get("OPENAI_API_KEY")

    # Check for API key (similar to main app)
    if not openai_key:
        print("❌ OPENAI_API_KEY environment variable not set!")
        print("Please set your OpenAI API key to run this test.")
        print("Example: export OPENAI_API_KEY='sk-your-key-here'")
        return

    print("="*50)
    print("[TEST 1]: Create agent (cache miss)")
    print("="*50)

    agent1 = await get_or_create_agent(
        session_id=session_id,
        provider="openai",
        api_key=openai_key,
        model="gpt-4o-mini",
        mode="Specialized Subagents (3 Specialists)",
        agent_factory_method=lambda: AgentFactory.create_subagent_orchestrator(
            model="gpt-4o-mini",
            api_key=openai_key,
            provider="openai",
            mode="Specialized Subagents (3 Specialists)"
        )
    )

    print(f"\nCache stats: {get_cache_stats()}")

    print("\n"+"="*50)
    print("[TEST 2: Get same agent (cache hit)")
    print("="*50)

    agent2 = await get_or_create_agent(
        session_id=session_id,
        provider="openai",
        api_key=openai_key,
        model="gpt-4o-mini",
        mode="Specialized Subagents (3 Specialists)",
        agent_factory_method=lambda: AgentFactory.create_subagent_orchestrator(
            model="gpt-4o-mini",
            api_key=openai_key,
            provider="openai",
            mode="Specialized Subagents (3 Specialists)"
        )
    )

    print(f"\nCache stats: {get_cache_stats()}")

    # Verify they're the same object
    print(f"\nSame agent object? {agent1 is agent2}")

    print("\n"+"="*50)
    print("[TEST 3]: Different session (cache miss)")
    print("=" * 50)

    agent3 = await get_or_create_agent(
        session_id="different_session_456",
        provider="openai",
        api_key=openai_key,
        model="gpt-4o-mini",
        mode="Specialized Subagents (3 Specialists)",
        agent_factory_method=lambda: AgentFactory.create_subagent_orchestrator(
            model="gpt-4o-mini",
            api_key=openai_key,
            provider="openai",
            mode="Specialized Subagents (3 Specialists)"
        )
    )

    print(f"\nCache stats: {get_cache_stats()}")

asyncio.run(test_cache())