@lovable.dev/workflows-sdk 0.0.0-dev.5 → 0.0.0-dev.52

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.
@@ -1,99 +0,0 @@
1
- import { WorkflowRequestParseError, inMemoryRunStore, parseWorkflowRequest, runWorkflow } from "@tanstack/workflow-core";
2
- //#region src/stacks/tanstack/sse.ts
3
- const HEADERS = {
4
- "content-type": "text/event-stream; charset=utf-8",
5
- "cache-control": "no-cache, no-transform",
6
- connection: "keep-alive",
7
- "x-accel-buffering": "no"
8
- };
9
- function describe(error) {
10
- return error instanceof Error ? error.message : String(error);
11
- }
12
- function frame(name, data) {
13
- return `event: ${name}\ndata: ${JSON.stringify(data)}\n\n`;
14
- }
15
- /**
16
- * Encode a workflow event stream as `text/event-stream`. Each event keeps its
17
- * engine `type` as the SSE event name and its whole JSON as the data line.
18
- */
19
- function toServerSentEventsResponse(events) {
20
- const encoder = new TextEncoder();
21
- const body = new ReadableStream({ async start(controller) {
22
- let connected = true;
23
- const write = (text) => {
24
- if (!connected) return;
25
- try {
26
- controller.enqueue(encoder.encode(text));
27
- } catch {
28
- connected = false;
29
- }
30
- };
31
- const send = (name, data) => {
32
- try {
33
- write(frame(name, data));
34
- } catch (error) {
35
- write(frame("STREAM_ERRORED", { message: `cannot serialize ${name}: ${describe(error)}` }));
36
- }
37
- };
38
- try {
39
- for await (const event of events) send(event.type, event);
40
- } catch (error) {
41
- send("STREAM_ERRORED", { message: describe(error) });
42
- } finally {
43
- try {
44
- controller.close();
45
- } catch {
46
- connected = false;
47
- }
48
- }
49
- } });
50
- return new Response(body, { headers: HEADERS });
51
- }
52
- //#endregion
53
- //#region src/stacks/tanstack/handlers.ts
54
- function json(status, body) {
55
- return new Response(JSON.stringify(body), {
56
- status,
57
- headers: { "content-type": "application/json; charset=utf-8" }
58
- });
59
- }
60
- function resolveWorkflowId(ctx, param) {
61
- const fromParams = ctx.params?.[param];
62
- if (fromParams) return fromParams;
63
- return new URL(ctx.request.url).pathname.split("/").filter(Boolean).at(-1);
64
- }
65
- /** Build a TanStack Start `POST` handler serving every workflow in the collection. */
66
- function createTanStackWorkflowHandler(collection, options = {}) {
67
- const runStore = options.runStore ?? inMemoryRunStore();
68
- const param = options.workflowIdParam ?? "workflowId";
69
- const byId = new Map(collection.workflows.map((workflow) => [workflow.id, workflow]));
70
- return async (ctx) => {
71
- const workflowId = resolveWorkflowId(ctx, param);
72
- const workflow = workflowId === void 0 ? void 0 : byId.get(workflowId);
73
- if (!workflow) return json(404, {
74
- error: "unknown_workflow",
75
- workflowId: workflowId ?? ""
76
- });
77
- let params;
78
- try {
79
- params = await parseWorkflowRequest(ctx.request);
80
- } catch (error) {
81
- if (error instanceof WorkflowRequestParseError) return json(400, {
82
- error: "invalid_request",
83
- message: error.message
84
- });
85
- throw error;
86
- }
87
- if (params.abort) return json(501, {
88
- error: "abort_unsupported",
89
- message: "Cancelling an in-flight run needs a shared run registry (WOR-53)."
90
- });
91
- return toServerSentEventsResponse(runWorkflow({
92
- workflow,
93
- runStore,
94
- ...params
95
- }));
96
- };
97
- }
98
- //#endregion
99
- export { createTanStackWorkflowHandler, toServerSentEventsResponse };