From 6be69decdb746778d6c70f27f955f2d4d45448a8 Mon Sep 17 00:00:00 2001 From: Soumyadas15 Date: Mon, 26 May 2025 22:37:28 +0530 Subject: [PATCH] chore(dev): configuarble agents --- ...20250127000000_add_agent_id_to_threads.sql | 15 ----- .../20250524062639_agents_table.sql | 57 ++++++++++++------- ...0524064015_add_agent_id_to_threads_fix.sql | 19 ------- 3 files changed, 38 insertions(+), 53 deletions(-) delete mode 100644 backend/supabase/migrations/20250127000000_add_agent_id_to_threads.sql delete mode 100644 backend/supabase/migrations/20250524064015_add_agent_id_to_threads_fix.sql diff --git a/backend/supabase/migrations/20250127000000_add_agent_id_to_threads.sql b/backend/supabase/migrations/20250127000000_add_agent_id_to_threads.sql deleted file mode 100644 index 14014d3c..00000000 --- a/backend/supabase/migrations/20250127000000_add_agent_id_to_threads.sql +++ /dev/null @@ -1,15 +0,0 @@ -ALTER TABLE threads ADD COLUMN agent_id UUID REFERENCES agents(agent_id) ON DELETE SET NULL; - -CREATE INDEX idx_threads_agent_id ON threads(agent_id); - -UPDATE threads -SET agent_id = ( - SELECT a.agent_id - FROM agents a - WHERE a.account_id = threads.account_id - AND a.is_default = true - LIMIT 1 -) -WHERE agent_id IS NULL; - -COMMENT ON COLUMN threads.agent_id IS 'ID of the agent used for this conversation thread. If NULL, uses account default agent.'; \ No newline at end of file diff --git a/backend/supabase/migrations/20250524062639_agents_table.sql b/backend/supabase/migrations/20250524062639_agents_table.sql index 527497e2..8a4d4835 100644 --- a/backend/supabase/migrations/20250524062639_agents_table.sql +++ b/backend/supabase/migrations/20250524062639_agents_table.sql @@ -1,3 +1,5 @@ +BEGIN; + -- Create agents table for storing agent configurations CREATE TABLE IF NOT EXISTS agents ( agent_id UUID PRIMARY KEY DEFAULT gen_random_uuid(), @@ -12,13 +14,13 @@ CREATE TABLE IF NOT EXISTS agents ( updated_at TIMESTAMPTZ DEFAULT NOW() ); --- Add indexes for performance -CREATE INDEX idx_agents_account_id ON agents(account_id); -CREATE INDEX idx_agents_is_default ON agents(is_default); -CREATE INDEX idx_agents_created_at ON agents(created_at); +-- Add indexes for performance on agents table +CREATE INDEX IF NOT EXISTS idx_agents_account_id ON agents(account_id); +CREATE INDEX IF NOT EXISTS idx_agents_is_default ON agents(is_default); +CREATE INDEX IF NOT EXISTS idx_agents_created_at ON agents(created_at); -- Add unique constraint to ensure only one default agent per account -CREATE UNIQUE INDEX idx_agents_account_default ON agents(account_id, is_default) WHERE is_default = true; +CREATE UNIQUE INDEX IF NOT EXISTS idx_agents_account_default ON agents(account_id, is_default) WHERE is_default = true; -- Create function to update updated_at timestamp CREATE OR REPLACE FUNCTION update_agents_updated_at() @@ -29,26 +31,22 @@ BEGIN END; $$ LANGUAGE plpgsql; --- Create trigger for updated_at +-- Create trigger for updated_at (drop first if exists to avoid conflicts) +DROP TRIGGER IF EXISTS trigger_agents_updated_at ON agents; CREATE TRIGGER trigger_agents_updated_at BEFORE UPDATE ON agents FOR EACH ROW EXECUTE FUNCTION update_agents_updated_at(); --- Insert a default agent for each existing account -INSERT INTO agents (account_id, name, description, system_prompt, is_default) -SELECT - id, - 'Default Agent', - 'Default autonomous development agent', - 'You are an autonomous software development agent. You can create, modify, and deploy applications using the tools available to you. Always be helpful, accurate, and efficient in your responses.', - true -FROM basejump.accounts -ON CONFLICT DO NOTHING; - --- Add RLS policies +-- Enable RLS on agents table ALTER TABLE agents ENABLE ROW LEVEL SECURITY; +-- Drop existing policies if they exist to avoid conflicts +DROP POLICY IF EXISTS agents_select_own ON agents; +DROP POLICY IF EXISTS agents_insert_own ON agents; +DROP POLICY IF EXISTS agents_update_own ON agents; +DROP POLICY IF EXISTS agents_delete_own ON agents; + -- Policy for users to see their own agents CREATE POLICY agents_select_own ON agents FOR SELECT @@ -67,4 +65,25 @@ CREATE POLICY agents_update_own ON agents -- Policy for users to delete their own agents (except default) CREATE POLICY agents_delete_own ON agents FOR DELETE - USING (basejump.has_role_on_account(account_id, 'owner') AND is_default = false); \ No newline at end of file + USING (basejump.has_role_on_account(account_id, 'owner') AND is_default = false); + +-- NOTE: Default agent insertion has been removed per requirement + +-- Add agent_id column to threads table if it doesn't exist +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM information_schema.columns + WHERE table_name='threads' AND column_name='agent_id') THEN + ALTER TABLE threads ADD COLUMN agent_id UUID REFERENCES agents(agent_id) ON DELETE SET NULL; + CREATE INDEX idx_threads_agent_id ON threads(agent_id); + COMMENT ON COLUMN threads.agent_id IS 'ID of the agent used for this conversation thread. If NULL, uses account default agent.'; + END IF; +END $$; + +-- Update existing threads to leave agent_id NULL (no default agents inserted) +-- (Optional: if you prefer to leave existing threads with NULL agent_id, this step can be omitted.) +-- UPDATE threads +-- SET agent_id = NULL +-- WHERE agent_id IS NULL; + +COMMIT; diff --git a/backend/supabase/migrations/20250524064015_add_agent_id_to_threads_fix.sql b/backend/supabase/migrations/20250524064015_add_agent_id_to_threads_fix.sql deleted file mode 100644 index 1ae8e88d..00000000 --- a/backend/supabase/migrations/20250524064015_add_agent_id_to_threads_fix.sql +++ /dev/null @@ -1,19 +0,0 @@ -DO $$ -BEGIN - IF NOT EXISTS (SELECT 1 FROM information_schema.columns - WHERE table_name='threads' AND column_name='agent_id') THEN - ALTER TABLE threads ADD COLUMN agent_id UUID REFERENCES agents(agent_id) ON DELETE SET NULL; - CREATE INDEX idx_threads_agent_id ON threads(agent_id); - COMMENT ON COLUMN threads.agent_id IS 'ID of the agent used for this conversation thread. If NULL, uses account default agent.'; - END IF; -END $$; - -UPDATE threads -SET agent_id = ( - SELECT a.agent_id - FROM agents a - WHERE a.account_id = threads.account_id - AND a.is_default = true - LIMIT 1 -) -WHERE agent_id IS NULL;