Skip to main content

Crate wabot_addon_chat_bot_pg

Crate wabot_addon_chat_bot_pg 

Source
Expand description

Postgres ChatRepository + ChatMemory — port of wabot-ts addon/chat-bot/pg (ChatPgQueries / ChatItemPgQueries).

ⓘ
let db = PgDatabase::from_env().await?;
let chats = Arc::new(PgChatRepository::new(db.pool().clone()));
let memories = PgChatMemoryFactory::new(db.pool().clone());

// one memory per chat, same as the in-memory wiring
let memory: Arc<dyn ChatMemory> = memories.for_chat(chat.id());

§Two lookup strategies, mirroring TS

  • Chat by connection uses JSONB containment (data->'connections' @> $1), which a GIN index on data can serve. A chat can be reachable from several channels, so the match has to be “contains this connection”, not equality.
  • Chat items by chat reads a promoted chat_id column rather than digging into the document, because it runs on every single turn and wants a plain btree index on (chat_id, created_at).

§Storage shape note

wabot-rust’s ChatItem is a bare enum with no identity, whereas TS’s ChatItem is an Entity. This addon wraps it in ChatItemData, which adds the entity fields and the chatId the promoted column reads. That makes the table compatible with TS’s, but the document body nests the item under item rather than spreading it — see the Phase 5b notes in CLAUDE.md about cross-stack JSON compatibility before pointing both frameworks at one chat-items table.

Structs§

ChatItemData
Stored form of a ChatItem: entity fields + the owning chat id
PgChatMemory
Postgres-backed ChatMemory for one chat.
PgChatMemoryFactory
Builds one PgChatMemory per chat off a shared pool. The chat-bot layer wants a ChatMemory scoped to a single chat, so the chat id is bound at construction, not passed per call.
PgChatRepository

Constants§

CHATS_TABLE
CHAT_ITEMS_TABLE