@engenty/wizards-runtime 0.14.2 → 0.16.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 (263) hide show
  1. package/apps/runtime/dist/agents/studio-assistant.js +180 -4
  2. package/apps/runtime/dist/agents/website.js +10 -5
  3. package/apps/runtime/dist/app.js +27 -2
  4. package/apps/runtime/dist/auth/index.js +5 -0
  5. package/apps/runtime/dist/broker/account.js +5 -0
  6. package/apps/runtime/dist/broker/call.js +2 -0
  7. package/apps/runtime/dist/broker/host.js +1 -0
  8. package/apps/runtime/dist/broker/index.js +5 -0
  9. package/apps/runtime/dist/broker/rwx.js +293 -11
  10. package/apps/runtime/dist/cli/main.js +7 -0
  11. package/apps/runtime/dist/cli/move-files.js +68 -0
  12. package/apps/runtime/dist/cli/move-folder-sources.js +120 -0
  13. package/apps/runtime/dist/connectors/plugin-connections.js +74 -4
  14. package/apps/runtime/dist/db/migrations/tenant/0040_rwx_elements.sql +21 -0
  15. package/apps/runtime/dist/db/migrations/tenant/0041_broker_log_client.sql +1 -0
  16. package/apps/runtime/dist/db/migrations/tenant/0042_client_spaces.sql +16 -0
  17. package/apps/runtime/dist/db/migrations/tenant/0043_space_member_invite.sql +19 -0
  18. package/apps/runtime/dist/db/migrations/tenant/0044_client_portal.sql +8 -0
  19. package/apps/runtime/dist/db/migrations/tenant/0045_client_portal_no_slug.sql +2 -0
  20. package/apps/runtime/dist/db/migrations/tenant/0046_space_file_hash.sql +2 -0
  21. package/apps/runtime/dist/db/migrations/tenant/0047_knowledge_folders.sql +3 -0
  22. package/apps/runtime/dist/db/migrations/tenant/0048_space_chunk_kind.sql +1 -0
  23. package/apps/runtime/dist/db/migrations/tenant/0049_knowledge_sources.sql +57 -0
  24. package/apps/runtime/dist/db/migrations/tenant/0050_dateien.sql +71 -0
  25. package/apps/runtime/dist/db/migrations/tenant/0051_dateien_drafts.sql +23 -0
  26. package/apps/runtime/dist/db/migrations/tenant/0052_dateien_plugin_kinds.sql +5 -0
  27. package/apps/runtime/dist/db/migrations/tenant/0053_dateien_file_title.sql +3 -0
  28. package/apps/runtime/dist/db/migrations/tenant/0054_dateien_ocr.sql +14 -0
  29. package/apps/runtime/dist/db/migrations/tenant/0055_dateien_doc_reread.sql +3 -0
  30. package/apps/runtime/dist/db/migrations/tenant/meta/0040_snapshot.json +5153 -0
  31. package/apps/runtime/dist/db/migrations/tenant/meta/0041_snapshot.json +5160 -0
  32. package/apps/runtime/dist/db/migrations/tenant/meta/0042_snapshot.json +5286 -0
  33. package/apps/runtime/dist/db/migrations/tenant/meta/0043_snapshot.json +5293 -0
  34. package/apps/runtime/dist/db/migrations/tenant/meta/0044_snapshot.json +5344 -0
  35. package/apps/runtime/dist/db/migrations/tenant/meta/0045_snapshot.json +5329 -0
  36. package/apps/runtime/dist/db/migrations/tenant/meta/0046_snapshot.json +5343 -0
  37. package/apps/runtime/dist/db/migrations/tenant/meta/0047_snapshot.json +5391 -0
  38. package/apps/runtime/dist/db/migrations/tenant/meta/0048_snapshot.json +5398 -0
  39. package/apps/runtime/dist/db/migrations/tenant/meta/0049_snapshot.json +5793 -0
  40. package/apps/runtime/dist/db/migrations/tenant/meta/0050_snapshot.json +6288 -0
  41. package/apps/runtime/dist/db/migrations/tenant/meta/0051_snapshot.json +6461 -0
  42. package/apps/runtime/dist/db/migrations/tenant/meta/0052_snapshot.json +6497 -0
  43. package/apps/runtime/dist/db/migrations/tenant/meta/0053_snapshot.json +6504 -0
  44. package/apps/runtime/dist/db/migrations/tenant/meta/0054_snapshot.json +6504 -0
  45. package/apps/runtime/dist/db/migrations/tenant/meta/0055_snapshot.json +6504 -0
  46. package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +112 -0
  47. package/apps/runtime/dist/db/schema.js +296 -5
  48. package/apps/runtime/dist/docs/dev/api.md +49 -2
  49. package/apps/runtime/dist/docs/dev/architecture.md +13 -2
  50. package/apps/runtime/dist/docs/dev/plugins/connections.md +30 -2
  51. package/apps/runtime/dist/docs/dev/plugins/index.md +1 -1
  52. package/apps/runtime/dist/docs/dev/plugins/reference.md +7 -4
  53. package/apps/runtime/dist/docs/dev/plugins/server.md +11 -5
  54. package/apps/runtime/dist/docs/dev/plugins/space.md +130 -4
  55. package/apps/runtime/dist/docs/dev/plugins/studio.md +13 -3
  56. package/apps/runtime/dist/docs/user-guide/command-line.md +2 -0
  57. package/apps/runtime/dist/docs/user-guide/plugins.md +1 -1
  58. package/apps/runtime/dist/docs/user-guide/space.md +6 -6
  59. package/apps/runtime/dist/docs/user-guide/steps.md +1 -1
  60. package/apps/runtime/dist/documents/ocr.js +72 -0
  61. package/apps/runtime/dist/documents/parse.js +81 -12
  62. package/apps/runtime/dist/elements/caller.js +47 -0
  63. package/apps/runtime/dist/elements/catalogue.js +14 -0
  64. package/apps/runtime/dist/elements/connections.js +60 -0
  65. package/apps/runtime/dist/elements/gate.js +81 -0
  66. package/apps/runtime/dist/elements/knowledge.js +308 -0
  67. package/apps/runtime/dist/elements/outside.js +103 -0
  68. package/apps/runtime/dist/elements/specs.js +102 -0
  69. package/apps/runtime/dist/elements/wizards.js +72 -0
  70. package/apps/runtime/dist/engenty/connections-google/connectors/drive.js +3 -0
  71. package/apps/runtime/dist/engenty/connections-microsoft/onedrive.js +3 -0
  72. package/apps/runtime/dist/engenty/connections-s3/s3.js +6 -0
  73. package/apps/runtime/dist/engine/runner.js +19 -2
  74. package/apps/runtime/dist/engine/steps.js +1 -1
  75. package/apps/runtime/dist/env.js +11 -2
  76. package/apps/runtime/dist/files/blobs.js +57 -1
  77. package/apps/runtime/dist/files/objects.js +1 -0
  78. package/apps/runtime/dist/files/pictures.js +41 -0
  79. package/apps/runtime/dist/files/storage.js +14 -26
  80. package/apps/runtime/dist/files/thumbs.js +77 -0
  81. package/apps/runtime/dist/files/upload-check.js +2 -2
  82. package/apps/runtime/dist/index.js +22 -0
  83. package/apps/runtime/dist/live/views.js +2 -1
  84. package/apps/runtime/dist/manage.js +4 -1
  85. package/apps/runtime/dist/mcp/elements.js +88 -0
  86. package/apps/runtime/dist/mcp/handler.js +2 -0
  87. package/apps/runtime/dist/mcp/scopes.js +5 -0
  88. package/apps/runtime/dist/mcp/tools.js +5 -0
  89. package/apps/runtime/dist/models.js +82 -13
  90. package/apps/runtime/dist/mounts/cache.js +71 -0
  91. package/apps/runtime/dist/mounts/drafts.js +367 -0
  92. package/apps/runtime/dist/mounts/folder.js +148 -0
  93. package/apps/runtime/dist/mounts/mounts.js +1315 -0
  94. package/apps/runtime/dist/mounts/source.js +179 -0
  95. package/apps/runtime/dist/mounts/units.js +97 -0
  96. package/apps/runtime/dist/move-files.js +4 -0
  97. package/apps/runtime/dist/plugins/loader.js +73 -1
  98. package/apps/runtime/dist/plugins/registry.js +2 -0
  99. package/apps/runtime/dist/prompts/authoring.md +2 -2
  100. package/apps/runtime/dist/prompts/goose.md +13 -1
  101. package/apps/runtime/dist/prompts/tools.md +8 -2
  102. package/apps/runtime/dist/render/convert.js +188 -1
  103. package/apps/runtime/dist/routes/api.js +7 -2
  104. package/apps/runtime/dist/routes/client-contract.js +164 -0
  105. package/apps/runtime/dist/routes/client-spaces.js +58 -0
  106. package/apps/runtime/dist/routes/dateien.js +173 -0
  107. package/apps/runtime/dist/routes/elements.js +76 -0
  108. package/apps/runtime/dist/routes/plugins.js +2 -1
  109. package/apps/runtime/dist/routes/runs.js +24 -10
  110. package/apps/runtime/dist/routes/studio.js +168 -8
  111. package/apps/runtime/dist/secrets/signing.js +16 -0
  112. package/apps/runtime/dist/services/client-portal.js +439 -0
  113. package/apps/runtime/dist/services/client-spaces.js +228 -0
  114. package/apps/runtime/dist/services/cloud.js +2 -0
  115. package/apps/runtime/dist/services/knowledge-model.js +24 -8
  116. package/apps/runtime/dist/services/knowledge.js +72 -42
  117. package/apps/runtime/dist/services/links.js +17 -0
  118. package/apps/runtime/dist/services/members-studio.js +5 -1
  119. package/apps/runtime/dist/services/page-pictures.js +162 -0
  120. package/apps/runtime/dist/services/runs.js +22 -6
  121. package/apps/runtime/dist/services/space-categories.js +41 -5
  122. package/apps/runtime/dist/services/space-data.js +165 -8
  123. package/apps/runtime/dist/services/space-files.js +102 -19
  124. package/apps/runtime/dist/services/space-index.js +384 -41
  125. package/apps/runtime/dist/services/space-origin.js +25 -1
  126. package/apps/runtime/dist/services/space-sources.js +937 -0
  127. package/apps/runtime/dist/services/spaces.js +60 -12
  128. package/apps/runtime/dist/services/synced-spaces.js +10 -1
  129. package/apps/runtime/dist/services/work-ai.js +53 -24
  130. package/apps/runtime/dist/services/work-chats.js +3 -2
  131. package/apps/runtime/dist/services/work-connections.js +2 -2
  132. package/apps/runtime/dist/sharing/index.js +2 -0
  133. package/apps/runtime/dist/store/index.js +29 -8
  134. package/apps/runtime/dist/store/shared-lists.js +30 -17
  135. package/apps/runtime/dist/sync/records.js +22 -27
  136. package/apps/runtime/dist/texts.js +4 -0
  137. package/apps/runtime/dist/tools/index.js +5 -4
  138. package/apps/runtime/dist/tools/pages.js +1 -1
  139. package/apps/runtime/dist/tools/skills.js +80 -54
  140. package/apps/runtime/dist/tools/space.js +8 -152
  141. package/apps/runtime/dist/widgets/step.js +2 -1
  142. package/apps/web/dist/assets/AboutDialog-0q4P80yv.js +1 -0
  143. package/apps/web/dist/assets/BlockEditor-CUYFH_CU.js +173 -0
  144. package/apps/web/dist/assets/BlockEditor-CtW6lw0y.css +1 -0
  145. package/apps/web/dist/assets/CapabilitiesPage-BlZP8DYb.js +2 -0
  146. package/apps/web/dist/assets/{ChangelogDialog-D_dQOMIq.js → ChangelogDialog-CDN7kMs1.js} +1 -1
  147. package/apps/web/dist/assets/{CodeEditor-nxoOt8ge.js → CodeEditor-D-Y16fdk.js} +1 -1
  148. package/apps/web/dist/assets/EditorPage-CrMVsgUt.js +3 -0
  149. package/apps/web/dist/assets/{FlowPreview-aMfwcPul.js → FlowPreview-DXnk7m9P.js} +1 -1
  150. package/apps/web/dist/assets/{LangSwitch-DpD0inR4.js → LangSwitch-Dkd0FBIH.js} +1 -1
  151. package/apps/web/dist/assets/Marketplace-Cty40VwG.js +2 -0
  152. package/apps/web/dist/assets/NewWizardChannels-Czool7s1.js +1 -0
  153. package/apps/web/dist/assets/NewWizardPage-CfVe_4a2.js +7 -0
  154. package/apps/web/dist/assets/OnboardingPage-D3SEpTmt.js +1 -0
  155. package/apps/web/dist/assets/PreviewPane-CQDKQO6z.js +7 -0
  156. package/apps/web/dist/assets/PublicRunner-BDZNnlQw.js +1 -0
  157. package/apps/web/dist/assets/ReadOnly-B7MUySpC.js +1 -0
  158. package/apps/web/dist/assets/SettingsPage-Bgurv3l2.js +2 -0
  159. package/apps/web/dist/assets/SetupPage-C3km8vqv.js +1 -0
  160. package/apps/web/dist/assets/{SharePage-N80EhoD7.js → SharePage-UwEJBIzq.js} +1 -1
  161. package/apps/web/dist/assets/SkillEditor-BaEiqqXl.js +1 -0
  162. package/apps/web/dist/assets/Skills-Bv6XbKxc.js +9 -0
  163. package/apps/web/dist/assets/SpacePage-D9PXLSU_.js +2 -0
  164. package/apps/web/dist/assets/UpdateCheck-C7I43zFZ.js +2 -0
  165. package/apps/web/dist/assets/changelog-DwgWiSZb.js +1 -0
  166. package/apps/web/dist/assets/circle-check-DDLzlcXP.js +1 -0
  167. package/apps/web/dist/assets/code-DsnwzN1x.js +1 -0
  168. package/apps/web/dist/assets/code-editor-Ca5EAxmd.js +2 -0
  169. package/apps/web/dist/assets/code-xml-S4IFi5Jc.js +1 -0
  170. package/apps/web/dist/assets/connection-sign-in-hI3HXhq4.js +1 -0
  171. package/apps/web/dist/assets/{dist-H1UN89OA.js → dist-B3Ray2U5.js} +1 -1
  172. package/apps/web/dist/assets/dist-CQpLJoQZ.js +10 -0
  173. package/apps/web/dist/assets/{dist-GlRRx9Xl.js → dist-D1uGQ_tB.js} +1 -1
  174. package/apps/web/dist/assets/{dist-BdDd1FxP.js → dist-DFI-tsfx.js} +1 -1
  175. package/apps/web/dist/assets/eye-TJSuiHHO.js +1 -0
  176. package/apps/web/dist/assets/floating-ui.dom-pwdNGv4b.js +1 -0
  177. package/apps/web/dist/assets/host-D5hNHbhF.js +125 -0
  178. package/apps/web/dist/assets/i18n-BBzMbGoo.js +55 -0
  179. package/apps/web/dist/assets/index-CyBrvupM.js +10 -0
  180. package/apps/web/dist/assets/index-w-3J3ZJ9.css +1 -0
  181. package/apps/web/dist/assets/{json-CEsfHdlZ.js → json-Ce3iHrtf.js} +1 -1
  182. package/apps/web/dist/assets/link-CPL8pB1P.js +1 -0
  183. package/apps/web/dist/assets/list-checks-DaxmUPqK.js +1 -0
  184. package/apps/web/dist/assets/{markdown-5-I9_izp.js → markdown-hqKQy2pp.js} +1 -1
  185. package/apps/web/dist/assets/marked.esm-DQ7XA7AC.js +67 -0
  186. package/apps/web/dist/assets/oss-credits-BrwV0a4F.js +1 -0
  187. package/apps/web/dist/assets/pdf-Dg-raaUo.js +54 -0
  188. package/apps/web/dist/assets/pdf.worker.min-68CMzziE.js +1 -0
  189. package/apps/web/dist/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
  190. package/apps/web/dist/assets/phone-B4uKDBFq.js +1 -0
  191. package/apps/web/dist/assets/read-aloud-Bfp9_bUY.js +1 -0
  192. package/apps/web/dist/assets/{runners-CyIvJIHr.js → runners-CUpBpSji.js} +1 -1
  193. package/apps/web/dist/assets/scroll-text-BEnPzQEi.js +1 -0
  194. package/apps/web/dist/assets/session-CMQo8P-G.js +2 -0
  195. package/apps/web/dist/assets/table-5B9G3oA6.js +1 -0
  196. package/apps/web/dist/assets/theme-CdX0marK.js +11 -0
  197. package/apps/web/dist/assets/ui-BCfu5sia.js +16 -0
  198. package/apps/web/dist/assets/upload-CAlHC232.js +1 -0
  199. package/apps/web/dist/assets/{useMutation-DEkZhp5c.js → useMutation-1fV4Nnxq.js} +1 -1
  200. package/apps/web/dist/assets/viewers-ChriFcRy.js +2 -0
  201. package/apps/web/dist/assets/volume-2-BqlBtCIJ.js +1 -0
  202. package/apps/web/dist/assets/w3c-keyname-BOAvb0qz.js +1 -0
  203. package/apps/web/dist/assets/xlsx-CuSbHU4R.js +141 -0
  204. package/apps/web/dist/index.html +19 -14
  205. package/apps/web/dist/mcp/flow.html +2 -2
  206. package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +223 -4
  207. package/node_modules/@engenty/wizards/dist/plugin/mount.d.ts +123 -0
  208. package/node_modules/@engenty/wizards/dist/plugin/mount.js +7 -0
  209. package/node_modules/@engenty/wizards/dist/plugin/studio.d.ts +110 -1
  210. package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +1 -1
  211. package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +24 -0
  212. package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +24 -0
  213. package/node_modules/@engenty/wizards-shared/dist/knowledge.d.ts +33 -4
  214. package/node_modules/@engenty/wizards-shared/dist/run.d.ts +6 -0
  215. package/node_modules/@engenty/wizards-shared/dist/run.js +12 -0
  216. package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +21 -4
  217. package/node_modules/@engenty/wizards-shared/dist/rwx.js +30 -3
  218. package/node_modules/@engenty/wizards-shared/dist/spaces.d.ts +11 -1
  219. package/node_modules/@engenty/wizards-shared/dist/spaces.js +9 -2
  220. package/node_modules/@engenty/wizards-shared/dist/text-merge.d.ts +6 -0
  221. package/node_modules/@engenty/wizards-shared/dist/text-merge.js +25 -0
  222. package/package.json +3 -1
  223. package/plugin/.claude-plugin/plugin.json +1 -1
  224. package/plugin/README.md +3 -0
  225. package/plugin/plugin.json +1 -1
  226. package/plugin/skills/work-in-space/SKILL.md +16 -0
  227. package/apps/web/dist/assets/AboutDialog-Cag3RJuT.js +0 -1
  228. package/apps/web/dist/assets/CapabilitiesPage-BQYiN0gT.js +0 -2
  229. package/apps/web/dist/assets/EditorPage-C4_8LsQN.js +0 -3
  230. package/apps/web/dist/assets/Marketplace-CpzD9pNQ.js +0 -2
  231. package/apps/web/dist/assets/NewWizardChannels-CcE3UMVb.js +0 -1
  232. package/apps/web/dist/assets/NewWizardPage-BzN7zrQx.js +0 -7
  233. package/apps/web/dist/assets/OnboardingPage-ztHT01ah.js +0 -1
  234. package/apps/web/dist/assets/PreviewPane-BabMbBZI.js +0 -9
  235. package/apps/web/dist/assets/PublicRunner-D-JmtqCi.js +0 -1
  236. package/apps/web/dist/assets/ReadOnly-GAORKKqV.js +0 -1
  237. package/apps/web/dist/assets/SettingsPage-BKAiYmXl.js +0 -2
  238. package/apps/web/dist/assets/SetupPage-C1EZZHIp.js +0 -1
  239. package/apps/web/dist/assets/SkillEditor-CqQMg2MC.js +0 -1
  240. package/apps/web/dist/assets/Skills-CnhpgoOA.js +0 -9
  241. package/apps/web/dist/assets/SpacePage-CRR8JvV1.js +0 -2
  242. package/apps/web/dist/assets/UpdateCheck-CfXZBQNv.js +0 -2
  243. package/apps/web/dist/assets/changelog-6MqC8Vy8.js +0 -1
  244. package/apps/web/dist/assets/circle-alert-C8Ma-bPY.js +0 -1
  245. package/apps/web/dist/assets/circle-check-Bu9Z3mlJ.js +0 -1
  246. package/apps/web/dist/assets/code-editor-CfTGqOpp.js +0 -2
  247. package/apps/web/dist/assets/code-xml-B4kLFZSj.js +0 -1
  248. package/apps/web/dist/assets/connection-sign-in-B2D0O8W-.js +0 -1
  249. package/apps/web/dist/assets/dist-EBVTpvRr.js +0 -10
  250. package/apps/web/dist/assets/eye-BpoKpIdt.js +0 -1
  251. package/apps/web/dist/assets/host-BlKXX7YK.js +0 -118
  252. package/apps/web/dist/assets/i18n-BJ7Ld6O6.js +0 -55
  253. package/apps/web/dist/assets/index-66tE1oti.js +0 -10
  254. package/apps/web/dist/assets/index-D18DvnxX.css +0 -1
  255. package/apps/web/dist/assets/oss-credits-IdX5bc71.js +0 -1
  256. package/apps/web/dist/assets/phone-Ctvf-Am-.js +0 -1
  257. package/apps/web/dist/assets/read-aloud-Da_RSm2S.js +0 -1
  258. package/apps/web/dist/assets/scroll-text-ChrjRu9Y.js +0 -1
  259. package/apps/web/dist/assets/session-SXPtTWRK.js +0 -2
  260. package/apps/web/dist/assets/theme-Br1S0j5h.js +0 -11
  261. package/apps/web/dist/assets/ui-Uqnx1UpK.js +0 -82
  262. package/apps/web/dist/assets/upload-BchUnQGV.js +0 -1
  263. package/apps/web/dist/assets/volume-2-CU_oy81Z.js +0 -1
