@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
@@ -0,0 +1,103 @@
1
+ import { createTool } from "@mastra/core/tools";
2
+ import { z } from "zod";
3
+ import { currentFeatures } from "../features.js";
4
+ import { sandboxCapabilities, shellToolDescription } from "../sandbox/index.js";
5
+ import { activeSkills } from "../services/skills.js";
6
+ import { defaultSpace, ownedSpace } from "../services/spaces.js";
7
+ import { chatSandbox } from "../services/work-sandbox.js";
8
+ import { clip } from "../tools/shared.js";
9
+ import { personCaller } from "./caller.js";
10
+ import { elementTools } from "./catalogue.js";
11
+ import { elementCall } from "./gate.js";
12
+ import { ELEMENT_SPECS } from "./specs.js";
13
+ /** `run_command` in the outside client's sandbox: only for clients that may run elements. */
14
+ export const RUN_COMMAND = {
15
+ scope: "elements:run",
16
+ input: z.object({ command: z.string(), timeoutSeconds: z.number().int().optional() }),
17
+ };
18
+ /** The space a call names, checked against the token; the person's first one when none. */
19
+ export async function outsideSpace(who, spaceId) {
20
+ return spaceId ? ownedSpace(who.userId, spaceId) : defaultSpace(who.userId);
21
+ }
22
+ /** The sandbox an outside client's skills run in: one per person, space and client. */
23
+ function outsideSandbox(who, spaceId) {
24
+ const caps = sandboxCapabilities();
25
+ if (!caps) {
26
+ return null;
27
+ }
28
+ const key = `outside-${who.tenantId}-${who.userId}-${spaceId}-${who.client}`.replace(/[^A-Za-z0-9_-]/g, "_");
29
+ return { handle: () => chatSandbox(key), workdir: caps.workdir };
30
+ }
31
+ /** The catalogue's tools for an outside caller in a space, by the token's scopes. */
32
+ export async function outsideTools(who, spaceId, signal) {
33
+ const space = await outsideSpace(who, spaceId);
34
+ const sandbox = who.scopes.includes("elements:run") ? outsideSandbox(who, space.id) : null;
35
+ const caller = personCaller({
36
+ space: { id: space.id, name: space.name },
37
+ userId: who.userId,
38
+ role: who.role,
39
+ signal,
40
+ client: who.client,
41
+ sandbox,
42
+ });
43
+ const skills = (await currentFeatures()).skills === true ? await activeSkills(space.id) : null;
44
+ const tools = await elementTools(caller, {
45
+ knowledge: true,
46
+ skills,
47
+ wizards: true,
48
+ connections: true,
49
+ });
50
+ if (sandbox) {
51
+ const report = (result) => ({
52
+ exitCode: result.exitCode,
53
+ stdout: clip(result.stdout, 8000),
54
+ stderr: clip(result.stderr, 4000),
55
+ });
56
+ tools.run_command = createTool({
57
+ id: "run_command",
58
+ description: shellToolDescription(sandboxCapabilities()),
59
+ inputSchema: RUN_COMMAND.input,
60
+ execute: (input) => elementCall(caller, { action: "run_command", about: "sandbox", perm: "x", input }, async () => report(await sandbox.handle().exec(input.command, {
61
+ timeoutMs: Math.min((input.timeoutSeconds ?? 120) * 1000, 600_000),
62
+ }))),
63
+ });
64
+ }
65
+ // What the token's scopes allow, nothing more.
66
+ for (const name of Object.keys(tools)) {
67
+ const scope = name === "run_command" ? RUN_COMMAND.scope : ELEMENT_SPECS[name]?.scope;
68
+ if (!(scope && who.scopes.includes(scope))) {
69
+ delete tools[name];
70
+ }
71
+ }
72
+ return tools;
73
+ }
74
+ /** One element call of an outside client. */
75
+ export async function callOutside(who, spaceId, name, input, signal) {
76
+ const tools = await outsideTools(who, spaceId, signal);
77
+ const tool = tools[name];
78
+ if (!tool?.execute) {
79
+ return {
80
+ ok: false,
81
+ error: `${name} is not open to this client in this space: the token's scopes, or nothing of the kind in the space.`,
82
+ };
83
+ }
84
+ let data = input ?? {};
85
+ if (tool.inputSchema) {
86
+ const parsed = tool.inputSchema.safeParse(data);
87
+ if (!parsed.success) {
88
+ return { ok: false, error: "Invalid input", data: parsed.error.issues };
89
+ }
90
+ data = parsed.data;
91
+ }
92
+ try {
93
+ const result = await tool.execute(data);
94
+ if (result && typeof result === "object" && "error" in result) {
95
+ const { error, ...data } = result;
96
+ return { ok: false, error, data };
97
+ }
98
+ return { ok: true, result };
99
+ }
100
+ catch (err) {
101
+ return { ok: false, error: String(err?.message ?? err).slice(0, 400) };
102
+ }
103
+ }
@@ -0,0 +1,102 @@
1
+ import { whereSchema } from "@engenty/wizards-shared/knowledge";
2
+ import { z } from "zod";
3
+ import { prompt } from "../prompts.js";
4
+ const where = whereSchema.optional().describe(prompt("tools", "tool.space.where"));
5
+ export const pageReadInput = {
6
+ path: z.string().describe("The page's path, as space_search or space_list give it: pages/…"),
7
+ section: z
8
+ .string()
9
+ .optional()
10
+ .describe("Only this section: a heading of the page, or the start of one"),
11
+ from: z.number().int().min(0).optional().describe("Character to continue a long page at"),
12
+ };
13
+ export const ELEMENT_SPECS = {
14
+ space_search: {
15
+ scope: "data:read",
16
+ description: () => prompt("tools", "tool.space_search"),
17
+ input: z.object({
18
+ query: z.string().describe("What to look for: a question or keywords"),
19
+ where,
20
+ limit: z.number().int().min(1).max(12).optional().describe("Items to get, 6 by default"),
21
+ }),
22
+ },
23
+ space_list: {
24
+ scope: "data:read",
25
+ description: () => prompt("tools", "tool.space_list"),
26
+ input: z.object({ where }),
27
+ },
28
+ page_read: {
29
+ scope: "data:read",
30
+ description: () => prompt("tools", "tool.page_read.knowledge"),
31
+ input: z.object(pageReadInput),
32
+ },
33
+ table_read: {
34
+ scope: "data:read",
35
+ description: () => prompt("tools", "tool.table_read"),
36
+ input: z.object({
37
+ path: z.string().describe("The table's path: tables/…"),
38
+ where: whereSchema.optional().describe("Filter by columns, by their names"),
39
+ columns: z.array(z.string()).max(40).optional().describe("Only these columns"),
40
+ limit: z.number().int().min(1).max(1000).optional(),
41
+ offset: z.number().int().min(0).optional(),
42
+ }),
43
+ },
44
+ space_document: {
45
+ scope: "data:read",
46
+ description: () => prompt("tools", "tool.space_document"),
47
+ input: z.object({
48
+ path: z
49
+ .string()
50
+ .describe("The file's path: files/… or dateien/…, as the page's front matter or a hit names it"),
51
+ from: z.number().int().min(0).optional().describe("Character to start at"),
52
+ }),
53
+ },
54
+ skill_list: {
55
+ scope: "elements:read",
56
+ description: () => "Lists the space's skills you may read: their name and what each is for. Load one with skill_load.",
57
+ input: z.object({}),
58
+ },
59
+ skill_load: {
60
+ scope: "elements:read",
61
+ description: () => "Loads one of the space's skills by its name: its instructions and the files it brings. skill_list names them.",
62
+ input: z.object({ name: z.string().describe("The skill's name, as listed") }),
63
+ },
64
+ skill_read: {
65
+ scope: "elements:read",
66
+ description: () => prompt("tools", "tool.skill_read"),
67
+ input: z.object({
68
+ name: z.string().describe("The skill's name"),
69
+ path: z.string().describe("The file's path in the skill, e.g. references/forms.md"),
70
+ from: z.number().int().min(0).optional().describe("Character to continue a long file at"),
71
+ }),
72
+ },
73
+ connection_list: {
74
+ scope: "elements:read",
75
+ description: () => "Lists the actions of the space's connections (mail, calendar, CRM …) you may use: each action's name, what it does, whether it reads or writes, and its input. Call one with connection_call.",
76
+ input: z.object({}),
77
+ },
78
+ connection_call: {
79
+ scope: "elements:run",
80
+ description: () => "Calls one action of the space's connections, as connection_list names it, with its input. A read runs at once; a write, a send or a delete runs only where the space's rules allow it without a person's yes: otherwise it is refused and says why.",
81
+ input: z.object({
82
+ name: z.string().describe("The action's name, as connection_list gives it"),
83
+ input: z.record(z.string(), z.unknown()).default({}),
84
+ }),
85
+ },
86
+ wizard_list: {
87
+ scope: "elements:read",
88
+ description: () => "Lists the space's published wizards you may run: their id, title and what they do. Run one with wizard_run.",
89
+ input: z.object({}),
90
+ },
91
+ wizard_run: {
92
+ scope: "elements:run",
93
+ description: () => "Runs one of the space's published wizards as the person you work for, with answers for its first page. Returns the run: its result, or the page or confirmation it waits for.",
94
+ input: z.object({
95
+ wizardId: z.string().describe("The wizard's id, as wizard_list gives it"),
96
+ answers: z.record(z.string(), z.unknown()).default({}),
97
+ waitSeconds: z.number().int().min(0).max(45).default(20),
98
+ }),
99
+ },
100
+ };
101
+ /** The names the catalogue can give. */
102
+ export const ELEMENT_TOOL_NAMES = Object.keys(ELEMENT_SPECS);
@@ -0,0 +1,72 @@
1
+ import { createTool } from "@mastra/core/tools";
2
+ import { eq } from "drizzle-orm";
3
+ import { readableIds } from "../broker/rwx.js";
4
+ import { db, schema } from "../db/client.js";
5
+ import { runReport, startRun } from "../services/runs.js";
6
+ import { listWizards } from "../services/wizards.js";
7
+ import { attempt } from "../tools/shared.js";
8
+ import { elementCall } from "./gate.js";
9
+ import { ELEMENT_SPECS } from "./specs.js";
10
+ /**
11
+ * The space's published wizards as tools of the catalogue: list them (`r` on each) and run one
12
+ * (`x`) as the person calling, who answers its pages as anyone running it does. Only a signed-in
13
+ * person runs a wizard this way: a guest reaches a wizard by its link, Goose and a run's step do
14
+ * not start runs.
15
+ */
16
+ /** Whether the caller is a person who may start runs. */
17
+ const runsAs = (caller) => (caller.who.kind === "person" || caller.who.kind === "admin") && caller.who.id
18
+ ? caller.who.id
19
+ : null;
20
+ export async function wizardTools(caller) {
21
+ const userId = runsAs(caller);
22
+ if (!userId) {
23
+ return {};
24
+ }
25
+ // A client space runs the wizards of its space (docs/plans/client-spaces.md): they are listed
26
+ // from there and decided by the space's rules; a run started here belongs to the client space
27
+ // (docs/plans/client-portal.md).
28
+ const space = await db.query.space.findFirst({
29
+ columns: { kind: true, parentId: true },
30
+ where: eq(schema.space.id, caller.space.id),
31
+ });
32
+ const inClientSpace = space?.kind === "client" && Boolean(space.parentId);
33
+ const spaceId = inClientSpace && space?.parentId ? space.parentId : caller.space.id;
34
+ return {
35
+ wizard_list: createTool({
36
+ id: "wizard_list",
37
+ description: ELEMENT_SPECS.wizard_list.description(),
38
+ inputSchema: ELEMENT_SPECS.wizard_list.input,
39
+ execute: (input) => attempt(() => elementCall(caller, { action: "wizard_list", about: "wizards", perm: "r", input }, async () => {
40
+ const published = (await listWizards(userId, spaceId)).filter((w) => w.published);
41
+ const ok = new Set(await readableIds(spaceId, caller.subjects, "wizard", published.map((w) => w.id)));
42
+ return {
43
+ wizards: published
44
+ .filter((w) => ok.has(w.id))
45
+ .map((w) => ({ id: w.id, title: w.title, description: w.description ?? "" })),
46
+ };
47
+ })),
48
+ }),
49
+ wizard_run: createTool({
50
+ id: "wizard_run",
51
+ description: ELEMENT_SPECS.wizard_run.description(),
52
+ inputSchema: ELEMENT_SPECS.wizard_run.input,
53
+ execute: (input) => attempt(() => elementCall(caller, {
54
+ action: "wizard_run",
55
+ resource: { kind: "wizard", id: input.wizardId },
56
+ rulesIn: spaceId,
57
+ perm: "x",
58
+ input,
59
+ }, async () => {
60
+ const published = (await listWizards(userId, spaceId)).find((w) => w.id === input.wizardId && w.published);
61
+ if (!published) {
62
+ throw new Error(`No published wizard "${input.wizardId}" in this space.`);
63
+ }
64
+ const { runId, refused } = await startRun(userId, input.wizardId, input.answers, {
65
+ clientSpaceId: inClientSpace ? caller.space.id : null,
66
+ });
67
+ const report = await runReport(userId, runId, input.waitSeconds);
68
+ return { ...report, ...(refused ? { refusedAnswers: refused } : {}) };
69
+ })),
70
+ }),
71
+ };
72
+ }
@@ -286,5 +286,8 @@ export const driveConnector = defineConnector({
286
286
  id: "google-drive",
287
287
  moduleId: "connections-google",
288
288
  name: "Google Drive",
289
+ request: {
290
+ origins: () => ["https://www.googleapis.com"],
291
+ },
289
292
  toolPrefix: "gdrive",
290
293
  });
