Tables
Column-by-column reference for all 9 tables in the Drizzle/PostgreSQL schema.
7 min read
Level 3+ only. See ERD for how these tables relate, and Progressive Enhancement for when PostgreSQL is actually needed versus Anvio's default filesystem storage.
Source of truth: packages/db/src/schema.ts. All 9 tables below use Drizzle's pgTable() with UUID primary keys (defaultRandom()), and every table has a created_at timestamptz defaulting to now().
users
export const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom(),
email: varchar('email', { length: 255 }).notNull().unique(),
passwordHash: varchar('password_hash', { length: 255 }).notNull(),
roles: jsonb('roles').$type<string[]>().notNull().default(['user']),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK, gen_random_uuid() |
email | varchar(255) | NOT NULL, UNIQUE |
password_hash | varchar(255) | NOT NULL — bcrypt hash (seed script uses bcryptjs) |
roles | jsonb (string[]) | NOT NULL, default ["user"] |
created_at | timestamptz | NOT NULL, default now() |
This is a separate identity store from the runtime's AuthContext (JWT-based; see API Authentication). The seed script (packages/db/src/seed.ts) creates admin@anvio.local / admin123 with roles ['admin', 'user'] on first run.
personas
export const personas = pgTable('personas', {
id: uuid('id').primaryKey().defaultRandom(),
slug: varchar('slug', { length: 100 }).notNull().unique(),
profile: jsonb('profile').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK |
slug | varchar(100) | NOT NULL, UNIQUE — e.g. architect |
profile | jsonb | NOT NULL — the persona's spec (parsed by parsePersonaDefinition) |
created_at | timestamptz | NOT NULL, default now() |
Seeded from YAML files in configs/personas/ (see loadYamlDir() in seed.ts). No foreign keys — a standalone catalog table.
skills
export const skills = pgTable('skills', {
id: uuid('id').primaryKey().defaultRandom(),
slug: varchar('slug', { length: 100 }).notNull().unique(),
definition: jsonb('definition').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK |
slug | varchar(100) | NOT NULL, UNIQUE |
definition | jsonb | NOT NULL — the skill's spec (parsed by parseSkillDefinition) |
created_at | timestamptz | NOT NULL, default now() |
Seeded from configs/skills/. Linked to agents via the agent_skills join table.
agents
export const agents = pgTable('agents', {
id: uuid('id').primaryKey().defaultRandom(),
name: varchar('name', { length: 100 }).notNull().unique(),
spec: jsonb('spec').notNull(),
version: integer('version').notNull().default(1),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK |
name | varchar(100) | NOT NULL, UNIQUE — e.g. architect |
spec | jsonb | NOT NULL — full agent spec (parsed by parseAgentDefinition) |
version | integer | NOT NULL, default 1 |
created_at | timestamptz | NOT NULL, default now() |
Seeded from configs/agents/. Note: this table is the Postgres mirror of agent definitions. At Level 1, the same data lives as Markdown/YAML files under workspace/agents/ and is read via workspace.loader.loadAgent() — the REST API's GET /api/agents reads from the filesystem loader, not from this table, in the current controllers (apps/api/src/agents.controller.ts).
agent_skills
export const agentSkills = pgTable(
'agent_skills',
{
agentId: uuid('agent_id').notNull().references(() => agents.id, { onDelete: 'cascade' }),
skillId: uuid('skill_id').notNull().references(() => skills.id, { onDelete: 'cascade' }),
},
(table) => [primaryKey({ columns: [table.agentId, table.skillId] })],
);| Column | Type | Constraints |
|---|---|---|
agent_id | uuid | PK (composite), FK → agents.id, ON DELETE CASCADE |
skill_id | uuid | PK (composite), FK → skills.id, ON DELETE CASCADE |
Pure many-to-many join table — no extra columns. No surrogate id.
sessions
export const sessions = pgTable('sessions', {
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
agentId: uuid('agent_id').notNull().references(() => agents.id),
channel: varchar('channel', { length: 50 }).notNull().default('web-chat'),
state: jsonb('state').notNull().default({}),
lastActiveAt: timestamp('last_active_at', { withTimezone: true }).notNull().defaultNow(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK |
user_id | uuid | NOT NULL, FK → users.id, ON DELETE CASCADE |
agent_id | uuid | NOT NULL, FK → agents.id (no cascade) |
channel | varchar(50) | NOT NULL, default 'web-chat' — one of the ChannelType values (web-chat, telegram, whatsapp, discord, slack, teams, matrix, email, signal, google-chat, feishu, sms, mattermost, rest, cli) |
state | jsonb | NOT NULL, default {} |
last_active_at | timestamptz | NOT NULL, default now() |
created_at | timestamptz | NOT NULL, default now() |
This Postgres sessions table is a different representation from the runtime's StoredSession type (packages/core), which is what the filesystem SessionStore and ChannelSessionBridge actually operate on at Level 1–2 (JSONL files, or SQLite state.db at Level 2). Fields like channelThread and detached (used heavily by the Channel Hub — see Channel Adapters) live in that runtime type, not as dedicated columns here. Treat this table as the Level 3 persistence target, not a 1:1 mirror of the in-memory session model.
session_messages
export const sessionMessages = pgTable('session_messages', {
id: uuid('id').primaryKey().defaultRandom(),
sessionId: uuid('session_id').notNull().references(() => sessions.id, { onDelete: 'cascade' }),
role: varchar('role', { length: 20 }).notNull(),
content: jsonb('content').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK |
session_id | uuid | NOT NULL, FK → sessions.id, ON DELETE CASCADE |
role | varchar(20) | NOT NULL — expected values align with MessageRole in core types: system, user, assistant, tool |
content | jsonb | NOT NULL |
created_at | timestamptz | NOT NULL, default now() |
The full conversation transcript for a session.
memory_entries
export const memoryEntries = pgTable('memory_entries', {
id: uuid('id').primaryKey().defaultRandom(),
sessionId: uuid('session_id').references(() => sessions.id, { onDelete: 'set null' }),
userId: uuid('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
type: varchar('type', { length: 50 }).notNull(),
content: text('content').notNull(),
metadata: jsonb('metadata').default({}),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK |
session_id | uuid | Nullable, FK → sessions.id, ON DELETE SET NULL |
user_id | uuid | NOT NULL, FK → users.id, ON DELETE CASCADE |
type | varchar(50) | NOT NULL — memory kind (e.g. fact, preference, summary) |
content | text | NOT NULL — unstructured, unlike jsonb used elsewhere |
metadata | jsonb | nullable, default {} |
created_at | timestamptz | NOT NULL, default now() |
Backs PostgresLongTermMemory (packages/memory/src/postgres-long-term.ts), one implementation of the LongTermMemoryPort interface. At Level 1–2, the default memory provider is filesystem-based instead (workspace/memory/) — this table only fills up if you've configured memory.provider: postgres in anvio.yaml.
tool_executions
export const toolExecutions = pgTable('tool_executions', {
id: uuid('id').primaryKey().defaultRandom(),
sessionId: uuid('session_id').notNull().references(() => sessions.id, { onDelete: 'cascade' }),
toolName: varchar('tool_name', { length: 100 }).notNull(),
input: jsonb('input').notNull(),
output: jsonb('output'),
status: varchar('status', { length: 50 }).notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});| Column | Type | Constraints |
|---|---|---|
id | uuid | PK |
session_id | uuid | NOT NULL, FK → sessions.id, ON DELETE CASCADE |
tool_name | varchar(100) | NOT NULL — e.g. browser_navigate, file_read |
input | jsonb | NOT NULL |
output | jsonb | Nullable — absent while the tool call is in-flight |
status | varchar(50) | NOT NULL — free-form status string |
created_at | timestamptz | NOT NULL, default now() |
Audit trail for tool gateway calls, correlated with the anvio.tool.executed.v1 event (see Event Model).
Exported TypeScript types
packages/db/src/schema.ts exports inferred row types for direct use elsewhere in the monorepo:
export type User = typeof users.$inferSelect;
export type Agent = typeof agents.$inferSelect;
export type Session = typeof sessions.$inferSelect;
export type Persona = typeof personas.$inferSelect;
export type Skill = typeof skills.$inferSelect;No inferred types are exported for agentSkills, sessionMessages, memoryEntries, or toolExecutions as of this writing — consumers of those tables (e.g. PostgresLongTermMemory) use typeof memoryEntries.$inferSelect inline instead of a named export.
Last updated on