@claroshq/claros 0.0.0-stage → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +16 -2
  3. package/assets/dashboard/apple-touch-icon.png +0 -0
  4. package/assets/dashboard/assets/bricolage-grotesque-latin-ext-wght-normal-CcLUaPy7.woff2 +0 -0
  5. package/assets/dashboard/assets/bricolage-grotesque-latin-wght-normal-DLoelf7F.woff2 +0 -0
  6. package/assets/dashboard/assets/bricolage-grotesque-vietnamese-wght-normal-BUzh504Q.woff2 +0 -0
  7. package/assets/dashboard/assets/index-Dvs1k10B.js +23 -0
  8. package/assets/dashboard/assets/index-gR64IOwq.css +1 -0
  9. package/assets/dashboard/assets/instrument-sans-latin-ext-wght-normal-B5bTHO_g.woff2 +0 -0
  10. package/assets/dashboard/assets/instrument-sans-latin-wght-normal-BbzFLZTg.woff2 +0 -0
  11. package/assets/dashboard/assets/jetbrains-mono-cyrillic-ext-wght-normal-EocZY2iu.woff2 +0 -0
  12. package/assets/dashboard/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
  13. package/assets/dashboard/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
  14. package/assets/dashboard/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
  15. package/assets/dashboard/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
  16. package/assets/dashboard/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
  17. package/assets/dashboard/assets/quicksand-latin-ext-wght-normal-CrJ75Ewg.woff2 +0 -0
  18. package/assets/dashboard/assets/quicksand-latin-wght-normal-Buj9m_3d.woff2 +0 -0
  19. package/assets/dashboard/assets/quicksand-vietnamese-wght-normal-ei70gACF.woff2 +0 -0
  20. package/assets/dashboard/claros.js +122 -0
  21. package/assets/dashboard/favicon-16.png +0 -0
  22. package/assets/dashboard/favicon-32.png +0 -0
  23. package/assets/dashboard/favicon.ico +0 -0
  24. package/assets/dashboard/favicon.svg +17 -0
  25. package/assets/dashboard/fonts-license.txt +119 -0
  26. package/assets/dashboard/index.html +34 -0
  27. package/assets/migrations/0000_baseline.sql +343 -0
  28. package/assets/migrations/0001_kb_fts.sql +22 -0
  29. package/assets/migrations/0002_instance_layer.sql +51 -0
  30. package/assets/migrations/0003_suppressions_lower_email.sql +22 -0
  31. package/assets/migrations/0004_claim_tokens.sql +9 -0
  32. package/assets/migrations/0005_topics_transports.sql +36 -0
  33. package/assets/migrations/0006_ops_alerts.sql +28 -0
  34. package/assets/migrations/0007_integration_surface.sql +46 -0
  35. package/assets/migrations/0008_p5_engine.sql +31 -0
  36. package/assets/migrations/0009_p5b_readiness.sql +11 -0
  37. package/assets/migrations/meta/0000_snapshot.json +2489 -0
  38. package/assets/migrations/meta/0002_snapshot.json +2645 -0
  39. package/assets/migrations/meta/0004_snapshot.json +2698 -0
  40. package/assets/migrations/meta/0005_snapshot.json +2959 -0
  41. package/assets/migrations/meta/0006_snapshot.json +3138 -0
  42. package/assets/migrations/meta/0007_snapshot.json +3469 -0
  43. package/assets/migrations/meta/0008_snapshot.json +3709 -0
  44. package/assets/migrations/meta/0009_snapshot.json +3785 -0
  45. package/assets/migrations/meta/_journal.json +76 -0
  46. package/assets/worker/index.js +57863 -0
  47. package/bin/claros.mjs +13 -0
  48. package/dist/apply.d.ts +10 -0
  49. package/dist/apply.d.ts.map +1 -0
  50. package/dist/apply.js +72 -0
  51. package/dist/apply.js.map +1 -0
  52. package/dist/assets.d.ts +15 -0
  53. package/dist/assets.d.ts.map +1 -0
  54. package/dist/assets.js +48 -0
  55. package/dist/assets.js.map +1 -0
  56. package/dist/claim.d.ts +12 -0
  57. package/dist/claim.d.ts.map +1 -0
  58. package/dist/claim.js +26 -0
  59. package/dist/claim.js.map +1 -0
  60. package/dist/cli.d.ts +13 -0
  61. package/dist/cli.d.ts.map +1 -0
  62. package/dist/cli.js +158 -0
  63. package/dist/cli.js.map +1 -0
  64. package/dist/cloudflare-api.d.ts +48 -0
  65. package/dist/cloudflare-api.d.ts.map +1 -0
  66. package/dist/cloudflare-api.js +94 -0
  67. package/dist/cloudflare-api.js.map +1 -0
  68. package/dist/import.d.ts +16 -0
  69. package/dist/import.d.ts.map +1 -0
  70. package/dist/import.js +97 -0
  71. package/dist/import.js.map +1 -0
  72. package/dist/ops.d.ts +84 -0
  73. package/dist/ops.d.ts.map +1 -0
  74. package/dist/ops.js +552 -0
  75. package/dist/ops.js.map +1 -0
  76. package/dist/state.d.ts +53 -0
  77. package/dist/state.d.ts.map +1 -0
  78. package/dist/state.js +87 -0
  79. package/dist/state.js.map +1 -0
  80. package/dist/template.d.ts +77 -0
  81. package/dist/template.d.ts.map +1 -0
  82. package/dist/template.js +127 -0
  83. package/dist/template.js.map +1 -0
  84. package/dist/version.d.ts +3 -0
  85. package/dist/version.d.ts.map +1 -0
  86. package/dist/version.js +36 -0
  87. package/dist/version.js.map +1 -0
  88. package/dist/wrangler.d.ts +116 -0
  89. package/dist/wrangler.d.ts.map +1 -0
  90. package/dist/wrangler.js +210 -0
  91. package/dist/wrangler.js.map +1 -0
  92. package/package.json +53 -4
