Overview
SqliteChannel keeps a conversation as rows in a SQLite database. One database file can hold many conversations, told apart by name. Writes are serialised by SQLite and by a lock on the connection, so a fan-out step can post from several threads at once, and several processes can share the file.
Import
sqlite3 is imported when a channel is created, not when swarms is imported.
Constructor
str
required
The database file, created if missing. Its directory must already exist: unlike
FileChannel, SqliteChannel does not create parent directories, and a missing one raises sqlite3.OperationalError: unable to open database file. ":memory:" keeps the conversation in this process only.str
default:"default"
Which conversation in the file this channel reads and writes. Channels with different names share the file without seeing each other’s messages.
check_same_thread=False, switches the database to WAL mode so readers never block writers, and creates the table and index if they do not exist.
Attributes
Schema
channel holds the conversation name. created_at is SQLite’s CURRENT_TIMESTAMP, in UTC, formatted YYYY-MM-DD HH:MM:SS. The table is ordinary SQLite, so any client or BI tool can query it.
Methods
post()
Insert one row into this conversation.messages()
Return every message in this conversation, oldest first.{"agent_name": ..., "content": ...} dictionaries, ordered by id.
fetch_messages()
Return rows with their ids and timestamps, optionally filtered by speaker and limited to the latest few.agent_name(str, optional): Only this speaker’s rows.limit(int, optional): At most this many of the latest rows. They are still returned oldest first.
{"id": ..., "agent_name": ..., "content": ..., "created_at": ...} dictionaries.
list_conversations()
A class method that lists every conversation name stored in a database file.[] if the file has no messages table.
clear()
Delete every row in this conversation. Other conversations in the file are untouched.close()
Close the connection. The file keeps every message.Usage Examples
Many conversations in one file
Query the history with SQL
Notes
- One machine. SQLite’s locking needs a local file system. Use
HttpChannelto share a conversation across machines. - WAL files. WAL mode keeps
-waland-shmfiles next to the database while it is open. Copy all three, or close every connection first, when you move the database.