@engenty/wizards-runtime 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/apps/runtime/dist/agents/architect.js +4 -4
  2. package/apps/runtime/dist/agents/studio-assistant.js +2 -31
  3. package/apps/runtime/dist/app.js +4 -4
  4. package/apps/runtime/dist/assistant/knows.js +2 -2
  5. package/apps/runtime/dist/auth/index.js +1 -1
  6. package/apps/runtime/dist/authoring/example.js +1 -1
  7. package/apps/runtime/dist/authoring/guide.js +35 -6
  8. package/apps/runtime/dist/broker/account.js +85 -16
  9. package/apps/runtime/dist/broker/call.js +137 -37
  10. package/apps/runtime/dist/broker/log.js +19 -5
  11. package/apps/runtime/dist/broker/rwx.js +12 -9
  12. package/apps/runtime/dist/connectors/index.js +52 -11
  13. package/apps/runtime/dist/connectors/person-connections.js +279 -0
  14. package/apps/runtime/dist/connectors/person-sign-ins.js +74 -3
  15. package/apps/runtime/dist/connectors/same-service.js +14 -0
  16. package/apps/runtime/dist/connectors/space-choice.js +116 -0
  17. package/apps/runtime/dist/connectors/space-connections.js +40 -2
  18. package/apps/runtime/dist/db/client.js +8 -0
  19. package/apps/runtime/dist/db/kept-accounts.js +88 -0
  20. package/apps/runtime/dist/db/migrations/tenant/0075_person_connection.sql +25 -0
  21. package/apps/runtime/dist/db/migrations/tenant/0076_clients_in_spaces.sql +28 -0
  22. package/apps/runtime/dist/db/migrations/tenant/0077_client_spaces_at_root.sql +21 -0
  23. package/apps/runtime/dist/db/migrations/tenant/meta/0075_snapshot.json +7859 -0
  24. package/apps/runtime/dist/db/migrations/tenant/meta/0076_snapshot.json +7859 -0
  25. package/apps/runtime/dist/db/migrations/tenant/meta/0077_snapshot.json +7859 -0
  26. package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +21 -0
  27. package/apps/runtime/dist/db/schema.js +67 -16
  28. package/apps/runtime/dist/docs/dev/api.md +8 -8
  29. package/apps/runtime/dist/docs/dev/wizard-format/index.md +3 -1
  30. package/apps/runtime/dist/elements/caller.js +1 -1
  31. package/apps/runtime/dist/elements/knowledge.js +5 -5
  32. package/apps/runtime/dist/elements/wizards.js +7 -11
  33. package/apps/runtime/dist/engine/notices.js +13 -1
  34. package/apps/runtime/dist/engine/requirements.js +116 -3
  35. package/apps/runtime/dist/engine/run-needs.js +58 -0
  36. package/apps/runtime/dist/engine/runner.js +229 -23
  37. package/apps/runtime/dist/engine/starts.js +9 -0
  38. package/apps/runtime/dist/engine/types.js +5 -1
  39. package/apps/runtime/dist/harness/claude.js +3 -1
  40. package/apps/runtime/dist/manage.js +1 -1
  41. package/apps/runtime/dist/mcp/tools.js +3 -3
  42. package/apps/runtime/dist/plugins/runs.js +2 -1
  43. package/apps/runtime/dist/prompts/architect.md +3 -3
  44. package/apps/runtime/dist/prompts/authoring.md +17 -6
  45. package/apps/runtime/dist/prompts/goose.md +0 -5
  46. package/apps/runtime/dist/routes/api.js +9 -4
  47. package/apps/runtime/dist/routes/client-contract.js +2 -2
  48. package/apps/runtime/dist/routes/client-spaces.js +1 -16
  49. package/apps/runtime/dist/routes/connections.js +6 -1
  50. package/apps/runtime/dist/routes/marketplace.js +27 -7
  51. package/apps/runtime/dist/routes/plugins.js +1 -1
  52. package/apps/runtime/dist/routes/runs.js +157 -11
  53. package/apps/runtime/dist/routes/studio.js +42 -5
  54. package/apps/runtime/dist/services/client-portal.js +58 -49
  55. package/apps/runtime/dist/services/client-spaces.js +57 -125
  56. package/apps/runtime/dist/services/links.js +3 -3
  57. package/apps/runtime/dist/services/marketplace.js +6 -2
  58. package/apps/runtime/dist/services/members-studio.js +1 -1
  59. package/apps/runtime/dist/services/query-vectors.js +1 -1
  60. package/apps/runtime/dist/services/runs.js +19 -20
  61. package/apps/runtime/dist/services/space-parent.js +4 -11
  62. package/apps/runtime/dist/services/spaces.js +56 -47
  63. package/apps/runtime/dist/services/work-ai.js +3 -3
  64. package/apps/runtime/dist/services/work-chats.js +14 -6
  65. package/apps/runtime/dist/sharing/state.js +7 -3
  66. package/apps/runtime/dist/store/index.js +28 -8
  67. package/apps/runtime/dist/store/shared-lists.js +2 -2
  68. package/apps/runtime/dist/sync/hub.js +9 -5
  69. package/apps/runtime/dist/tools/connector.js +4 -2
  70. package/apps/web/dist/assets/{AboutDialog-BxT7chz8.js → AboutDialog-QWzpEK8M.js} +1 -1
  71. package/apps/web/dist/assets/{BlockEditor-BX8ZxxIc.js → BlockEditor-C44bI_ih.js} +1 -1
  72. package/apps/web/dist/assets/{Bundle-Dkpi5E6W.js → Bundle-DZyQ1FP7.js} +1 -1
  73. package/apps/web/dist/assets/CapabilitiesPage-DAr0-gN1.js +2 -0
  74. package/apps/web/dist/assets/{ChangelogDialog-j-vHLy-V.js → ChangelogDialog-CdHqJV2P.js} +1 -1
  75. package/apps/web/dist/assets/{CodeEditor-CfM2A-yS.js → CodeEditor-BRufUwu0.js} +1 -1
  76. package/apps/web/dist/assets/ConnectorsSection-BO6jHrYU.js +1 -0
  77. package/apps/web/dist/assets/EditorPage-Bx-fKnsY.js +3 -0
  78. package/apps/web/dist/assets/{FlowPreview-E0kuvM5n.js → FlowPreview-aj58c-gH.js} +1 -1
  79. package/apps/web/dist/assets/{LangSwitch-DhQmCtyY.js → LangSwitch-BNkytlus.js} +1 -1
  80. package/apps/web/dist/assets/Marketplace-D3hdvrkz.js +2 -0
  81. package/apps/web/dist/assets/NewWizardChannels-fveQcQlY.js +1 -0
  82. package/apps/web/dist/assets/NewWizardPage-BTX1g2t1.js +7 -0
  83. package/apps/web/dist/assets/OnboardingPage-DwF6KdQy.js +1 -0
  84. package/apps/web/dist/assets/ProtocolSection-YnJcMN9E.js +1 -0
  85. package/apps/web/dist/assets/PublicRunner-Bp4DxTrD.js +1 -0
  86. package/apps/web/dist/assets/{ReadOnly-CkToYpY5.js → ReadOnly-C6qmTD35.js} +1 -1
  87. package/apps/web/dist/assets/{Results-C9qxCpUQ.js → Results-BNuuBiwU.js} +1 -1
  88. package/apps/web/dist/assets/{RunDrawer-BRXH71U8.js → RunDrawer-CMgN-XTB.js} +1 -1
  89. package/apps/web/dist/assets/SettingsPage-CuUB26YJ.js +2 -0
  90. package/apps/web/dist/assets/SetupPage-llGzMVeH.js +1 -0
  91. package/apps/web/dist/assets/{SharePage-Boj1R928.js → SharePage-CgQp_PT6.js} +1 -1
  92. package/apps/web/dist/assets/SkillEditor-ARx8QPGp.js +1 -0
  93. package/apps/web/dist/assets/Skills-frfXXEcF.js +9 -0
  94. package/apps/web/dist/assets/SpacePage-Dv6ocsc_.js +2 -0
  95. package/apps/web/dist/assets/StudioHome-B0bZRm1L.js +1 -0
  96. package/apps/web/dist/assets/TableEditor-CfzTLmJF.js +7 -0
  97. package/apps/web/dist/assets/{UpdateCheck-DFor0F9T.js → UpdateCheck-D3QhfI5c.js} +2 -2
  98. package/apps/web/dist/assets/{brand-Ij-I_Lms.js → brand-DKuVyYhH.js} +1 -1
  99. package/apps/web/dist/assets/changelog-Db3nvWao.js +1 -0
  100. package/apps/web/dist/assets/{code-editor-BVgSljQq.js → code-editor-BLwDuQLI.js} +2 -2
  101. package/apps/web/dist/assets/host-C-AAp8Bz.js +69 -0
  102. package/apps/web/dist/assets/i18n-Dyi0kvaA.js +55 -0
  103. package/apps/web/dist/assets/index-CduFlFCp.css +1 -0
  104. package/apps/web/dist/assets/index-CtE0rJEL.js +102 -0
  105. package/apps/web/dist/assets/{read-aloud-pd-PWi_5.js → read-aloud-DGS8mWgC.js} +1 -1
  106. package/apps/web/dist/assets/session-EIDP3Wzc.js +1 -0
  107. package/apps/web/dist/assets/theme-B46aPLtX.js +14 -0
  108. package/apps/web/dist/assets/ui-DSgksc1H.js +16 -0
  109. package/apps/web/dist/assets/{useMutation-CUZC19i3.js → useMutation-fT44ij_b.js} +1 -1
  110. package/apps/web/dist/assets/{viewers-D-8HZrcq.js → viewers-DvA5VsXw.js} +2 -2
  111. package/apps/web/dist/index.html +18 -13
  112. package/apps/web/dist/mcp/flow.html +62 -62
  113. package/node_modules/@engenty/wizards-shared/dist/catalog-prompt.js +2 -2
  114. package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +67 -0
  115. package/node_modules/@engenty/wizards-shared/dist/definition.d.ts +10324 -7
  116. package/node_modules/@engenty/wizards-shared/dist/definition.js +32 -6
  117. package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +17 -23
  118. package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +17 -23
  119. package/node_modules/@engenty/wizards-shared/dist/json-schema.js +3 -3
  120. package/node_modules/@engenty/wizards-shared/dist/marketplace-search.js +7 -1
  121. package/node_modules/@engenty/wizards-shared/dist/marketplace.d.ts +9 -2
  122. package/node_modules/@engenty/wizards-shared/dist/page-data.d.ts +72 -0
  123. package/node_modules/@engenty/wizards-shared/dist/page-data.js +5 -0
  124. package/node_modules/@engenty/wizards-shared/dist/page-look.d.ts +17 -0
  125. package/node_modules/@engenty/wizards-shared/dist/page-look.js +118 -0
  126. package/node_modules/@engenty/wizards-shared/dist/page-styles.d.ts +6 -0
  127. package/node_modules/@engenty/wizards-shared/dist/page-styles.js +98 -0
  128. package/node_modules/@engenty/wizards-shared/dist/page-texts.d.ts +36 -0
  129. package/node_modules/@engenty/wizards-shared/dist/page-texts.js +119 -0
  130. package/node_modules/@engenty/wizards-shared/dist/requires.js +2 -0
  131. package/node_modules/@engenty/wizards-shared/dist/run.d.ts +56 -1
  132. package/node_modules/@engenty/wizards-shared/dist/run.js +25 -0
  133. package/node_modules/@engenty/wizards-shared/dist/runner-texts.d.ts +27 -1
  134. package/node_modules/@engenty/wizards-shared/dist/runner-texts.js +26 -0
  135. package/node_modules/@engenty/wizards-shared/dist/store.d.ts +24 -1
  136. package/node_modules/@engenty/wizards-shared/dist/transition.d.ts +2 -1
  137. package/node_modules/@engenty/wizards-shared/dist/transition.js +10 -2
  138. package/node_modules/@engenty/wizards-shared/dist/wizard-text.js +1 -1
  139. package/package.json +1 -1
  140. package/plugin/.claude-plugin/plugin.json +1 -1
  141. package/plugin/plugin.json +1 -1
  142. package/apps/web/dist/assets/CapabilitiesPage-CfSb23wy.js +0 -2
  143. package/apps/web/dist/assets/ConnectorsSection-fRBZYfZu.js +0 -1
  144. package/apps/web/dist/assets/EditorPage-CrFyN_Qn.js +0 -3
  145. package/apps/web/dist/assets/Marketplace-BdkNOARm.js +0 -2
  146. package/apps/web/dist/assets/NewWizardChannels-BATgI9GR.js +0 -1
  147. package/apps/web/dist/assets/NewWizardPage-B5V5ScKh.js +0 -7
  148. package/apps/web/dist/assets/OnboardingPage-DWpDqlCi.js +0 -1
  149. package/apps/web/dist/assets/PreviewPane-Br2w92Wh.js +0 -7
  150. package/apps/web/dist/assets/ProtocolSection-DMCojdZu.js +0 -1
  151. package/apps/web/dist/assets/PublicRunner-Cjhfeqi3.js +0 -1
  152. package/apps/web/dist/assets/SettingsPage-Dobd7tAA.js +0 -2
  153. package/apps/web/dist/assets/SetupPage-BkMjwBlV.js +0 -1
  154. package/apps/web/dist/assets/SkillEditor-DtVRUYqS.js +0 -1
  155. package/apps/web/dist/assets/Skills-DK1Pv-Xh.js +0 -9
  156. package/apps/web/dist/assets/SpacePage-CjgBRkkw.js +0 -2
  157. package/apps/web/dist/assets/StudioHome-B5HcJJ4Q.js +0 -1
  158. package/apps/web/dist/assets/changelog-n9RARUzp.js +0 -1
  159. package/apps/web/dist/assets/host-BELYIPMT.js +0 -69
  160. package/apps/web/dist/assets/i18n-DPqdH1PJ.js +0 -55
  161. package/apps/web/dist/assets/index-CVZlO0z3.css +0 -1
  162. package/apps/web/dist/assets/index-wJZVaKLg.js +0 -10
  163. package/apps/web/dist/assets/runners-D9DHhaD9.js +0 -1
  164. package/apps/web/dist/assets/session-Mt-n8MI0.js +0 -1
  165. package/apps/web/dist/assets/theme-BQhQx_UL.js +0 -14
  166. package/apps/web/dist/assets/ui-D4qQLcbs.js +0 -16
