chore(dev): configuarble agents

This commit is contained in:
Soumyadas15 2025-05-26 22:37:28 +05:30
parent a66f12498c
commit 6be69decdb
3 changed files with 38 additions and 53 deletions

View File

@ -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.';

View File

@ -1,3 +1,5 @@
BEGIN;
-- Create agents table for storing agent configurations -- Create agents table for storing agent configurations
CREATE TABLE IF NOT EXISTS agents ( CREATE TABLE IF NOT EXISTS agents (
agent_id UUID PRIMARY KEY DEFAULT gen_random_uuid(), agent_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
@ -12,13 +14,13 @@ CREATE TABLE IF NOT EXISTS agents (
updated_at TIMESTAMPTZ DEFAULT NOW() updated_at TIMESTAMPTZ DEFAULT NOW()
); );
-- Add indexes for performance -- Add indexes for performance on agents table
CREATE INDEX idx_agents_account_id ON agents(account_id); CREATE INDEX IF NOT EXISTS idx_agents_account_id ON agents(account_id);
CREATE INDEX idx_agents_is_default ON agents(is_default); CREATE INDEX IF NOT EXISTS idx_agents_is_default ON agents(is_default);
CREATE INDEX idx_agents_created_at ON agents(created_at); CREATE INDEX IF NOT EXISTS idx_agents_created_at ON agents(created_at);
-- Add unique constraint to ensure only one default agent per account -- 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 function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_agents_updated_at() CREATE OR REPLACE FUNCTION update_agents_updated_at()
@ -29,26 +31,22 @@ BEGIN
END; END;
$$ LANGUAGE plpgsql; $$ 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 CREATE TRIGGER trigger_agents_updated_at
BEFORE UPDATE ON agents BEFORE UPDATE ON agents
FOR EACH ROW FOR EACH ROW
EXECUTE FUNCTION update_agents_updated_at(); EXECUTE FUNCTION update_agents_updated_at();
-- Insert a default agent for each existing account -- Enable RLS on agents table
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
ALTER TABLE agents ENABLE ROW LEVEL SECURITY; 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 -- Policy for users to see their own agents
CREATE POLICY agents_select_own ON agents CREATE POLICY agents_select_own ON agents
FOR SELECT FOR SELECT
@ -68,3 +66,24 @@ CREATE POLICY agents_update_own ON agents
CREATE POLICY agents_delete_own ON agents CREATE POLICY agents_delete_own ON agents
FOR DELETE FOR DELETE
USING (basejump.has_role_on_account(account_id, 'owner') AND is_default = false); 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;

View File

@ -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;