File size: 2,602 Bytes
ba98117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import logging
import sys
from app.core.database import engine, init_db
from app.core.config import settings
from psycopg_pool import AsyncConnectionPool
from app.agent.factory import get_graph_runnable
from langchain_core.messages import HumanMessage

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

if sys.platform == "win32":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


async def verify():
    print("Verifying Unified Driver Implementation...")
    lg_pool = None
    try:
        # 1. Initialize DB Tables
        await init_db()

        # 2. Setup Pool for LangGraph
        pg_uri = str(settings.DB_URI).replace("+asyncpg", "").replace("+psycopg", "")
        lg_pool = AsyncConnectionPool(
            conninfo=pg_uri, max_size=1, kwargs={"autocommit": True}
        )
        await lg_pool.open()

        # 3. Get Runnable & Invoke
        async with lg_pool.connection() as conn:
            # This will internally assert that db_manager.langgraph_pool is ready
            graph = await get_graph_runnable(conn)
            print("Graph runnable obtained.")

            # 4. Invoke Graph
            print("Invoking graph...")
            config = {"configurable": {"thread_id": "verify_unified_1"}}
            input_message = HumanMessage(content="Hello, unified world!")

            final_response = ""
            async for event in graph.astream_events(
                {"messages": [input_message]}, config, version="v1"
            ):
                kind = event["event"]
                if kind == "on_chain_end":
                    if event["name"] == "agent":
                        output_messages = event["data"]["output"]["messages"]
                        if output_messages:
                            ai_message = output_messages[-1]
                            final_response = ai_message.content
                            print(f"AI Response: {final_response}")

            if "Echo: Hello, unified world!" in final_response:
                print("SUCCESS: Graph returned expected response.")
            else:
                print(f"FAILURE: Unexpected response: {final_response}")

    except Exception as e:
        print(f"ERROR: {e}")
        import traceback

        traceback.print_exc()
    finally:
        if lg_pool:
            await lg_pool.close()
        await engine.dispose()


if __name__ == "__main__":
    try:
        asyncio.run(verify())
    except KeyboardInterrupt:
        pass