@engenty/wizards-runtime 0.13.0 → 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.
- package/apps/runtime/dist/agents/architect.js +8 -7
- package/apps/runtime/dist/agents/goose-connections.js +82 -0
- package/apps/runtime/dist/agents/space-agent.js +9 -2
- package/apps/runtime/dist/agents/studio-assistant.js +101 -67
- package/apps/runtime/dist/agents/website.js +1 -1
- package/apps/runtime/dist/app.js +17 -1
- package/apps/runtime/dist/assistant/knows.js +6 -5
- package/apps/runtime/dist/authoring/guide.js +25 -5
- package/apps/runtime/dist/broker/account.js +166 -0
- package/apps/runtime/dist/broker/call.js +417 -0
- package/apps/runtime/dist/broker/caller.js +10 -0
- package/apps/runtime/dist/broker/grant.js +152 -0
- package/apps/runtime/dist/broker/host.js +51 -0
- package/apps/runtime/dist/broker/index.js +8 -0
- package/apps/runtime/dist/broker/kept.js +31 -0
- package/apps/runtime/dist/broker/log.js +240 -0
- package/apps/runtime/dist/broker/registry.js +9 -0
- package/apps/runtime/dist/broker/rwx.js +367 -0
- package/apps/runtime/dist/broker/send.js +189 -0
- package/apps/runtime/dist/connectors/builtin.js +1 -1
- package/apps/runtime/dist/connectors/external.js +38 -37
- package/apps/runtime/dist/connectors/index.js +30 -91
- package/apps/runtime/dist/connectors/mail.js +0 -8
- package/apps/runtime/dist/connectors/person-sign-ins.js +73 -0
- package/apps/runtime/dist/connectors/plugin-connections.js +65 -92
- package/apps/runtime/dist/connectors/smtp.js +1 -1
- package/apps/runtime/dist/connectors/space-connections.js +436 -0
- package/apps/runtime/dist/db/adopt.js +3 -3
- package/apps/runtime/dist/db/client.js +9 -0
- package/apps/runtime/dist/db/live-triggers.js +65 -23
- package/apps/runtime/dist/db/migrations/tenant/0027_work_chats.sql +52 -0
- package/apps/runtime/dist/db/migrations/tenant/0028_chat_approvals.sql +17 -0
- package/apps/runtime/dist/db/migrations/tenant/0029_work_chats_live.sql +49 -0
- package/apps/runtime/dist/db/migrations/tenant/0031_space.sql +624 -0
- package/apps/runtime/dist/db/migrations/tenant/0032_rwx_rule.sql +18 -0
- package/apps/runtime/dist/db/migrations/tenant/0033_broker_log.sql +23 -0
- package/apps/runtime/dist/db/migrations/tenant/0034_space_connection.sql +35 -0
- package/apps/runtime/dist/db/migrations/tenant/0035_run_grant.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/0036_approval_held_for.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/0037_ways_out.sql +5 -0
- package/apps/runtime/dist/db/migrations/tenant/0038_heard.sql +11 -0
- package/apps/runtime/dist/db/migrations/tenant/0039_members_studio.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0027_snapshot.json +383 -155
- package/apps/runtime/dist/db/migrations/tenant/meta/0028_snapshot.json +4468 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0029_snapshot.json +4475 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0030_snapshot.json +4629 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0031_snapshot.json +4629 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0032_snapshot.json +4757 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0033_snapshot.json +5039 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0034_snapshot.json +5022 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0035_snapshot.json +5029 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0036_snapshot.json +5037 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0037_snapshot.json +5076 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0038_snapshot.json +5145 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0039_snapshot.json +5153 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +86 -2
- package/apps/runtime/dist/db/schema.js +295 -72
- package/apps/runtime/dist/docs/dev/architecture.md +215 -0
- package/apps/runtime/dist/docs/dev/broker.md +298 -0
- package/apps/runtime/dist/docs/dev/index.md +5 -1
- package/apps/runtime/dist/docs/dev/integration/index.md +2 -0
- package/apps/runtime/dist/docs/dev/meta.json +3 -0
- package/apps/runtime/dist/docs/dev/plugins/connections.md +8 -5
- package/apps/runtime/dist/docs/dev/plugins/events.md +1 -1
- package/apps/runtime/dist/docs/dev/plugins/examples.md +1 -0
- package/apps/runtime/dist/docs/dev/plugins/index.md +2 -2
- package/apps/runtime/dist/docs/dev/plugins/reference.md +3 -2
- package/apps/runtime/dist/docs/dev/plugins/space.md +16 -6
- package/apps/runtime/dist/docs/dev/plugins/studio.md +2 -2
- package/apps/runtime/dist/docs/dev/plugins/tools.md +0 -1
- package/apps/runtime/dist/docs/dev/skills.md +16 -9
- package/apps/runtime/dist/docs/dev/wizard-format/agent-steps.md +40 -5
- package/apps/runtime/dist/docs/dev/wizard-format/lists-and-connections.md +17 -2
- package/apps/runtime/dist/docs/dev/wizard-format/templates.md +2 -0
- package/apps/runtime/dist/docs/dev/wizard-format/views-and-widgets.md +22 -2
- package/apps/runtime/dist/docs/user-guide/capabilities.md +132 -0
- package/apps/runtime/dist/docs/user-guide/cloud-sync.md +2 -0
- package/apps/runtime/dist/docs/user-guide/command-line.md +1 -0
- package/apps/runtime/dist/docs/user-guide/connectors.md +63 -7
- package/apps/runtime/dist/docs/user-guide/index.md +10 -1
- package/apps/runtime/dist/docs/user-guide/meta.json +6 -1
- package/apps/runtime/dist/docs/user-guide/protocol.md +80 -0
- package/apps/runtime/dist/docs/user-guide/skills.md +8 -4
- package/apps/runtime/dist/docs/user-guide/space.md +5 -4
- package/apps/runtime/dist/docs/user-guide/together.md +16 -2
- package/apps/runtime/dist/docs/user-guide/troubleshooting.md +2 -2
- package/apps/runtime/dist/docs/user-guide/work-mode.md +128 -0
- package/apps/runtime/dist/engenty/connections-external/build-connector.js +2 -5
- package/apps/runtime/dist/engenty/connections-external/importer/classify.js +72 -0
- package/apps/runtime/dist/engenty/connections-google/connectors/gmail.js +1 -1
- package/apps/runtime/dist/engenty/connections-microsoft/outlook.js +3 -2
- package/apps/runtime/dist/engenty/connections-sdk/types.js +2 -0
- package/apps/runtime/dist/engenty/connections-slack/connector.js +2 -1
- package/apps/runtime/dist/engine/resources.js +43 -12
- package/apps/runtime/dist/engine/runner.js +75 -23
- package/apps/runtime/dist/engine/steps.js +30 -12
- package/apps/runtime/dist/engine/template.js +18 -1
- package/apps/runtime/dist/env.js +9 -4
- package/apps/runtime/dist/index.js +18 -2
- package/apps/runtime/dist/live/feed.js +2 -2
- package/apps/runtime/dist/live/origin.js +2 -2
- package/apps/runtime/dist/live/views.js +51 -26
- package/apps/runtime/dist/manage.js +7 -0
- package/apps/runtime/dist/mcp/flow-app.js +1 -1
- package/apps/runtime/dist/mcp/tools.js +75 -34
- package/apps/runtime/dist/models.js +1 -1
- package/apps/runtime/dist/motion-graphics/project.js +8 -8
- package/apps/runtime/dist/motion-graphics/tools.js +1 -1
- package/apps/runtime/dist/plugins/events.js +2 -2
- package/apps/runtime/dist/plugins/jobs.js +4 -1
- package/apps/runtime/dist/plugins/loader.js +2 -2
- package/apps/runtime/dist/prompts/architect.md +3 -3
- package/apps/runtime/dist/prompts/authoring.md +46 -12
- package/apps/runtime/dist/prompts/goose.md +7 -7
- package/apps/runtime/dist/prompts/mcp.md +6 -6
- package/apps/runtime/dist/prompts/services.md +4 -4
- package/apps/runtime/dist/prompts/step.md +2 -2
- package/apps/runtime/dist/prompts/tools.md +39 -10
- package/apps/runtime/dist/routes/api.js +31 -20
- package/apps/runtime/dist/routes/broker.js +316 -0
- package/apps/runtime/dist/routes/connections.js +71 -0
- package/apps/runtime/dist/routes/delivery.js +25 -0
- package/apps/runtime/dist/routes/discover.js +17 -0
- package/apps/runtime/dist/routes/live.js +11 -1
- package/apps/runtime/dist/routes/person.js +4 -1
- package/apps/runtime/dist/routes/plugins.js +3 -1
- package/apps/runtime/dist/routes/rules.js +26 -0
- package/apps/runtime/dist/routes/runs.js +54 -33
- package/apps/runtime/dist/routes/skills.js +6 -6
- package/apps/runtime/dist/routes/studio.js +153 -144
- package/apps/runtime/dist/routes/work.js +199 -0
- package/apps/runtime/dist/sandbox/index.js +27 -0
- package/apps/runtime/dist/services/access.js +38 -17
- package/apps/runtime/dist/services/api.js +38 -13
- package/apps/runtime/dist/services/architect.js +3 -3
- package/apps/runtime/dist/services/assistant-chats.js +9 -1
- package/apps/runtime/dist/services/brand.js +12 -12
- package/apps/runtime/dist/services/broker-bell.js +69 -0
- package/apps/runtime/dist/services/broker-notify.js +127 -0
- package/apps/runtime/dist/services/client-steps.js +79 -0
- package/apps/runtime/dist/services/cloud.js +42 -42
- package/apps/runtime/dist/services/command-risk.js +51 -0
- package/apps/runtime/dist/services/discover.js +85 -0
- package/apps/runtime/dist/services/knowledge-model.js +13 -13
- package/apps/runtime/dist/services/knowledge.js +117 -80
- package/apps/runtime/dist/services/manage-reports.js +74 -7
- package/apps/runtime/dist/services/members-studio.js +165 -0
- package/apps/runtime/dist/services/onboarding.js +22 -22
- package/apps/runtime/dist/services/package-file.js +1 -1
- package/apps/runtime/dist/services/package.js +22 -22
- package/apps/runtime/dist/services/pages.js +7 -7
- package/apps/runtime/dist/services/runs.js +4 -3
- package/apps/runtime/dist/services/seen.js +55 -0
- package/apps/runtime/dist/services/shares.js +23 -13
- package/apps/runtime/dist/services/site.js +23 -23
- package/apps/runtime/dist/services/skills-registry.js +15 -15
- package/apps/runtime/dist/services/skills.js +44 -44
- package/apps/runtime/dist/services/space-categories.js +35 -35
- package/apps/runtime/dist/services/space-data.js +130 -111
- package/apps/runtime/dist/services/{project-files.js → space-files.js} +84 -75
- package/apps/runtime/dist/services/{project-index.js → space-index.js} +116 -73
- package/apps/runtime/dist/services/space-origin.js +19 -11
- package/apps/runtime/dist/services/spaces.js +478 -612
- package/apps/runtime/dist/services/synced-spaces.js +668 -0
- package/apps/runtime/dist/services/themes.js +15 -15
- package/apps/runtime/dist/services/widgets.js +6 -6
- package/apps/runtime/dist/services/wizards.js +112 -38
- package/apps/runtime/dist/services/work-ai.js +257 -0
- package/apps/runtime/dist/services/work-approvals.js +104 -0
- package/apps/runtime/dist/services/work-chats.js +908 -0
- package/apps/runtime/dist/services/work-connections.js +90 -0
- package/apps/runtime/dist/services/work-files.js +85 -0
- package/apps/runtime/dist/services/work-mastra.js +73 -0
- package/apps/runtime/dist/services/work-sandbox.js +42 -0
- package/apps/runtime/dist/sharing/files.js +14 -4
- package/apps/runtime/dist/sharing/index.js +40 -39
- package/apps/runtime/dist/sharing/jobs.js +67 -17
- package/apps/runtime/dist/sharing/targets/connector.js +10 -6
- package/apps/runtime/dist/sharing/targets/email.js +10 -8
- package/apps/runtime/dist/sharing/targets/slack.js +10 -8
- package/apps/runtime/dist/sharing/targets/storage.js +6 -4
- package/apps/runtime/dist/store/index.js +14 -3
- package/apps/runtime/dist/store/shared-lists.js +4 -2
- package/apps/runtime/dist/sync/apply.js +2 -2
- package/apps/runtime/dist/sync/client.js +113 -113
- package/apps/runtime/dist/sync/hub.js +41 -41
- package/apps/runtime/dist/sync/log.js +12 -12
- package/apps/runtime/dist/sync/records.js +118 -120
- package/apps/runtime/dist/texts.js +38 -2
- package/apps/runtime/dist/tools/connections.js +112 -0
- package/apps/runtime/dist/tools/connector.js +157 -67
- package/apps/runtime/dist/tools/index.js +28 -13
- package/apps/runtime/dist/tools/mail.js +40 -11
- package/apps/runtime/dist/tools/pages.js +11 -5
- package/apps/runtime/dist/tools/plugin.js +43 -7
- package/apps/runtime/dist/tools/skills.js +1 -1
- package/apps/runtime/dist/tools/{project.js → space.js} +30 -25
- package/apps/runtime/dist/tools/store.js +3 -2
- package/apps/runtime/dist/widgets/step.js +10 -10
- package/apps/web/dist/assets/AboutDialog-DgfeipUc.js +1 -0
- package/apps/web/dist/assets/CapabilitiesPage-BoYJvgIu.js +2 -0
- package/apps/web/dist/assets/{ChangelogDialog-SwyId00t.js → ChangelogDialog-Duab4zTO.js} +1 -1
- package/apps/web/dist/assets/{CodeEditor-BwZsk5Al.js → CodeEditor-nxoOt8ge.js} +1 -1
- package/apps/web/dist/assets/EditorPage-ZrgGpYMC.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-DtSpjVTl.js → FlowPreview-aMfwcPul.js} +1 -1
- package/apps/web/dist/assets/LangSwitch-DpD0inR4.js +1 -0
- package/apps/web/dist/assets/Marketplace-CpzD9pNQ.js +2 -0
- package/apps/web/dist/assets/NewWizardChannels-IB18afWN.js +1 -0
- package/apps/web/dist/assets/NewWizardPage-rYbcmPGG.js +7 -0
- package/apps/web/dist/assets/OnboardingPage-ztHT01ah.js +1 -0
- package/apps/web/dist/assets/PreviewPane-DviWTLfi.js +9 -0
- package/apps/web/dist/assets/PublicRunner-C2geZhhM.js +1 -0
- package/apps/web/dist/assets/ReadOnly-kSb_Kbdm.js +1 -0
- package/apps/web/dist/assets/SettingsPage-DM2h8N0J.js +2 -0
- package/apps/web/dist/assets/SetupPage-Wfyz6xH4.js +1 -0
- package/apps/web/dist/assets/SharePage-CyWQorWB.js +1 -0
- package/apps/web/dist/assets/SkillEditor-CqQMg2MC.js +1 -0
- package/apps/web/dist/assets/Skills-CnhpgoOA.js +9 -0
- package/apps/web/dist/assets/SpacePage-q4Bk3N7k.js +2 -0
- package/apps/web/dist/assets/UpdateCheck-C2CE6CC3.js +2 -0
- package/apps/web/dist/assets/changelog-B6hN1Cp7.js +1 -0
- package/apps/web/dist/assets/circle-alert-C8Ma-bPY.js +1 -0
- package/apps/web/dist/assets/circle-check-Bu9Z3mlJ.js +1 -0
- package/apps/web/dist/assets/code-editor-CfTGqOpp.js +2 -0
- package/apps/web/dist/assets/{code-xml-TwKzzMtA.js → code-xml-B4kLFZSj.js} +1 -1
- package/apps/web/dist/assets/connection-sign-in-CIiv0t2p.js +1 -0
- package/apps/web/dist/assets/esm-Xwn4GtqG.js +7 -0
- package/apps/web/dist/assets/{eye-Brd-xPUJ.js → eye-BpoKpIdt.js} +1 -1
- package/apps/web/dist/assets/host-BrP-ENHg.css +1 -0
- package/apps/web/dist/assets/host-DJ7-p0Sc.js +118 -0
- package/apps/web/dist/assets/i18n-BJ7Ld6O6.js +55 -0
- package/apps/web/dist/assets/index-D18DvnxX.css +1 -0
- package/apps/web/dist/assets/index-D2akynS-.js +10 -0
- package/apps/web/dist/assets/logo-BpnjXTRy.js +1 -0
- package/apps/web/dist/assets/oss-credits-IdX5bc71.js +1 -0
- package/apps/web/dist/assets/{phone-DhkHteoJ.js → phone-Ctvf-Am-.js} +1 -1
- package/apps/web/dist/assets/read-aloud-Da_RSm2S.js +1 -0
- package/apps/web/dist/assets/{runners-G-XYaDgR.js → runners-CyIvJIHr.js} +1 -1
- package/apps/web/dist/assets/scroll-text-ChrjRu9Y.js +1 -0
- package/apps/web/dist/assets/session-SXPtTWRK.js +2 -0
- package/apps/web/dist/assets/theme-CbTcTFWy.js +11 -0
- package/apps/web/dist/assets/ui-Uqnx1UpK.js +82 -0
- package/apps/web/dist/assets/{upload-BYZTwJTD.js → upload-BchUnQGV.js} +1 -1
- package/apps/web/dist/assets/{useMutation-D0QP_k2-.js → useMutation-DEkZhp5c.js} +1 -1
- package/apps/web/dist/assets/{volume-2-CVHoxdGM.js → volume-2-CU_oy81Z.js} +1 -1
- package/apps/web/dist/assets/with-selector-CoMyWBAX.js +1 -0
- package/apps/web/dist/index.html +17 -10
- package/apps/web/dist/mcp/flow.html +119 -64
- package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +20 -10
- package/node_modules/@engenty/wizards-shared/dist/assistant.d.ts +6 -1
- package/node_modules/@engenty/wizards-shared/dist/assistant.js +6 -1
- package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +236 -2
- package/node_modules/@engenty/wizards-shared/dist/connectors.js +52 -2
- package/node_modules/@engenty/wizards-shared/dist/definition.d.ts +150 -8
- package/node_modules/@engenty/wizards-shared/dist/definition.js +46 -11
- package/node_modules/@engenty/wizards-shared/dist/discover.d.ts +21 -0
- package/node_modules/@engenty/wizards-shared/dist/discover.js +1 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +26 -2
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +26 -2
- package/node_modules/@engenty/wizards-shared/dist/fit.js +1 -1
- package/node_modules/@engenty/wizards-shared/dist/json-schema.d.ts +1 -1
- package/node_modules/@engenty/wizards-shared/dist/json-schema.js +1 -1
- package/node_modules/@engenty/wizards-shared/dist/live.d.ts +2 -2
- package/node_modules/@engenty/wizards-shared/dist/live.js +1 -1
- package/node_modules/@engenty/wizards-shared/dist/marketplace.js +3 -1
- package/node_modules/@engenty/wizards-shared/dist/run.d.ts +54 -6
- package/node_modules/@engenty/wizards-shared/dist/run.js +85 -0
- package/node_modules/@engenty/wizards-shared/dist/runner-texts.d.ts +15 -1
- package/node_modules/@engenty/wizards-shared/dist/runner-texts.js +14 -0
- package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +42 -0
- package/node_modules/@engenty/wizards-shared/dist/rwx.js +29 -0
- package/node_modules/@engenty/wizards-shared/dist/skills.d.ts +1 -1
- package/node_modules/@engenty/wizards-shared/dist/space-data.d.ts +2 -2
- package/node_modules/@engenty/wizards-shared/dist/{projects.d.ts → spaces.d.ts} +39 -15
- package/node_modules/@engenty/wizards-shared/dist/{projects.js → spaces.js} +16 -9
- package/node_modules/@engenty/wizards-shared/dist/store.d.ts +7 -2
- package/node_modules/@engenty/wizards-shared/dist/surface.d.ts +35 -4
- package/node_modules/@engenty/wizards-shared/dist/surface.js +121 -3
- package/node_modules/@engenty/wizards-shared/dist/sync.d.ts +1 -1
- package/node_modules/@engenty/wizards-shared/dist/theme-look.js +1 -1
- package/node_modules/@engenty/wizards-shared/dist/wizard-package.d.ts +2 -2
- package/node_modules/@engenty/wizards-shared/dist/wizard-package.js +3 -3
- package/package.json +4 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/plugin.json +1 -1
- package/plugin/skills/build-wizard/SKILL.md +1 -1
- package/apps/runtime/dist/services/projects.js +0 -473
- package/apps/web/dist/assets/AboutDialog-B-JxkELB.js +0 -1
- package/apps/web/dist/assets/EditorPage-DQ_2FOzI.js +0 -3
- package/apps/web/dist/assets/Marketplace-DoH4YO3D.js +0 -2
- package/apps/web/dist/assets/NewWizardChannels-Tgoexgj4.js +0 -1
- package/apps/web/dist/assets/NewWizardPage-B24AF482.js +0 -7
- package/apps/web/dist/assets/OnboardingPage-BEvq4slJ.js +0 -1
- package/apps/web/dist/assets/PreviewPane-B_v7Y8R7.js +0 -10
- package/apps/web/dist/assets/ProjectPage-ClRuhFJo.js +0 -10
- package/apps/web/dist/assets/PublicRunner-MicrpVsv.js +0 -1
- package/apps/web/dist/assets/ReadOnly-BXjv-MSv.js +0 -1
- package/apps/web/dist/assets/SettingsPage-DY8O1r43.js +0 -2
- package/apps/web/dist/assets/SetupPage-BOVkVpMu.js +0 -1
- package/apps/web/dist/assets/SharePage-DXGzJSnR.js +0 -1
- package/apps/web/dist/assets/SkillEditor-BzzTDeaU.js +0 -1
- package/apps/web/dist/assets/changelog-BLdS-0Bj.js +0 -1
- package/apps/web/dist/assets/esm-BdxDT9Km.js +0 -7
- package/apps/web/dist/assets/i18n-8-Sw11tB.js +0 -55
- package/apps/web/dist/assets/index-OsdQFHmh.js +0 -49
- package/apps/web/dist/assets/index-QPipX5Fi.css +0 -1
- package/apps/web/dist/assets/lock-C0uxcOlq.js +0 -1
- package/apps/web/dist/assets/logo-CGNoptDg.js +0 -1
- package/apps/web/dist/assets/oss-credits-DpRWUPjD.js +0 -1
- package/apps/web/dist/assets/session-BWI-KENJ.js +0 -2
- package/apps/web/dist/assets/theme-D7krsx0_.js +0 -11
- package/apps/web/dist/assets/ui-DSXG3f49.js +0 -83
- /package/apps/runtime/dist/db/migrations/tenant/{0027_space_skill.sql → 0030_space_skill.sql} +0 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { and, desc, eq, gt, inArray, lt, not, or, sql } from "drizzle-orm";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
import { db, schema } from "../db/client.js";
|
|
5
|
+
/**
|
|
6
|
+
* What the broker's admins hear of (docs/plans/broker.md, "Notifications"), one of
|
|
7
|
+
* `CRITICAL_KINDS` (shared/connectors): `refused_write` – a *Nie*, the grant or the AI-step rule
|
|
8
|
+
* stopped a write, a delete or a send; `outside_audience` – a guest's run reached a connection
|
|
9
|
+
* that is for the space's members or admins; `host_not_in_grant`; `limit_reached` – a guest's run
|
|
10
|
+
* or a wizard's guests have sent their mails for the run or the day; `sign_in_failed` – a sign-in
|
|
11
|
+
* ran out or the service refused it; `first_use` of a connection of the space; `rule_changed` –
|
|
12
|
+
* RWX, or a connection's rules or audience.
|
|
13
|
+
*/
|
|
14
|
+
export { CRITICAL_KINDS } from "@engenty/wizards-shared/connectors";
|
|
15
|
+
/** The action under which a changed rule is logged; the connection is the one changed, or `rwx`. */
|
|
16
|
+
export const RULE_CHANGE_ACTION = "change_rules";
|
|
17
|
+
/** The result under which a sign-in that ran out or was refused is logged. */
|
|
18
|
+
export const SIGN_IN_FAILED = "sign_in_failed";
|
|
19
|
+
/**
|
|
20
|
+
* Which critical kind a row is, if any: the one place that decides. Every refusal of a write, a
|
|
21
|
+
* delete or a send is critical; a refused read only where a guest reached outside a connection's
|
|
22
|
+
* audience. A connection's first use and a changed rule are critical though nothing was refused.
|
|
23
|
+
*/
|
|
24
|
+
export function criticalOf(row, context = {}) {
|
|
25
|
+
if (row.actionId === RULE_CHANGE_ACTION) {
|
|
26
|
+
return "rule_changed";
|
|
27
|
+
}
|
|
28
|
+
if (row.result === SIGN_IN_FAILED) {
|
|
29
|
+
return "sign_in_failed";
|
|
30
|
+
}
|
|
31
|
+
if (row.outcome === "deny") {
|
|
32
|
+
if (row.result === "run_limit" || row.result === "day_limit") {
|
|
33
|
+
return "limit_reached";
|
|
34
|
+
}
|
|
35
|
+
if (row.connectionId.startsWith("host:") && row.result === "not_in_grant") {
|
|
36
|
+
return "host_not_in_grant";
|
|
37
|
+
}
|
|
38
|
+
if (row.rule === "audience" && row.whoKind === "guest") {
|
|
39
|
+
return "outside_audience";
|
|
40
|
+
}
|
|
41
|
+
return row.kind === "read" ? null : "refused_write";
|
|
42
|
+
}
|
|
43
|
+
return row.result === "ok" && context.firstUse ? "first_use" : null;
|
|
44
|
+
}
|
|
45
|
+
/** A field whose value is never written down, whatever the action calls it. */
|
|
46
|
+
const SECRET = /token|secret|password|passwd|key|auth|credential|cookie|session|bearer|_base64$/i;
|
|
47
|
+
/** The widest a logged value gets. */
|
|
48
|
+
const SAMPLE = 80;
|
|
49
|
+
/** What the log keeps of a call's input. */
|
|
50
|
+
export function summarizeInput(input) {
|
|
51
|
+
const whole = JSON.stringify(input ?? null) ?? "null";
|
|
52
|
+
const fields = {};
|
|
53
|
+
let sample = null;
|
|
54
|
+
if (input && typeof input === "object" && !Array.isArray(input)) {
|
|
55
|
+
for (const [key, value] of Object.entries(input)) {
|
|
56
|
+
fields[key] = typeof value === "string" ? value.length : (JSON.stringify(value) ?? "").length;
|
|
57
|
+
if (sample === null && typeof value === "string" && value && !SECRET.test(key)) {
|
|
58
|
+
sample = `${key}: ${value.replace(/\s+/g, " ").slice(0, SAMPLE)}`;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return JSON.stringify({
|
|
63
|
+
fields,
|
|
64
|
+
sample,
|
|
65
|
+
size: whole.length,
|
|
66
|
+
sha256: createHash("sha256").update(whole).digest("hex"),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const listeners = new Set();
|
|
70
|
+
/** Hears every critical row as it is written: the report to the Manage-App (services/manage-reports.ts). */
|
|
71
|
+
export function onCritical(listener) {
|
|
72
|
+
listeners.add(listener);
|
|
73
|
+
return () => listeners.delete(listener);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Writes one row, deciding whether it is critical; a log that cannot be written never stops the
|
|
77
|
+
* call. A critical row is also a line of the runtime's own log, so the log ring and „wizards
|
|
78
|
+
* report“ carry it (docs/plans/remote-debug.md), and goes to whoever listens.
|
|
79
|
+
*/
|
|
80
|
+
export async function writeLog(row, context = {}) {
|
|
81
|
+
const criticalKind = criticalOf(row, context);
|
|
82
|
+
const written = { id: nanoid(14), ...row, critical: criticalKind !== null, criticalKind };
|
|
83
|
+
try {
|
|
84
|
+
await db.insert(schema.brokerLog).values(written);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
console.error("[broker] the log could not be written:", err);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (criticalKind) {
|
|
91
|
+
console.warn(`[broker] ${criticalKind}: ${row.whoKind}${row.whoId ? ` ${row.whoId}` : ""} → ${row.connectionId} ${row.actionId} (${row.kind}): ${row.outcome}, ${row.result ?? ""}${row.reason ? ` – ${row.reason}` : ""}`);
|
|
92
|
+
const full = written;
|
|
93
|
+
for (const listener of listeners) {
|
|
94
|
+
try {
|
|
95
|
+
listener(full);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
console.error("[broker] a listener failed:", err);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** A rule changed by an admin: RWX (`connectionId` "rwx") or a connection's rules or audience. */
|
|
104
|
+
export function logRuleChange(change) {
|
|
105
|
+
return writeLog({
|
|
106
|
+
at: new Date(),
|
|
107
|
+
durationMs: 0,
|
|
108
|
+
whoKind: "admin",
|
|
109
|
+
whoId: change.userId,
|
|
110
|
+
runId: null,
|
|
111
|
+
stepId: null,
|
|
112
|
+
chatId: null,
|
|
113
|
+
wizardId: null,
|
|
114
|
+
spaceId: change.spaceId,
|
|
115
|
+
connectionId: change.connectionId,
|
|
116
|
+
actionId: RULE_CHANGE_ACTION,
|
|
117
|
+
kind: "write",
|
|
118
|
+
inputSummary: summarizeInput(change.summary),
|
|
119
|
+
outcome: "allow",
|
|
120
|
+
rule: null,
|
|
121
|
+
reason: change.reason,
|
|
122
|
+
result: "ok",
|
|
123
|
+
resultSize: null,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/** The cursor as the API hands it out: `<ms>.<id>`. */
|
|
127
|
+
export function cursorOf(row) {
|
|
128
|
+
return `${row.at.getTime()}.${row.id}`;
|
|
129
|
+
}
|
|
130
|
+
/** A cursor the API took; null where it is none. */
|
|
131
|
+
export function readCursor(text) {
|
|
132
|
+
const match = /^(\d{1,15})\.([A-Za-z0-9_-]{1,40})$/.exec(text);
|
|
133
|
+
return match ? { at: new Date(Number(match[1])), id: match[2] } : null;
|
|
134
|
+
}
|
|
135
|
+
/** The widest the log answers at once. */
|
|
136
|
+
export const LOG_PAGE = 200;
|
|
137
|
+
/** The results of a call the person asked turned down, beside an `outcome` of `deny`. */
|
|
138
|
+
const REFUSED_BY_PERSON = ["declined"];
|
|
139
|
+
/** `%` and `_` stand for themselves in a search. */
|
|
140
|
+
function likeOf(text) {
|
|
141
|
+
return `%${text.replace(/[\\%_]/g, (c) => `\\${c}`)}%`;
|
|
142
|
+
}
|
|
143
|
+
/** A space's log, newest first, filtered. */
|
|
144
|
+
export async function logOfSpace(spaceId, query = {}) {
|
|
145
|
+
const log = schema.brokerLog;
|
|
146
|
+
const where = [eq(log.spaceId, spaceId)];
|
|
147
|
+
if (query.connection) {
|
|
148
|
+
where.push(eq(log.connectionId, query.connection));
|
|
149
|
+
}
|
|
150
|
+
if (query.who) {
|
|
151
|
+
where.push(eq(log.whoKind, query.who));
|
|
152
|
+
}
|
|
153
|
+
if (query.outcome) {
|
|
154
|
+
where.push(eq(log.outcome, query.outcome));
|
|
155
|
+
}
|
|
156
|
+
if (query.result) {
|
|
157
|
+
where.push(eq(log.result, query.result));
|
|
158
|
+
}
|
|
159
|
+
if (query.refused !== undefined) {
|
|
160
|
+
const refused = or(eq(log.outcome, "deny"), and(eq(log.outcome, "ask"), inArray(log.result, REFUSED_BY_PERSON)));
|
|
161
|
+
where.push(query.refused ? refused : not(refused));
|
|
162
|
+
}
|
|
163
|
+
if (query.critical !== undefined) {
|
|
164
|
+
where.push(eq(log.critical, query.critical));
|
|
165
|
+
}
|
|
166
|
+
if (query.since) {
|
|
167
|
+
where.push(gt(log.at, query.since));
|
|
168
|
+
}
|
|
169
|
+
if (query.until) {
|
|
170
|
+
where.push(lt(log.at, query.until));
|
|
171
|
+
}
|
|
172
|
+
const text = query.q?.trim();
|
|
173
|
+
if (text) {
|
|
174
|
+
const like = likeOf(text);
|
|
175
|
+
const found = [
|
|
176
|
+
log.actionId,
|
|
177
|
+
log.connectionId,
|
|
178
|
+
log.reason,
|
|
179
|
+
log.result,
|
|
180
|
+
log.rule,
|
|
181
|
+
log.whoId,
|
|
182
|
+
log.inputSummary,
|
|
183
|
+
].map((column) => sql `${column} like ${like} escape '\\'`);
|
|
184
|
+
const { who = [], connections = [], wizards = [] } = query.matches ?? {};
|
|
185
|
+
if (who.length) {
|
|
186
|
+
found.push(inArray(log.whoId, who));
|
|
187
|
+
}
|
|
188
|
+
if (connections.length) {
|
|
189
|
+
found.push(inArray(log.connectionId, connections));
|
|
190
|
+
}
|
|
191
|
+
if (wizards.length) {
|
|
192
|
+
found.push(inArray(log.wizardId, wizards));
|
|
193
|
+
}
|
|
194
|
+
where.push(or(...found));
|
|
195
|
+
}
|
|
196
|
+
if (query.before) {
|
|
197
|
+
where.push(or(lt(log.at, query.before.at), and(eq(log.at, query.before.at), lt(log.id, query.before.id))));
|
|
198
|
+
}
|
|
199
|
+
return db.query.brokerLog.findMany({
|
|
200
|
+
where: and(...where),
|
|
201
|
+
orderBy: [desc(log.at), desc(log.id)],
|
|
202
|
+
limit: Math.min(Math.max(query.limit ?? LOG_PAGE, 1), LOG_PAGE),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/** One line of a space's log. */
|
|
206
|
+
export async function logLine(spaceId, id) {
|
|
207
|
+
const row = await db.query.brokerLog.findFirst({
|
|
208
|
+
where: and(eq(schema.brokerLog.spaceId, spaceId), eq(schema.brokerLog.id, id)),
|
|
209
|
+
});
|
|
210
|
+
return row ?? null;
|
|
211
|
+
}
|
|
212
|
+
/** The critical rows of these spaces since a time, newest first. */
|
|
213
|
+
export async function criticalSince(spaceIds, since, limit = LOG_PAGE) {
|
|
214
|
+
if (!spaceIds.length) {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
return db.query.brokerLog.findMany({
|
|
218
|
+
where: and(inArray(schema.brokerLog.spaceId, spaceIds), eq(schema.brokerLog.critical, true), gt(schema.brokerLog.at, since)),
|
|
219
|
+
orderBy: [desc(schema.brokerLog.at)],
|
|
220
|
+
limit,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/** When a connection was last used, by whom and for what: „Letzte Nutzung“. */
|
|
224
|
+
export async function lastUseOf(connectionId,
|
|
225
|
+
/** A wizard's connection ids repeat across wizards: name the wizard to tell them apart. */
|
|
226
|
+
within = {}) {
|
|
227
|
+
const row = await db.query.brokerLog.findFirst({
|
|
228
|
+
where: and(eq(schema.brokerLog.connectionId, connectionId), ...(within.wizardId ? [eq(schema.brokerLog.wizardId, within.wizardId)] : [])),
|
|
229
|
+
orderBy: [desc(schema.brokerLog.at)],
|
|
230
|
+
});
|
|
231
|
+
return row
|
|
232
|
+
? {
|
|
233
|
+
at: row.at,
|
|
234
|
+
who: { kind: row.whoKind, id: row.whoId },
|
|
235
|
+
actionId: row.actionId,
|
|
236
|
+
kind: row.kind,
|
|
237
|
+
outcome: row.outcome,
|
|
238
|
+
}
|
|
239
|
+
: null;
|
|
240
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const reaches = new Map();
|
|
2
|
+
/** A tool or target (by its id) reaches outside through these actions. Registering twice is fine. */
|
|
3
|
+
export function reachesOutside(id, list) {
|
|
4
|
+
reaches.set(id, Array.isArray(list) ? list : [list]);
|
|
5
|
+
}
|
|
6
|
+
/** What is registered, for tests and the Protokoll page later. */
|
|
7
|
+
export function outsideReaches() {
|
|
8
|
+
return reaches;
|
|
9
|
+
}
|
|
@@ -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
|
+
}
|