@engenty/wizards-runtime 0.15.0 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/apps/runtime/dist/agents/studio-assistant.js +138 -9
- package/apps/runtime/dist/agents/website.js +10 -5
- package/apps/runtime/dist/app.js +18 -0
- package/apps/runtime/dist/auth/index.js +5 -0
- package/apps/runtime/dist/broker/account.js +5 -0
- package/apps/runtime/dist/broker/index.js +5 -0
- package/apps/runtime/dist/broker/rwx.js +255 -7
- package/apps/runtime/dist/cli/main.js +7 -0
- package/apps/runtime/dist/cli/move-files.js +84 -0
- package/apps/runtime/dist/cli/move-folder-sources.js +120 -0
- package/apps/runtime/dist/connectors/plugin-connections.js +74 -4
- package/apps/runtime/dist/db/migrations/tenant/0042_client_spaces.sql +16 -0
- package/apps/runtime/dist/db/migrations/tenant/0043_space_member_invite.sql +19 -0
- package/apps/runtime/dist/db/migrations/tenant/0044_client_portal.sql +8 -0
- package/apps/runtime/dist/db/migrations/tenant/0045_client_portal_no_slug.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0046_space_file_hash.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0047_knowledge_folders.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/0048_space_chunk_kind.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/0049_knowledge_sources.sql +57 -0
- package/apps/runtime/dist/db/migrations/tenant/0050_dateien.sql +71 -0
- package/apps/runtime/dist/db/migrations/tenant/0051_dateien_drafts.sql +23 -0
- package/apps/runtime/dist/db/migrations/tenant/0052_dateien_plugin_kinds.sql +5 -0
- package/apps/runtime/dist/db/migrations/tenant/0053_dateien_file_title.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/0054_dateien_ocr.sql +14 -0
- package/apps/runtime/dist/db/migrations/tenant/0055_dateien_doc_reread.sql +3 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0042_snapshot.json +5286 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0043_snapshot.json +5293 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0044_snapshot.json +5344 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0045_snapshot.json +5329 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0046_snapshot.json +5343 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0047_snapshot.json +5391 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0048_snapshot.json +5398 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0049_snapshot.json +5793 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0050_snapshot.json +6288 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0051_snapshot.json +6461 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0052_snapshot.json +6497 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0053_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0054_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0055_snapshot.json +6504 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +98 -0
- package/apps/runtime/dist/db/schema.js +294 -5
- package/apps/runtime/dist/docs/dev/api.md +25 -0
- package/apps/runtime/dist/docs/dev/plugins/connections.md +30 -2
- package/apps/runtime/dist/docs/dev/plugins/index.md +1 -1
- package/apps/runtime/dist/docs/dev/plugins/reference.md +7 -4
- package/apps/runtime/dist/docs/dev/plugins/server.md +11 -5
- package/apps/runtime/dist/docs/dev/plugins/space.md +130 -4
- package/apps/runtime/dist/docs/dev/plugins/studio.md +13 -3
- package/apps/runtime/dist/docs/user-guide/command-line.md +2 -0
- package/apps/runtime/dist/docs/user-guide/plugins.md +1 -1
- package/apps/runtime/dist/docs/user-guide/space.md +6 -6
- package/apps/runtime/dist/docs/user-guide/steps.md +1 -1
- package/apps/runtime/dist/documents/ocr.js +72 -0
- package/apps/runtime/dist/documents/parse.js +90 -12
- package/apps/runtime/dist/elements/caller.js +5 -3
- package/apps/runtime/dist/elements/gate.js +1 -1
- package/apps/runtime/dist/elements/knowledge.js +181 -12
- package/apps/runtime/dist/elements/specs.js +1 -1
- package/apps/runtime/dist/elements/wizards.js +15 -2
- package/apps/runtime/dist/engenty/connections-google/connectors/drive.js +3 -0
- package/apps/runtime/dist/engenty/connections-microsoft/onedrive.js +3 -0
- package/apps/runtime/dist/engenty/connections-s3/s3.js +6 -0
- package/apps/runtime/dist/engine/runner.js +19 -2
- package/apps/runtime/dist/engine/steps.js +1 -1
- package/apps/runtime/dist/env.js +6 -0
- package/apps/runtime/dist/files/blobs.js +57 -1
- package/apps/runtime/dist/files/objects.js +1 -0
- package/apps/runtime/dist/files/pictures.js +41 -0
- package/apps/runtime/dist/files/storage.js +14 -26
- package/apps/runtime/dist/files/thumbs.js +77 -0
- package/apps/runtime/dist/files/upload-check.js +2 -2
- package/apps/runtime/dist/index.js +22 -0
- package/apps/runtime/dist/live/views.js +2 -1
- package/apps/runtime/dist/manage.js +4 -1
- package/apps/runtime/dist/mcp/tools.js +5 -0
- package/apps/runtime/dist/models.js +82 -13
- package/apps/runtime/dist/mounts/cache.js +71 -0
- package/apps/runtime/dist/mounts/drafts.js +367 -0
- package/apps/runtime/dist/mounts/folder.js +148 -0
- package/apps/runtime/dist/mounts/mounts.js +1315 -0
- package/apps/runtime/dist/mounts/source.js +179 -0
- package/apps/runtime/dist/mounts/units.js +97 -0
- package/apps/runtime/dist/move-files.js +4 -0
- package/apps/runtime/dist/plugins/loader.js +73 -1
- package/apps/runtime/dist/plugins/registry.js +2 -0
- package/apps/runtime/dist/prompts/goose.md +13 -1
- package/apps/runtime/dist/prompts/tools.md +5 -2
- package/apps/runtime/dist/render/convert.js +188 -1
- package/apps/runtime/dist/routes/api.js +6 -1
- package/apps/runtime/dist/routes/client-contract.js +164 -0
- package/apps/runtime/dist/routes/client-spaces.js +58 -0
- package/apps/runtime/dist/routes/dateien.js +173 -0
- package/apps/runtime/dist/routes/elements.js +2 -2
- package/apps/runtime/dist/routes/plugins.js +2 -1
- package/apps/runtime/dist/routes/runs.js +24 -10
- package/apps/runtime/dist/routes/studio.js +168 -8
- package/apps/runtime/dist/secrets/signing.js +16 -0
- package/apps/runtime/dist/services/client-portal.js +439 -0
- package/apps/runtime/dist/services/client-spaces.js +228 -0
- package/apps/runtime/dist/services/cloud.js +2 -0
- package/apps/runtime/dist/services/knowledge-model.js +24 -8
- package/apps/runtime/dist/services/knowledge.js +72 -42
- package/apps/runtime/dist/services/links.js +17 -0
- package/apps/runtime/dist/services/members-studio.js +5 -1
- package/apps/runtime/dist/services/page-pictures.js +162 -0
- package/apps/runtime/dist/services/runs.js +22 -6
- package/apps/runtime/dist/services/space-categories.js +41 -5
- package/apps/runtime/dist/services/space-data.js +165 -8
- package/apps/runtime/dist/services/space-files.js +102 -19
- package/apps/runtime/dist/services/space-index.js +384 -41
- package/apps/runtime/dist/services/space-origin.js +25 -1
- package/apps/runtime/dist/services/space-sources.js +937 -0
- package/apps/runtime/dist/services/spaces.js +60 -12
- package/apps/runtime/dist/services/synced-spaces.js +10 -1
- package/apps/runtime/dist/services/work-ai.js +29 -8
- package/apps/runtime/dist/services/work-chats.js +3 -2
- package/apps/runtime/dist/services/work-files.js +25 -1
- package/apps/runtime/dist/sharing/index.js +2 -0
- package/apps/runtime/dist/store/index.js +29 -8
- package/apps/runtime/dist/store/shared-lists.js +30 -17
- package/apps/runtime/dist/sync/records.js +22 -27
- package/apps/runtime/dist/texts.js +4 -0
- package/apps/runtime/dist/tools/pages.js +1 -1
- package/apps/runtime/dist/widgets/step.js +2 -1
- package/apps/web/dist/assets/AboutDialog-C9SnsGno.js +1 -0
- package/apps/web/dist/assets/BlockEditor-CUYFH_CU.js +173 -0
- package/apps/web/dist/assets/BlockEditor-CtW6lw0y.css +1 -0
- package/apps/web/dist/assets/CapabilitiesPage-DvSSybt0.js +2 -0
- package/apps/web/dist/assets/{ChangelogDialog-Do7pwmFv.js → ChangelogDialog-B28TK333.js} +1 -1
- package/apps/web/dist/assets/{CodeEditor-Cdox-51M.js → CodeEditor-D-Y16fdk.js} +1 -1
- package/apps/web/dist/assets/EditorPage-Duu8swX6.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-BPS86xzA.js → FlowPreview-DXnk7m9P.js} +1 -1
- package/apps/web/dist/assets/{LangSwitch-BRHbi_Sd.js → LangSwitch-Dkd0FBIH.js} +1 -1
- package/apps/web/dist/assets/Marketplace-Cty40VwG.js +2 -0
- package/apps/web/dist/assets/NewWizardChannels-B12l2_Jh.js +1 -0
- package/apps/web/dist/assets/NewWizardPage-B10K8vND.js +7 -0
- package/apps/web/dist/assets/OnboardingPage-D3SEpTmt.js +1 -0
- package/apps/web/dist/assets/PreviewPane-DhhQ06Ic.js +7 -0
- package/apps/web/dist/assets/PublicRunner-B8ncIxIP.js +1 -0
- package/apps/web/dist/assets/ReadOnly-DwewuYi8.js +1 -0
- package/apps/web/dist/assets/SettingsPage-C-gGIJLJ.js +2 -0
- package/apps/web/dist/assets/SetupPage-CdsrHW-w.js +1 -0
- package/apps/web/dist/assets/{SharePage-PKHX8a8a.js → SharePage-CZClatA_.js} +1 -1
- package/apps/web/dist/assets/SkillEditor-BaEiqqXl.js +1 -0
- package/apps/web/dist/assets/Skills-Bv6XbKxc.js +9 -0
- package/apps/web/dist/assets/SpacePage-CYhxcjj0.js +2 -0
- package/apps/web/dist/assets/UpdateCheck-CLPUsKVT.js +2 -0
- package/apps/web/dist/assets/changelog-y5oxYzPd.js +1 -0
- package/apps/web/dist/assets/circle-check-DDLzlcXP.js +1 -0
- package/apps/web/dist/assets/code-DsnwzN1x.js +1 -0
- package/apps/web/dist/assets/code-editor-Ca5EAxmd.js +2 -0
- package/apps/web/dist/assets/code-xml-S4IFi5Jc.js +1 -0
- package/apps/web/dist/assets/connection-sign-in-DECedzrs.js +1 -0
- package/apps/web/dist/assets/{dist-H1UN89OA.js → dist-B3Ray2U5.js} +1 -1
- package/apps/web/dist/assets/dist-CQpLJoQZ.js +10 -0
- package/apps/web/dist/assets/{dist-GlRRx9Xl.js → dist-D1uGQ_tB.js} +1 -1
- package/apps/web/dist/assets/{dist-BdDd1FxP.js → dist-DFI-tsfx.js} +1 -1
- package/apps/web/dist/assets/eye-TJSuiHHO.js +1 -0
- package/apps/web/dist/assets/floating-ui.dom-pwdNGv4b.js +1 -0
- package/apps/web/dist/assets/host-CJdLkzfY.js +125 -0
- package/apps/web/dist/assets/i18n-BBzMbGoo.js +55 -0
- package/apps/web/dist/assets/index-CmonJZHC.js +10 -0
- package/apps/web/dist/assets/index-w-3J3ZJ9.css +1 -0
- package/apps/web/dist/assets/{json-CEsfHdlZ.js → json-Ce3iHrtf.js} +1 -1
- package/apps/web/dist/assets/link-CPL8pB1P.js +1 -0
- package/apps/web/dist/assets/list-checks-DaxmUPqK.js +1 -0
- package/apps/web/dist/assets/{markdown-5-I9_izp.js → markdown-hqKQy2pp.js} +1 -1
- package/apps/web/dist/assets/marked.esm-DQ7XA7AC.js +67 -0
- package/apps/web/dist/assets/oss-credits-BrwV0a4F.js +1 -0
- package/apps/web/dist/assets/pdf-Dg-raaUo.js +54 -0
- package/apps/web/dist/assets/pdf.worker.min-68CMzziE.js +1 -0
- package/apps/web/dist/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
- package/apps/web/dist/assets/phone-B4uKDBFq.js +1 -0
- package/apps/web/dist/assets/read-aloud-Bfp9_bUY.js +1 -0
- package/apps/web/dist/assets/{runners-BoPaPsjN.js → runners-CUpBpSji.js} +1 -1
- package/apps/web/dist/assets/scroll-text-BEnPzQEi.js +1 -0
- package/apps/web/dist/assets/session-CMQo8P-G.js +2 -0
- package/apps/web/dist/assets/table-5B9G3oA6.js +1 -0
- package/apps/web/dist/assets/theme-D6KUBJnh.js +11 -0
- package/apps/web/dist/assets/ui-BCfu5sia.js +16 -0
- package/apps/web/dist/assets/upload-CAlHC232.js +1 -0
- package/apps/web/dist/assets/{useMutation-7_FOHoEl.js → useMutation-1fV4Nnxq.js} +1 -1
- package/apps/web/dist/assets/viewers-ChriFcRy.js +2 -0
- package/apps/web/dist/assets/volume-2-BqlBtCIJ.js +1 -0
- package/apps/web/dist/assets/w3c-keyname-BOAvb0qz.js +1 -0
- package/apps/web/dist/assets/xlsx-CuSbHU4R.js +141 -0
- package/apps/web/dist/index.html +19 -14
- package/apps/web/dist/mcp/flow.html +2 -2
- package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +223 -4
- package/node_modules/@engenty/wizards/dist/plugin/mount.d.ts +123 -0
- package/node_modules/@engenty/wizards/dist/plugin/mount.js +7 -0
- package/node_modules/@engenty/wizards/dist/plugin/studio.d.ts +110 -1
- package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +1 -1
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +24 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +24 -0
- package/node_modules/@engenty/wizards-shared/dist/knowledge.d.ts +33 -4
- package/node_modules/@engenty/wizards-shared/dist/run.d.ts +3 -0
- package/node_modules/@engenty/wizards-shared/dist/run.js +6 -0
- package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +21 -4
- package/node_modules/@engenty/wizards-shared/dist/rwx.js +27 -3
- package/node_modules/@engenty/wizards-shared/dist/spaces.d.ts +11 -1
- package/node_modules/@engenty/wizards-shared/dist/spaces.js +9 -2
- package/node_modules/@engenty/wizards-shared/dist/text-merge.d.ts +6 -0
- package/node_modules/@engenty/wizards-shared/dist/text-merge.js +25 -0
- package/package.json +3 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/plugin.json +1 -1
- package/apps/web/dist/assets/AboutDialog-DaWEIHFN.js +0 -1
- package/apps/web/dist/assets/CapabilitiesPage-Dt91U_sK.js +0 -2
- package/apps/web/dist/assets/EditorPage-DS_a5K3s.js +0 -3
- package/apps/web/dist/assets/Marketplace-CS7JTgiv.js +0 -2
- package/apps/web/dist/assets/NewWizardChannels-D-OLsex6.js +0 -1
- package/apps/web/dist/assets/NewWizardPage-BM_pkk41.js +0 -7
- package/apps/web/dist/assets/OnboardingPage-Cpn5pubC.js +0 -1
- package/apps/web/dist/assets/PreviewPane-BOk1-yO4.js +0 -9
- package/apps/web/dist/assets/PublicRunner-CgzMZb0r.js +0 -1
- package/apps/web/dist/assets/ReadOnly-8DEx-ly3.js +0 -1
- package/apps/web/dist/assets/SettingsPage-CcrknFcI.js +0 -2
- package/apps/web/dist/assets/SetupPage-D5nzjgPU.js +0 -1
- package/apps/web/dist/assets/SkillEditor-BsXTqNli.js +0 -1
- package/apps/web/dist/assets/Skills-C_X1ZsKg.js +0 -9
- package/apps/web/dist/assets/SpacePage-wZGqUoSQ.js +0 -2
- package/apps/web/dist/assets/UpdateCheck-D6x7MqEl.js +0 -2
- package/apps/web/dist/assets/changelog-M-eOBqgu.js +0 -1
- package/apps/web/dist/assets/circle-alert-DJbCyxcg.js +0 -1
- package/apps/web/dist/assets/circle-check-jqtsKi_b.js +0 -1
- package/apps/web/dist/assets/code-editor-BpCEnf8H.js +0 -2
- package/apps/web/dist/assets/code-xml-q88cSTV8.js +0 -1
- package/apps/web/dist/assets/connection-sign-in-UJ6xk-TD.js +0 -1
- package/apps/web/dist/assets/dist-EBVTpvRr.js +0 -10
- package/apps/web/dist/assets/eye-CgkvVlba.js +0 -1
- package/apps/web/dist/assets/host-Bd_bon_c.js +0 -118
- package/apps/web/dist/assets/i18n-0fUKyVRE.js +0 -55
- package/apps/web/dist/assets/index-D18DvnxX.css +0 -1
- package/apps/web/dist/assets/index-DOgwxnto.js +0 -10
- package/apps/web/dist/assets/oss-credits-IdX5bc71.js +0 -1
- package/apps/web/dist/assets/phone-CjPvZ0nk.js +0 -1
- package/apps/web/dist/assets/read-aloud-CLXo_asN.js +0 -1
- package/apps/web/dist/assets/scroll-text-2BhJO_W7.js +0 -1
- package/apps/web/dist/assets/session-CpYa5hwd.js +0 -2
- package/apps/web/dist/assets/theme-Bj3wNQR0.js +0 -11
- package/apps/web/dist/assets/ui-CktezodE.js +0 -82
- package/apps/web/dist/assets/upload-CHGCFVZK.js +0 -1
- package/apps/web/dist/assets/volume-2-Bw5KASy8.js +0 -1
|
@@ -15,7 +15,7 @@ import { currentTenant } from "../tenants/tenant.js";
|
|
|
15
15
|
import { ServiceError } from "./errors.js";
|
|
16
16
|
import { freeSlug, pageTree } from "./knowledge.js";
|
|
17
17
|
import { categoryRow, categoryRows, numberOf, setItemCategories, } from "./space-categories.js";
|
|
18
|
-
import { keepText, queueIndex } from "./space-index.js";
|
|
18
|
+
import { keepText, queueIndex, seenAsPicture } from "./space-index.js";
|
|
19
19
|
const XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
20
20
|
// --- tables from sheets ------------------------------------------------------------------
|
|
21
21
|
const NUMBER = /^-?[\d\s.,']+\s*(€|eur|%|kg|t|h|mm|cm|m|km|stk\.?|stück)?$/i;
|
|
@@ -223,7 +223,8 @@ function titleOf(markdown, name) {
|
|
|
223
223
|
/** Why a person should compare a read scan with its original; null when it read well. */
|
|
224
224
|
function reviewOf(parsed, markdown) {
|
|
225
225
|
const illegible = (markdown.match(/\[illegible\]/gi) ?? []).length;
|
|
226
|
-
|
|
226
|
+
const scan = parsed.via === "vision" || parsed.via === "ocr";
|
|
227
|
+
if (scan && (illegible >= 3 || markdown.replace(/\s/g, "").length < 200)) {
|
|
227
228
|
return "Hard to read: please compare with the original.";
|
|
228
229
|
}
|
|
229
230
|
return null;
|
|
@@ -259,12 +260,24 @@ export async function dropConverted(fileId) {
|
|
|
259
260
|
/**
|
|
260
261
|
* Reads an uploaded document into Knowledge: a table per sheet of a workbook, else a page — with
|
|
261
262
|
* sub-pages when it is long and has headings of its own. The file stays as the original. A file
|
|
262
|
-
* nothing can read stays a file; UnreadableDocument says why.
|
|
263
|
+
* nothing can read stays a file; UnreadableDocument says why. So does a picture the index embeds
|
|
264
|
+
* as a picture.
|
|
263
265
|
*/
|
|
264
|
-
export async function convertDocument(
|
|
266
|
+
export async function convertDocument(row, data) {
|
|
267
|
+
// Where the file stands now: an upload into a folder is moved there while it is read.
|
|
268
|
+
const place = await db.query.spaceFile.findFirst({
|
|
269
|
+
where: eq(schema.spaceFile.id, row.id),
|
|
270
|
+
columns: { parentId: true, position: true },
|
|
271
|
+
});
|
|
272
|
+
const file = { ...row, ...place };
|
|
265
273
|
const mime = documentMime(file.name, file.mime);
|
|
266
274
|
await dropConverted(file.id);
|
|
267
275
|
const items = [];
|
|
276
|
+
// Where the embedding model sees pictures, a picture is found by what it shows: it stays a
|
|
277
|
+
// file, and no vision model writes a page of what is in it.
|
|
278
|
+
if (await seenAsPicture(mime)) {
|
|
279
|
+
return { text: "", textHash: null, pages: null, chars: 0, items };
|
|
280
|
+
}
|
|
268
281
|
if (mime === XLSX || mime === "text/csv") {
|
|
269
282
|
const sheets = await sheetsOf(data, mime);
|
|
270
283
|
const base = titleOf("", file.name);
|
|
@@ -282,6 +295,9 @@ export async function convertDocument(file, data) {
|
|
|
282
295
|
title,
|
|
283
296
|
slug: await freeSlug(file.spaceId, title, "table"),
|
|
284
297
|
columns,
|
|
298
|
+
// Where the file stands: what it was read into stands there in its place.
|
|
299
|
+
parentId: file.parentId,
|
|
300
|
+
position: file.position,
|
|
285
301
|
fileId: file.id,
|
|
286
302
|
origin: file.origin,
|
|
287
303
|
originLabel: file.originLabel,
|
|
@@ -320,8 +336,8 @@ export async function convertDocument(file, data) {
|
|
|
320
336
|
const id = await insertPage(file, {
|
|
321
337
|
title,
|
|
322
338
|
markdown,
|
|
323
|
-
parentId:
|
|
324
|
-
position:
|
|
339
|
+
parentId: file.parentId,
|
|
340
|
+
position: file.position,
|
|
325
341
|
review,
|
|
326
342
|
});
|
|
327
343
|
queueIndex(`p:${id}`);
|
|
@@ -336,8 +352,8 @@ export async function convertDocument(file, data) {
|
|
|
336
352
|
const parentId = await insertPage(file, {
|
|
337
353
|
title,
|
|
338
354
|
markdown: split.intro,
|
|
339
|
-
parentId:
|
|
340
|
-
position:
|
|
355
|
+
parentId: file.parentId,
|
|
356
|
+
position: file.position,
|
|
341
357
|
review,
|
|
342
358
|
});
|
|
343
359
|
const ids = [];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { formatTableCell } from "@engenty/wizards-shared/engenty/data-tables";
|
|
2
2
|
import { slugOf, } from "@engenty/wizards-shared/knowledge";
|
|
3
|
-
import { and, asc, count, eq, inArray, isNotNull, isNull } from "drizzle-orm";
|
|
3
|
+
import { and, asc, count, eq, inArray, isNotNull, isNull, sql } from "drizzle-orm";
|
|
4
4
|
import { assertRwx, pageResources, readable, tableResources } from "../broker/rwx.js";
|
|
5
5
|
import { db, schema } from "../db/client.js";
|
|
6
6
|
import { embeddingModel, systemOneAccess } from "../models.js";
|
|
@@ -181,9 +181,43 @@ async function readableItems(spaceId, who, items) {
|
|
|
181
181
|
: { kind: i.kind, id: i.id, fileId: i.fileId }));
|
|
182
182
|
return items.filter((i) => ok.has(i.id));
|
|
183
183
|
}
|
|
184
|
+
/** About four lines of a card. */
|
|
185
|
+
const EXCERPT_CHARS = 320;
|
|
184
186
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
+
* The start of a text as plain words, for a card: without Markdown's marks, pictures, links'
|
|
188
|
+
* addresses, tables, task boxes and a first heading that only repeats the title; null when
|
|
189
|
+
* nothing is left.
|
|
190
|
+
*/
|
|
191
|
+
export function excerptOf(markdown, title) {
|
|
192
|
+
const lines = (markdown ?? "")
|
|
193
|
+
.replace(/<[^>]+>/g, " ")
|
|
194
|
+
.replace(/!\[[^\]]*\]\([^)]*\)/g, " ")
|
|
195
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
196
|
+
.split("\n")
|
|
197
|
+
// A table's rows say little out of their table.
|
|
198
|
+
.filter((line) => !line.trimStart().startsWith("|"))
|
|
199
|
+
.map((line) => line
|
|
200
|
+
.replace(/^\s{0,3}(#{1,6}|>|[-*+]|\d+\.)\s+/, "")
|
|
201
|
+
.replace(/^\[[ xX]\]\s+/, "")
|
|
202
|
+
.replace(/[*_`~|]/g, "")
|
|
203
|
+
.trim())
|
|
204
|
+
.filter((line) => line && !/^[-:=\s]+$/.test(line));
|
|
205
|
+
if (lines[0]?.toLowerCase() === title.trim().toLowerCase()) {
|
|
206
|
+
lines.shift();
|
|
207
|
+
}
|
|
208
|
+
const text = lines.join(" ").replace(/\s+/g, " ").trim();
|
|
209
|
+
if (!text) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
if (text.length <= EXCERPT_CHARS) {
|
|
213
|
+
return text;
|
|
214
|
+
}
|
|
215
|
+
const cut = text.slice(0, EXCERPT_CHARS);
|
|
216
|
+
return `${cut.slice(0, cut.lastIndexOf(" ") > 200 ? cut.lastIndexOf(" ") : EXCERPT_CHARS)} …`;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Everything Knowledge holds that `who` may read, as a tree: pages with their sub-pages, tables
|
|
220
|
+
* and files, each with the page it sits in (`parentId`) and its place there; and categories.
|
|
187
221
|
*/
|
|
188
222
|
export async function listKnowledge(userId, spaceId, who) {
|
|
189
223
|
const space = await ownedSpace(userId, spaceId);
|
|
@@ -213,16 +247,26 @@ export async function listKnowledge(userId, spaceId, who) {
|
|
|
213
247
|
const tPaths = await tablePaths(space.id);
|
|
214
248
|
const fPaths = await filePaths(space.id);
|
|
215
249
|
const pages = await db.query.spacePage.findMany({
|
|
216
|
-
where: and(eq(schema.spacePage.spaceId, space.id), isNull(schema.spacePage.wizardId)
|
|
250
|
+
where: and(eq(schema.spacePage.spaceId, space.id), isNull(schema.spacePage.wizardId)),
|
|
217
251
|
columns: { markdown: false },
|
|
252
|
+
extras: {
|
|
253
|
+
blank: sql `length(trim(${schema.spacePage.markdown})) = 0`.as("blank"),
|
|
254
|
+
head: sql `substr(${schema.spacePage.markdown}, 1, 2000)`.as("head"),
|
|
255
|
+
},
|
|
218
256
|
});
|
|
257
|
+
const loose = files.filter((f) => !converted.has(f.id));
|
|
258
|
+
// What each page holds: its sub-pages, and the tables and files in it.
|
|
259
|
+
const inside = (id) => (tree.has(id) ? id : null);
|
|
219
260
|
const children = new Map();
|
|
220
|
-
for (const
|
|
221
|
-
|
|
222
|
-
|
|
261
|
+
for (const parent of [
|
|
262
|
+
...[...tree.values()].map((n) => n.parentId),
|
|
263
|
+
...tables.map((t) => t.parentId && inside(t.parentId)),
|
|
264
|
+
...loose.map((f) => f.parentId && inside(f.parentId)),
|
|
265
|
+
]) {
|
|
266
|
+
if (parent) {
|
|
267
|
+
children.set(parent, (children.get(parent) ?? 0) + 1);
|
|
223
268
|
}
|
|
224
269
|
}
|
|
225
|
-
const loose = files.filter((f) => !converted.has(f.id));
|
|
226
270
|
const cats = await itemCategoriesOf([
|
|
227
271
|
...pages.map((p) => ({ pageId: p.id })),
|
|
228
272
|
...tables.map((t) => ({ tableId: t.id })),
|
|
@@ -236,6 +280,10 @@ export async function listKnowledge(userId, spaceId, who) {
|
|
|
236
280
|
title: p.title,
|
|
237
281
|
path: tree.get(p.id)?.path ?? "",
|
|
238
282
|
children: children.get(p.id) ?? 0,
|
|
283
|
+
parentId: tree.get(p.id)?.parentId ?? null,
|
|
284
|
+
position: p.position,
|
|
285
|
+
blank: Boolean(p.blank),
|
|
286
|
+
excerpt: excerptOf(p.head, p.title),
|
|
239
287
|
rows: null,
|
|
240
288
|
format: null,
|
|
241
289
|
origin: p.origin,
|
|
@@ -254,6 +302,10 @@ export async function listKnowledge(userId, spaceId, who) {
|
|
|
254
302
|
title: t.title,
|
|
255
303
|
path: tPaths.get(t.id) ?? "",
|
|
256
304
|
children: 0,
|
|
305
|
+
parentId: t.parentId && inside(t.parentId),
|
|
306
|
+
position: t.position,
|
|
307
|
+
blank: false,
|
|
308
|
+
excerpt: null,
|
|
257
309
|
rows: rowCounts.find((r) => r.tableId === t.id)?.n ?? 0,
|
|
258
310
|
format: t.format,
|
|
259
311
|
origin: t.origin,
|
|
@@ -272,6 +324,10 @@ export async function listKnowledge(userId, spaceId, who) {
|
|
|
272
324
|
title: f.name,
|
|
273
325
|
path: fPaths.get(f.id) ?? "",
|
|
274
326
|
children: 0,
|
|
327
|
+
parentId: f.parentId && inside(f.parentId),
|
|
328
|
+
position: f.position,
|
|
329
|
+
blank: false,
|
|
330
|
+
excerpt: excerptOf(f.description, f.name),
|
|
275
331
|
rows: null,
|
|
276
332
|
format: null,
|
|
277
333
|
origin: f.origin,
|
|
@@ -346,34 +402,6 @@ async function itemsHaving(userId, spaceId, having, who) {
|
|
|
346
402
|
const rowIds = having.flatMap((h) => (h.rowId ? [h.rowId] : []));
|
|
347
403
|
const all = await listKnowledge(userId, space.id, who);
|
|
348
404
|
const tree = await pageTree(space.id);
|
|
349
|
-
const subPages = pageIds.filter((id) => !all.items.some((i) => i.key === `p:${id}`));
|
|
350
|
-
const subItems = [];
|
|
351
|
-
if (subPages.length) {
|
|
352
|
-
const cats = await itemCategoriesOf(subPages.map((pageId) => ({ pageId })));
|
|
353
|
-
for (const page of await db.query.spacePage.findMany({
|
|
354
|
-
where: inArray(schema.spacePage.id, subPages),
|
|
355
|
-
columns: { markdown: false },
|
|
356
|
-
})) {
|
|
357
|
-
subItems.push({
|
|
358
|
-
key: `p:${page.id}`,
|
|
359
|
-
kind: "page",
|
|
360
|
-
id: page.id,
|
|
361
|
-
title: tree.get(page.id)?.titles.join(" › ") ?? page.title,
|
|
362
|
-
path: tree.get(page.id)?.path ?? "",
|
|
363
|
-
children: [...tree.values()].filter((n) => n.parentId === page.id).length,
|
|
364
|
-
rows: null,
|
|
365
|
-
format: null,
|
|
366
|
-
origin: page.origin,
|
|
367
|
-
originLabel: page.originLabel,
|
|
368
|
-
kept: page.kept,
|
|
369
|
-
review: page.review,
|
|
370
|
-
fileId: page.fileId,
|
|
371
|
-
status: "ready",
|
|
372
|
-
categories: cats.get(`p:${page.id}`) ?? [],
|
|
373
|
-
updatedAt: page.updatedAt.toISOString(),
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
405
|
const rowTables = new Map();
|
|
378
406
|
for (let i = 0; i < rowIds.length; i += 500) {
|
|
379
407
|
for (const r of await db.query.spaceTableRow.findMany({
|
|
@@ -391,12 +419,14 @@ async function itemsHaving(userId, spaceId, having, who) {
|
|
|
391
419
|
}
|
|
392
420
|
}
|
|
393
421
|
return {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
422
|
+
// A sub-page says where it stands, beside its title: „Handbuch › Montage“.
|
|
423
|
+
items: all.items
|
|
424
|
+
.filter((i) => (i.kind === "page" && pageIds.includes(i.id)) ||
|
|
425
|
+
(i.kind === "table" && tableIds.includes(i.id)) ||
|
|
426
|
+
(i.kind === "file" && fileIds.includes(i.id)))
|
|
427
|
+
.map((i) => i.kind === "page" && i.parentId
|
|
428
|
+
? { ...i, place: tree.get(i.id)?.titles.slice(0, -1).join(" › ") }
|
|
429
|
+
: i),
|
|
400
430
|
rows: [...rowTables].map(([tableId, n]) => ({
|
|
401
431
|
tableId,
|
|
402
432
|
title: all.items.find((i) => i.key === `t:${tableId}`)?.title ?? "",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { eq } from "drizzle-orm";
|
|
2
2
|
import { db, schema } from "../db/client.js";
|
|
3
|
+
import { removeAssetFiles } from "../files/storage.js";
|
|
3
4
|
import { dropLinks, dropRuns } from "../tenants/control.js";
|
|
4
5
|
/** Takes a wizard's public tokens and its runs out of the control database. */
|
|
5
6
|
export async function forgetWizardLinks(wizardId) {
|
|
@@ -14,3 +15,19 @@ export async function forgetWizardLinks(wizardId) {
|
|
|
14
15
|
]);
|
|
15
16
|
await dropRuns(runs.map((r) => r.id));
|
|
16
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Takes the runs of a client space away with what they made: they belong to it, though their
|
|
20
|
+
* wizards stay the parent's (docs/plans/client-portal.md).
|
|
21
|
+
*/
|
|
22
|
+
export async function removeClientSpaceRuns(spaceId) {
|
|
23
|
+
const runs = await db
|
|
24
|
+
.select({ id: schema.run.id, shareToken: schema.run.shareToken })
|
|
25
|
+
.from(schema.run)
|
|
26
|
+
.where(eq(schema.run.clientSpaceId, spaceId));
|
|
27
|
+
for (const { id } of runs) {
|
|
28
|
+
await removeAssetFiles(id);
|
|
29
|
+
}
|
|
30
|
+
await db.delete(schema.run).where(eq(schema.run.clientSpaceId, spaceId));
|
|
31
|
+
await dropLinks(runs.map((r) => r.shareToken).filter((t) => Boolean(t)));
|
|
32
|
+
await dropRuns(runs.map((r) => r.id));
|
|
33
|
+
}
|
|
@@ -77,8 +77,12 @@ async function spaceOfItem(kind, id, sub) {
|
|
|
77
77
|
case "runs": {
|
|
78
78
|
const run = await db.query.run.findFirst({
|
|
79
79
|
where: and(eq(schema.run.id, id), eq(schema.run.tenantId, tenant)),
|
|
80
|
-
columns: { wizardId: true },
|
|
80
|
+
columns: { wizardId: true, clientSpaceId: true },
|
|
81
81
|
});
|
|
82
|
+
// A run of a client space is the client space's, though its wizard is the parent's.
|
|
83
|
+
if (run?.clientSpaceId) {
|
|
84
|
+
return run.clientSpaceId;
|
|
85
|
+
}
|
|
82
86
|
return run ? spaceOfItem("wizards", run.wizardId) : null;
|
|
83
87
|
}
|
|
84
88
|
case "tables":
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pictures of a page a plugin writes (`server.spaceData.putPage` `images`), copied into the
|
|
3
|
+
* space: each fetched once, its bytes kept once by their hash, the Markdown pointed at the copy as
|
|
4
|
+
* `asset://ID`, the outside address kept as the file's `source`. A copy is a space file of the
|
|
5
|
+
* kind `picture`: found in the search by what it shows, shown in its page, no item of Wissen's
|
|
6
|
+
* tree. One no page shows any more goes with the plugin's next write.
|
|
7
|
+
* docs/plans/media-and-files.md, section 1.
|
|
8
|
+
*/
|
|
9
|
+
import { featureOn } from "@engenty/wizards-shared/features";
|
|
10
|
+
import { and, eq, inArray, sql } from "drizzle-orm";
|
|
11
|
+
import { nanoid } from "nanoid";
|
|
12
|
+
import { db, schema } from "../db/client.js";
|
|
13
|
+
import { currentFeatures } from "../features.js";
|
|
14
|
+
import { blobThumb, putBlob } from "../files/blobs.js";
|
|
15
|
+
import { checkUpload, pictureSize } from "../files/upload-check.js";
|
|
16
|
+
import { fetchWebImage } from "../files/web-images.js";
|
|
17
|
+
import { describePicture, removeOriginFiles } from "./space-files.js";
|
|
18
|
+
import { queueIndex, seenAsPicture } from "./space-index.js";
|
|
19
|
+
/** Pictures one page keeps, and below what a picture is a button or a bullet, not content. */
|
|
20
|
+
const MAX_PICTURES = 30;
|
|
21
|
+
const MIN_BYTES = 10_000;
|
|
22
|
+
const MIN_SIDE = 200;
|
|
23
|
+
const PARALLEL = 4;
|
|
24
|
+
const ASSET_REF = /asset:\/\/([A-Za-z0-9_-]{6,40})/g;
|
|
25
|
+
/** The ids of the space's files a page's Markdown shows as `asset://ID`. */
|
|
26
|
+
export const shownFiles = (markdown) => new Set([...markdown.matchAll(ASSET_REF)].map((m) => m[1]));
|
|
27
|
+
/** Whether a plugin's pages keep copies of their pictures: the switch `knowledgeMedia`. */
|
|
28
|
+
export async function copiesPictures() {
|
|
29
|
+
return featureOn(await currentFeatures(), "knowledgeMedia");
|
|
30
|
+
}
|
|
31
|
+
/** One picture from the web into the space, or the copy it has of the same bytes; null when it is none. */
|
|
32
|
+
async function copyPicture(origin, spaceId, label, image, signal) {
|
|
33
|
+
const fetched = await fetchWebImage(image.url, signal);
|
|
34
|
+
if (!fetched || fetched.data.byteLength < MIN_BYTES || fetched.mime === "image/svg+xml") {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const size = pictureSize(fetched.data, fetched.mime);
|
|
38
|
+
if (!size || Math.min(size.width, size.height) < MIN_SIDE) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const name = decodeURIComponent(new URL(image.url).pathname.split("/").pop() || "bild");
|
|
42
|
+
// The bytes say what it is; a picture is drawn again without its metadata.
|
|
43
|
+
const checked = await checkUpload({
|
|
44
|
+
name,
|
|
45
|
+
mime: fetched.mime,
|
|
46
|
+
data: fetched.data,
|
|
47
|
+
fallbackName: "bild",
|
|
48
|
+
}).catch(() => null);
|
|
49
|
+
if (!checked?.mime.startsWith("image/")) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const hash = await putBlob(checked.data);
|
|
53
|
+
const originKey = `picture:${hash}`;
|
|
54
|
+
const scope = and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.origin, origin), eq(schema.spaceFile.originKey, originKey));
|
|
55
|
+
const had = await db.query.spaceFile.findFirst({ where: scope, columns: { id: true } });
|
|
56
|
+
if (had) {
|
|
57
|
+
return had.id;
|
|
58
|
+
}
|
|
59
|
+
const seen = await seenAsPicture(checked.mime);
|
|
60
|
+
const [row] = await db
|
|
61
|
+
.insert(schema.spaceFile)
|
|
62
|
+
.values({
|
|
63
|
+
id: nanoid(16),
|
|
64
|
+
spaceId,
|
|
65
|
+
kind: "picture",
|
|
66
|
+
name: checked.name,
|
|
67
|
+
mime: checked.mime,
|
|
68
|
+
size: checked.data.byteLength,
|
|
69
|
+
hash,
|
|
70
|
+
description: image.alt?.trim().slice(0, 600) ?? "",
|
|
71
|
+
source: image.url,
|
|
72
|
+
origin,
|
|
73
|
+
originKey,
|
|
74
|
+
originLabel: label,
|
|
75
|
+
// Where the embedding model doesn't see pictures, a sentence says what it shows first.
|
|
76
|
+
status: seen ? "ready" : "pending",
|
|
77
|
+
})
|
|
78
|
+
.onConflictDoNothing()
|
|
79
|
+
.returning();
|
|
80
|
+
if (!row) {
|
|
81
|
+
// Another page of the same run kept the same bytes a moment before.
|
|
82
|
+
return ((await db.query.spaceFile.findFirst({ where: scope, columns: { id: true } }))?.id ?? null);
|
|
83
|
+
}
|
|
84
|
+
void blobThumb(hash, row.mime).catch(() => null);
|
|
85
|
+
if (seen) {
|
|
86
|
+
queueIndex(`f:${row.id}`);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
describePicture(row);
|
|
90
|
+
}
|
|
91
|
+
return row.id;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Copies the pictures of a page into the space and points its Markdown at the copies:
|
|
95
|
+
* `` becomes ``. A picture the Markdown doesn't show is added
|
|
96
|
+
* at its end. A picture copied for another page of the plugin before (the same address, or the
|
|
97
|
+
* same bytes) is not fetched again. What can't be fetched, or is an icon, stays as it was.
|
|
98
|
+
*/
|
|
99
|
+
export async function copyPagePictures(origin, input) {
|
|
100
|
+
const list = [
|
|
101
|
+
...new Map(input.images.filter((i) => /^https?:\/\//i.test(i.url)).map((i) => [i.url, i])).values(),
|
|
102
|
+
].slice(0, MAX_PICTURES);
|
|
103
|
+
if (!list.length) {
|
|
104
|
+
return input.markdown;
|
|
105
|
+
}
|
|
106
|
+
const label = input.label?.trim().slice(0, 120) || null;
|
|
107
|
+
const ids = new Map();
|
|
108
|
+
const known = await db.query.spaceFile.findMany({
|
|
109
|
+
where: and(eq(schema.spaceFile.spaceId, input.space), eq(schema.spaceFile.origin, origin), eq(schema.spaceFile.kind, "picture"), inArray(schema.spaceFile.source, list.map((i) => i.url))),
|
|
110
|
+
columns: { id: true, source: true },
|
|
111
|
+
});
|
|
112
|
+
for (const k of known) {
|
|
113
|
+
ids.set(k.source, k.id);
|
|
114
|
+
}
|
|
115
|
+
const missing = list.filter((i) => !ids.has(i.url));
|
|
116
|
+
let next = 0;
|
|
117
|
+
await Promise.all(Array.from({ length: Math.min(PARALLEL, missing.length) }, async () => {
|
|
118
|
+
while (next < missing.length && !input.signal?.aborted) {
|
|
119
|
+
const image = missing[next++];
|
|
120
|
+
const id = await copyPicture(origin, input.space, label, image, input.signal).catch((err) => {
|
|
121
|
+
console.error(`[plugin ${origin}] picture`, image.url, err);
|
|
122
|
+
return null;
|
|
123
|
+
});
|
|
124
|
+
if (id) {
|
|
125
|
+
ids.set(image.url, id);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}));
|
|
129
|
+
const shown = new Set();
|
|
130
|
+
const markdown = input.markdown.replace(/!\[([^\]\n]*)\]\(\s*<?(https?:\/\/[^)\s>]+)>?(?:\s+"[^"]*")?\s*\)/g, (whole, alt, url) => {
|
|
131
|
+
const id = ids.get(url);
|
|
132
|
+
if (!id) {
|
|
133
|
+
return whole;
|
|
134
|
+
}
|
|
135
|
+
shown.add(url);
|
|
136
|
+
return ``;
|
|
137
|
+
});
|
|
138
|
+
const added = list
|
|
139
|
+
.filter((i) => ids.has(i.url) && !shown.has(i.url))
|
|
140
|
+
.map((i) => `![${(i.alt ?? "").replace(/[[\]\n]/g, " ").trim()}](asset://${ids.get(i.url)})`);
|
|
141
|
+
return added.length ? `${markdown.trimEnd()}\n\n${added.join("\n\n")}\n` : markdown;
|
|
142
|
+
}
|
|
143
|
+
/** Takes out those of the plugin's pictures `ids` that no page of the space shows any more. */
|
|
144
|
+
export async function dropUnshownPictures(origin, spaceId, ids) {
|
|
145
|
+
const list = [...new Set(ids)];
|
|
146
|
+
if (!list.length) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const rows = await db.query.spaceFile.findMany({
|
|
150
|
+
where: and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.origin, origin), eq(schema.spaceFile.kind, "picture"), inArray(schema.spaceFile.id, list)),
|
|
151
|
+
columns: { id: true, originKey: true },
|
|
152
|
+
});
|
|
153
|
+
for (const row of rows) {
|
|
154
|
+
const shown = await db.query.spacePage.findFirst({
|
|
155
|
+
where: and(eq(schema.spacePage.spaceId, spaceId), sql `instr(${schema.spacePage.markdown}, ${`asset://${row.id}`}) > 0`),
|
|
156
|
+
columns: { id: true },
|
|
157
|
+
});
|
|
158
|
+
if (!shown && row.originKey) {
|
|
159
|
+
await removeOriginFiles(origin, spaceId, row.originKey);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FIELD_WAYS, formatsFor, isDecidedField, shownFields, slotText, wizardLang, } from "@engenty/wizards-shared/definition";
|
|
2
2
|
import { note } from "@engenty/wizards-shared/run";
|
|
3
|
-
import { and, count, desc, eq, gte, inArray, max, or, sql } from "drizzle-orm";
|
|
3
|
+
import { and, count, desc, eq, gte, inArray, isNull, max, or, sql } from "drizzle-orm";
|
|
4
4
|
import { canSpend, MICROS_PER_CREDIT } from "../credits/credits.js";
|
|
5
5
|
import { db, schema } from "../db/client.js";
|
|
6
6
|
import { answerAsk } from "../engine/asks.js";
|
|
@@ -361,9 +361,11 @@ export function refusedInput(err) {
|
|
|
361
361
|
}
|
|
362
362
|
/**
|
|
363
363
|
* Runs the published version of a wizard for the admin, from their own AI client: a live run
|
|
364
|
-
* that is theirs — no visitor, no share link needed. With answers the first page is filled.
|
|
364
|
+
* that is theirs — no visitor, no share link needed. With answers the first page is filled. In a
|
|
365
|
+
* client space the wizard is its parent's and the run belongs to the client space
|
|
366
|
+
* (docs/plans/client-portal.md).
|
|
365
367
|
*/
|
|
366
|
-
export async function startRun(userId, wizardId, answers = {}) {
|
|
368
|
+
export async function startRun(userId, wizardId, answers = {}, { clientSpaceId = null } = {}) {
|
|
367
369
|
const w = await ownedWizard(userId, wizardId);
|
|
368
370
|
if (w.publishedVersion === null) {
|
|
369
371
|
throw new ServiceError("refused", "This wizard isn’t published yet. Publish it, or try the draft with start_test_run.");
|
|
@@ -384,6 +386,7 @@ export async function startRun(userId, wizardId, answers = {}) {
|
|
|
384
386
|
version: w.publishedVersion,
|
|
385
387
|
mode: "live",
|
|
386
388
|
userId,
|
|
389
|
+
clientSpaceId,
|
|
387
390
|
}).catch((err) => {
|
|
388
391
|
if (err instanceof ModelUnavailableError) {
|
|
389
392
|
throw new ServiceError("refused", saidOf(err));
|
|
@@ -496,6 +499,8 @@ export async function draftStepView(userId, wizardId, stepId, lang) {
|
|
|
496
499
|
error: null,
|
|
497
500
|
ask: null,
|
|
498
501
|
answer: null,
|
|
502
|
+
clientSpaceId: null,
|
|
503
|
+
clientSharedAt: null,
|
|
499
504
|
shareToken: null,
|
|
500
505
|
};
|
|
501
506
|
const view = await runView(run, await brandView(w.spaceId, def.themes));
|
|
@@ -534,6 +539,15 @@ export async function listRuns(userId, wizardId) {
|
|
|
534
539
|
* `filter`; and each wizard with how many it has and when its last one came, the latest first.
|
|
535
540
|
*/
|
|
536
541
|
export async function listResults(spaceId, filter = {}) {
|
|
542
|
+
// A client space shows the runs started in it, of its parent's wizards; a space only its own
|
|
543
|
+
// (docs/plans/client-portal.md).
|
|
544
|
+
const space = await db.query.space.findFirst({
|
|
545
|
+
columns: { kind: true, parentId: true },
|
|
546
|
+
where: eq(schema.space.id, spaceId),
|
|
547
|
+
});
|
|
548
|
+
const client = space?.kind === "client" && space.parentId ? spaceId : null;
|
|
549
|
+
const wizardSpace = client && space?.parentId ? space.parentId : spaceId;
|
|
550
|
+
const inSpace = client ? eq(schema.run.clientSpaceId, client) : isNull(schema.run.clientSpaceId);
|
|
537
551
|
const wizards = await db
|
|
538
552
|
.select({
|
|
539
553
|
id: schema.wizard.id,
|
|
@@ -542,8 +556,8 @@ export async function listResults(spaceId, filter = {}) {
|
|
|
542
556
|
lastAt: max(schema.run.updatedAt),
|
|
543
557
|
})
|
|
544
558
|
.from(schema.wizard)
|
|
545
|
-
.leftJoin(schema.run, and(eq(schema.run.wizardId, schema.wizard.id), eq(schema.run.status, "done")))
|
|
546
|
-
.where(and(eq(schema.wizard.spaceId,
|
|
559
|
+
.leftJoin(schema.run, and(eq(schema.run.wizardId, schema.wizard.id), eq(schema.run.status, "done"), inSpace))
|
|
560
|
+
.where(and(eq(schema.wizard.spaceId, wizardSpace), eq(schema.wizard.tenantId, currentTenant())))
|
|
547
561
|
.groupBy(schema.wizard.id)
|
|
548
562
|
// Wizards without a result last: SQLite sorts null below every time.
|
|
549
563
|
.orderBy(desc(max(schema.run.updatedAt)), desc(schema.wizard.updatedAt));
|
|
@@ -562,9 +576,10 @@ export async function listResults(spaceId, filter = {}) {
|
|
|
562
576
|
version: schema.run.version,
|
|
563
577
|
costMicros: schema.run.costMicros,
|
|
564
578
|
updatedAt: schema.run.updatedAt,
|
|
579
|
+
clientSharedAt: schema.run.clientSharedAt,
|
|
565
580
|
})
|
|
566
581
|
.from(schema.run)
|
|
567
|
-
.where(and(inArray(schema.run.wizardId, ids), eq(schema.run.status, "done"), filter.mode ? eq(schema.run.mode, filter.mode) : undefined, filter.days
|
|
582
|
+
.where(and(inArray(schema.run.wizardId, ids), eq(schema.run.status, "done"), inSpace, filter.mode ? eq(schema.run.mode, filter.mode) : undefined, filter.days
|
|
568
583
|
? gte(schema.run.updatedAt, new Date(Date.now() - filter.days * 86_400_000))
|
|
569
584
|
: undefined, like
|
|
570
585
|
? or(titled?.size ? inArray(schema.run.wizardId, [...titled]) : undefined, sql `lower(${schema.run.state}) like ${like} escape '\\'`)
|
|
@@ -594,6 +609,7 @@ export async function listResults(spaceId, filter = {}) {
|
|
|
594
609
|
doneAt: r.updatedAt.toISOString(),
|
|
595
610
|
credits: Math.ceil(r.costMicros / MICROS_PER_CREDIT),
|
|
596
611
|
assets: assetCounts.find((a) => a.runId === r.id)?.n ?? 0,
|
|
612
|
+
...(client ? { clientShared: Boolean(r.clientSharedAt) } : {}),
|
|
597
613
|
})),
|
|
598
614
|
};
|
|
599
615
|
}
|
|
@@ -18,6 +18,9 @@ export function itemKey(ref) {
|
|
|
18
18
|
if ("tableId" in ref) {
|
|
19
19
|
return `t:${ref.tableId}`;
|
|
20
20
|
}
|
|
21
|
+
if ("mountFileId" in ref) {
|
|
22
|
+
return `m:${ref.mountFileId}`;
|
|
23
|
+
}
|
|
21
24
|
return `f:${ref.fileId}`;
|
|
22
25
|
}
|
|
23
26
|
function keyOfRow(r) {
|
|
@@ -27,7 +30,9 @@ function keyOfRow(r) {
|
|
|
27
30
|
? `r:${r.rowId}`
|
|
28
31
|
: r.tableId
|
|
29
32
|
? `t:${r.tableId}`
|
|
30
|
-
:
|
|
33
|
+
: r.mountFileId
|
|
34
|
+
? `m:${r.mountFileId}`
|
|
35
|
+
: `f:${r.fileId}`;
|
|
31
36
|
}
|
|
32
37
|
function refWhere(ref) {
|
|
33
38
|
const t = schema.spaceItemCategory;
|
|
@@ -40,6 +45,9 @@ function refWhere(ref) {
|
|
|
40
45
|
if ("tableId" in ref) {
|
|
41
46
|
return eq(t.tableId, ref.tableId);
|
|
42
47
|
}
|
|
48
|
+
if ("mountFileId" in ref) {
|
|
49
|
+
return eq(t.mountFileId, ref.mountFileId);
|
|
50
|
+
}
|
|
43
51
|
return eq(t.fileId, ref.fileId);
|
|
44
52
|
}
|
|
45
53
|
// --- typed values ---------------------------------------------------------------
|
|
@@ -185,7 +193,7 @@ export async function listCategories(spaceId) {
|
|
|
185
193
|
db
|
|
186
194
|
.select({
|
|
187
195
|
categoryId: t.categoryId,
|
|
188
|
-
n: sql `count(DISTINCT coalesce(${t.pageId}, ${t.rowId}, ${t.tableId}, ${t.fileId}))`,
|
|
196
|
+
n: sql `count(DISTINCT coalesce(${t.pageId}, ${t.rowId}, ${t.tableId}, ${t.fileId}, ${t.mountFileId}))`,
|
|
189
197
|
})
|
|
190
198
|
.from(t)
|
|
191
199
|
.where(inArray(t.categoryId, ids))
|
|
@@ -355,11 +363,16 @@ async function reindexHolders(categoryId) {
|
|
|
355
363
|
rowId: schema.spaceItemCategory.rowId,
|
|
356
364
|
tableId: schema.spaceItemCategory.tableId,
|
|
357
365
|
fileId: schema.spaceItemCategory.fileId,
|
|
366
|
+
mountFileId: schema.spaceItemCategory.mountFileId,
|
|
358
367
|
})
|
|
359
368
|
.from(schema.spaceItemCategory)
|
|
360
369
|
.where(eq(schema.spaceItemCategory.categoryId, categoryId));
|
|
361
370
|
const tables = new Set();
|
|
362
371
|
for (const r of rows) {
|
|
372
|
+
// A mounted file's words don't hold its categories: nothing to index again.
|
|
373
|
+
if (r.mountFileId) {
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
363
376
|
if (r.rowId) {
|
|
364
377
|
const row = await db.query.spaceTableRow.findFirst({
|
|
365
378
|
where: eq(schema.spaceTableRow.id, r.rowId),
|
|
@@ -562,6 +575,7 @@ export async function setItemCategories(spaceId, ref, input, by) {
|
|
|
562
575
|
...("rowId" in ref ? { rowId: ref.rowId } : {}),
|
|
563
576
|
...("tableId" in ref ? { tableId: ref.tableId } : {}),
|
|
564
577
|
...("fileId" in ref ? { fileId: ref.fileId } : {}),
|
|
578
|
+
...("mountFileId" in ref ? { mountFileId: ref.mountFileId } : {}),
|
|
565
579
|
...n,
|
|
566
580
|
by,
|
|
567
581
|
})));
|
|
@@ -585,6 +599,13 @@ async function itemInSpace(spaceId, ref) {
|
|
|
585
599
|
});
|
|
586
600
|
return file?.spaceId === spaceId;
|
|
587
601
|
}
|
|
602
|
+
if ("mountFileId" in ref) {
|
|
603
|
+
const file = await db.query.spaceMountFile.findFirst({
|
|
604
|
+
where: eq(schema.spaceMountFile.id, ref.mountFileId),
|
|
605
|
+
columns: { spaceId: true },
|
|
606
|
+
});
|
|
607
|
+
return file?.spaceId === spaceId;
|
|
608
|
+
}
|
|
588
609
|
const tableId = "tableId" in ref
|
|
589
610
|
? ref.tableId
|
|
590
611
|
: (await db.query.spaceTableRow.findFirst({
|
|
@@ -624,6 +645,7 @@ export async function itemCategoriesOf(refs) {
|
|
|
624
645
|
["rowId", t.rowId],
|
|
625
646
|
["tableId", t.tableId],
|
|
626
647
|
["fileId", t.fileId],
|
|
648
|
+
["mountFileId", t.mountFileId],
|
|
627
649
|
]) {
|
|
628
650
|
const list = ids(k);
|
|
629
651
|
for (let i = 0; i < list.length; i += 500) {
|
|
@@ -813,7 +835,13 @@ export async function filterItems(spaceId, where) {
|
|
|
813
835
|
})).map((v) => [v.value.toLowerCase(), v.id]));
|
|
814
836
|
const t = schema.spaceItemCategory;
|
|
815
837
|
const matched = await db
|
|
816
|
-
.select({
|
|
838
|
+
.select({
|
|
839
|
+
pageId: t.pageId,
|
|
840
|
+
rowId: t.rowId,
|
|
841
|
+
tableId: t.tableId,
|
|
842
|
+
fileId: t.fileId,
|
|
843
|
+
mountFileId: t.mountFileId,
|
|
844
|
+
})
|
|
817
845
|
.from(t)
|
|
818
846
|
.where(and(eq(t.categoryId, category.id), condition(category, w, valueIds)));
|
|
819
847
|
const keys = new Set();
|
|
@@ -840,6 +868,9 @@ export async function filterItems(spaceId, where) {
|
|
|
840
868
|
if (m.fileId) {
|
|
841
869
|
keys.add(`f:${m.fileId}`);
|
|
842
870
|
}
|
|
871
|
+
if (m.mountFileId) {
|
|
872
|
+
keys.add(`m:${m.mountFileId}`);
|
|
873
|
+
}
|
|
843
874
|
}
|
|
844
875
|
result = result ? new Set([...result].filter((k) => keys.has(k))) : keys;
|
|
845
876
|
}
|
|
@@ -848,14 +879,19 @@ export async function filterItems(spaceId, where) {
|
|
|
848
879
|
rows: new Set(),
|
|
849
880
|
tables: new Set(),
|
|
850
881
|
files: new Set(),
|
|
882
|
+
mounted: new Set(),
|
|
851
883
|
size: 0,
|
|
852
884
|
};
|
|
853
885
|
for (const key of result ?? []) {
|
|
854
886
|
const id = key.slice(2);
|
|
855
|
-
({ p: out.pages, r: out.rows, t: out.tables, f: out.files })[key[0]].add(id);
|
|
887
|
+
({ p: out.pages, r: out.rows, t: out.tables, f: out.files, m: out.mounted })[key[0]].add(id);
|
|
856
888
|
}
|
|
857
889
|
// A table counts once, however many of its rows are left.
|
|
858
890
|
out.size =
|
|
859
|
-
out.pages.size +
|
|
891
|
+
out.pages.size +
|
|
892
|
+
out.files.size +
|
|
893
|
+
out.mounted.size +
|
|
894
|
+
out.tables.size +
|
|
895
|
+
(out.tables.size ? 0 : out.rows.size);
|
|
860
896
|
return out;
|
|
861
897
|
}
|