@@ -2,7 +2,7 @@ import { TableColumnValueError, tableColumnsSchema, } from "@engenty/wizards-sha
2
2
  import { note } from "@engenty/wizards-shared/run";
3
3
  import { editCells, SPACE_DATA_LIMITS, } from "@engenty/wizards-shared/space-data";
4
4
  import { rowKey } from "@engenty/wizards-shared/store";
5
- import { and, asc, count, desc, eq, inArray, isNotNull, isNull, max } from "drizzle-orm";
5
+ import { and, asc, count, desc, eq, inArray, isNotNull, isNull, max, ne } from "drizzle-orm";
6
6
  import { nanoid } from "nanoid";
7
7
  import { assertRwx, pageResources, readable, tableResources } from "../broker/rwx.js";
8
8
  import { db, schema } from "../db/client.js";
@@ -139,12 +139,15 @@ export async function createTable(userId, spaceId, input, lang = "en") {
139
139
  : [{ id: "name", name: "Name", type: "text" }];
140
140
  const id = nanoid(12);
141
141
  const wizardId = await ownerOf(space.id, input.wizardId);
142
+ const parentId = wizardId ? null : await parentOf(space.id, input.parentId);
142
143
  const title = input.title.trim() || serverText(lang, "tableTitle");
143
144
  await db.insert(schema.spaceTable).values({
144
145
  id,
145
146
  tenantId: currentTenant(),
146
147
  spaceId: space.id,
147
148
  wizardId,
149
+ parentId,
150
+ position: wizardId ? 0 : await nextPosition(space.id, parentId),
148
151
  title,
149
152
  slug: wizardId ? null : await freeSlug(space.id, title, "table"),
150
153
  format: input.format ?? null,
@@ -405,12 +408,26 @@ async function parentOf(spaceId, parentId, pageId) {
405
408
  }
406
409
  return parent.id;
407
410
  }
411
+ /**
412
+ * The place after the last of a folder's contents: its sub-pages, tables and documents share one
413
+ * order. `parentId` null: the top of Knowledge.
414
+ */
408
415
  async function nextPosition(spaceId, parentId) {
409
- const [{ last }] = await db
410
- .select({ last: max(schema.spacePage.position) })
411
- .from(schema.spacePage)
412
- .where(and(eq(schema.spacePage.spaceId, spaceId), isNull(schema.spacePage.wizardId), parentId ? eq(schema.spacePage.parentId, parentId) : isNull(schema.spacePage.parentId)));
413
- return (last ?? -1) + 1;
416
+ const lasts = await Promise.all([
417
+ db
418
+ .select({ last: max(schema.spacePage.position) })
419
+ .from(schema.spacePage)
420
+ .where(and(eq(schema.spacePage.spaceId, spaceId), isNull(schema.spacePage.wizardId), parentId ? eq(schema.spacePage.parentId, parentId) : isNull(schema.spacePage.parentId))),
421
+ db
422
+ .select({ last: max(schema.spaceTable.position) })
423
+ .from(schema.spaceTable)
424
+ .where(and(eq(schema.spaceTable.spaceId, spaceId), isNull(schema.spaceTable.wizardId), parentId ? eq(schema.spaceTable.parentId, parentId) : isNull(schema.spaceTable.parentId))),
425
+ db
426
+ .select({ last: max(schema.spaceFile.position) })
427
+ .from(schema.spaceFile)
428
+ .where(and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.kind, "document"), parentId ? eq(schema.spaceFile.parentId, parentId) : isNull(schema.spaceFile.parentId))),
429
+ ]);
430
+ return Math.max(-1, ...lasts.map(([{ last }]) => last ?? -1)) + 1;
414
431
  }
