spine-framework 0.3.124 → 0.3.126

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 (319) hide show
  1. package/.framework/cli/commands/agents.ts +35 -37
  2. package/.framework/cli/commands/auth.ts +35 -34
  3. package/.framework/cli/commands/create-app.ts +104 -40
  4. package/.framework/cli/commands/dev.ts +104 -69
  5. package/.framework/cli/commands/doctor.ts +179 -69
  6. package/.framework/cli/commands/generate.ts +83 -143
  7. package/.framework/cli/commands/init.ts +72 -34
  8. package/.framework/cli/commands/install-app.ts +260 -125
  9. package/.framework/cli/commands/items.ts +33 -44
  10. package/.framework/cli/commands/migrate.ts +122 -40
  11. package/.framework/cli/commands/migrations.ts +33 -40
  12. package/.framework/cli/commands/pipelines.ts +31 -39
  13. package/.framework/cli/commands/status.ts +47 -23
  14. package/.framework/cli/commands/system.ts +47 -37
  15. package/.framework/cli/commands/test.ts +112 -68
  16. package/.framework/cli/commands/validate-app.ts +215 -121
  17. package/.framework/cli/context.ts +112 -130
  18. package/.framework/cli/env-loader.ts +47 -18
  19. package/.framework/cli/index.ts +81 -42
  20. package/.framework/docs/runtime-types.md +1024 -0
  21. package/.framework/functions/_shared/agent-runner.ts +1296 -961
  22. package/.framework/functions/_shared/app-manifest.ts +82 -68
  23. package/.framework/functions/_shared/audit.ts +135 -83
  24. package/.framework/functions/_shared/{cil-state.ts → cil.ts} +158 -56
  25. package/.framework/functions/_shared/db.ts +80 -126
  26. package/.framework/functions/_shared/hash-api-key.ts +63 -0
  27. package/.framework/functions/_shared/index.ts +224 -335
  28. package/.framework/functions/_shared/kb.ts +319 -0
  29. package/.framework/functions/_shared/llm.ts +92 -0
  30. package/.framework/functions/_shared/logging.ts +288 -0
  31. package/.framework/functions/_shared/middleware.ts +329 -365
  32. package/.framework/functions/_shared/outcomes.ts +36 -0
  33. package/.framework/functions/_shared/permissions.ts +359 -643
  34. package/.framework/functions/_shared/pipeline-runner.ts +243 -316
  35. package/.framework/functions/_shared/principal.ts +376 -468
  36. package/.framework/functions/_shared/resolve-credentials.ts +102 -0
  37. package/.framework/functions/_shared/resolve-ids.ts +188 -16
  38. package/.framework/functions/_shared/retry-deliveries.ts +104 -0
  39. package/.framework/functions/_shared/schema-utils.ts +611 -373
  40. package/.framework/functions/_shared/testing.ts +207 -137
  41. package/.framework/functions/_shared/tracing.ts +276 -0
  42. package/.framework/functions/_shared/trigger-engine.ts +159 -211
  43. package/.framework/functions/_shared/webhook-registration.ts +95 -81
  44. package/.framework/functions/_shared/webhook-registry.ts +153 -75
  45. package/.framework/functions/account-nodes.ts +31 -53
  46. package/.framework/functions/adapters/generic-webhook-inbound.ts +56 -0
  47. package/.framework/functions/adapters/generic-webhook-outbound.ts +43 -0
  48. package/.framework/functions/adapters/openai-outbound.ts +40 -0
  49. package/.framework/functions/admin-data.ts +685 -537
  50. package/.framework/functions/admin-funnel.ts +31 -20
  51. package/.framework/functions/admin-install-funnel.ts +47 -28
  52. package/.framework/functions/admin-signals.ts +47 -29
  53. package/.framework/functions/ai-agents.ts +122 -147
  54. package/.framework/functions/ai-interactions.ts +180 -90
  55. package/.framework/functions/api-keys.ts +250 -192
  56. package/.framework/functions/apps.ts +203 -197
  57. package/.framework/functions/auth.ts +98 -91
  58. package/.framework/functions/cil-agent-ingest.ts +773 -0
  59. package/.framework/functions/cil-agent.ts +592 -0
  60. package/.framework/functions/cil-callmap.ts +1227 -0
  61. package/.framework/functions/cil-config.ts +583 -0
  62. package/.framework/functions/cil-context.ts +445 -0
  63. package/.framework/functions/cil-doc.ts +1752 -0
  64. package/.framework/functions/cil-embeddings.ts +1202 -0
  65. package/.framework/functions/cil-graph.ts +585 -0
  66. package/.framework/functions/cil-ingest.ts +2416 -0
  67. package/.framework/functions/cil-pipeline.ts +318 -0
  68. package/.framework/functions/cil-project.ts +499 -0
  69. package/.framework/functions/cil-validate.ts +469 -0
  70. package/.framework/functions/debug-auth.ts +73 -53
  71. package/.framework/functions/deliveries.ts +117 -0
  72. package/.framework/functions/embeddings.ts +528 -516
  73. package/.framework/functions/eval-harness.ts +158 -106
  74. package/.framework/functions/integration-outbound.ts +311 -0
  75. package/.framework/functions/integration-routes.ts +240 -466
  76. package/.framework/functions/integrations.ts +132 -140
  77. package/.framework/functions/invites.ts +401 -201
  78. package/.framework/functions/item-progress.ts +113 -53
  79. package/.framework/functions/kb.ts +28 -0
  80. package/.framework/functions/logs.ts +239 -222
  81. package/.framework/functions/observability.ts +308 -259
  82. package/.framework/functions/pipeline-executions.ts +179 -235
  83. package/.framework/functions/pipelines.ts +163 -205
  84. package/.framework/functions/prompt-configs.ts +109 -156
  85. package/.framework/functions/roles.ts +97 -147
  86. package/.framework/functions/system-cron.ts +132 -218
  87. package/.framework/functions/system.ts +112 -52
  88. package/.framework/functions/tests.ts +32 -28
  89. package/.framework/functions/timers.ts +162 -210
  90. package/.framework/functions/triggers.ts +282 -346
  91. package/.framework/functions/types.ts +283 -400
  92. package/.framework/migrations/001_core_schema.sql +630 -0
  93. package/.framework/migrations/002_core_rls_and_functions.sql +532 -0
  94. package/.framework/migrations/{003_system_data.sql → 003_core_system_data.sql} +26 -153
  95. package/.framework/migrations/004_core_rpc_and_broker.sql +421 -0
  96. package/.framework/migrations/005_core_observability.sql +598 -0
  97. package/.framework/migrations/006_security_hardening_rls.sql +97 -0
  98. package/.framework/migrations/007_kb_article_list.sql +237 -0
  99. package/.framework/migrations/008_items_target.sql +8 -0
  100. package/.framework/migrations/009_roles_slug_unique.sql +11 -0
  101. package/.framework/public/spine.config.json +91 -0
  102. package/.framework/scripts/app-install-cli.ts +112 -98
  103. package/.framework/src/App.tsx +67 -20
  104. package/.framework/src/apps/admin/index.tsx +97 -27
  105. package/.framework/src/components/AppWrapper.tsx +50 -11
  106. package/.framework/src/components/CustomAppLoader.tsx +131 -22
  107. package/.framework/src/components/admin/AdminListPage.tsx +75 -8
  108. package/.framework/src/components/admin/AdminSidebar.tsx +148 -36
  109. package/.framework/src/components/admin/AdminStatsCard.tsx +48 -29
  110. package/.framework/src/components/admin/PageInstructions.tsx +49 -0
  111. package/.framework/src/components/admin/SortableTableHeader.tsx +49 -2
  112. package/.framework/src/components/app-shell/GenericAppShell.tsx +69 -11
  113. package/.framework/src/components/app-shell/GenericDetailPage.tsx +55 -10
  114. package/.framework/src/components/app-shell/GenericListPage.tsx +76 -9
  115. package/.framework/src/components/app-sidebar.tsx +50 -3
  116. package/.framework/src/components/auth/ProtectedRoute.tsx +53 -45
  117. package/.framework/src/components/kb/KBArticleEditor.tsx +409 -0
  118. package/.framework/src/components/kb/KBArticleList.tsx +557 -0
  119. package/.framework/src/components/layout/AppShell.tsx +54 -16
  120. package/.framework/src/components/layout/Header.tsx +50 -38
  121. package/.framework/src/components/layout/Layout.tsx +48 -32
  122. package/.framework/src/components/layout/Sidebar.tsx +167 -16
  123. package/.framework/src/components/runtime/DataDetailHeader.tsx +33 -1
  124. package/.framework/src/components/runtime/DataDetailPage.tsx +289 -4
  125. package/.framework/src/components/runtime/DataFilters.tsx +78 -1
  126. package/.framework/src/components/runtime/DataHeader.tsx +32 -29
  127. package/.framework/src/components/runtime/DataListPage.tsx +161 -2
  128. package/.framework/src/components/runtime/DataStats.tsx +48 -1
  129. package/.framework/src/components/runtime/DataTable.tsx +153 -1
  130. package/.framework/src/components/runtime/SchemaDetailForm.tsx +77 -46
  131. package/.framework/src/components/runtime/index.ts +16 -9
  132. package/.framework/src/components/search-form.tsx +33 -1
  133. package/.framework/src/components/shared/AgentView.tsx +48 -51
  134. package/.framework/src/components/shared/FieldRenderer.tsx +84 -67
  135. package/.framework/src/components/shared/InviteManager.tsx +113 -52
  136. package/.framework/src/components/shared/MemberList.tsx +182 -0
  137. package/.framework/src/components/shared/SchemaFields.tsx +96 -78
  138. package/.framework/src/components/ui/DataTable.tsx +86 -56
  139. package/.framework/src/components/ui/Form.tsx +188 -109
  140. package/.framework/src/components/ui/ItemCard.tsx +86 -58
  141. package/.framework/src/components/ui/ItemListView.tsx +88 -61
  142. package/.framework/src/components/ui/LoadingSpinner.tsx +52 -27
  143. package/.framework/src/components/ui/Modal.tsx +81 -4
  144. package/.framework/src/components/ui/RichTextEditor.tsx +101 -9
  145. package/.framework/src/components/ui/accordion.tsx +97 -5
  146. package/.framework/src/components/ui/alert-dialog.tsx +225 -13
  147. package/.framework/src/components/ui/alert.tsx +113 -6
  148. package/.framework/src/components/ui/aspect-ratio.tsx +49 -2
  149. package/.framework/src/components/ui/avatar.tsx +129 -7
  150. package/.framework/src/components/ui/badge.tsx +65 -3
  151. package/.framework/src/components/ui/breadcrumb.tsx +145 -8
  152. package/.framework/src/components/ui/button-group.tsx +81 -4
  153. package/.framework/src/components/ui/button.tsx +49 -2
  154. package/.framework/src/components/ui/calendar.tsx +65 -3
  155. package/.framework/src/components/ui/card.tsx +145 -8
  156. package/.framework/src/components/ui/carousel.tsx +129 -7
  157. package/.framework/src/components/ui/chart.tsx +161 -10
  158. package/.framework/src/components/ui/checkbox.tsx +49 -2
  159. package/.framework/src/components/ui/collapsible.tsx +81 -5
  160. package/.framework/src/components/ui/combobox.tsx +305 -20
  161. package/.framework/src/components/ui/command.tsx +177 -11
  162. package/.framework/src/components/ui/context-menu.tsx +273 -17
  163. package/.framework/src/components/ui/dialog.tsx +209 -27
  164. package/.framework/src/components/ui/direction.tsx +64 -4
  165. package/.framework/src/components/ui/drawer.tsx +193 -12
  166. package/.framework/src/components/ui/dropdown-menu.tsx +273 -18
  167. package/.framework/src/components/ui/empty.tsx +145 -9
  168. package/.framework/src/components/ui/field.tsx +209 -14
  169. package/.framework/src/components/ui/hover-card.tsx +81 -4
  170. package/.framework/src/components/ui/input-group.tsx +162 -10
  171. package/.framework/src/components/ui/input-otp.tsx +98 -5
  172. package/.framework/src/components/ui/input.tsx +49 -2
  173. package/.framework/src/components/ui/item.tsx +225 -13
  174. package/.framework/src/components/ui/kbd.tsx +65 -3
  175. package/.framework/src/components/ui/label.tsx +49 -2
  176. package/.framework/src/components/ui/menubar.tsx +289 -17
  177. package/.framework/src/components/ui/native-select.tsx +97 -5
  178. package/.framework/src/components/ui/navigation-menu.tsx +177 -10
  179. package/.framework/src/components/ui/pagination.tsx +161 -9
  180. package/.framework/src/components/ui/popover.tsx +145 -8
  181. package/.framework/src/components/ui/progress.tsx +49 -2
  182. package/.framework/src/components/ui/radio-group.tsx +65 -3
  183. package/.framework/src/components/ui/resizable.tsx +82 -4
  184. package/.framework/src/components/ui/scroll-area.tsx +65 -3
  185. package/.framework/src/components/ui/select.tsx +193 -11
  186. package/.framework/src/components/ui/separator.tsx +49 -2
  187. package/.framework/src/components/ui/sheet.tsx +193 -11
  188. package/.framework/src/components/ui/sidebar.tsx +481 -38
  189. package/.framework/src/components/ui/skeleton.tsx +49 -2
  190. package/.framework/src/components/ui/slider.tsx +49 -2
  191. package/.framework/src/components/ui/sonner.tsx +49 -2
  192. package/.framework/src/components/ui/spinner.tsx +49 -2
  193. package/.framework/src/components/ui/switch.tsx +49 -2
  194. package/.framework/src/components/ui/table-primitives.tsx +160 -33
  195. package/.framework/src/components/ui/table.tsx +161 -9
  196. package/.framework/src/components/ui/tabs.tsx +113 -6
  197. package/.framework/src/components/ui/textarea.tsx +49 -2
  198. package/.framework/src/components/ui/toggle-group.tsx +81 -4
  199. package/.framework/src/components/ui/toggle.tsx +65 -3
  200. package/.framework/src/components/ui/tooltip.tsx +97 -5
  201. package/.framework/src/contexts/AppContext.tsx +129 -45
  202. package/.framework/src/contexts/AuthContext.tsx +187 -144
  203. package/.framework/src/hooks/use-mobile.ts +48 -2
  204. package/.framework/src/hooks/useApi.ts +208 -159
  205. package/.framework/src/hooks/useAppPath.ts +32 -23
  206. package/.framework/src/hooks/useApps.ts +51 -20
  207. package/.framework/src/hooks/useEntityList.ts +32 -78
  208. package/.framework/src/hooks/useEntityRecord.ts +35 -96
  209. package/.framework/src/hooks/useForm.ts +32 -92
  210. package/.framework/src/hooks/useListSchema.ts +49 -78
  211. package/.framework/src/hooks/useSchemaRecord.ts +114 -105
  212. package/.framework/src/lib/api.ts +195 -116
  213. package/.framework/src/lib/supabase.ts +98 -64
  214. package/.framework/src/lib/utils.ts +287 -151
  215. package/.framework/src/main.tsx +49 -3
  216. package/.framework/src/pages/DashboardPage.tsx +52 -29
  217. package/.framework/src/pages/NotFoundPage.tsx +32 -10
  218. package/.framework/src/pages/SetupPage.tsx +32 -12
  219. package/.framework/src/pages/admin/AIAgentDetailPage.tsx +119 -10
  220. package/.framework/src/pages/admin/AIAgentsPage.tsx +48 -19
  221. package/.framework/src/pages/admin/APIKeyDetailPage.tsx +187 -17
  222. package/.framework/src/pages/admin/APIKeysPage.tsx +98 -22
  223. package/.framework/src/pages/admin/AlertsConfigPage.tsx +113 -26
  224. package/.framework/src/pages/admin/AppDetailPage.tsx +125 -8
  225. package/.framework/src/pages/admin/AppsPage.tsx +49 -2
  226. package/.framework/src/pages/admin/DeliveriesPage.tsx +196 -0
  227. package/.framework/src/pages/admin/DesignedPage.tsx +32 -18
  228. package/.framework/src/pages/admin/EmbeddingDetailPage.tsx +215 -484
  229. package/.framework/src/pages/admin/EmbeddingsPage.tsx +116 -319
  230. package/.framework/src/pages/admin/ExtendedShadcnTestPage.tsx +34 -9
  231. package/.framework/src/pages/admin/IncrementalShadcnTestPage.tsx +34 -9
  232. package/.framework/src/pages/admin/IntegratedDashboard.tsx +32 -16
  233. package/.framework/src/pages/admin/IntegrationDetailPage.tsx +183 -26
  234. package/.framework/src/pages/admin/IntegrationsPage.tsx +48 -19
  235. package/.framework/src/pages/admin/InvitesPage.tsx +48 -14
  236. package/.framework/src/pages/admin/LogsPage.tsx +422 -75
  237. package/.framework/src/pages/admin/MinimalShadcnTestPage.tsx +34 -9
  238. package/.framework/src/pages/admin/ObservabilityDashboard.tsx +113 -24
  239. package/.framework/src/pages/admin/PipelineDetailPage.tsx +206 -34
  240. package/.framework/src/pages/admin/PipelineExecutionsPage.tsx +50 -17
  241. package/.framework/src/pages/admin/PipelinesPage.tsx +160 -21
  242. package/.framework/src/pages/admin/PromptConfigDetailPage.tsx +378 -88
  243. package/.framework/src/pages/admin/PromptConfigsPage.tsx +125 -16
  244. package/.framework/src/pages/admin/ProperlyDesignedPage.tsx +49 -10
  245. package/.framework/src/pages/admin/RoleDetailPage.tsx +130 -10
  246. package/.framework/src/pages/admin/RolesPage.tsx +171 -18
  247. package/.framework/src/pages/admin/SelectTestPage.tsx +34 -9
  248. package/.framework/src/pages/admin/ShadcnTestPage.tsx +49 -11
  249. package/.framework/src/pages/admin/SimpleDashboard.tsx +49 -10
  250. package/.framework/src/pages/admin/TestRunDetailPage.tsx +49 -4
  251. package/.framework/src/pages/admin/TestingDashboard.tsx +98 -28
  252. package/.framework/src/pages/admin/TimerDetailPage.tsx +143 -33
  253. package/.framework/src/pages/admin/TimersPage.tsx +48 -20
  254. package/.framework/src/pages/admin/TriggerDetailPage.tsx +118 -30
  255. package/.framework/src/pages/admin/TriggersPage.tsx +64 -19
  256. package/.framework/src/pages/admin/TypeDetailPage.tsx +138 -15
  257. package/.framework/src/pages/admin/TypesPage.tsx +48 -20
  258. package/.framework/src/pages/admin/cil/CILAgentPage.tsx +465 -0
  259. package/.framework/src/pages/admin/cil/CILChunkDetailPage.tsx +787 -0
  260. package/.framework/src/pages/admin/cil/CILDashboardPage.tsx +699 -0
  261. package/.framework/src/pages/admin/cil/CILFileListPage.tsx +647 -0
  262. package/.framework/src/pages/admin/cil/CILGraphPage.tsx +616 -0
  263. package/.framework/src/pages/admin/cil/CILIngestionPage.tsx +1402 -0
  264. package/.framework/src/pages/admin/cil/CILProjectsPage.tsx +614 -0
  265. package/.framework/src/pages/admin/cil/CILSearchPage.tsx +571 -0
  266. package/.framework/src/pages/admin/cil/FlexiblePipelinePage.tsx +731 -0
  267. package/.framework/src/pages/admin/kb/KBAdminPage.tsx +64 -0
  268. package/.framework/src/pages/auth/AuthConfirmPage.tsx +52 -51
  269. package/.framework/src/pages/auth/ForgotPasswordPage.tsx +31 -22
  270. package/.framework/src/pages/auth/LoginPage.tsx +50 -23
  271. package/.framework/src/pages/auth/PendingApprovalPage.tsx +31 -15
  272. package/.framework/src/pages/auth/RegisterPage.tsx +281 -317
  273. package/.framework/src/pages/spine-framework/APIPage.tsx +33 -0
  274. package/.framework/src/pages/spine-framework/CLIPage.tsx +33 -0
  275. package/.framework/src/types/auth.ts +128 -71
  276. package/.framework/src/types/types.ts +373 -157
  277. package/custom/.devin/skills/spine-workbench-discovery/SKILL.md +39 -0
  278. package/custom/.devin/skills/spine-workbench-idea/SKILL.md +36 -0
  279. package/custom/.devin/skills/spine-workbench-implement/SKILL.md +46 -0
  280. package/custom/.devin/skills/spine-workbench-initiative/SKILL.md +28 -0
  281. package/custom/.devin/skills/spine-workbench-plan/SKILL.md +38 -0
  282. package/docs/README.md +9 -130
  283. package/netlify.toml +5 -2
  284. package/package.json +13 -25
  285. package/scripts/kb-ingest-docs.ts +203 -0
  286. package/spine.config.json +65 -4
  287. package/.framework/migrations/001_schema.sql +0 -425
  288. package/.framework/migrations/002_rls_and_functions.sql +0 -763
  289. package/.framework/migrations/004_enhancements.sql +0 -43
  290. package/.framework/migrations/005_auth_user_trigger.sql +0 -105
  291. package/.framework/migrations/006_fix_current_actor_id.sql +0 -21
  292. package/.framework/migrations/016_invites.sql +0 -27
  293. package/.framework/migrations/017_fix_get_account_apps.sql +0 -78
  294. package/.framework/migrations/018_apps_required_roles.sql +0 -17
  295. package/.framework/migrations/019_triggers_unique_constraint.sql +0 -13
  296. package/.framework/migrations/020_get_account_apps_role_filter.sql +0 -117
  297. package/.framework/migrations/021_app_domains.sql +0 -112
  298. package/.framework/migrations/022_route_prefix_domain_unique.sql +0 -21
  299. package/.framework/migrations/023_match_embeddings_rpc.sql +0 -78
  300. package/.framework/migrations/024_messages_account_id.sql +0 -9
  301. package/.framework/migrations/025_retype_threads_messages.sql +0 -240
  302. package/.framework/migrations/026_fix_rls_policies.sql +0 -47
  303. package/.framework/migrations/027_embedding_lifecycle.sql +0 -176
  304. package/.framework/migrations/028_fix_message_data_permissions.sql +0 -49
  305. package/.framework/migrations/029_tighten_support_triage_prompt.sql +0 -41
  306. package/.framework/migrations/030_messages_updated_by.sql +0 -6
  307. package/.framework/migrations/031_seed_upsert_constraints.sql +0 -35
  308. package/docs/APPS.md +0 -232
  309. package/docs/ARCHITECTURE.md +0 -246
  310. package/docs/INSTALL.md +0 -155
  311. package/docs/design-schema-spec.md +0 -1837
  312. package/docs/dev-guides/01-create-an-app.md +0 -183
  313. package/docs/dev-guides/02-create-a-function.md +0 -178
  314. package/docs/dev-guides/03-create-a-component.md +0 -178
  315. package/docs/dev-guides/04-webhook-handlers.md +0 -129
  316. package/docs/dev-guides/05-testing.md +0 -168
  317. package/docs/dev-guides/README.md +0 -40
  318. package/docs/npm-packages-plan.md +0 -288
  319. package/docs/spine-core--api-reference.md +0 -822
