-- Project Chimera Database Schema -- Compatible with PostgreSQL (with PGVector) and SQLite -- Enable PGVector extension (PostgreSQL only) -- CREATE EXTENSION IF NOT EXISTS vector; -- Configuration Table CREATE TABLE IF NOT EXISTS config ( key VARCHAR(255) PRIMARY KEY, value TEXT NOT NULL, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Personality Prompts Table CREATE TABLE IF NOT EXISTS personality_prompts ( id TEXT PRIMARY KEY, name VARCHAR(255) NOT NULL, content TEXT NOT NULL, is_active BOOLEAN DEFAULT FALSE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- LLM Providers Table CREATE TABLE IF NOT EXISTS llm_providers ( id TEXT PRIMARY KEY, name VARCHAR(255) NOT NULL, provider_type VARCHAR(50) NOT NULL, model VARCHAR(255) NOT NULL, api_key TEXT, base_url TEXT, priority INTEGER DEFAULT 0, enabled BOOLEAN DEFAULT TRUE, config TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Facts Table (with vector support for PostgreSQL) CREATE TABLE IF NOT EXISTS facts ( id TEXT PRIMARY KEY, content TEXT NOT NULL, -- embedding vector(384), -- Uncomment for PostgreSQL with PGVector embedding_json TEXT, -- JSON serialized embedding for SQLite source_chat_id BIGINT, source_message_id BIGINT, timestamp TIMESTAMP NOT NULL, confidence REAL DEFAULT 1.0, tags TEXT, -- JSON array metadata TEXT -- JSON object ); -- Create index for vector similarity (PostgreSQL only) -- CREATE INDEX IF NOT EXISTS facts_embedding_idx ON facts -- USING ivfflat (embedding vector_cosine_ops); -- User Profiles Table CREATE TABLE IF NOT EXISTS user_profiles ( user_id BIGINT PRIMARY KEY, username VARCHAR(255), first_name VARCHAR(255), last_name VARCHAR(255), facts TEXT, -- JSON array interaction_count INTEGER DEFAULT 0, last_seen TIMESTAMP, sentiment_history TEXT, -- JSON array metadata TEXT, -- JSON object created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Conversation Goals Table CREATE TABLE IF NOT EXISTS conversation_goals ( id TEXT PRIMARY KEY, chat_id BIGINT NOT NULL, description TEXT NOT NULL, status VARCHAR(50) DEFAULT 'active', progress REAL DEFAULT 0.0, actions_taken INTEGER DEFAULT 0, success_metrics TEXT, -- JSON object created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, completed_at TIMESTAMP ); -- Chat State Table (for cooldowns and tracking) CREATE TABLE IF NOT EXISTS chat_states ( chat_id BIGINT PRIMARY KEY, last_proactive_message TIMESTAMP, cooldown_until TIMESTAMP, message_count_today INTEGER DEFAULT 0, active_goal_id TEXT, metadata TEXT, -- JSON object updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (active_goal_id) REFERENCES conversation_goals(id) ); -- Audit Log Table CREATE TABLE IF NOT EXISTS audit_logs ( id TEXT PRIMARY KEY, service VARCHAR(100) NOT NULL, action VARCHAR(255) NOT NULL, chat_id BIGINT, user_id BIGINT, details TEXT, -- JSON object timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Indexes for performance CREATE INDEX IF NOT EXISTS idx_audit_logs_timestamp ON audit_logs(timestamp DESC); CREATE INDEX IF NOT EXISTS idx_audit_logs_chat ON audit_logs(chat_id); CREATE INDEX IF NOT EXISTS idx_conversation_goals_chat ON conversation_goals(chat_id); CREATE INDEX IF NOT EXISTS idx_conversation_goals_status ON conversation_goals(status); CREATE INDEX IF NOT EXISTS idx_facts_chat ON facts(source_chat_id); CREATE INDEX IF NOT EXISTS idx_facts_timestamp ON facts(timestamp DESC); -- Insert default configuration (PostgreSQL syntax) INSERT INTO config (key, value) VALUES ('proactive_interval', '30'), ('max_context_messages', '100'), ('typing_delay_min', '1.0'), ('typing_delay_max', '5.0'), ('cooldown_min', '30'), ('cooldown_max', '120'), ('max_messages_per_chat_per_hour', '20'), ('relevance_threshold', '0.7'), ('log_level', 'INFO') ON CONFLICT (key) DO NOTHING;