@@ -0,0 +1,343 @@
1
+ CREATE TABLE `api_keys` (
2
+ `id` text PRIMARY KEY NOT NULL,
3
+ `tenant_id` text NOT NULL,
4
+ `key_hash` text NOT NULL,
5
+ `prefix` text NOT NULL,
6
+ `label` text,
7
+ `kind` text DEFAULT 'secret' NOT NULL,
8
+ `allowed_origins` text,
9
+ `last_used_at` integer,
10
+ `created_at` integer,
11
+ `revoked_at` integer,
12
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
13
+ );
14
+ --> statement-breakpoint
15
+ CREATE UNIQUE INDEX `uq_api_keys_hash_active` ON `api_keys` (`key_hash`) WHERE revoked_at IS NULL;--> statement-breakpoint
16
+ CREATE INDEX `idx_api_keys_tenant` ON `api_keys` (`tenant_id`);--> statement-breakpoint
17
+ CREATE TABLE `contact_conflicts` (
18
+ `id` text PRIMARY KEY NOT NULL,
19
+ `tenant_id` text NOT NULL,
20
+ `contact_id` text NOT NULL,
21
+ `field` text NOT NULL,
22
+ `rejected_value` text NOT NULL,
23
+ `event_id` text NOT NULL,
24
+ `created_at` integer,
25
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
26
+ FOREIGN KEY (`contact_id`) REFERENCES `contacts`(`id`) ON UPDATE no action ON DELETE no action
27
+ );
28
+ --> statement-breakpoint
29
+ CREATE INDEX `idx_contact_conflicts_tenant` ON `contact_conflicts` (`tenant_id`,`created_at`);--> statement-breakpoint
30
+ CREATE INDEX `idx_contact_conflicts_contact` ON `contact_conflicts` (`contact_id`);--> statement-breakpoint
31
+ CREATE TABLE `contacts` (
32
+ `id` text PRIMARY KEY NOT NULL,
33
+ `tenant_id` text NOT NULL,
34
+ `external_id` text NOT NULL,
35
+ `email` text,
36
+ `name` text,
37
+ `company` text,
38
+ `properties` text,
39
+ `lifecycle_state` text NOT NULL,
40
+ `engagement_depth` text,
41
+ `payment_status` text DEFAULT 'free',
42
+ `first_seen_at` integer,
43
+ `last_seen_at` integer,
44
+ `activated_at` integer,
45
+ `created_at` integer,
46
+ `event_count_bucket_current` integer DEFAULT 0,
47
+ `event_count_bucket_prev` integer DEFAULT 0,
48
+ `last_counter_reset_at` integer,
49
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
50
+ );
51
+ --> statement-breakpoint
52
+ CREATE UNIQUE INDEX `uq_contacts_tenant_external_id` ON `contacts` (`tenant_id`,`external_id`);--> statement-breakpoint
53
+ CREATE UNIQUE INDEX `uq_contacts_tenant_email` ON `contacts` (`tenant_id`,`email`) WHERE email IS NOT NULL;--> statement-breakpoint
54
+ CREATE INDEX `idx_contacts_tenant_state` ON `contacts` (`tenant_id`,`lifecycle_state`);--> statement-breakpoint
55
+ CREATE INDEX `idx_contacts_last_seen` ON `contacts` (`tenant_id`,`last_seen_at`);--> statement-breakpoint
56
+ CREATE INDEX `idx_contacts_tenant_created` ON `contacts` (`tenant_id`,`created_at`,`id`);--> statement-breakpoint
57
+ CREATE TABLE `events` (
58
+ `id` text PRIMARY KEY NOT NULL,
59
+ `tenant_id` text NOT NULL,
60
+ `contact_id` text NOT NULL,
61
+ `type` text NOT NULL,
62
+ `event_name` text,
63
+ `properties` text,
64
+ `context` text,
65
+ `message_id` text,
66
+ `timestamp` integer NOT NULL,
67
+ `received_at` integer NOT NULL,
68
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
69
+ FOREIGN KEY (`contact_id`) REFERENCES `contacts`(`id`) ON UPDATE no action ON DELETE no action
70
+ );
71
+ --> statement-breakpoint
72
+ CREATE INDEX `idx_events_contact_time` ON `events` (`contact_id`,`timestamp`);--> statement-breakpoint
73
+ CREATE INDEX `idx_events_tenant_name` ON `events` (`tenant_id`,`event_name`,`timestamp`);--> statement-breakpoint
74
+ CREATE INDEX `idx_events_tenant_time` ON `events` (`tenant_id`,`timestamp`);--> statement-breakpoint
75
+ CREATE INDEX `idx_events_dedup_lookup` ON `events` (`tenant_id`,`message_id`) WHERE message_id IS NOT NULL;--> statement-breakpoint
76
+ CREATE TABLE `flow_memberships` (
77
+ `id` text PRIMARY KEY NOT NULL,
78
+ `tenant_id` text NOT NULL,
79
+ `contact_id` text NOT NULL,
80
+ `flow_id` text NOT NULL,
81
+ `current_step` integer DEFAULT 1 NOT NULL,
82
+ `status` text DEFAULT 'active' NOT NULL,
83
+ `entered_at` integer NOT NULL,
84
+ `completed_at` integer,
85
+ `exited_at` integer,
86
+ `exit_reason` text,
87
+ `condition_error` text,
88
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
89
+ FOREIGN KEY (`contact_id`) REFERENCES `contacts`(`id`) ON UPDATE no action ON DELETE no action,
90
+ FOREIGN KEY (`flow_id`) REFERENCES `flows`(`id`) ON UPDATE no action ON DELETE no action
91
+ );
92
+ --> statement-breakpoint
93
+ CREATE UNIQUE INDEX `uq_flow_membership_active` ON `flow_memberships` (`contact_id`,`flow_id`) WHERE status = 'active';--> statement-breakpoint
94
+ CREATE INDEX `idx_flow_members_contact` ON `flow_memberships` (`contact_id`,`status`);--> statement-breakpoint
95
+ CREATE INDEX `idx_flow_members_flow` ON `flow_memberships` (`flow_id`,`status`,`current_step`);--> statement-breakpoint
96
+ CREATE TABLE `flows` (
97
+ `id` text PRIMARY KEY NOT NULL,
98
+ `tenant_id` text NOT NULL,
99
+ `name` text NOT NULL,
100
+ `description` text,
101
+ `priority` integer DEFAULT 0,
102
+ `trigger_type` text NOT NULL,
103
+ `trigger_config` text NOT NULL,
104
+ `steps` text NOT NULL,
105
+ `source` text DEFAULT 'manual',
106
+ `content_mode` text DEFAULT 'ai_drafted',
107
+ `status` text DEFAULT 'draft',
108
+ `approval_mode` text DEFAULT 'require',
109
+ `flow_class` text DEFAULT 'nurture' NOT NULL,
110
+ `reentry_policy` text DEFAULT 'cooldown' NOT NULL,
111
+ `reentry_cooldown_days` integer DEFAULT 30,
112
+ `prompt_source` text,
113
+ `compiled_plan` text,
114
+ `compiled_at` integer,
115
+ `compile_status` text,
116
+ `compile_error` text,
117
+ `created_at` integer,
118
+ `updated_at` integer,
119
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
120
+ );
121
+ --> statement-breakpoint
122
+ CREATE TABLE `invites` (
123
+ `id` text PRIMARY KEY NOT NULL,
124
+ `tenant_id` text NOT NULL,
125
+ `email` text NOT NULL,
126
+ `role` text DEFAULT 'member' NOT NULL,
127
+ `token_hash` text NOT NULL,
128
+ `invited_by` text NOT NULL,
129
+ `expires_at` integer NOT NULL,
130
+ `accepted_at` integer,
131
+ `created_at` integer,
132
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
133
+ FOREIGN KEY (`invited_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
134
+ );
135
+ --> statement-breakpoint
136
+ CREATE INDEX `idx_invites_token_hash` ON `invites` (`token_hash`);--> statement-breakpoint
137
+ CREATE INDEX `idx_invites_tenant` ON `invites` (`tenant_id`);--> statement-breakpoint
138
+ CREATE TABLE `kb_entries` (
139
+ `id` text PRIMARY KEY NOT NULL,
140
+ `tenant_id` text NOT NULL,
141
+ `title` text NOT NULL,
142
+ `content` text NOT NULL,
143
+ `content_type` text DEFAULT 'markdown',
144
+ `source` text DEFAULT 'manual',
145
+ `source_url` text,
146
+ `tags` text,
147
+ `embedding` text,
148
+ `embedding_status` text,
149
+ `embedding_error` text,
150
+ `is_active` integer DEFAULT true,
151
+ `created_at` integer,
152
+ `updated_at` integer,
153
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
154
+ );
155
+ --> statement-breakpoint
156
+ CREATE INDEX `idx_kb_entries_tenant_created` ON `kb_entries` (`tenant_id`,`created_at`,`id`);--> statement-breakpoint
157
+ CREATE TABLE `lifecycle_messages` (
158
+ `id` text PRIMARY KEY NOT NULL,
159
+ `tenant_id` text NOT NULL,
160
+ `contact_id` text NOT NULL,
161
+ `flow_id` text NOT NULL,
162
+ `membership_id` text NOT NULL,
163
+ `flow_step_order` integer,
164
+ `status` text NOT NULL,
165
+ `feedback` text,
166
+ `subject` text,
167
+ `body_html` text,
168
+ `body_text` text,
169
+ `brain_reasoning` text,
170
+ `brain_action_type` text,
171
+ `scheduled_send_at` integer,
172
+ `approved_at` integer,
173
+ `sent_at` integer,
174
+ `retry_count` integer DEFAULT 0,
175
+ `provider_message_id` text,
176
+ `recipient_address` text,
177
+ `created_at` integer,
178
+ `updated_at` integer,
179
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
180
+ FOREIGN KEY (`contact_id`) REFERENCES `contacts`(`id`) ON UPDATE no action ON DELETE no action,
181
+ FOREIGN KEY (`flow_id`) REFERENCES `flows`(`id`) ON UPDATE no action ON DELETE no action,
182
+ FOREIGN KEY (`membership_id`) REFERENCES `flow_memberships`(`id`) ON UPDATE no action ON DELETE no action
183
+ );
184
+ --> statement-breakpoint
185
+ CREATE INDEX `idx_messages_drain` ON `lifecycle_messages` (`tenant_id`,`status`,`scheduled_send_at`) WHERE status IN ('approved', 'sending');--> statement-breakpoint
186
+ CREATE INDEX `idx_messages_contact` ON `lifecycle_messages` (`contact_id`,`status`,`created_at`);--> statement-breakpoint
187
+ CREATE INDEX `idx_messages_contact_sent` ON `lifecycle_messages` (`contact_id`,`sent_at`) WHERE status IN ('sending', 'sent');--> statement-breakpoint
188
+ CREATE UNIQUE INDEX `uq_messages_membership_step` ON `lifecycle_messages` (`membership_id`,`flow_step_order`);--> statement-breakpoint
189
+ CREATE INDEX `idx_messages_generation` ON `lifecycle_messages` (`tenant_id`,`status`,`created_at`) WHERE status IN ('pending_generation', 'generating');--> statement-breakpoint
190
+ CREATE INDEX `idx_messages_provider_id` ON `lifecycle_messages` (`provider_message_id`) WHERE provider_message_id IS NOT NULL;--> statement-breakpoint
191
+ CREATE INDEX `idx_messages_tenant_sent` ON `lifecycle_messages` (`tenant_id`,`sent_at`) WHERE sent_at IS NOT NULL;--> statement-breakpoint
192
+ CREATE TABLE `lifecycle_transitions` (
193
+ `id` text PRIMARY KEY NOT NULL,
194
+ `tenant_id` text NOT NULL,
195
+ `contact_id` text NOT NULL,
196
+ `from_state` text NOT NULL,
197
+ `to_state` text NOT NULL,
198
+ `trigger_event_id` text,
199
+ `metadata` text,
200
+ `transitioned_at` integer NOT NULL,
201
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
202
+ FOREIGN KEY (`contact_id`) REFERENCES `contacts`(`id`) ON UPDATE no action ON DELETE no action
203
+ );
204
+ --> statement-breakpoint
205
+ CREATE INDEX `idx_transitions_contact` ON `lifecycle_transitions` (`contact_id`,`transitioned_at`);--> statement-breakpoint
206
+ CREATE INDEX `idx_transitions_tenant_time` ON `lifecycle_transitions` (`tenant_id`,`transitioned_at`);--> statement-breakpoint
207
+ CREATE TABLE `llm_configs` (
208
+ `id` text PRIMARY KEY NOT NULL,
209
+ `tenant_id` text NOT NULL,
210
+ `provider` text NOT NULL,
211
+ `config` text NOT NULL,
212
+ `is_active` integer DEFAULT true,
213
+ `created_at` integer,
214
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
215
+ );
216
+ --> statement-breakpoint
217
+ CREATE TABLE `magic_link_tokens` (
218
+ `id` text PRIMARY KEY NOT NULL,
219
+ `tenant_id` text NOT NULL,
220
+ `user_id` text NOT NULL,
221
+ `token_hash` text NOT NULL,
222
+ `expires_at` integer NOT NULL,
223
+ `consumed_at` integer,
224
+ `created_at` integer,
225
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
226
+ FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
227
+ );
228
+ --> statement-breakpoint
229
+ CREATE INDEX `idx_magic_link_tokens_hash` ON `magic_link_tokens` (`token_hash`);--> statement-breakpoint
230
+ CREATE TABLE `message_events` (
231
+ `id` text PRIMARY KEY NOT NULL,
232
+ `tenant_id` text NOT NULL,
233
+ `message_id` text NOT NULL,
234
+ `event_type` text NOT NULL,
235
+ `occurred_at` integer NOT NULL,
236
+ `provider_event_id` text,
237
+ `metadata` text,
238
+ `created_at` integer,
239
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
240
+ FOREIGN KEY (`message_id`) REFERENCES `lifecycle_messages`(`id`) ON UPDATE no action ON DELETE no action
241
+ );
242
+ --> statement-breakpoint
243
+ CREATE INDEX `idx_message_events_message` ON `message_events` (`message_id`,`occurred_at`);--> statement-breakpoint
244
+ CREATE INDEX `idx_message_events_tenant_type_day` ON `message_events` (`tenant_id`,`event_type`,`occurred_at`);--> statement-breakpoint
245
+ CREATE UNIQUE INDEX `uq_message_events_provider_dedup` ON `message_events` (`tenant_id`,`provider_event_id`) WHERE provider_event_id IS NOT NULL;--> statement-breakpoint
246
+ CREATE TABLE `retention_grid_snapshots` (
247
+ `tenant_id` text NOT NULL,
248
+ `snapshot_date` text NOT NULL,
249
+ `tenure_bucket` text NOT NULL,
250
+ `recency_bucket` text NOT NULL,
251
+ `contact_count` integer NOT NULL,
252
+ `paying_count` integer NOT NULL,
253
+ PRIMARY KEY(`tenant_id`, `snapshot_date`, `tenure_bucket`, `recency_bucket`),
254
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
255
+ );
256
+ --> statement-breakpoint
257
+ CREATE TABLE `scan_checkpoints` (
258
+ `scan_phase` text NOT NULL,
259
+ `tenant_id` text NOT NULL,
260
+ `last_id` text NOT NULL,
261
+ `started_at` integer NOT NULL,
262
+ `discard_count` integer DEFAULT 0 NOT NULL,
263
+ PRIMARY KEY(`scan_phase`, `tenant_id`),
264
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
265
+ );
266
+ --> statement-breakpoint
267
+ CREATE TABLE `sessions` (
268
+ `id` text PRIMARY KEY NOT NULL,
269
+ `tenant_id` text NOT NULL,
270
+ `user_id` text NOT NULL,
271
+ `expires_at` integer NOT NULL,
272
+ `created_at` integer,
273
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
274
+ FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
275
+ );
276
+ --> statement-breakpoint
277
+ CREATE TABLE `suppressions` (
278
+ `id` text PRIMARY KEY NOT NULL,
279
+ `tenant_id` text NOT NULL,
280
+ `email` text NOT NULL,
281
+ `reason` text NOT NULL,
282
+ `source` text,
283
+ `created_at` integer,
284
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
285
+ );
286
+ --> statement-breakpoint
287
+ CREATE TABLE `templates` (
288
+ `id` text PRIMARY KEY NOT NULL,
289
+ `tenant_id` text NOT NULL,
290
+ `name` text NOT NULL,
291
+ `slug` text NOT NULL,
292
+ `subject` text NOT NULL,
293
+ `body_html` text NOT NULL,
294
+ `body_text` text,
295
+ `variables` text,
296
+ `category` text,
297
+ `is_active` integer DEFAULT true,
298
+ `created_at` integer,
299
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
300
+ );
301
+ --> statement-breakpoint
302
+ CREATE UNIQUE INDEX `uq_templates_tenant_slug` ON `templates` (`tenant_id`,`slug`);--> statement-breakpoint
303
+ CREATE TABLE `tenants` (
304
+ `id` text PRIMARY KEY NOT NULL,
305
+ `name` text NOT NULL,
306
+ `slug` text NOT NULL,
307
+ `plan` text DEFAULT 'free',
308
+ `settings` text,
309
+ `business_model` text,
310
+ `created_at` integer
311
+ );
312
+ --> statement-breakpoint
313
+ CREATE UNIQUE INDEX `tenants_slug_unique` ON `tenants` (`slug`);--> statement-breakpoint
314
+ CREATE TABLE `transport_configs` (
315
+ `id` text PRIMARY KEY NOT NULL,
316
+ `tenant_id` text NOT NULL,
317
+ `provider` text NOT NULL,
318
+ `config` text NOT NULL,
319
+ `is_active` integer DEFAULT true,
320
+ `dkim_verified` integer DEFAULT false,
321
+ `from_email` text NOT NULL,
322
+ `from_name` text,
323
+ `daily_limit` integer,
324
+ `created_at` integer,
325
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
326
+ );
327
+ --> statement-breakpoint
328
+ CREATE TABLE `users` (
329
+ `id` text PRIMARY KEY NOT NULL,
330
+ `tenant_id` text NOT NULL,
331
+ `email` text NOT NULL,
332
+ `name` text,
333
+ `role` text DEFAULT 'member' NOT NULL,
334
+ `last_login_at` integer,
335
+ `deactivated_at` integer,
336
+ `pending_email` text,
337
+ `pending_email_token_hash` text,
338
+ `pending_email_expires_at` integer,
339
+ `created_at` integer,
340
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
341
+ );
342
+ --> statement-breakpoint
343
+ CREATE UNIQUE INDEX `uq_users_tenant_email` ON `users` (`tenant_id`,`email`);
@@ -0,0 +1,22 @@
1
+ -- Full-text search over the knowledge base (SQLite FTS5), kept in sync with
2
+ -- kb_entries by triggers. The D1 store ranks matches with bm25().
3
+ CREATE VIRTUAL TABLE `kb_fts` USING fts5(
4
+ `title`,
5
+ `content`,
6
+ `tags`,
7
+ content='kb_entries',
8
+ content_rowid='rowid'
9
+ );
10
+ --> statement-breakpoint
11
+ CREATE TRIGGER `kb_entries_fts_insert` AFTER INSERT ON `kb_entries` BEGIN
12
+ INSERT INTO kb_fts(rowid, title, content, tags) VALUES (new.rowid, new.title, new.content, coalesce(new.tags, ''));
13
+ END;
14
+ --> statement-breakpoint
15
+ CREATE TRIGGER `kb_entries_fts_delete` AFTER DELETE ON `kb_entries` BEGIN
16
+ INSERT INTO kb_fts(kb_fts, rowid, title, content, tags) VALUES ('delete', old.rowid, old.title, old.content, coalesce(old.tags, ''));
17
+ END;
18
+ --> statement-breakpoint
19
+ CREATE TRIGGER `kb_entries_fts_update` AFTER UPDATE ON `kb_entries` BEGIN
20
+ INSERT INTO kb_fts(kb_fts, rowid, title, content, tags) VALUES ('delete', old.rowid, old.title, old.content, coalesce(old.tags, ''));
21
+ INSERT INTO kb_fts(rowid, title, content, tags) VALUES (new.rowid, new.title, new.content, coalesce(new.tags, ''));
22
+ END;
@@ -0,0 +1,51 @@
1
+ CREATE TABLE `instance_settings` (
2
+ `key` text PRIMARY KEY NOT NULL,
3
+ `value` text NOT NULL,
4
+ `updated_at` integer
5
+ );
6
+ --> statement-breakpoint
7
+ CREATE TABLE `memberships` (
8
+ `user_id` text NOT NULL,
9
+ `tenant_id` text NOT NULL,
10
+ `role` text NOT NULL,
11
+ `created_at` integer,
12
+ PRIMARY KEY(`user_id`, `tenant_id`),
13
+ FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
14
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
15
+ );
16
+ --> statement-breakpoint
17
+ CREATE INDEX `idx_memberships_tenant` ON `memberships` (`tenant_id`);--> statement-breakpoint
18
+ CREATE TABLE `__new_api_keys` (
19
+ `id` text PRIMARY KEY NOT NULL,
20
+ `tenant_id` text,
21
+ `key_hash` text NOT NULL,
22
+ `prefix` text NOT NULL,
23
+ `label` text,
24
+ `kind` text DEFAULT 'secret' NOT NULL,
25
+ `allowed_origins` text,
26
+ `last_used_at` integer,
27
+ `created_at` integer,
28
+ `revoked_at` integer,
29
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
30
+ );
31
+ --> statement-breakpoint
32
+ INSERT INTO `__new_api_keys`("id", "tenant_id", "key_hash", "prefix", "label", "kind", "allowed_origins", "last_used_at", "created_at", "revoked_at") SELECT "id", "tenant_id", "key_hash", "prefix", "label", "kind", "allowed_origins", "last_used_at", "created_at", "revoked_at" FROM `api_keys`;--> statement-breakpoint
33
+ DROP TABLE `api_keys`;--> statement-breakpoint
34
+ ALTER TABLE `__new_api_keys` RENAME TO `api_keys`;--> statement-breakpoint
35
+ CREATE UNIQUE INDEX `uq_api_keys_hash_active` ON `api_keys` (`key_hash`) WHERE revoked_at IS NULL;--> statement-breakpoint
36
+ CREATE INDEX `idx_api_keys_tenant` ON `api_keys` (`tenant_id`);--> statement-breakpoint
37
+ ALTER TABLE `tenants` ADD `archived_at` integer;--> statement-breakpoint
38
+ ALTER TABLE `tenants` ADD `public_host` text;--> statement-breakpoint
39
+ ALTER TABLE `tenants` ADD `locale` text;--> statement-breakpoint
40
+ ALTER TABLE `tenants` ADD `config_version` integer DEFAULT 0 NOT NULL;--> statement-breakpoint
41
+ CREATE UNIQUE INDEX `uq_tenants_public_host` ON `tenants` (`public_host`) WHERE public_host IS NOT NULL;--> statement-breakpoint
42
+ ALTER TABLE `users` ADD `instance_role` text;--> statement-breakpoint
43
+ -- Backfill (multi-product, expand step): every existing user becomes a member
44
+ -- of their home product with their current role.
45
+ INSERT OR IGNORE INTO `memberships` (`user_id`, `tenant_id`, `role`, `created_at`)
46
+ SELECT `id`, `tenant_id`, `role`, COALESCE(`created_at`, CAST(unixepoch('subsec') * 1000 AS INTEGER)) FROM `users`;--> statement-breakpoint
47
+ -- Backfill: the active owners of the oldest product become instance admins.
48
+ UPDATE `users` SET `instance_role` = 'admin'
49
+ WHERE `role` = 'owner'
50
+ AND `deactivated_at` IS NULL
51
+ AND `tenant_id` = (SELECT `id` FROM `tenants` ORDER BY `created_at` IS NULL, `created_at` ASC, `id` ASC LIMIT 1);
@@ -0,0 +1,22 @@
1
+ -- Case-insensitive suppression uniqueness, the SQLite twin of Postgres
2
+ -- migration 0013: a unique expression index on (tenant_id, lower(email)).
3
+ -- Drizzle-kit cannot express this index in the schema DSL, so it lives here
4
+ -- only (see drizzle/schema-sqlite/suppressions.ts).
5
+ --
6
+ -- Step 1: delete case-variant duplicates, keeping the oldest row per
7
+ -- (tenant_id, lower(email)) (same rule as Postgres 0013; rows without
8
+ -- created_at sort last, ties broken by id).
9
+ DELETE FROM `suppressions`
10
+ WHERE `id` NOT IN (
11
+ SELECT `id` FROM (
12
+ SELECT `id`, ROW_NUMBER() OVER (
13
+ PARTITION BY `tenant_id`, lower(`email`)
14
+ ORDER BY `created_at` IS NULL, `created_at` ASC, `id` ASC
15
+ ) AS `rn`
16
+ FROM `suppressions`
17
+ )
18
+ WHERE `rn` = 1
19
+ );
20
+ --> statement-breakpoint
21
+ -- Step 2: the unique expression index (lower() folds ASCII only on SQLite).
22
+ CREATE UNIQUE INDEX `uq_suppressions_tenant_email_lower` ON `suppressions` (`tenant_id`, lower(`email`));
@@ -0,0 +1,9 @@
1
+ CREATE TABLE `claim_tokens` (
2
+ `id` text PRIMARY KEY NOT NULL,
3
+ `token_hash` text NOT NULL,
4
+ `expires_at` integer NOT NULL,
5
+ `consumed_at` integer,
6
+ `created_at` integer
7
+ );
8
+ --> statement-breakpoint
9
+ CREATE UNIQUE INDEX `uq_claim_tokens_token_hash` ON `claim_tokens` (`token_hash`);
@@ -0,0 +1,36 @@
1
+ CREATE TABLE `contact_topics` (
2
+ `tenant_id` text NOT NULL,
3
+ `contact_id` text NOT NULL,
4
+ `topic_id` text NOT NULL,
5
+ `status` text NOT NULL,
6
+ `updated_at` integer,
7
+ `source` text,
8
+ PRIMARY KEY(`contact_id`, `topic_id`),
9
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
10
+ FOREIGN KEY (`contact_id`) REFERENCES `contacts`(`id`) ON UPDATE no action ON DELETE no action,
11
+ FOREIGN KEY (`topic_id`) REFERENCES `topics`(`id`) ON UPDATE no action ON DELETE no action
12
+ );
13
+ --> statement-breakpoint
14
+ CREATE INDEX `idx_contact_topics_tenant_topic` ON `contact_topics` (`tenant_id`,`topic_id`);--> statement-breakpoint
15
+ CREATE TABLE `topics` (
16
+ `id` text PRIMARY KEY NOT NULL,
17
+ `tenant_id` text NOT NULL,
18
+ `slug` text NOT NULL,
19
+ `name` text NOT NULL,
20
+ `kind` text NOT NULL,
21
+ `description` text,
22
+ `default_subscribed` integer DEFAULT true NOT NULL,
23
+ `created_at` integer,
24
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
25
+ );
26
+ --> statement-breakpoint
27
+ CREATE UNIQUE INDEX `uq_topics_tenant_slug` ON `topics` (`tenant_id`,`slug`);--> statement-breakpoint
28
+ ALTER TABLE `flows` ADD `topic_id` text REFERENCES topics(id);--> statement-breakpoint
29
+ ALTER TABLE `lifecycle_messages` ADD `topic_id` text;--> statement-breakpoint
30
+ ALTER TABLE `lifecycle_messages` ADD `transport_id` text;--> statement-breakpoint
31
+ ALTER TABLE `lifecycle_messages` ADD `status_reason` text;--> statement-breakpoint
32
+ ALTER TABLE `transport_configs` ADD `kind` text;--> statement-breakpoint
33
+ ALTER TABLE `transport_configs` ADD `name` text;--> statement-breakpoint
34
+ ALTER TABLE `transport_configs` ADD `reply_to` text;--> statement-breakpoint
35
+ -- Existing transports keep their provider as their kind.
36
+ UPDATE `transport_configs` SET `kind` = `provider` WHERE `kind` IS NULL;
@@ -0,0 +1,28 @@
1
+ CREATE TABLE `ops_alerts` (
2
+ `fingerprint` text PRIMARY KEY NOT NULL,
3
+ `kind` text NOT NULL,
4
+ `tenant_id` text,
5
+ `severity` text NOT NULL,
6
+ `message` text NOT NULL,
7
+ `first_at` integer NOT NULL,
8
+ `last_at` integer NOT NULL,
9
+ `count` integer DEFAULT 1 NOT NULL,
10
+ `last_notified_at` integer,
11
+ `notified_count` integer DEFAULT 0 NOT NULL,
12
+ `status` text DEFAULT 'open' NOT NULL,
13
+ `muted_until` integer
14
+ );
15
+ --> statement-breakpoint
16
+ CREATE TABLE `ops_events` (
17
+ `id` text PRIMARY KEY NOT NULL,
18
+ `at` integer NOT NULL,
19
+ `tenant_id` text,
20
+ `kind` text NOT NULL,
21
+ `severity` text NOT NULL,
22
+ `fingerprint` text NOT NULL,
23
+ `message` text NOT NULL,
24
+ `context` text
25
+ );
26
+ --> statement-breakpoint
27
+ CREATE INDEX `idx_ops_events_fingerprint_at` ON `ops_events` (`fingerprint`,`at`);--> statement-breakpoint
28
+ CREATE INDEX `idx_ops_events_at` ON `ops_events` (`at`);
@@ -0,0 +1,46 @@
1
+ CREATE TABLE `webhook_endpoints` (
2
+ `id` text PRIMARY KEY NOT NULL,
3
+ `tenant_id` text NOT NULL,
4
+ `slug` text NOT NULL,
5
+ `kind` text DEFAULT 'webhook' NOT NULL,
6
+ `url` text NOT NULL,
7
+ `secret` text,
8
+ `events` text NOT NULL,
9
+ `config` text,
10
+ `enabled` integer DEFAULT true NOT NULL,
11
+ `created_at` integer,
12
+ `updated_at` integer,
13
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
14
+ );
15
+ --> statement-breakpoint
16
+ CREATE UNIQUE INDEX `uq_webhook_endpoints_tenant_slug` ON `webhook_endpoints` (`tenant_id`,`slug`);--> statement-breakpoint
17
+ CREATE TABLE `webhook_outbox` (
18
+ `id` text PRIMARY KEY NOT NULL,
19
+ `tenant_id` text NOT NULL,
20
+ `endpoint_id` text NOT NULL,
21
+ `event` text NOT NULL,
22
+ `payload` text NOT NULL,
23
+ `status` text DEFAULT 'pending' NOT NULL,
24
+ `attempts` integer DEFAULT 0 NOT NULL,
25
+ `next_attempt_at` integer NOT NULL,
26
+ `last_error` text,
27
+ `dedup_key` text,
28
+ `created_at` integer NOT NULL,
29
+ `delivered_at` integer,
30
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
31
+ );
32
+ --> statement-breakpoint
33
+ CREATE INDEX `idx_webhook_outbox_due` ON `webhook_outbox` (`tenant_id`,`next_attempt_at`) WHERE status = 'pending';--> statement-breakpoint
34
+ CREATE INDEX `idx_webhook_outbox_tenant_event` ON `webhook_outbox` (`tenant_id`,`event`,`created_at`);--> statement-breakpoint
35
+ CREATE UNIQUE INDEX `uq_webhook_outbox_dedup` ON `webhook_outbox` (`tenant_id`,`endpoint_id`,`dedup_key`) WHERE dedup_key IS NOT NULL;--> statement-breakpoint
36
+ ALTER TABLE `api_keys` ADD `scopes` text;--> statement-breakpoint
37
+ ALTER TABLE `flows` ADD `slug` text;--> statement-breakpoint
38
+ CREATE UNIQUE INDEX `uq_flows_tenant_slug` ON `flows` (`tenant_id`,`slug`) WHERE slug IS NOT NULL;--> statement-breakpoint
39
+ ALTER TABLE `lifecycle_messages` ADD `approved_by` text;--> statement-breakpoint
40
+ ALTER TABLE `lifecycle_messages` ADD `rejected_by` text;--> statement-breakpoint
41
+ ALTER TABLE `lifecycle_messages` ADD `edited_by` text;--> statement-breakpoint
42
+ ALTER TABLE `lifecycle_messages` ADD `edited_at` integer;--> statement-breakpoint
43
+ ALTER TABLE `lifecycle_messages` ADD `original_subject` text;--> statement-breakpoint
44
+ ALTER TABLE `lifecycle_messages` ADD `original_body_html` text;--> statement-breakpoint
45
+ ALTER TABLE `lifecycle_messages` ADD `original_body_text` text;--> statement-breakpoint
46
+ ALTER TABLE `lifecycle_messages` ADD `forced_action_type` text;
@@ -0,0 +1,31 @@
1
+ CREATE TABLE `contact_dates` (
2
+ `tenant_id` text NOT NULL,
3
+ `contact_id` text NOT NULL,
4
+ `property` text NOT NULL,
5
+ `value` integer NOT NULL,
6
+ `updated_at` integer,
7
+ PRIMARY KEY(`tenant_id`, `contact_id`, `property`),
8
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action,
9
+ FOREIGN KEY (`contact_id`) REFERENCES `contacts`(`id`) ON UPDATE no action ON DELETE no action
10
+ );
11
+ --> statement-breakpoint
12
+ CREATE INDEX `idx_contact_dates_scan` ON `contact_dates` (`tenant_id`,`property`,`value`);--> statement-breakpoint
13
+ ALTER TABLE `flow_memberships` ADD `anchor_at` integer;--> statement-breakpoint
14
+ CREATE UNIQUE INDEX `uq_flow_membership_anchor` ON `flow_memberships` (`tenant_id`,`flow_id`,`contact_id`,`anchor_at`) WHERE anchor_at IS NOT NULL;--> statement-breakpoint
15
+ ALTER TABLE `kb_entries` ADD `category` text;--> statement-breakpoint
16
+ ALTER TABLE `kb_entries` ADD `external_key` text;--> statement-breakpoint
17
+ ALTER TABLE `kb_entries` ADD `data` text;--> statement-breakpoint
18
+ ALTER TABLE `kb_entries` ADD `pinned` integer DEFAULT false NOT NULL;--> statement-breakpoint
19
+ ALTER TABLE `kb_entries` ADD `imported_at` integer;--> statement-breakpoint
20
+ CREATE UNIQUE INDEX `uq_kb_entries_external_key` ON `kb_entries` (`tenant_id`,`external_key`) WHERE external_key IS NOT NULL;--> statement-breakpoint
21
+ CREATE INDEX `idx_kb_entries_category` ON `kb_entries` (`tenant_id`,`category`);--> statement-breakpoint
22
+ ALTER TABLE `lifecycle_messages` ADD `kb_entry_id` text;--> statement-breakpoint
23
+ ALTER TABLE `lifecycle_messages` ADD `imported` integer DEFAULT false NOT NULL;--> statement-breakpoint
24
+ ALTER TABLE `lifecycle_messages` ADD `import_key` text;--> statement-breakpoint
25
+ ALTER TABLE `lifecycle_messages` ADD `expires_at` integer;--> statement-breakpoint
26
+ ALTER TABLE `lifecycle_messages` ADD `context_snapshot` text;--> statement-breakpoint
27
+ ALTER TABLE `lifecycle_messages` ADD `choose_decision` text;--> statement-breakpoint
28
+ ALTER TABLE `lifecycle_messages` ADD `opening_line` text;--> statement-breakpoint
29
+ CREATE UNIQUE INDEX `uq_messages_import_key` ON `lifecycle_messages` (`tenant_id`,`import_key`) WHERE import_key IS NOT NULL;--> statement-breakpoint
30
+ CREATE INDEX `idx_messages_kb_entry` ON `lifecycle_messages` (`tenant_id`,`contact_id`,`kb_entry_id`) WHERE kb_entry_id IS NOT NULL;--> statement-breakpoint
31
+ CREATE INDEX `idx_messages_expires` ON `lifecycle_messages` (`tenant_id`,`expires_at`) WHERE expires_at IS NOT NULL;
@@ -0,0 +1,11 @@
1
+ CREATE TABLE `tenant_daily_usage` (
2
+ `tenant_id` text NOT NULL,
3
+ `day` text NOT NULL,
4
+ `llm_tokens` integer DEFAULT 0 NOT NULL,
5
+ `digest_sent_at` integer,
6
+ PRIMARY KEY(`tenant_id`, `day`),
7
+ FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON UPDATE no action ON DELETE no action
8
+ );
9
+ --> statement-breakpoint
10
+ ALTER TABLE `flow_memberships` ADD `trigger_properties` text;--> statement-breakpoint
11
+ ALTER TABLE `topics` ADD `mirror_only` integer DEFAULT false NOT NULL;