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 ondatacan 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_idcolumn 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§
- Chat
Item Data - Stored form of a
ChatItem: entity fields + the owning chat id - PgChat
Memory - Postgres-backed
ChatMemoryfor one chat. - PgChat
Memory Factory - Builds one
PgChatMemoryper chat off a shared pool. The chat-bot layer wants aChatMemoryscoped to a single chat, so the chat id is bound at construction, not passed per call. - PgChat
Repository