AAnvio
Database

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(),
});
ColumnTypeConstraints
iduuidPK, gen_random_uuid()
emailvarchar(255)NOT NULL, UNIQUE
password_hashvarchar(255)NOT NULL — bcrypt hash (seed script uses bcryptjs)
rolesjsonb (string[])NOT NULL, default ["user"]
created_attimestamptzNOT 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(),
});
ColumnTypeConstraints
iduuidPK
slugvarchar(100)NOT NULL, UNIQUE — e.g. architect
profilejsonbNOT NULL — the persona's spec (parsed by parsePersonaDefinition)
created_attimestamptzNOT 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(),
});
ColumnTypeConstraints
iduuidPK
slugvarchar(100)NOT NULL, UNIQUE
definitionjsonbNOT NULL — the skill's spec (parsed by parseSkillDefinition)
created_attimestamptzNOT 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(),
});
ColumnTypeConstraints
iduuidPK
namevarchar(100)NOT NULL, UNIQUE — e.g. architect
specjsonbNOT NULL — full agent spec (parsed by parseAgentDefinition)
versionintegerNOT NULL, default 1
created_attimestamptzNOT 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] })],
);
ColumnTypeConstraints
agent_iduuidPK (composite), FK → agents.id, ON DELETE CASCADE
skill_iduuidPK (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(),
});
ColumnTypeConstraints
iduuidPK
user_iduuidNOT NULL, FK → users.id, ON DELETE CASCADE
agent_iduuidNOT NULL, FK → agents.id (no cascade)
channelvarchar(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)
statejsonbNOT NULL, default {}
last_active_attimestamptzNOT NULL, default now()
created_attimestamptzNOT 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(),
});
ColumnTypeConstraints
iduuidPK
session_iduuidNOT NULL, FK → sessions.id, ON DELETE CASCADE
rolevarchar(20)NOT NULL — expected values align with MessageRole in core types: system, user, assistant, tool
contentjsonbNOT NULL
created_attimestamptzNOT 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(),
});
ColumnTypeConstraints
iduuidPK
session_iduuidNullable, FK → sessions.id, ON DELETE SET NULL
user_iduuidNOT NULL, FK → users.id, ON DELETE CASCADE
typevarchar(50)NOT NULL — memory kind (e.g. fact, preference, summary)
contenttextNOT NULL — unstructured, unlike jsonb used elsewhere
metadatajsonbnullable, default {}
created_attimestamptzNOT 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(),
});
ColumnTypeConstraints
iduuidPK
session_iduuidNOT NULL, FK → sessions.id, ON DELETE CASCADE
tool_namevarchar(100)NOT NULL — e.g. browser_navigate, file_read
inputjsonbNOT NULL
outputjsonbNullable — absent while the tool call is in-flight
statusvarchar(50)NOT NULL — free-form status string
created_attimestamptzNOT 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.

Edit on GitHub

Last updated on

On this page