Spaces:
Sleeping
Sleeping
File size: 10,126 Bytes
e4dc511 18b34cc e4dc511 18b34cc e4dc511 18b34cc e4dc511 18b34cc e4dc511 18b34cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | -- Sentio β PostgreSQL schema for Supabase
-- Run this in the Supabase SQL editor to initialise the database.
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- Extensions
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 1. User profiles (linked to Supabase auth.users)
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS profiles (
id UUID PRIMARY KEY REFERENCES auth.users ON DELETE CASCADE,
username TEXT UNIQUE,
display_name TEXT,
bio TEXT,
onboarding_completed BOOLEAN DEFAULT FALSE,
cognitive_style JSONB DEFAULT '{}',
preferences JSONB DEFAULT '{"notifications": {"daily": true, "weekly": true, "assessments": false}}',
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Auto-create profile on user signup
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS trigger AS $$
BEGIN
INSERT INTO public.profiles (id, display_name)
VALUES (NEW.id, NEW.raw_user_meta_data->>'full_name')
ON CONFLICT (id) DO NOTHING;
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public;
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION handle_new_user();
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 2. Cognitive bias taxonomy (seeded)
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS biases (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
slug TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
category TEXT NOT NULL CHECK (category IN ('memory','social','decision','self','belief','reasoning')),
description TEXT NOT NULL,
example TEXT NOT NULL,
research_summary TEXT,
detection_signals JSONB DEFAULT '[]',
related_bias_slugs TEXT[] DEFAULT '{}',
severity_weight FLOAT DEFAULT 1.0,
prevalence_pct INT DEFAULT 50,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_biases_category ON biases(category);
CREATE INDEX IF NOT EXISTS idx_biases_slug ON biases(slug);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 3. Validated assessments (seeded)
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS assessments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
slug TEXT UNIQUE NOT NULL,
title TEXT NOT NULL,
description TEXT,
validated_tool TEXT,
research_citation TEXT,
questions JSONB NOT NULL DEFAULT '[]',
scoring_algorithm JSONB NOT NULL DEFAULT '{}',
target_biases TEXT[] DEFAULT '{}',
estimated_minutes INT DEFAULT 10,
category TEXT DEFAULT 'general',
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 4. User assessment results
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS assessment_results (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE,
assessment_id UUID REFERENCES assessments(id),
raw_scores JSONB NOT NULL DEFAULT '{}',
computed_scores JSONB NOT NULL DEFAULT '{}',
bias_implications JSONB DEFAULT '{}',
completed_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_results_user ON assessment_results(user_id);
CREATE INDEX IF NOT EXISTS idx_results_assessment ON assessment_results(assessment_id);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 5. Journal entries
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS journal_entries (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE,
content TEXT NOT NULL,
prompt_used TEXT,
mood TEXT,
sentiment_score FLOAT,
detected_biases JSONB DEFAULT '[]',
themes JSONB DEFAULT '[]',
emotions JSONB DEFAULT '[]',
embedding VECTOR(384),
processing_status TEXT DEFAULT 'pending' CHECK (processing_status IN ('pending','processing','done','failed')),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_journal_user ON journal_entries(user_id);
CREATE INDEX IF NOT EXISTS idx_journal_created ON journal_entries(created_at DESC);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 6. User bias profiles (updated by background tasks)
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS user_bias_profiles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE UNIQUE,
bias_scores JSONB NOT NULL DEFAULT '{}',
category_scores JSONB DEFAULT '{"memory":0,"social":0,"decision":0,"self":0,"belief":0,"reasoning":0}',
dominant_category TEXT,
archetype TEXT,
archetype_description TEXT,
confidence FLOAT DEFAULT 0.0,
sources JSONB DEFAULT '{"journal_entries":0,"assessments":0}',
last_updated TIMESTAMPTZ DEFAULT NOW()
);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 7. Therapist directory (seeded)
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS therapists (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
initials TEXT,
credentials TEXT[] DEFAULT '{}',
specializations TEXT[] DEFAULT '{}',
languages TEXT[] DEFAULT '{"English"}',
bio TEXT,
approach TEXT,
session_formats TEXT[] DEFAULT '{"online"}',
price_range JSONB DEFAULT '{"min": 500, "max": 1500, "currency": "INR"}',
availability JSONB DEFAULT '{"status": "available"}',
contact_info JSONB DEFAULT '{}',
verified BOOLEAN DEFAULT TRUE,
rating FLOAT DEFAULT 4.5,
review_count INT DEFAULT 0,
experience_years INT DEFAULT 5,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 8. Booking / connection requests
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS bookings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE,
therapist_id UUID REFERENCES therapists(id),
requested_at TIMESTAMPTZ,
message TEXT,
status TEXT DEFAULT 'pending' CHECK (status IN ('pending','accepted','declined','cancelled')),
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_bookings_user ON bookings(user_id);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 9. AI chat history
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS ai_conversations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE,
messages JSONB NOT NULL DEFAULT '[]',
context_summary TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_conversations_user ON ai_conversations(user_id);
-- βββββββββββββββββββββββββββββββββββββββββββββ
-- 10. RAG knowledge base (embeddings for psychology articles)
-- βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE IF NOT EXISTS knowledge_articles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
content TEXT NOT NULL,
category TEXT,
source_url TEXT,
source_citation TEXT,
embedding VECTOR(384),
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_knowledge_category ON knowledge_articles(category);
CREATE INDEX IF NOT EXISTS idx_knowledge_embedding ON knowledge_articles USING ivfflat (embedding vector_cosine_ops) WITH (lists = 50);
-- RPC function for vector similarity search (used by rag_service.py)
CREATE OR REPLACE FUNCTION match_knowledge(
query_embedding VECTOR(384),
match_threshold FLOAT DEFAULT 0.65,
match_count INT DEFAULT 10
)
RETURNS TABLE (
id UUID,
title TEXT,
content TEXT,
category TEXT,
source_url TEXT,
source_citation TEXT,
similarity FLOAT
)
LANGUAGE sql STABLE
AS $$
SELECT
id,
title,
content,
category,
source_url,
source_citation,
1 - (embedding <=> query_embedding) AS similarity
FROM knowledge_articles
WHERE 1 - (embedding <=> query_embedding) > match_threshold
ORDER BY embedding <=> query_embedding
LIMIT match_count;
$$;
|