@@ -15,14 +15,17 @@ import { claimClientInvites } from "./client-spaces.js";
15
15
  import { notFound, ServiceError } from "./errors.js";
16
16
  import { startRun } from "./runs.js";
17
17
  import { spaceFileContent, spaceFiles } from "./space-files.js";
18
+ import { brandSpaceId } from "./space-parent.js";
18
19
  import { listSpaces } from "./spaces.js";
19
20
  import { listWizards } from "./wizards.js";
20
21
  import { createChat, readChat, sendMessage } from "./work-chats.js";
21
22
  const clientSubjects = (who) => subjectsOfPerson(who.id, "client");
22
- /** The contract is there for clients of an organization with client spaces and the broker on. */
23
+ /** The contract is there for clients of an organization with clients and the broker on. */
23
24
  async function requirePortal(who) {
24
25
  const features = await currentFeatures();
25
- if (features.clientSpaces !== true || !featureOn(features, "broker")) {
26
+ if (features.clientSpaces !== true ||
27
+ features.orgSpaces !== true ||
28
+ !featureOn(features, "broker")) {
26
29
  throw notFound();
27
30
  }
28
31
  if (who.role !== "client") {
@@ -35,24 +38,24 @@ async function requirePortal(who) {
35
38
  * client-portal.md, "Addresses"): both are not found, alike in answer and in time.
36
39
  */
37
40
  const asClient = (who) => and(eq(schema.spaceMember.userId, who.id), eq(schema.spaceMember.role, "client"), eq(schema.space.tenantId, currentTenant()));
38
- /** One of the client's client spaces, with its parent. */
41
+ /**
42
+ * One of the client spaces the client is in, with the spaces whose wizards it runs: its own and
43
+ * the organization's root.
44
+ */
39
45
  async function clientSpace(who, spaceId) {
40
46
  await requirePortal(who);
41
47
  const [row] = await db
42
48
  .select({ space: schema.space })
43
49
  .from(schema.space)
44
50
  .innerJoin(schema.spaceMember, eq(schema.spaceMember.spaceId, schema.space.id))
45
- .where(and(eq(schema.space.id, spaceId), asClient(who)))
51
+ .where(and(eq(schema.space.id, spaceId), eq(schema.space.kind, "client"), asClient(who)))
46
52
  .limit(1);
47
- const parent = row?.space.parentId
48
- ? await db.query.space.findFirst({ where: eq(schema.space.id, row.space.parentId) })
49
- : undefined;
50
- if (!(row && parent)) {
53
+ if (!row?.space.parentId) {
51
54
  throw notFound();
52
55
  }
53
- return { space: row.space, parent };
56
+ return { space: row.space, sources: [row.space.id, row.space.parentId] };
54
57
  }
55
- /** Whether the person is one of the client space's clients. */
58
+ /** Whether the person is one of the space's clients. */
56
59
  async function isClientOf(userId, spaceId) {
57
60
  const row = await db.query.spaceMember.findFirst({
58
61
  columns: { id: true },
@@ -61,8 +64,8 @@ async function isClientOf(userId, spaceId) {
61
64
  return Boolean(row);
62
65
  }
63
66
  /**
64
- * Whether a client opens a run: one of their client space's, started by them or shared with them
65
- * by the firm. The run's own routes ask it too (routes/runs.ts).
67
+ * Whether a client opens a run: one of their client space's, started by them or shared with them by the
68
+ * firm. The run's own routes ask it too (routes/runs.ts).
66
69
  */
67
70
  export async function clientMayOpen(userId, run) {
68
71
  if (!run.clientSpaceId || !(run.userId === userId || run.clientSharedAt)) {
@@ -72,9 +75,9 @@ export async function clientMayOpen(userId, run) {
72
75
  }
73
76
  /** A logo's address on the runtime, for a page on another origin. */
74
77
  const absolute = (path) => (path ? `${env.publicUrl}${path}` : null);
75
- /** The firm's brand, as its space shows it: the client portal wears it. */
76
- async function firmBrand(parentId) {
77
- const brand = await brandView(parentId);
78
+ /** The space's brand, its own or the organization's it takes: the client portal wears it. */
79
+ async function firmBrand(spaceId) {
80
+ const brand = await brandView(await brandSpaceId(spaceId));
78
81
  return { ...brand, logoUrl: absolute(brand.logoUrl) };
79
82
  }
80
83
  // --- who and where ---------------------------------------------------------------------------
@@ -94,42 +97,40 @@ export async function clientMe(who) {
94
97
  },
95
98
  };
96
99
  }
97
- /** The client's client spaces, each with the firm's space and brand. */
100
+ /** The spaces the client is in, each with the organization's name and the brand it wears. */
98
101
  export async function clientSpaces(who) {
99
102
  await requirePortal(who);
100
103
  await claimClientInvites(who.id, who.email);
101
- // A client space, or with `orgSpaces` any space under the root shared with them: both have a
102
- // parent whose brand the portal wears.
103
- const spaces = (await listSpaces(who.id)).filter((s) => s.parentId);
104
- const parents = spaces.length
105
- ? await db.query.space.findMany({
106
- columns: { id: true, name: true },
107
- where: inArray(schema.space.id, spaces.map((s) => s.parentId)),
108
- })
109
- : [];
104
+ const spaces = (await listSpaces(who.id)).filter((s) => s.kind === "client" && s.parentId);
105
+ const org = spaces.length ? await tenantInfo(who.tenantId).catch(() => null) : null;
110
106
  return Promise.all(spaces.map(async (s) => ({
111
107
  id: s.id,
112
108
  name: s.name,
113
- firm: {
114
- name: parents.find((p) => p.id === s.parentId)?.name ?? "",
115
- brand: await firmBrand(s.parentId),
116
- },
109
+ firm: { name: org?.name ?? "", brand: await firmBrand(s.id) },
117
110
  })));
118
111
  }
119
- // --- wizards ---------------------------------------------------------------------------------
120
- /** The parent's published wizards the firm opened to its clients: read and run. */
121
- async function openWizards(who, parent) {
122
- const published = (await listWizards(who.id, parent.id)).filter((w) => w.published);
123
- const ids = published.map((w) => w.id);
124
- const readable = new Set(await readableIds(parent.id, clientSubjects(who), "wizard", ids));
112
+ /**
113
+ * The published wizards the firm opened to its clients, of the space and of the organization's
114
+ * root, each with the space whose rules decide it: read and run.
115
+ */
116
+ async function openedWizards(who, sources) {
125
117
  const open = [];
126
- for (const w of published) {
127
- if (readable.has(w.id) &&
128
- (await decide(parent.id, clientSubjects(who), { kind: "wizard", id: w.id }, "x")).ok) {
129
- open.push(w);
118
+ for (const spaceId of sources) {
119
+ const published = (await listWizards(who.id, spaceId)).filter((w) => w.published);
120
+ const ids = published.map((w) => w.id);
121
+ const readable = new Set(await readableIds(spaceId, clientSubjects(who), "wizard", ids));
122
+ for (const wizard of published) {
123
+ if (readable.has(wizard.id) &&
124
+ (await decide(spaceId, clientSubjects(who), { kind: "wizard", id: wizard.id }, "x")).ok) {
125
+ open.push({ spaceId, wizard });
126
+ }
130
127
  }
131
128
  }
132
- return open.map((w) => ({
129
+ return open;
130
+ }
131
+ /** The opened wizards as the contract lists them. */
132
+ async function openWizards(who, sources) {
133
+ return (await openedWizards(who, sources)).map(({ wizard: w }) => ({
133
134
  id: w.id,
134
135
  title: w.title,
135
136
  description: w.description ?? "",
@@ -138,15 +139,23 @@ async function openWizards(who, parent) {
138
139
  }));
139
140
  }
140
141
  export async function clientWizards(who, spaceId) {
141
- const { parent } = await clientSpace(who, spaceId);
142
- return openWizards(who, parent);
142
+ const { sources } = await clientSpace(who, spaceId);
143
+ return openWizards(who, sources);
143
144
  }
144
145
  /**
145
- * Starts one of the opened wizards in the client space: the wizard is the parent's, the run the
146
- * client space's. A line of the audit like every element call.
146
+ * Starts one of the opened wizards in the space: the run is the space's, whichever space the
147
+ * wizard is in. A line of the audit like every element call.
147
148
  */
148
149
  export async function clientStartWizard(who, spaceId, wizardId) {
149
- const { space, parent } = await clientSpace(who, spaceId);
150
+ const { space, sources } = await clientSpace(who, spaceId);
151
+ const opened = (await openedWizards(who, sources)).find((o) => o.wizard.id === wizardId);
152
+ // A wizard not opened is refused by the rules of the space it is in, as one opened is decided.
153
+ const home = opened
154
+ ? opened.spaceId
155
+ : ((await db.query.wizard.findFirst({
156
+ columns: { spaceId: true },
157
+ where: and(eq(schema.wizard.id, wizardId), eq(schema.wizard.tenantId, currentTenant())),
158
+ }))?.spaceId ?? space.id);
150
159
  const caller = personCaller({
151
160
  space: { id: space.id, name: space.name },
152
161
  userId: who.id,
@@ -157,11 +166,11 @@ export async function clientStartWizard(who, spaceId, wizardId) {
157
166
  return elementCall(caller, {
158
167
  action: "wizard_start",
159
168
  resource: { kind: "wizard", id: wizardId },
160
- rulesIn: parent.id,
169
+ rulesIn: sources.includes(home) ? home : space.id,
161
170
  perm: "x",
162
171
  input: { wizardId },
163
172
  }, async () => {
164
- if (!(await openWizards(who, parent)).some((w) => w.id === wizardId)) {
173
+ if (!opened) {
165
174
  throw notFound();
166
175
  }
167
176
  const { runId } = await startRun(who.id, wizardId, {}, { clientSpaceId: space.id });
@@ -232,7 +241,7 @@ export async function clientOpenRun(who, runId) {
232
241
  * firm shared, the wizards they can start.
233
242
  */
234
243
  export async function clientForYou(who, spaceId) {
235
- const { space, parent } = await clientSpace(who, spaceId);
244
+ const { space, sources } = await clientSpace(who, spaceId);
236
245
  const [waiting, shared, wizards] = await Promise.all([
237
246
  db.query.run.findMany({
238
247
  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)))),
@@ -244,7 +253,7 @@ export async function clientForYou(who, spaceId) {
244
253
  orderBy: [desc(schema.run.clientSharedAt)],
245
254
  limit: 10,
246
255
  }),
247
- openWizards(who, parent),
256
+ openWizards(who, sources),
248
257
  ]);
249
258
  const titles = await titlesOf([...waiting, ...shared].map((r) => r.wizardId));
250
259
  // What the firm's team is asked waits for them, not for the client.
@@ -1,126 +1,49 @@
1
1
  import { note } from "@engenty/wizards-shared/run";
2
- import { and, count, eq, inArray, isNull } from "drizzle-orm";
2
+ import { and, eq, isNull } from "drizzle-orm";
3
3
  import { nanoid } from "nanoid";
4
4
  import { openToClients, seedClientRules } from "../broker/rwx.js";
5
5
  import { db, schema } from "../db/client.js";
6
6
  import { currentFeatures } from "../features.js";
7
7
  import { inviteMember, managed } from "../manage.js";
8
- import { spaceChanged } from "../plugins/events.js";
9
8
  import { currentTenant } from "../tenants/tenant.js";
10
- import { currentRole, requireAdmin, requireBuild } from "./access.js";
9
+ import { currentRole, requireAdmin } from "./access.js";
11
10
  import { notFound, ServiceError } from "./errors.js";
12
- import { ownedSpace, removeSpace } from "./spaces.js";
11
+ import { ownedSpace } from "./spaces.js";
13
12
  /*
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.
13
+ * Client spaces and who is in a space (docs/plans/org-spaces.md): a client is a client space,
14
+ * beside the team spaces under the organization's root; its client's people are in it
15
+ * (`space_member`, role `client`) and see it alone. A team space takes no clients: who of the
16
+ * team is in it decides only where it is shared with its members only. Clients are behind the
17
+ * `clientSpaces` switch and need `orgSpaces`; a team space's members need `orgSpaces` alone.
17
18
  */
18
- /** Client spaces are behind their switch: while it is off, they are not there. */
19
+ /** Clients are behind their switch, and in the organization's spaces: else they are not there. */
19
20
  export async function requireClientSpaces() {
20
- if ((await currentFeatures()).clientSpaces !== true) {
21
+ const features = await currentFeatures();
22
+ if (features.clientSpaces !== true || features.orgSpaces !== true) {
21
23
  throw notFound();
22
24
  }
23
25
  }
24
- /**
25
- * Who is in a space is kept where there are client spaces, and with `orgSpaces`, where a space
26
- * may be shared with its members only (docs/plans/org-spaces.md). Kund:innen need client spaces.
27
- */
26
+ /** Who is in a space is kept with `orgSpaces`; a client in it needs `clientSpaces` too. */
28
27
  async function requireSpaceMembers(role) {
29
- const features = await currentFeatures();
30
- if (features.clientSpaces === true || (role !== "client" && features.orgSpaces === true)) {
28
+ if (role === "client") {
29
+ await requireClientSpaces();
31
30
  return;
32
31
  }
33
- throw notFound();
34
- }
35
- /** A space of the firm that may hold client spaces: not a client space itself. */
36
- async function firmSpace(userId, spaceId) {
37
- const space = await ownedSpace(userId, spaceId);
38
- if (space.kind !== "space" || currentRole() === "client") {
32
+ if ((await currentFeatures()).orgSpaces !== true) {
39
33
  throw notFound();
40
34
  }
41
- return space;
42
35
  }
43
36
  /**
44
- * A space the firm shares with people: a client space, and with `orgSpaces` any space under the
45
- * organization's root (docs/plans/org-spaces.md) — not the root, not someone's Mein Space.
37
+ * A space with people of its own, under the organization's root: a team space or a client space,
38
+ * never the root or someone's Mein Space. `client`: only a client space.
46
39
  */
47
- async function clientSpace(userId, spaceId) {
40
+ async function sharedSpace(userId, spaceId, as) {
48
41
  const space = await ownedSpace(userId, spaceId);
49
- if (currentRole() === "client") {
42
+ const fits = as === "client" ? space.kind === "client" : space.kind !== "personal";
43
+ if (currentRole() === "client" || !fits || !space.parentId) {
50
44
  throw notFound();
51
45
  }
52
- if (space.kind === "client") {
53
- return space;
54
- }
55
- if (space.kind === "space" && space.parentId && (await currentFeatures()).orgSpaces === true) {
56
- return space;
57
- }
58
- throw notFound();
59
- }
60
- /** A space that is not a client space gets the clients' rules with its first client. */
61
- async function openToItsClients(space) {
62
- if (space.kind !== "client") {
63
- await seedClientRules(space.id);
64
- }
65
- }
66
- /** The client spaces of a space, with how many people are in each, newest first. */
67
- export async function listClientSpaces(userId, spaceId) {
68
- await requireClientSpaces();
69
- const space = await firmSpace(userId, spaceId);
70
- const rows = await db.query.space.findMany({
71
- where: and(eq(schema.space.parentId, space.id), eq(schema.space.tenantId, currentTenant())),
72
- orderBy: (s, { desc }) => [desc(s.createdAt)],
73
- });
74
- const counts = rows.length
75
- ? await db
76
- .select({ spaceId: schema.spaceMember.spaceId, role: schema.spaceMember.role, n: count() })
77
- .from(schema.spaceMember)
78
- .where(inArray(schema.spaceMember.spaceId, rows.map((r) => r.id)))
79
- .groupBy(schema.spaceMember.spaceId, schema.spaceMember.role)
80
- : [];
81
- const people = (id, role) => counts.find((c) => c.spaceId === id && c.role === role)?.n ?? 0;
82
- return rows.map((r) => ({
83
- id: r.id,
84
- name: r.name,
85
- createdAt: r.createdAt.toISOString(),
86
- clients: people(r.id, "client"),
87
- members: people(r.id, "member"),
88
- }));
89
- }
90
- /** A new client space under a space of the firm. Its admins make them. */
91
- export async function createClientSpace(userId, spaceId, name) {
92
- await requireClientSpaces();
93
- await requireBuild();
94
- await requireAdmin();
95
- const space = await firmSpace(userId, spaceId);
96
- const trimmed = name.trim();
97
- if (!trimmed) {
98
- throw new ServiceError("invalid", "A client space needs a name.");
99
- }
100
- const id = nanoid(12);
101
- await db.insert(schema.space).values({
102
- id,
103
- tenantId: currentTenant(),
104
- name: trimmed,
105
- brand: {},
106
- kind: "client",
107
- parentId: space.id,
108
- });
109
- spaceChanged("space.created", id);
110
- return { id };
111
- }
112
- /** Renames a client space. Its admins do. */
113
- export async function renameClientSpace(userId, spaceId, name) {
114
- await requireClientSpaces();
115
- await requireAdmin();
116
- const space = await clientSpace(userId, spaceId);
117
- const trimmed = name.trim() || space.name;
118
- await db
119
- .update(schema.space)
120
- .set({ name: trimmed, updatedAt: new Date() })
121
- .where(eq(schema.space.id, space.id));
122
- spaceChanged("space.updated", space.id);
123
- return { name: trimmed };
46
+ return space;
124
47
  }
125
48
  /**
126
49
  * Shares a run of a client space with its clients, on purpose, or takes it back
@@ -131,32 +54,37 @@ export async function shareWithClient(userId, runId, shared) {
131
54
  if (currentRole() === "client") {
132
55
  throw notFound();
133
56
  }
134
- const run = await db.query.run.findFirst({
135
- columns: { id: true, clientSpaceId: true, status: true },
136
- where: and(eq(schema.run.id, runId), eq(schema.run.tenantId, currentTenant())),
137
- });
138
- if (!run?.clientSpaceId) {
57
+ const [run] = await db
58
+ .select({
59
+ id: schema.run.id,
60
+ clientSpaceId: schema.run.clientSpaceId,
61
+ status: schema.run.status,
62
+ wizardSpaceId: schema.wizard.spaceId,
63
+ })
64
+ .from(schema.run)
65
+ .innerJoin(schema.wizard, eq(schema.wizard.id, schema.run.wizardId))
66
+ .where(and(eq(schema.run.id, runId), eq(schema.run.tenantId, currentTenant())))
67
+ .limit(1);
68
+ if (!run) {
139
69
  throw notFound();
140
70
  }
141
- await clientSpace(userId, run.clientSpaceId);
71
+ // The space the run is in: the one it carries, else its wizard's.
72
+ const space = await sharedSpace(userId, run.clientSpaceId ?? run.wizardSpaceId, "client");
142
73
  if (shared && run.status !== "done") {
143
74
  throw new ServiceError("refused", note("onlyFinishedResultsShared"));
144
75
  }
145
76
  const sharedAt = shared ? new Date() : null;
146
- await db.update(schema.run).set({ clientSharedAt: sharedAt }).where(eq(schema.run.id, run.id));
77
+ // Shared, it carries its space, where the clients look for it.
78
+ await db
79
+ .update(schema.run)
80
+ .set({ clientSharedAt: sharedAt, clientSpaceId: space.id })
81
+ .where(eq(schema.run.id, run.id));
147
82
  return { sharedAt: sharedAt?.toISOString() ?? null };
148
83
  }
149
- /** Closes a client space: its data, results and chats go with it. */
150
- export async function deleteClientSpace(userId, spaceId) {
151
- await requireClientSpaces();
152
- await requireAdmin();
153
- const space = await clientSpace(userId, spaceId);
154
- await removeSpace(space.id);
155
- }
156
- /** Who is in a client space: the client's people and the firm's assigned members. */
84
+ /** Who is in a space: its clients and the people of the organization chosen for it. */
157
85
  export async function listSpaceMembers(userId, spaceId) {
158
86
  await requireSpaceMembers();
159
- const space = await clientSpace(userId, spaceId);
87
+ const space = await sharedSpace(userId, spaceId);
160
88
  const rows = await db.query.spaceMember.findMany({
161
89
  where: eq(schema.spaceMember.spaceId, space.id),
162
90
  orderBy: (m, { asc }) => [asc(m.createdAt)],
@@ -168,11 +96,11 @@ export async function listSpaceMembers(userId, spaceId) {
168
96
  addedAt: m.createdAt.toISOString(),
169
97
  }));
170
98
  }
171
- /** Puts a person of the organization into a client space, as a client or as the firm's. */
99
+ /** Puts a person of the organization into a space: a client into a client space, a member into either. */
172
100
  export async function addSpaceMember(userId, spaceId, member) {
173
101
  await requireSpaceMembers(member.role);
174
102
  await requireAdmin();
175
- const space = await clientSpace(userId, spaceId);
103
+ const space = await sharedSpace(userId, spaceId, member.role === "client" ? "client" : undefined);
176
104
  await db
177
105
  .insert(schema.spaceMember)
178
106
  .values({
@@ -188,14 +116,14 @@ export async function addSpaceMember(userId, spaceId, member) {
188
116
  set: { role: member.role },
189
117
  });
190
118
  if (member.role === "client") {
191
- await openToItsClients(space);
119
+ await seedClientRules(space.id);
192
120
  }
193
121
  }
194
- /** Takes a person out of a client space. */
122
+ /** Takes a person out of a space. */
195
123
  export async function removeSpaceMember(userId, spaceId, memberId) {
196
124
  await requireSpaceMembers();
197
125
  await requireAdmin();
198
- const space = await clientSpace(userId, spaceId);
126
+ const space = await sharedSpace(userId, spaceId);
199
127
  await db
200
128
  .delete(schema.spaceMember)
201
129
  .where(and(eq(schema.spaceMember.spaceId, space.id), eq(schema.spaceMember.userId, memberId)));
@@ -203,18 +131,22 @@ export async function removeSpaceMember(userId, spaceId, memberId) {
203
131
  /** What of a space its clients reach: the items of each kind opened to them. */
204
132
  export async function clientOpenOf(userId, spaceId) {
205
133
  await requireClientSpaces();
206
- const space = await firmSpace(userId, spaceId);
134
+ const space = await ownedSpace(userId, spaceId);
135
+ if (currentRole() === "client") {
136
+ throw notFound();
137
+ }
207
138
  return openToClients(space.id);
208
139
  }
209
140
  /**
210
- * Invites a client into a client space by their address: the Manage-App invites them into the
211
- * organization as a `client` (taking no seat), and the client space keeps the address until they
212
- * sign in for the first time (`claimClientInvites`). Cloud only: a local install has no accounts.
141
+ * Invites one of a client's people into their client space by their address: the Manage-App
142
+ * invites them into the organization as a `client` (taking no seat), and the client space keeps
143
+ * the address until they sign in for the first time on the organization's page
144
+ * (`claimClientInvites`). Cloud only: a local install has no accounts.
213
145
  */
214
146
  export async function inviteClient(userId, spaceId, input) {
215
147
  await requireClientSpaces();
216
148
  await requireAdmin();
217
- const space = await clientSpace(userId, spaceId);
149
+ const space = await sharedSpace(userId, spaceId, "client");
218
150
  if (!managed) {
219
151
  throw new ServiceError("refused", note("clientsInCloud"));
220
152
  }
@@ -229,7 +161,7 @@ export async function inviteClient(userId, spaceId, input) {
229
161
  role: "client",
230
162
  addedBy: userId,
231
163
  });
232
- await openToItsClients(space);
164
+ await seedClientRules(space.id);
233
165
  }
234
166
  /**
235
167
  * A person's first sign-in: the client spaces that invited their address take them in. Called
@@ -16,10 +16,10 @@ export async function forgetWizardLinks(wizardId) {
16
16
  await dropRuns(runs.map((r) => r.id));
17
17
  }
18
18
  /**
19
- * Takes the runs of a client space away with what they made: they belong to it, though their
20
- * wizards stay the parent's (docs/plans/client-portal.md).
19
+ * Takes the runs a space carries away with what they made: a client's, and those of the root's
20
+ * wizards started in it. They belong to it, though their wizards are elsewhere.
21
21
  */
22
- export async function removeClientSpaceRuns(spaceId) {
22
+ export async function removeRunsIn(spaceId) {
23
23
  const runs = await db
24
24
  .select({ id: schema.run.id, shareToken: schema.run.shareToken })
25
25
  .from(schema.run)
@@ -1,6 +1,6 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { statSync } from "node:fs";
3
- import { DEFINITION_VERSION, readable, wizardSchema, } from "@engenty/wizards-shared/definition";
3
+ import { DEFINITION_VERSION, FORMAT_VERSIONS, formatVersionOf, readable, wizardSchema, } from "@engenty/wizards-shared/definition";
4
4
  import { capabilitiesOf, costTierOf, effortOf, formatsOf, MARKETPLACE_LANGS, } from "@engenty/wizards-shared/marketplace";
5
5
  import { wizardOutline } from "@engenty/wizards-shared/marketplace-entry";
6
6
  import { searchEntries } from "@engenty/wizards-shared/marketplace-search";
@@ -160,6 +160,7 @@ function addFacets(a, b) {
160
160
  format: add(a.format, b.format),
161
161
  capability: add(a.capability, b.capability),
162
162
  plan: add(a.plan, b.plan),
163
+ version: add(a.version, b.version),
163
164
  };
164
165
  }
165
166
  /**
@@ -190,8 +191,11 @@ function asEntry(summary, starred, unavailable, optional = new Set()) {
190
191
  const lacking = summary.capabilities.filter((c) => unavailable.has(c));
191
192
  return {
192
193
  ...summary,
194
+ // A marketplace that still writes the version the earlier way is read as today's.
195
+ version: String(formatVersionOf(summary.version)),
193
196
  credits: null,
194
- usable: summary.version <= DEFINITION_VERSION,
197
+ // A format this app reads.
198
+ usable: FORMAT_VERSIONS.includes(formatVersionOf(summary.version)),
195
199
  starred: starred.has(summary.id),
196
200
  missing: lacking.filter((c) => !optional.has(c)),
197
201
  optional: lacking.filter((c) => optional.has(c)),
@@ -80,7 +80,7 @@ export async function spaceOfItem(kind, id, sub) {
80
80
  where: and(eq(schema.run.id, id), eq(schema.run.tenantId, tenant)),
81
81
  columns: { wizardId: true, clientSpaceId: true },
82
82
  });
83
- // A run of a client space is the client space's, though its wizard is the parent's.
83
+ // A run that carries a space is that space's, though its wizard is the root's.
84
84
  if (run?.clientSpaceId) {
85
85
  return run.clientSpaceId;
86
86
  }
@@ -4,7 +4,7 @@ import { embeddingModel } from "../models.js";
4
4
  /**
5
5
  * The vector of a search's query. The embedding model is the index's too: in an Ollama that is
6
6
  * embedding a folder, a query's call waits behind every passage sent before it (36 s measured).
7
- * So a query is embedded once (the palette asks twice, a client space's search once per space),
7
+ * So a query is embedded once (the palette asks twice, a client's search once per space),
8
8
  * the indexers send nothing new while one is on its way (`searchesFirst`), and a search waits
9
9
  * only as long as it is given.
10
10
  */
@@ -450,9 +450,9 @@ export function refusedInput(err) {
450
450
  }
451
451
  /**
452
452
  * Runs the published version of a wizard for the admin, from their own AI client: a live run
453
- * that is theirs — no visitor, no share link needed. With answers the first page is filled. In a
454
- * client space the wizard is its parent's and the run belongs to the client space
455
- * (docs/plans/client-portal.md).
453
+ * that is theirs — no visitor, no share link needed. With answers the first page is filled. A run
454
+ * that carries a space (`clientSpaceId`) belongs to it, whichever space its wizard is in
455
+ * (docs/plans/org-spaces.md).
456
456
  */
457
457
  export async function startRun(userId, wizardId, answers = {}, { clientSpaceId = null, chatId = null, } = {}) {
458
458
  const w = await ownedWizard(userId, wizardId);
@@ -708,21 +708,20 @@ export async function runTimeline(run, canAct) {
708
708
  * `filter`; and each wizard with how many it has and when its last one came, the latest first.
709
709
  */
710
710
  export async function listResults(spaceId, filter = {}) {
711
- // A client space shows the runs started in it, of its parent's wizards; a space only its own
712
- // (docs/plans/client-portal.md). With `orgSpaces` a space under the root shows its own wizards'
713
- // runs and the runs of the root's wizards started in it (docs/plans/org-spaces.md).
711
+ // A space shows its own wizards' runs, a client's among them, and with `orgSpaces` the runs of
712
+ // the root's wizards started in it (docs/plans/org-spaces.md). A run in another space than its
713
+ // wizard's, or a client's, carries that space (`clientSpaceId`).
714
714
  const reads = await readsOf(spaceId);
715
- const client = reads?.as === "client" ? spaceId : null;
716
- const wizardSpaces = reads?.as === "client"
717
- ? [reads.parentId]
718
- : reads?.as === "root"
719
- ? [spaceId, reads.parentId]
720
- : [spaceId];
721
- const inSpace = client
722
- ? eq(schema.run.clientSpaceId, client)
723
- : reads?.as === "root"
724
- ? or(and(eq(schema.wizard.spaceId, spaceId), isNull(schema.run.clientSpaceId)), and(eq(schema.wizard.spaceId, reads.parentId), eq(schema.run.clientSpaceId, spaceId)))
725
- : isNull(schema.run.clientSpaceId);
715
+ const wizardSpaces = reads ? [spaceId, reads.parentId] : [spaceId];
716
+ // In a client space, each result says whether it is shared with its clients.
717
+ const client = await db.query.spaceMember.findFirst({
718
+ columns: { id: true },
719
+ where: and(eq(schema.spaceMember.spaceId, spaceId), eq(schema.spaceMember.role, "client")),
720
+ });
721
+ const here = or(isNull(schema.run.clientSpaceId), eq(schema.run.clientSpaceId, spaceId));
722
+ const inSpace = reads
723
+ ? or(and(eq(schema.wizard.spaceId, spaceId), here), and(eq(schema.wizard.spaceId, reads.parentId), eq(schema.run.clientSpaceId, spaceId)))
724
+ : here;
726
725
  const wizards = await db
727
726
  .select({
728
727
  id: schema.wizard.id,
@@ -745,9 +744,9 @@ export async function listResults(spaceId, filter = {}) {
745
744
  const ids = wizards.map((w) => w.id).filter((id) => !filter.wizardId || id === filter.wizardId);
746
745
  // The runs of this space, by the wizards listed: the root's only those started here.
747
746
  const fromRoot = new Set(wizards.filter((w) => w.spaceId !== spaceId).map((w) => w.id));
748
- const runsHere = reads?.as === "root"
749
- ? or(and(inArray(schema.run.wizardId, ids.filter((id) => !fromRoot.has(id)).concat("")), isNull(schema.run.clientSpaceId)), and(inArray(schema.run.wizardId, [...fromRoot].concat("")), eq(schema.run.clientSpaceId, spaceId)))
750
- : inSpace;
747
+ const runsHere = reads
748
+ ? or(and(inArray(schema.run.wizardId, ids.filter((id) => !fromRoot.has(id)).concat("")), here), and(inArray(schema.run.wizardId, [...fromRoot].concat("")), eq(schema.run.clientSpaceId, spaceId)))
749
+ : here;
751
750
  const rows = ids.length
752
751
  ? await db
753
752
  .select({
@@ -3,19 +3,13 @@ import { db, schema } from "../db/client.js";
3
3
  import { currentFeatures } from "../features.js";
4
4
  export async function readsOf(spaceId) {
5
5
  const space = await db.query.space.findFirst({
6
- columns: { kind: true, parentId: true },
6
+ columns: { parentId: true },
7
7
  where: eq(schema.space.id, spaceId),
8
8
  });
9
- if (!space?.parentId) {
9
+ if (!space?.parentId || (await currentFeatures()).orgSpaces !== true) {
10
10
  return null;
11
11
  }
12
- if (space.kind === "client") {
13
- return { parentId: space.parentId, as: "client" };
14
- }
15
- if ((await currentFeatures()).orgSpaces === true) {
16
- return { parentId: space.parentId, as: "root" };
17
- }
18
- return null;
12
+ return { parentId: space.parentId };
19
13
  }
20
14
  /**
21
15
  * The space whose brand files (logos, patterns, brand assets) a space shows: the root's where the
@@ -23,12 +17,11 @@ export async function readsOf(spaceId) {
23
17
  */
24
18
  export async function brandSpaceId(spaceId) {
25
19
  const space = await db.query.space.findFirst({
26
- columns: { brandFrom: true, parentId: true, kind: true },
20
+ columns: { brandFrom: true, parentId: true },
27
21
  where: eq(schema.space.id, spaceId),
28
22
  });
29
23
  if (space?.brandFrom === "root" &&
30
24
  space.parentId &&
31
- space.kind !== "client" &&
32
25
  (await currentFeatures()).orgSpaces === true) {
33
26
  return space.parentId;
34
27
  }