@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
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import { featureOn } from "@engenty/wizards-shared/features";
|
|
3
|
+
import { spaceFileLimit } from "@engenty/wizards-shared/spaces";
|
|
4
|
+
import { and, eq, like } from "drizzle-orm";
|
|
5
|
+
import { db, knownTenants, schema, withTenant } from "../db/client.js";
|
|
6
|
+
import { documentMime } from "../documents/parse.js";
|
|
7
|
+
import { currentFeatures } from "../features.js";
|
|
8
|
+
import { managed } from "../manage.js";
|
|
9
|
+
import { moveKnowledgeItem } from "../services/space-data.js";
|
|
10
|
+
import { addSpaceFile } from "../services/space-files.js";
|
|
11
|
+
import { CORE, registerCoreSource, runDueSources, startSource } from "../services/space-sources.js";
|
|
12
|
+
import { mountFileContent, onMountChanged, readerOf } from "./mounts.js";
|
|
13
|
+
/**
|
|
14
|
+
* „Aus Dateien übernehmen“ (docs/plans/media-and-files.md, step 8): the core's kind of source of
|
|
15
|
+
* Knowledge on a mount of Dateien. It takes a folder of a mount, or the whole mount, into a folder
|
|
16
|
+
* of Knowledge, copied and read as an upload is: `.md` and `.txt` become pages, tables and
|
|
17
|
+
* documents are read as uploads are, a directory's `README.md` or `index.md` is its folder's
|
|
18
|
+
* text. It reads through the mount's reader and runs again when the mount tells of a change in
|
|
19
|
+
* its folder.
|
|
20
|
+
*
|
|
21
|
+
* Its address is the wizards' path of the folder: `dateien/<mount>/<folder>`.
|
|
22
|
+
*/
|
|
23
|
+
export const KIND = "dateien";
|
|
24
|
+
/** Read as pages of their own: their text is the page. */
|
|
25
|
+
const PAGE = /\.(md|markdown|txt)$/i;
|
|
26
|
+
/** A folder's text. */
|
|
27
|
+
const FOLDER_TEXT = /^(readme|index)\.md$/i;
|
|
28
|
+
/** The mount and the folder in it an address names. */
|
|
29
|
+
export function parseAddress(address) {
|
|
30
|
+
const [head, slug, ...rest] = address
|
|
31
|
+
.trim()
|
|
32
|
+
.replace(/^\/+|\/+$/g, "")
|
|
33
|
+
.split("/");
|
|
34
|
+
return head === "dateien" && slug ? { slug, folder: rest.filter(Boolean).join("/") } : null;
|
|
35
|
+
}
|
|
36
|
+
export const addressOf = (slug, folder = "") => ["dateien", slug, ...folder.split("/").filter(Boolean)].join("/");
|
|
37
|
+
async function mountOf(source) {
|
|
38
|
+
const at = parseAddress(source.address);
|
|
39
|
+
const mount = at
|
|
40
|
+
? await db.query.spaceMount.findFirst({
|
|
41
|
+
where: and(eq(schema.spaceMount.spaceId, source.space), eq(schema.spaceMount.slug, at.slug)),
|
|
42
|
+
})
|
|
43
|
+
: null;
|
|
44
|
+
if (!(at && mount)) {
|
|
45
|
+
throw new Error(`Dateien has no folder "${source.address}".`);
|
|
46
|
+
}
|
|
47
|
+
return { mount, folder: at.folder };
|
|
48
|
+
}
|
|
49
|
+
/** The mounts of a space a source can name, by name and slug. */
|
|
50
|
+
export function mountsOf(spaceId) {
|
|
51
|
+
return db.query.spaceMount.findMany({
|
|
52
|
+
where: eq(schema.spaceMount.spaceId, spaceId),
|
|
53
|
+
columns: { name: true, slug: true },
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/** A file's path below the source's folder. */
|
|
57
|
+
const below = (folder, path) => (folder ? path.slice(folder.length + 1) : path);
|
|
58
|
+
/** What a list says changed with the bytes: the source's version, else size and time. */
|
|
59
|
+
const fingerprintOf = (e) => e.version ?? `${e.size}:${e.modified}`;
|
|
60
|
+
/** A page's title: its first heading, else the file's name without its ending. */
|
|
61
|
+
function titleOf(markdown, name) {
|
|
62
|
+
const heading = markdown.match(/^#\s+(.+)$/m)?.[1]?.trim();
|
|
63
|
+
return (heading || name.replace(/\.[a-z0-9]{1,8}$/i, "")).slice(0, 120);
|
|
64
|
+
}
|
|
65
|
+
export const fromDateien = {
|
|
66
|
+
kind: KIND,
|
|
67
|
+
match: (address) => (parseAddress(address) ? address.trim().replace(/^\/+|\/+$/g, "") : null),
|
|
68
|
+
reader: {
|
|
69
|
+
async list(source, ctx) {
|
|
70
|
+
const { mount, folder } = await mountOf(source);
|
|
71
|
+
const found = [];
|
|
72
|
+
for await (const entry of (await readerOf(mount)).list(folder || undefined, {
|
|
73
|
+
signal: ctx.signal,
|
|
74
|
+
})) {
|
|
75
|
+
const parts = below(folder, entry.path).split("/");
|
|
76
|
+
const name = parts[parts.length - 1];
|
|
77
|
+
found.push({
|
|
78
|
+
address: entry.path,
|
|
79
|
+
title: name,
|
|
80
|
+
fingerprint: fingerprintOf(entry),
|
|
81
|
+
// A README stands for its folder; any other file is named by its own part.
|
|
82
|
+
path: FOLDER_TEXT.test(name) ? parts.slice(0, -1) : parts,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return found;
|
|
86
|
+
},
|
|
87
|
+
async read(source, entry, ctx) {
|
|
88
|
+
const { mount, folder } = await mountOf(source);
|
|
89
|
+
const reader = await readerOf(mount);
|
|
90
|
+
const now = await reader.stat(entry.address);
|
|
91
|
+
if (!now) {
|
|
92
|
+
throw new Error(`"${entry.address}" is not in the folder any more.`);
|
|
93
|
+
}
|
|
94
|
+
const fingerprint = fingerprintOf(now);
|
|
95
|
+
const name = basename(entry.address);
|
|
96
|
+
const mime = documentMime(name, now.mime ?? "") || "application/octet-stream";
|
|
97
|
+
const limit = spaceFileLimit("document", mime);
|
|
98
|
+
if (now.size > limit) {
|
|
99
|
+
return {
|
|
100
|
+
kind: "failed",
|
|
101
|
+
note: `Larger than ${Math.round(limit / 1_000_000)} MB.`,
|
|
102
|
+
fingerprint,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const data = await reader.read(entry.address, { signal: ctx.signal });
|
|
106
|
+
if (PAGE.test(name)) {
|
|
107
|
+
const markdown = Buffer.from(data).toString("utf8");
|
|
108
|
+
if (!FOLDER_TEXT.test(name)) {
|
|
109
|
+
return { kind: "page", title: titleOf(markdown, name), markdown, fingerprint };
|
|
110
|
+
}
|
|
111
|
+
// A README is called as its folder.
|
|
112
|
+
const parts = below(folder, entry.address).split("/");
|
|
113
|
+
const dir = parts.at(-2) ?? basename(folder || mount.name);
|
|
114
|
+
return { kind: "page", title: titleOf(markdown, dir), markdown, fingerprint, folder: true };
|
|
115
|
+
}
|
|
116
|
+
return { kind: "document", name, mime, data, fingerprint };
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
const on = async () => !managed &&
|
|
121
|
+
featureOn(await currentFeatures(), "dateien") &&
|
|
122
|
+
featureOn(await currentFeatures(), "knowledgeTree");
|
|
123
|
+
/** Sources run this often as well, for a change no watcher told of. */
|
|
124
|
+
const DUE_MS = 15 * 60_000;
|
|
125
|
+
/**
|
|
126
|
+
* The kind in the core's list, and its sources run when their mount changes under their folder
|
|
127
|
+
* (and daily besides, as their `everyHours` says).
|
|
128
|
+
*/
|
|
129
|
+
export function startFileSources() {
|
|
130
|
+
registerCoreSource({ ...fromDateien, everyHours: 24 }, on);
|
|
131
|
+
onMountChanged((mount, paths) => {
|
|
132
|
+
void (async () => {
|
|
133
|
+
const rows = await db.query.spaceSource.findMany({
|
|
134
|
+
where: and(eq(schema.spaceSource.spaceId, mount.spaceId), eq(schema.spaceSource.plugin, CORE), eq(schema.spaceSource.kind, KIND), eq(schema.spaceSource.state, "taken"), like(schema.spaceSource.address, `dateien/${mount.slug}%`)),
|
|
135
|
+
});
|
|
136
|
+
for (const row of rows) {
|
|
137
|
+
const at = parseAddress(row.address);
|
|
138
|
+
if (at?.slug === mount.slug &&
|
|
139
|
+
paths.some((p) => !at.folder || p === at.folder || p.startsWith(`${at.folder}/`))) {
|
|
140
|
+
startSource(row.id);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
})().catch((err) => console.error("[sources] dateien", err));
|
|
144
|
+
});
|
|
145
|
+
if (managed) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const due = async () => {
|
|
149
|
+
for (const tenant of await knownTenants()) {
|
|
150
|
+
await withTenant(tenant, () => runDueSources(CORE)).catch((err) => console.error(`[sources] due ${tenant}`, err));
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
// At start as well: a source that never ran (`wizards move-folder-sources`) reads now.
|
|
154
|
+
setTimeout(() => void due(), 5000).unref();
|
|
155
|
+
setInterval(() => void due(), DUE_MS).unref();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* „In Wissen übernehmen“: one file of a mount copied into Knowledge, read as an upload of it is,
|
|
159
|
+
* into a folder of Knowledge or at the top. The copy is the space's own; the source does not
|
|
160
|
+
* follow it.
|
|
161
|
+
*/
|
|
162
|
+
export async function copyToKnowledge(userId, spaceId, fileId, who, parentId) {
|
|
163
|
+
const { file, data } = await mountFileContent(spaceId, fileId, who);
|
|
164
|
+
const mount = await db.query.spaceMount.findFirst({
|
|
165
|
+
where: eq(schema.spaceMount.id, file.mountId),
|
|
166
|
+
columns: { slug: true },
|
|
167
|
+
});
|
|
168
|
+
const row = await addSpaceFile(spaceId, {
|
|
169
|
+
kind: "document",
|
|
170
|
+
name: file.name,
|
|
171
|
+
mime: file.mime,
|
|
172
|
+
data,
|
|
173
|
+
source: mount ? `dateien/${mount.slug}/${file.path}` : undefined,
|
|
174
|
+
});
|
|
175
|
+
if (parentId) {
|
|
176
|
+
await moveKnowledgeItem(userId, spaceId, { key: `f:${row.id}`, parentId });
|
|
177
|
+
}
|
|
178
|
+
return row;
|
|
179
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mounted file's text in passages that say where they stand in it: its headings, the page of
|
|
3
|
+
* the original, and the characters from `start` to `end`. The text itself is not kept (decision
|
|
4
|
+
* D3): the words go into a contentless index once, and a snippet or a vector reads the passage
|
|
5
|
+
* from the original again by its place. Cut as Wissen's pages are (space-index.ts): at headings,
|
|
6
|
+
* a long section at its paragraphs.
|
|
7
|
+
*/
|
|
8
|
+
/** A passage longer than this is cut at paragraphs into parts about PART_CHARS long. */
|
|
9
|
+
const SECTION_CHARS = 2400;
|
|
10
|
+
const PART_CHARS = 1800;
|
|
11
|
+
/** Passages of one file; a longer one keeps its start. */
|
|
12
|
+
export const MAX_TEXT_UNITS = 2000;
|
|
13
|
+
const HEADING = /^(#{1,4})\s+(.+?)\s*#*\s*$/;
|
|
14
|
+
/** A page's start: Wissen's mark, or the converter's page break. */
|
|
15
|
+
const SHEET = /^\s*(?:<!--\s*S\.\s*(\d+)\s*-->|<page-break number="(\d+)"[^>]*><\/page-break>)\s*$/;
|
|
16
|
+
/** The parts of [start, end) of `text`, cut at blank lines, none much longer than PART_CHARS. */
|
|
17
|
+
function parts(text, start, end) {
|
|
18
|
+
if (end - start <= SECTION_CHARS) {
|
|
19
|
+
return [[start, end]];
|
|
20
|
+
}
|
|
21
|
+
const out = [];
|
|
22
|
+
let from = start;
|
|
23
|
+
const blank = /\n\s*\n/g;
|
|
24
|
+
blank.lastIndex = start;
|
|
25
|
+
let cut = start;
|
|
26
|
+
for (let m = blank.exec(text); m && m.index < end; m = blank.exec(text)) {
|
|
27
|
+
if (m.index - from > PART_CHARS && cut > from) {
|
|
28
|
+
out.push([from, cut]);
|
|
29
|
+
from = cut;
|
|
30
|
+
}
|
|
31
|
+
cut = m.index + m[0].length;
|
|
32
|
+
}
|
|
33
|
+
// What has no blank line to cut at is cut at the length.
|
|
34
|
+
while (end - from > SECTION_CHARS) {
|
|
35
|
+
out.push([from, from + PART_CHARS]);
|
|
36
|
+
from += PART_CHARS;
|
|
37
|
+
}
|
|
38
|
+
out.push([from, end]);
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
export function textUnits(text) {
|
|
42
|
+
const units = [];
|
|
43
|
+
const stack = [];
|
|
44
|
+
let sheet = null;
|
|
45
|
+
let heading = null;
|
|
46
|
+
let sectionSheet = null;
|
|
47
|
+
let start = 0;
|
|
48
|
+
let fence = false;
|
|
49
|
+
const close = (end) => {
|
|
50
|
+
if (text.slice(start, end).trim()) {
|
|
51
|
+
for (const [s, e] of parts(text, start, end)) {
|
|
52
|
+
if (text.slice(s, e).trim()) {
|
|
53
|
+
units.push({ heading, sheet: sectionSheet, start: s, end: e });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
let at = 0;
|
|
59
|
+
for (const line of text.split("\n")) {
|
|
60
|
+
const lineEnd = at + line.length + 1;
|
|
61
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
62
|
+
fence = !fence;
|
|
63
|
+
}
|
|
64
|
+
const mark = fence ? null : SHEET.exec(line);
|
|
65
|
+
const h = fence || mark ? null : HEADING.exec(line);
|
|
66
|
+
if (mark) {
|
|
67
|
+
sheet = Number(mark[1] ?? mark[2]);
|
|
68
|
+
if (!text.slice(start, at).trim()) {
|
|
69
|
+
sectionSheet = sheet;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (h) {
|
|
73
|
+
close(at);
|
|
74
|
+
const level = h[1].length;
|
|
75
|
+
while (stack.length && stack.at(-1).level >= level) {
|
|
76
|
+
stack.pop();
|
|
77
|
+
}
|
|
78
|
+
stack.push({ level, text: h[2].trim() });
|
|
79
|
+
heading = stack.map((s) => s.text).join(" › ");
|
|
80
|
+
sectionSheet = sheet;
|
|
81
|
+
start = lineEnd;
|
|
82
|
+
}
|
|
83
|
+
at = lineEnd;
|
|
84
|
+
}
|
|
85
|
+
close(text.length);
|
|
86
|
+
return units.slice(0, MAX_TEXT_UNITS);
|
|
87
|
+
}
|
|
88
|
+
/** A passage's text, as its words were indexed and its vector made. */
|
|
89
|
+
export function unitText(text, unit) {
|
|
90
|
+
const body = text
|
|
91
|
+
.slice(unit.start, unit.end)
|
|
92
|
+
.replace(/<page-break[^>]*><\/page-break>/g, "")
|
|
93
|
+
.trim();
|
|
94
|
+
return unit.heading ? `## ${unit.heading}\n\n${body}` : body;
|
|
95
|
+
}
|
|
96
|
+
/** A path in the mount as the words that find it: `Kunden › A1 › angebot.pdf`. */
|
|
97
|
+
export const pathWords = (path) => path.split("/").join(" › ");
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// `wizards move-files` where there is no `wizards` command: in the cloud runtime's container,
|
|
2
|
+
// `node apps/runtime/dist/move-files.js` with the runtime's environment (cli/move-files.ts).
|
|
3
|
+
import { moveFiles } from "./cli/move-files.js";
|
|
4
|
+
process.exit(await moveFiles());
|
|
@@ -4,7 +4,7 @@ import { fileURLToPath } from "node:url";
|
|
|
4
4
|
import { FIELD_KINDS, PLUGIN_ID, PLUGIN_TOOL_NAME, wizardSchema, } from "@engenty/wizards-shared/definition";
|
|
5
5
|
import { BUILT_IN_RUNNERS, OUTPUT_KINDS, RESERVED_PATHS, RUNNER_ID, } from "@engenty/wizards-shared/runners";
|
|
6
6
|
import { generateText, Output } from "ai";
|
|
7
|
-
import { eq } from "drizzle-orm";
|
|
7
|
+
import { and, eq } from "drizzle-orm";
|
|
8
8
|
import { createJiti } from "jiti";
|
|
9
9
|
import { z } from "zod";
|
|
10
10
|
import { readPage } from "../agents/website.js";
|
|
@@ -16,9 +16,11 @@ import { env } from "../env.js";
|
|
|
16
16
|
import { currentFeatures } from "../features.js";
|
|
17
17
|
import { managed } from "../manage.js";
|
|
18
18
|
import { conversationAccess, ModelUnavailableError, textModel } from "../models.js";
|
|
19
|
+
import { pluginMountChanged, pluginMounts } from "../mounts/mounts.js";
|
|
19
20
|
import { seal, unseal } from "../secrets/crypto.js";
|
|
20
21
|
import { putIndexEntry, removeIndexEntries } from "../services/space-index.js";
|
|
21
22
|
import { originData } from "../services/space-origin.js";
|
|
23
|
+
import { addSourceEntries, isCoreKind, runDueSources, sourceView, startSource, } from "../services/space-sources.js";
|
|
22
24
|
import { readSetting, writeSetting } from "../settings.js";
|
|
23
25
|
import { safeFetch } from "../tools/net-guard.js";
|
|
24
26
|
import { channelsApi } from "./channels.js";
|
|
@@ -93,6 +95,8 @@ async function importFactory(entry) {
|
|
|
93
95
|
}
|
|
94
96
|
return factory;
|
|
95
97
|
}
|
|
98
|
+
/** The job that reads a plugin's sources of Knowledge again on their schedule. */
|
|
99
|
+
const SOURCES_JOB = "knowledge_sources";
|
|
96
100
|
/** A job runs at most this often. */
|
|
97
101
|
const MIN_EVERY_MS = 60_000;
|
|
98
102
|
/** The built-in runners and the sub-paths of a wizard's address that are something else. */
|
|
@@ -128,6 +132,19 @@ function apiFor(record) {
|
|
|
128
132
|
throw new Error(`${kind} "${name}": a name is lower case, digits and "_".`);
|
|
129
133
|
}
|
|
130
134
|
};
|
|
135
|
+
/** A source of one of the plugin's kinds, of the current tenant. */
|
|
136
|
+
const ownSource = async (id) => {
|
|
137
|
+
const row = await db.query.spaceSource.findFirst({
|
|
138
|
+
where: and(eq(schema.spaceSource.id, id), eq(schema.spaceSource.plugin, source.id)),
|
|
139
|
+
});
|
|
140
|
+
if (!row) {
|
|
141
|
+
throw Object.assign(new Error("There is no such source."), {
|
|
142
|
+
status: 404,
|
|
143
|
+
code: "not_found",
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return row;
|
|
147
|
+
};
|
|
131
148
|
return {
|
|
132
149
|
plugin: source.manifest,
|
|
133
150
|
resolvePath: (relative) => resolve(source.root, relative),
|
|
@@ -226,6 +243,61 @@ function apiFor(record) {
|
|
|
226
243
|
remove: (space, key) => removeIndexEntries(source.id, space, key),
|
|
227
244
|
},
|
|
228
245
|
spaceData: originData(source.id),
|
|
246
|
+
registerKnowledgeSource(kind) {
|
|
247
|
+
if (!/^[a-z0-9-]{1,24}$/.test(kind.kind)) {
|
|
248
|
+
throw new Error(`Source "${kind.kind}": a kind is lower case, digits and "-".`);
|
|
249
|
+
}
|
|
250
|
+
const taken = loadedPlugins().some((p) => p.source.id !== source.id && p.knowledgeSources.has(kind.kind));
|
|
251
|
+
if (taken || isCoreKind(kind.kind) || record.knowledgeSources.has(kind.kind)) {
|
|
252
|
+
throw new Error(`Source "${kind.kind}" is taken.`);
|
|
253
|
+
}
|
|
254
|
+
record.knowledgeSources.set(kind.kind, kind);
|
|
255
|
+
// Its sources are read again on their schedule, in every tenant that has the plugin.
|
|
256
|
+
if (!record.jobs.has(SOURCES_JOB)) {
|
|
257
|
+
record.jobs.set(SOURCES_JOB, {
|
|
258
|
+
everyMs: 15 * 60_000,
|
|
259
|
+
handler: () => runDueSources(source.id),
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
registerMountKind(kind) {
|
|
264
|
+
if (!/^[a-z0-9-]{1,24}$/.test(kind.kind) || kind.kind === "folder") {
|
|
265
|
+
throw new Error(`Mount kind "${kind.kind}": a kind is lower case, digits and "-".`);
|
|
266
|
+
}
|
|
267
|
+
const taken = loadedPlugins().some((p) => p.source.id !== source.id && p.mountKinds.has(kind.kind));
|
|
268
|
+
if (taken || record.mountKinds.has(kind.kind)) {
|
|
269
|
+
throw new Error(`Mount kind "${kind.kind}" is taken.`);
|
|
270
|
+
}
|
|
271
|
+
record.mountKinds.set(kind.kind, kind);
|
|
272
|
+
},
|
|
273
|
+
mounts: {
|
|
274
|
+
list: (space) => pluginMounts(source.id, space),
|
|
275
|
+
changed: (id) => pluginMountChanged(source.id, id),
|
|
276
|
+
},
|
|
277
|
+
knowledgeSources: {
|
|
278
|
+
async list(space) {
|
|
279
|
+
const rows = await db.query.spaceSource.findMany({
|
|
280
|
+
where: and(eq(schema.spaceSource.plugin, source.id), space ? eq(schema.spaceSource.spaceId, space) : undefined),
|
|
281
|
+
});
|
|
282
|
+
return Promise.all(rows.map(sourceView));
|
|
283
|
+
},
|
|
284
|
+
async byToken(token) {
|
|
285
|
+
const row = await db.query.spaceSource.findFirst({
|
|
286
|
+
where: and(eq(schema.spaceSource.plugin, source.id), eq(schema.spaceSource.token, token)),
|
|
287
|
+
});
|
|
288
|
+
return row ? sourceView(row) : null;
|
|
289
|
+
},
|
|
290
|
+
async run(id) {
|
|
291
|
+
const row = await ownSource(id);
|
|
292
|
+
if (row.state === "taken") {
|
|
293
|
+
startSource(row.id);
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
async addEntries(id, addresses) {
|
|
297
|
+
const row = await ownSource(id);
|
|
298
|
+
await addSourceEntries(row.spaceId, row.id, addresses);
|
|
299
|
+
},
|
|
300
|
+
},
|
|
229
301
|
spaces: {
|
|
230
302
|
async brand(space) {
|
|
231
303
|
const p = await db.query.space.findFirst({ where: eq(schema.space.id, space) });
|
|
@@ -78,7 +78,7 @@ THE SPACE
|
|
|
78
78
|
- Typography: the brand's fonts (set_typography), each with what it is for ("Headings", "Body text") and the weights it uses — from the website's stylesheet (read_website lists its fonts) or a brand guide; system and generic faces are no brand fonts. A font's licence and what it may be used for (web, documents, print, images, video) only where a brand guide, the font's own page or the person says so; the wizards keep to it. A type scale (text styles: name, size, weight, line height, letter spacing) only when a brand guide gives one.
|
|
79
79
|
- Asset sections are optional too: tabs of one kind each ("Icons", "Background images"), made with set_asset_sections only when the space holds enough assets of clearly different kinds or the person asks; a handful of pictures stay in the general tab.
|
|
80
80
|
- Files the person attached are stored already; the message names them. look_at_file shows you a picture or a document's text. Give a file a description with describe_file when it has none or the person says what it is. Take facts from a document when asked.
|
|
81
|
-
- Knowledge is what the wizards' steps look things up in: pages, tables and files, kept apart by typed categories. Documents the person attaches are read into Knowledge by themselves: say so; do not copy their text into facts unless asked. A website, a sitemap or a folder whose entries should
|
|
81
|
+
- Knowledge is what the wizards' steps look things up in: pages, tables and files, kept apart by typed categories. Documents the person attaches are read into Knowledge by themselves: say so; do not copy their text into facts unless asked. What the person tells you, or what you read for them on the web, goes in with write_knowledge_page when they want the wizards to know it: one page per subject, with a title that names it, its text from the source or the person, never invented. A website, a sitemap or a folder of Dateien whose entries should stay current in Knowledge is a source: where propose_source is there, use it: the source fills a folder of its own in Knowledge, and the person takes the proposal on its card; where it is not, write the pages once with write_knowledge_page when they ask, and say that keeping them current needs the Sources plugin.
|
|
82
82
|
- Talking from another page than Info or Brand, change title, about, logos, colours and facts only when the person asks.
|
|
83
83
|
- A space that is read-only (synced from a local install, or an organization that builds elsewhere) has no tools: say where it is changed.
|
|
84
84
|
|
|
@@ -159,6 +159,18 @@ Fetch a file from a public address into the space: a logo, an asset (image, grap
|
|
|
159
159
|
<!-- section: tool.describe_file -->
|
|
160
160
|
Set the description of one of the space's files.
|
|
161
161
|
|
|
162
|
+
<!-- section: tool.write_knowledge_page -->
|
|
163
|
+
Write a page of Knowledge in Markdown: a new one, or with `path` the page there replaced, title and text (read it with page_read first and keep what should stay). `parent` puts it under another page. The steps of the space's wizards look it up from then on.
|
|
164
|
+
|
|
165
|
+
<!-- section: tool.propose_source -->
|
|
166
|
+
Propose a source of the space's Knowledge that the person names: a website (the page that lists what belongs in, e.g. sfg.at/foerderungen/), a sitemap.xml, or a folder of Dateien by its path (`dateien/<mount>/<folder>`, `dateien/<mount>` for all of it) where the space mounts one. Lists what it holds, leaves out what is no content, and proposes the Kategorien that keep its entries apart. Shows the person a card to take it; nothing is written before. Taken, it fills a folder of its own in Knowledge and keeps it current.
|
|
167
|
+
|
|
168
|
+
<!-- section: tool.take_source -->
|
|
169
|
+
Take a proposed source when the person agrees in words: its Kategorien go into the space, its folder is made and its first run reads the entries into it, in the background.
|
|
170
|
+
|
|
171
|
+
<!-- section: tool.list_sources -->
|
|
172
|
+
List the sources of the space's Knowledge: what each holds, how much of it is read, and whether it is reading now.
|
|
173
|
+
|
|
162
174
|
<!-- section: tool.look_at_file -->
|
|
163
175
|
Look at one of the space's files: a picture is shown to you, a document comes as text.
|
|
164
176
|
|
|
@@ -115,7 +115,7 @@ Write a page of this wizard in Markdown, kept for every later run and shown to t
|
|
|
115
115
|
<!-- Knowledge (tools/space.ts) -->
|
|
116
116
|
|
|
117
117
|
<!-- section: tool.space_search -->
|
|
118
|
-
Search the space's Knowledge: its pages, tables and files, its images, videos and sounds by their description, and what its plugins keep (wiki pages, questions and answers). Gives items with their path, categories and best section, each checked for relevance. Ask in their language, with the words they would use; filter with `where` when the task names a category's value. Then read the items you need whole with page_read or table_read.
|
|
118
|
+
Search the space's Knowledge: its pages, tables and files, its images, videos and sounds by their description, and what its plugins keep (wiki pages, questions and answers); also the folders mounted in Dateien you may use (paths dateien/…, a hit says the folder in `in`). Gives items with their path, categories and best section, each checked for relevance. Ask in their language, with the words they would use; filter with `where` when the task names a category's value. Then read the items you need whole with page_read or table_read.
|
|
119
119
|
|
|
120
120
|
<!-- The `where` input of space_search and space_list. -->
|
|
121
121
|
<!-- section: tool.space.where -->
|
|
@@ -132,7 +132,10 @@ Read a page of Knowledge whole by its path: front matter (title, categories, its
|
|
|
132
132
|
Read a table of Knowledge by its path: its rows as CSV, filtered by its columns (by their names, like `where` of space_search). A few hundred rows at a time: pass `offset` to continue.
|
|
133
133
|
|
|
134
134
|
<!-- section: tool.space_document -->
|
|
135
|
-
Read a file of Knowledge as text by its path (files/…): a file that is not a page, or the original a page was read from. Long files come in parts: pass `from` to continue.
|
|
135
|
+
Read a file of Knowledge as text by its path (files/…): a file that is not a page, or the original a page was read from. Also a file of a folder mounted in Dateien (dateien/…), read from where it lives. Long files come in parts: pass `from` to continue.
|
|
136
|
+
|
|
137
|
+
<!-- section: tool.dateien_draft -->
|
|
138
|
+
Write a new text for a file of a folder mounted in Dateien (dateien/…), as a draft. Read it first with space_document and send the whole text. The file itself changes only when a person publishes the draft in Dateien; say so.
|
|
136
139
|
|
|
137
140
|
<!-- Skills of the space (tools/skills.ts); Goose gets the same tools. `names` joined with ", ". -->
|
|
138
141
|
|
|
@@ -3,7 +3,58 @@ import ExcelJS from "exceljs";
|
|
|
3
3
|
import { marked } from "marked";
|
|
4
4
|
import TurndownService from "turndown";
|
|
5
5
|
import { offlineContext } from "./chromium.js";
|
|
6
|
-
|
|
6
|
+
/** Elements that turndown writes as blocks of their own, with blank lines around them. */
|
|
7
|
+
const BLOCKS = new Set([
|
|
8
|
+
"ADDRESS",
|
|
9
|
+
"ARTICLE",
|
|
10
|
+
"ASIDE",
|
|
11
|
+
"BLOCKQUOTE",
|
|
12
|
+
"DIV",
|
|
13
|
+
"DL",
|
|
14
|
+
"FIGCAPTION",
|
|
15
|
+
"FIGURE",
|
|
16
|
+
"FOOTER",
|
|
17
|
+
"H1",
|
|
18
|
+
"H2",
|
|
19
|
+
"H3",
|
|
20
|
+
"H4",
|
|
21
|
+
"H5",
|
|
22
|
+
"H6",
|
|
23
|
+
"HEADER",
|
|
24
|
+
"HR",
|
|
25
|
+
"LI",
|
|
26
|
+
"OL",
|
|
27
|
+
"P",
|
|
28
|
+
"PRE",
|
|
29
|
+
"SECTION",
|
|
30
|
+
"TABLE",
|
|
31
|
+
"UL",
|
|
32
|
+
]);
|
|
33
|
+
const holdsBlock = (node) => [...node.childNodes].some((child) => BLOCKS.has(child.nodeName) || holdsBlock(child));
|
|
34
|
+
/**
|
|
35
|
+
* A link around blocks (a teaser: a `<div>` with a picture and a heading in one `<a>`) would come
|
|
36
|
+
* out with blank lines inside its brackets, which no Markdown reads as a link. Its content goes
|
|
37
|
+
* on one line instead: `[ Titel](…)`.
|
|
38
|
+
*/
|
|
39
|
+
function linkBlocks(service) {
|
|
40
|
+
return service.addRule("blockLink", {
|
|
41
|
+
filter: (node) => node.nodeName === "A" && Boolean(node.getAttribute("href")) && holdsBlock(node),
|
|
42
|
+
replacement: (text, node) => {
|
|
43
|
+
const inner = text
|
|
44
|
+
.replace(/\s*\n+\s*/g, " ")
|
|
45
|
+
.replace(/(^|\s)#{1,6}\s+/g, "$1")
|
|
46
|
+
.trim();
|
|
47
|
+
if (!inner) {
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
const el = node;
|
|
51
|
+
const href = (el.getAttribute("href") ?? "").replace(/([()])/g, "\\$1");
|
|
52
|
+
const title = el.getAttribute("title");
|
|
53
|
+
return `[${inner}](${href}${title ? ` "${title.replace(/"/g, '\\"')}"` : ""})`;
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const turndown = linkBlocks(new TurndownService({ headingStyle: "atx", codeBlockStyle: "fenced" }));
|
|
7
58
|
export const MIME = {
|
|
8
59
|
png: "image/png",
|
|
9
60
|
mp4: "video/mp4",
|
|
@@ -44,6 +95,142 @@ export function htmlToMarkdown(html) {
|
|
|
44
95
|
const body = html.match(/<body[^>]*>([\s\S]*)<\/body>/i)?.[1] ?? html;
|
|
45
96
|
return turndown.turndown(body.replace(/<style[\s\S]*?<\/style>/gi, "").replace(/<script[\s\S]*?<\/script>/gi, ""));
|
|
46
97
|
}
|
|
98
|
+
// --- a page's content, without what every page of the site has -----------------------------
|
|
99
|
+
/** Elements that are a site's frame, never a page's content. */
|
|
100
|
+
const FRAME_TAGS = new Set([
|
|
101
|
+
"NAV",
|
|
102
|
+
"FOOTER",
|
|
103
|
+
"ASIDE",
|
|
104
|
+
"FORM",
|
|
105
|
+
"DIALOG",
|
|
106
|
+
"BUTTON",
|
|
107
|
+
"IFRAME",
|
|
108
|
+
"NOSCRIPT",
|
|
109
|
+
"TEMPLATE",
|
|
110
|
+
]);
|
|
111
|
+
const FRAME_ROLES = /^(navigation|banner|contentinfo|search|dialog|alertdialog|complementary|menu|menubar)$/i;
|
|
112
|
+
/** Classes and ids of skip links, cookie notices, breadcrumbs and share buttons. */
|
|
113
|
+
const FRAME_NAMES = /(^|[\s_-])(skip-link|screen-reader-text|cookies?|consent|gdpr|borlabs|breadcrumbs?|share|sharing|social)([\s_-]|$)/i;
|
|
114
|
+
const within = (node, names) => {
|
|
115
|
+
for (let at = node.parentNode; at; at = at.parentNode) {
|
|
116
|
+
if (names.includes(at.nodeName)) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
};
|
|
122
|
+
/** An element of the site's frame: its menus, its header and footer, hidden parts, notices. */
|
|
123
|
+
function frame(node) {
|
|
124
|
+
if (FRAME_TAGS.has(node.nodeName)) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
// A header inside an article or the main part is the content's own heading.
|
|
128
|
+
if (node.nodeName === "HEADER" && !within(node, ["ARTICLE", "MAIN"])) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
const get = (name) => node.getAttribute?.(name) ?? "";
|
|
132
|
+
return (get("aria-hidden") === "true" ||
|
|
133
|
+
node.hasAttribute?.("hidden") ||
|
|
134
|
+
FRAME_ROLES.test(get("role")) ||
|
|
135
|
+
FRAME_NAMES.test(`${get("class")} ${get("id")}`));
|
|
136
|
+
}
|
|
137
|
+
// A rule of its own, not `remove`: turndown asks `remove` only of elements no rule converts,
|
|
138
|
+
// and links, pictures and headings have rules.
|
|
139
|
+
const content = linkBlocks(new TurndownService({ headingStyle: "atx", codeBlockStyle: "fenced" }).addRule("frame", {
|
|
140
|
+
filter: (node) => frame(node),
|
|
141
|
+
replacement: () => "",
|
|
142
|
+
}));
|
|
143
|
+
/**
|
|
144
|
+
* A web page's content as Markdown: its main part where it marks one (`<main>`,
|
|
145
|
+
* `role="main"`), else its body, without the site's frame — navigation, the site's header and
|
|
146
|
+
* footer, asides, forms, hidden menus, skip links, cookie notices, breadcrumbs.
|
|
147
|
+
*/
|
|
148
|
+
export function htmlToContent(html) {
|
|
149
|
+
const main = html.match(/<main\b[^>]*>([\s\S]*)<\/main>/i)?.[1] ??
|
|
150
|
+
html.match(/<[a-z]+\b[^>]*\brole\s*=\s*["']main["'][^>]*>[\s\S]*/i)?.[0];
|
|
151
|
+
const part = main ?? html.match(/<body[^>]*>([\s\S]*)<\/body>/i)?.[1] ?? html;
|
|
152
|
+
return (content
|
|
153
|
+
.turndown(part.replace(/<(script|style|svg)\b[\s\S]*?<\/\1>/gi, ""))
|
|
154
|
+
// A link that held only an icon, now gone, shows nothing.
|
|
155
|
+
.replace(/(?<!!)\[\]\([^)\n]*\)/g, "")
|
|
156
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
157
|
+
.trim());
|
|
158
|
+
}
|
|
159
|
+
/** The largest picture `srcset` offers up to this width; where all are wider, the smallest. */
|
|
160
|
+
const MAX_PICTURE_WIDTH = 2000;
|
|
161
|
+
/** Drawn smaller than this, a picture is an icon or a bullet, not content. */
|
|
162
|
+
const MIN_PICTURE_SIDE = 200;
|
|
163
|
+
/** What an address or a class says of an icon, a sprite, a spinner or a tracking pixel. */
|
|
164
|
+
const NO_PICTURE = /(^|[\s/_.-])(icons?|sprites?|emojis?|spinner|loader|spacer|pixel|tracking)([\s/_.-]|$)/i;
|
|
165
|
+
/** An attribute of a tag by its exact name: `src`, not the `src` of `data-src`. */
|
|
166
|
+
const attrOf = (tag, name) => tag
|
|
167
|
+
.match(new RegExp(`\\s${name}\\s*=\\s*("([^"]*)"|'([^']*)'|([^\\s>]+))`, "i"))
|
|
168
|
+
?.slice(2)
|
|
169
|
+
.find((v) => v !== undefined) ?? "";
|
|
170
|
+
/** The candidate of a `srcset` to keep, with its width or its density where the set says one. */
|
|
171
|
+
function fromSrcset(srcset) {
|
|
172
|
+
const candidates = srcset
|
|
173
|
+
.split(/,\s+/)
|
|
174
|
+
.map((part) => part.trim().split(/\s+/))
|
|
175
|
+
.filter(([url]) => url)
|
|
176
|
+
.map(([url, d = "1x"]) => ({
|
|
177
|
+
url: url.replace(/,$/, ""),
|
|
178
|
+
w: d.endsWith("w") ? Number.parseInt(d, 10) : null,
|
|
179
|
+
x: d.endsWith("x") ? Number.parseFloat(d) : null,
|
|
180
|
+
}));
|
|
181
|
+
const wide = candidates.filter((c) => c.w).sort((a, b) => (a.w ?? 0) - (b.w ?? 0));
|
|
182
|
+
if (wide.length) {
|
|
183
|
+
const pick = wide.filter((c) => (c.w ?? 0) <= MAX_PICTURE_WIDTH).pop() ?? wide[0];
|
|
184
|
+
return { url: pick.url, width: pick.w, density: 1 };
|
|
185
|
+
}
|
|
186
|
+
const dense = candidates.sort((a, b) => (a.x ?? 1) - (b.x ?? 1)).pop();
|
|
187
|
+
return dense ? { url: dense.url, width: null, density: dense.x ?? 1 } : null;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* A page's `<img>` tags, each pointed at one absolute address: the largest `srcset` entry up to
|
|
191
|
+
* 2000 px, else `src`, or what a lazy loader keeps in `data-srcset`/`data-src`. Icons, sprites,
|
|
192
|
+
* drawings (SVG) and pictures drawn under 200 px on their longer side go: what `htmlToMarkdown` and `htmlToContent`
|
|
193
|
+
* then write as `` is a picture of the page.
|
|
194
|
+
*/
|
|
195
|
+
export function absolutePictures(html, base) {
|
|
196
|
+
return html.replace(/<img\b[^>]*>/gi, (tag) => {
|
|
197
|
+
const lazy = (name) => attrOf(tag, `data-${name}`) || attrOf(tag, `data-lazy-${name}`);
|
|
198
|
+
const set = fromSrcset(lazy("srcset") || attrOf(tag, "srcset"));
|
|
199
|
+
const plain = lazy("src") || attrOf(tag, "data-original") || attrOf(tag, "src");
|
|
200
|
+
const raw = set?.url || plain;
|
|
201
|
+
let url;
|
|
202
|
+
try {
|
|
203
|
+
url = new URL(raw.trim(), base);
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return "";
|
|
207
|
+
}
|
|
208
|
+
const side = (name) => Number.parseInt(attrOf(tag, name), 10);
|
|
209
|
+
// The size it is drawn at, by its longer side, times the density `srcset` offers it in.
|
|
210
|
+
const drawn = Math.max(side("width") || 0, side("height") || 0) * (set?.density ?? 1);
|
|
211
|
+
const tiny = drawn > 0 && drawn < MIN_PICTURE_SIDE && !((set?.width ?? 0) >= MIN_PICTURE_SIDE);
|
|
212
|
+
if (!/^https?:$/.test(url.protocol) ||
|
|
213
|
+
/\.(svg|ico)$/i.test(url.pathname) ||
|
|
214
|
+
NO_PICTURE.test(url.pathname) ||
|
|
215
|
+
NO_PICTURE.test(attrOf(tag, "class")) ||
|
|
216
|
+
tiny) {
|
|
217
|
+
return "";
|
|
218
|
+
}
|
|
219
|
+
// Markdown ends an address at `)`.
|
|
220
|
+
const href = url.toString().replace(/\(/g, "%28").replace(/\)/g, "%29");
|
|
221
|
+
return `<img src="${href}" alt="${attrOf(tag, "alt").replace(/"/g, """)}">`;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/** The pictures Markdown shows, each once, in their order. */
|
|
225
|
+
export function picturesOf(markdown) {
|
|
226
|
+
const seen = new Map();
|
|
227
|
+
for (const m of markdown.matchAll(/!\[([^\]\n]*)\]\((https?:\/\/[^)\s]+)(?:\s+"[^"]*")?\)/g)) {
|
|
228
|
+
if (!seen.has(m[2])) {
|
|
229
|
+
seen.set(m[2], { url: m[2], alt: m[1].trim() });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return [...seen.values()];
|
|
233
|
+
}
|
|
47
234
|
export async function htmlToPdf(html) {
|
|
48
235
|
const context = await offlineContext();
|
|
49
236
|
try {
|