File size: 4,379 Bytes
fb15d01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- ==========================================
-- SCHEMA DATABASE N.E.X.A (Supabase PostgreSQL)
-- Arsitektur 5 Tabel: Enterprise Vectorized Memory
-- ==========================================

-- 1. Table untuk Memori Kontekstual (Agar AI bisa mengingat chat sebelumnya)
CREATE TABLE IF NOT EXISTS nexa_chat_memories (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  role TEXT NOT NULL, -- Berisi 'user' (Anda) atau 'nexa'
  content TEXT NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- 2. Table untuk Sistem Deduplikasi Keuangan (Mencegah Pencatatan Ganda dari Tasker/Telegram)
CREATE TABLE IF NOT EXISTS nexa_finance_dedup (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  composite_key TEXT NOT NULL, -- Format rahasia: "Nominal_Merchant"
  transaction_time TIMESTAMP WITH TIME ZONE NOT NULL,
  source TEXT NOT NULL, -- Jalur masuk: 'TELEGRAM_MANUAL' atau 'TASKER_LIVIN'
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_finance_dedup_time ON nexa_finance_dedup(transaction_time);
CREATE INDEX IF NOT EXISTS idx_finance_dedup_key ON nexa_finance_dedup(composite_key);

-- 3. Table Profil Pengguna (Fakta Permanen tentang Tuan Faqih)
CREATE TABLE IF NOT EXISTS nexa_user_profile (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  content TEXT NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- 4. Table Identitas Inti AI (Pengetahuan tentang N.E.X.A)
CREATE TABLE IF NOT EXISTS nexa_core_identity (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  content TEXT NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- 5. Table Gudang Ide Mentah (2nd Brain, sinkron dengan Google Docs)
CREATE TABLE IF NOT EXISTS nexa_2nd_brain (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  content TEXT NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- 6. Table Vault Arsip (Metadata dokumen/foto penting yang disimpan di Google Drive)
-- Menyimpan indeks + teks OCR agar bisa dicari secara natural via N.E.X.A
CREATE TABLE IF NOT EXISTS nexa_vault_items (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  drive_file_id TEXT NOT NULL,
  drive_web_view_link TEXT,
  file_name TEXT,
  mime_type TEXT,
  category TEXT,
  telegram_file_id TEXT,
  source TEXT DEFAULT 'TELEGRAM',
  status TEXT DEFAULT 'DRAFT', -- DRAFT | CONFIRMED | SKIPPED
  metadata_json JSONB,
  ocr_text TEXT,
  confirmed_at TIMESTAMP WITH TIME ZONE,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_vault_items_created_at ON nexa_vault_items(created_at);
CREATE INDEX IF NOT EXISTS idx_vault_items_drive_file_id ON nexa_vault_items(drive_file_id);

-- NOTE (Jika tabel sudah terlanjur dibuat tanpa kolom di atas, jalankan ALTER berikut di Supabase SQL Editor):
-- ALTER TABLE public.nexa_vault_items
--   ADD COLUMN IF NOT EXISTS status TEXT DEFAULT 'DRAFT',
--   ADD COLUMN IF NOT EXISTS metadata_json JSONB,
--   ADD COLUMN IF NOT EXISTS confirmed_at TIMESTAMP WITH TIME ZONE;

-- ==========================================
-- [PHASE 6 — Pilar 8.2] BEHAVIORAL PATTERN ENGINE
-- ==========================================
-- 7. Table Transaksi Tertunda (Digunakan saat server restart untuk recovery)
CREATE TABLE IF NOT EXISTS nexa_pending_transactions (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  composite_key TEXT NOT NULL UNIQUE,
  tx_data JSONB NOT NULL,
  telegram_sent BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- 8. Table Log Perilaku (Melacak rutinitas harian untuk insight proaktif)
-- Jalankan SQL ini di Supabase SQL Editor sebelum deploy.
CREATE TABLE IF NOT EXISTS nexa_behavior_log (
  id          BIGSERIAL PRIMARY KEY,
  event_type  TEXT NOT NULL,      -- 'WAKE_UP' | 'MOOD_DETECTED' | 'FINANCE_RECORD' | dll
  event_data  JSONB DEFAULT '{}', -- Payload konteks spesifik per event_type
  day_of_week INT,                -- 0=Minggu ... 6=Sabtu (waktu Jakarta)
  hour_of_day INT,                -- 0-23 (waktu Jakarta)
  created_at  TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_behavior_log_type ON nexa_behavior_log(event_type);
CREATE INDEX IF NOT EXISTS idx_behavior_log_created ON nexa_behavior_log(created_at);
CREATE INDEX IF NOT EXISTS idx_behavior_log_day_hour ON nexa_behavior_log(day_of_week, hour_of_day);