@pgflow/client 0.7.2 → 0.7.3
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 +8 -0
- package/dist/client/src/browser.d.ts.map +1 -0
- package/dist/client/src/index.d.ts.map +1 -0
- package/dist/client/src/lib/FlowRun.d.ts.map +1 -0
- package/dist/client/src/lib/FlowStep.d.ts.map +1 -0
- package/dist/{src → client/src}/lib/PgflowClient.d.ts +5 -1
- package/dist/client/src/lib/PgflowClient.d.ts.map +1 -0
- package/dist/{src → client/src}/lib/SupabaseBroadcastAdapter.d.ts +1 -0
- package/dist/client/src/lib/SupabaseBroadcastAdapter.d.ts.map +1 -0
- package/dist/client/src/lib/eventAdapters.d.ts.map +1 -0
- package/dist/client/src/lib/types.d.ts.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +221 -216
- package/dist/index.js.map +1 -1
- package/dist/package.json +2 -1
- package/dist/pgflow-client.browser.js +1 -1
- package/dist/pgflow-client.browser.js.map +1 -1
- package/package.json +5 -4
- package/dist/src/browser.d.ts.map +0 -1
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/lib/FlowRun.d.ts.map +0 -1
- package/dist/src/lib/FlowStep.d.ts.map +0 -1
- package/dist/src/lib/PgflowClient.d.ts.map +0 -1
- package/dist/src/lib/SupabaseBroadcastAdapter.d.ts.map +0 -1
- package/dist/src/lib/eventAdapters.d.ts.map +0 -1
- package/dist/src/lib/types.d.ts.map +0 -1
- /package/dist/{src → client/src}/browser.d.ts +0 -0
- /package/dist/{src → client/src}/index.d.ts +0 -0
- /package/dist/{src → client/src}/lib/FlowRun.d.ts +0 -0
- /package/dist/{src → client/src}/lib/FlowStep.d.ts +0 -0
- /package/dist/{src → client/src}/lib/eventAdapters.d.ts +0 -0
- /package/dist/{src → client/src}/lib/types.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -1,61 +1,62 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Q = (s) => {
|
|
2
2
|
throw TypeError(s);
|
|
3
3
|
};
|
|
4
|
-
var O = (s, t, r) => t.has(s) ||
|
|
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) ?
|
|
6
|
-
var H = (s, t, r,
|
|
7
|
-
set _(
|
|
8
|
-
|
|
4
|
+
var O = (s, t, r) => t.has(s) || Q("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) ? Q("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(s) : t.set(s, r), f = (s, t, r, i) => (O(s, t, "write to private field"), i ? i.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, i) => ({
|
|
7
|
+
set _(n) {
|
|
8
|
+
f(s, t, n, r);
|
|
9
9
|
},
|
|
10
10
|
get _() {
|
|
11
|
-
return e(s, t,
|
|
11
|
+
return e(s, t, i);
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
|
-
import { v4 as
|
|
14
|
+
import { v4 as st } from "uuid";
|
|
15
15
|
import { createNanoEvents as L } from "nanoevents";
|
|
16
|
-
var
|
|
17
|
-
function
|
|
16
|
+
var p = /* @__PURE__ */ ((s) => (s.Started = "started", s.Completed = "completed", s.Failed = "failed", s))(p || {});
|
|
17
|
+
function dt(s) {
|
|
18
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
|
|
20
|
+
function ct(s) {
|
|
21
21
|
return s.status === "started";
|
|
22
22
|
}
|
|
23
|
-
function
|
|
23
|
+
function pt(s) {
|
|
24
24
|
return s.status === "completed";
|
|
25
25
|
}
|
|
26
|
-
function
|
|
26
|
+
function ht(s) {
|
|
27
27
|
return s.status === "failed";
|
|
28
28
|
}
|
|
29
29
|
var h = /* @__PURE__ */ ((s) => (s.Created = "created", s.Started = "started", s.Completed = "completed", s.Failed = "failed", s))(h || {});
|
|
30
|
-
function
|
|
30
|
+
function N(s) {
|
|
31
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 ht(s) {
|
|
34
|
-
return K(s) && s.status === "started" && "event_type" in s && s.event_type === "step:started";
|
|
35
|
-
}
|
|
36
33
|
function ft(s) {
|
|
37
|
-
return
|
|
34
|
+
return N(s) && s.status === "started" && "event_type" in s && s.event_type === "step:started";
|
|
38
35
|
}
|
|
39
36
|
function _t(s) {
|
|
40
|
-
return
|
|
37
|
+
return N(s) && s.status === "completed" && "event_type" in s && s.event_type === "step:completed";
|
|
41
38
|
}
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
function mt(s) {
|
|
40
|
+
return N(s) && s.status === "failed" && "event_type" in s && s.event_type === "step:failed";
|
|
41
|
+
}
|
|
42
|
+
var S, C, F, I, W, M, _, j, X, G, Y, Z, v, k, K;
|
|
43
|
+
class rt {
|
|
44
44
|
/**
|
|
45
45
|
* Creates a new instance of SupabaseBroadcastAdapter
|
|
46
46
|
*
|
|
47
47
|
* @param supabase - Supabase client instance
|
|
48
48
|
*/
|
|
49
49
|
constructor(t, r = {}) {
|
|
50
|
-
d(this,
|
|
50
|
+
d(this, _);
|
|
51
51
|
d(this, S);
|
|
52
52
|
d(this, C, /* @__PURE__ */ new Map());
|
|
53
53
|
d(this, F, L());
|
|
54
|
-
d(this, U);
|
|
55
54
|
d(this, I);
|
|
55
|
+
d(this, W);
|
|
56
|
+
d(this, M);
|
|
56
57
|
// Store unsubscribe functions per run ID for reference equality
|
|
57
58
|
d(this, k, /* @__PURE__ */ new Map());
|
|
58
|
-
|
|
59
|
+
f(this, S, t), f(this, I, r.reconnectDelayMs ?? 2e3), f(this, W, r.stabilizationDelayMs ?? 300), f(this, M, r.schedule ?? setTimeout);
|
|
59
60
|
}
|
|
60
61
|
/**
|
|
61
62
|
* Fetches flow definition metadata from the database
|
|
@@ -63,17 +64,17 @@ class st {
|
|
|
63
64
|
* @param flow_slug - Flow slug to fetch
|
|
64
65
|
*/
|
|
65
66
|
async fetchFlowDefinition(t) {
|
|
66
|
-
const [r,
|
|
67
|
+
const [r, i] = await Promise.all([
|
|
67
68
|
e(this, S).schema("pgflow").from("flows").select("*").eq("flow_slug", t).single(),
|
|
68
69
|
e(this, S).schema("pgflow").from("steps").select("*").eq("flow_slug", t).order("step_index", { ascending: !0 })
|
|
69
70
|
]);
|
|
70
71
|
if (r.error) throw r.error;
|
|
71
72
|
if (!r.data) throw new Error(`Flow "${t}" not found`);
|
|
72
|
-
if (
|
|
73
|
-
const
|
|
73
|
+
if (i.error) throw i.error;
|
|
74
|
+
const n = Array.isArray(i.data) ? i.data : [];
|
|
74
75
|
return {
|
|
75
76
|
flow: r.data,
|
|
76
|
-
steps:
|
|
77
|
+
steps: n
|
|
77
78
|
};
|
|
78
79
|
}
|
|
79
80
|
/**
|
|
@@ -87,8 +88,8 @@ class st {
|
|
|
87
88
|
return () => {
|
|
88
89
|
try {
|
|
89
90
|
r();
|
|
90
|
-
} catch (
|
|
91
|
-
console.warn("Could not unsubscribe from run event - emitter may have been disposed",
|
|
91
|
+
} catch (i) {
|
|
92
|
+
console.warn("Could not unsubscribe from run event - emitter may have been disposed", i);
|
|
92
93
|
}
|
|
93
94
|
};
|
|
94
95
|
}
|
|
@@ -103,8 +104,8 @@ class st {
|
|
|
103
104
|
return () => {
|
|
104
105
|
try {
|
|
105
106
|
r();
|
|
106
|
-
} catch (
|
|
107
|
-
console.warn("Could not unsubscribe from step event - emitter may have been disposed",
|
|
107
|
+
} catch (i) {
|
|
108
|
+
console.warn("Could not unsubscribe from step event - emitter may have been disposed", i);
|
|
108
109
|
}
|
|
109
110
|
};
|
|
110
111
|
}
|
|
@@ -117,26 +118,26 @@ class st {
|
|
|
117
118
|
async subscribeToRun(t) {
|
|
118
119
|
const r = `pgflow:run:${t}`;
|
|
119
120
|
if (e(this, C).has(t)) {
|
|
120
|
-
const
|
|
121
|
-
if (
|
|
122
|
-
return
|
|
123
|
-
m(this,
|
|
121
|
+
const c = e(this, k).get(t);
|
|
122
|
+
if (c)
|
|
123
|
+
return c;
|
|
124
|
+
m(this, _, K).call(this, t);
|
|
124
125
|
}
|
|
125
|
-
const
|
|
126
|
-
|
|
126
|
+
const i = e(this, S).channel(r);
|
|
127
|
+
i.on("broadcast", { event: "*" }, m(this, _, j).bind(this)), i.on("system", { event: "closed" }, () => {
|
|
127
128
|
console.log(`Channel ${r} closed`);
|
|
128
|
-
}),
|
|
129
|
-
console.log(`Channel ${r} error:`,
|
|
130
|
-
}), console.log(`Subscribing to channel ${r}...`), await new Promise((
|
|
129
|
+
}), i.on("system", { event: "error" }, (c) => {
|
|
130
|
+
console.log(`Channel ${r} error:`, c), m(this, _, G).call(this, t, r, i, c.error);
|
|
131
|
+
}), console.log(`Subscribing to channel ${r}...`), await new Promise((c, u) => {
|
|
131
132
|
const l = setTimeout(() => {
|
|
132
133
|
u(new Error(`Subscription timeout for channel ${r}`));
|
|
133
134
|
}, 2e4);
|
|
134
|
-
|
|
135
|
-
console.log(`Channel ${r} subscription status:`, b), b === "SUBSCRIBED" && (clearTimeout(l),
|
|
135
|
+
i.subscribe((b) => {
|
|
136
|
+
console.log(`Channel ${r} subscription status:`, b), b === "SUBSCRIBED" && (clearTimeout(l), c());
|
|
136
137
|
});
|
|
137
|
-
}), e(this, C).set(t,
|
|
138
|
-
const
|
|
139
|
-
return e(this, k).set(t,
|
|
138
|
+
}), await new Promise((c) => e(this, M).call(this, c, e(this, W))), e(this, C).set(t, i);
|
|
139
|
+
const g = () => this.unsubscribe(t);
|
|
140
|
+
return e(this, k).set(t, g), g;
|
|
140
141
|
}
|
|
141
142
|
/**
|
|
142
143
|
* Unsubscribes from a run's events
|
|
@@ -144,7 +145,7 @@ class st {
|
|
|
144
145
|
* @param run_id - Run ID to unsubscribe from
|
|
145
146
|
*/
|
|
146
147
|
unsubscribe(t) {
|
|
147
|
-
m(this,
|
|
148
|
+
m(this, _, K).call(this, t);
|
|
148
149
|
}
|
|
149
150
|
/**
|
|
150
151
|
* Fetches current state of a run and its steps
|
|
@@ -152,24 +153,24 @@ class st {
|
|
|
152
153
|
* @param run_id - Run ID to fetch
|
|
153
154
|
*/
|
|
154
155
|
async getRunWithStates(t) {
|
|
155
|
-
const { data: r, error:
|
|
156
|
-
if (
|
|
156
|
+
const { data: r, error: i } = await e(this, S).schema("pgflow").rpc("get_run_with_states", { run_id: t });
|
|
157
|
+
if (i) throw i;
|
|
157
158
|
if (!r) throw new Error(`No data returned for run ${t}`);
|
|
158
159
|
return r;
|
|
159
160
|
}
|
|
160
161
|
}
|
|
161
|
-
S = new WeakMap(), C = new WeakMap(), F = new WeakMap(),
|
|
162
|
+
S = new WeakMap(), C = new WeakMap(), F = new WeakMap(), I = new WeakMap(), W = new WeakMap(), M = new WeakMap(), _ = new WeakSet(), /**
|
|
162
163
|
* Handle broadcast messages from Supabase
|
|
163
164
|
* @param payload - The message payload
|
|
164
165
|
*/
|
|
165
166
|
j = function(t) {
|
|
166
|
-
const { event: r, payload:
|
|
167
|
-
m(this,
|
|
167
|
+
const { event: r, payload: i } = t, n = i;
|
|
168
|
+
m(this, _, X).call(this, n), r.startsWith("run:") ? e(this, F).emit("runEvent", n) : r.startsWith("step:") && e(this, F).emit("stepEvent", n);
|
|
168
169
|
}, /**
|
|
169
170
|
* Parse JSON string fields in broadcast event data
|
|
170
171
|
* @param eventData - The event data object to parse
|
|
171
172
|
*/
|
|
172
|
-
|
|
173
|
+
X = function(t) {
|
|
173
174
|
if ("output" in t && typeof t.output == "string")
|
|
174
175
|
try {
|
|
175
176
|
t.output = JSON.parse(t.output);
|
|
@@ -180,67 +181,67 @@ V = function(t) {
|
|
|
180
181
|
t.input = JSON.parse(t.input);
|
|
181
182
|
} catch {
|
|
182
183
|
}
|
|
183
|
-
},
|
|
184
|
-
console.error(`Channel ${r} error:`,
|
|
185
|
-
e(this, C).has(t) && await m(this,
|
|
186
|
-
}, e(this,
|
|
184
|
+
}, G = async function(t, r, i, n) {
|
|
185
|
+
console.error(`Channel ${r} error:`, n), e(this, M).call(this, async () => {
|
|
186
|
+
e(this, C).has(t) && await m(this, _, Z).call(this, t, r);
|
|
187
|
+
}, e(this, I));
|
|
187
188
|
}, /**
|
|
188
189
|
* Creates and configures a channel for a run
|
|
189
190
|
* @param run_id - The run ID
|
|
190
191
|
* @param channelName - The channel name
|
|
191
192
|
* @returns The configured RealtimeChannel
|
|
192
193
|
*/
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
return
|
|
196
|
-
},
|
|
194
|
+
Y = function(t, r) {
|
|
195
|
+
const i = e(this, S).channel(r);
|
|
196
|
+
return i.on("broadcast", { event: "*" }, m(this, _, j).bind(this)), i;
|
|
197
|
+
}, Z = async function(t, r) {
|
|
197
198
|
console.log(`Attempting to reconnect to ${r}`);
|
|
198
199
|
try {
|
|
199
|
-
const
|
|
200
|
-
m(this,
|
|
201
|
-
const
|
|
202
|
-
|
|
200
|
+
const i = await this.getRunWithStates(t);
|
|
201
|
+
m(this, _, v).call(this, t, i);
|
|
202
|
+
const n = m(this, _, Y).call(this, t, r);
|
|
203
|
+
n.on("system", { event: "subscribed" }, () => {
|
|
203
204
|
console.log(`Reconnected and subscribed to channel ${r}`);
|
|
204
|
-
}),
|
|
205
|
+
}), n.on("system", { event: "closed" }, () => {
|
|
205
206
|
console.log(`Reconnected channel ${r} closed`);
|
|
206
|
-
}),
|
|
207
|
+
}), n.on(
|
|
207
208
|
"system",
|
|
208
209
|
{ event: "error" },
|
|
209
|
-
(
|
|
210
|
-
),
|
|
211
|
-
} catch (
|
|
212
|
-
console.error(`Failed to reconnect to ${r}:`,
|
|
210
|
+
(g) => m(this, _, G).call(this, t, r, n, g.error)
|
|
211
|
+
), n.subscribe(), e(this, C).set(t, n);
|
|
212
|
+
} catch (i) {
|
|
213
|
+
console.error(`Failed to reconnect to ${r}:`, i);
|
|
213
214
|
}
|
|
214
215
|
}, /**
|
|
215
216
|
* Refresh client state from a state snapshot
|
|
216
217
|
* @param run_id - The run ID
|
|
217
218
|
* @param state - The state snapshot
|
|
218
219
|
*/
|
|
219
|
-
|
|
220
|
+
v = function(t, r) {
|
|
220
221
|
if (!r || !r.run) return;
|
|
221
|
-
const
|
|
222
|
+
const i = {
|
|
222
223
|
event_type: `run:${r.run.status}`,
|
|
223
224
|
...r.run
|
|
224
225
|
};
|
|
225
|
-
if (e(this, F).emit("runEvent",
|
|
226
|
-
for (const
|
|
227
|
-
const
|
|
228
|
-
event_type: `step:${
|
|
229
|
-
...
|
|
226
|
+
if (e(this, F).emit("runEvent", i), r.steps && Array.isArray(r.steps))
|
|
227
|
+
for (const n of r.steps) {
|
|
228
|
+
const g = {
|
|
229
|
+
event_type: `step:${n.status}`,
|
|
230
|
+
...n
|
|
230
231
|
};
|
|
231
|
-
e(this, F).emit("stepEvent",
|
|
232
|
+
e(this, F).emit("stepEvent", g);
|
|
232
233
|
}
|
|
233
234
|
}, k = new WeakMap(), /**
|
|
234
235
|
* Unsubscribes from a run's events
|
|
235
236
|
*
|
|
236
237
|
* @param run_id - Run ID to unsubscribe from
|
|
237
238
|
*/
|
|
238
|
-
|
|
239
|
+
K = function(t) {
|
|
239
240
|
const r = e(this, C).get(t);
|
|
240
241
|
r && (r.unsubscribe(), e(this, C).delete(t), e(this, k).delete(t));
|
|
241
242
|
};
|
|
242
|
-
var o, $,
|
|
243
|
-
class
|
|
243
|
+
var o, $, x, J, tt;
|
|
244
|
+
class it {
|
|
244
245
|
/**
|
|
245
246
|
* Creates a new FlowStep instance
|
|
246
247
|
*
|
|
@@ -250,13 +251,13 @@ class rt {
|
|
|
250
251
|
d(this, J);
|
|
251
252
|
d(this, o);
|
|
252
253
|
d(this, $, L());
|
|
253
|
-
d(this,
|
|
254
|
+
d(this, x, {
|
|
254
255
|
[h.Created]: 0,
|
|
255
256
|
[h.Started]: 1,
|
|
256
257
|
[h.Completed]: 2,
|
|
257
258
|
[h.Failed]: 3
|
|
258
259
|
});
|
|
259
|
-
|
|
260
|
+
f(this, o, t);
|
|
260
261
|
}
|
|
261
262
|
/**
|
|
262
263
|
* Get the run ID this step belongs to
|
|
@@ -330,25 +331,25 @@ class rt {
|
|
|
330
331
|
* @returns Promise that resolves with the step instance when the status is reached
|
|
331
332
|
*/
|
|
332
333
|
waitForStatus(t, r) {
|
|
333
|
-
const
|
|
334
|
-
return this.status === t ? Promise.resolve(this) : new Promise((
|
|
334
|
+
const i = (r == null ? void 0 : r.timeoutMs) ?? 3e5, { signal: n } = r || {};
|
|
335
|
+
return this.status === t ? Promise.resolve(this) : new Promise((g, c) => {
|
|
335
336
|
let u, l = !1;
|
|
336
|
-
|
|
337
|
-
l || (l = !0,
|
|
338
|
-
},
|
|
337
|
+
i > 0 && (u = setTimeout(() => {
|
|
338
|
+
l || (l = !0, P(), c(new Error(`Timeout waiting for step ${this.step_slug} to reach status '${t}'`)));
|
|
339
|
+
}, i));
|
|
339
340
|
let b;
|
|
340
|
-
if (
|
|
341
|
+
if (n) {
|
|
341
342
|
const E = () => {
|
|
342
|
-
l || (l = !0, u && clearTimeout(u),
|
|
343
|
+
l || (l = !0, u && clearTimeout(u), P(), c(new Error(`Aborted waiting for step ${this.step_slug} to reach status '${t}'`)));
|
|
343
344
|
};
|
|
344
|
-
|
|
345
|
-
|
|
345
|
+
n.addEventListener("abort", E), b = () => {
|
|
346
|
+
n.removeEventListener("abort", E);
|
|
346
347
|
};
|
|
347
348
|
}
|
|
348
|
-
const
|
|
349
|
+
const P = this.on("*", (E) => {
|
|
349
350
|
if (E.status === t) {
|
|
350
351
|
if (l) return;
|
|
351
|
-
l = !0, u && clearTimeout(u), b && b(),
|
|
352
|
+
l = !0, u && clearTimeout(u), b && b(), P(), g(this);
|
|
352
353
|
}
|
|
353
354
|
});
|
|
354
355
|
});
|
|
@@ -374,18 +375,18 @@ class rt {
|
|
|
374
375
|
* with event emission instead of direct state manipulation.
|
|
375
376
|
*/
|
|
376
377
|
updateState(t) {
|
|
377
|
-
if (t.step_slug !== e(this, o).step_slug || t.run_id !== e(this, o).run_id || !m(this, J,
|
|
378
|
+
if (t.step_slug !== e(this, o).step_slug || t.run_id !== e(this, o).run_id || !m(this, J, tt).call(this, e(this, o).status, t.status))
|
|
378
379
|
return !1;
|
|
379
380
|
switch (t.status) {
|
|
380
381
|
case h.Started:
|
|
381
|
-
|
|
382
|
+
f(this, o, {
|
|
382
383
|
...e(this, o),
|
|
383
384
|
status: h.Started,
|
|
384
385
|
started_at: typeof t.started_at == "string" ? new Date(t.started_at) : /* @__PURE__ */ new Date()
|
|
385
386
|
}), e(this, $).emit("started", t);
|
|
386
387
|
break;
|
|
387
388
|
case h.Completed:
|
|
388
|
-
|
|
389
|
+
f(this, o, {
|
|
389
390
|
...e(this, o),
|
|
390
391
|
status: h.Completed,
|
|
391
392
|
completed_at: typeof t.completed_at == "string" ? new Date(t.completed_at) : /* @__PURE__ */ new Date(),
|
|
@@ -393,7 +394,7 @@ class rt {
|
|
|
393
394
|
}), e(this, $).emit("completed", t);
|
|
394
395
|
break;
|
|
395
396
|
case h.Failed:
|
|
396
|
-
|
|
397
|
+
f(this, o, {
|
|
397
398
|
...e(this, o),
|
|
398
399
|
status: h.Failed,
|
|
399
400
|
failed_at: typeof t.failed_at == "string" ? new Date(t.failed_at) : /* @__PURE__ */ new Date(),
|
|
@@ -407,21 +408,21 @@ class rt {
|
|
|
407
408
|
return e(this, $).emit("*", t), !0;
|
|
408
409
|
}
|
|
409
410
|
}
|
|
410
|
-
o = new WeakMap(), $ = new WeakMap(),
|
|
411
|
+
o = new WeakMap(), $ = new WeakMap(), x = new WeakMap(), J = new WeakSet(), /**
|
|
411
412
|
* Determines if a status should be updated based on precedence
|
|
412
413
|
*
|
|
413
414
|
* @param currentStatus - Current status
|
|
414
415
|
* @param newStatus - New status
|
|
415
416
|
* @returns true if the status should be updated, false otherwise
|
|
416
417
|
*/
|
|
417
|
-
|
|
418
|
+
tt = function(t, r) {
|
|
418
419
|
if (t === h.Completed || t === h.Failed)
|
|
419
420
|
return !1;
|
|
420
|
-
const
|
|
421
|
-
return e(this,
|
|
421
|
+
const i = e(this, x)[t];
|
|
422
|
+
return e(this, x)[r] > i;
|
|
422
423
|
};
|
|
423
|
-
var a, R,
|
|
424
|
-
class
|
|
424
|
+
var a, R, T, q, U, A, D, B, et;
|
|
425
|
+
class V {
|
|
425
426
|
/**
|
|
426
427
|
* Creates a new FlowRun instance
|
|
427
428
|
*
|
|
@@ -431,16 +432,16 @@ class Q {
|
|
|
431
432
|
d(this, D);
|
|
432
433
|
d(this, a);
|
|
433
434
|
d(this, R, L());
|
|
434
|
-
d(this,
|
|
435
|
-
d(this,
|
|
436
|
-
[
|
|
437
|
-
[
|
|
438
|
-
[
|
|
435
|
+
d(this, T, /* @__PURE__ */ new Map());
|
|
436
|
+
d(this, q, {
|
|
437
|
+
[p.Started]: 0,
|
|
438
|
+
[p.Completed]: 1,
|
|
439
|
+
[p.Failed]: 2
|
|
439
440
|
});
|
|
440
|
-
d(this,
|
|
441
|
+
d(this, U, !1);
|
|
441
442
|
// Track number of listeners
|
|
442
|
-
d(this,
|
|
443
|
-
|
|
443
|
+
d(this, A, 0);
|
|
444
|
+
f(this, a, t);
|
|
444
445
|
}
|
|
445
446
|
/**
|
|
446
447
|
* Get the run ID
|
|
@@ -516,10 +517,10 @@ class Q {
|
|
|
516
517
|
* @returns Function to unsubscribe from the event
|
|
517
518
|
*/
|
|
518
519
|
on(t, r) {
|
|
519
|
-
H(this,
|
|
520
|
-
const
|
|
520
|
+
H(this, A)._++;
|
|
521
|
+
const i = e(this, R).on(t, r);
|
|
521
522
|
return () => {
|
|
522
|
-
|
|
523
|
+
i(), H(this, A)._--, m(this, D, B).call(this);
|
|
523
524
|
};
|
|
524
525
|
}
|
|
525
526
|
/**
|
|
@@ -529,10 +530,10 @@ class Q {
|
|
|
529
530
|
* @returns FlowStep instance for the specified step
|
|
530
531
|
*/
|
|
531
532
|
step(t) {
|
|
532
|
-
const r = e(this,
|
|
533
|
+
const r = e(this, T).get(t);
|
|
533
534
|
if (r)
|
|
534
535
|
return r;
|
|
535
|
-
const
|
|
536
|
+
const i = new it({
|
|
536
537
|
run_id: this.run_id,
|
|
537
538
|
step_slug: t,
|
|
538
539
|
status: h.Created,
|
|
@@ -543,10 +544,10 @@ class Q {
|
|
|
543
544
|
completed_at: null,
|
|
544
545
|
failed_at: null
|
|
545
546
|
});
|
|
546
|
-
return e(this,
|
|
547
|
+
return e(this, T).set(
|
|
547
548
|
t,
|
|
548
|
-
|
|
549
|
-
),
|
|
549
|
+
i
|
|
550
|
+
), i;
|
|
550
551
|
}
|
|
551
552
|
/**
|
|
552
553
|
* Check if this run has a specific step
|
|
@@ -555,7 +556,7 @@ class Q {
|
|
|
555
556
|
* @returns true if the step exists, false otherwise
|
|
556
557
|
*/
|
|
557
558
|
hasStep(t) {
|
|
558
|
-
return e(this,
|
|
559
|
+
return e(this, T).has(t);
|
|
559
560
|
}
|
|
560
561
|
/**
|
|
561
562
|
* Wait for the run to reach a specific status
|
|
@@ -565,33 +566,33 @@ class Q {
|
|
|
565
566
|
* @returns Promise that resolves with the run instance when the status is reached
|
|
566
567
|
*/
|
|
567
568
|
waitForStatus(t, r) {
|
|
568
|
-
const
|
|
569
|
-
return this.status === t ? Promise.resolve(this) : new Promise((
|
|
569
|
+
const i = (r == null ? void 0 : r.timeoutMs) ?? 3e5, { signal: n } = r || {};
|
|
570
|
+
return this.status === t ? Promise.resolve(this) : new Promise((g, c) => {
|
|
570
571
|
let u, l = !1;
|
|
571
|
-
|
|
572
|
-
l || (l = !0,
|
|
572
|
+
i > 0 && (u = setTimeout(() => {
|
|
573
|
+
l || (l = !0, P(), c(
|
|
573
574
|
new Error(
|
|
574
575
|
`Timeout waiting for run ${this.run_id} to reach status '${t}'`
|
|
575
576
|
)
|
|
576
577
|
));
|
|
577
|
-
},
|
|
578
|
+
}, i));
|
|
578
579
|
let b;
|
|
579
|
-
if (
|
|
580
|
+
if (n) {
|
|
580
581
|
const E = () => {
|
|
581
|
-
l || (l = !0, u && clearTimeout(u),
|
|
582
|
+
l || (l = !0, u && clearTimeout(u), P(), c(
|
|
582
583
|
new Error(
|
|
583
584
|
`Aborted waiting for run ${this.run_id} to reach status '${t}'`
|
|
584
585
|
)
|
|
585
586
|
));
|
|
586
587
|
};
|
|
587
|
-
|
|
588
|
-
|
|
588
|
+
n.addEventListener("abort", E), b = () => {
|
|
589
|
+
n.removeEventListener("abort", E);
|
|
589
590
|
};
|
|
590
591
|
}
|
|
591
|
-
const
|
|
592
|
+
const P = this.on("*", (E) => {
|
|
592
593
|
if (E.status === t) {
|
|
593
594
|
if (l) return;
|
|
594
|
-
l = !0, u && clearTimeout(u), b && b(),
|
|
595
|
+
l = !0, u && clearTimeout(u), b && b(), P(), g(this);
|
|
595
596
|
}
|
|
596
597
|
});
|
|
597
598
|
});
|
|
@@ -617,30 +618,30 @@ class Q {
|
|
|
617
618
|
* with event emission instead of direct state manipulation.
|
|
618
619
|
*/
|
|
619
620
|
updateState(t) {
|
|
620
|
-
if (t.run_id !== e(this, a).run_id || !m(this, D,
|
|
621
|
+
if (t.run_id !== e(this, a).run_id || !m(this, D, et).call(this, e(this, a).status, t.status))
|
|
621
622
|
return !1;
|
|
622
623
|
switch (t.status) {
|
|
623
|
-
case
|
|
624
|
-
|
|
624
|
+
case p.Started:
|
|
625
|
+
f(this, a, {
|
|
625
626
|
...e(this, a),
|
|
626
|
-
status:
|
|
627
|
+
status: p.Started,
|
|
627
628
|
started_at: typeof t.started_at == "string" ? new Date(t.started_at) : /* @__PURE__ */ new Date(),
|
|
628
629
|
remaining_steps: "remaining_steps" in t ? Number(t.remaining_steps) : e(this, a).remaining_steps
|
|
629
630
|
}), e(this, R).emit("started", t);
|
|
630
631
|
break;
|
|
631
|
-
case
|
|
632
|
-
|
|
632
|
+
case p.Completed:
|
|
633
|
+
f(this, a, {
|
|
633
634
|
...e(this, a),
|
|
634
|
-
status:
|
|
635
|
+
status: p.Completed,
|
|
635
636
|
completed_at: typeof t.completed_at == "string" ? new Date(t.completed_at) : /* @__PURE__ */ new Date(),
|
|
636
637
|
output: t.output,
|
|
637
638
|
remaining_steps: 0
|
|
638
639
|
}), e(this, R).emit("completed", t), m(this, D, B).call(this);
|
|
639
640
|
break;
|
|
640
|
-
case
|
|
641
|
-
|
|
641
|
+
case p.Failed:
|
|
642
|
+
f(this, a, {
|
|
642
643
|
...e(this, a),
|
|
643
|
-
status:
|
|
644
|
+
status: p.Failed,
|
|
644
645
|
failed_at: typeof t.failed_at == "string" ? new Date(t.failed_at) : /* @__PURE__ */ new Date(),
|
|
645
646
|
error_message: typeof t.error_message == "string" ? t.error_message : "Unknown error",
|
|
646
647
|
error: new Error(
|
|
@@ -667,14 +668,14 @@ class Q {
|
|
|
667
668
|
* Clean up all resources held by this run
|
|
668
669
|
*/
|
|
669
670
|
dispose() {
|
|
670
|
-
e(this,
|
|
671
|
+
e(this, U) || (e(this, T).clear(), f(this, R, L()), f(this, A, 0), f(this, U, !0));
|
|
671
672
|
}
|
|
672
673
|
}
|
|
673
|
-
a = new WeakMap(), R = new WeakMap(),
|
|
674
|
+
a = new WeakMap(), R = new WeakMap(), T = new WeakMap(), q = new WeakMap(), U = new WeakMap(), A = new WeakMap(), D = new WeakSet(), /**
|
|
674
675
|
* Checks if auto-dispose should be triggered (when in terminal state with no listeners)
|
|
675
676
|
*/
|
|
676
677
|
B = function() {
|
|
677
|
-
e(this,
|
|
678
|
+
e(this, U) || this.status !== p.Completed && this.status !== p.Failed || e(this, A) === 0 && this.dispose();
|
|
678
679
|
}, /**
|
|
679
680
|
* Determines if a status should be updated based on precedence
|
|
680
681
|
*
|
|
@@ -682,45 +683,45 @@ B = function() {
|
|
|
682
683
|
* @param newStatus - New status
|
|
683
684
|
* @returns true if the status should be updated, false otherwise
|
|
684
685
|
*/
|
|
685
|
-
|
|
686
|
-
if (t ===
|
|
686
|
+
et = function(t, r) {
|
|
687
|
+
if (t === p.Completed || t === p.Failed)
|
|
687
688
|
return !1;
|
|
688
|
-
const
|
|
689
|
-
return e(this,
|
|
689
|
+
const i = e(this, q)[t];
|
|
690
|
+
return e(this, q)[r] > i;
|
|
690
691
|
};
|
|
691
692
|
function nt(s) {
|
|
692
693
|
switch (s.status) {
|
|
693
|
-
case
|
|
694
|
+
case p.Started:
|
|
694
695
|
return {
|
|
695
696
|
event_type: "run:started",
|
|
696
697
|
run_id: s.run_id,
|
|
697
698
|
flow_slug: s.flow_slug,
|
|
698
|
-
status:
|
|
699
|
+
status: p.Started,
|
|
699
700
|
started_at: s.started_at,
|
|
700
701
|
remaining_steps: s.remaining_steps,
|
|
701
702
|
input: s.input
|
|
702
703
|
};
|
|
703
|
-
case
|
|
704
|
+
case p.Completed:
|
|
704
705
|
return {
|
|
705
706
|
event_type: "run:completed",
|
|
706
707
|
run_id: s.run_id,
|
|
707
708
|
flow_slug: s.flow_slug,
|
|
708
|
-
status:
|
|
709
|
+
status: p.Completed,
|
|
709
710
|
completed_at: s.completed_at,
|
|
710
711
|
output: s.output
|
|
711
712
|
};
|
|
712
|
-
case
|
|
713
|
+
case p.Failed:
|
|
713
714
|
return {
|
|
714
715
|
event_type: "run:failed",
|
|
715
716
|
run_id: s.run_id,
|
|
716
717
|
flow_slug: s.flow_slug,
|
|
717
|
-
status:
|
|
718
|
+
status: p.Failed,
|
|
718
719
|
failed_at: s.failed_at,
|
|
719
720
|
error_message: s.error_message
|
|
720
721
|
};
|
|
721
722
|
}
|
|
722
723
|
}
|
|
723
|
-
function
|
|
724
|
+
function at(s) {
|
|
724
725
|
switch (s.status) {
|
|
725
726
|
case h.Started:
|
|
726
727
|
return {
|
|
@@ -750,27 +751,31 @@ function it(s) {
|
|
|
750
751
|
};
|
|
751
752
|
}
|
|
752
753
|
}
|
|
753
|
-
var
|
|
754
|
-
class
|
|
754
|
+
var z, w, y;
|
|
755
|
+
class gt {
|
|
755
756
|
/**
|
|
756
757
|
* Creates a new PgflowClient instance
|
|
757
758
|
*
|
|
758
759
|
* @param supabaseClient - Supabase client instance
|
|
760
|
+
* @param opts - Optional configuration
|
|
759
761
|
*/
|
|
760
|
-
constructor(t) {
|
|
761
|
-
d(this,
|
|
762
|
-
d(this,
|
|
762
|
+
constructor(t, r = {}) {
|
|
763
|
+
d(this, z);
|
|
764
|
+
d(this, w);
|
|
763
765
|
// Use the widest event type - keeps the compiler happy but
|
|
764
766
|
// still provides the structural API we need (updateState/step/...)
|
|
765
767
|
d(this, y, /* @__PURE__ */ new Map());
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
}), e(this,
|
|
770
|
-
const n = e(this, y).get(
|
|
768
|
+
f(this, z, t), f(this, w, new rt(t, {
|
|
769
|
+
stabilizationDelayMs: r.realtimeStabilizationDelayMs,
|
|
770
|
+
schedule: r.schedule
|
|
771
|
+
})), e(this, w).onRunEvent((i) => {
|
|
772
|
+
const n = e(this, y).get(i.run_id);
|
|
773
|
+
n && n.updateState(nt(i));
|
|
774
|
+
}), e(this, w).onStepEvent((i) => {
|
|
775
|
+
const n = e(this, y).get(i.run_id);
|
|
771
776
|
if (n) {
|
|
772
|
-
const
|
|
773
|
-
n.step(
|
|
777
|
+
const g = i.step_slug;
|
|
778
|
+
n.step(g).updateState(at(i));
|
|
774
779
|
}
|
|
775
780
|
});
|
|
776
781
|
}
|
|
@@ -782,11 +787,11 @@ class mt {
|
|
|
782
787
|
* @param run_id - Optional run ID (will be generated if not provided)
|
|
783
788
|
* @returns Promise that resolves with the FlowRun instance
|
|
784
789
|
*/
|
|
785
|
-
async startFlow(t, r,
|
|
786
|
-
const
|
|
787
|
-
run_id:
|
|
790
|
+
async startFlow(t, r, i) {
|
|
791
|
+
const n = i || st(), g = {
|
|
792
|
+
run_id: n,
|
|
788
793
|
flow_slug: t,
|
|
789
|
-
status:
|
|
794
|
+
status: p.Started,
|
|
790
795
|
input: r,
|
|
791
796
|
output: null,
|
|
792
797
|
error: null,
|
|
@@ -796,19 +801,19 @@ class mt {
|
|
|
796
801
|
failed_at: null,
|
|
797
802
|
remaining_steps: -1
|
|
798
803
|
// Use -1 to indicate unknown until first snapshot arrives
|
|
799
|
-
},
|
|
800
|
-
e(this, y).set(
|
|
801
|
-
const { data: u, error: l } = await e(this,
|
|
804
|
+
}, c = new V(g);
|
|
805
|
+
e(this, y).set(n, c), await e(this, w).subscribeToRun(n);
|
|
806
|
+
const { data: u, error: l } = await e(this, z).schema("pgflow").rpc("start_flow_with_states", {
|
|
802
807
|
flow_slug: t,
|
|
803
808
|
input: r,
|
|
804
|
-
run_id:
|
|
809
|
+
run_id: n
|
|
805
810
|
});
|
|
806
811
|
if (l)
|
|
807
|
-
throw this.dispose(
|
|
808
|
-
if (u.run &&
|
|
812
|
+
throw this.dispose(n), l;
|
|
813
|
+
if (u.run && c.applySnapshot(u.run), u.steps && Array.isArray(u.steps))
|
|
809
814
|
for (const b of u.steps)
|
|
810
|
-
|
|
811
|
-
return
|
|
815
|
+
c.step(b.step_slug).applySnapshot(b);
|
|
816
|
+
return c;
|
|
812
817
|
}
|
|
813
818
|
/**
|
|
814
819
|
* Dispose a specific flow run
|
|
@@ -817,7 +822,7 @@ class mt {
|
|
|
817
822
|
*/
|
|
818
823
|
dispose(t) {
|
|
819
824
|
const r = e(this, y).get(t);
|
|
820
|
-
r && (e(this,
|
|
825
|
+
r && (e(this, w).unsubscribe(t), r.dispose(), e(this, y).delete(t));
|
|
821
826
|
}
|
|
822
827
|
/**
|
|
823
828
|
* Dispose all flow runs
|
|
@@ -831,33 +836,33 @@ class mt {
|
|
|
831
836
|
* Fetch flow definition metadata
|
|
832
837
|
*/
|
|
833
838
|
async fetchFlowDefinition(t) {
|
|
834
|
-
return e(this,
|
|
839
|
+
return e(this, w).fetchFlowDefinition(t);
|
|
835
840
|
}
|
|
836
841
|
/**
|
|
837
842
|
* Register a callback for run events
|
|
838
843
|
* @returns Function to unsubscribe from the event
|
|
839
844
|
*/
|
|
840
845
|
onRunEvent(t) {
|
|
841
|
-
return e(this,
|
|
846
|
+
return e(this, w).onRunEvent(t);
|
|
842
847
|
}
|
|
843
848
|
/**
|
|
844
849
|
* Register a callback for step events
|
|
845
850
|
* @returns Function to unsubscribe from the event
|
|
846
851
|
*/
|
|
847
852
|
onStepEvent(t) {
|
|
848
|
-
return e(this,
|
|
853
|
+
return e(this, w).onStepEvent(t);
|
|
849
854
|
}
|
|
850
855
|
/**
|
|
851
856
|
* Subscribe to a flow run's events
|
|
852
857
|
*/
|
|
853
858
|
async subscribeToRun(t) {
|
|
854
|
-
return await e(this,
|
|
859
|
+
return await e(this, w).subscribeToRun(t);
|
|
855
860
|
}
|
|
856
861
|
/**
|
|
857
862
|
* Fetch current state of a run and its steps
|
|
858
863
|
*/
|
|
859
864
|
async getRunWithStates(t) {
|
|
860
|
-
return e(this,
|
|
865
|
+
return e(this, w).getRunWithStates(t);
|
|
861
866
|
}
|
|
862
867
|
/**
|
|
863
868
|
* Get a flow run by ID
|
|
@@ -870,18 +875,18 @@ class mt {
|
|
|
870
875
|
if (r)
|
|
871
876
|
return r;
|
|
872
877
|
try {
|
|
873
|
-
const { run:
|
|
874
|
-
if (!
|
|
878
|
+
const { run: i, steps: n } = await this.getRunWithStates(t);
|
|
879
|
+
if (!i)
|
|
875
880
|
return null;
|
|
876
|
-
if (!
|
|
881
|
+
if (!i.run_id || !i.flow_slug || !i.status)
|
|
877
882
|
throw new Error("Invalid run data: missing required fields");
|
|
878
|
-
if (!Object.values(
|
|
879
|
-
throw new Error(`Invalid run data: invalid status '${
|
|
880
|
-
const
|
|
881
|
-
run_id:
|
|
882
|
-
flow_slug:
|
|
883
|
-
status:
|
|
884
|
-
input:
|
|
883
|
+
if (!Object.values(p).includes(i.status))
|
|
884
|
+
throw new Error(`Invalid run data: invalid status '${i.status}'`);
|
|
885
|
+
const c = {
|
|
886
|
+
run_id: i.run_id,
|
|
887
|
+
flow_slug: i.flow_slug,
|
|
888
|
+
status: i.status,
|
|
889
|
+
input: i.input,
|
|
885
890
|
output: null,
|
|
886
891
|
error: null,
|
|
887
892
|
error_message: null,
|
|
@@ -889,35 +894,35 @@ class mt {
|
|
|
889
894
|
completed_at: null,
|
|
890
895
|
failed_at: null,
|
|
891
896
|
remaining_steps: 0
|
|
892
|
-
}, u = new
|
|
893
|
-
if (u.applySnapshot(
|
|
894
|
-
for (const l of
|
|
897
|
+
}, u = new V(c);
|
|
898
|
+
if (u.applySnapshot(i), e(this, y).set(t, u), await e(this, w).subscribeToRun(t), n && Array.isArray(n))
|
|
899
|
+
for (const l of n) {
|
|
895
900
|
if (!l.step_slug || !l.status)
|
|
896
901
|
throw new Error("Invalid step data: missing required fields");
|
|
897
902
|
u.step(l.step_slug).applySnapshot(l);
|
|
898
903
|
}
|
|
899
904
|
return u;
|
|
900
|
-
} catch (
|
|
901
|
-
if (console.error("Error getting run:",
|
|
902
|
-
throw
|
|
905
|
+
} catch (i) {
|
|
906
|
+
if (console.error("Error getting run:", i), i instanceof Error && (i.message.includes("Invalid run data") || i.message.includes("Invalid step data")))
|
|
907
|
+
throw i;
|
|
903
908
|
return null;
|
|
904
909
|
}
|
|
905
910
|
}
|
|
906
911
|
}
|
|
907
|
-
|
|
912
|
+
z = new WeakMap(), w = new WeakMap(), y = new WeakMap();
|
|
908
913
|
export {
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
914
|
+
V as FlowRun,
|
|
915
|
+
p as FlowRunStatus,
|
|
916
|
+
it as FlowStep,
|
|
912
917
|
h as FlowStepStatus,
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
918
|
+
gt as PgflowClient,
|
|
919
|
+
pt as isFlowRunCompletedEvent,
|
|
920
|
+
dt as isFlowRunEvent,
|
|
921
|
+
ht as isFlowRunFailedEvent,
|
|
922
|
+
ct as isFlowRunStartedEvent,
|
|
923
|
+
_t as isStepCompletedEvent,
|
|
924
|
+
N as isStepEvent,
|
|
925
|
+
mt as isStepFailedEvent,
|
|
926
|
+
ft as isStepStartedEvent
|
|
922
927
|
};
|
|
923
928
|
//# sourceMappingURL=index.js.map
|