-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(memory-store): Add entry tables
Signed-off-by: Diwank Singh Tomer <[email protected]>
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
BEGIN; | ||
|
||
-- Drop foreign key constraint if it exists | ||
ALTER TABLE IF EXISTS entries | ||
DROP CONSTRAINT IF EXISTS fk_entries_session; | ||
|
||
-- Drop indexes | ||
DROP INDEX IF EXISTS idx_entries_by_session; | ||
|
||
-- Drop the hypertable (this will also drop the table) | ||
DROP TABLE IF EXISTS entries; | ||
|
||
-- Drop the enum type | ||
DROP TYPE IF EXISTS chat_role; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
BEGIN; | ||
|
||
-- Create chat_role enum | ||
CREATE TYPE chat_role AS ENUM('user', 'assistant', 'tool', 'system'); | ||
|
||
-- Create entries table | ||
CREATE TABLE IF NOT EXISTS entries ( | ||
session_id UUID NOT NULL, | ||
entry_id UUID NOT NULL, | ||
source TEXT NOT NULL, | ||
role chat_role NOT NULL, | ||
event_type TEXT NOT NULL DEFAULT 'message.create', | ||
name TEXT, | ||
content JSONB[] NOT NULL, | ||
tool_call_id TEXT DEFAULT NULL, | ||
tool_calls JSONB[] NOT NULL DEFAULT '{}', | ||
token_count INTEGER NOT NULL, | ||
model TEXT NOT NULL, | ||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
timestamp TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
CONSTRAINT pk_entries PRIMARY KEY (session_id, entry_id, created_at) | ||
); | ||
|
||
-- Convert to hypertable if not already | ||
SELECT | ||
create_hypertable ( | ||
'entries', | ||
by_range ('created_at', INTERVAL '1 day'), | ||
if_not_exists => TRUE | ||
); | ||
|
||
SELECT | ||
add_dimension ( | ||
'entries', | ||
by_hash ('session_id', 2), | ||
if_not_exists => TRUE | ||
); | ||
|
||
-- Create indexes for efficient querying | ||
CREATE INDEX IF NOT EXISTS idx_entries_by_session ON entries (session_id DESC, entry_id DESC); | ||
|
||
-- Add foreign key constraint to sessions table | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT 1 FROM pg_constraint WHERE conname = 'fk_entries_session' | ||
) THEN | ||
ALTER TABLE entries | ||
ADD CONSTRAINT fk_entries_session | ||
FOREIGN KEY (session_id) | ||
REFERENCES sessions(session_id); | ||
END IF; | ||
END $$; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
BEGIN; | ||
|
||
-- Drop trigger first | ||
DROP TRIGGER IF EXISTS trg_enforce_leaf_nodes ON entry_relations; | ||
|
||
-- Drop function | ||
DROP FUNCTION IF EXISTS enforce_leaf_nodes (); | ||
|
||
-- Drop the table and its constraints | ||
DROP TABLE IF EXISTS entry_relations CASCADE; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
BEGIN; | ||
|
||
-- Create citext extension if not exists | ||
CREATE EXTENSION IF NOT EXISTS citext; | ||
|
||
-- Create entry_relations table | ||
CREATE TABLE IF NOT EXISTS entry_relations ( | ||
session_id UUID NOT NULL, | ||
head UUID NOT NULL, | ||
relation CITEXT NOT NULL, | ||
tail UUID NOT NULL, | ||
is_leaf BOOLEAN NOT NULL DEFAULT FALSE, | ||
CONSTRAINT pk_entry_relations PRIMARY KEY (session_id, head, relation, tail) | ||
); | ||
|
||
-- Add foreign key constraint to sessions table | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT 1 FROM pg_constraint WHERE conname = 'fk_entry_relations_session' | ||
) THEN | ||
ALTER TABLE entry_relations | ||
ADD CONSTRAINT fk_entry_relations_session | ||
FOREIGN KEY (session_id) | ||
REFERENCES sessions(session_id); | ||
END IF; | ||
END $$; | ||
|
||
-- Create indexes for efficient querying | ||
CREATE INDEX idx_entry_relations_components ON entry_relations (session_id, head, relation, tail); | ||
|
||
CREATE INDEX idx_entry_relations_leaf ON entry_relations (session_id, relation, is_leaf); | ||
|
||
CREATE | ||
OR REPLACE FUNCTION enforce_leaf_nodes () RETURNS TRIGGER AS $$ | ||
BEGIN | ||
IF NEW.is_leaf THEN | ||
-- Ensure no other relations point to this leaf node as a head | ||
IF EXISTS ( | ||
SELECT 1 FROM entry_relations | ||
WHERE tail = NEW.head AND session_id = NEW.session_id | ||
) THEN | ||
RAISE EXCEPTION 'Cannot assign relations to a leaf node.'; | ||
END IF; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER trg_enforce_leaf_nodes BEFORE INSERT | ||
OR | ||
UPDATE ON entry_relations FOR EACH ROW | ||
EXECUTE FUNCTION enforce_leaf_nodes (); | ||
|
||
COMMIT; |