spine-framework 0.2.1 → 1.0.0

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 (425) hide show
  1. package/LICENSE.md +213 -8
  2. package/README.md +327 -0
  3. package/package.json +107 -217
  4. package/src/cli/commands/init.ts +192 -0
  5. package/src/cli/commands/install.ts +25 -0
  6. package/src/cli/commands/list.ts +33 -0
  7. package/src/cli/commands/migrate.ts +24 -0
  8. package/src/cli/index.ts +46 -0
  9. package/src/components/AppLayout.tsx +21 -0
  10. package/src/components/AuthGuard.tsx +21 -0
  11. package/src/components/RoleGuard.tsx +23 -0
  12. package/src/components/index.ts +3 -0
  13. package/src/contexts/AppContext.tsx +122 -0
  14. package/src/contexts/AuthContext.tsx +87 -0
  15. package/src/contexts/SpineContext.tsx +46 -0
  16. package/src/contexts/index.ts +3 -0
  17. package/src/hooks/index.ts +4 -0
  18. package/src/hooks/useItems.ts +78 -0
  19. package/src/hooks/useThreads.ts +73 -0
  20. package/src/hooks/useWebSocket.ts +97 -0
  21. package/src/index.ts +22 -0
  22. package/src/types/index.ts +163 -0
  23. package/src/utils/api.ts +88 -0
  24. package/src/utils/client.ts +146 -0
  25. package/src/utils/config.ts +20 -0
  26. package/src/utils/index.ts +3 -0
  27. package/.framework/README.md +0 -129
  28. package/.framework/cli/bin.cjs +0 -14
  29. package/.framework/cli/commands/agents.ts +0 -153
  30. package/.framework/cli/commands/auth.ts +0 -94
  31. package/.framework/cli/commands/create-app.ts +0 -185
  32. package/.framework/cli/commands/dev.ts +0 -113
  33. package/.framework/cli/commands/doctor.ts +0 -442
  34. package/.framework/cli/commands/generate.ts +0 -332
  35. package/.framework/cli/commands/init.ts +0 -186
  36. package/.framework/cli/commands/install-app.ts +0 -565
  37. package/.framework/cli/commands/items.ts +0 -253
  38. package/.framework/cli/commands/migrate.ts +0 -139
  39. package/.framework/cli/commands/migrations.ts +0 -141
  40. package/.framework/cli/commands/pipelines.ts +0 -166
  41. package/.framework/cli/commands/status.ts +0 -197
  42. package/.framework/cli/commands/system.ts +0 -184
  43. package/.framework/cli/commands/test.ts +0 -227
  44. package/.framework/cli/commands/uninstall-app.ts +0 -166
  45. package/.framework/cli/context.ts +0 -268
  46. package/.framework/cli/env-loader.ts +0 -36
  47. package/.framework/cli/index.ts +0 -116
  48. package/.framework/cli/welcome.cjs +0 -45
  49. package/.framework/docs/API.md +0 -384
  50. package/.framework/docs/STABILITY.md +0 -52
  51. package/.framework/docs/admin-routes.md +0 -76
  52. package/.framework/docs/api-docs-progress.md +0 -38
  53. package/.framework/docs/api-governance.md +0 -146
  54. package/.framework/docs/api-testing-results.md +0 -212
  55. package/.framework/docs/apis/admin-configs.md +0 -567
  56. package/.framework/docs/apis/admin-data.md +0 -272
  57. package/.framework/docs/apis/index.md +0 -231
  58. package/.framework/docs/apis/internal.md +0 -295
  59. package/.framework/docs/apis/runtime.md +0 -537
  60. package/.framework/docs/assembly-launch-guide.md +0 -138
  61. package/.framework/docs/audit-results.md +0 -590
  62. package/.framework/docs/authorization-model.md +0 -170
  63. package/.framework/docs/db-api-inventory.md +0 -95
  64. package/.framework/docs/examples/custom-app/README.md +0 -77
  65. package/.framework/docs/examples/custom-function/README.md +0 -27
  66. package/.framework/docs/examples/custom-function/handler.ts +0 -48
  67. package/.framework/docs/examples/custom-webhook/README.md +0 -68
  68. package/.framework/docs/gap-remediation-backlog.md +0 -103
  69. package/.framework/docs/guides/cli-guide.md +0 -224
  70. package/.framework/docs/guides/getting-started.md +0 -103
  71. package/.framework/docs/guides/import-guide.md +0 -193
  72. package/.framework/docs/guides/testing-guide.md +0 -229
  73. package/.framework/docs/permission-examples.md +0 -326
  74. package/.framework/docs/ui-adoption-verification.md +0 -111
  75. package/.framework/docs/ui-api-coverage.md +0 -84
  76. package/.framework/docs/v2-compatibility-audit.md +0 -228
  77. package/.framework/functions/.gitkeep +0 -1
  78. package/.framework/functions/_shared/agent-runner.ts +0 -1097
  79. package/.framework/functions/_shared/app-manifest.ts +0 -184
  80. package/.framework/functions/_shared/audit.ts +0 -150
  81. package/.framework/functions/_shared/db.ts +0 -178
  82. package/.framework/functions/_shared/index.ts +0 -391
  83. package/.framework/functions/_shared/middleware.ts +0 -490
  84. package/.framework/functions/_shared/permissions.ts +0 -1325
  85. package/.framework/functions/_shared/pipeline-runner.ts +0 -731
  86. package/.framework/functions/_shared/principal.ts +0 -818
  87. package/.framework/functions/_shared/resolve-ids.ts +0 -106
  88. package/.framework/functions/_shared/schema-utils.ts +0 -967
  89. package/.framework/functions/_shared/testing.ts +0 -258
  90. package/.framework/functions/_shared/trigger-engine.ts +0 -425
  91. package/.framework/functions/_shared/webhook-registration.ts +0 -168
  92. package/.framework/functions/_shared/webhook-registry.ts +0 -129
  93. package/.framework/functions/account-nodes.ts +0 -111
  94. package/.framework/functions/admin-data.ts +0 -606
  95. package/.framework/functions/ai-agents.ts +0 -323
  96. package/.framework/functions/api-keys.ts +0 -376
  97. package/.framework/functions/apps.ts +0 -483
  98. package/.framework/functions/auth.ts +0 -196
  99. package/.framework/functions/debug-auth.ts +0 -107
  100. package/.framework/functions/embeddings.ts +0 -556
  101. package/.framework/functions/integration-routes.ts +0 -523
  102. package/.framework/functions/integrations.ts +0 -319
  103. package/.framework/functions/item-progress.ts +0 -272
  104. package/.framework/functions/logs.ts +0 -438
  105. package/.framework/functions/observability.ts +0 -275
  106. package/.framework/functions/pipeline-executions.ts +0 -494
  107. package/.framework/functions/pipelines.ts +0 -485
  108. package/.framework/functions/prompt-configs.ts +0 -339
  109. package/.framework/functions/roles.ts +0 -387
  110. package/.framework/functions/system-cron.ts +0 -742
  111. package/.framework/functions/system.ts +0 -323
  112. package/.framework/functions/tests.ts +0 -119
  113. package/.framework/functions/timers.ts +0 -357
  114. package/.framework/functions/triggers.ts +0 -563
  115. package/.framework/functions/types.ts +0 -604
  116. package/.framework/index.html +0 -16
  117. package/.framework/migrations/000_foundation.sql +0 -1266
  118. package/.framework/migrations/001_seed.sql +0 -163
  119. package/.framework/migrations/002_seed_constraints.sql +0 -19
  120. package/.framework/migrations/003_auth_user_trigger.sql +0 -67
  121. package/.framework/src/App.tsx +0 -133
  122. package/.framework/src/apps/admin/index.tsx +0 -173
  123. package/.framework/src/components/AppWrapper.tsx +0 -56
  124. package/.framework/src/components/CustomAppLoader.tsx +0 -116
  125. package/.framework/src/components/admin/AdminListPage.tsx +0 -151
  126. package/.framework/src/components/admin/AdminSidebar.tsx +0 -166
  127. package/.framework/src/components/admin/AdminStatsCard.tsx +0 -62
  128. package/.framework/src/components/admin/SortableTableHeader.tsx +0 -42
  129. package/.framework/src/components/app-shell/GenericAppShell.tsx +0 -181
  130. package/.framework/src/components/app-shell/GenericDetailPage.tsx +0 -200
  131. package/.framework/src/components/app-shell/GenericListPage.tsx +0 -116
  132. package/.framework/src/components/app-sidebar.tsx +0 -228
  133. package/.framework/src/components/auth/ProtectedRoute.tsx +0 -88
  134. package/.framework/src/components/layout/AppShell.tsx +0 -91
  135. package/.framework/src/components/layout/Header.tsx +0 -88
  136. package/.framework/src/components/layout/Layout.tsx +0 -95
  137. package/.framework/src/components/layout/Sidebar.tsx +0 -329
  138. package/.framework/src/components/runtime/DataDetailHeader.tsx +0 -77
  139. package/.framework/src/components/runtime/DataDetailPage.tsx +0 -171
  140. package/.framework/src/components/runtime/DataFilters.tsx +0 -91
  141. package/.framework/src/components/runtime/DataHeader.tsx +0 -68
  142. package/.framework/src/components/runtime/DataListPage.tsx +0 -124
  143. package/.framework/src/components/runtime/DataStats.tsx +0 -70
  144. package/.framework/src/components/runtime/DataTable.tsx +0 -174
  145. package/.framework/src/components/runtime/SchemaDetailForm.tsx +0 -134
  146. package/.framework/src/components/runtime/index.ts +0 -18
  147. package/.framework/src/components/search-form.tsx +0 -29
  148. package/.framework/src/components/shared/AgentView.tsx +0 -213
  149. package/.framework/src/components/shared/FieldRenderer.tsx +0 -478
  150. package/.framework/src/components/shared/SchemaFields.tsx +0 -226
  151. package/.framework/src/components/ui/DataTable.tsx +0 -343
  152. package/.framework/src/components/ui/Form.tsx +0 -281
  153. package/.framework/src/components/ui/ItemCard.tsx +0 -296
  154. package/.framework/src/components/ui/ItemListView.tsx +0 -308
  155. package/.framework/src/components/ui/LoadingSpinner.tsx +0 -52
  156. package/.framework/src/components/ui/Modal.tsx +0 -61
  157. package/.framework/src/components/ui/RichTextEditor.tsx +0 -210
  158. package/.framework/src/components/ui/accordion.tsx +0 -82
  159. package/.framework/src/components/ui/alert-dialog.tsx +0 -197
  160. package/.framework/src/components/ui/alert.tsx +0 -76
  161. package/.framework/src/components/ui/aspect-ratio.tsx +0 -11
  162. package/.framework/src/components/ui/avatar.tsx +0 -110
  163. package/.framework/src/components/ui/badge.tsx +0 -49
  164. package/.framework/src/components/ui/breadcrumb.tsx +0 -122
  165. package/.framework/src/components/ui/button-group.tsx +0 -83
  166. package/.framework/src/components/ui/button.tsx +0 -65
  167. package/.framework/src/components/ui/calendar.tsx +0 -222
  168. package/.framework/src/components/ui/card.tsx +0 -100
  169. package/.framework/src/components/ui/carousel.tsx +0 -240
  170. package/.framework/src/components/ui/chart.tsx +0 -368
  171. package/.framework/src/components/ui/checkbox.tsx +0 -31
  172. package/.framework/src/components/ui/collapsible.tsx +0 -33
  173. package/.framework/src/components/ui/combobox.tsx +0 -299
  174. package/.framework/src/components/ui/command.tsx +0 -193
  175. package/.framework/src/components/ui/context-menu.tsx +0 -261
  176. package/.framework/src/components/ui/dialog.tsx +0 -165
  177. package/.framework/src/components/ui/direction.tsx +0 -6
  178. package/.framework/src/components/ui/drawer.tsx +0 -132
  179. package/.framework/src/components/ui/dropdown-menu.tsx +0 -269
  180. package/.framework/src/components/ui/empty.tsx +0 -104
  181. package/.framework/src/components/ui/field.tsx +0 -238
  182. package/.framework/src/components/ui/hover-card.tsx +0 -42
  183. package/.framework/src/components/ui/input-group.tsx +0 -153
  184. package/.framework/src/components/ui/input-otp.tsx +0 -87
  185. package/.framework/src/components/ui/input.tsx +0 -19
  186. package/.framework/src/components/ui/item.tsx +0 -196
  187. package/.framework/src/components/ui/kbd.tsx +0 -26
  188. package/.framework/src/components/ui/label.tsx +0 -22
  189. package/.framework/src/components/ui/menubar.tsx +0 -277
  190. package/.framework/src/components/ui/native-select.tsx +0 -61
  191. package/.framework/src/components/ui/navigation-menu.tsx +0 -164
  192. package/.framework/src/components/ui/pagination.tsx +0 -129
  193. package/.framework/src/components/ui/popover.tsx +0 -87
  194. package/.framework/src/components/ui/progress.tsx +0 -31
  195. package/.framework/src/components/ui/radio-group.tsx +0 -42
  196. package/.framework/src/components/ui/resizable.tsx +0 -50
  197. package/.framework/src/components/ui/scroll-area.tsx +0 -53
  198. package/.framework/src/components/ui/select.tsx +0 -195
  199. package/.framework/src/components/ui/separator.tsx +0 -26
  200. package/.framework/src/components/ui/sheet.tsx +0 -145
  201. package/.framework/src/components/ui/sidebar.tsx +0 -706
  202. package/.framework/src/components/ui/skeleton.tsx +0 -13
  203. package/.framework/src/components/ui/slider.tsx +0 -59
  204. package/.framework/src/components/ui/sonner.tsx +0 -47
  205. package/.framework/src/components/ui/spinner.tsx +0 -10
  206. package/.framework/src/components/ui/switch.tsx +0 -33
  207. package/.framework/src/components/ui/table-primitives.tsx +0 -141
  208. package/.framework/src/components/ui/table.tsx +0 -114
  209. package/.framework/src/components/ui/tabs.tsx +0 -90
  210. package/.framework/src/components/ui/textarea.tsx +0 -18
  211. package/.framework/src/components/ui/toggle-group.tsx +0 -89
  212. package/.framework/src/components/ui/toggle.tsx +0 -45
  213. package/.framework/src/components/ui/tooltip.tsx +0 -57
  214. package/.framework/src/contexts/AppContext.tsx +0 -133
  215. package/.framework/src/contexts/AuthContext.tsx +0 -371
  216. package/.framework/src/hooks/use-mobile.ts +0 -19
  217. package/.framework/src/hooks/useApi.ts +0 -526
  218. package/.framework/src/hooks/useApps.ts +0 -114
  219. package/.framework/src/hooks/useEntityList.ts +0 -190
  220. package/.framework/src/hooks/useEntityRecord.ts +0 -308
  221. package/.framework/src/hooks/useForm.ts +0 -307
  222. package/.framework/src/hooks/useListSchema.ts +0 -264
  223. package/.framework/src/hooks/useSchemaRecord.ts +0 -223
  224. package/.framework/src/index.css +0 -128
  225. package/.framework/src/lib/api.ts +0 -156
  226. package/.framework/src/lib/supabase.ts +0 -94
  227. package/.framework/src/lib/utils.ts +0 -317
  228. package/.framework/src/main.tsx +0 -27
  229. package/.framework/src/pages/DashboardPage.tsx +0 -181
  230. package/.framework/src/pages/NotFoundPage.tsx +0 -39
  231. package/.framework/src/pages/admin/AIAgentDetailPage.tsx +0 -161
  232. package/.framework/src/pages/admin/AIAgentsPage.tsx +0 -318
  233. package/.framework/src/pages/admin/APIKeyDetailPage.tsx +0 -199
  234. package/.framework/src/pages/admin/APIKeysPage.tsx +0 -303
  235. package/.framework/src/pages/admin/AlertsConfigPage.tsx +0 -523
  236. package/.framework/src/pages/admin/AppDetailPage.tsx +0 -493
  237. package/.framework/src/pages/admin/AppsPage.tsx +0 -355
  238. package/.framework/src/pages/admin/DesignedPage.tsx +0 -491
  239. package/.framework/src/pages/admin/EmbeddingDetailPage.tsx +0 -534
  240. package/.framework/src/pages/admin/EmbeddingsPage.tsx +0 -424
  241. package/.framework/src/pages/admin/ExtendedShadcnTestPage.tsx +0 -176
  242. package/.framework/src/pages/admin/IncrementalShadcnTestPage.tsx +0 -109
  243. package/.framework/src/pages/admin/IntegratedDashboard.tsx +0 -402
  244. package/.framework/src/pages/admin/IntegrationDetailPage.tsx +0 -187
  245. package/.framework/src/pages/admin/IntegrationsPage.tsx +0 -301
  246. package/.framework/src/pages/admin/LogsPage.tsx +0 -283
  247. package/.framework/src/pages/admin/MinimalShadcnTestPage.tsx +0 -85
  248. package/.framework/src/pages/admin/ObservabilityDashboard.tsx +0 -470
  249. package/.framework/src/pages/admin/PipelineDetailPage.tsx +0 -183
  250. package/.framework/src/pages/admin/PipelineExecutionsPage.tsx +0 -279
  251. package/.framework/src/pages/admin/PipelinesPage.tsx +0 -390
  252. package/.framework/src/pages/admin/PromptConfigDetailPage.tsx +0 -299
  253. package/.framework/src/pages/admin/PromptConfigsPage.tsx +0 -292
  254. package/.framework/src/pages/admin/ProperlyDesignedPage.tsx +0 -434
  255. package/.framework/src/pages/admin/RoleDetailPage.tsx +0 -273
  256. package/.framework/src/pages/admin/RolesPage.tsx +0 -292
  257. package/.framework/src/pages/admin/SelectTestPage.tsx +0 -61
  258. package/.framework/src/pages/admin/ShadcnTestPage.tsx +0 -588
  259. package/.framework/src/pages/admin/SimpleDashboard.tsx +0 -387
  260. package/.framework/src/pages/admin/TestRunDetailPage.tsx +0 -172
  261. package/.framework/src/pages/admin/TestingDashboard.tsx +0 -257
  262. package/.framework/src/pages/admin/TimerDetailPage.tsx +0 -151
  263. package/.framework/src/pages/admin/TimersPage.tsx +0 -376
  264. package/.framework/src/pages/admin/TriggerDetailPage.tsx +0 -149
  265. package/.framework/src/pages/admin/TriggersPage.tsx +0 -381
  266. package/.framework/src/pages/admin/TypeDetailPage.tsx +0 -694
  267. package/.framework/src/pages/admin/TypesPage.tsx +0 -295
  268. package/.framework/src/pages/auth/LoginPage.tsx +0 -187
  269. package/.framework/src/pages/auth/RegisterPage.tsx +0 -163
  270. package/.framework/src/pages/spine-framework/APIPage.tsx +0 -17
  271. package/.framework/src/pages/spine-framework/CLIPage.tsx +0 -25
  272. package/.framework/src/types/auth.ts +0 -125
  273. package/.framework/src/types/types.ts +0 -407
  274. package/STRUCTURE.md +0 -150
  275. package/bin/spine-framework.cjs +0 -62
  276. package/bin/welcome.cjs +0 -45
  277. package/bin/ws-shim.cjs +0 -8
  278. package/bin/ws-shim.ts +0 -10
  279. package/config/components.json +0 -25
  280. package/config/deno.lock +0 -108
  281. package/config/package-lock.json +0 -17183
  282. package/config/postcss.config.cjs +0 -10
  283. package/config/tailwind.config.cjs +0 -78
  284. package/config/tsconfig.build.json +0 -32
  285. package/config/tsconfig.cli.json +0 -18
  286. package/config/tsconfig.json +0 -41
  287. package/config/tsconfig.node.json +0 -17
  288. package/config/tsconfig.node.tsbuildinfo +0 -1
  289. package/config/tsconfig.tsbuildinfo +0 -1
  290. package/config/typedoc.json +0 -16
  291. package/config/vite.config.d.ts +0 -2
  292. package/config/vite.config.ts +0 -71
  293. package/dist/cli/commands/agents.d.ts +0 -39
  294. package/dist/cli/commands/agents.d.ts.map +0 -1
  295. package/dist/cli/commands/auth.d.ts +0 -36
  296. package/dist/cli/commands/auth.d.ts.map +0 -1
  297. package/dist/cli/commands/create-app.d.ts +0 -23
  298. package/dist/cli/commands/create-app.d.ts.map +0 -1
  299. package/dist/cli/commands/dev.d.ts +0 -24
  300. package/dist/cli/commands/dev.d.ts.map +0 -1
  301. package/dist/cli/commands/doctor.d.ts +0 -42
  302. package/dist/cli/commands/doctor.d.ts.map +0 -1
  303. package/dist/cli/commands/generate.d.ts +0 -36
  304. package/dist/cli/commands/generate.d.ts.map +0 -1
  305. package/dist/cli/commands/init.d.ts +0 -20
  306. package/dist/cli/commands/init.d.ts.map +0 -1
  307. package/dist/cli/commands/install-app.d.ts +0 -30
  308. package/dist/cli/commands/install-app.d.ts.map +0 -1
  309. package/dist/cli/commands/items.d.ts +0 -45
  310. package/dist/cli/commands/items.d.ts.map +0 -1
  311. package/dist/cli/commands/migrate.d.ts +0 -21
  312. package/dist/cli/commands/migrate.d.ts.map +0 -1
  313. package/dist/cli/commands/migrations.d.ts +0 -41
  314. package/dist/cli/commands/migrations.d.ts.map +0 -1
  315. package/dist/cli/commands/pipelines.d.ts +0 -40
  316. package/dist/cli/commands/pipelines.d.ts.map +0 -1
  317. package/dist/cli/commands/status.d.ts +0 -23
  318. package/dist/cli/commands/status.d.ts.map +0 -1
  319. package/dist/cli/commands/system.d.ts +0 -29
  320. package/dist/cli/commands/system.d.ts.map +0 -1
  321. package/dist/cli/commands/test.d.ts +0 -46
  322. package/dist/cli/commands/test.d.ts.map +0 -1
  323. package/dist/cli/commands/uninstall-app.d.ts +0 -23
  324. package/dist/cli/commands/uninstall-app.d.ts.map +0 -1
  325. package/dist/cli/context.d.ts +0 -88
  326. package/dist/cli/context.d.ts.map +0 -1
  327. package/dist/cli/env-loader.d.ts +0 -14
  328. package/dist/cli/env-loader.d.ts.map +0 -1
  329. package/dist/cli/index.d.ts +0 -41
  330. package/dist/cli/index.d.ts.map +0 -1
  331. package/dist/functions/_shared/agent-runner.d.ts +0 -156
  332. package/dist/functions/_shared/agent-runner.d.ts.map +0 -1
  333. package/dist/functions/_shared/app-manifest.d.ts +0 -68
  334. package/dist/functions/_shared/app-manifest.d.ts.map +0 -1
  335. package/dist/functions/_shared/audit.d.ts +0 -91
  336. package/dist/functions/_shared/audit.d.ts.map +0 -1
  337. package/dist/functions/_shared/db.d.ts +0 -125
  338. package/dist/functions/_shared/db.d.ts.map +0 -1
  339. package/dist/functions/_shared/index.d.ts +0 -299
  340. package/dist/functions/_shared/index.d.ts.map +0 -1
  341. package/dist/functions/_shared/middleware.d.ts +0 -315
  342. package/dist/functions/_shared/middleware.d.ts.map +0 -1
  343. package/dist/functions/_shared/permissions.d.ts +0 -626
  344. package/dist/functions/_shared/permissions.d.ts.map +0 -1
  345. package/dist/functions/_shared/pipeline-runner.d.ts +0 -124
  346. package/dist/functions/_shared/pipeline-runner.d.ts.map +0 -1
  347. package/dist/functions/_shared/principal.d.ts +0 -284
  348. package/dist/functions/_shared/principal.d.ts.map +0 -1
  349. package/dist/functions/_shared/resolve-ids.d.ts +0 -10
  350. package/dist/functions/_shared/resolve-ids.d.ts.map +0 -1
  351. package/dist/functions/_shared/schema-utils.d.ts +0 -181
  352. package/dist/functions/_shared/schema-utils.d.ts.map +0 -1
  353. package/dist/functions/_shared/testing.d.ts +0 -172
  354. package/dist/functions/_shared/testing.d.ts.map +0 -1
  355. package/dist/functions/_shared/trigger-engine.d.ts +0 -140
  356. package/dist/functions/_shared/trigger-engine.d.ts.map +0 -1
  357. package/dist/functions/_shared/webhook-registration.d.ts +0 -81
  358. package/dist/functions/_shared/webhook-registration.d.ts.map +0 -1
  359. package/dist/functions/_shared/webhook-registry.d.ts +0 -57
  360. package/dist/functions/_shared/webhook-registry.d.ts.map +0 -1
  361. package/dist/functions/account-nodes.d.ts +0 -48
  362. package/dist/functions/account-nodes.d.ts.map +0 -1
  363. package/dist/functions/admin-data.d.ts +0 -178
  364. package/dist/functions/admin-data.d.ts.map +0 -1
  365. package/dist/functions/ai-agents.d.ts +0 -125
  366. package/dist/functions/ai-agents.d.ts.map +0 -1
  367. package/dist/functions/api-keys.d.ts +0 -140
  368. package/dist/functions/api-keys.d.ts.map +0 -1
  369. package/dist/functions/apps.d.ts +0 -163
  370. package/dist/functions/apps.d.ts.map +0 -1
  371. package/dist/functions/auth.d.ts +0 -74
  372. package/dist/functions/auth.d.ts.map +0 -1
  373. package/dist/functions/debug-auth.d.ts +0 -33
  374. package/dist/functions/debug-auth.d.ts.map +0 -1
  375. package/dist/functions/embeddings.d.ts +0 -205
  376. package/dist/functions/embeddings.d.ts.map +0 -1
  377. package/dist/functions/integration-routes.d.ts +0 -45
  378. package/dist/functions/integration-routes.d.ts.map +0 -1
  379. package/dist/functions/integrations.d.ts +0 -124
  380. package/dist/functions/integrations.d.ts.map +0 -1
  381. package/dist/functions/item-progress.d.ts +0 -41
  382. package/dist/functions/item-progress.d.ts.map +0 -1
  383. package/dist/functions/logs.d.ts +0 -162
  384. package/dist/functions/logs.d.ts.map +0 -1
  385. package/dist/functions/observability.d.ts +0 -123
  386. package/dist/functions/observability.d.ts.map +0 -1
  387. package/dist/functions/pipeline-executions.d.ts +0 -190
  388. package/dist/functions/pipeline-executions.d.ts.map +0 -1
  389. package/dist/functions/pipelines.d.ts +0 -171
  390. package/dist/functions/pipelines.d.ts.map +0 -1
  391. package/dist/functions/prompt-configs.d.ts +0 -125
  392. package/dist/functions/prompt-configs.d.ts.map +0 -1
  393. package/dist/functions/roles.d.ts +0 -118
  394. package/dist/functions/roles.d.ts.map +0 -1
  395. package/dist/functions/system-cron.d.ts +0 -65
  396. package/dist/functions/system-cron.d.ts.map +0 -1
  397. package/dist/functions/system.d.ts +0 -29
  398. package/dist/functions/system.d.ts.map +0 -1
  399. package/dist/functions/tests.d.ts +0 -28
  400. package/dist/functions/tests.d.ts.map +0 -1
  401. package/dist/functions/timers.d.ts +0 -139
  402. package/dist/functions/timers.d.ts.map +0 -1
  403. package/dist/functions/triggers.d.ts +0 -203
  404. package/dist/functions/triggers.d.ts.map +0 -1
  405. package/dist/functions/types.d.ts +0 -151
  406. package/dist/functions/types.d.ts.map +0 -1
  407. package/dist/src/types/types.d.ts +0 -364
  408. package/dist/src/types/types.d.ts.map +0 -1
  409. package/index.html +0 -13
  410. package/netlify.toml +0 -36
  411. package/package-project.json +0 -71
  412. package/scripts/app-install-cli.ts +0 -286
  413. package/scripts/assemble-frontend.sh +0 -76
  414. package/scripts/assemble-functions.sh +0 -62
  415. package/scripts/assemble.sh +0 -41
  416. package/scripts/boundary-check.sh +0 -106
  417. package/scripts/build-manifest.sh +0 -80
  418. package/scripts/check-core-integrity.sh +0 -82
  419. package/scripts/ingest-chunks.cjs +0 -202
  420. package/scripts/kb-chunk-parser.cjs +0 -312
  421. package/scripts/kb-chunk-parser.ts +0 -330
  422. package/scripts/load-test-app-install.ts +0 -484
  423. package/scripts/netlify-dev-wrapper.sh +0 -22
  424. package/scripts/verify-integrity.sh +0 -69
  425. package/vitest.config.ts +0 -45
