feat: add chat goal persistence foundation

This commit is contained in:
Michael Suchacz
2026-05-22 09:48:07 +00:00
parent 167ac7b879
commit e8e379c2a9
24 changed files with 1979 additions and 121 deletions
+51
View File
@@ -312,6 +312,14 @@ CREATE TYPE chat_client_type AS ENUM (
'api'
);
CREATE TYPE chat_goal_status AS ENUM (
'active',
'paused',
'complete',
'cleared',
'replaced'
);
CREATE TYPE chat_message_role AS ENUM (
'system',
'user',
@@ -1669,6 +1677,30 @@ CREATE TABLE chat_files (
data bytea NOT NULL
);
CREATE TABLE chat_goals (
id uuid DEFAULT gen_random_uuid() NOT NULL,
root_chat_id uuid NOT NULL,
created_from_chat_id uuid,
objective text NOT NULL,
status chat_goal_status NOT NULL,
completion_summary text,
created_by_user_id uuid NOT NULL,
completed_by_user_id uuid,
completed_by_agent boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
completed_at timestamp with time zone,
cleared_at timestamp with time zone,
replaced_at timestamp with time zone,
CONSTRAINT chat_goals_cleared_at_status_check CHECK (((status = 'cleared'::chat_goal_status) = (cleared_at IS NOT NULL))),
CONSTRAINT chat_goals_completed_at_status_check CHECK (((status = 'complete'::chat_goal_status) = (completed_at IS NOT NULL))),
CONSTRAINT chat_goals_completed_by_agent_status_check CHECK (((completed_by_agent = false) OR (status = 'complete'::chat_goal_status))),
CONSTRAINT chat_goals_completed_by_user_status_check CHECK (((completed_by_user_id IS NULL) OR (status = 'complete'::chat_goal_status))),
CONSTRAINT chat_goals_completion_summary_status_check CHECK (((completion_summary IS NULL) OR (status = 'complete'::chat_goal_status))),
CONSTRAINT chat_goals_objective_not_empty CHECK ((length(btrim(objective)) > 0)),
CONSTRAINT chat_goals_replaced_at_status_check CHECK (((status = 'replaced'::chat_goal_status) = (replaced_at IS NOT NULL)))
);
CREATE TABLE chat_messages (
id bigint NOT NULL,
chat_id uuid NOT NULL,
@@ -3844,6 +3876,9 @@ ALTER TABLE ONLY chat_file_links
ALTER TABLE ONLY chat_files
ADD CONSTRAINT chat_files_pkey PRIMARY KEY (id);
ALTER TABLE ONLY chat_goals
ADD CONSTRAINT chat_goals_pkey PRIMARY KEY (id);
ALTER TABLE ONLY chat_messages
ADD CONSTRAINT chat_messages_pkey PRIMARY KEY (id);
@@ -4274,6 +4309,10 @@ CREATE INDEX idx_chat_files_org ON chat_files USING btree (organization_id);
CREATE INDEX idx_chat_files_owner ON chat_files USING btree (owner_id);
CREATE UNIQUE INDEX idx_chat_goals_current ON chat_goals USING btree (root_chat_id) WHERE (status = ANY (ARRAY['active'::chat_goal_status, 'paused'::chat_goal_status]));
CREATE INDEX idx_chat_goals_root_created ON chat_goals USING btree (root_chat_id, created_at DESC, id DESC);
CREATE INDEX idx_chat_messages_chat ON chat_messages USING btree (chat_id);
CREATE INDEX idx_chat_messages_chat_created ON chat_messages USING btree (chat_id, created_at);
@@ -4632,6 +4671,18 @@ ALTER TABLE ONLY chat_files
ALTER TABLE ONLY chat_files
ADD CONSTRAINT chat_files_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY chat_goals
ADD CONSTRAINT chat_goals_completed_by_user_id_fkey FOREIGN KEY (completed_by_user_id) REFERENCES users(id);
ALTER TABLE ONLY chat_goals
ADD CONSTRAINT chat_goals_created_by_user_id_fkey FOREIGN KEY (created_by_user_id) REFERENCES users(id);
ALTER TABLE ONLY chat_goals
ADD CONSTRAINT chat_goals_created_from_chat_id_fkey FOREIGN KEY (created_from_chat_id) REFERENCES chats(id) ON DELETE SET NULL;
ALTER TABLE ONLY chat_goals
ADD CONSTRAINT chat_goals_root_chat_id_fkey FOREIGN KEY (root_chat_id) REFERENCES chats(id) ON DELETE CASCADE;
ALTER TABLE ONLY chat_messages
ADD CONSTRAINT chat_messages_api_key_id_fkey FOREIGN KEY (api_key_id) REFERENCES api_keys(id) ON DELETE SET NULL;