415
432
  export async function createPage(userId, spaceId, input) {
416
433
  const space = await ownedSpace(userId, spaceId);
@@ -475,7 +492,10 @@ export async function updatePage(_userId, pageId, patch) {
475
492
  ? await parentOf(page.spaceId, patch.parentId, page.id)
476
493
  : page.parentId;
477
494
  const moved = title !== page.title || wizardId !== page.wizardId || parentId !== page.parentId;
478
- const edited = moved || (patch.markdown !== undefined && patch.markdown !== page.markdown);
495
+ const texted = patch.markdown !== undefined && patch.markdown !== page.markdown;
496
+ // A source's own folder renamed or moved stays the source's: its name is the source's name.
497
+ const sourceFolder = Boolean(page.origin?.startsWith("source:")) && page.originKey === "root";
498
+ const edited = (moved && !sourceFolder) || texted;
479
499
  const at = new Date();
480
500
  await db
481
501
  .update(schema.spacePage)
@@ -497,6 +517,15 @@ export async function updatePage(_userId, pageId, patch) {
497
517
  updatedAt: at,
498
518
  })
499
519
  .where(eq(schema.spacePage.id, page.id));
520
+ if (sourceFolder && title !== page.title && page.origin) {
521
+ await db
522
+ .update(schema.spaceSource)
523
+ .set({ label: title, updatedAt: at })
524
+ .where(eq(schema.spaceSource.id, page.origin.slice("source:".length)));
525
+ for (const table of [schema.spacePage, schema.spaceTable]) {
526
+ await db.update(table).set({ originLabel: title }).where(eq(table.origin, page.origin));
527
+ }
528
+ }
500
529
  if (moved) {
501
530
  queueIndex(`P:${page.id}`);
502
531
  }
@@ -511,6 +540,133 @@ export async function deletePage(_userId, pageId) {
511
540
  await db.delete(schema.spacePage).where(eq(schema.spacePage.id, page.id));
512
541
  await dropOriginal(page);
513
542
  }
543
+ /**
544
+ * Moves an item of Knowledge (`p:`, `t:` or `f:` and its id) into a folder — a page of the
545
+ * space's own, or the top for null — at `index` among the folder's contents, at the end when left
546
+ * out. A page's path follows it; a table's and a file's do not.
547
+ */
548
+ export async function moveKnowledgeItem(userId, spaceId, input) {
549
+ const space = await ownedSpace(userId, spaceId);
550
+ await requireWritableSpace(space.id);
551
+ await placeItem(space.id, input.key, input.parentId);
552
+ if (input.index !== undefined) {
553
+ await orderFolder(space.id, input.parentId ?? null, input.key, input.index);
554
+ }
555
+ }
556
+ /**
557
+ * Puts an item of Knowledge into a folder of the space, at the end of its contents, unless it is
558
+ * there. Moved, not edited: what a plugin or a source wrote stays theirs.
559
+ */
560
+ export async function placeItem(spaceId, key, parentId) {
561
+ const kind = key[0];
562
+ const id = key.slice(2);
563
+ const own = (row) => {
564
+ if (!row || row.spaceId !== spaceId) {
565
+ throw notFound();
566
+ }
567
+ return row;
568
+ };
569
+ if (kind === "p") {
570
+ const page = own(await pageRow(id));
571
+ if (page.wizardId) {
572
+ throw notFound();
573
+ }
574
+ const parent = await parentOf(spaceId, parentId, page.id);
575
+ if (parent !== page.parentId) {
576
+ await db
577
+ .update(schema.spacePage)
578
+ .set({
579
+ parentId: parent,
580
+ slug: await freeSlug(spaceId, page.title, { parentId: parent }, page.id),
581
+ position: await nextPosition(spaceId, parent),
582
+ updatedAt: new Date(),
583
+ })
584
+ .where(eq(schema.spacePage.id, page.id));
585
+ queueIndex(`P:${page.id}`);
586
+ }
587
+ }
588
+ else if (kind === "t") {
589
+ const table = own(await tableRow(id));
590
+ if (table.wizardId) {
591
+ throw notFound();
592
+ }
593
+ const parent = await parentOf(spaceId, parentId);
594
+ if (parent !== table.parentId) {
595
+ await db
596
+ .update(schema.spaceTable)
597
+ .set({ parentId: parent, position: await nextPosition(spaceId, parent) })
598
+ .where(eq(schema.spaceTable.id, table.id));
599
+ }
600
+ }
601
+ else if (kind === "f") {
602
+ const file = own(await db.query.spaceFile.findFirst({
603
+ where: and(eq(schema.spaceFile.id, id), eq(schema.spaceFile.kind, "document")),
604
+ }));
605
+ const parent = await parentOf(spaceId, parentId);
606
+ if (parent !== file.parentId) {
607
+ const position = await nextPosition(spaceId, parent);
608
+ await db
609
+ .update(schema.spaceFile)
610
+ .set({ parentId: parent, position })
611
+ .where(eq(schema.spaceFile.id, file.id));
612
+ // What it was read into goes with it: its first page (its parts stay under that) and its
613
+ // tables.
614
+ const was = file.parentId
615
+ ? eq(schema.spacePage.parentId, file.parentId)
616
+ : isNull(schema.spacePage.parentId);
617
+ for (const page of await db.query.spacePage.findMany({
618
+ where: and(eq(schema.spacePage.fileId, file.id), was),
619
+ columns: { id: true },
620
+ })) {
621
+ await placeItem(spaceId, `p:${page.id}`, parent);
622
+ }
623
+ await db
624
+ .update(schema.spaceTable)
625
+ .set({ parentId: parent, position })
626
+ .where(eq(schema.spaceTable.fileId, file.id));
627
+ }
628
+ }
629
+ else {
630
+ throw notFound();
631
+ }
632
+ }
633
+ /** Puts `key` at `index` among a folder's contents and numbers them all again. */
634
+ async function orderFolder(spaceId, parentId, key, index) {
635
+ const [pages, tables, files] = await Promise.all([
636
+ db.query.spacePage.findMany({
637
+ where: and(eq(schema.spacePage.spaceId, spaceId), isNull(schema.spacePage.wizardId), parentId ? eq(schema.spacePage.parentId, parentId) : isNull(schema.spacePage.parentId)),
638
+ columns: { id: true, position: true, title: true },
639
+ }),
640
+ db.query.spaceTable.findMany({
641
+ where: and(eq(schema.spaceTable.spaceId, spaceId), isNull(schema.spaceTable.wizardId), parentId ? eq(schema.spaceTable.parentId, parentId) : isNull(schema.spaceTable.parentId)),
642
+ columns: { id: true, position: true, title: true },
643
+ }),
644
+ db.query.spaceFile.findMany({
645
+ where: and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.kind, "document"), parentId ? eq(schema.spaceFile.parentId, parentId) : isNull(schema.spaceFile.parentId)),
646
+ columns: { id: true, position: true, name: true },
647
+ }),
648
+ ]);
649
+ const all = [
650
+ ...pages.map((p) => ({ key: `p:${p.id}`, position: p.position, title: p.title })),
651
+ ...tables.map((t) => ({ key: `t:${t.id}`, position: t.position, title: t.title })),
652
+ ...files.map((f) => ({ key: `f:${f.id}`, position: f.position, title: f.name })),
653
+ ].sort((a, b) => a.position - b.position || a.title.localeCompare(b.title, "de"));
654
+ const moving = all.find((i) => i.key === key);
655
+ if (!moving) {
656
+ return;
657
+ }
658
+ const rest = all.filter((i) => i !== moving);
659
+ rest.splice(Math.max(0, Math.min(index, rest.length)), 0, moving);
660
+ for (const [position, item] of rest.entries()) {
661
+ const id = item.key.slice(2);
662
+ const table = item.key[0] === "p"
663
+ ? schema.spacePage
664
+ : item.key[0] === "t"
665
+ ? schema.spaceTable
666
+ : schema.spaceFile;
667
+ await db.update(table).set({ position }).where(eq(table.id, id));
668
+ }
669
+ }
514
670
  /** Puts a page's sub-pages into the order of `ids`. */
