a7d6d88f6f
CI / changes (push) Has been cancelled
CI / cd libs/checkpoint (push) Has been cancelled
CI / cd libs/checkpoint-conformance (push) Has been cancelled
CI / cd libs/checkpoint-postgres (push) Has been cancelled
CI / cd libs/checkpoint-sqlite (push) Has been cancelled
CI / cd libs/cli (push) Has been cancelled
CI / cd libs/prebuilt (push) Has been cancelled
CI / cd libs/sdk-py (push) Has been cancelled
CI / cd libs/langgraph (push) Has been cancelled
CI / Check SDK methods matching (push) Has been cancelled
CI / Check CLI schema hasn't changed #3.13 (push) Has been cancelled
CI / CLI integration test (push) Has been cancelled
CI / sdk-py integration test (push) Has been cancelled
CI / CI Success (push) Has been cancelled
baseline / benchmark (push) Has been cancelled
Deploy Redirects to GitHub Pages / deploy (push) Has been cancelled
146 lines
4.6 KiB
Python
146 lines
4.6 KiB
Python
from contextlib import asynccontextmanager, contextmanager
|
|
from uuid import uuid4
|
|
|
|
from langgraph.store.memory import InMemoryStore
|
|
from langgraph.store.postgres import AsyncPostgresStore, PostgresStore
|
|
from psycopg import AsyncConnection, Connection
|
|
|
|
DEFAULT_POSTGRES_URI = "postgres://postgres:postgres@localhost:5442/"
|
|
|
|
|
|
@contextmanager
|
|
def _store_memory():
|
|
store = InMemoryStore()
|
|
yield store
|
|
|
|
|
|
@contextmanager
|
|
def _store_postgres():
|
|
database = f"test_{uuid4().hex[:16]}"
|
|
# create unique db
|
|
with Connection.connect(DEFAULT_POSTGRES_URI, autocommit=True) as conn:
|
|
conn.execute(f"CREATE DATABASE {database}")
|
|
try:
|
|
# yield store
|
|
with PostgresStore.from_conn_string(DEFAULT_POSTGRES_URI + database) as store:
|
|
store.setup()
|
|
yield store
|
|
finally:
|
|
# drop unique db
|
|
with Connection.connect(DEFAULT_POSTGRES_URI, autocommit=True) as conn:
|
|
conn.execute(f"DROP DATABASE {database}")
|
|
|
|
|
|
@contextmanager
|
|
def _store_postgres_pipe():
|
|
database = f"test_{uuid4().hex[:16]}"
|
|
# create unique db
|
|
with Connection.connect(DEFAULT_POSTGRES_URI, autocommit=True) as conn:
|
|
conn.execute(f"CREATE DATABASE {database}")
|
|
try:
|
|
# yield store
|
|
with PostgresStore.from_conn_string(DEFAULT_POSTGRES_URI + database) as store:
|
|
store.setup() # Run in its own transaction
|
|
with PostgresStore.from_conn_string(
|
|
DEFAULT_POSTGRES_URI + database, pipeline=True
|
|
) as store:
|
|
yield store
|
|
finally:
|
|
# drop unique db
|
|
with Connection.connect(DEFAULT_POSTGRES_URI, autocommit=True) as conn:
|
|
conn.execute(f"DROP DATABASE {database}")
|
|
|
|
|
|
@contextmanager
|
|
def _store_postgres_pool():
|
|
database = f"test_{uuid4().hex[:16]}"
|
|
# create unique db
|
|
with Connection.connect(DEFAULT_POSTGRES_URI, autocommit=True) as conn:
|
|
conn.execute(f"CREATE DATABASE {database}")
|
|
try:
|
|
# yield store
|
|
with PostgresStore.from_conn_string(
|
|
DEFAULT_POSTGRES_URI + database, pool_config={"max_size": 10}
|
|
) as store:
|
|
store.setup()
|
|
yield store
|
|
finally:
|
|
# drop unique db
|
|
with Connection.connect(DEFAULT_POSTGRES_URI, autocommit=True) as conn:
|
|
conn.execute(f"DROP DATABASE {database}")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def _store_postgres_aio():
|
|
database = f"test_{uuid4().hex[:16]}"
|
|
async with await AsyncConnection.connect(
|
|
DEFAULT_POSTGRES_URI, autocommit=True
|
|
) as conn:
|
|
await conn.execute(f"CREATE DATABASE {database}")
|
|
try:
|
|
async with AsyncPostgresStore.from_conn_string(
|
|
DEFAULT_POSTGRES_URI + database
|
|
) as store:
|
|
await store.setup()
|
|
yield store
|
|
finally:
|
|
async with await AsyncConnection.connect(
|
|
DEFAULT_POSTGRES_URI, autocommit=True
|
|
) as conn:
|
|
await conn.execute(f"DROP DATABASE {database}")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def _store_postgres_aio_pipe():
|
|
database = f"test_{uuid4().hex[:16]}"
|
|
async with await AsyncConnection.connect(
|
|
DEFAULT_POSTGRES_URI, autocommit=True
|
|
) as conn:
|
|
await conn.execute(f"CREATE DATABASE {database}")
|
|
try:
|
|
async with AsyncPostgresStore.from_conn_string(
|
|
DEFAULT_POSTGRES_URI + database
|
|
) as store:
|
|
await store.setup() # Run in its own transaction
|
|
async with AsyncPostgresStore.from_conn_string(
|
|
DEFAULT_POSTGRES_URI + database, pipeline=True
|
|
) as store:
|
|
yield store
|
|
finally:
|
|
async with await AsyncConnection.connect(
|
|
DEFAULT_POSTGRES_URI, autocommit=True
|
|
) as conn:
|
|
await conn.execute(f"DROP DATABASE {database}")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def _store_postgres_aio_pool():
|
|
database = f"test_{uuid4().hex[:16]}"
|
|
async with await AsyncConnection.connect(
|
|
DEFAULT_POSTGRES_URI, autocommit=True
|
|
) as conn:
|
|
await conn.execute(f"CREATE DATABASE {database}")
|
|
try:
|
|
async with AsyncPostgresStore.from_conn_string(
|
|
DEFAULT_POSTGRES_URI + database,
|
|
pool_config={"max_size": 10},
|
|
) as store:
|
|
await store.setup()
|
|
yield store
|
|
finally:
|
|
async with await AsyncConnection.connect(
|
|
DEFAULT_POSTGRES_URI, autocommit=True
|
|
) as conn:
|
|
await conn.execute(f"DROP DATABASE {database}")
|
|
|
|
|
|
__all__ = [
|
|
"_store_memory",
|
|
"_store_postgres",
|
|
"_store_postgres_pipe",
|
|
"_store_postgres_pool",
|
|
"_store_postgres_aio",
|
|
"_store_postgres_aio_pipe",
|
|
"_store_postgres_aio_pool",
|
|
]
|