wizards 0.2.48 → 0.2.50
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/README.md +2 -1
- package/apps/runtime/dist/apple/wizards-apple +0 -0
- package/apps/runtime/dist/cli/status.js +1 -1
- package/apps/runtime/dist/db/migrations/tenant/0013_run_origin.sql +1 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0013_snapshot.json +3139 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +7 -0
- package/apps/runtime/dist/db/schema.js +5 -0
- package/apps/runtime/dist/docs/user/meta.json +1 -0
- package/apps/runtime/dist/docs/user/models.md +25 -0
- package/apps/runtime/dist/docs/user/test-and-share.md +7 -1
- package/apps/runtime/dist/docs/user/wordpress.md +103 -0
- package/apps/runtime/dist/engine/device.js +182 -0
- package/apps/runtime/dist/engine/events.js +3 -0
- package/apps/runtime/dist/engine/input.js +3 -0
- package/apps/runtime/dist/engine/prepare.js +1 -0
- package/apps/runtime/dist/engine/requirements.js +6 -109
- package/apps/runtime/dist/engine/runner.js +1 -0
- package/apps/runtime/dist/engine/steps.js +2 -1
- package/apps/runtime/dist/env.js +1 -1
- package/apps/runtime/dist/harness/apple.js +258 -0
- package/apps/runtime/dist/harness/model.js +3 -1
- package/apps/runtime/dist/mcp/auth.js +3 -0
- package/apps/runtime/dist/mcp/scopes.js +11 -2
- package/apps/runtime/dist/media/transcribe.js +6 -1
- package/apps/runtime/dist/models.js +63 -9
- package/apps/runtime/dist/routes/api.js +232 -1
- package/apps/runtime/dist/routes/plugins.js +6 -2
- package/apps/runtime/dist/routes/runs.js +65 -3
- package/apps/runtime/dist/routes/studio.js +11 -1
- package/apps/runtime/dist/services/api.js +244 -0
- package/apps/runtime/dist/services/engenty-shapes.js +1 -1
- package/apps/runtime/dist/services/package.js +77 -9
- package/apps/runtime/dist/services/runs.js +4 -3
- package/apps/runtime/dist/services/site.js +262 -0
- package/apps/web/dist/assets/AboutDialog-BRjyFW7N.js +1 -0
- package/apps/web/dist/assets/ChangelogDialog-DU-fYMNG.js +1 -0
- package/apps/web/dist/assets/ChatView-Chv8rH9v.js +10 -0
- package/apps/web/dist/assets/EditorPage-BEEiRjpL.js +3 -0
- package/apps/web/dist/assets/FlowPreview-911P5v6v.js +1 -0
- package/apps/web/dist/assets/{Harness-DAR1u4Dk.js → Harness-DE9pS-ZP.js} +2 -2
- package/apps/web/dist/assets/Marketplace-DfoRgS49.js +2 -0
- package/apps/web/dist/assets/NewWizardPage-BwZ39he_.js +1 -0
- package/apps/web/dist/assets/ProjectPage-BB8feiW1.js +2 -0
- package/apps/web/dist/assets/PublicRunner-JsgGibnC.js +1 -0
- package/apps/web/dist/assets/ReadOnly-ClY8xMQS.js +1 -0
- package/apps/web/dist/assets/RunDrawer-CxJtIQlP.js +4 -0
- package/apps/web/dist/assets/RunnerView-DvIBDzr1.js +1 -0
- package/apps/web/dist/assets/SettingsPage-DJBQvyom.js +1 -0
- package/apps/web/dist/assets/SetupPage-BVqbshmT.js +1 -0
- package/apps/web/dist/assets/SharePage-ChQxeHFC.js +1 -0
- package/apps/web/dist/assets/brand-CNF5NxD9.js +2 -0
- package/apps/web/dist/assets/changelog-OdE5-SsX.js +1 -0
- package/apps/web/dist/assets/circle-alert-r2qc1Vej.js +1 -0
- package/apps/web/dist/assets/engenty-eXZ-EAwd.js +1 -0
- package/apps/web/dist/assets/esm-cYET-omK.js +7 -0
- package/apps/web/dist/assets/{fluffy-engenty-D7rFrNcJ.js → fluffy-engenty-ClRou7yu.js} +1 -1
- package/apps/web/dist/assets/index-CDgxPZY_.js +11 -0
- package/apps/web/dist/assets/jsx-runtime-BNakU3Ej.js +1 -0
- package/apps/web/dist/assets/lock-N-C6-ZeG.js +1 -0
- package/apps/web/dist/assets/logo-C4V2_3Ar.js +1 -0
- package/apps/web/dist/assets/oss-credits-BQQX0kvk.js +1 -0
- package/apps/web/dist/assets/pdfmake-Cx9scTbD.js +228 -0
- package/apps/web/dist/assets/rolldown-runtime-hePW80VL.js +1 -0
- package/apps/web/dist/assets/session-Cpg5S8B3.js +1 -0
- package/apps/web/dist/assets/settings-menu-B763_KXf.js +1 -0
- package/apps/web/dist/assets/ui-DKcggL84.js +133 -0
- package/apps/web/dist/assets/upload-D24SDj7W.js +1 -0
- package/apps/web/dist/assets/useMutation-DKyQXgyj.js +1 -0
- package/apps/web/dist/assets/vfs_fonts-DllDSYt1.js +1 -0
- package/apps/web/dist/assets/volume-2-C-Y7-S4b.js +1 -0
- package/apps/web/dist/embed.js +57 -8
- package/apps/web/dist/index.html +11 -15
- package/apps/web/dist/mcp/flow.html +434 -206
- package/modules/calls/dist/client.css +1 -1
- package/modules/sms/dist/client.css +1 -1
- package/modules/whatsapp/dist/client.css +1 -1
- package/node_modules/@engenty-wizards/shared/dist/definition.d.ts +2 -0
- package/node_modules/@engenty-wizards/shared/dist/fit.d.ts +71 -0
- package/node_modules/@engenty-wizards/shared/dist/fit.js +381 -0
- package/package.json +1 -1
- package/apps/web/dist/assets/AboutDialog-CuiVGknS.js +0 -1
- package/apps/web/dist/assets/ChangelogDialog-DCEQD3Jd.js +0 -1
- package/apps/web/dist/assets/ChatView-DFxrG2PD.js +0 -10
- package/apps/web/dist/assets/EditorPage-B5rzKNMy.js +0 -3
- package/apps/web/dist/assets/FlowPreview-CYoR5DRY.js +0 -1
- package/apps/web/dist/assets/HomePage-BnYdh_Nx.js +0 -1
- package/apps/web/dist/assets/Marketplace-BDQNwMDB.js +0 -2
- package/apps/web/dist/assets/NewWizardPage-DztTEUlG.js +0 -1
- package/apps/web/dist/assets/ProjectPage-B7-Lqx9a.js +0 -2
- package/apps/web/dist/assets/PublicRunner-CaH3bHHd.js +0 -1
- package/apps/web/dist/assets/ReadOnly-DnB47DQC.js +0 -1
- package/apps/web/dist/assets/RunDrawer-Dv0KRqGA.js +0 -4
- package/apps/web/dist/assets/RunnerView-Bzwv1u4j.js +0 -2
- package/apps/web/dist/assets/SettingsPage-oVO7CKvc.js +0 -1
- package/apps/web/dist/assets/SetupPage-D--8cWA0.js +0 -1
- package/apps/web/dist/assets/SharePage-DjU08q7T.js +0 -1
- package/apps/web/dist/assets/ThemeSwitch-C3EJRN06.js +0 -1
- package/apps/web/dist/assets/arrow-right-F7X_SjK9.js +0 -1
- package/apps/web/dist/assets/changelog-BRoVI_JJ.js +0 -1
- package/apps/web/dist/assets/circle-alert-DdpaAYgd.js +0 -1
- package/apps/web/dist/assets/definition-CHacJYmI.js +0 -56
- package/apps/web/dist/assets/engenty-Cm8yZLON.js +0 -1
- package/apps/web/dist/assets/esm-rJY59Ixx.js +0 -7
- package/apps/web/dist/assets/external-link-Dfc1VFvS.js +0 -1
- package/apps/web/dist/assets/index-CX_yxZVQ.js +0 -79
- package/apps/web/dist/assets/jsx-runtime-D3jfb0Ew.js +0 -1
- package/apps/web/dist/assets/lock-Bu1wDFs8.js +0 -1
- package/apps/web/dist/assets/logo-C80CWCDM.js +0 -1
- package/apps/web/dist/assets/oss-credits-CIxnNZor.js +0 -1
- package/apps/web/dist/assets/react-dom-DenslgJm.js +0 -1
- package/apps/web/dist/assets/refresh-cw-BUDPOT39.js +0 -1
- package/apps/web/dist/assets/session-Blj2os_v.js +0 -1
- package/apps/web/dist/assets/settings-menu-BvxRnNtf.js +0 -1
- package/apps/web/dist/assets/sparkles-DWGJluyV.js +0 -1
- package/apps/web/dist/assets/trash-D9UB6TtS.js +0 -1
- package/apps/web/dist/assets/ui-GsuU9WTj.js +0 -9
- package/apps/web/dist/assets/upload-BngimDBR.js +0 -1
- package/apps/web/dist/assets/useMutation-DKcIz55V.js +0 -1
- package/apps/web/dist/assets/volume-2-BZju_bOP.js +0 -1
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
2
|
import { bodyLimit } from "hono/body-limit";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
+
import { balanceCredits } from "../credits/credits.js";
|
|
4
5
|
import { withTenant } from "../db/client.js";
|
|
6
|
+
import { env } from "../env.js";
|
|
5
7
|
import { allowed } from "../limits.js";
|
|
8
|
+
import { managed, tenantInfo } from "../manage.js";
|
|
6
9
|
import { authenticate, principalOf } from "../mcp/auth.js";
|
|
7
|
-
import {
|
|
10
|
+
import { loadedPlugin, pluginIdsOf } from "../plugins/registry.js";
|
|
11
|
+
import { asRole, mayBuild, mayCreate } from "../services/access.js";
|
|
12
|
+
import { apiRun, apiRuns, apiSpaceDetails, apiSpaces, apiWizards } from "../services/api.js";
|
|
8
13
|
import { ServiceError } from "../services/errors.js";
|
|
9
14
|
import { deleteFile, draftFiles, writeFile } from "../services/files.js";
|
|
15
|
+
import { asLang, searchMarketplace } from "../services/marketplace.js";
|
|
16
|
+
import { exportWizard, PACKAGE_MIME } from "../services/package.js";
|
|
17
|
+
import { siteJob, siteSchema, startSiteJob } from "../services/site.js";
|
|
10
18
|
import { checkOnServer, checkSchema, listSyncedSpaces, patchSyncedWizard, removeSyncedSpace, removeSyncedWizard, rotateSyncedLink, syncDataSchema, syncSettingsSchema, syncSpaceData, syncWizard, syncWizardSchema, } from "../services/spaces.js";
|
|
11
19
|
import { createWizard, ownedWizard, publishWizard, writeDraft } from "../services/wizards.js";
|
|
12
20
|
import { codeOf, tenantStatus } from "../tenants/control.js";
|
|
21
|
+
import { dispatchAs } from "./plugins.js";
|
|
13
22
|
const importSchema = z.object({
|
|
14
23
|
/** An earlier import's wizard: it is updated instead of a new one being made. */
|
|
15
24
|
wizardId: z.string().optional(),
|
|
@@ -24,6 +33,10 @@ const importSchema = z.object({
|
|
|
24
33
|
/** Runs a call in the caller's tenant, as the caller's role. */
|
|
25
34
|
const inTenantAs = (who, fn) => withTenant(who.tenantId, () => asRole(who.role, fn));
|
|
26
35
|
const HOUR = 60 * 60 * 1000;
|
|
36
|
+
/** Where the account of a managed runtime is looked after: plan, credits, keys. */
|
|
37
|
+
const accountUrl = () => env.manage.url || null;
|
|
38
|
+
/** Plugins of the plans above Free that a client offers when the tenant lacks them. */
|
|
39
|
+
const PRO_PLUGINS = ["contacts", "appointments"];
|
|
27
40
|
/** A wizard with its workspace: 25 MB of files as base64, and the definition. */
|
|
28
41
|
const WIZARD_BYTES = 48 * 1024 * 1024;
|
|
29
42
|
const tooLarge = bodyLimit({
|
|
@@ -54,12 +67,230 @@ function withinSyncLimit(c, who) {
|
|
|
54
67
|
? null
|
|
55
68
|
: c.json({ error: "Zu viele Übertragungen. Bitte später noch einmal.", code: "rate_limited" }, 429);
|
|
56
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* A plugin's routes for a client with a key or token (the WordPress plugin's Kontakte, Termine):
|
|
72
|
+
* the plugin's own `registerHttpRoute`s, as the key's person. Reading needs `wizards:read`,
|
|
73
|
+
* anything else `wizards:write`. A plugin the tenant's plan lacks answers `403 { code: "plugin" }`.
|
|
74
|
+
*/
|
|
75
|
+
async function pluginCall(c) {
|
|
76
|
+
const who = await caller(c, [c.req.method === "GET" ? "wizards:read" : "wizards:write"]);
|
|
77
|
+
if (who instanceof Response) {
|
|
78
|
+
return who;
|
|
79
|
+
}
|
|
80
|
+
const id = c.req.param("id") ?? "";
|
|
81
|
+
if (!(await pluginIdsOf(who.tenantId)).has(id)) {
|
|
82
|
+
return loadedPlugin(id) || PRO_PLUGINS.includes(id)
|
|
83
|
+
? c.json({
|
|
84
|
+
error: "Dieses Plugin gehört nicht zu deinem Paket. Mit Pro ist es dabei.",
|
|
85
|
+
code: "plugin",
|
|
86
|
+
plugin: id,
|
|
87
|
+
}, 403)
|
|
88
|
+
: c.json({ error: "not found", code: "not_found" }, 404);
|
|
89
|
+
}
|
|
90
|
+
return inTenantAs(who, () => dispatchAs(c, {
|
|
91
|
+
id: who.userId,
|
|
92
|
+
tenantId: who.tenantId,
|
|
93
|
+
role: who.role,
|
|
94
|
+
name: who.name ?? "",
|
|
95
|
+
email: who.email ?? "",
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
const runsQuery = z.object({
|
|
99
|
+
space: z.string().optional(),
|
|
100
|
+
wizard: z.string().optional(),
|
|
101
|
+
status: z.enum(["done", "open", "all"]).optional(),
|
|
102
|
+
q: z.string().max(200).optional(),
|
|
103
|
+
days: z.coerce.number().int().min(1).max(3650).optional(),
|
|
104
|
+
page: z.coerce.number().int().min(1).max(10_000).optional(),
|
|
105
|
+
});
|
|
57
106
|
/**
|
|
58
107
|
* The runtime's API for other programs. `/spaces/…` is how a local install's project comes
|
|
59
108
|
* here: the wizards it publishes, under the ids they have there (docs/manage-contract.md).
|
|
60
109
|
* `POST /wizards/import` takes a whole wizard as a new one of a tenant that builds here.
|
|
61
110
|
*/
|
|
62
111
|
export const apiRoutes = new Hono()
|
|
112
|
+
// Who the key or token acts for, and what the account has: plan, plugins, spaces, credits.
|
|
113
|
+
.get("/me", async (c) => {
|
|
114
|
+
const who = await caller(c, []);
|
|
115
|
+
if (who instanceof Response) {
|
|
116
|
+
return who;
|
|
117
|
+
}
|
|
118
|
+
const info = managed ? await tenantInfo(who.tenantId).catch(() => null) : null;
|
|
119
|
+
return inTenantAs(who, async () => {
|
|
120
|
+
const plugins = [...(await pluginIdsOf(who.tenantId))].sort();
|
|
121
|
+
const account = accountUrl();
|
|
122
|
+
return c.json({
|
|
123
|
+
user: { id: who.userId, name: who.name ?? "", email: who.email ?? "", role: who.role },
|
|
124
|
+
tenant: {
|
|
125
|
+
id: who.tenantId,
|
|
126
|
+
name: info?.name ?? "",
|
|
127
|
+
plan: info?.plan ?? null,
|
|
128
|
+
credits: await balanceCredits(),
|
|
129
|
+
},
|
|
130
|
+
/** Makes and changes wizards and spaces here; Free builds in the local app. */
|
|
131
|
+
build: await mayBuild(),
|
|
132
|
+
create: await mayCreate(),
|
|
133
|
+
plugins,
|
|
134
|
+
/** What a plan above this one would add. */
|
|
135
|
+
missing: PRO_PLUGINS.filter((id) => !plugins.includes(id)),
|
|
136
|
+
spaces: await apiSpaces(who.userId),
|
|
137
|
+
scopes: who.scopes,
|
|
138
|
+
client: who.client,
|
|
139
|
+
urls: {
|
|
140
|
+
app: env.appUrl,
|
|
141
|
+
embed: `${env.appUrl}/embed.js`,
|
|
142
|
+
studio: `${env.appUrl}/studio`,
|
|
143
|
+
newWizard: `${env.appUrl}/studio/new`,
|
|
144
|
+
space: `${env.appUrl}/studio/space`,
|
|
145
|
+
account: account,
|
|
146
|
+
billing: account ? `${account}/billing` : null,
|
|
147
|
+
upgrade: account ? `${account}/billing?upgrade=pro` : null,
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
})
|
|
152
|
+
// The wizards of the caller's spaces: published ones with their link and ID, drafts without.
|
|
153
|
+
.get("/wizards", async (c) => {
|
|
154
|
+
const who = await caller(c, ["wizards:read"]);
|
|
155
|
+
if (who instanceof Response) {
|
|
156
|
+
return who;
|
|
157
|
+
}
|
|
158
|
+
return inTenantAs(who, async () => c.json({ wizards: await apiWizards(who.userId, c.req.query("space")) }));
|
|
159
|
+
})
|
|
160
|
+
// The published wizard as a `.wizard` package: what a WordPress site runs on its own; with
|
|
161
|
+
// `knowledge=1` the project's pages come along as files (up to the snapshot's cap).
|
|
162
|
+
.get("/wizards/:id/package", async (c) => {
|
|
163
|
+
const who = await caller(c, ["wizards:read"]);
|
|
164
|
+
if (who instanceof Response) {
|
|
165
|
+
return who;
|
|
166
|
+
}
|
|
167
|
+
return inTenantAs(who, async () => {
|
|
168
|
+
const found = await exportWizard(who.userId, c.req.param("id"), {
|
|
169
|
+
published: true,
|
|
170
|
+
knowledge: c.req.query("knowledge") === "1",
|
|
171
|
+
});
|
|
172
|
+
if (!found) {
|
|
173
|
+
return c.json({ error: "Der Wizard ist noch nicht veröffentlicht.", code: "not_found" }, 404);
|
|
174
|
+
}
|
|
175
|
+
return new Response(new Uint8Array(found.zip), {
|
|
176
|
+
headers: {
|
|
177
|
+
"content-type": PACKAGE_MIME,
|
|
178
|
+
"content-disposition": `attachment; filename="${found.name}"`,
|
|
179
|
+
"cache-control": "no-store",
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
})
|
|
184
|
+
// A new wizard from a template, published at once: what a website starts with.
|
|
185
|
+
.post("/wizards", async (c) => {
|
|
186
|
+
const who = await caller(c, ["wizards:write", "wizards:publish"]);
|
|
187
|
+
if (who instanceof Response) {
|
|
188
|
+
return who;
|
|
189
|
+
}
|
|
190
|
+
const body = z
|
|
191
|
+
.object({
|
|
192
|
+
starterId: z.string().min(1).max(200),
|
|
193
|
+
spaceId: z.string().optional(),
|
|
194
|
+
lang: z.string().max(10).optional(),
|
|
195
|
+
publish: z.boolean().default(true),
|
|
196
|
+
})
|
|
197
|
+
.parse(await c.req.json());
|
|
198
|
+
return inTenantAs(who, async () => {
|
|
199
|
+
const writer = { source: "mcp", client: who.client };
|
|
200
|
+
const created = await createWizard(who.userId, { projectId: body.spaceId, starterId: body.starterId, lang: body.lang }, writer);
|
|
201
|
+
let published = false;
|
|
202
|
+
if (body.publish && !created.issues.length) {
|
|
203
|
+
published = await publishWizard(who.userId, created.id).then(() => true, () => false);
|
|
204
|
+
}
|
|
205
|
+
const wizard = (await apiWizards(who.userId)).find((w) => w.id === created.id);
|
|
206
|
+
return c.json({ wizard, published, issues: created.issues });
|
|
207
|
+
});
|
|
208
|
+
})
|
|
209
|
+
// What a new wizard can start from: the plugins' starters first, then the marketplace's.
|
|
210
|
+
.get("/templates", async (c) => {
|
|
211
|
+
const who = await caller(c, ["wizards:read"]);
|
|
212
|
+
if (who instanceof Response) {
|
|
213
|
+
return who;
|
|
214
|
+
}
|
|
215
|
+
const q = c.req.query("q")?.trim() ?? "";
|
|
216
|
+
return inTenantAs(who, async () => {
|
|
217
|
+
const { entries } = await searchMarketplace({
|
|
218
|
+
q,
|
|
219
|
+
lang: asLang(c.req.query("lang")),
|
|
220
|
+
limit: 40,
|
|
221
|
+
});
|
|
222
|
+
return c.json({
|
|
223
|
+
templates: entries
|
|
224
|
+
.filter((e) => e.usable && (q || e.starter))
|
|
225
|
+
.map((e) => ({
|
|
226
|
+
id: e.id,
|
|
227
|
+
title: e.title,
|
|
228
|
+
pitch: e.pitch,
|
|
229
|
+
avatar: e.avatar,
|
|
230
|
+
language: e.language,
|
|
231
|
+
useCases: e.useCases,
|
|
232
|
+
formats: e.formats,
|
|
233
|
+
plan: e.plan ?? "free",
|
|
234
|
+
plugin: e.plugin ?? null,
|
|
235
|
+
/** Plugins the tenant lacks: a wizard made from it would not run. */
|
|
236
|
+
needs: e.needs ?? [],
|
|
237
|
+
credits: e.credits,
|
|
238
|
+
})),
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
})
|
|
242
|
+
// What visitors left: the live runs of the caller's wizards, by pages of 50.
|
|
243
|
+
.get("/runs", async (c) => {
|
|
244
|
+
const who = await caller(c, ["runs:read"]);
|
|
245
|
+
if (who instanceof Response) {
|
|
246
|
+
return who;
|
|
247
|
+
}
|
|
248
|
+
const query = runsQuery.parse(c.req.query());
|
|
249
|
+
return inTenantAs(who, async () => c.json(await apiRuns(who.userId, {
|
|
250
|
+
spaceId: query.space,
|
|
251
|
+
wizardId: query.wizard,
|
|
252
|
+
status: query.status,
|
|
253
|
+
q: query.q,
|
|
254
|
+
days: query.days,
|
|
255
|
+
page: query.page,
|
|
256
|
+
})));
|
|
257
|
+
})
|
|
258
|
+
.get("/runs/:id", async (c) => {
|
|
259
|
+
const who = await caller(c, ["runs:read"]);
|
|
260
|
+
if (who instanceof Response) {
|
|
261
|
+
return who;
|
|
262
|
+
}
|
|
263
|
+
return inTenantAs(who, async () => c.json(await apiRun(who.userId, c.req.param("id"))));
|
|
264
|
+
})
|
|
265
|
+
// The space's own details: title, about, colours, facts, logo, how much Wissen it holds.
|
|
266
|
+
.get("/space", async (c) => {
|
|
267
|
+
const who = await caller(c, ["wizards:read"]);
|
|
268
|
+
if (who instanceof Response) {
|
|
269
|
+
return who;
|
|
270
|
+
}
|
|
271
|
+
return inTenantAs(who, async () => c.json(await apiSpaceDetails(who.userId, c.req.query("space"))));
|
|
272
|
+
})
|
|
273
|
+
// A website reads itself into the space (services/site.ts); answers with the job to ask after.
|
|
274
|
+
.post("/space/site", tooLarge, async (c) => {
|
|
275
|
+
const who = await caller(c, ["wizards:write"]);
|
|
276
|
+
if (who instanceof Response) {
|
|
277
|
+
return who;
|
|
278
|
+
}
|
|
279
|
+
if (!allowed(`site:${who.tenantId}`, 20, HOUR)) {
|
|
280
|
+
return c.json({ error: "Zu oft hintereinander. Bitte später noch einmal.", code: "rate_limited" }, 429);
|
|
281
|
+
}
|
|
282
|
+
const body = siteSchema.parse(await c.req.json());
|
|
283
|
+
return inTenantAs(who, async () => c.json(await startSiteJob(who, body)));
|
|
284
|
+
})
|
|
285
|
+
.get("/space/site/:job", async (c) => {
|
|
286
|
+
const who = await caller(c, ["wizards:read"]);
|
|
287
|
+
if (who instanceof Response) {
|
|
288
|
+
return who;
|
|
289
|
+
}
|
|
290
|
+
return inTenantAs(who, () => c.json(siteJob(c.req.param("job"))));
|
|
291
|
+
})
|
|
292
|
+
.all("/plugins/:id", pluginCall)
|
|
293
|
+
.all("/plugins/:id/*", pluginCall)
|
|
63
294
|
.get("/spaces", async (c) => {
|
|
64
295
|
const who = await caller(c, ["wizards:read"]);
|
|
65
296
|
if (who instanceof Response) {
|
|
@@ -41,8 +41,11 @@ export function answerOf(err) {
|
|
|
41
41
|
? { status: e.status, body: { error: String(e.message ?? e.code), code: e.code } }
|
|
42
42
|
: null;
|
|
43
43
|
}
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Calls the plugin route the request names, as `user`: a signed-in person of the studio, or the
|
|
46
|
+
* person behind an API key or token (`/api/v1/plugins/…`).
|
|
47
|
+
*/
|
|
48
|
+
export async function dispatchAs(c, user) {
|
|
46
49
|
const id = c.req.param("id") ?? "";
|
|
47
50
|
const plugin = (await pluginIdsOf(user.tenantId)).has(id) ? loadedPlugin(id) : undefined;
|
|
48
51
|
if (!plugin) {
|
|
@@ -90,6 +93,7 @@ async function dispatch(c) {
|
|
|
90
93
|
}
|
|
91
94
|
return c.json({ error: "not found", code: "not_found" }, 404);
|
|
92
95
|
}
|
|
96
|
+
const dispatch = (c) => dispatchAs(c, c.get("user"));
|
|
93
97
|
const TYPES = {
|
|
94
98
|
"client.js": { kind: "studio", type: "text/javascript; charset=utf-8" },
|
|
95
99
|
"client.css": { kind: "styles", type: "text/css; charset=utf-8" },
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FORMATS, LIST_FORMATS } from "@engenty-wizards/shared/definition";
|
|
1
|
+
import { FORMATS, LIST_FORMATS, TEXT_CLASSES } from "@engenty-wizards/shared/definition";
|
|
2
2
|
import { TableColumnValueError } from "@engenty-wizards/shared/engenty/data-tables";
|
|
3
3
|
import { getConnInfo } from "@hono/node-server/conninfo";
|
|
4
4
|
import { and, eq } from "drizzle-orm";
|
|
@@ -13,6 +13,7 @@ import { ConnectError, connectionViews, connectWithCredentials, disconnect, fini
|
|
|
13
13
|
import { finishPluginOAuth, pluginStateTenant } from "../connectors/plugin-connections.js";
|
|
14
14
|
import { db, schema, withTenant } from "../db/client.js";
|
|
15
15
|
import { answerAsk, pendingAsk } from "../engine/asks.js";
|
|
16
|
+
import { answerDevice, offerDevice } from "../engine/device.js";
|
|
16
17
|
import { emitEvent, signalChanged, subscribe } from "../engine/events.js";
|
|
17
18
|
import { pushAvailable, setPushDevice } from "../engine/push.js";
|
|
18
19
|
import { liveResources } from "../engine/resources.js";
|
|
@@ -57,6 +58,23 @@ export function clientIp(c) {
|
|
|
57
58
|
* as good as the cookie.
|
|
58
59
|
*/
|
|
59
60
|
const VISITOR_HEADER = "x-wizards-visitor";
|
|
61
|
+
/** An http(s) address without credentials or fragment, or null for anything else. */
|
|
62
|
+
function webAddress(raw) {
|
|
63
|
+
if (!raw) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const url = new URL(raw);
|
|
68
|
+
if (!(url.protocol === "http:" || url.protocol === "https:") || url.username || url.password) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
url.hash = "";
|
|
72
|
+
return url.toString();
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
60
78
|
function visitorId(c, create, embedded = false) {
|
|
61
79
|
let vid = c.req.header(VISITOR_HEADER) || getCookie(c, VISITOR_COOKIE) || null;
|
|
62
80
|
if (!vid && create) {
|
|
@@ -226,7 +244,12 @@ export const publicRoutes = new Hono()
|
|
|
226
244
|
return c.json({ error: reason }, 403);
|
|
227
245
|
}
|
|
228
246
|
const body = z
|
|
229
|
-
.object({
|
|
247
|
+
.object({
|
|
248
|
+
turnstileToken: z.string().optional(),
|
|
249
|
+
embedded: z.boolean().optional(),
|
|
250
|
+
// The website page that embeds the wizard: shown with the result.
|
|
251
|
+
origin: z.string().max(2000).optional(),
|
|
252
|
+
})
|
|
230
253
|
.parse(await c.req.json().catch(() => ({})));
|
|
231
254
|
const ip = clientIp(c);
|
|
232
255
|
if (!(await verifyTurnstile(body.turnstileToken, ip))) {
|
|
@@ -251,6 +274,7 @@ export const publicRoutes = new Hono()
|
|
|
251
274
|
mode: "live",
|
|
252
275
|
visitorId: vid,
|
|
253
276
|
ipHash,
|
|
277
|
+
origin: body.embedded ? webAddress(body.origin) : null,
|
|
254
278
|
});
|
|
255
279
|
return c.json({ runId });
|
|
256
280
|
}
|
|
@@ -367,7 +391,14 @@ export const runRoutes = new Hono()
|
|
|
367
391
|
await stream.writeSSE({ event: "view", data: JSON.stringify(await viewOf(run)) });
|
|
368
392
|
}
|
|
369
393
|
};
|
|
370
|
-
const unsubscribe = subscribe(id, () => {
|
|
394
|
+
const unsubscribe = subscribe(id, (signal) => {
|
|
395
|
+
if (signal.device) {
|
|
396
|
+
// The runtime asks the phone around the runner to think: not a change of the run.
|
|
397
|
+
if (!closed) {
|
|
398
|
+
void stream.writeSSE({ event: "device", data: JSON.stringify(signal.device) });
|
|
399
|
+
}
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
371
402
|
void push();
|
|
372
403
|
});
|
|
373
404
|
stream.onAbort(() => {
|
|
@@ -382,6 +413,37 @@ export const runRoutes = new Hono()
|
|
|
382
413
|
}
|
|
383
414
|
}
|
|
384
415
|
});
|
|
416
|
+
})
|
|
417
|
+
// The runner says what the device around it offers the run (the app's Apple Intelligence):
|
|
418
|
+
// from then on short text calls go there first. Renewed on every connect; see engine/device.ts.
|
|
419
|
+
.post("/:id/device", async (c) => {
|
|
420
|
+
const run = await accessibleRun(c, c.req.param("id"));
|
|
421
|
+
if (!run) {
|
|
422
|
+
return c.json({ error: "not found" }, 404);
|
|
423
|
+
}
|
|
424
|
+
const offer = z
|
|
425
|
+
.object({
|
|
426
|
+
think: z
|
|
427
|
+
.object({
|
|
428
|
+
classes: z.array(z.enum(TEXT_CLASSES)).max(TEXT_CLASSES.length),
|
|
429
|
+
maxChars: z.number().int().min(500).max(50_000),
|
|
430
|
+
})
|
|
431
|
+
.optional(),
|
|
432
|
+
})
|
|
433
|
+
.parse(await c.req.json().catch(() => ({})));
|
|
434
|
+
offerDevice(run.id, offer);
|
|
435
|
+
return c.json({ ok: true });
|
|
436
|
+
})
|
|
437
|
+
// The device's answer to one call the stream carried as a `device` event.
|
|
438
|
+
.post("/:id/device/:reqId", async (c) => {
|
|
439
|
+
const run = await accessibleRun(c, c.req.param("id"));
|
|
440
|
+
if (!run) {
|
|
441
|
+
return c.json({ error: "not found" }, 404);
|
|
442
|
+
}
|
|
443
|
+
const answer = z
|
|
444
|
+
.object({ text: z.string().max(200_000).optional(), error: z.string().max(500).optional() })
|
|
445
|
+
.parse(await c.req.json().catch(() => ({})));
|
|
446
|
+
return c.json({ ok: answerDevice(run.id, c.req.param("reqId"), answer) });
|
|
385
447
|
})
|
|
386
448
|
.post("/:id/pages/:stepId", async (c) => {
|
|
387
449
|
const run = await accessibleRun(c, c.req.param("id"));
|
|
@@ -23,6 +23,7 @@ import { estimateRun } from "../credits/estimate.js";
|
|
|
23
23
|
import { missingModels, stepsWithoutModel } from "../engine/requirements.js";
|
|
24
24
|
import { env } from "../env.js";
|
|
25
25
|
import { feature, features } from "../features.js";
|
|
26
|
+
import { appleStatus } from "../harness/apple.js";
|
|
26
27
|
import { freshAuth, loginEnv } from "../harness/env.js";
|
|
27
28
|
import { detectHarness, detectHarnesses, HARNESS_IDS, harness, installSpec, } from "../harness/index.js";
|
|
28
29
|
import { closeTerminal, openTerminal, resizeTerminal, subscribeTerminal, writeTerminal, } from "../harness/terminal.js";
|
|
@@ -227,6 +228,8 @@ export const studio = new Hono()
|
|
|
227
228
|
}
|
|
228
229
|
: null,
|
|
229
230
|
models: managed ? null : localModelSettings(),
|
|
231
|
+
/** Apple Intelligence on this Mac: offered where the machine can run it (harness/apple.ts). */
|
|
232
|
+
apple: managed ? null : await appleStatus(),
|
|
230
233
|
/** The AI clients this runtime can think with — installed or not, and how each is signed in. */
|
|
231
234
|
harnesses: managed ? [] : await detectHarnesses(),
|
|
232
235
|
/** Installed AI clients whose subscription can answer the studio chat. */
|
|
@@ -802,7 +805,14 @@ export const studio = new Hono()
|
|
|
802
805
|
return c.json(await importWizard(c.get("user").id, c.req.param("id"), new Uint8Array(await file.arrayBuffer()), file.name));
|
|
803
806
|
})
|
|
804
807
|
.get("/wizards/:id/export", async (c) => {
|
|
805
|
-
const
|
|
808
|
+
const found = await exportWizard(c.get("user").id, c.req.param("id"), {
|
|
809
|
+
published: c.req.query("published") === "1",
|
|
810
|
+
knowledge: c.req.query("knowledge") === "1",
|
|
811
|
+
});
|
|
812
|
+
if (!found) {
|
|
813
|
+
return c.json({ error: "Der Wizard ist noch nicht veröffentlicht." }, 404);
|
|
814
|
+
}
|
|
815
|
+
const { name, zip } = found;
|
|
806
816
|
return c.body(new Uint8Array(zip), 200, {
|
|
807
817
|
"content-type": PACKAGE_MIME,
|
|
808
818
|
"content-disposition": `attachment; filename="${name}"`,
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { standaloneEntry } from "@engenty-wizards/shared/fit";
|
|
2
|
+
import { pluginsOf } from "@engenty-wizards/shared/marketplace";
|
|
3
|
+
import { and, count, desc, eq, gte, inArray, isNotNull, or, sql } from "drizzle-orm";
|
|
4
|
+
import { MICROS_PER_CREDIT } from "../credits/credits.js";
|
|
5
|
+
import { db, schema } from "../db/client.js";
|
|
6
|
+
import { env } from "../env.js";
|
|
7
|
+
import { wizardUnavailable } from "../limits.js";
|
|
8
|
+
import { codeOf } from "../tenants/control.js";
|
|
9
|
+
import { currentTenant } from "../tenants/tenant.js";
|
|
10
|
+
import { mayBuild, projectWritable, READ_ONLY_TENANT } from "./access.js";
|
|
11
|
+
import { mainLogoId } from "./brand.js";
|
|
12
|
+
import { notFound, ServiceError } from "./errors.js";
|
|
13
|
+
import { knowledgeCounts } from "./knowledge.js";
|
|
14
|
+
import { listProjects, ownedProject } from "./projects.js";
|
|
15
|
+
import { runReport } from "./runs.js";
|
|
16
|
+
import { shareUrlFor } from "./shares.js";
|
|
17
|
+
import { shareUrl, studioUrl } from "./wizards.js";
|
|
18
|
+
/** The spaces the caller works with, as the studio lists them, and whether each may change here. */
|
|
19
|
+
export async function apiSpaces(userId) {
|
|
20
|
+
const projects = await listProjects(userId);
|
|
21
|
+
return Promise.all(projects.map(async (p) => ({
|
|
22
|
+
id: p.id,
|
|
23
|
+
name: p.name,
|
|
24
|
+
title: p.brand.name ?? "",
|
|
25
|
+
wizards: p.wizardCount,
|
|
26
|
+
writable: await projectWritable(p),
|
|
27
|
+
})));
|
|
28
|
+
}
|
|
29
|
+
/** The space a call names, or the one a caller means when it names none: the first one listed. */
|
|
30
|
+
export async function apiSpace(userId, spaceId) {
|
|
31
|
+
if (spaceId) {
|
|
32
|
+
return ownedProject(userId, spaceId);
|
|
33
|
+
}
|
|
34
|
+
const [first] = await listProjects(userId);
|
|
35
|
+
if (!first) {
|
|
36
|
+
// A plan that builds nothing here has a space only once its local install synced one.
|
|
37
|
+
if (!(await mayBuild())) {
|
|
38
|
+
throw new ServiceError("read_only", READ_ONLY_TENANT);
|
|
39
|
+
}
|
|
40
|
+
throw notFound();
|
|
41
|
+
}
|
|
42
|
+
return first;
|
|
43
|
+
}
|
|
44
|
+
/** The space's own details: what it says about itself, and what its wizards look things up in. */
|
|
45
|
+
export async function apiSpaceDetails(userId, spaceId) {
|
|
46
|
+
const p = await apiSpace(userId, spaceId);
|
|
47
|
+
const logo = await mainLogoId(p.id);
|
|
48
|
+
return {
|
|
49
|
+
id: p.id,
|
|
50
|
+
name: p.name,
|
|
51
|
+
title: p.brand.name ?? "",
|
|
52
|
+
about: p.brand.about ?? "",
|
|
53
|
+
colors: p.brand.colors ?? [],
|
|
54
|
+
facts: p.facts.map(({ label, value }) => ({ label, value })),
|
|
55
|
+
logoUrl: logo ? `${env.appUrl}/api/public/logos/${logo}` : null,
|
|
56
|
+
knowledge: await knowledgeCounts(p.id),
|
|
57
|
+
writable: await projectWritable(p),
|
|
58
|
+
studioUrl: `${env.appUrl}/studio/space`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The wizards of the caller's spaces, newest change first. A published one carries what a website
|
|
63
|
+
* embeds it with: its link's token and the ID the mobile app takes.
|
|
64
|
+
*/
|
|
65
|
+
export async function apiWizards(userId, spaceId) {
|
|
66
|
+
const spaces = (await listProjects(userId)).filter((p) => !spaceId || p.id === spaceId);
|
|
67
|
+
if (!spaces.length) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
const rows = await db.query.wizard.findMany({
|
|
71
|
+
where: and(eq(schema.wizard.tenantId, currentTenant()), inArray(schema.wizard.projectId, spaces.map((p) => p.id))),
|
|
72
|
+
orderBy: [desc(schema.wizard.updatedAt)],
|
|
73
|
+
});
|
|
74
|
+
const published = rows.filter((w) => w.publishedVersion !== null);
|
|
75
|
+
const versions = published.length
|
|
76
|
+
? await db.query.wizardVersion.findMany({
|
|
77
|
+
where: or(...published.map((w) => and(eq(schema.wizardVersion.wizardId, w.id), eq(schema.wizardVersion.version, w.publishedVersion)))),
|
|
78
|
+
columns: { wizardId: true, definition: true },
|
|
79
|
+
})
|
|
80
|
+
: [];
|
|
81
|
+
return Promise.all(rows.map(async (w) => {
|
|
82
|
+
const live = versions.find((v) => v.wizardId === w.id)?.definition;
|
|
83
|
+
const def = live ?? w.draft;
|
|
84
|
+
const reason = live ? await wizardUnavailable(w) : null;
|
|
85
|
+
return {
|
|
86
|
+
id: w.id,
|
|
87
|
+
spaceId: w.projectId,
|
|
88
|
+
title: def.title,
|
|
89
|
+
description: def.description ?? "",
|
|
90
|
+
avatar: def.avatar ?? null,
|
|
91
|
+
published: Boolean(live),
|
|
92
|
+
publishedVersion: w.publishedVersion,
|
|
93
|
+
token: live ? w.shareToken : null,
|
|
94
|
+
code: live ? await codeOf(w.shareToken) : null,
|
|
95
|
+
shareUrl: live ? shareUrl(w.shareToken) : null,
|
|
96
|
+
shareEnabled: w.shareEnabled,
|
|
97
|
+
iconUrl: live
|
|
98
|
+
? `${env.appUrl}/api/public/wizards/${w.shareToken}/icons/icon-192.png`
|
|
99
|
+
: null,
|
|
100
|
+
available: Boolean(live) && !reason,
|
|
101
|
+
unavailableReason: reason,
|
|
102
|
+
dailyRunLimit: w.dailyRunLimit,
|
|
103
|
+
/** The plugins its steps call: a wizard that books needs `appointments`. */
|
|
104
|
+
plugins: pluginsOf(def),
|
|
105
|
+
/** Whether it runs on a website by itself, in the gallery's words (packages/shared/src/fit.ts). */
|
|
106
|
+
standalone: standaloneEntry(def),
|
|
107
|
+
studioUrl: studioUrl(w.id),
|
|
108
|
+
updatedAt: w.updatedAt.toISOString(),
|
|
109
|
+
};
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
/** A value as one line of text: what the person typed or picked. */
|
|
113
|
+
function answerText(field, value) {
|
|
114
|
+
if (value === undefined || value === null || value === "") {
|
|
115
|
+
return "";
|
|
116
|
+
}
|
|
117
|
+
if (typeof value === "boolean") {
|
|
118
|
+
return value ? "Ja" : "Nein";
|
|
119
|
+
}
|
|
120
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
121
|
+
return String(value);
|
|
122
|
+
}
|
|
123
|
+
if (Array.isArray(value)) {
|
|
124
|
+
if (["file", "image", "audio", "signature"].includes(field.kind)) {
|
|
125
|
+
return value.length === 1 ? "1 Datei" : `${value.length} Dateien`;
|
|
126
|
+
}
|
|
127
|
+
return value
|
|
128
|
+
.map((v) => (typeof v === "object" && v ? Object.values(v).join(" ") : String(v)))
|
|
129
|
+
.join(", ");
|
|
130
|
+
}
|
|
131
|
+
if (typeof value === "object") {
|
|
132
|
+
const v = value;
|
|
133
|
+
const said = v.label ?? v.address ?? v.text ?? v.name ?? v.start;
|
|
134
|
+
return typeof said === "string" ? said : JSON.stringify(value).slice(0, 200);
|
|
135
|
+
}
|
|
136
|
+
return "";
|
|
137
|
+
}
|
|
138
|
+
/** What the person answered on the run's pages, by the fields' labels, in the order asked. */
|
|
139
|
+
function answersOf(run) {
|
|
140
|
+
const answers = [];
|
|
141
|
+
for (const step of run.definition.steps) {
|
|
142
|
+
if (step.type !== "page") {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
for (const field of step.fields) {
|
|
146
|
+
const value = answerText(field, run.state.values[field.id]);
|
|
147
|
+
if (value) {
|
|
148
|
+
answers.push({ field: field.id, label: field.label, kind: field.kind, value });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return answers;
|
|
153
|
+
}
|
|
154
|
+
/** The person's address where a page asked for one: the first e-mail field answered. */
|
|
155
|
+
function emailOf(answers) {
|
|
156
|
+
return (answers.find((a) => a.kind === "email")?.value ??
|
|
157
|
+
answers.find((a) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(a.value))?.value ??
|
|
158
|
+
null);
|
|
159
|
+
}
|
|
160
|
+
const PAGE = 50;
|
|
161
|
+
/** The live runs of the caller's wizards, newest first, by pages of 50: what visitors left. */
|
|
162
|
+
export async function apiRuns(userId, filter = {}) {
|
|
163
|
+
const wizards = await apiWizards(userId, filter.spaceId);
|
|
164
|
+
const ids = wizards.map((w) => w.id).filter((id) => !filter.wizardId || id === filter.wizardId);
|
|
165
|
+
const page = Math.max(1, filter.page ?? 1);
|
|
166
|
+
if (!ids.length) {
|
|
167
|
+
return { runs: [], page, pages: 0, total: 0 };
|
|
168
|
+
}
|
|
169
|
+
const q = filter.q?.trim().toLowerCase();
|
|
170
|
+
const like = q ? `%${q.replace(/[\\%_]/g, (c) => `\\${c}`)}%` : null;
|
|
171
|
+
const status = filter.status ?? "done";
|
|
172
|
+
const where = and(inArray(schema.run.wizardId, ids), eq(schema.run.mode, "live"), status === "done"
|
|
173
|
+
? eq(schema.run.status, "done")
|
|
174
|
+
: status === "open"
|
|
175
|
+
? inArray(schema.run.status, ["waiting_input", "running"])
|
|
176
|
+
: undefined, filter.days
|
|
177
|
+
? gte(schema.run.createdAt, new Date(Date.now() - filter.days * 86_400_000))
|
|
178
|
+
: undefined, like
|
|
179
|
+
? or(sql `lower(${schema.run.state}) like ${like} escape '\\'`, sql `lower(coalesce(${schema.run.origin}, '')) like ${like} escape '\\'`)
|
|
180
|
+
: undefined);
|
|
181
|
+
const [{ total }] = await db.select({ total: count() }).from(schema.run).where(where);
|
|
182
|
+
const rows = await db.query.run.findMany({
|
|
183
|
+
where,
|
|
184
|
+
orderBy: [desc(schema.run.createdAt)],
|
|
185
|
+
limit: PAGE,
|
|
186
|
+
offset: (page - 1) * PAGE,
|
|
187
|
+
});
|
|
188
|
+
const assets = rows.length
|
|
189
|
+
? await db
|
|
190
|
+
.select({ runId: schema.asset.runId, n: count() })
|
|
191
|
+
.from(schema.asset)
|
|
192
|
+
.where(and(isNotNull(schema.asset.runId), inArray(schema.asset.runId, rows.map((r) => r.id))))
|
|
193
|
+
.groupBy(schema.asset.runId)
|
|
194
|
+
: [];
|
|
195
|
+
return {
|
|
196
|
+
runs: rows.map((r) => {
|
|
197
|
+
const answers = answersOf(r);
|
|
198
|
+
return {
|
|
199
|
+
id: r.id,
|
|
200
|
+
wizardId: r.wizardId,
|
|
201
|
+
wizard: wizards.find((w) => w.id === r.wizardId)?.title ?? r.definition.title,
|
|
202
|
+
status: r.status,
|
|
203
|
+
step: r.definition.steps.find((s) => s.id === r.cursor)?.title ?? null,
|
|
204
|
+
createdAt: r.createdAt.toISOString(),
|
|
205
|
+
updatedAt: r.updatedAt.toISOString(),
|
|
206
|
+
origin: r.origin,
|
|
207
|
+
email: emailOf(answers),
|
|
208
|
+
/** The first answers, for a line in a list. */
|
|
209
|
+
summary: answers
|
|
210
|
+
.slice(0, 3)
|
|
211
|
+
.map((a) => a.value)
|
|
212
|
+
.join(" · ")
|
|
213
|
+
.slice(0, 200),
|
|
214
|
+
credits: Math.ceil(r.costMicros / MICROS_PER_CREDIT),
|
|
215
|
+
files: assets.find((a) => a.runId === r.id)?.n ?? 0,
|
|
216
|
+
};
|
|
217
|
+
}),
|
|
218
|
+
page,
|
|
219
|
+
pages: Math.ceil(total / PAGE),
|
|
220
|
+
total,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/** One run with everything a reader of the result needs: the answers, the outputs, the downloads. */
|
|
224
|
+
export async function apiRun(userId, runId) {
|
|
225
|
+
// Refuses what is no live run of the tenant.
|
|
226
|
+
const report = await runReport(userId, runId, 0, "live");
|
|
227
|
+
const run = await db.query.run.findFirst({
|
|
228
|
+
where: and(eq(schema.run.id, runId), eq(schema.run.tenantId, currentTenant())),
|
|
229
|
+
});
|
|
230
|
+
if (!run) {
|
|
231
|
+
throw notFound();
|
|
232
|
+
}
|
|
233
|
+
const answers = answersOf(run);
|
|
234
|
+
return {
|
|
235
|
+
...report,
|
|
236
|
+
wizard: run.definition.title,
|
|
237
|
+
createdAt: run.createdAt.toISOString(),
|
|
238
|
+
updatedAt: run.updatedAt.toISOString(),
|
|
239
|
+
origin: run.origin,
|
|
240
|
+
email: emailOf(answers),
|
|
241
|
+
answers: answers.map(({ label, kind, value }) => ({ label, kind, value })),
|
|
242
|
+
shareUrl: run.shareToken ? shareUrlFor(run.shareToken) : null,
|
|
243
|
+
};
|
|
244
|
+
}
|