Skip to content

Commit

Permalink
wip(agents-api): Add transitions migrations
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
creatorrr committed Dec 13, 2024
1 parent 83ea8c3 commit 3d56569
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
41 changes: 40 additions & 1 deletion memory-store/migrations/00010_tasks.sql
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
-- write your migration here
-- Create tasks table
CREATE TABLE tasks (
developer_id UUID NOT NULL,
canonical_name CITEXT NOT NULL CONSTRAINT ct_tasks_canonical_name_length CHECK (length(canonical_name) >= 1 AND length(canonical_name) <= 255),
agent_id UUID NOT NULL,
task_id UUID NOT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL CONSTRAINT ct_tasks_name_length CHECK (length(name) >= 1 AND length(name) <= 255),
description TEXT DEFAULT NULL CONSTRAINT ct_tasks_description_length CHECK (description IS NULL OR length(description) <= 1000),
input_schema JSON NOT NULL,
tools JSON[] DEFAULT ARRAY[]::JSON[],
inherit_tools BOOLEAN DEFAULT FALSE,
workflows JSON[] DEFAULT ARRAY[]::JSON[],
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
metadata JSONB DEFAULT '{}'::JSONB,
CONSTRAINT pk_tasks PRIMARY KEY (developer_id, task_id),
CONSTRAINT uq_tasks_canonical_name_unique UNIQUE (developer_id, canonical_name),
CONSTRAINT fk_tasks_agent
FOREIGN KEY (developer_id, agent_id)
REFERENCES agents(developer_id, agent_id),
CONSTRAINT ct_tasks_canonical_name_valid_identifier CHECK (canonical_name ~ '^[a-zA-Z][a-zA-Z0-9_]*$')
);

-- Create sorted index on task_id (optimized for UUID v7)
CREATE INDEX idx_tasks_id_sorted ON tasks (task_id DESC);

-- Create foreign key constraint and index on developer_id
CREATE INDEX idx_tasks_developer ON tasks (developer_id);

-- Create a GIN index on the entire metadata column
CREATE INDEX idx_tasks_metadata ON tasks USING GIN (metadata);

-- Create trigger to automatically update updated_at
CREATE TRIGGER trg_tasks_updated_at
BEFORE UPDATE ON tasks
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

-- Add comment to table
COMMENT ON TABLE tasks IS 'Stores tasks associated with AI agents for developers';
31 changes: 31 additions & 0 deletions memory-store/migrations/00011_executions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Migration to create executions table
CREATE TABLE executions (
developer_id UUID NOT NULL,
task_id UUID NOT NULL,
execution_id UUID NOT NULL,
input JSONB NOT NULL,
-- TODO: These will be generated using continuous aggregates from transitions
-- status TEXT DEFAULT 'pending',
-- output JSONB DEFAULT NULL,
-- error TEXT DEFAULT NULL,
-- updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
metadata JSONB NOT NULL DEFAULT '{}'::JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT pk_executions PRIMARY KEY (execution_id),
CONSTRAINT fk_executions_developer
FOREIGN KEY (developer_id) REFERENCES developers(developer_id),
CONSTRAINT fk_executions_task
FOREIGN KEY (developer_id, task_id) REFERENCES tasks(developer_id, task_id)
);

-- Create sorted index on execution_id (optimized for UUID v7)
CREATE INDEX idx_executions_execution_id_sorted ON executions (execution_id DESC);

-- Create index on developer_id
CREATE INDEX idx_executions_developer_id ON executions (developer_id);

-- Create a GIN index on the metadata column
CREATE INDEX idx_executions_metadata ON executions USING GIN (metadata);

-- Add comment to table
COMMENT ON TABLE executions IS 'Stores executions associated with AI agents for developers';
66 changes: 66 additions & 0 deletions memory-store/migrations/00012_transitions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
-- Create transition type enum
CREATE TYPE transition_type AS ENUM (
'init',
'finish',
'init_branch',
'finish_branch',
'wait',
'resume',
'error',
'step',
'cancelled'
);

-- Create transition cursor type
CREATE TYPE transition_cursor AS (
workflow_name TEXT,
step_index INT
);

-- Create transitions table
CREATE TABLE transitions (
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
execution_id UUID NOT NULL,
transition_id UUID NOT NULL,
type transition_type NOT NULL,
step_definition JSONB NOT NULL,
step_label TEXT DEFAULT NULL,
current_step transition_cursor NOT NULL,
next_step transition_cursor DEFAULT NULL,
output JSONB,
task_token TEXT DEFAULT NULL,
metadata JSONB DEFAULT '{}'::JSONB,
CONSTRAINT pk_transitions PRIMARY KEY (created_at, execution_id, transition_id)
);

-- Convert to hypertable
SELECT create_hypertable('transitions', 'created_at');

-- Create unique constraint for current step
CREATE UNIQUE INDEX idx_transitions_current ON transitions (execution_id, current_step, created_at DESC);

-- Create unique constraint for next step (excluding nulls)
CREATE UNIQUE INDEX idx_transitions_next ON transitions (execution_id, next_step, created_at DESC)
WHERE next_step IS NOT NULL;

-- Create unique constraint for step label (excluding nulls)
CREATE UNIQUE INDEX idx_transitions_label ON transitions (execution_id, step_label, created_at DESC)
WHERE step_label IS NOT NULL;

-- Create sorted index on transition_id (optimized for UUID v7)
CREATE INDEX idx_transitions_transition_id_sorted ON transitions (transition_id DESC, created_at DESC);

-- Create sorted index on execution_id (optimized for UUID v7)
CREATE INDEX idx_transitions_execution_id_sorted ON transitions (execution_id DESC, created_at DESC);

-- Create a GIN index on the metadata column
CREATE INDEX idx_transitions_metadata ON transitions USING GIN (metadata);

-- Add foreign key constraint
ALTER TABLE transitions
ADD CONSTRAINT fk_transitions_execution
FOREIGN KEY (execution_id)
REFERENCES executions(execution_id);

-- Add comment to table
COMMENT ON TABLE transitions IS 'Stores transitions associated with AI agents for developers';

0 comments on commit 3d56569

Please sign in to comment.