@engenty/wizards-runtime 0.16.2 → 0.17.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 +13 -6
- package/apps/runtime/dist/app.js +2 -0
- package/apps/runtime/dist/assistant/knows.js +15 -2
- package/apps/runtime/dist/broker/account.js +20 -1
- package/apps/runtime/dist/broker/call.js +131 -22
- package/apps/runtime/dist/broker/grant.js +25 -1
- package/apps/runtime/dist/broker/host.js +8 -2
- package/apps/runtime/dist/broker/index.js +5 -4
- package/apps/runtime/dist/broker/log.js +178 -24
- package/apps/runtime/dist/broker/over.js +38 -0
- package/apps/runtime/dist/broker/rights.js +192 -0
- package/apps/runtime/dist/broker/rwx.js +309 -53
- package/apps/runtime/dist/broker/send.js +109 -44
- package/apps/runtime/dist/bundles/fetch.js +119 -0
- package/apps/runtime/dist/bundles/index.js +621 -0
- package/apps/runtime/dist/bundles/read.js +607 -0
- package/apps/runtime/dist/connectors/external.js +532 -61
- package/apps/runtime/dist/connectors/person-sign-ins.js +1 -0
- package/apps/runtime/dist/connectors/reload.js +73 -0
- package/apps/runtime/dist/connectors/space-connections.js +118 -6
- package/apps/runtime/dist/db/migrations/tenant/0058_mcp_state_across_roles.sql +26 -0
- package/apps/runtime/dist/db/migrations/tenant/0059_space_connection_paused.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0060_space_bundle.sql +21 -0
- package/apps/runtime/dist/db/migrations/tenant/0061_space_connection_limits.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0058_snapshot.json +6696 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0059_snapshot.json +6710 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0060_snapshot.json +6857 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0061_snapshot.json +6871 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +28 -0
- package/apps/runtime/dist/db/schema.js +81 -3
- package/apps/runtime/dist/docs/dev/broker.md +316 -50
- package/apps/runtime/dist/docs/dev/plugins/space.md +13 -2
- package/apps/runtime/dist/docs/user-guide/capabilities.md +172 -10
- package/apps/runtime/dist/docs/user-guide/connectors.md +57 -7
- package/apps/runtime/dist/docs/user-guide/protocol.md +44 -7
- package/apps/runtime/dist/docs/user-guide/test-and-share.md +1 -1
- package/apps/runtime/dist/docs/user-guide/together.md +4 -4
- package/apps/runtime/dist/elements/caller.js +2 -2
- package/apps/runtime/dist/elements/knowledge.js +16 -12
- package/apps/runtime/dist/engenty/connections-external/build-connector.js +23 -8
- package/apps/runtime/dist/engenty/connections-external/import-service.js +18 -9
- package/apps/runtime/dist/engenty/connections-external/importer/classify.js +12 -0
- package/apps/runtime/dist/engenty/connections-external/invoke/http-invoker.js +1 -0
- package/apps/runtime/dist/engenty/connections-external/net/guarded-fetch.js +6 -3
- package/apps/runtime/dist/engenty/connections-sdk/oauth2.js +2 -2
- package/apps/runtime/dist/engenty/shims/web-ingest.js +64 -0
- package/apps/runtime/dist/engine/resources.js +15 -6
- package/apps/runtime/dist/engine/runner.js +2 -1
- package/apps/runtime/dist/engine/steps.js +3 -2
- package/apps/runtime/dist/env.js +2 -0
- package/apps/runtime/dist/index.js +19 -2
- 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/mcp/handler.js +2 -2
- package/apps/runtime/dist/mcp/seen.js +71 -51
- package/apps/runtime/dist/mcp/tools.js +5 -1
- package/apps/runtime/dist/models.js +18 -0
- package/apps/runtime/dist/prompts/mcp.md +1 -1
- package/apps/runtime/dist/prompts/tools.md +17 -1
- package/apps/runtime/dist/routes/api.js +2 -2
- package/apps/runtime/dist/routes/broker.js +59 -20
- package/apps/runtime/dist/routes/bundles.js +91 -0
- package/apps/runtime/dist/routes/connections.js +14 -3
- package/apps/runtime/dist/routes/live.js +4 -2
- package/apps/runtime/dist/routes/rules.js +53 -9
- package/apps/runtime/dist/routes/studio.js +36 -20
- package/apps/runtime/dist/services/api.js +4 -2
- package/apps/runtime/dist/services/cloud.js +2 -0
- package/apps/runtime/dist/services/draft-events.js +79 -6
- package/apps/runtime/dist/services/errors.js +1 -0
- package/apps/runtime/dist/services/files.js +2 -2
- package/apps/runtime/dist/services/knowledge.js +93 -27
- package/apps/runtime/dist/services/members-studio.js +22 -2
- package/apps/runtime/dist/services/skills.js +61 -5
- package/apps/runtime/dist/services/space-categories.js +83 -5
- package/apps/runtime/dist/services/space-index.js +173 -25
- package/apps/runtime/dist/services/space-origin.js +28 -11
- package/apps/runtime/dist/services/synced-spaces.js +5 -1
- package/apps/runtime/dist/services/wizards.js +48 -2
- package/apps/runtime/dist/services/work-ai.js +15 -5
- package/apps/runtime/dist/sharing/jobs.js +8 -2
- package/apps/runtime/dist/sync/hub.js +17 -3
- package/apps/runtime/dist/sync/records.js +6 -1
- package/apps/runtime/dist/texts.js +42 -5
- package/apps/runtime/dist/tools/connections.js +8 -0
- package/apps/runtime/dist/tools/connector.js +13 -1
- package/apps/runtime/dist/tools/index.js +21 -10
- package/apps/runtime/dist/tools/skills.js +11 -1
- package/apps/runtime/dist/tools/web-search.js +36 -0
- package/apps/web/dist/assets/{AboutDialog-BCcD8hKm.js → AboutDialog-mZCBU7KF.js} +1 -1
- package/apps/web/dist/assets/{BlockEditor-DgaUm2pR.js → BlockEditor-Dk0QwLSU.js} +1 -1
- package/apps/web/dist/assets/CapabilitiesPage-GkpgF-wZ.js +2 -0
- package/apps/web/dist/assets/{ChangelogDialog-B08elwyr.js → ChangelogDialog-BhyilZWA.js} +1 -1
- package/apps/web/dist/assets/{CodeEditor-BiyOk_SC.js → CodeEditor-DULYZbuE.js} +1 -1
- package/apps/web/dist/assets/EditorPage-D_mCdYdZ.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-DF_6TvZ0.js → FlowPreview-ykxZxWnk.js} +1 -1
- package/apps/web/dist/assets/{LangSwitch-Ba4OWLzJ.js → LangSwitch-iO54w9AP.js} +1 -1
- package/apps/web/dist/assets/{Marketplace-DqziLf66.js → Marketplace-C97fH7UC.js} +2 -2
- package/apps/web/dist/assets/{NewWizardChannels-CsIGp0nZ.js → NewWizardChannels-CsSV7sm2.js} +1 -1
- package/apps/web/dist/assets/{NewWizardPage-CzQAkKMy.js → NewWizardPage-Ebgw-Vf_.js} +1 -1
- package/apps/web/dist/assets/{OnboardingPage-B9gr2OX5.js → OnboardingPage-BZlloT7v.js} +1 -1
- package/apps/web/dist/assets/PreviewPane-CJuqY5uA.js +7 -0
- package/apps/web/dist/assets/ProtocolSection-CPaDKVpZ.js +1 -0
- package/apps/web/dist/assets/{PublicRunner-BVs8-Vu3.js → PublicRunner-4Lf4SnwB.js} +1 -1
- package/apps/web/dist/assets/{ReadOnly--4CIJCDD.js → ReadOnly-CXQ41R9Q.js} +1 -1
- package/apps/web/dist/assets/SettingsPage-C-8BgK9O.js +2 -0
- package/apps/web/dist/assets/{SetupPage-9Zk-KUGA.js → SetupPage-4_K083Wl.js} +1 -1
- package/apps/web/dist/assets/{SharePage-D_Ab0gjr.js → SharePage-CQjgMcDI.js} +1 -1
- package/apps/web/dist/assets/{SkillEditor-DvtameHg.js → SkillEditor-DT5hQwbg.js} +1 -1
- package/apps/web/dist/assets/{Skills-Cf1Kl4Fy.js → Skills-D7V_9_xo.js} +1 -1
- package/apps/web/dist/assets/SpacePage-D3rYS1bA.js +2 -0
- package/apps/web/dist/assets/{UpdateCheck-DweZBVP6.js → UpdateCheck-DVgAJmy8.js} +2 -2
- package/apps/web/dist/assets/changelog-DzQCUSsx.js +1 -0
- package/apps/web/dist/assets/{code-editor-B1RbFTAZ.js → code-editor-5T7ilBjd.js} +2 -2
- package/apps/web/dist/assets/{host-CKS5U5Vg.js → host-CCijW1hj.js} +6 -6
- package/apps/web/dist/assets/i18n-DCPDesT-.js +55 -0
- package/apps/web/dist/assets/{index-Ckcn5vy7.js → index-C_iWl8iS.js} +3 -3
- package/apps/web/dist/assets/index-zcPlodHx.css +1 -0
- package/apps/web/dist/assets/minus-BpYkOkYw.js +1 -0
- package/apps/web/dist/assets/{read-aloud-Bq88XUHC.js → read-aloud-Qb719not.js} +1 -1
- package/apps/web/dist/assets/{runners-DvYVe9Dt.js → runners-BeZ84YH_.js} +1 -1
- package/apps/web/dist/assets/{session-D2bjiA3P.js → session-CEJRkSP5.js} +1 -1
- package/apps/web/dist/assets/{theme-CNJIC2be.js → theme-y_owNosi.js} +1 -1
- package/apps/web/dist/assets/{ui-Mplfhz_Z.js → ui-BacfkQ7Q.js} +1 -1
- package/apps/web/dist/assets/{useMutation-8QRM-z4M.js → useMutation-CgzFr4qH.js} +1 -1
- package/apps/web/dist/assets/{viewers-CEocUV_2.js → viewers-CkyHAjnk.js} +2 -2
- package/apps/web/dist/index.html +12 -12
- package/apps/web/dist/mcp/flow.html +2 -2
- package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +7 -2
- package/node_modules/@engenty/wizards-shared/dist/bundles.d.ts +219 -0
- package/node_modules/@engenty/wizards-shared/dist/bundles.js +19 -0
- package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +117 -4
- package/node_modules/@engenty/wizards-shared/dist/connectors.js +14 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +12 -0
- package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +12 -0
- package/node_modules/@engenty/wizards-shared/dist/run.d.ts +11 -0
- package/node_modules/@engenty/wizards-shared/dist/run.js +22 -0
- package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +84 -1
- package/node_modules/@engenty/wizards-shared/dist/rwx.js +69 -1
- package/node_modules/@engenty/wizards-shared/dist/skills.d.ts +20 -3
- package/node_modules/@engenty/wizards-shared/dist/skills.js +5 -2
- 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-DuJJV1ll.js +0 -2
- package/apps/web/dist/assets/EditorPage-EMFTHEYD.js +0 -3
- package/apps/web/dist/assets/PreviewPane-DSOV3o-1.js +0 -7
- package/apps/web/dist/assets/SettingsPage-z2u8lSe2.js +0 -2
- package/apps/web/dist/assets/SpacePage-CyqVzX9b.js +0 -2
- package/apps/web/dist/assets/changelog-C85v9OWg.js +0 -1
- package/apps/web/dist/assets/connection-sign-in-CaVvh5GQ.js +0 -1
- package/apps/web/dist/assets/i18n-Bymk5Ev-.js +0 -55
- package/apps/web/dist/assets/index-w-3J3ZJ9.css +0 -1
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { featureOn } from "@engenty/wizards-shared/features";
|
|
3
|
+
import { SKILL_LIMITS } from "@engenty/wizards-shared/skills";
|
|
4
|
+
import { and, asc, eq, inArray, or } from "drizzle-orm";
|
|
5
|
+
import { nanoid } from "nanoid";
|
|
6
|
+
import { freeNames, importConnector, namesFromHost, previewSource, } from "../connectors/external.js";
|
|
7
|
+
import { db, inTransaction, schema } from "../db/client.js";
|
|
8
|
+
import { currentFeatures } from "../features.js";
|
|
9
|
+
import { errorText, noteOf } from "../model-errors.js";
|
|
10
|
+
import { requireAdmin, requireWritableSpace } from "../services/access.js";
|
|
11
|
+
import { notFound, ServiceError } from "../services/errors.js";
|
|
12
|
+
import { checkAlways, checkContent, contentHash, insertSkill, keepBlobs, skillsOn, } from "../services/skills.js";
|
|
13
|
+
import { ownedSpace } from "../services/spaces.js";
|
|
14
|
+
import { currentTenant } from "../tenants/tenant.js";
|
|
15
|
+
import { fetchRepoZip, parseRepoAddress } from "./fetch.js";
|
|
16
|
+
import { Archive, MAX_ARCHIVE_MB, readPlugin, } from "./read.js";
|
|
17
|
+
const refuse = (server, params) => new ServiceError("invalid", { server, params });
|
|
18
|
+
/** Part of Fähigkeiten, and an admin's: the space, where the asker may change it. */
|
|
19
|
+
async function adminSpace(userId, spaceId, change = true) {
|
|
20
|
+
if (!featureOn(await currentFeatures(), "capabilities")) {
|
|
21
|
+
throw notFound();
|
|
22
|
+
}
|
|
23
|
+
const space = await ownedSpace(userId, spaceId);
|
|
24
|
+
if (change) {
|
|
25
|
+
await requireWritableSpace(space.id);
|
|
26
|
+
}
|
|
27
|
+
await requireAdmin();
|
|
28
|
+
return space;
|
|
29
|
+
}
|
|
30
|
+
async function bundleRow(id) {
|
|
31
|
+
const row = await db.query.spaceBundle.findFirst({
|
|
32
|
+
where: and(eq(schema.spaceBundle.id, id), eq(schema.spaceBundle.tenantId, currentTenant())),
|
|
33
|
+
});
|
|
34
|
+
if (!row) {
|
|
35
|
+
throw notFound();
|
|
36
|
+
}
|
|
37
|
+
return row;
|
|
38
|
+
}
|
|
39
|
+
// --- reading ----------------------------------------------------------------------------------
|
|
40
|
+
async function readSource(source, lang) {
|
|
41
|
+
const say = (err) => errorText(err, lang);
|
|
42
|
+
if ("file" in source) {
|
|
43
|
+
const { name, data } = source.file;
|
|
44
|
+
if (!/\.(zip|plugin)$/i.test(name)) {
|
|
45
|
+
throw refuse("bundleUploadKind");
|
|
46
|
+
}
|
|
47
|
+
if (data.length > MAX_ARCHIVE_MB * 1_000_000) {
|
|
48
|
+
throw refuse("bundleTooLarge", { mb: MAX_ARCHIVE_MB });
|
|
49
|
+
}
|
|
50
|
+
const read = readPlugin(new Archive(data), {
|
|
51
|
+
path: source.path,
|
|
52
|
+
fallbackName: name.replace(/\.(zip|plugin)$/i, ""),
|
|
53
|
+
say,
|
|
54
|
+
});
|
|
55
|
+
const path = "plugin" in read ? read.plugin.root.replace(/\/$/, "") : (source.path ?? "");
|
|
56
|
+
const origin = { kind: "upload", file: name.slice(0, 200), path };
|
|
57
|
+
return { origin, read };
|
|
58
|
+
}
|
|
59
|
+
const address = parseRepoAddress(source.address);
|
|
60
|
+
if (!address) {
|
|
61
|
+
throw refuse("bundleNotARepository");
|
|
62
|
+
}
|
|
63
|
+
const read = readPlugin(new Archive(await fetchRepoZip(address)), {
|
|
64
|
+
path: source.path ?? address.path,
|
|
65
|
+
fallbackName: address.repo.split("/")[1] ?? address.repo,
|
|
66
|
+
say,
|
|
67
|
+
});
|
|
68
|
+
const path = "plugin" in read ? read.plugin.root.replace(/\/$/, "") : address.path;
|
|
69
|
+
const origin = { kind: "github", repo: address.repo, ref: address.ref, path };
|
|
70
|
+
return { origin, read };
|
|
71
|
+
}
|
|
72
|
+
/** Of what a space would take: the servers as the plugin names them and each skill's content. */
|
|
73
|
+
function hashOf(plugin) {
|
|
74
|
+
return createHash("sha256")
|
|
75
|
+
.update(JSON.stringify([
|
|
76
|
+
plugin.format,
|
|
77
|
+
plugin.name,
|
|
78
|
+
plugin.version,
|
|
79
|
+
plugin.servers
|
|
80
|
+
.map((s) => [s.key, s.url, s.transport, s.headers, s.keyHeader])
|
|
81
|
+
.sort((a, b) => String(a[0]).localeCompare(String(b[0]))),
|
|
82
|
+
plugin.skills
|
|
83
|
+
.map((s) => [s.path, contentHash(s.intake.content)])
|
|
84
|
+
.sort((a, b) => a[0].localeCompare(b[0])),
|
|
85
|
+
]))
|
|
86
|
+
.digest("hex");
|
|
87
|
+
}
|
|
88
|
+
/** The space's record of a plugin from this origin: the same repository and folder, or the same file's plugin. */
|
|
89
|
+
async function sameOrigin(spaceId, origin, name) {
|
|
90
|
+
const rows = await db.query.spaceBundle.findMany({
|
|
91
|
+
where: eq(schema.spaceBundle.spaceId, spaceId),
|
|
92
|
+
});
|
|
93
|
+
return (rows.find((row) => origin.kind === "github"
|
|
94
|
+
? row.origin.kind === "github" &&
|
|
95
|
+
row.origin.repo.toLowerCase() === origin.repo.toLowerCase() &&
|
|
96
|
+
row.origin.path === origin.path
|
|
97
|
+
: row.origin.kind === "upload" && row.name === name) ?? null);
|
|
98
|
+
}
|
|
99
|
+
const fixedHeaders = (server) => server.headers.map((h) => ({ name: h.name, value: h.value, description: null }));
|
|
100
|
+
/** `slack` → `Slack`: a connector's name where the admin gives none. */
|
|
101
|
+
const nameOf = (key) => `${key.slice(0, 1).toUpperCase()}${key.slice(1)}`.slice(0, 120);
|
|
102
|
+
const skillPreview = (skill) => {
|
|
103
|
+
const { content, skipped } = skill.intake;
|
|
104
|
+
return {
|
|
105
|
+
path: skill.path,
|
|
106
|
+
name: content.name,
|
|
107
|
+
description: content.description,
|
|
108
|
+
chars: content.body.length,
|
|
109
|
+
files: content.files.length + content.blobFiles.length,
|
|
110
|
+
scripts: [...content.files, ...content.blobFiles].some((f) => /\.(py|sh|bash|zsh|js|mjs|cjs|ts|rb|php|go|rs)$/i.test(f.path)),
|
|
111
|
+
skipped: skipped.length,
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
/** What is kept of a list of things left out: never the words of a refusal. */
|
|
115
|
+
const kept = (leftOut) => leftOut.map(({ detail: _detail, ...rest }) => rest);
|
|
116
|
+
/** Skills are taken where the space has them: with their switch off they are named as left out. */
|
|
117
|
+
async function withSkills(plugin) {
|
|
118
|
+
if (await skillsOn()) {
|
|
119
|
+
return plugin;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
...plugin,
|
|
123
|
+
skills: [],
|
|
124
|
+
leftOut: [
|
|
125
|
+
...plugin.leftOut,
|
|
126
|
+
...plugin.skills.map((s) => ({ kind: "skill", name: s.path })),
|
|
127
|
+
],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Reads a plugin before anything is taken: its name and version, each server over http as „Per
|
|
132
|
+
* Adresse“ reads an address (its sign-in, its actions with what each was read as), its skills,
|
|
133
|
+
* and what is left out. A server that cannot be read is left out with the reason in words: a
|
|
134
|
+
* private address on a managed runtime, a server that does not answer. A repository of several
|
|
135
|
+
* plugins answers with the list to choose from.
|
|
136
|
+
*/
|
|
137
|
+
export async function previewBundle(userId, spaceId, source, lang, choices = {}) {
|
|
138
|
+
const space = await adminSpace(userId, spaceId);
|
|
139
|
+
const { origin, read } = await readSource(source, lang);
|
|
140
|
+
if ("choices" in read) {
|
|
141
|
+
return { preview: null, plugins: read.choices };
|
|
142
|
+
}
|
|
143
|
+
const plugin = await withSkills(read.plugin);
|
|
144
|
+
const found = await Promise.all(plugin.servers.map(async (server) => {
|
|
145
|
+
try {
|
|
146
|
+
const preview = await previewSource(space.id, {
|
|
147
|
+
sourceKind: "mcp",
|
|
148
|
+
sourceUrl: server.url,
|
|
149
|
+
signIn: choices[server.key]?.signIn,
|
|
150
|
+
headers: fixedHeaders(server),
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
key: server.key,
|
|
154
|
+
url: server.url,
|
|
155
|
+
transport: server.transport,
|
|
156
|
+
headers: server.headers.map((h) => h.name),
|
|
157
|
+
keyHeader: server.keyHeader,
|
|
158
|
+
source: preview,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
return {
|
|
163
|
+
kind: "server",
|
|
164
|
+
name: server.key,
|
|
165
|
+
reason: "refused",
|
|
166
|
+
detail: errorText(err, lang).slice(0, 400),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
}));
|
|
170
|
+
const have = await db.$count(schema.spaceSkill, eq(schema.spaceSkill.spaceId, space.id));
|
|
171
|
+
return {
|
|
172
|
+
plugins: [],
|
|
173
|
+
preview: {
|
|
174
|
+
name: plugin.name,
|
|
175
|
+
version: plugin.version,
|
|
176
|
+
description: plugin.description,
|
|
177
|
+
format: plugin.format,
|
|
178
|
+
origin,
|
|
179
|
+
hash: hashOf(plugin),
|
|
180
|
+
servers: found.filter((s) => "source" in s),
|
|
181
|
+
skills: plugin.skills.map(skillPreview),
|
|
182
|
+
leftOut: [...found.filter((s) => "kind" in s), ...plugin.leftOut],
|
|
183
|
+
skillsFree: Math.max(0, SKILL_LIMITS.perSpace - have),
|
|
184
|
+
added: (await sameOrigin(space.id, origin, plugin.name))?.id ?? null,
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
// --- views ------------------------------------------------------------------------------------
|
|
189
|
+
async function viewOf(row) {
|
|
190
|
+
const [connectors, connections, skills] = await Promise.all([
|
|
191
|
+
db.query.spaceConnector.findMany({
|
|
192
|
+
where: and(eq(schema.spaceConnector.spaceId, row.spaceId), eq(schema.spaceConnector.bundleId, row.id)),
|
|
193
|
+
orderBy: [asc(schema.spaceConnector.createdAt)],
|
|
194
|
+
}),
|
|
195
|
+
db.query.spaceConnection.findMany({
|
|
196
|
+
where: and(eq(schema.spaceConnection.spaceId, row.spaceId), eq(schema.spaceConnection.bundleId, row.id)),
|
|
197
|
+
orderBy: [asc(schema.spaceConnection.setUpAt)],
|
|
198
|
+
}),
|
|
199
|
+
db.query.spaceSkill.findMany({
|
|
200
|
+
where: and(eq(schema.spaceSkill.spaceId, row.spaceId), eq(schema.spaceSkill.bundleId, row.id)),
|
|
201
|
+
orderBy: [asc(schema.spaceSkill.name)],
|
|
202
|
+
}),
|
|
203
|
+
]);
|
|
204
|
+
return {
|
|
205
|
+
id: row.id,
|
|
206
|
+
spaceId: row.spaceId,
|
|
207
|
+
name: row.name,
|
|
208
|
+
version: row.version,
|
|
209
|
+
description: row.description,
|
|
210
|
+
format: row.format,
|
|
211
|
+
origin: row.origin,
|
|
212
|
+
hash: row.hash,
|
|
213
|
+
leftOut: row.leftOut,
|
|
214
|
+
connectors: connectors
|
|
215
|
+
.map((c) => ({ id: c.id, name: c.record.name }))
|
|
216
|
+
.sort((a, b) => a.name.localeCompare(b.name)),
|
|
217
|
+
connections: connections.map((c) => ({ id: c.id, name: c.name, connectorId: c.connectorId })),
|
|
218
|
+
skills: skills.map((k) => ({
|
|
219
|
+
id: k.id,
|
|
220
|
+
name: k.name,
|
|
221
|
+
description: k.description,
|
|
222
|
+
mode: k.mode,
|
|
223
|
+
})),
|
|
224
|
+
addedAt: row.createdAt.toISOString(),
|
|
225
|
+
updatedAt: row.updatedAt.toISOString(),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/** What came together in a space, each with its connectors, connections and skills. */
|
|
229
|
+
export async function listBundles(userId, spaceId) {
|
|
230
|
+
const space = await adminSpace(userId, spaceId, false);
|
|
231
|
+
const rows = await db.query.spaceBundle.findMany({
|
|
232
|
+
where: eq(schema.spaceBundle.spaceId, space.id),
|
|
233
|
+
orderBy: [asc(schema.spaceBundle.createdAt)],
|
|
234
|
+
});
|
|
235
|
+
return Promise.all(rows.map(viewOf));
|
|
236
|
+
}
|
|
237
|
+
// --- taking -----------------------------------------------------------------------------------
|
|
238
|
+
/**
|
|
239
|
+
* One server of a plugin as a connector of the space. Its sign-in is the admin's choice, else
|
|
240
|
+
* what the server describes itself; where it asks for one and describes none, the header its
|
|
241
|
+
* plugin names for a key.
|
|
242
|
+
*/
|
|
243
|
+
async function importServer(userId, spaceId, bundleId, server, choice) {
|
|
244
|
+
const suggested = await freeNames(spaceId, namesFromHost(new URL(server.url).host));
|
|
245
|
+
const request = {
|
|
246
|
+
sourceKind: "mcp",
|
|
247
|
+
sourceUrl: server.url,
|
|
248
|
+
id: choice?.id ?? suggested.id,
|
|
249
|
+
toolPrefix: choice?.toolPrefix ?? suggested.toolPrefix,
|
|
250
|
+
name: choice?.name ?? nameOf(server.key),
|
|
251
|
+
...(choice?.signIn ? { signIn: choice.signIn } : {}),
|
|
252
|
+
...(choice?.actionKinds ? { actionKinds: choice.actionKinds } : {}),
|
|
253
|
+
...(choice?.oauthClientId ? { oauthClientId: choice.oauthClientId } : {}),
|
|
254
|
+
...(choice?.oauthClientSecret ? { oauthClientSecret: choice.oauthClientSecret } : {}),
|
|
255
|
+
};
|
|
256
|
+
const from = { bundleId, headers: fixedHeaders(server) };
|
|
257
|
+
try {
|
|
258
|
+
return await importConnector(userId, spaceId, request, from);
|
|
259
|
+
}
|
|
260
|
+
catch (err) {
|
|
261
|
+
if (!choice?.signIn && server.keyHeader && noteOf(err)?.code === "connectorSignInUnknown") {
|
|
262
|
+
const signIn = { kind: "header", ...server.keyHeader };
|
|
263
|
+
return importConnector(userId, spaceId, { ...request, signIn }, from);
|
|
264
|
+
}
|
|
265
|
+
throw err;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async function addSkill(spaceId, bundleId, skill) {
|
|
269
|
+
await keepBlobs(skill.intake.blobs);
|
|
270
|
+
return insertSkill(spaceId, skill.intake.content, {
|
|
271
|
+
source: "plugin",
|
|
272
|
+
origin: skill.path,
|
|
273
|
+
originHash: contentHash(skill.intake.content),
|
|
274
|
+
rename: true,
|
|
275
|
+
bundleId,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/** Takes back what an import made so far: nothing of a plugin stays where a part of it failed. */
|
|
279
|
+
async function dropBundle(spaceId, id) {
|
|
280
|
+
await inTransaction(async () => {
|
|
281
|
+
const connectors = await db
|
|
282
|
+
.select({ id: schema.spaceConnector.id })
|
|
283
|
+
.from(schema.spaceConnector)
|
|
284
|
+
.where(and(eq(schema.spaceConnector.spaceId, spaceId), eq(schema.spaceConnector.bundleId, id)));
|
|
285
|
+
const ids = connectors.map((c) => c.id);
|
|
286
|
+
await db
|
|
287
|
+
.delete(schema.spaceConnection)
|
|
288
|
+
.where(and(eq(schema.spaceConnection.spaceId, spaceId), ids.length
|
|
289
|
+
? or(eq(schema.spaceConnection.bundleId, id), inArray(schema.spaceConnection.connectorId, ids))
|
|
290
|
+
: eq(schema.spaceConnection.bundleId, id)));
|
|
291
|
+
await db
|
|
292
|
+
.delete(schema.spaceConnector)
|
|
293
|
+
.where(and(eq(schema.spaceConnector.spaceId, spaceId), eq(schema.spaceConnector.bundleId, id)));
|
|
294
|
+
await db
|
|
295
|
+
.delete(schema.spaceSkill)
|
|
296
|
+
.where(and(eq(schema.spaceSkill.spaceId, spaceId), eq(schema.spaceSkill.bundleId, id)));
|
|
297
|
+
await db.delete(schema.spaceBundle).where(eq(schema.spaceBundle.id, id));
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Takes a plugin into the space as it was shown: its servers over http as connectors, its skills
|
|
302
|
+
* as skills, and the record of what came together. A plugin with skills and no server over http
|
|
303
|
+
* brings its skills alone, one with a server and no skills its connector alone; one with neither
|
|
304
|
+
* is refused. A server that fails stops the import and nothing stays: the admin leaves it out
|
|
305
|
+
* (`take: false`) and tries again.
|
|
306
|
+
*/
|
|
307
|
+
export async function importBundle(userId, spaceId, source, input, lang) {
|
|
308
|
+
const space = await adminSpace(userId, spaceId);
|
|
309
|
+
const { origin, read } = await readSource(source, lang);
|
|
310
|
+
if ("choices" in read) {
|
|
311
|
+
throw refuse("bundleChoose");
|
|
312
|
+
}
|
|
313
|
+
const plugin = await withSkills(read.plugin);
|
|
314
|
+
const hash = hashOf(plugin);
|
|
315
|
+
// Taken only as it was shown: a change since the person looked is shown first.
|
|
316
|
+
if (input.hash && input.hash !== hash) {
|
|
317
|
+
throw new ServiceError("revision_conflict", { server: "bundleChanged" });
|
|
318
|
+
}
|
|
319
|
+
if (await sameOrigin(space.id, origin, plugin.name)) {
|
|
320
|
+
throw refuse("bundleAlreadyThere", { name: plugin.name });
|
|
321
|
+
}
|
|
322
|
+
const unknown = Object.keys(input.servers ?? {}).find((key) => !plugin.servers.some((s) => s.key === key));
|
|
323
|
+
if (unknown !== undefined) {
|
|
324
|
+
throw refuse("bundleServerUnknown", { key: unknown });
|
|
325
|
+
}
|
|
326
|
+
const servers = plugin.servers.filter((s) => input.servers?.[s.key]?.take !== false);
|
|
327
|
+
const skills = input.skills
|
|
328
|
+
? plugin.skills.filter((s) => input.skills?.includes(s.path))
|
|
329
|
+
: plugin.skills;
|
|
330
|
+
if (!(servers.length || skills.length)) {
|
|
331
|
+
throw refuse("bundleNothingToTake");
|
|
332
|
+
}
|
|
333
|
+
const have = await db.$count(schema.spaceSkill, eq(schema.spaceSkill.spaceId, space.id));
|
|
334
|
+
if (have + skills.length > SKILL_LIMITS.perSpace) {
|
|
335
|
+
throw refuse("skillsTooMany");
|
|
336
|
+
}
|
|
337
|
+
const id = nanoid(14);
|
|
338
|
+
const taken = new Set(servers.map((s) => s.key));
|
|
339
|
+
await db.insert(schema.spaceBundle).values({
|
|
340
|
+
id,
|
|
341
|
+
tenantId: currentTenant(),
|
|
342
|
+
spaceId: space.id,
|
|
343
|
+
name: plugin.name,
|
|
344
|
+
version: plugin.version,
|
|
345
|
+
description: plugin.description,
|
|
346
|
+
format: plugin.format,
|
|
347
|
+
origin,
|
|
348
|
+
hash,
|
|
349
|
+
// A server the admin left out is one the space did not take.
|
|
350
|
+
leftOut: kept([
|
|
351
|
+
...plugin.servers
|
|
352
|
+
.filter((s) => !taken.has(s.key))
|
|
353
|
+
.map((s) => ({ kind: "server", name: s.key })),
|
|
354
|
+
...plugin.leftOut,
|
|
355
|
+
]),
|
|
356
|
+
addedBy: userId,
|
|
357
|
+
});
|
|
358
|
+
const connectors = [];
|
|
359
|
+
const warnings = [];
|
|
360
|
+
try {
|
|
361
|
+
for (const server of servers) {
|
|
362
|
+
try {
|
|
363
|
+
const made = await importServer(userId, space.id, id, server, input.servers?.[server.key]);
|
|
364
|
+
connectors.push(made.connector);
|
|
365
|
+
warnings.push(...made.warnings.map((w) => `${server.key}: ${w}`));
|
|
366
|
+
}
|
|
367
|
+
catch (err) {
|
|
368
|
+
throw new ServiceError("invalid", {
|
|
369
|
+
server: "bundleServerFailed",
|
|
370
|
+
params: { key: server.key, detail: errorText(err, lang).slice(0, 400) },
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
for (const skill of skills) {
|
|
375
|
+
await addSkill(space.id, id, skill);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
catch (err) {
|
|
379
|
+
await dropBundle(space.id, id);
|
|
380
|
+
throw err;
|
|
381
|
+
}
|
|
382
|
+
return { bundle: await viewOf(await bundleRow(id)), connectors, warnings };
|
|
383
|
+
}
|
|
384
|
+
// --- updating ---------------------------------------------------------------------------------
|
|
385
|
+
/** The plugin at a bundle's origin now, read the way it was first. */
|
|
386
|
+
async function readAgain(row, file, lang) {
|
|
387
|
+
const { origin } = row;
|
|
388
|
+
let source;
|
|
389
|
+
if (file) {
|
|
390
|
+
source = { file, path: origin.path };
|
|
391
|
+
}
|
|
392
|
+
else if (origin.kind === "github") {
|
|
393
|
+
const tree = origin.ref ? `/tree/${encodeURIComponent(origin.ref)}` : "";
|
|
394
|
+
source = { address: `https://github.com/${origin.repo}${tree}`, path: origin.path };
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
throw refuse("bundleUploadAgain");
|
|
398
|
+
}
|
|
399
|
+
const { read } = await readSource(source, lang);
|
|
400
|
+
if ("choices" in read) {
|
|
401
|
+
throw refuse("bundleNoPlugin");
|
|
402
|
+
}
|
|
403
|
+
return withSkills(read.plugin);
|
|
404
|
+
}
|
|
405
|
+
/** What the origin has now, set against what the space took. */
|
|
406
|
+
async function changesOf(row, plugin) {
|
|
407
|
+
const [skills, connectors] = await Promise.all([
|
|
408
|
+
db.query.spaceSkill.findMany({
|
|
409
|
+
where: and(eq(schema.spaceSkill.spaceId, row.spaceId), eq(schema.spaceSkill.bundleId, row.id)),
|
|
410
|
+
}),
|
|
411
|
+
db.query.spaceConnector.findMany({
|
|
412
|
+
where: and(eq(schema.spaceConnector.spaceId, row.spaceId), eq(schema.spaceConnector.bundleId, row.id)),
|
|
413
|
+
}),
|
|
414
|
+
]);
|
|
415
|
+
const byPath = new Map(skills.map((k) => [k.origin ?? "", k]));
|
|
416
|
+
const paths = new Set(plugin.skills.map((s) => s.path));
|
|
417
|
+
const urls = new Set(plugin.servers.map((s) => s.url));
|
|
418
|
+
const have = new Set(connectors.map((c) => c.record.source_url));
|
|
419
|
+
// A server the admin left out when the plugin was taken stays out; the record names it.
|
|
420
|
+
const leftOutBefore = new Set(row.leftOut.filter((l) => l.kind === "server" && !l.reason).map((l) => l.name));
|
|
421
|
+
return {
|
|
422
|
+
added: plugin.skills.filter((s) => !byPath.has(s.path)),
|
|
423
|
+
changed: plugin.skills.flatMap((s) => {
|
|
424
|
+
const skill = byPath.get(s.path);
|
|
425
|
+
return skill && skill.originHash !== contentHash(s.intake.content)
|
|
426
|
+
? [{ skill, next: s }]
|
|
427
|
+
: [];
|
|
428
|
+
}),
|
|
429
|
+
removed: skills.filter((k) => !paths.has(k.origin ?? "")),
|
|
430
|
+
newServers: plugin.servers.filter((s) => !(have.has(s.url) || leftOutBefore.has(s.key))),
|
|
431
|
+
goneConnectors: connectors.filter((c) => !urls.has(c.record.source_url)),
|
|
432
|
+
leftOutBefore,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Whether the origin changed since the plugin was taken, and what: skills new, changed or gone,
|
|
437
|
+
* servers new or no longer named. A plugin from a file has no origin to ask (`upload`).
|
|
438
|
+
*/
|
|
439
|
+
export async function bundleUpdate(userId, id, lang) {
|
|
440
|
+
const row = await bundleRow(id);
|
|
441
|
+
await adminSpace(userId, row.spaceId, false);
|
|
442
|
+
if (row.origin.kind === "upload") {
|
|
443
|
+
return { update: null, upload: true };
|
|
444
|
+
}
|
|
445
|
+
const plugin = await readAgain(row, null, lang);
|
|
446
|
+
const hash = hashOf(plugin);
|
|
447
|
+
if (hash === row.hash) {
|
|
448
|
+
return { update: null, upload: false };
|
|
449
|
+
}
|
|
450
|
+
return { update: await updateOf(row, plugin, hash), upload: false };
|
|
451
|
+
}
|
|
452
|
+
async function updateOf(row, plugin, hash) {
|
|
453
|
+
const changes = await changesOf(row, plugin);
|
|
454
|
+
return {
|
|
455
|
+
version: plugin.version,
|
|
456
|
+
hash,
|
|
457
|
+
skills: {
|
|
458
|
+
added: changes.added.map((s) => s.intake.content.name),
|
|
459
|
+
changed: changes.changed.map((c) => c.skill.name),
|
|
460
|
+
removed: changes.removed.map((k) => k.name),
|
|
461
|
+
},
|
|
462
|
+
servers: {
|
|
463
|
+
added: changes.newServers.map((s) => ({ key: s.key, url: s.url })),
|
|
464
|
+
removed: changes.goneConnectors.map((c) => ({ connectorId: c.id, name: c.record.name })),
|
|
465
|
+
},
|
|
466
|
+
leftOut: kept(plugin.leftOut),
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Takes the origin's state of now. A skill that changed is replaced and keeps its name, when it
|
|
471
|
+
* counts and whether its scripts run; a new one comes on demand; one the origin dropped goes. A
|
|
472
|
+
* connector, its connections and their sign-ins stay as they are. A new server becomes a new
|
|
473
|
+
* connector, „nicht geprüft“ like the first; one that cannot be imported as it stands is named
|
|
474
|
+
* and the rest is taken. A connector whose server the origin no longer names leaves the bundle
|
|
475
|
+
* and stays a connector of its own: nothing a wizard uses goes away, and no sign-in is sent to
|
|
476
|
+
* an address that changed. A plugin from a file is updated with the new file.
|
|
477
|
+
*/
|
|
478
|
+
export async function applyBundleUpdate(userId, id, input, lang) {
|
|
479
|
+
const row = await bundleRow(id);
|
|
480
|
+
const space = await adminSpace(userId, row.spaceId);
|
|
481
|
+
const plugin = await readAgain(row, input.file ?? null, lang);
|
|
482
|
+
const hash = hashOf(plugin);
|
|
483
|
+
// Taken only as it was shown: a change since the person looked is shown first.
|
|
484
|
+
if (input.hash && input.hash !== hash) {
|
|
485
|
+
throw new ServiceError("revision_conflict", { server: "bundleChanged" });
|
|
486
|
+
}
|
|
487
|
+
const changes = await changesOf(row, plugin);
|
|
488
|
+
const others = await db.$count(schema.spaceSkill, eq(schema.spaceSkill.spaceId, space.id));
|
|
489
|
+
if (others - changes.removed.length + changes.added.length > SKILL_LIMITS.perSpace) {
|
|
490
|
+
throw refuse("skillsTooMany");
|
|
491
|
+
}
|
|
492
|
+
// What the space would refuse is said before anything is written.
|
|
493
|
+
const replaced = [];
|
|
494
|
+
for (const { skill, next } of changes.changed) {
|
|
495
|
+
const content = checkContent({ ...next.intake.content, name: skill.name });
|
|
496
|
+
await checkAlways(skill.spaceId, skill.id, skill.mode, content.body);
|
|
497
|
+
replaced.push({ skill, next, content });
|
|
498
|
+
}
|
|
499
|
+
for (const { skill, next, content } of replaced) {
|
|
500
|
+
await keepBlobs(next.intake.blobs);
|
|
501
|
+
await db
|
|
502
|
+
.update(schema.spaceSkill)
|
|
503
|
+
.set({
|
|
504
|
+
description: content.description,
|
|
505
|
+
body: content.body,
|
|
506
|
+
meta: content.meta,
|
|
507
|
+
files: content.files,
|
|
508
|
+
blobFiles: content.blobFiles,
|
|
509
|
+
originHash: contentHash(next.intake.content),
|
|
510
|
+
updatedAt: new Date(),
|
|
511
|
+
})
|
|
512
|
+
.where(eq(schema.spaceSkill.id, skill.id));
|
|
513
|
+
}
|
|
514
|
+
if (changes.removed.length) {
|
|
515
|
+
await db.delete(schema.spaceSkill).where(inArray(schema.spaceSkill.id, changes.removed.map((k) => k.id)));
|
|
516
|
+
}
|
|
517
|
+
for (const skill of changes.added) {
|
|
518
|
+
await addSkill(space.id, row.id, skill);
|
|
519
|
+
}
|
|
520
|
+
for (const connector of changes.goneConnectors) {
|
|
521
|
+
await db
|
|
522
|
+
.update(schema.spaceConnector)
|
|
523
|
+
.set({ bundleId: null })
|
|
524
|
+
.where(and(eq(schema.spaceConnector.spaceId, space.id), eq(schema.spaceConnector.id, connector.id)));
|
|
525
|
+
await db
|
|
526
|
+
.update(schema.spaceConnection)
|
|
527
|
+
.set({ bundleId: null })
|
|
528
|
+
.where(and(eq(schema.spaceConnection.spaceId, space.id), eq(schema.spaceConnection.connectorId, connector.id)));
|
|
529
|
+
}
|
|
530
|
+
const notTaken = [];
|
|
531
|
+
for (const server of changes.newServers) {
|
|
532
|
+
try {
|
|
533
|
+
await importServer(userId, space.id, row.id, server, undefined);
|
|
534
|
+
}
|
|
535
|
+
catch (err) {
|
|
536
|
+
notTaken.push({ key: server.key, detail: errorText(err, lang).slice(0, 400) });
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
const out = new Set([...changes.leftOutBefore, ...notTaken.map((n) => n.key)]);
|
|
540
|
+
await db
|
|
541
|
+
.update(schema.spaceBundle)
|
|
542
|
+
.set({
|
|
543
|
+
name: plugin.name,
|
|
544
|
+
version: plugin.version,
|
|
545
|
+
description: plugin.description,
|
|
546
|
+
format: plugin.format,
|
|
547
|
+
hash,
|
|
548
|
+
leftOut: kept([
|
|
549
|
+
...plugin.servers
|
|
550
|
+
.filter((s) => out.has(s.key))
|
|
551
|
+
.map((s) => ({ kind: "server", name: s.key })),
|
|
552
|
+
...plugin.leftOut,
|
|
553
|
+
]),
|
|
554
|
+
...(input.file && row.origin.kind === "upload"
|
|
555
|
+
? { origin: { ...row.origin, file: input.file.name.slice(0, 200) } }
|
|
556
|
+
: {}),
|
|
557
|
+
updatedAt: new Date(),
|
|
558
|
+
})
|
|
559
|
+
.where(eq(schema.spaceBundle.id, row.id));
|
|
560
|
+
return { bundle: await viewOf(await bundleRow(row.id)), notTaken };
|
|
561
|
+
}
|
|
562
|
+
// --- removing ---------------------------------------------------------------------------------
|
|
563
|
+
/** The names by which a definition's connections reach the space's connections `ids`. */
|
|
564
|
+
function usedIn(definition, ids) {
|
|
565
|
+
return (definition.connections ?? []).flatMap((c) => {
|
|
566
|
+
const name = c.spaceConnection ? ids.get(c.spaceConnection) : undefined;
|
|
567
|
+
return name === undefined ? [] : [name];
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* What removing takes with it, asked before it is removed: the connectors, their connections,
|
|
572
|
+
* the skills, and the wizards whose draft or published version uses one of the connections.
|
|
573
|
+
*/
|
|
574
|
+
export async function bundleRemoval(userId, id) {
|
|
575
|
+
const row = await bundleRow(id);
|
|
576
|
+
await adminSpace(userId, row.spaceId, false);
|
|
577
|
+
const view = await viewOf(row);
|
|
578
|
+
const connectorIds = view.connectors.map((c) => c.id);
|
|
579
|
+
const connections = connectorIds.length
|
|
580
|
+
? await db.query.spaceConnection.findMany({
|
|
581
|
+
where: and(eq(schema.spaceConnection.spaceId, row.spaceId), or(eq(schema.spaceConnection.bundleId, row.id), inArray(schema.spaceConnection.connectorId, connectorIds))),
|
|
582
|
+
orderBy: [asc(schema.spaceConnection.setUpAt)],
|
|
583
|
+
})
|
|
584
|
+
: [];
|
|
585
|
+
const names = new Map(connections.map((c) => [c.id, c.name]));
|
|
586
|
+
const wizards = [];
|
|
587
|
+
if (names.size) {
|
|
588
|
+
const rows = await db.query.wizard.findMany({
|
|
589
|
+
where: eq(schema.wizard.spaceId, row.spaceId),
|
|
590
|
+
columns: { id: true, title: true, draft: true, publishedVersion: true },
|
|
591
|
+
orderBy: [asc(schema.wizard.title)],
|
|
592
|
+
});
|
|
593
|
+
for (const wizard of rows) {
|
|
594
|
+
const published = wizard.publishedVersion === null
|
|
595
|
+
? null
|
|
596
|
+
: await db.query.wizardVersion.findFirst({
|
|
597
|
+
where: and(eq(schema.wizardVersion.wizardId, wizard.id), eq(schema.wizardVersion.version, wizard.publishedVersion)),
|
|
598
|
+
columns: { definition: true },
|
|
599
|
+
});
|
|
600
|
+
const used = new Set([
|
|
601
|
+
...usedIn(wizard.draft, names),
|
|
602
|
+
...(published ? usedIn(published.definition, names) : []),
|
|
603
|
+
]);
|
|
604
|
+
if (used.size) {
|
|
605
|
+
wizards.push({ id: wizard.id, title: wizard.title, connections: [...used].sort() });
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return {
|
|
610
|
+
connectors: view.connectors,
|
|
611
|
+
connections: connections.map((c) => ({ id: c.id, name: c.name })),
|
|
612
|
+
skills: view.skills.map((k) => ({ id: k.id, name: k.name })),
|
|
613
|
+
wizards,
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
/** Removes what came together, all at once: the connectors, their connections and the skills. */
|
|
617
|
+
export async function removeBundle(userId, id) {
|
|
618
|
+
const row = await bundleRow(id);
|
|
619
|
+
await adminSpace(userId, row.spaceId);
|
|
620
|
+
await dropBundle(row.spaceId, row.id);
|
|
621
|
+
}
|