wizards 0.2.46 → 0.2.48
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/{project-assistant.js → studio-assistant.js} +199 -69
- package/apps/runtime/dist/agents/website.js +25 -2
- package/apps/runtime/dist/app.js +5 -1
- package/apps/runtime/dist/assistant/browser.js +26 -0
- package/apps/runtime/dist/assistant/docs.js +158 -0
- package/apps/runtime/dist/assistant/knows.js +325 -0
- package/apps/runtime/dist/assistant/primer.js +26 -0
- package/apps/runtime/dist/authoring/guide.js +5 -0
- package/apps/runtime/dist/cli/features.js +50 -0
- package/apps/runtime/dist/cli/main.js +4 -1
- package/apps/runtime/dist/connectors/plugin-connections.js +123 -29
- package/apps/runtime/dist/db/migrations/tenant/0012_assistant_chat.sql +40 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0012_snapshot.json +3132 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +7 -0
- package/apps/runtime/dist/db/schema.js +30 -0
- package/apps/runtime/dist/docs/dev/index.md +34 -0
- package/apps/runtime/dist/docs/dev/meta.json +7 -0
- package/apps/runtime/dist/docs/dev/plugins/connections.md +70 -0
- package/apps/runtime/dist/docs/dev/plugins/events.md +86 -0
- package/apps/runtime/dist/docs/dev/plugins/examples.md +99 -0
- package/apps/runtime/dist/docs/dev/plugins/index.md +72 -0
- package/apps/runtime/dist/docs/dev/plugins/layout.md +88 -0
- package/apps/runtime/dist/docs/dev/plugins/meta.json +29 -0
- package/apps/runtime/dist/docs/dev/plugins/pro-modules.md +59 -0
- package/apps/runtime/dist/docs/dev/plugins/quick-start.md +92 -0
- package/apps/runtime/dist/docs/dev/plugins/reference.md +298 -0
- package/apps/runtime/dist/docs/dev/plugins/reloading.md +75 -0
- package/apps/runtime/dist/docs/dev/plugins/routes.md +123 -0
- package/apps/runtime/dist/docs/dev/plugins/runners.md +184 -0
- package/apps/runtime/dist/docs/dev/plugins/server.md +191 -0
- package/apps/runtime/dist/docs/dev/plugins/shipping.md +98 -0
- package/apps/runtime/dist/docs/dev/plugins/space.md +260 -0
- package/apps/runtime/dist/docs/dev/plugins/starters.md +62 -0
- package/apps/runtime/dist/docs/dev/plugins/studio.md +261 -0
- package/apps/runtime/dist/docs/dev/plugins/styling.md +109 -0
- package/apps/runtime/dist/docs/dev/plugins/tables.md +102 -0
- package/apps/runtime/dist/docs/dev/plugins/tools.md +128 -0
- package/apps/runtime/dist/docs/meta.json +3 -0
- package/apps/runtime/dist/docs/user/ai-apps.md +99 -0
- package/apps/runtime/dist/docs/user/assistant.md +52 -0
- package/apps/runtime/dist/docs/user/build-a-wizard.md +79 -0
- package/apps/runtime/dist/docs/user/command-line.md +76 -0
- package/apps/runtime/dist/docs/user/connectors.md +80 -0
- package/apps/runtime/dist/docs/user/data.md +61 -0
- package/apps/runtime/dist/docs/user/first-start.md +42 -0
- package/apps/runtime/dist/docs/user/index.md +45 -0
- package/apps/runtime/dist/docs/user/install.md +76 -0
- package/apps/runtime/dist/docs/user/meta.json +29 -0
- package/apps/runtime/dist/docs/user/models.md +100 -0
- package/apps/runtime/dist/docs/user/plugins.md +61 -0
- package/apps/runtime/dist/docs/user/run.md +92 -0
- package/apps/runtime/dist/docs/user/server.md +50 -0
- package/apps/runtime/dist/docs/user/space.md +97 -0
- package/apps/runtime/dist/docs/user/steps.md +196 -0
- package/apps/runtime/dist/docs/user/test-and-share.md +210 -0
- package/apps/runtime/dist/docs/user/troubleshooting.md +57 -0
- package/apps/runtime/dist/engine/runner.js +12 -5
- package/apps/runtime/dist/features.js +123 -0
- package/apps/runtime/dist/manage.js +24 -0
- package/apps/runtime/dist/plugins/discovery.js +11 -0
- package/apps/runtime/dist/plugins/loader.js +12 -0
- package/apps/runtime/dist/routes/internal.js +3 -0
- package/apps/runtime/dist/routes/studio.js +126 -73
- package/apps/runtime/dist/services/assistant-chats.js +267 -0
- package/apps/runtime/dist/services/project-index.js +1 -1
- package/apps/runtime/dist/services/space-origin.js +45 -2
- package/apps/web/dist/assets/{AboutDialog-DkY1j7k6.js → AboutDialog-CuiVGknS.js} +1 -1
- package/apps/web/dist/assets/{ChangelogDialog-820LI5Jt.js → ChangelogDialog-DCEQD3Jd.js} +1 -1
- package/apps/web/dist/assets/ChatView-DFxrG2PD.js +10 -0
- package/apps/web/dist/assets/EditorPage-B5rzKNMy.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-ChXfiMn_.js → FlowPreview-CYoR5DRY.js} +1 -1
- package/apps/web/dist/assets/{Harness-CfUfchNw.js → Harness-DAR1u4Dk.js} +1 -1
- package/apps/web/dist/assets/HomePage-BnYdh_Nx.js +1 -0
- package/apps/web/dist/assets/Marketplace-BDQNwMDB.js +2 -0
- package/apps/web/dist/assets/{NewWizardPage-C3NqEGmg.js → NewWizardPage-DztTEUlG.js} +1 -1
- package/apps/web/dist/assets/ProjectPage-B7-Lqx9a.js +2 -0
- package/apps/web/dist/assets/PublicRunner-CaH3bHHd.js +1 -0
- package/apps/web/dist/assets/{ReadOnly-C9P9aiU6.js → ReadOnly-DnB47DQC.js} +1 -1
- package/apps/web/dist/assets/RunDrawer-Dv0KRqGA.js +4 -0
- package/apps/web/dist/assets/RunnerView-Bzwv1u4j.js +2 -0
- package/apps/web/dist/assets/SettingsPage-oVO7CKvc.js +1 -0
- package/apps/web/dist/assets/SetupPage-D--8cWA0.js +1 -0
- package/apps/web/dist/assets/{SharePage-D5AvnhlY.js → SharePage-DjU08q7T.js} +1 -1
- package/apps/web/dist/assets/ThemeSwitch-C3EJRN06.js +1 -0
- package/apps/web/dist/assets/arrow-right-F7X_SjK9.js +1 -0
- package/apps/web/dist/assets/changelog-BRoVI_JJ.js +1 -0
- package/apps/web/dist/assets/{circle-alert-CPo3Naay.js → circle-alert-DdpaAYgd.js} +1 -1
- package/apps/web/dist/assets/{definition-CQGiBQ4X.js → definition-CHacJYmI.js} +2 -2
- package/apps/web/dist/assets/{external-link-CpU8aJY2.js → external-link-Dfc1VFvS.js} +1 -1
- package/apps/web/dist/assets/index-CX_yxZVQ.js +79 -0
- package/apps/web/dist/assets/index-Dr0bv52Q.css +1 -0
- package/apps/web/dist/assets/{lock-C_WlXIQ0.js → lock-Bu1wDFs8.js} +1 -1
- package/apps/web/dist/assets/{refresh-cw-Cli5WGnW.js → refresh-cw-BUDPOT39.js} +1 -1
- package/apps/web/dist/assets/{session-miDEdw2c.js → session-Blj2os_v.js} +1 -1
- package/apps/web/dist/assets/{settings-menu-ROT8Lbbj.js → settings-menu-BvxRnNtf.js} +1 -1
- package/apps/web/dist/assets/{sparkles-CP5H-O1E.js → sparkles-DWGJluyV.js} +1 -1
- package/apps/web/dist/assets/{trash-DU-hZpV0.js → trash-D9UB6TtS.js} +1 -1
- package/apps/web/dist/assets/ui-GsuU9WTj.js +9 -0
- package/apps/web/dist/assets/{upload-DMq5N6Fw.js → upload-BngimDBR.js} +1 -1
- package/apps/web/dist/assets/{volume-2-0SfkvOho.js → volume-2-BZju_bOP.js} +1 -1
- package/apps/web/dist/index.html +11 -11
- package/apps/web/dist/mcp/flow.html +51 -51
- package/node_modules/@engenty-wizards/plugin-sdk/dist/index.d.ts +103 -7
- package/node_modules/@engenty-wizards/plugin-sdk/dist/studio.d.ts +58 -4
- package/node_modules/@engenty-wizards/shared/dist/assistant.d.ts +113 -0
- package/node_modules/@engenty-wizards/shared/dist/assistant.js +55 -0
- package/node_modules/@engenty-wizards/shared/dist/definition.d.ts +56 -1
- package/node_modules/@engenty-wizards/shared/dist/definition.js +94 -4
- package/node_modules/@engenty-wizards/shared/dist/feature-flags.d.ts +54 -0
- package/node_modules/@engenty-wizards/shared/dist/feature-flags.js +46 -0
- package/node_modules/@engenty-wizards/shared/dist/features.d.ts +36 -0
- package/node_modules/@engenty-wizards/shared/dist/features.js +78 -0
- package/node_modules/@engenty-wizards/shared/dist/run.d.ts +3 -1
- package/package.json +1 -1
- package/apps/web/dist/assets/ChatView-DBl0iRMg.js +0 -10
- package/apps/web/dist/assets/EditorPage-BwJ-hqPg.js +0 -3
- package/apps/web/dist/assets/HomePage-BG5OZJkv.js +0 -1
- package/apps/web/dist/assets/Marketplace-BWhUJ4UX.js +0 -2
- package/apps/web/dist/assets/ProjectPage-Btf0TeP_.js +0 -2
- package/apps/web/dist/assets/PublicRunner-BxYinn04.js +0 -1
- package/apps/web/dist/assets/RunDrawer-DfnEMPTq.js +0 -4
- package/apps/web/dist/assets/RunnerView-CLrGb-oq.js +0 -2
- package/apps/web/dist/assets/SettingsPage-FWH84btd.js +0 -1
- package/apps/web/dist/assets/SetupPage-BI0kUNVW.js +0 -1
- package/apps/web/dist/assets/ThemeSwitch-C1qYFoYP.js +0 -1
- package/apps/web/dist/assets/arrow-right-Dpp_mpHe.js +0 -1
- package/apps/web/dist/assets/changelog-B6QMrVxX.js +0 -1
- package/apps/web/dist/assets/index-CIO9W4pe.js +0 -78
- package/apps/web/dist/assets/index-DKHClEy3.css +0 -1
- package/apps/web/dist/assets/ui-D92EkdEB.js +0 -9
|
@@ -2,40 +2,66 @@ import { anthropic } from "@ai-sdk/anthropic";
|
|
|
2
2
|
import { FACT_TYPES, factKey, PROJECT_FILE_KINDS, PROJECT_LIMITS, projectFileLimit, } from "@engenty-wizards/shared/projects";
|
|
3
3
|
import { Agent } from "@mastra/core/agent";
|
|
4
4
|
import { createTool } from "@mastra/core/tools";
|
|
5
|
+
import { nanoid } from "nanoid";
|
|
5
6
|
import { z } from "zod";
|
|
7
|
+
import { knowsTools } from "../assistant/knows.js";
|
|
8
|
+
import { PRIMER } from "../assistant/primer.js";
|
|
6
9
|
import { attachTools, costOf, gatewayTools, textModel } from "../models.js";
|
|
7
10
|
import { ServiceError } from "../services/errors.js";
|
|
8
11
|
import { knowledgeCounts } from "../services/knowledge.js";
|
|
9
12
|
import { addProjectFile, projectFileContent, projectFiles, updateProjectFile, } from "../services/project-files.js";
|
|
10
13
|
import { documentText } from "../services/project-index.js";
|
|
11
|
-
import { brandColorSchema, ownedProject, updateProject } from "../services/projects.js";
|
|
14
|
+
import { brandColorSchema, ownedProject, updateProject, } from "../services/projects.js";
|
|
12
15
|
import { categoryRows } from "../services/space-categories.js";
|
|
13
16
|
import { currentTenant } from "../tenants/tenant.js";
|
|
14
17
|
import { safeFetch } from "../tools/net-guard.js";
|
|
15
18
|
import { assistantPageOf, assistantToolsOf } from "../tools/plugin.js";
|
|
16
19
|
import { readWebsite } from "./website.js";
|
|
17
20
|
const MAX_STEPS = 24;
|
|
18
|
-
const SYSTEM = `You
|
|
21
|
+
const SYSTEM = `You are the assistant of the engenty wizards studio. You stand on every page of the studio except the wizard editor, which has a chat of its own: the space's pages, the settings, the plugins' pages. You help the person who works here understand the app, set it up and find their way.
|
|
19
22
|
|
|
20
|
-
You talk to the
|
|
23
|
+
You talk to the person. Answer in their language, briefly and warmly: a few sentences, a short list where it helps. Never mention JSON, ids, keys or tool names. Call it a space (German „Space“), never a project: the tools say project, the app says space.
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
${PRIMER}
|
|
26
|
+
|
|
27
|
+
WHAT YOU KNOW
|
|
28
|
+
- How the app works: from the guides of this installed version. Before you explain how something works, docs_search (and docs_read a page); link the page you used (engenty.ai/docs/…). How wizards are written: authoring_guide. Never guess at a menu, a button or a feature.
|
|
29
|
+
- Live data only from tools, never from memory: gallery_search / gallery_detail for templates, plans for plans and prices, account for the person, team, plan and credits, usage for what was spent, wizards and runs for the space, models for what thinks and makes here.
|
|
30
|
+
- You never buy, upgrade, top up or change a plan or a setting that costs money. Explain it, then open the page where the person does it (navigate, or give the billing page's link).
|
|
31
|
+
- Earlier chats: their summaries come with every message. chats_search and chat_read find more when the person refers to something said before.
|
|
32
|
+
|
|
33
|
+
THE PAGE
|
|
34
|
+
- Every message comes with the page the person writes from: its address, title, the section in view, an open item, what is selected, a line on what it holds and its fields. Answer about what they see; "this" and "here" mean that page.
|
|
35
|
+
- In the browser you may: navigate (open a studio page), highlight (dim the page except one element, or ring it, with a short card), look_at_page (what is visible: headings, buttons, fields with their values), read_section (one area in full), fill_field (put a value into a field, not saved), scroll_to. A page may add tools of its own (filter its list, show a week): use them to show what you talk about. A page you open during an answer brings its tools too: navigate and look_at_page list them, page_tool runs them. Targets are what look_at_page lists.
|
|
36
|
+
- Show rather than describe: open the page and highlight the place when the person asks where or how. look_at_page first when you are not sure what is there.
|
|
37
|
+
- fill_field never saves: say that the person checks and saves. You never press buttons for the person.
|
|
38
|
+
|
|
39
|
+
THE SPACE
|
|
40
|
+
- Where a space's tools are there, you also fill the space in: what every wizard of the space draws on — who they are (title, about), the brand (logos, colours), assets (images, graphics, videos), documents, and facts (label → value: address, VAT id, phone, opening hours — anything). The space as it is now comes with every message then; you change it ONLY through tools.
|
|
24
41
|
- A website: read_website it first. You get the page's text, colours from its stylesheet, logo candidates and the links to imprint / contact / about. Read those pages too (at most four more) for address, VAT id, register number, phone, email, bank details, who runs it.
|
|
25
|
-
- Facts: one fact per thing, a short label in the
|
|
26
|
-
- About: two to five sentences — what they do, for whom, what sets them apart, the tone they write in. Rewrite it only when you learned something new or the
|
|
42
|
+
- Facts: one fact per thing, a short label in the person's language ("Adresse", "UID", "Telefon"), the value on one line where it fits. Only what a source or the person states — never guess, never invent. Keep the facts that are there; a fact with a known label is updated.
|
|
43
|
+
- About: two to five sentences — what they do, for whom, what sets them apart, the tone they write in. Rewrite it only when you learned something new or the person asks.
|
|
27
44
|
- Colours: two to six, the main one first, each with a name that says what it is for ("Primär", "Hintergrund", "Text"). Take them from the stylesheet's variables or its most used colours, never from photos. set_colors replaces the list: pass all colours that should remain.
|
|
28
45
|
- Logos: save the best candidate (an SVG or a large PNG rather than a favicon); a second one only if it truly differs (for dark ground, a signet). A few images that show the business (header, team, products) may be saved as assets — a handful, never everything.
|
|
29
|
-
- Files the
|
|
30
|
-
-
|
|
31
|
-
-
|
|
46
|
+
- 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.
|
|
47
|
+
- Wissen (German „Wissen“) is what the wizards' steps look things up in: pages, tables and files, kept apart by typed Kategorien. Documents the person attaches are read into Wissen by themselves: say so; do not copy their text into facts unless asked. A website, a sitemap or a folder whose entries should go into Wissen is a source: where a tool proposes sources, use it (the person takes the proposal on its card); where there is none, say that sources need the Sources plugin.
|
|
48
|
+
- Talking from another page than Info & brand, change title, about, logos, colours and facts only when the person asks.
|
|
49
|
+
- A space that is read-only (synced from a local install, or a team that builds elsewhere) has no tools: say where it is changed.
|
|
32
50
|
|
|
33
|
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
51
|
+
HOW YOU WORK
|
|
52
|
+
- Call independent tools together. Work in few steps.
|
|
53
|
+
- Finish with one to three sentences: what you did or found, what is still open, and at most one question.`;
|
|
54
|
+
const LABELS = {
|
|
55
|
+
save_logo: { de: "Holt das Logo …", en: "Fetching the logo …" },
|
|
56
|
+
save_file: { de: "Holt eine Datei …", en: "Fetching a file …" },
|
|
57
|
+
set_profile: { de: "Schreibt Titel und Beschreibung …", en: "Writing title and description …" },
|
|
58
|
+
set_facts: { de: "Trägt Fakten ein …", en: "Entering facts …" },
|
|
59
|
+
set_colors: { de: "Trägt die Farben ein …", en: "Entering the colours …" },
|
|
60
|
+
look_at_file: { de: "Sieht sich eine Datei an …", en: "Looking at a file …" },
|
|
61
|
+
describe_file: { de: "Beschreibt eine Datei …", en: "Describing a file …" },
|
|
62
|
+
web_search: { de: "Sucht im Web …", en: "Searching the web …" },
|
|
63
|
+
};
|
|
64
|
+
function activity(tool, args, lang) {
|
|
39
65
|
const host = (u) => {
|
|
40
66
|
try {
|
|
41
67
|
return new URL(/^https?:/i.test(String(u)) ? String(u) : `https://${u}`).hostname;
|
|
@@ -44,25 +70,22 @@ function activity(tool, args) {
|
|
|
44
70
|
return "";
|
|
45
71
|
}
|
|
46
72
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return "Trägt Fakten ein …";
|
|
57
|
-
case "set_colors":
|
|
58
|
-
return "Trägt die Farben ein …";
|
|
59
|
-
case "look_at_file":
|
|
60
|
-
return "Sieht sich eine Datei an …";
|
|
61
|
-
case "describe_file":
|
|
62
|
-
return "Beschreibt eine Datei …";
|
|
63
|
-
default:
|
|
64
|
-
return null;
|
|
73
|
+
if (tool === "read_website") {
|
|
74
|
+
const at = args.url ? host(args.url) : "";
|
|
75
|
+
return lang === "de"
|
|
76
|
+
? at
|
|
77
|
+
? `Liest ${at} …`
|
|
78
|
+
: "Liest die Website …"
|
|
79
|
+
: at
|
|
80
|
+
? `Reading ${at} …`
|
|
81
|
+
: "Reading the website …";
|
|
65
82
|
}
|
|
83
|
+
const key = tool === "save_file" && args.kind === "logo"
|
|
84
|
+
? "save_logo"
|
|
85
|
+
: tool === "remove_facts"
|
|
86
|
+
? "set_facts"
|
|
87
|
+
: tool;
|
|
88
|
+
return LABELS[key]?.[lang] ?? null;
|
|
66
89
|
}
|
|
67
90
|
async function attempt(run) {
|
|
68
91
|
try {
|
|
@@ -94,11 +117,14 @@ async function projectState(userId, projectId) {
|
|
|
94
117
|
})),
|
|
95
118
|
};
|
|
96
119
|
}
|
|
97
|
-
|
|
98
|
-
|
|
120
|
+
/** The space's own tools: what fills it in. */
|
|
121
|
+
function spaceTools(input, project, wrote) {
|
|
122
|
+
const { signal } = input;
|
|
123
|
+
const userId = input.user.id;
|
|
124
|
+
const projectId = project.id;
|
|
99
125
|
// Said from inside the tool: an installed AI client runs tools without the stream showing it.
|
|
100
126
|
const doing = (tool, args = {}) => {
|
|
101
|
-
const label = activity(tool, args);
|
|
127
|
+
const label = activity(tool, args, input.lang);
|
|
102
128
|
if (label) {
|
|
103
129
|
input.onActivity?.(label);
|
|
104
130
|
}
|
|
@@ -297,38 +323,128 @@ function buildTools(input, resolved, wrote) {
|
|
|
297
323
|
},
|
|
298
324
|
}),
|
|
299
325
|
};
|
|
300
|
-
|
|
301
|
-
|
|
326
|
+
return tools;
|
|
327
|
+
}
|
|
328
|
+
/** A browser tool as the model calls it: sent to the page, its answer awaited. */
|
|
329
|
+
function browserTool(input, spec) {
|
|
330
|
+
let inputSchema;
|
|
331
|
+
try {
|
|
332
|
+
inputSchema = z.fromJSONSchema(spec.parameters);
|
|
302
333
|
}
|
|
303
|
-
|
|
304
|
-
|
|
334
|
+
catch {
|
|
335
|
+
return null;
|
|
305
336
|
}
|
|
306
|
-
return
|
|
337
|
+
return createTool({
|
|
338
|
+
id: spec.name,
|
|
339
|
+
description: spec.description,
|
|
340
|
+
inputSchema,
|
|
341
|
+
// The id is the one `traced` gave the call: the browser answers to it.
|
|
342
|
+
execute: async (args, ctx) => input.callBrowser({
|
|
343
|
+
toolCallId: ctx?.toolCallId ?? nanoid(10),
|
|
344
|
+
toolCallName: spec.name,
|
|
345
|
+
args,
|
|
346
|
+
}),
|
|
347
|
+
});
|
|
307
348
|
}
|
|
308
|
-
/**
|
|
309
|
-
|
|
349
|
+
/** Text of a tool's result for the chat's record, cut short. */
|
|
350
|
+
function resultText(out) {
|
|
351
|
+
if (typeof out === "string") {
|
|
352
|
+
return out.slice(0, 1200);
|
|
353
|
+
}
|
|
354
|
+
try {
|
|
355
|
+
return (JSON.stringify(out, (key, value) => (key === "image" ? "(picture)" : value))?.slice(0, 1200) ?? "");
|
|
356
|
+
}
|
|
357
|
+
catch {
|
|
358
|
+
return "";
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Every tool says when it is called and what it gave back (AG-UI's tool call), from inside: an
|
|
363
|
+
* installed AI client runs tools without the stream showing it. A browser tool's call carries the
|
|
364
|
+
* same id to the page.
|
|
365
|
+
*/
|
|
366
|
+
function traced(tools, input, record) {
|
|
367
|
+
for (const [name, tool] of Object.entries(tools)) {
|
|
368
|
+
const run = tool?.execute;
|
|
369
|
+
if (typeof run !== "function") {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
tool.execute = async (args, ctx = {}) => {
|
|
373
|
+
const toolCallId = nanoid(10);
|
|
374
|
+
const call = { toolCallId, toolCallName: name, args };
|
|
375
|
+
input.onToolCall?.(call);
|
|
376
|
+
const out = await run(args, { ...ctx, toolCallId });
|
|
377
|
+
const content = resultText(out);
|
|
378
|
+
record.push({ ...call, result: content });
|
|
379
|
+
input.onToolResult?.({ toolCallId, content });
|
|
380
|
+
return out;
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/** The part of the space page a route stands on. */
|
|
385
|
+
function partOf(route) {
|
|
386
|
+
const m = route.match(/^\/space\/(info|knowledge|data|results)/);
|
|
387
|
+
return m ? m[1] : route === "/space" ? "info" : null;
|
|
388
|
+
}
|
|
389
|
+
/** One turn of the studio assistant. */
|
|
390
|
+
export async function runStudioAssistant(input) {
|
|
310
391
|
let changed = false;
|
|
311
392
|
const resolved = await textModel("high");
|
|
312
393
|
const wrote = () => {
|
|
313
394
|
changed = true;
|
|
314
395
|
input.onChanged?.();
|
|
315
396
|
};
|
|
316
|
-
const
|
|
397
|
+
const cards = [];
|
|
398
|
+
const { project } = input;
|
|
399
|
+
const space = project && input.writable ? project : null;
|
|
400
|
+
const tools = {
|
|
401
|
+
...knowsTools({
|
|
402
|
+
user: input.user,
|
|
403
|
+
project: project
|
|
404
|
+
? { id: project.id, name: project.name, mcpServers: project.mcpServers }
|
|
405
|
+
: null,
|
|
406
|
+
lang: input.lang,
|
|
407
|
+
chatId: input.chat.id,
|
|
408
|
+
doing: (label) => input.onActivity?.(label),
|
|
409
|
+
}),
|
|
410
|
+
...(space ? spaceTools(input, space, wrote) : {}),
|
|
411
|
+
};
|
|
317
412
|
// What the tenant's plugins let the assistant do besides: a source of knowledge, say.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
413
|
+
if (space) {
|
|
414
|
+
Object.assign(tools, await assistantToolsOf({
|
|
415
|
+
tenantId: currentTenant(),
|
|
416
|
+
space: { id: space.id, name: space.name },
|
|
417
|
+
userId: input.user.id,
|
|
418
|
+
signal: input.signal ?? new AbortController().signal,
|
|
419
|
+
emit: (message) => input.onActivity?.(message),
|
|
420
|
+
changed: wrote,
|
|
421
|
+
card: (card) => {
|
|
422
|
+
cards.push(card);
|
|
423
|
+
input.onCard?.(card);
|
|
424
|
+
},
|
|
425
|
+
}));
|
|
426
|
+
}
|
|
427
|
+
for (const spec of input.browserTools) {
|
|
428
|
+
if (tools[spec.name]) {
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
const tool = browserTool(input, spec);
|
|
432
|
+
if (tool) {
|
|
433
|
+
tools[spec.name] = tool;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
const toolCalls = [];
|
|
437
|
+
traced(tools, input, toolCalls);
|
|
438
|
+
if (resolved.vendor === "anthropic") {
|
|
439
|
+
tools.web_search = anthropic.tools.webSearch_20250305({ maxUses: 5 });
|
|
440
|
+
}
|
|
441
|
+
else if (resolved.gateway) {
|
|
442
|
+
tools.web_search = gatewayTools.perplexitySearch({ maxResults: 6 });
|
|
443
|
+
}
|
|
328
444
|
attachTools(resolved, tools);
|
|
329
445
|
const agent = new Agent({
|
|
330
|
-
id: "
|
|
331
|
-
name: "
|
|
446
|
+
id: "studio-assistant",
|
|
447
|
+
name: "Studio assistant",
|
|
332
448
|
instructions: {
|
|
333
449
|
role: "system",
|
|
334
450
|
content: SYSTEM,
|
|
@@ -337,28 +453,42 @@ export async function runProjectAssistant(input) {
|
|
|
337
453
|
model: resolved.model,
|
|
338
454
|
tools,
|
|
339
455
|
});
|
|
340
|
-
const page = input.
|
|
341
|
-
|
|
456
|
+
const page = input.page;
|
|
457
|
+
const pluginPage = page?.plugin && project
|
|
458
|
+
? await assistantPageOf(currentTenant(), page.plugin, { id: project.id, name: project.name })
|
|
342
459
|
: null;
|
|
460
|
+
const part = page ? partOf(page.route) : null;
|
|
461
|
+
const { history } = input;
|
|
343
462
|
const context = [
|
|
463
|
+
history.earlier.length
|
|
464
|
+
? `Earlier chats with this person, newest first (chats_search finds more):\n${history.earlier
|
|
465
|
+
.map((c) => `- ${c.closedAt.slice(0, 10)} "${c.title}": ${c.summary.replace(/\n+/g, " ")}`)
|
|
466
|
+
.join("\n")}`
|
|
467
|
+
: "",
|
|
468
|
+
history.running ? `What this chat said before the messages above:\n${history.running}` : "",
|
|
344
469
|
page
|
|
470
|
+
? `Current page:\n\`\`\`json\n${JSON.stringify(page)}\n\`\`\``
|
|
471
|
+
: "The person writes from a page that tells nothing about itself.",
|
|
472
|
+
pluginPage
|
|
345
473
|
? [
|
|
346
|
-
`The
|
|
347
|
-
...
|
|
474
|
+
`The page is the plugin "${pluginPage.name}"'s${pluginPage.description ? `: ${pluginPage.description}` : "."} They most likely mean what that page holds: use its tools first (their names begin with "${page?.plugin?.replace(/-/g, "_")}_"). Space settings stay possible when they ask for them.`,
|
|
475
|
+
...pluginPage.blocks,
|
|
348
476
|
].join("\n\n")
|
|
349
477
|
: "",
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
478
|
+
space
|
|
479
|
+
? `The space now:\n\`\`\`json\n${JSON.stringify(await projectState(input.user.id, space.id))}\n\`\`\``
|
|
480
|
+
: project
|
|
481
|
+
? `The space "${project.name}" is read-only here: nothing of it can be changed in this studio.`
|
|
482
|
+
: "No space is open.",
|
|
483
|
+
part === "knowledge"
|
|
484
|
+
? "The person talks to you from Wissen: they want the wizards to know something."
|
|
353
485
|
: "",
|
|
354
486
|
input.message,
|
|
355
487
|
]
|
|
356
488
|
.filter(Boolean)
|
|
357
489
|
.join("\n\n");
|
|
358
490
|
const stream = await agent.stream([
|
|
359
|
-
...
|
|
360
|
-
.slice(-12)
|
|
361
|
-
.map((m) => m.role === "user"
|
|
491
|
+
...history.messages.map((m) => m.role === "user"
|
|
362
492
|
? { role: "user", content: m.content }
|
|
363
493
|
: { role: "assistant", content: m.content }),
|
|
364
494
|
{ role: "user", content: context },
|
|
@@ -378,7 +508,7 @@ export async function runProjectAssistant(input) {
|
|
|
378
508
|
else if (chunk.type === "tool-call") {
|
|
379
509
|
// The model's own search has no tool of ours that could say so.
|
|
380
510
|
if (payload.toolName === "web_search") {
|
|
381
|
-
input.onActivity?.(
|
|
511
|
+
input.onActivity?.(LABELS.web_search[input.lang]);
|
|
382
512
|
}
|
|
383
513
|
// Keep what it says before and after a tool call apart.
|
|
384
514
|
if (reply.trim() && !reply.endsWith("\n\n")) {
|
|
@@ -391,5 +521,5 @@ export async function runProjectAssistant(input) {
|
|
|
391
521
|
}
|
|
392
522
|
}
|
|
393
523
|
const usage = (await stream.totalUsage) ?? (await stream.usage);
|
|
394
|
-
return { reply: reply.trim(), changed, costUsd: costOf(resolved, usage) };
|
|
524
|
+
return { reply: reply.trim(), changed, cards, toolCalls, costUsd: costOf(resolved, usage) };
|
|
395
525
|
}
|
|
@@ -126,9 +126,31 @@ const linkText = (inner) => inner
|
|
|
126
126
|
.replace(/\s+/g, " ")
|
|
127
127
|
.trim()
|
|
128
128
|
.slice(0, 120);
|
|
129
|
+
/** The `<script type="application/ld+json">` blocks of a page, parsed; a `@graph` is spread out. */
|
|
130
|
+
export function jsonLdOf(html) {
|
|
131
|
+
const out = [];
|
|
132
|
+
for (const m of html.matchAll(/<script\b[^>]*type\s*=\s*["']application\/ld\+json["'][^>]*>([\s\S]*?)<\/script>/gi)) {
|
|
133
|
+
let parsed;
|
|
134
|
+
try {
|
|
135
|
+
parsed = JSON.parse(m[1].trim());
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
for (const item of Array.isArray(parsed) ? parsed : [parsed]) {
|
|
141
|
+
const graph = item?.["@graph"];
|
|
142
|
+
out.push(...(Array.isArray(graph) ? graph : [item]));
|
|
143
|
+
}
|
|
144
|
+
if (out.length >= 50) {
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return out.slice(0, 50);
|
|
149
|
+
}
|
|
129
150
|
/**
|
|
130
|
-
* A page for a plugin (`server.web.read`): its whole text as Markdown
|
|
131
|
-
* `readWebsite`, which picks what the assistant needs of a
|
|
151
|
+
* A page for a plugin (`server.web.read`): its whole text as Markdown, every link and its
|
|
152
|
+
* structured data, as against `readWebsite`, which picks what the assistant needs of a
|
|
153
|
+
* company's site.
|
|
132
154
|
*/
|
|
133
155
|
export async function readPage(raw, signal) {
|
|
134
156
|
const { res, base, html } = await loadPage(raw, signal);
|
|
@@ -148,6 +170,7 @@ export async function readPage(raw, signal) {
|
|
|
148
170
|
description: metaOf(html, "description") || metaOf(html, "og:description"),
|
|
149
171
|
text: htmlToMarkdown(html).slice(0, 400_000),
|
|
150
172
|
links,
|
|
173
|
+
jsonld: jsonLdOf(html),
|
|
151
174
|
};
|
|
152
175
|
}
|
|
153
176
|
export async function readWebsite(raw, signal) {
|
package/apps/runtime/dist/app.js
CHANGED
|
@@ -12,6 +12,7 @@ import { ZodError } from "zod";
|
|
|
12
12
|
import { authRoutes, principalOf } from "./auth/index.js";
|
|
13
13
|
import { db, schema, withTenant } from "./db/client.js";
|
|
14
14
|
import { basePath, env } from "./env.js";
|
|
15
|
+
import { features } from "./features.js";
|
|
15
16
|
import { allowed, hashIp } from "./limits.js";
|
|
16
17
|
import { linkPreview } from "./link-preview.js";
|
|
17
18
|
import { discovery, managed } from "./manage.js";
|
|
@@ -61,7 +62,8 @@ const hostAllowed = (host) => ownHosts.has(host) || loopback.test(host);
|
|
|
61
62
|
const ticketedRun = /^\/api\/runs\/([^/]+)(\/|$)/;
|
|
62
63
|
app.use("*", async (c, next) => {
|
|
63
64
|
if (!managed && !hostAllowed(c.req.header("host") ?? new URL(c.req.url).host)) {
|
|
64
|
-
|
|
65
|
+
// Says where it does answer: the studio opened under another name shows the way there.
|
|
66
|
+
return c.json({ error: "Forbidden host", code: "host", appUrl: env.appUrl }, 403);
|
|
65
67
|
}
|
|
66
68
|
const origin = c.req.header("origin");
|
|
67
69
|
const path = c.req.path.startsWith(`${basePath}/`)
|
|
@@ -134,6 +136,8 @@ app.get("/api/config", (c) => c.json({
|
|
|
134
136
|
devLogin: !managed && env.devLogin,
|
|
135
137
|
/** The product's own site, for a runtime that runs alone. */
|
|
136
138
|
site: managed ? null : env.local.cloudUrl,
|
|
139
|
+
/** The feature switches before anyone is signed in: the code's, the environment's, the file's. */
|
|
140
|
+
features: features(),
|
|
137
141
|
}));
|
|
138
142
|
// OAuth discovery for MCP clients (RFC 9728): the Manage-App is the authorization server.
|
|
139
143
|
app.get("/.well-known/oauth-protected-resource/*", async (c) => {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BROWSER_TOOL_TIMEOUT_MS } from "@engenty-wizards/shared/assistant";
|
|
2
|
+
const waiting = new Map();
|
|
3
|
+
/** The browser's answer to `callId`; a note for the model when it gives none in time. */
|
|
4
|
+
export function awaitBrowser(userId, callId, signal) {
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
const done = (result) => {
|
|
7
|
+
clearTimeout(timer);
|
|
8
|
+
signal?.removeEventListener("abort", aborted);
|
|
9
|
+
waiting.delete(callId);
|
|
10
|
+
resolve(result);
|
|
11
|
+
};
|
|
12
|
+
const aborted = () => done({ error: "The person closed the page." });
|
|
13
|
+
const timer = setTimeout(() => done({ error: "The page did not answer in time; it may be closed or busy." }), BROWSER_TOOL_TIMEOUT_MS);
|
|
14
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
15
|
+
waiting.set(callId, { userId, resolve: done });
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** Hands the browser's result to the turn that waits for it; false when none does. */
|
|
19
|
+
export function answerBrowser(userId, callId, result) {
|
|
20
|
+
const found = waiting.get(callId);
|
|
21
|
+
if (!found || found.userId !== userId) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
found.resolve(result);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, relative } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/**
|
|
5
|
+
* The user guide and the plugin guide (docs/content), as the studio assistant reads them: bundled
|
|
6
|
+
* with each release (the build copies them to dist/docs), so an answer matches the installed
|
|
7
|
+
* version. Links point to engenty.ai/docs.
|
|
8
|
+
*/
|
|
9
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const SITE = "https://engenty.ai/docs";
|
|
11
|
+
/** Built: dist/docs. From source: the repo's docs/content. */
|
|
12
|
+
function contentDir() {
|
|
13
|
+
for (const dir of [
|
|
14
|
+
join(here, "..", "docs"),
|
|
15
|
+
join(here, "..", "..", "..", "..", "docs", "content"),
|
|
16
|
+
]) {
|
|
17
|
+
if (existsSync(join(dir, "user"))) {
|
|
18
|
+
return dir;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
let pages = null;
|
|
24
|
+
function walk(dir) {
|
|
25
|
+
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => entry.isDirectory()
|
|
26
|
+
? walk(join(dir, entry.name))
|
|
27
|
+
: /\.mdx?$/.test(entry.name)
|
|
28
|
+
? [join(dir, entry.name)]
|
|
29
|
+
: []);
|
|
30
|
+
}
|
|
31
|
+
function parse(root, file) {
|
|
32
|
+
const raw = readFileSync(file, "utf8");
|
|
33
|
+
const front = raw.match(/^---\n([\s\S]*?)\n---\n?/);
|
|
34
|
+
const meta = Object.fromEntries((front?.[1] ?? "")
|
|
35
|
+
.split("\n")
|
|
36
|
+
.map((line) => line.match(/^(\w+):\s*(.*)$/))
|
|
37
|
+
.filter((m) => Boolean(m))
|
|
38
|
+
.map((m) => [m[1], m[2].replace(/^["']|["']$/g, "")]));
|
|
39
|
+
const text = raw
|
|
40
|
+
.slice(front?.[0].length ?? 0)
|
|
41
|
+
// Pictures say nothing to the assistant; their alt text stays.
|
|
42
|
+
.replace(/!\[([^\]]*)\]\([^)]*\)/g, "($1)")
|
|
43
|
+
.trim();
|
|
44
|
+
const id = relative(root, file)
|
|
45
|
+
.replace(/\\/g, "/")
|
|
46
|
+
.replace(/\.mdx?$/, "");
|
|
47
|
+
const sections = [];
|
|
48
|
+
let heading = meta.title ?? id;
|
|
49
|
+
let lines = [];
|
|
50
|
+
for (const line of text.split("\n")) {
|
|
51
|
+
const h = line.match(/^#{2,3}\s+(.*)$/);
|
|
52
|
+
if (h) {
|
|
53
|
+
if (lines.join("").trim()) {
|
|
54
|
+
sections.push({ heading, text: lines.join("\n").trim() });
|
|
55
|
+
}
|
|
56
|
+
heading = h[1].trim();
|
|
57
|
+
lines = [];
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
lines.push(line);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (lines.join("").trim()) {
|
|
64
|
+
sections.push({ heading, text: lines.join("\n").trim() });
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
id,
|
|
68
|
+
title: meta.title ?? id,
|
|
69
|
+
description: meta.description ?? "",
|
|
70
|
+
url: `${SITE}/${id.replace(/\/index$/, "")}`,
|
|
71
|
+
text,
|
|
72
|
+
sections,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Every page of the guides; read once. */
|
|
76
|
+
export function docPages() {
|
|
77
|
+
if (!pages) {
|
|
78
|
+
const root = contentDir();
|
|
79
|
+
pages = root ? walk(root).map((file) => parse(root, file)) : [];
|
|
80
|
+
}
|
|
81
|
+
return pages;
|
|
82
|
+
}
|
|
83
|
+
const fold = (s) => s.toLowerCase().normalize("NFD").replace(/\p{M}/gu, "");
|
|
84
|
+
function termsOf(query) {
|
|
85
|
+
return [
|
|
86
|
+
...new Set(fold(query)
|
|
87
|
+
.split(/[^\p{L}\p{N}]+/u)
|
|
88
|
+
.filter((w) => w.length >= 3)),
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
function count(hay, term) {
|
|
92
|
+
let n = 0;
|
|
93
|
+
for (let at = hay.indexOf(term); at >= 0; at = hay.indexOf(term, at + term.length)) {
|
|
94
|
+
n++;
|
|
95
|
+
}
|
|
96
|
+
return n;
|
|
97
|
+
}
|
|
98
|
+
/** The sections of the guides that speak most of the query's words, best first. */
|
|
99
|
+
export function searchDocs(query, limit = 6) {
|
|
100
|
+
const terms = termsOf(query);
|
|
101
|
+
if (!terms.length) {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
const hits = [];
|
|
105
|
+
for (const page of docPages()) {
|
|
106
|
+
const title = fold(`${page.title} ${page.description}`);
|
|
107
|
+
for (const section of page.sections) {
|
|
108
|
+
const heading = fold(section.heading);
|
|
109
|
+
const body = fold(section.text);
|
|
110
|
+
let score = 0;
|
|
111
|
+
let matched = 0;
|
|
112
|
+
for (const term of terms) {
|
|
113
|
+
const found = count(title, term) * 3 + count(heading, term) * 4 + count(body, term);
|
|
114
|
+
if (found) {
|
|
115
|
+
matched++;
|
|
116
|
+
score += Math.log(1 + found);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (matched) {
|
|
120
|
+
hits.push({ page, heading: section.heading, text: section.text, score: score * matched });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return hits
|
|
125
|
+
.sort((a, b) => b.score - a.score)
|
|
126
|
+
.slice(0, limit)
|
|
127
|
+
.map((h) => ({
|
|
128
|
+
page: h.page.id,
|
|
129
|
+
title: h.page.title,
|
|
130
|
+
section: h.heading,
|
|
131
|
+
url: h.page.url,
|
|
132
|
+
excerpt: h.text.slice(0, 700),
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
/** One page of the guides, from character `from` on. */
|
|
136
|
+
export function readDoc(id, from = 0, size = 14_000) {
|
|
137
|
+
const wanted = id
|
|
138
|
+
.replace(/^https?:\/\/[^/]+\/docs\//, "")
|
|
139
|
+
.replace(/^\/+|\/+$/g, "")
|
|
140
|
+
.replace(/\.mdx?$/, "");
|
|
141
|
+
const page = docPages().find((p) => p.id === wanted) ??
|
|
142
|
+
docPages().find((p) => p.id === `${wanted}/index`) ??
|
|
143
|
+
docPages().find((p) => p.id.endsWith(`/${wanted}`));
|
|
144
|
+
if (!page) {
|
|
145
|
+
return {
|
|
146
|
+
error: `No page "${id}".`,
|
|
147
|
+
pages: docPages().map((p) => `${p.id}: ${p.title}`),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const start = Math.max(0, Math.min(from, page.text.length));
|
|
151
|
+
return {
|
|
152
|
+
page: page.id,
|
|
153
|
+
title: page.title,
|
|
154
|
+
url: page.url,
|
|
155
|
+
text: page.text.slice(start, start + size),
|
|
156
|
+
next: start + size < page.text.length ? start + size : null,
|
|
157
|
+
};
|
|
158
|
+
}
|