@anvia/memory-prisma ​
@anvia/memory-prisma stores Anvia agent memory through an application's Prisma client. It is designed for teams that want conversation data and migrations to remain in their existing Prisma workflow.
Install ​
pnpm add @anvia/memory-prisma @anvia/core @prisma/clientThe package is ESM-only and peers with @anvia/core >=0.13.0 <1.0.0 and @prisma/client >=7.9.1 <8.0.0.
Generate the model file ​
Preview the generated Prisma models:
npx @anvia/memory-prisma initWrite them to prisma/models/anvia-memory.prisma:
npx @anvia/memory-prisma init --writeFor projects that do not use a multi-file Prisma schema, explicitly append the generated block:
npx @anvia/memory-prisma init --write --append-to-schemaThen use the normal Prisma workflow:
npx prisma validate
npx prisma migrate dev --name add_anvia_memory
npx prisma generateThe command is a dry run unless --write is present. Review generated changes before migrating.
Create the store ​
import { AgentBuilder } from '@anvia/core/agent'
import { createPrismaMemoryStore } from '@anvia/memory-prisma'
import { prisma } from './db.js'
const memory = createPrismaMemoryStore(prisma, {
scope: {
metadataKeys: ['tenantId'],
},
})
const agent = new AgentBuilder('support', model)
.memory(memory, { savePolicy: 'turn' })
.build()The conventional client path expects delegates named agentMemorySession, agentMemoryMessage, and agentMemoryError.
Custom model names ​
Use PrismaMemoryStore.fromDelegates(...) when generated delegate names differ. Supply session and message delegates plus a transaction function; supply the errors delegate unless errors: 'ignore' is configured.
Inspection requires the session delegate's optional findMany and findUnique methods. Compaction additionally requires messages.deleteMany. The basic memory methods continue to work when those optional interfaces are unavailable.
Schema and transaction ownership ​
The package does not migrate the database at runtime. The generated schema defines:
AgentMemorySessionAgentMemoryMessageAgentMemoryError
The session scope is unique, message positions are unique per memory session, and child rows cascade when a session is deleted. Pass Prisma transaction options through transaction, for example an isolation level supported by your configured connector.
Production patterns ​
- Commit the generated schema or append block and manage it with Prisma migrations.
- Re-run Prisma Client generation after schema changes.
- Reuse the application's singleton Prisma client and close it through the application's lifecycle.
- Use stable tenant metadata in the memory scope where necessary.
- Keep message validation enabled at persistence boundaries.
- Treat memory scope as isolation of histories, not user authorization.
Read Memory save policies and Memory sessions for agent-side setup.