Metadata-Version: 2.5
Name: crewai_persistence_sql
Version: 0.1.0
Summary: SQL persistence backend for CrewAI Flows via SQLAlchemy (SQLite, PostgreSQL, MySQL) with built-in message pruning via agentstate-reducer
Project-URL: Homepage, https://github.com/skamalj/crewai_persistence_sql
Project-URL: Repository, https://github.com/skamalj/crewai_persistence_sql.git
Author-email: Kamal <skamalj@github.com>
Keywords: agent-state,crewai,flows,mysql,persistence,postgres,sql,sqlalchemy,sqlite
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: <3.14,>=3.10
Requires-Dist: crewai
Requires-Dist: sqlalchemy>=2.0
Provides-Extra: mysql
Requires-Dist: pymysql>=1.1; extra == 'mysql'
Provides-Extra: postgres
Requires-Dist: psycopg[binary]>=3.1; extra == 'postgres'
Provides-Extra: reducer
Requires-Dist: agentstate-reducer>=0.3.0; extra == 'reducer'
Description-Content-Type: text/markdown

# crewai-persistence-sql

SQL persistence backend for [CrewAI Flows](https://docs.crewai.com/concepts/flows) via **SQLAlchemy** — one provider for **SQLite, PostgreSQL, MySQL**, and more, selected by connection URL. Optional built-in message pruning via [agentstate-reducer](https://pypi.org/project/agentstate-reducer/). Implements CrewAI's `FlowPersistence` interface.

## Installation

```bash
pip install crewai-persistence-sql               # SQLite built-in
pip install "crewai-persistence-sql[postgres]"   # + psycopg
pip install "crewai-persistence-sql[mysql]"      # + pymysql
pip install "crewai-persistence-sql[reducer]"    # + message pruning
```

**Requires Python 3.10–3.13.**

## Usage

```python
from crewai.flow.flow import Flow, start
from crewai.flow.persistence import persist
from crewai_persistence_sql import SQLFlowPersistence

backend = SQLFlowPersistence(url="sqlite:///crewai_flows.db")
# or  "postgresql+psycopg://user:pass@host/db"  /  "mysql+pymysql://user:pass@host/db"

@persist(backend)
class MyFlow(Flow[MyState]):
    @start()
    def begin(self):
        self.state.counter += 1
```

With message pruning:

```python
from agentstate_reducer import MessageReducer
backend = SQLFlowPersistence(
    url="sqlite:///crewai_flows.db",
    reducer=MessageReducer(min_messages=10, max_messages=20),
    messages_key="messages",
)
```

## API

### `SQLFlowPersistence(url=None, *, table_name="crewai_flow_states", engine=None, reducer=None, messages_key="messages")`

| Parameter | Description |
|---|---|
| `url` | SQLAlchemy connection URL |
| `table_name` | table (auto-created if absent) |
| `engine` | optional pre-built SQLAlchemy `Engine` (supply this or `url`) |
| `reducer` | optional `MessageReducer` to prune `messages_key` before save |
| `messages_key` | state field holding the message list (default `"messages"`) |

## Data model

One row per flow: `flow_uuid` (primary key) + `data` (the flow state as a JSON string). Each save upserts (portable update-then-insert), so `load_state` returns the latest.

## License

MIT