@@ -282,5 +282,8 @@ export const microsoftOneDriveConnector = defineConnector({
282
282
  id: "microsoft-onedrive",
283
283
  moduleId: "connections-microsoft",
284
284
  name: "Microsoft OneDrive",
285
+ request: {
286
+ origins: () => ["https://graph.microsoft.com"],
287
+ },
285
288
  toolPrefix: "onedrive",
286
289
  });
@@ -265,6 +265,12 @@ export const s3Connector = defineConnector({
265
265
  id: "s3",
266
266
  moduleId: "connections-s3",
267
267
  name: "S3 Bucket",
268
+ request: {
269
+ origins: (ctx) => [new URL(bucketUrl(parseCredentials(ctx.accessToken))).origin],
270
+ async sign(ctx, request) {
271
+ return clientFor(parseCredentials(ctx.accessToken)).sign(request);
272
+ },
273
+ },
268
274
  storage: {
269
275
  async write(ctx, input) {
270
276
  const creds = parseCredentials(ctx.accessToken);
@@ -101,6 +101,20 @@ export async function loadSpace(run) {
101
101
  }
102
102
  return p;
103
103
  }
104
+ /** Where a run's data is: the client space it was started in, else its wizard's space. */
105
+ async function dataSpaceOf(run, space) {
106
+ if (!run.clientSpaceId) {
107
+ return { id: space.id, name: space.name };
108
+ }
109
+ const client = await db.query.space.findFirst({
110
+ columns: { id: true, name: true },
111
+ where: eq(schema.space.id, run.clientSpaceId),
112
+ });
113
+ if (!client) {
114
+ throw new Error("client space missing");
115
+ }
116
+ return client;
117
+ }
104
118
  async function updateRun(runId, patch) {
105
119
  await db
106
120
  .update(schema.run)
@@ -215,11 +229,13 @@ export async function createRun(input) {
215
229
  lang: input.lang ?? null,
216
230
  unattended: input.unattended ?? false,
217
231
  client: input.client ?? null,
232
+ clientSpaceId: input.clientSpaceId ?? null,
218
233
  status: first.type === "page" ? "waiting_input" : "running",
219
234
  cursor: first.id,
220
235
  state,
221
- // Without an account nobody can come back for a result later: it is kept a week.
222
- expiresAt: input.mode === "live"
236
+ // Without an account nobody can come back for a result later: it is kept a week. A run of a
237
+ // client space is the client's, kept with it.
238
+ expiresAt: input.mode === "live" && !input.clientSpaceId
223
239
  ? new Date(Date.now() + env.limits.resultTtlDays * 24 * 60 * 60 * 1000)
224
240
  : null,
225
241
  });
@@ -474,6 +490,7 @@ export async function makeContext(run, space, state, signal, own = {}) {
474
490
  lists: await storedLists(run, holder),
475
491
  },
476
492
  space,
493
+ dataSpace: await dataSpaceOf(run, space),
477
494
  spaceFiles: (await spaceFiles(space.id)).filter((f) => f.status === "ready"),
478
495
  signal,
479
496
  resources,
@@ -520,7 +520,7 @@ export async function runAgentStep(step, ctx) {
520
520
  `Today is ${today(ctx)}.`,
521
521
  brandBlock(ctx),
522
522
  // Knowledge: what the space holds for its wizards, and how to find and read it.
523
- await knowledgeBlock(ctx.space.id),
523
+ await knowledgeBlock(ctx.dataSpace.id),
524
524
  // Skills: always on whole, on demand by name and description (docs/plans/skills.md).
525
525
  skillsBlock(await skillsOf(ctx), {
526
526
  tools: takesTools(resolved),
@@ -53,7 +53,7 @@ const port = num("API_PORT", 24368);
53
53
  /** From source the Vite dev server on :5181 serves the pages; built, this server does. */
54
54
  const fromSource = import.meta.url.endsWith(".ts");
55
55
  const sandboxImage = str("SANDBOX_IMAGE", "engenty-sandbox:latest");
56
- const ROLES = ["studio", "doors", "web", "runner", "all"];
56
+ const ROLES = ["studio", "doors", "api", "web", "runner", "all"];
57
57
  function role() {
58
58
  const given = str("RUNTIME_ROLE", "all");
59
59
  if (!ROLES.includes(given)) {
@@ -73,7 +73,9 @@ export const env = {
73
73
  /**
74
74
  * What this process does. `studio`: the makers' half (authoring, publishing, sign-in, MCP, the
75
75
  * studio's pages). `doors`: the customers' half (wizard pages, their API, shared results, the
76
- * plugins' public routes). `web`: both halves. `runner`: takes the runs that wait and drives
76
+ * plugins' public routes). `api`: outside clients alone, MCP and `/api/v1`, so they scale and
77
+ * fail apart from the studio (docs/plans/modular.md); `studio` serves them too until the cloud
78
+ * routes them to an `api` process. `web`: both halves. `runner`: takes the runs that wait and drives
77
79
  * them (it serves the routes too, for what the web hands it). `all`: everything in one process
78
80
  * (a runtime that runs alone). docs/plans/runner-process.md.
79
81
  */
@@ -83,6 +85,7 @@ export const env = {
83
85
  studio: ["studio", "web", "runner", "all"].includes(role()),
84
86
  doors: ["doors", "web", "runner", "all"].includes(role()),
85
87
  runner: ["runner", "all"].includes(role()),
88
+ api: ["api", "studio", "web", "runner", "all"].includes(role()),
86
89
  },
87
90
  runner: {
88
91
  /** This runner's name in the control database; a new one each start. */
@@ -107,6 +110,12 @@ export const env = {
107
110
  * file carry it. Default: APP_URL, the same origin.
108
111
  */
109
112
  publicUrl: str("PUBLIC_URL", appUrl).replace(/\/$/, ""),
113
+ /**
114
+ * Where a client of an organization works (docs/plans/client-portal.md): the client portal on
115
+ * the organization's host, `{host}` its `acme.wizards.page`. A client who signs in at the studio
116
+ * is sent there.
117
+ */
118
+ clientPortalUrl: str("CLIENT_PORTAL_URL", "https://{host}/portal"),
110
119
  /** More host names this server answers under, besides APP_URL's and the loopback names. */
111
120
  allowedHosts: list("ALLOWED_HOSTS"),
112
121
  dataDir,
@@ -1,6 +1,12 @@
1
1
  import { createHash } from "node:crypto";
2
+ import { sql } from "drizzle-orm";
3
+ import { db } from "../db/client.js";
2
4
  import { objects } from "./objects.js";
3
- /** Content-addressed file store for workspaces: a blob never changes, so snapshots are just lists of hashes. */
5
+ import { hasThumb, makeThumb } from "./thumbs.js";
6
+ /**
7
+ * Content-addressed file store for workspaces, skills and the space's files: a blob never
8
+ * changes, so snapshots are just lists of hashes, and the same bytes are stored once.
9
+ */
4
10
  const blobKey = (hash) => `blobs/${hash.slice(0, 2)}/${hash}`;
5
11
  export async function putBlob(data) {
6
12
  const hash = createHash("sha256").update(data).digest("hex");
@@ -33,3 +39,53 @@ export async function putBlobOf(hash, data) {
33
39
  await objects.put(blobKey(hash), data);
34
40
  }
35
41
  }
42
+ /** What is made from a blob (a thumbnail), under its hash; it goes with the blob. */
43
+ const derivedPrefix = (hash) => `derived/${hash}/`;
44
+ const thumbKey = (hash) => `${derivedPrefix(hash)}thumb.jpg`;
45
+ /**
46
+ * The 320 px JPEG of a blob that is a picture or a PDF (thumbs.ts): kept the first time it is
47
+ * asked for, made then when it isn't there yet (bytes that arrived by sync, or before a crash).
48
+ * One that doesn't decode is kept as empty, so it is tried once. Null when there is none.
49
+ */
50
+ export async function blobThumb(hash, mime) {
51
+ if (!hasThumb(mime) || !/^[a-f0-9]{64}$/.test(hash)) {
52
+ return null;
53
+ }
54
+ const kept = await objects.get(thumbKey(hash));
55
+ if (kept) {
56
+ return kept.byteLength ? kept : null;
57
+ }
58
+ const data = await objects.get(blobKey(hash));
59
+ if (!data) {
60
+ return null;
61
+ }
62
+ const thumb = await makeThumb(data, mime);
63
+ await objects.put(thumbKey(hash), thumb ?? new Uint8Array());
64
+ return thumb;
65
+ }
66
+ /**
67
+ * Whether a row of this tenant still names the hash: a space file (its bytes or its text), a
68
+ * workspace or kept file, a published version's or a run's workspace, a skill's file, a
69
+ * mounted file's draft.
70
+ */
71
+ async function blobInUse(hash) {
72
+ const like = `%"${hash}"%`;
73
+ const found = await db.all(sql `
74
+ SELECT 1 FROM space_file WHERE hash = ${hash} OR text_hash = ${hash}
75
+ UNION ALL SELECT 1 FROM wizard_file WHERE hash = ${hash}
76
+ UNION ALL SELECT 1 FROM store_file WHERE hash = ${hash}
77
+ UNION ALL SELECT 1 FROM wizard_version WHERE files LIKE ${like}
78
+ UNION ALL SELECT 1 FROM run WHERE files LIKE ${like}
79
+ UNION ALL SELECT 1 FROM space_skill WHERE blob_files LIKE ${like}
80
+ UNION ALL SELECT 1 FROM space_mount_draft WHERE blob = ${hash}
81
+ LIMIT 1`);
82
+ return found.length > 0;
83
+ }
84
+ /** Deletes a blob and what was made from it once no row names it any more. */
85
+ export async function releaseBlob(hash) {
86
+ if (!hash || !/^[a-f0-9]{64}$/.test(hash) || (await blobInUse(hash))) {
87
+ return;
88
+ }
89
+ await objects.remove(blobKey(hash));
90
+ await objects.removePrefix(derivedPrefix(hash));
91
+ }
@@ -90,6 +90,7 @@ export const objects = {
90
90
  get: (key) => store.get(tenantKey(key)),
91
91
  has: (key) => store.has(tenantKey(key)),
92
92
  remove: (key) => store.remove(tenantKey(key)),
93
+ removePrefix: (prefix) => store.removePrefix(tenantKey(prefix)),
93
94
  };
94
95
  /** Whether the store answers (health.ts). */
95
96
  export const probeObjects = () => store.probe();
@@ -0,0 +1,41 @@
1
+ // Pictures for the embedding model: an image as a JPEG it looks at, a PDF as pictures of its
2
+ // pages. @napi-rs/canvas comes with pdf-parse and has prebuilt binaries, so nothing is compiled.
3
+ /** The side EmbeddingGemma sees a picture at; larger only costs time. */
4
+ export const EMBED_SIDE = 768;
5
+ /** A PDF's pages beyond this are found by their text only. */
6
+ export const MAX_PAGE_PICTURES = 40;
7
+ /** An image as a JPEG no larger than `side` on its longer side. */
8
+ export async function jpegOf(bytes, side = EMBED_SIDE) {
9
+ const { createCanvas, loadImage } = await import("@napi-rs/canvas");
10
+ const image = await loadImage(Buffer.from(bytes));
11
+ const scale = Math.min(1, side / Math.max(image.width, image.height));
12
+ const w = Math.max(1, Math.round(image.width * scale));
13
+ const h = Math.max(1, Math.round(image.height * scale));
14
+ const canvas = createCanvas(w, h);
15
+ const ctx = canvas.getContext("2d");
16
+ // Transparent PNGs on white, as people see them.
17
+ ctx.fillStyle = "#fff";
18
+ ctx.fillRect(0, 0, w, h);
19
+ ctx.drawImage(image, 0, 0, w, h);
20
+ return canvas.encode("jpeg", 82);
21
+ }
22
+ /** Pictures of a PDF's first `MAX_PAGE_PICTURES` pages, EMBED_SIDE wide, by their 1-based number. */
23
+ export async function pdfPages(bytes) {
24
+ const { PDFParse } = await import("pdf-parse");
25
+ const parser = new PDFParse({ data: new Uint8Array(bytes) });
26
+ try {
27
+ const shots = await parser.getScreenshot({
28
+ desiredWidth: EMBED_SIDE,
29
+ first: MAX_PAGE_PICTURES,
30
+ imageDataUrl: false,
31
+ });
32
+ const pages = [];
33
+ for (const s of shots.pages) {
34
+ pages.push({ page: s.pageNumber, jpeg: await jpegOf(s.data) });
35
+ }
36
+ return pages;
37
+ }
38
+ finally {
39
+ await parser.destroy();
40
+ }
41
+ }
@@ -5,6 +5,7 @@ import { nanoid } from "nanoid";
5
5
  import { db, schema } from "../db/client.js";
6
6
  import { env } from "../env.js";
7
7
  import { currentTenant } from "../tenants/tenant.js";
8
+ import { getBlob } from "./blobs.js";
8
9
  import { objects } from "./objects.js";
9
10
  const assetKey = (file) => `assets/${file}`;
10
11
  const EXT = {
@@ -67,6 +68,16 @@ export async function loadAssetText(id) {
67
68
  const found = await loadAsset(id);
68
69
  return found ? found.data.toString("utf8") : "";
69
70
  }
71
+ /** A picture `asset://ID` names: an asset of a run, or a file of the space (a logo, a pattern). */
72
+ async function pictureOf(id) {
73
+ const found = await loadAsset(id);
74
+ if (found) {
75
+ return { mime: found.row.mime, data: found.data };
76
+ }
77
+ const file = await db.query.spaceFile.findFirst({ where: eq(schema.spaceFile.id, id) });
78
+ const data = file?.hash ? await getBlob(file.hash).catch(() => null) : null;
79
+ return file && data ? { mime: file.mime, data } : null;
80
+ }
70
81
  /**
71
82
  * Generated HTML points at images as `asset://ID`; previews and downloads get them
72
83
  * inlined as data URIs, so every file stands on its own. Only the tenant's assets resolve.
@@ -75,37 +86,14 @@ export async function inlineAssetRefs(html) {
75
86
  const ids = [...new Set([...html.matchAll(/asset:\/\/([A-Za-z0-9_-]{6,40})/g)].map((m) => m[1]))];
76
87
  let out = html;
77
88
  for (const id of ids) {
78
- const found = await loadAsset(id);
79
- const uri = found?.row.mime.startsWith("image/")
80
- ? `data:${found.row.mime};base64,${found.data.toString("base64")}`
89
+ const found = await pictureOf(id);
90
+ const uri = found?.mime.startsWith("image/")
91
+ ? `data:${found.mime};base64,${found.data.toString("base64")}`
81
92
  : "";
82
93
  out = out.split(`asset://${id}`).join(uri);
83
94
  }
84
95
  return out;
85
96
  }
86
- /** Gives an asset new content under the id it has: what points to it keeps pointing to it. */
87
- export async function replaceAsset(id, input) {
88
- const row = await db.query.asset.findFirst({ where: eq(schema.asset.id, id) });
89
- if (!row) {
90
- return;
91
- }
92
- const bytes = Buffer.from(input.data);
93
- const file = `${id}-${nanoid(6)}.${extFor(input.mime)}`;
94
- await objects.put(assetKey(file), bytes);
95
- await db
96
- .update(schema.asset)
97
- .set({ mime: input.mime, name: input.name, path: file, size: bytes.byteLength })
98
- .where(eq(schema.asset.id, id));
99
- await objects.remove(assetKey(row.path));
100
- }
101
- /** Deletes one asset: its file and its row. */
102
- export async function removeAsset(id) {
103
- const row = await db.query.asset.findFirst({ where: eq(schema.asset.id, id) });
104
- if (row) {
105
- await objects.remove(assetKey(row.path));
106
- await db.delete(schema.asset).where(eq(schema.asset.id, id));
107
- }
108
- }
109
97
  /** Deletes every file a run made (uploads, results, renders, video folders) and their rows. */
110
98
  export async function removeAssetFiles(runId) {
111
99
  const rows = await db.query.asset.findMany({ where: eq(schema.asset.runId, runId) });