@engenty/wizards-runtime 0.15.0 → 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.
- package/apps/runtime/dist/agents/studio-assistant.js +138 -9
- package/apps/runtime/dist/agents/website.js +10 -5
- package/apps/runtime/dist/app.js +18 -0
- package/apps/runtime/dist/auth/index.js +5 -0
- package/apps/runtime/dist/broker/account.js +5 -0
- package/apps/runtime/dist/broker/index.js +5 -0
- package/apps/runtime/dist/broker/rwx.js +255 -7
- package/apps/runtime/dist/cli/main.js +7 -0
- package/apps/runtime/dist/cli/move-files.js +68 -0
- package/apps/runtime/dist/cli/move-folder-sources.js +120 -0
- package/apps/runtime/dist/connectors/plugin-connections.js +74 -4
- package/apps/runtime/dist/db/migrations/tenant/0042_client_spaces.sql +16 -0
- package/apps/runtime/dist/db/migrations/tenant/0043_space_member_invite.sql +19 -0
- package/apps/runtime/dist/db/migrations/tenant/0044_client_portal.sql +8 -0
- package/apps/runtime/dist/db/migrations/tenant/0045_client_portal_no_slug.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0046_space_file_hash.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0047_knowledge_folders.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/0048_space_chunk_kind.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/0049_knowledge_sources.sql +57 -0
- package/apps/runtime/dist/db/migrations/tenant/0050_dateien.sql +71 -0
- package/apps/runtime/dist/db/migrations/tenant/0051_dateien_drafts.sql +23 -0
- package/apps/runtime/dist/db/migrations/tenant/0052_dateien_plugin_kinds.sql +5 -0
- package/apps/runtime/dist/db/migrations/tenant/0053_dateien_file_title.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/0054_dateien_ocr.sql +14 -0
- package/apps/runtime/dist/db/migrations/tenant/0055_dateien_doc_reread.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0042_snapshot.json +5286 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0043_snapshot.json +5293 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0044_snapshot.json +5344 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0045_snapshot.json +5329 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0046_snapshot.json +5343 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0047_snapshot.json +5391 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0048_snapshot.json +5398 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0049_snapshot.json +5793 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0050_snapshot.json +6288 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0051_snapshot.json +6461 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0052_snapshot.json +6497 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0053_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0054_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0055_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +98 -0
- package/apps/runtime/dist/db/schema.js +294 -5
- package/apps/runtime/dist/docs/dev/api.md +25 -0
- package/apps/runtime/dist/docs/dev/plugins/connections.md +30 -2
- package/apps/runtime/dist/docs/dev/plugins/index.md +1 -1
- package/apps/runtime/dist/docs/dev/plugins/reference.md +7 -4
- package/apps/runtime/dist/docs/dev/plugins/server.md +11 -5
- package/apps/runtime/dist/docs/dev/plugins/space.md +130 -4
- package/apps/runtime/dist/docs/dev/plugins/studio.md +13 -3
- package/apps/runtime/dist/docs/user-guide/command-line.md +2 -0
- package/apps/runtime/dist/docs/user-guide/plugins.md +1 -1
- package/apps/runtime/dist/docs/user-guide/space.md +6 -6
- package/apps/runtime/dist/docs/user-guide/steps.md +1 -1
- package/apps/runtime/dist/documents/ocr.js +72 -0
- package/apps/runtime/dist/documents/parse.js +81 -12
- package/apps/runtime/dist/elements/caller.js +5 -3
- package/apps/runtime/dist/elements/gate.js +1 -1
- package/apps/runtime/dist/elements/knowledge.js +181 -12
- package/apps/runtime/dist/elements/specs.js +1 -1
- package/apps/runtime/dist/elements/wizards.js +15 -2
- package/apps/runtime/dist/engenty/connections-google/connectors/drive.js +3 -0
- package/apps/runtime/dist/engenty/connections-microsoft/onedrive.js +3 -0
- package/apps/runtime/dist/engenty/connections-s3/s3.js +6 -0
- package/apps/runtime/dist/engine/runner.js +19 -2
- package/apps/runtime/dist/engine/steps.js +1 -1
- package/apps/runtime/dist/env.js +6 -0
- package/apps/runtime/dist/files/blobs.js +57 -1
- package/apps/runtime/dist/files/objects.js +1 -0
- package/apps/runtime/dist/files/pictures.js +41 -0
- package/apps/runtime/dist/files/storage.js +14 -26
- package/apps/runtime/dist/files/thumbs.js +77 -0
- package/apps/runtime/dist/files/upload-check.js +2 -2
- package/apps/runtime/dist/index.js +22 -0
- package/apps/runtime/dist/live/views.js +2 -1
- package/apps/runtime/dist/manage.js +4 -1
- package/apps/runtime/dist/mcp/tools.js +5 -0
- package/apps/runtime/dist/models.js +82 -13
- package/apps/runtime/dist/mounts/cache.js +71 -0
- package/apps/runtime/dist/mounts/drafts.js +367 -0
- package/apps/runtime/dist/mounts/folder.js +148 -0
- package/apps/runtime/dist/mounts/mounts.js +1315 -0
- package/apps/runtime/dist/mounts/source.js +179 -0
- package/apps/runtime/dist/mounts/units.js +97 -0
- package/apps/runtime/dist/move-files.js +4 -0
- package/apps/runtime/dist/plugins/loader.js +73 -1
- package/apps/runtime/dist/plugins/registry.js +2 -0
- package/apps/runtime/dist/prompts/goose.md +13 -1
- package/apps/runtime/dist/prompts/tools.md +5 -2
- package/apps/runtime/dist/render/convert.js +188 -1
- package/apps/runtime/dist/routes/api.js +6 -1
- package/apps/runtime/dist/routes/client-contract.js +164 -0
- package/apps/runtime/dist/routes/client-spaces.js +58 -0
- package/apps/runtime/dist/routes/dateien.js +173 -0
- package/apps/runtime/dist/routes/elements.js +2 -2
- package/apps/runtime/dist/routes/plugins.js +2 -1
- package/apps/runtime/dist/routes/runs.js +24 -10
- package/apps/runtime/dist/routes/studio.js +168 -8
- package/apps/runtime/dist/secrets/signing.js +16 -0
- package/apps/runtime/dist/services/client-portal.js +439 -0
- package/apps/runtime/dist/services/client-spaces.js +228 -0
- package/apps/runtime/dist/services/cloud.js +2 -0
- package/apps/runtime/dist/services/knowledge-model.js +24 -8
- package/apps/runtime/dist/services/knowledge.js +72 -42
- package/apps/runtime/dist/services/links.js +17 -0
- package/apps/runtime/dist/services/members-studio.js +5 -1
- package/apps/runtime/dist/services/page-pictures.js +162 -0
- package/apps/runtime/dist/services/runs.js +22 -6
- package/apps/runtime/dist/services/space-categories.js +41 -5
- package/apps/runtime/dist/services/space-data.js +165 -8
- package/apps/runtime/dist/services/space-files.js +102 -19
- package/apps/runtime/dist/services/space-index.js +384 -41
- package/apps/runtime/dist/services/space-origin.js +25 -1
- package/apps/runtime/dist/services/space-sources.js +937 -0
- package/apps/runtime/dist/services/spaces.js +60 -12
- package/apps/runtime/dist/services/synced-spaces.js +10 -1
- package/apps/runtime/dist/services/work-ai.js +29 -8
- package/apps/runtime/dist/services/work-chats.js +3 -2
- package/apps/runtime/dist/sharing/index.js +2 -0
- package/apps/runtime/dist/store/index.js +29 -8
- package/apps/runtime/dist/store/shared-lists.js +30 -17
- package/apps/runtime/dist/sync/records.js +22 -27
- package/apps/runtime/dist/texts.js +4 -0
- package/apps/runtime/dist/tools/pages.js +1 -1
- package/apps/runtime/dist/widgets/step.js +2 -1
- package/apps/web/dist/assets/AboutDialog-0q4P80yv.js +1 -0
- package/apps/web/dist/assets/BlockEditor-CUYFH_CU.js +173 -0
- package/apps/web/dist/assets/BlockEditor-CtW6lw0y.css +1 -0
- package/apps/web/dist/assets/CapabilitiesPage-BlZP8DYb.js +2 -0
- package/apps/web/dist/assets/{ChangelogDialog-Do7pwmFv.js → ChangelogDialog-CDN7kMs1.js} +1 -1
- package/apps/web/dist/assets/{CodeEditor-Cdox-51M.js → CodeEditor-D-Y16fdk.js} +1 -1
- package/apps/web/dist/assets/EditorPage-CrMVsgUt.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-BPS86xzA.js → FlowPreview-DXnk7m9P.js} +1 -1
- package/apps/web/dist/assets/{LangSwitch-BRHbi_Sd.js → LangSwitch-Dkd0FBIH.js} +1 -1
- package/apps/web/dist/assets/Marketplace-Cty40VwG.js +2 -0
- package/apps/web/dist/assets/NewWizardChannels-Czool7s1.js +1 -0
- package/apps/web/dist/assets/NewWizardPage-CfVe_4a2.js +7 -0
- package/apps/web/dist/assets/OnboardingPage-D3SEpTmt.js +1 -0
- package/apps/web/dist/assets/PreviewPane-CQDKQO6z.js +7 -0
- package/apps/web/dist/assets/PublicRunner-BDZNnlQw.js +1 -0
- package/apps/web/dist/assets/ReadOnly-B7MUySpC.js +1 -0
- package/apps/web/dist/assets/SettingsPage-Bgurv3l2.js +2 -0
- package/apps/web/dist/assets/SetupPage-C3km8vqv.js +1 -0
- package/apps/web/dist/assets/{SharePage-PKHX8a8a.js → SharePage-UwEJBIzq.js} +1 -1
- package/apps/web/dist/assets/SkillEditor-BaEiqqXl.js +1 -0
- package/apps/web/dist/assets/Skills-Bv6XbKxc.js +9 -0
- package/apps/web/dist/assets/SpacePage-D9PXLSU_.js +2 -0
- package/apps/web/dist/assets/UpdateCheck-C7I43zFZ.js +2 -0
- package/apps/web/dist/assets/changelog-DwgWiSZb.js +1 -0
- package/apps/web/dist/assets/circle-check-DDLzlcXP.js +1 -0
- package/apps/web/dist/assets/code-DsnwzN1x.js +1 -0
- package/apps/web/dist/assets/code-editor-Ca5EAxmd.js +2 -0
- package/apps/web/dist/assets/code-xml-S4IFi5Jc.js +1 -0
- package/apps/web/dist/assets/connection-sign-in-hI3HXhq4.js +1 -0
- package/apps/web/dist/assets/{dist-H1UN89OA.js → dist-B3Ray2U5.js} +1 -1
- package/apps/web/dist/assets/dist-CQpLJoQZ.js +10 -0
- package/apps/web/dist/assets/{dist-GlRRx9Xl.js → dist-D1uGQ_tB.js} +1 -1
- package/apps/web/dist/assets/{dist-BdDd1FxP.js → dist-DFI-tsfx.js} +1 -1
- package/apps/web/dist/assets/eye-TJSuiHHO.js +1 -0
- package/apps/web/dist/assets/floating-ui.dom-pwdNGv4b.js +1 -0
- package/apps/web/dist/assets/host-D5hNHbhF.js +125 -0
- package/apps/web/dist/assets/i18n-BBzMbGoo.js +55 -0
- package/apps/web/dist/assets/index-CyBrvupM.js +10 -0
- package/apps/web/dist/assets/index-w-3J3ZJ9.css +1 -0
- package/apps/web/dist/assets/{json-CEsfHdlZ.js → json-Ce3iHrtf.js} +1 -1
- package/apps/web/dist/assets/link-CPL8pB1P.js +1 -0
- package/apps/web/dist/assets/list-checks-DaxmUPqK.js +1 -0
- package/apps/web/dist/assets/{markdown-5-I9_izp.js → markdown-hqKQy2pp.js} +1 -1
- package/apps/web/dist/assets/marked.esm-DQ7XA7AC.js +67 -0
- package/apps/web/dist/assets/oss-credits-BrwV0a4F.js +1 -0
- package/apps/web/dist/assets/pdf-Dg-raaUo.js +54 -0
- package/apps/web/dist/assets/pdf.worker.min-68CMzziE.js +1 -0
- package/apps/web/dist/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
- package/apps/web/dist/assets/phone-B4uKDBFq.js +1 -0
- package/apps/web/dist/assets/read-aloud-Bfp9_bUY.js +1 -0
- package/apps/web/dist/assets/{runners-BoPaPsjN.js → runners-CUpBpSji.js} +1 -1
- package/apps/web/dist/assets/scroll-text-BEnPzQEi.js +1 -0
- package/apps/web/dist/assets/session-CMQo8P-G.js +2 -0
- package/apps/web/dist/assets/table-5B9G3oA6.js +1 -0
- package/apps/web/dist/assets/theme-CdX0marK.js +11 -0
- package/apps/web/dist/assets/ui-BCfu5sia.js +16 -0
- package/apps/web/dist/assets/upload-CAlHC232.js +1 -0
- package/apps/web/dist/assets/{useMutation-7_FOHoEl.js → useMutation-1fV4Nnxq.js} +1 -1
- package/apps/web/dist/assets/viewers-ChriFcRy.js +2 -0
- package/apps/web/dist/assets/volume-2-BqlBtCIJ.js +1 -0
- package/apps/web/dist/assets/w3c-keyname-BOAvb0qz.js +1 -0
- package/apps/web/dist/assets/xlsx-CuSbHU4R.js +141 -0
- package/apps/web/dist/index.html +19 -14
- package/apps/web/dist/mcp/flow.html +2 -2
- package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +223 -4
- package/node_modules/@engenty/wizards/dist/plugin/mount.d.ts +123 -0
- package/node_modules/@engenty/wizards/dist/plugin/mount.js +7 -0
- package/node_modules/@engenty/wizards/dist/plugin/studio.d.ts +110 -1
- package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +1 -1
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +24 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +24 -0
- package/node_modules/@engenty/wizards-shared/dist/knowledge.d.ts +33 -4
- package/node_modules/@engenty/wizards-shared/dist/run.d.ts +3 -0
- package/node_modules/@engenty/wizards-shared/dist/run.js +6 -0
- package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +21 -4
- package/node_modules/@engenty/wizards-shared/dist/rwx.js +27 -3
- package/node_modules/@engenty/wizards-shared/dist/spaces.d.ts +11 -1
- package/node_modules/@engenty/wizards-shared/dist/spaces.js +9 -2
- package/node_modules/@engenty/wizards-shared/dist/text-merge.d.ts +6 -0
- package/node_modules/@engenty/wizards-shared/dist/text-merge.js +25 -0
- package/package.json +3 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/plugin.json +1 -1
- package/apps/web/dist/assets/AboutDialog-DaWEIHFN.js +0 -1
- package/apps/web/dist/assets/CapabilitiesPage-Dt91U_sK.js +0 -2
- package/apps/web/dist/assets/EditorPage-DS_a5K3s.js +0 -3
- package/apps/web/dist/assets/Marketplace-CS7JTgiv.js +0 -2
- package/apps/web/dist/assets/NewWizardChannels-D-OLsex6.js +0 -1
- package/apps/web/dist/assets/NewWizardPage-BM_pkk41.js +0 -7
- package/apps/web/dist/assets/OnboardingPage-Cpn5pubC.js +0 -1
- package/apps/web/dist/assets/PreviewPane-BOk1-yO4.js +0 -9
- package/apps/web/dist/assets/PublicRunner-CgzMZb0r.js +0 -1
- package/apps/web/dist/assets/ReadOnly-8DEx-ly3.js +0 -1
- package/apps/web/dist/assets/SettingsPage-CcrknFcI.js +0 -2
- package/apps/web/dist/assets/SetupPage-D5nzjgPU.js +0 -1
- package/apps/web/dist/assets/SkillEditor-BsXTqNli.js +0 -1
- package/apps/web/dist/assets/Skills-C_X1ZsKg.js +0 -9
- package/apps/web/dist/assets/SpacePage-wZGqUoSQ.js +0 -2
- package/apps/web/dist/assets/UpdateCheck-D6x7MqEl.js +0 -2
- package/apps/web/dist/assets/changelog-M-eOBqgu.js +0 -1
- package/apps/web/dist/assets/circle-alert-DJbCyxcg.js +0 -1
- package/apps/web/dist/assets/circle-check-jqtsKi_b.js +0 -1
- package/apps/web/dist/assets/code-editor-BpCEnf8H.js +0 -2
- package/apps/web/dist/assets/code-xml-q88cSTV8.js +0 -1
- package/apps/web/dist/assets/connection-sign-in-UJ6xk-TD.js +0 -1
- package/apps/web/dist/assets/dist-EBVTpvRr.js +0 -10
- package/apps/web/dist/assets/eye-CgkvVlba.js +0 -1
- package/apps/web/dist/assets/host-Bd_bon_c.js +0 -118
- package/apps/web/dist/assets/i18n-0fUKyVRE.js +0 -55
- package/apps/web/dist/assets/index-D18DvnxX.css +0 -1
- package/apps/web/dist/assets/index-DOgwxnto.js +0 -10
- package/apps/web/dist/assets/oss-credits-IdX5bc71.js +0 -1
- package/apps/web/dist/assets/phone-CjPvZ0nk.js +0 -1
- package/apps/web/dist/assets/read-aloud-CLXo_asN.js +0 -1
- package/apps/web/dist/assets/scroll-text-2BhJO_W7.js +0 -1
- package/apps/web/dist/assets/session-CpYa5hwd.js +0 -2
- package/apps/web/dist/assets/theme-Bj3wNQR0.js +0 -11
- package/apps/web/dist/assets/ui-CktezodE.js +0 -82
- package/apps/web/dist/assets/upload-CHGCFVZK.js +0 -1
- package/apps/web/dist/assets/volume-2-Bw5KASy8.js +0 -1
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { createTool } from "@mastra/core/tools";
|
|
2
|
-
import { and, eq } from "drizzle-orm";
|
|
3
|
-
import {
|
|
2
|
+
import { and, eq, inArray } from "drizzle-orm";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { assertRwx, decide } from "../broker/rwx.js";
|
|
4
5
|
import { db, schema } from "../db/client.js";
|
|
6
|
+
import { openDraft, saveDraft } from "../mounts/drafts.js";
|
|
7
|
+
import { fileByPath, mountFileText, usableMounts } from "../mounts/mounts.js";
|
|
5
8
|
import { prompt } from "../prompts.js";
|
|
6
9
|
import { filePaths, knowledgeCounts, listForStep, readPage, readTable, resolvePath, } from "../services/knowledge.js";
|
|
7
10
|
import { documentText, hasIndexEntries, searchKnowledge } from "../services/space-index.js";
|
|
@@ -17,10 +20,48 @@ import { ELEMENT_SPECS } from "./specs.js";
|
|
|
17
20
|
* (broker/rwx.ts), and each call is a line of its log (gate.ts).
|
|
18
21
|
*/
|
|
19
22
|
export { pageReadInput } from "./specs.js";
|
|
23
|
+
/**
|
|
24
|
+
* Where the caller's Knowledge is read: the space, and for a client space its parent too
|
|
25
|
+
* (docs/plans/client-spaces.md). The rules decide each item of the parent as for any reader: a
|
|
26
|
+
* client reads only what the parent opened to clients, the firm reads all of it.
|
|
27
|
+
*/
|
|
28
|
+
async function readsFrom(spaceId) {
|
|
29
|
+
const space = await db.query.space.findFirst({
|
|
30
|
+
columns: { id: true, name: true, kind: true, parentId: true },
|
|
31
|
+
where: eq(schema.space.id, spaceId),
|
|
32
|
+
});
|
|
33
|
+
if (!(space?.kind === "client" && space.parentId)) {
|
|
34
|
+
return [{ id: spaceId, name: space?.name ?? "", own: true }];
|
|
35
|
+
}
|
|
36
|
+
const parent = await db.query.space.findFirst({
|
|
37
|
+
columns: { id: true, name: true },
|
|
38
|
+
where: eq(schema.space.id, space.parentId),
|
|
39
|
+
});
|
|
40
|
+
return [
|
|
41
|
+
{ id: spaceId, name: space.name, own: true },
|
|
42
|
+
...(parent ? [{ id: parent.id, name: parent.name, own: false }] : []),
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
/** The first space that has it: the client space before its parent. A refusal stands. */
|
|
46
|
+
async function firstFound(spaces, read) {
|
|
47
|
+
let missing = null;
|
|
48
|
+
for (const space of spaces) {
|
|
49
|
+
try {
|
|
50
|
+
return await read(space.id);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
if (err?.code === "forbidden") {
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
missing ??= err;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
throw missing;
|
|
60
|
+
}
|
|
20
61
|
/** A page of Knowledge by its path, for the caller: its front matter and Markdown, in parts. */
|
|
21
62
|
export function readKnowledgePage(caller, input) {
|
|
22
63
|
return attempt(() => elementCall(caller, { action: "page_read", perm: "r", input }, async () => {
|
|
23
|
-
const read = await
|
|
64
|
+
const read = await firstFound(await readsFrom(caller.space.id), (spaceId) => readPage(spaceId, input.path, input, caller.subjects));
|
|
24
65
|
await caller.note?.({ code: "reads", params: { name: read.path } });
|
|
25
66
|
return read;
|
|
26
67
|
}));
|
|
@@ -29,9 +70,19 @@ export function readKnowledgePage(caller, input) {
|
|
|
29
70
|
export async function knowledgeTools(caller) {
|
|
30
71
|
const spaceId = caller.space.id;
|
|
31
72
|
const who = caller.subjects;
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
|
|
73
|
+
const spaces = await readsFrom(spaceId);
|
|
74
|
+
const counts = { pages: 0, tables: 0, files: 0 };
|
|
75
|
+
let entries = false;
|
|
76
|
+
for (const space of spaces) {
|
|
77
|
+
const of = await knowledgeCounts(space.id);
|
|
78
|
+
counts.pages += of.pages;
|
|
79
|
+
counts.tables += of.tables;
|
|
80
|
+
counts.files += of.files;
|
|
81
|
+
entries ||= await hasIndexEntries(space.id);
|
|
82
|
+
}
|
|
83
|
+
// The folders of Dateien the caller may use: searched, and read by their path.
|
|
84
|
+
const mounted = (await usableMounts(spaceId, who)).length > 0;
|
|
85
|
+
if (!(counts.pages || counts.tables || counts.files || entries || mounted)) {
|
|
35
86
|
return {};
|
|
36
87
|
}
|
|
37
88
|
const call = (action, input, run) => attempt(() => elementCall(caller, { action, perm: "r", input }, run));
|
|
@@ -41,7 +92,7 @@ export async function knowledgeTools(caller) {
|
|
|
41
92
|
description: prompt("tools", "tool.space_search"),
|
|
42
93
|
inputSchema: ELEMENT_SPECS.space_search.input,
|
|
43
94
|
execute: (input) => call("space_search", input, async () => {
|
|
44
|
-
const found = await
|
|
95
|
+
const found = await searchAll(spaces, input.query, {
|
|
45
96
|
where: input.where,
|
|
46
97
|
limit: input.limit,
|
|
47
98
|
signal: caller.signal,
|
|
@@ -58,6 +109,7 @@ export async function knowledgeTools(caller) {
|
|
|
58
109
|
return {
|
|
59
110
|
hits: found.hits.map((h) => ({
|
|
60
111
|
...(h.path ? { path: h.path } : {}),
|
|
112
|
+
...(h.mount ? { in: `Dateien › ${h.mount.name}` } : {}),
|
|
61
113
|
...(h.open ? { open: h.open } : {}),
|
|
62
114
|
title: h.title,
|
|
63
115
|
...(h.categories.length ? { categories: h.categories } : {}),
|
|
@@ -78,6 +130,13 @@ export async function knowledgeTools(caller) {
|
|
|
78
130
|
inputSchema: ELEMENT_SPECS.space_list.input,
|
|
79
131
|
execute: (input) => call("space_list", input, async () => {
|
|
80
132
|
const listed = await listForStep(spaceId, input.where, who);
|
|
133
|
+
const parent = spaces.find((p) => !p.own);
|
|
134
|
+
if (parent) {
|
|
135
|
+
return {
|
|
136
|
+
...listed,
|
|
137
|
+
[`from ${parent.name}`]: await listForStep(parent.id, input.where, who),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
81
140
|
await caller.note?.({ code: "listsKnowledge" });
|
|
82
141
|
return listed;
|
|
83
142
|
}),
|
|
@@ -97,31 +156,42 @@ export async function knowledgeTools(caller) {
|
|
|
97
156
|
description: prompt("tools", "tool.table_read"),
|
|
98
157
|
inputSchema: ELEMENT_SPECS.table_read.input,
|
|
99
158
|
execute: (input) => call("table_read", input, async () => {
|
|
100
|
-
const read = await readTable(
|
|
159
|
+
const read = await firstFound(spaces, (id) => readTable(id, input.path, input, who));
|
|
101
160
|
await caller.note?.({ code: "reads", params: { name: read.path } });
|
|
102
161
|
return read;
|
|
103
162
|
}),
|
|
104
163
|
});
|
|
105
164
|
}
|
|
106
|
-
if (counts.files) {
|
|
165
|
+
if (counts.files || mounted) {
|
|
107
166
|
tools.space_document = createTool({
|
|
108
167
|
id: "space_document",
|
|
109
168
|
description: prompt("tools", "tool.space_document"),
|
|
110
169
|
inputSchema: ELEMENT_SPECS.space_document.input,
|
|
111
170
|
execute: (input) => call("space_document", input, async () => {
|
|
112
171
|
const { path, from } = input;
|
|
113
|
-
|
|
172
|
+
if (path.startsWith("dateien/")) {
|
|
173
|
+
return readMounted(path, from);
|
|
174
|
+
}
|
|
175
|
+
const wanted = path.startsWith("files/") ? path : `files/${path}`;
|
|
176
|
+
const inSpace = await firstFound(spaces, async (id) => {
|
|
177
|
+
const hit = await resolvePath(id, wanted);
|
|
178
|
+
if (hit?.kind !== "file") {
|
|
179
|
+
throw new Error("not here");
|
|
180
|
+
}
|
|
181
|
+
return id;
|
|
182
|
+
}).catch(() => spaceId);
|
|
183
|
+
const found = await resolvePath(inSpace, wanted);
|
|
114
184
|
if (found?.kind !== "file") {
|
|
115
185
|
const there = [...(await filePaths(spaceId)).values()].slice(0, 40);
|
|
116
186
|
throw new Error(`No file "${path}". There are: ${there.join(", ")}`);
|
|
117
187
|
}
|
|
118
188
|
const file = await db.query.spaceFile.findFirst({
|
|
119
|
-
where: and(eq(schema.spaceFile.id, found.id), eq(schema.spaceFile.spaceId,
|
|
189
|
+
where: and(eq(schema.spaceFile.id, found.id), eq(schema.spaceFile.spaceId, inSpace)),
|
|
120
190
|
});
|
|
121
191
|
if (!file) {
|
|
122
192
|
throw new Error(`No file "${path}".`);
|
|
123
193
|
}
|
|
124
|
-
await assertRwx(
|
|
194
|
+
await assertRwx(inSpace, who, { kind: "document", id: file.id }, "r");
|
|
125
195
|
await caller.note?.({ code: "reads", params: { name: file.name } });
|
|
126
196
|
const text = (await documentText(file)) || file.description;
|
|
127
197
|
const start = Math.min(from ?? 0, text.length);
|
|
@@ -135,5 +205,104 @@ export async function knowledgeTools(caller) {
|
|
|
135
205
|
}),
|
|
136
206
|
});
|
|
137
207
|
}
|
|
208
|
+
// A Work chat's AI writes drafts of a mounted folder's files where the folder lets agents do it;
|
|
209
|
+
// a person publishes them. Not offered to outside clients: their scopes don't write yet.
|
|
210
|
+
const drafts = !caller.client &&
|
|
211
|
+
caller.who.id &&
|
|
212
|
+
who.some((s) => s.kind === "agent") &&
|
|
213
|
+
(await draftableMounts(spaceId, who)).length > 0;
|
|
214
|
+
if (drafts) {
|
|
215
|
+
const userId = caller.who.id;
|
|
216
|
+
tools.dateien_draft = createTool({
|
|
217
|
+
id: "dateien_draft",
|
|
218
|
+
description: prompt("tools", "tool.dateien_draft"),
|
|
219
|
+
inputSchema: z.object({
|
|
220
|
+
path: z.string().describe("The file's path in Dateien: dateien/…"),
|
|
221
|
+
text: z.string().max(200_000).describe("The file's whole new text"),
|
|
222
|
+
}),
|
|
223
|
+
execute: (input) => attempt(() => elementCall(caller, { action: "dateien_draft", perm: "w", input: { path: input.path } }, async () => {
|
|
224
|
+
const found = await fileByPath(spaceId, input.path, who);
|
|
225
|
+
if (!found) {
|
|
226
|
+
throw new Error(`No file "${input.path}" in the folders you may use.`);
|
|
227
|
+
}
|
|
228
|
+
const open = await openDraft(spaceId, found.file.id, who);
|
|
229
|
+
if (open.edit !== "text") {
|
|
230
|
+
throw new Error(`"${input.path}" is not a text file: it can't be written as text.`);
|
|
231
|
+
}
|
|
232
|
+
await saveDraft(userId, spaceId, found.file.id, who, {
|
|
233
|
+
text: input.text,
|
|
234
|
+
...(open.draft ? {} : { base: open.base, baseText: open.baseText ?? undefined }),
|
|
235
|
+
});
|
|
236
|
+
return {
|
|
237
|
+
ok: true,
|
|
238
|
+
note: "Kept as a draft. A person publishes it in Dateien.",
|
|
239
|
+
};
|
|
240
|
+
})),
|
|
241
|
+
});
|
|
242
|
+
}
|
|
138
243
|
return tools;
|
|
244
|
+
/** A file of a mount, read from where it lives: the mount's rules decided before. */
|
|
245
|
+
async function readMounted(path, from) {
|
|
246
|
+
const found = await fileByPath(spaceId, path, who);
|
|
247
|
+
if (!found) {
|
|
248
|
+
throw new Error(`No file "${path}" in the folders you may use.`);
|
|
249
|
+
}
|
|
250
|
+
await caller.note?.({ code: "reads", params: { name: found.file.name } });
|
|
251
|
+
const text = await mountFileText(found.mount, found.file).catch(() => {
|
|
252
|
+
throw new Error(`"${path}" can't be read now: its folder is not there.`);
|
|
253
|
+
});
|
|
254
|
+
if (!text) {
|
|
255
|
+
return { path, text: "", note: `${found.file.mime}: no text of its own.`, length: 0 };
|
|
256
|
+
}
|
|
257
|
+
const start = Math.min(from ?? 0, text.length);
|
|
258
|
+
const part = text.slice(start, start + 12_000);
|
|
259
|
+
return {
|
|
260
|
+
path,
|
|
261
|
+
text: clip(part, 12_000),
|
|
262
|
+
next: start + part.length < text.length ? start + part.length : null,
|
|
263
|
+
length: text.length,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/** The mounts an AI for the caller may write drafts in: changeable, and agents may write. */
|
|
268
|
+
async function draftableMounts(spaceId, who) {
|
|
269
|
+
const ids = (await usableMounts(spaceId, who)).map((m) => m.id);
|
|
270
|
+
const agents = who.filter((s) => s.kind === "agent");
|
|
271
|
+
const ok = await Promise.all(ids.map(async (id) => (await decide(spaceId, agents, { kind: "mount", id }, "w")).ok));
|
|
272
|
+
return writableMountIds(spaceId, ids.filter((_, i) => ok[i]));
|
|
273
|
+
}
|
|
274
|
+
/** The mounts among these whose files may be changed. */
|
|
275
|
+
async function writableMountIds(spaceId, ids) {
|
|
276
|
+
if (!ids.length) {
|
|
277
|
+
return [];
|
|
278
|
+
}
|
|
279
|
+
const rows = await db.query.spaceMount.findMany({
|
|
280
|
+
columns: { id: true },
|
|
281
|
+
where: and(eq(schema.spaceMount.spaceId, spaceId), eq(schema.spaceMount.writable, true), inArray(schema.spaceMount.id, ids)),
|
|
282
|
+
});
|
|
283
|
+
return rows.map((r) => r.id);
|
|
284
|
+
}
|
|
285
|
+
/** A search over the client space and its parent, the best of both by relevance. */
|
|
286
|
+
async function searchAll(spaces, query, opts) {
|
|
287
|
+
if (spaces.length === 1) {
|
|
288
|
+
return searchKnowledge(spaces[0].id, query, opts);
|
|
289
|
+
}
|
|
290
|
+
const each = await Promise.all(spaces.map((p) => searchKnowledge(p.id, query, opts)));
|
|
291
|
+
const limit = Math.min(Math.max(1, opts?.limit ?? 6), 12);
|
|
292
|
+
const hits = each
|
|
293
|
+
.flatMap((found) => found.hits)
|
|
294
|
+
.sort((a, b) => (b.relevance ?? 0) - (a.relevance ?? 0))
|
|
295
|
+
.slice(0, limit);
|
|
296
|
+
return {
|
|
297
|
+
hits,
|
|
298
|
+
checked: each.every((f) => f.checked),
|
|
299
|
+
candidates: each.reduce((n, f) => n + f.candidates, 0),
|
|
300
|
+
filtered: each.some((f) => f.filtered !== null)
|
|
301
|
+
? each.reduce((n, f) => n + (f.filtered ?? 0), 0)
|
|
302
|
+
: null,
|
|
303
|
+
hidden: each
|
|
304
|
+
.flatMap((found) => found.hidden)
|
|
305
|
+
.sort((a, b) => (b.relevance ?? 0) - (a.relevance ?? 0))
|
|
306
|
+
.slice(0, limit),
|
|
307
|
+
};
|
|
139
308
|
}
|
|
@@ -47,7 +47,7 @@ export const ELEMENT_SPECS = {
|
|
|
47
47
|
input: z.object({
|
|
48
48
|
path: z
|
|
49
49
|
.string()
|
|
50
|
-
.describe("The file's path: files/…, as the page's front matter or a hit names it"),
|
|
50
|
+
.describe("The file's path: files/… or dateien/…, as the page's front matter or a hit names it"),
|
|
51
51
|
from: z.number().int().min(0).optional().describe("Character to start at"),
|
|
52
52
|
}),
|
|
53
53
|
},
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { createTool } from "@mastra/core/tools";
|
|
2
|
+
import { eq } from "drizzle-orm";
|
|
2
3
|
import { readableIds } from "../broker/rwx.js";
|
|
4
|
+
import { db, schema } from "../db/client.js";
|
|
3
5
|
import { runReport, startRun } from "../services/runs.js";
|
|
4
6
|
import { listWizards } from "../services/wizards.js";
|
|
5
7
|
import { attempt } from "../tools/shared.js";
|
|
@@ -20,7 +22,15 @@ export async function wizardTools(caller) {
|
|
|
20
22
|
if (!userId) {
|
|
21
23
|
return {};
|
|
22
24
|
}
|
|
23
|
-
|
|
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;
|
|
24
34
|
return {
|
|
25
35
|
wizard_list: createTool({
|
|
26
36
|
id: "wizard_list",
|
|
@@ -43,6 +53,7 @@ export async function wizardTools(caller) {
|
|
|
43
53
|
execute: (input) => attempt(() => elementCall(caller, {
|
|
44
54
|
action: "wizard_run",
|
|
45
55
|
resource: { kind: "wizard", id: input.wizardId },
|
|
56
|
+
rulesIn: spaceId,
|
|
46
57
|
perm: "x",
|
|
47
58
|
input,
|
|
48
59
|
}, async () => {
|
|
@@ -50,7 +61,9 @@ export async function wizardTools(caller) {
|
|
|
50
61
|
if (!published) {
|
|
51
62
|
throw new Error(`No published wizard "${input.wizardId}" in this space.`);
|
|
52
63
|
}
|
|
53
|
-
const { runId, refused } = await startRun(userId, input.wizardId, input.answers
|
|
64
|
+
const { runId, refused } = await startRun(userId, input.wizardId, input.answers, {
|
|
65
|
+
clientSpaceId: inClientSpace ? caller.space.id : null,
|
|
66
|
+
});
|
|
54
67
|
const report = await runReport(userId, runId, input.waitSeconds);
|
|
55
68
|
return { ...report, ...(refused ? { refusedAnswers: refused } : {}) };
|
|
56
69
|
})),
|
|
@@ -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
|
-
|
|
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.
|
|
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),
|
package/apps/runtime/dist/env.js
CHANGED
|
@@ -110,6 +110,12 @@ export const env = {
|
|
|
110
110
|
* file carry it. Default: APP_URL, the same origin.
|
|
111
111
|
*/
|
|
112
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"),
|
|
113
119
|
/** More host names this server answers under, besides APP_URL's and the loopback names. */
|
|
114
120
|
allowedHosts: list("ALLOWED_HOSTS"),
|
|
115
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
|
-
|
|
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
|
|
79
|
-
const uri = found?.
|
|
80
|
-
? `data:${found.
|
|
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) });
|