@engenty/wizards-runtime 0.12.2 → 0.14.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 (328) hide show
  1. package/apps/runtime/dist/agents/architect.js +8 -7
  2. package/apps/runtime/dist/agents/goose-connections.js +82 -0
  3. package/apps/runtime/dist/agents/space-agent.js +9 -2
  4. package/apps/runtime/dist/agents/studio-assistant.js +126 -71
  5. package/apps/runtime/dist/agents/website.js +1 -1
  6. package/apps/runtime/dist/app.js +28 -3
  7. package/apps/runtime/dist/assistant/knows.js +6 -5
  8. package/apps/runtime/dist/authoring/guide.js +25 -5
  9. package/apps/runtime/dist/broker/account.js +166 -0
  10. package/apps/runtime/dist/broker/call.js +417 -0
  11. package/apps/runtime/dist/broker/caller.js +10 -0
  12. package/apps/runtime/dist/broker/grant.js +152 -0
  13. package/apps/runtime/dist/broker/host.js +51 -0
  14. package/apps/runtime/dist/broker/index.js +8 -0
  15. package/apps/runtime/dist/broker/kept.js +31 -0
  16. package/apps/runtime/dist/broker/log.js +240 -0
  17. package/apps/runtime/dist/broker/registry.js +9 -0
  18. package/apps/runtime/dist/broker/rwx.js +367 -0
  19. package/apps/runtime/dist/broker/send.js +189 -0
  20. package/apps/runtime/dist/connectors/builtin.js +1 -1
  21. package/apps/runtime/dist/connectors/external.js +38 -37
  22. package/apps/runtime/dist/connectors/index.js +30 -91
  23. package/apps/runtime/dist/connectors/mail.js +0 -8
  24. package/apps/runtime/dist/connectors/person-sign-ins.js +73 -0
  25. package/apps/runtime/dist/connectors/plugin-connections.js +65 -92
  26. package/apps/runtime/dist/connectors/smtp.js +1 -1
  27. package/apps/runtime/dist/connectors/space-connections.js +436 -0
  28. package/apps/runtime/dist/credits/estimate.js +3 -3
  29. package/apps/runtime/dist/db/adopt.js +3 -3
  30. package/apps/runtime/dist/db/client.js +9 -0
  31. package/apps/runtime/dist/db/live-triggers.js +65 -23
  32. package/apps/runtime/dist/db/migrations/tenant/0027_work_chats.sql +52 -0
  33. package/apps/runtime/dist/db/migrations/tenant/0028_chat_approvals.sql +17 -0
  34. package/apps/runtime/dist/db/migrations/tenant/0029_work_chats_live.sql +49 -0
  35. package/apps/runtime/dist/db/migrations/tenant/0031_space.sql +624 -0
  36. package/apps/runtime/dist/db/migrations/tenant/0032_rwx_rule.sql +18 -0
  37. package/apps/runtime/dist/db/migrations/tenant/0033_broker_log.sql +23 -0
  38. package/apps/runtime/dist/db/migrations/tenant/0034_space_connection.sql +35 -0
  39. package/apps/runtime/dist/db/migrations/tenant/0035_run_grant.sql +1 -0
  40. package/apps/runtime/dist/db/migrations/tenant/0036_approval_held_for.sql +1 -0
  41. package/apps/runtime/dist/db/migrations/tenant/0037_ways_out.sql +5 -0
  42. package/apps/runtime/dist/db/migrations/tenant/0038_heard.sql +11 -0
  43. package/apps/runtime/dist/db/migrations/tenant/0039_members_studio.sql +1 -0
  44. package/apps/runtime/dist/db/migrations/tenant/meta/0027_snapshot.json +383 -155
  45. package/apps/runtime/dist/db/migrations/tenant/meta/0028_snapshot.json +4468 -0
  46. package/apps/runtime/dist/db/migrations/tenant/meta/0029_snapshot.json +4475 -0
  47. package/apps/runtime/dist/db/migrations/tenant/meta/0030_snapshot.json +4629 -0
  48. package/apps/runtime/dist/db/migrations/tenant/meta/0031_snapshot.json +4629 -0
  49. package/apps/runtime/dist/db/migrations/tenant/meta/0032_snapshot.json +4757 -0
  50. package/apps/runtime/dist/db/migrations/tenant/meta/0033_snapshot.json +5039 -0
  51. package/apps/runtime/dist/db/migrations/tenant/meta/0034_snapshot.json +5022 -0
  52. package/apps/runtime/dist/db/migrations/tenant/meta/0035_snapshot.json +5029 -0
  53. package/apps/runtime/dist/db/migrations/tenant/meta/0036_snapshot.json +5037 -0
  54. package/apps/runtime/dist/db/migrations/tenant/meta/0037_snapshot.json +5076 -0
  55. package/apps/runtime/dist/db/migrations/tenant/meta/0038_snapshot.json +5145 -0
  56. package/apps/runtime/dist/db/migrations/tenant/meta/0039_snapshot.json +5153 -0
  57. package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +86 -2
  58. package/apps/runtime/dist/db/schema.js +295 -72
  59. package/apps/runtime/dist/docs/dev/architecture.md +215 -0
  60. package/apps/runtime/dist/docs/dev/broker.md +298 -0
  61. package/apps/runtime/dist/docs/dev/index.md +5 -1
  62. package/apps/runtime/dist/docs/dev/integration/index.md +2 -0
  63. package/apps/runtime/dist/docs/dev/meta.json +3 -0
  64. package/apps/runtime/dist/docs/dev/plugins/connections.md +8 -5
  65. package/apps/runtime/dist/docs/dev/plugins/events.md +1 -1
  66. package/apps/runtime/dist/docs/dev/plugins/examples.md +1 -0
  67. package/apps/runtime/dist/docs/dev/plugins/index.md +2 -2
  68. package/apps/runtime/dist/docs/dev/plugins/reference.md +3 -2
  69. package/apps/runtime/dist/docs/dev/plugins/space.md +16 -6
  70. package/apps/runtime/dist/docs/dev/plugins/studio.md +2 -2
  71. package/apps/runtime/dist/docs/dev/plugins/tools.md +0 -1
  72. package/apps/runtime/dist/docs/dev/skills.md +16 -9
  73. package/apps/runtime/dist/docs/dev/wizard-format/agent-steps.md +59 -6
  74. package/apps/runtime/dist/docs/dev/wizard-format/lists-and-connections.md +17 -2
  75. package/apps/runtime/dist/docs/dev/wizard-format/templates.md +2 -0
  76. package/apps/runtime/dist/docs/dev/wizard-format/views-and-widgets.md +22 -2
  77. package/apps/runtime/dist/docs/user-guide/capabilities.md +132 -0
  78. package/apps/runtime/dist/docs/user-guide/cloud-sync.md +2 -0
  79. package/apps/runtime/dist/docs/user-guide/command-line.md +1 -0
  80. package/apps/runtime/dist/docs/user-guide/connectors.md +63 -7
  81. package/apps/runtime/dist/docs/user-guide/index.md +10 -1
  82. package/apps/runtime/dist/docs/user-guide/meta.json +6 -1
  83. package/apps/runtime/dist/docs/user-guide/models.md +105 -30
  84. package/apps/runtime/dist/docs/user-guide/protocol.md +80 -0
  85. package/apps/runtime/dist/docs/user-guide/skills.md +8 -4
  86. package/apps/runtime/dist/docs/user-guide/space.md +5 -4
  87. package/apps/runtime/dist/docs/user-guide/together.md +29 -2
  88. package/apps/runtime/dist/docs/user-guide/troubleshooting.md +2 -2
  89. package/apps/runtime/dist/docs/user-guide/work-mode.md +128 -0
  90. package/apps/runtime/dist/engenty/connections-external/build-connector.js +2 -5
  91. package/apps/runtime/dist/engenty/connections-external/importer/classify.js +72 -0
  92. package/apps/runtime/dist/engenty/connections-google/connectors/gmail.js +1 -1
  93. package/apps/runtime/dist/engenty/connections-microsoft/outlook.js +3 -2
  94. package/apps/runtime/dist/engenty/connections-sdk/types.js +2 -0
  95. package/apps/runtime/dist/engenty/connections-slack/connector.js +2 -1
  96. package/apps/runtime/dist/engine/decide.js +1 -1
  97. package/apps/runtime/dist/engine/requirements.js +139 -11
  98. package/apps/runtime/dist/engine/resources.js +43 -12
  99. package/apps/runtime/dist/engine/runner.js +121 -27
  100. package/apps/runtime/dist/engine/steps.js +42 -16
  101. package/apps/runtime/dist/engine/template.js +35 -1
  102. package/apps/runtime/dist/env.js +9 -4
  103. package/apps/runtime/dist/index.js +18 -2
  104. package/apps/runtime/dist/link-preview.js +9 -0
  105. package/apps/runtime/dist/live/feed.js +2 -2
  106. package/apps/runtime/dist/live/origin.js +2 -2
  107. package/apps/runtime/dist/live/views.js +51 -26
  108. package/apps/runtime/dist/manage.js +32 -0
  109. package/apps/runtime/dist/mcp/flow-app.js +1 -1
  110. package/apps/runtime/dist/mcp/handler.js +55 -1
  111. package/apps/runtime/dist/mcp/seen.js +47 -0
  112. package/apps/runtime/dist/mcp/tools.js +75 -34
  113. package/apps/runtime/dist/models.js +203 -12
  114. package/apps/runtime/dist/motion-graphics/project.js +8 -8
  115. package/apps/runtime/dist/motion-graphics/step.js +2 -2
  116. package/apps/runtime/dist/motion-graphics/tools.js +1 -1
  117. package/apps/runtime/dist/plugins/events.js +2 -2
  118. package/apps/runtime/dist/plugins/jobs.js +4 -1
  119. package/apps/runtime/dist/plugins/loader.js +2 -2
  120. package/apps/runtime/dist/prompts/architect.md +3 -3
  121. package/apps/runtime/dist/prompts/authoring.md +46 -12
  122. package/apps/runtime/dist/prompts/goose.md +14 -13
  123. package/apps/runtime/dist/prompts/mcp.md +6 -6
  124. package/apps/runtime/dist/prompts/safety.md +18 -0
  125. package/apps/runtime/dist/prompts/services.md +4 -4
  126. package/apps/runtime/dist/prompts/step.md +3 -3
  127. package/apps/runtime/dist/prompts/tools.md +39 -10
  128. package/apps/runtime/dist/routes/api.js +31 -20
  129. package/apps/runtime/dist/routes/broker.js +316 -0
  130. package/apps/runtime/dist/routes/connections.js +71 -0
  131. package/apps/runtime/dist/routes/delivery.js +25 -0
  132. package/apps/runtime/dist/routes/discover.js +17 -0
  133. package/apps/runtime/dist/routes/live.js +11 -1
  134. package/apps/runtime/dist/routes/marketplace.js +3 -3
  135. package/apps/runtime/dist/routes/person.js +4 -1
  136. package/apps/runtime/dist/routes/plugins.js +3 -1
  137. package/apps/runtime/dist/routes/rules.js +26 -0
  138. package/apps/runtime/dist/routes/runs.js +59 -33
  139. package/apps/runtime/dist/routes/skills.js +6 -6
  140. package/apps/runtime/dist/routes/studio.js +256 -148
  141. package/apps/runtime/dist/routes/work.js +199 -0
  142. package/apps/runtime/dist/safety.js +60 -18
  143. package/apps/runtime/dist/sandbox/index.js +27 -0
  144. package/apps/runtime/dist/services/access.js +38 -17
  145. package/apps/runtime/dist/services/api.js +38 -13
  146. package/apps/runtime/dist/services/architect.js +3 -3
  147. package/apps/runtime/dist/services/assistant-chats.js +9 -1
  148. package/apps/runtime/dist/services/brand.js +12 -12
  149. package/apps/runtime/dist/services/broker-bell.js +69 -0
  150. package/apps/runtime/dist/services/broker-notify.js +127 -0
  151. package/apps/runtime/dist/services/client-steps.js +79 -0
  152. package/apps/runtime/dist/services/cloud.js +42 -42
  153. package/apps/runtime/dist/services/command-risk.js +51 -0
  154. package/apps/runtime/dist/services/discover.js +85 -0
  155. package/apps/runtime/dist/services/knowledge-model.js +13 -13
  156. package/apps/runtime/dist/services/knowledge.js +117 -80
  157. package/apps/runtime/dist/services/manage-reports.js +77 -7
  158. package/apps/runtime/dist/services/marketplace.js +7 -31
  159. package/apps/runtime/dist/services/members-studio.js +165 -0
  160. package/apps/runtime/dist/services/onboarding.js +22 -22
  161. package/apps/runtime/dist/services/package-file.js +1 -1
  162. package/apps/runtime/dist/services/package.js +22 -22
  163. package/apps/runtime/dist/services/pages.js +13 -9
  164. package/apps/runtime/dist/services/runs.js +4 -3
  165. package/apps/runtime/dist/services/seen.js +55 -0
  166. package/apps/runtime/dist/services/shares.js +28 -13
  167. package/apps/runtime/dist/services/site.js +23 -23
  168. package/apps/runtime/dist/services/skills-registry.js +15 -15
  169. package/apps/runtime/dist/services/skills.js +44 -44
  170. package/apps/runtime/dist/services/space-categories.js +35 -35
  171. package/apps/runtime/dist/services/space-data.js +130 -111
  172. package/apps/runtime/dist/services/{project-files.js → space-files.js} +84 -75
  173. package/apps/runtime/dist/services/{project-index.js → space-index.js} +116 -73
  174. package/apps/runtime/dist/services/space-origin.js +19 -11
  175. package/apps/runtime/dist/services/spaces.js +478 -598
  176. package/apps/runtime/dist/services/synced-spaces.js +668 -0
  177. package/apps/runtime/dist/services/themes.js +15 -15
  178. package/apps/runtime/dist/services/widgets.js +6 -6
  179. package/apps/runtime/dist/services/wizards.js +112 -38
  180. package/apps/runtime/dist/services/work-ai.js +257 -0
  181. package/apps/runtime/dist/services/work-approvals.js +104 -0
  182. package/apps/runtime/dist/services/work-chats.js +908 -0
  183. package/apps/runtime/dist/services/work-connections.js +90 -0
  184. package/apps/runtime/dist/services/work-files.js +85 -0
  185. package/apps/runtime/dist/services/work-mastra.js +73 -0
  186. package/apps/runtime/dist/services/work-sandbox.js +42 -0
  187. package/apps/runtime/dist/sharing/files.js +14 -4
  188. package/apps/runtime/dist/sharing/index.js +40 -39
  189. package/apps/runtime/dist/sharing/jobs.js +67 -17
  190. package/apps/runtime/dist/sharing/targets/connector.js +10 -6
  191. package/apps/runtime/dist/sharing/targets/email.js +10 -8
  192. package/apps/runtime/dist/sharing/targets/slack.js +10 -8
  193. package/apps/runtime/dist/sharing/targets/storage.js +6 -4
  194. package/apps/runtime/dist/store/index.js +14 -3
  195. package/apps/runtime/dist/store/shared-lists.js +4 -2
  196. package/apps/runtime/dist/sync/apply.js +2 -2
  197. package/apps/runtime/dist/sync/client.js +113 -113
  198. package/apps/runtime/dist/sync/hub.js +41 -41
  199. package/apps/runtime/dist/sync/log.js +12 -12
  200. package/apps/runtime/dist/sync/records.js +118 -120
  201. package/apps/runtime/dist/texts.js +44 -2
  202. package/apps/runtime/dist/tools/connections.js +112 -0
  203. package/apps/runtime/dist/tools/connector.js +157 -67
  204. package/apps/runtime/dist/tools/index.js +31 -15
  205. package/apps/runtime/dist/tools/mail.js +40 -11
  206. package/apps/runtime/dist/tools/pages.js +11 -5
  207. package/apps/runtime/dist/tools/plugin.js +43 -7
  208. package/apps/runtime/dist/tools/skills.js +1 -1
  209. package/apps/runtime/dist/tools/{project.js → space.js} +30 -25
  210. package/apps/runtime/dist/tools/store.js +3 -2
  211. package/apps/runtime/dist/widgets/step.js +10 -10
  212. package/apps/web/dist/assets/AboutDialog-DgfeipUc.js +1 -0
  213. package/apps/web/dist/assets/CapabilitiesPage-BoYJvgIu.js +2 -0
  214. package/apps/web/dist/assets/{ChangelogDialog-CKLgFL6r.js → ChangelogDialog-Duab4zTO.js} +1 -1
  215. package/apps/web/dist/assets/{CodeEditor-B3nto_26.js → CodeEditor-nxoOt8ge.js} +1 -1
  216. package/apps/web/dist/assets/EditorPage-ZrgGpYMC.js +3 -0
  217. package/apps/web/dist/assets/{FlowPreview-zJCtixSi.js → FlowPreview-aMfwcPul.js} +1 -1
  218. package/apps/web/dist/assets/LangSwitch-DpD0inR4.js +1 -0
  219. package/apps/web/dist/assets/Marketplace-CpzD9pNQ.js +2 -0
  220. package/apps/web/dist/assets/NewWizardChannels-IB18afWN.js +1 -0
  221. package/apps/web/dist/assets/NewWizardPage-rYbcmPGG.js +7 -0
  222. package/apps/web/dist/assets/OnboardingPage-ztHT01ah.js +1 -0
  223. package/apps/web/dist/assets/PreviewPane-DviWTLfi.js +9 -0
  224. package/apps/web/dist/assets/PublicRunner-C2geZhhM.js +1 -0
  225. package/apps/web/dist/assets/ReadOnly-kSb_Kbdm.js +1 -0
  226. package/apps/web/dist/assets/SettingsPage-DM2h8N0J.js +2 -0
  227. package/apps/web/dist/assets/SetupPage-Wfyz6xH4.js +1 -0
  228. package/apps/web/dist/assets/SharePage-CyWQorWB.js +1 -0
  229. package/apps/web/dist/assets/SkillEditor-CqQMg2MC.js +1 -0
  230. package/apps/web/dist/assets/Skills-CnhpgoOA.js +9 -0
  231. package/apps/web/dist/assets/SpacePage-q4Bk3N7k.js +2 -0
  232. package/apps/web/dist/assets/UpdateCheck-C2CE6CC3.js +2 -0
  233. package/apps/web/dist/assets/changelog-B6hN1Cp7.js +1 -0
  234. package/apps/web/dist/assets/circle-alert-C8Ma-bPY.js +1 -0
  235. package/apps/web/dist/assets/circle-check-Bu9Z3mlJ.js +1 -0
  236. package/apps/web/dist/assets/code-editor-CfTGqOpp.js +2 -0
  237. package/apps/web/dist/assets/{code-xml-Hx7YxCg0.js → code-xml-B4kLFZSj.js} +1 -1
  238. package/apps/web/dist/assets/connection-sign-in-CIiv0t2p.js +1 -0
  239. package/apps/web/dist/assets/esm-Xwn4GtqG.js +7 -0
  240. package/apps/web/dist/assets/{eye-BkMFLnYu.js → eye-BpoKpIdt.js} +1 -1
  241. package/apps/web/dist/assets/host-BrP-ENHg.css +1 -0
  242. package/apps/web/dist/assets/host-DJ7-p0Sc.js +118 -0
  243. package/apps/web/dist/assets/i18n-BJ7Ld6O6.js +55 -0
  244. package/apps/web/dist/assets/index-D18DvnxX.css +1 -0
  245. package/apps/web/dist/assets/index-D2akynS-.js +10 -0
  246. package/apps/web/dist/assets/logo-BpnjXTRy.js +1 -0
  247. package/apps/web/dist/assets/oss-credits-IdX5bc71.js +1 -0
  248. package/apps/web/dist/assets/{phone-DMfwuh9V.js → phone-Ctvf-Am-.js} +1 -1
  249. package/apps/web/dist/assets/read-aloud-Da_RSm2S.js +1 -0
  250. package/apps/web/dist/assets/{runners-D5qz6b_s.js → runners-CyIvJIHr.js} +1 -1
  251. package/apps/web/dist/assets/scroll-text-ChrjRu9Y.js +1 -0
  252. package/apps/web/dist/assets/session-SXPtTWRK.js +2 -0
  253. package/apps/web/dist/assets/theme-CbTcTFWy.js +11 -0
  254. package/apps/web/dist/assets/ui-Uqnx1UpK.js +82 -0
  255. package/apps/web/dist/assets/{upload-DQJwLSGj.js → upload-BchUnQGV.js} +1 -1
  256. package/apps/web/dist/assets/{useMutation-C20c2EH3.js → useMutation-DEkZhp5c.js} +1 -1
  257. package/apps/web/dist/assets/{volume-2-CC5qamYT.js → volume-2-CU_oy81Z.js} +1 -1
  258. package/apps/web/dist/assets/with-selector-CoMyWBAX.js +1 -0
  259. package/apps/web/dist/index.html +17 -10
  260. package/apps/web/dist/mcp/flow.html +119 -64
  261. package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +20 -10
  262. package/node_modules/@engenty/wizards-shared/dist/assistant.d.ts +7 -1
  263. package/node_modules/@engenty/wizards-shared/dist/assistant.js +11 -1
  264. package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +236 -2
  265. package/node_modules/@engenty/wizards-shared/dist/connectors.js +52 -2
  266. package/node_modules/@engenty/wizards-shared/dist/definition.d.ts +257 -8
  267. package/node_modules/@engenty/wizards-shared/dist/definition.js +113 -15
  268. package/node_modules/@engenty/wizards-shared/dist/discover.d.ts +21 -0
  269. package/node_modules/@engenty/wizards-shared/dist/discover.js +1 -0
  270. package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +38 -2
  271. package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +38 -2
  272. package/node_modules/@engenty/wizards-shared/dist/fit.js +1 -1
  273. package/node_modules/@engenty/wizards-shared/dist/json-schema.d.ts +1 -1
  274. package/node_modules/@engenty/wizards-shared/dist/json-schema.js +1 -1
  275. package/node_modules/@engenty/wizards-shared/dist/live.d.ts +2 -2
  276. package/node_modules/@engenty/wizards-shared/dist/live.js +1 -1
  277. package/node_modules/@engenty/wizards-shared/dist/marketplace.d.ts +0 -5
  278. package/node_modules/@engenty/wizards-shared/dist/marketplace.js +3 -1
  279. package/node_modules/@engenty/wizards-shared/dist/run.d.ts +115 -6
  280. package/node_modules/@engenty/wizards-shared/dist/run.js +135 -0
  281. package/node_modules/@engenty/wizards-shared/dist/runner-texts.d.ts +15 -1
  282. package/node_modules/@engenty/wizards-shared/dist/runner-texts.js +14 -0
  283. package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +42 -0
  284. package/node_modules/@engenty/wizards-shared/dist/rwx.js +29 -0
  285. package/node_modules/@engenty/wizards-shared/dist/skills.d.ts +1 -1
  286. package/node_modules/@engenty/wizards-shared/dist/space-data.d.ts +2 -2
  287. package/node_modules/@engenty/wizards-shared/dist/{projects.d.ts → spaces.d.ts} +39 -15
  288. package/node_modules/@engenty/wizards-shared/dist/{projects.js → spaces.js} +16 -9
  289. package/node_modules/@engenty/wizards-shared/dist/store.d.ts +7 -2
  290. package/node_modules/@engenty/wizards-shared/dist/surface.d.ts +35 -4
  291. package/node_modules/@engenty/wizards-shared/dist/surface.js +121 -3
  292. package/node_modules/@engenty/wizards-shared/dist/sync.d.ts +1 -1
  293. package/node_modules/@engenty/wizards-shared/dist/theme-look.js +1 -1
  294. package/node_modules/@engenty/wizards-shared/dist/tiers.d.ts +195 -0
  295. package/node_modules/@engenty/wizards-shared/dist/tiers.js +98 -0
  296. package/node_modules/@engenty/wizards-shared/dist/wizard-package.d.ts +2 -2
  297. package/node_modules/@engenty/wizards-shared/dist/wizard-package.js +3 -3
  298. package/package.json +4 -1
  299. package/plugin/.claude-plugin/plugin.json +1 -1
  300. package/plugin/plugin.json +1 -1
  301. package/plugin/skills/build-wizard/SKILL.md +1 -1
  302. package/apps/runtime/dist/services/projects.js +0 -473
  303. package/apps/web/dist/assets/AboutDialog-CLgnoYTI.js +0 -1
  304. package/apps/web/dist/assets/EditorPage-D9JDdAAI.js +0 -3
  305. package/apps/web/dist/assets/Marketplace-D4e0Oac9.js +0 -2
  306. package/apps/web/dist/assets/NewWizardChannels-B4N7GIYh.js +0 -1
  307. package/apps/web/dist/assets/NewWizardPage-Dpt0Mzk8.js +0 -7
  308. package/apps/web/dist/assets/OnboardingPage-CDoYVjRC.js +0 -1
  309. package/apps/web/dist/assets/PreviewPane-DPB5nEd5.js +0 -10
  310. package/apps/web/dist/assets/ProjectPage-408YOoNa.js +0 -10
  311. package/apps/web/dist/assets/PublicRunner-BZ66xdRX.js +0 -1
  312. package/apps/web/dist/assets/ReadOnly-C9oGMkUB.js +0 -1
  313. package/apps/web/dist/assets/SettingsPage-Dq3AZwJS.js +0 -2
  314. package/apps/web/dist/assets/SetupPage-CP3lbyZc.js +0 -1
  315. package/apps/web/dist/assets/SharePage-DdMm1Y8K.js +0 -1
  316. package/apps/web/dist/assets/SkillEditor-b5NMFB90.js +0 -1
  317. package/apps/web/dist/assets/changelog-82-DHogQ.js +0 -1
  318. package/apps/web/dist/assets/esm-BdxDT9Km.js +0 -7
  319. package/apps/web/dist/assets/i18n-CKKh3Pn0.js +0 -55
  320. package/apps/web/dist/assets/index-Cy2NHzAx.js +0 -49
  321. package/apps/web/dist/assets/index-wR_-1hfN.css +0 -1
  322. package/apps/web/dist/assets/lock-CGXM95Mi.js +0 -1
  323. package/apps/web/dist/assets/logo-CGNoptDg.js +0 -1
  324. package/apps/web/dist/assets/oss-credits-DpRWUPjD.js +0 -1
  325. package/apps/web/dist/assets/session-DRy0k0nh.js +0 -2
  326. package/apps/web/dist/assets/theme-DNPm6r4e.js +0 -11
  327. package/apps/web/dist/assets/ui-CwFQAfE-.js +0 -83
  328. /package/apps/runtime/dist/db/migrations/tenant/{0027_space_skill.sql → 0030_space_skill.sql} +0 -0
