@pgflow/client 0.0.0-array-map-steps-cd94242a-20251008042921 → 0.0.0-control-plane-a947cb71-20251121164755

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