@engenty/wizards-runtime 0.16.1 → 0.17.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 +13 -6
- package/apps/runtime/dist/assistant/knows.js +18 -3
- package/apps/runtime/dist/broker/call.js +16 -3
- package/apps/runtime/dist/broker/grant.js +25 -1
- package/apps/runtime/dist/broker/index.js +2 -2
- package/apps/runtime/dist/broker/log.js +30 -11
- package/apps/runtime/dist/broker/rwx.js +14 -2
- package/apps/runtime/dist/broker/send.js +84 -32
- package/apps/runtime/dist/connectors/space-connections.js +47 -2
- package/apps/runtime/dist/db/control-schema.js +7 -0
- package/apps/runtime/dist/db/migrations/control/0014_run_index_generation.sql +1 -0
- package/apps/runtime/dist/db/migrations/control/0015_run_index_wake.sql +3 -0
- package/apps/runtime/dist/db/migrations/control/meta/0014_snapshot.json +629 -0
- package/apps/runtime/dist/db/migrations/control/meta/0015_snapshot.json +650 -0
- package/apps/runtime/dist/db/migrations/control/meta/_journal.json +14 -0
- package/apps/runtime/dist/db/migrations/tenant/0056_run_claim_rev.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0057_run_wake_at.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0056_snapshot.json +6520 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0057_snapshot.json +6527 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +14 -0
- package/apps/runtime/dist/db/schema.js +17 -3
- package/apps/runtime/dist/docs/dev/broker.md +72 -26
- package/apps/runtime/dist/docs/dev/plugins/space.md +13 -2
- package/apps/runtime/dist/docs/user-guide/capabilities.md +51 -7
- package/apps/runtime/dist/docs/user-guide/connectors.md +6 -3
- package/apps/runtime/dist/docs/user-guide/protocol.md +30 -4
- package/apps/runtime/dist/docs/user-guide/together.md +4 -4
- package/apps/runtime/dist/elements/knowledge.js +17 -12
- package/apps/runtime/dist/engine/runner.js +344 -52
- package/apps/runtime/dist/engine/steps.js +3 -2
- package/apps/runtime/dist/env.js +18 -0
- package/apps/runtime/dist/index.js +8 -0
- package/apps/runtime/dist/live/hub.js +17 -3
- package/apps/runtime/dist/live/origin.js +6 -4
- package/apps/runtime/dist/live/views.js +38 -7
- package/apps/runtime/dist/models.js +18 -0
- package/apps/runtime/dist/mounts/mounts.js +3 -0
- package/apps/runtime/dist/plugins/runs.js +3 -0
- package/apps/runtime/dist/prompts/mcp.md +3 -0
- package/apps/runtime/dist/prompts/tools.md +1 -1
- package/apps/runtime/dist/routes/api.js +2 -2
- package/apps/runtime/dist/routes/broker.js +59 -20
- package/apps/runtime/dist/routes/live.js +4 -2
- package/apps/runtime/dist/routes/runs.js +17 -14
- package/apps/runtime/dist/routes/studio.js +13 -2
- package/apps/runtime/dist/services/api.js +5 -3
- package/apps/runtime/dist/services/errors.js +1 -0
- package/apps/runtime/dist/services/knowledge.js +95 -28
- package/apps/runtime/dist/services/manage-reports.js +2 -0
- package/apps/runtime/dist/services/members-studio.js +22 -2
- package/apps/runtime/dist/services/query-vectors.js +60 -0
- package/apps/runtime/dist/services/relevance.js +12 -0
- package/apps/runtime/dist/services/runs.js +16 -2
- package/apps/runtime/dist/services/space-categories.js +83 -5
- package/apps/runtime/dist/services/space-index.js +99 -27
- package/apps/runtime/dist/services/space-origin.js +28 -11
- package/apps/runtime/dist/services/work-ai.js +15 -5
- package/apps/runtime/dist/sharing/index.js +3 -0
- package/apps/runtime/dist/sharing/jobs.js +2 -0
- package/apps/runtime/dist/sync/hub.js +17 -3
- package/apps/runtime/dist/tenants/control.js +46 -14
- package/apps/runtime/dist/texts.js +6 -4
- package/apps/runtime/dist/tools/index.js +13 -7
- package/apps/runtime/dist/tools/web-search.js +36 -0
- package/apps/web/dist/assets/{AboutDialog-C9SnsGno.js → AboutDialog-B2Xmxyj4.js} +1 -1
- package/apps/web/dist/assets/{BlockEditor-CUYFH_CU.js → BlockEditor-D0HoUSih.js} +1 -1
- package/apps/web/dist/assets/CapabilitiesPage-C1th2cZE.js +2 -0
- package/apps/web/dist/assets/{ChangelogDialog-B28TK333.js → ChangelogDialog-BIrJbAKi.js} +1 -1
- package/apps/web/dist/assets/{CodeEditor-D-Y16fdk.js → CodeEditor-DUVcXI5P.js} +1 -1
- package/apps/web/dist/assets/EditorPage-56sIqEfF.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-DXnk7m9P.js → FlowPreview-BIag8_Be.js} +1 -1
- package/apps/web/dist/assets/{LangSwitch-Dkd0FBIH.js → LangSwitch-CJ00IgaO.js} +1 -1
- package/apps/web/dist/assets/Marketplace-D7-ot9Dz.js +2 -0
- package/apps/web/dist/assets/{NewWizardChannels-B12l2_Jh.js → NewWizardChannels-DI3SQ4iS.js} +1 -1
- package/apps/web/dist/assets/NewWizardPage-CytheHxX.js +7 -0
- package/apps/web/dist/assets/OnboardingPage-_91Pa8Ad.js +1 -0
- package/apps/web/dist/assets/PreviewPane-BDsYm-_K.js +7 -0
- package/apps/web/dist/assets/ProtocolSection-D_zaxHEL.js +1 -0
- package/apps/web/dist/assets/PublicRunner-CEgXSk3r.js +1 -0
- package/apps/web/dist/assets/{ReadOnly-DwewuYi8.js → ReadOnly-CWf5x74a.js} +1 -1
- package/apps/web/dist/assets/SettingsPage-Dr8SDk-L.js +2 -0
- package/apps/web/dist/assets/SetupPage-B68n0l4I.js +1 -0
- package/apps/web/dist/assets/{SharePage-CZClatA_.js → SharePage-DfznN7ar.js} +1 -1
- package/apps/web/dist/assets/SkillEditor-BIcIczRc.js +1 -0
- package/apps/web/dist/assets/{Skills-Bv6XbKxc.js → Skills-CN4QE7Kx.js} +6 -6
- package/apps/web/dist/assets/SpacePage-DiVTwhnJ.js +2 -0
- package/apps/web/dist/assets/{UpdateCheck-CLPUsKVT.js → UpdateCheck-QhBI3rlQ.js} +2 -2
- package/apps/web/dist/assets/changelog-a_CuplMX.js +1 -0
- package/apps/web/dist/assets/{code-editor-Ca5EAxmd.js → code-editor-DkPo1C-L.js} +2 -2
- package/apps/web/dist/assets/{host-CJdLkzfY.js → host-D6H6Z9Jp.js} +18 -18
- package/apps/web/dist/assets/i18n-BaPKbQ1s.js +55 -0
- package/apps/web/dist/assets/{index-w-3J3ZJ9.css → index-CCUG6Vnh.css} +1 -1
- package/apps/web/dist/assets/{index-CmonJZHC.js → index-De0Wb42y.js} +4 -4
- package/apps/web/dist/assets/{read-aloud-Bfp9_bUY.js → read-aloud-CnFwDSGD.js} +1 -1
- package/apps/web/dist/assets/{runners-CUpBpSji.js → runners-0sTPbWzr.js} +1 -1
- package/apps/web/dist/assets/session-BMiUrqHg.js +2 -0
- package/apps/web/dist/assets/theme-CkNTe-_Z.js +12 -0
- package/apps/web/dist/assets/ui-DAk_vjrL.js +16 -0
- package/apps/web/dist/assets/{useMutation-1fV4Nnxq.js → useMutation-isHfB33W.js} +1 -1
- package/apps/web/dist/assets/{viewers-ChriFcRy.js → viewers-ijNZHJFG.js} +2 -2
- package/apps/web/dist/index.html +12 -12
- package/apps/web/dist/mcp/flow.html +66 -66
- package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +15 -4
- package/node_modules/@engenty/wizards/dist/plugin/thread.js +30 -0
- package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +13 -1
- package/node_modules/@engenty/wizards-shared/dist/connectors.js +8 -0
- package/node_modules/@engenty/wizards-shared/dist/definition.d.ts +168 -0
- package/node_modules/@engenty/wizards-shared/dist/definition.js +52 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +18 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +18 -0
- package/node_modules/@engenty/wizards-shared/dist/fit.d.ts +2 -0
- package/node_modules/@engenty/wizards-shared/dist/fit.js +4 -0
- package/node_modules/@engenty/wizards-shared/dist/knowledge.d.ts +5 -0
- package/node_modules/@engenty/wizards-shared/dist/run.d.ts +11 -1
- package/node_modules/@engenty/wizards-shared/dist/run.js +14 -0
- package/node_modules/@engenty/wizards-shared/dist/runner-texts.d.ts +7 -1
- package/node_modules/@engenty/wizards-shared/dist/runner-texts.js +6 -0
- package/node_modules/@engenty/wizards-shared/dist/spaces.d.ts +3 -2
- package/node_modules/@engenty/wizards-shared/dist/spaces.js +12 -3
- package/package.json +1 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/plugin.json +1 -1
- package/apps/web/dist/assets/CapabilitiesPage-DvSSybt0.js +0 -2
- package/apps/web/dist/assets/EditorPage-Duu8swX6.js +0 -3
- package/apps/web/dist/assets/Marketplace-Cty40VwG.js +0 -2
- package/apps/web/dist/assets/NewWizardPage-B10K8vND.js +0 -7
- package/apps/web/dist/assets/OnboardingPage-D3SEpTmt.js +0 -1
- package/apps/web/dist/assets/PreviewPane-DhhQ06Ic.js +0 -7
- package/apps/web/dist/assets/PublicRunner-B8ncIxIP.js +0 -1
- package/apps/web/dist/assets/SettingsPage-C-gGIJLJ.js +0 -2
- package/apps/web/dist/assets/SetupPage-CdsrHW-w.js +0 -1
- package/apps/web/dist/assets/SkillEditor-BaEiqqXl.js +0 -1
- package/apps/web/dist/assets/SpacePage-CYhxcjj0.js +0 -2
- package/apps/web/dist/assets/changelog-y5oxYzPd.js +0 -1
- package/apps/web/dist/assets/connection-sign-in-DECedzrs.js +0 -1
- package/apps/web/dist/assets/i18n-BBzMbGoo.js +0 -55
- package/apps/web/dist/assets/session-CMQo8P-G.js +0 -2
- package/apps/web/dist/assets/theme-D6KUBJnh.js +0 -11
- package/apps/web/dist/assets/ui-BCfu5sia.js +0 -16
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { formatTableCell } from "@engenty/wizards-shared/engenty/data-tables";
|
|
2
2
|
import { slugOf, } from "@engenty/wizards-shared/knowledge";
|
|
3
3
|
import { and, asc, count, eq, inArray, isNotNull, isNull, sql } from "drizzle-orm";
|
|
4
|
-
import { assertRwx, pageResources, readable, tableResources } from "../broker/rwx.js";
|
|
4
|
+
import { assertRwx, brokerOn, pageResources, readable, readableMounts, tableResources, } from "../broker/rwx.js";
|
|
5
5
|
import { db, schema } from "../db/client.js";
|
|
6
6
|
import { embeddingModel, systemOneAccess } from "../models.js";
|
|
7
7
|
import { prompt } from "../prompts.js";
|
|
8
8
|
import { notFound, ServiceError } from "./errors.js";
|
|
9
|
-
import { categoryLines, categoryRows, dateOf, filterItems, itemCategoriesOf, listCategories, numberOf, } from "./space-categories.js";
|
|
9
|
+
import { categoriesOver, categoryLines, categoryRows, categoryUse, dateOf, filterItems, itemCategoriesOf, listCategories, numberOf, } from "./space-categories.js";
|
|
10
10
|
import { ownedSpace } from "./spaces.js";
|
|
11
11
|
/** Gives each item without a slug one, unique among its siblings, and keeps it. */
|
|
12
12
|
async function slugsFor(rows, siblingsOf, save) {
|
|
@@ -240,8 +240,9 @@ export async function listKnowledge(userId, spaceId, who) {
|
|
|
240
240
|
embeddingModel()
|
|
241
241
|
.then(Boolean)
|
|
242
242
|
.catch(() => false),
|
|
243
|
+
// Clef in Ollama on this computer is too slow for a search (checksRelevance).
|
|
243
244
|
systemOneAccess()
|
|
244
|
-
.then(Boolean)
|
|
245
|
+
.then((access) => Boolean(access && !access.local))
|
|
245
246
|
.catch(() => false),
|
|
246
247
|
]);
|
|
247
248
|
const tPaths = await tablePaths(space.id);
|
|
@@ -518,8 +519,11 @@ export async function readPage(spaceId, path, opts, who) {
|
|
|
518
519
|
const node = tree.get(page.id);
|
|
519
520
|
const categories = await categoryRows(spaceId);
|
|
520
521
|
const lines = categoryLines(categories, (await itemCategoriesOf([{ pageId: page.id }])).get(`p:${page.id}`));
|
|
521
|
-
|
|
522
|
-
|
|
522
|
+
// Its sub-pages, as far as `who` may read them: one closed to them is not named.
|
|
523
|
+
const under = [...tree.values()].filter((n) => n.parentId === page.id);
|
|
524
|
+
const okUnder = await readable(spaceId, who, await pageResources(under.map((n) => n.id)));
|
|
525
|
+
const children = under
|
|
526
|
+
.filter((n) => okUnder.has(n.id))
|
|
523
527
|
.sort((a, b) => a.position - b.position)
|
|
524
528
|
.map((n) => n.path);
|
|
525
529
|
const original = page.fileId ? (await filePaths(spaceId)).get(page.fileId) : undefined;
|
|
@@ -649,11 +653,10 @@ export async function readTable(spaceId, path, opts, who) {
|
|
|
649
653
|
* has; with one, the items it leaves that `who` may read, with their paths and categories.
|
|
650
654
|
*/
|
|
651
655
|
export async function listForStep(spaceId, where, who) {
|
|
652
|
-
const categories = await listCategories(spaceId);
|
|
653
|
-
const taken = categories.filter((c) => !c.proposed);
|
|
654
656
|
if (!Object.keys(where ?? {}).length) {
|
|
657
|
+
const categories = (await categoriesFor(spaceId, who)).filter((c) => !c.proposed);
|
|
655
658
|
return {
|
|
656
|
-
categories:
|
|
659
|
+
categories: categories.map((c) => ({
|
|
657
660
|
name: c.name,
|
|
658
661
|
type: c.type,
|
|
659
662
|
...(c.unit ? { unit: c.unit } : {}),
|
|
@@ -666,6 +669,7 @@ export async function listForStep(spaceId, where, who) {
|
|
|
666
669
|
hint: 'Pass `where` to list the items of values, e.g. { "Baustelle": "Graz Süd" }.',
|
|
667
670
|
};
|
|
668
671
|
}
|
|
672
|
+
const taken = (await categoryRows(spaceId)).filter((c) => !c.proposed);
|
|
669
673
|
const filtered = await filterItems(spaceId, where);
|
|
670
674
|
const [tree, tPaths, fPaths] = await Promise.all([
|
|
671
675
|
pageTree(spaceId),
|
|
@@ -728,34 +732,97 @@ export async function listForStep(spaceId, where, who) {
|
|
|
728
732
|
];
|
|
729
733
|
return { items: items.slice(0, 200), total: items.length };
|
|
730
734
|
}
|
|
731
|
-
/**
|
|
732
|
-
|
|
735
|
+
/**
|
|
736
|
+
* The space's categories as `who` sees them: how many items have each, a choice's values and the
|
|
737
|
+
* ranges, counted over the pages, tables, files and mounts they may read. Whoever may read all of
|
|
738
|
+
* it gets them as the space has them.
|
|
739
|
+
*/
|
|
740
|
+
export async function categoriesFor(spaceId, who) {
|
|
741
|
+
const all = await listCategories(spaceId);
|
|
742
|
+
if (!(all.length && (await brokerOn()))) {
|
|
743
|
+
return all;
|
|
744
|
+
}
|
|
745
|
+
const use = await categoryUse(spaceId);
|
|
746
|
+
const places = [...use.items, ...use.values];
|
|
747
|
+
const ids = (key) => [
|
|
748
|
+
...new Set(places.flatMap((p) => (p[key] ? [p[key]] : []))),
|
|
749
|
+
];
|
|
750
|
+
const pages = await readable(spaceId, who, await pageResources(ids("pageId")));
|
|
751
|
+
const tables = await readable(spaceId, who, await tableResources(ids("tableId")));
|
|
752
|
+
const files = await readable(spaceId, who, ids("fileId").map((id) => ({ kind: "document", id })));
|
|
753
|
+
const mounts = await readableMounts(spaceId, who, ids("mountId"));
|
|
754
|
+
const open = (p) => p.pageId
|
|
755
|
+
? pages.has(p.pageId)
|
|
756
|
+
: p.tableId
|
|
757
|
+
? tables.has(p.tableId)
|
|
758
|
+
: p.fileId
|
|
759
|
+
? files.has(p.fileId)
|
|
760
|
+
: p.mountId
|
|
761
|
+
? mounts.has(p.mountId)
|
|
762
|
+
: false;
|
|
763
|
+
if (places.every(open)) {
|
|
764
|
+
return all;
|
|
765
|
+
}
|
|
766
|
+
return categoriesOver(all, { items: use.items.filter(open), values: use.values.filter(open) });
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* Which kinds of Knowledge a space holds at all: which tools a caller is offered. It says nothing
|
|
770
|
+
* of what they may read: each read of a tool is the broker's, and a refusal names the item.
|
|
771
|
+
*/
|
|
772
|
+
export async function knowledgeKinds(spaceId) {
|
|
773
|
+
const [page, table, file] = await Promise.all([
|
|
774
|
+
db.query.spacePage.findFirst({
|
|
775
|
+
where: and(eq(schema.spacePage.spaceId, spaceId), isNull(schema.spacePage.wizardId)),
|
|
776
|
+
columns: { id: true },
|
|
777
|
+
}),
|
|
778
|
+
db.query.spaceTable.findFirst({
|
|
779
|
+
where: and(eq(schema.spaceTable.spaceId, spaceId), isNull(schema.spaceTable.wizardId)),
|
|
780
|
+
columns: { id: true },
|
|
781
|
+
}),
|
|
782
|
+
db.query.spaceFile.findFirst({
|
|
783
|
+
where: and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.kind, "document")),
|
|
784
|
+
columns: { id: true },
|
|
785
|
+
}),
|
|
786
|
+
]);
|
|
787
|
+
return { pages: Boolean(page), tables: Boolean(table), files: Boolean(file) };
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* How much Knowledge a space holds for `who`: the pages, tables and documents they may read. What
|
|
791
|
+
* a step's instructions, Goose and the space's details say counts nothing else.
|
|
792
|
+
*/
|
|
793
|
+
export async function knowledgeCounts(spaceId, who) {
|
|
733
794
|
const [pages, tables, files] = await Promise.all([
|
|
734
|
-
db
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
db
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
db
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
795
|
+
db.query.spacePage.findMany({
|
|
796
|
+
where: and(eq(schema.spacePage.spaceId, spaceId), isNull(schema.spacePage.wizardId)),
|
|
797
|
+
columns: { id: true, fileId: true },
|
|
798
|
+
}),
|
|
799
|
+
db.query.spaceTable.findMany({
|
|
800
|
+
where: and(eq(schema.spaceTable.spaceId, spaceId), isNull(schema.spaceTable.wizardId)),
|
|
801
|
+
columns: { id: true, fileId: true },
|
|
802
|
+
}),
|
|
803
|
+
db.query.spaceFile.findMany({
|
|
804
|
+
where: and(eq(schema.spaceFile.spaceId, spaceId), eq(schema.spaceFile.kind, "document")),
|
|
805
|
+
columns: { id: true },
|
|
806
|
+
}),
|
|
807
|
+
]);
|
|
808
|
+
const ok = await readable(spaceId, who, [
|
|
809
|
+
...pages.map((p) => ({ kind: "page", id: p.id, fileId: p.fileId })),
|
|
810
|
+
...tables.map((t) => ({ kind: "table", id: t.id, fileId: t.fileId })),
|
|
811
|
+
...files.map((f) => ({ kind: "document", id: f.id })),
|
|
746
812
|
]);
|
|
747
|
-
|
|
813
|
+
const read = (rows) => rows.filter((r) => ok.has(r.id)).length;
|
|
814
|
+
return { pages: read(pages), tables: read(tables), files: read(files) };
|
|
748
815
|
}
|
|
749
816
|
/**
|
|
750
|
-
* What
|
|
751
|
-
* by, the tools to find and read. Empty when
|
|
817
|
+
* What a step of the space is told about Knowledge: how much of it `who` may read, the categories
|
|
818
|
+
* to filter by as they see them, the tools to find and read. Empty when that is nothing.
|
|
752
819
|
*/
|
|
753
|
-
export async function knowledgeBlock(spaceId) {
|
|
754
|
-
const counts = await knowledgeCounts(spaceId);
|
|
820
|
+
export async function knowledgeBlock(spaceId, who) {
|
|
821
|
+
const counts = await knowledgeCounts(spaceId, who);
|
|
755
822
|
if (!(counts.pages || counts.tables || counts.files)) {
|
|
756
823
|
return "";
|
|
757
824
|
}
|
|
758
|
-
const categories = (await
|
|
825
|
+
const categories = (await categoriesFor(spaceId, who)).filter((c) => !c.proposed && c.count);
|
|
759
826
|
const what = [
|
|
760
827
|
counts.pages ? `${counts.pages} pages` : "",
|
|
761
828
|
counts.tables ? `${counts.tables} tables` : "",
|
|
@@ -110,6 +110,8 @@ export async function reportAllWizards() {
|
|
|
110
110
|
}
|
|
111
111
|
const STATE = {
|
|
112
112
|
waiting_input: "waiting",
|
|
113
|
+
// Manage knows five states; asleep or with a person, the run waits.
|
|
114
|
+
sleeping: "waiting",
|
|
113
115
|
running: "running",
|
|
114
116
|
done: "finished",
|
|
115
117
|
failed: "failed",
|
|
@@ -61,9 +61,10 @@ const PUBLISHING = [
|
|
|
61
61
|
];
|
|
62
62
|
/**
|
|
63
63
|
* The space of an item, by the kind and id the studio's addresses name it with
|
|
64
|
-
* (`/spaces/:id`, `/wizards/:id`, …; `runs` for a run's wizard), and what follows them.
|
|
64
|
+
* (`/spaces/:id`, `/wizards/:id`, …; `runs` for a run's wizard), and what follows them. `null`
|
|
65
|
+
* for what is in no space, or in none that is still there.
|
|
65
66
|
*/
|
|
66
|
-
async function spaceOfItem(kind, id, sub) {
|
|
67
|
+
export async function spaceOfItem(kind, id, sub) {
|
|
67
68
|
const one = async (found) => (await found)?.spaceId ?? null;
|
|
68
69
|
const tenant = currentTenant();
|
|
69
70
|
switch (kind) {
|
|
@@ -120,6 +121,9 @@ async function spaceOfItem(kind, id, sub) {
|
|
|
120
121
|
where: eq(schema.spaceConnection.id, id),
|
|
121
122
|
columns: { spaceId: true },
|
|
122
123
|
}));
|
|
124
|
+
case "presence":
|
|
125
|
+
// Who is in a wizard, page or table (`/presence/:kind/:item`) is in that item's space.
|
|
126
|
+
return sub ? spaceOfItem(`${id}s`, sub) : null;
|
|
123
127
|
default:
|
|
124
128
|
return null;
|
|
125
129
|
}
|
|
@@ -131,6 +135,22 @@ async function spaceOfItem(kind, id, sub) {
|
|
|
131
135
|
export async function closedToMe(spaceId) {
|
|
132
136
|
return (await ruledMember()) && !(await openToMembers(spaceId));
|
|
133
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* The spaces whose Studio the person at hand reaches, where the rule narrows them: the ones open
|
|
140
|
+
* to members. `null` for everyone the rule does not count for, who reaches every space. For what
|
|
141
|
+
* is sent unasked (the live stream, the sync's listing): a space that is not among them, closed
|
|
142
|
+
* or gone, is not named.
|
|
143
|
+
*/
|
|
144
|
+
export async function studioSpaces() {
|
|
145
|
+
if (!(await ruledMember())) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
const open = await db.query.space.findMany({
|
|
149
|
+
where: and(eq(schema.space.tenantId, currentTenant()), eq(schema.space.membersStudio, true)),
|
|
150
|
+
columns: { id: true },
|
|
151
|
+
});
|
|
152
|
+
return new Set(open.map((s) => s.id));
|
|
153
|
+
}
|
|
134
154
|
/**
|
|
135
155
|
* The studio's „closed“ answer for an item of a space closed to the person at hand: the same 403
|
|
136
156
|
* through every door (the studio API, MCP, `/api/v1`, Goose). `kind` as the studio's addresses
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
2
|
+
import { embedMany } from "ai";
|
|
3
|
+
import { embeddingModel } from "../models.js";
|
|
4
|
+
/**
|
|
5
|
+
* The vector of a search's query. The embedding model is the index's too: in an Ollama that is
|
|
6
|
+
* embedding a folder, a query's call waits behind every passage sent before it (36 s measured).
|
|
7
|
+
* So a query is embedded once (the palette asks twice, a client space's search once per space),
|
|
8
|
+
* the indexers send nothing new while one is on its way (`searchesFirst`), and a search waits
|
|
9
|
+
* only as long as it is given.
|
|
10
|
+
*/
|
|
11
|
+
/** How many queries' vectors are kept. */
|
|
12
|
+
const KEPT = 64;
|
|
13
|
+
/** An embedding model that has not answered in this time is not going to. */
|
|
14
|
+
const GIVE_UP_MS = 120_000;
|
|
15
|
+
const vectors = new Map();
|
|
16
|
+
let pending = 0;
|
|
17
|
+
let waiting = [];
|
|
18
|
+
/**
|
|
19
|
+
* The query's vector and the model it comes from. Null without an embedding model or when it
|
|
20
|
+
* failed; undefined when it did not come within `waitMs` (it is kept for the next ask).
|
|
21
|
+
*/
|
|
22
|
+
export async function queryVector(query, waitMs) {
|
|
23
|
+
const resolved = await embeddingModel().catch(() => null);
|
|
24
|
+
if (!resolved) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const key = `${resolved.ref}\n${query}`;
|
|
28
|
+
let found = vectors.get(key);
|
|
29
|
+
if (!found) {
|
|
30
|
+
pending++;
|
|
31
|
+
found = embedMany({
|
|
32
|
+
model: resolved.model,
|
|
33
|
+
values: [query],
|
|
34
|
+
abortSignal: AbortSignal.timeout(GIVE_UP_MS),
|
|
35
|
+
})
|
|
36
|
+
.then((r) => ({ vector: Array.from(r.embeddings[0]), model: resolved.ref }))
|
|
37
|
+
.catch(() => {
|
|
38
|
+
vectors.delete(key);
|
|
39
|
+
return null;
|
|
40
|
+
})
|
|
41
|
+
.finally(() => {
|
|
42
|
+
pending--;
|
|
43
|
+
if (!pending) {
|
|
44
|
+
for (const go of waiting) {
|
|
45
|
+
go();
|
|
46
|
+
}
|
|
47
|
+
waiting = [];
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
vectors.set(key, found);
|
|
51
|
+
if (vectors.size > KEPT) {
|
|
52
|
+
vectors.delete(vectors.keys().next().value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return Promise.race([found, sleep(waitMs, undefined, { ref: false })]);
|
|
56
|
+
}
|
|
57
|
+
/** Resolves once no query's vector is on its way: an indexer calls it before it sends a batch. */
|
|
58
|
+
export function searchesFirst() {
|
|
59
|
+
return pending ? new Promise((resolve) => waiting.push(resolve)) : Promise.resolve();
|
|
60
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { decide } from "../engine/decide.js";
|
|
2
|
+
import { systemOneAccess } from "../models.js";
|
|
2
3
|
import { prompt } from "../prompts.js";
|
|
3
4
|
/**
|
|
4
5
|
* The last step of a search of Knowledge: does each candidate the index found help answer the
|
|
@@ -15,6 +16,14 @@ const clip = (value, max) => {
|
|
|
15
16
|
const s = value.replace(/\s+/g, " ").trim();
|
|
16
17
|
return s.length > max ? `${s.slice(0, max - 1)}…` : s;
|
|
17
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* Whether a search's candidates are checked. Not by Clef in Ollama on this computer: on an M4
|
|
21
|
+
* it took 18 s for one candidate and a minute for eight, never within TIMEOUT_MS, and the
|
|
22
|
+
* embedding model waited behind it. Without a decision model, the classifier class's model answers.
|
|
23
|
+
*/
|
|
24
|
+
export async function checksRelevance() {
|
|
25
|
+
return !(await systemOneAccess().catch(() => null))?.local;
|
|
26
|
+
}
|
|
18
27
|
/**
|
|
19
28
|
* P(relevant) for each candidate, in their order. Null for all of them when no way decided in
|
|
20
29
|
* time: the search then keeps the order the index gave.
|
|
@@ -23,6 +32,9 @@ export async function judgeRelevance(query, candidates, signal) {
|
|
|
23
32
|
if (!candidates.length) {
|
|
24
33
|
return [];
|
|
25
34
|
}
|
|
35
|
+
if (!(await checksRelevance())) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
26
38
|
const questions = {};
|
|
27
39
|
const state = {
|
|
28
40
|
query: clip(query, 600),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FIELD_WAYS, formatsFor, isDecidedField, shownFields, slotText, wizardLang, } from "@engenty/wizards-shared/definition";
|
|
1
|
+
import { FIELD_WAYS, formatsFor, isDecidedField, shownFields, slotText, waitDurationMs, wizardLang, } from "@engenty/wizards-shared/definition";
|
|
2
2
|
import { note } from "@engenty/wizards-shared/run";
|
|
3
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";
|
|
@@ -232,6 +232,14 @@ async function waitingFor(run, current, draft) {
|
|
|
232
232
|
hint: prompt("mcp", "hint.sign_in"),
|
|
233
233
|
};
|
|
234
234
|
}
|
|
235
|
+
if (run.status === "sleeping" && current) {
|
|
236
|
+
return {
|
|
237
|
+
wait: current.id,
|
|
238
|
+
title: current.title,
|
|
239
|
+
until: run.wakeAt?.toISOString() ?? null,
|
|
240
|
+
hint: prompt("mcp", "hint.sleeping"),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
235
243
|
if (run.status !== "waiting_input" || !current) {
|
|
236
244
|
return undefined;
|
|
237
245
|
}
|
|
@@ -464,7 +472,9 @@ export async function draftStepView(userId, wizardId, stepId, lang) {
|
|
|
464
472
|
? "waiting_input"
|
|
465
473
|
: step.type === "result"
|
|
466
474
|
? "done"
|
|
467
|
-
: "
|
|
475
|
+
: step.type === "wait"
|
|
476
|
+
? "sleeping"
|
|
477
|
+
: "running";
|
|
468
478
|
const now = new Date();
|
|
469
479
|
// That run lends its id too: the outputs' files are reached through it.
|
|
470
480
|
const run = {
|
|
@@ -493,6 +503,9 @@ export async function draftStepView(userId, wizardId, stepId, lang) {
|
|
|
493
503
|
}),
|
|
494
504
|
definition: def,
|
|
495
505
|
status,
|
|
506
|
+
wakeAt: step.type === "wait" && step.for ? new Date(now.getTime() + waitDurationMs(step.for)) : null,
|
|
507
|
+
claim: 0,
|
|
508
|
+
rev: 0,
|
|
496
509
|
cursor: step.id,
|
|
497
510
|
state,
|
|
498
511
|
lang: lang && lang !== wizardLang(def) && (await storedTranslation(w.id, lang)) ? lang : null,
|
|
@@ -525,6 +538,7 @@ export async function listRuns(userId, wizardId) {
|
|
|
525
538
|
id: r.id,
|
|
526
539
|
mode: r.mode,
|
|
527
540
|
status: r.status,
|
|
541
|
+
wakeAt: r.wakeAt?.toISOString() ?? null,
|
|
528
542
|
version: r.version,
|
|
529
543
|
createdAt: r.createdAt.toISOString(),
|
|
530
544
|
credits: Math.ceil(r.costMicros / MICROS_PER_CREDIT),
|
|
@@ -232,17 +232,95 @@ export async function listCategories(spaceId) {
|
|
|
232
232
|
count: v.n,
|
|
233
233
|
summary: Boolean(v.summary),
|
|
234
234
|
})),
|
|
235
|
-
range: c.type
|
|
236
|
-
? { min: range.minNum, max: range.maxNum ?? range.minNum }
|
|
237
|
-
: c.type === "date" && range?.minAt !== null && range?.minAt !== undefined
|
|
238
|
-
? { min: dateText(range.minAt), max: dateText(range.maxAt ?? range.minAt) }
|
|
239
|
-
: null,
|
|
235
|
+
range: rangeOf(c.type, range),
|
|
240
236
|
alike: [],
|
|
241
237
|
};
|
|
242
238
|
category.alike = alikeValues(category.values);
|
|
243
239
|
return category;
|
|
244
240
|
});
|
|
245
241
|
}
|
|
242
|
+
/** A number's or a date's lowest and highest value, as a category shows them. */
|
|
243
|
+
function rangeOf(type, of) {
|
|
244
|
+
if (type === "number" && of?.minNum !== null && of?.minNum !== undefined) {
|
|
245
|
+
return { min: of.minNum, max: of.maxNum ?? of.minNum };
|
|
246
|
+
}
|
|
247
|
+
if (type === "date" && of?.minAt !== null && of?.minAt !== undefined) {
|
|
248
|
+
return { min: dateText(of.minAt), max: dateText(of.maxAt ?? of.minAt) };
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
/** The use of the space's categories by place, for a reader who may not read every place. */
|
|
253
|
+
export async function categoryUse(spaceId) {
|
|
254
|
+
const ids = (await categoryRows(spaceId)).map((c) => c.id);
|
|
255
|
+
if (!ids.length) {
|
|
256
|
+
return { items: [], values: [] };
|
|
257
|
+
}
|
|
258
|
+
const t = schema.spaceItemCategory;
|
|
259
|
+
const row = schema.spaceTableRow;
|
|
260
|
+
const file = schema.spaceMountFile;
|
|
261
|
+
// A row lies in its table, a mounted file in its mount.
|
|
262
|
+
const tableId = sql `coalesce(${t.tableId}, ${row.tableId})`;
|
|
263
|
+
const place = { pageId: t.pageId, tableId, fileId: t.fileId, mountId: file.mountId };
|
|
264
|
+
const [items, values] = await Promise.all([
|
|
265
|
+
db
|
|
266
|
+
.select({
|
|
267
|
+
...place,
|
|
268
|
+
categoryId: t.categoryId,
|
|
269
|
+
n: sql `count(DISTINCT coalesce(${t.pageId}, ${t.rowId}, ${t.tableId}, ${t.fileId}, ${t.mountFileId}))`,
|
|
270
|
+
minNum: sql `min(${t.num})`,
|
|
271
|
+
maxNum: sql `max(${t.num})`,
|
|
272
|
+
minAt: sql `min(${t.at})`,
|
|
273
|
+
maxAt: sql `max(${t.at})`,
|
|
274
|
+
})
|
|
275
|
+
.from(t)
|
|
276
|
+
.leftJoin(row, eq(row.id, t.rowId))
|
|
277
|
+
.leftJoin(file, eq(file.id, t.mountFileId))
|
|
278
|
+
.where(inArray(t.categoryId, ids))
|
|
279
|
+
.groupBy(t.categoryId, t.pageId, tableId, t.fileId, file.mountId),
|
|
280
|
+
db
|
|
281
|
+
.select({ ...place, valueId: t.valueId, n: sql `count(*)` })
|
|
282
|
+
.from(t)
|
|
283
|
+
.leftJoin(row, eq(row.id, t.rowId))
|
|
284
|
+
.leftJoin(file, eq(file.id, t.mountFileId))
|
|
285
|
+
.where(and(inArray(t.categoryId, ids), isNotNull(t.valueId)))
|
|
286
|
+
.groupBy(t.valueId, t.pageId, tableId, t.fileId, file.mountId),
|
|
287
|
+
]);
|
|
288
|
+
return { items, values };
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* The categories counted over a part of their use: how many items have each, a choice's values
|
|
292
|
+
* and the ranges, from `use` alone. A value that only items outside it have is left out; one no
|
|
293
|
+
* item has stays, as the category's own.
|
|
294
|
+
*/
|
|
295
|
+
export function categoriesOver(all, use) {
|
|
296
|
+
const byValue = new Map();
|
|
297
|
+
for (const u of use.values) {
|
|
298
|
+
if (u.valueId) {
|
|
299
|
+
byValue.set(u.valueId, (byValue.get(u.valueId) ?? 0) + u.n);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const least = (list) => list.reduce((m, x) => (x === null ? m : m === null ? x : Math.min(m, x)), null);
|
|
303
|
+
const most = (list) => list.reduce((m, x) => (x === null ? m : m === null ? x : Math.max(m, x)), null);
|
|
304
|
+
return all.map((c) => {
|
|
305
|
+
const mine = use.items.filter((u) => u.categoryId === c.id);
|
|
306
|
+
const values = c.values
|
|
307
|
+
.filter((v) => byValue.has(v.id) || v.count === 0)
|
|
308
|
+
.map((v) => ({ ...v, count: byValue.get(v.id) ?? 0 }));
|
|
309
|
+
const kept = new Set(values.map((v) => v.id));
|
|
310
|
+
return {
|
|
311
|
+
...c,
|
|
312
|
+
count: mine.reduce((sum, u) => sum + u.n, 0),
|
|
313
|
+
values,
|
|
314
|
+
range: rangeOf(c.type, {
|
|
315
|
+
minNum: least(mine.map((u) => u.minNum)),
|
|
316
|
+
maxNum: most(mine.map((u) => u.maxNum)),
|
|
317
|
+
minAt: least(mine.map((u) => u.minAt)),
|
|
318
|
+
maxAt: most(mine.map((u) => u.maxAt)),
|
|
319
|
+
}),
|
|
320
|
+
alike: c.alike.filter(([a, b]) => kept.has(a) && kept.has(b)),
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
}
|
|
246
324
|
/** Pairs of a choice's values that look like one value written two ways, to offer merging. */
|
|
247
325
|
function alikeValues(list) {
|
|
248
326
|
const pairs = [];
|