@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
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { formatTableCell } from "@engenty/wizards-shared/engenty/data-tables";
|
|
2
2
|
import { embedMany } from "ai";
|
|
3
3
|
import { and, eq, inArray, isNotNull, isNull, sql } from "drizzle-orm";
|
|
4
|
+
import { brokerOn, forbidden, pageResources, readable, readsEverything, tableResources, } from "../broker/rwx.js";
|
|
4
5
|
import { db, schema, withTenant } from "../db/client.js";
|
|
5
6
|
import { createSearchChunks } from "../engenty/search-index/chunks.js";
|
|
6
7
|
import { getBlob, putBlob } from "../files/blobs.js";
|
|
@@ -29,7 +30,7 @@ async function embed(texts) {
|
|
|
29
30
|
return { vectors: result.embeddings.map((e) => Array.from(e)), model: resolved.ref };
|
|
30
31
|
}
|
|
31
32
|
export async function dropIndex(fileId) {
|
|
32
|
-
await db.delete(schema.
|
|
33
|
+
await db.delete(schema.spaceChunk).where(eq(schema.spaceChunk.fileId, fileId));
|
|
33
34
|
}
|
|
34
35
|
export async function documentText(file) {
|
|
35
36
|
return file.textHash ? (await getBlob(file.textHash)).toString("utf8") : "";
|
|
@@ -45,11 +46,11 @@ export async function keepText(text) {
|
|
|
45
46
|
async function writeUnits(scope, units) {
|
|
46
47
|
const before = await db
|
|
47
48
|
.select({
|
|
48
|
-
text: schema.
|
|
49
|
-
embedding: schema.
|
|
50
|
-
model: schema.
|
|
49
|
+
text: schema.spaceChunk.text,
|
|
50
|
+
embedding: schema.spaceChunk.embedding,
|
|
51
|
+
model: schema.spaceChunk.model,
|
|
51
52
|
})
|
|
52
|
-
.from(schema.
|
|
53
|
+
.from(schema.spaceChunk)
|
|
53
54
|
.where(scope);
|
|
54
55
|
const resolved = await embeddingModel().catch(() => null);
|
|
55
56
|
const kept = new Map(before
|
|
@@ -58,7 +59,7 @@ async function writeUnits(scope, units) {
|
|
|
58
59
|
const missing = [...new Set(units.map((u) => u.text).filter((t) => !kept.has(t)))];
|
|
59
60
|
// Without an embedding model, or when it fails, the keywords still find the unit.
|
|
60
61
|
const embedded = await embed(missing).catch((err) => {
|
|
61
|
-
console.error("[
|
|
62
|
+
console.error("[space-index] embedding failed", err);
|
|
62
63
|
return null;
|
|
63
64
|
});
|
|
64
65
|
if (embedded) {
|
|
@@ -67,9 +68,9 @@ async function writeUnits(scope, units) {
|
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
const model = embedded?.model ?? (kept.size ? (resolved?.ref ?? null) : null);
|
|
70
|
-
await db.delete(schema.
|
|
71
|
+
await db.delete(schema.spaceChunk).where(scope);
|
|
71
72
|
for (let i = 0; i < units.length; i += 100) {
|
|
72
|
-
await db.insert(schema.
|
|
73
|
+
await db.insert(schema.spaceChunk).values(units.slice(i, i + 100).map((u) => {
|
|
73
74
|
const vector = kept.get(u.text) ?? null;
|
|
74
75
|
return { ...u, embedding: vector, model: vector ? model : null };
|
|
75
76
|
}));
|
|
@@ -144,15 +145,15 @@ function partsOf(body) {
|
|
|
144
145
|
}
|
|
145
146
|
const head = (titles, lines) => [titles.join(" › "), lines.length ? lines.join(" · ") : ""].filter(Boolean).join("\n");
|
|
146
147
|
async function indexPage(pageId) {
|
|
147
|
-
const scope = eq(schema.
|
|
148
|
+
const scope = eq(schema.spaceChunk.pageId, pageId);
|
|
148
149
|
const page = await db.query.spacePage.findFirst({ where: eq(schema.spacePage.id, pageId) });
|
|
149
150
|
if (!page || page.wizardId) {
|
|
150
|
-
await db.delete(schema.
|
|
151
|
+
await db.delete(schema.spaceChunk).where(scope);
|
|
151
152
|
return;
|
|
152
153
|
}
|
|
153
154
|
const [tree, categories] = await Promise.all([
|
|
154
|
-
pageTree(page.
|
|
155
|
-
categoryRows(page.
|
|
155
|
+
pageTree(page.spaceId),
|
|
156
|
+
categoryRows(page.spaceId),
|
|
156
157
|
]);
|
|
157
158
|
const node = tree.get(page.id);
|
|
158
159
|
const lines = categoryLines(categories, (await itemCategoriesOf([{ pageId: page.id }])).get(`p:${page.id}`));
|
|
@@ -168,7 +169,7 @@ async function indexPage(pageId) {
|
|
|
168
169
|
const heading = path.join(" › ") || null;
|
|
169
170
|
for (const part of partsOf(section.body)) {
|
|
170
171
|
units.push({
|
|
171
|
-
|
|
172
|
+
spaceId: page.spaceId,
|
|
172
173
|
pageId: page.id,
|
|
173
174
|
heading,
|
|
174
175
|
sheet: section.sheet,
|
|
@@ -182,10 +183,10 @@ async function indexPage(pageId) {
|
|
|
182
183
|
async function indexRows(tableId, rowIds) {
|
|
183
184
|
const table = await db.query.spaceTable.findFirst({ where: eq(schema.spaceTable.id, tableId) });
|
|
184
185
|
const scope = rowIds
|
|
185
|
-
? inArray(schema.
|
|
186
|
-
: eq(schema.
|
|
186
|
+
? inArray(schema.spaceChunk.rowId, rowIds)
|
|
187
|
+
: eq(schema.spaceChunk.tableId, tableId);
|
|
187
188
|
if (!table || table.wizardId) {
|
|
188
|
-
await db.delete(schema.
|
|
189
|
+
await db.delete(schema.spaceChunk).where(scope);
|
|
189
190
|
return;
|
|
190
191
|
}
|
|
191
192
|
const rows = await db.query.spaceTableRow.findMany({
|
|
@@ -193,7 +194,7 @@ async function indexRows(tableId, rowIds) {
|
|
|
193
194
|
? and(eq(schema.spaceTableRow.tableId, table.id), inArray(schema.spaceTableRow.id, rowIds))
|
|
194
195
|
: eq(schema.spaceTableRow.tableId, table.id),
|
|
195
196
|
});
|
|
196
|
-
const categories = await categoryRows(table.
|
|
197
|
+
const categories = await categoryRows(table.spaceId);
|
|
197
198
|
const cats = await itemCategoriesOf([
|
|
198
199
|
{ tableId: table.id },
|
|
199
200
|
...rows.map((r) => ({ rowId: r.id })),
|
|
@@ -211,7 +212,7 @@ async function indexRows(tableId, rowIds) {
|
|
|
211
212
|
.join("\n\n")}`
|
|
212
213
|
: cells.map(([name, v]) => `${name}: ${v}`).join("\n");
|
|
213
214
|
return {
|
|
214
|
-
|
|
215
|
+
spaceId: table.spaceId,
|
|
215
216
|
tableId: table.id,
|
|
216
217
|
rowId: row.id,
|
|
217
218
|
heading: table.format === "faq" ? (cells[0]?.[1] ?? null) : null,
|
|
@@ -229,12 +230,12 @@ export function indexedFile(file) {
|
|
|
229
230
|
}
|
|
230
231
|
/** An asset stands in the index as one unit: its name, its kind, its categories, its description. */
|
|
231
232
|
async function indexAsset(file) {
|
|
232
|
-
const categories = await categoryRows(file.
|
|
233
|
+
const categories = await categoryRows(file.spaceId);
|
|
233
234
|
const lines = categoryLines(categories, (await itemCategoriesOf([{ fileId: file.id }])).get(`f:${file.id}`));
|
|
234
235
|
const kind = file.mime.split("/")[0];
|
|
235
|
-
await writeUnits(eq(schema.
|
|
236
|
+
await writeUnits(eq(schema.spaceChunk.fileId, file.id), [
|
|
236
237
|
{
|
|
237
|
-
|
|
238
|
+
spaceId: file.spaceId,
|
|
238
239
|
fileId: file.id,
|
|
239
240
|
heading: file.name,
|
|
240
241
|
idx: 0,
|
|
@@ -245,7 +246,7 @@ async function indexAsset(file) {
|
|
|
245
246
|
]);
|
|
246
247
|
}
|
|
247
248
|
async function indexFile(fileId) {
|
|
248
|
-
const file = await db.query.
|
|
249
|
+
const file = await db.query.spaceFile.findFirst({ where: eq(schema.spaceFile.id, fileId) });
|
|
249
250
|
if (!(file && indexedFile(file) && file.status === "ready")) {
|
|
250
251
|
return;
|
|
251
252
|
}
|
|
@@ -260,25 +261,25 @@ async function indexFile(fileId) {
|
|
|
260
261
|
where: eq(schema.spaceTable.fileId, file.id),
|
|
261
262
|
columns: { id: true },
|
|
262
263
|
}));
|
|
263
|
-
const scope = eq(schema.
|
|
264
|
+
const scope = eq(schema.spaceChunk.fileId, file.id);
|
|
264
265
|
if (converted) {
|
|
265
266
|
// Its text is the page's now; the file stands behind it as the original.
|
|
266
|
-
await db.delete(schema.
|
|
267
|
+
await db.delete(schema.spaceChunk).where(scope);
|
|
267
268
|
return;
|
|
268
269
|
}
|
|
269
270
|
const passages = await db
|
|
270
|
-
.select({ heading: schema.
|
|
271
|
-
.from(schema.
|
|
271
|
+
.select({ heading: schema.spaceChunk.heading, idx: schema.spaceChunk.idx })
|
|
272
|
+
.from(schema.spaceChunk)
|
|
272
273
|
.where(scope);
|
|
273
274
|
// Passages of a document read before Knowledge had pages stay until it is read again.
|
|
274
275
|
if (passages.some((p) => p.heading === null)) {
|
|
275
276
|
return;
|
|
276
277
|
}
|
|
277
|
-
const categories = await categoryRows(file.
|
|
278
|
+
const categories = await categoryRows(file.spaceId);
|
|
278
279
|
const lines = categoryLines(categories, (await itemCategoriesOf([{ fileId: file.id }])).get(`f:${file.id}`));
|
|
279
280
|
await writeUnits(scope, [
|
|
280
281
|
{
|
|
281
|
-
|
|
282
|
+
spaceId: file.spaceId,
|
|
282
283
|
fileId: file.id,
|
|
283
284
|
heading: file.name,
|
|
284
285
|
idx: 0,
|
|
@@ -287,7 +288,7 @@ async function indexFile(fileId) {
|
|
|
287
288
|
]);
|
|
288
289
|
}
|
|
289
290
|
async function indexValue(valueId) {
|
|
290
|
-
const scope = eq(schema.
|
|
291
|
+
const scope = eq(schema.spaceChunk.valueId, valueId);
|
|
291
292
|
const value = await db.query.spaceCategoryValue.findFirst({
|
|
292
293
|
where: eq(schema.spaceCategoryValue.id, valueId),
|
|
293
294
|
});
|
|
@@ -297,11 +298,11 @@ async function indexValue(valueId) {
|
|
|
297
298
|
})
|
|
298
299
|
: undefined;
|
|
299
300
|
if (!(value?.summary && category)) {
|
|
300
|
-
await db.delete(schema.
|
|
301
|
+
await db.delete(schema.spaceChunk).where(scope);
|
|
301
302
|
return;
|
|
302
303
|
}
|
|
303
304
|
const units = partsOf(value.summary).map((part, i) => ({
|
|
304
|
-
|
|
305
|
+
spaceId: category.spaceId,
|
|
305
306
|
valueId: value.id,
|
|
306
307
|
heading: `${category.name}: ${value.value}`,
|
|
307
308
|
idx: i,
|
|
@@ -360,7 +361,7 @@ let working = Promise.resolve();
|
|
|
360
361
|
function run(tenant, key) {
|
|
361
362
|
working = working
|
|
362
363
|
.then(() => withTenant(tenant, () => indexItem(key)))
|
|
363
|
-
.catch((err) => console.error(`[
|
|
364
|
+
.catch((err) => console.error(`[space-index] ${key}`, err));
|
|
364
365
|
}
|
|
365
366
|
/** Indexes an item a moment from now; a change before then waits again. */
|
|
366
367
|
export function queueIndex(key) {
|
|
@@ -392,14 +393,14 @@ export async function flushIndex() {
|
|
|
392
393
|
// --- plugins' texts ---------------------------------------------------------------------
|
|
393
394
|
/** Puts a plugin's text into the space's index, in the place of what its key held. */
|
|
394
395
|
export async function putIndexEntry(plugin, entry) {
|
|
395
|
-
const space = await db.query.
|
|
396
|
+
const space = await db.query.space.findFirst({ where: eq(schema.space.id, entry.space) });
|
|
396
397
|
if (!space) {
|
|
397
398
|
throw new ServiceError("not_found", `There is no space "${entry.space}".`);
|
|
398
399
|
}
|
|
399
400
|
const text = `${entry.title.trim()}\n\n${entry.text.trim()}`;
|
|
400
401
|
const chunks = createSearchChunks({ doc_id: `${plugin}:${entry.key}`, text, ...CHUNK }).slice(0, MAX_UNITS);
|
|
401
|
-
await writeUnits(and(eq(schema.
|
|
402
|
-
|
|
402
|
+
await writeUnits(and(eq(schema.spaceChunk.spaceId, entry.space), eq(schema.spaceChunk.plugin, plugin), eq(schema.spaceChunk.ref, entry.key)), chunks.map((c) => ({
|
|
403
|
+
spaceId: entry.space,
|
|
403
404
|
plugin,
|
|
404
405
|
ref: entry.key,
|
|
405
406
|
title: entry.title.trim().slice(0, 300),
|
|
@@ -411,18 +412,18 @@ export async function putIndexEntry(plugin, entry) {
|
|
|
411
412
|
/** Takes a plugin's text out of the space's index; without a key, all of the plugin's texts. */
|
|
412
413
|
export async function removeIndexEntries(plugin, space, key) {
|
|
413
414
|
await db
|
|
414
|
-
.delete(schema.
|
|
415
|
-
.where(and(eq(schema.
|
|
415
|
+
.delete(schema.spaceChunk)
|
|
416
|
+
.where(and(eq(schema.spaceChunk.spaceId, space), eq(schema.spaceChunk.plugin, plugin), key === undefined ? undefined : eq(schema.spaceChunk.ref, key)));
|
|
416
417
|
}
|
|
417
418
|
/** Whether a plugin the tenant has put a text into the space's index. */
|
|
418
|
-
export async function hasIndexEntries(
|
|
419
|
+
export async function hasIndexEntries(spaceId) {
|
|
419
420
|
const plugins = [...(await pluginIdsOf(currentTenant()))];
|
|
420
421
|
if (!plugins.length) {
|
|
421
422
|
return false;
|
|
422
423
|
}
|
|
423
|
-
const found = await db.query.
|
|
424
|
+
const found = await db.query.spaceChunk.findFirst({
|
|
424
425
|
columns: { id: true },
|
|
425
|
-
where: and(eq(schema.
|
|
426
|
+
where: and(eq(schema.spaceChunk.spaceId, spaceId), inArray(schema.spaceChunk.plugin, plugins)),
|
|
426
427
|
});
|
|
427
428
|
return Boolean(found);
|
|
428
429
|
}
|
|
@@ -491,53 +492,95 @@ function itemOf(u) {
|
|
|
491
492
|
return `x:${u.plugin}:${u.ref}`;
|
|
492
493
|
}
|
|
493
494
|
/**
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* leaves the classifier out.
|
|
495
|
+
* What `who` may read among the units found: a page or file by its own rule, a row by its
|
|
496
|
+
* table's, a value's summary (made of many items) only where they may read everything; a
|
|
497
|
+
* plugin's text is not the broker's yet. Null while the switch is off: everything.
|
|
498
498
|
*/
|
|
499
|
-
|
|
499
|
+
async function readableUnits(spaceId, who, units) {
|
|
500
|
+
if (!(await brokerOn())) {
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
const ids = (pick) => [
|
|
504
|
+
...new Set(units.flatMap((u) => (pick(u) ? [pick(u)] : []))),
|
|
505
|
+
];
|
|
506
|
+
const [pages, tables, files, all] = await Promise.all([
|
|
507
|
+
pageResources(ids((u) => u.page_id)).then((r) => readable(spaceId, who, r)),
|
|
508
|
+
tableResources(ids((u) => u.table_id)).then((r) => readable(spaceId, who, r)),
|
|
509
|
+
readable(spaceId, who, ids((u) => u.file_id).map((id) => ({ kind: "document", id }))),
|
|
510
|
+
readsEverything(spaceId, who),
|
|
511
|
+
]);
|
|
512
|
+
return (u) => {
|
|
513
|
+
if (u.page_id) {
|
|
514
|
+
return pages.has(u.page_id);
|
|
515
|
+
}
|
|
516
|
+
if (u.row_id) {
|
|
517
|
+
return u.table_id !== null && tables.has(u.table_id);
|
|
518
|
+
}
|
|
519
|
+
if (u.value_id) {
|
|
520
|
+
return all;
|
|
521
|
+
}
|
|
522
|
+
if (u.file_id) {
|
|
523
|
+
return files.has(u.file_id);
|
|
524
|
+
}
|
|
525
|
+
return Boolean(u.plugin);
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Searches Knowledge for `who`: items whose categories pass `where` and that they may read, then
|
|
530
|
+
* the units the keywords, the trigrams and the vectors rank best, merged by rank (RRF), then the
|
|
531
|
+
* classifier's judgement of each of the CANDIDATES best. Gives up to `limit` items with their
|
|
532
|
+
* best section. `check: false` leaves the classifier out. While the `broker` switch is on, a call
|
|
533
|
+
* without `who` is refused, and an item they may not read is in no hit, candidate or count.
|
|
534
|
+
*/
|
|
535
|
+
export async function searchKnowledge(spaceId, query, opts = {}) {
|
|
500
536
|
const limit = Math.min(Math.max(1, opts.limit ?? 6), 12);
|
|
501
|
-
const
|
|
537
|
+
const on = await brokerOn();
|
|
538
|
+
if (on && !opts.who?.length) {
|
|
539
|
+
throw forbidden({ kind: "document", id: "*" }, "r");
|
|
540
|
+
}
|
|
541
|
+
const filter = await filterItems(spaceId, opts.where);
|
|
502
542
|
if (filter && !filter.size) {
|
|
503
543
|
return { hits: [], candidates: 0, checked: false, filtered: 0 };
|
|
504
544
|
}
|
|
505
545
|
const plugins = [...(await pluginIdsOf(currentTenant()))];
|
|
506
546
|
const inFilter = filter ? filtered(filter) : null;
|
|
507
547
|
// A filter keeps plugins' texts and summaries out: they have no categories.
|
|
508
|
-
const where = and(sql `c.
|
|
548
|
+
const where = and(sql `c.space_id = ${spaceId}`, searchable(plugins), filter
|
|
509
549
|
? (inFilter ?? sql `(c.page_id IS NOT NULL OR c.row_id IS NOT NULL OR c.file_id IS NOT NULL)`)
|
|
510
550
|
: undefined);
|
|
511
|
-
|
|
551
|
+
// What the rules or a long filter take out afterwards must not starve the candidates.
|
|
552
|
+
const take = on || (filter && !inFilter) ? 300 : CANDIDATES;
|
|
512
553
|
const lists = [];
|
|
513
554
|
const words = keywordQuery(query);
|
|
514
555
|
if (words) {
|
|
515
556
|
lists.push(await db.all(sql `
|
|
516
|
-
SELECT ${COLUMNS} FROM
|
|
517
|
-
WHERE
|
|
518
|
-
ORDER BY bm25(
|
|
557
|
+
SELECT ${COLUMNS} FROM space_chunk_fts f JOIN space_chunk c ON c.id = f.rowid
|
|
558
|
+
WHERE space_chunk_fts MATCH ${words} AND ${where}
|
|
559
|
+
ORDER BY bm25(space_chunk_fts) LIMIT ${take}`));
|
|
519
560
|
}
|
|
520
561
|
const grams = trigramQuery(query);
|
|
521
562
|
if (grams) {
|
|
522
563
|
lists.push(await db
|
|
523
564
|
.all(sql `
|
|
524
|
-
SELECT ${COLUMNS} FROM
|
|
525
|
-
WHERE
|
|
526
|
-
ORDER BY bm25(
|
|
565
|
+
SELECT ${COLUMNS} FROM space_chunk_tri f JOIN space_chunk c ON c.id = f.rowid
|
|
566
|
+
WHERE space_chunk_tri MATCH ${grams} AND ${where}
|
|
567
|
+
ORDER BY bm25(space_chunk_tri) LIMIT ${take}`)
|
|
527
568
|
.catch(() => []));
|
|
528
569
|
}
|
|
529
570
|
const embedded = await embed([query]).catch(() => null);
|
|
530
571
|
if (embedded) {
|
|
531
572
|
lists.push(await db.all(sql `
|
|
532
|
-
SELECT ${COLUMNS} FROM
|
|
573
|
+
SELECT ${COLUMNS} FROM space_chunk c
|
|
533
574
|
WHERE ${where} AND c.model = ${embedded.model}
|
|
534
575
|
ORDER BY vector_distance_cos(c.embedding, ${vectorBytes(embedded.vectors[0])}) LIMIT ${take}`));
|
|
535
576
|
}
|
|
577
|
+
const allowed = await readableUnits(spaceId, opts.who ?? [], lists.flat());
|
|
536
578
|
// Reciprocal rank fusion: a unit the lists rank high comes first.
|
|
537
579
|
const scored = new Map();
|
|
538
580
|
for (const list of lists) {
|
|
539
581
|
list
|
|
540
582
|
.filter((u) => !filter || inFilter || leaves(filter, u))
|
|
583
|
+
.filter((u) => !allowed || allowed(u))
|
|
541
584
|
.slice(0, CANDIDATES)
|
|
542
585
|
.forEach((unit, rank) => {
|
|
543
586
|
const entry = scored.get(unit.id) ?? { unit, score: 0 };
|
|
@@ -549,7 +592,7 @@ export async function searchKnowledge(projectId, query, opts = {}) {
|
|
|
549
592
|
if (!candidates.length) {
|
|
550
593
|
return { hits: [], candidates: 0, checked: false, filtered: filter?.size ?? null };
|
|
551
594
|
}
|
|
552
|
-
const described = await describe(
|
|
595
|
+
const described = await describe(spaceId, candidates.map((c) => c.unit));
|
|
553
596
|
const judged = opts.check === false
|
|
554
597
|
? null
|
|
555
598
|
: await judgeRelevance(query, described.map((d) => ({
|
|
@@ -586,16 +629,16 @@ export async function searchKnowledge(projectId, query, opts = {}) {
|
|
|
586
629
|
};
|
|
587
630
|
}
|
|
588
631
|
/** What a step and the studio see of each unit: the item's path, title and categories. */
|
|
589
|
-
async function describe(
|
|
632
|
+
async function describe(spaceId, units) {
|
|
590
633
|
const pageIds = [...new Set(units.flatMap((u) => (u.page_id ? [u.page_id] : [])))];
|
|
591
634
|
const rowIds = [...new Set(units.flatMap((u) => (u.row_id ? [u.row_id] : [])))];
|
|
592
635
|
const fileIds = [...new Set(units.flatMap((u) => (u.file_id ? [u.file_id] : [])))];
|
|
593
636
|
const valueIds = [...new Set(units.flatMap((u) => (u.value_id ? [u.value_id] : [])))];
|
|
594
637
|
const [tree, tPaths, fPaths, categories, rows, files, values] = await Promise.all([
|
|
595
|
-
pageIds.length ? pageTree(
|
|
596
|
-
rowIds.length ? tablePaths(
|
|
597
|
-
fileIds.length ? filePaths(
|
|
598
|
-
categoryRows(
|
|
638
|
+
pageIds.length ? pageTree(spaceId) : new Map(),
|
|
639
|
+
rowIds.length ? tablePaths(spaceId) : new Map(),
|
|
640
|
+
fileIds.length ? filePaths(spaceId) : new Map(),
|
|
641
|
+
categoryRows(spaceId),
|
|
599
642
|
rowIds.length
|
|
600
643
|
? db.query.spaceTableRow.findMany({
|
|
601
644
|
where: inArray(schema.spaceTableRow.id, rowIds),
|
|
@@ -603,8 +646,8 @@ async function describe(projectId, units) {
|
|
|
603
646
|
})
|
|
604
647
|
: [],
|
|
605
648
|
fileIds.length
|
|
606
|
-
? db.query.
|
|
607
|
-
where: inArray(schema.
|
|
649
|
+
? db.query.spaceFile.findMany({
|
|
650
|
+
where: inArray(schema.spaceFile.id, fileIds),
|
|
608
651
|
columns: { id: true, name: true },
|
|
609
652
|
})
|
|
610
653
|
: [],
|
|
@@ -698,25 +741,25 @@ async function describe(projectId, units) {
|
|
|
698
741
|
});
|
|
699
742
|
}
|
|
700
743
|
/** Puts every item of a space into the index again: after a sync, or a change of embedding model. */
|
|
701
|
-
export async function reindexSpace(
|
|
744
|
+
export async function reindexSpace(spaceId) {
|
|
702
745
|
const [pages, tables, files, values] = await Promise.all([
|
|
703
746
|
db.query.spacePage.findMany({
|
|
704
|
-
where: and(eq(schema.spacePage.
|
|
747
|
+
where: and(eq(schema.spacePage.spaceId, spaceId), isNull(schema.spacePage.wizardId)),
|
|
705
748
|
columns: { id: true },
|
|
706
749
|
}),
|
|
707
750
|
db.query.spaceTable.findMany({
|
|
708
|
-
where: and(eq(schema.spaceTable.
|
|
751
|
+
where: and(eq(schema.spaceTable.spaceId, spaceId), isNull(schema.spaceTable.wizardId)),
|
|
709
752
|
columns: { id: true },
|
|
710
753
|
}),
|
|
711
|
-
db.query.
|
|
712
|
-
where: and(eq(schema.
|
|
754
|
+
db.query.spaceFile.findMany({
|
|
755
|
+
where: and(eq(schema.spaceFile.spaceId, spaceId), inArray(schema.spaceFile.kind, ["document", "asset"])),
|
|
713
756
|
columns: { id: true, kind: true, mime: true },
|
|
714
757
|
}),
|
|
715
758
|
db
|
|
716
759
|
.select({ id: schema.spaceCategoryValue.id })
|
|
717
760
|
.from(schema.spaceCategoryValue)
|
|
718
761
|
.innerJoin(schema.spaceCategory, eq(schema.spaceCategory.id, schema.spaceCategoryValue.categoryId))
|
|
719
|
-
.where(and(eq(schema.spaceCategory.
|
|
762
|
+
.where(and(eq(schema.spaceCategory.spaceId, spaceId), isNotNull(schema.spaceCategoryValue.summary))),
|
|
720
763
|
]);
|
|
721
764
|
for (const p of pages) {
|
|
722
765
|
queueIndex(`p:${p.id}`);
|
|
@@ -733,17 +776,17 @@ export async function reindexSpace(projectId) {
|
|
|
733
776
|
}
|
|
734
777
|
/** Every space into the index again, with the embedding model that is set up now. */
|
|
735
778
|
export async function reindexAll() {
|
|
736
|
-
const
|
|
737
|
-
for (const p of
|
|
779
|
+
const spaces = await db.query.space.findMany({ columns: { id: true } });
|
|
780
|
+
for (const p of spaces) {
|
|
738
781
|
await reindexSpace(p.id);
|
|
739
782
|
}
|
|
740
783
|
}
|
|
741
784
|
/** The image, video and sound assets the index does not hold yet: added at start, once each. */
|
|
742
785
|
export async function indexMissingAssets() {
|
|
743
786
|
const rows = await db.all(sql `
|
|
744
|
-
SELECT f.id, f.kind, f.mime FROM
|
|
787
|
+
SELECT f.id, f.kind, f.mime FROM space_file f
|
|
745
788
|
WHERE f.kind = 'asset' AND f.status = 'ready'
|
|
746
|
-
AND NOT EXISTS (SELECT 1 FROM
|
|
789
|
+
AND NOT EXISTS (SELECT 1 FROM space_chunk c WHERE c.file_id = f.id)`);
|
|
747
790
|
for (const row of rows.filter(indexedFile)) {
|
|
748
791
|
queueIndex(`f:${row.id}`);
|
|
749
792
|
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { and, desc, eq, isNull } from "drizzle-orm";
|
|
2
|
+
import { currentCaller } from "../broker/caller.js";
|
|
3
|
+
import { currentReader, subjectsOfCaller } from "../broker/rwx.js";
|
|
2
4
|
import { db, schema } from "../db/client.js";
|
|
3
5
|
import { UnreadableDocument } from "../documents/parse.js";
|
|
4
6
|
import { ServiceError } from "./errors.js";
|
|
5
7
|
import { filePaths, pageTree, tablePaths } from "./knowledge.js";
|
|
6
8
|
import { convertDocument, fillCategories, summarizeValues } from "./knowledge-model.js";
|
|
7
|
-
import { projectFileContent, putOriginFile } from "./project-files.js";
|
|
8
|
-
import { queueIndex } from "./project-index.js";
|
|
9
9
|
import { putCategory, setItemCategories } from "./space-categories.js";
|
|
10
10
|
import { listOrigin, putOriginPage, putOriginTable, removeOrigin } from "./space-data.js";
|
|
11
|
+
import { putOriginFile, spaceFileContent } from "./space-files.js";
|
|
12
|
+
import { queueIndex } from "./space-index.js";
|
|
11
13
|
/**
|
|
12
14
|
* `server.spaceData` of a plugin: Knowledge it writes under keys of its own, with categories. The
|
|
13
15
|
* plugin's id is the origin of everything it writes; a person who changes an item keeps it.
|
|
@@ -19,7 +21,7 @@ export function originData(origin) {
|
|
|
19
21
|
let tableId = null;
|
|
20
22
|
if (c.column) {
|
|
21
23
|
const table = await db.query.spaceTable.findFirst({
|
|
22
|
-
where: and(eq(schema.spaceTable.
|
|
24
|
+
where: and(eq(schema.spaceTable.spaceId, space), eq(schema.spaceTable.origin, origin), eq(schema.spaceTable.originKey, c.column.table)),
|
|
23
25
|
columns: { id: true },
|
|
24
26
|
});
|
|
25
27
|
if (!table) {
|
|
@@ -72,7 +74,7 @@ export function originData(origin) {
|
|
|
72
74
|
},
|
|
73
75
|
async putDocument(input) {
|
|
74
76
|
const id = await putOriginFile(origin, { ...input, categories: undefined });
|
|
75
|
-
const file = await db.query.
|
|
77
|
+
const file = await db.query.spaceFile.findFirst({ where: eq(schema.spaceFile.id, id) });
|
|
76
78
|
if (!file) {
|
|
77
79
|
throw new ServiceError("not_found", "The document was removed while it was being read.");
|
|
78
80
|
}
|
|
@@ -100,14 +102,14 @@ export function originData(origin) {
|
|
|
100
102
|
throw err;
|
|
101
103
|
}
|
|
102
104
|
await db
|
|
103
|
-
.update(schema.
|
|
105
|
+
.update(schema.spaceFile)
|
|
104
106
|
.set({
|
|
105
107
|
textHash: converted.textHash,
|
|
106
108
|
pages: converted.pages,
|
|
107
109
|
chars: converted.chars,
|
|
108
110
|
updatedAt: new Date(),
|
|
109
111
|
})
|
|
110
|
-
.where(eq(schema.
|
|
112
|
+
.where(eq(schema.spaceFile.id, id));
|
|
111
113
|
const [tree, tables] = await Promise.all([pageTree(input.space), tablePaths(input.space)]);
|
|
112
114
|
const items = await Promise.all(converted.items.map(async (key) => {
|
|
113
115
|
const itemId = key.slice(2);
|
|
@@ -160,7 +162,7 @@ export function originData(origin) {
|
|
|
160
162
|
async readPages(space, opts) {
|
|
161
163
|
const tree = await pageTree(space);
|
|
162
164
|
const rows = await db.query.spacePage.findMany({
|
|
163
|
-
where: and(eq(schema.spacePage.
|
|
165
|
+
where: and(eq(schema.spacePage.spaceId, space), isNull(schema.spacePage.wizardId), opts?.parent ? eq(schema.spacePage.parentId, opts.parent) : undefined),
|
|
164
166
|
orderBy: [schema.spacePage.position, schema.spacePage.title],
|
|
165
167
|
});
|
|
166
168
|
return rows.map((r) => ({
|
|
@@ -175,9 +177,9 @@ export function originData(origin) {
|
|
|
175
177
|
}));
|
|
176
178
|
},
|
|
177
179
|
async files(space, kind = "document") {
|
|
178
|
-
const rows = await db.query.
|
|
179
|
-
where: and(eq(schema.
|
|
180
|
-
orderBy: [desc(schema.
|
|
180
|
+
const rows = await db.query.spaceFile.findMany({
|
|
181
|
+
where: and(eq(schema.spaceFile.spaceId, space), eq(schema.spaceFile.kind, kind), eq(schema.spaceFile.status, "ready")),
|
|
182
|
+
orderBy: [desc(schema.spaceFile.updatedAt)],
|
|
181
183
|
});
|
|
182
184
|
return rows.map((r) => ({
|
|
183
185
|
id: r.id,
|
|
@@ -189,7 +191,13 @@ export function originData(origin) {
|
|
|
189
191
|
}));
|
|
190
192
|
},
|
|
191
193
|
async readFile(space, fileId) {
|
|
192
|
-
|
|
194
|
+
// As whom the plugin was called for, and as itself: a tool in a step or a Work chat as its
|
|
195
|
+
// caller, an HTTP route as the person it answers, a job as the runtime itself (readingAs).
|
|
196
|
+
// Anything else reads as the plugin alone, which no seed allows. A deny for it always wins.
|
|
197
|
+
const caller = currentCaller();
|
|
198
|
+
const plugin = { kind: "plugin", id: origin };
|
|
199
|
+
const who = [...(caller ? await subjectsOfCaller(caller) : currentReader()), plugin];
|
|
200
|
+
const { row, data } = await spaceFileContent(space, fileId, who);
|
|
193
201
|
return {
|
|
194
202
|
id: row.id,
|
|
195
203
|
name: row.name,
|