@engenty/wizards-runtime 0.13.0 → 0.14.1
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 +26 -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/{0027_space_skill.sql → 0030_space_skill.sql} +8 -8
- 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-SpDcn8VZ.js +1 -0
- package/apps/web/dist/assets/CapabilitiesPage-Bgnm_3dk.js +2 -0
- package/apps/web/dist/assets/{ChangelogDialog-SwyId00t.js → ChangelogDialog-DAnjV6cZ.js} +1 -1
- package/apps/web/dist/assets/{CodeEditor-BwZsk5Al.js → CodeEditor-nxoOt8ge.js} +1 -1
- package/apps/web/dist/assets/EditorPage-B98C-9Nv.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-D8IYLg_S.js +1 -0
- package/apps/web/dist/assets/NewWizardPage-DlBt0Mfq.js +7 -0
- package/apps/web/dist/assets/OnboardingPage-ztHT01ah.js +1 -0
- package/apps/web/dist/assets/PreviewPane-DC1kAi3x.js +9 -0
- package/apps/web/dist/assets/PublicRunner-D6lKP0j8.js +1 -0
- package/apps/web/dist/assets/ReadOnly-DFsuKPCw.js +1 -0
- package/apps/web/dist/assets/SettingsPage-DkHeCO2c.js +2 -0
- package/apps/web/dist/assets/SetupPage-DZ8xRkfS.js +1 -0
- package/apps/web/dist/assets/SharePage-494ZPsjQ.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-CWlU_BZP.js +2 -0
- package/apps/web/dist/assets/UpdateCheck-BKNkNJyG.js +2 -0
- package/apps/web/dist/assets/changelog-BhMZS-rs.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-Cngd_QDO.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-C0WQlRKB.js +118 -0
- package/apps/web/dist/assets/i18n-BJ7Ld6O6.js +55 -0
- package/apps/web/dist/assets/index-ClVtdDUZ.js +10 -0
- package/apps/web/dist/assets/index-D18DvnxX.css +1 -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-BCau3f6O.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
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import { CONNECTION_ACTION_KINDS, CONNECTION_AUDIENCES, CONNECTION_LIMITS, CONNECTION_OUTCOMES, CONNECTION_SIGN_INS, DEFAULT_CONNECTION_RULES, } from "@engenty/wizards-shared/connectors";
|
|
2
|
+
import { note } from "@engenty/wizards-shared/run";
|
|
3
|
+
import { and, asc, eq, inArray } from "drizzle-orm";
|
|
4
|
+
import { nanoid } from "nanoid";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { inAudience, logRuleChange } from "../broker/index.js";
|
|
7
|
+
import { db, schema } from "../db/client.js";
|
|
8
|
+
import { buildAuthorizationUrl, createOAuth2Pkce, exchangeAuthorizationCode, } from "../engenty/connections-sdk/oauth2.js";
|
|
9
|
+
import { seal, unseal } from "../secrets/crypto.js";
|
|
10
|
+
import { isAdminRole, requireAdmin, requireWritableSpace } from "../services/access.js";
|
|
11
|
+
import { notFound, ServiceError } from "../services/errors.js";
|
|
12
|
+
import { ownedSpace } from "../services/spaces.js";
|
|
13
|
+
import { currentTenant } from "../tenants/tenant.js";
|
|
14
|
+
import { connectorFetch, resolveEnv, viaAccount } from "./builtin.js";
|
|
15
|
+
import { resolveConnector } from "./external.js";
|
|
16
|
+
import { connectRedirectUri as redirectUri } from "./index.js";
|
|
17
|
+
import { AccountConnectError, redeemTicket, startThroughAccount } from "./via-account.js";
|
|
18
|
+
export const rulesSchema = z.object(Object.fromEntries(CONNECTION_ACTION_KINDS.map((k) => [k, z.enum(CONNECTION_OUTCOMES)])));
|
|
19
|
+
export const newConnectionSchema = z.object({
|
|
20
|
+
connectorId: z.string().min(1).max(200),
|
|
21
|
+
name: z.string().trim().min(1).max(CONNECTION_LIMITS.name),
|
|
22
|
+
signIn: z.enum(CONNECTION_SIGN_INS),
|
|
23
|
+
audience: z.enum(CONNECTION_AUDIENCES).default("members"),
|
|
24
|
+
rules: rulesSchema.default(DEFAULT_CONNECTION_RULES),
|
|
25
|
+
/** The connector's actions it is for; left out: every action. */
|
|
26
|
+
actions: z.array(z.string().max(200)).max(200).default([]),
|
|
27
|
+
});
|
|
28
|
+
export const connectionPatchSchema = z.object({
|
|
29
|
+
name: z.string().trim().min(1).max(CONNECTION_LIMITS.name).optional(),
|
|
30
|
+
audience: z.enum(CONNECTION_AUDIENCES).optional(),
|
|
31
|
+
rules: rulesSchema.optional(),
|
|
32
|
+
actions: z.array(z.string().max(200)).max(200).optional(),
|
|
33
|
+
});
|
|
34
|
+
// --- reading ---------------------------------------------------------------------------------
|
|
35
|
+
/** A connection by id, in this tenant; not found where the id is another tenant's or nobody's. */
|
|
36
|
+
export async function spaceConnectionById(id) {
|
|
37
|
+
const row = await db.query.spaceConnection.findFirst({
|
|
38
|
+
where: eq(schema.spaceConnection.id, id),
|
|
39
|
+
});
|
|
40
|
+
if (!row) {
|
|
41
|
+
throw notFound();
|
|
42
|
+
}
|
|
43
|
+
// The space check says whether the row is this tenant's.
|
|
44
|
+
await ownedSpace("", row.spaceId);
|
|
45
|
+
return row;
|
|
46
|
+
}
|
|
47
|
+
/** A connection of this space by id, or null. */
|
|
48
|
+
export async function spaceConnectionOf(spaceId, id) {
|
|
49
|
+
const row = await db.query.spaceConnection.findFirst({
|
|
50
|
+
where: and(eq(schema.spaceConnection.id, id), eq(schema.spaceConnection.spaceId, spaceId)),
|
|
51
|
+
});
|
|
52
|
+
return row ?? null;
|
|
53
|
+
}
|
|
54
|
+
export async function listSpaceConnections(spaceId) {
|
|
55
|
+
return db.query.spaceConnection.findMany({
|
|
56
|
+
where: eq(schema.spaceConnection.spaceId, spaceId),
|
|
57
|
+
orderBy: [asc(schema.spaceConnection.setUpAt)],
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/** The connector behind a connection: a built-in one, or one the space imported. */
|
|
61
|
+
export async function connectorOfRow(row) {
|
|
62
|
+
const found = await resolveConnector(row.spaceId, row.connectorId);
|
|
63
|
+
if (!found) {
|
|
64
|
+
throw new ServiceError("not_found", note("connectorGone", { id: row.connectorId }));
|
|
65
|
+
}
|
|
66
|
+
return found.connector;
|
|
67
|
+
}
|
|
68
|
+
/** The space's connections as the authoring guide lists them: id, name, connector, sign-in, actions. */
|
|
69
|
+
export async function guideSpaceConnections(spaceId) {
|
|
70
|
+
const rows = await listSpaceConnections(spaceId);
|
|
71
|
+
const listed = [];
|
|
72
|
+
for (const row of rows) {
|
|
73
|
+
const connector = (await resolveConnector(spaceId, row.connectorId))?.connector;
|
|
74
|
+
if (!connector) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const actions = row.actions.length
|
|
78
|
+
? connector.actions.filter((a) => row.actions.includes(a.id))
|
|
79
|
+
: connector.actions;
|
|
80
|
+
listed.push({
|
|
81
|
+
id: row.id,
|
|
82
|
+
name: row.name,
|
|
83
|
+
connector: connector.name,
|
|
84
|
+
signIn: row.signIn,
|
|
85
|
+
actions: actions.map((a) => ({ id: a.id, group: a.group })),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return listed;
|
|
89
|
+
}
|
|
90
|
+
/** What the studio shows: never a secret. `userId` tells whose own sign-in to look up. */
|
|
91
|
+
export async function spaceConnectionViews(spaceId, userId) {
|
|
92
|
+
const rows = await listSpaceConnections(spaceId);
|
|
93
|
+
const people = rows.some((row) => row.signIn === "person");
|
|
94
|
+
const signIns = people
|
|
95
|
+
? await db
|
|
96
|
+
.select({
|
|
97
|
+
connectionId: schema.spaceConnectionSignIn.connectionId,
|
|
98
|
+
userId: schema.spaceConnectionSignIn.userId,
|
|
99
|
+
label: schema.spaceConnectionSignIn.label,
|
|
100
|
+
signedInAt: schema.spaceConnectionSignIn.signedInAt,
|
|
101
|
+
})
|
|
102
|
+
.from(schema.spaceConnectionSignIn)
|
|
103
|
+
.where(inArray(schema.spaceConnectionSignIn.connectionId, rows.map((row) => row.id)))
|
|
104
|
+
: [];
|
|
105
|
+
return Promise.all(rows.map(async (row) => {
|
|
106
|
+
const connector = (await resolveConnector(spaceId, row.connectorId))?.connector ?? null;
|
|
107
|
+
const theirs = row.signIn === "person" ? signIns.filter((s) => s.connectionId === row.id) : [];
|
|
108
|
+
const mine = theirs.find((s) => s.userId === userId);
|
|
109
|
+
return {
|
|
110
|
+
id: row.id,
|
|
111
|
+
spaceId: row.spaceId,
|
|
112
|
+
connectorId: row.connectorId,
|
|
113
|
+
connector: connector
|
|
114
|
+
? {
|
|
115
|
+
id: connector.id,
|
|
116
|
+
name: connector.name,
|
|
117
|
+
auth: connector.auth.kind,
|
|
118
|
+
description: connector.description,
|
|
119
|
+
...(connector.auth.kind === "api_key"
|
|
120
|
+
? { fields: connector.auth.apiKey.fields }
|
|
121
|
+
: {}),
|
|
122
|
+
}
|
|
123
|
+
: null,
|
|
124
|
+
name: row.name,
|
|
125
|
+
signIn: row.signIn,
|
|
126
|
+
audience: row.audience,
|
|
127
|
+
rules: row.rules,
|
|
128
|
+
actions: row.actions,
|
|
129
|
+
plugin: row.plugin,
|
|
130
|
+
account: row.signIn === "space" && row.secret
|
|
131
|
+
? {
|
|
132
|
+
label: row.label ?? connector?.name ?? row.name,
|
|
133
|
+
signedInAt: row.setUpAt.toISOString(),
|
|
134
|
+
}
|
|
135
|
+
: null,
|
|
136
|
+
mySignIn: row.signIn === "person" && mine
|
|
137
|
+
? { label: mine.label, signedInAt: mine.signedInAt.toISOString() }
|
|
138
|
+
: null,
|
|
139
|
+
setUpBy: row.setUpBy,
|
|
140
|
+
setUpAt: row.setUpAt.toISOString(),
|
|
141
|
+
lastUsedAt: row.lastUsedAt?.toISOString() ?? null,
|
|
142
|
+
signedIn: theirs.length,
|
|
143
|
+
};
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
// --- setting up, admins only -----------------------------------------------------------------
|
|
147
|
+
/** The actions named exist on the connector. */
|
|
148
|
+
function knownActions(connector, actions) {
|
|
149
|
+
for (const id of actions) {
|
|
150
|
+
if (!connector.actions.some((a) => a.id === id)) {
|
|
151
|
+
throw new ServiceError("invalid", note("connectorNoAction", { name: connector.name, id }));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return [...new Set(actions)];
|
|
155
|
+
}
|
|
156
|
+
export async function createSpaceConnection(userId, spaceId, input,
|
|
157
|
+
/** The plugin that sets it up through `server.connections`, where one does. */
|
|
158
|
+
plugin = null) {
|
|
159
|
+
await ownedSpace(userId ?? "", spaceId);
|
|
160
|
+
await requireWritableSpace(spaceId);
|
|
161
|
+
await requireAdmin();
|
|
162
|
+
const found = await resolveConnector(spaceId, input.connectorId);
|
|
163
|
+
if (!found || found.connector.auth.kind === "browser") {
|
|
164
|
+
throw new ServiceError("invalid", note("connectorGone", { id: input.connectorId }));
|
|
165
|
+
}
|
|
166
|
+
const count = await db.$count(schema.spaceConnection, eq(schema.spaceConnection.spaceId, spaceId));
|
|
167
|
+
if (count >= CONNECTION_LIMITS.perSpace) {
|
|
168
|
+
throw new ServiceError("refused", note("tooManyConnections"));
|
|
169
|
+
}
|
|
170
|
+
const [row] = await db
|
|
171
|
+
.insert(schema.spaceConnection)
|
|
172
|
+
.values({
|
|
173
|
+
id: nanoid(14),
|
|
174
|
+
spaceId,
|
|
175
|
+
connectorId: found.connector.id,
|
|
176
|
+
plugin,
|
|
177
|
+
name: input.name,
|
|
178
|
+
signIn: input.signIn,
|
|
179
|
+
audience: input.audience,
|
|
180
|
+
rules: input.rules,
|
|
181
|
+
actions: knownActions(found.connector, input.actions),
|
|
182
|
+
setUpBy: userId,
|
|
183
|
+
})
|
|
184
|
+
.returning();
|
|
185
|
+
return row;
|
|
186
|
+
}
|
|
187
|
+
export async function updateSpaceConnection(userId, id, patch) {
|
|
188
|
+
const row = await spaceConnectionById(id);
|
|
189
|
+
await ownedSpace(userId, row.spaceId);
|
|
190
|
+
await requireWritableSpace(row.spaceId);
|
|
191
|
+
await requireAdmin();
|
|
192
|
+
const actions = patch.actions !== undefined
|
|
193
|
+
? knownActions(await connectorOfRow(row), patch.actions)
|
|
194
|
+
: undefined;
|
|
195
|
+
const [updated] = await db
|
|
196
|
+
.update(schema.spaceConnection)
|
|
197
|
+
.set({
|
|
198
|
+
...(patch.name !== undefined ? { name: patch.name } : {}),
|
|
199
|
+
...(patch.audience !== undefined ? { audience: patch.audience } : {}),
|
|
200
|
+
...(patch.rules !== undefined ? { rules: patch.rules } : {}),
|
|
201
|
+
...(actions !== undefined ? { actions } : {}),
|
|
202
|
+
updatedAt: new Date(),
|
|
203
|
+
})
|
|
204
|
+
.where(eq(schema.spaceConnection.id, row.id))
|
|
205
|
+
.returning();
|
|
206
|
+
// Who may use it, and what each kind of action may do: a change there is heard of.
|
|
207
|
+
const rulesChanged = patch.rules !== undefined && JSON.stringify(patch.rules) !== JSON.stringify(row.rules);
|
|
208
|
+
const audienceChanged = patch.audience !== undefined && patch.audience !== row.audience;
|
|
209
|
+
if (rulesChanged || audienceChanged) {
|
|
210
|
+
await logRuleChange({
|
|
211
|
+
userId,
|
|
212
|
+
spaceId: row.spaceId,
|
|
213
|
+
connectionId: row.id,
|
|
214
|
+
summary: {
|
|
215
|
+
...(audienceChanged ? { audience: `${row.audience} → ${updated.audience}` } : {}),
|
|
216
|
+
...(rulesChanged ? { rules: changedRules(row.rules, updated.rules) } : {}),
|
|
217
|
+
},
|
|
218
|
+
reason: [
|
|
219
|
+
audienceChanged ? `The audience is now ${updated.audience}.` : "",
|
|
220
|
+
rulesChanged ? `The rules changed: ${changedRules(row.rules, updated.rules)}.` : "",
|
|
221
|
+
]
|
|
222
|
+
.filter(Boolean)
|
|
223
|
+
.join(" "),
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return updated;
|
|
227
|
+
}
|
|
228
|
+
/** "write: ask → allow, send: ask → deny" */
|
|
229
|
+
function changedRules(before, after) {
|
|
230
|
+
return Object.keys(after)
|
|
231
|
+
.filter((k) => before[k] !== after[k])
|
|
232
|
+
.map((k) => `${k}: ${before[k]} → ${after[k]}`)
|
|
233
|
+
.join(", ");
|
|
234
|
+
}
|
|
235
|
+
export async function removeSpaceConnection(userId, id) {
|
|
236
|
+
const row = await spaceConnectionById(id);
|
|
237
|
+
await ownedSpace(userId, row.spaceId);
|
|
238
|
+
await requireWritableSpace(row.spaceId);
|
|
239
|
+
await requireAdmin();
|
|
240
|
+
await db.delete(schema.spaceConnection).where(eq(schema.spaceConnection.id, row.id));
|
|
241
|
+
}
|
|
242
|
+
/** The admins sign in as the space; a member signs in to a connection in which each person does, if it is for them. */
|
|
243
|
+
async function allowSignIn(row, as) {
|
|
244
|
+
if (as.as === "space") {
|
|
245
|
+
if (row.signIn !== "space") {
|
|
246
|
+
throw new ServiceError("invalid", note("connectionEachPersonSignsIn"));
|
|
247
|
+
}
|
|
248
|
+
await requireAdmin();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (row.signIn !== "person") {
|
|
252
|
+
throw new ServiceError("invalid", note("connectionSignsInAsSpace"));
|
|
253
|
+
}
|
|
254
|
+
const who = { kind: isAdminRole(as.role) ? "admin" : "person", id: as.userId };
|
|
255
|
+
if (!inAudience(row.audience, who)) {
|
|
256
|
+
throw new ServiceError("forbidden", note("connectionNotForYou"));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/** Keeps a sign-in: the space's in its row, a person's in theirs. */
|
|
260
|
+
async function keepSignIn(row, as, label, stored) {
|
|
261
|
+
if (as.as === "space") {
|
|
262
|
+
await db
|
|
263
|
+
.update(schema.spaceConnection)
|
|
264
|
+
.set({ secret: seal(stored), label, updatedAt: new Date() })
|
|
265
|
+
.where(eq(schema.spaceConnection.id, row.id));
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
await db
|
|
269
|
+
.insert(schema.spaceConnectionSignIn)
|
|
270
|
+
.values({ connectionId: row.id, userId: as.userId, label, secret: seal(stored) })
|
|
271
|
+
.onConflictDoUpdate({
|
|
272
|
+
target: [schema.spaceConnectionSignIn.connectionId, schema.spaceConnectionSignIn.userId],
|
|
273
|
+
set: { label, secret: seal(stored), signedInAt: new Date(), updatedAt: new Date() },
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/** Signs in with what the person typed: the connector tries the keys once, then they are kept sealed. */
|
|
277
|
+
export async function signInWithCredentials(id, as, values) {
|
|
278
|
+
const row = await spaceConnectionById(id);
|
|
279
|
+
await allowSignIn(row, as);
|
|
280
|
+
const connector = await connectorOfRow(row);
|
|
281
|
+
if (connector.auth.kind !== "api_key") {
|
|
282
|
+
throw new ServiceError("invalid", note("connectorSignsIn", { name: connector.name }));
|
|
283
|
+
}
|
|
284
|
+
const credentials = {};
|
|
285
|
+
for (const field of connector.auth.apiKey.fields) {
|
|
286
|
+
const value = String(values[field.key] ?? "").trim();
|
|
287
|
+
if (!value && field.required !== false) {
|
|
288
|
+
throw new ServiceError("invalid", note("fieldMissing", { label: field.label }));
|
|
289
|
+
}
|
|
290
|
+
if (value) {
|
|
291
|
+
credentials[field.key] = value.slice(0, 500);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
let account;
|
|
295
|
+
try {
|
|
296
|
+
account = await connector.auth.apiKey.verify(credentials, connectorFetch);
|
|
297
|
+
}
|
|
298
|
+
catch (err) {
|
|
299
|
+
throw new ServiceError("refused", note("connectingNotWork", {
|
|
300
|
+
host: "",
|
|
301
|
+
detail: String(err?.message ?? err).slice(0, 200),
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
await keepSignIn(row, as, account.label, {
|
|
305
|
+
accessToken: JSON.stringify(credentials),
|
|
306
|
+
refreshToken: null,
|
|
307
|
+
expiresAt: null,
|
|
308
|
+
scopes: [],
|
|
309
|
+
});
|
|
310
|
+
return { label: account.label };
|
|
311
|
+
}
|
|
312
|
+
/** The provider rights the connection's actions need, and the connector's base rights. */
|
|
313
|
+
function scopesOf(connector, actions) {
|
|
314
|
+
const scopes = new Set(connector.auth.kind === "oauth2" ? connector.auth.oauth2.baseScopes : []);
|
|
315
|
+
const wanted = actions.length
|
|
316
|
+
? connector.actions.filter((a) => actions.includes(a.id))
|
|
317
|
+
: connector.actions;
|
|
318
|
+
for (const action of wanted) {
|
|
319
|
+
for (const scope of action.providerScopes ?? []) {
|
|
320
|
+
scopes.add(scope);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return [...scopes];
|
|
324
|
+
}
|
|
325
|
+
/** The provider's sign-in page for a connection; the callback (`/api/connect/callback`) finishes it. */
|
|
326
|
+
export async function startSpaceConnectionOAuth(id, as, lang) {
|
|
327
|
+
const row = await spaceConnectionById(id);
|
|
328
|
+
await allowSignIn(row, as);
|
|
329
|
+
const connector = await connectorOfRow(row);
|
|
330
|
+
if (connector.auth.kind !== "oauth2") {
|
|
331
|
+
throw new ServiceError("invalid", note("connectorTakesKeys", { name: connector.name }));
|
|
332
|
+
}
|
|
333
|
+
const scopes = scopesOf(connector, row.actions);
|
|
334
|
+
const base = {
|
|
335
|
+
spaceConnection: row.id,
|
|
336
|
+
tenantId: currentTenant(),
|
|
337
|
+
userId: as.as === "person" ? as.userId : null,
|
|
338
|
+
exp: Date.now() + 15 * 60_000,
|
|
339
|
+
lang,
|
|
340
|
+
};
|
|
341
|
+
if (await viaAccount(connector)) {
|
|
342
|
+
// The Manage-App keeps the PKCE verifier; ours stays empty.
|
|
343
|
+
try {
|
|
344
|
+
return await startThroughAccount(scopes, seal({ ...base, verifier: "" }), redirectUri());
|
|
345
|
+
}
|
|
346
|
+
catch (err) {
|
|
347
|
+
throw err instanceof AccountConnectError ? new ServiceError("refused", err.message) : err;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
await connector.auth.oauth2.registerClient?.();
|
|
351
|
+
const pkce = createOAuth2Pkce();
|
|
352
|
+
return buildAuthorizationUrl({
|
|
353
|
+
connector,
|
|
354
|
+
pkce,
|
|
355
|
+
redirectUri: redirectUri(),
|
|
356
|
+
scopes,
|
|
357
|
+
state: seal({ ...base, verifier: pkce.codeVerifier }),
|
|
358
|
+
resolveEnv,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/** A state the callback can tell is a space connection's: the tenant to finish it in, or null. */
|
|
362
|
+
export function spaceConnectionStateTenant(rawState) {
|
|
363
|
+
const state = unseal(rawState);
|
|
364
|
+
return state && typeof state.spaceConnection === "string" ? state.tenantId : null;
|
|
365
|
+
}
|
|
366
|
+
/** The provider came back for a connection of the space: keeps the sign-in it started. */
|
|
367
|
+
export async function finishSpaceConnectionOAuth(answer, rawState) {
|
|
368
|
+
const state = unseal(rawState);
|
|
369
|
+
if (!state?.spaceConnection || state.exp < Date.now()) {
|
|
370
|
+
throw new Error("The sign-in has expired. Sign in again.");
|
|
371
|
+
}
|
|
372
|
+
const row = await db.query.spaceConnection.findFirst({
|
|
373
|
+
where: eq(schema.spaceConnection.id, state.spaceConnection),
|
|
374
|
+
});
|
|
375
|
+
if (!row) {
|
|
376
|
+
throw new Error("The connection this sign-in belongs to is gone.");
|
|
377
|
+
}
|
|
378
|
+
const connector = await connectorOfRow(row);
|
|
379
|
+
if (connector.auth.kind !== "oauth2") {
|
|
380
|
+
throw new Error("not an oauth connector");
|
|
381
|
+
}
|
|
382
|
+
const tokens = "ticket" in answer
|
|
383
|
+
? await redeemTicket(answer.ticket)
|
|
384
|
+
: await exchangeAuthorizationCode({
|
|
385
|
+
code: answer.code,
|
|
386
|
+
codeVerifier: state.verifier,
|
|
387
|
+
config: connector.auth.oauth2,
|
|
388
|
+
redirectUri: redirectUri(),
|
|
389
|
+
resolveEnv,
|
|
390
|
+
});
|
|
391
|
+
// A provider that will not name the account still connected it.
|
|
392
|
+
const account = (await connector.auth.oauth2
|
|
393
|
+
.resolveAccount?.(tokens.accessToken, fetch)
|
|
394
|
+
.catch(() => null)) ?? { label: connector.name };
|
|
395
|
+
const stored = {
|
|
396
|
+
accessToken: tokens.accessToken,
|
|
397
|
+
refreshToken: tokens.refreshToken,
|
|
398
|
+
expiresAt: tokens.expiresAt?.toISOString() ?? null,
|
|
399
|
+
scopes: tokens.grantedScopes,
|
|
400
|
+
...("ticket" in answer ? { via: "account" } : {}),
|
|
401
|
+
};
|
|
402
|
+
// The person's role was checked when the sign-in started; the state is ours, sealed.
|
|
403
|
+
const as = state.userId === null
|
|
404
|
+
? { as: "space" }
|
|
405
|
+
: { as: "person", userId: state.userId, role: "member" };
|
|
406
|
+
if (as.as === "space" && row.signIn !== "space") {
|
|
407
|
+
throw new Error("The connection no longer signs in as the space.");
|
|
408
|
+
}
|
|
409
|
+
if (as.as === "person" && row.signIn !== "person") {
|
|
410
|
+
throw new Error("The connection no longer signs in per person.");
|
|
411
|
+
}
|
|
412
|
+
await keepSignIn(row, as, account.label, stored);
|
|
413
|
+
return { connectionId: row.id };
|
|
414
|
+
}
|
|
415
|
+
/** Forgets a sign-in: the space's (admins) or the person's own. */
|
|
416
|
+
export async function removeSignIn(id, as) {
|
|
417
|
+
const row = await spaceConnectionById(id);
|
|
418
|
+
if (as.as === "space") {
|
|
419
|
+
await requireAdmin();
|
|
420
|
+
await db
|
|
421
|
+
.update(schema.spaceConnection)
|
|
422
|
+
.set({ secret: null, label: null, updatedAt: new Date() })
|
|
423
|
+
.where(eq(schema.spaceConnection.id, row.id));
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
await db
|
|
427
|
+
.delete(schema.spaceConnectionSignIn)
|
|
428
|
+
.where(and(eq(schema.spaceConnectionSignIn.connectionId, row.id), eq(schema.spaceConnectionSignIn.userId, as.userId)));
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Keeps a sign-in as the space that a plugin or a command already made (`server.connections`
|
|
432
|
+
* after the provider came back): the same account for the same space again replaces the first.
|
|
433
|
+
*/
|
|
434
|
+
export async function keepSpaceSignIn(row, label, stored) {
|
|
435
|
+
await keepSignIn(row, { as: "space" }, label, stored);
|
|
436
|
+
}
|
|
@@ -13,7 +13,7 @@ import { control, controlDb, openTenantDb } from "./client.js";
|
|
|
13
13
|
* and folders stay where they are.
|
|
14
14
|
*/
|
|
15
15
|
const TABLES = [
|
|
16
|
-
"
|
|
16
|
+
"space",
|
|
17
17
|
"wizard",
|
|
18
18
|
"wizard_version",
|
|
19
19
|
"wizard_file",
|
|
@@ -21,7 +21,7 @@ const TABLES = [
|
|
|
21
21
|
"run",
|
|
22
22
|
"run_event",
|
|
23
23
|
"asset",
|
|
24
|
-
"
|
|
24
|
+
"space_connector",
|
|
25
25
|
"store_holder",
|
|
26
26
|
"store_row",
|
|
27
27
|
"store_file",
|
|
@@ -54,7 +54,7 @@ export async function adoptLegacyData() {
|
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
56
|
const legacy = createClient({ url: `file:${legacyFile}` });
|
|
57
|
-
const hasOwner = (await legacy.execute("PRAGMA table_info(
|
|
57
|
+
const hasOwner = (await legacy.execute("PRAGMA table_info(space)")).rows.some((r) => r.name === "owner_id");
|
|
58
58
|
if (!hasOwner) {
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
@@ -116,6 +116,22 @@ async function openTenant(id) {
|
|
|
116
116
|
opening.delete(id);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* 0.13.0 shipped `space_skill` as tenant migration 0027; since 0.14.0 it is 0030, after Work
|
|
121
|
+
* mode's 0027–0029. drizzle runs only migrations newer than the last one it applied, so a
|
|
122
|
+
* database from 0.13.0 would skip 0027–0029 and fail on 0030. Forgetting the old entry lets
|
|
123
|
+
* 0027–0039 run; 0030 leaves the table it finds as it is (the same table, IF NOT EXISTS).
|
|
124
|
+
*/
|
|
125
|
+
const OLD_SPACE_SKILL = 1793266571661;
|
|
126
|
+
async function forgetRenumbered(client) {
|
|
127
|
+
const journal = await client.execute("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = '__drizzle_migrations'");
|
|
128
|
+
if (journal.rows.length) {
|
|
129
|
+
await client.execute({
|
|
130
|
+
sql: "DELETE FROM __drizzle_migrations WHERE created_at = ?",
|
|
131
|
+
args: [OLD_SPACE_SKILL],
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
119
135
|
async function openTenantNow(id) {
|
|
120
136
|
let row = await controlDb.query.tenant.findFirst({ where: eq(control.tenant.id, id) });
|
|
121
137
|
if (!row) {
|
|
@@ -130,6 +146,7 @@ async function openTenantNow(id) {
|
|
|
130
146
|
try {
|
|
131
147
|
await whileBusy(async () => {
|
|
132
148
|
await prepare(client, url);
|
|
149
|
+
await forgetRenumbered(client);
|
|
133
150
|
await migrate(db, { migrationsFolder: migrationsFolder("tenant") });
|
|
134
151
|
});
|
|
135
152
|
}
|
|
@@ -167,6 +184,15 @@ export function tenantDb(id) {
|
|
|
167
184
|
return db;
|
|
168
185
|
}
|
|
169
186
|
const ready = new Map();
|
|
187
|
+
/** The open client of a tenant's database, for a store of its own beside ours (Mastra's). */
|
|
188
|
+
export async function tenantClient(id) {
|
|
189
|
+
await tenantDb(id);
|
|
190
|
+
const client = clients.get(id);
|
|
191
|
+
if (!client) {
|
|
192
|
+
throw new Error(`The database of tenant ${id} is not open.`);
|
|
193
|
+
}
|
|
194
|
+
return client;
|
|
195
|
+
}
|
|
170
196
|
/** Closes and forgets an open tenant database (the tenant was deleted or moved). */
|
|
171
197
|
export function closeTenantDb(id) {
|
|
172
198
|
opened.delete(id);
|