@@ -1,1266 +0,0 @@
1
- -- =============================================================================
2
- -- 000_foundation.sql
3
- -- Spine Framework — Complete schema for a fresh install
4
- -- Generated from live production DB audit: May 28, 2026
5
- -- =============================================================================
6
- -- Run this on a blank Supabase project to create all core tables.
7
- -- Follow with 001_seed.sql to insert bootstrap data.
8
- -- =============================================================================
9
-
10
- -- ---------------------------------------------------------------------------
11
- -- Extensions
12
- -- ---------------------------------------------------------------------------
13
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
14
- CREATE EXTENSION IF NOT EXISTS "pgcrypto";
15
- CREATE EXTENSION IF NOT EXISTS "vector";
16
-
17
- -- ---------------------------------------------------------------------------
18
- -- Helper Functions (must exist before triggers)
19
- -- ---------------------------------------------------------------------------
20
-
21
- CREATE OR REPLACE FUNCTION public.set_updated_at()
22
- RETURNS TRIGGER LANGUAGE plpgsql AS $$
23
- BEGIN
24
- NEW.updated_at = now();
25
- RETURN NEW;
26
- END;
27
- $$;
28
-
29
- CREATE OR REPLACE FUNCTION public.current_actor_id()
30
- RETURNS uuid LANGUAGE plpgsql SECURITY DEFINER AS $$
31
- DECLARE
32
- v_auth_id UUID;
33
- v_person_id UUID;
34
- BEGIN
35
- v_auth_id := auth.uid();
36
- SELECT p.id INTO v_person_id
37
- FROM public.people p
38
- WHERE p.auth_uid = v_auth_id AND p.is_active = true
39
- LIMIT 1;
40
- RETURN v_person_id;
41
- END;
42
- $$;
43
-
44
- CREATE OR REPLACE FUNCTION public.person_is_system_admin(person_uuid uuid)
45
- RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER AS $$
46
- BEGIN
47
- RETURN EXISTS (
48
- SELECT 1 FROM public.people p
49
- JOIN public.roles r ON p.role_id = r.id
50
- WHERE p.id = person_uuid AND r.slug = 'system_admin'
51
- );
52
- END;
53
- $$;
54
-
55
- CREATE OR REPLACE FUNCTION public.get_accessible_accounts(actor_id uuid)
56
- RETURNS SETOF uuid LANGUAGE plpgsql SECURITY DEFINER AS $$
57
- DECLARE
58
- actor_person_id uuid;
59
- BEGIN
60
- SELECT p.id INTO actor_person_id
61
- FROM public.people p
62
- WHERE p.id = actor_id AND p.is_active = true
63
- LIMIT 1;
64
-
65
- IF actor_person_id IS NULL THEN
66
- SELECT p.id INTO actor_person_id
67
- FROM public.people p
68
- WHERE p.auth_uid = actor_id AND p.is_active = true
69
- LIMIT 1;
70
- END IF;
71
-
72
- IF actor_person_id IS NULL THEN
73
- RETURN;
74
- END IF;
75
-
76
- IF EXISTS (
77
- SELECT 1 FROM public.people p
78
- JOIN public.roles r ON p.role_id = r.id
79
- WHERE p.id = actor_person_id AND r.slug = 'system_admin'
80
- ) THEN
81
- RETURN QUERY SELECT a.id FROM public.accounts a;
82
- RETURN;
83
- END IF;
84
-
85
- RETURN QUERY
86
- WITH RECURSIVE account_tree AS (
87
- SELECT p.account_id AS id FROM public.people p WHERE p.id = actor_person_id
88
- UNION ALL
89
- SELECT ap.descendant_id
90
- FROM public.account_paths ap
91
- JOIN account_tree at ON ap.ancestor_id = at.id
92
- )
93
- SELECT id FROM account_tree;
94
- END;
95
- $$;
96
-
97
- CREATE OR REPLACE FUNCTION public.update_account_paths()
98
- RETURNS TRIGGER LANGUAGE plpgsql AS $$
99
- BEGIN
100
- IF TG_OP = 'INSERT' THEN
101
- IF NEW.parent_id IS NOT NULL THEN
102
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth)
103
- SELECT ancestor_id, NEW.id, depth + 1 FROM public.account_paths WHERE descendant_id = NEW.parent_id;
104
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth) VALUES (NEW.parent_id, NEW.id, 1);
105
- END IF;
106
- RETURN NEW;
107
- END IF;
108
- IF TG_OP = 'UPDATE' AND OLD.parent_id IS DISTINCT FROM NEW.parent_id THEN
109
- DELETE FROM public.account_paths WHERE descendant_id = NEW.id;
110
- IF NEW.parent_id IS NOT NULL THEN
111
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth)
112
- SELECT ancestor_id, NEW.id, depth + 1 FROM public.account_paths WHERE descendant_id = NEW.parent_id;
113
- INSERT INTO public.account_paths (ancestor_id, descendant_id, depth) VALUES (NEW.parent_id, NEW.id, 1);
114
- END IF;
115
- RETURN NEW;
116
- END IF;
117
- IF TG_OP = 'DELETE' THEN
118
- DELETE FROM public.account_paths WHERE descendant_id = OLD.id;
119
- RETURN OLD;
120
- END IF;
121
- RETURN NULL;
122
- END;
123
- $$;
124
-
125
- -- ---------------------------------------------------------------------------
126
- -- Core Tables (dependency order)
127
- -- ---------------------------------------------------------------------------
128
-
129
- -- roles (no dependencies)
130
- CREATE TABLE IF NOT EXISTS public.roles (
131
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
132
- slug text NOT NULL,
133
- name text NOT NULL,
134
- description text,
135
- permissions jsonb DEFAULT '[]'::jsonb,
136
- is_system boolean DEFAULT false,
137
- is_active boolean DEFAULT true,
138
- is_protected boolean DEFAULT false,
139
- app_id uuid,
140
- account_id uuid,
141
- created_at timestamptz DEFAULT now(),
142
- updated_at timestamptz DEFAULT now(),
143
- UNIQUE (app_id, slug)
144
- );
145
-
146
- -- apps (no dependencies on other custom tables)
147
- CREATE TABLE IF NOT EXISTS public.apps (
148
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
149
- slug text NOT NULL UNIQUE,
150
- name text NOT NULL,
151
- description text,
152
- icon text,
153
- color text,
154
- version text DEFAULT '0.1.0',
155
- app_type text DEFAULT 'custom',
156
- source text DEFAULT 'local',
157
- account_id uuid,
158
- owner_account_id uuid,
159
- is_active boolean DEFAULT true,
160
- is_system boolean DEFAULT false,
161
- min_role text,
162
- config jsonb DEFAULT '{}'::jsonb,
163
- nav_items jsonb DEFAULT '[]'::jsonb,
164
- route_prefix text,
165
- renderer text DEFAULT 'react',
166
- manifest_path text,
167
- integration_deps jsonb DEFAULT '[]'::jsonb,
168
- metadata jsonb DEFAULT '{}'::jsonb,
169
- created_at timestamptz DEFAULT now(),
170
- updated_at timestamptz DEFAULT now()
171
- );
172
- CREATE UNIQUE INDEX IF NOT EXISTS idx_apps_route_prefix ON public.apps (route_prefix) WHERE (is_active = true AND route_prefix IS NOT NULL);
173
-
174
- -- types (depends on apps)
175
- CREATE TABLE IF NOT EXISTS public.types (
176
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
177
- app_id uuid REFERENCES public.apps(id) ON DELETE SET NULL,
178
- kind text NOT NULL,
179
- slug text NOT NULL,
180
- name text NOT NULL,
181
- description text,
182
- icon text,
183
- color text,
184
- design_schema jsonb DEFAULT '{}'::jsonb,
185
- validation_schema jsonb DEFAULT '{}'::jsonb,
186
- ownership text DEFAULT 'tenant'::text,
187
- is_active boolean DEFAULT true,
188
- created_at timestamptz DEFAULT now(),
189
- updated_at timestamptz DEFAULT now(),
190
- UNIQUE (app_id, kind, slug)
191
- );
192
-
193
- -- accounts (depends on types, apps, people — people FK added after people table)
194
- CREATE TABLE IF NOT EXISTS public.accounts (
195
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
196
- parent_id uuid REFERENCES public.accounts(id) ON DELETE SET NULL,
197
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
198
- slug text NOT NULL UNIQUE,
199
- display_name text NOT NULL,
200
- description text,
201
- data jsonb DEFAULT '{}'::jsonb,
202
- is_active boolean DEFAULT true,
203
- created_at timestamptz DEFAULT now(),
204
- updated_at timestamptz DEFAULT now(),
205
- design_schema jsonb DEFAULT '{}'::jsonb,
206
- validation_schema jsonb DEFAULT '{}'::jsonb,
207
- app_id uuid REFERENCES public.apps(id) ON DELETE SET NULL,
208
- created_by uuid,
209
- updated_by uuid
210
- );
211
-
212
- -- account_paths (depends on accounts, RLS disabled intentionally — internal trigger-managed)
213
- CREATE TABLE IF NOT EXISTS public.account_paths (
214
- ancestor_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
215
- descendant_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
216
- depth integer NOT NULL,
217
- PRIMARY KEY (ancestor_id, descendant_id)
218
- );
219
-
220
- -- people (depends on accounts, types, roles, apps)
221
- CREATE TABLE IF NOT EXISTS public.people (
222
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
223
- auth_uid uuid UNIQUE,
224
- email text NOT NULL,
225
- full_name text NOT NULL,
226
- avatar_url text,
227
- phone text,
228
- status text DEFAULT 'active',
229
- data jsonb DEFAULT '{}'::jsonb,
230
- is_active boolean DEFAULT true,
231
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
232
- app_id uuid REFERENCES public.apps(id) ON DELETE SET NULL,
233
- role_id uuid REFERENCES public.roles(id) ON DELETE SET NULL,
234
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
235
- created_at timestamptz DEFAULT now(),
236
- updated_at timestamptz DEFAULT now(),
237
- design_schema jsonb DEFAULT '{}'::jsonb,
238
- validation_schema jsonb DEFAULT '{}'::jsonb,
239
- created_by uuid
240
- );
241
-
242
- -- add deferred FKs now that people exists
243
- DO $$ BEGIN
244
- ALTER TABLE public.accounts ADD CONSTRAINT accounts_created_by_fkey FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
245
- EXCEPTION WHEN duplicate_object THEN NULL; END $$;
246
- DO $$ BEGIN
247
- ALTER TABLE public.accounts ADD CONSTRAINT accounts_updated_by_fkey FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
248
- EXCEPTION WHEN duplicate_object THEN NULL; END $$;
249
- DO $$ BEGIN
250
- ALTER TABLE public.people ADD CONSTRAINT people_created_by_fkey FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
251
- EXCEPTION WHEN duplicate_object THEN NULL; END $$;
252
-
253
- -- add FK on roles.account_id and roles.app_id
254
- DO $$ BEGIN
255
- ALTER TABLE public.roles ADD CONSTRAINT roles_account_id_fkey FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
256
- EXCEPTION WHEN duplicate_object THEN NULL; END $$;
257
- DO $$ BEGIN
258
- ALTER TABLE public.roles ADD CONSTRAINT roles_app_id_fkey FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
259
- EXCEPTION WHEN duplicate_object THEN NULL; END $$;
260
-
261
- -- add FK on apps.account_id and apps.owner_account_id
262
- DO $$ BEGIN
263
- ALTER TABLE public.apps ADD CONSTRAINT apps_account_id_fkey FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
264
- EXCEPTION WHEN duplicate_object THEN NULL; END $$;
265
- DO $$ BEGIN
266
- ALTER TABLE public.apps ADD CONSTRAINT apps_owner_account_id_fkey FOREIGN KEY (owner_account_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
267
- EXCEPTION WHEN duplicate_object THEN NULL; END $$;
268
-
269
- -- items (depends on accounts, types, apps)
270
- CREATE TABLE IF NOT EXISTS public.items (
271
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
272
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
273
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
274
- app_id uuid REFERENCES public.apps(id) ON DELETE SET NULL,
275
- title text,
276
- description text,
277
- status text DEFAULT 'active',
278
- data jsonb DEFAULT '{}'::jsonb,
279
- is_active boolean DEFAULT true,
280
- design_schema jsonb DEFAULT '{}'::jsonb,
281
- validation_schema jsonb DEFAULT '{}'::jsonb,
282
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
283
- updated_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
284
- created_at timestamptz DEFAULT now(),
285
- updated_at timestamptz DEFAULT now()
286
- );
287
-
288
- -- link_types (depends on apps)
289
- CREATE TABLE IF NOT EXISTS public.link_types (
290
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
291
- app_id uuid REFERENCES public.apps(id) ON DELETE CASCADE,
292
- slug text NOT NULL,
293
- name text NOT NULL,
294
- description text,
295
- icon text,
296
- color text,
297
- config jsonb DEFAULT '{}'::jsonb,
298
- is_active boolean DEFAULT true,
299
- created_at timestamptz DEFAULT now(),
300
- updated_at timestamptz DEFAULT now()
301
- );
302
-
303
- -- links (depends on accounts, types, link_types)
304
- CREATE TABLE IF NOT EXISTS public.links (
305
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
306
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
307
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
308
- link_type_id uuid REFERENCES public.link_types(id) ON DELETE CASCADE,
309
- source_type text NOT NULL,
310
- source_id uuid NOT NULL,
311
- target_type text NOT NULL,
312
- target_id uuid NOT NULL,
313
- link_type text,
314
- metadata jsonb DEFAULT '{}'::jsonb,
315
- data jsonb DEFAULT '{}'::jsonb,
316
- design_schema jsonb DEFAULT '{}'::jsonb,
317
- validation_schema jsonb DEFAULT '{}'::jsonb,
318
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
319
- updated_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
320
- created_at timestamptz DEFAULT now(),
321
- updated_at timestamptz DEFAULT now()
322
- );
323
-
324
- -- threads (depends on accounts, types, apps, people)
325
- CREATE TABLE IF NOT EXISTS public.threads (
326
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
327
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
328
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
329
- app_id uuid REFERENCES public.apps(id) ON DELETE SET NULL,
330
- title text,
331
- target_type text NOT NULL,
332
- target_id uuid NOT NULL,
333
- visibility text DEFAULT 'internal',
334
- status text DEFAULT 'open',
335
- data jsonb DEFAULT '{}'::jsonb,
336
- is_active boolean DEFAULT true,
337
- design_schema jsonb DEFAULT '{}'::jsonb,
338
- validation_schema jsonb DEFAULT '{}'::jsonb,
339
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
340
- updated_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
341
- created_at timestamptz DEFAULT now(),
342
- updated_at timestamptz DEFAULT now()
343
- );
344
-
345
- -- messages (depends on threads, types, accounts, people)
346
- CREATE TABLE IF NOT EXISTS public.messages (
347
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
348
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
349
- thread_id uuid NOT NULL REFERENCES public.threads(id) ON DELETE CASCADE,
350
- content text NOT NULL,
351
- direction text DEFAULT 'outbound',
352
- sequence integer NOT NULL,
353
- visibility text DEFAULT 'internal',
354
- data jsonb DEFAULT '{}'::jsonb,
355
- is_active boolean DEFAULT true,
356
- design_schema jsonb DEFAULT '{}'::jsonb,
357
- validation_schema jsonb DEFAULT '{}'::jsonb,
358
- person_id uuid REFERENCES public.people(id) ON DELETE SET NULL,
359
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
360
- created_at timestamptz DEFAULT now(),
361
- updated_at timestamptz DEFAULT now(),
362
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE
363
- );
364
-
365
- -- attachments (depends on accounts, types, people)
366
- CREATE TABLE IF NOT EXISTS public.attachments (
367
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
368
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
369
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
370
- filename text NOT NULL,
371
- file_size integer,
372
- mime_type text,
373
- storage_path text,
374
- storage_provider text DEFAULT 'supabase',
375
- metadata jsonb DEFAULT '{}'::jsonb,
376
- data jsonb DEFAULT '{}'::jsonb,
377
- design_schema jsonb DEFAULT '{}'::jsonb,
378
- validation_schema jsonb DEFAULT '{}'::jsonb,
379
- uploaded_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
380
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
381
- created_at timestamptz DEFAULT now(),
382
- updated_at timestamptz DEFAULT now()
383
- );
384
-
385
- -- watchers (depends on accounts, types, people)
386
- CREATE TABLE IF NOT EXISTS public.watchers (
387
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
388
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
389
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
390
- target_type text NOT NULL,
391
- target_id uuid NOT NULL,
392
- person_id uuid NOT NULL REFERENCES public.people(id) ON DELETE CASCADE,
393
- watch_type text DEFAULT 'all',
394
- notification_level text DEFAULT 'all',
395
- metadata jsonb DEFAULT '{}'::jsonb,
396
- data jsonb DEFAULT '{}'::jsonb,
397
- design_schema jsonb DEFAULT '{}'::jsonb,
398
- validation_schema jsonb DEFAULT '{}'::jsonb,
399
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
400
- created_at timestamptz DEFAULT now(),
401
- updated_at timestamptz DEFAULT now(),
402
- UNIQUE (target_type, target_id, person_id)
403
- );
404
-
405
- -- pipelines (depends on accounts, apps, people)
406
- CREATE TABLE IF NOT EXISTS public.pipelines (
407
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
408
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
409
- app_id uuid REFERENCES public.apps(id) ON DELETE CASCADE,
410
- name text NOT NULL,
411
- description text,
412
- steps jsonb DEFAULT '[]'::jsonb,
413
- config jsonb DEFAULT '{}'::jsonb,
414
- ownership text DEFAULT 'tenant',
415
- is_system boolean DEFAULT false,
416
- is_active boolean DEFAULT true,
417
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
418
- created_at timestamptz DEFAULT now(),
419
- updated_at timestamptz DEFAULT now()
420
- );
421
-
422
- -- pipeline_executions (depends on accounts, pipelines, people)
423
- CREATE TABLE IF NOT EXISTS public.pipeline_executions (
424
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
425
- pipeline_id uuid NOT NULL REFERENCES public.pipelines(id) ON DELETE CASCADE,
426
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
427
- status text DEFAULT 'pending',
428
- trigger_data jsonb DEFAULT '{}'::jsonb,
429
- result jsonb DEFAULT '{}'::jsonb,
430
- error_message text,
431
- started_at timestamptz,
432
- completed_at timestamptz,
433
- duration_ms integer,
434
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
435
- created_at timestamptz DEFAULT now()
436
- );
437
-
438
- -- triggers (depends on accounts, apps, pipelines, people)
439
- CREATE TABLE IF NOT EXISTS public.triggers (
440
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
441
- app_id uuid REFERENCES public.apps(id) ON DELETE CASCADE,
442
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
443
- name text NOT NULL,
444
- description text,
445
- trigger_type text NOT NULL,
446
- event_type text,
447
- config jsonb DEFAULT '{}'::jsonb,
448
- pipeline_id uuid REFERENCES public.pipelines(id) ON DELETE SET NULL,
449
- metadata jsonb DEFAULT '{}'::jsonb,
450
- ownership text DEFAULT 'tenant',
451
- is_system boolean DEFAULT false,
452
- is_active boolean DEFAULT true,
453
- trigger_count integer DEFAULT 0,
454
- last_triggered timestamptz,
455
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
456
- created_at timestamptz DEFAULT now(),
457
- updated_at timestamptz DEFAULT now()
458
- );
459
-
460
- -- trigger_executions (depends on triggers)
461
- CREATE TABLE IF NOT EXISTS public.trigger_executions (
462
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
463
- trigger_id uuid NOT NULL REFERENCES public.triggers(id) ON DELETE CASCADE,
464
- status text DEFAULT 'pending',
465
- triggered_at timestamptz DEFAULT now(),
466
- started_at timestamptz,
467
- completed_at timestamptz,
468
- trigger_data jsonb DEFAULT '{}'::jsonb,
469
- result jsonb DEFAULT '{}'::jsonb,
470
- error_message text,
471
- duration_ms integer,
472
- created_at timestamptz DEFAULT now()
473
- );
474
-
475
- -- timers (depends on accounts, apps, pipelines, people)
476
- CREATE TABLE IF NOT EXISTS public.timers (
477
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
478
- app_id uuid REFERENCES public.apps(id) ON DELETE CASCADE,
479
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
480
- name text NOT NULL,
481
- description text,
482
- timer_type text NOT NULL,
483
- config jsonb DEFAULT '{}'::jsonb,
484
- pipeline_id uuid REFERENCES public.pipelines(id) ON DELETE SET NULL,
485
- metadata jsonb DEFAULT '{}'::jsonb,
486
- ownership text DEFAULT 'tenant',
487
- is_system boolean DEFAULT false,
488
- is_active boolean DEFAULT true,
489
- last_execution timestamptz,
490
- next_execution timestamptz,
491
- execution_count integer DEFAULT 0,
492
- success_count integer DEFAULT 0,
493
- failure_count integer DEFAULT 0,
494
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
495
- created_at timestamptz DEFAULT now(),
496
- updated_at timestamptz DEFAULT now()
497
- );
498
-
499
- -- ai_agents (depends on accounts, apps, people)
500
- CREATE TABLE IF NOT EXISTS public.ai_agents (
501
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
502
- app_id uuid REFERENCES public.apps(id) ON DELETE CASCADE,
503
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
504
- name text NOT NULL,
505
- description text,
506
- agent_type text NOT NULL,
507
- model_config jsonb DEFAULT '{}'::jsonb,
508
- system_prompt text,
509
- tools jsonb DEFAULT '[]'::jsonb,
510
- capabilities jsonb DEFAULT '[]'::jsonb,
511
- constraints jsonb DEFAULT '{}'::jsonb,
512
- metadata jsonb DEFAULT '{}'::jsonb,
513
- ownership text DEFAULT 'tenant',
514
- is_system boolean DEFAULT false,
515
- is_active boolean DEFAULT true,
516
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
517
- created_at timestamptz DEFAULT now(),
518
- updated_at timestamptz DEFAULT now()
519
- );
520
-
521
- -- embeddings (depends on accounts)
522
- CREATE TABLE IF NOT EXISTS public.embeddings (
523
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
524
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
525
- model_id text NOT NULL,
526
- document_id text NOT NULL,
527
- chunk_index integer NOT NULL,
528
- content text NOT NULL,
529
- embedding vector(1536),
530
- metadata jsonb DEFAULT '{}'::jsonb,
531
- created_at timestamptz DEFAULT now(),
532
- UNIQUE (model_id, document_id, chunk_index)
533
- );
534
-
535
- -- integrations (depends on accounts, apps, people)
536
- CREATE TABLE IF NOT EXISTS public.integrations (
537
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
538
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
539
- app_id uuid REFERENCES public.apps(id) ON DELETE CASCADE,
540
- name text NOT NULL,
541
- slug text,
542
- provider text,
543
- config jsonb DEFAULT '{}'::jsonb,
544
- credentials jsonb DEFAULT '{}'::jsonb,
545
- status text DEFAULT 'active',
546
- is_active boolean DEFAULT true,
547
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
548
- created_at timestamptz DEFAULT now(),
549
- updated_at timestamptz DEFAULT now()
550
- );
551
-
552
- -- prompt_configs (depends on accounts, apps, people)
553
- CREATE TABLE IF NOT EXISTS public.prompt_configs (
554
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
555
- app_id uuid REFERENCES public.apps(id) ON DELETE CASCADE,
556
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
557
- slug text NOT NULL,
558
- name text NOT NULL,
559
- description text,
560
- template text,
561
- variables jsonb DEFAULT '[]'::jsonb,
562
- config jsonb DEFAULT '{}'::jsonb,
563
- ownership text DEFAULT 'tenant',
564
- is_system boolean DEFAULT false,
565
- is_active boolean DEFAULT true,
566
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
567
- created_at timestamptz DEFAULT now(),
568
- updated_at timestamptz DEFAULT now()
569
- );
570
-
571
- -- api_keys (depends on accounts, integrations, people)
572
- CREATE TABLE IF NOT EXISTS public.api_keys (
573
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
574
- integration_id uuid REFERENCES public.integrations(id) ON DELETE SET NULL,
575
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
576
- name text NOT NULL,
577
- key_value text UNIQUE NOT NULL,
578
- key_prefix text NOT NULL,
579
- key_type text NOT NULL,
580
- permissions jsonb DEFAULT '[]'::jsonb,
581
- rate_limit integer DEFAULT 1000,
582
- is_active boolean DEFAULT true,
583
- expires_at timestamptz,
584
- metadata jsonb DEFAULT '{}'::jsonb,
585
- ownership text DEFAULT 'tenant',
586
- is_system boolean DEFAULT false,
587
- machine_type text,
588
- is_internal boolean DEFAULT false,
589
- scopes text[] DEFAULT '{}',
590
- usage_count integer DEFAULT 0,
591
- last_used_at timestamptz,
592
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
593
- created_at timestamptz DEFAULT now(),
594
- updated_at timestamptz DEFAULT now()
595
- );
596
-
597
- -- api_key_usage_logs (depends on api_keys, accounts)
598
- CREATE TABLE IF NOT EXISTS public.api_key_usage_logs (
599
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
600
- api_key_id uuid NOT NULL REFERENCES public.api_keys(id) ON DELETE CASCADE,
601
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
602
- request_method text,
603
- request_path text,
604
- request_ip text,
605
- user_agent text,
606
- response_status integer,
607
- response_size integer,
608
- duration_ms integer,
609
- success boolean DEFAULT true,
610
- error_message text,
611
- metadata jsonb DEFAULT '{}'::jsonb,
612
- created_at timestamptz DEFAULT now()
613
- );
614
-
615
- -- logs (depends on accounts, people)
616
- CREATE TABLE IF NOT EXISTS public.logs (
617
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
618
- level text NOT NULL,
619
- message text NOT NULL,
620
- context jsonb DEFAULT '{}'::jsonb,
621
- source text,
622
- source_type text,
623
- source_id uuid,
624
- person_id uuid REFERENCES public.people(id) ON DELETE SET NULL,
625
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
626
- metadata jsonb DEFAULT '{}'::jsonb,
627
- created_at timestamptz DEFAULT now()
628
- );
629
-
630
- -- actions (depends on accounts, people)
631
- CREATE TABLE IF NOT EXISTS public.actions (
632
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
633
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
634
- name text NOT NULL,
635
- slug text NOT NULL,
636
- description text,
637
- handler text NOT NULL,
638
- handler_module text DEFAULT 'functions',
639
- config jsonb DEFAULT '{}'::jsonb,
640
- input_schema jsonb DEFAULT '{}'::jsonb,
641
- output_schema jsonb DEFAULT '{}'::jsonb,
642
- ownership text DEFAULT 'tenant',
643
- is_system boolean DEFAULT false,
644
- default_machine_principal_id uuid,
645
- required_scopes text[] DEFAULT '{}',
646
- is_active boolean DEFAULT true,
647
- timeout_seconds integer DEFAULT 300,
648
- retry_count integer DEFAULT 3,
649
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
650
- created_at timestamptz DEFAULT now()
651
- );
652
-
653
- -- schedules (depends on accounts, actions, people)
654
- CREATE TABLE IF NOT EXISTS public.schedules (
655
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
656
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
657
- action_id uuid REFERENCES public.actions(id) ON DELETE CASCADE,
658
- name text NOT NULL,
659
- description text,
660
- cron_expression text,
661
- config jsonb DEFAULT '{}'::jsonb,
662
- machine_principal_id uuid,
663
- delegated_scopes text[] DEFAULT '{}',
664
- is_active boolean DEFAULT true,
665
- is_paused boolean DEFAULT false,
666
- pause_reason text,
667
- max_retries integer DEFAULT 3,
668
- success_count integer DEFAULT 0,
669
- failure_count integer DEFAULT 0,
670
- last_run_at timestamptz,
671
- next_run_at timestamptz,
672
- last_error text,
673
- last_error_at timestamptz,
674
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
675
- created_at timestamptz DEFAULT now(),
676
- updated_at timestamptz DEFAULT now()
677
- );
678
-
679
- -- schedule_executions (depends on accounts, schedules)
680
- CREATE TABLE IF NOT EXISTS public.schedule_executions (
681
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
682
- schedule_id uuid NOT NULL REFERENCES public.schedules(id) ON DELETE CASCADE,
683
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
684
- machine_principal_id uuid,
685
- status text DEFAULT 'pending',
686
- input_params jsonb DEFAULT '{}'::jsonb,
687
- output_result jsonb DEFAULT '{}'::jsonb,
688
- error_message text,
689
- duration_ms integer,
690
- created_at timestamptz DEFAULT now()
691
- );
692
-
693
- -- test_runs (RLS disabled — written by service-role test harness only)
694
- CREATE TABLE IF NOT EXISTS public.test_runs (
695
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
696
- suite text NOT NULL,
697
- status text NOT NULL,
698
- started_at timestamptz NOT NULL DEFAULT now(),
699
- finished_at timestamptz,
700
- duration_ms integer,
701
- total integer,
702
- passed integer,
703
- failed integer,
704
- skipped integer,
705
- triggered_by text DEFAULT 'agent',
706
- account_id uuid REFERENCES public.accounts(id) ON DELETE NO ACTION
707
- );
708
-
709
- -- test_results (RLS disabled — written by service-role test harness only)
710
- CREATE TABLE IF NOT EXISTS public.test_results (
711
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
712
- run_id uuid NOT NULL REFERENCES public.test_runs(id) ON DELETE CASCADE,
713
- suite text NOT NULL,
714
- file text,
715
- describe text,
716
- name text NOT NULL,
717
- status text NOT NULL,
718
- duration_ms integer,
719
- error text,
720
- created_at timestamptz NOT NULL DEFAULT now()
721
- );
722
-
723
- -- item_progress (depends on accounts, types, items, apps, people)
724
- CREATE TABLE IF NOT EXISTS public.item_progress (
725
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
726
- type_id uuid NOT NULL REFERENCES public.types(id) ON DELETE RESTRICT,
727
- account_id uuid NOT NULL REFERENCES public.accounts(id) ON DELETE CASCADE,
728
- app_id uuid REFERENCES public.apps(id) ON DELETE SET NULL,
729
- person_id uuid NOT NULL REFERENCES public.people(id) ON DELETE CASCADE,
730
- item_id uuid NOT NULL REFERENCES public.items(id) ON DELETE CASCADE,
731
- title text,
732
- description text,
733
- status text NOT NULL DEFAULT 'not_started',
734
- score integer,
735
- data jsonb NOT NULL DEFAULT '{}'::jsonb,
736
- is_active boolean NOT NULL DEFAULT true,
737
- design_schema jsonb NOT NULL DEFAULT '{}'::jsonb,
738
- validation_schema jsonb NOT NULL DEFAULT '{}'::jsonb,
739
- created_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
740
- updated_by uuid REFERENCES public.people(id) ON DELETE SET NULL,
741
- created_at timestamptz NOT NULL DEFAULT now(),
742
- updated_at timestamptz NOT NULL DEFAULT now()
743
- );
744
-
745
- -- webhook_handlers (depends on accounts)
746
- CREATE TABLE IF NOT EXISTS public.webhook_handlers (
747
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
748
- name varchar NOT NULL UNIQUE,
749
- function_name varchar NOT NULL,
750
- description text,
751
- events jsonb DEFAULT '[]'::jsonb,
752
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
753
- is_active boolean DEFAULT true,
754
- is_deleted boolean DEFAULT false,
755
- deleted_at timestamptz,
756
- created_at timestamptz DEFAULT now(),
757
- updated_at timestamptz DEFAULT now()
758
- );
759
-
760
- -- app_installations (depends on accounts)
761
- CREATE TABLE IF NOT EXISTS public.app_installations (
762
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
763
- account_id uuid REFERENCES public.accounts(id) ON DELETE CASCADE,
764
- app_slug varchar NOT NULL,
765
- is_enabled boolean DEFAULT true,
766
- installed_at timestamptz DEFAULT now(),
767
- updated_at timestamptz DEFAULT now(),
768
- UNIQUE (account_id, app_slug)
769
- );
770
-
771
- -- ---------------------------------------------------------------------------
772
- -- Indexes
773
- -- ---------------------------------------------------------------------------
774
-
775
- CREATE INDEX IF NOT EXISTS idx_account_paths_ancestor ON public.account_paths (ancestor_id);
776
- CREATE INDEX IF NOT EXISTS idx_account_paths_descendant ON public.account_paths (descendant_id);
777
-
778
- CREATE INDEX IF NOT EXISTS idx_accounts_parent_id ON public.accounts (parent_id);
779
- CREATE INDEX IF NOT EXISTS idx_accounts_type_id ON public.accounts (type_id);
780
- CREATE INDEX IF NOT EXISTS idx_accounts_active ON public.accounts (is_active);
781
-
782
- CREATE INDEX IF NOT EXISTS idx_actions_account ON public.actions (account_id);
783
- CREATE INDEX IF NOT EXISTS idx_actions_slug ON public.actions (slug);
784
-
785
- CREATE INDEX IF NOT EXISTS idx_ai_agents_account ON public.ai_agents (account_id);
786
- CREATE INDEX IF NOT EXISTS idx_ai_agents_app ON public.ai_agents (app_id);
787
-
788
- CREATE INDEX IF NOT EXISTS idx_akul_account ON public.api_key_usage_logs (account_id);
789
- CREATE INDEX IF NOT EXISTS idx_akul_api_key ON public.api_key_usage_logs (api_key_id);
790
- CREATE INDEX IF NOT EXISTS idx_akul_created ON public.api_key_usage_logs (created_at);
791
-
792
- CREATE INDEX IF NOT EXISTS idx_api_keys_account ON public.api_keys (account_id);
793
- CREATE INDEX IF NOT EXISTS idx_api_keys_integration ON public.api_keys (integration_id);
794
- CREATE INDEX IF NOT EXISTS idx_api_keys_key_value ON public.api_keys (key_value);
795
-
796
- CREATE INDEX IF NOT EXISTS idx_app_installations_account ON public.app_installations (account_id) WHERE (is_enabled = true);
797
- CREATE INDEX IF NOT EXISTS idx_app_installations_slug ON public.app_installations (app_slug) WHERE (is_enabled = true);
798
-
799
- CREATE INDEX IF NOT EXISTS idx_apps_owner ON public.apps (owner_account_id);
800
- CREATE INDEX IF NOT EXISTS idx_apps_slug ON public.apps (slug);
801
-
802
- CREATE INDEX IF NOT EXISTS idx_attachments_account ON public.attachments (account_id);
803
- CREATE INDEX IF NOT EXISTS idx_attachments_type ON public.attachments (type_id);
804
-
805
- CREATE INDEX IF NOT EXISTS idx_embeddings_account ON public.embeddings (account_id);
806
- CREATE INDEX IF NOT EXISTS idx_embeddings_model_doc ON public.embeddings (model_id, document_id);
807
-
808
- CREATE INDEX IF NOT EXISTS idx_integrations_account ON public.integrations (account_id);
809
- CREATE INDEX IF NOT EXISTS idx_integrations_app ON public.integrations (app_id);
810
-
811
- CREATE INDEX IF NOT EXISTS idx_item_progress_account ON public.item_progress (account_id);
812
- CREATE INDEX IF NOT EXISTS idx_item_progress_active ON public.item_progress (account_id, is_active) WHERE (is_active = true);
813
- CREATE INDEX IF NOT EXISTS idx_item_progress_data_gin ON public.item_progress USING gin (data);
814
- CREATE INDEX IF NOT EXISTS idx_item_progress_item ON public.item_progress (item_id);
815
- CREATE INDEX IF NOT EXISTS idx_item_progress_person ON public.item_progress (person_id);
816
- CREATE INDEX IF NOT EXISTS idx_item_progress_status ON public.item_progress (status);
817
- CREATE INDEX IF NOT EXISTS idx_item_progress_type ON public.item_progress (type_id);
818
-
819
- CREATE INDEX IF NOT EXISTS idx_items_account ON public.items (account_id);
820
- CREATE INDEX IF NOT EXISTS idx_items_active ON public.items (is_active);
821
- CREATE INDEX IF NOT EXISTS idx_items_created_at ON public.items (created_at);
822
- CREATE INDEX IF NOT EXISTS idx_items_type ON public.items (type_id);
823
-
824
- CREATE INDEX IF NOT EXISTS idx_link_types_slug ON public.link_types (slug);
825
-
826
- CREATE INDEX IF NOT EXISTS idx_links_account ON public.links (account_id);
827
- CREATE INDEX IF NOT EXISTS idx_links_link_type ON public.links (link_type_id);
828
- CREATE INDEX IF NOT EXISTS idx_links_source ON public.links (source_type, source_id);
829
- CREATE INDEX IF NOT EXISTS idx_links_target ON public.links (target_type, target_id);
830
-
831
- CREATE INDEX IF NOT EXISTS idx_logs_account ON public.logs (account_id);
832
- CREATE INDEX IF NOT EXISTS idx_logs_created ON public.logs (created_at);
833
-
834
- CREATE INDEX IF NOT EXISTS idx_messages_thread ON public.messages (thread_id);
835
-
836
- CREATE INDEX IF NOT EXISTS idx_people_account ON public.people (account_id);
837
- CREATE INDEX IF NOT EXISTS idx_people_role ON public.people (role_id);
838
-
839
- CREATE INDEX IF NOT EXISTS idx_pe_account ON public.pipeline_executions (account_id);
840
- CREATE INDEX IF NOT EXISTS idx_pe_pipeline ON public.pipeline_executions (pipeline_id);
841
- CREATE INDEX IF NOT EXISTS idx_pe_status ON public.pipeline_executions (status);
842
-
843
- CREATE INDEX IF NOT EXISTS idx_pipelines_account ON public.pipelines (account_id);
844
- CREATE INDEX IF NOT EXISTS idx_pipelines_app ON public.pipelines (app_id);
845
-
846
- CREATE INDEX IF NOT EXISTS idx_prompt_configs_account ON public.prompt_configs (account_id);
847
- CREATE INDEX IF NOT EXISTS idx_prompt_configs_app ON public.prompt_configs (app_id);
848
- CREATE INDEX IF NOT EXISTS idx_prompt_configs_slug ON public.prompt_configs (slug);
849
-
850
- CREATE INDEX IF NOT EXISTS idx_roles_slug ON public.roles (slug);
851
-
852
- CREATE INDEX IF NOT EXISTS idx_se_account ON public.schedule_executions (account_id);
853
- CREATE INDEX IF NOT EXISTS idx_se_schedule ON public.schedule_executions (schedule_id);
854
-
855
- CREATE INDEX IF NOT EXISTS idx_schedules_account ON public.schedules (account_id);
856
- CREATE INDEX IF NOT EXISTS idx_schedules_action ON public.schedules (action_id);
857
- CREATE INDEX IF NOT EXISTS idx_schedules_next_run ON public.schedules (next_run_at);
858
-
859
- CREATE INDEX IF NOT EXISTS idx_test_results_run_id ON public.test_results (run_id);
860
- CREATE INDEX IF NOT EXISTS idx_test_runs_started_at ON public.test_runs (started_at DESC);
861
- CREATE INDEX IF NOT EXISTS idx_test_runs_suite ON public.test_runs (suite);
862
-
863
- CREATE INDEX IF NOT EXISTS idx_threads_account ON public.threads (account_id);
864
- CREATE INDEX IF NOT EXISTS idx_threads_active ON public.threads (is_active);
865
- CREATE INDEX IF NOT EXISTS idx_threads_target ON public.threads (target_type, target_id);
866
- CREATE INDEX IF NOT EXISTS idx_threads_type ON public.threads (type_id);
867
-
868
- CREATE INDEX IF NOT EXISTS idx_timers_account ON public.timers (account_id);
869
- CREATE INDEX IF NOT EXISTS idx_timers_app ON public.timers (app_id);
870
- CREATE INDEX IF NOT EXISTS idx_timers_pipeline ON public.timers (pipeline_id);
871
-
872
- CREATE INDEX IF NOT EXISTS idx_te_trigger ON public.trigger_executions (trigger_id);
873
-
874
- CREATE INDEX IF NOT EXISTS idx_triggers_account ON public.triggers (account_id);
875
- CREATE INDEX IF NOT EXISTS idx_triggers_app ON public.triggers (app_id);
876
- CREATE INDEX IF NOT EXISTS idx_triggers_pipeline ON public.triggers (pipeline_id);
877
-
878
- CREATE INDEX IF NOT EXISTS idx_types_kind ON public.types (kind);
879
- CREATE INDEX IF NOT EXISTS idx_types_slug ON public.types (slug);
880
-
881
- CREATE INDEX IF NOT EXISTS idx_watchers_person ON public.watchers (person_id);
882
- CREATE INDEX IF NOT EXISTS idx_watchers_target ON public.watchers (target_type, target_id);
883
- CREATE INDEX IF NOT EXISTS idx_watchers_type ON public.watchers (type_id);
884
-
885
- CREATE INDEX IF NOT EXISTS idx_webhook_handlers_account ON public.webhook_handlers (account_id) WHERE (is_deleted = false);
886
- CREATE INDEX IF NOT EXISTS idx_webhook_handlers_events ON public.webhook_handlers USING gin (events) WHERE (is_active = true);
887
- CREATE INDEX IF NOT EXISTS idx_webhook_handlers_name ON public.webhook_handlers (name) WHERE ((is_active = true) AND (is_deleted = false));
888
-
889
- -- ---------------------------------------------------------------------------
890
- -- Triggers
891
- -- ---------------------------------------------------------------------------
892
-
893
- CREATE OR REPLACE TRIGGER account_paths_trigger
894
- AFTER INSERT OR UPDATE OR DELETE ON public.accounts
895
- FOR EACH ROW EXECUTE FUNCTION public.update_account_paths();
896
-
897
- CREATE OR REPLACE TRIGGER app_installations_updated_at
898
- BEFORE UPDATE ON public.app_installations
899
- FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
900
-
901
- CREATE OR REPLACE TRIGGER webhook_handlers_updated_at
902
- BEFORE UPDATE ON public.webhook_handlers
903
- FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
904
-
905
- -- ---------------------------------------------------------------------------
906
- -- Row Level Security
907
- -- ---------------------------------------------------------------------------
908
-
909
- -- Enable RLS on all tables (account_paths, test_runs, test_results are explicitly skipped)
910
- ALTER TABLE public.accounts ENABLE ROW LEVEL SECURITY;
911
- ALTER TABLE public.actions ENABLE ROW LEVEL SECURITY;
912
- ALTER TABLE public.ai_agents ENABLE ROW LEVEL SECURITY;
913
- ALTER TABLE public.api_key_usage_logs ENABLE ROW LEVEL SECURITY;
914
- ALTER TABLE public.api_keys ENABLE ROW LEVEL SECURITY;
915
- ALTER TABLE public.app_installations ENABLE ROW LEVEL SECURITY;
916
- ALTER TABLE public.apps ENABLE ROW LEVEL SECURITY;
917
- ALTER TABLE public.attachments ENABLE ROW LEVEL SECURITY;
918
- ALTER TABLE public.embeddings ENABLE ROW LEVEL SECURITY;
919
- ALTER TABLE public.integrations ENABLE ROW LEVEL SECURITY;
920
- ALTER TABLE public.item_progress ENABLE ROW LEVEL SECURITY;
921
- ALTER TABLE public.items ENABLE ROW LEVEL SECURITY;
922
- ALTER TABLE public.link_types ENABLE ROW LEVEL SECURITY;
923
- ALTER TABLE public.links ENABLE ROW LEVEL SECURITY;
924
- ALTER TABLE public.logs ENABLE ROW LEVEL SECURITY;
925
- ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
926
- ALTER TABLE public.people ENABLE ROW LEVEL SECURITY;
927
- ALTER TABLE public.pipeline_executions ENABLE ROW LEVEL SECURITY;
928
- ALTER TABLE public.pipelines ENABLE ROW LEVEL SECURITY;
929
- ALTER TABLE public.prompt_configs ENABLE ROW LEVEL SECURITY;
930
- ALTER TABLE public.roles ENABLE ROW LEVEL SECURITY;
931
- ALTER TABLE public.schedule_executions ENABLE ROW LEVEL SECURITY;
932
- ALTER TABLE public.schedules ENABLE ROW LEVEL SECURITY;
933
- ALTER TABLE public.threads ENABLE ROW LEVEL SECURITY;
934
- ALTER TABLE public.timers ENABLE ROW LEVEL SECURITY;
935
- ALTER TABLE public.trigger_executions ENABLE ROW LEVEL SECURITY;
936
- ALTER TABLE public.triggers ENABLE ROW LEVEL SECURITY;
937
- ALTER TABLE public.types ENABLE ROW LEVEL SECURITY;
938
- ALTER TABLE public.watchers ENABLE ROW LEVEL SECURITY;
939
- ALTER TABLE public.webhook_handlers ENABLE ROW LEVEL SECURITY;
940
-
941
- -- NOTE: account_paths, test_runs, test_results have RLS intentionally disabled.
942
- -- account_paths is managed exclusively by trigger (update_account_paths).
943
- -- test_runs/test_results are written by service_role from the test harness.
944
-
945
- -- RLS Policies (idempotent - uses DO blocks to handle existing policies)
946
-
947
- DO $$ BEGIN CREATE POLICY accounts_access ON public.accounts FOR ALL TO public
948
- USING ((id IN (SELECT get_accessible_accounts(current_actor_id()))) OR current_actor_id() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
949
-
950
- DO $$ BEGIN CREATE POLICY actions_access ON public.actions FOR ALL TO public
951
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
952
-
953
- DO $$ BEGIN CREATE POLICY ai_agents_access ON public.ai_agents FOR ALL TO public
954
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
955
-
956
- DO $$ BEGIN CREATE POLICY akul_access ON public.api_key_usage_logs FOR ALL TO public
957
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
958
-
959
- DO $$ BEGIN CREATE POLICY api_keys_access ON public.api_keys FOR ALL TO public
960
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR (id = auth.uid()) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
961
-
962
- DO $$ BEGIN CREATE POLICY app_installations_select ON public.app_installations FOR SELECT TO authenticated USING (true); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
963
- DO $$ BEGIN CREATE POLICY app_installations_admin ON public.app_installations FOR ALL TO authenticated
964
- USING ((SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin')
965
- WITH CHECK ((SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin'); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
966
- DO $$ BEGIN CREATE POLICY app_installations_service ON public.app_installations FOR ALL TO service_role USING (true) WITH CHECK (true); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
967
-
968
- DO $$ BEGIN CREATE POLICY apps_access ON public.apps FOR ALL TO public
969
- USING ((is_system = true) OR (owner_account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
970
-
971
- DO $$ BEGIN CREATE POLICY attachments_access ON public.attachments FOR ALL TO public
972
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR ((design_schema->>'scope' = 'platform') AND auth.uid() IS NOT NULL) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
973
-
974
- DO $$ BEGIN CREATE POLICY embeddings_access ON public.embeddings FOR ALL TO public
975
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
976
-
977
- DO $$ BEGIN CREATE POLICY integrations_access ON public.integrations FOR ALL TO public
978
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
979
-
980
- DO $$ BEGIN CREATE POLICY item_progress_access ON public.item_progress FOR ALL TO public
981
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
982
-
983
- DO $$ BEGIN CREATE POLICY items_access ON public.items FOR ALL TO public
984
- USING ((account_id IN (SELECT get_accessible_accounts(current_actor_id()))) OR ((design_schema->>'scope' = 'platform') AND current_actor_id() IS NOT NULL) OR current_actor_id() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
985
-
986
- DO $$ BEGIN CREATE POLICY link_types_access ON public.link_types FOR ALL TO public
987
- USING ((app_id IS NULL) OR (EXISTS (SELECT 1 FROM apps a WHERE a.id = link_types.app_id AND a.owner_account_id IN (SELECT get_accessible_accounts(auth.uid())))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
988
-
989
- DO $$ BEGIN CREATE POLICY links_access ON public.links FOR ALL TO public
990
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR ((design_schema->>'scope' = 'platform') AND auth.uid() IS NOT NULL) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
991
-
992
- DO $$ BEGIN CREATE POLICY logs_access ON public.logs FOR ALL TO public
993
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
994
-
995
- DO $$ BEGIN CREATE POLICY messages_access ON public.messages FOR ALL TO public
996
- USING ((EXISTS (SELECT 1 FROM threads t WHERE t.id = messages.thread_id AND (t.account_id IN (SELECT get_accessible_accounts(current_actor_id())) OR (t.design_schema->>'scope' = 'platform' AND current_actor_id() IS NOT NULL)))) OR (design_schema->>'scope' = 'platform' AND current_actor_id() IS NOT NULL) OR current_actor_id() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
997
-
998
- DO $$ BEGIN CREATE POLICY people_access ON public.people FOR ALL TO public
999
- USING ((id = current_actor_id()) OR (account_id IN (SELECT get_accessible_accounts(current_actor_id()))) OR current_actor_id() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1000
-
1001
- DO $$ BEGIN CREATE POLICY pe_access ON public.pipeline_executions FOR ALL TO public
1002
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1003
-
1004
- DO $$ BEGIN CREATE POLICY pipelines_access ON public.pipelines FOR ALL TO public
1005
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1006
-
1007
- DO $$ BEGIN CREATE POLICY prompt_configs_access ON public.prompt_configs FOR ALL TO public
1008
- USING ((app_id IS NULL) OR (EXISTS (SELECT 1 FROM apps a WHERE a.id = prompt_configs.app_id AND a.owner_account_id IN (SELECT get_accessible_accounts(auth.uid())))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1009
-
1010
- DO $$ BEGIN CREATE POLICY roles_read ON public.roles FOR SELECT TO public USING (true); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1011
- DO $$ BEGIN CREATE POLICY roles_modify ON public.roles FOR ALL TO public
1012
- USING (((is_system = true) AND person_is_system_admin(current_actor_id())) OR (is_system = false) OR current_actor_id() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1013
-
1014
- DO $$ BEGIN CREATE POLICY se_access ON public.schedule_executions FOR ALL TO public
1015
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1016
-
1017
- DO $$ BEGIN CREATE POLICY schedules_access ON public.schedules FOR ALL TO public
1018
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1019
-
1020
- DO $$ BEGIN CREATE POLICY threads_access ON public.threads FOR ALL TO public
1021
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR (design_schema->>'scope' = 'platform' AND auth.uid() IS NOT NULL) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1022
-
1023
- DO $$ BEGIN CREATE POLICY timers_access ON public.timers FOR ALL TO public
1024
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1025
-
1026
- DO $$ BEGIN CREATE POLICY te_access ON public.trigger_executions FOR ALL TO public
1027
- USING ((EXISTS (SELECT 1 FROM triggers t WHERE t.id = trigger_executions.trigger_id AND t.account_id IN (SELECT get_accessible_accounts(auth.uid())))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1028
-
1029
- DO $$ BEGIN CREATE POLICY triggers_access ON public.triggers FOR ALL TO public
1030
- USING ((account_id IN (SELECT get_accessible_accounts(auth.uid()))) OR auth.uid() IS NULL); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1031
-
1032
- DO $$ BEGIN CREATE POLICY types_access ON public.types FOR ALL TO public
1033
- USING (current_actor_id() IS NOT NULL OR true); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1034
-
1035
- DO $$ BEGIN CREATE POLICY watchers_access ON public.watchers FOR ALL TO public
1036
- USING (auth.uid() IS NOT NULL OR true); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1037
-
1038
- DO $$ BEGIN CREATE POLICY webhook_handlers_service_role ON public.webhook_handlers FOR ALL TO service_role USING (true) WITH CHECK (true); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1039
- DO $$ BEGIN CREATE POLICY webhook_handlers_system_admin ON public.webhook_handlers FOR ALL TO authenticated
1040
- USING ((SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin')
1041
- WITH CHECK ((SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin'); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
1042
-
1043
- -- ---------------------------------------------------------------------------
1044
- -- Additional Functions (depend on tables existing)
1045
- -- ---------------------------------------------------------------------------
1046
-
1047
- CREATE OR REPLACE FUNCTION public.get_account_ancestors(account_id uuid)
1048
- RETURNS TABLE(id uuid, slug text, display_name text, depth integer) LANGUAGE plpgsql AS $$
1049
- BEGIN
1050
- RETURN QUERY
1051
- SELECT a.id, a.slug, a.display_name, ap.depth
1052
- FROM public.accounts a
1053
- JOIN public.account_paths ap ON a.id = ap.ancestor_id
1054
- WHERE ap.descendant_id = account_id
1055
- ORDER BY ap.depth;
1056
- END;
1057
- $$;
1058
-
1059
- CREATE OR REPLACE FUNCTION public.get_account_descendants(account_id uuid, max_depth integer DEFAULT NULL)
1060
- RETURNS TABLE(id uuid, slug text, display_name text, depth integer) LANGUAGE plpgsql AS $$
1061
- BEGIN
1062
- RETURN QUERY
1063
- WITH RECURSIVE descendants AS (
1064
- SELECT a.id, a.slug, a.display_name, 0 as depth
1065
- FROM public.accounts a WHERE a.id = account_id
1066
- UNION ALL
1067
- SELECT a.id, a.slug, a.display_name, ap.depth + 1
1068
- FROM public.accounts a
1069
- JOIN public.account_paths ap ON a.id = ap.descendant_id
1070
- WHERE ap.ancestor_id = account_id
1071
- AND (max_depth IS NULL OR ap.depth < max_depth)
1072
- )
1073
- SELECT d.id, d.slug, d.display_name, d.depth FROM descendants d WHERE d.id != account_id ORDER BY d.depth, d.display_name;
1074
- END;
1075
- $$;
1076
-
1077
- CREATE OR REPLACE FUNCTION public.get_account_hierarchy(parent_account_id uuid)
1078
- RETURNS TABLE(id uuid, slug text, display_name text, level integer) LANGUAGE plpgsql AS $$
1079
- BEGIN
1080
- RETURN QUERY
1081
- WITH RECURSIVE account_tree AS (
1082
- SELECT a.id, a.slug, a.display_name, 1 as level FROM public.accounts a
1083
- WHERE a.parent_id = parent_account_id AND a.is_active = true
1084
- UNION ALL
1085
- SELECT a.id, a.slug, a.display_name, at.level + 1
1086
- FROM public.accounts a INNER JOIN account_tree at ON a.parent_id = at.id
1087
- WHERE a.is_active = true
1088
- )
1089
- SELECT id, slug, display_name, level FROM account_tree ORDER BY level, display_name;
1090
- END;
1091
- $$;
1092
-
1093
- CREATE OR REPLACE FUNCTION public.get_account_apps(account_id uuid, include_system boolean DEFAULT true, include_inactive boolean DEFAULT false)
1094
- RETURNS TABLE(id uuid, slug text, name text, description text, icon text, color text, version text, app_type text, source text, owner_account_id uuid, is_active boolean, is_system boolean, min_role text, config jsonb, nav_items jsonb, route_prefix text, renderer text, created_at timestamptz) LANGUAGE plpgsql AS $$
1095
- BEGIN
1096
- RETURN QUERY
1097
- SELECT a.id, a.slug, a.name, a.description, a.icon, a.color, a.version, a.app_type, a.source,
1098
- a.owner_account_id, a.is_active, a.is_system, a.min_role, a.config, a.nav_items, a.route_prefix, a.renderer, a.created_at
1099
- FROM public.apps a
1100
- WHERE (include_system OR a.is_system = false)
1101
- AND (include_inactive OR a.is_active = true)
1102
- AND (a.is_system OR a.owner_account_id = get_account_apps.account_id)
1103
- ORDER BY a.is_system DESC, a.app_type, a.name;
1104
- END;
1105
- $$;
1106
-
1107
- CREATE OR REPLACE FUNCTION public.get_app_manifest(app_slug text)
1108
- RETURNS jsonb LANGUAGE plpgsql AS $$
1109
- DECLARE manifest_path TEXT;
1110
- BEGIN
1111
- SELECT a.manifest_path INTO manifest_path FROM public.apps a WHERE a.slug = app_slug;
1112
- IF manifest_path IS NULL THEN RETURN NULL; END IF;
1113
- RETURN jsonb_build_object('source', 'manifest', 'path', manifest_path, 'note', 'Manifest content loaded by application layer');
1114
- END;
1115
- $$;
1116
-
1117
- CREATE OR REPLACE FUNCTION public.get_app_schema(app_slug text)
1118
- RETURNS jsonb LANGUAGE plpgsql AS $$
1119
- DECLARE app_schema jsonb;
1120
- BEGIN
1121
- SELECT jsonb_build_object('id', id, 'slug', slug, 'name', name, 'description', description, 'icon', icon, 'color', color, 'version', version, 'app_type', app_type, 'source', source, 'config', config, 'nav_items', nav_items, 'min_role', min_role, 'integration_deps', integration_deps, 'metadata', metadata)
1122
- INTO app_schema FROM public.apps WHERE slug = get_app_schema.app_slug AND is_active = true;
1123
- RETURN COALESCE(app_schema, '{}'::jsonb);
1124
- END;
1125
- $$;
1126
-
1127
- CREATE OR REPLACE FUNCTION public.is_app_available(app_slug text, account_id uuid)
1128
- RETURNS boolean LANGUAGE plpgsql AS $$
1129
- BEGIN
1130
- RETURN EXISTS (SELECT 1 FROM public.apps WHERE slug = is_app_available.app_slug AND is_active = true AND (is_system = true OR owner_account_id = is_app_available.account_id));
1131
- END;
1132
- $$;
1133
-
1134
- CREATE OR REPLACE FUNCTION public.get_type_schema(kind text, slug text, app_id uuid DEFAULT NULL)
1135
- RETURNS jsonb LANGUAGE plpgsql AS $$
1136
- DECLARE type_schema jsonb;
1137
- BEGIN
1138
- SELECT design_schema INTO type_schema FROM public.types
1139
- WHERE kind = get_type_schema.kind AND slug = get_type_schema.slug
1140
- AND (app_id = get_type_schema.app_id OR (app_id IS NULL AND get_type_schema.app_id IS NULL))
1141
- AND is_active = true
1142
- ORDER BY app_id DESC NULLS LAST LIMIT 1;
1143
- RETURN COALESCE(type_schema, '{}');
1144
- END;
1145
- $$;
1146
-
1147
- CREATE OR REPLACE FUNCTION public.increment_trigger_count(p_trigger_id uuid)
1148
- RETURNS integer LANGUAGE plpgsql AS $$
1149
- DECLARE v_count integer;
1150
- BEGIN
1151
- UPDATE public.triggers SET trigger_count = COALESCE(trigger_count, 0) + 1 WHERE id = p_trigger_id RETURNING trigger_count INTO v_count;
1152
- RETURN v_count;
1153
- END;
1154
- $$;
1155
-
1156
- CREATE OR REPLACE FUNCTION public.update_app_version(app_id uuid, new_version text)
1157
- RETURNS void LANGUAGE plpgsql AS $$
1158
- BEGIN
1159
- UPDATE public.apps SET version = new_version, updated_at = now() WHERE id = update_app_version.app_id;
1160
- END;
1161
- $$;
1162
-
1163
- CREATE OR REPLACE FUNCTION public.generate_api_key(key_type text, key_prefix text DEFAULT 'sp_')
1164
- RETURNS text LANGUAGE plpgsql AS $$
1165
- DECLARE key_value text; key_suffix text;
1166
- BEGIN
1167
- key_suffix := encode(gen_random_bytes(24), 'base64');
1168
- key_suffix := replace(replace(replace(key_suffix, '/', ''), '+', ''), '=', '');
1169
- key_suffix := substring(key_suffix, 1, 32);
1170
- key_value := key_prefix || CASE key_type WHEN 'public' THEN 'pk_' WHEN 'secret' THEN 'sc_' WHEN 'webhook' THEN 'wh_' ELSE 'sk_' END || key_suffix;
1171
- RETURN key_value;
1172
- END;
1173
- $$;
1174
-
1175
- CREATE OR REPLACE FUNCTION public.get_due_schedules(p_now timestamptz DEFAULT now())
1176
- RETURNS TABLE(id uuid, account_id uuid, action_id uuid, machine_principal_id uuid, config jsonb, delegated_scopes text[]) LANGUAGE plpgsql AS $$
1177
- BEGIN
1178
- RETURN QUERY SELECT s.id, s.account_id, s.action_id, s.machine_principal_id, s.config, s.delegated_scopes
1179
- FROM public.schedules s WHERE s.is_active = true AND s.is_paused = false AND (s.next_run_at IS NULL OR s.next_run_at <= p_now);
1180
- END;
1181
- $$;
1182
-
1183
- CREATE OR REPLACE FUNCTION public.get_due_timers(p_now timestamptz DEFAULT now())
1184
- RETURNS TABLE(id uuid, account_id uuid, app_id uuid, name text, pipeline_id uuid, created_by uuid, execution_count integer, next_execution timestamptz) LANGUAGE plpgsql AS $$
1185
- BEGIN
1186
- RETURN QUERY SELECT t.id, t.account_id, t.app_id, t.name, t.pipeline_id, t.created_by, t.execution_count, t.next_execution
1187
- FROM public.timers t WHERE t.is_active = true AND t.next_execution IS NOT NULL AND t.next_execution <= p_now;
1188
- END;
1189
- $$;
1190
-
1191
- CREATE OR REPLACE FUNCTION public.validate_machine_principal(p_key_value text, p_required_scope text DEFAULT NULL)
1192
- RETURNS TABLE(is_valid boolean, machine_id uuid, account_id uuid, scopes text[], machine_type text, is_internal boolean, created_by uuid, error_message text) LANGUAGE plpgsql AS $$
1193
- DECLARE
1194
- key_record RECORD;
1195
- BEGIN
1196
- SELECT * INTO key_record
1197
- FROM public.api_keys
1198
- WHERE key_value = p_key_value AND is_active = true
1199
- AND (expires_at IS NULL OR expires_at > now());
1200
-
1201
- IF NOT FOUND THEN
1202
- RETURN QUERY SELECT false, NULL::uuid, NULL::uuid, NULL::text[],
1203
- NULL::text, NULL::boolean, NULL::uuid, 'Invalid or inactive machine principal'::text;
1204
- RETURN;
1205
- END IF;
1206
-
1207
- IF p_required_scope IS NOT NULL AND
1208
- NOT (p_required_scope = ANY(key_record.scopes) OR '*:*' = ANY(key_record.scopes)) THEN
1209
- RETURN QUERY SELECT false, NULL::uuid, NULL::uuid, NULL::text[],
1210
- NULL::text, NULL::boolean, NULL::uuid, ('Insufficient scope: ' || p_required_scope)::text;
1211
- RETURN;
1212
- END IF;
1213
-
1214
- UPDATE public.api_keys SET last_used_at = now(), usage_count = COALESCE(usage_count, 0) + 1 WHERE id = key_record.id;
1215
-
1216
- RETURN QUERY SELECT true, key_record.id, key_record.account_id, key_record.scopes,
1217
- key_record.machine_type, key_record.is_internal, key_record.created_by, NULL::text;
1218
- END;
1219
- $$;
1220
-
1221
- CREATE OR REPLACE FUNCTION public.validate_schedule_creator(p_schedule_id uuid)
1222
- RETURNS TABLE(is_valid boolean, error_message text) LANGUAGE plpgsql AS $$
1223
- DECLARE sched RECORD; creator RECORD;
1224
- BEGIN
1225
- SELECT * INTO sched FROM public.schedules WHERE id = p_schedule_id;
1226
- IF NOT FOUND THEN RETURN QUERY SELECT false, 'Schedule not found'::text; RETURN; END IF;
1227
-
1228
- SELECT is_active INTO creator FROM public.people WHERE id = sched.created_by;
1229
- IF creator IS NULL OR NOT creator.is_active THEN
1230
- UPDATE public.schedules SET is_paused = true, pause_reason = 'Creator deactivated' WHERE id = p_schedule_id;
1231
- RETURN QUERY SELECT false, 'Schedule creator deactivated; schedule paused'::text; RETURN;
1232
- END IF;
1233
-
1234
- RETURN QUERY SELECT true, NULL::text;
1235
- END;
1236
- $$;
1237
-
1238
- CREATE OR REPLACE FUNCTION public.cleanup_old_logs(p_retention_days integer DEFAULT 30)
1239
- RETURNS TABLE(deleted_count bigint) LANGUAGE plpgsql AS $$
1240
- BEGIN
1241
- RETURN QUERY WITH deleted AS (DELETE FROM logs WHERE created_at < NOW() - INTERVAL '1 day' * p_retention_days RETURNING id)
1242
- SELECT COUNT(*)::bigint FROM deleted;
1243
- END;
1244
- $$;
1245
-
1246
- CREATE OR REPLACE FUNCTION public.match_embeddings(query_embedding vector, match_count integer DEFAULT 10, similarity_threshold float DEFAULT 0.7, filter_account_ids uuid[] DEFAULT NULL, filter_vector_type text DEFAULT 'document')
1247
- RETURNS TABLE(id uuid, document_id text, content text, metadata jsonb, account_id uuid, similarity float) LANGUAGE plpgsql AS $$
1248
- BEGIN
1249
- RETURN QUERY SELECT e.id, e.document_id, e.content, e.metadata, e.account_id, 1 - (e.embedding <=> query_embedding) AS similarity
1250
- FROM public.embeddings e
1251
- WHERE (filter_account_ids IS NULL OR e.account_id = ANY(filter_account_ids))
1252
- AND e.metadata->>'vector_type' = filter_vector_type
1253
- AND 1 - (e.embedding <=> query_embedding) >= similarity_threshold
1254
- ORDER BY e.embedding <=> query_embedding LIMIT match_count;
1255
- END;
1256
- $$;
1257
-
1258
- CREATE OR REPLACE FUNCTION public.search_similar_embeddings(p_account_id uuid, p_model_id text, p_query_embedding vector, p_threshold float DEFAULT 0.7, p_limit integer DEFAULT 10)
1259
- RETURNS TABLE(id uuid, content text, similarity float, metadata jsonb, document_id text) LANGUAGE plpgsql AS $$
1260
- BEGIN
1261
- RETURN QUERY SELECT e.id, e.content, (1 - (e.embedding <=> p_query_embedding))::float as similarity, e.metadata, e.document_id
1262
- FROM public.embeddings e
1263
- WHERE e.account_id = p_account_id AND e.model_id = p_model_id AND 1 - (e.embedding <=> p_query_embedding) > p_threshold
1264
- ORDER BY e.embedding <=> p_query_embedding LIMIT p_limit;
1265
- END;
1266
- $$;