@@ -1,763 +0,0 @@
1
- -- Migration 002: Functions, RLS Policies, Indexes, and Foreign Keys
2
- -- Consolidated from migrations 003, 004, 005, 006, 012, 014
3
-
4
- -- ============================================
5
- -- FUNCTIONS
6
- -- ============================================
7
-
8
- -- Account hierarchy traversal
9
- CREATE OR REPLACE FUNCTION public.get_accessible_accounts(actor_id uuid)
10
- RETURNS TABLE (account_id uuid) AS $$
11
- BEGIN
12
- IF EXISTS (
13
- SELECT 1 FROM public.people p
14
- JOIN public.roles r ON p.role_id = r.id
15
- WHERE p.id = actor_id AND r.slug = 'system_admin'
16
- ) THEN
17
- RETURN QUERY SELECT a.id FROM public.accounts a;
18
- RETURN;
19
- END IF;
20
- RETURN QUERY
21
- WITH RECURSIVE account_tree AS (
22
- SELECT p.account_id as id FROM public.people p WHERE p.id = actor_id
23
- UNION ALL
24
- SELECT ap.descendant_id
25
- FROM public.account_paths ap
26
- JOIN account_tree at ON ap.ancestor_id = at.id
27
- )
28
- SELECT id FROM account_tree;
29
- END;
30
- $$ LANGUAGE plpgsql SECURITY DEFINER;
31
-
32
- -- Get current actor ID from auth
33
- CREATE OR REPLACE FUNCTION public.current_actor_id()
34
- RETURNS uuid AS $$
35
- DECLARE
36
- v_auth_id UUID;
37
- v_person_id UUID;
38
- BEGIN
39
- v_auth_id := auth.uid();
40
-
41
- -- Look up internal person ID from auth_uid
42
- SELECT p.id INTO v_person_id
43
- FROM public.people p
44
- WHERE p.auth_uid = v_auth_id AND p.is_active = true
45
- LIMIT 1;
46
-
47
- RETURN v_person_id;
48
- END;
49
- $$ LANGUAGE plpgsql SECURITY DEFINER STABLE;
50
-
51
- -- Check if person is system admin
52
- CREATE OR REPLACE FUNCTION public.person_is_system_admin(person_uuid uuid)
53
- RETURNS boolean AS $$
54
- BEGIN
55
- RETURN EXISTS (
56
- SELECT 1 FROM public.people p
57
- JOIN public.roles r ON p.role_id = r.id
58
- WHERE p.id = person_uuid AND r.slug = 'system_admin'
59
- );
60
- END;
61
- $$ LANGUAGE plpgsql STABLE;
62
-
63
- -- Update account paths on account changes
64
- CREATE OR REPLACE FUNCTION public.update_account_paths()
65
- RETURNS TRIGGER AS $$
66
- BEGIN
67
- IF TG_OP = 'INSERT' THEN
68
- IF NEW.parent_id IS NOT NULL THEN
69
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth)
70
- SELECT ancestor_id, NEW.id, depth + 1 FROM public.account_paths WHERE descendant_id = NEW.parent_id;
71
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth) VALUES (NEW.parent_id, NEW.id, 1);
72
- END IF;
73
- RETURN NEW;
74
- END IF;
75
- IF TG_OP = 'UPDATE' AND OLD.parent_id IS DISTINCT FROM NEW.parent_id THEN
76
- DELETE FROM public.account_paths WHERE descendant_id = NEW.id;
77
- IF NEW.parent_id IS NOT NULL THEN
78
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth)
79
- SELECT ancestor_id, NEW.id, depth + 1 FROM public.account_paths WHERE descendant_id = NEW.parent_id;
80
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth) VALUES (NEW.parent_id, NEW.id, 1);
81
- END IF;
82
- RETURN NEW;
83
- END IF;
84
- IF TG_OP = 'DELETE' THEN
85
- DELETE FROM public.account_paths WHERE descendant_id = OLD.id;
86
- RETURN OLD;
87
- END IF;
88
- RETURN NULL;
89
- END;
90
- $$ LANGUAGE plpgsql;
91
-
92
- -- Trigger for account paths
93
- DROP TRIGGER IF EXISTS account_paths_trigger ON public.accounts;
94
- CREATE TRIGGER account_paths_trigger
95
- AFTER INSERT OR UPDATE OR DELETE ON public.accounts
96
- FOR EACH ROW EXECUTE FUNCTION public.update_account_paths();
97
-
98
- -- set_updated_at trigger function (from 014_webhook_registry.sql)
99
- CREATE OR REPLACE FUNCTION public.set_updated_at()
100
- RETURNS TRIGGER AS $$
101
- BEGIN
102
- NEW.updated_at = now();
103
- RETURN NEW;
104
- END;
105
- $$ LANGUAGE plpgsql;
106
-
107
- -- ============================================
108
- -- FOREIGN KEYS
109
- -- ============================================
110
-
111
- -- accounts FKs
112
- ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_parent
113
- FOREIGN KEY (parent_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
114
- ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_type
115
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
116
- ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_app
117
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
118
- ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_created_by
119
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
120
- ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_updated_by
121
- FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
122
-
123
- -- people FKs
124
- ALTER TABLE public.people ADD CONSTRAINT people_account_id_fkey
125
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
126
- ALTER TABLE public.people ADD CONSTRAINT people_app_id_fkey
127
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
128
- ALTER TABLE public.people ADD CONSTRAINT people_role_id_fkey
129
- FOREIGN KEY (role_id) REFERENCES public.roles(id) ON DELETE SET NULL;
130
- ALTER TABLE public.people ADD CONSTRAINT people_type_id_fkey
131
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
132
- ALTER TABLE public.people ADD CONSTRAINT people_created_by_fkey
133
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
134
-
135
- -- types FK
136
- ALTER TABLE public.types ADD CONSTRAINT fk_types_app
137
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
138
-
139
- -- apps FKs
140
- ALTER TABLE public.apps ADD CONSTRAINT fk_apps_account
141
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
142
- ALTER TABLE public.apps ADD CONSTRAINT fk_apps_owner_account
143
- FOREIGN KEY (owner_account_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
144
-
145
- -- roles FKs
146
- ALTER TABLE public.roles ADD CONSTRAINT fk_roles_app
147
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
148
- ALTER TABLE public.roles ADD CONSTRAINT fk_roles_account
149
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
150
-
151
- -- items FKs
152
- ALTER TABLE public.items ADD CONSTRAINT fk_items_type
153
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
154
- ALTER TABLE public.items ADD CONSTRAINT fk_items_account
155
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
156
- ALTER TABLE public.items ADD CONSTRAINT fk_items_app
157
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
158
- ALTER TABLE public.items ADD CONSTRAINT fk_items_created_by
159
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
160
- ALTER TABLE public.items ADD CONSTRAINT fk_items_updated_by
161
- FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
162
-
163
- -- threads FKs
164
- ALTER TABLE public.threads ADD CONSTRAINT fk_threads_type
165
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
166
- ALTER TABLE public.threads ADD CONSTRAINT fk_threads_account
167
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
168
- ALTER TABLE public.threads ADD CONSTRAINT fk_threads_created_by
169
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
170
- ALTER TABLE public.threads ADD CONSTRAINT fk_threads_updated_by
171
- FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
172
-
173
- -- messages FKs
174
- ALTER TABLE public.messages ADD CONSTRAINT fk_messages_type
175
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
176
- ALTER TABLE public.messages ADD CONSTRAINT fk_messages_thread
177
- FOREIGN KEY (thread_id) REFERENCES public.threads(id) ON DELETE CASCADE;
178
- ALTER TABLE public.messages ADD CONSTRAINT fk_messages_person
179
- FOREIGN KEY (person_id) REFERENCES public.people(id) ON DELETE SET NULL;
180
- ALTER TABLE public.messages ADD CONSTRAINT fk_messages_created_by
181
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
182
-
183
- -- links FKs
184
- ALTER TABLE public.links ADD CONSTRAINT fk_links_type
185
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
186
- ALTER TABLE public.links ADD CONSTRAINT fk_links_account
187
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
188
- ALTER TABLE public.links ADD CONSTRAINT fk_links_link_type
189
- FOREIGN KEY (link_type_id) REFERENCES public.link_types(id) ON DELETE SET NULL;
190
- ALTER TABLE public.links ADD CONSTRAINT fk_links_created_by
191
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
192
- ALTER TABLE public.links ADD CONSTRAINT fk_links_updated_by
193
- FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
194
-
195
- -- attachments FKs
196
- ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_type
197
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
198
- ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_account
199
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
200
- ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_uploaded_by
201
- FOREIGN KEY (uploaded_by) REFERENCES public.people(id) ON DELETE SET NULL;
202
- ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_created_by
203
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
204
-
205
- -- watchers FKs
206
- ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_type
207
- FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
208
- ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_account
209
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
210
- ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_person
211
- FOREIGN KEY (person_id) REFERENCES public.people(id) ON DELETE CASCADE;
212
- ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_created_by
213
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
214
-
215
- -- link_types FK
216
- ALTER TABLE public.link_types ADD CONSTRAINT fk_link_types_app
217
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
218
-
219
- -- pipelines FKs
220
- ALTER TABLE public.pipelines ADD CONSTRAINT fk_pipelines_app
221
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
222
- ALTER TABLE public.pipelines ADD CONSTRAINT fk_pipelines_account
223
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
224
- ALTER TABLE public.pipelines ADD CONSTRAINT fk_pipelines_created_by
225
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
226
-
227
- -- pipeline_executions FKs
228
- ALTER TABLE public.pipeline_executions ADD CONSTRAINT fk_pe_pipeline
229
- FOREIGN KEY (pipeline_id) REFERENCES public.pipelines(id) ON DELETE CASCADE;
230
- ALTER TABLE public.pipeline_executions ADD CONSTRAINT fk_pe_account
231
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
232
- ALTER TABLE public.pipeline_executions ADD CONSTRAINT fk_pe_created_by
233
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
234
-
235
- -- triggers FKs
236
- ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_app
237
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
238
- ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_account
239
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
240
- ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_pipeline
241
- FOREIGN KEY (pipeline_id) REFERENCES public.pipelines(id) ON DELETE SET NULL;
242
- ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_created_by
243
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
244
-
245
- -- trigger_executions FK
246
- ALTER TABLE public.trigger_executions ADD CONSTRAINT fk_te_trigger
247
- FOREIGN KEY (trigger_id) REFERENCES public.triggers(id) ON DELETE CASCADE;
248
-
249
- -- timers FKs
250
- ALTER TABLE public.timers ADD CONSTRAINT fk_timers_app
251
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
252
- ALTER TABLE public.timers ADD CONSTRAINT fk_timers_account
253
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
254
- ALTER TABLE public.timers ADD CONSTRAINT fk_timers_pipeline
255
- FOREIGN KEY (pipeline_id) REFERENCES public.pipelines(id) ON DELETE SET NULL;
256
- ALTER TABLE public.timers ADD CONSTRAINT fk_timers_created_by
257
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
258
-
259
- -- ai_agents FKs
260
- ALTER TABLE public.ai_agents ADD CONSTRAINT fk_ai_agents_app
261
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
262
- ALTER TABLE public.ai_agents ADD CONSTRAINT fk_ai_agents_account
263
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
264
- ALTER TABLE public.ai_agents ADD CONSTRAINT fk_ai_agents_created_by
265
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
266
-
267
- -- embeddings FK
268
- ALTER TABLE public.embeddings ADD CONSTRAINT fk_embeddings_account
269
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
270
-
271
- -- integrations FKs
272
- ALTER TABLE public.integrations ADD CONSTRAINT fk_integrations_app
273
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
274
- ALTER TABLE public.integrations ADD CONSTRAINT fk_integrations_account
275
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
276
- ALTER TABLE public.integrations ADD CONSTRAINT fk_integrations_created_by
277
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
278
-
279
- -- prompt_configs FKs
280
- ALTER TABLE public.prompt_configs ADD CONSTRAINT fk_prompt_configs_app
281
- FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
282
- ALTER TABLE public.prompt_configs ADD CONSTRAINT fk_prompt_configs_account
283
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
284
- ALTER TABLE public.prompt_configs ADD CONSTRAINT fk_prompt_configs_created_by
285
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
286
-
287
- -- api_keys FKs
288
- ALTER TABLE public.api_keys ADD CONSTRAINT fk_api_keys_integration
289
- FOREIGN KEY (integration_id) REFERENCES public.integrations(id) ON DELETE SET NULL;
290
- ALTER TABLE public.api_keys ADD CONSTRAINT fk_api_keys_account
291
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
292
- ALTER TABLE public.api_keys ADD CONSTRAINT fk_api_keys_created_by
293
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
294
-
295
- -- api_key_usage_logs FKs
296
- ALTER TABLE public.api_key_usage_logs ADD CONSTRAINT fk_akul_api_key
297
- FOREIGN KEY (api_key_id) REFERENCES public.api_keys(id) ON DELETE CASCADE;
298
- ALTER TABLE public.api_key_usage_logs ADD CONSTRAINT fk_akul_account
299
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
300
-
301
- -- logs FKs
302
- ALTER TABLE public.logs ADD CONSTRAINT fk_logs_person
303
- FOREIGN KEY (person_id) REFERENCES public.people(id) ON DELETE SET NULL;
304
- ALTER TABLE public.logs ADD CONSTRAINT fk_logs_account
305
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
306
-
307
- -- actions FKs
308
- ALTER TABLE public.actions ADD CONSTRAINT fk_actions_account
309
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
310
- ALTER TABLE public.actions ADD CONSTRAINT fk_actions_machine_principal
311
- FOREIGN KEY (default_machine_principal_id) REFERENCES public.api_keys(id) ON DELETE SET NULL;
312
- ALTER TABLE public.actions ADD CONSTRAINT fk_actions_created_by
313
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
314
-
315
- -- schedules FKs
316
- ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_account
317
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
318
- ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_action
319
- FOREIGN KEY (action_id) REFERENCES public.actions(id) ON DELETE CASCADE;
320
- ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_machine
321
- FOREIGN KEY (machine_principal_id) REFERENCES public.api_keys(id) ON DELETE SET NULL;
322
- ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_created_by
323
- FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
324
-
325
- -- schedule_executions FKs
326
- ALTER TABLE public.schedule_executions ADD CONSTRAINT fk_se_schedule
327
- FOREIGN KEY (schedule_id) REFERENCES public.schedules(id) ON DELETE CASCADE;
328
- ALTER TABLE public.schedule_executions ADD CONSTRAINT fk_se_account
329
- FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
330
- ALTER TABLE public.schedule_executions ADD CONSTRAINT fk_se_machine
331
- FOREIGN KEY (machine_principal_id) REFERENCES public.api_keys(id) ON DELETE SET NULL;
332
-
333
- -- ============================================
334
- -- INDEXES
335
- -- ============================================
336
-
337
- -- account_paths indexes
338
- CREATE INDEX IF NOT EXISTS idx_account_paths_ancestor ON public.account_paths(ancestor_id);
339
- CREATE INDEX IF NOT EXISTS idx_account_paths_descendant ON public.account_paths(descendant_id);
340
-
341
- -- accounts
342
- CREATE INDEX IF NOT EXISTS idx_accounts_parent_id ON public.accounts(parent_id);
343
- CREATE INDEX IF NOT EXISTS idx_accounts_type_id ON public.accounts(type_id);
344
- CREATE INDEX IF NOT EXISTS idx_accounts_active ON public.accounts(is_active);
345
-
346
- -- people
347
- CREATE INDEX IF NOT EXISTS idx_people_account ON public.people(account_id);
348
- CREATE INDEX IF NOT EXISTS idx_people_auth_uid ON public.people(auth_uid);
349
- CREATE INDEX IF NOT EXISTS idx_people_email ON public.people(email);
350
- CREATE INDEX IF NOT EXISTS idx_people_role ON public.people(role_id);
351
- CREATE INDEX IF NOT EXISTS idx_people_active ON public.people(is_active);
352
-
353
- -- types
354
- CREATE INDEX IF NOT EXISTS idx_types_slug ON public.types(slug);
355
- CREATE INDEX IF NOT EXISTS idx_types_kind ON public.types(kind);
356
-
357
- -- apps
358
- CREATE INDEX IF NOT EXISTS idx_apps_slug ON public.apps(slug);
359
- CREATE INDEX IF NOT EXISTS idx_apps_owner ON public.apps(owner_account_id);
360
- CREATE UNIQUE INDEX IF NOT EXISTS idx_apps_route_prefix ON public.apps(route_prefix)
361
- WHERE is_active = true AND route_prefix IS NOT NULL;
362
-
363
- -- roles
364
- CREATE INDEX IF NOT EXISTS idx_roles_slug ON public.roles(slug);
365
-
366
- -- items
367
- CREATE INDEX IF NOT EXISTS idx_items_type ON public.items(type_id);
368
- CREATE INDEX IF NOT EXISTS idx_items_account ON public.items(account_id);
369
- CREATE INDEX IF NOT EXISTS idx_items_active ON public.items(is_active);
370
- CREATE INDEX IF NOT EXISTS idx_items_created_at ON public.items(created_at);
371
-
372
- -- threads
373
- CREATE INDEX IF NOT EXISTS idx_threads_type ON public.threads(type_id);
374
- CREATE INDEX IF NOT EXISTS idx_threads_account ON public.threads(account_id);
375
- CREATE INDEX IF NOT EXISTS idx_threads_target ON public.threads(target_type, target_id);
376
- CREATE INDEX IF NOT EXISTS idx_threads_active ON public.threads(is_active);
377
-
378
- -- messages
379
- CREATE INDEX IF NOT EXISTS idx_messages_thread ON public.messages(thread_id);
380
- CREATE INDEX IF NOT EXISTS idx_messages_type ON public.messages(type_id);
381
- CREATE INDEX IF NOT EXISTS idx_messages_person ON public.messages(person_id);
382
-
383
- -- links
384
- CREATE INDEX IF NOT EXISTS idx_links_type ON public.links(type_id);
385
- CREATE INDEX IF NOT EXISTS idx_links_account ON public.links(account_id);
386
- CREATE INDEX IF NOT EXISTS idx_links_source ON public.links(source_type, source_id);
387
- CREATE INDEX IF NOT EXISTS idx_links_target ON public.links(target_type, target_id);
388
-
389
- -- attachments
390
- CREATE INDEX IF NOT EXISTS idx_attachments_type ON public.attachments(type_id);
391
- CREATE INDEX IF NOT EXISTS idx_attachments_account ON public.attachments(account_id);
392
-
393
- -- watchers
394
- CREATE INDEX IF NOT EXISTS idx_watchers_type ON public.watchers(type_id);
395
- CREATE INDEX IF NOT EXISTS idx_watchers_target ON public.watchers(target_type, target_id);
396
- CREATE INDEX IF NOT EXISTS idx_watchers_person ON public.watchers(person_id);
397
-
398
- -- link_types
399
- CREATE INDEX IF NOT EXISTS idx_link_types_slug ON public.link_types(slug);
400
-
401
- -- pipelines
402
- CREATE INDEX IF NOT EXISTS idx_pipelines_app ON public.pipelines(app_id);
403
- CREATE INDEX IF NOT EXISTS idx_pipelines_account ON public.pipelines(account_id);
404
-
405
- -- pipeline_executions
406
- CREATE INDEX IF NOT EXISTS idx_pe_pipeline ON public.pipeline_executions(pipeline_id);
407
- CREATE INDEX IF NOT EXISTS idx_pe_account ON public.pipeline_executions(account_id);
408
- CREATE INDEX IF NOT EXISTS idx_pe_status ON public.pipeline_executions(status);
409
-
410
- -- triggers
411
- CREATE INDEX IF NOT EXISTS idx_triggers_app ON public.triggers(app_id);
412
- CREATE INDEX IF NOT EXISTS idx_triggers_account ON public.triggers(account_id);
413
- CREATE INDEX IF NOT EXISTS idx_triggers_pipeline ON public.triggers(pipeline_id);
414
-
415
- -- trigger_executions
416
- CREATE INDEX IF NOT EXISTS idx_te_trigger ON public.trigger_executions(trigger_id);
417
-
418
- -- timers
419
- CREATE INDEX IF NOT EXISTS idx_timers_app ON public.timers(app_id);
420
- CREATE INDEX IF NOT EXISTS idx_timers_account ON public.timers(account_id);
421
- CREATE INDEX IF NOT EXISTS idx_timers_pipeline ON public.timers(pipeline_id);
422
-
423
- -- ai_agents
424
- CREATE INDEX IF NOT EXISTS idx_ai_agents_app ON public.ai_agents(app_id);
425
- CREATE INDEX IF NOT EXISTS idx_ai_agents_account ON public.ai_agents(account_id);
426
-
427
- -- embeddings
428
- CREATE INDEX IF NOT EXISTS idx_embeddings_account ON public.embeddings(account_id);
429
- CREATE INDEX IF NOT EXISTS idx_embeddings_model_doc ON public.embeddings(model_id, document_id);
430
-
431
- -- integrations
432
- CREATE INDEX IF NOT EXISTS idx_integrations_app ON public.integrations(app_id);
433
- CREATE INDEX IF NOT EXISTS idx_integrations_account ON public.integrations(account_id);
434
-
435
- -- prompt_configs
436
- CREATE INDEX IF NOT EXISTS idx_prompt_configs_slug ON public.prompt_configs(slug);
437
- CREATE INDEX IF NOT EXISTS idx_prompt_configs_app ON public.prompt_configs(app_id);
438
- CREATE INDEX IF NOT EXISTS idx_prompt_configs_account ON public.prompt_configs(account_id);
439
-
440
- -- api_keys
441
- CREATE INDEX IF NOT EXISTS idx_api_keys_integration ON public.api_keys(integration_id);
442
- CREATE INDEX IF NOT EXISTS idx_api_keys_account ON public.api_keys(account_id);
443
- CREATE INDEX IF NOT EXISTS idx_api_keys_key_value ON public.api_keys(key_value);
444
-
445
- -- api_key_usage_logs
446
- CREATE INDEX IF NOT EXISTS idx_akul_api_key ON public.api_key_usage_logs(api_key_id);
447
- CREATE INDEX IF NOT EXISTS idx_akul_account ON public.api_key_usage_logs(account_id);
448
- CREATE INDEX IF NOT EXISTS idx_akul_created ON public.api_key_usage_logs(created_at);
449
-
450
- -- logs
451
- CREATE INDEX IF NOT EXISTS idx_logs_account ON public.logs(account_id);
452
- CREATE INDEX IF NOT EXISTS idx_logs_level ON public.logs(level);
453
- CREATE INDEX IF NOT EXISTS idx_logs_created ON public.logs(created_at);
454
- CREATE INDEX IF NOT EXISTS idx_logs_source ON public.logs(source_type, source_id);
455
-
456
- -- actions
457
- CREATE INDEX IF NOT EXISTS idx_actions_account ON public.actions(account_id);
458
- CREATE INDEX IF NOT EXISTS idx_actions_slug ON public.actions(slug);
459
-
460
- -- schedules
461
- CREATE INDEX IF NOT EXISTS idx_schedules_account ON public.schedules(account_id);
462
- CREATE INDEX IF NOT EXISTS idx_schedules_action ON public.schedules(action_id);
463
- CREATE INDEX IF NOT EXISTS idx_schedules_next_run ON public.schedules(next_run_at);
464
-
465
- -- schedule_executions
466
- CREATE INDEX IF NOT EXISTS idx_se_schedule ON public.schedule_executions(schedule_id);
467
- CREATE INDEX IF NOT EXISTS idx_se_account ON public.schedule_executions(account_id);
468
-
469
- -- test tables
470
- CREATE INDEX IF NOT EXISTS idx_test_results_run_id ON public.test_results(run_id);
471
- CREATE INDEX IF NOT EXISTS idx_test_runs_suite ON public.test_runs(suite);
472
- CREATE INDEX IF NOT EXISTS idx_test_runs_started_at ON public.test_runs(started_at DESC);
473
-
474
- -- item_progress
475
- CREATE INDEX IF NOT EXISTS idx_item_progress_person ON public.item_progress(person_id);
476
- CREATE INDEX IF NOT EXISTS idx_item_progress_item ON public.item_progress(item_id);
477
- CREATE INDEX IF NOT EXISTS idx_item_progress_account ON public.item_progress(account_id);
478
- CREATE INDEX IF NOT EXISTS idx_item_progress_type ON public.item_progress(type_id);
479
- CREATE INDEX IF NOT EXISTS idx_item_progress_status ON public.item_progress(status);
480
- CREATE INDEX IF NOT EXISTS idx_item_progress_data_gin ON public.item_progress USING gin(data);
481
- CREATE INDEX IF NOT EXISTS idx_item_progress_active ON public.item_progress(account_id, is_active) WHERE is_active = true;
482
-
483
- -- webhook_handlers
484
- CREATE INDEX IF NOT EXISTS idx_webhook_handlers_name ON public.webhook_handlers(name) WHERE is_active = true AND is_deleted = false;
485
- CREATE INDEX IF NOT EXISTS idx_webhook_handlers_account ON public.webhook_handlers(account_id) WHERE is_deleted = false;
486
- CREATE INDEX IF NOT EXISTS idx_webhook_handlers_events ON public.webhook_handlers USING GIN(events) WHERE is_active = true;
487
-
488
- -- ============================================
489
- -- ENABLE RLS ON ALL TABLES
490
- -- ============================================
491
-
492
- ALTER TABLE public.accounts ENABLE ROW LEVEL SECURITY;
493
- ALTER TABLE public.people ENABLE ROW LEVEL SECURITY;
494
- ALTER TABLE public.types ENABLE ROW LEVEL SECURITY;
495
- ALTER TABLE public.apps ENABLE ROW LEVEL SECURITY;
496
- ALTER TABLE public.roles ENABLE ROW LEVEL SECURITY;
497
- ALTER TABLE public.items ENABLE ROW LEVEL SECURITY;
498
- ALTER TABLE public.threads ENABLE ROW LEVEL SECURITY;
499
- ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
500
- ALTER TABLE public.links ENABLE ROW LEVEL SECURITY;
501
- ALTER TABLE public.attachments ENABLE ROW LEVEL SECURITY;
502
- ALTER TABLE public.watchers ENABLE ROW LEVEL SECURITY;
503
- ALTER TABLE public.link_types ENABLE ROW LEVEL SECURITY;
504
- ALTER TABLE public.pipelines ENABLE ROW LEVEL SECURITY;
505
- ALTER TABLE public.pipeline_executions ENABLE ROW LEVEL SECURITY;
506
- ALTER TABLE public.triggers ENABLE ROW LEVEL SECURITY;
507
- ALTER TABLE public.trigger_executions ENABLE ROW LEVEL SECURITY;
508
- ALTER TABLE public.timers ENABLE ROW LEVEL SECURITY;
509
- ALTER TABLE public.ai_agents ENABLE ROW LEVEL SECURITY;
510
- ALTER TABLE public.embeddings ENABLE ROW LEVEL SECURITY;
511
- ALTER TABLE public.integrations ENABLE ROW LEVEL SECURITY;
512
- ALTER TABLE public.prompt_configs ENABLE ROW LEVEL SECURITY;
513
- ALTER TABLE public.api_keys ENABLE ROW LEVEL SECURITY;
514
- ALTER TABLE public.api_key_usage_logs ENABLE ROW LEVEL SECURITY;
515
- ALTER TABLE public.logs ENABLE ROW LEVEL SECURITY;
516
- ALTER TABLE public.actions ENABLE ROW LEVEL SECURITY;
517
- ALTER TABLE public.schedules ENABLE ROW LEVEL SECURITY;
518
- ALTER TABLE public.schedule_executions ENABLE ROW LEVEL SECURITY;
519
- ALTER TABLE public.item_progress ENABLE ROW LEVEL SECURITY;
520
- ALTER TABLE public.webhook_handlers ENABLE ROW LEVEL SECURITY;
521
-
522
- -- ============================================
523
- -- RLS POLICIES
524
- -- ============================================
525
-
526
- -- accounts
527
- DROP POLICY IF EXISTS accounts_access ON public.accounts;
528
- CREATE POLICY accounts_access ON public.accounts FOR ALL
529
- USING (id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
530
- OR public.current_actor_id() IS NULL);
531
-
532
- -- people
533
- DROP POLICY IF EXISTS people_access ON public.people;
534
- CREATE POLICY people_access ON public.people FOR ALL
535
- USING (account_id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
536
- OR public.current_actor_id() IS NULL);
537
-
538
- -- types
539
- DROP POLICY IF EXISTS types_access ON public.types;
540
- CREATE POLICY types_access ON public.types FOR ALL
541
- USING (public.current_actor_id() IS NOT NULL OR true);
542
-
543
- -- apps
544
- DROP POLICY IF EXISTS apps_access ON public.apps;
545
- CREATE POLICY apps_access ON public.apps FOR ALL
546
- USING (is_system = true OR owner_account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
547
- OR auth.uid() IS NULL);
548
-
549
- -- roles
550
- DROP POLICY IF EXISTS roles_read ON public.roles;
551
- CREATE POLICY roles_read ON public.roles FOR SELECT USING (true);
552
- DROP POLICY IF EXISTS roles_modify ON public.roles;
553
- CREATE POLICY roles_modify ON public.roles FOR ALL
554
- USING ((is_system = true AND public.person_is_system_admin(public.current_actor_id()))
555
- OR is_system = false OR public.current_actor_id() IS NULL);
556
-
557
- -- items (with platform scope visibility from 012_rls_scope.sql)
558
- DROP POLICY IF EXISTS items_access ON public.items;
559
- CREATE POLICY items_access ON public.items FOR ALL
560
- USING (
561
- account_id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
562
- OR (design_schema->>'scope' = 'platform' AND public.current_actor_id() IS NOT NULL)
563
- OR public.current_actor_id() IS NULL
564
- );
565
-
566
- -- threads (runtime record design_schema is the source of truth for scope)
567
- DROP POLICY IF EXISTS threads_access ON public.threads;
568
- CREATE POLICY threads_access ON public.threads FOR ALL
569
- USING (
570
- design_schema->>'scope' = 'platform' AND public.current_actor_id() IS NOT NULL
571
- OR account_id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
572
- OR public.current_actor_id() IS NULL
573
- );
574
-
575
- -- messages (inherits thread access from the runtime record design_schema; visibility
576
- -- filters internal notes to support/support_admin roles)
577
- DROP POLICY IF EXISTS messages_access ON public.messages;
578
- CREATE POLICY messages_access ON public.messages FOR ALL
579
- USING (
580
- EXISTS (
581
- SELECT 1 FROM public.threads t
582
- WHERE t.id = messages.thread_id
583
- AND (
584
- t.design_schema->>'scope' = 'platform' AND public.current_actor_id() IS NOT NULL
585
- OR t.account_id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
586
- )
587
- )
588
- AND (
589
- messages.visibility IS NULL
590
- OR messages.visibility = 'external'
591
- OR (
592
- messages.visibility = 'internal'
593
- AND EXISTS (
594
- SELECT 1 FROM public.people p
595
- JOIN public.roles r ON p.role_id = r.id
596
- WHERE p.id = public.current_actor_id()
597
- AND (r.slug IN ('support', 'support_admin') OR r.is_system = true)
598
- )
599
- )
600
- )
601
- OR public.current_actor_id() IS NULL
602
- );
603
-
604
- -- links (with platform scope visibility from 012_rls_scope.sql)
605
- DROP POLICY IF EXISTS links_access ON public.links;
606
- CREATE POLICY links_access ON public.links FOR ALL
607
- USING (
608
- account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
609
- OR (design_schema->>'scope' = 'platform' AND auth.uid() IS NOT NULL)
610
- OR auth.uid() IS NULL
611
- );
612
-
613
- -- attachments (with platform scope visibility from 012_rls_scope.sql)
614
- DROP POLICY IF EXISTS attachments_access ON public.attachments;
615
- CREATE POLICY attachments_access ON public.attachments FOR ALL
616
- USING (
617
- account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
618
- OR (design_schema->>'scope' = 'platform' AND auth.uid() IS NOT NULL)
619
- OR auth.uid() IS NULL
620
- );
621
-
622
- -- watchers
623
- DROP POLICY IF EXISTS watchers_access ON public.watchers;
624
- CREATE POLICY watchers_access ON public.watchers FOR ALL
625
- USING (auth.uid() IS NOT NULL OR true);
626
-
627
- -- link_types
628
- DROP POLICY IF EXISTS link_types_access ON public.link_types;
629
- CREATE POLICY link_types_access ON public.link_types FOR ALL
630
- USING (app_id IS NULL
631
- OR EXISTS (SELECT 1 FROM public.apps a WHERE a.id = link_types.app_id
632
- AND a.owner_account_id IN (SELECT public.get_accessible_accounts(auth.uid())))
633
- OR auth.uid() IS NULL);
634
-
635
- -- pipelines
636
- DROP POLICY IF EXISTS pipelines_access ON public.pipelines;
637
- CREATE POLICY pipelines_access ON public.pipelines FOR ALL
638
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
639
- OR auth.uid() IS NULL);
640
-
641
- -- pipeline_executions
642
- DROP POLICY IF EXISTS pe_access ON public.pipeline_executions;
643
- CREATE POLICY pe_access ON public.pipeline_executions FOR ALL
644
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
645
- OR auth.uid() IS NULL);
646
-
647
- -- triggers
648
- DROP POLICY IF EXISTS triggers_access ON public.triggers;
649
- CREATE POLICY triggers_access ON public.triggers FOR ALL
650
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
651
- OR auth.uid() IS NULL);
652
-
653
- -- trigger_executions
654
- DROP POLICY IF EXISTS te_access ON public.trigger_executions;
655
- CREATE POLICY te_access ON public.trigger_executions FOR ALL
656
- USING (EXISTS (SELECT 1 FROM public.triggers t
657
- WHERE t.id = trigger_executions.trigger_id
658
- AND t.account_id IN (SELECT public.get_accessible_accounts(auth.uid())))
659
- OR auth.uid() IS NULL);
660
-
661
- -- timers
662
- DROP POLICY IF EXISTS timers_access ON public.timers;
663
- CREATE POLICY timers_access ON public.timers FOR ALL
664
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
665
- OR auth.uid() IS NULL);
666
-
667
- -- ai_agents
668
- DROP POLICY IF EXISTS ai_agents_access ON public.ai_agents;
669
- CREATE POLICY ai_agents_access ON public.ai_agents FOR ALL
670
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
671
- OR auth.uid() IS NULL);
672
-
673
- -- embeddings
674
- DROP POLICY IF EXISTS embeddings_access ON public.embeddings;
675
- CREATE POLICY embeddings_access ON public.embeddings FOR ALL
676
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
677
- OR auth.uid() IS NULL);
678
-
679
- -- integrations
680
- DROP POLICY IF EXISTS integrations_access ON public.integrations;
681
- CREATE POLICY integrations_access ON public.integrations FOR ALL
682
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
683
- OR auth.uid() IS NULL);
684
-
685
- -- prompt_configs
686
- DROP POLICY IF EXISTS prompt_configs_access ON public.prompt_configs;
687
- CREATE POLICY prompt_configs_access ON public.prompt_configs FOR ALL
688
- USING ((app_id IS NULL OR EXISTS (SELECT 1 FROM public.apps a
689
- WHERE a.id = prompt_configs.app_id
690
- AND a.owner_account_id IN (SELECT public.get_accessible_accounts(auth.uid()))))
691
- OR auth.uid() IS NULL);
692
-
693
- -- api_keys
694
- DROP POLICY IF EXISTS api_keys_access ON public.api_keys;
695
- CREATE POLICY api_keys_access ON public.api_keys FOR ALL
696
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
697
- OR id = auth.uid() OR auth.uid() IS NULL);
698
-
699
- -- api_key_usage_logs
700
- DROP POLICY IF EXISTS akul_access ON public.api_key_usage_logs;
701
- CREATE POLICY akul_access ON public.api_key_usage_logs FOR ALL
702
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
703
- OR auth.uid() IS NULL);
704
-
705
- -- logs
706
- DROP POLICY IF EXISTS logs_access ON public.logs;
707
- CREATE POLICY logs_access ON public.logs FOR ALL
708
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
709
- OR auth.uid() IS NULL);
710
-
711
- -- actions
712
- DROP POLICY IF EXISTS actions_access ON public.actions;
713
- CREATE POLICY actions_access ON public.actions FOR ALL
714
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
715
- OR auth.uid() IS NULL);
716
-
717
- -- schedules
718
- DROP POLICY IF EXISTS schedules_access ON public.schedules;
719
- CREATE POLICY schedules_access ON public.schedules FOR ALL
720
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
721
- OR auth.uid() IS NULL);
722
-
723
- -- schedule_executions
724
- DROP POLICY IF EXISTS se_access ON public.schedule_executions;
725
- CREATE POLICY se_access ON public.schedule_executions FOR ALL
726
- USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
727
- OR auth.uid() IS NULL);
728
-
729
- -- item_progress
730
- DROP POLICY IF EXISTS item_progress_access ON public.item_progress;
731
- CREATE POLICY item_progress_access ON public.item_progress FOR ALL
732
- USING (
733
- account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
734
- OR auth.uid() IS NULL
735
- );
736
-
737
- -- webhook_handlers
738
- DROP POLICY IF EXISTS webhook_handlers_system_admin ON public.webhook_handlers;
739
- CREATE POLICY webhook_handlers_system_admin ON public.webhook_handlers
740
- FOR ALL
741
- TO authenticated
742
- USING (
743
- (SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin'
744
- )
745
- WITH CHECK (
746
- (SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin'
747
- );
748
-
749
- DROP POLICY IF EXISTS webhook_handlers_service_role ON public.webhook_handlers;
750
- CREATE POLICY webhook_handlers_service_role ON public.webhook_handlers
751
- FOR ALL
752
- TO service_role
753
- USING (true)
754
- WITH CHECK (true);
755
-
756
- -- ============================================
757
- -- TRIGGERS FOR UPDATED_AT
758
- -- ============================================
759
-
760
- CREATE TRIGGER webhook_handlers_updated_at
761
- BEFORE UPDATE ON public.webhook_handlers
762
- FOR EACH ROW
763
- EXECUTE FUNCTION public.set_updated_at();