Skip to content

API Reference parrot.storage.chat

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.storage.chat

View source

Unified hot+cold chat storage.

Redis (via RedisConversation) for fast access to recent turns. DynamoDB (via ConversationDynamoDB) for permanent history, search, and analytics.

FEAT-103: Migrated from DocumentDB to DynamoDB.

Alias CHAT_HOT_TTL_HOURS

Re-exported from parrot.conf.CHAT_HOT_TTL_HOURS.

Alias CONVERSATION_DELETED_TOPIC

Re-exported from parrot.memory.persistence.CONVERSATION_DELETED_TOPIC.

Class ChatStorage

Unified chat persistence with Redis hot cache and DynamoDB cold storage.

Parameter Type Default Description
redis_conversation Optional[Any] None
dynamodb Optional[Any] None
document_db Optional[Any] None

Attributes:

Attribute Type Description
cleanup_retry_base_delay float
hot_seed_turns int
cold_backend Optional[Any] The cold-tier ConversationBackend (read-only), or None when no backend is configured.
hot_redis_client Optional[Any] The Redis client behind the hot tier (read-only), or None when there is no Redis hot tier.

Method ChatStorage.initialize

Connect DynamoDB backend and set up Redis.

initialize() -> None

Method ChatStorage.register_memory_cleaner

Register a (user_id, agent_name, session_id) cleaner for one working-memory backend.

Called after the tombstone is committed, once per delete and again on every cleanup retry, so it MUST be idempotent.

register_memory_cleaner(cleaner: Callable[[str, str, str], Awaitable[None]]) -> None

Parameter Type Default Description
cleaner Callable[[str, str, str], Awaitable[None]]

Method ChatStorage.register_turn_memory_cleaner

Register a (user_id, agent_name, session_id, turn_id) cleaner for one working-memory backend.

Run after a TURN tombstone is committed (and on every cleanup retry), so a deleted turn is not fed to the LLM from a bot's working memory until its TTL. MUST be idempotent.

register_turn_memory_cleaner(cleaner: Callable[[str, str, str, str], Awaitable[None]]) -> None

Parameter Type Default Description
cleaner Callable[[str, str, str, str], Awaitable[None]]

Method ChatStorage.attach_deletion_publisher

Set (or clear with None) the callable that publishes deletion events: await publisher(topic, payload).

attach_deletion_publisher(publisher: Optional[Callable[[str, Dict[str, Any]], Awaitable[None]]]) -> None

Parameter Type Default Description
publisher Optional[Callable[[str, Dict[str, Any]], Awaitable[None]]]

Method ChatStorage.close

Release connections.

close() -> None

Method ChatStorage.store_turn

Upsert turn in the cold tier, update thread metadata, then upsert the hot tier.

Returns after the cold write and hot update complete. Older turns outside a truncated hot seed stay cold-only. Legacy cold backends without canonical write results invalidate the hot cache instead.

store_turn(turn: ChatStorageTurn) -> None

Parameter Type Default Description
turn ChatStorageTurn

Raises:

  • StorageError — any tier failed (retryable) — nothing is swallowed, nothing is backgrounded.
  • ConversationDeletedError — the session or the turn is tombstoned (terminal).

Method ChatStorage.handle_turn_completed

Bus handler for TURN_COMPLETED_TOPIC.

Retryable StorageError propagates (the bus retries, then dead-letters). ConversationDeletedError and InvalidTurnPayloadError are terminal: logged at WARNING with the turn id and acknowledged.

handle_turn_completed(envelope: EventEnvelope) -> None

Parameter Type Default Description
envelope EventEnvelope

Method ChatStorage.load_conversation

Load messages for a conversation, Redis-first with DynamoDB fallback.

load_conversation(user_id: str, session_id: str, agent_id: Optional[str] = None, limit: int = DEFAULT_LIST_LIMIT) -> List[Dict[str, Any]]

Parameter Type Default Description
user_id str
session_id str
agent_id Optional[str] None
limit int DEFAULT_LIST_LIMIT

Returns: List[Dict[str, Any]] — List of message dicts, sorted by timestamp ascending.

Raises:

  • ConversationDeletedError — the conversation is tombstoned (even if cleanup has not finished).
  • StorageUnavailableError — the deletion guard or the cold tier could not be read.

Method ChatStorage.get_conversation_metadata

Load conversation metadata.

Note: This method requires user_id and agent_id for DynamoDB queries. If they are not available, returns None.

get_conversation_metadata(session_id: str) -> Optional[Dict[str, Any]]

Parameter Type Default Description
session_id str

Returns: Optional[Dict[str, Any]]

Method ChatStorage.list_user_conversations

