PostgresMemoryStore

org.llm4s.agent.memory.PostgresMemoryStore
See thePostgresMemoryStore companion object
final class PostgresMemoryStore extends TransactionalMemoryStore, AutoCloseable

PostgreSQL implementation of MemoryStore. Persists agent memories to a Postgres table using JDBC.

Built either by PostgresMemoryStore.apply, which creates a connection pool and closes it on close, or by PostgresMemoryStore.fromDataSource, over a javax.sql.DataSource the caller owns and keeps open.

Attributes

Companion
object
Graph
Supertypes
trait AutoCloseable
trait MemoryStore
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

override def appliedWrite(key: AppliedWriteKey, token: Long): Result[(TransactionalMemoryStore, Option[AppliedWrite])]

The outcome recorded for key, if it was applied; fenced like every operation.

The outcome recorded for key, if it was applied; fenced like every operation.

Attributes

Definition Classes
override def appliedWriteCount: Result[Int]

How many applied keys the store holds.

How many applied keys the store holds.

Attributes

Definition Classes
override def applyAtomically(key: AppliedWriteKey, token: Long, add: Seq[Memory], remove: Seq[MemoryId], bounds: MemoryBounds): Result[(TransactionalMemoryStore, AppliedWrite)]

In one transaction: for a key already applied, its recorded outcome, writing nothing - even if the memories it stored have since been deleted. Otherwise stores add, deletes remove, then evicts beyond bounds (MemoryBounds.overflow, over the whole store), and records (key, outcome).

In one transaction: for a key already applied, its recorded outcome, writing nothing - even if the memories it stored have since been deleted. Otherwise stores add, deletes remove, then evicts beyond bounds (MemoryBounds.overflow, over the whole store), and records (key, outcome).

remove names what add replaces, as a consolidation's summary replaces its sources: if any memory of remove is no longer there, another writer replaced it first, so nothing is written and the outcome records that nothing was added or removed. Two writers that replace the same memories therefore never both store a replacement.

Attributes

Definition Classes
override def clear(): Result[MemoryStore]

Clear all memories from the store.

Clear all memories from the store.

Attributes

Returns

Empty store or error

Definition Classes
override def close(): Unit

Closes the connection pool if this store created it; a caller-owned DataSource is left open.

Closes the connection pool if this store created it; a caller-owned DataSource is left open.

Attributes

Definition Classes
AutoCloseable
override def count(filter: MemoryFilter): Result[Long]

Count memories matching a filter.

Count memories matching a filter.

Value parameters

filter

Criteria for counting memories

Attributes

Returns

Count or error

Definition Classes
override def delete(id: MemoryId): Result[MemoryStore]

Delete a memory by its ID.

Delete a memory by its ID.

Value parameters

id

The memory identifier

Attributes

Returns

Updated store or error

Definition Classes
override def deleteMatching(filter: MemoryFilter): Result[MemoryStore]

Delete all memories matching a filter.

Delete all memories matching a filter.

Value parameters

filter

Criteria for selecting memories to delete

Attributes

Returns

Updated store or error

Definition Classes
override def forgetApplied(key: AppliedWriteKey, token: Long): Result[TransactionalMemoryStore]

Removes key's record; fenced like every operation. A key that is not there is Right.

Removes key's record; fenced like every operation. A key that is not there is Right.

Attributes

Definition Classes
override def get(id: MemoryId): Result[Option[Memory]]

Retrieve a memory by its ID.

Retrieve a memory by its ID.

Value parameters

id

The memory identifier

Attributes

Returns

The memory if found, or error

Definition Classes
override def recall(filter: MemoryFilter, limit: Int): Result[Seq[Memory]]

Recall memories matching a filter.

Recall memories matching a filter.

This is the primary retrieval method for most use cases. Returns memories sorted by relevance (most relevant first).

Value parameters

filter

Criteria for filtering memories

limit

Maximum number of memories to return

Attributes

Returns

Matching memories sorted by relevance (most relevant first), or error

Definition Classes
override def recent(limit: Int, filter: MemoryFilter): Result[Seq[Memory]]

Get the most recent memories.

Get the most recent memories.

Value parameters

filter

Additional filter criteria

limit

Maximum number of memories

Attributes

Returns

Most recent memories

Definition Classes
override def replace(add: Seq[Memory], remove: Seq[MemoryId]): Result[MemoryStore]

Stores add, then deletes remove, in one transaction.

Stores add, then deletes remove, in one transaction.

Attributes

Definition Classes
override def search(query: String, topK: Int, filter: MemoryFilter): Result[Seq[ScoredMemory]]

Semantic search for memories similar to a query.

Semantic search for memories similar to a query.

This method uses embeddings for semantic similarity search. Falls back to keyword search if embeddings are not available.

Value parameters

filter

Additional filter criteria

query

The search query

topK

Number of results to return

Attributes

Returns

A list of ScoredMemory objects, sorted by relevance score (highest first), or error

Definition Classes
override def store(memory: Memory): Result[MemoryStore]

Store a new memory.

Store a new memory.

Value parameters

memory

The memory to store

Attributes

Returns

Updated store or error

Definition Classes
override def storeAll(memories: Seq[Memory]): Result[MemoryStore]

Store memories in one transaction, on one pooled connection: all of them are stored or, if any write fails, none is. The rows go to the server as JDBC batches of at most WriteBatchSize (1000), not one round trip each, and a memory whose id appears twice is written once, as its last occurrence - so a data source with pgjdbc's reWriteBatchedInserts=true can fold a batch into one multi-row insert.

Store memories in one transaction, on one pooled connection: all of them are stored or, if any write fails, none is. The rows go to the server as JDBC batches of at most WriteBatchSize (1000), not one round trip each, and a memory whose id appears twice is written once, as its last occurrence - so a data source with pgjdbc's reWriteBatchedInserts=true can fold a batch into one multi-row insert.

Attributes

Definition Classes
override def update(id: MemoryId, updateFn: Memory => Memory): Result[MemoryStore]

Update a memory.

Update a memory.

Value parameters

id

The memory identifier

update

Function to transform the memory

Attributes

Returns

Updated store or error

Definition Classes

Inherited methods

def exists(id: MemoryId): Result[Boolean]

Check if a memory exists.

Check if a memory exists.

Value parameters

id

The memory identifier

Attributes

Returns

True if memory exists

Inherited from:
MemoryStore
def getConversation(conversationId: String, limit: Int): Result[Seq[Memory]]

Get conversation history.

Get conversation history.

Value parameters

conversationId

The conversation identifier

limit

Maximum messages to return

Attributes

Returns

Conversation memories in chronological order

Inherited from:
MemoryStore
def getEntityMemories(entityId: EntityId): Result[Seq[Memory]]

Get all memories for a specific entity.

Get all memories for a specific entity.

Value parameters

entityId

The entity identifier

Attributes

Returns

All memories related to this entity

Inherited from:
MemoryStore
def important(threshold: Double, limit: Int): Result[Seq[Memory]]

Get high-importance memories.

Get high-importance memories.

Value parameters

limit

Maximum number of memories

threshold

Minimum importance score (0.0 to 1.0)

Attributes

Returns

Important memories

Inherited from:
MemoryStore

Concrete fields

val tableName: String