515
671
  export async function orderSubPages(_userId, pageId, ids) {
516
672
  const page = await pageRow(pageId);
@@ -775,8 +931,9 @@ export async function listOrigin(origin, space) {
775
931
  updatedAt: true,
776
932
  },
777
933
  }),
934
+ // A page's pictures go with its pages, not as items of their own.
778
935
  db.query.spaceFile.findMany({
779
- where: and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.origin, origin)),
936
+ where: and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.origin, origin), ne(schema.spaceFile.kind, "picture")),
780
937
  columns: { id: true, originKey: true, name: true, updatedAt: true },
781
938
  }),
782
939
  ]);
@@ -2,10 +2,12 @@ import { note } from "@engenty/wizards-shared/run";
2
2
  import { SPACE_LIMITS, spaceFileLimit, } from "@engenty/wizards-shared/spaces";
3
3
  import { generateText } from "ai";
4
4
  import { and, asc, count, eq, max } from "drizzle-orm";
5
+ import { nanoid } from "nanoid";
5
6
  import { assertRwx } from "../broker/rwx.js";
6
7
  import { db, schema } from "../db/client.js";
7
8
  import { documentMime, UnreadableDocument } from "../documents/parse.js";
8
- import { loadAsset, removeAsset, replaceAsset, saveAsset } from "../files/storage.js";
9
+ import { blobThumb, getBlob, putBlob, releaseBlob } from "../files/blobs.js";
10
+ import { hasThumb } from "../files/thumbs.js";
9
11
  import { checkUpload } from "../files/upload-check.js";