@@ -0,0 +1,367 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { featureOn } from "@engenty/wizards-shared/features";
3
+ import { note } from "@engenty/wizards-shared/run";
4
+ import { RWX_ALL, RWX_LIMITS, RWX_ROLES, } from "@engenty/wizards-shared/rwx";
5
+ import { and, eq, inArray } from "drizzle-orm";
6
+ import { nanoid } from "nanoid";
7
+ import { db, schema } from "../db/client.js";
8
+ import { currentFeatures } from "../features.js";
9
+ import { currentRole, isAdminRole, MEMBERS_EDIT_ONLY, roleOf } from "../services/access.js";
10
+ import { notFound, ServiceError } from "../services/errors.js";
11
+ import { ownedSpace } from "../services/spaces.js";
12
+ import { currentTenant } from "../tenants/tenant.js";
13
+ import { logRuleChange } from "./log.js";
14
+ /** The resource kinds this runtime asks the broker about today; the others wait for their reader. */
15
+ export const ENFORCED = ["document", "table", "page"];
16
+ export async function brokerOn() {
17
+ return featureOn(await currentFeatures(), "broker");
18
+ }
19
+ // --- who ------------------------------------------------------------------------------------
20
+ /**
21
+ * The runtime itself, with no person, guest or run behind it: a plugin's job (`server.every`,
22
+ * plugins/jobs.ts). It may read, write and run everything in the space; a deny
23
+ * written for a subject it asks beside (the plugin) still wins. It never comes from a request:
24
+ * no helper below returns it, a rule cannot name it (`system` is no `RwxSubjectKind`),
25
+ * `decideWith` knows it by identity rather than by its kind, and test/rwx.test.ts fails on a file
26
+ * that uses it outside the places listed there. No subject at all stays a no.
27
+ */
28
+ export const SYSTEM = Object.freeze({ kind: "system", id: null });
29
+ export const subjectsOfGuest = () => [{ kind: "guest", id: null }];
30
+ /** A signed-in person: their own rows, and their role's. */
31
+ export const subjectsOfPerson = (userId, role) => [
32
+ { kind: "person", id: userId },
33
+ { kind: "role", id: role },
34
+ ];
35
+ /** Goose for a person: its own rows for them, and the person's. */
36
+ export const subjectsOfGoose = (userId, role) => [
37
+ { kind: "goose", id: userId },
38
+ ...subjectsOfPerson(userId, role),
39
+ ];
40
+ export const subjectsOfHolder = (holder) => holder.kind === "guest" ? subjectsOfGuest() : subjectsOfPerson(holder.id, holder.role);
41
+ /** A run's step: who holds the run (a guest or a person), and the wizard it runs. */
42
+ export function subjectsOf(ctx) {
43
+ return [
44
+ ...(ctx.holder ? subjectsOfHolder(ctx.holder) : []),
45
+ { kind: "wizard", id: ctx.store.wizardId },
46
+ ];
47
+ }
48
+ const reader = new AsyncLocalStorage();
49
+ /**
50
+ * Runs `fn` reading as `who`, for code the runtime hands a call to without a broker caller: a
51
+ * plugin's HTTP route reads as the person it answers, a plugin's job as the runtime itself. Code
52
+ * outside reads as nobody, which is a no.
53
+ */
54
+ export function readingAs(who, fn) {
55
+ return reader.run(who, fn);
56
+ }
57
+ /** Whom the code runs reading for (`readingAs`), or nobody. */
58
+ export const currentReader = () => reader.getStore() ?? [];
59
+ /**
60
+ * Whom code the runtime hands a call to (a plugin's tool in a step or a Work chat) runs for: the
61
+ * person, guest or Goose behind the caller, a fixed step's holder, and the run's wizard. Never
62
+ * `SYSTEM`; a caller with no one behind it gives no subject, which is a no.
63
+ */
64
+ export async function subjectsOfCaller(caller) {
65
+ const wizard = caller.where.wizardId
66
+ ? [{ kind: "wizard", id: caller.where.wizardId }]
67
+ : [];
68
+ return [...(await subjectsOfWho(caller.who)), ...wizard];
69
+ }
70
+ async function subjectsOfWho(who) {
71
+ switch (who.kind) {
72
+ case "guest":
73
+ return subjectsOfGuest();
74
+ case "person":
75
+ case "admin":
76
+ return who.id ? subjectsOfPerson(who.id, await roleOf(who.id)) : [];
77
+ case "goose":
78
+ return who.id ? subjectsOfGoose(who.id, await roleOf(who.id)) : [];
79
+ case "step":
80
+ return who.for ? subjectsOfWho(who.for) : [];
81
+ case "plugin":
82
+ return who.id ? [{ kind: "plugin", id: who.id }] : [];
83
+ }
84
+ }
85
+ /** Who holds a run: the signed-in person with their role, else the visitor of the shared link. */
86
+ export async function runHolder(run) {
87
+ if (run.userId) {
88
+ return { kind: "person", id: run.userId, role: await roleOf(run.userId) };
89
+ }
90
+ return { kind: "guest", id: run.visitorId ?? run.id };
91
+ }
92
+ // --- deciding --------------------------------------------------------------------------------
93
+ const about = (rule, subjects) => subjects.some((s) => s.kind === rule.subjectKind && (s.id ?? null) === (rule.subjectId ?? null));
94
+ const names = (rule, resource) => rule.resourceKind === resource.kind &&
95
+ (rule.resourceId === RWX_ALL || rule.resourceId === resource.id);
96
+ /** The first rule about the subjects and the resource that says `flag` for `perm`. */
97
+ const matching = (rules, subjects, resource, perm, flag) => rules.find((r) => about(r, subjects) && names(r, resource) && r[perm] === flag);
98
+ /**
99
+ * The runtime itself (`SYSTEM`): yes, unless a deny names a subject it asks beside, on the
100
+ * resource or on the document behind it.
101
+ */
102
+ function decideSystem(rules, others, resource, perm) {
103
+ const behind = resource.fileId ? [{ kind: "document", id: resource.fileId }] : [];
104
+ for (const r of [{ kind: resource.kind, id: resource.id }, ...behind]) {
105
+ const deny = matching(rules, others, r, perm, "deny");
106
+ if (deny) {
107
+ return { ok: false, rule: deny.id, reason: `${r.kind} ${r.id}: denied`, of: r };
108
+ }
109
+ }
110
+ return {
111
+ ok: true,
112
+ reason: `${resource.kind} ${resource.id}: the runtime itself`,
113
+ of: { kind: resource.kind, id: resource.id },
114
+ };
115
+ }
116
+ /** One resource against the rules, without the document behind it. */
117
+ function decideOne(rules, subjects, resource, perm) {
118
+ const where = `${resource.kind} ${resource.id}`;
119
+ const of = { kind: resource.kind, id: resource.id };
120
+ const deny = matching(rules, subjects, resource, perm, "deny");
121
+ if (deny) {
122
+ return { ok: false, rule: deny.id, reason: `${where}: denied`, of };
123
+ }
124
+ const allow = matching(rules, subjects, resource, perm, "allow");
125
+ if (allow) {
126
+ return { ok: true, rule: allow.id, reason: `${where}: allowed`, of };
127
+ }
128
+ return { ok: false, reason: `${where}: no rule`, of };
129
+ }
130
+ /**
131
+ * The decision from rules already loaded: no subject is a no; the runtime itself is a yes but for
132
+ * a deny on a subject beside it; else a deny wins, then any allow, else no rule.
133
+ */
134
+ export function decideWith(rules, subjects, resource, perm) {
135
+ if (!subjects.length) {
136
+ return {
137
+ ok: false,
138
+ reason: `${resource.kind} ${resource.id}: no subject`,
139
+ of: { kind: resource.kind, id: resource.id },
140
+ };
141
+ }
142
+ if (subjects.includes(SYSTEM)) {
143
+ const others = subjects.filter((s) => s !== SYSTEM);
144
+ return decideSystem(rules, others, resource, perm);
145
+ }
146
+ const own = decideOne(rules, subjects, resource, perm);
147
+ if (!(own.ok && resource.fileId)) {
148
+ return own;
149
+ }
150
+ const behind = decideOne(rules, subjects, { kind: "document", id: resource.fileId }, perm);
151
+ return behind.ok ? own : behind;
152
+ }
153
+ /** The rules of a space, seeded first where it has none yet. */
154
+ async function rulesOf(spaceId) {
155
+ const rows = await db.query.rwxRule.findMany({ where: eq(schema.rwxRule.spaceId, spaceId) });
156
+ if (rows.length) {
157
+ return rows;
158
+ }
159
+ await seedRwx(spaceId);
160
+ return db.query.rwxRule.findMany({ where: eq(schema.rwxRule.spaceId, spaceId) });
161
+ }
162
+ /** Whether the subjects may do `perm` on the resource. With the switch off: yes. */
163
+ export async function decide(spaceId, subjects, resource, perm) {
164
+ if (!(await brokerOn())) {
165
+ return { ok: true, reason: "off", of: { kind: resource.kind, id: resource.id } };
166
+ }
167
+ return decideWith(await rulesOf(spaceId), subjects, resource, perm);
168
+ }
169
+ const WHAT = {
170
+ document: "rwxDocument",
171
+ table: "rwxTable",
172
+ page: "rwxPage",
173
+ connection: "rwxConnection",
174
+ mail: "rwxMail",
175
+ host: "rwxHost",
176
+ sandbox: "rwxSandbox",
177
+ };
178
+ const VERB = { r: "rwxRead", w: "rwxWrite", x: "rwxRun" };
179
+ /** The refusal: *verboten*, the resource's kind and id, nothing of its content. */
180
+ export const forbidden = (resource, perm) => new ServiceError("forbidden", note("forbidden", { what: note(WHAT[resource.kind]), verb: note(VERB[perm]), id: resource.id }), { resource: resource.kind, id: resource.id });
181
+ /** Refuses with 403 unless the subjects may do `perm` on the resource. */
182
+ export async function assertRwx(spaceId, subjects, resource, perm) {
183
+ const decision = await decide(spaceId, subjects, resource, perm);
184
+ if (!decision.ok) {
185
+ throw forbidden(decision.of, perm);
186
+ }
187
+ }
188
+ /** The resources among `items` the subjects may read, by id. With the switch off: all of them. */
189
+ export async function readable(spaceId, subjects, items) {
190
+ if (!(await brokerOn())) {
191
+ return new Set(items.map((i) => i.id));
192
+ }
193
+ const rules = await rulesOf(spaceId);
194
+ return new Set(items.filter((i) => decideWith(rules, subjects, i, "r").ok).map((i) => i.id));
195
+ }
196
+ /** The ids of `kind` among `ids` the subjects may read, in their order. */
197
+ export async function readableIds(spaceId, subjects, kind, ids) {
198
+ const ok = await readable(spaceId, subjects, ids.map((id) => ({ kind, id })));
199
+ return ids.filter((id) => ok.has(id));
200
+ }
201
+ /** Pages by id with the document behind each, for `readable`. */
202
+ export async function pageResources(pageIds) {
203
+ if (!pageIds.length) {
204
+ return [];
205
+ }
206
+ const rows = await db.query.spacePage.findMany({
207
+ where: inArray(schema.spacePage.id, pageIds),
208
+ columns: { id: true, fileId: true },
209
+ });
210
+ return rows.map((p) => ({ kind: "page", id: p.id, fileId: p.fileId }));
211
+ }
212
+ /** Tables by id with the document behind each, for `readable`. */
213
+ export async function tableResources(tableIds) {
214
+ if (!tableIds.length) {
215
+ return [];
216
+ }
217
+ const rows = await db.query.spaceTable.findMany({
218
+ where: inArray(schema.spaceTable.id, tableIds),
219
+ columns: { id: true, fileId: true },
220
+ });
221
+ return rows.map((t) => ({ kind: "table", id: t.id, fileId: t.fileId }));
222
+ }
223
+ /**
224
+ * Whether the subjects may read everything the enforced kinds hold: every kind allowed on `*`
225
+ * and no deny for them anywhere. What is made of many items at once (a category value's
226
+ * summary) is shown only then.
227
+ */
228
+ export async function readsEverything(spaceId, subjects) {
229
+ if (!(await brokerOn())) {
230
+ return true;
231
+ }
232
+ const rules = await rulesOf(spaceId);
233
+ return ENFORCED.every((kind) => decideWith(rules, subjects, { kind, id: RWX_ALL }, "r").ok &&
234
+ !rules.some((r) => about(r, subjects) && r.resourceKind === kind && r.r === "deny"));
235
+ }
236
+ // --- the seed ---------------------------------------------------------------------------------
237
+ /**
238
+ * What every space starts with, so the switch can go on without changing what people can do
239
+ * today: admins and owners read, write and execute everything of the enforced kinds, members
240
+ * read and write, guests read. A deny an admin writes afterwards wins over these.
241
+ */
242
+ export async function seedRwx(spaceId) {
243
+ const seeded = [];
244
+ for (const kind of ENFORCED) {
245
+ for (const role of ["admin", "owner"]) {
246
+ seeded.push(rule("role", role, kind, { r: "allow", w: "allow", x: "allow" }));
247
+ }
248
+ seeded.push(rule("role", "member", kind, { r: "allow", w: "allow", x: null }));
249
+ seeded.push(rule("guest", null, kind, { r: "allow", w: null, x: null }));
250
+ }
251
+ await db
252
+ .insert(schema.rwxRule)
253
+ .values(seeded.map((r) => row(spaceId, r, "seed")))
254
+ .onConflictDoNothing();
255
+ }
256
+ const rule = (subjectKind, subjectId, resourceKind, flags) => ({ subjectKind, subjectId, resourceKind, resourceId: RWX_ALL, ...flags });
257
+ const row = (spaceId, r, createdBy) => ({
258
+ id: nanoid(12),
259
+ tenantId: currentTenant(),
260
+ spaceId,
261
+ ...r,
262
+ createdBy,
263
+ });
264
+ // --- the rules, for the studio ---------------------------------------------------------------
265
+ const view = (r) => ({
266
+ id: r.id,
267
+ subjectKind: r.subjectKind,
268
+ subjectId: r.subjectId,
269
+ resourceKind: r.resourceKind,
270
+ resourceId: r.resourceId,
271
+ r: r.r,
272
+ w: r.w,
273
+ x: r.x,
274
+ createdBy: r.createdBy,
275
+ createdAt: r.createdAt.toISOString(),
276
+ });
277
+ /** The rules are behind their switch: while it is off, they are not there. */
278
+ async function requireBroker() {
279
+ if (!(await brokerOn())) {
280
+ throw notFound();
281
+ }
282
+ }
283
+ /** Only the space's admins read and write its rules. */
284
+ function requireRuleAdmin() {
285
+ if (!isAdminRole(currentRole())) {
286
+ throw new ServiceError("forbidden", MEMBERS_EDIT_ONLY);
287
+ }
288
+ }
289
+ const key = (r) => [r.subjectKind, r.subjectId ?? "", r.resourceKind, r.resourceId].join("\u0000");
290
+ /** A rule as a person wrote it, checked for what the schema cannot say. */
291
+ function checkRule(r) {
292
+ const refuse = (what) => new ServiceError("invalid", what);
293
+ if (r.subjectKind === "guest" && r.subjectId !== null) {
294
+ throw refuse("A guest rule has no subject id.");
295
+ }
296
+ if (r.subjectKind === "role" && !RWX_ROLES.includes(r.subjectId ?? "")) {
297
+ throw refuse("A role is member, admin or owner.");
298
+ }
299
+ if (r.subjectKind !== "guest" && !r.subjectId) {
300
+ throw refuse(`A ${r.subjectKind} rule names the ${r.subjectKind}'s id.`);
301
+ }
302
+ if (!r.resourceId.trim()) {
303
+ throw refuse("A rule names the resource's id, or * for all of the kind.");
304
+ }
305
+ if (r.r === null && r.w === null && r.x === null) {
306
+ throw refuse("A rule sets at least one of r, w and x.");
307
+ }
308
+ }
309
+ export async function listRules(userId, spaceId) {
310
+ const space = await ownedSpace(userId, spaceId);
311
+ await requireBroker();
312
+ requireRuleAdmin();
313
+ return (await rulesOf(space.id)).map(view);
314
+ }
315
+ /**
316
+ * The whole list, as the page saves it: what is there is replaced. An empty list brings the seed
317
+ * back the next time the space is read.
318
+ */
319
+ export async function putRules(userId, spaceId, rules) {
320
+ const space = await ownedSpace(userId, spaceId);
321
+ await requireBroker();
322
+ requireRuleAdmin();
323
+ if (rules.length > RWX_LIMITS.rules) {
324
+ throw new ServiceError("invalid", `A space holds at most ${RWX_LIMITS.rules} rules.`);
325
+ }
326
+ const seen = new Set();
327
+ for (const r of rules) {
328
+ checkRule(r);
329
+ if (seen.has(key(r))) {
330
+ throw new ServiceError("invalid", "Two rules name the same subject and resource.");
331
+ }
332
+ seen.add(key(r));
333
+ }
334
+ await db.transaction(async (tx) => {
335
+ await tx.delete(schema.rwxRule).where(eq(schema.rwxRule.spaceId, space.id));
336
+ if (rules.length) {
337
+ await tx.insert(schema.rwxRule).values(rules.map((r) => row(space.id, r, userId)));
338
+ }
339
+ });
340
+ await logRuleChange({
341
+ userId,
342
+ spaceId: space.id,
343
+ connectionId: "rwx",
344
+ summary: { rules: rules.length },
345
+ reason: `The RWX rules were replaced: ${rules.length} now.`,
346
+ });
347
+ return (await rulesOf(space.id)).map(view);
348
+ }
349
+ export async function deleteRule(userId, spaceId, ruleId) {
350
+ const space = await ownedSpace(userId, spaceId);
351
+ await requireBroker();
352
+ requireRuleAdmin();
353
+ const gone = await db
354
+ .delete(schema.rwxRule)
355
+ .where(and(eq(schema.rwxRule.spaceId, space.id), eq(schema.rwxRule.id, ruleId)))
356
+ .returning({ id: schema.rwxRule.id });
357
+ if (!gone.length) {
358
+ throw notFound();
359
+ }
360
+ await logRuleChange({
361
+ userId,
362
+ spaceId: space.id,
363
+ connectionId: "rwx",
364
+ summary: { deleted: ruleId },
365
+ reason: "An RWX rule was deleted.",
366
+ });
367
+ }
@@ -0,0 +1,189 @@
1
+ import { and, eq, gte, notLike } from "drizzle-orm";
2
+ import { db, schema } from "../db/client.js";
3
+ import { summarizeInput, writeLog } from "./log.js";
4
+ /*
5
+ * Sends from a guest's run (docs/plans/capabilities.md, A6; rule 7 of the connections plan): a
6
+ * mail leaves only for an address the run itself collected — what the person typed into a page
7
+ * field of kind `email` — or for an address in one of the space's own mail domains. A run sends a
8
+ * few, a wizard's guests a number per day the wizard sets. Everything beyond is refused and
9
+ * logged as critical. Members' runs are not bound: they send as they always did.
10
+ */
11
+ /** Mails one guest's run sends, from any address. */
12
+ export const GUEST_MAILS_PER_RUN = 3;
13
+ const ADDRESS = /^[^\s@<>,;]+@[^\s@<>,;]+\.[^\s@<>,;]+$/;
14
+ /** The addresses a send's input names: `to`, `cc`, `bcc`, `recipients`, as strings, lists or objects. */
15
+ export function recipientsOf(input) {
16
+ if (!input || typeof input !== "object") {
17
+ return [];
18
+ }
19
+ const found = new Set();
20
+ const take = (value) => {
21
+ if (typeof value === "string") {
22
+ for (const part of value.split(/[\s,;]+/)) {
23
+ const address = part
24
+ .replace(/^.*<|>$/g, "")
25
+ .trim()
26
+ .toLowerCase();
27
+ if (address) {
28
+ found.add(address);
29
+ }
30
+ }
31
+ }
32
+ else if (Array.isArray(value)) {
33
+ value.forEach(take);
34
+ }
35
+ else if (value && typeof value === "object") {
36
+ const o = value;
37
+ take(o.email ?? o.address ?? o.emailAddress);
38
+ }
39
+ };
40
+ for (const key of ["to", "cc", "bcc", "recipients", "recipient"]) {
41
+ take(input[key]);
42
+ }
43
+ return [...found];
44
+ }
45
+ /** The domain of an address, lower case; null for what is no address. */
46
+ const domainOf = (address) => ADDRESS.test(address) ? address.slice(address.lastIndexOf("@") + 1).toLowerCase() : null;
47
+ /**
48
+ * The addresses a run collected: what its page fields of kind `email` hold. Nothing confirms an
49
+ * address today, so the field's value counts as the run's own.
50
+ */
51
+ export function runAddresses(run) {
52
+ const own = new Set();
53
+ for (const step of run.definition.steps) {
54
+ if (step.type !== "page") {
55
+ continue;
56
+ }
57
+ for (const field of step.fields ?? []) {
58
+ const value = run.state.values[field.id];
59
+ if (field.kind === "email" && typeof value === "string" && ADDRESS.test(value.trim())) {
60
+ own.add(value.trim().toLowerCase());
61
+ }
62
+ }
63
+ }
64
+ return [...own];
65
+ }
66
+ /** Whether an address is in one of the space's mail domains, or below one. */
67
+ export function inMailDomains(address, domains) {
68
+ const domain = domainOf(address);
69
+ return (!!domain &&
70
+ domains.some((d) => {
71
+ const own = d.trim().toLowerCase();
72
+ return own && (domain === own || domain.endsWith(`.${own}`));
73
+ }));
74
+ }
75
+ /** Today's first moment, as the per-day count sees it. */
76
+ const startOfToday = () => {
77
+ const now = new Date();
78
+ return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
79
+ };
80
+ /** How many mails guests sent through this run, and through this wizard today. */
81
+ async function guestMailsSent(where) {
82
+ // Mails only: a request to a host is a send too, counted nowhere here.
83
+ const sent = and(eq(schema.brokerLog.whoKind, "guest"), eq(schema.brokerLog.kind, "send"), eq(schema.brokerLog.result, "ok"), notLike(schema.brokerLog.connectionId, "host:%"));
84
+ const [inRun, today] = await Promise.all([
85
+ where.runId
86
+ ? db.$count(schema.brokerLog, and(sent, eq(schema.brokerLog.runId, where.runId)))
87
+ : 0,
88
+ where.wizardId
89
+ ? db.$count(schema.brokerLog, and(sent, eq(schema.brokerLog.wizardId, where.wizardId), gte(schema.brokerLog.at, startOfToday())))
90
+ : 0,
91
+ ]);
92
+ return { run: inRun, day: today };
93
+ }
94
+ /**
95
+ * Whether a guest's run may send to these addresses now: each one the run's own or in the space's
96
+ * domains, and the run and the wizard under their caps. The run, the wizard and the space are
97
+ * read here; a run or wizard not known refuses.
98
+ */
99
+ export async function guestSendCheck(where, recipients) {
100
+ const refuse = (refused, reason) => ({
101
+ ok: false,
102
+ refused,
103
+ reason,
104
+ });
105
+ if (!recipients.length) {
106
+ return refuse("not_allowed_recipient", "The send names no recipient.");
107
+ }
108
+ const run = where.runId
109
+ ? await db.query.run.findFirst({
110
+ where: eq(schema.run.id, where.runId),
111
+ columns: { definition: true, state: true, wizardId: true },
112
+ })
113
+ : null;
114
+ if (!run) {
115
+ return refuse("not_allowed_recipient", "The run is not known here.");
116
+ }
117
+ const wizard = await db.query.wizard.findFirst({
118
+ where: eq(schema.wizard.id, run.wizardId),
119
+ columns: { spaceId: true, dailyMailLimit: true },
120
+ });
121
+ const space = wizard
122
+ ? await db.query.space.findFirst({
123
+ where: eq(schema.space.id, wizard.spaceId),
124
+ columns: { mailDomains: true },
125
+ })
126
+ : null;
127
+ const own = runAddresses(run);
128
+ const domains = space?.mailDomains ?? [];
129
+ const foreign = recipients.find((address) => !(own.includes(address.toLowerCase()) || inMailDomains(address, domains)));
130
+ if (foreign) {
131
+ return refuse("not_allowed_recipient", `A guest's run sends only to its own address or the space's domains, not to ${foreign}.`);
132
+ }
133
+ const sent = await guestMailsSent({ ...where, wizardId: where.wizardId ?? run.wizardId });
134
+ if (sent.run >= GUEST_MAILS_PER_RUN) {
135
+ return refuse("run_limit", `This run has sent its ${GUEST_MAILS_PER_RUN} mails.`);
136
+ }
137
+ const perDay = wizard?.dailyMailLimit ?? 50;
138
+ if (sent.day >= perDay) {
139
+ return refuse("day_limit", `This wizard's guests have sent today's ${perDay} mails.`);
140
+ }
141
+ return { ok: true };
142
+ }
143
+ /**
144
+ * A mail from the server's own address, which no account holds: the one send that is not a
145
+ * connector's action. A guest's run is bound by the rules above; the call is logged like any
146
+ * other, under the connection `mail:server`.
147
+ */
148
+ export async function brokerSend(call) {
149
+ const started = Date.now();
150
+ const { who, where } = call;
151
+ const line = (outcome, reason, result) => writeLog({
152
+ at: new Date(started),
153
+ durationMs: Date.now() - started,
154
+ whoKind: who.kind,
155
+ whoId: who.id ?? null,
156
+ runId: where.runId ?? null,
157
+ stepId: where.stepId ?? null,
158
+ chatId: where.chatId ?? null,
159
+ wizardId: where.wizardId ?? null,
160
+ spaceId: where.spaceId ?? null,
161
+ connectionId: "mail:server",
162
+ actionId: "send_message",
163
+ kind: "send",
164
+ inputSummary: summarizeInput(call.input),
165
+ outcome,
166
+ rule: "guest_send",
167
+ reason,
168
+ result,
169
+ resultSize: null,
170
+ });
171
+ let reason = "The server's address sends for members.";
172
+ if (where.guest) {
173
+ const check = await guestSendCheck(where, call.recipients);
174
+ if (!check.ok) {
175
+ await line("deny", check.reason, check.refused);
176
+ return check;
177
+ }
178
+ reason = "The recipients are the run's own or the space's.";
179
+ }
180
+ try {
181
+ const result = await call.handler();
182
+ await line("allow", reason, "ok");
183
+ return { ok: true, result };
184
+ }
185
+ catch (err) {
186
+ await line("allow", reason, err instanceof Error ? err.constructor.name || "Error" : "Error");
187
+ throw err;
188
+ }
189
+ }
@@ -17,7 +17,7 @@ import { smtpConnector } from "./smtp.js";
17
17
  import { googleThroughAccount } from "./via-account.js";
18
18
  /**
19
19
  * engenty's own connectors, as engenty ships them: Gmail, Drive, Calendar, Contacts, Outlook,
20
- * OneDrive, Slack, GitHub, HubSpot, S3 — and SMTP, for sending from any mailbox. Every project
20
+ * OneDrive, Slack, GitHub, HubSpot, S3 — and SMTP, for sending from any mailbox. Every space
21
21
  * has them; an OAuth one can be connected once its client is configured.
22
22
  */
23
23
  export const BUILTIN_CONNECTORS = [