@pgflow/client 0.7.1 → 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/dist/index.js CHANGED
@@ -1,61 +1,61 @@
1
- var N = (e) => {
2
- throw TypeError(e);
1
+ var N = (s) => {
2
+ throw TypeError(s);
3
3
  };
4
- var O = (e, t, s) => t.has(e) || N("Cannot " + s);
5
- var r = (e, t, s) => (O(e, t, "read from private field"), s ? s.call(e) : t.get(e)), c = (e, t, s) => t.has(e) ? N("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(e) : t.set(e, s), g = (e, t, s, n) => (O(e, t, "write to private field"), n ? n.call(e, s) : t.set(e, s), s), w = (e, t, s) => (O(e, t, "access private method"), s);
6
- var H = (e, t, s, n) => ({
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
- g(e, t, i, s);
8
+ _(s, t, i, r);
9
9
  },
10
10
  get _() {
11
- return r(e, t, n);
11
+ return e(s, t, n);
12
12
  }
13
13
  });
14
- import { v4 as st } from "uuid";
14
+ import { v4 as et } from "uuid";
15
15
  import { createNanoEvents as L } from "nanoevents";
16
- var u = /* @__PURE__ */ ((e) => (e.Started = "started", e.Completed = "completed", e.Failed = "failed", e))(u || {});
17
- function ct(e) {
18
- return !!e && typeof e == "object" && "run_id" in e && "flow_slug" in e && !("step_slug" in e) && "status" in e && (e.status === "started" || e.status === "completed" || e.status === "failed");
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 pt(e) {
21
- return e.status === "started";
20
+ function dt(s) {
21
+ return s.status === "started";
22
22
  }
23
- function ht(e) {
24
- return e.status === "completed";
23
+ function ct(s) {
24
+ return s.status === "completed";
25
25
  }
26
- function _t(e) {
27
- return e.status === "failed";
26
+ function pt(s) {
27
+ return s.status === "failed";
28
28
  }
29
- var l = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Completed = "completed", e.Failed = "failed", e))(l || {});
30
- function K(e) {
31
- return !!e && typeof e == "object" && "run_id" in e && "step_slug" in e && "status" in e && (e.status === "started" || e.status === "completed" || e.status === "failed");
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 ft(e) {
34
- return K(e) && e.status === "started" && "event_type" in e && e.event_type === "step:started";
33
+ function ht(s) {
34
+ return K(s) && s.status === "started" && "event_type" in s && s.event_type === "step:started";
35
35
  }
36
- function mt(e) {
37
- return K(e) && e.status === "completed" && "event_type" in e && e.event_type === "step:completed";
36
+ function ft(s) {
37
+ return K(s) && s.status === "completed" && "event_type" in s && s.event_type === "step:completed";
38
38
  }
39
- function gt(e) {
40
- return K(e) && e.status === "failed" && "event_type" in e && e.event_type === "step:failed";
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, M, I, f, j, X, z, Y, Z, v, k, G;
43
- class rt {
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, s = {}) {
50
- c(this, f);
51
- c(this, S);
52
- c(this, C, /* @__PURE__ */ new Map());
53
- c(this, F, L());
54
- c(this, M);
55
- c(this, I);
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
- c(this, k, /* @__PURE__ */ new Map());
58
- g(this, S, t), g(this, M, s.reconnectDelayMs ?? 2e3), g(this, I, s.schedule ?? setTimeout);
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 [s, n] = await Promise.all([
67
- r(this, S).schema("pgflow").from("flows").select("*").eq("flow_slug", t).single(),
68
- r(this, S).schema("pgflow").from("steps").select("*").eq("flow_slug", t).order("step_index", { ascending: !0 })
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 (s.error) throw s.error;
71
- if (!s.data) throw new Error(`Flow "${t}" not found`);
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: s.data,
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 s = r(this, F).on("runEvent", t);
86
+ const r = e(this, F).on("runEvent", t);
87
87
  return () => {
88
88
  try {
89
- s();
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 s = r(this, F).on("stepEvent", t);
102
+ const r = e(this, F).on("stepEvent", t);
103
103
  return () => {
104
104
  try {
105
- s();
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 s = `pgflow:run:${t}`;
119
- if (r(this, C).has(t)) {
120
- const _ = r(this, k).get(t);
121
- if (_)
122
- return _;
123
- w(this, f, G).call(this, t);
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 = r(this, S).channel(s);
126
- n.on("broadcast", { event: "*" }, w(this, f, j).bind(this)), n.on("system", { event: "closed" }, () => {
127
- console.log(`Channel ${s} closed`);
128
- }), n.on("system", { event: "error" }, (_) => {
129
- console.log(`Channel ${s} error:`, _), w(this, f, z).call(this, t, s, n, _.error);
130
- }), console.log(`Subscribing to channel ${s}...`), await new Promise((_, h) => {
131
- const d = setTimeout(() => {
132
- h(new Error(`Subscription timeout for channel ${s}`));
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((m) => {
135
- console.log(`Channel ${s} subscription status:`, m), m === "SUBSCRIBED" && (clearTimeout(d), _());
134
+ n.subscribe((b) => {
135
+ console.log(`Channel ${r} subscription status:`, b), b === "SUBSCRIBED" && (clearTimeout(l), p());
136
136
  });
137
- }), r(this, C).set(t, n);
138
- const a = () => this.unsubscribe(t);
139
- return r(this, k).set(t, a), a;
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
- w(this, f, G).call(this, t);
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: s, error: n } = await r(this, S).schema("pgflow").rpc("get_run_with_states", { run_id: t });
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 (!s) throw new Error(`No data returned for run ${t}`);
158
- return s;
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(), M = new WeakMap(), I = new WeakMap(), f = new WeakSet(), /**
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: s, payload: n } = t, i = n;
167
- w(this, f, X).call(this, i), s.startsWith("run:") ? r(this, F).emit("runEvent", i) : s.startsWith("step:") && r(this, F).emit("stepEvent", i);
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
- X = function(t) {
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, s, n, i) {
184
- console.error(`Channel ${s} error:`, i), r(this, I).call(this, async () => {
185
- r(this, C).has(t) && await w(this, f, Z).call(this, t, s);
186
- }, r(this, M));
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
- Y = function(t, s) {
194
- const n = r(this, S).channel(s);
195
- return n.on("broadcast", { event: "*" }, w(this, f, j).bind(this)), n;
196
- }, Z = async function(t, s) {
197
- console.log(`Attempting to reconnect to ${s}`);
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
- w(this, f, v).call(this, t, n);
201
- const i = w(this, f, Y).call(this, t, s);
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 ${s}`);
203
+ console.log(`Reconnected and subscribed to channel ${r}`);
204
204
  }), i.on("system", { event: "closed" }, () => {
205
- console.log(`Reconnected channel ${s} closed`);
205
+ console.log(`Reconnected channel ${r} closed`);
206
206
  }), i.on(
207
207
  "system",
208
208
  { event: "error" },
209
- (a) => w(this, f, z).call(this, t, s, i, a.error)
210
- ), i.subscribe(), r(this, C).set(t, i);
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 ${s}:`, n);
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
- v = function(t, s) {
220
- if (!s || !s.run) return;
219
+ Z = function(t, r) {
220
+ if (!r || !r.run) return;
221
221
  const n = {
222
- event_type: `run:${s.run.status}`,
223
- ...s.run
222
+ event_type: `run:${r.run.status}`,
223
+ ...r.run
224
224
  };
225
- if (r(this, F).emit("runEvent", n), s.steps && Array.isArray(s.steps))
226
- for (const i of s.steps) {
227
- const a = {
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
- r(this, F).emit("stepEvent", a);
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 s = r(this, C).get(t);
240
- s && (s.unsubscribe(), r(this, C).delete(t), r(this, k).delete(t));
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 p, $, W, J, tt;
243
- class nt {
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
- c(this, J);
251
- c(this, p);
252
- c(this, $, L());
253
- c(this, W, {
254
- [l.Created]: 0,
255
- [l.Started]: 1,
256
- [l.Completed]: 2,
257
- [l.Failed]: 3
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
- g(this, p, t);
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 r(this, p).run_id;
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 r(this, p).step_slug;
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 r(this, p).status;
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 r(this, p).started_at;
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 r(this, p).completed_at;
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 r(this, p).failed_at;
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 r(this, p).output;
301
+ return e(this, o).output;
302
302
  }
303
303
  /**
304
304
  * Get the error object
305
305
  */
306
306
  get error() {
307
- return r(this, p).error;
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 r(this, p).error_message;
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, s) {
323
- return r(this, $).on(t, s);
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, s) {
333
- const n = (s == null ? void 0 : s.timeoutMs) ?? 3e5, { signal: i } = s || {};
334
- return this.status === t ? Promise.resolve(this) : new Promise((a, _) => {
335
- let h, d = !1;
336
- n > 0 && (h = setTimeout(() => {
337
- d || (d = !0, A(), _(new Error(`Timeout waiting for step ${this.step_slug} to reach status '${t}'`)));
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 m;
339
+ let b;
340
340
  if (i) {
341
341
  const E = () => {
342
- d || (d = !0, h && clearTimeout(h), A(), _(new Error(`Aborted waiting for step ${this.step_slug} to reach status '${t}'`)));
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), m = () => {
344
+ i.addEventListener("abort", E), b = () => {
345
345
  i.removeEventListener("abort", E);
346
346
  };
347
347
  }
348
- const A = this.on("*", (E) => {
348
+ const T = this.on("*", (E) => {
349
349
  if (E.status === t) {
350
- if (d) return;
351
- d = !0, h && clearTimeout(h), m && m(), A(), a(this);
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 !== r(this, p).step_slug || t.run_id !== r(this, p).run_id || !w(this, J, tt).call(this, r(this, p).status, t.status))
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 l.Started:
371
- g(this, p, {
372
- ...r(this, p),
373
- status: l.Started,
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
- }), r(this, $).emit("started", t);
385
+ }), e(this, $).emit("started", t);
376
386
  break;
377
- case l.Completed:
378
- g(this, p, {
379
- ...r(this, p),
380
- status: l.Completed,
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
- }), r(this, $).emit("completed", t);
393
+ }), e(this, $).emit("completed", t);
384
394
  break;
385
- case l.Failed:
386
- g(this, p, {
387
- ...r(this, p),
388
- status: l.Failed,
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
- }), r(this, $).emit("failed", t);
402
+ }), e(this, $).emit("failed", t);
393
403
  break;
394
404
  default:
395
405
  return !1;
396
406
  }
397
- return r(this, $).emit("*", t), !0;
407
+ return e(this, $).emit("*", t), !0;
398
408
  }
399
409
  }
400
- p = new WeakMap(), $ = new WeakMap(), W = new WeakMap(), J = new WeakSet(), /**
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
- tt = function(t, s) {
408
- if (t === l.Completed || t === l.Failed)
417
+ v = function(t, r) {
418
+ if (t === h.Completed || t === h.Failed)
409
419
  return !1;
410
- const n = r(this, W)[t];
411
- return r(this, W)[s] > n;
420
+ const n = e(this, W)[t];
421
+ return e(this, W)[r] > n;
412
422
  };
413
- var o, R, D, x, U, P, T, B, et;
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
- c(this, T);
422
- c(this, o);
423
- c(this, R, L());
424
- c(this, D, /* @__PURE__ */ new Map());
425
- c(this, x, {
426
- [u.Started]: 0,
427
- [u.Completed]: 1,
428
- [u.Failed]: 2
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
- c(this, U, !1);
440
+ d(this, M, !1);
431
441
  // Track number of listeners
432
- c(this, P, 0);
433
- g(this, o, t);
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 r(this, o).run_id;
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 r(this, o).flow_slug;
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 r(this, o).status;
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 r(this, o).started_at;
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 r(this, o).completed_at;
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 r(this, o).failed_at;
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 r(this, o).input;
485
+ return e(this, a).input;
476
486
  }
477
487
  /**
478
488
  * Get the flow output
479
489
  */
480
490
  get output() {
481
- return r(this, o).output;
491
+ return e(this, a).output;
482
492
  }
483
493
  /**
484
494
  * Get the error object
485
495
  */
486
496
  get error() {
487
- return r(this, o).error;
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 r(this, o).error_message;
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 r(this, o).remaining_steps;
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, s) {
518
+ on(t, r) {
509
519
  H(this, P)._++;
510
- const n = r(this, R).on(t, s);
520
+ const n = e(this, R).on(t, r);
511
521
  return () => {
512
- n(), H(this, P)._--, w(this, T, B).call(this);
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 s = r(this, D).get(t);
523
- if (s)
524
- return s;
525
- const n = new nt({
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: l.Created,
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 r(this, D).set(
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 r(this, D).has(t);
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, s) {
558
- const n = (s == null ? void 0 : s.timeoutMs) ?? 3e5, { signal: i } = s || {};
559
- return this.status === t ? Promise.resolve(this) : new Promise((a, _) => {
560
- let h, d = !1;
561
- n > 0 && (h = setTimeout(() => {
562
- d || (d = !0, A(), _(
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 m;
578
+ let b;
569
579
  if (i) {
570
580
  const E = () => {
571
- d || (d = !0, h && clearTimeout(h), A(), _(
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), m = () => {
587
+ i.addEventListener("abort", E), b = () => {
578
588
  i.removeEventListener("abort", E);
579
589
  };
580
590
  }
581
- const A = this.on("*", (E) => {
591
+ const T = this.on("*", (E) => {
582
592
  if (E.status === t) {
583
- if (d) return;
584
- d = !0, h && clearTimeout(h), m && m(), A(), a(this);
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 !== r(this, o).run_id || !w(this, T, et).call(this, r(this, o).status, t.status))
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 u.Started:
604
- g(this, o, {
605
- ...r(this, o),
606
- status: u.Started,
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) : r(this, o).remaining_steps
609
- }), r(this, R).emit("started", t);
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 u.Completed:
612
- g(this, o, {
613
- ...r(this, o),
614
- status: u.Completed,
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
- }), r(this, R).emit("completed", t), w(this, T, B).call(this);
638
+ }), e(this, R).emit("completed", t), m(this, D, B).call(this);
619
639
  break;
620
- case u.Failed:
621
- g(this, o, {
622
- ...r(this, o),
623
- status: u.Failed,
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
- }), r(this, R).emit("failed", t), w(this, T, B).call(this);
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 r(this, R).emit("*", t), !0;
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, s) {
644
- return this.step(t).updateState(s);
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
- r(this, U) || (r(this, D).clear(), g(this, R, L()), g(this, P, 0), g(this, U, !0));
670
+ e(this, M) || (e(this, A).clear(), _(this, R, L()), _(this, P, 0), _(this, M, !0));
651
671
  }
652
672
  }
653
- o = new WeakMap(), R = new WeakMap(), D = new WeakMap(), x = new WeakMap(), U = new WeakMap(), P = new WeakMap(), T = new WeakSet(), /**
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
- r(this, U) || this.status !== u.Completed && this.status !== u.Failed || r(this, P) === 0 && this.dispose();
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
- et = function(t, s) {
666
- if (t === u.Completed || t === u.Failed)
685
+ tt = function(t, r) {
686
+ if (t === c.Completed || t === c.Failed)
667
687
  return !1;
668
- const n = r(this, x)[t];
669
- return r(this, x)[s] > n;
688
+ const n = e(this, x)[t];
689
+ return e(this, x)[r] > n;
670
690
  };
671
- function it(e) {
672
- switch (e.status) {
673
- case u.Started:
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: e.run_id,
739
- flow_slug: e.flow_slug,
740
- status: u.Started,
741
- started_at: e.started_at,
742
- remaining_steps: e.remaining_steps,
743
- input: e.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 "completed":
703
+ case c.Completed:
746
704
  return {
747
705
  event_type: "run:completed",
748
- run_id: e.run_id,
749
- flow_slug: e.flow_slug,
750
- status: u.Completed,
751
- completed_at: e.completed_at,
752
- output: e.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 "failed":
712
+ case c.Failed:
755
713
  return {
756
714
  event_type: "run:failed",
757
- run_id: e.run_id,
758
- flow_slug: e.flow_slug,
759
- status: u.Failed,
760
- failed_at: e.failed_at,
761
- error_message: "Flow failed"
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 V(e) {
769
- switch (e.status) {
770
- case "created":
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: e.run_id,
775
- step_slug: e.step_slug,
776
- status: l.Started,
777
- started_at: e.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 "completed":
733
+ case h.Completed:
780
734
  return {
781
735
  event_type: "step:completed",
782
- run_id: e.run_id,
783
- step_slug: e.step_slug,
784
- status: l.Completed,
785
- completed_at: e.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 "failed":
742
+ case h.Failed:
790
743
  return {
791
744
  event_type: "step:failed",
792
- run_id: e.run_id,
793
- step_slug: e.step_slug,
794
- status: l.Failed,
795
- failed_at: e.failed_at,
796
- error_message: e.error_message || "Step failed"
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, b, y;
803
- class wt {
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
- c(this, q);
811
- c(this, b);
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
- c(this, y, /* @__PURE__ */ new Map());
815
- g(this, q, t), g(this, b, new rt(t)), r(this, b).onRunEvent((s) => {
816
- const n = r(this, y).get(s.run_id);
817
- n && n.updateState(it(s));
818
- }), r(this, b).onStepEvent((s) => {
819
- const n = r(this, y).get(s.run_id);
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 = s.step_slug;
822
- n.step(i).updateState(at(s));
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, s, n) {
835
- const i = n || st(), a = {
785
+ async startFlow(t, r, n) {
786
+ const i = n || et(), w = {
836
787
  run_id: i,
837
788
  flow_slug: t,
838
- status: u.Started,
839
- input: s,
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
- }, _ = new Q(a);
849
- r(this, y).set(i, _), await r(this, b).subscribeToRun(i);
850
- const { data: h, error: d } = await r(this, q).schema("pgflow").rpc("start_flow_with_states", {
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: s,
803
+ input: r,
853
804
  run_id: i
854
805
  });
855
- if (d)
856
- throw this.dispose(i), d;
857
- if (h.run && _.updateState(ut(h.run)), h.steps && Array.isArray(h.steps))
858
- for (const m of h.steps)
859
- _.step(m.step_slug).updateState(V(m));
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 s = r(this, y).get(t);
869
- s && (r(this, b).unsubscribe(t), s.dispose(), r(this, y).delete(t));
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 r(this, y).keys())
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 r(this, b).fetchFlowDefinition(t);
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 r(this, b).onRunEvent(t);
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 r(this, b).onStepEvent(t);
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 r(this, b).subscribeToRun(t);
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 r(this, b).getRunWithStates(t);
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 s = r(this, y).get(t);
919
- if (s)
920
- return s;
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
- const a = n;
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(u).includes(a.status))
929
- throw new Error(`Invalid run data: invalid status '${a.status}'`);
930
- const h = {
931
- run_id: a.run_id,
932
- flow_slug: a.flow_slug,
933
- status: a.status,
934
- input: a.input,
935
- output: a.output,
936
- error: a.error_message ? new Error(a.error_message) : null,
937
- error_message: a.error_message || null,
938
- started_at: a.started_at ? new Date(a.started_at) : null,
939
- completed_at: a.completed_at ? new Date(a.completed_at) : null,
940
- failed_at: a.failed_at ? new Date(a.failed_at) : null,
941
- remaining_steps: a.remaining_steps || 0
942
- }, d = new Q(h);
943
- if (r(this, y).set(t, d), await r(this, b).subscribeToRun(t), i && Array.isArray(i))
944
- for (const m of i) {
945
- if (!m.step_slug || !m.status)
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
- d.step(m.step_slug).updateState(V(m));
897
+ u.step(l.step_slug).applySnapshot(l);
948
898
  }
949
- return d;
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(), b = new WeakMap(), y = new WeakMap();
907
+ q = new WeakMap(), g = new WeakMap(), y = new WeakMap();
958
908
  export {
959
909
  Q as FlowRun,
960
- u as FlowRunStatus,
961
- nt as FlowStep,
962
- l as FlowStepStatus,
963
- wt as PgflowClient,
964
- ht as isFlowRunCompletedEvent,
965
- ct as isFlowRunEvent,
966
- _t as isFlowRunFailedEvent,
967
- pt as isFlowRunStartedEvent,
968
- mt as isStepCompletedEvent,
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
- gt as isStepFailedEvent,
971
- ft as isStepStartedEvent
920
+ _t as isStepFailedEvent,
921
+ ht as isStepStartedEvent
972
922
  };
973
923
  //# sourceMappingURL=index.js.map