10
12
  import { embeddingModel, textModel } from "../models.js";
11
13
  import { spaceFileChanged } from "../plugins/events.js";
@@ -35,6 +37,7 @@ export function fileView(row) {
35
37
  indexed: row.indexed,
36
38
  pages: row.pages,
37
39
  chars: row.chars,
40
+ thumb: Boolean(row.hash) && hasThumb(row.mime),
38
41
  createdAt: row.createdAt.toISOString(),
39
42
  };
40
43
  }
@@ -55,6 +58,10 @@ export async function spaceFile(spaceId, fileId) {
55
58
  }
56
59
  return row;
57
60
  }
61
+ /** A file's bytes, from the blob store; null when they are not there. */
62
+ export async function spaceFileBytes(row) {
63
+ return row.hash ? getBlob(row.hash).catch(() => null) : null;
64
+ }
58
65
  /**
59
66
  * A file's bytes. A document's go out only to a `who` the broker lets read it (broker/rwx.ts),
60
67
  * and to nobody when none is given; a logo, pattern or asset is the brand's, not Knowledge, and
@@ -65,11 +72,19 @@ export async function spaceFileContent(spaceId, fileId, who = []) {
65
72
  if (row.kind === "document") {
66
73
  await assertRwx(spaceId, who, { kind: "document", id: row.id }, "r");
67
74
  }
68
- const found = await loadAsset(row.id);
69
- if (!found) {
75
+ const data = await spaceFileBytes(row);
76
+ if (!data) {
70
77
  throw notFound();
71
78
  }
72
- return { row, data: found.data };
79
+ return { row, data };
80
+ }
81
+ /** A file's thumbnail (files/thumbs.ts), to the same people as its bytes; null when it has none. */
82
+ export async function spaceFileThumb(spaceId, fileId, who = []) {
83
+ const row = await spaceFile(spaceId, fileId);
84
+ if (row.kind === "document") {
85
+ await assertRwx(spaceId, who, { kind: "document", id: row.id }, "r");
86
+ }
87
+ return row.hash ? blobThumb(row.hash, row.mime) : null;
73
88
  }
