Description
The CWArchive class stores and manages Chat, Bot, and Model objects in a single on-disk file. It supports synchronous or asynchronous (chunked) loading and provides basic add, remove, and retrieval operations.
- Purpose: File-based persistence of chatbot sessions, bots, and model configurations.
- Asynchronous Support: Optionally splits data into chunks and loads them in parallel, with a tunable delay.
- Archive Operations: Simple API to add, remove, or fetch objects, plus automatic saving when used as a context manager.
Attributes
| Attribute | Type | Description |
|---|---|---|
| archive_path | str | Filesystem path where the archive is stored. |
| archive_data | dict[int, Chat | Bot | Model] | Dictionary of stored objects, keyed by integer IDs. |
| archive_id | int | Next available integer ID when adding a new item. |
| archive_delay | float | Seconds to wait between async load operations (default 0.07). |
| archive_asynchronous | bool | Whether loading/saving runs asynchronously (True by default). |
Methods
| Method | Returns | Purpose |
|---|---|---|
| archive_path (getter) | str | Gets the current archive file path. |
| archive_path (setter) | None | Sets a new archive file path. |
| archive_data (getter) | dict[int, Chat | Bot | Model] | Retrieves the in-memory archive data. |
| archive_data (setter) | None | Replaces the in-memory archive data with a new dict. |
| archive_id (getter) | int | Returns the next available ID. |
| archive_id (setter) | None | Sets the next available ID (use carefully). |
| archive_delay (getter) | float | Gets the delay between asynchronous loads. |
| archive_delay (setter) | None | Sets a new asynchronous delay value. |
| archive_asynchronous (getter) | bool | Checks if async mode is enabled. |
| archive_asynchronous (setter) | None | Enables or disables asynchronous mode. |
| define | None | Updates file path and asynchronous delay together. |
| get_id_from_element | list[int] | Returns all IDs matching a given object instance. |
| is_valid_id | bool | Checks if a given ID exists in the archive. |
| add | None | Adds a Chat/Bot/Model to the archive under the next ID. |
| remove | None | Removes items by ID or by object; supports collections and duplicate-handling modes. |
| save | None | Serializes and writes archive_data to disk. |
| retrieve | dict[int, Chat | Bot | Model] | Loads archive data from disk (async or sync depending on settings). |
Example
Code
from chatweaver import CWArchive, Chat, Bot
# 1. Create an archive (asynchronous by default)
archive = CWArchive(path="my_archive.txt")
# 2. Add a new Chat object
my_chat = Chat(api_key="sk-1234567890abcdef...", title="Session One")
archive.add(my_chat)
# 3. Save the archive to disk
archive.save()
# 4. Inspect loaded data (better to use archive_data directly than retrieve())
for item_id, item_obj in archive.archive_data.items():
print(item_id, item_obj)
# 5. Remove the chat by its ID
archive.remove(0)
archive.save()
# 6. Disable asynchronous loading if concurrency issues arise
archive.archive_asynchronous = False
# Using CWArchive as a context manager
my_bot = Bot(api_key="sk-1234567890abcdef...", rules="Be helpful!", name="HelperBot")
with CWArchive(path="my_archive.txt") as arc:
arc.add(my_bot)
# save() is called automatically on exit