@engenty/wizards-runtime 0.15.0 → 0.16.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/studio-assistant.js +138 -9
- package/apps/runtime/dist/agents/website.js +10 -5
- package/apps/runtime/dist/app.js +18 -0
- package/apps/runtime/dist/auth/index.js +5 -0
- package/apps/runtime/dist/broker/account.js +5 -0
- package/apps/runtime/dist/broker/index.js +5 -0
- package/apps/runtime/dist/broker/rwx.js +255 -7
- package/apps/runtime/dist/cli/main.js +7 -0
- package/apps/runtime/dist/cli/move-files.js +84 -0
- package/apps/runtime/dist/cli/move-folder-sources.js +120 -0
- package/apps/runtime/dist/connectors/plugin-connections.js +74 -4
- package/apps/runtime/dist/db/migrations/tenant/0042_client_spaces.sql +16 -0
- package/apps/runtime/dist/db/migrations/tenant/0043_space_member_invite.sql +19 -0
- package/apps/runtime/dist/db/migrations/tenant/0044_client_portal.sql +8 -0
- package/apps/runtime/dist/db/migrations/tenant/0045_client_portal_no_slug.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0046_space_file_hash.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0047_knowledge_folders.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/0048_space_chunk_kind.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/0049_knowledge_sources.sql +57 -0
- package/apps/runtime/dist/db/migrations/tenant/0050_dateien.sql +71 -0
- package/apps/runtime/dist/db/migrations/tenant/0051_dateien_drafts.sql +23 -0
- package/apps/runtime/dist/db/migrations/tenant/0052_dateien_plugin_kinds.sql +5 -0
- package/apps/runtime/dist/db/migrations/tenant/0053_dateien_file_title.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/0054_dateien_ocr.sql +14 -0
- package/apps/runtime/dist/db/migrations/tenant/0055_dateien_doc_reread.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0042_snapshot.json +5286 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0043_snapshot.json +5293 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0044_snapshot.json +5344 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0045_snapshot.json +5329 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0046_snapshot.json +5343 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0047_snapshot.json +5391 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0048_snapshot.json +5398 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0049_snapshot.json +5793 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0050_snapshot.json +6288 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0051_snapshot.json +6461 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0052_snapshot.json +6497 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0053_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0054_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0055_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +98 -0
- package/apps/runtime/dist/db/schema.js +294 -5
- package/apps/runtime/dist/docs/dev/api.md +25 -0
- package/apps/runtime/dist/docs/dev/plugins/connections.md +30 -2
- package/apps/runtime/dist/docs/dev/plugins/index.md +1 -1
- package/apps/runtime/dist/docs/dev/plugins/reference.md +7 -4
- package/apps/runtime/dist/docs/dev/plugins/server.md +11 -5
- package/apps/runtime/dist/docs/dev/plugins/space.md +130 -4
- package/apps/runtime/dist/docs/dev/plugins/studio.md +13 -3
- package/apps/runtime/dist/docs/user-guide/command-line.md +2 -0
- package/apps/runtime/dist/docs/user-guide/plugins.md +1 -1
- package/apps/runtime/dist/docs/user-guide/space.md +6 -6
- package/apps/runtime/dist/docs/user-guide/steps.md +1 -1
- package/apps/runtime/dist/documents/ocr.js +72 -0
- package/apps/runtime/dist/documents/parse.js +90 -12
- package/apps/runtime/dist/elements/caller.js +5 -3
- package/apps/runtime/dist/elements/gate.js +1 -1
- package/apps/runtime/dist/elements/knowledge.js +181 -12
- package/apps/runtime/dist/elements/specs.js +1 -1
- package/apps/runtime/dist/elements/wizards.js +15 -2
- package/apps/runtime/dist/engenty/connections-google/connectors/drive.js +3 -0
- package/apps/runtime/dist/engenty/connections-microsoft/onedrive.js +3 -0
- package/apps/runtime/dist/engenty/connections-s3/s3.js +6 -0
- package/apps/runtime/dist/engine/runner.js +19 -2
- package/apps/runtime/dist/engine/steps.js +1 -1
- package/apps/runtime/dist/env.js +6 -0
- package/apps/runtime/dist/files/blobs.js +57 -1
- package/apps/runtime/dist/files/objects.js +1 -0
- package/apps/runtime/dist/files/pictures.js +41 -0
- package/apps/runtime/dist/files/storage.js +14 -26
- package/apps/runtime/dist/files/thumbs.js +77 -0
- package/apps/runtime/dist/files/upload-check.js +2 -2
- package/apps/runtime/dist/index.js +22 -0
- package/apps/runtime/dist/live/views.js +2 -1
- package/apps/runtime/dist/manage.js +4 -1
- package/apps/runtime/dist/mcp/tools.js +5 -0
- package/apps/runtime/dist/models.js +82 -13
- package/apps/runtime/dist/mounts/cache.js +71 -0
- package/apps/runtime/dist/mounts/drafts.js +367 -0
- package/apps/runtime/dist/mounts/folder.js +148 -0
- package/apps/runtime/dist/mounts/mounts.js +1315 -0
- package/apps/runtime/dist/mounts/source.js +179 -0
- package/apps/runtime/dist/mounts/units.js +97 -0
- package/apps/runtime/dist/move-files.js +4 -0
- package/apps/runtime/dist/plugins/loader.js +73 -1
- package/apps/runtime/dist/plugins/registry.js +2 -0
- package/apps/runtime/dist/prompts/goose.md +13 -1
- package/apps/runtime/dist/prompts/tools.md +5 -2
- package/apps/runtime/dist/render/convert.js +188 -1
- package/apps/runtime/dist/routes/api.js +6 -1
- package/apps/runtime/dist/routes/client-contract.js +164 -0
- package/apps/runtime/dist/routes/client-spaces.js +58 -0
- package/apps/runtime/dist/routes/dateien.js +173 -0
- package/apps/runtime/dist/routes/elements.js +2 -2
- package/apps/runtime/dist/routes/plugins.js +2 -1
- package/apps/runtime/dist/routes/runs.js +24 -10
- package/apps/runtime/dist/routes/studio.js +168 -8
- package/apps/runtime/dist/secrets/signing.js +16 -0
- package/apps/runtime/dist/services/client-portal.js +439 -0
- package/apps/runtime/dist/services/client-spaces.js +228 -0
- package/apps/runtime/dist/services/cloud.js +2 -0
- package/apps/runtime/dist/services/knowledge-model.js +24 -8
- package/apps/runtime/dist/services/knowledge.js +72 -42
- package/apps/runtime/dist/services/links.js +17 -0
- package/apps/runtime/dist/services/members-studio.js +5 -1
- package/apps/runtime/dist/services/page-pictures.js +162 -0
- package/apps/runtime/dist/services/runs.js +22 -6
- package/apps/runtime/dist/services/space-categories.js +41 -5
- package/apps/runtime/dist/services/space-data.js +165 -8
- package/apps/runtime/dist/services/space-files.js +102 -19
- package/apps/runtime/dist/services/space-index.js +384 -41
- package/apps/runtime/dist/services/space-origin.js +25 -1
- package/apps/runtime/dist/services/space-sources.js +937 -0
- package/apps/runtime/dist/services/spaces.js +60 -12
- package/apps/runtime/dist/services/synced-spaces.js +10 -1
- package/apps/runtime/dist/services/work-ai.js +29 -8
- package/apps/runtime/dist/services/work-chats.js +3 -2
- package/apps/runtime/dist/services/work-files.js +25 -1
- package/apps/runtime/dist/sharing/index.js +2 -0
- package/apps/runtime/dist/store/index.js +29 -8
- package/apps/runtime/dist/store/shared-lists.js +30 -17
- package/apps/runtime/dist/sync/records.js +22 -27
- package/apps/runtime/dist/texts.js +4 -0
- package/apps/runtime/dist/tools/pages.js +1 -1
- package/apps/runtime/dist/widgets/step.js +2 -1
- package/apps/web/dist/assets/AboutDialog-C9SnsGno.js +1 -0
- package/apps/web/dist/assets/BlockEditor-CUYFH_CU.js +173 -0
- package/apps/web/dist/assets/BlockEditor-CtW6lw0y.css +1 -0
- package/apps/web/dist/assets/CapabilitiesPage-DvSSybt0.js +2 -0
- package/apps/web/dist/assets/{ChangelogDialog-Do7pwmFv.js → ChangelogDialog-B28TK333.js} +1 -1
- package/apps/web/dist/assets/{CodeEditor-Cdox-51M.js → CodeEditor-D-Y16fdk.js} +1 -1
- package/apps/web/dist/assets/EditorPage-Duu8swX6.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-BPS86xzA.js → FlowPreview-DXnk7m9P.js} +1 -1
- package/apps/web/dist/assets/{LangSwitch-BRHbi_Sd.js → LangSwitch-Dkd0FBIH.js} +1 -1
- package/apps/web/dist/assets/Marketplace-Cty40VwG.js +2 -0
- package/apps/web/dist/assets/NewWizardChannels-B12l2_Jh.js +1 -0
- package/apps/web/dist/assets/NewWizardPage-B10K8vND.js +7 -0
- package/apps/web/dist/assets/OnboardingPage-D3SEpTmt.js +1 -0
- package/apps/web/dist/assets/PreviewPane-DhhQ06Ic.js +7 -0
- package/apps/web/dist/assets/PublicRunner-B8ncIxIP.js +1 -0
- package/apps/web/dist/assets/ReadOnly-DwewuYi8.js +1 -0
- package/apps/web/dist/assets/SettingsPage-C-gGIJLJ.js +2 -0
- package/apps/web/dist/assets/SetupPage-CdsrHW-w.js +1 -0
- package/apps/web/dist/assets/{SharePage-PKHX8a8a.js → SharePage-CZClatA_.js} +1 -1
- package/apps/web/dist/assets/SkillEditor-BaEiqqXl.js +1 -0
- package/apps/web/dist/assets/Skills-Bv6XbKxc.js +9 -0
- package/apps/web/dist/assets/SpacePage-CYhxcjj0.js +2 -0
- package/apps/web/dist/assets/UpdateCheck-CLPUsKVT.js +2 -0
- package/apps/web/dist/assets/changelog-y5oxYzPd.js +1 -0
- package/apps/web/dist/assets/circle-check-DDLzlcXP.js +1 -0
- package/apps/web/dist/assets/code-DsnwzN1x.js +1 -0
- package/apps/web/dist/assets/code-editor-Ca5EAxmd.js +2 -0
- package/apps/web/dist/assets/code-xml-S4IFi5Jc.js +1 -0
- package/apps/web/dist/assets/connection-sign-in-DECedzrs.js +1 -0
- package/apps/web/dist/assets/{dist-H1UN89OA.js → dist-B3Ray2U5.js} +1 -1
- package/apps/web/dist/assets/dist-CQpLJoQZ.js +10 -0
- package/apps/web/dist/assets/{dist-GlRRx9Xl.js → dist-D1uGQ_tB.js} +1 -1
- package/apps/web/dist/assets/{dist-BdDd1FxP.js → dist-DFI-tsfx.js} +1 -1
- package/apps/web/dist/assets/eye-TJSuiHHO.js +1 -0
- package/apps/web/dist/assets/floating-ui.dom-pwdNGv4b.js +1 -0
- package/apps/web/dist/assets/host-CJdLkzfY.js +125 -0
- package/apps/web/dist/assets/i18n-BBzMbGoo.js +55 -0
- package/apps/web/dist/assets/index-CmonJZHC.js +10 -0
- package/apps/web/dist/assets/index-w-3J3ZJ9.css +1 -0
- package/apps/web/dist/assets/{json-CEsfHdlZ.js → json-Ce3iHrtf.js} +1 -1
- package/apps/web/dist/assets/link-CPL8pB1P.js +1 -0
- package/apps/web/dist/assets/list-checks-DaxmUPqK.js +1 -0
- package/apps/web/dist/assets/{markdown-5-I9_izp.js → markdown-hqKQy2pp.js} +1 -1
- package/apps/web/dist/assets/marked.esm-DQ7XA7AC.js +67 -0
- package/apps/web/dist/assets/oss-credits-BrwV0a4F.js +1 -0
- package/apps/web/dist/assets/pdf-Dg-raaUo.js +54 -0
- package/apps/web/dist/assets/pdf.worker.min-68CMzziE.js +1 -0
- package/apps/web/dist/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
- package/apps/web/dist/assets/phone-B4uKDBFq.js +1 -0
- package/apps/web/dist/assets/read-aloud-Bfp9_bUY.js +1 -0
- package/apps/web/dist/assets/{runners-BoPaPsjN.js → runners-CUpBpSji.js} +1 -1
- package/apps/web/dist/assets/scroll-text-BEnPzQEi.js +1 -0
- package/apps/web/dist/assets/session-CMQo8P-G.js +2 -0
- package/apps/web/dist/assets/table-5B9G3oA6.js +1 -0
- package/apps/web/dist/assets/theme-D6KUBJnh.js +11 -0
- package/apps/web/dist/assets/ui-BCfu5sia.js +16 -0
- package/apps/web/dist/assets/upload-CAlHC232.js +1 -0
- package/apps/web/dist/assets/{useMutation-7_FOHoEl.js → useMutation-1fV4Nnxq.js} +1 -1
- package/apps/web/dist/assets/viewers-ChriFcRy.js +2 -0
- package/apps/web/dist/assets/volume-2-BqlBtCIJ.js +1 -0
- package/apps/web/dist/assets/w3c-keyname-BOAvb0qz.js +1 -0
- package/apps/web/dist/assets/xlsx-CuSbHU4R.js +141 -0
- package/apps/web/dist/index.html +19 -14
- package/apps/web/dist/mcp/flow.html +2 -2
- package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +223 -4
- package/node_modules/@engenty/wizards/dist/plugin/mount.d.ts +123 -0
- package/node_modules/@engenty/wizards/dist/plugin/mount.js +7 -0
- package/node_modules/@engenty/wizards/dist/plugin/studio.d.ts +110 -1
- package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +1 -1
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +24 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +24 -0
- package/node_modules/@engenty/wizards-shared/dist/knowledge.d.ts +33 -4
- package/node_modules/@engenty/wizards-shared/dist/run.d.ts +3 -0
- package/node_modules/@engenty/wizards-shared/dist/run.js +6 -0
- package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +21 -4
- package/node_modules/@engenty/wizards-shared/dist/rwx.js +27 -3
- package/node_modules/@engenty/wizards-shared/dist/spaces.d.ts +11 -1
- package/node_modules/@engenty/wizards-shared/dist/spaces.js +9 -2
- package/node_modules/@engenty/wizards-shared/dist/text-merge.d.ts +6 -0
- package/node_modules/@engenty/wizards-shared/dist/text-merge.js +25 -0
- package/package.json +3 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/plugin.json +1 -1
- package/apps/web/dist/assets/AboutDialog-DaWEIHFN.js +0 -1
- package/apps/web/dist/assets/CapabilitiesPage-Dt91U_sK.js +0 -2
- package/apps/web/dist/assets/EditorPage-DS_a5K3s.js +0 -3
- package/apps/web/dist/assets/Marketplace-CS7JTgiv.js +0 -2
- package/apps/web/dist/assets/NewWizardChannels-D-OLsex6.js +0 -1
- package/apps/web/dist/assets/NewWizardPage-BM_pkk41.js +0 -7
- package/apps/web/dist/assets/OnboardingPage-Cpn5pubC.js +0 -1
- package/apps/web/dist/assets/PreviewPane-BOk1-yO4.js +0 -9
- package/apps/web/dist/assets/PublicRunner-CgzMZb0r.js +0 -1
- package/apps/web/dist/assets/ReadOnly-8DEx-ly3.js +0 -1
- package/apps/web/dist/assets/SettingsPage-CcrknFcI.js +0 -2
- package/apps/web/dist/assets/SetupPage-D5nzjgPU.js +0 -1
- package/apps/web/dist/assets/SkillEditor-BsXTqNli.js +0 -1
- package/apps/web/dist/assets/Skills-C_X1ZsKg.js +0 -9
- package/apps/web/dist/assets/SpacePage-wZGqUoSQ.js +0 -2
- package/apps/web/dist/assets/UpdateCheck-D6x7MqEl.js +0 -2
- package/apps/web/dist/assets/changelog-M-eOBqgu.js +0 -1
- package/apps/web/dist/assets/circle-alert-DJbCyxcg.js +0 -1
- package/apps/web/dist/assets/circle-check-jqtsKi_b.js +0 -1
- package/apps/web/dist/assets/code-editor-BpCEnf8H.js +0 -2
- package/apps/web/dist/assets/code-xml-q88cSTV8.js +0 -1
- package/apps/web/dist/assets/connection-sign-in-UJ6xk-TD.js +0 -1
- package/apps/web/dist/assets/dist-EBVTpvRr.js +0 -10
- package/apps/web/dist/assets/eye-CgkvVlba.js +0 -1
- package/apps/web/dist/assets/host-Bd_bon_c.js +0 -118
- package/apps/web/dist/assets/i18n-0fUKyVRE.js +0 -55
- package/apps/web/dist/assets/index-D18DvnxX.css +0 -1
- package/apps/web/dist/assets/index-DOgwxnto.js +0 -10
- package/apps/web/dist/assets/oss-credits-IdX5bc71.js +0 -1
- package/apps/web/dist/assets/phone-CjPvZ0nk.js +0 -1
- package/apps/web/dist/assets/read-aloud-CLXo_asN.js +0 -1
- package/apps/web/dist/assets/scroll-text-2BhJO_W7.js +0 -1
- package/apps/web/dist/assets/session-CpYa5hwd.js +0 -2
- package/apps/web/dist/assets/theme-Bj3wNQR0.js +0 -11
- package/apps/web/dist/assets/ui-CktezodE.js +0 -82
- package/apps/web/dist/assets/upload-CHGCFVZK.js +0 -1
- package/apps/web/dist/assets/volume-2-Bw5KASy8.js +0 -1
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { featureOn } from "@engenty/wizards-shared/features";
|
|
2
|
+
import { and, desc, eq, inArray, isNotNull, ne, or } from "drizzle-orm";
|
|
3
|
+
import { decide, readableIds, subjectsOfPerson } from "../broker/rwx.js";
|
|
4
|
+
import { db, schema } from "../db/client.js";
|
|
5
|
+
import { personCaller } from "../elements/caller.js";
|
|
6
|
+
import { elementCall } from "../elements/gate.js";
|
|
7
|
+
import { env } from "../env.js";
|
|
8
|
+
import { currentFeatures } from "../features.js";
|
|
9
|
+
import { tenantInfo } from "../manage.js";
|
|
10
|
+
import { runTicket, runViewTicket } from "../secrets/signing.js";
|
|
11
|
+
import { currentTenant } from "../tenants/tenant.js";
|
|
12
|
+
import { brandView } from "./brand.js";
|
|
13
|
+
import { claimClientInvites } from "./client-spaces.js";
|
|
14
|
+
import { notFound, ServiceError } from "./errors.js";
|
|
15
|
+
import { startRun } from "./runs.js";
|
|
16
|
+
import { spaceFileContent, spaceFiles } from "./space-files.js";
|
|
17
|
+
import { listSpaces } from "./spaces.js";
|
|
18
|
+
import { listWizards } from "./wizards.js";
|
|
19
|
+
import { createChat, readChat, sendMessage } from "./work-chats.js";
|
|
20
|
+
const clientSubjects = (who) => subjectsOfPerson(who.id, "client");
|
|
21
|
+
/** The contract is there for clients of an organization with client spaces and the broker on. */
|
|
22
|
+
async function requirePortal(who) {
|
|
23
|
+
const features = await currentFeatures();
|
|
24
|
+
if (features.clientSpaces !== true || !featureOn(features, "broker")) {
|
|
25
|
+
throw notFound();
|
|
26
|
+
}
|
|
27
|
+
if (who.role !== "client") {
|
|
28
|
+
throw new ServiceError("forbidden", "Only for clients of the organization.");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Where the person is one of the clients: a join with `space_member` (role `client`), so the
|
|
33
|
+
* same one query answers whether an item is missing or is someone else's (docs/plans/
|
|
34
|
+
* client-portal.md, "Addresses"): both are not found, alike in answer and in time.
|
|
35
|
+
*/
|
|
36
|
+
const asClient = (who) => and(eq(schema.spaceMember.userId, who.id), eq(schema.spaceMember.role, "client"), eq(schema.space.kind, "client"), eq(schema.space.tenantId, currentTenant()));
|
|
37
|
+
/** One of the client's client spaces, with its parent. */
|
|
38
|
+
async function clientSpace(who, spaceId) {
|
|
39
|
+
await requirePortal(who);
|
|
40
|
+
const [row] = await db
|
|
41
|
+
.select({ space: schema.space })
|
|
42
|
+
.from(schema.space)
|
|
43
|
+
.innerJoin(schema.spaceMember, eq(schema.spaceMember.spaceId, schema.space.id))
|
|
44
|
+
.where(and(eq(schema.space.id, spaceId), asClient(who)))
|
|
45
|
+
.limit(1);
|
|
46
|
+
const parent = row?.space.parentId
|
|
47
|
+
? await db.query.space.findFirst({ where: eq(schema.space.id, row.space.parentId) })
|
|
48
|
+
: undefined;
|
|
49
|
+
if (!(row && parent)) {
|
|
50
|
+
throw notFound();
|
|
51
|
+
}
|
|
52
|
+
return { space: row.space, parent };
|
|
53
|
+
}
|
|
54
|
+
/** Whether the person is one of the client space's clients. */
|
|
55
|
+
async function isClientOf(userId, spaceId) {
|
|
56
|
+
const row = await db.query.spaceMember.findFirst({
|
|
57
|
+
columns: { id: true },
|
|
58
|
+
where: and(eq(schema.spaceMember.spaceId, spaceId), eq(schema.spaceMember.userId, userId), eq(schema.spaceMember.role, "client")),
|
|
59
|
+
});
|
|
60
|
+
return Boolean(row);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Whether a client opens a run: one of their client space's, started by them or shared with them
|
|
64
|
+
* by the firm. The run's own routes ask it too (routes/runs.ts).
|
|
65
|
+
*/
|
|
66
|
+
export async function clientMayOpen(userId, run) {
|
|
67
|
+
if (!run.clientSpaceId || !(run.userId === userId || run.clientSharedAt)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return isClientOf(userId, run.clientSpaceId);
|
|
71
|
+
}
|
|
72
|
+
/** A logo's address on the runtime, for a page on another origin. */
|
|
73
|
+
const absolute = (path) => (path ? `${env.publicUrl}${path}` : null);
|
|
74
|
+
/** The firm's brand, as its space shows it: the client portal wears it. */
|
|
75
|
+
async function firmBrand(parentId) {
|
|
76
|
+
const brand = await brandView(parentId);
|
|
77
|
+
return { ...brand, logoUrl: absolute(brand.logoUrl) };
|
|
78
|
+
}
|
|
79
|
+
// --- who and where ---------------------------------------------------------------------------
|
|
80
|
+
/** The person and their organization: the portal checks the host against its address. */
|
|
81
|
+
export async function clientMe(who) {
|
|
82
|
+
await requirePortal(who);
|
|
83
|
+
// A client who signs in only at the portal takes up their invitations here.
|
|
84
|
+
await claimClientInvites(who.id, who.email);
|
|
85
|
+
const org = await tenantInfo(who.tenantId).catch(() => null);
|
|
86
|
+
return {
|
|
87
|
+
person: { id: who.id, name: who.name, email: who.email },
|
|
88
|
+
organization: {
|
|
89
|
+
id: who.tenantId,
|
|
90
|
+
name: org?.name ?? "",
|
|
91
|
+
slug: org?.slug ?? null,
|
|
92
|
+
pagesHost: org?.pagesHost ?? null,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** The client's client spaces, each with the firm's space and brand. */
|
|
97
|
+
export async function clientSpaces(who) {
|
|
98
|
+
await requirePortal(who);
|
|
99
|
+
await claimClientInvites(who.id, who.email);
|
|
100
|
+
const spaces = (await listSpaces(who.id)).filter((s) => s.kind === "client" && s.parentId);
|
|
101
|
+
const parents = spaces.length
|
|
102
|
+
? await db.query.space.findMany({
|
|
103
|
+
columns: { id: true, name: true },
|
|
104
|
+
where: inArray(schema.space.id, spaces.map((s) => s.parentId)),
|
|
105
|
+
})
|
|
106
|
+
: [];
|
|
107
|
+
return Promise.all(spaces.map(async (s) => ({
|
|
108
|
+
id: s.id,
|
|
109
|
+
name: s.name,
|
|
110
|
+
firm: {
|
|
111
|
+
name: parents.find((p) => p.id === s.parentId)?.name ?? "",
|
|
112
|
+
brand: await firmBrand(s.parentId),
|
|
113
|
+
},
|
|
114
|
+
})));
|
|
115
|
+
}
|
|
116
|
+
// --- wizards ---------------------------------------------------------------------------------
|
|
117
|
+
/** The parent's published wizards the firm opened to its clients: read and run. */
|
|
118
|
+
async function openWizards(who, parent) {
|
|
119
|
+
const published = (await listWizards(who.id, parent.id)).filter((w) => w.published);
|
|
120
|
+
const ids = published.map((w) => w.id);
|
|
121
|
+
const readable = new Set(await readableIds(parent.id, clientSubjects(who), "wizard", ids));
|
|
122
|
+
const open = [];
|
|
123
|
+
for (const w of published) {
|
|
124
|
+
if (readable.has(w.id) &&
|
|
125
|
+
(await decide(parent.id, clientSubjects(who), { kind: "wizard", id: w.id }, "x")).ok) {
|
|
126
|
+
open.push(w);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return open.map((w) => ({
|
|
130
|
+
id: w.id,
|
|
131
|
+
title: w.title,
|
|
132
|
+
description: w.description ?? "",
|
|
133
|
+
avatar: w.avatar,
|
|
134
|
+
steps: w.stepCount,
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
export async function clientWizards(who, spaceId) {
|
|
138
|
+
const { parent } = await clientSpace(who, spaceId);
|
|
139
|
+
return openWizards(who, parent);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Starts one of the opened wizards in the client space: the wizard is the parent's, the run the
|
|
143
|
+
* client space's. A line of the audit like every element call.
|
|
144
|
+
*/
|
|
145
|
+
export async function clientStartWizard(who, spaceId, wizardId) {
|
|
146
|
+
const { space, parent } = await clientSpace(who, spaceId);
|
|
147
|
+
const caller = personCaller({
|
|
148
|
+
space: { id: space.id, name: space.name },
|
|
149
|
+
userId: who.id,
|
|
150
|
+
role: "client",
|
|
151
|
+
signal: new AbortController().signal,
|
|
152
|
+
client: who.client,
|
|
153
|
+
});
|
|
154
|
+
return elementCall(caller, {
|
|
155
|
+
action: "wizard_start",
|
|
156
|
+
resource: { kind: "wizard", id: wizardId },
|
|
157
|
+
rulesIn: parent.id,
|
|
158
|
+
perm: "x",
|
|
159
|
+
input: { wizardId },
|
|
160
|
+
}, async () => {
|
|
161
|
+
if (!(await openWizards(who, parent)).some((w) => w.id === wizardId)) {
|
|
162
|
+
throw notFound();
|
|
163
|
+
}
|
|
164
|
+
const { runId } = await startRun(who.id, wizardId, {}, { clientSpaceId: space.id });
|
|
165
|
+
return { runId, ticket: runTicket(runId) };
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
// --- runs and results ------------------------------------------------------------------------
|
|
169
|
+
/** The runs of the client space the client sees: their own, and those shared with them. */
|
|
170
|
+
const seenRuns = (who, spaceId) => and(eq(schema.run.clientSpaceId, spaceId), or(eq(schema.run.userId, who.id), isNotNull(schema.run.clientSharedAt)));
|
|
171
|
+
async function titlesOf(wizardIds) {
|
|
172
|
+
const rows = wizardIds.length
|
|
173
|
+
? await db.query.wizard.findMany({
|
|
174
|
+
columns: { id: true, title: true },
|
|
175
|
+
where: inArray(schema.wizard.id, [...new Set(wizardIds)]),
|
|
176
|
+
})
|
|
177
|
+
: [];
|
|
178
|
+
return new Map(rows.map((w) => [w.id, w.title]));
|
|
179
|
+
}
|
|
180
|
+
const resultOf = (who, titles) => (r) => ({
|
|
181
|
+
runId: r.id,
|
|
182
|
+
wizardId: r.wizardId,
|
|
183
|
+
title: titles.get(r.wizardId) ?? r.definition.title,
|
|
184
|
+
avatar: r.definition.avatar,
|
|
185
|
+
status: r.status,
|
|
186
|
+
mine: r.userId === who.id,
|
|
187
|
+
sharedAt: r.clientSharedAt?.toISOString() ?? null,
|
|
188
|
+
updatedAt: r.updatedAt.toISOString(),
|
|
189
|
+
});
|
|
190
|
+
/** The results of the client space the client sees, newest first. */
|
|
191
|
+
export async function clientResults(who, spaceId) {
|
|
192
|
+
const { space } = await clientSpace(who, spaceId);
|
|
193
|
+
const rows = await db.query.run.findMany({
|
|
194
|
+
where: and(seenRuns(who, space.id), eq(schema.run.status, "done")),
|
|
195
|
+
orderBy: [desc(schema.run.updatedAt)],
|
|
196
|
+
limit: 100,
|
|
197
|
+
});
|
|
198
|
+
const titles = await titlesOf(rows.map((r) => r.wizardId));
|
|
199
|
+
return rows.map(resultOf(who, titles));
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* A ticket for one run the client sees, by the run alone (a deep link, `/portal/r/<run>`): the
|
|
203
|
+
* run's own routes open with it (`?rt=`); its client space comes along. A run the firm shared
|
|
204
|
+
* gets a ticket that only reads it.
|
|
205
|
+
*/
|
|
206
|
+
export async function clientOpenRun(who, runId) {
|
|
207
|
+
await requirePortal(who);
|
|
208
|
+
const [row] = await db
|
|
209
|
+
.select({ id: schema.run.id, spaceId: schema.space.id, userId: schema.run.userId })
|
|
210
|
+
.from(schema.run)
|
|
211
|
+
.innerJoin(schema.space, eq(schema.space.id, schema.run.clientSpaceId))
|
|
212
|
+
.innerJoin(schema.spaceMember, eq(schema.spaceMember.spaceId, schema.space.id))
|
|
213
|
+
.where(and(eq(schema.run.id, runId), asClient(who), or(eq(schema.run.userId, who.id), isNotNull(schema.run.clientSharedAt))))
|
|
214
|
+
.limit(1);
|
|
215
|
+
if (!row) {
|
|
216
|
+
throw notFound();
|
|
217
|
+
}
|
|
218
|
+
// The client's own run they go on with; one the firm shared they only read.
|
|
219
|
+
const mine = row.userId === who.id;
|
|
220
|
+
return {
|
|
221
|
+
runId: row.id,
|
|
222
|
+
spaceId: row.spaceId,
|
|
223
|
+
readOnly: !mine,
|
|
224
|
+
ticket: mine ? runTicket(row.id) : runViewTicket(row.id),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* What waits for the client: their runs waiting on a page or an answer, the latest results the
|
|
229
|
+
* firm shared, the wizards they can start.
|
|
230
|
+
*/
|
|
231
|
+
export async function clientForYou(who, spaceId) {
|
|
232
|
+
const { space, parent } = await clientSpace(who, spaceId);
|
|
233
|
+
const [waiting, shared, wizards] = await Promise.all([
|
|
234
|
+
db.query.run.findMany({
|
|
235
|
+
where: and(eq(schema.run.clientSpaceId, space.id), eq(schema.run.userId, who.id), or(eq(schema.run.status, "waiting_input"), and(eq(schema.run.status, "running"), isNotNull(schema.run.ask)))),
|
|
236
|
+
orderBy: [desc(schema.run.updatedAt)],
|
|
237
|
+
limit: 20,
|
|
238
|
+
}),
|
|
239
|
+
db.query.run.findMany({
|
|
240
|
+
where: and(eq(schema.run.clientSpaceId, space.id), isNotNull(schema.run.clientSharedAt), eq(schema.run.status, "done")),
|
|
241
|
+
orderBy: [desc(schema.run.clientSharedAt)],
|
|
242
|
+
limit: 10,
|
|
243
|
+
}),
|
|
244
|
+
openWizards(who, parent),
|
|
245
|
+
]);
|
|
246
|
+
const titles = await titlesOf([...waiting, ...shared].map((r) => r.wizardId));
|
|
247
|
+
return {
|
|
248
|
+
waiting: waiting.map(resultOf(who, titles)),
|
|
249
|
+
shared: shared.map(resultOf(who, titles)),
|
|
250
|
+
wizards,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
// --- chats -----------------------------------------------------------------------------------
|
|
254
|
+
/** The client space's chats the client is a member of: the firm's other chats stay internal. */
|
|
255
|
+
async function memberChats(who, spaceId) {
|
|
256
|
+
const mine = await db
|
|
257
|
+
.select({ chatId: schema.chatMember.chatId })
|
|
258
|
+
.from(schema.chatMember)
|
|
259
|
+
.where(and(eq(schema.chatMember.kind, "person"), eq(schema.chatMember.memberId, who.id)));
|
|
260
|
+
if (!mine.length) {
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
return db.query.chat.findMany({
|
|
264
|
+
where: and(eq(schema.chat.spaceId, spaceId), inArray(schema.chat.id, mine.map((m) => m.chatId))),
|
|
265
|
+
orderBy: [desc(schema.chat.updatedAt)],
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
export async function clientChats(who, spaceId) {
|
|
269
|
+
const { space } = await clientSpace(who, spaceId);
|
|
270
|
+
return (await memberChats(who, space.id)).map((c) => ({
|
|
271
|
+
id: c.id,
|
|
272
|
+
title: c.title,
|
|
273
|
+
updatedAt: c.updatedAt.toISOString(),
|
|
274
|
+
last: c.lastText ? { name: c.lastAuthorName ?? "", text: c.lastText } : null,
|
|
275
|
+
answering: Boolean(c.answeringSince),
|
|
276
|
+
}));
|
|
277
|
+
}
|
|
278
|
+
/** One chat the client is a member of, in one of their client spaces, by the chat alone. */
|
|
279
|
+
async function memberChat(who, chatId) {
|
|
280
|
+
await requirePortal(who);
|
|
281
|
+
const [row] = await db
|
|
282
|
+
.select({ chat: schema.chat })
|
|
283
|
+
.from(schema.chat)
|
|
284
|
+
.innerJoin(schema.space, eq(schema.space.id, schema.chat.spaceId))
|
|
285
|
+
.innerJoin(schema.spaceMember, eq(schema.spaceMember.spaceId, schema.space.id))
|
|
286
|
+
.innerJoin(schema.chatMember, and(eq(schema.chatMember.chatId, schema.chat.id), eq(schema.chatMember.kind, "person"), eq(schema.chatMember.memberId, who.id)))
|
|
287
|
+
.where(and(eq(schema.chat.id, chatId), asClient(who)))
|
|
288
|
+
.limit(1);
|
|
289
|
+
if (!row) {
|
|
290
|
+
throw notFound();
|
|
291
|
+
}
|
|
292
|
+
return row.chat;
|
|
293
|
+
}
|
|
294
|
+
/** A tool's name in a message part: `tool-<name>`, or a dynamic tool's own. */
|
|
295
|
+
const toolOf = (p) => p.type === "dynamic-tool" && typeof p.toolName === "string"
|
|
296
|
+
? p.toolName
|
|
297
|
+
: p.type.startsWith("tool-")
|
|
298
|
+
? p.type.slice(5)
|
|
299
|
+
: null;
|
|
300
|
+
/**
|
|
301
|
+
* A chat's messages as the client reads them: who wrote, the text, and the AI's views and the
|
|
302
|
+
* runs it started as cards, in the order the AI wrote them (no other tool calls). A run shows
|
|
303
|
+
* only where the client opens it.
|
|
304
|
+
*/
|
|
305
|
+
export async function clientChatMessages(who, chatId) {
|
|
306
|
+
const chat = await memberChat(who, chatId);
|
|
307
|
+
const read = await readChat(who, chat.id);
|
|
308
|
+
const done = (p) => p.state === "output-available" && p.output && typeof p.output === "object";
|
|
309
|
+
const runIds = read.messages.flatMap((m) => m.parts
|
|
310
|
+
.filter((p) => toolOf(p) === "wizard_run" && done(p))
|
|
311
|
+
.map((p) => p.output.runId)
|
|
312
|
+
.filter((id) => typeof id === "string"));
|
|
313
|
+
const runs = runIds.length
|
|
314
|
+
? await db.query.run.findMany({
|
|
315
|
+
where: and(inArray(schema.run.id, runIds), seenRuns(who, chat.spaceId)),
|
|
316
|
+
})
|
|
317
|
+
: [];
|
|
318
|
+
const titles = await titlesOf(runs.map((r) => r.wizardId));
|
|
319
|
+
const cardOf = (p) => {
|
|
320
|
+
const tool = toolOf(p);
|
|
321
|
+
if (tool === "show_view" && done(p) && p.output.ok === true) {
|
|
322
|
+
const input = (p.input ?? {});
|
|
323
|
+
return {
|
|
324
|
+
kind: "view",
|
|
325
|
+
id: String(p.toolCallId ?? ""),
|
|
326
|
+
title: input.title ?? null,
|
|
327
|
+
components: input.components ?? [],
|
|
328
|
+
data: (input.data ?? {}),
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
if (tool === "wizard_run" && done(p)) {
|
|
332
|
+
const run = runs.find((r) => r.id === p.output.runId);
|
|
333
|
+
return run
|
|
334
|
+
? {
|
|
335
|
+
kind: "run",
|
|
336
|
+
runId: run.id,
|
|
337
|
+
wizardId: run.wizardId,
|
|
338
|
+
title: titles.get(run.wizardId) ?? run.definition.title,
|
|
339
|
+
avatar: run.definition.avatar,
|
|
340
|
+
status: run.status,
|
|
341
|
+
}
|
|
342
|
+
: null;
|
|
343
|
+
}
|
|
344
|
+
return null;
|
|
345
|
+
};
|
|
346
|
+
const partOf = (p) => p.type === "text"
|
|
347
|
+
? typeof p.text === "string" && p.text.trim()
|
|
348
|
+
? { kind: "text", text: p.text.trim() }
|
|
349
|
+
: null
|
|
350
|
+
: cardOf(p);
|
|
351
|
+
return {
|
|
352
|
+
spaceId: chat.spaceId,
|
|
353
|
+
chat: { id: chat.id, title: chat.title, answering: Boolean(read.chat.answeringSince) },
|
|
354
|
+
messages: read.messages
|
|
355
|
+
.map((m) => {
|
|
356
|
+
const parts = m.parts.map(partOf).filter((p) => p !== null);
|
|
357
|
+
return {
|
|
358
|
+
id: m.id,
|
|
359
|
+
author: { kind: m.author.kind, name: m.author.name, me: m.author.id === who.id },
|
|
360
|
+
text: parts.flatMap((p) => (p.kind === "text" ? [p.text] : [])).join("\n\n"),
|
|
361
|
+
parts,
|
|
362
|
+
createdAt: m.createdAt,
|
|
363
|
+
};
|
|
364
|
+
})
|
|
365
|
+
.filter((m) => m.parts.length),
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Writes in one of the client's chats, or starts a new one. The firm's AI answers every message
|
|
370
|
+
* (docs/plans/client-portal.md, phase 1), as the client and with a client's tools (work-ai.ts):
|
|
371
|
+
* its turn runs on, the next read shows the answer. The firm's people read the chat as well.
|
|
372
|
+
*/
|
|
373
|
+
export async function clientChatPost(who, spaceId, input) {
|
|
374
|
+
const { space } = await clientSpace(who, spaceId);
|
|
375
|
+
const chatId = input.chatId
|
|
376
|
+
? (await memberChat(who, input.chatId)).id
|
|
377
|
+
: (await createChat(who, { spaceId: space.id })).id;
|
|
378
|
+
const sent = await sendMessage(who, chatId, input.text, { ask: true });
|
|
379
|
+
if (sent.kind === "turn") {
|
|
380
|
+
// Nobody follows the stream here: it is read to its end so the turn runs through.
|
|
381
|
+
void sent.stream.pipeTo(new WritableStream()).catch(() => undefined);
|
|
382
|
+
}
|
|
383
|
+
return { chatId, sent: sent.kind };
|
|
384
|
+
}
|
|
385
|
+
// --- files -----------------------------------------------------------------------------------
|
|
386
|
+
/** The client space's documents and files, to download: one still being read is there already. */
|
|
387
|
+
export async function clientFiles(who, spaceId) {
|
|
388
|
+
const { space } = await clientSpace(who, spaceId);
|
|
389
|
+
const rows = (await spaceFiles(space.id)).filter((f) => (f.kind === "document" || f.kind === "asset") && f.status !== "failed");
|
|
390
|
+
const shown = new Set(await readableIds(space.id, clientSubjects(who), "document", rows.filter((f) => f.kind === "document").map((f) => f.id)));
|
|
391
|
+
return rows
|
|
392
|
+
.filter((f) => f.kind !== "document" || shown.has(f.id))
|
|
393
|
+
.map((f) => ({
|
|
394
|
+
id: f.id,
|
|
395
|
+
name: f.name,
|
|
396
|
+
mime: f.mime,
|
|
397
|
+
size: f.size,
|
|
398
|
+
createdAt: f.createdAt.toISOString(),
|
|
399
|
+
}));
|
|
400
|
+
}
|
|
401
|
+
/** One file of the client's client spaces, by the file alone (a deep link, `/portal/f/<file>`). */
|
|
402
|
+
async function clientFileRow(who, fileId) {
|
|
403
|
+
await requirePortal(who);
|
|
404
|
+
const [row] = await db
|
|
405
|
+
.select({ file: schema.spaceFile })
|
|
406
|
+
.from(schema.spaceFile)
|
|
407
|
+
.innerJoin(schema.space, eq(schema.space.id, schema.spaceFile.spaceId))
|
|
408
|
+
.innerJoin(schema.spaceMember, eq(schema.spaceMember.spaceId, schema.space.id))
|
|
409
|
+
.where(and(eq(schema.spaceFile.id, fileId), inArray(schema.spaceFile.kind, ["document", "asset"]), ne(schema.spaceFile.status, "failed"), asClient(who)))
|
|
410
|
+
.limit(1);
|
|
411
|
+
const file = row?.file;
|
|
412
|
+
const readable = file &&
|
|
413
|
+
(file.kind !== "document" ||
|
|
414
|
+
(await readableIds(file.spaceId, clientSubjects(who), "document", [file.id])).length > 0);
|
|
415
|
+
if (!(file && readable)) {
|
|
416
|
+
throw notFound();
|
|
417
|
+
}
|
|
418
|
+
return file;
|
|
419
|
+
}
|
|
420
|
+
/** What a file is, and its client space. */
|
|
421
|
+
export async function clientFileInfo(who, fileId) {
|
|
422
|
+
const f = await clientFileRow(who, fileId);
|
|
423
|
+
return {
|
|
424
|
+
spaceId: f.spaceId,
|
|
425
|
+
file: {
|
|
426
|
+
id: f.id,
|
|
427
|
+
name: f.name,
|
|
428
|
+
mime: f.mime,
|
|
429
|
+
size: f.size,
|
|
430
|
+
createdAt: f.createdAt.toISOString(),
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
/** One file's bytes, as the client may read it. */
|
|
435
|
+
export async function clientFile(who, fileId) {
|
|
436
|
+
const f = await clientFileRow(who, fileId);
|
|
437
|
+
const { row, data } = await spaceFileContent(f.spaceId, f.id, clientSubjects(who));
|
|
438
|
+
return { name: row.name, mime: row.mime, data };
|
|
439
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { note } from "@engenty/wizards-shared/run";
|
|
2
|
+
import { and, count, eq, inArray, isNull } from "drizzle-orm";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
import { openToClients } from "../broker/rwx.js";
|
|
5
|
+
import { db, schema } from "../db/client.js";
|
|
6
|
+
import { currentFeatures } from "../features.js";
|
|
7
|
+
import { inviteMember, managed } from "../manage.js";
|
|
8
|
+
import { spaceChanged } from "../plugins/events.js";
|
|
9
|
+
import { currentTenant } from "../tenants/tenant.js";
|
|
10
|
+
import { currentRole, requireAdmin, requireBuild } from "./access.js";
|
|
11
|
+
import { notFound, ServiceError } from "./errors.js";
|
|
12
|
+
import { ownedSpace, removeSpace } from "./spaces.js";
|
|
13
|
+
/*
|
|
14
|
+
* Client spaces (docs/plans/client-spaces.md): a space's clients, each in a client space under it.
|
|
15
|
+
* The firm makes them in its space; who is in one (`space_member`) decides which client spaces a
|
|
16
|
+
* client of the organization sees. Behind the `clientSpaces` switch.
|
|
17
|
+
*/
|
|
18
|
+
/** Client spaces are behind their switch: while it is off, they are not there. */
|
|
19
|
+
export async function requireClientSpaces() {
|
|
20
|
+
if ((await currentFeatures()).clientSpaces !== true) {
|
|
21
|
+
throw notFound();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** A space of the firm that may hold client spaces: not a client space itself. */
|
|
25
|
+
async function firmSpace(userId, spaceId) {
|
|
26
|
+
const space = await ownedSpace(userId, spaceId);
|
|
27
|
+
if (space.kind !== "space" || currentRole() === "client") {
|
|
28
|
+
throw notFound();
|
|
29
|
+
}
|
|
30
|
+
return space;
|
|
31
|
+
}
|
|
32
|
+
/** A client space, as the firm reaches it. */
|
|
33
|
+
async function clientSpace(userId, spaceId) {
|
|
34
|
+
const space = await ownedSpace(userId, spaceId);
|
|
35
|
+
if (space.kind !== "client" || currentRole() === "client") {
|
|
36
|
+
throw notFound();
|
|
37
|
+
}
|
|
38
|
+
return space;
|
|
39
|
+
}
|
|
40
|
+
/** The client spaces of a space, with how many people are in each, newest first. */
|
|
41
|
+
export async function listClientSpaces(userId, spaceId) {
|
|
42
|
+
await requireClientSpaces();
|
|
43
|
+
const space = await firmSpace(userId, spaceId);
|
|
44
|
+
const rows = await db.query.space.findMany({
|
|
45
|
+
where: and(eq(schema.space.parentId, space.id), eq(schema.space.tenantId, currentTenant())),
|
|
46
|
+
orderBy: (s, { desc }) => [desc(s.createdAt)],
|
|
47
|
+
});
|
|
48
|
+
const counts = rows.length
|
|
49
|
+
? await db
|
|
50
|
+
.select({ spaceId: schema.spaceMember.spaceId, role: schema.spaceMember.role, n: count() })
|
|
51
|
+
.from(schema.spaceMember)
|
|
52
|
+
.where(inArray(schema.spaceMember.spaceId, rows.map((r) => r.id)))
|
|
53
|
+
.groupBy(schema.spaceMember.spaceId, schema.spaceMember.role)
|
|
54
|
+
: [];
|
|
55
|
+
const people = (id, role) => counts.find((c) => c.spaceId === id && c.role === role)?.n ?? 0;
|
|
56
|
+
return rows.map((r) => ({
|
|
57
|
+
id: r.id,
|
|
58
|
+
name: r.name,
|
|
59
|
+
createdAt: r.createdAt.toISOString(),
|
|
60
|
+
clients: people(r.id, "client"),
|
|
61
|
+
members: people(r.id, "member"),
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
/** A new client space under a space of the firm. Its admins make them. */
|
|
65
|
+
export async function createClientSpace(userId, spaceId, name) {
|
|
66
|
+
await requireClientSpaces();
|
|
67
|
+
await requireBuild();
|
|
68
|
+
await requireAdmin();
|
|
69
|
+
const space = await firmSpace(userId, spaceId);
|
|
70
|
+
const trimmed = name.trim();
|
|
71
|
+
if (!trimmed) {
|
|
72
|
+
throw new ServiceError("invalid", "A client space needs a name.");
|
|
73
|
+
}
|
|
74
|
+
const id = nanoid(12);
|
|
75
|
+
await db.insert(schema.space).values({
|
|
76
|
+
id,
|
|
77
|
+
tenantId: currentTenant(),
|
|
78
|
+
name: trimmed,
|
|
79
|
+
brand: {},
|
|
80
|
+
kind: "client",
|
|
81
|
+
parentId: space.id,
|
|
82
|
+
});
|
|
83
|
+
spaceChanged("space.created", id);
|
|
84
|
+
return { id };
|
|
85
|
+
}
|
|
86
|
+
/** Renames a client space. Its admins do. */
|
|
87
|
+
export async function renameClientSpace(userId, spaceId, name) {
|
|
88
|
+
await requireClientSpaces();
|
|
89
|
+
await requireAdmin();
|
|
90
|
+
const space = await clientSpace(userId, spaceId);
|
|
91
|
+
const trimmed = name.trim() || space.name;
|
|
92
|
+
await db
|
|
93
|
+
.update(schema.space)
|
|
94
|
+
.set({ name: trimmed, updatedAt: new Date() })
|
|
95
|
+
.where(eq(schema.space.id, space.id));
|
|
96
|
+
spaceChanged("space.updated", space.id);
|
|
97
|
+
return { name: trimmed };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Shares a run of a client space with its clients, on purpose, or takes it back
|
|
101
|
+
* (docs/plans/client-portal.md): until then a run the firm makes there stays the firm's.
|
|
102
|
+
*/
|
|
103
|
+
export async function shareWithClient(userId, runId, shared) {
|
|
104
|
+
await requireClientSpaces();
|
|
105
|
+
if (currentRole() === "client") {
|
|
106
|
+
throw notFound();
|
|
107
|
+
}
|
|
108
|
+
const run = await db.query.run.findFirst({
|
|
109
|
+
columns: { id: true, clientSpaceId: true, status: true },
|
|
110
|
+
where: and(eq(schema.run.id, runId), eq(schema.run.tenantId, currentTenant())),
|
|
111
|
+
});
|
|
112
|
+
if (!run?.clientSpaceId) {
|
|
113
|
+
throw notFound();
|
|
114
|
+
}
|
|
115
|
+
await clientSpace(userId, run.clientSpaceId);
|
|
116
|
+
if (shared && run.status !== "done") {
|
|
117
|
+
throw new ServiceError("refused", note("onlyFinishedResultsShared"));
|
|
118
|
+
}
|
|
119
|
+
const sharedAt = shared ? new Date() : null;
|
|
120
|
+
await db.update(schema.run).set({ clientSharedAt: sharedAt }).where(eq(schema.run.id, run.id));
|
|
121
|
+
return { sharedAt: sharedAt?.toISOString() ?? null };
|
|
122
|
+
}
|
|
123
|
+
/** Closes a client space: its data, results and chats go with it. */
|
|
124
|
+
export async function deleteClientSpace(userId, spaceId) {
|
|
125
|
+
await requireClientSpaces();
|
|
126
|
+
await requireAdmin();
|
|
127
|
+
const space = await clientSpace(userId, spaceId);
|
|
128
|
+
await removeSpace(space.id);
|
|
129
|
+
}
|
|
130
|
+
/** Who is in a client space: the client's people and the firm's assigned members. */
|
|
131
|
+
export async function listSpaceMembers(userId, spaceId) {
|
|
132
|
+
await requireClientSpaces();
|
|
133
|
+
const space = await clientSpace(userId, spaceId);
|
|
134
|
+
const rows = await db.query.spaceMember.findMany({
|
|
135
|
+
where: eq(schema.spaceMember.spaceId, space.id),
|
|
136
|
+
orderBy: (m, { asc }) => [asc(m.createdAt)],
|
|
137
|
+
});
|
|
138
|
+
return rows.map((m) => ({
|
|
139
|
+
userId: m.userId,
|
|
140
|
+
email: m.email,
|
|
141
|
+
role: m.role,
|
|
142
|
+
addedAt: m.createdAt.toISOString(),
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
145
|
+
/** Puts a person of the organization into a client space, as a client or as the firm's. */
|
|
146
|
+
export async function addSpaceMember(userId, spaceId, member) {
|
|
147
|
+
await requireClientSpaces();
|
|
148
|
+
await requireAdmin();
|
|
149
|
+
const space = await clientSpace(userId, spaceId);
|
|
150
|
+
await db
|
|
151
|
+
.insert(schema.spaceMember)
|
|
152
|
+
.values({
|
|
153
|
+
id: nanoid(12),
|
|
154
|
+
tenantId: currentTenant(),
|
|
155
|
+
spaceId: space.id,
|
|
156
|
+
userId: member.userId,
|
|
157
|
+
role: member.role,
|
|
158
|
+
addedBy: userId,
|
|
159
|
+
})
|
|
160
|
+
.onConflictDoUpdate({
|
|
161
|
+
target: [schema.spaceMember.spaceId, schema.spaceMember.userId],
|
|
162
|
+
set: { role: member.role },
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/** Takes a person out of a client space. */
|
|
166
|
+
export async function removeSpaceMember(userId, spaceId, memberId) {
|
|
167
|
+
await requireClientSpaces();
|
|
168
|
+
await requireAdmin();
|
|
169
|
+
const space = await clientSpace(userId, spaceId);
|
|
170
|
+
await db
|
|
171
|
+
.delete(schema.spaceMember)
|
|
172
|
+
.where(and(eq(schema.spaceMember.spaceId, space.id), eq(schema.spaceMember.userId, memberId)));
|
|
173
|
+
}
|
|
174
|
+
/** What of a space its clients reach: the items of each kind opened to them. */
|
|
175
|
+
export async function clientOpenOf(userId, spaceId) {
|
|
176
|
+
await requireClientSpaces();
|
|
177
|
+
const space = await firmSpace(userId, spaceId);
|
|
178
|
+
return openToClients(space.id);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Invites a client into a client space by their address: the Manage-App invites them into the
|
|
182
|
+
* organization as a `client` (taking no seat), and the client space keeps the address until they
|
|
183
|
+
* sign in for the first time (`claimClientInvites`). Cloud only: a local install has no accounts.
|
|
184
|
+
*/
|
|
185
|
+
export async function inviteClient(userId, spaceId, input) {
|
|
186
|
+
await requireClientSpaces();
|
|
187
|
+
await requireAdmin();
|
|
188
|
+
const space = await clientSpace(userId, spaceId);
|
|
189
|
+
if (!managed) {
|
|
190
|
+
throw new ServiceError("refused", note("clientsInCloud"));
|
|
191
|
+
}
|
|
192
|
+
const email = input.email.trim().toLowerCase();
|
|
193
|
+
await inviteMember(currentTenant(), { actorId: userId, email, role: "client", lang: input.lang });
|
|
194
|
+
await db.insert(schema.spaceMember).values({
|
|
195
|
+
id: nanoid(12),
|
|
196
|
+
tenantId: currentTenant(),
|
|
197
|
+
spaceId: space.id,
|
|
198
|
+
userId: null,
|
|
199
|
+
email,
|
|
200
|
+
role: "client",
|
|
201
|
+
addedBy: userId,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* A person's first sign-in: the client spaces that invited their address take them in. Called
|
|
206
|
+
* where the runtime signs a person in.
|
|
207
|
+
*/
|
|
208
|
+
export async function claimClientInvites(userId, email) {
|
|
209
|
+
const address = email.trim().toLowerCase();
|
|
210
|
+
if (!address) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const pending = await db.query.spaceMember.findMany({
|
|
214
|
+
where: and(eq(schema.spaceMember.tenantId, currentTenant()), eq(schema.spaceMember.email, address), isNull(schema.spaceMember.userId)),
|
|
215
|
+
});
|
|
216
|
+
for (const row of pending) {
|
|
217
|
+
const already = await db.query.spaceMember.findFirst({
|
|
218
|
+
columns: { id: true },
|
|
219
|
+
where: and(eq(schema.spaceMember.spaceId, row.spaceId), eq(schema.spaceMember.userId, userId)),
|
|
220
|
+
});
|
|
221
|
+
if (already) {
|
|
222
|
+
await db.delete(schema.spaceMember).where(eq(schema.spaceMember.id, row.id));
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
await db.update(schema.spaceMember).set({ userId }).where(eq(schema.spaceMember.id, row.id));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -140,6 +140,8 @@ async function spaceDataOf(spaceId) {
|
|
|
140
140
|
title: t.title,
|
|
141
141
|
format: t.format,
|
|
142
142
|
originLabel: t.originLabel,
|
|
143
|
+
parentId: t.parentId,
|
|
144
|
+
position: t.position,
|
|
143
145
|
columns: t.columns,
|
|
144
146
|
rows: rows.filter((r) => r.tableId === t.id).map((r) => ({ id: r.id, cells: r.cells })),
|
|
145
147
|
})),
|