39 lines
1.6 KiB
SQL
39 lines
1.6 KiB
SQL
-- =====================================================================
|
|
-- conversation service — 0001_init.sql
|
|
-- Database-per-service: owns `conversations`, `messages`, `tool_calls`.
|
|
-- user_id is a bare BIGINT (see iam's migration note on why there's no
|
|
-- cross-database foreign key here).
|
|
-- =====================================================================
|
|
|
|
CREATE TABLE IF NOT EXISTS conversations (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
user_id BIGINT,
|
|
title TEXT NOT NULL DEFAULT 'New conversation',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS conversations_user_id_idx ON conversations (user_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS messages (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
conversation_id BIGINT NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
|
|
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'tool', 'system')),
|
|
content TEXT NOT NULL,
|
|
tool_name TEXT,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS messages_conversation_id_idx ON messages (conversation_id, created_at);
|
|
|
|
-- Audit log of every tool the agent executed on the host, surfaced in
|
|
-- the UI's tool cards and useful for debugging.
|
|
CREATE TABLE IF NOT EXISTS tool_calls (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
message_id BIGINT REFERENCES messages(id) ON DELETE CASCADE,
|
|
tool_name TEXT NOT NULL,
|
|
arguments JSONB NOT NULL,
|
|
result TEXT,
|
|
is_error BOOLEAN NOT NULL DEFAULT false,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|