74
89
  /**
75
90
  * Assets are what wizards put into what they make: pictures, clips, recordings, and files to
@@ -77,6 +92,10 @@ export async function spaceFileContent(spaceId, fileId, who = []) {
77
92
  */
78
93
  const ASSET_TYPES = /^((image|video|audio|font)\/|application\/(pdf|json)$|model\/gltf)/;
79
94
  function accepts(kind, mime) {
95
+ // A page's pictures come only with their page (page-pictures.ts).
96
+ if (kind === "picture") {
97
+ return false;
98
+ }
80
99
  if (kind === "logo" || kind === "pattern") {
81
100
  return mime.startsWith("image/");
82
101
  }
@@ -115,23 +134,19 @@ export async function addSpaceFile(spaceId, input) {
115
134
  if (n >= SPACE_LIMITS.files[input.kind]) {
116
135
  throw new ServiceError("refused", note("roomMostFiles", { count: SPACE_LIMITS.files[input.kind] }));
117
136
  }
118
- const ref = await saveAsset({
119
- kind: input.kind === "logo" ? "logo" : "space",
120
- mime,
121
- name,
122
- data,
123
- });
137
+ const hash = await putBlob(data);
124
138
  const description = input.description?.trim().slice(0, 600) ?? "";
125
139
  const work = input.kind === "document" || (!description && SEEN_BY_MODEL.includes(mime));
126
140
  const [row] = await db
127
141
  .insert(schema.spaceFile)
128
142
  .values({
129
- id: ref.id,
143
+ id: nanoid(16),
130
144
  spaceId,
131
145
  kind: input.kind,
132
146
  name,
133
147
  mime,
134
148
  size: data.byteLength,
149
+ hash,
135
150
  description,
136
151
  source: input.source ?? null,
137
152
  position: (last ?? -1) + 1,
@@ -140,8 +155,10 @@ export async function addSpaceFile(spaceId, input) {
140
155
  .returning();
141
156
  if (input.kind === "logo") {
142
157
  // Logos are shown on public pages, where only the control database knows the tenant.
143
- await putLink(ref.id, "logo", ref.id);
158
+ await putLink(row.id, "logo", row.id);
144
159
  }
160
+ // Made now, so lists show it at once.
161
+ void blobThumb(hash, mime).catch(() => null);
145
162
  if (work) {
146
163
  void prepare(row);
147
164
  }
@@ -241,6 +258,25 @@ async function describeImage(row, data) {
241
258
  });
242
259
  return result.text.trim().slice(0, 600);
243
260
  }
261
+ /**
262
+ * A picture of a page (kind `picture`) where the embedding model doesn't see pictures: a sentence
263
+ * says what it shows, after the words it came with, and the index finds it by them.
264
+ */
265
+ export function describePicture(row) {
266
+ void slot(async () => {
267
+ const data = await spaceFileBytes(row);
268
+ const said = data ? await describeImage(row, new Uint8Array(data)).catch(() => "") : "";
269
+ await db
270
+ .update(schema.spaceFile)
271
+ .set({
272
+ status: "ready",
273
+ description: [row.description, said].filter(Boolean).join("\n").slice(0, 600),
274
+ updatedAt: new Date(),
275
+ })
276
+ .where(eq(schema.spaceFile.id, row.id));
277
+ queueIndex(`f:${row.id}`);
278
+ });
279
+ }
244
280
  async function describeDocument(row, text) {
245
281
  const reader = await textModel("classifier");
246
282
  const result = await generateText({
@@ -282,11 +318,11 @@ async function prepare(row) {
282
318
  .set({ ...values, updatedAt: new Date() })
283
319
  .where(eq(schema.spaceFile.id, row.id));
284
320
  try {
285
- const found = await loadAsset(row.id);
321
+ const found = await spaceFileBytes(row);
286
322
  if (!found) {
287
323
  return;
288
324
  }
289
- const data = new Uint8Array(found.data);
325
+ const data = new Uint8Array(found);
290
326
  if (row.kind !== "document") {
291
327
  const description = await describeImage(row, data).catch(() => "");
292
328
  await set({ status: "ready", ...(description ? { description } : {}) });
@@ -352,6 +388,50 @@ export async function updateSpaceFile(spaceId, fileId, patch) {
352
388
  }
353
389
  return updated;
354
390
  }
391
+ /**
392
+ * A page's picture made a brand asset („In Marke übernehmen“): the same bytes under a row of the
393
+ * kind `asset`, at the end of the assets. One the brand has already is given back as it is.
394
+ */
395
+ export async function pictureToBrand(spaceId, fileId) {
396
+ await requireWritableSpace(spaceId);
397
+ const row = await spaceFile(spaceId, fileId);
398
+ if (row.kind !== "picture" || !row.hash) {
399
+ throw new ServiceError("invalid", note("filesGoHere"));
400
+ }
401
+ const scope = and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.kind, "asset"));
402
+ const had = await db.query.spaceFile.findFirst({
403
+ where: and(scope, eq(schema.spaceFile.hash, row.hash)),
404
+ });
405
+ if (had) {
406
+ return had;
407
+ }
408
+ const [{ n, last }] = await db
409
+ .select({ n: count(), last: max(schema.spaceFile.position) })
410
+ .from(schema.spaceFile)
411
+ .where(scope);
412
+ if (n >= SPACE_LIMITS.files.asset) {
413
+ throw new ServiceError("refused", note("roomMostFiles", { count: SPACE_LIMITS.files.asset }));
414
+ }
415
+ const [asset] = await db
416
+ .insert(schema.spaceFile)
417
+ .values({
418
+ id: nanoid(16),
419
+ spaceId,
420
+ kind: "asset",
421
+ name: row.name,
422
+ mime: row.mime,
423
+ size: row.size,
424
+ hash: row.hash,
425
+ description: row.description,
426
+ source: row.source,
427
+ position: (last ?? -1) + 1,
428
+ status: "ready",
429
+ })
430
+ .returning();
431
+ queueIndex(`f:${asset.id}`);
432
+ spaceFileChanged("space.file.ready", asset);
433
+ return asset;
434
+ }
355
435
  /** Puts the files of one kind into the order of `ids`; the first logo is the one end users see. */
356
436
  export async function orderSpaceFiles(spaceId, kind, ids) {
357
437
  await requireWritableSpace(spaceId);
@@ -372,7 +452,8 @@ async function removeRow(row) {
372
452
  const { dropConverted } = await import("./knowledge-model.js");
373
453
  await dropConverted(row.id);
374
454
  await db.delete(schema.spaceFile).where(eq(schema.spaceFile.id, row.id));
375
- await removeAsset(row.id);
455
+ await releaseBlob(row.hash);
456
+ await releaseBlob(row.textHash);
376
457
  if (row.kind === "logo" || row.mime.startsWith("font/")) {
377
458
  await dropLinks([row.id]);
378
459
  }
@@ -409,6 +490,7 @@ export async function putOriginFile(origin, input) {
409
490
  name,
410
491
  mime,
411
492
  size: input.data.byteLength,
493
+ hash: await putBlob(input.data),
412
494
  description: input.description?.trim().slice(0, 600) ?? existing?.description ?? "",
413
495
  originLabel: input.label?.trim().slice(0, 120) || null,
414
496
  status: "ready",
@@ -420,8 +502,9 @@ export async function putOriginFile(origin, input) {
420
502
  let id = existing?.id;
421
503
  if (existing) {
422
504
  // New content under the id it has: pages that name it as their original keep it.
423
- await replaceAsset(existing.id, { mime, name, data: input.data });
424
505
  await db.update(schema.spaceFile).set(values).where(eq(schema.spaceFile.id, existing.id));
506
+ await releaseBlob(existing.hash);
507
+ await releaseBlob(existing.textHash);
425
508
  }
426
509
  else {
427
510
  const [{ n, last }] = await db
@@ -431,10 +514,9 @@ export async function putOriginFile(origin, input) {
431
514
  if (n >= SPACE_LIMITS.files.document) {
432
515
  throw new ServiceError("refused", note("spaceMostDocuments", { count: SPACE_LIMITS.files.document }));
433
516
  }
434
- const ref = await saveAsset({ kind: "space", mime, name, data: input.data });
435
- id = ref.id;
517
+ id = nanoid(16);
436
518
  await db.insert(schema.spaceFile).values({
437
- id: ref.id,
519
+ id,
438
520
  spaceId: input.space,
439
521
  kind: "document",
440
522
  position: (last ?? -1) + 1,
@@ -443,6 +525,7 @@ export async function putOriginFile(origin, input) {
443
525
  ...values,
444
526
  });
445
527
  }
528
+ void blobThumb(values.hash, mime).catch(() => null);
446
529
  if (input.categories) {
447
530
  await setItemCategories(input.space, { fileId: id }, input.categories, "origin");
448
531
  }