List conversations for a user from DynamoDB.

A tombstoned thread is hidden even while its cleanup is running, and limit counts LIVE threads: when hidden threads leave a full window short, the window is read again, larger (query_threads has no cursor), until limit live threads are found, the store is exhausted or the since cutoff is reached.

list_user_conversations(user_id: str, agent_id: Optional[str] = None, limit: int = DEFAULT_LIST_LIMIT, since: Optional[datetime] = None) -> List[Dict[str, Any]]

Parameter Type Default Description
user_id str
agent_id Optional[str] None
limit int DEFAULT_LIST_LIMIT
since Optional[datetime] None

Returns: List[Dict[str, Any]] — List of thread metadata dicts, sorted by updated_at desc.

Method ChatStorage.create_conversation

Create a conversation thread in DynamoDB.

create_conversation(user_id: str, session_id: str, agent_id: str, title: str = 'New Conversation') -> Optional[Dict[str, Any]]

Parameter Type Default Description
user_id str
session_id str
agent_id str
title str 'New Conversation'

Returns: Optional[Dict[str, Any]]

Method ChatStorage.update_conversation_title

Update the title of a conversation in DynamoDB.

Note: DynamoDB requires user_id and agent_id to build the PK.

update_conversation_title(session_id: str, title: str, user_id: Optional[str] = None, agent_id: Optional[str] = None) -> bool

Parameter Type Default Description
session_id str
title str
user_id Optional[str] None
agent_id Optional[str] None

Returns: bool

Method ChatStorage.assert_conversation_active

Public deletion guard for callers that write a thread row directly (raises ConversationDeletedError).

assert_conversation_active(user_id: str, agent_id: str, session_id: str) -> None

Parameter Type Default Description
user_id str
agent_id str
session_id str

Method ChatStorage.delete_conversation

Tombstone first, then clear cold turns, companions, artifacts and the hot tier, run the cleaners, publish the event.

delete_conversation(user_id: str, session_id: str, agent_id: Optional[str] = None) -> bool

Parameter Type Default Description
user_id str
session_id str
agent_id Optional[str] None

Returns: bool

Raises:

  • StorageError — the tombstone could not be committed — the delete is NOT acknowledged and nothing is cleared.

Method ChatStorage.delete_turn

Turn-scoped tombstone first, then delete from the cold and hot tiers; True if either tier removed the turn.

delete_turn(session_id: str, turn_id: str, user_id: Optional[str] = None, agent_id: Optional[str] = None) -> bool

Parameter Type Default Description
session_id str
turn_id str
user_id Optional[str] None
agent_id Optional[str] None

Returns: bool

Method ChatStorage.handle_conversation_deleted

Idempotent cleanup retry for CONVERSATION_DELETED_TOPIC; raises StorageWriteError while any step fails.

handle_conversation_deleted(envelope: EventEnvelope) -> None

Parameter Type Default Description
envelope EventEnvelope

Method ChatStorage.get_context_for_agent

Return recent messages formatted for LLM context window.

get_context_for_agent(user_id: str, session_id: str, agent_id: Optional[str] = None, max_turns: int = DEFAULT_CONTEXT_TURNS, model: str = 'claude') -> List[Dict[str, str]]

Parameter Type Default Description
user_id str
session_id str
agent_id Optional[str] None
max_turns int DEFAULT_CONTEXT_TURNS
model str 'claude'

Returns: List[Dict[str, str]] — List of {role, content} dicts ready for the model API.

Raises:

  • ConversationDeletedError — the conversation is tombstoned.

Alias ChatStorageTurn

Re-exported from parrot.storage.models.ChatStorageTurn.

Alias ConversationDeletedError

Re-exported from parrot.storage.errors.ConversationDeletedError.

Alias ConversationTurn

Re-exported from parrot.memory.abstract.ConversationTurn.

Attribute DEFAULT_CONTEXT_TURNS

Attribute DEFAULT_LIST_LIMIT

Attribute HOT_SEED_METADATA_KEY

Alias IndexNotReadyError

Re-exported from parrot.storage.errors.IndexNotReadyError.

Alias InvalidTurnPayloadError

Re-exported from parrot.storage.errors.InvalidTurnPayloadError.

Alias MessageRole

Re-exported from parrot.storage.models.MessageRole.

Alias StorageError

Re-exported from parrot.storage.errors.StorageError.

Alias StorageUnavailableError

Re-exported from parrot.storage.errors.StorageUnavailableError.

Alias StorageWriteError

Re-exported from parrot.storage.errors.StorageWriteError.

Alias render_history

Re-exported from parrot.memory.render.render_history.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally