File size: 4,198 Bytes
55b6d21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- 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;