@pgflow/client 0.7.0 → 0.7.2
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/CHANGELOG.md +31 -0
- package/README.md +5 -1
- package/dist/index.js +362 -412
- package/dist/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/dist/pgflow-client.browser.js +1 -1
- package/dist/pgflow-client.browser.js.map +1 -1
- package/dist/src/lib/FlowRun.d.ts +8 -0
- package/dist/src/lib/FlowRun.d.ts.map +1 -1
- package/dist/src/lib/FlowStep.d.ts +8 -0
- package/dist/src/lib/FlowStep.d.ts.map +1 -1
- package/dist/src/lib/PgflowClient.d.ts +1 -2
- package/dist/src/lib/PgflowClient.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
var N = (
|
|
2
|
-
throw TypeError(
|
|
1
|
+
var N = (s) => {
|
|
2
|
+
throw TypeError(s);
|
|
3
3
|
};
|
|
4
|
-
var O = (
|
|
5
|
-
var
|
|
6
|
-
var H = (
|
|
4
|
+
var O = (s, t, r) => t.has(s) || N("Cannot " + r);
|
|
5
|
+
var e = (s, t, r) => (O(s, t, "read from private field"), r ? r.call(s) : t.get(s)), d = (s, t, r) => t.has(s) ? N("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(s) : t.set(s, r), _ = (s, t, r, n) => (O(s, t, "write to private field"), n ? n.call(s, r) : t.set(s, r), r), m = (s, t, r) => (O(s, t, "access private method"), r);
|
|
6
|
+
var H = (s, t, r, n) => ({
|
|
7
7
|
set _(i) {
|
|
8
|
-
|
|
8
|
+
_(s, t, i, r);
|
|
9
9
|
},
|
|
10
10
|
get _() {
|
|
11
|
-
return
|
|
11
|
+
return e(s, t, n);
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
|
-
import { v4 as
|
|
14
|
+
import { v4 as et } from "uuid";
|
|
15
15
|
import { createNanoEvents as L } from "nanoevents";
|
|
16
|
-
var
|
|
17
|
-
function
|
|
18
|
-
return !!
|
|
16
|
+
var c = /* @__PURE__ */ ((s) => (s.Started = "started", s.Completed = "completed", s.Failed = "failed", s))(c || {});
|
|
17
|
+
function lt(s) {
|
|
18
|
+
return !!s && typeof s == "object" && "run_id" in s && "flow_slug" in s && !("step_slug" in s) && "status" in s && (s.status === "started" || s.status === "completed" || s.status === "failed");
|
|
19
19
|
}
|
|
20
|
-
function
|
|
21
|
-
return
|
|
20
|
+
function dt(s) {
|
|
21
|
+
return s.status === "started";
|
|
22
22
|
}
|
|
23
|
-
function
|
|
24
|
-
return
|
|
23
|
+
function ct(s) {
|
|
24
|
+
return s.status === "completed";
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
return
|
|
26
|
+
function pt(s) {
|
|
27
|
+
return s.status === "failed";
|
|
28
28
|
}
|
|
29
|
-
var
|
|
30
|
-
function K(
|
|
31
|
-
return !!
|
|
29
|
+
var h = /* @__PURE__ */ ((s) => (s.Created = "created", s.Started = "started", s.Completed = "completed", s.Failed = "failed", s))(h || {});
|
|
30
|
+
function K(s) {
|
|
31
|
+
return !!s && typeof s == "object" && "run_id" in s && "step_slug" in s && "status" in s && (s.status === "started" || s.status === "completed" || s.status === "failed");
|
|
32
32
|
}
|
|
33
|
-
function
|
|
34
|
-
return K(
|
|
33
|
+
function ht(s) {
|
|
34
|
+
return K(s) && s.status === "started" && "event_type" in s && s.event_type === "step:started";
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
return K(
|
|
36
|
+
function ft(s) {
|
|
37
|
+
return K(s) && s.status === "completed" && "event_type" in s && s.event_type === "step:completed";
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
return K(
|
|
39
|
+
function _t(s) {
|
|
40
|
+
return K(s) && s.status === "failed" && "event_type" in s && s.event_type === "step:failed";
|
|
41
41
|
}
|
|
42
|
-
var S, C, F,
|
|
43
|
-
class
|
|
42
|
+
var S, C, F, U, I, f, j, V, z, X, Y, Z, k, G;
|
|
43
|
+
class st {
|
|
44
44
|
/**
|
|
45
45
|
* Creates a new instance of SupabaseBroadcastAdapter
|
|
46
46
|
*
|
|
47
47
|
* @param supabase - Supabase client instance
|
|
48
48
|
*/
|
|
49
|
-
constructor(t,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
constructor(t, r = {}) {
|
|
50
|
+
d(this, f);
|
|
51
|
+
d(this, S);
|
|
52
|
+
d(this, C, /* @__PURE__ */ new Map());
|
|
53
|
+
d(this, F, L());
|
|
54
|
+
d(this, U);
|
|
55
|
+
d(this, I);
|
|
56
56
|
// Store unsubscribe functions per run ID for reference equality
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
d(this, k, /* @__PURE__ */ new Map());
|
|
58
|
+
_(this, S, t), _(this, U, r.reconnectDelayMs ?? 2e3), _(this, I, r.schedule ?? setTimeout);
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
61
|
* Fetches flow definition metadata from the database
|
|
@@ -63,16 +63,16 @@ class rt {
|
|
|
63
63
|
* @param flow_slug - Flow slug to fetch
|
|
64
64
|
*/
|
|
65
65
|
async fetchFlowDefinition(t) {
|
|
66
|
-
const [
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
const [r, n] = await Promise.all([
|
|
67
|
+
e(this, S).schema("pgflow").from("flows").select("*").eq("flow_slug", t).single(),
|
|
68
|
+
e(this, S).schema("pgflow").from("steps").select("*").eq("flow_slug", t).order("step_index", { ascending: !0 })
|
|
69
69
|
]);
|
|
70
|
-
if (
|
|
71
|
-
if (!
|
|
70
|
+
if (r.error) throw r.error;
|
|
71
|
+
if (!r.data) throw new Error(`Flow "${t}" not found`);
|
|
72
72
|
if (n.error) throw n.error;
|
|
73
73
|
const i = Array.isArray(n.data) ? n.data : [];
|
|
74
74
|
return {
|
|
75
|
-
flow:
|
|
75
|
+
flow: r.data,
|
|
76
76
|
steps: i
|
|
77
77
|
};
|
|
78
78
|
}
|
|
@@ -83,10 +83,10 @@ class rt {
|
|
|
83
83
|
* @returns Function to unsubscribe from the event
|
|
84
84
|
*/
|
|
85
85
|
onRunEvent(t) {
|
|
86
|
-
const
|
|
86
|
+
const r = e(this, F).on("runEvent", t);
|
|
87
87
|
return () => {
|
|
88
88
|
try {
|
|
89
|
-
|
|
89
|
+
r();
|
|
90
90
|
} catch (n) {
|
|
91
91
|
console.warn("Could not unsubscribe from run event - emitter may have been disposed", n);
|
|
92
92
|
}
|
|
@@ -99,10 +99,10 @@ class rt {
|
|
|
99
99
|
* @returns Function to unsubscribe from the event
|
|
100
100
|
*/
|
|
101
101
|
onStepEvent(t) {
|
|
102
|
-
const
|
|
102
|
+
const r = e(this, F).on("stepEvent", t);
|
|
103
103
|
return () => {
|
|
104
104
|
try {
|
|
105
|
-
|
|
105
|
+
r();
|
|
106
106
|
} catch (n) {
|
|
107
107
|
console.warn("Could not unsubscribe from step event - emitter may have been disposed", n);
|
|
108
108
|
}
|
|
@@ -115,28 +115,28 @@ class rt {
|
|
|
115
115
|
* @returns Function to unsubscribe
|
|
116
116
|
*/
|
|
117
117
|
async subscribeToRun(t) {
|
|
118
|
-
const
|
|
119
|
-
if (
|
|
120
|
-
const
|
|
121
|
-
if (
|
|
122
|
-
return
|
|
123
|
-
|
|
118
|
+
const r = `pgflow:run:${t}`;
|
|
119
|
+
if (e(this, C).has(t)) {
|
|
120
|
+
const p = e(this, k).get(t);
|
|
121
|
+
if (p)
|
|
122
|
+
return p;
|
|
123
|
+
m(this, f, G).call(this, t);
|
|
124
124
|
}
|
|
125
|
-
const n =
|
|
126
|
-
n.on("broadcast", { event: "*" },
|
|
127
|
-
console.log(`Channel ${
|
|
128
|
-
}), n.on("system", { event: "error" }, (
|
|
129
|
-
console.log(`Channel ${
|
|
130
|
-
}), console.log(`Subscribing to channel ${
|
|
131
|
-
const
|
|
132
|
-
|
|
125
|
+
const n = e(this, S).channel(r);
|
|
126
|
+
n.on("broadcast", { event: "*" }, m(this, f, j).bind(this)), n.on("system", { event: "closed" }, () => {
|
|
127
|
+
console.log(`Channel ${r} closed`);
|
|
128
|
+
}), n.on("system", { event: "error" }, (p) => {
|
|
129
|
+
console.log(`Channel ${r} error:`, p), m(this, f, z).call(this, t, r, n, p.error);
|
|
130
|
+
}), console.log(`Subscribing to channel ${r}...`), await new Promise((p, u) => {
|
|
131
|
+
const l = setTimeout(() => {
|
|
132
|
+
u(new Error(`Subscription timeout for channel ${r}`));
|
|
133
133
|
}, 2e4);
|
|
134
|
-
n.subscribe((
|
|
135
|
-
console.log(`Channel ${
|
|
134
|
+
n.subscribe((b) => {
|
|
135
|
+
console.log(`Channel ${r} subscription status:`, b), b === "SUBSCRIBED" && (clearTimeout(l), p());
|
|
136
136
|
});
|
|
137
|
-
}),
|
|
138
|
-
const
|
|
139
|
-
return
|
|
137
|
+
}), e(this, C).set(t, n);
|
|
138
|
+
const w = () => this.unsubscribe(t);
|
|
139
|
+
return e(this, k).set(t, w), w;
|
|
140
140
|
}
|
|
141
141
|
/**
|
|
142
142
|
* Unsubscribes from a run's events
|
|
@@ -144,7 +144,7 @@ class rt {
|
|
|
144
144
|
* @param run_id - Run ID to unsubscribe from
|
|
145
145
|
*/
|
|
146
146
|
unsubscribe(t) {
|
|
147
|
-
|
|
147
|
+
m(this, f, G).call(this, t);
|
|
148
148
|
}
|
|
149
149
|
/**
|
|
150
150
|
* Fetches current state of a run and its steps
|
|
@@ -152,24 +152,24 @@ class rt {
|
|
|
152
152
|
* @param run_id - Run ID to fetch
|
|
153
153
|
*/
|
|
154
154
|
async getRunWithStates(t) {
|
|
155
|
-
const { data:
|
|
155
|
+
const { data: r, error: n } = await e(this, S).schema("pgflow").rpc("get_run_with_states", { run_id: t });
|
|
156
156
|
if (n) throw n;
|
|
157
|
-
if (!
|
|
158
|
-
return
|
|
157
|
+
if (!r) throw new Error(`No data returned for run ${t}`);
|
|
158
|
+
return r;
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
|
-
S = new WeakMap(), C = new WeakMap(), F = new WeakMap(),
|
|
161
|
+
S = new WeakMap(), C = new WeakMap(), F = new WeakMap(), U = new WeakMap(), I = new WeakMap(), f = new WeakSet(), /**
|
|
162
162
|
* Handle broadcast messages from Supabase
|
|
163
163
|
* @param payload - The message payload
|
|
164
164
|
*/
|
|
165
165
|
j = function(t) {
|
|
166
|
-
const { event:
|
|
167
|
-
|
|
166
|
+
const { event: r, payload: n } = t, i = n;
|
|
167
|
+
m(this, f, V).call(this, i), r.startsWith("run:") ? e(this, F).emit("runEvent", i) : r.startsWith("step:") && e(this, F).emit("stepEvent", i);
|
|
168
168
|
}, /**
|
|
169
169
|
* Parse JSON string fields in broadcast event data
|
|
170
170
|
* @param eventData - The event data object to parse
|
|
171
171
|
*/
|
|
172
|
-
|
|
172
|
+
V = function(t) {
|
|
173
173
|
if ("output" in t && typeof t.output == "string")
|
|
174
174
|
try {
|
|
175
175
|
t.output = JSON.parse(t.output);
|
|
@@ -180,55 +180,55 @@ X = function(t) {
|
|
|
180
180
|
t.input = JSON.parse(t.input);
|
|
181
181
|
} catch {
|
|
182
182
|
}
|
|
183
|
-
}, z = async function(t,
|
|
184
|
-
console.error(`Channel ${
|
|
185
|
-
|
|
186
|
-
},
|
|
183
|
+
}, z = async function(t, r, n, i) {
|
|
184
|
+
console.error(`Channel ${r} error:`, i), e(this, I).call(this, async () => {
|
|
185
|
+
e(this, C).has(t) && await m(this, f, Y).call(this, t, r);
|
|
186
|
+
}, e(this, U));
|
|
187
187
|
}, /**
|
|
188
188
|
* Creates and configures a channel for a run
|
|
189
189
|
* @param run_id - The run ID
|
|
190
190
|
* @param channelName - The channel name
|
|
191
191
|
* @returns The configured RealtimeChannel
|
|
192
192
|
*/
|
|
193
|
-
|
|
194
|
-
const n =
|
|
195
|
-
return n.on("broadcast", { event: "*" },
|
|
196
|
-
},
|
|
197
|
-
console.log(`Attempting to reconnect to ${
|
|
193
|
+
X = function(t, r) {
|
|
194
|
+
const n = e(this, S).channel(r);
|
|
195
|
+
return n.on("broadcast", { event: "*" }, m(this, f, j).bind(this)), n;
|
|
196
|
+
}, Y = async function(t, r) {
|
|
197
|
+
console.log(`Attempting to reconnect to ${r}`);
|
|
198
198
|
try {
|
|
199
199
|
const n = await this.getRunWithStates(t);
|
|
200
|
-
|
|
201
|
-
const i =
|
|
200
|
+
m(this, f, Z).call(this, t, n);
|
|
201
|
+
const i = m(this, f, X).call(this, t, r);
|
|
202
202
|
i.on("system", { event: "subscribed" }, () => {
|
|
203
|
-
console.log(`Reconnected and subscribed to channel ${
|
|
203
|
+
console.log(`Reconnected and subscribed to channel ${r}`);
|
|
204
204
|
}), i.on("system", { event: "closed" }, () => {
|
|
205
|
-
console.log(`Reconnected channel ${
|
|
205
|
+
console.log(`Reconnected channel ${r} closed`);
|
|
206
206
|
}), i.on(
|
|
207
207
|
"system",
|
|
208
208
|
{ event: "error" },
|
|
209
|
-
(
|
|
210
|
-
), i.subscribe(),
|
|
209
|
+
(w) => m(this, f, z).call(this, t, r, i, w.error)
|
|
210
|
+
), i.subscribe(), e(this, C).set(t, i);
|
|
211
211
|
} catch (n) {
|
|
212
|
-
console.error(`Failed to reconnect to ${
|
|
212
|
+
console.error(`Failed to reconnect to ${r}:`, n);
|
|
213
213
|
}
|
|
214
214
|
}, /**
|
|
215
215
|
* Refresh client state from a state snapshot
|
|
216
216
|
* @param run_id - The run ID
|
|
217
217
|
* @param state - The state snapshot
|
|
218
218
|
*/
|
|
219
|
-
|
|
220
|
-
if (!
|
|
219
|
+
Z = function(t, r) {
|
|
220
|
+
if (!r || !r.run) return;
|
|
221
221
|
const n = {
|
|
222
|
-
event_type: `run:${
|
|
223
|
-
...
|
|
222
|
+
event_type: `run:${r.run.status}`,
|
|
223
|
+
...r.run
|
|
224
224
|
};
|
|
225
|
-
if (
|
|
226
|
-
for (const i of
|
|
227
|
-
const
|
|
225
|
+
if (e(this, F).emit("runEvent", n), r.steps && Array.isArray(r.steps))
|
|
226
|
+
for (const i of r.steps) {
|
|
227
|
+
const w = {
|
|
228
228
|
event_type: `step:${i.status}`,
|
|
229
229
|
...i
|
|
230
230
|
};
|
|
231
|
-
|
|
231
|
+
e(this, F).emit("stepEvent", w);
|
|
232
232
|
}
|
|
233
233
|
}, k = new WeakMap(), /**
|
|
234
234
|
* Unsubscribes from a run's events
|
|
@@ -236,81 +236,81 @@ v = function(t, s) {
|
|
|
236
236
|
* @param run_id - Run ID to unsubscribe from
|
|
237
237
|
*/
|
|
238
238
|
G = function(t) {
|
|
239
|
-
const
|
|
240
|
-
|
|
239
|
+
const r = e(this, C).get(t);
|
|
240
|
+
r && (r.unsubscribe(), e(this, C).delete(t), e(this, k).delete(t));
|
|
241
241
|
};
|
|
242
|
-
var
|
|
243
|
-
class
|
|
242
|
+
var o, $, W, J, v;
|
|
243
|
+
class rt {
|
|
244
244
|
/**
|
|
245
245
|
* Creates a new FlowStep instance
|
|
246
246
|
*
|
|
247
247
|
* @param initialState - Initial state for the step
|
|
248
248
|
*/
|
|
249
249
|
constructor(t) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
[
|
|
255
|
-
[
|
|
256
|
-
[
|
|
257
|
-
[
|
|
250
|
+
d(this, J);
|
|
251
|
+
d(this, o);
|
|
252
|
+
d(this, $, L());
|
|
253
|
+
d(this, W, {
|
|
254
|
+
[h.Created]: 0,
|
|
255
|
+
[h.Started]: 1,
|
|
256
|
+
[h.Completed]: 2,
|
|
257
|
+
[h.Failed]: 3
|
|
258
258
|
});
|
|
259
|
-
|
|
259
|
+
_(this, o, t);
|
|
260
260
|
}
|
|
261
261
|
/**
|
|
262
262
|
* Get the run ID this step belongs to
|
|
263
263
|
*/
|
|
264
264
|
get run_id() {
|
|
265
|
-
return
|
|
265
|
+
return e(this, o).run_id;
|
|
266
266
|
}
|
|
267
267
|
/**
|
|
268
268
|
* Get the step slug
|
|
269
269
|
*/
|
|
270
270
|
get step_slug() {
|
|
271
|
-
return
|
|
271
|
+
return e(this, o).step_slug;
|
|
272
272
|
}
|
|
273
273
|
/**
|
|
274
274
|
* Get the current status
|
|
275
275
|
*/
|
|
276
276
|
get status() {
|
|
277
|
-
return
|
|
277
|
+
return e(this, o).status;
|
|
278
278
|
}
|
|
279
279
|
/**
|
|
280
280
|
* Get the started_at timestamp
|
|
281
281
|
*/
|
|
282
282
|
get started_at() {
|
|
283
|
-
return
|
|
283
|
+
return e(this, o).started_at;
|
|
284
284
|
}
|
|
285
285
|
/**
|
|
286
286
|
* Get the completed_at timestamp
|
|
287
287
|
*/
|
|
288
288
|
get completed_at() {
|
|
289
|
-
return
|
|
289
|
+
return e(this, o).completed_at;
|
|
290
290
|
}
|
|
291
291
|
/**
|
|
292
292
|
* Get the failed_at timestamp
|
|
293
293
|
*/
|
|
294
294
|
get failed_at() {
|
|
295
|
-
return
|
|
295
|
+
return e(this, o).failed_at;
|
|
296
296
|
}
|
|
297
297
|
/**
|
|
298
298
|
* Get the step output
|
|
299
299
|
*/
|
|
300
300
|
get output() {
|
|
301
|
-
return
|
|
301
|
+
return e(this, o).output;
|
|
302
302
|
}
|
|
303
303
|
/**
|
|
304
304
|
* Get the error object
|
|
305
305
|
*/
|
|
306
306
|
get error() {
|
|
307
|
-
return
|
|
307
|
+
return e(this, o).error;
|
|
308
308
|
}
|
|
309
309
|
/**
|
|
310
310
|
* Get the error message
|
|
311
311
|
*/
|
|
312
312
|
get error_message() {
|
|
313
|
-
return
|
|
313
|
+
return e(this, o).error_message;
|
|
314
314
|
}
|
|
315
315
|
/**
|
|
316
316
|
* Register an event handler for a step event
|
|
@@ -319,8 +319,8 @@ class nt {
|
|
|
319
319
|
* @param callback - Callback function to execute when event is emitted
|
|
320
320
|
* @returns Function to unsubscribe from the event
|
|
321
321
|
*/
|
|
322
|
-
on(t,
|
|
323
|
-
return
|
|
322
|
+
on(t, r) {
|
|
323
|
+
return e(this, $).on(t, r);
|
|
324
324
|
}
|
|
325
325
|
/**
|
|
326
326
|
* Wait for the step to reach a specific status
|
|
@@ -329,88 +329,98 @@ class nt {
|
|
|
329
329
|
* @param options - Optional timeout and abort signal
|
|
330
330
|
* @returns Promise that resolves with the step instance when the status is reached
|
|
331
331
|
*/
|
|
332
|
-
waitForStatus(t,
|
|
333
|
-
const n = (
|
|
334
|
-
return this.status === t ? Promise.resolve(this) : new Promise((
|
|
335
|
-
let
|
|
336
|
-
n > 0 && (
|
|
337
|
-
|
|
332
|
+
waitForStatus(t, r) {
|
|
333
|
+
const n = (r == null ? void 0 : r.timeoutMs) ?? 3e5, { signal: i } = r || {};
|
|
334
|
+
return this.status === t ? Promise.resolve(this) : new Promise((w, p) => {
|
|
335
|
+
let u, l = !1;
|
|
336
|
+
n > 0 && (u = setTimeout(() => {
|
|
337
|
+
l || (l = !0, T(), p(new Error(`Timeout waiting for step ${this.step_slug} to reach status '${t}'`)));
|
|
338
338
|
}, n));
|
|
339
|
-
let
|
|
339
|
+
let b;
|
|
340
340
|
if (i) {
|
|
341
341
|
const E = () => {
|
|
342
|
-
|
|
342
|
+
l || (l = !0, u && clearTimeout(u), T(), p(new Error(`Aborted waiting for step ${this.step_slug} to reach status '${t}'`)));
|
|
343
343
|
};
|
|
344
|
-
i.addEventListener("abort", E),
|
|
344
|
+
i.addEventListener("abort", E), b = () => {
|
|
345
345
|
i.removeEventListener("abort", E);
|
|
346
346
|
};
|
|
347
347
|
}
|
|
348
|
-
const
|
|
348
|
+
const T = this.on("*", (E) => {
|
|
349
349
|
if (E.status === t) {
|
|
350
|
-
if (
|
|
351
|
-
|
|
350
|
+
if (l) return;
|
|
351
|
+
l = !0, u && clearTimeout(u), b && b(), T(), w(this);
|
|
352
352
|
}
|
|
353
353
|
});
|
|
354
354
|
});
|
|
355
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* Apply state from database snapshot (no events emitted)
|
|
358
|
+
* Used when initializing state from start_flow_with_states() or get_run_with_states()
|
|
359
|
+
*
|
|
360
|
+
* @internal This method is only intended for use by PgflowClient.
|
|
361
|
+
* Applications should not call this directly.
|
|
362
|
+
*/
|
|
363
|
+
applySnapshot(t) {
|
|
364
|
+
e(this, o).status = t.status, e(this, o).started_at = t.started_at ? new Date(t.started_at) : null, e(this, o).completed_at = t.completed_at ? new Date(t.completed_at) : null, e(this, o).failed_at = t.failed_at ? new Date(t.failed_at) : null, e(this, o).error_message = t.error_message, e(this, o).error = t.error_message ? new Error(t.error_message) : null;
|
|
365
|
+
}
|
|
356
366
|
/**
|
|
357
367
|
* Updates the step state based on an event
|
|
358
|
-
*
|
|
368
|
+
*
|
|
359
369
|
* @internal This method is only intended for use by FlowRun and tests.
|
|
360
370
|
* Applications should not call this directly - state updates should come from
|
|
361
371
|
* database events through the PgflowClient.
|
|
362
|
-
*
|
|
372
|
+
*
|
|
363
373
|
* TODO: After v1.0, make this method private and refactor tests to use PgflowClient
|
|
364
374
|
* with event emission instead of direct state manipulation.
|
|
365
375
|
*/
|
|
366
376
|
updateState(t) {
|
|
367
|
-
if (t.step_slug !==
|
|
377
|
+
if (t.step_slug !== e(this, o).step_slug || t.run_id !== e(this, o).run_id || !m(this, J, v).call(this, e(this, o).status, t.status))
|
|
368
378
|
return !1;
|
|
369
379
|
switch (t.status) {
|
|
370
|
-
case
|
|
371
|
-
|
|
372
|
-
...
|
|
373
|
-
status:
|
|
380
|
+
case h.Started:
|
|
381
|
+
_(this, o, {
|
|
382
|
+
...e(this, o),
|
|
383
|
+
status: h.Started,
|
|
374
384
|
started_at: typeof t.started_at == "string" ? new Date(t.started_at) : /* @__PURE__ */ new Date()
|
|
375
|
-
}),
|
|
385
|
+
}), e(this, $).emit("started", t);
|
|
376
386
|
break;
|
|
377
|
-
case
|
|
378
|
-
|
|
379
|
-
...
|
|
380
|
-
status:
|
|
387
|
+
case h.Completed:
|
|
388
|
+
_(this, o, {
|
|
389
|
+
...e(this, o),
|
|
390
|
+
status: h.Completed,
|
|
381
391
|
completed_at: typeof t.completed_at == "string" ? new Date(t.completed_at) : /* @__PURE__ */ new Date(),
|
|
382
392
|
output: t.output
|
|
383
|
-
}),
|
|
393
|
+
}), e(this, $).emit("completed", t);
|
|
384
394
|
break;
|
|
385
|
-
case
|
|
386
|
-
|
|
387
|
-
...
|
|
388
|
-
status:
|
|
395
|
+
case h.Failed:
|
|
396
|
+
_(this, o, {
|
|
397
|
+
...e(this, o),
|
|
398
|
+
status: h.Failed,
|
|
389
399
|
failed_at: typeof t.failed_at == "string" ? new Date(t.failed_at) : /* @__PURE__ */ new Date(),
|
|
390
400
|
error_message: typeof t.error_message == "string" ? t.error_message : "Unknown error",
|
|
391
401
|
error: new Error(typeof t.error_message == "string" ? t.error_message : "Unknown error")
|
|
392
|
-
}),
|
|
402
|
+
}), e(this, $).emit("failed", t);
|
|
393
403
|
break;
|
|
394
404
|
default:
|
|
395
405
|
return !1;
|
|
396
406
|
}
|
|
397
|
-
return
|
|
407
|
+
return e(this, $).emit("*", t), !0;
|
|
398
408
|
}
|
|
399
409
|
}
|
|
400
|
-
|
|
410
|
+
o = new WeakMap(), $ = new WeakMap(), W = new WeakMap(), J = new WeakSet(), /**
|
|
401
411
|
* Determines if a status should be updated based on precedence
|
|
402
412
|
*
|
|
403
413
|
* @param currentStatus - Current status
|
|
404
414
|
* @param newStatus - New status
|
|
405
415
|
* @returns true if the status should be updated, false otherwise
|
|
406
416
|
*/
|
|
407
|
-
|
|
408
|
-
if (t ===
|
|
417
|
+
v = function(t, r) {
|
|
418
|
+
if (t === h.Completed || t === h.Failed)
|
|
409
419
|
return !1;
|
|
410
|
-
const n =
|
|
411
|
-
return
|
|
420
|
+
const n = e(this, W)[t];
|
|
421
|
+
return e(this, W)[r] > n;
|
|
412
422
|
};
|
|
413
|
-
var
|
|
423
|
+
var a, R, A, x, M, P, D, B, tt;
|
|
414
424
|
class Q {
|
|
415
425
|
/**
|
|
416
426
|
* Creates a new FlowRun instance
|
|
@@ -418,85 +428,85 @@ class Q {
|
|
|
418
428
|
* @param initialState - Initial state for the run
|
|
419
429
|
*/
|
|
420
430
|
constructor(t) {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
[
|
|
427
|
-
[
|
|
428
|
-
[
|
|
431
|
+
d(this, D);
|
|
432
|
+
d(this, a);
|
|
433
|
+
d(this, R, L());
|
|
434
|
+
d(this, A, /* @__PURE__ */ new Map());
|
|
435
|
+
d(this, x, {
|
|
436
|
+
[c.Started]: 0,
|
|
437
|
+
[c.Completed]: 1,
|
|
438
|
+
[c.Failed]: 2
|
|
429
439
|
});
|
|
430
|
-
|
|
440
|
+
d(this, M, !1);
|
|
431
441
|
// Track number of listeners
|
|
432
|
-
|
|
433
|
-
|
|
442
|
+
d(this, P, 0);
|
|
443
|
+
_(this, a, t);
|
|
434
444
|
}
|
|
435
445
|
/**
|
|
436
446
|
* Get the run ID
|
|
437
447
|
*/
|
|
438
448
|
get run_id() {
|
|
439
|
-
return
|
|
449
|
+
return e(this, a).run_id;
|
|
440
450
|
}
|
|
441
451
|
/**
|
|
442
452
|
* Get the flow slug
|
|
443
453
|
*/
|
|
444
454
|
get flow_slug() {
|
|
445
|
-
return
|
|
455
|
+
return e(this, a).flow_slug;
|
|
446
456
|
}
|
|
447
457
|
/**
|
|
448
458
|
* Get the current status
|
|
449
459
|
*/
|
|
450
460
|
get status() {
|
|
451
|
-
return
|
|
461
|
+
return e(this, a).status;
|
|
452
462
|
}
|
|
453
463
|
/**
|
|
454
464
|
* Get the started_at timestamp
|
|
455
465
|
*/
|
|
456
466
|
get started_at() {
|
|
457
|
-
return
|
|
467
|
+
return e(this, a).started_at;
|
|
458
468
|
}
|
|
459
469
|
/**
|
|
460
470
|
* Get the completed_at timestamp
|
|
461
471
|
*/
|
|
462
472
|
get completed_at() {
|
|
463
|
-
return
|
|
473
|
+
return e(this, a).completed_at;
|
|
464
474
|
}
|
|
465
475
|
/**
|
|
466
476
|
* Get the failed_at timestamp
|
|
467
477
|
*/
|
|
468
478
|
get failed_at() {
|
|
469
|
-
return
|
|
479
|
+
return e(this, a).failed_at;
|
|
470
480
|
}
|
|
471
481
|
/**
|
|
472
482
|
* Get the flow input
|
|
473
483
|
*/
|
|
474
484
|
get input() {
|
|
475
|
-
return
|
|
485
|
+
return e(this, a).input;
|
|
476
486
|
}
|
|
477
487
|
/**
|
|
478
488
|
* Get the flow output
|
|
479
489
|
*/
|
|
480
490
|
get output() {
|
|
481
|
-
return
|
|
491
|
+
return e(this, a).output;
|
|
482
492
|
}
|
|
483
493
|
/**
|
|
484
494
|
* Get the error object
|
|
485
495
|
*/
|
|
486
496
|
get error() {
|
|
487
|
-
return
|
|
497
|
+
return e(this, a).error;
|
|
488
498
|
}
|
|
489
499
|
/**
|
|
490
500
|
* Get the error message
|
|
491
501
|
*/
|
|
492
502
|
get error_message() {
|
|
493
|
-
return
|
|
503
|
+
return e(this, a).error_message;
|
|
494
504
|
}
|
|
495
505
|
/**
|
|
496
506
|
* Get the number of remaining steps
|
|
497
507
|
*/
|
|
498
508
|
get remaining_steps() {
|
|
499
|
-
return
|
|
509
|
+
return e(this, a).remaining_steps;
|
|
500
510
|
}
|
|
501
511
|
/**
|
|
502
512
|
* Register an event handler for a run event
|
|
@@ -505,11 +515,11 @@ class Q {
|
|
|
505
515
|
* @param callback - Callback function to execute when event is emitted
|
|
506
516
|
* @returns Function to unsubscribe from the event
|
|
507
517
|
*/
|
|
508
|
-
on(t,
|
|
518
|
+
on(t, r) {
|
|
509
519
|
H(this, P)._++;
|
|
510
|
-
const n =
|
|
520
|
+
const n = e(this, R).on(t, r);
|
|
511
521
|
return () => {
|
|
512
|
-
n(), H(this, P)._--,
|
|
522
|
+
n(), H(this, P)._--, m(this, D, B).call(this);
|
|
513
523
|
};
|
|
514
524
|
}
|
|
515
525
|
/**
|
|
@@ -519,13 +529,13 @@ class Q {
|
|
|
519
529
|
* @returns FlowStep instance for the specified step
|
|
520
530
|
*/
|
|
521
531
|
step(t) {
|
|
522
|
-
const
|
|
523
|
-
if (
|
|
524
|
-
return
|
|
525
|
-
const n = new
|
|
532
|
+
const r = e(this, A).get(t);
|
|
533
|
+
if (r)
|
|
534
|
+
return r;
|
|
535
|
+
const n = new rt({
|
|
526
536
|
run_id: this.run_id,
|
|
527
537
|
step_slug: t,
|
|
528
|
-
status:
|
|
538
|
+
status: h.Created,
|
|
529
539
|
output: null,
|
|
530
540
|
error: null,
|
|
531
541
|
error_message: null,
|
|
@@ -533,7 +543,7 @@ class Q {
|
|
|
533
543
|
completed_at: null,
|
|
534
544
|
failed_at: null
|
|
535
545
|
});
|
|
536
|
-
return
|
|
546
|
+
return e(this, A).set(
|
|
537
547
|
t,
|
|
538
548
|
n
|
|
539
549
|
), n;
|
|
@@ -545,7 +555,7 @@ class Q {
|
|
|
545
555
|
* @returns true if the step exists, false otherwise
|
|
546
556
|
*/
|
|
547
557
|
hasStep(t) {
|
|
548
|
-
return
|
|
558
|
+
return e(this, A).has(t);
|
|
549
559
|
}
|
|
550
560
|
/**
|
|
551
561
|
* Wait for the run to reach a specific status
|
|
@@ -554,84 +564,94 @@ class Q {
|
|
|
554
564
|
* @param options - Optional timeout and abort signal
|
|
555
565
|
* @returns Promise that resolves with the run instance when the status is reached
|
|
556
566
|
*/
|
|
557
|
-
waitForStatus(t,
|
|
558
|
-
const n = (
|
|
559
|
-
return this.status === t ? Promise.resolve(this) : new Promise((
|
|
560
|
-
let
|
|
561
|
-
n > 0 && (
|
|
562
|
-
|
|
567
|
+
waitForStatus(t, r) {
|
|
568
|
+
const n = (r == null ? void 0 : r.timeoutMs) ?? 3e5, { signal: i } = r || {};
|
|
569
|
+
return this.status === t ? Promise.resolve(this) : new Promise((w, p) => {
|
|
570
|
+
let u, l = !1;
|
|
571
|
+
n > 0 && (u = setTimeout(() => {
|
|
572
|
+
l || (l = !0, T(), p(
|
|
563
573
|
new Error(
|
|
564
574
|
`Timeout waiting for run ${this.run_id} to reach status '${t}'`
|
|
565
575
|
)
|
|
566
576
|
));
|
|
567
577
|
}, n));
|
|
568
|
-
let
|
|
578
|
+
let b;
|
|
569
579
|
if (i) {
|
|
570
580
|
const E = () => {
|
|
571
|
-
|
|
581
|
+
l || (l = !0, u && clearTimeout(u), T(), p(
|
|
572
582
|
new Error(
|
|
573
583
|
`Aborted waiting for run ${this.run_id} to reach status '${t}'`
|
|
574
584
|
)
|
|
575
585
|
));
|
|
576
586
|
};
|
|
577
|
-
i.addEventListener("abort", E),
|
|
587
|
+
i.addEventListener("abort", E), b = () => {
|
|
578
588
|
i.removeEventListener("abort", E);
|
|
579
589
|
};
|
|
580
590
|
}
|
|
581
|
-
const
|
|
591
|
+
const T = this.on("*", (E) => {
|
|
582
592
|
if (E.status === t) {
|
|
583
|
-
if (
|
|
584
|
-
|
|
593
|
+
if (l) return;
|
|
594
|
+
l = !0, u && clearTimeout(u), b && b(), T(), w(this);
|
|
585
595
|
}
|
|
586
596
|
});
|
|
587
597
|
});
|
|
588
598
|
}
|
|
599
|
+
/**
|
|
600
|
+
* Apply state from database snapshot (no events emitted)
|
|
601
|
+
* Used when initializing state from start_flow_with_states() or get_run_with_states()
|
|
602
|
+
*
|
|
603
|
+
* @internal This method is only intended for use by PgflowClient.
|
|
604
|
+
* Applications should not call this directly.
|
|
605
|
+
*/
|
|
606
|
+
applySnapshot(t) {
|
|
607
|
+
e(this, a).status = t.status, e(this, a).input = t.input, e(this, a).output = t.output, e(this, a).started_at = t.started_at ? new Date(t.started_at) : null, e(this, a).completed_at = t.completed_at ? new Date(t.completed_at) : null, e(this, a).failed_at = t.failed_at ? new Date(t.failed_at) : null, e(this, a).remaining_steps = t.remaining_steps, e(this, a).error_message = null, e(this, a).error = null;
|
|
608
|
+
}
|
|
589
609
|
/**
|
|
590
610
|
* Updates the run state based on an event
|
|
591
|
-
*
|
|
611
|
+
*
|
|
592
612
|
* @internal This method is only intended for use by PgflowClient and tests.
|
|
593
613
|
* Applications should not call this directly - state updates should come from
|
|
594
614
|
* database events through the PgflowClient.
|
|
595
|
-
*
|
|
615
|
+
*
|
|
596
616
|
* TODO: After v1.0, make this method private and refactor tests to use PgflowClient
|
|
597
617
|
* with event emission instead of direct state manipulation.
|
|
598
618
|
*/
|
|
599
619
|
updateState(t) {
|
|
600
|
-
if (t.run_id !==
|
|
620
|
+
if (t.run_id !== e(this, a).run_id || !m(this, D, tt).call(this, e(this, a).status, t.status))
|
|
601
621
|
return !1;
|
|
602
622
|
switch (t.status) {
|
|
603
|
-
case
|
|
604
|
-
|
|
605
|
-
...
|
|
606
|
-
status:
|
|
623
|
+
case c.Started:
|
|
624
|
+
_(this, a, {
|
|
625
|
+
...e(this, a),
|
|
626
|
+
status: c.Started,
|
|
607
627
|
started_at: typeof t.started_at == "string" ? new Date(t.started_at) : /* @__PURE__ */ new Date(),
|
|
608
|
-
remaining_steps: "remaining_steps" in t ? Number(t.remaining_steps) :
|
|
609
|
-
}),
|
|
628
|
+
remaining_steps: "remaining_steps" in t ? Number(t.remaining_steps) : e(this, a).remaining_steps
|
|
629
|
+
}), e(this, R).emit("started", t);
|
|
610
630
|
break;
|
|
611
|
-
case
|
|
612
|
-
|
|
613
|
-
...
|
|
614
|
-
status:
|
|
631
|
+
case c.Completed:
|
|
632
|
+
_(this, a, {
|
|
633
|
+
...e(this, a),
|
|
634
|
+
status: c.Completed,
|
|
615
635
|
completed_at: typeof t.completed_at == "string" ? new Date(t.completed_at) : /* @__PURE__ */ new Date(),
|
|
616
636
|
output: t.output,
|
|
617
637
|
remaining_steps: 0
|
|
618
|
-
}),
|
|
638
|
+
}), e(this, R).emit("completed", t), m(this, D, B).call(this);
|
|
619
639
|
break;
|
|
620
|
-
case
|
|
621
|
-
|
|
622
|
-
...
|
|
623
|
-
status:
|
|
640
|
+
case c.Failed:
|
|
641
|
+
_(this, a, {
|
|
642
|
+
...e(this, a),
|
|
643
|
+
status: c.Failed,
|
|
624
644
|
failed_at: typeof t.failed_at == "string" ? new Date(t.failed_at) : /* @__PURE__ */ new Date(),
|
|
625
645
|
error_message: typeof t.error_message == "string" ? t.error_message : "Unknown error",
|
|
626
646
|
error: new Error(
|
|
627
647
|
typeof t.error_message == "string" ? t.error_message : "Unknown error"
|
|
628
648
|
)
|
|
629
|
-
}),
|
|
649
|
+
}), e(this, R).emit("failed", t), m(this, D, B).call(this);
|
|
630
650
|
break;
|
|
631
651
|
default:
|
|
632
652
|
return !1;
|
|
633
653
|
}
|
|
634
|
-
return
|
|
654
|
+
return e(this, R).emit("*", t), !0;
|
|
635
655
|
}
|
|
636
656
|
/**
|
|
637
657
|
* Updates a step state based on an event
|
|
@@ -640,21 +660,21 @@ class Q {
|
|
|
640
660
|
* @param event - Event data to update the step with
|
|
641
661
|
* @returns true if the state was updated, false otherwise
|
|
642
662
|
*/
|
|
643
|
-
updateStepState(t,
|
|
644
|
-
return this.step(t).updateState(
|
|
663
|
+
updateStepState(t, r) {
|
|
664
|
+
return this.step(t).updateState(r);
|
|
645
665
|
}
|
|
646
666
|
/**
|
|
647
667
|
* Clean up all resources held by this run
|
|
648
668
|
*/
|
|
649
669
|
dispose() {
|
|
650
|
-
|
|
670
|
+
e(this, M) || (e(this, A).clear(), _(this, R, L()), _(this, P, 0), _(this, M, !0));
|
|
651
671
|
}
|
|
652
672
|
}
|
|
653
|
-
|
|
673
|
+
a = new WeakMap(), R = new WeakMap(), A = new WeakMap(), x = new WeakMap(), M = new WeakMap(), P = new WeakMap(), D = new WeakSet(), /**
|
|
654
674
|
* Checks if auto-dispose should be triggered (when in terminal state with no listeners)
|
|
655
675
|
*/
|
|
656
676
|
B = function() {
|
|
657
|
-
|
|
677
|
+
e(this, M) || this.status !== c.Completed && this.status !== c.Failed || e(this, P) === 0 && this.dispose();
|
|
658
678
|
}, /**
|
|
659
679
|
* Determines if a status should be updated based on precedence
|
|
660
680
|
*
|
|
@@ -662,164 +682,95 @@ B = function() {
|
|
|
662
682
|
* @param newStatus - New status
|
|
663
683
|
* @returns true if the status should be updated, false otherwise
|
|
664
684
|
*/
|
|
665
|
-
|
|
666
|
-
if (t ===
|
|
685
|
+
tt = function(t, r) {
|
|
686
|
+
if (t === c.Completed || t === c.Failed)
|
|
667
687
|
return !1;
|
|
668
|
-
const n =
|
|
669
|
-
return
|
|
688
|
+
const n = e(this, x)[t];
|
|
689
|
+
return e(this, x)[r] > n;
|
|
670
690
|
};
|
|
671
|
-
function
|
|
672
|
-
switch (
|
|
673
|
-
case
|
|
674
|
-
return {
|
|
675
|
-
event_type: "run:started",
|
|
676
|
-
run_id: e.run_id,
|
|
677
|
-
flow_slug: e.flow_slug,
|
|
678
|
-
status: u.Started,
|
|
679
|
-
started_at: e.started_at,
|
|
680
|
-
remaining_steps: e.remaining_steps,
|
|
681
|
-
input: e.input
|
|
682
|
-
};
|
|
683
|
-
case u.Completed:
|
|
684
|
-
return {
|
|
685
|
-
event_type: "run:completed",
|
|
686
|
-
run_id: e.run_id,
|
|
687
|
-
flow_slug: e.flow_slug,
|
|
688
|
-
status: u.Completed,
|
|
689
|
-
completed_at: e.completed_at,
|
|
690
|
-
output: e.output
|
|
691
|
-
};
|
|
692
|
-
case u.Failed:
|
|
693
|
-
return {
|
|
694
|
-
event_type: "run:failed",
|
|
695
|
-
run_id: e.run_id,
|
|
696
|
-
flow_slug: e.flow_slug,
|
|
697
|
-
status: u.Failed,
|
|
698
|
-
failed_at: e.failed_at,
|
|
699
|
-
error_message: e.error_message
|
|
700
|
-
};
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
function at(e) {
|
|
704
|
-
switch (e.status) {
|
|
705
|
-
case l.Started:
|
|
706
|
-
return {
|
|
707
|
-
event_type: "step:started",
|
|
708
|
-
run_id: e.run_id,
|
|
709
|
-
step_slug: e.step_slug,
|
|
710
|
-
status: l.Started,
|
|
711
|
-
started_at: e.started_at
|
|
712
|
-
};
|
|
713
|
-
case l.Completed:
|
|
714
|
-
return {
|
|
715
|
-
event_type: "step:completed",
|
|
716
|
-
run_id: e.run_id,
|
|
717
|
-
step_slug: e.step_slug,
|
|
718
|
-
status: l.Completed,
|
|
719
|
-
completed_at: e.completed_at,
|
|
720
|
-
output: e.output
|
|
721
|
-
};
|
|
722
|
-
case l.Failed:
|
|
723
|
-
return {
|
|
724
|
-
event_type: "step:failed",
|
|
725
|
-
run_id: e.run_id,
|
|
726
|
-
step_slug: e.step_slug,
|
|
727
|
-
status: l.Failed,
|
|
728
|
-
failed_at: e.failed_at,
|
|
729
|
-
error_message: e.error_message
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
function ut(e) {
|
|
734
|
-
switch (e.status) {
|
|
735
|
-
case "started":
|
|
691
|
+
function nt(s) {
|
|
692
|
+
switch (s.status) {
|
|
693
|
+
case c.Started:
|
|
736
694
|
return {
|
|
737
695
|
event_type: "run:started",
|
|
738
|
-
run_id:
|
|
739
|
-
flow_slug:
|
|
740
|
-
status:
|
|
741
|
-
started_at:
|
|
742
|
-
remaining_steps:
|
|
743
|
-
input:
|
|
696
|
+
run_id: s.run_id,
|
|
697
|
+
flow_slug: s.flow_slug,
|
|
698
|
+
status: c.Started,
|
|
699
|
+
started_at: s.started_at,
|
|
700
|
+
remaining_steps: s.remaining_steps,
|
|
701
|
+
input: s.input
|
|
744
702
|
};
|
|
745
|
-
case
|
|
703
|
+
case c.Completed:
|
|
746
704
|
return {
|
|
747
705
|
event_type: "run:completed",
|
|
748
|
-
run_id:
|
|
749
|
-
flow_slug:
|
|
750
|
-
status:
|
|
751
|
-
completed_at:
|
|
752
|
-
output:
|
|
706
|
+
run_id: s.run_id,
|
|
707
|
+
flow_slug: s.flow_slug,
|
|
708
|
+
status: c.Completed,
|
|
709
|
+
completed_at: s.completed_at,
|
|
710
|
+
output: s.output
|
|
753
711
|
};
|
|
754
|
-
case
|
|
712
|
+
case c.Failed:
|
|
755
713
|
return {
|
|
756
714
|
event_type: "run:failed",
|
|
757
|
-
run_id:
|
|
758
|
-
flow_slug:
|
|
759
|
-
status:
|
|
760
|
-
failed_at:
|
|
761
|
-
error_message:
|
|
762
|
-
// Database doesn't have error_message for runs
|
|
715
|
+
run_id: s.run_id,
|
|
716
|
+
flow_slug: s.flow_slug,
|
|
717
|
+
status: c.Failed,
|
|
718
|
+
failed_at: s.failed_at,
|
|
719
|
+
error_message: s.error_message
|
|
763
720
|
};
|
|
764
|
-
default:
|
|
765
|
-
throw new Error(`Unknown run status: ${e.status}`);
|
|
766
721
|
}
|
|
767
722
|
}
|
|
768
|
-
function
|
|
769
|
-
switch (
|
|
770
|
-
case
|
|
771
|
-
case "started":
|
|
723
|
+
function it(s) {
|
|
724
|
+
switch (s.status) {
|
|
725
|
+
case h.Started:
|
|
772
726
|
return {
|
|
773
727
|
event_type: "step:started",
|
|
774
|
-
run_id:
|
|
775
|
-
step_slug:
|
|
776
|
-
status:
|
|
777
|
-
started_at:
|
|
728
|
+
run_id: s.run_id,
|
|
729
|
+
step_slug: s.step_slug,
|
|
730
|
+
status: h.Started,
|
|
731
|
+
started_at: s.started_at
|
|
778
732
|
};
|
|
779
|
-
case
|
|
733
|
+
case h.Completed:
|
|
780
734
|
return {
|
|
781
735
|
event_type: "step:completed",
|
|
782
|
-
run_id:
|
|
783
|
-
step_slug:
|
|
784
|
-
status:
|
|
785
|
-
completed_at:
|
|
786
|
-
output:
|
|
787
|
-
// Database doesn't have output in step_states
|
|
736
|
+
run_id: s.run_id,
|
|
737
|
+
step_slug: s.step_slug,
|
|
738
|
+
status: h.Completed,
|
|
739
|
+
completed_at: s.completed_at,
|
|
740
|
+
output: s.output
|
|
788
741
|
};
|
|
789
|
-
case
|
|
742
|
+
case h.Failed:
|
|
790
743
|
return {
|
|
791
744
|
event_type: "step:failed",
|
|
792
|
-
run_id:
|
|
793
|
-
step_slug:
|
|
794
|
-
status:
|
|
795
|
-
failed_at:
|
|
796
|
-
error_message:
|
|
745
|
+
run_id: s.run_id,
|
|
746
|
+
step_slug: s.step_slug,
|
|
747
|
+
status: h.Failed,
|
|
748
|
+
failed_at: s.failed_at,
|
|
749
|
+
error_message: s.error_message
|
|
797
750
|
};
|
|
798
|
-
default:
|
|
799
|
-
throw new Error(`Unknown step status: ${e.status}`);
|
|
800
751
|
}
|
|
801
752
|
}
|
|
802
|
-
var q,
|
|
803
|
-
class
|
|
753
|
+
var q, g, y;
|
|
754
|
+
class mt {
|
|
804
755
|
/**
|
|
805
756
|
* Creates a new PgflowClient instance
|
|
806
757
|
*
|
|
807
758
|
* @param supabaseClient - Supabase client instance
|
|
808
759
|
*/
|
|
809
760
|
constructor(t) {
|
|
810
|
-
|
|
811
|
-
|
|
761
|
+
d(this, q);
|
|
762
|
+
d(this, g);
|
|
812
763
|
// Use the widest event type - keeps the compiler happy but
|
|
813
764
|
// still provides the structural API we need (updateState/step/...)
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
const n =
|
|
817
|
-
n && n.updateState(
|
|
818
|
-
}),
|
|
819
|
-
const n =
|
|
765
|
+
d(this, y, /* @__PURE__ */ new Map());
|
|
766
|
+
_(this, q, t), _(this, g, new st(t)), e(this, g).onRunEvent((r) => {
|
|
767
|
+
const n = e(this, y).get(r.run_id);
|
|
768
|
+
n && n.updateState(nt(r));
|
|
769
|
+
}), e(this, g).onStepEvent((r) => {
|
|
770
|
+
const n = e(this, y).get(r.run_id);
|
|
820
771
|
if (n) {
|
|
821
|
-
const i =
|
|
822
|
-
n.step(i).updateState(
|
|
772
|
+
const i = r.step_slug;
|
|
773
|
+
n.step(i).updateState(it(r));
|
|
823
774
|
}
|
|
824
775
|
});
|
|
825
776
|
}
|
|
@@ -831,12 +782,12 @@ class wt {
|
|
|
831
782
|
* @param run_id - Optional run ID (will be generated if not provided)
|
|
832
783
|
* @returns Promise that resolves with the FlowRun instance
|
|
833
784
|
*/
|
|
834
|
-
async startFlow(t,
|
|
835
|
-
const i = n ||
|
|
785
|
+
async startFlow(t, r, n) {
|
|
786
|
+
const i = n || et(), w = {
|
|
836
787
|
run_id: i,
|
|
837
788
|
flow_slug: t,
|
|
838
|
-
status:
|
|
839
|
-
input:
|
|
789
|
+
status: c.Started,
|
|
790
|
+
input: r,
|
|
840
791
|
output: null,
|
|
841
792
|
error: null,
|
|
842
793
|
error_message: null,
|
|
@@ -845,19 +796,19 @@ class wt {
|
|
|
845
796
|
failed_at: null,
|
|
846
797
|
remaining_steps: -1
|
|
847
798
|
// Use -1 to indicate unknown until first snapshot arrives
|
|
848
|
-
},
|
|
849
|
-
|
|
850
|
-
const { data:
|
|
799
|
+
}, p = new Q(w);
|
|
800
|
+
e(this, y).set(i, p), await e(this, g).subscribeToRun(i);
|
|
801
|
+
const { data: u, error: l } = await e(this, q).schema("pgflow").rpc("start_flow_with_states", {
|
|
851
802
|
flow_slug: t,
|
|
852
|
-
input:
|
|
803
|
+
input: r,
|
|
853
804
|
run_id: i
|
|
854
805
|
});
|
|
855
|
-
if (
|
|
856
|
-
throw this.dispose(i),
|
|
857
|
-
if (
|
|
858
|
-
for (const
|
|
859
|
-
|
|
860
|
-
return
|
|
806
|
+
if (l)
|
|
807
|
+
throw this.dispose(i), l;
|
|
808
|
+
if (u.run && p.applySnapshot(u.run), u.steps && Array.isArray(u.steps))
|
|
809
|
+
for (const b of u.steps)
|
|
810
|
+
p.step(b.step_slug).applySnapshot(b);
|
|
811
|
+
return p;
|
|
861
812
|
}
|
|
862
813
|
/**
|
|
863
814
|
* Dispose a specific flow run
|
|
@@ -865,14 +816,14 @@ class wt {
|
|
|
865
816
|
* @param runId - Run ID to dispose
|
|
866
817
|
*/
|
|
867
818
|
dispose(t) {
|
|
868
|
-
const
|
|
869
|
-
|
|
819
|
+
const r = e(this, y).get(t);
|
|
820
|
+
r && (e(this, g).unsubscribe(t), r.dispose(), e(this, y).delete(t));
|
|
870
821
|
}
|
|
871
822
|
/**
|
|
872
823
|
* Dispose all flow runs
|
|
873
824
|
*/
|
|
874
825
|
disposeAll() {
|
|
875
|
-
for (const t of
|
|
826
|
+
for (const t of e(this, y).keys())
|
|
876
827
|
this.dispose(t);
|
|
877
828
|
}
|
|
878
829
|
// Delegate IFlowRealtime methods to the adapter
|
|
@@ -880,33 +831,33 @@ class wt {
|
|
|
880
831
|
* Fetch flow definition metadata
|
|
881
832
|
*/
|
|
882
833
|
async fetchFlowDefinition(t) {
|
|
883
|
-
return
|
|
834
|
+
return e(this, g).fetchFlowDefinition(t);
|
|
884
835
|
}
|
|
885
836
|
/**
|
|
886
837
|
* Register a callback for run events
|
|
887
838
|
* @returns Function to unsubscribe from the event
|
|
888
839
|
*/
|
|
889
840
|
onRunEvent(t) {
|
|
890
|
-
return
|
|
841
|
+
return e(this, g).onRunEvent(t);
|
|
891
842
|
}
|
|
892
843
|
/**
|
|
893
844
|
* Register a callback for step events
|
|
894
845
|
* @returns Function to unsubscribe from the event
|
|
895
846
|
*/
|
|
896
847
|
onStepEvent(t) {
|
|
897
|
-
return
|
|
848
|
+
return e(this, g).onStepEvent(t);
|
|
898
849
|
}
|
|
899
850
|
/**
|
|
900
851
|
* Subscribe to a flow run's events
|
|
901
852
|
*/
|
|
902
853
|
async subscribeToRun(t) {
|
|
903
|
-
return await
|
|
854
|
+
return await e(this, g).subscribeToRun(t);
|
|
904
855
|
}
|
|
905
856
|
/**
|
|
906
857
|
* Fetch current state of a run and its steps
|
|
907
858
|
*/
|
|
908
859
|
async getRunWithStates(t) {
|
|
909
|
-
return
|
|
860
|
+
return e(this, g).getRunWithStates(t);
|
|
910
861
|
}
|
|
911
862
|
/**
|
|
912
863
|
* Get a flow run by ID
|
|
@@ -915,38 +866,37 @@ class wt {
|
|
|
915
866
|
* @returns Promise that resolves with the FlowRun instance or null if not found
|
|
916
867
|
*/
|
|
917
868
|
async getRun(t) {
|
|
918
|
-
const
|
|
919
|
-
if (
|
|
920
|
-
return
|
|
869
|
+
const r = e(this, y).get(t);
|
|
870
|
+
if (r)
|
|
871
|
+
return r;
|
|
921
872
|
try {
|
|
922
873
|
const { run: n, steps: i } = await this.getRunWithStates(t);
|
|
923
874
|
if (!n)
|
|
924
875
|
return null;
|
|
925
|
-
|
|
926
|
-
if (!a.run_id || !a.flow_slug || !a.status)
|
|
876
|
+
if (!n.run_id || !n.flow_slug || !n.status)
|
|
927
877
|
throw new Error("Invalid run data: missing required fields");
|
|
928
|
-
if (!Object.values(
|
|
929
|
-
throw new Error(`Invalid run data: invalid status '${
|
|
930
|
-
const
|
|
931
|
-
run_id:
|
|
932
|
-
flow_slug:
|
|
933
|
-
status:
|
|
934
|
-
input:
|
|
935
|
-
output:
|
|
936
|
-
error:
|
|
937
|
-
error_message:
|
|
938
|
-
started_at:
|
|
939
|
-
completed_at:
|
|
940
|
-
failed_at:
|
|
941
|
-
remaining_steps:
|
|
942
|
-
},
|
|
943
|
-
if (
|
|
944
|
-
for (const
|
|
945
|
-
if (!
|
|
878
|
+
if (!Object.values(c).includes(n.status))
|
|
879
|
+
throw new Error(`Invalid run data: invalid status '${n.status}'`);
|
|
880
|
+
const p = {
|
|
881
|
+
run_id: n.run_id,
|
|
882
|
+
flow_slug: n.flow_slug,
|
|
883
|
+
status: n.status,
|
|
884
|
+
input: n.input,
|
|
885
|
+
output: null,
|
|
886
|
+
error: null,
|
|
887
|
+
error_message: null,
|
|
888
|
+
started_at: null,
|
|
889
|
+
completed_at: null,
|
|
890
|
+
failed_at: null,
|
|
891
|
+
remaining_steps: 0
|
|
892
|
+
}, u = new Q(p);
|
|
893
|
+
if (u.applySnapshot(n), e(this, y).set(t, u), await e(this, g).subscribeToRun(t), i && Array.isArray(i))
|
|
894
|
+
for (const l of i) {
|
|
895
|
+
if (!l.step_slug || !l.status)
|
|
946
896
|
throw new Error("Invalid step data: missing required fields");
|
|
947
|
-
|
|
897
|
+
u.step(l.step_slug).applySnapshot(l);
|
|
948
898
|
}
|
|
949
|
-
return
|
|
899
|
+
return u;
|
|
950
900
|
} catch (n) {
|
|
951
901
|
if (console.error("Error getting run:", n), n instanceof Error && (n.message.includes("Invalid run data") || n.message.includes("Invalid step data")))
|
|
952
902
|
throw n;
|
|
@@ -954,20 +904,20 @@ class wt {
|
|
|
954
904
|
}
|
|
955
905
|
}
|
|
956
906
|
}
|
|
957
|
-
q = new WeakMap(),
|
|
907
|
+
q = new WeakMap(), g = new WeakMap(), y = new WeakMap();
|
|
958
908
|
export {
|
|
959
909
|
Q as FlowRun,
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
910
|
+
c as FlowRunStatus,
|
|
911
|
+
rt as FlowStep,
|
|
912
|
+
h as FlowStepStatus,
|
|
913
|
+
mt as PgflowClient,
|
|
914
|
+
ct as isFlowRunCompletedEvent,
|
|
915
|
+
lt as isFlowRunEvent,
|
|
916
|
+
pt as isFlowRunFailedEvent,
|
|
917
|
+
dt as isFlowRunStartedEvent,
|
|
918
|
+
ft as isStepCompletedEvent,
|
|
969
919
|
K as isStepEvent,
|
|
970
|
-
|
|
971
|
-
|
|
920
|
+
_t as isStepFailedEvent,
|
|
921
|
+
ht as isStepStartedEvent
|
|
972
922
|
};
|
|
973
923
|
//# sourceMappingURL=index.js.map
|