@lovable.dev/workflows-sdk 0.0.0-dev.24 → 0.0.0-dev.25
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/dist/index.d.ts +5 -0
- package/dist/index.js +133 -11
- package/dist/vite.js +0 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -281,10 +281,15 @@ interface StepReference {
|
|
|
281
281
|
occurrence: number;
|
|
282
282
|
}
|
|
283
283
|
//#endregion
|
|
284
|
+
//#region src/stacks/tanstack/project-auth.d.ts
|
|
285
|
+
type ProjectSecretResolver = () => string | undefined | Promise<string | undefined>;
|
|
286
|
+
//#endregion
|
|
284
287
|
//#region src/stacks/tanstack/steps.d.ts
|
|
285
288
|
interface StepsHandlerOptions {
|
|
286
289
|
/** Route parameter carrying the workflow ID. @default "workflowId" */
|
|
287
290
|
workflowIdParam?: string;
|
|
291
|
+
/** @default Resolves `LOVABLE_API_KEY` from the server runtime. */
|
|
292
|
+
getProjectSecret?: ProjectSecretResolver;
|
|
288
293
|
}
|
|
289
294
|
interface RouteCtx {
|
|
290
295
|
request: Request;
|
package/dist/index.js
CHANGED
|
@@ -399,6 +399,57 @@ async function replay(options) {
|
|
|
399
399
|
}
|
|
400
400
|
function noop() {}
|
|
401
401
|
//#endregion
|
|
402
|
+
//#region src/stacks/tanstack/project-auth.ts
|
|
403
|
+
const PROJECT_SECRET_ENV_VAR = "LOVABLE_API_KEY";
|
|
404
|
+
let cloudflareEnvPromise;
|
|
405
|
+
function readProcessSecret() {
|
|
406
|
+
const value = (globalThis.process?.env)?.[PROJECT_SECRET_ENV_VAR];
|
|
407
|
+
return typeof value === "string" && value ? value : void 0;
|
|
408
|
+
}
|
|
409
|
+
async function readCloudflareSecret() {
|
|
410
|
+
cloudflareEnvPromise ??= import(
|
|
411
|
+
/* @vite-ignore */
|
|
412
|
+
"cloudflare:workers"
|
|
413
|
+
).then((module) => module.env).catch(() => void 0);
|
|
414
|
+
const value = (await cloudflareEnvPromise)?.[PROJECT_SECRET_ENV_VAR];
|
|
415
|
+
return typeof value === "string" && value ? value : void 0;
|
|
416
|
+
}
|
|
417
|
+
async function resolveProjectSecret() {
|
|
418
|
+
return readProcessSecret() ?? await readCloudflareSecret();
|
|
419
|
+
}
|
|
420
|
+
function bearerToken(request) {
|
|
421
|
+
const header = request.headers.get("authorization");
|
|
422
|
+
if (!header) return void 0;
|
|
423
|
+
const parts = header.trim().split(/\s+/);
|
|
424
|
+
if (parts.length !== 2 || parts[0].toLowerCase() !== "bearer") return void 0;
|
|
425
|
+
return parts[1] || void 0;
|
|
426
|
+
}
|
|
427
|
+
async function equalSecrets(left, right) {
|
|
428
|
+
const encoder = new TextEncoder();
|
|
429
|
+
const [leftHash, rightHash] = await Promise.all([crypto.subtle.digest("SHA-256", encoder.encode(left)), crypto.subtle.digest("SHA-256", encoder.encode(right))]);
|
|
430
|
+
const leftBytes = new Uint8Array(leftHash);
|
|
431
|
+
const rightBytes = new Uint8Array(rightHash);
|
|
432
|
+
let difference = 0;
|
|
433
|
+
for (let index = 0; index < leftBytes.length; index += 1) difference |= leftBytes[index] ^ rightBytes[index];
|
|
434
|
+
return difference === 0;
|
|
435
|
+
}
|
|
436
|
+
async function authorizeProjectRequest(request, getProjectSecret) {
|
|
437
|
+
let projectSecret;
|
|
438
|
+
try {
|
|
439
|
+
projectSecret = await getProjectSecret();
|
|
440
|
+
} catch {
|
|
441
|
+
return "unavailable";
|
|
442
|
+
}
|
|
443
|
+
if (!projectSecret) return "unavailable";
|
|
444
|
+
const token = bearerToken(request);
|
|
445
|
+
if (!token) return "unauthorized";
|
|
446
|
+
try {
|
|
447
|
+
return await equalSecrets(token, projectSecret) ? "authorized" : "unauthorized";
|
|
448
|
+
} catch {
|
|
449
|
+
return "unavailable";
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
//#endregion
|
|
402
453
|
//#region src/stacks/tanstack/steps.ts
|
|
403
454
|
function json(status, body) {
|
|
404
455
|
return new Response(JSON.stringify(body), {
|
|
@@ -406,6 +457,17 @@ function json(status, body) {
|
|
|
406
457
|
headers: { "content-type": "application/json; charset=utf-8" }
|
|
407
458
|
});
|
|
408
459
|
}
|
|
460
|
+
function unauthorized() {
|
|
461
|
+
const response = json(401, { error: "unauthorized" });
|
|
462
|
+
response.headers.set("www-authenticate", "Bearer");
|
|
463
|
+
return response;
|
|
464
|
+
}
|
|
465
|
+
function authUnavailable() {
|
|
466
|
+
return json(503, {
|
|
467
|
+
error: "workflow_auth_unavailable",
|
|
468
|
+
message: "LOVABLE_API_KEY is not configured in this app runtime"
|
|
469
|
+
});
|
|
470
|
+
}
|
|
409
471
|
function resolveWorkflowId(ctx, param) {
|
|
410
472
|
const fromParams = ctx.params?.[param];
|
|
411
473
|
if (fromParams) return fromParams;
|
|
@@ -467,7 +529,60 @@ function respond(runId, inputHash, before, outcome, executed) {
|
|
|
467
529
|
function prepare(collection, options) {
|
|
468
530
|
return {
|
|
469
531
|
param: options.workflowIdParam ?? "workflowId",
|
|
470
|
-
byId: new Map(collection.workflows.map((workflow) => [workflow.id, workflow]))
|
|
532
|
+
byId: new Map(collection.workflows.map((workflow) => [workflow.id, workflow])),
|
|
533
|
+
getProjectSecret: options.getProjectSecret ?? resolveProjectSecret
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
async function validateSchema(schema, value) {
|
|
537
|
+
return schema["~standard"].validate(value);
|
|
538
|
+
}
|
|
539
|
+
function validationError(label, issues) {
|
|
540
|
+
const detail = issues.map((issue) => issue.message).join("; ");
|
|
541
|
+
return `workflow ${label} failed validation${detail ? `: ${detail}` : ""}`;
|
|
542
|
+
}
|
|
543
|
+
async function validateInput(workflow, input) {
|
|
544
|
+
if (!workflow.inputSchema) return { input };
|
|
545
|
+
let result;
|
|
546
|
+
try {
|
|
547
|
+
result = await validateSchema(workflow.inputSchema, input);
|
|
548
|
+
} catch {
|
|
549
|
+
return json(500, {
|
|
550
|
+
error: "input_validation_failed",
|
|
551
|
+
message: "workflow input schema could not be evaluated"
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
if (result.issues) return json(400, {
|
|
555
|
+
error: "invalid_input",
|
|
556
|
+
message: validationError("input", result.issues)
|
|
557
|
+
});
|
|
558
|
+
return { input: result.value };
|
|
559
|
+
}
|
|
560
|
+
async function validateOutput(workflow, outcome) {
|
|
561
|
+
if (outcome.status !== "completed" || !workflow.outputSchema) return outcome;
|
|
562
|
+
let result;
|
|
563
|
+
try {
|
|
564
|
+
result = await validateSchema(workflow.outputSchema, outcome.output);
|
|
565
|
+
} catch {
|
|
566
|
+
return {
|
|
567
|
+
status: "failed",
|
|
568
|
+
memo: outcome.memo,
|
|
569
|
+
steps: [],
|
|
570
|
+
results: outcome.results,
|
|
571
|
+
emitted: outcome.emitted,
|
|
572
|
+
error: "workflow output schema could not be evaluated"
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
if (result.issues) return {
|
|
576
|
+
status: "failed",
|
|
577
|
+
memo: outcome.memo,
|
|
578
|
+
steps: [],
|
|
579
|
+
results: outcome.results,
|
|
580
|
+
emitted: outcome.emitted,
|
|
581
|
+
error: validationError("output", result.issues)
|
|
582
|
+
};
|
|
583
|
+
return {
|
|
584
|
+
...outcome,
|
|
585
|
+
output: result.value
|
|
471
586
|
};
|
|
472
587
|
}
|
|
473
588
|
async function resolve(ctx, param, byId) {
|
|
@@ -491,7 +606,11 @@ async function resolve(ctx, param, byId) {
|
|
|
491
606
|
error: "invalid_request",
|
|
492
607
|
message: body
|
|
493
608
|
});
|
|
494
|
-
const
|
|
609
|
+
const wireInput = body.input ?? null;
|
|
610
|
+
const validated = await validateInput(workflow, wireInput);
|
|
611
|
+
if (validated instanceof Response) return validated;
|
|
612
|
+
const { input } = validated;
|
|
613
|
+
const inputHash = await hashInput(wireInput);
|
|
495
614
|
if (body.memo !== void 0 && body.memo.length > 0 && body.inputHash !== inputHash) return json(400, {
|
|
496
615
|
error: "invalid_request",
|
|
497
616
|
message: body.inputHash === void 0 ? "inputHash must be sent with a memo; echo the value from the previous response" : "input does not match the run this memo came from"
|
|
@@ -499,27 +618,30 @@ async function resolve(ctx, param, byId) {
|
|
|
499
618
|
return {
|
|
500
619
|
workflow,
|
|
501
620
|
body,
|
|
621
|
+
input,
|
|
502
622
|
runId: body.runId ?? `run_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`,
|
|
503
623
|
inputHash
|
|
504
624
|
};
|
|
505
625
|
}
|
|
506
626
|
function createStepsHandler(collection, options = {}) {
|
|
507
|
-
const { param, byId } = prepare(collection, options);
|
|
627
|
+
const { param, byId, getProjectSecret } = prepare(collection, options);
|
|
508
628
|
return async (ctx) => {
|
|
629
|
+
const auth = await authorizeProjectRequest(ctx.request, getProjectSecret);
|
|
630
|
+
if (auth === "unauthorized") return unauthorized();
|
|
631
|
+
if (auth === "unavailable") return authUnavailable();
|
|
509
632
|
const resolved = await resolve(ctx, param, byId);
|
|
510
633
|
if (resolved instanceof Response) return resolved;
|
|
511
|
-
const { workflow, body, runId, inputHash } = resolved;
|
|
512
|
-
const input = body.input ?? null;
|
|
634
|
+
const { workflow, body, input, runId, inputHash } = resolved;
|
|
513
635
|
const before = body.memo ?? [];
|
|
514
636
|
if (body.action === "execute") {
|
|
515
637
|
const selected = body.step;
|
|
516
|
-
const outcome = await replay({
|
|
638
|
+
const outcome = await validateOutput(workflow, await replay({
|
|
517
639
|
workflow,
|
|
518
640
|
input,
|
|
519
641
|
memo: before,
|
|
520
642
|
runId,
|
|
521
643
|
shouldExecute: (intent) => intent.kind !== "step" || operationKey(intent) === operationKey(selected)
|
|
522
|
-
});
|
|
644
|
+
}));
|
|
523
645
|
const executed = outcome.results.find((result) => result.kind === "step" && operationKey(result) === operationKey(selected));
|
|
524
646
|
if (executed === void 0 && outcome.status !== "failed") return json(409, {
|
|
525
647
|
error: "stale_step",
|
|
@@ -527,22 +649,22 @@ function createStepsHandler(collection, options = {}) {
|
|
|
527
649
|
});
|
|
528
650
|
return respond(runId, inputHash, before, outcome, executed);
|
|
529
651
|
}
|
|
530
|
-
const described = await replay({
|
|
652
|
+
const described = await validateOutput(workflow, await replay({
|
|
531
653
|
workflow,
|
|
532
654
|
input,
|
|
533
655
|
memo: before,
|
|
534
656
|
runId,
|
|
535
657
|
shouldExecute: (intent) => intent.kind !== "step"
|
|
536
|
-
});
|
|
658
|
+
}));
|
|
537
659
|
const only = described.steps.length === 1 ? described.steps[0] : void 0;
|
|
538
660
|
if (described.status !== "pending" || only?.kind !== "step") return respond(runId, inputHash, before, described);
|
|
539
|
-
const outcome = await replay({
|
|
661
|
+
const outcome = await validateOutput(workflow, await replay({
|
|
540
662
|
workflow,
|
|
541
663
|
input,
|
|
542
664
|
memo: described.memo,
|
|
543
665
|
runId,
|
|
544
666
|
shouldExecute: (intent) => intent.kind !== "step" || operationKey(intent) === operationKey(only)
|
|
545
|
-
});
|
|
667
|
+
}));
|
|
546
668
|
const executed = outcome.results.find((result) => result.kind === "step" && operationKey(result) === operationKey(only));
|
|
547
669
|
if (executed === void 0 && outcome.status !== "failed") return json(409, {
|
|
548
670
|
error: "stale_step",
|
package/dist/vite.js
CHANGED
|
@@ -167,8 +167,6 @@ function buildRouteSource(routeFile, routeLiteral, param, specifier, root, facto
|
|
|
167
167
|
return `${GENERATED_BANNER}
|
|
168
168
|
// route: ${routeLiteral}
|
|
169
169
|
// emitted to: ${normalizePath(relative(root, routeFile))}
|
|
170
|
-
// This route is unauthenticated: any caller who reaches it can run any declared
|
|
171
|
-
// workflow. Signed invocation is not available yet.
|
|
172
170
|
|
|
173
171
|
import { createFileRoute } from "@tanstack/react-router";
|
|
174
172
|
|