English | 中文
card-box-core is a data and context orchestration library centered around Card and CardBox.
Cardis the atomic unit of information.- Logical "updates" are done by creating a new
Card(for example,Card.update(...)returns a new object with a newcard_id). - This makes transformation flows traceable and auditable.
CardBoxis an ordered collection ofcard_ids.- The main workflow processes context in linear order to keep complexity low.
- Multi-turn context and strategy transformations are all built around
CardBox.
CardStoreis the read/write entry point for cards and delegates persistence to storage adapters.CardBoxstores references only; actual card data is loaded from the storage layer.
- Business logic is encapsulated in independent
Strategyunits. ContextEngine.transform(...)orchestrates strategy chains with low coupling between strategies.- Built-in examples:
ExtractCodeStrategy,PdfToTextStrategy,InlineTextFileContentStrategy.
ContextEnginecan logCardandCardBoxtransformation history based onhistory_level.trace_idspans an entire processing flow for correlated auditing.
Card: Supports text, JSON, tool calls, file references, and more.CardBox: Ordered sequence of context references.ContextEngine: Providestransform,to_api, andcall_model.AsyncPostgresStorageAdapter: Async PostgreSQL persistence.LLMAdapter: Unified LLM access (LiteLLM / optional Interactions backend).
python -m venv .venv
source .venv/bin/activate
pip install -e .Optional extras:
pip install -e .[test]
pip install -e .[interactions]Set one of the following DSN environment variables:
export POSTGRES_STORAGE_ADAPTER_DSN="postgresql://user:pass@localhost:5432/cardbox_db"
# or
export CARD_BOX_POSTGRES_DSN="postgresql://user:pass@localhost:5432/cardbox_db"main.py demonstrates a minimal end-to-end flow:
- Initialize
AsyncPostgresStorageAdapter(with optional auto bootstrap). - Create and persist
Cardinstances. - Save and load a
CardBox. - Print the loaded result.
Run:
python main.pypython -m unittest discover -s tests -p 'test_*.py' -vNotes:
tests/test_llm_providers.pydepends on external model environment variables and will be skipped when not configured.- Other tests prefer local stubs and avoid real external services.
Use configure at startup to override default settings:
from card_box_core.config import configure
configure({
"POSTGRES_STORAGE_ADAPTER": {
"dsn": "postgresql://user:pass@localhost:5432/cardbox_db",
"auto_bootstrap": True,
},
"LLM_BACKEND": "litellm",
})