Repository navigation
Expand file tree
/
Copy pathschema.ts
More file actions
2064 lines (1844 loc) · 83.8 KB
/
Copy pathschema.ts
File metadata and controls
2064 lines (1844 loc) · 83.8 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2026 Rob Macrae. All rights reserved.
// SPDX-License-Identifier: LicenseRef-Proprietary
/**
* Database Schema
*
* D1 (SQLite) schema for durable state.
* This is the source of truth - Durable Objects can be rebuilt from this.
*/
export const SCHEMA = `
-- Users
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
-- Dashboards
CREATE TABLE IF NOT EXISTS dashboards (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
owner_id TEXT NOT NULL REFERENCES users(id),
setup_guide TEXT,
-- Desktop: when this dashboard was downloaded from a cloud account, the id of
-- the source cloud dashboard (for the downloaded ✓ state + two-way sync). NULL
-- for purely-local dashboards.
cloud_id TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_dashboards_owner ON dashboards(owner_id);
-- Dashboard members (for sharing)
CREATE TABLE IF NOT EXISTS dashboard_members (
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id),
role TEXT NOT NULL CHECK (role IN ('owner', 'editor', 'viewer')),
added_at TEXT NOT NULL DEFAULT (datetime('now')),
PRIMARY KEY (dashboard_id, user_id)
);
-- Dashboard invitations (pending access for non-existing users)
CREATE TABLE IF NOT EXISTS dashboard_invitations (
id TEXT PRIMARY KEY,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
email TEXT NOT NULL,
role TEXT NOT NULL CHECK (role IN ('editor', 'viewer')),
invited_by TEXT NOT NULL REFERENCES users(id),
token TEXT NOT NULL UNIQUE,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
expires_at TEXT NOT NULL,
accepted_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_invitations_dashboard ON dashboard_invitations(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_invitations_email ON dashboard_invitations(email);
-- Dashboard items (notes, todos, terminal attachments, links, browsers)
CREATE TABLE IF NOT EXISTS dashboard_items (
id TEXT PRIMARY KEY,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
type TEXT NOT NULL,
content TEXT NOT NULL DEFAULT '',
position_x INTEGER NOT NULL DEFAULT 0,
position_y INTEGER NOT NULL DEFAULT 0,
width INTEGER NOT NULL DEFAULT 200,
height INTEGER NOT NULL DEFAULT 150,
metadata TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_items_dashboard ON dashboard_items(dashboard_id);
-- Dashboard edges (connections between blocks)
CREATE TABLE IF NOT EXISTS dashboard_edges (
id TEXT PRIMARY KEY,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
source_item_id TEXT NOT NULL REFERENCES dashboard_items(id) ON DELETE CASCADE,
target_item_id TEXT NOT NULL REFERENCES dashboard_items(id) ON DELETE CASCADE,
source_handle TEXT,
target_handle TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_edges_dashboard ON dashboard_edges(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_edges_source ON dashboard_edges(source_item_id);
CREATE INDEX IF NOT EXISTS idx_edges_target ON dashboard_edges(target_item_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_edges_unique
ON dashboard_edges(dashboard_id, source_item_id, target_item_id, source_handle, target_handle);
-- Sessions (map dashboard terminals to sandbox sessions)
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
item_id TEXT NOT NULL REFERENCES dashboard_items(id) ON DELETE CASCADE,
owner_user_id TEXT NOT NULL REFERENCES users(id),
owner_name TEXT NOT NULL DEFAULT '',
sandbox_session_id TEXT NOT NULL,
sandbox_machine_id TEXT NOT NULL DEFAULT '',
pty_id TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL CHECK (status IN ('creating', 'active', 'stopped', 'error')),
region TEXT NOT NULL DEFAULT 'local',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
stopped_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_sessions_dashboard ON sessions(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_sessions_item ON sessions(item_id);
-- Dashboard sandbox mapping (one sandbox per dashboard)
-- SECURITY CRITICAL: The PRIMARY KEY on dashboard_id enforces that each dashboard
-- gets exactly ONE dedicated VM. This isolation is essential for secrets security.
-- DO NOT modify this to allow multiple dashboards per sandbox or shared sandboxes.
-- The secrets broker runs per-sandbox and would leak secrets/approvals between
-- dashboards if this 1:1 mapping were ever broken.
CREATE TABLE IF NOT EXISTS dashboard_sandboxes (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
sandbox_session_id TEXT NOT NULL,
sandbox_machine_id TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
-- Agent profiles (reusable agent configurations)
CREATE TABLE IF NOT EXISTS agent_profiles (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
tools TEXT NOT NULL DEFAULT '[]',
system_prompt TEXT NOT NULL DEFAULT '',
policy TEXT NOT NULL DEFAULT '{}',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
-- User subagents (Claude Code subagent favorites)
CREATE TABLE IF NOT EXISTS user_subagents (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
prompt TEXT NOT NULL DEFAULT '',
tools TEXT NOT NULL DEFAULT '[]',
source TEXT NOT NULL DEFAULT 'custom',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_user_subagents_user ON user_subagents(user_id);
-- User custom model endpoints (Ollama / vLLM / self-hosted / cloud BYO).
-- See PLAN-custom-endpoints.md. The API key (if any) is a user_secrets entry
-- referenced by secret_name — this table never stores the key.
CREATE TABLE IF NOT EXISTS user_model_providers (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
label TEXT NOT NULL,
base_url TEXT NOT NULL,
format TEXT NOT NULL DEFAULT 'openai', -- 'openai' | 'anthropic'
model_id TEXT NOT NULL,
secret_name TEXT, -- ref to user_secrets.name (the API key), nullable
context_window INTEGER,
max_output_tokens INTEGER,
compatible_harnesses TEXT NOT NULL DEFAULT '[]', -- JSON array of harness ids
is_local INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_user_model_providers_user ON user_model_providers(user_id);
-- User agent skills (Claude Code slash command favorites)
CREATE TABLE IF NOT EXISTS user_agent_skills (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
command TEXT NOT NULL DEFAULT '',
args TEXT NOT NULL DEFAULT '[]',
source TEXT NOT NULL DEFAULT 'custom',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_user_agent_skills_user ON user_agent_skills(user_id);
-- User MCP tools (Model Context Protocol tool configurations)
CREATE TABLE IF NOT EXISTS user_mcp_tools (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
server_url TEXT NOT NULL DEFAULT '',
transport TEXT NOT NULL DEFAULT 'stdio' CHECK (transport IN ('stdio', 'sse', 'streamable-http')),
config TEXT NOT NULL DEFAULT '{}',
source TEXT NOT NULL DEFAULT 'custom',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_user_mcp_tools_user ON user_mcp_tools(user_id);
-- User secrets and environment variables
-- type='secret' → brokered (for API keys, credentials)
-- type='env_var' → set directly (for regular config)
CREATE TABLE IF NOT EXISTS user_secrets (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL DEFAULT '',
name TEXT NOT NULL,
value TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
type TEXT NOT NULL DEFAULT 'secret' CHECK (type IN ('secret', 'env_var')),
broker_protected INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_user_secrets_user ON user_secrets(user_id);
CREATE INDEX IF NOT EXISTS idx_user_secrets_dashboard ON user_secrets(dashboard_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_secrets_unique_name ON user_secrets(user_id, dashboard_id, name);
-- OAuth state (short-lived)
CREATE TABLE IF NOT EXISTS oauth_states (
state TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
provider TEXT NOT NULL,
metadata TEXT NOT NULL DEFAULT '{}',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_oauth_states_user ON oauth_states(user_id);
-- User integrations (OAuth tokens)
CREATE TABLE IF NOT EXISTS user_integrations (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
provider TEXT NOT NULL,
access_token TEXT NOT NULL,
refresh_token TEXT,
scope TEXT,
token_type TEXT,
expires_at TEXT,
metadata TEXT NOT NULL DEFAULT '{}',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_integrations_user_provider
ON user_integrations(user_id, provider);
-- Drive mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS drive_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
folder_id TEXT NOT NULL,
folder_name TEXT NOT NULL,
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing_cache', 'syncing_workspace', 'ready', 'error')),
total_files INTEGER NOT NULL DEFAULT 0,
total_bytes INTEGER NOT NULL DEFAULT 0,
cache_synced_files INTEGER NOT NULL DEFAULT 0,
cache_synced_bytes INTEGER NOT NULL DEFAULT 0,
cache_last_path TEXT,
workspace_synced_files INTEGER NOT NULL DEFAULT 0,
workspace_synced_bytes INTEGER NOT NULL DEFAULT 0,
large_files INTEGER NOT NULL DEFAULT 0,
large_bytes INTEGER NOT NULL DEFAULT 0,
last_sync_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_drive_mirrors_user ON drive_mirrors(user_id);
-- GitHub mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS github_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
repo_id TEXT NOT NULL,
repo_owner TEXT NOT NULL,
repo_name TEXT NOT NULL,
repo_branch TEXT NOT NULL,
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing_cache', 'syncing_workspace', 'ready', 'error')),
total_files INTEGER NOT NULL DEFAULT 0,
total_bytes INTEGER NOT NULL DEFAULT 0,
cache_synced_files INTEGER NOT NULL DEFAULT 0,
cache_synced_bytes INTEGER NOT NULL DEFAULT 0,
workspace_synced_files INTEGER NOT NULL DEFAULT 0,
workspace_synced_bytes INTEGER NOT NULL DEFAULT 0,
large_files INTEGER NOT NULL DEFAULT 0,
large_bytes INTEGER NOT NULL DEFAULT 0,
last_sync_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_github_mirrors_user ON github_mirrors(user_id);
-- GitHub repo history (per dashboard)
CREATE TABLE IF NOT EXISTS github_repo_history (
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
repo_id TEXT NOT NULL,
repo_owner TEXT NOT NULL,
repo_name TEXT NOT NULL,
repo_branch TEXT NOT NULL,
last_linked_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now')),
PRIMARY KEY (dashboard_id, repo_owner, repo_name)
);
CREATE INDEX IF NOT EXISTS idx_github_repo_history_user ON github_repo_history(user_id);
CREATE INDEX IF NOT EXISTS idx_github_repo_history_dashboard ON github_repo_history(dashboard_id);
-- Box mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS box_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
folder_id TEXT NOT NULL,
folder_name TEXT NOT NULL,
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing_cache', 'syncing_workspace', 'ready', 'error')),
total_files INTEGER NOT NULL DEFAULT 0,
total_bytes INTEGER NOT NULL DEFAULT 0,
cache_synced_files INTEGER NOT NULL DEFAULT 0,
cache_synced_bytes INTEGER NOT NULL DEFAULT 0,
workspace_synced_files INTEGER NOT NULL DEFAULT 0,
workspace_synced_bytes INTEGER NOT NULL DEFAULT 0,
large_files INTEGER NOT NULL DEFAULT 0,
large_bytes INTEGER NOT NULL DEFAULT 0,
last_sync_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_box_mirrors_user ON box_mirrors(user_id);
-- OneDrive mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS onedrive_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
folder_id TEXT NOT NULL,
folder_name TEXT NOT NULL,
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing_cache', 'syncing_workspace', 'ready', 'error')),
total_files INTEGER NOT NULL DEFAULT 0,
total_bytes INTEGER NOT NULL DEFAULT 0,
cache_synced_files INTEGER NOT NULL DEFAULT 0,
cache_synced_bytes INTEGER NOT NULL DEFAULT 0,
workspace_synced_files INTEGER NOT NULL DEFAULT 0,
workspace_synced_bytes INTEGER NOT NULL DEFAULT 0,
large_files INTEGER NOT NULL DEFAULT 0,
large_bytes INTEGER NOT NULL DEFAULT 0,
last_sync_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_onedrive_mirrors_user ON onedrive_mirrors(user_id);
-- Gmail mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS gmail_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
email_address TEXT NOT NULL,
label_ids TEXT NOT NULL DEFAULT '["INBOX"]',
history_id TEXT,
watch_expiration TEXT,
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing', 'watching', 'ready', 'error')),
last_synced_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_gmail_mirrors_user ON gmail_mirrors(user_id);
CREATE INDEX IF NOT EXISTS idx_gmail_mirrors_email ON gmail_mirrors(email_address);
-- Gmail messages (metadata cache)
CREATE TABLE IF NOT EXISTS gmail_messages (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
message_id TEXT NOT NULL,
thread_id TEXT NOT NULL,
internal_date TEXT NOT NULL,
from_header TEXT,
to_header TEXT,
subject TEXT,
snippet TEXT,
labels TEXT NOT NULL DEFAULT '[]',
size_estimate INTEGER,
body_state TEXT NOT NULL DEFAULT 'none' CHECK (body_state IN ('none', 'snippet', 'full')),
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_gmail_messages_user ON gmail_messages(user_id);
CREATE INDEX IF NOT EXISTS idx_gmail_messages_dashboard ON gmail_messages(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_gmail_messages_thread ON gmail_messages(thread_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_gmail_messages_message ON gmail_messages(dashboard_id, message_id);
-- Gmail action audit log
CREATE TABLE IF NOT EXISTS gmail_actions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
message_id TEXT NOT NULL,
action TEXT NOT NULL CHECK (action IN ('label_add', 'label_remove', 'archive', 'trash', 'mark_read', 'mark_unread')),
details TEXT NOT NULL DEFAULT '{}',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_gmail_actions_user ON gmail_actions(user_id);
CREATE INDEX IF NOT EXISTS idx_gmail_actions_dashboard ON gmail_actions(dashboard_id);
-- Outlook email mirrors
CREATE TABLE IF NOT EXISTS outlook_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
email_address TEXT NOT NULL,
folder_id TEXT NOT NULL DEFAULT 'inbox',
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing', 'ready', 'error')),
last_synced_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_outlook_mirrors_user ON outlook_mirrors(user_id);
CREATE INDEX IF NOT EXISTS idx_outlook_mirrors_email ON outlook_mirrors(email_address);
-- Outlook messages (metadata cache)
CREATE TABLE IF NOT EXISTS outlook_messages (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
message_id TEXT NOT NULL,
conversation_id TEXT,
received_date TEXT NOT NULL,
from_address TEXT,
from_name TEXT,
to_addresses TEXT,
subject TEXT,
body_preview TEXT,
is_read INTEGER NOT NULL DEFAULT 0,
has_attachments INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_outlook_messages_user ON outlook_messages(user_id);
CREATE INDEX IF NOT EXISTS idx_outlook_messages_dashboard ON outlook_messages(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_outlook_messages_conversation ON outlook_messages(conversation_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_outlook_messages_message ON outlook_messages(dashboard_id, message_id);
-- Outlook action audit log
CREATE TABLE IF NOT EXISTS outlook_actions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
message_id TEXT NOT NULL,
action TEXT NOT NULL CHECK (action IN ('archive', 'delete', 'mark_read', 'mark_unread')),
details TEXT NOT NULL DEFAULT '{}',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_outlook_actions_user ON outlook_actions(user_id);
CREATE INDEX IF NOT EXISTS idx_outlook_actions_dashboard ON outlook_actions(dashboard_id);
-- Calendar mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS calendar_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
email_address TEXT NOT NULL,
calendar_id TEXT NOT NULL DEFAULT 'primary',
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing', 'watching', 'ready', 'error')),
sync_token TEXT,
last_synced_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_calendar_mirrors_user ON calendar_mirrors(user_id);
-- Calendar events (metadata cache)
CREATE TABLE IF NOT EXISTS calendar_events (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
event_id TEXT NOT NULL,
calendar_id TEXT NOT NULL,
summary TEXT,
description TEXT,
location TEXT,
start_time TEXT NOT NULL,
end_time TEXT NOT NULL,
all_day INTEGER NOT NULL DEFAULT 0,
status TEXT,
html_link TEXT,
organizer_email TEXT,
attendees TEXT NOT NULL DEFAULT '[]',
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_calendar_events_user ON calendar_events(user_id);
CREATE INDEX IF NOT EXISTS idx_calendar_events_dashboard ON calendar_events(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_calendar_events_start ON calendar_events(start_time);
CREATE UNIQUE INDEX IF NOT EXISTS idx_calendar_events_event ON calendar_events(dashboard_id, event_id);
-- Contacts mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS contacts_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
email_address TEXT NOT NULL,
sync_token TEXT,
status TEXT NOT NULL CHECK (status IN ('idle', 'syncing', 'ready', 'error')),
last_synced_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_contacts_mirrors_user ON contacts_mirrors(user_id);
-- Contacts cache
CREATE TABLE IF NOT EXISTS contacts (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
resource_name TEXT NOT NULL,
display_name TEXT,
given_name TEXT,
family_name TEXT,
email_addresses TEXT NOT NULL DEFAULT '[]',
phone_numbers TEXT NOT NULL DEFAULT '[]',
organizations TEXT NOT NULL DEFAULT '[]',
photo_url TEXT,
notes TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_contacts_user ON contacts(user_id);
CREATE INDEX IF NOT EXISTS idx_contacts_dashboard ON contacts(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts(display_name);
CREATE UNIQUE INDEX IF NOT EXISTS idx_contacts_resource ON contacts(dashboard_id, resource_name);
-- Sheets mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS sheets_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
email_address TEXT NOT NULL,
spreadsheet_id TEXT,
spreadsheet_name TEXT,
status TEXT NOT NULL CHECK (status IN ('idle', 'linked', 'error')),
last_accessed_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_sheets_mirrors_user ON sheets_mirrors(user_id);
-- Forms mirrors (per dashboard)
CREATE TABLE IF NOT EXISTS forms_mirrors (
dashboard_id TEXT PRIMARY KEY REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
email_address TEXT NOT NULL,
form_id TEXT,
form_title TEXT,
status TEXT NOT NULL CHECK (status IN ('idle', 'linked', 'error')),
last_accessed_at TEXT,
sync_error TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_forms_mirrors_user ON forms_mirrors(user_id);
-- Form responses cache
CREATE TABLE IF NOT EXISTS form_responses (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
form_id TEXT NOT NULL,
response_id TEXT NOT NULL,
respondent_email TEXT,
submitted_at TEXT NOT NULL,
answers TEXT NOT NULL DEFAULT '{}',
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_form_responses_user ON form_responses(user_id);
CREATE INDEX IF NOT EXISTS idx_form_responses_dashboard ON form_responses(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_form_responses_form ON form_responses(form_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_form_responses_response ON form_responses(dashboard_id, response_id);
-- Auth sessions (first-party login)
CREATE TABLE IF NOT EXISTS user_sessions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_user_sessions_user ON user_sessions(user_id);
CREATE INDEX IF NOT EXISTS idx_user_sessions_expires ON user_sessions(expires_at);
-- Personal access tokens for CLI / external tools (orcabot push/pull).
-- Only the SHA-256 hash of the token is stored; the plaintext is shown once at creation.
CREATE TABLE IF NOT EXISTS api_tokens (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token_hash TEXT NOT NULL UNIQUE,
name TEXT NOT NULL DEFAULT 'cli',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
last_used_at TEXT,
expires_at TEXT,
revoked_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_api_tokens_user ON api_tokens(user_id);
CREATE INDEX IF NOT EXISTS idx_api_tokens_hash ON api_tokens(token_hash);
-- OAuth login states (short-lived)
CREATE TABLE IF NOT EXISTS auth_states (
state TEXT PRIMARY KEY,
redirect_url TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
-- Recipes (workflow definitions)
CREATE TABLE IF NOT EXISTS recipes (
id TEXT PRIMARY KEY,
dashboard_id TEXT REFERENCES dashboards(id) ON DELETE SET NULL,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
steps TEXT NOT NULL DEFAULT '[]',
created_by TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_recipes_dashboard ON recipes(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_recipes_created_by ON recipes(created_by);
-- Executions (workflow runs)
CREATE TABLE IF NOT EXISTS executions (
id TEXT PRIMARY KEY,
recipe_id TEXT NOT NULL REFERENCES recipes(id) ON DELETE CASCADE,
status TEXT NOT NULL CHECK (status IN ('pending', 'running', 'paused', 'completed', 'failed')),
current_step_id TEXT,
context TEXT NOT NULL DEFAULT '{}',
started_at TEXT NOT NULL DEFAULT (datetime('now')),
completed_at TEXT,
error TEXT
);
CREATE INDEX IF NOT EXISTS idx_executions_recipe ON executions(recipe_id);
CREATE INDEX IF NOT EXISTS idx_executions_status ON executions(status);
-- Artifacts (outputs from executions)
CREATE TABLE IF NOT EXISTS artifacts (
id TEXT PRIMARY KEY,
execution_id TEXT NOT NULL REFERENCES executions(id) ON DELETE CASCADE,
step_id TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('file', 'log', 'summary', 'output')),
name TEXT NOT NULL,
content TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_artifacts_execution ON artifacts(execution_id);
-- Schedules (cron or event triggers)
-- recipe_id is nullable: edge-based schedules use dashboard_item_id instead
CREATE TABLE IF NOT EXISTS schedules (
id TEXT PRIMARY KEY,
recipe_id TEXT REFERENCES recipes(id) ON DELETE CASCADE,
dashboard_id TEXT REFERENCES dashboards(id) ON DELETE CASCADE,
dashboard_item_id TEXT,
command TEXT,
name TEXT NOT NULL,
cron TEXT,
event_trigger TEXT,
enabled INTEGER NOT NULL DEFAULT 1,
last_run_at TEXT,
next_run_at TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
CHECK (recipe_id IS NOT NULL OR dashboard_item_id IS NOT NULL),
CHECK (dashboard_item_id IS NULL OR dashboard_id IS NOT NULL)
);
CREATE INDEX IF NOT EXISTS idx_schedules_recipe ON schedules(recipe_id);
CREATE INDEX IF NOT EXISTS idx_schedules_next_run ON schedules(next_run_at);
CREATE INDEX IF NOT EXISTS idx_schedules_dashboard ON schedules(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_schedules_item ON schedules(dashboard_item_id);
-- Schedule executions (track each cron/manual trigger)
CREATE TABLE IF NOT EXISTS schedule_executions (
id TEXT PRIMARY KEY,
schedule_id TEXT NOT NULL REFERENCES schedules(id) ON DELETE CASCADE,
status TEXT NOT NULL CHECK (status IN ('running', 'completed', 'failed', 'timed_out')),
triggered_by TEXT NOT NULL DEFAULT 'cron',
terminals_json TEXT NOT NULL DEFAULT '[]',
started_at TEXT NOT NULL DEFAULT (datetime('now')),
completed_at TEXT,
error TEXT
);
CREATE INDEX IF NOT EXISTS idx_schedule_executions_schedule ON schedule_executions(schedule_id);
CREATE INDEX IF NOT EXISTS idx_schedule_executions_status ON schedule_executions(status);
-- System health (cached health check results)
CREATE TABLE IF NOT EXISTS system_health (
service TEXT PRIMARY KEY,
is_healthy INTEGER NOT NULL DEFAULT 0,
last_check_at TEXT NOT NULL DEFAULT (datetime('now')),
last_error TEXT,
consecutive_failures INTEGER NOT NULL DEFAULT 0
);
-- Secret domain allowlist (approved domains for custom secrets)
CREATE TABLE IF NOT EXISTS user_secret_allowlist (
id TEXT PRIMARY KEY,
secret_id TEXT NOT NULL REFERENCES user_secrets(id) ON DELETE CASCADE,
domain TEXT NOT NULL,
header_name TEXT NOT NULL DEFAULT 'Authorization',
header_format TEXT NOT NULL DEFAULT 'Bearer %s',
created_by TEXT NOT NULL REFERENCES users(id),
created_at TEXT NOT NULL DEFAULT (datetime('now')),
revoked_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_secret_allowlist_secret ON user_secret_allowlist(secret_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_secret_allowlist_unique ON user_secret_allowlist(secret_id, domain) WHERE revoked_at IS NULL;
-- Pending domain approval requests (for notification)
CREATE TABLE IF NOT EXISTS pending_domain_approvals (
id TEXT PRIMARY KEY,
secret_id TEXT NOT NULL REFERENCES user_secrets(id) ON DELETE CASCADE,
domain TEXT NOT NULL,
requested_at TEXT NOT NULL DEFAULT (datetime('now')),
dismissed_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_pending_approvals_secret ON pending_domain_approvals(secret_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_pending_approvals_unique ON pending_domain_approvals(secret_id, domain) WHERE dismissed_at IS NULL;
-- Dashboard templates (global, shareable)
CREATE TABLE IF NOT EXISTS dashboard_templates (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
category TEXT NOT NULL DEFAULT 'custom' CHECK (category IN ('coding', 'automation', 'documentation', 'custom')),
preview_image_url TEXT,
author_id TEXT NOT NULL REFERENCES users(id),
author_name TEXT NOT NULL DEFAULT '',
items_json TEXT NOT NULL DEFAULT '[]',
edges_json TEXT NOT NULL DEFAULT '[]',
viewport_json TEXT,
setup_guide TEXT,
item_count INTEGER NOT NULL DEFAULT 0,
is_featured INTEGER NOT NULL DEFAULT 0,
use_count INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'approved' CHECK (status IN ('pending_review', 'approved', 'rejected')),
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_templates_category ON dashboard_templates(category);
CREATE INDEX IF NOT EXISTS idx_templates_featured ON dashboard_templates(is_featured);
CREATE INDEX IF NOT EXISTS idx_templates_author ON dashboard_templates(author_id);
-- Terminal integrations (per-terminal integration bindings)
-- SECURITY: Each binding is immutable once created (terminal_id, provider, user_integration_id cannot change)
-- Uses soft delete to preserve audit history
CREATE TABLE IF NOT EXISTS terminal_integrations (
id TEXT PRIMARY KEY,
terminal_id TEXT NOT NULL,
item_id TEXT,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
provider TEXT NOT NULL,
user_integration_id TEXT REFERENCES user_integrations(id),
active_policy_id TEXT,
account_email TEXT,
account_label TEXT,
deleted_at TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
created_by TEXT NOT NULL REFERENCES users(id),
CHECK (provider IN ('browser', 'whatsapp') OR user_integration_id IS NOT NULL)
);
CREATE INDEX IF NOT EXISTS idx_terminal_integrations_terminal ON terminal_integrations(terminal_id);
CREATE INDEX IF NOT EXISTS idx_terminal_integrations_dashboard ON terminal_integrations(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_terminal_integrations_user ON terminal_integrations(user_id);
-- Integration policies (append-only policy revisions)
CREATE TABLE IF NOT EXISTS integration_policies (
id TEXT PRIMARY KEY,
terminal_integration_id TEXT NOT NULL REFERENCES terminal_integrations(id),
version INTEGER NOT NULL,
policy TEXT NOT NULL,
security_level TEXT NOT NULL CHECK (security_level IN ('restricted', 'elevated', 'full')),
created_at TEXT NOT NULL DEFAULT (datetime('now')),
created_by TEXT NOT NULL REFERENCES users(id),
UNIQUE(terminal_integration_id, version)
);
CREATE INDEX IF NOT EXISTS idx_policies_terminal_integration ON integration_policies(terminal_integration_id);
-- Integration audit log (all gateway operations)
CREATE TABLE IF NOT EXISTS integration_audit_log (
id TEXT PRIMARY KEY,
terminal_integration_id TEXT NOT NULL REFERENCES terminal_integrations(id),
terminal_id TEXT NOT NULL,
dashboard_id TEXT NOT NULL,
user_id TEXT NOT NULL,
provider TEXT NOT NULL,
action TEXT NOT NULL,
resource_id TEXT,
policy_id TEXT NOT NULL REFERENCES integration_policies(id),
policy_version INTEGER NOT NULL,
policy_decision TEXT NOT NULL CHECK (policy_decision IN ('allowed', 'denied', 'filtered')),
denial_reason TEXT,
request_summary TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_audit_terminal ON integration_audit_log(terminal_id);
CREATE INDEX IF NOT EXISTS idx_audit_dashboard ON integration_audit_log(dashboard_id, created_at);
CREATE INDEX IF NOT EXISTS idx_audit_policy ON integration_audit_log(policy_id);
CREATE INDEX IF NOT EXISTS idx_audit_terminal_integration ON integration_audit_log(terminal_integration_id);
-- High-risk capability confirmations (anti-social-engineering audit trail)
CREATE TABLE IF NOT EXISTS high_risk_confirmations (
id TEXT PRIMARY KEY,
terminal_integration_id TEXT NOT NULL REFERENCES terminal_integrations(id),
capability TEXT NOT NULL,
confirmed_at TEXT NOT NULL DEFAULT (datetime('now')),
confirmed_by TEXT NOT NULL REFERENCES users(id),
user_agent TEXT,
ip_address TEXT
);
CREATE INDEX IF NOT EXISTS idx_high_risk_terminal_integration ON high_risk_confirmations(terminal_integration_id);
-- Messaging subscriptions (inbound channel/chat bindings for messaging blocks)
CREATE TABLE IF NOT EXISTS messaging_subscriptions (
id TEXT PRIMARY KEY,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
item_id TEXT NOT NULL REFERENCES dashboard_items(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
provider TEXT NOT NULL,
channel_id TEXT,
channel_name TEXT,
chat_id TEXT,
team_id TEXT,
webhook_id TEXT,
webhook_secret TEXT,
status TEXT NOT NULL CHECK (status IN ('pending', 'active', 'paused', 'error')),
last_message_at TEXT,
error_message TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_messaging_subs_dashboard ON messaging_subscriptions(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_messaging_subs_item ON messaging_subscriptions(item_id);
CREATE INDEX IF NOT EXISTS idx_messaging_subs_webhook ON messaging_subscriptions(webhook_id);
CREATE INDEX IF NOT EXISTS idx_messaging_subs_provider_channel ON messaging_subscriptions(provider, channel_id);
CREATE INDEX IF NOT EXISTS idx_messaging_subs_provider_team_channel ON messaging_subscriptions(provider, team_id, channel_id);
-- Unique per block + provider + channel + chat (allows multi-channel per block).
-- WhatsApp uses channel_id=phone_number_id + chat_id=sender_phone, so we need both.
-- Telegram/Matrix use chat_id only. Slack/Discord use channel_id only.
-- COALESCE('') prevents SQLite from ignoring NULLs in the unique constraint.
-- Migration guard: deactivate any legacy rows with both fields null before
-- recreating the index, since COALESCE('','') would cause collisions.
-- Exclude bridge (personal WhatsApp) subscriptions which intentionally have
-- both fields null — they use webhook_id prefix 'bridge_' for identification.
UPDATE messaging_subscriptions
SET status = 'error', error_message = 'migrated: missing channel scope'
WHERE channel_id IS NULL AND chat_id IS NULL AND status IN ('pending', 'active')
AND webhook_id NOT LIKE 'bridge_%';
DROP INDEX IF EXISTS idx_messaging_subs_active_channel;
CREATE UNIQUE INDEX IF NOT EXISTS idx_messaging_subs_active_channel
ON messaging_subscriptions(dashboard_id, item_id, provider, COALESCE(channel_id, ''), COALESCE(chat_id, ''))
WHERE status IN ('pending', 'active');
-- Inbound messages (buffer for messages arriving while VM is sleeping)
CREATE TABLE IF NOT EXISTS inbound_messages (
id TEXT PRIMARY KEY,
subscription_id TEXT NOT NULL REFERENCES messaging_subscriptions(id) ON DELETE CASCADE,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
provider TEXT NOT NULL,
platform_message_id TEXT NOT NULL,
sender_id TEXT,
sender_name TEXT,
channel_id TEXT,
channel_name TEXT,
message_text TEXT,
message_metadata TEXT NOT NULL DEFAULT '{}',
status TEXT NOT NULL CHECK (status IN ('buffered', 'delivering', 'delivered', 'failed', 'expired')),
delivery_attempts INTEGER NOT NULL DEFAULT 0,
claimed_at TEXT,
delivered_terminals TEXT NOT NULL DEFAULT '[]',
delivered_at TEXT,
expires_at TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_inbound_messages_sub ON inbound_messages(subscription_id);
CREATE INDEX IF NOT EXISTS idx_inbound_messages_dashboard ON inbound_messages(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_inbound_messages_status ON inbound_messages(status);
CREATE UNIQUE INDEX IF NOT EXISTS idx_inbound_messages_dedup ON inbound_messages(subscription_id, platform_message_id);
CREATE INDEX IF NOT EXISTS idx_inbound_messages_expires ON inbound_messages(expires_at);
-- ============================================
-- Agent State: Tasks & Memory
-- ============================================
-- Agent tasks (per-dashboard, optionally scoped to session/terminal)
CREATE TABLE IF NOT EXISTS agent_tasks (
id TEXT PRIMARY KEY,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
session_id TEXT, -- Optional: scope to specific terminal
parent_id TEXT REFERENCES agent_tasks(id), -- Subtasks
-- Task content
subject TEXT NOT NULL,
description TEXT,
status TEXT NOT NULL DEFAULT 'pending'
CHECK (status IN ('pending', 'in_progress', 'blocked', 'completed', 'cancelled')),
priority INTEGER NOT NULL DEFAULT 0,
-- Dependencies (JSON arrays of task IDs)
blocked_by TEXT NOT NULL DEFAULT '[]',
blocks TEXT NOT NULL DEFAULT '[]',
-- Agent metadata
owner_agent TEXT, -- Which agent owns this task
metadata TEXT NOT NULL DEFAULT '{}', -- Flexible JSON for agent-specific data
-- Timestamps
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
started_at TEXT,
completed_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_dashboard ON agent_tasks(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_status ON agent_tasks(dashboard_id, status);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_session ON agent_tasks(session_id);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_parent ON agent_tasks(parent_id);
-- Agent memory (key-value store with optional categorization)
CREATE TABLE IF NOT EXISTS agent_memory (
id TEXT PRIMARY KEY,
dashboard_id TEXT NOT NULL REFERENCES dashboards(id) ON DELETE CASCADE,
session_id TEXT, -- Optional: scope to specific terminal
-- Memory content
key TEXT NOT NULL, -- e.g., "project_structure", "last_error"
value TEXT NOT NULL, -- JSON blob
memory_type TEXT NOT NULL DEFAULT 'fact'
CHECK (memory_type IN ('fact', 'context', 'preference', 'summary', 'checkpoint')),
-- Categorization
tags TEXT NOT NULL DEFAULT '[]', -- JSON array for filtering
-- Lifecycle
expires_at TEXT, -- Optional TTL
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_agent_memory_dashboard ON agent_memory(dashboard_id);
CREATE INDEX IF NOT EXISTS idx_agent_memory_key ON agent_memory(dashboard_id, key);
CREATE INDEX IF NOT EXISTS idx_agent_memory_session ON agent_memory(session_id);
CREATE INDEX IF NOT EXISTS idx_agent_memory_type ON agent_memory(dashboard_id, memory_type);
CREATE UNIQUE INDEX IF NOT EXISTS idx_agent_memory_unique_key ON agent_memory(dashboard_id, key) WHERE session_id IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS idx_agent_memory_unique_key_session ON agent_memory(dashboard_id, session_id, key) WHERE session_id IS NOT NULL;
-- ============================================
-- User Subscriptions (Stripe billing)
-- ============================================
CREATE TABLE IF NOT EXISTS user_subscriptions (
id TEXT PRIMARY KEY,